blob: 0734cb35887d7a5d62aff6f576f362284ea7b26c [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
Takashi Iwai95a962c2014-10-29 16:03:58 +010080/**
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
83 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010084int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
85{
86 mutex_lock(&preset_mutex);
87 list_add_tail(&preset->list, &hda_preset_tables);
88 mutex_unlock(&preset_mutex);
89 return 0;
90}
Takashi Iwai2698ea92013-12-18 07:45:52 +010091EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010092
Takashi Iwai95a962c2014-10-29 16:03:58 +010093/**
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
96 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010097int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
98{
99 mutex_lock(&preset_mutex);
100 list_del(&preset->list);
101 mutex_unlock(&preset_mutex);
102 return 0;
103}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100104EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai83012a72012-08-24 18:38:08 +0200106#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +0100107#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200108static void hda_power_work(struct work_struct *work);
109static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100111
112static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700113{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100114 struct hda_bus *bus = codec->bus;
115
116 if ((power_up && codec->pm_up_notified) ||
117 (!power_up && !codec->pm_up_notified))
118 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700119 if (bus->ops.pm_notify)
120 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100121 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700122}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100123
Takashi Iwaicb53c622007-08-10 17:21:45 +0200124#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100125#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200126static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200127#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100128#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200129#endif
130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_location(u32 cfg)
139{
140 static char *bases[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
142 };
143 static unsigned char specials_idx[] = {
144 0x07, 0x08,
145 0x17, 0x18, 0x19,
146 0x37, 0x38
147 };
148 static char *specials[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
152 };
153 int i;
154 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
155 if ((cfg & 0x0f) < 7)
156 return bases[cfg & 0x0f];
157 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
158 if (cfg == specials_idx[i])
159 return specials[i];
160 }
161 return "UNKNOWN";
162}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100163EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400164
Takashi Iwaid5191e52009-11-16 14:58:17 +0100165/**
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
168 *
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
171 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172const char *snd_hda_get_jack_connectivity(u32 cfg)
173{
174 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
175
176 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
177}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100178EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400179
Takashi Iwaid5191e52009-11-16 14:58:17 +0100180/**
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
183 *
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
186 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400187const char *snd_hda_get_jack_type(u32 cfg)
188{
189 static char *jack_types[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200193 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400194 };
195
196 return jack_types[(cfg & AC_DEFCFG_DEVICE)
197 >> AC_DEFCFG_DEVICE_SHIFT];
198}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100199EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400200
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201/*
202 * Compose a 32bit command word to be sent to the HD-audio controller
203 */
204static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200205make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100206 unsigned int verb, unsigned int parm)
207{
208 u32 val;
209
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200210 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200211 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100212 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200213 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800214 return ~0;
215 }
216
217 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100218 val |= (u32)nid << 20;
219 val |= verb << 8;
220 val |= parm;
221 return val;
222}
223
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224/*
225 * Send and receive a verb
226 */
227static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200228 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229{
230 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200232
Wu Fengguang6430aee2009-07-17 16:49:19 +0800233 if (cmd == ~0)
234 return -1;
235
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200236 if (res)
237 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200239 snd_hda_power_up(codec);
240 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200241 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
242 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100243 for (;;) {
244 trace_hda_send_cmd(codec, cmd);
245 err = bus->ops.command(bus, cmd);
246 if (err != -EAGAIN)
247 break;
248 /* process pending verbs */
249 bus->ops.get_response(bus, codec->addr);
250 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200251 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800252 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200253 trace_hda_get_response(codec, *res);
254 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200255 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 mutex_unlock(&bus->cmd_mutex);
257 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100258 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200259 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100260 codec_dbg(codec,
261 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200262 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200263 bus->ops.bus_reset(bus);
264 }
265 goto again;
266 }
267 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100268 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200269 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 return err;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200277 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * @verb: the verb to send
279 * @parm: the parameter for the verb
280 *
281 * Send a single command and read the corresponding response.
282 *
283 * Returns the obtained response value, or -1 for an error.
284 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200285unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200286 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned int verb, unsigned int parm)
288{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200289 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200290 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200291 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700292 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return res;
294}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100295EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200301 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * @verb: the verb to send
303 * @parm: the parameter for the verb
304 *
305 * Send a single command without waiting for response.
306 *
307 * Returns 0 if successful, or a negative error code.
308 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200309int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
310 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200312 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100313 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200314 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200315 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100317EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/**
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
323 *
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
326 */
327void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
328{
329 for (; seq->nid; seq++)
330 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
331}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100332EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/**
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
337 * @nid: NID to parse
338 * @start_id: the pointer to store the start NID
339 *
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
342 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200343int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned int parm;
347
348 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300349 if (parm == -1) {
350 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200351 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *start_id = (parm >> 16) & 0x7fff;
354 return (int)(parm & 0x7fff);
355}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100356EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358/* connection list element */
359struct hda_conn_list {
360 struct list_head list;
361 int len;
362 hda_nid_t nid;
363 hda_nid_t conns[0];
364};
365
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200366/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100367static struct hda_conn_list *
368lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200369{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100370 struct hda_conn_list *p;
371 list_for_each_entry(p, &codec->conn_list, list) {
372 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200373 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200374 }
375 return NULL;
376}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100378static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
379 const hda_nid_t *list)
380{
381 struct hda_conn_list *p;
382
383 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
384 if (!p)
385 return -ENOMEM;
386 p->len = len;
387 p->nid = nid;
388 memcpy(p->conns, list, len * sizeof(hda_nid_t));
389 list_add(&p->list, &codec->conn_list);
390 return 0;
391}
392
393static void remove_conn_list(struct hda_codec *codec)
394{
395 while (!list_empty(&codec->conn_list)) {
396 struct hda_conn_list *p;
397 p = list_first_entry(&codec->conn_list, typeof(*p), list);
398 list_del(&p->list);
399 kfree(p);
400 }
401}
402
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200403/* read the connection and add to the cache */
404static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100406 hda_nid_t list[32];
407 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200409
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200410 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100411 if (len == -ENOSPC) {
412 len = snd_hda_get_num_raw_conns(codec, nid);
413 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
414 if (!result)
415 return -ENOMEM;
416 len = snd_hda_get_raw_connections(codec, nid, result, len);
417 }
418 if (len >= 0)
419 len = snd_hda_override_conn_list(codec, nid, len, result);
420 if (result != list)
421 kfree(result);
422 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200423}
Takashi Iwaidce20792011-06-24 14:10:28 +0200424
425/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100426 * snd_hda_get_conn_list - get connection list
427 * @codec: the HDA codec
428 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100429 * @listp: the pointer to store NID list
430 *
431 * Parses the connection list of the given widget and stores the pointer
432 * to the list of NIDs.
433 *
434 * Returns the number of connections, or a negative error code.
435 *
436 * Note that the returned pointer isn't protected against the list
437 * modification. If snd_hda_override_conn_list() might be called
438 * concurrently, protect with a mutex appropriately.
439 */
440int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
441 const hda_nid_t **listp)
442{
443 bool added = false;
444
445 for (;;) {
446 int err;
447 const struct hda_conn_list *p;
448
449 /* if the connection-list is already cached, read it */
450 p = lookup_conn_list(codec, nid);
451 if (p) {
452 if (listp)
453 *listp = p->conns;
454 return p->len;
455 }
456 if (snd_BUG_ON(added))
457 return -EINVAL;
458
459 err = read_and_add_raw_conns(codec, nid);
460 if (err < 0)
461 return err;
462 added = true;
463 }
464}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100465EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100466
467/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200468 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * @codec: the HDA codec
470 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200471 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * @max_conns: max. number of connections to store
473 *
474 * Parses the connection list of the given widget and stores the list
475 * of NIDs.
476 *
477 * Returns the number of connections, or a negative error code.
478 */
479int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200480 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200481{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100482 const hda_nid_t *list;
483 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200484
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100485 if (len > 0 && conn_list) {
486 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100487 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200488 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200489 return -EINVAL;
490 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100491 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200492 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200493
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100494 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200495}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100496EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200497
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498/* return CONNLIST_LEN parameter of the given widget */
499static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
500{
501 unsigned int wcaps = get_wcaps(codec, nid);
502 unsigned int parm;
503
504 if (!(wcaps & AC_WCAP_CONN_LIST) &&
505 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
506 return 0;
507
508 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
509 if (parm == -1)
510 parm = 0;
511 return parm;
512}
513
514int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
515{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100516 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100517}
518
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200519/**
520 * snd_hda_get_raw_connections - copy connection list without cache
521 * @codec: the HDA codec
522 * @nid: NID to parse
523 * @conn_list: connection list array
524 * @max_conns: max. number of connections to store
525 *
526 * Like snd_hda_get_connections(), copy the connection list but without
527 * checking through the connection-list cache.
528 * Currently called only from hda_proc.c, so not exported.
529 */
530int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
531 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100536 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100537 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Takashi Iwai4eea3092013-02-07 18:18:19 +0100539 parm = get_num_conns(codec, nid);
540 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200541 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (parm & AC_CLIST_LONG) {
544 /* long form */
545 shift = 16;
546 num_elems = 2;
547 } else {
548 /* short form */
549 shift = 8;
550 num_elems = 4;
551 }
552 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 mask = (1 << (shift-1)) - 1;
554
Takashi Iwai0ba21762007-04-16 11:29:14 +0200555 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0; /* no connection */
557
558 if (conn_len == 1) {
559 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100564 if (conn_list)
565 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 1;
567 }
568
569 /* multi connection */
570 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 prev_nid = 0;
572 for (i = 0; i < conn_len; i++) {
573 int range_val;
574 hda_nid_t val, n;
575
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200576 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 parm = snd_hda_codec_read(codec, nid, 0,
578 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200579 if (parm == -1 && codec->bus->rirb_error)
580 return -EIO;
581 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100583 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100584 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100585 codec_dbg(codec,
586 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200587 nid, i, parm);
588 return 0;
589 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 parm >>= shift;
591 if (range_val) {
592 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200593 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200595 "invalid dep_range_val %x:%x\n",
596 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100599 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100600 if (conn_list) {
601 if (conns >= max_conns)
602 return -ENOSPC;
603 conn_list[conns] = n;
604 }
605 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100606 }
607 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100608 if (conn_list) {
609 if (conns >= max_conns)
610 return -ENOSPC;
611 conn_list[conns] = val;
612 }
613 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100615 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 return conns;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200621 * snd_hda_override_conn_list - add/modify the connection-list to cache
622 * @codec: the HDA codec
623 * @nid: NID to parse
624 * @len: number of connection list entries
625 * @list: the list of connection entries
626 *
627 * Add or modify the given connection-list to the cache. If the corresponding
628 * cache already exists, invalidate it and append a new one.
629 *
630 * Returns zero or a negative error code.
631 */
632int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
633 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100635 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200636
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100637 p = lookup_conn_list(codec, nid);
638 if (p) {
639 list_del(&p->list);
640 kfree(p);
641 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200642
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100643 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100645EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200646
647/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 * snd_hda_get_conn_index - get the connection index of the given NID
649 * @codec: the HDA codec
650 * @mux: NID containing the list
651 * @nid: NID to select
652 * @recursive: 1 when searching NID recursively, otherwise 0
653 *
654 * Parses the connection list of the widget @mux and checks whether the
655 * widget @nid is present. If it is, return the connection index.
656 * Otherwise it returns -1.
657 */
658int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
659 hda_nid_t nid, int recursive)
660{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100661 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 int i, nums;
663
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100664 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200665 for (i = 0; i < nums; i++)
666 if (conn[i] == nid)
667 return i;
668 if (!recursive)
669 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100670 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100671 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200672 return -1;
673 }
674 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200675 for (i = 0; i < nums; i++) {
676 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
677 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
678 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200679 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
680 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200681 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200682 return -1;
683}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100684EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Mengdong Linf1aa0682013-08-26 21:35:21 -0400686
687/* return DEVLIST_LEN parameter of the given widget */
688static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
689{
690 unsigned int wcaps = get_wcaps(codec, nid);
691 unsigned int parm;
692
693 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
694 get_wcaps_type(wcaps) != AC_WID_PIN)
695 return 0;
696
697 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
698 if (parm == -1 && codec->bus->rirb_error)
699 parm = 0;
700 return parm & AC_DEV_LIST_LEN_MASK;
701}
702
703/**
704 * snd_hda_get_devices - copy device list without cache
705 * @codec: the HDA codec
706 * @nid: NID of the pin to parse
707 * @dev_list: device list array
708 * @max_devices: max. number of devices to store
709 *
710 * Copy the device list. This info is dynamic and so not cached.
711 * Currently called only from hda_proc.c, so not exported.
712 */
713int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
714 u8 *dev_list, int max_devices)
715{
716 unsigned int parm;
717 int i, dev_len, devices;
718
719 parm = get_num_devices(codec, nid);
720 if (!parm) /* not multi-stream capable */
721 return 0;
722
723 dev_len = parm + 1;
724 dev_len = dev_len < max_devices ? dev_len : max_devices;
725
726 devices = 0;
727 while (devices < dev_len) {
728 parm = snd_hda_codec_read(codec, nid, 0,
729 AC_VERB_GET_DEVICE_LIST, devices);
730 if (parm == -1 && codec->bus->rirb_error)
731 break;
732
733 for (i = 0; i < 8; i++) {
734 dev_list[devices] = (u8)parm;
735 parm >>= 4;
736 devices++;
737 if (devices >= dev_len)
738 break;
739 }
740 }
741 return devices;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/**
745 * snd_hda_queue_unsol_event - add an unsolicited event to queue
746 * @bus: the BUS
747 * @res: unsolicited event (lower 32bit of RIRB entry)
748 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
749 *
750 * Adds the given event to the queue. The events are processed in
751 * the workqueue asynchronously. Call this function in the interrupt
752 * hanlder when RIRB receives an unsolicited event.
753 *
754 * Returns 0 if successful, or a negative error code.
755 */
756int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
757{
758 struct hda_bus_unsolicited *unsol;
759 unsigned int wp;
760
Wang YanQing2195b062013-05-07 11:27:33 +0800761 if (!bus || !bus->workq)
762 return 0;
763
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200764 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100765 unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
767 unsol->wp = wp;
768
769 wp <<= 1;
770 unsol->queue[wp] = res;
771 unsol->queue[wp + 1] = res_ex;
772
Takashi Iwai6acaed32009-01-12 10:09:24 +0100773 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return 0;
776}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100777EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800780 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
David Howellsc4028952006-11-22 14:57:56 +0000782static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Takashi Iwai922c88a2015-02-17 14:46:40 +0100784 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
785 struct hda_bus_unsolicited *unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct hda_codec *codec;
787 unsigned int rp, caddr, res;
788
789 while (unsol->rp != unsol->wp) {
790 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
791 unsol->rp = rp;
792 rp <<= 1;
793 res = unsol->queue[rp];
794 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200795 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 continue;
797 codec = bus->caddr_tbl[caddr & 0x0f];
798 if (codec && codec->patch_ops.unsol_event)
799 codec->patch_ops.unsol_event(codec, res);
800 }
801}
802
803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * destructor
805 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100806static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200808 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100809 return;
810
811 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100812 if (bus->workq)
813 flush_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (bus->ops.private_free)
815 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100816 if (bus->workq)
817 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100822static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai2565c892014-02-19 11:41:09 +0100824 snd_hda_bus_free(device->device_data);
825 return 0;
826}
827
828static int snd_hda_bus_dev_disconnect(struct snd_device *device)
829{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100831 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/**
836 * snd_hda_bus_new - create a HDA bus
837 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * @busp: the pointer to store the created bus instance
839 *
840 * Returns 0 if successful, or a negative error code.
841 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100842int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100843 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct hda_bus *bus;
846 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100847 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100848 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .dev_free = snd_hda_bus_dev_free,
850 };
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (busp)
853 *busp = NULL;
854
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200855 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100857 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENOMEM;
859 }
860
861 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100862 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200863 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100865 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100867 snprintf(bus->workq_name, sizeof(bus->workq_name),
868 "hd-audio%d", card->number);
869 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100870 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100871 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100872 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100873 kfree(bus);
874 return -ENOMEM;
875 }
876
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
878 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 snd_hda_bus_free(bus);
880 return err;
881 }
882 if (busp)
883 *busp = bus;
884 return 0;
885}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100886EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100888#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200889#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200890 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200891#else
892#define is_generic_config(codec) 0
893#endif
894
Takashi Iwai645f10c2008-11-28 15:07:37 +0100895#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100896#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
897#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100898#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100899#endif
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * find a matching codec preset
903 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200904static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200905find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100907 struct hda_codec_preset_list *tbl;
908 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200909 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911 again:
912 mutex_lock(&preset_mutex);
913 list_for_each_entry(tbl, &hda_preset_tables, list) {
914 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100915 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100916 continue;
917 }
918 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100920 if (preset->afg && preset->afg != codec->afg)
921 continue;
922 if (preset->mfg && preset->mfg != codec->mfg)
923 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200924 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200926 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200927 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100928 preset->rev == codec->revision_id)) {
929 mutex_unlock(&preset_mutex);
930 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100934 module_put(tbl->owner);
935 }
936 mutex_unlock(&preset_mutex);
937
938 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100939 if (!mod_requested)
Takashi Iwai66797f32014-10-27 15:15:44 +0100940 request_module("snd-hda-codec-id:%08x",
941 codec->vendor_id);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100942 else
Takashi Iwai66797f32014-10-27 15:15:44 +0100943 request_module("snd-hda-codec-id:%04x*",
944 (codec->vendor_id >> 16) & 0xffff);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100945 mod_requested++;
946 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 return NULL;
949}
950
951/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200952 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200954static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 const struct hda_vendor_id *c;
957 const char *vendor = NULL;
958 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200959 char tmp[16];
960
961 if (codec->vendor_name)
962 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 for (c = hda_vendor_ids; c->id; c++) {
965 if (c->id == vendor_id) {
966 vendor = c->name;
967 break;
968 }
969 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200970 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sprintf(tmp, "Generic %04x", vendor_id);
972 vendor = tmp;
973 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200974 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
975 if (!codec->vendor_name)
976 return -ENOMEM;
977
978 get_chip_name:
979 if (codec->chip_name)
980 return 0;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200983 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
984 else {
985 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
986 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
987 }
988 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200989 return -ENOMEM;
990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100996static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200998 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 hda_nid_t nid;
1000
1001 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1002 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001003 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001004 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001005 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001006 case AC_GRP_AUDIO_FUNCTION:
1007 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001008 codec->afg_function_id = function_id & 0xff;
1009 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001010 break;
1011 case AC_GRP_MODEM_FUNCTION:
1012 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001013 codec->mfg_function_id = function_id & 0xff;
1014 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001015 break;
1016 default:
1017 break;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001023 * read widget caps for each widget and store in cache
1024 */
1025static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1026{
1027 int i;
1028 hda_nid_t nid;
1029
1030 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1031 &codec->start_nid);
1032 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001033 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001034 return -ENOMEM;
1035 nid = codec->start_nid;
1036 for (i = 0; i < codec->num_nodes; i++, nid++)
1037 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1038 AC_PAR_AUDIO_WIDGET_CAP);
1039 return 0;
1040}
1041
Takashi Iwai3be14142009-02-20 14:11:16 +01001042/* read all pin default configurations and save codec->init_pins */
1043static int read_pin_defaults(struct hda_codec *codec)
1044{
1045 int i;
1046 hda_nid_t nid = codec->start_nid;
1047
1048 for (i = 0; i < codec->num_nodes; i++, nid++) {
1049 struct hda_pincfg *pin;
1050 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001051 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001052 if (wid_type != AC_WID_PIN)
1053 continue;
1054 pin = snd_array_new(&codec->init_pins);
1055 if (!pin)
1056 return -ENOMEM;
1057 pin->nid = nid;
1058 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001060 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1061 AC_VERB_GET_PIN_WIDGET_CONTROL,
1062 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001063 }
1064 return 0;
1065}
1066
1067/* look up the given pin config list and return the item matching with NID */
1068static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1069 struct snd_array *array,
1070 hda_nid_t nid)
1071{
1072 int i;
1073 for (i = 0; i < array->used; i++) {
1074 struct hda_pincfg *pin = snd_array_elem(array, i);
1075 if (pin->nid == nid)
1076 return pin;
1077 }
1078 return NULL;
1079}
1080
Takashi Iwai3be14142009-02-20 14:11:16 +01001081/* set the current pin config value for the given NID.
1082 * the value is cached, and read via snd_hda_codec_get_pincfg()
1083 */
1084int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1085 hda_nid_t nid, unsigned int cfg)
1086{
1087 struct hda_pincfg *pin;
1088
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001089 /* the check below may be invalid when pins are added by a fixup
1090 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1091 * for now
1092 */
1093 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001094 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1095 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001096 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001097
Takashi Iwai3be14142009-02-20 14:11:16 +01001098 pin = look_up_pincfg(codec, list, nid);
1099 if (!pin) {
1100 pin = snd_array_new(list);
1101 if (!pin)
1102 return -ENOMEM;
1103 pin->nid = nid;
1104 }
1105 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001106 return 0;
1107}
1108
Takashi Iwaid5191e52009-11-16 14:58:17 +01001109/**
1110 * snd_hda_codec_set_pincfg - Override a pin default configuration
1111 * @codec: the HDA codec
1112 * @nid: NID to set the pin config
1113 * @cfg: the pin default config value
1114 *
1115 * Override a pin default configuration value in the cache.
1116 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1117 * priority than the real hardware value.
1118 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001119int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1120 hda_nid_t nid, unsigned int cfg)
1121{
Takashi Iwai346ff702009-02-23 09:42:57 +01001122 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001123}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001124EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001125
Takashi Iwaid5191e52009-11-16 14:58:17 +01001126/**
1127 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1128 * @codec: the HDA codec
1129 * @nid: NID to get the pin config
1130 *
1131 * Get the current pin config value of the given pin NID.
1132 * If the pincfg value is cached or overridden via sysfs or driver,
1133 * returns the cached value.
1134 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001135unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1136{
1137 struct hda_pincfg *pin;
1138
Takashi Iwai648a8d22014-02-25 10:38:13 +01001139#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001140 {
1141 unsigned int cfg = 0;
1142 mutex_lock(&codec->user_mutex);
1143 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1144 if (pin)
1145 cfg = pin->cfg;
1146 mutex_unlock(&codec->user_mutex);
1147 if (cfg)
1148 return cfg;
1149 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001150#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001151 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1152 if (pin)
1153 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001154 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1155 if (pin)
1156 return pin->cfg;
1157 return 0;
1158}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001159EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001160
Takashi Iwai95a962c2014-10-29 16:03:58 +01001161/**
1162 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1163 * @codec: the HDA codec
1164 * @nid: pin NID
1165 * @val: assigned pinctl value
1166 *
1167 * This function stores the given value to a pinctl target value in the
1168 * pincfg table. This isn't always as same as the actually written value
1169 * but can be referred at any time via snd_hda_codec_get_pin_target().
1170 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001171int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1172 unsigned int val)
1173{
1174 struct hda_pincfg *pin;
1175
1176 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1177 if (!pin)
1178 return -EINVAL;
1179 pin->target = val;
1180 return 0;
1181}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001182EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001183
Takashi Iwai95a962c2014-10-29 16:03:58 +01001184/**
1185 * snd_hda_codec_get_pin_target - return the current pinctl target value
1186 * @codec: the HDA codec
1187 * @nid: pin NID
1188 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001189int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1190{
1191 struct hda_pincfg *pin;
1192
1193 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1194 if (!pin)
1195 return 0;
1196 return pin->target;
1197}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001198EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001199
Takashi Iwai92ee6162009-12-27 11:18:59 +01001200/**
1201 * snd_hda_shutup_pins - Shut up all pins
1202 * @codec: the HDA codec
1203 *
1204 * Clear all pin controls to shup up before suspend for avoiding click noise.
1205 * The controls aren't cached so that they can be resumed properly.
1206 */
1207void snd_hda_shutup_pins(struct hda_codec *codec)
1208{
1209 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001210 /* don't shut up pins when unloading the driver; otherwise it breaks
1211 * the default pin setup at the next load of the driver
1212 */
1213 if (codec->bus->shutdown)
1214 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001215 for (i = 0; i < codec->init_pins.used; i++) {
1216 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1217 /* use read here for syncing after issuing each verb */
1218 snd_hda_codec_read(codec, pin->nid, 0,
1219 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1220 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001221 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001222}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001223EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001224
Takashi Iwai2a439522011-07-26 09:52:50 +02001225#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001226/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1227static void restore_shutup_pins(struct hda_codec *codec)
1228{
1229 int i;
1230 if (!codec->pins_shutup)
1231 return;
1232 if (codec->bus->shutdown)
1233 return;
1234 for (i = 0; i < codec->init_pins.used; i++) {
1235 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1236 snd_hda_codec_write(codec, pin->nid, 0,
1237 AC_VERB_SET_PIN_WIDGET_CONTROL,
1238 pin->ctrl);
1239 }
1240 codec->pins_shutup = 0;
1241}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001242#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001243
David Henningsson26a6cb62012-10-09 15:04:21 +02001244static void hda_jackpoll_work(struct work_struct *work)
1245{
1246 struct hda_codec *codec =
1247 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001248
1249 snd_hda_jack_set_dirty_all(codec);
1250 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001251
1252 if (!codec->jackpoll_interval)
1253 return;
1254
David Henningsson26a6cb62012-10-09 15:04:21 +02001255 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1256 codec->jackpoll_interval);
1257}
1258
Takashi Iwai01751f52007-08-10 16:59:39 +02001259static void init_hda_cache(struct hda_cache_rec *cache,
1260 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001261static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001262
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001263/* release all pincfg lists */
1264static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001265{
Takashi Iwai346ff702009-02-23 09:42:57 +01001266 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001267#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001268 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001269#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001270 snd_array_free(&codec->init_pins);
1271}
1272
Takashi Iwai54d17402005-11-21 16:33:22 +01001273/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001274 * audio-converter setup caches
1275 */
1276struct hda_cvt_setup {
1277 hda_nid_t nid;
1278 u8 stream_tag;
1279 u8 channel_id;
1280 u16 format_id;
1281 unsigned char active; /* cvt is currently used */
1282 unsigned char dirty; /* setups should be cleared */
1283};
1284
1285/* get or create a cache entry for the given audio converter NID */
1286static struct hda_cvt_setup *
1287get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1288{
1289 struct hda_cvt_setup *p;
1290 int i;
1291
1292 for (i = 0; i < codec->cvt_setups.used; i++) {
1293 p = snd_array_elem(&codec->cvt_setups, i);
1294 if (p->nid == nid)
1295 return p;
1296 }
1297 p = snd_array_new(&codec->cvt_setups);
1298 if (p)
1299 p->nid = nid;
1300 return p;
1301}
1302
1303/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001304 * Dynamic symbol binding for the codec parsers
1305 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001306
1307#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001308 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001309
1310static void unload_parser(struct hda_codec *codec)
1311{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001312 if (codec->parser)
1313 symbol_put_addr(codec->parser);
1314 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001315}
1316
1317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * codec destructor
1319 */
1320static void snd_hda_codec_free(struct hda_codec *codec)
1321{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001322 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001324 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001325 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001326 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001327#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001328 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001329 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001332 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001333 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001334 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001335 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001336 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 codec->bus->caddr_tbl[codec->addr] = NULL;
1338 if (codec->patch_ops.free)
1339 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001340 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001341 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001342 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001343 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001344 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001345 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001346 kfree(codec->vendor_name);
1347 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001348 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001349 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001350 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001351 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001354static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1355 hda_nid_t fg, unsigned int power_state);
1356
Takashi Iwaid8193872012-08-31 07:54:38 -07001357static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001358 unsigned int power_state);
1359
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001360static int snd_hda_codec_dev_register(struct snd_device *device)
1361{
1362 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001363 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001364
Takashi Iwaid604b392014-02-28 13:42:09 +01001365 if (err < 0)
1366 return err;
1367 snd_hda_register_beep_device(codec);
1368 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001369}
1370
1371static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1372{
1373 struct hda_codec *codec = device->device_data;
1374
Takashi Iwaid604b392014-02-28 13:42:09 +01001375 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001376 device_del(&codec->dev);
1377 return 0;
1378}
1379
Takashi Iwai2565c892014-02-19 11:41:09 +01001380static int snd_hda_codec_dev_free(struct snd_device *device)
1381{
1382 snd_hda_codec_free(device->device_data);
1383 return 0;
1384}
1385
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001386/* just free the container */
1387static void snd_hda_codec_dev_release(struct device *dev)
1388{
1389 kfree(container_of(dev, struct hda_codec, dev));
1390}
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392/**
1393 * snd_hda_codec_new - create a HDA codec
1394 * @bus: the bus to assign
1395 * @codec_addr: the codec address
1396 * @codecp: the pointer to store the generated codec
1397 *
1398 * Returns 0 if successful, or a negative error code.
1399 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001400int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001401 unsigned int codec_addr,
1402 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001405 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001406 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001408 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001409 .dev_register = snd_hda_codec_dev_register,
1410 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001411 .dev_free = snd_hda_codec_dev_free,
1412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Takashi Iwaida3cec32008-08-08 17:12:14 +02001414 if (snd_BUG_ON(!bus))
1415 return -EINVAL;
1416 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001420 dev_err(bus->card->dev,
1421 "address 0x%x is already occupied\n",
1422 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EBUSY;
1424 }
1425
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001426 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001428 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENOMEM;
1430 }
1431
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001432 device_initialize(&codec->dev);
1433 codec->dev.parent = &bus->card->card_dev;
1434 codec->dev.class = sound_class;
1435 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001436 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001437 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1438 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001439 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 codec->bus = bus;
1442 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001443 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001444 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001445 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001446 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001447 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001448 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1449 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001450 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001451 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001452 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001453 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001454 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001455 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001456 INIT_LIST_HEAD(&codec->conn_list);
1457
David Henningsson26a6cb62012-10-09 15:04:21 +02001458 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001459 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001460 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Takashi Iwai83012a72012-08-24 18:38:08 +02001462#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001463 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001464 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1465 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1466 * the caller has to power down appropriatley after initialization
1467 * phase.
1468 */
1469 hda_keep_power_on(codec);
1470#endif
1471
Takashi Iwai648a8d22014-02-25 10:38:13 +01001472 snd_hda_sysfs_init(codec);
1473
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001474 if (codec->bus->modelname) {
1475 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1476 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001477 err = -ENODEV;
1478 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001479 }
1480 }
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001483 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 bus->caddr_tbl[codec_addr] = codec;
1486
Takashi Iwai0ba21762007-04-16 11:29:14 +02001487 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1488 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001489 if (codec->vendor_id == -1)
1490 /* read again, hopefully the access method was corrected
1491 * in the last read...
1492 */
1493 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1494 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1496 AC_PAR_SUBSYSTEM_ID);
1497 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1498 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001500 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001502 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001503 err = -ENODEV;
1504 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
Takashi Iwaid8193872012-08-31 07:54:38 -07001507 fg = codec->afg ? codec->afg : codec->mfg;
1508 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001509 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001510 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001511 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001512 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001513 err = read_pin_defaults(codec);
1514 if (err < 0)
1515 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001516
Takashi Iwai0ba21762007-04-16 11:29:14 +02001517 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001518 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001519 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001520 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001521 }
1522
Takashi Iwai83012a72012-08-24 18:38:08 +02001523#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001524 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001525 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001526#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001527 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001528 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001529#ifdef CONFIG_PM
1530 if (!codec->d3_stop_clk || !codec->epss)
1531 bus->power_keep_link_on = 1;
1532#endif
1533
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001534
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001535 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001536 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001537
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001538 snd_hda_codec_proc_new(codec);
1539
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001540 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001541
1542 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1543 codec->subsystem_id, codec->revision_id);
1544 snd_component_add(codec->bus->card, component);
1545
Takashi Iwai2565c892014-02-19 11:41:09 +01001546 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1547 if (err < 0)
1548 goto error;
1549
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001550 if (codecp)
1551 *codecp = codec;
1552 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001553
1554 error:
1555 snd_hda_codec_free(codec);
1556 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001557}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001558EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001559
Takashi Iwai95a962c2014-10-29 16:03:58 +01001560/**
1561 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1562 * @codec: the HDA codec
1563 *
1564 * Forcibly refresh the all widget caps and the init pin configurations of
1565 * the given codec.
1566 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001567int snd_hda_codec_update_widgets(struct hda_codec *codec)
1568{
1569 hda_nid_t fg;
1570 int err;
1571
1572 /* Assume the function group node does not change,
1573 * only the widget nodes may change.
1574 */
1575 kfree(codec->wcaps);
1576 fg = codec->afg ? codec->afg : codec->mfg;
1577 err = read_widget_caps(codec, fg);
1578 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001579 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001580 return err;
1581 }
1582
1583 snd_array_free(&codec->init_pins);
1584 err = read_pin_defaults(codec);
1585
1586 return err;
1587}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001588EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001589
1590
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001591#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001592/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1593static bool is_likely_hdmi_codec(struct hda_codec *codec)
1594{
1595 hda_nid_t nid = codec->start_nid;
1596 int i;
1597
1598 for (i = 0; i < codec->num_nodes; i++, nid++) {
1599 unsigned int wcaps = get_wcaps(codec, nid);
1600 switch (get_wcaps_type(wcaps)) {
1601 case AC_WID_AUD_IN:
1602 return false; /* HDMI parser supports only HDMI out */
1603 case AC_WID_AUD_OUT:
1604 if (!(wcaps & AC_WCAP_DIGITAL))
1605 return false;
1606 break;
1607 }
1608 }
1609 return true;
1610}
1611#else
1612/* no HDMI codec parser support */
1613#define is_likely_hdmi_codec(codec) false
1614#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1615
Takashi Iwaid5191e52009-11-16 14:58:17 +01001616/**
1617 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1618 * @codec: the HDA codec
1619 *
1620 * Start parsing of the given codec tree and (re-)initialize the whole
1621 * patch instance.
1622 *
1623 * Returns 0 if successful or a negative error code.
1624 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001625int snd_hda_codec_configure(struct hda_codec *codec)
1626{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001627 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001628 int err;
1629
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001630 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001631 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001632 err = get_codec_name(codec);
1633 if (err < 0)
1634 return err;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001637 if (!is_generic_config(codec) && codec->preset)
1638 patch = codec->preset->patch;
1639 if (!patch) {
1640 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001641 if (is_likely_hdmi_codec(codec)) {
1642#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001643 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001644#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1645 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001646#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001647 }
1648 if (!patch) {
1649#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1650 patch = load_parser(codec, snd_hda_parse_generic_codec);
1651#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1652 patch = snd_hda_parse_generic_codec;
1653#endif
1654 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001655 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001656 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001657 return -ENODEV;
1658 }
Takashi Iwai82467612007-07-27 19:15:54 +02001659 }
1660
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001661 err = patch(codec);
1662 if (err < 0) {
1663 unload_parser(codec);
1664 return err;
1665 }
Takashi Iwai82467612007-07-27 19:15:54 +02001666
Takashi Iwaif62faed2009-12-23 09:27:51 +01001667 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001668 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001669 snprintf(codec->bus->card->mixername,
1670 sizeof(codec->bus->card->mixername),
1671 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001674EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Takashi Iwaied360812012-08-08 17:12:52 +02001676/* update the stream-id if changed */
1677static void update_pcm_stream_id(struct hda_codec *codec,
1678 struct hda_cvt_setup *p, hda_nid_t nid,
1679 u32 stream_tag, int channel_id)
1680{
1681 unsigned int oldval, newval;
1682
1683 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1684 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1685 newval = (stream_tag << 4) | channel_id;
1686 if (oldval != newval)
1687 snd_hda_codec_write(codec, nid, 0,
1688 AC_VERB_SET_CHANNEL_STREAMID,
1689 newval);
1690 p->stream_tag = stream_tag;
1691 p->channel_id = channel_id;
1692 }
1693}
1694
1695/* update the format-id if changed */
1696static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1697 hda_nid_t nid, int format)
1698{
1699 unsigned int oldval;
1700
1701 if (p->format_id != format) {
1702 oldval = snd_hda_codec_read(codec, nid, 0,
1703 AC_VERB_GET_STREAM_FORMAT, 0);
1704 if (oldval != format) {
1705 msleep(1);
1706 snd_hda_codec_write(codec, nid, 0,
1707 AC_VERB_SET_STREAM_FORMAT,
1708 format);
1709 }
1710 p->format_id = format;
1711 }
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/**
1715 * snd_hda_codec_setup_stream - set up the codec for streaming
1716 * @codec: the CODEC to set up
1717 * @nid: the NID to set up
1718 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1719 * @channel_id: channel id to pass, zero based.
1720 * @format: stream format.
1721 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001722void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1723 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int channel_id, int format)
1725{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001726 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001727 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001728 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001729 int i;
1730
Takashi Iwai0ba21762007-04-16 11:29:14 +02001731 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001732 return;
1733
Takashi Iwai4e76a882014-02-25 12:21:03 +01001734 codec_dbg(codec,
1735 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1736 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001737 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001738 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001739 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001740
1741 if (codec->pcm_format_first)
1742 update_pcm_format(codec, p, nid, format);
1743 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1744 if (!codec->pcm_format_first)
1745 update_pcm_format(codec, p, nid, format);
1746
Takashi Iwaieb541332010-08-06 13:48:11 +02001747 p->active = 1;
1748 p->dirty = 0;
1749
1750 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001751 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001752 list_for_each_entry(c, &codec->bus->codec_list, list) {
1753 for (i = 0; i < c->cvt_setups.used; i++) {
1754 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001755 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001756 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001757 p->dirty = 1;
1758 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001761EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Takashi Iwaif0cea792010-08-13 11:56:53 +02001763static void really_cleanup_stream(struct hda_codec *codec,
1764 struct hda_cvt_setup *q);
1765
Takashi Iwaid5191e52009-11-16 14:58:17 +01001766/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001767 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001768 * @codec: the CODEC to clean up
1769 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001770 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001771 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001772void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1773 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001774{
Takashi Iwaieb541332010-08-06 13:48:11 +02001775 struct hda_cvt_setup *p;
1776
Takashi Iwai888afa12008-03-18 09:57:50 +01001777 if (!nid)
1778 return;
1779
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001780 if (codec->no_sticky_stream)
1781 do_now = 1;
1782
Takashi Iwai4e76a882014-02-25 12:21:03 +01001783 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001784 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001785 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001786 /* here we just clear the active flag when do_now isn't set;
1787 * actual clean-ups will be done later in
1788 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1789 */
1790 if (do_now)
1791 really_cleanup_stream(codec, p);
1792 else
1793 p->active = 0;
1794 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001795}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001796EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001797
Takashi Iwaieb541332010-08-06 13:48:11 +02001798static void really_cleanup_stream(struct hda_codec *codec,
1799 struct hda_cvt_setup *q)
1800{
1801 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001802 if (q->stream_tag || q->channel_id)
1803 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1804 if (q->format_id)
1805 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1806);
Takashi Iwaieb541332010-08-06 13:48:11 +02001807 memset(q, 0, sizeof(*q));
1808 q->nid = nid;
1809}
1810
1811/* clean up the all conflicting obsolete streams */
1812static void purify_inactive_streams(struct hda_codec *codec)
1813{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001814 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001815 int i;
1816
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001817 list_for_each_entry(c, &codec->bus->codec_list, list) {
1818 for (i = 0; i < c->cvt_setups.used; i++) {
1819 struct hda_cvt_setup *p;
1820 p = snd_array_elem(&c->cvt_setups, i);
1821 if (p->dirty)
1822 really_cleanup_stream(c, p);
1823 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001824 }
1825}
1826
Takashi Iwai2a439522011-07-26 09:52:50 +02001827#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001828/* clean up all streams; called from suspend */
1829static void hda_cleanup_all_streams(struct hda_codec *codec)
1830{
1831 int i;
1832
1833 for (i = 0; i < codec->cvt_setups.used; i++) {
1834 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1835 if (p->stream_tag)
1836 really_cleanup_stream(codec, p);
1837 }
1838}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001839#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/*
1842 * amp access functions
1843 */
1844
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001845/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001846#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001847#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001848#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1849#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001851#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001854static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001855 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Takashi Iwai01751f52007-08-10 16:59:39 +02001857 memset(cache, 0, sizeof(*cache));
1858 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001859 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001860}
1861
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001862static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001863{
Takashi Iwai603c4012008-07-30 15:01:44 +02001864 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001868static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwai01751f52007-08-10 16:59:39 +02001870 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1871 u16 cur = cache->hash[idx];
1872 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001875 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (info->key == key)
1877 return info;
1878 cur = info->next;
1879 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001880 return NULL;
1881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001883/* query the hash. allocate an entry if not found. */
1884static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1885 u32 key)
1886{
1887 struct hda_cache_head *info = get_hash(cache, key);
1888 if (!info) {
1889 u16 idx, cur;
1890 /* add a new hash entry */
1891 info = snd_array_new(&cache->buf);
1892 if (!info)
1893 return NULL;
1894 cur = snd_array_index(&cache->buf, info);
1895 info->key = key;
1896 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001897 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001898 idx = key % (u16)ARRAY_SIZE(cache->hash);
1899 info->next = cache->hash[idx];
1900 cache->hash[idx] = cur;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return info;
1903}
1904
Takashi Iwai01751f52007-08-10 16:59:39 +02001905/* query and allocate an amp hash entry */
1906static inline struct hda_amp_info *
1907get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1908{
1909 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1910}
1911
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001912/* overwrite the value with the key in the caps hash */
1913static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1914{
1915 struct hda_amp_info *info;
1916
1917 mutex_lock(&codec->hash_mutex);
1918 info = get_alloc_amp_hash(codec, key);
1919 if (!info) {
1920 mutex_unlock(&codec->hash_mutex);
1921 return -EINVAL;
1922 }
1923 info->amp_caps = val;
1924 info->head.val |= INFO_AMP_CAPS;
1925 mutex_unlock(&codec->hash_mutex);
1926 return 0;
1927}
1928
1929/* query the value from the caps hash; if not found, fetch the current
1930 * value from the given function and store in the hash
1931 */
1932static unsigned int
1933query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1934 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1935{
1936 struct hda_amp_info *info;
1937 unsigned int val;
1938
1939 mutex_lock(&codec->hash_mutex);
1940 info = get_alloc_amp_hash(codec, key);
1941 if (!info) {
1942 mutex_unlock(&codec->hash_mutex);
1943 return 0;
1944 }
1945 if (!(info->head.val & INFO_AMP_CAPS)) {
1946 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1947 val = func(codec, nid, dir);
1948 write_caps_hash(codec, key, val);
1949 } else {
1950 val = info->amp_caps;
1951 mutex_unlock(&codec->hash_mutex);
1952 }
1953 return val;
1954}
1955
1956static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1957 int direction)
1958{
1959 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1960 nid = codec->afg;
1961 return snd_hda_param_read(codec, nid,
1962 direction == HDA_OUTPUT ?
1963 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1964}
1965
Takashi Iwaid5191e52009-11-16 14:58:17 +01001966/**
1967 * query_amp_caps - query AMP capabilities
1968 * @codec: the HD-auio codec
1969 * @nid: the NID to query
1970 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1971 *
1972 * Query AMP capabilities for the given widget and direction.
1973 * Returns the obtained capability bits.
1974 *
1975 * When cap bits have been already read, this doesn't read again but
1976 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001978u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001980 return query_caps_hash(codec, nid, direction,
1981 HDA_HASH_KEY(nid, direction, 0),
1982 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001984EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Takashi Iwaid5191e52009-11-16 14:58:17 +01001986/**
David Henningsson861a04e2014-09-23 10:38:17 +02001987 * snd_hda_check_amp_caps - query AMP capabilities
1988 * @codec: the HD-audio codec
1989 * @nid: the NID to query
1990 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001991 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001992 *
1993 * Check whether the widget has the given amp capability for the direction.
1994 */
1995bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1996 int dir, unsigned int bits)
1997{
1998 if (!nid)
1999 return false;
2000 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2001 if (query_amp_caps(codec, nid, dir) & bits)
2002 return true;
2003 return false;
2004}
2005EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2006
2007/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002008 * snd_hda_override_amp_caps - Override the AMP capabilities
2009 * @codec: the CODEC to clean up
2010 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002011 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01002012 * @caps: the capability bits to set
2013 *
2014 * Override the cached AMP caps bits value by the given one.
2015 * This function is useful if the driver needs to adjust the AMP ranges,
2016 * e.g. limit to 0dB, etc.
2017 *
2018 * Returns zero if successful or a negative error code.
2019 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002020int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2021 unsigned int caps)
2022{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002023 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002025EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002026
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002027static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2028 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002029{
2030 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2031}
2032
Takashi Iwaid5191e52009-11-16 14:58:17 +01002033/**
2034 * snd_hda_query_pin_caps - Query PIN capabilities
2035 * @codec: the HD-auio codec
2036 * @nid: the NID to query
2037 *
2038 * Query PIN capabilities for the given widget.
2039 * Returns the obtained capability bits.
2040 *
2041 * When cap bits have been already read, this doesn't read again but
2042 * returns the cached value.
2043 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002044u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2045{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002046 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002047 read_pin_cap);
2048}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002049EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002050
Wu Fengguang864f92b2009-11-18 12:38:02 +08002051/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002052 * snd_hda_override_pin_caps - Override the pin capabilities
2053 * @codec: the CODEC
2054 * @nid: the NID to override
2055 * @caps: the capability bits to set
2056 *
2057 * Override the cached PIN capabilitiy bits value by the given one.
2058 *
2059 * Returns zero if successful or a negative error code.
2060 */
2061int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2062 unsigned int caps)
2063{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002064 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002065}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002066EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002067
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002068/* read or sync the hash value with the current value;
2069 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002071static struct hda_amp_info *
2072update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002073 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002075 struct hda_amp_info *info;
2076 unsigned int parm, val = 0;
2077 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002079 retry:
2080 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2081 if (!info)
2082 return NULL;
2083 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2084 if (!val_read) {
2085 mutex_unlock(&codec->hash_mutex);
2086 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2087 parm |= direction == HDA_OUTPUT ?
2088 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2089 parm |= index;
2090 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002091 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002092 val &= 0xff;
2093 val_read = true;
2094 mutex_lock(&codec->hash_mutex);
2095 goto retry;
2096 }
2097 info->vol[ch] = val;
2098 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002099 } else if (init_only)
2100 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002101 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
2104/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002105 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002107static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108 hda_nid_t nid, int ch, int direction, int index,
2109 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 u32 parm;
2112
2113 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2114 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2115 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002116 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2117 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002118 ; /* set the zero value as a fake mute */
2119 else
2120 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2122}
2123
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124/**
2125 * snd_hda_codec_amp_read - Read AMP value
2126 * @codec: HD-audio codec
2127 * @nid: NID to read the AMP value
2128 * @ch: channel (left=0 or right=1)
2129 * @direction: #HDA_INPUT or #HDA_OUTPUT
2130 * @index: the index value (only for input direction)
2131 *
2132 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 */
Takashi Iwai834be882006-03-01 14:16:17 +01002134int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2135 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002137 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002138 unsigned int val = 0;
2139
2140 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002141 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002142 if (info)
2143 val = info->vol[ch];
2144 mutex_unlock(&codec->hash_mutex);
2145 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002147EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Takashi Iwai280e57d2012-12-14 10:32:21 +01002149static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2150 int direction, int idx, int mask, int val,
2151 bool init_only)
2152{
2153 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002154 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002155 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002156
2157 if (snd_BUG_ON(mask & ~0xff))
2158 mask &= 0xff;
2159 val &= mask;
2160
2161 mutex_lock(&codec->hash_mutex);
2162 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2163 if (!info) {
2164 mutex_unlock(&codec->hash_mutex);
2165 return 0;
2166 }
2167 val |= info->vol[ch] & ~mask;
2168 if (info->vol[ch] == val) {
2169 mutex_unlock(&codec->hash_mutex);
2170 return 0;
2171 }
2172 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002173 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002174 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002175 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002176 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002177 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002178 return 1;
2179}
2180
Takashi Iwaid5191e52009-11-16 14:58:17 +01002181/**
2182 * snd_hda_codec_amp_update - update the AMP value
2183 * @codec: HD-audio codec
2184 * @nid: NID to read the AMP value
2185 * @ch: channel (left=0 or right=1)
2186 * @direction: #HDA_INPUT or #HDA_OUTPUT
2187 * @idx: the index value (only for input direction)
2188 * @mask: bit mask to set
2189 * @val: the bits value to set
2190 *
2191 * Update the AMP value with a bit mask.
2192 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002193 */
Takashi Iwai834be882006-03-01 14:16:17 +01002194int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2195 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002197 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002199EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Takashi Iwaid5191e52009-11-16 14:58:17 +01002201/**
2202 * snd_hda_codec_amp_stereo - update the AMP stereo values
2203 * @codec: HD-audio codec
2204 * @nid: NID to read the AMP value
2205 * @direction: #HDA_INPUT or #HDA_OUTPUT
2206 * @idx: the index value (only for input direction)
2207 * @mask: bit mask to set
2208 * @val: the bits value to set
2209 *
2210 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2211 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002212 */
2213int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2214 int direction, int idx, int mask, int val)
2215{
2216 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002217
2218 if (snd_BUG_ON(mask & ~0xff))
2219 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002220 for (ch = 0; ch < 2; ch++)
2221 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2222 idx, mask, val);
2223 return ret;
2224}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002225EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002226
Takashi Iwai95a962c2014-10-29 16:03:58 +01002227/**
2228 * snd_hda_codec_amp_init - initialize the AMP value
2229 * @codec: the HDA codec
2230 * @nid: NID to read the AMP value
2231 * @ch: channel (left=0 or right=1)
2232 * @dir: #HDA_INPUT or #HDA_OUTPUT
2233 * @idx: the index value (only for input direction)
2234 * @mask: bit mask to set
2235 * @val: the bits value to set
2236 *
2237 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002238 * the first access. If the amp was already initialized / updated beforehand,
2239 * this does nothing.
2240 */
2241int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2242 int dir, int idx, int mask, int val)
2243{
2244 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2245}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002246EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002247
Takashi Iwai95a962c2014-10-29 16:03:58 +01002248/**
2249 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2250 * @codec: the HDA codec
2251 * @nid: NID to read the AMP value
2252 * @dir: #HDA_INPUT or #HDA_OUTPUT
2253 * @idx: the index value (only for input direction)
2254 * @mask: bit mask to set
2255 * @val: the bits value to set
2256 *
2257 * Call snd_hda_codec_amp_init() for both stereo channels.
2258 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002259int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2260 int dir, int idx, int mask, int val)
2261{
2262 int ch, ret = 0;
2263
2264 if (snd_BUG_ON(mask & ~0xff))
2265 mask &= 0xff;
2266 for (ch = 0; ch < 2; ch++)
2267 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2268 idx, mask, val);
2269 return ret;
2270}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002271EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002272
Takashi Iwaid5191e52009-11-16 14:58:17 +01002273/**
2274 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2275 * @codec: HD-audio codec
2276 *
2277 * Resume the all amp commands from the cache.
2278 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002279void snd_hda_codec_resume_amp(struct hda_codec *codec)
2280{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002281 int i;
2282
Takashi Iwaic370dd62012-12-13 18:30:04 +01002283 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002284 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002285 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2286 struct hda_amp_info *buffer;
2287 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002288 hda_nid_t nid;
2289 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002290 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002291
2292 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002293 if (!buffer->head.dirty)
2294 continue;
2295 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002296 info = *buffer;
2297 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002298 if (!key)
2299 continue;
2300 nid = key & 0xff;
2301 idx = (key >> 16) & 0xff;
2302 dir = (key >> 24) & 0xff;
2303 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002304 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002305 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002306 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002307 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2308 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002309 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002310 }
2311 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002312 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002313}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002314EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002316static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2317 unsigned int ofs)
2318{
2319 u32 caps = query_amp_caps(codec, nid, dir);
2320 /* get num steps */
2321 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2322 if (ofs < caps)
2323 caps -= ofs;
2324 return caps;
2325}
2326
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327/**
2328 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002329 * @kcontrol: referred ctl element
2330 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002331 *
2332 * The control element is supposed to have the private_value field
2333 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2334 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002335int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2339 u16 nid = get_amp_nid(kcontrol);
2340 u8 chs = get_amp_channels(kcontrol);
2341 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002342 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002344 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2345 uinfo->count = chs == 3 ? 2 : 1;
2346 uinfo->value.integer.min = 0;
2347 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2348 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002349 codec_warn(codec,
2350 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2351 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return -EINVAL;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return 0;
2355}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002356EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002358
2359static inline unsigned int
2360read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2361 int ch, int dir, int idx, unsigned int ofs)
2362{
2363 unsigned int val;
2364 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2365 val &= HDA_AMP_VOLMASK;
2366 if (val >= ofs)
2367 val -= ofs;
2368 else
2369 val = 0;
2370 return val;
2371}
2372
2373static inline int
2374update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2375 int ch, int dir, int idx, unsigned int ofs,
2376 unsigned int val)
2377{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002378 unsigned int maxval;
2379
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002380 if (val > 0)
2381 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002382 /* ofs = 0: raw max value */
2383 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002384 if (val > maxval)
2385 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002386 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2387 HDA_AMP_VOLMASK, val);
2388}
2389
Takashi Iwaid5191e52009-11-16 14:58:17 +01002390/**
2391 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002392 * @kcontrol: ctl element
2393 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002394 *
2395 * The control element is supposed to have the private_value field
2396 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2397 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002398int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2402 hda_nid_t nid = get_amp_nid(kcontrol);
2403 int chs = get_amp_channels(kcontrol);
2404 int dir = get_amp_direction(kcontrol);
2405 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002406 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 long *valp = ucontrol->value.integer.value;
2408
2409 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002410 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002412 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return 0;
2414}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002415EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Takashi Iwaid5191e52009-11-16 14:58:17 +01002417/**
2418 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002419 * @kcontrol: ctl element
2420 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002421 *
2422 * The control element is supposed to have the private_value field
2423 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2424 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2429 hda_nid_t nid = get_amp_nid(kcontrol);
2430 int chs = get_amp_channels(kcontrol);
2431 int dir = get_amp_direction(kcontrol);
2432 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002433 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 long *valp = ucontrol->value.integer.value;
2435 int change = 0;
2436
Takashi Iwaicb53c622007-08-10 17:21:45 +02002437 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002438 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002439 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002440 valp++;
2441 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002442 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002443 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002444 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return change;
2446}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002447EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449/**
2450 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002451 * @kcontrol: ctl element
2452 * @op_flag: operation flag
2453 * @size: byte size of input TLV
2454 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002455 *
2456 * The control element is supposed to have the private_value field
2457 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2458 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002459int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2460 unsigned int size, unsigned int __user *_tlv)
2461{
2462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2463 hda_nid_t nid = get_amp_nid(kcontrol);
2464 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002465 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002466 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002467 u32 caps, val1, val2;
2468
2469 if (size < 4 * sizeof(unsigned int))
2470 return -ENOMEM;
2471 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002472 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2473 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002474 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002475 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002476 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002477 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002478 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002479 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2480 return -EFAULT;
2481 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2482 return -EFAULT;
2483 if (put_user(val1, _tlv + 2))
2484 return -EFAULT;
2485 if (put_user(val2, _tlv + 3))
2486 return -EFAULT;
2487 return 0;
2488}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002489EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002490
Takashi Iwaid5191e52009-11-16 14:58:17 +01002491/**
2492 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2493 * @codec: HD-audio codec
2494 * @nid: NID of a reference widget
2495 * @dir: #HDA_INPUT or #HDA_OUTPUT
2496 * @tlv: TLV data to be stored, at least 4 elements
2497 *
2498 * Set (static) TLV data for a virtual master volume using the AMP caps
2499 * obtained from the reference NID.
2500 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002501 */
2502void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2503 unsigned int *tlv)
2504{
2505 u32 caps;
2506 int nums, step;
2507
2508 caps = query_amp_caps(codec, nid, dir);
2509 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2510 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2511 step = (step + 1) * 25;
2512 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2513 tlv[1] = 2 * sizeof(unsigned int);
2514 tlv[2] = -nums * step;
2515 tlv[3] = step;
2516}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002517EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002518
2519/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002520static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002521find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002522{
2523 struct snd_ctl_elem_id id;
2524 memset(&id, 0, sizeof(id));
2525 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002526 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002527 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002528 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2529 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002530 strcpy(id.name, name);
2531 return snd_ctl_find_id(codec->bus->card, &id);
2532}
2533
Takashi Iwaid5191e52009-11-16 14:58:17 +01002534/**
2535 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2536 * @codec: HD-audio codec
2537 * @name: ctl id name string
2538 *
2539 * Get the control element with the given id string and IFACE_MIXER.
2540 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002541struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2542 const char *name)
2543{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002544 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002545}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002546EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002547
Takashi Iwaidcda5802012-10-12 17:24:51 +02002548static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002549 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002550{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002551 int i, idx;
2552 /* 16 ctlrs should be large enough */
2553 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2554 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002555 return idx;
2556 }
2557 return -EBUSY;
2558}
2559
Takashi Iwaid5191e52009-11-16 14:58:17 +01002560/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002561 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002562 * @codec: HD-audio codec
2563 * @nid: corresponding NID (optional)
2564 * @kctl: the control element to assign
2565 *
2566 * Add the given control element to an array inside the codec instance.
2567 * All control elements belonging to a codec are supposed to be added
2568 * by this function so that a proper clean-up works at the free or
2569 * reconfiguration time.
2570 *
2571 * If non-zero @nid is passed, the NID is assigned to the control element.
2572 * The assignment is shown in the codec proc file.
2573 *
2574 * snd_hda_ctl_add() checks the control subdev id field whether
2575 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002576 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2577 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002578 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002579int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2580 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002581{
2582 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002583 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002584 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002585
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002586 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002587 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002588 if (nid == 0)
2589 nid = get_amp_nid_(kctl->private_value);
2590 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002591 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2592 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002593 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002594 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002595 err = snd_ctl_add(codec->bus->card, kctl);
2596 if (err < 0)
2597 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002598 item = snd_array_new(&codec->mixers);
2599 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002600 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002601 item->kctl = kctl;
2602 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002603 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002604 return 0;
2605}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002606EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002607
Takashi Iwaid5191e52009-11-16 14:58:17 +01002608/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002609 * snd_hda_add_nid - Assign a NID to a control element
2610 * @codec: HD-audio codec
2611 * @nid: corresponding NID (optional)
2612 * @kctl: the control element to assign
2613 * @index: index to kctl
2614 *
2615 * Add the given control element to an array inside the codec instance.
2616 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2617 * NID:KCTL mapping - for example "Capture Source" selector.
2618 */
2619int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2620 unsigned int index, hda_nid_t nid)
2621{
2622 struct hda_nid_item *item;
2623
2624 if (nid > 0) {
2625 item = snd_array_new(&codec->nids);
2626 if (!item)
2627 return -ENOMEM;
2628 item->kctl = kctl;
2629 item->index = index;
2630 item->nid = nid;
2631 return 0;
2632 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002633 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2634 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002635 return -EINVAL;
2636}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002637EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002638
2639/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002640 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2641 * @codec: HD-audio codec
2642 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002643void snd_hda_ctls_clear(struct hda_codec *codec)
2644{
2645 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002646 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002647 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002648 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002649 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002650 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002651}
2652
Takashi Iwai95a962c2014-10-29 16:03:58 +01002653/**
2654 * snd_hda_lock_devices - pseudo device locking
2655 * @bus: the BUS
2656 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002657 * toggle card->shutdown to allow/disallow the device access (as a hack)
2658 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002659int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002660{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002661 struct snd_card *card = bus->card;
2662 struct hda_codec *codec;
2663
Takashi Iwaia65d6292009-02-23 16:57:04 +01002664 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002665 if (card->shutdown)
2666 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002667 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002668 if (!list_empty(&card->ctl_files))
2669 goto err_clear;
2670
2671 list_for_each_entry(codec, &bus->codec_list, list) {
2672 int pcm;
2673 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2674 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2675 if (!cpcm->pcm)
2676 continue;
2677 if (cpcm->pcm->streams[0].substream_opened ||
2678 cpcm->pcm->streams[1].substream_opened)
2679 goto err_clear;
2680 }
2681 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002682 spin_unlock(&card->files_lock);
2683 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002684
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002685 err_clear:
2686 card->shutdown = 0;
2687 err_unlock:
2688 spin_unlock(&card->files_lock);
2689 return -EINVAL;
2690}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002691EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002692
Takashi Iwai95a962c2014-10-29 16:03:58 +01002693/**
2694 * snd_hda_unlock_devices - pseudo device unlocking
2695 * @bus: the BUS
2696 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002697void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002698{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002699 struct snd_card *card = bus->card;
2700
2701 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002702 spin_lock(&card->files_lock);
2703 card->shutdown = 0;
2704 spin_unlock(&card->files_lock);
2705}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002706EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002707
Takashi Iwaid5191e52009-11-16 14:58:17 +01002708/**
2709 * snd_hda_codec_reset - Clear all objects assigned to the codec
2710 * @codec: HD-audio codec
2711 *
2712 * This frees the all PCM and control elements assigned to the codec, and
2713 * clears the caches and restores the pin default configurations.
2714 *
2715 * When a device is being used, it returns -EBSY. If successfully freed,
2716 * returns zero.
2717 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002718int snd_hda_codec_reset(struct hda_codec *codec)
2719{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002720 struct hda_bus *bus = codec->bus;
2721 struct snd_card *card = bus->card;
2722 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002723
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002724 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002725 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002726
2727 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002728 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002729#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002730 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002731 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002732#endif
2733 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002734 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002735 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002736 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002737 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002738 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002739 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002740 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002741 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002742 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002743 if (codec->patch_ops.free)
2744 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002745 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002746 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002747 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002748 codec->spec = NULL;
2749 free_hda_cache(&codec->amp_cache);
2750 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002751 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2752 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002753 /* free only driver_pins so that init_pins + user_pins are restored */
2754 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002755 snd_array_free(&codec->cvt_setups);
2756 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002757 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002758 codec->num_pcms = 0;
2759 codec->pcm_info = NULL;
2760 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002761 codec->slave_dig_outs = NULL;
2762 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002763 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002764 module_put(codec->owner);
2765 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002766
2767 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002768 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002769 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002770}
2771
Takashi Iwai6194b992014-06-06 18:12:16 +02002772typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002773
2774/* apply the function to all matching slave ctls in the mixer list */
2775static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002776 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002777{
2778 struct hda_nid_item *items;
2779 const char * const *s;
2780 int i, err;
2781
2782 items = codec->mixers.list;
2783 for (i = 0; i < codec->mixers.used; i++) {
2784 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002785 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002786 continue;
2787 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002788 char tmpname[sizeof(sctl->id.name)];
2789 const char *name = *s;
2790 if (suffix) {
2791 snprintf(tmpname, sizeof(tmpname), "%s %s",
2792 name, suffix);
2793 name = tmpname;
2794 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002795 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002796 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002797 if (err)
2798 return err;
2799 break;
2800 }
2801 }
2802 }
2803 return 0;
2804}
2805
Takashi Iwai6194b992014-06-06 18:12:16 +02002806static int check_slave_present(struct hda_codec *codec,
2807 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002808{
2809 return 1;
2810}
2811
Takashi Iwai18478e82012-03-09 17:51:10 +01002812/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002813static int get_kctl_0dB_offset(struct hda_codec *codec,
2814 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002815{
2816 int _tlv[4];
2817 const int *tlv = NULL;
2818 int val = -1;
2819
2820 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2821 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2822 mm_segment_t fs = get_fs();
2823 set_fs(get_ds());
2824 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2825 tlv = _tlv;
2826 set_fs(fs);
2827 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2828 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002829 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2830 int step = tlv[3];
2831 step &= ~TLV_DB_SCALE_MUTE;
2832 if (!step)
2833 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002834 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002835 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002836- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002837 return -1;
2838 }
2839 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002840 val = -tlv[2] / step;
2841 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002842 return val;
2843}
2844
2845/* call kctl->put with the given value(s) */
2846static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2847{
2848 struct snd_ctl_elem_value *ucontrol;
2849 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2850 if (!ucontrol)
2851 return -ENOMEM;
2852 ucontrol->value.integer.value[0] = val;
2853 ucontrol->value.integer.value[1] = val;
2854 kctl->put(kctl, ucontrol);
2855 kfree(ucontrol);
2856 return 0;
2857}
2858
2859/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002860static int init_slave_0dB(struct hda_codec *codec,
2861 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002862{
Takashi Iwai6194b992014-06-06 18:12:16 +02002863 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002864 if (offset > 0)
2865 put_kctl_with_value(slave, offset);
2866 return 0;
2867}
2868
2869/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002870static int init_slave_unmute(struct hda_codec *codec,
2871 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002872{
2873 return put_kctl_with_value(slave, 1);
2874}
2875
Takashi Iwaie8750942014-06-30 14:02:39 +02002876static int add_slave(struct hda_codec *codec,
2877 void *data, struct snd_kcontrol *slave)
2878{
2879 return snd_ctl_add_slave(data, slave);
2880}
2881
Takashi Iwaid5191e52009-11-16 14:58:17 +01002882/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002883 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002884 * @codec: HD-audio codec
2885 * @name: vmaster control name
2886 * @tlv: TLV data (optional)
2887 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002888 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002889 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002890 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002891 *
2892 * Create a virtual master control with the given name. The TLV data
2893 * must be either NULL or a valid data.
2894 *
2895 * @slaves is a NULL-terminated array of strings, each of which is a
2896 * slave control name. All controls with these names are assigned to
2897 * the new virtual master control.
2898 *
2899 * This function returns zero if successful or a negative error code.
2900 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002901int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002902 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002903 const char *suffix, bool init_slave_vol,
2904 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002905{
2906 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002907 int err;
2908
Takashi Iwai29e58532012-03-12 12:25:03 +01002909 if (ctl_ret)
2910 *ctl_ret = NULL;
2911
Takashi Iwai9322ca52012-02-03 14:28:01 +01002912 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002913 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002914 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002915 return 0;
2916 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002917 kctl = snd_ctl_make_virtual_master(name, tlv);
2918 if (!kctl)
2919 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002920 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002921 if (err < 0)
2922 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002923
Takashi Iwaie8750942014-06-30 14:02:39 +02002924 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002925 if (err < 0)
2926 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002927
2928 /* init with master mute & zero volume */
2929 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002930 if (init_slave_vol) {
2931 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002932 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002933 tlv ? init_slave_0dB : init_slave_unmute, &step);
2934 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002935
Takashi Iwai29e58532012-03-12 12:25:03 +01002936 if (ctl_ret)
2937 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002938 return 0;
2939}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002940EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002941
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002942/*
2943 * mute-LED control using vmaster
2944 */
2945static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_info *uinfo)
2947{
2948 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002949 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002950 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002951
Takashi Iwai3ff72212014-10-20 18:17:28 +02002952 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002953}
2954
2955static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2957{
2958 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2959 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2960 return 0;
2961}
2962
2963static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
2965{
2966 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2967 unsigned int old_mode = hook->mute_mode;
2968
2969 hook->mute_mode = ucontrol->value.enumerated.item[0];
2970 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2971 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2972 if (old_mode == hook->mute_mode)
2973 return 0;
2974 snd_hda_sync_vmaster_hook(hook);
2975 return 1;
2976}
2977
2978static struct snd_kcontrol_new vmaster_mute_mode = {
2979 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2980 .name = "Mute-LED Mode",
2981 .info = vmaster_mute_mode_info,
2982 .get = vmaster_mute_mode_get,
2983 .put = vmaster_mute_mode_put,
2984};
2985
Takashi Iwai95a962c2014-10-29 16:03:58 +01002986/**
2987 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2988 * @codec: the HDA codec
2989 * @hook: the vmaster hook object
2990 * @expose_enum_ctl: flag to create an enum ctl
2991 *
2992 * Add a mute-LED hook with the given vmaster switch kctl.
2993 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2994 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002995 */
2996int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002997 struct hda_vmaster_mute_hook *hook,
2998 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002999{
3000 struct snd_kcontrol *kctl;
3001
3002 if (!hook->hook || !hook->sw_kctl)
3003 return 0;
3004 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3005 hook->codec = codec;
3006 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01003007 if (!expose_enum_ctl)
3008 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003009 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3010 if (!kctl)
3011 return -ENOMEM;
3012 return snd_hda_ctl_add(codec, 0, kctl);
3013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003014EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003015
Takashi Iwai95a962c2014-10-29 16:03:58 +01003016/**
3017 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3018 * @hook: the vmaster hook
3019 *
3020 * Call the hook with the current value for synchronization.
3021 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003022 */
3023void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3024{
3025 if (!hook->hook || !hook->codec)
3026 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003027 /* don't call vmaster hook in the destructor since it might have
3028 * been already destroyed
3029 */
3030 if (hook->codec->bus->shutdown)
3031 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003032 switch (hook->mute_mode) {
3033 case HDA_VMUTE_FOLLOW_MASTER:
3034 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3035 break;
3036 default:
3037 hook->hook(hook->codec, hook->mute_mode);
3038 break;
3039 }
3040}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003041EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003042
3043
Takashi Iwaid5191e52009-11-16 14:58:17 +01003044/**
3045 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003046 * @kcontrol: referred ctl element
3047 * @uinfo: 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_info(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 int chs = get_amp_channels(kcontrol);
3056
3057 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3058 uinfo->count = chs == 3 ? 2 : 1;
3059 uinfo->value.integer.min = 0;
3060 uinfo->value.integer.max = 1;
3061 return 0;
3062}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003063EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Takashi Iwaid5191e52009-11-16 14:58:17 +01003065/**
3066 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003067 * @kcontrol: ctl element
3068 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003069 *
3070 * The control element is supposed to have the private_value field
3071 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3072 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003073int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3074 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
3076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3077 hda_nid_t nid = get_amp_nid(kcontrol);
3078 int chs = get_amp_channels(kcontrol);
3079 int dir = get_amp_direction(kcontrol);
3080 int idx = get_amp_index(kcontrol);
3081 long *valp = ucontrol->value.integer.value;
3082
3083 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003084 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003085 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003088 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return 0;
3090}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003091EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Takashi Iwaid5191e52009-11-16 14:58:17 +01003093/**
3094 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003095 * @kcontrol: ctl element
3096 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003097 *
3098 * The control element is supposed to have the private_value field
3099 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3100 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3102 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
3104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3105 hda_nid_t nid = get_amp_nid(kcontrol);
3106 int chs = get_amp_channels(kcontrol);
3107 int dir = get_amp_direction(kcontrol);
3108 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 long *valp = ucontrol->value.integer.value;
3110 int change = 0;
3111
Takashi Iwaicb53c622007-08-10 17:21:45 +02003112 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003113 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003114 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003115 HDA_AMP_MUTE,
3116 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003117 valp++;
3118 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003119 if (chs & 2)
3120 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003121 HDA_AMP_MUTE,
3122 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003123 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003124 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return change;
3126}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003127EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129/*
Takashi Iwai985be542005-11-02 18:26:49 +01003130 * bound volume controls
3131 *
3132 * bind multiple volumes (# indices, from 0)
3133 */
3134
3135#define AMP_VAL_IDX_SHIFT 19
3136#define AMP_VAL_IDX_MASK (0x0f<<19)
3137
Takashi Iwaid5191e52009-11-16 14:58:17 +01003138/**
3139 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003140 * @kcontrol: ctl element
3141 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003142 *
3143 * The control element is supposed to have the private_value field
3144 * set up via HDA_BIND_MUTE*() macros.
3145 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003146int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3147 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003148{
3149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3150 unsigned long pval;
3151 int err;
3152
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003153 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003154 pval = kcontrol->private_value;
3155 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3156 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3157 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003158 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003159 return err;
3160}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003161EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003162
Takashi Iwaid5191e52009-11-16 14:58:17 +01003163/**
3164 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003165 * @kcontrol: ctl element
3166 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003167 *
3168 * The control element is supposed to have the private_value field
3169 * set up via HDA_BIND_MUTE*() macros.
3170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003171int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3172 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003173{
3174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3175 unsigned long pval;
3176 int i, indices, err = 0, change = 0;
3177
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003178 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003179 pval = kcontrol->private_value;
3180 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3181 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003182 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3183 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003184 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3185 if (err < 0)
3186 break;
3187 change |= err;
3188 }
3189 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003190 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003191 return err < 0 ? err : change;
3192}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003193EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003194
Takashi Iwaid5191e52009-11-16 14:58:17 +01003195/**
3196 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003197 * @kcontrol: referred ctl element
3198 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003199 *
3200 * The control element is supposed to have the private_value field
3201 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003202 */
3203int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_info *uinfo)
3205{
3206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3207 struct hda_bind_ctls *c;
3208 int err;
3209
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003210 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003211 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003212 kcontrol->private_value = *c->values;
3213 err = c->ops->info(kcontrol, uinfo);
3214 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003215 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003216 return err;
3217}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003218EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003219
Takashi Iwaid5191e52009-11-16 14:58:17 +01003220/**
3221 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003222 * @kcontrol: ctl element
3223 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003224 *
3225 * The control element is supposed to have the private_value field
3226 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3227 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003228int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3229 struct snd_ctl_elem_value *ucontrol)
3230{
3231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3232 struct hda_bind_ctls *c;
3233 int err;
3234
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003235 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003236 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003237 kcontrol->private_value = *c->values;
3238 err = c->ops->get(kcontrol, ucontrol);
3239 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003240 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003241 return err;
3242}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003243EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003244
Takashi Iwaid5191e52009-11-16 14:58:17 +01003245/**
3246 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003247 * @kcontrol: ctl element
3248 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003249 *
3250 * The control element is supposed to have the private_value field
3251 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3252 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003253int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3254 struct snd_ctl_elem_value *ucontrol)
3255{
3256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3257 struct hda_bind_ctls *c;
3258 unsigned long *vals;
3259 int err = 0, change = 0;
3260
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003261 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003262 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003263 for (vals = c->values; *vals; vals++) {
3264 kcontrol->private_value = *vals;
3265 err = c->ops->put(kcontrol, ucontrol);
3266 if (err < 0)
3267 break;
3268 change |= err;
3269 }
3270 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003271 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003272 return err < 0 ? err : change;
3273}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003274EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003275
Takashi Iwaid5191e52009-11-16 14:58:17 +01003276/**
3277 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003278 * @kcontrol: ctl element
3279 * @op_flag: operation flag
3280 * @size: byte size of input TLV
3281 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003282 *
3283 * The control element is supposed to have the private_value field
3284 * set up via HDA_BIND_VOL() macro.
3285 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003286int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3287 unsigned int size, unsigned int __user *tlv)
3288{
3289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3290 struct hda_bind_ctls *c;
3291 int err;
3292
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003293 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003294 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003295 kcontrol->private_value = *c->values;
3296 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3297 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003298 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003299 return err;
3300}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003301EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003302
3303struct hda_ctl_ops snd_hda_bind_vol = {
3304 .info = snd_hda_mixer_amp_volume_info,
3305 .get = snd_hda_mixer_amp_volume_get,
3306 .put = snd_hda_mixer_amp_volume_put,
3307 .tlv = snd_hda_mixer_amp_tlv
3308};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003309EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003310
3311struct hda_ctl_ops snd_hda_bind_sw = {
3312 .info = snd_hda_mixer_amp_switch_info,
3313 .get = snd_hda_mixer_amp_switch_get,
3314 .put = snd_hda_mixer_amp_switch_put,
3315 .tlv = snd_hda_mixer_amp_tlv
3316};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003317EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003318
3319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 * SPDIF out controls
3321 */
3322
Takashi Iwai0ba21762007-04-16 11:29:14 +02003323static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3327 uinfo->count = 1;
3328 return 0;
3329}
3330
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3335 IEC958_AES0_NONAUDIO |
3336 IEC958_AES0_CON_EMPHASIS_5015 |
3337 IEC958_AES0_CON_NOT_COPYRIGHT;
3338 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3339 IEC958_AES1_CON_ORIGINAL;
3340 return 0;
3341}
3342
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3347 IEC958_AES0_NONAUDIO |
3348 IEC958_AES0_PRO_EMPHASIS_5015;
3349 return 0;
3350}
3351
Takashi Iwai0ba21762007-04-16 11:29:14 +02003352static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3353 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003356 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003357 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003359 mutex_lock(&codec->spdif_mutex);
3360 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003361 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3362 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3363 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3364 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003365 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 return 0;
3368}
3369
3370/* convert from SPDIF status bits to HDA SPDIF bits
3371 * bit 0 (DigEn) is always set zero (to be filled later)
3372 */
3373static unsigned short convert_from_spdif_status(unsigned int sbits)
3374{
3375 unsigned short val = 0;
3376
3377 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003378 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003380 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003382 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3383 IEC958_AES0_PRO_EMPHASIS_5015)
3384 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003386 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3387 IEC958_AES0_CON_EMPHASIS_5015)
3388 val |= AC_DIG1_EMPHASIS;
3389 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3390 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003392 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3394 }
3395 return val;
3396}
3397
3398/* convert to SPDIF status bits from HDA SPDIF bits
3399 */
3400static unsigned int convert_to_spdif_status(unsigned short val)
3401{
3402 unsigned int sbits = 0;
3403
Takashi Iwai0ba21762007-04-16 11:29:14 +02003404 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003406 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 sbits |= IEC958_AES0_PROFESSIONAL;
3408 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003409 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3411 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003412 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003414 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003416 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3418 sbits |= val & (0x7f << 8);
3419 }
3420 return sbits;
3421}
3422
Takashi Iwai2f728532008-09-25 16:32:41 +02003423/* set digital convert verbs both for the given NID and its slaves */
3424static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3425 int verb, int val)
3426{
Takashi Iwaidda14412011-05-02 11:29:30 +02003427 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003428
Takashi Iwai9e976972008-11-25 08:17:20 +01003429 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003430 d = codec->slave_dig_outs;
3431 if (!d)
3432 return;
3433 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003434 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003435}
3436
3437static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3438 int dig1, int dig2)
3439{
3440 if (dig1 != -1)
3441 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3442 if (dig2 != -1)
3443 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3444}
3445
Takashi Iwai0ba21762007-04-16 11:29:14 +02003446static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3447 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003450 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003451 struct hda_spdif_out *spdif;
3452 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 unsigned short val;
3454 int change;
3455
Ingo Molnar62932df2006-01-16 16:34:20 +01003456 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003457 spdif = snd_array_elem(&codec->spdif_out, idx);
3458 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003459 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3461 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3462 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003463 val = convert_from_spdif_status(spdif->status);
3464 val |= spdif->ctls & 1;
3465 change = spdif->ctls != val;
3466 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003467 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003468 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003469 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return change;
3471}
3472
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003473#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Takashi Iwai0ba21762007-04-16 11:29:14 +02003475static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3476 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003479 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003480 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003482 mutex_lock(&codec->spdif_mutex);
3483 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003484 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003485 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487}
3488
Stephen Warren74b654c2011-06-01 11:14:18 -06003489static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3490 int dig1, int dig2)
3491{
3492 set_dig_out_convert(codec, nid, dig1, dig2);
3493 /* unmute amp switch (if any) */
3494 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3495 (dig1 & AC_DIG1_ENABLE))
3496 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3497 HDA_AMP_MUTE, 0);
3498}
3499
Takashi Iwai0ba21762007-04-16 11:29:14 +02003500static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003504 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003505 struct hda_spdif_out *spdif;
3506 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 unsigned short val;
3508 int change;
3509
Ingo Molnar62932df2006-01-16 16:34:20 +01003510 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003511 spdif = snd_array_elem(&codec->spdif_out, idx);
3512 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003513 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003515 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003516 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003517 spdif->ctls = val;
3518 if (change && nid != (u16)-1)
3519 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003520 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 return change;
3522}
3523
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003524static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 {
3526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003528 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 .info = snd_hda_spdif_mask_info,
3530 .get = snd_hda_spdif_cmask_get,
3531 },
3532 {
3533 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003535 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 .info = snd_hda_spdif_mask_info,
3537 .get = snd_hda_spdif_pmask_get,
3538 },
3539 {
3540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003541 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 .info = snd_hda_spdif_mask_info,
3543 .get = snd_hda_spdif_default_get,
3544 .put = snd_hda_spdif_default_put,
3545 },
3546 {
3547 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003548 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 .info = snd_hda_spdif_out_switch_info,
3550 .get = snd_hda_spdif_out_switch_get,
3551 .put = snd_hda_spdif_out_switch_put,
3552 },
3553 { } /* end */
3554};
3555
3556/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003557 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003559 * @associated_nid: NID that new ctls associated with
3560 * @cvt_nid: converter NID
3561 * @type: HDA_PCM_TYPE_*
3562 * Creates controls related with the digital output.
3563 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 *
3565 * Returns 0 if successful, or a negative error code.
3566 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003567int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3568 hda_nid_t associated_nid,
3569 hda_nid_t cvt_nid,
3570 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003573 struct snd_kcontrol *kctl;
3574 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003575 int idx = 0;
3576 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003577 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003578 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003580 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003581 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003582 idx = spdif_index;
3583 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003584 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003585 /* suppose a single SPDIF device */
3586 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3587 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3588 if (!kctl)
3589 break;
3590 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003591 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003592 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003593 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003594 if (!bus->primary_dig_out_type)
3595 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003596
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003597 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003598 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003599 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003600 return -EBUSY;
3601 }
Stephen Warren7c935972011-06-01 11:14:17 -06003602 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003603 if (!spdif)
3604 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3606 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003607 if (!kctl)
3608 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003609 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003610 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003611 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003612 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return err;
3614 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003615 spdif->nid = cvt_nid;
3616 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003617 AC_VERB_GET_DIGI_CONVERT_1, 0);
3618 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return 0;
3620}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003621EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Takashi Iwai95a962c2014-10-29 16:03:58 +01003623/**
3624 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3625 * @codec: the HDA codec
3626 * @nid: widget NID
3627 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003628 * call within spdif_mutex lock
3629 */
Stephen Warren7c935972011-06-01 11:14:17 -06003630struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3631 hda_nid_t nid)
3632{
3633 int i;
3634 for (i = 0; i < codec->spdif_out.used; i++) {
3635 struct hda_spdif_out *spdif =
3636 snd_array_elem(&codec->spdif_out, i);
3637 if (spdif->nid == nid)
3638 return spdif;
3639 }
3640 return NULL;
3641}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003642EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003643
Takashi Iwai95a962c2014-10-29 16:03:58 +01003644/**
3645 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3646 * @codec: the HDA codec
3647 * @idx: the SPDIF ctl index
3648 *
3649 * Unassign the widget from the given SPDIF control.
3650 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003651void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3652{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003653 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003654
3655 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003656 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003657 spdif->nid = (u16)-1;
3658 mutex_unlock(&codec->spdif_mutex);
3659}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003660EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003661
Takashi Iwai95a962c2014-10-29 16:03:58 +01003662/**
3663 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3664 * @codec: the HDA codec
3665 * @idx: the SPDIF ctl idx
3666 * @nid: widget NID
3667 *
3668 * Assign the widget to the SPDIF control with the given index.
3669 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003670void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3671{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003672 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003673 unsigned short val;
3674
3675 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003676 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003677 if (spdif->nid != nid) {
3678 spdif->nid = nid;
3679 val = spdif->ctls;
3680 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3681 }
3682 mutex_unlock(&codec->spdif_mutex);
3683}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003684EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003687 * SPDIF sharing with analog output
3688 */
3689static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3690 struct snd_ctl_elem_value *ucontrol)
3691{
3692 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3693 ucontrol->value.integer.value[0] = mout->share_spdif;
3694 return 0;
3695}
3696
3697static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3698 struct snd_ctl_elem_value *ucontrol)
3699{
3700 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3701 mout->share_spdif = !!ucontrol->value.integer.value[0];
3702 return 0;
3703}
3704
3705static struct snd_kcontrol_new spdif_share_sw = {
3706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3707 .name = "IEC958 Default PCM Playback Switch",
3708 .info = snd_ctl_boolean_mono_info,
3709 .get = spdif_share_sw_get,
3710 .put = spdif_share_sw_put,
3711};
3712
Takashi Iwaid5191e52009-11-16 14:58:17 +01003713/**
3714 * snd_hda_create_spdif_share_sw - create Default PCM switch
3715 * @codec: the HDA codec
3716 * @mout: multi-out instance
3717 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003718int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3719 struct hda_multi_out *mout)
3720{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003721 struct snd_kcontrol *kctl;
3722
Takashi Iwai9a081602008-02-12 18:37:26 +01003723 if (!mout->dig_out_nid)
3724 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003725
3726 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3727 if (!kctl)
3728 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003729 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003730 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003731}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003732EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003733
3734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 * SPDIF input
3736 */
3737
3738#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3739
Takashi Iwai0ba21762007-04-16 11:29:14 +02003740static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3741 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742{
3743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3744
3745 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3746 return 0;
3747}
3748
Takashi Iwai0ba21762007-04-16 11:29:14 +02003749static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3750 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3753 hda_nid_t nid = kcontrol->private_value;
3754 unsigned int val = !!ucontrol->value.integer.value[0];
3755 int change;
3756
Ingo Molnar62932df2006-01-16 16:34:20 +01003757 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003759 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003761 snd_hda_codec_write_cache(codec, nid, 0,
3762 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003764 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return change;
3766}
3767
Takashi Iwai0ba21762007-04-16 11:29:14 +02003768static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3769 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
3771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3772 hda_nid_t nid = kcontrol->private_value;
3773 unsigned short val;
3774 unsigned int sbits;
3775
Andrew Paprocki3982d172007-12-19 12:13:44 +01003776 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 sbits = convert_to_spdif_status(val);
3778 ucontrol->value.iec958.status[0] = sbits;
3779 ucontrol->value.iec958.status[1] = sbits >> 8;
3780 ucontrol->value.iec958.status[2] = sbits >> 16;
3781 ucontrol->value.iec958.status[3] = sbits >> 24;
3782 return 0;
3783}
3784
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003785static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 {
3787 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003788 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 .info = snd_hda_spdif_in_switch_info,
3790 .get = snd_hda_spdif_in_switch_get,
3791 .put = snd_hda_spdif_in_switch_put,
3792 },
3793 {
3794 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003796 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 .info = snd_hda_spdif_mask_info,
3798 .get = snd_hda_spdif_in_status_get,
3799 },
3800 { } /* end */
3801};
3802
3803/**
3804 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3805 * @codec: the HDA codec
3806 * @nid: audio in widget NID
3807 *
3808 * Creates controls related with the SPDIF input.
3809 * Called from each patch supporting the SPDIF in.
3810 *
3811 * Returns 0 if successful, or a negative error code.
3812 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003813int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003816 struct snd_kcontrol *kctl;
3817 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003818 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Takashi Iwaidcda5802012-10-12 17:24:51 +02003820 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003821 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003822 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003823 return -EBUSY;
3824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3826 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003827 if (!kctl)
3828 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003830 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003831 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return err;
3833 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003834 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003835 snd_hda_codec_read(codec, nid, 0,
3836 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003837 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return 0;
3839}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003840EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003842/*
3843 * command cache
3844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Takashi Iwaic370dd62012-12-13 18:30:04 +01003846/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003847#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3848#define get_cmd_cache_nid(key) ((key) & 0xff)
3849#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3850
3851/**
3852 * snd_hda_codec_write_cache - send a single command with caching
3853 * @codec: the HDA codec
3854 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003855 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003856 * @verb: the verb to send
3857 * @parm: the parameter for the verb
3858 *
3859 * Send a single command without waiting for response.
3860 *
3861 * Returns 0 if successful, or a negative error code.
3862 */
3863int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003864 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003865{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003866 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003867 struct hda_cache_head *c;
3868 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003869 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003870
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003871 cache_only = codec->cached_write;
3872 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003873 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003874 if (err < 0)
3875 return err;
3876 }
3877
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003878 /* parm may contain the verb stuff for get/set amp */
3879 verb = verb | (parm >> 8);
3880 parm &= 0xff;
3881 key = build_cmd_cache_key(nid, verb);
3882 mutex_lock(&codec->bus->cmd_mutex);
3883 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003884 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003885 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003886 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003887 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003888 mutex_unlock(&codec->bus->cmd_mutex);
3889 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003890}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003891EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003892
Takashi Iwaid5191e52009-11-16 14:58:17 +01003893/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003894 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3895 * @codec: the HDA codec
3896 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003897 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003898 * @verb: the verb to send
3899 * @parm: the parameter for the verb
3900 *
3901 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3902 * command if the parameter is already identical with the cached value.
3903 * If not, it sends the command and refreshes the cache.
3904 *
3905 * Returns 0 if successful, or a negative error code.
3906 */
3907int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003908 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003909{
3910 struct hda_cache_head *c;
3911 u32 key;
3912
3913 /* parm may contain the verb stuff for get/set amp */
3914 verb = verb | (parm >> 8);
3915 parm &= 0xff;
3916 key = build_cmd_cache_key(nid, verb);
3917 mutex_lock(&codec->bus->cmd_mutex);
3918 c = get_hash(&codec->cmd_cache, key);
3919 if (c && c->val == parm) {
3920 mutex_unlock(&codec->bus->cmd_mutex);
3921 return 0;
3922 }
3923 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003924 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003925}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003926EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003927
3928/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003929 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3930 * @codec: HD-audio codec
3931 *
3932 * Execute all verbs recorded in the command caches to resume.
3933 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003934void snd_hda_codec_resume_cache(struct hda_codec *codec)
3935{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003936 int i;
3937
Takashi Iwaic370dd62012-12-13 18:30:04 +01003938 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003939 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003940 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3941 struct hda_cache_head *buffer;
3942 u32 key;
3943
3944 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3945 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003946 if (!key)
3947 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003948 if (!buffer->dirty)
3949 continue;
3950 buffer->dirty = 0;
3951 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003952 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3953 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003954 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003955 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003956 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003957}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003958EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003959
3960/**
3961 * snd_hda_sequence_write_cache - sequence writes with caching
3962 * @codec: the HDA codec
3963 * @seq: VERB array to send
3964 *
3965 * Send the commands sequentially from the given array.
3966 * Thte commands are recorded on cache for power-save and resume.
3967 * The array must be terminated with NID=0.
3968 */
3969void snd_hda_sequence_write_cache(struct hda_codec *codec,
3970 const struct hda_verb *seq)
3971{
3972 for (; seq->nid; seq++)
3973 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3974 seq->param);
3975}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003976EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003977
Takashi Iwaidc870f32013-01-22 15:24:30 +01003978/**
3979 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3980 * @codec: HD-audio codec
3981 */
3982void snd_hda_codec_flush_cache(struct hda_codec *codec)
3983{
3984 snd_hda_codec_resume_amp(codec);
3985 snd_hda_codec_resume_cache(codec);
3986}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003987EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003988
Takashi Iwai95a962c2014-10-29 16:03:58 +01003989/**
3990 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3991 * @codec: the HDA codec
3992 * @fg: function group (not used now)
3993 * @power_state: the power state to set (AC_PWRST_*)
3994 *
3995 * Set the given power state to all widgets that have the power control.
3996 * If the codec has power_filter set, it evaluates the power state and
3997 * filter out if it's unchanged as D3.
3998 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003999void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004000 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01004001{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004002 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004003 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01004004
Takashi Iwaicb53c622007-08-10 17:21:45 +02004005 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01004006 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004007 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004008 if (!(wcaps & AC_WCAP_POWER))
4009 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004010 if (codec->power_filter) {
4011 state = codec->power_filter(codec, nid, power_state);
4012 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004013 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02004014 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004015 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004016 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01004017 }
Takashi Iwai54d17402005-11-21 16:33:22 +01004018}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004019EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004020
4021/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004022 * supported power states check
4023 */
4024static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4025 unsigned int power_state)
4026{
4027 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4028
Mengdong Line037cb42012-08-10 14:11:58 +02004029 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004030 return false;
4031 if (sup & power_state)
4032 return true;
4033 else
4034 return false;
4035}
4036
4037/*
Takashi Iwai432c6412012-08-28 09:59:20 -07004038 * wait until the state is reached, returns the current state
4039 */
4040static unsigned int hda_sync_power_state(struct hda_codec *codec,
4041 hda_nid_t fg,
4042 unsigned int power_state)
4043{
4044 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4045 unsigned int state, actual_state;
4046
4047 for (;;) {
4048 state = snd_hda_codec_read(codec, fg, 0,
4049 AC_VERB_GET_POWER_STATE, 0);
4050 if (state & AC_PWRST_ERROR)
4051 break;
4052 actual_state = (state >> 4) & 0x0f;
4053 if (actual_state == power_state)
4054 break;
4055 if (time_after_eq(jiffies, end_time))
4056 break;
4057 /* wait until the codec reachs to the target state */
4058 msleep(1);
4059 }
4060 return state;
4061}
4062
Takashi Iwai95a962c2014-10-29 16:03:58 +01004063/**
4064 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4065 * @codec: the HDA codec
4066 * @nid: widget NID
4067 * @power_state: power state to evalue
4068 *
4069 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4070 * This can be used a codec power_filter callback.
4071 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004072unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4073 hda_nid_t nid,
4074 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004075{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004076 if (nid == codec->afg || nid == codec->mfg)
4077 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004078 if (power_state == AC_PWRST_D3 &&
4079 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4080 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4081 int eapd = snd_hda_codec_read(codec, nid, 0,
4082 AC_VERB_GET_EAPD_BTLENABLE, 0);
4083 if (eapd & 0x02)
4084 return AC_PWRST_D0;
4085 }
4086 return power_state;
4087}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004088EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004089
Takashi Iwai432c6412012-08-28 09:59:20 -07004090/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004091 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004092 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004093static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004094 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004095{
Takashi Iwaid8193872012-08-31 07:54:38 -07004096 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004097 int count;
4098 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004099 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004100
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004101 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004102 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004103 if (codec->depop_delay < 0)
4104 msleep(codec->epss ? 10 : 100);
4105 else if (codec->depop_delay > 0)
4106 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004107 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004108 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004109
4110 /* repeat power states setting at most 10 times*/
4111 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004112 if (codec->patch_ops.set_power_state)
4113 codec->patch_ops.set_power_state(codec, fg,
4114 power_state);
4115 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004116 state = power_state;
4117 if (codec->power_filter)
4118 state = codec->power_filter(codec, fg, state);
4119 if (state == power_state || power_state != AC_PWRST_D3)
4120 snd_hda_codec_read(codec, fg, flags,
4121 AC_VERB_SET_POWER_STATE,
4122 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004123 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004124 }
4125 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004126 if (!(state & AC_PWRST_ERROR))
4127 break;
4128 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004129
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004130 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004131}
Takashi Iwai54d17402005-11-21 16:33:22 +01004132
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004133/* sync power states of all widgets;
4134 * this is called at the end of codec parsing
4135 */
4136static void sync_power_up_states(struct hda_codec *codec)
4137{
4138 hda_nid_t nid = codec->start_nid;
4139 int i;
4140
Takashi Iwaiba615b82013-03-13 14:47:21 +01004141 /* don't care if no filter is used */
4142 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004143 return;
4144
4145 for (i = 0; i < codec->num_nodes; i++, nid++) {
4146 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004147 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004148 if (!(wcaps & AC_WCAP_POWER))
4149 continue;
4150 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4151 if (target == AC_PWRST_D0)
4152 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004153 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004154 snd_hda_codec_write(codec, nid, 0,
4155 AC_VERB_SET_POWER_STATE, target);
4156 }
4157}
4158
Takashi Iwai648a8d22014-02-25 10:38:13 +01004159#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004160/* execute additional init verbs */
4161static void hda_exec_init_verbs(struct hda_codec *codec)
4162{
4163 if (codec->init_verbs.list)
4164 snd_hda_sequence_write(codec, codec->init_verbs.list);
4165}
4166#else
4167static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4168#endif
4169
Takashi Iwai2a439522011-07-26 09:52:50 +02004170#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004171/*
4172 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004173 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004174 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004175static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004176{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004177 unsigned int state;
4178
Takashi Iwai989c3182012-11-19 14:14:58 +01004179 codec->in_pm = 1;
4180
Takashi Iwaicb53c622007-08-10 17:21:45 +02004181 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004182 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004183 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004184 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004185 /* Cancel delayed work if we aren't currently running from it. */
4186 if (!in_wq)
4187 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004188 spin_lock(&codec->power_lock);
4189 snd_hda_update_power_acct(codec);
4190 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004191 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004192 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004193 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004194 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004195 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004196 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004197}
4198
Takashi Iwaic370dd62012-12-13 18:30:04 +01004199/* mark all entries of cmd and amp caches dirty */
4200static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4201{
4202 int i;
4203 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4204 struct hda_cache_head *cmd;
4205 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4206 cmd->dirty = 1;
4207 }
4208 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4209 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004210 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004211 amp->head.dirty = 1;
4212 }
4213}
4214
Takashi Iwaicb53c622007-08-10 17:21:45 +02004215/*
4216 * kick up codec; used both from PM and power-save
4217 */
4218static void hda_call_codec_resume(struct hda_codec *codec)
4219{
Takashi Iwai989c3182012-11-19 14:14:58 +01004220 codec->in_pm = 1;
4221
Takashi Iwaic370dd62012-12-13 18:30:04 +01004222 hda_mark_cmd_cache_dirty(codec);
4223
Takashi Iwai7f308302012-05-08 16:52:23 +02004224 /* set as if powered on for avoiding re-entering the resume
4225 * in the resume / power-save sequence
4226 */
4227 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004228 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004229 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004230 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004231 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004232 if (codec->patch_ops.resume)
4233 codec->patch_ops.resume(codec);
4234 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004235 if (codec->patch_ops.init)
4236 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004237 snd_hda_codec_resume_amp(codec);
4238 snd_hda_codec_resume_cache(codec);
4239 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004240
4241 if (codec->jackpoll_interval)
4242 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004243 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004244 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004245
4246 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004247 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004248}
Takashi Iwai2a439522011-07-26 09:52:50 +02004249#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004250
Takashi Iwai54d17402005-11-21 16:33:22 +01004251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252/**
4253 * snd_hda_build_controls - build mixer controls
4254 * @bus: the BUS
4255 *
4256 * Creates mixer controls for each codec included in the bus.
4257 *
4258 * Returns 0 if successful, otherwise a negative error code.
4259 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004260int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004262 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Takashi Iwai0ba21762007-04-16 11:29:14 +02004264 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004265 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004266 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004267 codec_err(codec,
4268 "cannot build controls for #%d (error %d)\n",
4269 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004270 err = snd_hda_codec_reset(codec);
4271 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004272 codec_err(codec,
4273 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004274 return err;
4275 }
4276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004278 return 0;
4279}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004280EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004282/*
4283 * add standard channel maps if not specified
4284 */
4285static int add_std_chmaps(struct hda_codec *codec)
4286{
4287 int i, str, err;
4288
4289 for (i = 0; i < codec->num_pcms; i++) {
4290 for (str = 0; str < 2; str++) {
4291 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4292 struct hda_pcm_stream *hinfo =
4293 &codec->pcm_info[i].stream[str];
4294 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004295 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004296
4297 if (codec->pcm_info[i].own_chmap)
4298 continue;
4299 if (!pcm || !hinfo->substreams)
4300 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004301 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4302 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004303 hinfo->channels_max,
4304 0, &chmap);
4305 if (err < 0)
4306 return err;
4307 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4308 }
4309 }
4310 return 0;
4311}
4312
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004313/* default channel maps for 2.1 speakers;
4314 * since HD-audio supports only stereo, odd number channels are omitted
4315 */
4316const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4317 { .channels = 2,
4318 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4319 { .channels = 4,
4320 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4321 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4322 { }
4323};
4324EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4325
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004326int snd_hda_codec_build_controls(struct hda_codec *codec)
4327{
4328 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004329 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004330 /* continue to initialize... */
4331 if (codec->patch_ops.init)
4332 err = codec->patch_ops.init(codec);
4333 if (!err && codec->patch_ops.build_controls)
4334 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004335 if (err < 0)
4336 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004337
4338 /* we create chmaps here instead of build_pcms */
4339 err = add_std_chmaps(codec);
4340 if (err < 0)
4341 return err;
4342
David Henningsson26a6cb62012-10-09 15:04:21 +02004343 if (codec->jackpoll_interval)
4344 hda_jackpoll_work(&codec->jackpoll_work.work);
4345 else
4346 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004347 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return 0;
4349}
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351/*
4352 * stream formats
4353 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004354struct hda_rate_tbl {
4355 unsigned int hz;
4356 unsigned int alsa_bits;
4357 unsigned int hda_fmt;
4358};
4359
Takashi Iwai92f10b32010-08-03 14:21:00 +02004360/* rate = base * mult / div */
4361#define HDA_RATE(base, mult, div) \
4362 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4363 (((div) - 1) << AC_FMT_DIV_SHIFT))
4364
Takashi Iwaibefdf312005-08-22 13:57:55 +02004365static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004367
4368 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004369 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4370 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4371 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4372 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4373 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4374 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4375 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4376 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4377 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4378 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4379 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004380#define AC_PAR_PCM_RATE_BITS 11
4381 /* up to bits 10, 384kHZ isn't supported properly */
4382
4383 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004384 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004385
Takashi Iwaibefdf312005-08-22 13:57:55 +02004386 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387};
4388
4389/**
4390 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004391 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 * @rate: the sample rate
4393 * @channels: the number of channels
4394 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4395 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004396 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 *
4398 * Calculate the format bitset from the given rate, channels and th PCM format.
4399 *
4400 * Return zero if invalid.
4401 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004402unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4403 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 unsigned int channels,
4405 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004406 unsigned int maxbps,
4407 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
4409 int i;
4410 unsigned int val = 0;
4411
Takashi Iwaibefdf312005-08-22 13:57:55 +02004412 for (i = 0; rate_bits[i].hz; i++)
4413 if (rate_bits[i].hz == rate) {
4414 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 break;
4416 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004417 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004418 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return 0;
4420 }
4421
4422 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004423 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 return 0;
4425 }
4426 val |= channels - 1;
4427
4428 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004429 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004430 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004431 break;
4432 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004433 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 case 20:
4436 case 24:
4437 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004438 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004439 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004441 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004443 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 break;
4445 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004446 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004447 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return 0;
4449 }
4450
Anssi Hannula32c168c2010-08-03 13:28:57 +03004451 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004452 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return val;
4455}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004456EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004458static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4459 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004460{
4461 unsigned int val = 0;
4462 if (nid != codec->afg &&
4463 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4464 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4465 if (!val || val == -1)
4466 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4467 if (!val || val == -1)
4468 return 0;
4469 return val;
4470}
4471
4472static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4473{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004474 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004475 get_pcm_param);
4476}
4477
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004478static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4479 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004480{
4481 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4482 if (!streams || streams == -1)
4483 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4484 if (!streams || streams == -1)
4485 return 0;
4486 return streams;
4487}
4488
4489static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4490{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004491 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004492 get_stream_param);
4493}
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495/**
4496 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4497 * @codec: the HDA codec
4498 * @nid: NID to query
4499 * @ratesp: the pointer to store the detected rate bitflags
4500 * @formatsp: the pointer to store the detected formats
4501 * @bpsp: the pointer to store the detected format widths
4502 *
4503 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4504 * or @bsps argument is ignored.
4505 *
4506 * Returns 0 if successful, otherwise a negative error code.
4507 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004508int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4510{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004511 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004513 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004514 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 if (ratesp) {
4517 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004518 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004520 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004522 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004523 codec_err(codec,
4524 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4525 nid, val,
4526 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004527 return -EIO;
4528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 *ratesp = rates;
4530 }
4531
4532 if (formatsp || bpsp) {
4533 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004534 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004536 streams = query_stream_param(codec, nid);
4537 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 bps = 0;
4541 if (streams & AC_SUPFMT_PCM) {
4542 if (val & AC_SUPPCM_BITS_8) {
4543 formats |= SNDRV_PCM_FMTBIT_U8;
4544 bps = 8;
4545 }
4546 if (val & AC_SUPPCM_BITS_16) {
4547 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4548 bps = 16;
4549 }
4550 if (wcaps & AC_WCAP_DIGITAL) {
4551 if (val & AC_SUPPCM_BITS_32)
4552 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4553 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4554 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4555 if (val & AC_SUPPCM_BITS_24)
4556 bps = 24;
4557 else if (val & AC_SUPPCM_BITS_20)
4558 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004559 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4560 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4562 if (val & AC_SUPPCM_BITS_32)
4563 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 else if (val & AC_SUPPCM_BITS_24)
4565 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004566 else if (val & AC_SUPPCM_BITS_20)
4567 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004570#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004571 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004573 if (!bps)
4574 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004575 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004576#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004577 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004578 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 /* temporary hack: we have still no proper support
4580 * for the direct AC3 stream...
4581 */
4582 formats |= SNDRV_PCM_FMTBIT_U8;
4583 bps = 8;
4584 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004585 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004586 codec_err(codec,
4587 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4588 nid, val,
4589 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4590 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004591 return -EIO;
4592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if (formatsp)
4594 *formatsp = formats;
4595 if (bpsp)
4596 *bpsp = bps;
4597 }
4598
4599 return 0;
4600}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004601EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004604 * snd_hda_is_supported_format - Check the validity of the format
4605 * @codec: HD-audio codec
4606 * @nid: NID to check
4607 * @format: the HD-audio format value to check
4608 *
4609 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 *
4611 * Returns 1 if supported, 0 if not.
4612 */
4613int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4614 unsigned int format)
4615{
4616 int i;
4617 unsigned int val = 0, rate, stream;
4618
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004619 val = query_pcm_param(codec, nid);
4620 if (!val)
4621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
4623 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004624 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004625 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 if (val & (1 << i))
4627 break;
4628 return 0;
4629 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004630 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return 0;
4632
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004633 stream = query_stream_param(codec, nid);
4634 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return 0;
4636
4637 if (stream & AC_SUPFMT_PCM) {
4638 switch (format & 0xf0) {
4639 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004640 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return 0;
4642 break;
4643 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004644 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 return 0;
4646 break;
4647 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004648 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return 0;
4650 break;
4651 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004652 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 return 0;
4654 break;
4655 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004656 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 return 0;
4658 break;
4659 default:
4660 return 0;
4661 }
4662 } else {
4663 /* FIXME: check for float32 and AC3? */
4664 }
4665
4666 return 1;
4667}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004668EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
4670/*
4671 * PCM stuff
4672 */
4673static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4674 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004675 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
4677 return 0;
4678}
4679
4680static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4681 struct hda_codec *codec,
4682 unsigned int stream_tag,
4683 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004684 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
4686 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4687 return 0;
4688}
4689
4690static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4691 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004692 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Takashi Iwai888afa12008-03-18 09:57:50 +01004694 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return 0;
4696}
4697
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004698static int set_pcm_default_values(struct hda_codec *codec,
4699 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004701 int err;
4702
Takashi Iwai0ba21762007-04-16 11:29:14 +02004703 /* query support PCM information from the given NID */
4704 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004705 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004706 info->rates ? NULL : &info->rates,
4707 info->formats ? NULL : &info->formats,
4708 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004709 if (err < 0)
4710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 }
4712 if (info->ops.open == NULL)
4713 info->ops.open = hda_pcm_default_open_close;
4714 if (info->ops.close == NULL)
4715 info->ops.close = hda_pcm_default_open_close;
4716 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004717 if (snd_BUG_ON(!info->nid))
4718 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 info->ops.prepare = hda_pcm_default_prepare;
4720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004722 if (snd_BUG_ON(!info->nid))
4723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 info->ops.cleanup = hda_pcm_default_cleanup;
4725 }
4726 return 0;
4727}
4728
Takashi Iwaieb541332010-08-06 13:48:11 +02004729/*
4730 * codec prepare/cleanup entries
4731 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004732/**
4733 * snd_hda_codec_prepare - Prepare a stream
4734 * @codec: the HDA codec
4735 * @hinfo: PCM information
4736 * @stream: stream tag to assign
4737 * @format: format id to assign
4738 * @substream: PCM substream to assign
4739 *
4740 * Calls the prepare callback set by the codec with the given arguments.
4741 * Clean up the inactive streams when successful.
4742 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004743int snd_hda_codec_prepare(struct hda_codec *codec,
4744 struct hda_pcm_stream *hinfo,
4745 unsigned int stream,
4746 unsigned int format,
4747 struct snd_pcm_substream *substream)
4748{
4749 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004750 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004751 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4752 if (ret >= 0)
4753 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004754 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004755 return ret;
4756}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004757EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004758
Takashi Iwai95a962c2014-10-29 16:03:58 +01004759/**
4760 * snd_hda_codec_cleanup - Prepare a stream
4761 * @codec: the HDA codec
4762 * @hinfo: PCM information
4763 * @substream: PCM substream
4764 *
4765 * Calls the cleanup callback set by the codec with the given arguments.
4766 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004767void snd_hda_codec_cleanup(struct hda_codec *codec,
4768 struct hda_pcm_stream *hinfo,
4769 struct snd_pcm_substream *substream)
4770{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004771 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004772 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004773 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004774}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004775EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004776
Takashi Iwaid5191e52009-11-16 14:58:17 +01004777/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004778const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4779 "Audio", "SPDIF", "HDMI", "Modem"
4780};
4781
Takashi Iwai176d5332008-07-30 15:01:44 +02004782/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004783 * get the empty PCM device number to assign
4784 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004785static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004786{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004787 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004788 /* assigned to static slots up to dev#10; if more needed, assign
4789 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4790 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004791 static int audio_idx[HDA_PCM_NTYPES][5] = {
4792 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4793 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004794 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004795 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004796 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004797 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004798
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004799 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004800 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004801 return -EINVAL;
4802 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004803
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004804 for (i = 0; audio_idx[type][i] >= 0; i++) {
4805#ifndef CONFIG_SND_DYNAMIC_MINORS
4806 if (audio_idx[type][i] >= 8)
4807 break;
4808#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004809 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4810 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004811 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004812
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004813#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004814 /* non-fixed slots starting from 10 */
4815 for (i = 10; i < 32; i++) {
4816 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4817 return i;
4818 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004819#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004820
Takashi Iwai4e76a882014-02-25 12:21:03 +01004821 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004822 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004823#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004824 dev_warn(bus->card->dev,
4825 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004826#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004827 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004828}
4829
4830/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004831 * attach a new PCM stream
4832 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004833static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004834{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004835 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004836 struct hda_pcm_stream *info;
4837 int stream, err;
4838
Takashi Iwaib91f0802008-11-04 08:43:08 +01004839 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004840 return -EINVAL;
4841 for (stream = 0; stream < 2; stream++) {
4842 info = &pcm->stream[stream];
4843 if (info->substreams) {
4844 err = set_pcm_default_values(codec, info);
4845 if (err < 0)
4846 return err;
4847 }
4848 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004849 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004850}
4851
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004852/* assign all PCMs of the given codec */
4853int snd_hda_codec_build_pcms(struct hda_codec *codec)
4854{
4855 unsigned int pcm;
4856 int err;
4857
4858 if (!codec->num_pcms) {
4859 if (!codec->patch_ops.build_pcms)
4860 return 0;
4861 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004862 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004863 codec_err(codec,
4864 "cannot build PCMs for #%d (error %d)\n",
4865 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004866 err = snd_hda_codec_reset(codec);
4867 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004868 codec_err(codec,
4869 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004870 return err;
4871 }
4872 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004873 }
4874 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4875 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4876 int dev;
4877
4878 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004879 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004880
4881 if (!cpcm->pcm) {
4882 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4883 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004884 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004885 cpcm->device = dev;
4886 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004887 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004888 codec_err(codec,
4889 "cannot attach PCM stream %d for codec #%d\n",
4890 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004891 continue; /* no fatal error */
4892 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004893 }
4894 }
4895 return 0;
4896}
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898/**
4899 * snd_hda_build_pcms - build PCM information
4900 * @bus: the BUS
4901 *
4902 * Create PCM information for each codec included in the bus.
4903 *
4904 * The build_pcms codec patch is requested to set up codec->num_pcms and
4905 * codec->pcm_info properly. The array is referred by the top-level driver
4906 * to create its PCM instances.
4907 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4908 * callback.
4909 *
4910 * At least, substreams, channels_min and channels_max must be filled for
4911 * each stream. substreams = 0 indicates that the stream doesn't exist.
4912 * When rates and/or formats are zero, the supported values are queried
4913 * from the given nid. The nid is used also by the default ops.prepare
4914 * and ops.cleanup callbacks.
4915 *
4916 * The driver needs to call ops.open in its open callback. Similarly,
4917 * ops.close is supposed to be called in the close callback.
4918 * ops.prepare should be called in the prepare or hw_params callback
4919 * with the proper parameters for set up.
4920 * ops.cleanup should be called in hw_free for clean up of streams.
4921 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004922 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004924int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004926 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Takashi Iwai0ba21762007-04-16 11:29:14 +02004928 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004929 int err = snd_hda_codec_build_pcms(codec);
4930 if (err < 0)
4931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
4933 return 0;
4934}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004935EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 * snd_hda_add_new_ctls - create controls from the array
4939 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004940 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 *
4942 * This helper function creates and add new controls in the given array.
4943 * The array must be terminated with an empty entry as terminator.
4944 *
4945 * Returns 0 if successful, or a negative error code.
4946 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004947int snd_hda_add_new_ctls(struct hda_codec *codec,
4948 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004953 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004954 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004955 if (knew->iface == -1) /* skip this codec private value */
4956 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004957 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004958 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004959 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004960 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004961 if (addr > 0)
4962 kctl->id.device = addr;
4963 if (idx > 0)
4964 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004965 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004966 if (!err)
4967 break;
4968 /* try first with another device index corresponding to
4969 * the codec addr; if it still fails (or it's the
4970 * primary codec), then try another control index
4971 */
4972 if (!addr && codec->addr)
4973 addr = codec->addr;
4974 else if (!idx && !knew->index) {
4975 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004976 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004977 if (idx <= 0)
4978 return err;
4979 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004980 return err;
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
4983 return 0;
4984}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004985EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Takashi Iwai83012a72012-08-24 18:38:08 +02004987#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004988static void hda_power_work(struct work_struct *work)
4989{
4990 struct hda_codec *codec =
4991 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004992 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004993 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004994
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004995 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004996 if (codec->power_transition > 0) { /* during power-up sequence? */
4997 spin_unlock(&codec->power_lock);
4998 return;
4999 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02005000 if (!codec->power_on || codec->power_count) {
5001 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005002 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005003 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02005004 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005005 spin_unlock(&codec->power_lock);
5006
Dylan Reidd17344b2012-09-28 15:57:01 -07005007 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005008 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5009 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005010}
5011
5012static void hda_keep_power_on(struct hda_codec *codec)
5013{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005014 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005015 codec->power_count++;
5016 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005017 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005018 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005019 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005020}
5021
Takashi Iwaid5191e52009-11-16 14:58:17 +01005022/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005023void snd_hda_update_power_acct(struct hda_codec *codec)
5024{
5025 unsigned long delta = jiffies - codec->power_jiffies;
5026 if (codec->power_on)
5027 codec->power_on_acct += delta;
5028 else
5029 codec->power_off_acct += delta;
5030 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005031}
5032
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005033/* Transition to powered up, if wait_power_down then wait for a pending
5034 * transition to D3 to complete. A pending D3 transition is indicated
5035 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005036/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005037static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005038{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005039 /* Return if power_on or transitioning to power_on, unless currently
5040 * powering down. */
5041 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005042 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005043 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005044 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005045
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005046 cancel_delayed_work_sync(&codec->power_work);
5047
5048 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005049 /* If the power down delayed work was cancelled above before starting,
5050 * then there is no need to go through power up here.
5051 */
5052 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005053 if (codec->power_transition < 0)
5054 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005055 return;
5056 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005057
Takashi Iwaid66fee52011-08-02 15:39:31 +02005058 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005059 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005060 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005061 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005062 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005063 spin_unlock(&codec->power_lock);
5064
Takashi Iwaicb53c622007-08-10 17:21:45 +02005065 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005066
5067 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005068 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005069}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005070
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005071#define power_save(codec) \
5072 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005073
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005074/* Transition to powered down */
5075static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005076{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005077 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005078 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005079
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005080 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005081 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005082 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005083 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005084 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005085}
5086
5087/**
5088 * snd_hda_power_save - Power-up/down/sync the codec
5089 * @codec: HD-audio codec
5090 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005091 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005092 *
5093 * Change the power-up counter via @delta, and power up or down the hardware
5094 * appropriately. For the power-down, queue to the delayed action.
5095 * Passing zero to @delta means to synchronize the power state.
5096 */
5097void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5098{
5099 spin_lock(&codec->power_lock);
5100 codec->power_count += delta;
5101 trace_hda_power_count(codec);
5102 if (delta > 0)
5103 __snd_hda_power_up(codec, d3wait);
5104 else
5105 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005106 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005107}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005108EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005109
Takashi Iwaid5191e52009-11-16 14:58:17 +01005110/**
5111 * snd_hda_check_amp_list_power - Check the amp list and update the power
5112 * @codec: HD-audio codec
5113 * @check: the object containing an AMP list and the status
5114 * @nid: NID to check / update
5115 *
5116 * Check whether the given NID is in the amp list. If it's in the list,
5117 * check the current AMP status, and update the the power-status according
5118 * to the mute status.
5119 *
5120 * This function is supposed to be set or called from the check_power_status
5121 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005122 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005123int snd_hda_check_amp_list_power(struct hda_codec *codec,
5124 struct hda_loopback_check *check,
5125 hda_nid_t nid)
5126{
Takashi Iwai031024e2011-05-02 11:29:30 +02005127 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005128 int ch, v;
5129
5130 if (!check->amplist)
5131 return 0;
5132 for (p = check->amplist; p->nid; p++) {
5133 if (p->nid == nid)
5134 break;
5135 }
5136 if (!p->nid)
5137 return 0; /* nothing changed */
5138
5139 for (p = check->amplist; p->nid; p++) {
5140 for (ch = 0; ch < 2; ch++) {
5141 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5142 p->idx);
5143 if (!(v & HDA_AMP_MUTE) && v > 0) {
5144 if (!check->power_on) {
5145 check->power_on = 1;
5146 snd_hda_power_up(codec);
5147 }
5148 return 1;
5149 }
5150 }
5151 }
5152 if (check->power_on) {
5153 check->power_on = 0;
5154 snd_hda_power_down(codec);
5155 }
5156 return 0;
5157}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005158EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005161/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005162 * Channel mode helper
5163 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164
5165/**
5166 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005167 * @codec: the HDA codec
5168 * @uinfo: pointer to get/store the data
5169 * @chmode: channel mode array
5170 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005172int snd_hda_ch_mode_info(struct hda_codec *codec,
5173 struct snd_ctl_elem_info *uinfo,
5174 const struct hda_channel_mode *chmode,
5175 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005176{
5177 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5178 uinfo->count = 1;
5179 uinfo->value.enumerated.items = num_chmodes;
5180 if (uinfo->value.enumerated.item >= num_chmodes)
5181 uinfo->value.enumerated.item = num_chmodes - 1;
5182 sprintf(uinfo->value.enumerated.name, "%dch",
5183 chmode[uinfo->value.enumerated.item].channels);
5184 return 0;
5185}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005186EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005187
Takashi Iwaid5191e52009-11-16 14:58:17 +01005188/**
5189 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005190 * @codec: the HDA codec
5191 * @ucontrol: pointer to get/store the data
5192 * @chmode: channel mode array
5193 * @num_chmodes: channel mode array size
5194 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005195 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005196int snd_hda_ch_mode_get(struct hda_codec *codec,
5197 struct snd_ctl_elem_value *ucontrol,
5198 const struct hda_channel_mode *chmode,
5199 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005200 int max_channels)
5201{
5202 int i;
5203
5204 for (i = 0; i < num_chmodes; i++) {
5205 if (max_channels == chmode[i].channels) {
5206 ucontrol->value.enumerated.item[0] = i;
5207 break;
5208 }
5209 }
5210 return 0;
5211}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005212EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005213
Takashi Iwaid5191e52009-11-16 14:58:17 +01005214/**
5215 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005216 * @codec: the HDA codec
5217 * @ucontrol: pointer to get/store the data
5218 * @chmode: channel mode array
5219 * @num_chmodes: channel mode array size
5220 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005221 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005222int snd_hda_ch_mode_put(struct hda_codec *codec,
5223 struct snd_ctl_elem_value *ucontrol,
5224 const struct hda_channel_mode *chmode,
5225 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005226 int *max_channelsp)
5227{
5228 unsigned int mode;
5229
5230 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005231 if (mode >= num_chmodes)
5232 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005233 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005234 return 0;
5235 /* change the current channel setting */
5236 *max_channelsp = chmode[mode].channels;
5237 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005238 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005239 return 1;
5240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005241EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243/*
5244 * input MUX helper
5245 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005246
5247/**
5248 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005249 * @imux: imux helper object
5250 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005252int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5253 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
5255 unsigned int index;
5256
5257 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5258 uinfo->count = 1;
5259 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005260 if (!imux->num_items)
5261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 index = uinfo->value.enumerated.item;
5263 if (index >= imux->num_items)
5264 index = imux->num_items - 1;
5265 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5266 return 0;
5267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005268EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Takashi Iwaid5191e52009-11-16 14:58:17 +01005270/**
5271 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005272 * @codec: the HDA codec
5273 * @imux: imux helper object
5274 * @ucontrol: pointer to get/store the data
5275 * @nid: input mux NID
5276 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005277 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005278int snd_hda_input_mux_put(struct hda_codec *codec,
5279 const struct hda_input_mux *imux,
5280 struct snd_ctl_elem_value *ucontrol,
5281 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 unsigned int *cur_val)
5283{
5284 unsigned int idx;
5285
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005286 if (!imux->num_items)
5287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 idx = ucontrol->value.enumerated.item[0];
5289 if (idx >= imux->num_items)
5290 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005291 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005293 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5294 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 *cur_val = idx;
5296 return 1;
5297}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005298EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005301/**
5302 * snd_hda_enum_helper_info - Helper for simple enum ctls
5303 * @kcontrol: ctl element
5304 * @uinfo: pointer to get/store the data
5305 * @num_items: number of enum items
5306 * @texts: enum item string array
5307 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005308 * process kcontrol info callback of a simple string enum array
5309 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5310 */
5311int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5312 struct snd_ctl_elem_info *uinfo,
5313 int num_items, const char * const *texts)
5314{
5315 static const char * const texts_default[] = {
5316 "Disabled", "Enabled"
5317 };
5318
5319 if (!texts || !num_items) {
5320 num_items = 2;
5321 texts = texts_default;
5322 }
5323
Takashi Iwai3ff72212014-10-20 18:17:28 +02005324 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005325}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005326EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005327
5328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 * Multi-channel / digital-out PCM helper functions
5330 */
5331
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005332/* setup SPDIF output stream */
5333static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5334 unsigned int stream_tag, unsigned int format)
5335{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005336 struct hda_spdif_out *spdif;
5337 unsigned int curr_fmt;
5338 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005339
Laurence Darby3bef1c32012-11-03 17:00:06 +00005340 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5341 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5342 AC_VERB_GET_STREAM_FORMAT, 0);
5343 reset = codec->spdif_status_reset &&
5344 (spdif->ctls & AC_DIG1_ENABLE) &&
5345 curr_fmt != format;
5346
5347 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5348 updated */
5349 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005350 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005351 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005352 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005353 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005354 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005355 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005356 for (d = codec->slave_dig_outs; *d; d++)
5357 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5358 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005359 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005360 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005361 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005362 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005363 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005364}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005365
Takashi Iwai2f728532008-09-25 16:32:41 +02005366static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5367{
5368 snd_hda_codec_cleanup_stream(codec, nid);
5369 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005370 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005371 for (d = codec->slave_dig_outs; *d; d++)
5372 snd_hda_codec_cleanup_stream(codec, *d);
5373 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005374}
5375
Takashi Iwaid5191e52009-11-16 14:58:17 +01005376/**
5377 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5378 * @bus: HD-audio bus
5379 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005380void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5381{
5382 struct hda_codec *codec;
5383
5384 if (!bus)
5385 return;
5386 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005387 if (hda_codec_is_power_on(codec) &&
5388 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005389 codec->patch_ops.reboot_notify(codec);
5390 }
5391}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005392EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005393
Takashi Iwaid5191e52009-11-16 14:58:17 +01005394/**
5395 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005396 * @codec: the HDA codec
5397 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005399int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5400 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Ingo Molnar62932df2006-01-16 16:34:20 +01005402 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005403 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5404 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005405 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005407 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return 0;
5409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005410EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Takashi Iwaid5191e52009-11-16 14:58:17 +01005412/**
5413 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005414 * @codec: the HDA codec
5415 * @mout: hda_multi_out object
5416 * @stream_tag: stream tag to assign
5417 * @format: format id to assign
5418 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005419 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005420int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5421 struct hda_multi_out *mout,
5422 unsigned int stream_tag,
5423 unsigned int format,
5424 struct snd_pcm_substream *substream)
5425{
5426 mutex_lock(&codec->spdif_mutex);
5427 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5428 mutex_unlock(&codec->spdif_mutex);
5429 return 0;
5430}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005431EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005432
Takashi Iwaid5191e52009-11-16 14:58:17 +01005433/**
5434 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005435 * @codec: the HDA codec
5436 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005437 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005438int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5439 struct hda_multi_out *mout)
5440{
5441 mutex_lock(&codec->spdif_mutex);
5442 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5443 mutex_unlock(&codec->spdif_mutex);
5444 return 0;
5445}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005446EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005447
Takashi Iwaid5191e52009-11-16 14:58:17 +01005448/**
5449 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005450 * @codec: the HDA codec
5451 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005453int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5454 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
Ingo Molnar62932df2006-01-16 16:34:20 +01005456 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005458 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 return 0;
5460}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005461EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Takashi Iwaid5191e52009-11-16 14:58:17 +01005463/**
5464 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005465 * @codec: the HDA codec
5466 * @mout: hda_multi_out object
5467 * @substream: PCM substream to assign
5468 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005469 *
5470 * Open analog outputs and set up the hw-constraints.
5471 * If the digital outputs can be opened as slave, open the digital
5472 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005474int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5475 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005476 struct snd_pcm_substream *substream,
5477 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478{
Takashi Iwai9a081602008-02-12 18:37:26 +01005479 struct snd_pcm_runtime *runtime = substream->runtime;
5480 runtime->hw.channels_max = mout->max_channels;
5481 if (mout->dig_out_nid) {
5482 if (!mout->analog_rates) {
5483 mout->analog_rates = hinfo->rates;
5484 mout->analog_formats = hinfo->formats;
5485 mout->analog_maxbps = hinfo->maxbps;
5486 } else {
5487 runtime->hw.rates = mout->analog_rates;
5488 runtime->hw.formats = mout->analog_formats;
5489 hinfo->maxbps = mout->analog_maxbps;
5490 }
5491 if (!mout->spdif_rates) {
5492 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5493 &mout->spdif_rates,
5494 &mout->spdif_formats,
5495 &mout->spdif_maxbps);
5496 }
5497 mutex_lock(&codec->spdif_mutex);
5498 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005499 if ((runtime->hw.rates & mout->spdif_rates) &&
5500 (runtime->hw.formats & mout->spdif_formats)) {
5501 runtime->hw.rates &= mout->spdif_rates;
5502 runtime->hw.formats &= mout->spdif_formats;
5503 if (mout->spdif_maxbps < hinfo->maxbps)
5504 hinfo->maxbps = mout->spdif_maxbps;
5505 } else {
5506 mout->share_spdif = 0;
5507 /* FIXME: need notify? */
5508 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005509 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005510 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5513 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5514}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005515EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Takashi Iwaid5191e52009-11-16 14:58:17 +01005517/**
5518 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005519 * @codec: the HDA codec
5520 * @mout: hda_multi_out object
5521 * @stream_tag: stream tag to assign
5522 * @format: format id to assign
5523 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005524 *
5525 * Set up the i/o for analog out.
5526 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005528int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5529 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 unsigned int stream_tag,
5531 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005532 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Takashi Iwaidda14412011-05-02 11:29:30 +02005534 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005536 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 int i;
5538
Ingo Molnar62932df2006-01-16 16:34:20 +01005539 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005540 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005541 if (mout->dig_out_nid && mout->share_spdif &&
5542 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005544 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5545 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005546 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005548 setup_dig_out_stream(codec, mout->dig_out_nid,
5549 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 } else {
5551 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005552 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
5554 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005555 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005558 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5559 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005560 if (!mout->no_share_stream &&
5561 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005563 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5564 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005565 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005566 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5567 if (!mout->no_share_stream && mout->hp_out_nid[i])
5568 snd_hda_codec_setup_stream(codec,
5569 mout->hp_out_nid[i],
5570 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 /* surrounds */
5573 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005574 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005575 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5576 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005577 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005578 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5579 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005581
5582 /* extra surrounds */
5583 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5584 int ch = 0;
5585 if (!mout->extra_out_nid[i])
5586 break;
5587 if (chs >= (i + 1) * 2)
5588 ch = i * 2;
5589 else if (!mout->no_share_stream)
5590 break;
5591 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5592 stream_tag, ch, format);
5593 }
5594
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 return 0;
5596}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005597EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
Takashi Iwaid5191e52009-11-16 14:58:17 +01005599/**
5600 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005601 * @codec: the HDA codec
5602 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005604int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5605 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
Takashi Iwaidda14412011-05-02 11:29:30 +02005607 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 int i;
5609
5610 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005611 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005613 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005614 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5615 if (mout->hp_out_nid[i])
5616 snd_hda_codec_cleanup_stream(codec,
5617 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005618 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5619 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005620 snd_hda_codec_cleanup_stream(codec,
5621 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005622 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005624 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 mout->dig_out_used = 0;
5626 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005627 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 return 0;
5629}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005630EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Takashi Iwai47408602012-04-20 13:06:53 +02005632/**
5633 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005634 * @codec: the HDA codec
5635 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005636 *
5637 * Guess the suitable VREF pin bits to be set as the pin-control value.
5638 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5639 */
5640unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5641{
5642 unsigned int pincap;
5643 unsigned int oldval;
5644 oldval = snd_hda_codec_read(codec, pin, 0,
5645 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5646 pincap = snd_hda_query_pin_caps(codec, pin);
5647 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5648 /* Exception: if the default pin setup is vref50, we give it priority */
5649 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5650 return AC_PINCTL_VREF_80;
5651 else if (pincap & AC_PINCAP_VREF_50)
5652 return AC_PINCTL_VREF_50;
5653 else if (pincap & AC_PINCAP_VREF_100)
5654 return AC_PINCTL_VREF_100;
5655 else if (pincap & AC_PINCAP_VREF_GRD)
5656 return AC_PINCTL_VREF_GRD;
5657 return AC_PINCTL_VREF_HIZ;
5658}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005659EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005660
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005661/**
5662 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5663 * @codec: the HDA codec
5664 * @pin: referred pin NID
5665 * @val: pin ctl value to audit
5666 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005667unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5668 hda_nid_t pin, unsigned int val)
5669{
5670 static unsigned int cap_lists[][2] = {
5671 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5672 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5673 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5674 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5675 };
5676 unsigned int cap;
5677
5678 if (!val)
5679 return 0;
5680 cap = snd_hda_query_pin_caps(codec, pin);
5681 if (!cap)
5682 return val; /* don't know what to do... */
5683
5684 if (val & AC_PINCTL_OUT_EN) {
5685 if (!(cap & AC_PINCAP_OUT))
5686 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5687 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5688 val &= ~AC_PINCTL_HP_EN;
5689 }
5690
5691 if (val & AC_PINCTL_IN_EN) {
5692 if (!(cap & AC_PINCAP_IN))
5693 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5694 else {
5695 unsigned int vcap, vref;
5696 int i;
5697 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5698 vref = val & AC_PINCTL_VREFEN;
5699 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5700 if (vref == cap_lists[i][0] &&
5701 !(vcap & cap_lists[i][1])) {
5702 if (i == ARRAY_SIZE(cap_lists) - 1)
5703 vref = AC_PINCTL_VREF_HIZ;
5704 else
5705 vref = cap_lists[i + 1][0];
5706 }
5707 }
5708 val &= ~AC_PINCTL_VREFEN;
5709 val |= vref;
5710 }
5711 }
5712
5713 return val;
5714}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005715EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005716
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005717/**
5718 * _snd_hda_pin_ctl - Helper to set pin ctl value
5719 * @codec: the HDA codec
5720 * @pin: referred pin NID
5721 * @val: pin control value to set
5722 * @cached: access over codec pinctl cache or direct write
5723 *
5724 * This function is a helper to set a pin ctl value more safely.
5725 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5726 * value in pin target array via snd_hda_codec_set_pin_target(), then
5727 * actually writes the value via either snd_hda_codec_update_cache() or
5728 * snd_hda_codec_write() depending on @cached flag.
5729 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005730int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5731 unsigned int val, bool cached)
5732{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005733 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005734 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005735 if (cached)
5736 return snd_hda_codec_update_cache(codec, pin, 0,
5737 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5738 else
5739 return snd_hda_codec_write(codec, pin, 0,
5740 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5741}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005742EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005743
Takashi Iwai990061c2010-09-09 22:08:44 +02005744/**
5745 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005746 * @codec: the HDA codec
5747 * @imux: imux helper object
5748 * @label: the name of imux item to assign
5749 * @index: index number of imux item to assign
5750 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005751 *
5752 * When the same label is used already in the existing items, the number
5753 * suffix is appended to the label. This label index number is stored
5754 * to type_idx when non-NULL pointer is given.
5755 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005756int snd_hda_add_imux_item(struct hda_codec *codec,
5757 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005758 int index, int *type_idx)
5759{
5760 int i, label_idx = 0;
5761 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005762 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005763 return -EINVAL;
5764 }
5765 for (i = 0; i < imux->num_items; i++) {
5766 if (!strncmp(label, imux->items[i].label, strlen(label)))
5767 label_idx++;
5768 }
5769 if (type_idx)
5770 *type_idx = label_idx;
5771 if (label_idx > 0)
5772 snprintf(imux->items[imux->num_items].label,
5773 sizeof(imux->items[imux->num_items].label),
5774 "%s %d", label, label_idx);
5775 else
5776 strlcpy(imux->items[imux->num_items].label, label,
5777 sizeof(imux->items[imux->num_items].label));
5778 imux->items[imux->num_items].index = index;
5779 imux->num_items++;
5780 return 0;
5781}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005782EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005783
Takashi Iwai4a471b72005-12-07 13:56:29 +01005784
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785#ifdef CONFIG_PM
5786/*
5787 * power management
5788 */
5789
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005790
5791static void hda_async_suspend(void *data, async_cookie_t cookie)
5792{
5793 hda_call_codec_suspend(data, false);
5794}
5795
5796static void hda_async_resume(void *data, async_cookie_t cookie)
5797{
5798 hda_call_codec_resume(data);
5799}
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801/**
5802 * snd_hda_suspend - suspend the codecs
5803 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 *
5805 * Returns 0 if successful.
5806 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005807int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005809 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005810 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Takashi Iwai0ba21762007-04-16 11:29:14 +02005812 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005813 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005814 if (hda_codec_is_power_on(codec)) {
5815 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005816 async_schedule_domain(hda_async_suspend, codec,
5817 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005818 else
5819 hda_call_codec_suspend(codec, false);
5820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005822
5823 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005824 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 return 0;
5827}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005828EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
5830/**
5831 * snd_hda_resume - resume the codecs
5832 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 *
5834 * Returns 0 if successful.
5835 */
5836int snd_hda_resume(struct hda_bus *bus)
5837{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005838 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005839 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Takashi Iwai0ba21762007-04-16 11:29:14 +02005841 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005842 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005843 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005844 else
5845 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005847
5848 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005849 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return 0;
5852}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005853EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005854#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005855
5856/*
5857 * generic arrays
5858 */
5859
Takashi Iwaid5191e52009-11-16 14:58:17 +01005860/**
5861 * snd_array_new - get a new element from the given array
5862 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005863 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005864 * Get a new element from the given array. If it exceeds the
5865 * pre-allocated array size, re-allocate the array.
5866 *
5867 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005868 */
5869void *snd_array_new(struct snd_array *array)
5870{
Takashi Iwai12f17712012-10-10 08:59:14 +02005871 if (snd_BUG_ON(!array->elem_size))
5872 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005873 if (array->used >= array->alloced) {
5874 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005875 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005876 void *nlist;
5877 if (snd_BUG_ON(num >= 4096))
5878 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005879 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005880 if (!nlist)
5881 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005882 array->list = nlist;
5883 array->alloced = num;
5884 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005885 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005887EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005888
Takashi Iwaid5191e52009-11-16 14:58:17 +01005889/**
5890 * snd_array_free - free the given array elements
5891 * @array: the array object
5892 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005893void snd_array_free(struct snd_array *array)
5894{
5895 kfree(array->list);
5896 array->used = 0;
5897 array->alloced = 0;
5898 array->list = NULL;
5899}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005900EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005901
Takashi Iwaid5191e52009-11-16 14:58:17 +01005902/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005903 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5904 * @pcm: PCM caps bits
5905 * @buf: the string buffer to write
5906 * @buflen: the max buffer length
5907 *
5908 * used by hda_proc.c and hda_eld.c
5909 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005910void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5911{
5912 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5913 int i, j;
5914
5915 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5916 if (pcm & (AC_SUPPCM_BITS_8 << i))
5917 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5918
5919 buf[j] = '\0'; /* necessary when j == 0 */
5920}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005921EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005922
5923MODULE_DESCRIPTION("HDA codec core");
5924MODULE_LICENSE("GPL");