blob: 5e755eb6f19a0b79e1969af24eb62dd3f34902a8 [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,
Takashi Iwai781c7b92015-02-20 10:26:25 +01002151 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01002152{
2153 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002154 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002155
2156 if (snd_BUG_ON(mask & ~0xff))
2157 mask &= 0xff;
2158 val &= mask;
2159
2160 mutex_lock(&codec->hash_mutex);
2161 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2162 if (!info) {
2163 mutex_unlock(&codec->hash_mutex);
2164 return 0;
2165 }
2166 val |= info->vol[ch] & ~mask;
2167 if (info->vol[ch] == val) {
2168 mutex_unlock(&codec->hash_mutex);
2169 return 0;
2170 }
2171 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002172 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002173 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002174 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002175 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002176 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002177 return 1;
2178}
2179
Takashi Iwaid5191e52009-11-16 14:58:17 +01002180/**
2181 * snd_hda_codec_amp_update - update the AMP value
2182 * @codec: HD-audio codec
2183 * @nid: NID to read the AMP value
2184 * @ch: channel (left=0 or right=1)
2185 * @direction: #HDA_INPUT or #HDA_OUTPUT
2186 * @idx: the index value (only for input direction)
2187 * @mask: bit mask to set
2188 * @val: the bits value to set
2189 *
2190 * Update the AMP value with a bit mask.
2191 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002192 */
Takashi Iwai834be882006-03-01 14:16:17 +01002193int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2194 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Takashi Iwai781c7b92015-02-20 10:26:25 +01002196 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
2197 false, codec->cached_write);
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{
Takashi Iwai781c7b92015-02-20 10:26:25 +01002244 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
2245 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002246}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002247EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002248
Takashi Iwai95a962c2014-10-29 16:03:58 +01002249/**
2250 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2251 * @codec: the HDA codec
2252 * @nid: NID to read the AMP value
2253 * @dir: #HDA_INPUT or #HDA_OUTPUT
2254 * @idx: the index value (only for input direction)
2255 * @mask: bit mask to set
2256 * @val: the bits value to set
2257 *
2258 * Call snd_hda_codec_amp_init() for both stereo channels.
2259 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002260int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2261 int dir, int idx, int mask, int val)
2262{
2263 int ch, ret = 0;
2264
2265 if (snd_BUG_ON(mask & ~0xff))
2266 mask &= 0xff;
2267 for (ch = 0; ch < 2; ch++)
2268 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2269 idx, mask, val);
2270 return ret;
2271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002272EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002273
Takashi Iwaid5191e52009-11-16 14:58:17 +01002274/**
2275 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2276 * @codec: HD-audio codec
2277 *
2278 * Resume the all amp commands from the cache.
2279 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002280void snd_hda_codec_resume_amp(struct hda_codec *codec)
2281{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002282 int i;
2283
Takashi Iwaic370dd62012-12-13 18:30:04 +01002284 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002285 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002286 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2287 struct hda_amp_info *buffer;
2288 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002289 hda_nid_t nid;
2290 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002291 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002292
2293 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002294 if (!buffer->head.dirty)
2295 continue;
2296 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002297 info = *buffer;
2298 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002299 if (!key)
2300 continue;
2301 nid = key & 0xff;
2302 idx = (key >> 16) & 0xff;
2303 dir = (key >> 24) & 0xff;
2304 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002305 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002306 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002307 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002308 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2309 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002310 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002311 }
2312 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002313 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002314}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002315EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002317static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2318 unsigned int ofs)
2319{
2320 u32 caps = query_amp_caps(codec, nid, dir);
2321 /* get num steps */
2322 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2323 if (ofs < caps)
2324 caps -= ofs;
2325 return caps;
2326}
2327
Takashi Iwaid5191e52009-11-16 14:58:17 +01002328/**
2329 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002330 * @kcontrol: referred ctl element
2331 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002332 *
2333 * The control element is supposed to have the private_value field
2334 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2335 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002336int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2340 u16 nid = get_amp_nid(kcontrol);
2341 u8 chs = get_amp_channels(kcontrol);
2342 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002343 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002345 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2346 uinfo->count = chs == 3 ? 2 : 1;
2347 uinfo->value.integer.min = 0;
2348 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2349 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002350 codec_warn(codec,
2351 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2352 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return -EINVAL;
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return 0;
2356}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002357EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002359
2360static inline unsigned int
2361read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2362 int ch, int dir, int idx, unsigned int ofs)
2363{
2364 unsigned int val;
2365 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2366 val &= HDA_AMP_VOLMASK;
2367 if (val >= ofs)
2368 val -= ofs;
2369 else
2370 val = 0;
2371 return val;
2372}
2373
2374static inline int
2375update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2376 int ch, int dir, int idx, unsigned int ofs,
2377 unsigned int val)
2378{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002379 unsigned int maxval;
2380
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002381 if (val > 0)
2382 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002383 /* ofs = 0: raw max value */
2384 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002385 if (val > maxval)
2386 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002387 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2388 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002389}
2390
Takashi Iwaid5191e52009-11-16 14:58:17 +01002391/**
2392 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002393 * @kcontrol: ctl element
2394 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002395 *
2396 * The control element is supposed to have the private_value field
2397 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2398 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002399int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2403 hda_nid_t nid = get_amp_nid(kcontrol);
2404 int chs = get_amp_channels(kcontrol);
2405 int dir = get_amp_direction(kcontrol);
2406 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002407 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 long *valp = ucontrol->value.integer.value;
2409
2410 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002411 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002413 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return 0;
2415}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002416EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Takashi Iwaid5191e52009-11-16 14:58:17 +01002418/**
2419 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002420 * @kcontrol: ctl element
2421 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002422 *
2423 * The control element is supposed to have the private_value field
2424 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2425 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002426int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2430 hda_nid_t nid = get_amp_nid(kcontrol);
2431 int chs = get_amp_channels(kcontrol);
2432 int dir = get_amp_direction(kcontrol);
2433 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002434 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 long *valp = ucontrol->value.integer.value;
2436 int change = 0;
2437
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return change;
2445}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002446EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Takashi Iwaid5191e52009-11-16 14:58:17 +01002448/**
2449 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002450 * @kcontrol: ctl element
2451 * @op_flag: operation flag
2452 * @size: byte size of input TLV
2453 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002454 *
2455 * The control element is supposed to have the private_value field
2456 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2457 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002458int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2459 unsigned int size, unsigned int __user *_tlv)
2460{
2461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2462 hda_nid_t nid = get_amp_nid(kcontrol);
2463 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002464 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002465 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002466 u32 caps, val1, val2;
2467
2468 if (size < 4 * sizeof(unsigned int))
2469 return -ENOMEM;
2470 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002471 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2472 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002473 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002474 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002475 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002476 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002477 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002478 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2479 return -EFAULT;
2480 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2481 return -EFAULT;
2482 if (put_user(val1, _tlv + 2))
2483 return -EFAULT;
2484 if (put_user(val2, _tlv + 3))
2485 return -EFAULT;
2486 return 0;
2487}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002488EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002489
Takashi Iwaid5191e52009-11-16 14:58:17 +01002490/**
2491 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2492 * @codec: HD-audio codec
2493 * @nid: NID of a reference widget
2494 * @dir: #HDA_INPUT or #HDA_OUTPUT
2495 * @tlv: TLV data to be stored, at least 4 elements
2496 *
2497 * Set (static) TLV data for a virtual master volume using the AMP caps
2498 * obtained from the reference NID.
2499 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002500 */
2501void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2502 unsigned int *tlv)
2503{
2504 u32 caps;
2505 int nums, step;
2506
2507 caps = query_amp_caps(codec, nid, dir);
2508 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2509 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2510 step = (step + 1) * 25;
2511 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2512 tlv[1] = 2 * sizeof(unsigned int);
2513 tlv[2] = -nums * step;
2514 tlv[3] = step;
2515}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002516EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002517
2518/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002519static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002520find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002521{
2522 struct snd_ctl_elem_id id;
2523 memset(&id, 0, sizeof(id));
2524 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002525 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002526 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002527 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2528 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002529 strcpy(id.name, name);
2530 return snd_ctl_find_id(codec->bus->card, &id);
2531}
2532
Takashi Iwaid5191e52009-11-16 14:58:17 +01002533/**
2534 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2535 * @codec: HD-audio codec
2536 * @name: ctl id name string
2537 *
2538 * Get the control element with the given id string and IFACE_MIXER.
2539 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002540struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2541 const char *name)
2542{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002543 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002544}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002545EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002546
Takashi Iwaidcda5802012-10-12 17:24:51 +02002547static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002548 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002549{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002550 int i, idx;
2551 /* 16 ctlrs should be large enough */
2552 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2553 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002554 return idx;
2555 }
2556 return -EBUSY;
2557}
2558
Takashi Iwaid5191e52009-11-16 14:58:17 +01002559/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002560 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002561 * @codec: HD-audio codec
2562 * @nid: corresponding NID (optional)
2563 * @kctl: the control element to assign
2564 *
2565 * Add the given control element to an array inside the codec instance.
2566 * All control elements belonging to a codec are supposed to be added
2567 * by this function so that a proper clean-up works at the free or
2568 * reconfiguration time.
2569 *
2570 * If non-zero @nid is passed, the NID is assigned to the control element.
2571 * The assignment is shown in the codec proc file.
2572 *
2573 * snd_hda_ctl_add() checks the control subdev id field whether
2574 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002575 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2576 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002577 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002578int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2579 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002580{
2581 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002582 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002583 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002584
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002585 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002586 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002587 if (nid == 0)
2588 nid = get_amp_nid_(kctl->private_value);
2589 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002590 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2591 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002592 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002593 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002594 err = snd_ctl_add(codec->bus->card, kctl);
2595 if (err < 0)
2596 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002597 item = snd_array_new(&codec->mixers);
2598 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002599 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002600 item->kctl = kctl;
2601 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002602 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002603 return 0;
2604}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002605EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002606
Takashi Iwaid5191e52009-11-16 14:58:17 +01002607/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002608 * snd_hda_add_nid - Assign a NID to a control element
2609 * @codec: HD-audio codec
2610 * @nid: corresponding NID (optional)
2611 * @kctl: the control element to assign
2612 * @index: index to kctl
2613 *
2614 * Add the given control element to an array inside the codec instance.
2615 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2616 * NID:KCTL mapping - for example "Capture Source" selector.
2617 */
2618int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2619 unsigned int index, hda_nid_t nid)
2620{
2621 struct hda_nid_item *item;
2622
2623 if (nid > 0) {
2624 item = snd_array_new(&codec->nids);
2625 if (!item)
2626 return -ENOMEM;
2627 item->kctl = kctl;
2628 item->index = index;
2629 item->nid = nid;
2630 return 0;
2631 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002632 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2633 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002634 return -EINVAL;
2635}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002636EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002637
2638/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002639 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2640 * @codec: HD-audio codec
2641 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002642void snd_hda_ctls_clear(struct hda_codec *codec)
2643{
2644 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002645 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002646 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002647 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002648 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002649 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002650}
2651
Takashi Iwai95a962c2014-10-29 16:03:58 +01002652/**
2653 * snd_hda_lock_devices - pseudo device locking
2654 * @bus: the BUS
2655 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002656 * toggle card->shutdown to allow/disallow the device access (as a hack)
2657 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002658int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002659{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002660 struct snd_card *card = bus->card;
2661 struct hda_codec *codec;
2662
Takashi Iwaia65d6292009-02-23 16:57:04 +01002663 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002664 if (card->shutdown)
2665 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002666 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002667 if (!list_empty(&card->ctl_files))
2668 goto err_clear;
2669
2670 list_for_each_entry(codec, &bus->codec_list, list) {
2671 int pcm;
2672 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2673 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2674 if (!cpcm->pcm)
2675 continue;
2676 if (cpcm->pcm->streams[0].substream_opened ||
2677 cpcm->pcm->streams[1].substream_opened)
2678 goto err_clear;
2679 }
2680 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002681 spin_unlock(&card->files_lock);
2682 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002683
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002684 err_clear:
2685 card->shutdown = 0;
2686 err_unlock:
2687 spin_unlock(&card->files_lock);
2688 return -EINVAL;
2689}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002690EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002691
Takashi Iwai95a962c2014-10-29 16:03:58 +01002692/**
2693 * snd_hda_unlock_devices - pseudo device unlocking
2694 * @bus: the BUS
2695 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002696void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002697{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002698 struct snd_card *card = bus->card;
2699
2700 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002701 spin_lock(&card->files_lock);
2702 card->shutdown = 0;
2703 spin_unlock(&card->files_lock);
2704}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002705EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002706
Takashi Iwaid5191e52009-11-16 14:58:17 +01002707/**
2708 * snd_hda_codec_reset - Clear all objects assigned to the codec
2709 * @codec: HD-audio codec
2710 *
2711 * This frees the all PCM and control elements assigned to the codec, and
2712 * clears the caches and restores the pin default configurations.
2713 *
2714 * When a device is being used, it returns -EBSY. If successfully freed,
2715 * returns zero.
2716 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002717int snd_hda_codec_reset(struct hda_codec *codec)
2718{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002719 struct hda_bus *bus = codec->bus;
2720 struct snd_card *card = bus->card;
2721 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002722
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002723 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002724 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002725
2726 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002727 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002728#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002729 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002730 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002731#endif
2732 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002733 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002734 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002735 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002736 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002737 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002738 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002739 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002740 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002741 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002742 if (codec->patch_ops.free)
2743 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002744 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002745 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002746 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002747 codec->spec = NULL;
2748 free_hda_cache(&codec->amp_cache);
2749 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002750 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2751 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002752 /* free only driver_pins so that init_pins + user_pins are restored */
2753 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002754 snd_array_free(&codec->cvt_setups);
2755 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002756 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002757 codec->num_pcms = 0;
2758 codec->pcm_info = NULL;
2759 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002760 codec->slave_dig_outs = NULL;
2761 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002762 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002763 module_put(codec->owner);
2764 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002765
2766 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002767 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002768 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002769}
2770
Takashi Iwai6194b992014-06-06 18:12:16 +02002771typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002772
2773/* apply the function to all matching slave ctls in the mixer list */
2774static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002775 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002776{
2777 struct hda_nid_item *items;
2778 const char * const *s;
2779 int i, err;
2780
2781 items = codec->mixers.list;
2782 for (i = 0; i < codec->mixers.used; i++) {
2783 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002784 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002785 continue;
2786 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002787 char tmpname[sizeof(sctl->id.name)];
2788 const char *name = *s;
2789 if (suffix) {
2790 snprintf(tmpname, sizeof(tmpname), "%s %s",
2791 name, suffix);
2792 name = tmpname;
2793 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002794 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002795 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002796 if (err)
2797 return err;
2798 break;
2799 }
2800 }
2801 }
2802 return 0;
2803}
2804
Takashi Iwai6194b992014-06-06 18:12:16 +02002805static int check_slave_present(struct hda_codec *codec,
2806 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002807{
2808 return 1;
2809}
2810
Takashi Iwai18478e82012-03-09 17:51:10 +01002811/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002812static int get_kctl_0dB_offset(struct hda_codec *codec,
2813 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002814{
2815 int _tlv[4];
2816 const int *tlv = NULL;
2817 int val = -1;
2818
2819 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2820 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2821 mm_segment_t fs = get_fs();
2822 set_fs(get_ds());
2823 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2824 tlv = _tlv;
2825 set_fs(fs);
2826 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2827 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002828 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2829 int step = tlv[3];
2830 step &= ~TLV_DB_SCALE_MUTE;
2831 if (!step)
2832 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002833 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002834 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002835- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002836 return -1;
2837 }
2838 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002839 val = -tlv[2] / step;
2840 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002841 return val;
2842}
2843
2844/* call kctl->put with the given value(s) */
2845static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2846{
2847 struct snd_ctl_elem_value *ucontrol;
2848 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2849 if (!ucontrol)
2850 return -ENOMEM;
2851 ucontrol->value.integer.value[0] = val;
2852 ucontrol->value.integer.value[1] = val;
2853 kctl->put(kctl, ucontrol);
2854 kfree(ucontrol);
2855 return 0;
2856}
2857
2858/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002859static int init_slave_0dB(struct hda_codec *codec,
2860 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002861{
Takashi Iwai6194b992014-06-06 18:12:16 +02002862 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002863 if (offset > 0)
2864 put_kctl_with_value(slave, offset);
2865 return 0;
2866}
2867
2868/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002869static int init_slave_unmute(struct hda_codec *codec,
2870 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002871{
2872 return put_kctl_with_value(slave, 1);
2873}
2874
Takashi Iwaie8750942014-06-30 14:02:39 +02002875static int add_slave(struct hda_codec *codec,
2876 void *data, struct snd_kcontrol *slave)
2877{
2878 return snd_ctl_add_slave(data, slave);
2879}
2880
Takashi Iwaid5191e52009-11-16 14:58:17 +01002881/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002882 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002883 * @codec: HD-audio codec
2884 * @name: vmaster control name
2885 * @tlv: TLV data (optional)
2886 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002887 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002888 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002889 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002890 *
2891 * Create a virtual master control with the given name. The TLV data
2892 * must be either NULL or a valid data.
2893 *
2894 * @slaves is a NULL-terminated array of strings, each of which is a
2895 * slave control name. All controls with these names are assigned to
2896 * the new virtual master control.
2897 *
2898 * This function returns zero if successful or a negative error code.
2899 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002900int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002901 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002902 const char *suffix, bool init_slave_vol,
2903 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002904{
2905 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002906 int err;
2907
Takashi Iwai29e58532012-03-12 12:25:03 +01002908 if (ctl_ret)
2909 *ctl_ret = NULL;
2910
Takashi Iwai9322ca52012-02-03 14:28:01 +01002911 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002912 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002913 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002914 return 0;
2915 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002916 kctl = snd_ctl_make_virtual_master(name, tlv);
2917 if (!kctl)
2918 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002919 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002920 if (err < 0)
2921 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002922
Takashi Iwaie8750942014-06-30 14:02:39 +02002923 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002924 if (err < 0)
2925 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002926
2927 /* init with master mute & zero volume */
2928 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002929 if (init_slave_vol) {
2930 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002931 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002932 tlv ? init_slave_0dB : init_slave_unmute, &step);
2933 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002934
Takashi Iwai29e58532012-03-12 12:25:03 +01002935 if (ctl_ret)
2936 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002937 return 0;
2938}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002939EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002940
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002941/*
2942 * mute-LED control using vmaster
2943 */
2944static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_info *uinfo)
2946{
2947 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002948 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002949 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002950
Takashi Iwai3ff72212014-10-20 18:17:28 +02002951 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002952}
2953
2954static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_value *ucontrol)
2956{
2957 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2958 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2959 return 0;
2960}
2961
2962static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_value *ucontrol)
2964{
2965 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2966 unsigned int old_mode = hook->mute_mode;
2967
2968 hook->mute_mode = ucontrol->value.enumerated.item[0];
2969 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2970 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2971 if (old_mode == hook->mute_mode)
2972 return 0;
2973 snd_hda_sync_vmaster_hook(hook);
2974 return 1;
2975}
2976
2977static struct snd_kcontrol_new vmaster_mute_mode = {
2978 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2979 .name = "Mute-LED Mode",
2980 .info = vmaster_mute_mode_info,
2981 .get = vmaster_mute_mode_get,
2982 .put = vmaster_mute_mode_put,
2983};
2984
Takashi Iwai95a962c2014-10-29 16:03:58 +01002985/**
2986 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2987 * @codec: the HDA codec
2988 * @hook: the vmaster hook object
2989 * @expose_enum_ctl: flag to create an enum ctl
2990 *
2991 * Add a mute-LED hook with the given vmaster switch kctl.
2992 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2993 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002994 */
2995int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002996 struct hda_vmaster_mute_hook *hook,
2997 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002998{
2999 struct snd_kcontrol *kctl;
3000
3001 if (!hook->hook || !hook->sw_kctl)
3002 return 0;
3003 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3004 hook->codec = codec;
3005 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01003006 if (!expose_enum_ctl)
3007 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003008 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3009 if (!kctl)
3010 return -ENOMEM;
3011 return snd_hda_ctl_add(codec, 0, kctl);
3012}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003013EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003014
Takashi Iwai95a962c2014-10-29 16:03:58 +01003015/**
3016 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3017 * @hook: the vmaster hook
3018 *
3019 * Call the hook with the current value for synchronization.
3020 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003021 */
3022void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3023{
3024 if (!hook->hook || !hook->codec)
3025 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003026 /* don't call vmaster hook in the destructor since it might have
3027 * been already destroyed
3028 */
3029 if (hook->codec->bus->shutdown)
3030 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003031 switch (hook->mute_mode) {
3032 case HDA_VMUTE_FOLLOW_MASTER:
3033 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3034 break;
3035 default:
3036 hook->hook(hook->codec, hook->mute_mode);
3037 break;
3038 }
3039}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003040EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003041
3042
Takashi Iwaid5191e52009-11-16 14:58:17 +01003043/**
3044 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003045 * @kcontrol: referred ctl element
3046 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003047 *
3048 * The control element is supposed to have the private_value field
3049 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3050 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003051int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 int chs = get_amp_channels(kcontrol);
3055
3056 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3057 uinfo->count = chs == 3 ? 2 : 1;
3058 uinfo->value.integer.min = 0;
3059 uinfo->value.integer.max = 1;
3060 return 0;
3061}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003062EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Takashi Iwaid5191e52009-11-16 14:58:17 +01003064/**
3065 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003066 * @kcontrol: ctl element
3067 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003068 *
3069 * The control element is supposed to have the private_value field
3070 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3071 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3076 hda_nid_t nid = get_amp_nid(kcontrol);
3077 int chs = get_amp_channels(kcontrol);
3078 int dir = get_amp_direction(kcontrol);
3079 int idx = get_amp_index(kcontrol);
3080 long *valp = ucontrol->value.integer.value;
3081
3082 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003084 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003086 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003087 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return 0;
3089}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003090EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Takashi Iwaid5191e52009-11-16 14:58:17 +01003092/**
3093 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003094 * @kcontrol: ctl element
3095 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003096 *
3097 * The control element is supposed to have the private_value field
3098 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3099 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3101 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3104 hda_nid_t nid = get_amp_nid(kcontrol);
3105 int chs = get_amp_channels(kcontrol);
3106 int dir = get_amp_direction(kcontrol);
3107 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 long *valp = ucontrol->value.integer.value;
3109 int change = 0;
3110
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003111 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01003112 change = codec_amp_update(codec, nid, 0, dir, idx,
3113 HDA_AMP_MUTE,
3114 *valp ? 0 : HDA_AMP_MUTE, false,
3115 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003116 valp++;
3117 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003118 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01003119 change |= codec_amp_update(codec, nid, 1, dir, idx,
3120 HDA_AMP_MUTE,
3121 *valp ? 0 : HDA_AMP_MUTE, false,
3122 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003123 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return change;
3125}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003126EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128/*
Takashi Iwai985be542005-11-02 18:26:49 +01003129 * bound volume controls
3130 *
3131 * bind multiple volumes (# indices, from 0)
3132 */
3133
3134#define AMP_VAL_IDX_SHIFT 19
3135#define AMP_VAL_IDX_MASK (0x0f<<19)
3136
Takashi Iwaid5191e52009-11-16 14:58:17 +01003137/**
3138 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003139 * @kcontrol: ctl element
3140 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003141 *
3142 * The control element is supposed to have the private_value field
3143 * set up via HDA_BIND_MUTE*() macros.
3144 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003145int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003147{
3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 unsigned long pval;
3150 int err;
3151
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003152 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003153 pval = kcontrol->private_value;
3154 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3155 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3156 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003157 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003158 return err;
3159}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003160EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003161
Takashi Iwaid5191e52009-11-16 14:58:17 +01003162/**
3163 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003164 * @kcontrol: ctl element
3165 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003166 *
3167 * The control element is supposed to have the private_value field
3168 * set up via HDA_BIND_MUTE*() macros.
3169 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003170int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003172{
3173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3174 unsigned long pval;
3175 int i, indices, err = 0, change = 0;
3176
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003177 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003178 pval = kcontrol->private_value;
3179 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3180 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003181 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3182 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003183 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3184 if (err < 0)
3185 break;
3186 change |= err;
3187 }
3188 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003189 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003190 return err < 0 ? err : change;
3191}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003192EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003193
Takashi Iwaid5191e52009-11-16 14:58:17 +01003194/**
3195 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003196 * @kcontrol: referred ctl element
3197 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003198 *
3199 * The control element is supposed to have the private_value field
3200 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003201 */
3202int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_info *uinfo)
3204{
3205 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3206 struct hda_bind_ctls *c;
3207 int err;
3208
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003209 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003210 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003211 kcontrol->private_value = *c->values;
3212 err = c->ops->info(kcontrol, uinfo);
3213 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003214 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003215 return err;
3216}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003217EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003218
Takashi Iwaid5191e52009-11-16 14:58:17 +01003219/**
3220 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003221 * @kcontrol: ctl element
3222 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003223 *
3224 * The control element is supposed to have the private_value field
3225 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3226 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003227int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
3230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3231 struct hda_bind_ctls *c;
3232 int err;
3233
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003234 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003235 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003236 kcontrol->private_value = *c->values;
3237 err = c->ops->get(kcontrol, ucontrol);
3238 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003239 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003240 return err;
3241}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003242EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003243
Takashi Iwaid5191e52009-11-16 14:58:17 +01003244/**
3245 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003246 * @kcontrol: ctl element
3247 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003248 *
3249 * The control element is supposed to have the private_value field
3250 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3251 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003252int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3253 struct snd_ctl_elem_value *ucontrol)
3254{
3255 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3256 struct hda_bind_ctls *c;
3257 unsigned long *vals;
3258 int err = 0, change = 0;
3259
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003260 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003261 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003262 for (vals = c->values; *vals; vals++) {
3263 kcontrol->private_value = *vals;
3264 err = c->ops->put(kcontrol, ucontrol);
3265 if (err < 0)
3266 break;
3267 change |= err;
3268 }
3269 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003270 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003271 return err < 0 ? err : change;
3272}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003273EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003274
Takashi Iwaid5191e52009-11-16 14:58:17 +01003275/**
3276 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003277 * @kcontrol: ctl element
3278 * @op_flag: operation flag
3279 * @size: byte size of input TLV
3280 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003281 *
3282 * The control element is supposed to have the private_value field
3283 * set up via HDA_BIND_VOL() macro.
3284 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003285int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3286 unsigned int size, unsigned int __user *tlv)
3287{
3288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3289 struct hda_bind_ctls *c;
3290 int err;
3291
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003292 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003293 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003294 kcontrol->private_value = *c->values;
3295 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3296 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003297 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003298 return err;
3299}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003300EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003301
3302struct hda_ctl_ops snd_hda_bind_vol = {
3303 .info = snd_hda_mixer_amp_volume_info,
3304 .get = snd_hda_mixer_amp_volume_get,
3305 .put = snd_hda_mixer_amp_volume_put,
3306 .tlv = snd_hda_mixer_amp_tlv
3307};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003308EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003309
3310struct hda_ctl_ops snd_hda_bind_sw = {
3311 .info = snd_hda_mixer_amp_switch_info,
3312 .get = snd_hda_mixer_amp_switch_get,
3313 .put = snd_hda_mixer_amp_switch_put,
3314 .tlv = snd_hda_mixer_amp_tlv
3315};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003316EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003317
3318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * SPDIF out controls
3320 */
3321
Takashi Iwai0ba21762007-04-16 11:29:14 +02003322static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3323 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
3325 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3326 uinfo->count = 1;
3327 return 0;
3328}
3329
Takashi Iwai0ba21762007-04-16 11:29:14 +02003330static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3334 IEC958_AES0_NONAUDIO |
3335 IEC958_AES0_CON_EMPHASIS_5015 |
3336 IEC958_AES0_CON_NOT_COPYRIGHT;
3337 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3338 IEC958_AES1_CON_ORIGINAL;
3339 return 0;
3340}
3341
Takashi Iwai0ba21762007-04-16 11:29:14 +02003342static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3346 IEC958_AES0_NONAUDIO |
3347 IEC958_AES0_PRO_EMPHASIS_5015;
3348 return 0;
3349}
3350
Takashi Iwai0ba21762007-04-16 11:29:14 +02003351static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003355 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003356 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003358 mutex_lock(&codec->spdif_mutex);
3359 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003360 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3361 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3362 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3363 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003364 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 return 0;
3367}
3368
3369/* convert from SPDIF status bits to HDA SPDIF bits
3370 * bit 0 (DigEn) is always set zero (to be filled later)
3371 */
3372static unsigned short convert_from_spdif_status(unsigned int sbits)
3373{
3374 unsigned short val = 0;
3375
3376 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003377 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003379 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003381 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3382 IEC958_AES0_PRO_EMPHASIS_5015)
3383 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003385 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3386 IEC958_AES0_CON_EMPHASIS_5015)
3387 val |= AC_DIG1_EMPHASIS;
3388 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3389 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003391 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3393 }
3394 return val;
3395}
3396
3397/* convert to SPDIF status bits from HDA SPDIF bits
3398 */
3399static unsigned int convert_to_spdif_status(unsigned short val)
3400{
3401 unsigned int sbits = 0;
3402
Takashi Iwai0ba21762007-04-16 11:29:14 +02003403 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003405 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 sbits |= IEC958_AES0_PROFESSIONAL;
3407 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003408 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3410 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003411 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003413 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003415 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3417 sbits |= val & (0x7f << 8);
3418 }
3419 return sbits;
3420}
3421
Takashi Iwai2f728532008-09-25 16:32:41 +02003422/* set digital convert verbs both for the given NID and its slaves */
3423static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3424 int verb, int val)
3425{
Takashi Iwaidda14412011-05-02 11:29:30 +02003426 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003427
Takashi Iwai9e976972008-11-25 08:17:20 +01003428 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003429 d = codec->slave_dig_outs;
3430 if (!d)
3431 return;
3432 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003433 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003434}
3435
3436static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3437 int dig1, int dig2)
3438{
3439 if (dig1 != -1)
3440 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3441 if (dig2 != -1)
3442 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3443}
3444
Takashi Iwai0ba21762007-04-16 11:29:14 +02003445static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3446 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003449 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003450 struct hda_spdif_out *spdif;
3451 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 unsigned short val;
3453 int change;
3454
Ingo Molnar62932df2006-01-16 16:34:20 +01003455 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003456 spdif = snd_array_elem(&codec->spdif_out, idx);
3457 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003458 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3460 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3461 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003462 val = convert_from_spdif_status(spdif->status);
3463 val |= spdif->ctls & 1;
3464 change = spdif->ctls != val;
3465 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003466 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003467 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003468 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return change;
3470}
3471
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003472#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Takashi Iwai0ba21762007-04-16 11:29:14 +02003474static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3475 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
3477 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003478 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003479 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003481 mutex_lock(&codec->spdif_mutex);
3482 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003483 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003484 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return 0;
3486}
3487
Stephen Warren74b654c2011-06-01 11:14:18 -06003488static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3489 int dig1, int dig2)
3490{
3491 set_dig_out_convert(codec, nid, dig1, dig2);
3492 /* unmute amp switch (if any) */
3493 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3494 (dig1 & AC_DIG1_ENABLE))
3495 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3496 HDA_AMP_MUTE, 0);
3497}
3498
Takashi Iwai0ba21762007-04-16 11:29:14 +02003499static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3500 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003503 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003504 struct hda_spdif_out *spdif;
3505 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 unsigned short val;
3507 int change;
3508
Ingo Molnar62932df2006-01-16 16:34:20 +01003509 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003510 spdif = snd_array_elem(&codec->spdif_out, idx);
3511 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003512 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003514 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003515 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003516 spdif->ctls = val;
3517 if (change && nid != (u16)-1)
3518 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003519 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return change;
3521}
3522
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003523static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 {
3525 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003527 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 .info = snd_hda_spdif_mask_info,
3529 .get = snd_hda_spdif_cmask_get,
3530 },
3531 {
3532 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003534 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 .info = snd_hda_spdif_mask_info,
3536 .get = snd_hda_spdif_pmask_get,
3537 },
3538 {
3539 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003540 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 .info = snd_hda_spdif_mask_info,
3542 .get = snd_hda_spdif_default_get,
3543 .put = snd_hda_spdif_default_put,
3544 },
3545 {
3546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003547 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 .info = snd_hda_spdif_out_switch_info,
3549 .get = snd_hda_spdif_out_switch_get,
3550 .put = snd_hda_spdif_out_switch_put,
3551 },
3552 { } /* end */
3553};
3554
3555/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003556 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003558 * @associated_nid: NID that new ctls associated with
3559 * @cvt_nid: converter NID
3560 * @type: HDA_PCM_TYPE_*
3561 * Creates controls related with the digital output.
3562 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 *
3564 * Returns 0 if successful, or a negative error code.
3565 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003566int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3567 hda_nid_t associated_nid,
3568 hda_nid_t cvt_nid,
3569 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
3571 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003572 struct snd_kcontrol *kctl;
3573 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003574 int idx = 0;
3575 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003576 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003577 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003579 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003580 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003581 idx = spdif_index;
3582 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003583 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003584 /* suppose a single SPDIF device */
3585 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3586 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3587 if (!kctl)
3588 break;
3589 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003590 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003591 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003592 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003593 if (!bus->primary_dig_out_type)
3594 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003595
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003596 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003597 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003598 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003599 return -EBUSY;
3600 }
Stephen Warren7c935972011-06-01 11:14:17 -06003601 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003602 if (!spdif)
3603 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3605 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003606 if (!kctl)
3607 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003608 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003609 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003610 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003611 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return err;
3613 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003614 spdif->nid = cvt_nid;
3615 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003616 AC_VERB_GET_DIGI_CONVERT_1, 0);
3617 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return 0;
3619}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003620EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Takashi Iwai95a962c2014-10-29 16:03:58 +01003622/**
3623 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3624 * @codec: the HDA codec
3625 * @nid: widget NID
3626 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003627 * call within spdif_mutex lock
3628 */
Stephen Warren7c935972011-06-01 11:14:17 -06003629struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3630 hda_nid_t nid)
3631{
3632 int i;
3633 for (i = 0; i < codec->spdif_out.used; i++) {
3634 struct hda_spdif_out *spdif =
3635 snd_array_elem(&codec->spdif_out, i);
3636 if (spdif->nid == nid)
3637 return spdif;
3638 }
3639 return NULL;
3640}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003641EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003642
Takashi Iwai95a962c2014-10-29 16:03:58 +01003643/**
3644 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3645 * @codec: the HDA codec
3646 * @idx: the SPDIF ctl index
3647 *
3648 * Unassign the widget from the given SPDIF control.
3649 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003650void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3651{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003652 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003653
3654 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003655 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003656 spdif->nid = (u16)-1;
3657 mutex_unlock(&codec->spdif_mutex);
3658}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003659EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003660
Takashi Iwai95a962c2014-10-29 16:03:58 +01003661/**
3662 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3663 * @codec: the HDA codec
3664 * @idx: the SPDIF ctl idx
3665 * @nid: widget NID
3666 *
3667 * Assign the widget to the SPDIF control with the given index.
3668 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003669void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3670{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003671 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003672 unsigned short val;
3673
3674 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003675 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003676 if (spdif->nid != nid) {
3677 spdif->nid = nid;
3678 val = spdif->ctls;
3679 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3680 }
3681 mutex_unlock(&codec->spdif_mutex);
3682}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003683EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003686 * SPDIF sharing with analog output
3687 */
3688static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3689 struct snd_ctl_elem_value *ucontrol)
3690{
3691 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3692 ucontrol->value.integer.value[0] = mout->share_spdif;
3693 return 0;
3694}
3695
3696static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3697 struct snd_ctl_elem_value *ucontrol)
3698{
3699 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3700 mout->share_spdif = !!ucontrol->value.integer.value[0];
3701 return 0;
3702}
3703
3704static struct snd_kcontrol_new spdif_share_sw = {
3705 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3706 .name = "IEC958 Default PCM Playback Switch",
3707 .info = snd_ctl_boolean_mono_info,
3708 .get = spdif_share_sw_get,
3709 .put = spdif_share_sw_put,
3710};
3711
Takashi Iwaid5191e52009-11-16 14:58:17 +01003712/**
3713 * snd_hda_create_spdif_share_sw - create Default PCM switch
3714 * @codec: the HDA codec
3715 * @mout: multi-out instance
3716 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003717int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3718 struct hda_multi_out *mout)
3719{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003720 struct snd_kcontrol *kctl;
3721
Takashi Iwai9a081602008-02-12 18:37:26 +01003722 if (!mout->dig_out_nid)
3723 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003724
3725 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3726 if (!kctl)
3727 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003728 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003729 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003730}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003731EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003732
3733/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 * SPDIF input
3735 */
3736
3737#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3738
Takashi Iwai0ba21762007-04-16 11:29:14 +02003739static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3740 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3743
3744 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3745 return 0;
3746}
3747
Takashi Iwai0ba21762007-04-16 11:29:14 +02003748static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3749 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3752 hda_nid_t nid = kcontrol->private_value;
3753 unsigned int val = !!ucontrol->value.integer.value[0];
3754 int change;
3755
Ingo Molnar62932df2006-01-16 16:34:20 +01003756 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003758 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003760 snd_hda_codec_write_cache(codec, nid, 0,
3761 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003763 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return change;
3765}
3766
Takashi Iwai0ba21762007-04-16 11:29:14 +02003767static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3768 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3771 hda_nid_t nid = kcontrol->private_value;
3772 unsigned short val;
3773 unsigned int sbits;
3774
Andrew Paprocki3982d172007-12-19 12:13:44 +01003775 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 sbits = convert_to_spdif_status(val);
3777 ucontrol->value.iec958.status[0] = sbits;
3778 ucontrol->value.iec958.status[1] = sbits >> 8;
3779 ucontrol->value.iec958.status[2] = sbits >> 16;
3780 ucontrol->value.iec958.status[3] = sbits >> 24;
3781 return 0;
3782}
3783
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003784static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 {
3786 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003787 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 .info = snd_hda_spdif_in_switch_info,
3789 .get = snd_hda_spdif_in_switch_get,
3790 .put = snd_hda_spdif_in_switch_put,
3791 },
3792 {
3793 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003795 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 .info = snd_hda_spdif_mask_info,
3797 .get = snd_hda_spdif_in_status_get,
3798 },
3799 { } /* end */
3800};
3801
3802/**
3803 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3804 * @codec: the HDA codec
3805 * @nid: audio in widget NID
3806 *
3807 * Creates controls related with the SPDIF input.
3808 * Called from each patch supporting the SPDIF in.
3809 *
3810 * Returns 0 if successful, or a negative error code.
3811 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003812int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003815 struct snd_kcontrol *kctl;
3816 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003817 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Takashi Iwaidcda5802012-10-12 17:24:51 +02003819 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003820 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003821 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003822 return -EBUSY;
3823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3825 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003826 if (!kctl)
3827 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003829 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003830 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return err;
3832 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003833 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003834 snd_hda_codec_read(codec, nid, 0,
3835 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003836 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return 0;
3838}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003839EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003841/*
3842 * command cache
3843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Takashi Iwaic370dd62012-12-13 18:30:04 +01003845/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003846#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3847#define get_cmd_cache_nid(key) ((key) & 0xff)
3848#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3849
3850/**
3851 * snd_hda_codec_write_cache - send a single command with caching
3852 * @codec: the HDA codec
3853 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003854 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003855 * @verb: the verb to send
3856 * @parm: the parameter for the verb
3857 *
3858 * Send a single command without waiting for response.
3859 *
3860 * Returns 0 if successful, or a negative error code.
3861 */
3862int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003863 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003864{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003865 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003866 struct hda_cache_head *c;
3867 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003868 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003869
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003870 cache_only = codec->cached_write;
3871 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003872 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003873 if (err < 0)
3874 return err;
3875 }
3876
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003877 /* parm may contain the verb stuff for get/set amp */
3878 verb = verb | (parm >> 8);
3879 parm &= 0xff;
3880 key = build_cmd_cache_key(nid, verb);
3881 mutex_lock(&codec->bus->cmd_mutex);
3882 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003883 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003884 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003885 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003886 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003887 mutex_unlock(&codec->bus->cmd_mutex);
3888 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003889}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003890EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003891
Takashi Iwaid5191e52009-11-16 14:58:17 +01003892/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003893 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3894 * @codec: the HDA codec
3895 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003896 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003897 * @verb: the verb to send
3898 * @parm: the parameter for the verb
3899 *
3900 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3901 * command if the parameter is already identical with the cached value.
3902 * If not, it sends the command and refreshes the cache.
3903 *
3904 * Returns 0 if successful, or a negative error code.
3905 */
3906int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003907 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003908{
3909 struct hda_cache_head *c;
3910 u32 key;
3911
3912 /* parm may contain the verb stuff for get/set amp */
3913 verb = verb | (parm >> 8);
3914 parm &= 0xff;
3915 key = build_cmd_cache_key(nid, verb);
3916 mutex_lock(&codec->bus->cmd_mutex);
3917 c = get_hash(&codec->cmd_cache, key);
3918 if (c && c->val == parm) {
3919 mutex_unlock(&codec->bus->cmd_mutex);
3920 return 0;
3921 }
3922 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003923 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003924}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003925EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003926
3927/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003928 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3929 * @codec: HD-audio codec
3930 *
3931 * Execute all verbs recorded in the command caches to resume.
3932 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003933void snd_hda_codec_resume_cache(struct hda_codec *codec)
3934{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003935 int i;
3936
Takashi Iwaic370dd62012-12-13 18:30:04 +01003937 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003938 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003939 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3940 struct hda_cache_head *buffer;
3941 u32 key;
3942
3943 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3944 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003945 if (!key)
3946 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003947 if (!buffer->dirty)
3948 continue;
3949 buffer->dirty = 0;
3950 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003951 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3952 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003953 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003954 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003955 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003956}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003957EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003958
3959/**
3960 * snd_hda_sequence_write_cache - sequence writes with caching
3961 * @codec: the HDA codec
3962 * @seq: VERB array to send
3963 *
3964 * Send the commands sequentially from the given array.
3965 * Thte commands are recorded on cache for power-save and resume.
3966 * The array must be terminated with NID=0.
3967 */
3968void snd_hda_sequence_write_cache(struct hda_codec *codec,
3969 const struct hda_verb *seq)
3970{
3971 for (; seq->nid; seq++)
3972 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3973 seq->param);
3974}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003975EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003976
Takashi Iwaidc870f32013-01-22 15:24:30 +01003977/**
3978 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3979 * @codec: HD-audio codec
3980 */
3981void snd_hda_codec_flush_cache(struct hda_codec *codec)
3982{
3983 snd_hda_codec_resume_amp(codec);
3984 snd_hda_codec_resume_cache(codec);
3985}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003986EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003987
Takashi Iwai95a962c2014-10-29 16:03:58 +01003988/**
3989 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3990 * @codec: the HDA codec
3991 * @fg: function group (not used now)
3992 * @power_state: the power state to set (AC_PWRST_*)
3993 *
3994 * Set the given power state to all widgets that have the power control.
3995 * If the codec has power_filter set, it evaluates the power state and
3996 * filter out if it's unchanged as D3.
3997 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003998void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003999 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01004000{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004001 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004002 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01004003
Takashi Iwaicb53c622007-08-10 17:21:45 +02004004 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01004005 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004006 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004007 if (!(wcaps & AC_WCAP_POWER))
4008 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004009 if (codec->power_filter) {
4010 state = codec->power_filter(codec, nid, power_state);
4011 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004012 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02004013 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004014 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004015 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01004016 }
Takashi Iwai54d17402005-11-21 16:33:22 +01004017}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004018EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004019
4020/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004021 * supported power states check
4022 */
4023static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4024 unsigned int power_state)
4025{
4026 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4027
Mengdong Line037cb42012-08-10 14:11:58 +02004028 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004029 return false;
4030 if (sup & power_state)
4031 return true;
4032 else
4033 return false;
4034}
4035
4036/*
Takashi Iwai432c6412012-08-28 09:59:20 -07004037 * wait until the state is reached, returns the current state
4038 */
4039static unsigned int hda_sync_power_state(struct hda_codec *codec,
4040 hda_nid_t fg,
4041 unsigned int power_state)
4042{
4043 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4044 unsigned int state, actual_state;
4045
4046 for (;;) {
4047 state = snd_hda_codec_read(codec, fg, 0,
4048 AC_VERB_GET_POWER_STATE, 0);
4049 if (state & AC_PWRST_ERROR)
4050 break;
4051 actual_state = (state >> 4) & 0x0f;
4052 if (actual_state == power_state)
4053 break;
4054 if (time_after_eq(jiffies, end_time))
4055 break;
4056 /* wait until the codec reachs to the target state */
4057 msleep(1);
4058 }
4059 return state;
4060}
4061
Takashi Iwai95a962c2014-10-29 16:03:58 +01004062/**
4063 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4064 * @codec: the HDA codec
4065 * @nid: widget NID
4066 * @power_state: power state to evalue
4067 *
4068 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4069 * This can be used a codec power_filter callback.
4070 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004071unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4072 hda_nid_t nid,
4073 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004074{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004075 if (nid == codec->afg || nid == codec->mfg)
4076 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004077 if (power_state == AC_PWRST_D3 &&
4078 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4079 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4080 int eapd = snd_hda_codec_read(codec, nid, 0,
4081 AC_VERB_GET_EAPD_BTLENABLE, 0);
4082 if (eapd & 0x02)
4083 return AC_PWRST_D0;
4084 }
4085 return power_state;
4086}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004087EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004088
Takashi Iwai432c6412012-08-28 09:59:20 -07004089/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004090 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004091 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004092static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004093 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004094{
Takashi Iwaid8193872012-08-31 07:54:38 -07004095 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004096 int count;
4097 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004098 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004099
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004100 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004101 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004102 if (codec->depop_delay < 0)
4103 msleep(codec->epss ? 10 : 100);
4104 else if (codec->depop_delay > 0)
4105 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004106 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004107 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004108
4109 /* repeat power states setting at most 10 times*/
4110 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004111 if (codec->patch_ops.set_power_state)
4112 codec->patch_ops.set_power_state(codec, fg,
4113 power_state);
4114 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004115 state = power_state;
4116 if (codec->power_filter)
4117 state = codec->power_filter(codec, fg, state);
4118 if (state == power_state || power_state != AC_PWRST_D3)
4119 snd_hda_codec_read(codec, fg, flags,
4120 AC_VERB_SET_POWER_STATE,
4121 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004122 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004123 }
4124 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004125 if (!(state & AC_PWRST_ERROR))
4126 break;
4127 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004128
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004129 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004130}
Takashi Iwai54d17402005-11-21 16:33:22 +01004131
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004132/* sync power states of all widgets;
4133 * this is called at the end of codec parsing
4134 */
4135static void sync_power_up_states(struct hda_codec *codec)
4136{
4137 hda_nid_t nid = codec->start_nid;
4138 int i;
4139
Takashi Iwaiba615b82013-03-13 14:47:21 +01004140 /* don't care if no filter is used */
4141 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004142 return;
4143
4144 for (i = 0; i < codec->num_nodes; i++, nid++) {
4145 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004146 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004147 if (!(wcaps & AC_WCAP_POWER))
4148 continue;
4149 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4150 if (target == AC_PWRST_D0)
4151 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004152 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004153 snd_hda_codec_write(codec, nid, 0,
4154 AC_VERB_SET_POWER_STATE, target);
4155 }
4156}
4157
Takashi Iwai648a8d22014-02-25 10:38:13 +01004158#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004159/* execute additional init verbs */
4160static void hda_exec_init_verbs(struct hda_codec *codec)
4161{
4162 if (codec->init_verbs.list)
4163 snd_hda_sequence_write(codec, codec->init_verbs.list);
4164}
4165#else
4166static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4167#endif
4168
Takashi Iwai2a439522011-07-26 09:52:50 +02004169#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004170/*
4171 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004172 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004173 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004174static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004175{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004176 unsigned int state;
4177
Takashi Iwai989c3182012-11-19 14:14:58 +01004178 codec->in_pm = 1;
4179
Takashi Iwaicb53c622007-08-10 17:21:45 +02004180 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004181 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004182 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004183 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004184 /* Cancel delayed work if we aren't currently running from it. */
4185 if (!in_wq)
4186 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004187 spin_lock(&codec->power_lock);
4188 snd_hda_update_power_acct(codec);
4189 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004190 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004191 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004192 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004193 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004194 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004195 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004196}
4197
Takashi Iwaic370dd62012-12-13 18:30:04 +01004198/* mark all entries of cmd and amp caches dirty */
4199static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4200{
4201 int i;
4202 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4203 struct hda_cache_head *cmd;
4204 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4205 cmd->dirty = 1;
4206 }
4207 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4208 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004209 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004210 amp->head.dirty = 1;
4211 }
4212}
4213
Takashi Iwaicb53c622007-08-10 17:21:45 +02004214/*
4215 * kick up codec; used both from PM and power-save
4216 */
4217static void hda_call_codec_resume(struct hda_codec *codec)
4218{
Takashi Iwai989c3182012-11-19 14:14:58 +01004219 codec->in_pm = 1;
4220
Takashi Iwaic370dd62012-12-13 18:30:04 +01004221 hda_mark_cmd_cache_dirty(codec);
4222
Takashi Iwai7f308302012-05-08 16:52:23 +02004223 /* set as if powered on for avoiding re-entering the resume
4224 * in the resume / power-save sequence
4225 */
4226 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004227 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004228 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004229 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004230 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004231 if (codec->patch_ops.resume)
4232 codec->patch_ops.resume(codec);
4233 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004234 if (codec->patch_ops.init)
4235 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004236 snd_hda_codec_resume_amp(codec);
4237 snd_hda_codec_resume_cache(codec);
4238 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004239
4240 if (codec->jackpoll_interval)
4241 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004242 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004243 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004244
4245 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004246 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004247}
Takashi Iwai2a439522011-07-26 09:52:50 +02004248#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004249
Takashi Iwai54d17402005-11-21 16:33:22 +01004250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251/**
4252 * snd_hda_build_controls - build mixer controls
4253 * @bus: the BUS
4254 *
4255 * Creates mixer controls for each codec included in the bus.
4256 *
4257 * Returns 0 if successful, otherwise a negative error code.
4258 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004259int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004261 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Takashi Iwai0ba21762007-04-16 11:29:14 +02004263 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004264 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004265 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004266 codec_err(codec,
4267 "cannot build controls for #%d (error %d)\n",
4268 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004269 err = snd_hda_codec_reset(codec);
4270 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004271 codec_err(codec,
4272 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004273 return err;
4274 }
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004277 return 0;
4278}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004279EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004281/*
4282 * add standard channel maps if not specified
4283 */
4284static int add_std_chmaps(struct hda_codec *codec)
4285{
4286 int i, str, err;
4287
4288 for (i = 0; i < codec->num_pcms; i++) {
4289 for (str = 0; str < 2; str++) {
4290 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4291 struct hda_pcm_stream *hinfo =
4292 &codec->pcm_info[i].stream[str];
4293 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004294 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004295
4296 if (codec->pcm_info[i].own_chmap)
4297 continue;
4298 if (!pcm || !hinfo->substreams)
4299 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004300 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4301 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004302 hinfo->channels_max,
4303 0, &chmap);
4304 if (err < 0)
4305 return err;
4306 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4307 }
4308 }
4309 return 0;
4310}
4311
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004312/* default channel maps for 2.1 speakers;
4313 * since HD-audio supports only stereo, odd number channels are omitted
4314 */
4315const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4316 { .channels = 2,
4317 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4318 { .channels = 4,
4319 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4320 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4321 { }
4322};
4323EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004325int snd_hda_codec_build_controls(struct hda_codec *codec)
4326{
4327 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004328 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004329 /* continue to initialize... */
4330 if (codec->patch_ops.init)
4331 err = codec->patch_ops.init(codec);
4332 if (!err && codec->patch_ops.build_controls)
4333 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004334 if (err < 0)
4335 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004336
4337 /* we create chmaps here instead of build_pcms */
4338 err = add_std_chmaps(codec);
4339 if (err < 0)
4340 return err;
4341
David Henningsson26a6cb62012-10-09 15:04:21 +02004342 if (codec->jackpoll_interval)
4343 hda_jackpoll_work(&codec->jackpoll_work.work);
4344 else
4345 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004346 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348}
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350/*
4351 * stream formats
4352 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004353struct hda_rate_tbl {
4354 unsigned int hz;
4355 unsigned int alsa_bits;
4356 unsigned int hda_fmt;
4357};
4358
Takashi Iwai92f10b32010-08-03 14:21:00 +02004359/* rate = base * mult / div */
4360#define HDA_RATE(base, mult, div) \
4361 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4362 (((div) - 1) << AC_FMT_DIV_SHIFT))
4363
Takashi Iwaibefdf312005-08-22 13:57:55 +02004364static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004366
4367 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004368 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4369 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4370 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4371 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4372 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4373 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4374 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4375 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4376 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4377 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4378 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004379#define AC_PAR_PCM_RATE_BITS 11
4380 /* up to bits 10, 384kHZ isn't supported properly */
4381
4382 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004383 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004384
Takashi Iwaibefdf312005-08-22 13:57:55 +02004385 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386};
4387
4388/**
4389 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004390 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 * @rate: the sample rate
4392 * @channels: the number of channels
4393 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4394 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004395 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 *
4397 * Calculate the format bitset from the given rate, channels and th PCM format.
4398 *
4399 * Return zero if invalid.
4400 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004401unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4402 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 unsigned int channels,
4404 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004405 unsigned int maxbps,
4406 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
4408 int i;
4409 unsigned int val = 0;
4410
Takashi Iwaibefdf312005-08-22 13:57:55 +02004411 for (i = 0; rate_bits[i].hz; i++)
4412 if (rate_bits[i].hz == rate) {
4413 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 break;
4415 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004416 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004417 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return 0;
4419 }
4420
4421 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004422 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return 0;
4424 }
4425 val |= channels - 1;
4426
4427 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004428 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004429 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004430 break;
4431 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004432 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 case 20:
4435 case 24:
4436 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004437 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004438 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004440 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004442 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 break;
4444 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004445 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004446 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return 0;
4448 }
4449
Anssi Hannula32c168c2010-08-03 13:28:57 +03004450 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004451 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return val;
4454}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004455EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004457static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4458 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004459{
4460 unsigned int val = 0;
4461 if (nid != codec->afg &&
4462 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4463 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4464 if (!val || val == -1)
4465 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4466 if (!val || val == -1)
4467 return 0;
4468 return val;
4469}
4470
4471static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4472{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004473 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004474 get_pcm_param);
4475}
4476
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004477static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4478 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004479{
4480 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4481 if (!streams || streams == -1)
4482 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4483 if (!streams || streams == -1)
4484 return 0;
4485 return streams;
4486}
4487
4488static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4489{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004490 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004491 get_stream_param);
4492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494/**
4495 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4496 * @codec: the HDA codec
4497 * @nid: NID to query
4498 * @ratesp: the pointer to store the detected rate bitflags
4499 * @formatsp: the pointer to store the detected formats
4500 * @bpsp: the pointer to store the detected format widths
4501 *
4502 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4503 * or @bsps argument is ignored.
4504 *
4505 * Returns 0 if successful, otherwise a negative error code.
4506 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004507int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4509{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004510 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004512 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004513 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515 if (ratesp) {
4516 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004517 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004519 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004521 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004522 codec_err(codec,
4523 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4524 nid, val,
4525 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004526 return -EIO;
4527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 *ratesp = rates;
4529 }
4530
4531 if (formatsp || bpsp) {
4532 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004533 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004535 streams = query_stream_param(codec, nid);
4536 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539 bps = 0;
4540 if (streams & AC_SUPFMT_PCM) {
4541 if (val & AC_SUPPCM_BITS_8) {
4542 formats |= SNDRV_PCM_FMTBIT_U8;
4543 bps = 8;
4544 }
4545 if (val & AC_SUPPCM_BITS_16) {
4546 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4547 bps = 16;
4548 }
4549 if (wcaps & AC_WCAP_DIGITAL) {
4550 if (val & AC_SUPPCM_BITS_32)
4551 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4552 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4553 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4554 if (val & AC_SUPPCM_BITS_24)
4555 bps = 24;
4556 else if (val & AC_SUPPCM_BITS_20)
4557 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004558 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4559 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4561 if (val & AC_SUPPCM_BITS_32)
4562 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 else if (val & AC_SUPPCM_BITS_24)
4564 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004565 else if (val & AC_SUPPCM_BITS_20)
4566 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 }
4568 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004569#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004570 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004572 if (!bps)
4573 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004574 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004575#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004576 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004577 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /* temporary hack: we have still no proper support
4579 * for the direct AC3 stream...
4580 */
4581 formats |= SNDRV_PCM_FMTBIT_U8;
4582 bps = 8;
4583 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004584 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004585 codec_err(codec,
4586 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4587 nid, val,
4588 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4589 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004590 return -EIO;
4591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 if (formatsp)
4593 *formatsp = formats;
4594 if (bpsp)
4595 *bpsp = bps;
4596 }
4597
4598 return 0;
4599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004600EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004603 * snd_hda_is_supported_format - Check the validity of the format
4604 * @codec: HD-audio codec
4605 * @nid: NID to check
4606 * @format: the HD-audio format value to check
4607 *
4608 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 *
4610 * Returns 1 if supported, 0 if not.
4611 */
4612int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4613 unsigned int format)
4614{
4615 int i;
4616 unsigned int val = 0, rate, stream;
4617
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004618 val = query_pcm_param(codec, nid);
4619 if (!val)
4620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004623 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004624 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 if (val & (1 << i))
4626 break;
4627 return 0;
4628 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004629 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return 0;
4631
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004632 stream = query_stream_param(codec, nid);
4633 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 return 0;
4635
4636 if (stream & AC_SUPFMT_PCM) {
4637 switch (format & 0xf0) {
4638 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004639 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 return 0;
4641 break;
4642 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004643 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 return 0;
4645 break;
4646 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004647 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return 0;
4649 break;
4650 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004651 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 return 0;
4653 break;
4654 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004655 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 return 0;
4657 break;
4658 default:
4659 return 0;
4660 }
4661 } else {
4662 /* FIXME: check for float32 and AC3? */
4663 }
4664
4665 return 1;
4666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004667EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669/*
4670 * PCM stuff
4671 */
4672static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4673 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004674 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
4676 return 0;
4677}
4678
4679static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 unsigned int stream_tag,
4682 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004683 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
4685 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4686 return 0;
4687}
4688
4689static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4690 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004691 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
Takashi Iwai888afa12008-03-18 09:57:50 +01004693 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return 0;
4695}
4696
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004697static int set_pcm_default_values(struct hda_codec *codec,
4698 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004700 int err;
4701
Takashi Iwai0ba21762007-04-16 11:29:14 +02004702 /* query support PCM information from the given NID */
4703 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004704 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004705 info->rates ? NULL : &info->rates,
4706 info->formats ? NULL : &info->formats,
4707 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004708 if (err < 0)
4709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
4711 if (info->ops.open == NULL)
4712 info->ops.open = hda_pcm_default_open_close;
4713 if (info->ops.close == NULL)
4714 info->ops.close = hda_pcm_default_open_close;
4715 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004716 if (snd_BUG_ON(!info->nid))
4717 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 info->ops.prepare = hda_pcm_default_prepare;
4719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004721 if (snd_BUG_ON(!info->nid))
4722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 info->ops.cleanup = hda_pcm_default_cleanup;
4724 }
4725 return 0;
4726}
4727
Takashi Iwaieb541332010-08-06 13:48:11 +02004728/*
4729 * codec prepare/cleanup entries
4730 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004731/**
4732 * snd_hda_codec_prepare - Prepare a stream
4733 * @codec: the HDA codec
4734 * @hinfo: PCM information
4735 * @stream: stream tag to assign
4736 * @format: format id to assign
4737 * @substream: PCM substream to assign
4738 *
4739 * Calls the prepare callback set by the codec with the given arguments.
4740 * Clean up the inactive streams when successful.
4741 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004742int snd_hda_codec_prepare(struct hda_codec *codec,
4743 struct hda_pcm_stream *hinfo,
4744 unsigned int stream,
4745 unsigned int format,
4746 struct snd_pcm_substream *substream)
4747{
4748 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004749 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004750 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4751 if (ret >= 0)
4752 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004753 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004754 return ret;
4755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004756EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004757
Takashi Iwai95a962c2014-10-29 16:03:58 +01004758/**
4759 * snd_hda_codec_cleanup - Prepare a stream
4760 * @codec: the HDA codec
4761 * @hinfo: PCM information
4762 * @substream: PCM substream
4763 *
4764 * Calls the cleanup callback set by the codec with the given arguments.
4765 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004766void snd_hda_codec_cleanup(struct hda_codec *codec,
4767 struct hda_pcm_stream *hinfo,
4768 struct snd_pcm_substream *substream)
4769{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004770 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004771 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004772 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004773}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004774EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004775
Takashi Iwaid5191e52009-11-16 14:58:17 +01004776/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004777const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4778 "Audio", "SPDIF", "HDMI", "Modem"
4779};
4780
Takashi Iwai176d5332008-07-30 15:01:44 +02004781/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004782 * get the empty PCM device number to assign
4783 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004784static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004785{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004786 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004787 /* assigned to static slots up to dev#10; if more needed, assign
4788 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4789 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004790 static int audio_idx[HDA_PCM_NTYPES][5] = {
4791 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4792 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004793 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004794 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004795 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004796 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004797
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004798 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004799 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004800 return -EINVAL;
4801 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004802
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004803 for (i = 0; audio_idx[type][i] >= 0; i++) {
4804#ifndef CONFIG_SND_DYNAMIC_MINORS
4805 if (audio_idx[type][i] >= 8)
4806 break;
4807#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004808 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4809 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004810 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004811
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004812#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004813 /* non-fixed slots starting from 10 */
4814 for (i = 10; i < 32; i++) {
4815 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4816 return i;
4817 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004818#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004819
Takashi Iwai4e76a882014-02-25 12:21:03 +01004820 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004821 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004822#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004823 dev_warn(bus->card->dev,
4824 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004825#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004826 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004827}
4828
4829/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004830 * attach a new PCM stream
4831 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004832static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004833{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004834 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004835 struct hda_pcm_stream *info;
4836 int stream, err;
4837
Takashi Iwaib91f0802008-11-04 08:43:08 +01004838 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004839 return -EINVAL;
4840 for (stream = 0; stream < 2; stream++) {
4841 info = &pcm->stream[stream];
4842 if (info->substreams) {
4843 err = set_pcm_default_values(codec, info);
4844 if (err < 0)
4845 return err;
4846 }
4847 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004848 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004849}
4850
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004851/* assign all PCMs of the given codec */
4852int snd_hda_codec_build_pcms(struct hda_codec *codec)
4853{
4854 unsigned int pcm;
4855 int err;
4856
4857 if (!codec->num_pcms) {
4858 if (!codec->patch_ops.build_pcms)
4859 return 0;
4860 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004861 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004862 codec_err(codec,
4863 "cannot build PCMs for #%d (error %d)\n",
4864 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004865 err = snd_hda_codec_reset(codec);
4866 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004867 codec_err(codec,
4868 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004869 return err;
4870 }
4871 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004872 }
4873 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4874 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4875 int dev;
4876
4877 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004878 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004879
4880 if (!cpcm->pcm) {
4881 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4882 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004883 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004884 cpcm->device = dev;
4885 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004886 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004887 codec_err(codec,
4888 "cannot attach PCM stream %d for codec #%d\n",
4889 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004890 continue; /* no fatal error */
4891 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004892 }
4893 }
4894 return 0;
4895}
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897/**
4898 * snd_hda_build_pcms - build PCM information
4899 * @bus: the BUS
4900 *
4901 * Create PCM information for each codec included in the bus.
4902 *
4903 * The build_pcms codec patch is requested to set up codec->num_pcms and
4904 * codec->pcm_info properly. The array is referred by the top-level driver
4905 * to create its PCM instances.
4906 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4907 * callback.
4908 *
4909 * At least, substreams, channels_min and channels_max must be filled for
4910 * each stream. substreams = 0 indicates that the stream doesn't exist.
4911 * When rates and/or formats are zero, the supported values are queried
4912 * from the given nid. The nid is used also by the default ops.prepare
4913 * and ops.cleanup callbacks.
4914 *
4915 * The driver needs to call ops.open in its open callback. Similarly,
4916 * ops.close is supposed to be called in the close callback.
4917 * ops.prepare should be called in the prepare or hw_params callback
4918 * with the proper parameters for set up.
4919 * ops.cleanup should be called in hw_free for clean up of streams.
4920 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004921 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004923int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004925 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
Takashi Iwai0ba21762007-04-16 11:29:14 +02004927 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004928 int err = snd_hda_codec_build_pcms(codec);
4929 if (err < 0)
4930 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 }
4932 return 0;
4933}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004934EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 * snd_hda_add_new_ctls - create controls from the array
4938 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004939 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 *
4941 * This helper function creates and add new controls in the given array.
4942 * The array must be terminated with an empty entry as terminator.
4943 *
4944 * Returns 0 if successful, or a negative error code.
4945 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004946int snd_hda_add_new_ctls(struct hda_codec *codec,
4947 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004949 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
4951 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004952 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004953 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004954 if (knew->iface == -1) /* skip this codec private value */
4955 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004956 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004957 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004958 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004959 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004960 if (addr > 0)
4961 kctl->id.device = addr;
4962 if (idx > 0)
4963 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004964 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004965 if (!err)
4966 break;
4967 /* try first with another device index corresponding to
4968 * the codec addr; if it still fails (or it's the
4969 * primary codec), then try another control index
4970 */
4971 if (!addr && codec->addr)
4972 addr = codec->addr;
4973 else if (!idx && !knew->index) {
4974 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004975 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004976 if (idx <= 0)
4977 return err;
4978 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004979 return err;
4980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982 return 0;
4983}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004984EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwai83012a72012-08-24 18:38:08 +02004986#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004987static void hda_power_work(struct work_struct *work)
4988{
4989 struct hda_codec *codec =
4990 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004991 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004992 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004993
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004994 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004995 if (codec->power_transition > 0) { /* during power-up sequence? */
4996 spin_unlock(&codec->power_lock);
4997 return;
4998 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004999 if (!codec->power_on || codec->power_count) {
5000 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005001 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005002 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02005003 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005004 spin_unlock(&codec->power_lock);
5005
Dylan Reidd17344b2012-09-28 15:57:01 -07005006 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005007 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5008 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005009}
5010
5011static void hda_keep_power_on(struct hda_codec *codec)
5012{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005013 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005014 codec->power_count++;
5015 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005016 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005017 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005018 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005019}
5020
Takashi Iwaid5191e52009-11-16 14:58:17 +01005021/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005022void snd_hda_update_power_acct(struct hda_codec *codec)
5023{
5024 unsigned long delta = jiffies - codec->power_jiffies;
5025 if (codec->power_on)
5026 codec->power_on_acct += delta;
5027 else
5028 codec->power_off_acct += delta;
5029 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005030}
5031
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005032/* Transition to powered up, if wait_power_down then wait for a pending
5033 * transition to D3 to complete. A pending D3 transition is indicated
5034 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005035/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005036static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005037{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005038 /* Return if power_on or transitioning to power_on, unless currently
5039 * powering down. */
5040 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005041 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005042 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005043 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005044
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005045 cancel_delayed_work_sync(&codec->power_work);
5046
5047 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005048 /* If the power down delayed work was cancelled above before starting,
5049 * then there is no need to go through power up here.
5050 */
5051 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005052 if (codec->power_transition < 0)
5053 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005054 return;
5055 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005056
Takashi Iwaid66fee52011-08-02 15:39:31 +02005057 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005058 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005059 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005060 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005061 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005062 spin_unlock(&codec->power_lock);
5063
Takashi Iwaicb53c622007-08-10 17:21:45 +02005064 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005065
5066 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005067 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005068}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005069
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005070#define power_save(codec) \
5071 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005072
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005073/* Transition to powered down */
5074static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005075{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005076 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005077 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005078
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005079 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005080 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005081 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005082 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005083 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005084}
5085
5086/**
5087 * snd_hda_power_save - Power-up/down/sync the codec
5088 * @codec: HD-audio codec
5089 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005090 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005091 *
5092 * Change the power-up counter via @delta, and power up or down the hardware
5093 * appropriately. For the power-down, queue to the delayed action.
5094 * Passing zero to @delta means to synchronize the power state.
5095 */
5096void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5097{
5098 spin_lock(&codec->power_lock);
5099 codec->power_count += delta;
5100 trace_hda_power_count(codec);
5101 if (delta > 0)
5102 __snd_hda_power_up(codec, d3wait);
5103 else
5104 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005105 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005106}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005107EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005108
Takashi Iwaid5191e52009-11-16 14:58:17 +01005109/**
5110 * snd_hda_check_amp_list_power - Check the amp list and update the power
5111 * @codec: HD-audio codec
5112 * @check: the object containing an AMP list and the status
5113 * @nid: NID to check / update
5114 *
5115 * Check whether the given NID is in the amp list. If it's in the list,
5116 * check the current AMP status, and update the the power-status according
5117 * to the mute status.
5118 *
5119 * This function is supposed to be set or called from the check_power_status
5120 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005121 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005122int snd_hda_check_amp_list_power(struct hda_codec *codec,
5123 struct hda_loopback_check *check,
5124 hda_nid_t nid)
5125{
Takashi Iwai031024e2011-05-02 11:29:30 +02005126 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005127 int ch, v;
5128
5129 if (!check->amplist)
5130 return 0;
5131 for (p = check->amplist; p->nid; p++) {
5132 if (p->nid == nid)
5133 break;
5134 }
5135 if (!p->nid)
5136 return 0; /* nothing changed */
5137
5138 for (p = check->amplist; p->nid; p++) {
5139 for (ch = 0; ch < 2; ch++) {
5140 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5141 p->idx);
5142 if (!(v & HDA_AMP_MUTE) && v > 0) {
5143 if (!check->power_on) {
5144 check->power_on = 1;
5145 snd_hda_power_up(codec);
5146 }
5147 return 1;
5148 }
5149 }
5150 }
5151 if (check->power_on) {
5152 check->power_on = 0;
5153 snd_hda_power_down(codec);
5154 }
5155 return 0;
5156}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005157EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005160/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005161 * Channel mode helper
5162 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005163
5164/**
5165 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005166 * @codec: the HDA codec
5167 * @uinfo: pointer to get/store the data
5168 * @chmode: channel mode array
5169 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005171int snd_hda_ch_mode_info(struct hda_codec *codec,
5172 struct snd_ctl_elem_info *uinfo,
5173 const struct hda_channel_mode *chmode,
5174 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005175{
5176 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5177 uinfo->count = 1;
5178 uinfo->value.enumerated.items = num_chmodes;
5179 if (uinfo->value.enumerated.item >= num_chmodes)
5180 uinfo->value.enumerated.item = num_chmodes - 1;
5181 sprintf(uinfo->value.enumerated.name, "%dch",
5182 chmode[uinfo->value.enumerated.item].channels);
5183 return 0;
5184}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005185EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005186
Takashi Iwaid5191e52009-11-16 14:58:17 +01005187/**
5188 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005189 * @codec: the HDA codec
5190 * @ucontrol: pointer to get/store the data
5191 * @chmode: channel mode array
5192 * @num_chmodes: channel mode array size
5193 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005194 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005195int snd_hda_ch_mode_get(struct hda_codec *codec,
5196 struct snd_ctl_elem_value *ucontrol,
5197 const struct hda_channel_mode *chmode,
5198 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005199 int max_channels)
5200{
5201 int i;
5202
5203 for (i = 0; i < num_chmodes; i++) {
5204 if (max_channels == chmode[i].channels) {
5205 ucontrol->value.enumerated.item[0] = i;
5206 break;
5207 }
5208 }
5209 return 0;
5210}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005211EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005212
Takashi Iwaid5191e52009-11-16 14:58:17 +01005213/**
5214 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005215 * @codec: the HDA codec
5216 * @ucontrol: pointer to get/store the data
5217 * @chmode: channel mode array
5218 * @num_chmodes: channel mode array size
5219 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005220 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005221int snd_hda_ch_mode_put(struct hda_codec *codec,
5222 struct snd_ctl_elem_value *ucontrol,
5223 const struct hda_channel_mode *chmode,
5224 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005225 int *max_channelsp)
5226{
5227 unsigned int mode;
5228
5229 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005230 if (mode >= num_chmodes)
5231 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005232 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005233 return 0;
5234 /* change the current channel setting */
5235 *max_channelsp = chmode[mode].channels;
5236 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005237 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005238 return 1;
5239}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005240EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005241
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242/*
5243 * input MUX helper
5244 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005245
5246/**
5247 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005248 * @imux: imux helper object
5249 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005250 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005251int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5252 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
5254 unsigned int index;
5255
5256 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5257 uinfo->count = 1;
5258 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005259 if (!imux->num_items)
5260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 index = uinfo->value.enumerated.item;
5262 if (index >= imux->num_items)
5263 index = imux->num_items - 1;
5264 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5265 return 0;
5266}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005267EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Takashi Iwaid5191e52009-11-16 14:58:17 +01005269/**
5270 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005271 * @codec: the HDA codec
5272 * @imux: imux helper object
5273 * @ucontrol: pointer to get/store the data
5274 * @nid: input mux NID
5275 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005277int snd_hda_input_mux_put(struct hda_codec *codec,
5278 const struct hda_input_mux *imux,
5279 struct snd_ctl_elem_value *ucontrol,
5280 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 unsigned int *cur_val)
5282{
5283 unsigned int idx;
5284
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005285 if (!imux->num_items)
5286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 idx = ucontrol->value.enumerated.item[0];
5288 if (idx >= imux->num_items)
5289 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005290 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005292 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5293 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 *cur_val = idx;
5295 return 1;
5296}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005297EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005300/**
5301 * snd_hda_enum_helper_info - Helper for simple enum ctls
5302 * @kcontrol: ctl element
5303 * @uinfo: pointer to get/store the data
5304 * @num_items: number of enum items
5305 * @texts: enum item string array
5306 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005307 * process kcontrol info callback of a simple string enum array
5308 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5309 */
5310int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5311 struct snd_ctl_elem_info *uinfo,
5312 int num_items, const char * const *texts)
5313{
5314 static const char * const texts_default[] = {
5315 "Disabled", "Enabled"
5316 };
5317
5318 if (!texts || !num_items) {
5319 num_items = 2;
5320 texts = texts_default;
5321 }
5322
Takashi Iwai3ff72212014-10-20 18:17:28 +02005323 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005324}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005325EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005326
5327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 * Multi-channel / digital-out PCM helper functions
5329 */
5330
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005331/* setup SPDIF output stream */
5332static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5333 unsigned int stream_tag, unsigned int format)
5334{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005335 struct hda_spdif_out *spdif;
5336 unsigned int curr_fmt;
5337 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005338
Laurence Darby3bef1c32012-11-03 17:00:06 +00005339 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5340 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5341 AC_VERB_GET_STREAM_FORMAT, 0);
5342 reset = codec->spdif_status_reset &&
5343 (spdif->ctls & AC_DIG1_ENABLE) &&
5344 curr_fmt != format;
5345
5346 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5347 updated */
5348 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005349 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005350 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005351 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005352 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005353 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005354 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005355 for (d = codec->slave_dig_outs; *d; d++)
5356 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5357 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005358 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005359 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005360 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005361 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005362 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005363}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005364
Takashi Iwai2f728532008-09-25 16:32:41 +02005365static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5366{
5367 snd_hda_codec_cleanup_stream(codec, nid);
5368 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005369 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005370 for (d = codec->slave_dig_outs; *d; d++)
5371 snd_hda_codec_cleanup_stream(codec, *d);
5372 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005373}
5374
Takashi Iwaid5191e52009-11-16 14:58:17 +01005375/**
5376 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5377 * @bus: HD-audio bus
5378 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005379void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5380{
5381 struct hda_codec *codec;
5382
5383 if (!bus)
5384 return;
5385 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005386 if (hda_codec_is_power_on(codec) &&
5387 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005388 codec->patch_ops.reboot_notify(codec);
5389 }
5390}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005391EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005392
Takashi Iwaid5191e52009-11-16 14:58:17 +01005393/**
5394 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005395 * @codec: the HDA codec
5396 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005398int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5399 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Ingo Molnar62932df2006-01-16 16:34:20 +01005401 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005402 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5403 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005404 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005406 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return 0;
5408}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005409EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Takashi Iwaid5191e52009-11-16 14:58:17 +01005411/**
5412 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005413 * @codec: the HDA codec
5414 * @mout: hda_multi_out object
5415 * @stream_tag: stream tag to assign
5416 * @format: format id to assign
5417 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005418 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005419int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5420 struct hda_multi_out *mout,
5421 unsigned int stream_tag,
5422 unsigned int format,
5423 struct snd_pcm_substream *substream)
5424{
5425 mutex_lock(&codec->spdif_mutex);
5426 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5427 mutex_unlock(&codec->spdif_mutex);
5428 return 0;
5429}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005430EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005431
Takashi Iwaid5191e52009-11-16 14:58:17 +01005432/**
5433 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005434 * @codec: the HDA codec
5435 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005436 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005437int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5438 struct hda_multi_out *mout)
5439{
5440 mutex_lock(&codec->spdif_mutex);
5441 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5442 mutex_unlock(&codec->spdif_mutex);
5443 return 0;
5444}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005445EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005446
Takashi Iwaid5191e52009-11-16 14:58:17 +01005447/**
5448 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005449 * @codec: the HDA codec
5450 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005452int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5453 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454{
Ingo Molnar62932df2006-01-16 16:34:20 +01005455 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005457 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 return 0;
5459}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005460EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Takashi Iwaid5191e52009-11-16 14:58:17 +01005462/**
5463 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005464 * @codec: the HDA codec
5465 * @mout: hda_multi_out object
5466 * @substream: PCM substream to assign
5467 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005468 *
5469 * Open analog outputs and set up the hw-constraints.
5470 * If the digital outputs can be opened as slave, open the digital
5471 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005473int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5474 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005475 struct snd_pcm_substream *substream,
5476 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Takashi Iwai9a081602008-02-12 18:37:26 +01005478 struct snd_pcm_runtime *runtime = substream->runtime;
5479 runtime->hw.channels_max = mout->max_channels;
5480 if (mout->dig_out_nid) {
5481 if (!mout->analog_rates) {
5482 mout->analog_rates = hinfo->rates;
5483 mout->analog_formats = hinfo->formats;
5484 mout->analog_maxbps = hinfo->maxbps;
5485 } else {
5486 runtime->hw.rates = mout->analog_rates;
5487 runtime->hw.formats = mout->analog_formats;
5488 hinfo->maxbps = mout->analog_maxbps;
5489 }
5490 if (!mout->spdif_rates) {
5491 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5492 &mout->spdif_rates,
5493 &mout->spdif_formats,
5494 &mout->spdif_maxbps);
5495 }
5496 mutex_lock(&codec->spdif_mutex);
5497 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005498 if ((runtime->hw.rates & mout->spdif_rates) &&
5499 (runtime->hw.formats & mout->spdif_formats)) {
5500 runtime->hw.rates &= mout->spdif_rates;
5501 runtime->hw.formats &= mout->spdif_formats;
5502 if (mout->spdif_maxbps < hinfo->maxbps)
5503 hinfo->maxbps = mout->spdif_maxbps;
5504 } else {
5505 mout->share_spdif = 0;
5506 /* FIXME: need notify? */
5507 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005508 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005509 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5512 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5513}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005514EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Takashi Iwaid5191e52009-11-16 14:58:17 +01005516/**
5517 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005518 * @codec: the HDA codec
5519 * @mout: hda_multi_out object
5520 * @stream_tag: stream tag to assign
5521 * @format: format id to assign
5522 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005523 *
5524 * Set up the i/o for analog out.
5525 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005527int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5528 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 unsigned int stream_tag,
5530 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005531 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Takashi Iwaidda14412011-05-02 11:29:30 +02005533 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005535 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 int i;
5537
Ingo Molnar62932df2006-01-16 16:34:20 +01005538 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005539 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005540 if (mout->dig_out_nid && mout->share_spdif &&
5541 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005543 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5544 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005545 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005547 setup_dig_out_stream(codec, mout->dig_out_nid,
5548 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 } else {
5550 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005551 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 }
5553 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005554 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005557 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5558 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005559 if (!mout->no_share_stream &&
5560 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005562 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5563 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005564 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005565 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5566 if (!mout->no_share_stream && mout->hp_out_nid[i])
5567 snd_hda_codec_setup_stream(codec,
5568 mout->hp_out_nid[i],
5569 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 /* surrounds */
5572 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005573 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005574 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5575 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005576 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005577 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5578 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005580
5581 /* extra surrounds */
5582 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5583 int ch = 0;
5584 if (!mout->extra_out_nid[i])
5585 break;
5586 if (chs >= (i + 1) * 2)
5587 ch = i * 2;
5588 else if (!mout->no_share_stream)
5589 break;
5590 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5591 stream_tag, ch, format);
5592 }
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return 0;
5595}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005596EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Takashi Iwaid5191e52009-11-16 14:58:17 +01005598/**
5599 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005600 * @codec: the HDA codec
5601 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005603int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5604 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Takashi Iwaidda14412011-05-02 11:29:30 +02005606 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 int i;
5608
5609 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005610 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005612 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005613 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5614 if (mout->hp_out_nid[i])
5615 snd_hda_codec_cleanup_stream(codec,
5616 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005617 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5618 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005619 snd_hda_codec_cleanup_stream(codec,
5620 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005621 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005623 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 mout->dig_out_used = 0;
5625 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005626 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return 0;
5628}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005629EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Takashi Iwai47408602012-04-20 13:06:53 +02005631/**
5632 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005633 * @codec: the HDA codec
5634 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005635 *
5636 * Guess the suitable VREF pin bits to be set as the pin-control value.
5637 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5638 */
5639unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5640{
5641 unsigned int pincap;
5642 unsigned int oldval;
5643 oldval = snd_hda_codec_read(codec, pin, 0,
5644 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5645 pincap = snd_hda_query_pin_caps(codec, pin);
5646 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5647 /* Exception: if the default pin setup is vref50, we give it priority */
5648 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5649 return AC_PINCTL_VREF_80;
5650 else if (pincap & AC_PINCAP_VREF_50)
5651 return AC_PINCTL_VREF_50;
5652 else if (pincap & AC_PINCAP_VREF_100)
5653 return AC_PINCTL_VREF_100;
5654 else if (pincap & AC_PINCAP_VREF_GRD)
5655 return AC_PINCTL_VREF_GRD;
5656 return AC_PINCTL_VREF_HIZ;
5657}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005658EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005659
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005660/**
5661 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5662 * @codec: the HDA codec
5663 * @pin: referred pin NID
5664 * @val: pin ctl value to audit
5665 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005666unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5667 hda_nid_t pin, unsigned int val)
5668{
5669 static unsigned int cap_lists[][2] = {
5670 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5671 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5672 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5673 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5674 };
5675 unsigned int cap;
5676
5677 if (!val)
5678 return 0;
5679 cap = snd_hda_query_pin_caps(codec, pin);
5680 if (!cap)
5681 return val; /* don't know what to do... */
5682
5683 if (val & AC_PINCTL_OUT_EN) {
5684 if (!(cap & AC_PINCAP_OUT))
5685 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5686 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5687 val &= ~AC_PINCTL_HP_EN;
5688 }
5689
5690 if (val & AC_PINCTL_IN_EN) {
5691 if (!(cap & AC_PINCAP_IN))
5692 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5693 else {
5694 unsigned int vcap, vref;
5695 int i;
5696 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5697 vref = val & AC_PINCTL_VREFEN;
5698 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5699 if (vref == cap_lists[i][0] &&
5700 !(vcap & cap_lists[i][1])) {
5701 if (i == ARRAY_SIZE(cap_lists) - 1)
5702 vref = AC_PINCTL_VREF_HIZ;
5703 else
5704 vref = cap_lists[i + 1][0];
5705 }
5706 }
5707 val &= ~AC_PINCTL_VREFEN;
5708 val |= vref;
5709 }
5710 }
5711
5712 return val;
5713}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005714EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005715
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005716/**
5717 * _snd_hda_pin_ctl - Helper to set pin ctl value
5718 * @codec: the HDA codec
5719 * @pin: referred pin NID
5720 * @val: pin control value to set
5721 * @cached: access over codec pinctl cache or direct write
5722 *
5723 * This function is a helper to set a pin ctl value more safely.
5724 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5725 * value in pin target array via snd_hda_codec_set_pin_target(), then
5726 * actually writes the value via either snd_hda_codec_update_cache() or
5727 * snd_hda_codec_write() depending on @cached flag.
5728 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005729int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5730 unsigned int val, bool cached)
5731{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005732 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005733 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005734 if (cached)
5735 return snd_hda_codec_update_cache(codec, pin, 0,
5736 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5737 else
5738 return snd_hda_codec_write(codec, pin, 0,
5739 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5740}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005741EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005742
Takashi Iwai990061c2010-09-09 22:08:44 +02005743/**
5744 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005745 * @codec: the HDA codec
5746 * @imux: imux helper object
5747 * @label: the name of imux item to assign
5748 * @index: index number of imux item to assign
5749 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005750 *
5751 * When the same label is used already in the existing items, the number
5752 * suffix is appended to the label. This label index number is stored
5753 * to type_idx when non-NULL pointer is given.
5754 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005755int snd_hda_add_imux_item(struct hda_codec *codec,
5756 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005757 int index, int *type_idx)
5758{
5759 int i, label_idx = 0;
5760 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005761 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005762 return -EINVAL;
5763 }
5764 for (i = 0; i < imux->num_items; i++) {
5765 if (!strncmp(label, imux->items[i].label, strlen(label)))
5766 label_idx++;
5767 }
5768 if (type_idx)
5769 *type_idx = label_idx;
5770 if (label_idx > 0)
5771 snprintf(imux->items[imux->num_items].label,
5772 sizeof(imux->items[imux->num_items].label),
5773 "%s %d", label, label_idx);
5774 else
5775 strlcpy(imux->items[imux->num_items].label, label,
5776 sizeof(imux->items[imux->num_items].label));
5777 imux->items[imux->num_items].index = index;
5778 imux->num_items++;
5779 return 0;
5780}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005781EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005782
Takashi Iwai4a471b72005-12-07 13:56:29 +01005783
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784#ifdef CONFIG_PM
5785/*
5786 * power management
5787 */
5788
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005789
5790static void hda_async_suspend(void *data, async_cookie_t cookie)
5791{
5792 hda_call_codec_suspend(data, false);
5793}
5794
5795static void hda_async_resume(void *data, async_cookie_t cookie)
5796{
5797 hda_call_codec_resume(data);
5798}
5799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800/**
5801 * snd_hda_suspend - suspend the codecs
5802 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 *
5804 * Returns 0 if successful.
5805 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005806int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005808 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005809 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Takashi Iwai0ba21762007-04-16 11:29:14 +02005811 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005812 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005813 if (hda_codec_is_power_on(codec)) {
5814 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005815 async_schedule_domain(hda_async_suspend, codec,
5816 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005817 else
5818 hda_call_codec_suspend(codec, false);
5819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005821
5822 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005823 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return 0;
5826}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005827EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
5829/**
5830 * snd_hda_resume - resume the codecs
5831 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 *
5833 * Returns 0 if successful.
5834 */
5835int snd_hda_resume(struct hda_bus *bus)
5836{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005837 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005838 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Takashi Iwai0ba21762007-04-16 11:29:14 +02005840 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005841 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005842 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005843 else
5844 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005846
5847 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005848 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005849
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 return 0;
5851}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005852EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005853#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005854
5855/*
5856 * generic arrays
5857 */
5858
Takashi Iwaid5191e52009-11-16 14:58:17 +01005859/**
5860 * snd_array_new - get a new element from the given array
5861 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005862 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005863 * Get a new element from the given array. If it exceeds the
5864 * pre-allocated array size, re-allocate the array.
5865 *
5866 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005867 */
5868void *snd_array_new(struct snd_array *array)
5869{
Takashi Iwai12f17712012-10-10 08:59:14 +02005870 if (snd_BUG_ON(!array->elem_size))
5871 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005872 if (array->used >= array->alloced) {
5873 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005874 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005875 void *nlist;
5876 if (snd_BUG_ON(num >= 4096))
5877 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005878 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005879 if (!nlist)
5880 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005881 array->list = nlist;
5882 array->alloced = num;
5883 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005884 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005885}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005886EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005887
Takashi Iwaid5191e52009-11-16 14:58:17 +01005888/**
5889 * snd_array_free - free the given array elements
5890 * @array: the array object
5891 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005892void snd_array_free(struct snd_array *array)
5893{
5894 kfree(array->list);
5895 array->used = 0;
5896 array->alloced = 0;
5897 array->list = NULL;
5898}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005899EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005900
Takashi Iwaid5191e52009-11-16 14:58:17 +01005901/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005902 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5903 * @pcm: PCM caps bits
5904 * @buf: the string buffer to write
5905 * @buflen: the max buffer length
5906 *
5907 * used by hda_proc.c and hda_eld.c
5908 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005909void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5910{
5911 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5912 int i, j;
5913
5914 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5915 if (pcm & (AC_SUPPCM_BITS_8 << i))
5916 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5917
5918 buf[j] = '\0'; /* necessary when j == 0 */
5919}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005920EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005921
5922MODULE_DESCRIPTION("HDA codec core");
5923MODULE_LICENSE("GPL");