blob: ca98f5209f8fbe2123380c324c9a8ad8b6caf332 [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);
Danny Tholene8a7f132007-09-11 21:41:56 +0200349 if (parm == -1)
350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *start_id = (parm >> 16) & 0x7fff;
352 return (int)(parm & 0x7fff);
353}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100354EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100356/* connection list element */
357struct hda_conn_list {
358 struct list_head list;
359 int len;
360 hda_nid_t nid;
361 hda_nid_t conns[0];
362};
363
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200364/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100365static struct hda_conn_list *
366lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200367{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100368 struct hda_conn_list *p;
369 list_for_each_entry(p, &codec->conn_list, list) {
370 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200371 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 }
373 return NULL;
374}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200375
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100376static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
377 const hda_nid_t *list)
378{
379 struct hda_conn_list *p;
380
381 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
382 if (!p)
383 return -ENOMEM;
384 p->len = len;
385 p->nid = nid;
386 memcpy(p->conns, list, len * sizeof(hda_nid_t));
387 list_add(&p->list, &codec->conn_list);
388 return 0;
389}
390
391static void remove_conn_list(struct hda_codec *codec)
392{
393 while (!list_empty(&codec->conn_list)) {
394 struct hda_conn_list *p;
395 p = list_first_entry(&codec->conn_list, typeof(*p), list);
396 list_del(&p->list);
397 kfree(p);
398 }
399}
400
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200401/* read the connection and add to the cache */
402static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100404 hda_nid_t list[32];
405 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200406 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100409 if (len == -ENOSPC) {
410 len = snd_hda_get_num_raw_conns(codec, nid);
411 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
412 if (!result)
413 return -ENOMEM;
414 len = snd_hda_get_raw_connections(codec, nid, result, len);
415 }
416 if (len >= 0)
417 len = snd_hda_override_conn_list(codec, nid, len, result);
418 if (result != list)
419 kfree(result);
420 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200421}
Takashi Iwaidce20792011-06-24 14:10:28 +0200422
423/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100424 * snd_hda_get_conn_list - get connection list
425 * @codec: the HDA codec
426 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100427 * @listp: the pointer to store NID list
428 *
429 * Parses the connection list of the given widget and stores the pointer
430 * to the list of NIDs.
431 *
432 * Returns the number of connections, or a negative error code.
433 *
434 * Note that the returned pointer isn't protected against the list
435 * modification. If snd_hda_override_conn_list() might be called
436 * concurrently, protect with a mutex appropriately.
437 */
438int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
439 const hda_nid_t **listp)
440{
441 bool added = false;
442
443 for (;;) {
444 int err;
445 const struct hda_conn_list *p;
446
447 /* if the connection-list is already cached, read it */
448 p = lookup_conn_list(codec, nid);
449 if (p) {
450 if (listp)
451 *listp = p->conns;
452 return p->len;
453 }
454 if (snd_BUG_ON(added))
455 return -EINVAL;
456
457 err = read_and_add_raw_conns(codec, nid);
458 if (err < 0)
459 return err;
460 added = true;
461 }
462}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100463EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464
465/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200466 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * @codec: the HDA codec
468 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * @max_conns: max. number of connections to store
471 *
472 * Parses the connection list of the given widget and stores the list
473 * of NIDs.
474 *
475 * Returns the number of connections, or a negative error code.
476 */
477int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200478 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200479{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100480 const hda_nid_t *list;
481 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200482
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100483 if (len > 0 && conn_list) {
484 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100485 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200486 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200487 return -EINVAL;
488 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100489 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200490 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200491
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100492 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200493}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100494EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200495
Takashi Iwai4eea3092013-02-07 18:18:19 +0100496/* return CONNLIST_LEN parameter of the given widget */
497static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
498{
499 unsigned int wcaps = get_wcaps(codec, nid);
500 unsigned int parm;
501
502 if (!(wcaps & AC_WCAP_CONN_LIST) &&
503 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
504 return 0;
505
506 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
507 if (parm == -1)
508 parm = 0;
509 return parm;
510}
511
512int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
513{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100514 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100515}
516
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200517/**
518 * snd_hda_get_raw_connections - copy connection list without cache
519 * @codec: the HDA codec
520 * @nid: NID to parse
521 * @conn_list: connection list array
522 * @max_conns: max. number of connections to store
523 *
524 * Like snd_hda_get_connections(), copy the connection list but without
525 * checking through the connection-list cache.
526 * Currently called only from hda_proc.c, so not exported.
527 */
528int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
529 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100532 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100535 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Takashi Iwai4eea3092013-02-07 18:18:19 +0100537 parm = get_num_conns(codec, nid);
538 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200539 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (parm & AC_CLIST_LONG) {
542 /* long form */
543 shift = 16;
544 num_elems = 2;
545 } else {
546 /* short form */
547 shift = 8;
548 num_elems = 4;
549 }
550 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 mask = (1 << (shift-1)) - 1;
552
Takashi Iwai0ba21762007-04-16 11:29:14 +0200553 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0; /* no connection */
555
556 if (conn_len == 1) {
557 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100562 if (conn_list)
563 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 1;
565 }
566
567 /* multi connection */
568 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100569 prev_nid = 0;
570 for (i = 0; i < conn_len; i++) {
571 int range_val;
572 hda_nid_t val, n;
573
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200574 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100575 parm = snd_hda_codec_read(codec, nid, 0,
576 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200577 if (parm == -1 && codec->bus->rirb_error)
578 return -EIO;
579 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200580 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100581 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100582 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100583 codec_dbg(codec,
584 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200585 nid, i, parm);
586 return 0;
587 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 parm >>= shift;
589 if (range_val) {
590 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200591 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100592 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200593 "invalid dep_range_val %x:%x\n",
594 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100595 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100598 if (conn_list) {
599 if (conns >= max_conns)
600 return -ENOSPC;
601 conn_list[conns] = n;
602 }
603 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100604 }
605 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100606 if (conn_list) {
607 if (conns >= max_conns)
608 return -ENOSPC;
609 conn_list[conns] = val;
610 }
611 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100613 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 return conns;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619 * snd_hda_override_conn_list - add/modify the connection-list to cache
620 * @codec: the HDA codec
621 * @nid: NID to parse
622 * @len: number of connection list entries
623 * @list: the list of connection entries
624 *
625 * Add or modify the given connection-list to the cache. If the corresponding
626 * cache already exists, invalidate it and append a new one.
627 *
628 * Returns zero or a negative error code.
629 */
630int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
631 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100633 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200634
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100635 p = lookup_conn_list(codec, nid);
636 if (p) {
637 list_del(&p->list);
638 kfree(p);
639 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200640
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100643EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200644
645/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 * snd_hda_get_conn_index - get the connection index of the given NID
647 * @codec: the HDA codec
648 * @mux: NID containing the list
649 * @nid: NID to select
650 * @recursive: 1 when searching NID recursively, otherwise 0
651 *
652 * Parses the connection list of the widget @mux and checks whether the
653 * widget @nid is present. If it is, return the connection index.
654 * Otherwise it returns -1.
655 */
656int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
657 hda_nid_t nid, int recursive)
658{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100659 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 int i, nums;
661
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100662 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 for (i = 0; i < nums; i++)
664 if (conn[i] == nid)
665 return i;
666 if (!recursive)
667 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100668 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100669 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200670 return -1;
671 }
672 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200673 for (i = 0; i < nums; i++) {
674 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
675 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
676 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200677 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
678 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200679 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200680 return -1;
681}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100682EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Mengdong Linf1aa0682013-08-26 21:35:21 -0400684
685/* return DEVLIST_LEN parameter of the given widget */
686static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
687{
688 unsigned int wcaps = get_wcaps(codec, nid);
689 unsigned int parm;
690
691 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
692 get_wcaps_type(wcaps) != AC_WID_PIN)
693 return 0;
694
695 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
696 if (parm == -1 && codec->bus->rirb_error)
697 parm = 0;
698 return parm & AC_DEV_LIST_LEN_MASK;
699}
700
701/**
702 * snd_hda_get_devices - copy device list without cache
703 * @codec: the HDA codec
704 * @nid: NID of the pin to parse
705 * @dev_list: device list array
706 * @max_devices: max. number of devices to store
707 *
708 * Copy the device list. This info is dynamic and so not cached.
709 * Currently called only from hda_proc.c, so not exported.
710 */
711int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
712 u8 *dev_list, int max_devices)
713{
714 unsigned int parm;
715 int i, dev_len, devices;
716
717 parm = get_num_devices(codec, nid);
718 if (!parm) /* not multi-stream capable */
719 return 0;
720
721 dev_len = parm + 1;
722 dev_len = dev_len < max_devices ? dev_len : max_devices;
723
724 devices = 0;
725 while (devices < dev_len) {
726 parm = snd_hda_codec_read(codec, nid, 0,
727 AC_VERB_GET_DEVICE_LIST, devices);
728 if (parm == -1 && codec->bus->rirb_error)
729 break;
730
731 for (i = 0; i < 8; i++) {
732 dev_list[devices] = (u8)parm;
733 parm >>= 4;
734 devices++;
735 if (devices >= dev_len)
736 break;
737 }
738 }
739 return devices;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/**
743 * snd_hda_queue_unsol_event - add an unsolicited event to queue
744 * @bus: the BUS
745 * @res: unsolicited event (lower 32bit of RIRB entry)
746 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
747 *
748 * Adds the given event to the queue. The events are processed in
749 * the workqueue asynchronously. Call this function in the interrupt
750 * hanlder when RIRB receives an unsolicited event.
751 *
752 * Returns 0 if successful, or a negative error code.
753 */
754int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
755{
756 struct hda_bus_unsolicited *unsol;
757 unsigned int wp;
758
Wang YanQing2195b062013-05-07 11:27:33 +0800759 if (!bus || !bus->workq)
760 return 0;
761
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200762 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 unsol = bus->unsol;
764 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766
767 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
768 unsol->wp = wp;
769
770 wp <<= 1;
771 unsol->queue[wp] = res;
772 unsol->queue[wp + 1] = res_ex;
773
Takashi Iwai6acaed32009-01-12 10:09:24 +0100774 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return 0;
777}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100778EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800781 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
David Howellsc4028952006-11-22 14:57:56 +0000783static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Howellsc4028952006-11-22 14:57:56 +0000785 struct hda_bus_unsolicited *unsol =
786 container_of(work, struct hda_bus_unsolicited, work);
787 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct hda_codec *codec;
789 unsigned int rp, caddr, res;
790
791 while (unsol->rp != unsol->wp) {
792 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
793 unsol->rp = rp;
794 rp <<= 1;
795 res = unsol->queue[rp];
796 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200797 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 continue;
799 codec = bus->caddr_tbl[caddr & 0x0f];
800 if (codec && codec->patch_ops.unsol_event)
801 codec->patch_ops.unsol_event(codec, res);
802 }
803}
804
805/*
806 * initialize unsolicited queue
807 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200808static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct hda_bus_unsolicited *unsol;
811
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100812 if (bus->unsol) /* already initialized */
813 return 0;
814
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200815 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200816 if (!unsol) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100817 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -ENOMEM;
819 }
David Howellsc4028952006-11-22 14:57:56 +0000820 INIT_WORK(&unsol->work, process_unsol_events);
821 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 bus->unsol = unsol;
823 return 0;
824}
825
826/*
827 * destructor
828 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100829static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200831 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100832 return;
833
834 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100835 if (bus->workq)
836 flush_workqueue(bus->workq);
837 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree(bus->unsol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (bus->ops.private_free)
840 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100841 if (bus->workq)
842 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100847static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwai2565c892014-02-19 11:41:09 +0100849 snd_hda_bus_free(device->device_data);
850 return 0;
851}
852
853static int snd_hda_bus_dev_disconnect(struct snd_device *device)
854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100856 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/**
861 * snd_hda_bus_new - create a HDA bus
862 * @card: the card entry
863 * @temp: the template for hda_bus information
864 * @busp: the pointer to store the created bus instance
865 *
866 * Returns 0 if successful, or a negative error code.
867 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100868int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200869 const struct hda_bus_template *temp,
870 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct hda_bus *bus;
873 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100874 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100875 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .dev_free = snd_hda_bus_dev_free,
877 };
878
Takashi Iwaida3cec32008-08-08 17:12:14 +0200879 if (snd_BUG_ON(!temp))
880 return -EINVAL;
881 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (busp)
885 *busp = NULL;
886
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200887 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100889 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -ENOMEM;
891 }
892
893 bus->card = card;
894 bus->private_data = temp->private_data;
895 bus->pci = temp->pci;
896 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100897 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 bus->ops = temp->ops;
899
Ingo Molnar62932df2006-01-16 16:34:20 +0100900 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200901 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 INIT_LIST_HEAD(&bus->codec_list);
903
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100904 snprintf(bus->workq_name, sizeof(bus->workq_name),
905 "hd-audio%d", card->number);
906 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100907 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100908 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100909 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100910 kfree(bus);
911 return -ENOMEM;
912 }
913
Takashi Iwai0ba21762007-04-16 11:29:14 +0200914 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
915 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 snd_hda_bus_free(bus);
917 return err;
918 }
919 if (busp)
920 *busp = bus;
921 return 0;
922}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100923EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100925#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200926#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200927 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200928#else
929#define is_generic_config(codec) 0
930#endif
931
Takashi Iwai645f10c2008-11-28 15:07:37 +0100932#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100933#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
934#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100935#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100936#endif
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * find a matching codec preset
940 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200941static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200942find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100944 struct hda_codec_preset_list *tbl;
945 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200946 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100948 again:
949 mutex_lock(&preset_mutex);
950 list_for_each_entry(tbl, &hda_preset_tables, list) {
951 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100952 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100953 continue;
954 }
955 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100957 if (preset->afg && preset->afg != codec->afg)
958 continue;
959 if (preset->mfg && preset->mfg != codec->mfg)
960 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200961 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200963 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200964 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100965 preset->rev == codec->revision_id)) {
966 mutex_unlock(&preset_mutex);
967 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100971 module_put(tbl->owner);
972 }
973 mutex_unlock(&preset_mutex);
974
975 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100976 if (!mod_requested)
Takashi Iwai66797f32014-10-27 15:15:44 +0100977 request_module("snd-hda-codec-id:%08x",
978 codec->vendor_id);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100979 else
Takashi Iwai66797f32014-10-27 15:15:44 +0100980 request_module("snd-hda-codec-id:%04x*",
981 (codec->vendor_id >> 16) & 0xffff);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100982 mod_requested++;
983 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 return NULL;
986}
987
988/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200989 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200991static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 const struct hda_vendor_id *c;
994 const char *vendor = NULL;
995 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200996 char tmp[16];
997
998 if (codec->vendor_name)
999 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 for (c = hda_vendor_ids; c->id; c++) {
1002 if (c->id == vendor_id) {
1003 vendor = c->name;
1004 break;
1005 }
1006 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001007 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 sprintf(tmp, "Generic %04x", vendor_id);
1009 vendor = tmp;
1010 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001011 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1012 if (!codec->vendor_name)
1013 return -ENOMEM;
1014
1015 get_chip_name:
1016 if (codec->chip_name)
1017 return 0;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001020 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1021 else {
1022 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1023 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1024 }
1025 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001026 return -ENOMEM;
1027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001031 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001033static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001035 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 hda_nid_t nid;
1037
1038 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1039 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001040 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001041 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001042 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001043 case AC_GRP_AUDIO_FUNCTION:
1044 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001045 codec->afg_function_id = function_id & 0xff;
1046 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001047 break;
1048 case AC_GRP_MODEM_FUNCTION:
1049 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001050 codec->mfg_function_id = function_id & 0xff;
1051 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001052 break;
1053 default:
1054 break;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001060 * read widget caps for each widget and store in cache
1061 */
1062static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1063{
1064 int i;
1065 hda_nid_t nid;
1066
1067 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1068 &codec->start_nid);
1069 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001070 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001071 return -ENOMEM;
1072 nid = codec->start_nid;
1073 for (i = 0; i < codec->num_nodes; i++, nid++)
1074 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1075 AC_PAR_AUDIO_WIDGET_CAP);
1076 return 0;
1077}
1078
Takashi Iwai3be14142009-02-20 14:11:16 +01001079/* read all pin default configurations and save codec->init_pins */
1080static int read_pin_defaults(struct hda_codec *codec)
1081{
1082 int i;
1083 hda_nid_t nid = codec->start_nid;
1084
1085 for (i = 0; i < codec->num_nodes; i++, nid++) {
1086 struct hda_pincfg *pin;
1087 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001088 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001089 if (wid_type != AC_WID_PIN)
1090 continue;
1091 pin = snd_array_new(&codec->init_pins);
1092 if (!pin)
1093 return -ENOMEM;
1094 pin->nid = nid;
1095 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1096 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001097 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1098 AC_VERB_GET_PIN_WIDGET_CONTROL,
1099 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001100 }
1101 return 0;
1102}
1103
1104/* look up the given pin config list and return the item matching with NID */
1105static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1106 struct snd_array *array,
1107 hda_nid_t nid)
1108{
1109 int i;
1110 for (i = 0; i < array->used; i++) {
1111 struct hda_pincfg *pin = snd_array_elem(array, i);
1112 if (pin->nid == nid)
1113 return pin;
1114 }
1115 return NULL;
1116}
1117
Takashi Iwai3be14142009-02-20 14:11:16 +01001118/* set the current pin config value for the given NID.
1119 * the value is cached, and read via snd_hda_codec_get_pincfg()
1120 */
1121int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1122 hda_nid_t nid, unsigned int cfg)
1123{
1124 struct hda_pincfg *pin;
1125
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001126 /* the check below may be invalid when pins are added by a fixup
1127 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1128 * for now
1129 */
1130 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001131 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1132 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001133 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001134
Takashi Iwai3be14142009-02-20 14:11:16 +01001135 pin = look_up_pincfg(codec, list, nid);
1136 if (!pin) {
1137 pin = snd_array_new(list);
1138 if (!pin)
1139 return -ENOMEM;
1140 pin->nid = nid;
1141 }
1142 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001143 return 0;
1144}
1145
Takashi Iwaid5191e52009-11-16 14:58:17 +01001146/**
1147 * snd_hda_codec_set_pincfg - Override a pin default configuration
1148 * @codec: the HDA codec
1149 * @nid: NID to set the pin config
1150 * @cfg: the pin default config value
1151 *
1152 * Override a pin default configuration value in the cache.
1153 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1154 * priority than the real hardware value.
1155 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001156int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1157 hda_nid_t nid, unsigned int cfg)
1158{
Takashi Iwai346ff702009-02-23 09:42:57 +01001159 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001160}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001161EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001162
Takashi Iwaid5191e52009-11-16 14:58:17 +01001163/**
1164 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1165 * @codec: the HDA codec
1166 * @nid: NID to get the pin config
1167 *
1168 * Get the current pin config value of the given pin NID.
1169 * If the pincfg value is cached or overridden via sysfs or driver,
1170 * returns the cached value.
1171 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001172unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1173{
1174 struct hda_pincfg *pin;
1175
Takashi Iwai648a8d22014-02-25 10:38:13 +01001176#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001177 {
1178 unsigned int cfg = 0;
1179 mutex_lock(&codec->user_mutex);
1180 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1181 if (pin)
1182 cfg = pin->cfg;
1183 mutex_unlock(&codec->user_mutex);
1184 if (cfg)
1185 return cfg;
1186 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001187#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001188 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1189 if (pin)
1190 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001191 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1192 if (pin)
1193 return pin->cfg;
1194 return 0;
1195}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001196EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001197
Takashi Iwai95a962c2014-10-29 16:03:58 +01001198/**
1199 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1200 * @codec: the HDA codec
1201 * @nid: pin NID
1202 * @val: assigned pinctl value
1203 *
1204 * This function stores the given value to a pinctl target value in the
1205 * pincfg table. This isn't always as same as the actually written value
1206 * but can be referred at any time via snd_hda_codec_get_pin_target().
1207 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001208int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1209 unsigned int val)
1210{
1211 struct hda_pincfg *pin;
1212
1213 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1214 if (!pin)
1215 return -EINVAL;
1216 pin->target = val;
1217 return 0;
1218}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001219EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001220
Takashi Iwai95a962c2014-10-29 16:03:58 +01001221/**
1222 * snd_hda_codec_get_pin_target - return the current pinctl target value
1223 * @codec: the HDA codec
1224 * @nid: pin NID
1225 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001226int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1227{
1228 struct hda_pincfg *pin;
1229
1230 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1231 if (!pin)
1232 return 0;
1233 return pin->target;
1234}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001235EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001236
Takashi Iwai92ee6162009-12-27 11:18:59 +01001237/**
1238 * snd_hda_shutup_pins - Shut up all pins
1239 * @codec: the HDA codec
1240 *
1241 * Clear all pin controls to shup up before suspend for avoiding click noise.
1242 * The controls aren't cached so that they can be resumed properly.
1243 */
1244void snd_hda_shutup_pins(struct hda_codec *codec)
1245{
1246 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001247 /* don't shut up pins when unloading the driver; otherwise it breaks
1248 * the default pin setup at the next load of the driver
1249 */
1250 if (codec->bus->shutdown)
1251 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001252 for (i = 0; i < codec->init_pins.used; i++) {
1253 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1254 /* use read here for syncing after issuing each verb */
1255 snd_hda_codec_read(codec, pin->nid, 0,
1256 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1257 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001258 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001259}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001260EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001261
Takashi Iwai2a439522011-07-26 09:52:50 +02001262#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001263/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1264static void restore_shutup_pins(struct hda_codec *codec)
1265{
1266 int i;
1267 if (!codec->pins_shutup)
1268 return;
1269 if (codec->bus->shutdown)
1270 return;
1271 for (i = 0; i < codec->init_pins.used; i++) {
1272 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1273 snd_hda_codec_write(codec, pin->nid, 0,
1274 AC_VERB_SET_PIN_WIDGET_CONTROL,
1275 pin->ctrl);
1276 }
1277 codec->pins_shutup = 0;
1278}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001279#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001280
David Henningsson26a6cb62012-10-09 15:04:21 +02001281static void hda_jackpoll_work(struct work_struct *work)
1282{
1283 struct hda_codec *codec =
1284 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001285
1286 snd_hda_jack_set_dirty_all(codec);
1287 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001288
1289 if (!codec->jackpoll_interval)
1290 return;
1291
David Henningsson26a6cb62012-10-09 15:04:21 +02001292 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1293 codec->jackpoll_interval);
1294}
1295
Takashi Iwai01751f52007-08-10 16:59:39 +02001296static void init_hda_cache(struct hda_cache_rec *cache,
1297 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001298static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001299
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001300/* release all pincfg lists */
1301static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001302{
Takashi Iwai346ff702009-02-23 09:42:57 +01001303 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001304#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001305 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001306#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 snd_array_free(&codec->init_pins);
1308}
1309
Takashi Iwai54d17402005-11-21 16:33:22 +01001310/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001311 * audio-converter setup caches
1312 */
1313struct hda_cvt_setup {
1314 hda_nid_t nid;
1315 u8 stream_tag;
1316 u8 channel_id;
1317 u16 format_id;
1318 unsigned char active; /* cvt is currently used */
1319 unsigned char dirty; /* setups should be cleared */
1320};
1321
1322/* get or create a cache entry for the given audio converter NID */
1323static struct hda_cvt_setup *
1324get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1325{
1326 struct hda_cvt_setup *p;
1327 int i;
1328
1329 for (i = 0; i < codec->cvt_setups.used; i++) {
1330 p = snd_array_elem(&codec->cvt_setups, i);
1331 if (p->nid == nid)
1332 return p;
1333 }
1334 p = snd_array_new(&codec->cvt_setups);
1335 if (p)
1336 p->nid = nid;
1337 return p;
1338}
1339
1340/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001341 * Dynamic symbol binding for the codec parsers
1342 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001343
1344#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001345 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001346
1347static void unload_parser(struct hda_codec *codec)
1348{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001349 if (codec->parser)
1350 symbol_put_addr(codec->parser);
1351 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001352}
1353
1354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * codec destructor
1356 */
1357static void snd_hda_codec_free(struct hda_codec *codec)
1358{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001359 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001361 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001362 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001363 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001364#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001365 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001366 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001369 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001370 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001371 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001372 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001373 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 codec->bus->caddr_tbl[codec->addr] = NULL;
1375 if (codec->patch_ops.free)
1376 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001377 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001378 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001379 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001380 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001381 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001382 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001383 kfree(codec->vendor_name);
1384 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001385 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001386 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001387 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001388 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001391static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1392 hda_nid_t fg, unsigned int power_state);
1393
Takashi Iwaid8193872012-08-31 07:54:38 -07001394static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001395 unsigned int power_state);
1396
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001397static int snd_hda_codec_dev_register(struct snd_device *device)
1398{
1399 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001400 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001401
Takashi Iwaid604b392014-02-28 13:42:09 +01001402 if (err < 0)
1403 return err;
1404 snd_hda_register_beep_device(codec);
1405 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001406}
1407
1408static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1409{
1410 struct hda_codec *codec = device->device_data;
1411
Takashi Iwaid604b392014-02-28 13:42:09 +01001412 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001413 device_del(&codec->dev);
1414 return 0;
1415}
1416
Takashi Iwai2565c892014-02-19 11:41:09 +01001417static int snd_hda_codec_dev_free(struct snd_device *device)
1418{
1419 snd_hda_codec_free(device->device_data);
1420 return 0;
1421}
1422
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001423/* just free the container */
1424static void snd_hda_codec_dev_release(struct device *dev)
1425{
1426 kfree(container_of(dev, struct hda_codec, dev));
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/**
1430 * snd_hda_codec_new - create a HDA codec
1431 * @bus: the bus to assign
1432 * @codec_addr: the codec address
1433 * @codecp: the pointer to store the generated codec
1434 *
1435 * Returns 0 if successful, or a negative error code.
1436 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001437int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001438 unsigned int codec_addr,
1439 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001442 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001443 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001445 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001446 .dev_register = snd_hda_codec_dev_register,
1447 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001448 .dev_free = snd_hda_codec_dev_free,
1449 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Takashi Iwaida3cec32008-08-08 17:12:14 +02001451 if (snd_BUG_ON(!bus))
1452 return -EINVAL;
1453 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001457 dev_err(bus->card->dev,
1458 "address 0x%x is already occupied\n",
1459 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EBUSY;
1461 }
1462
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001463 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001465 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -ENOMEM;
1467 }
1468
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001469 device_initialize(&codec->dev);
1470 codec->dev.parent = &bus->card->card_dev;
1471 codec->dev.class = sound_class;
1472 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001473 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001474 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1475 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001476 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 codec->bus = bus;
1479 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001480 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001481 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001482 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001483 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001484 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001485 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1486 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001487 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001488 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001489 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001490 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001491 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001492 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001493 INIT_LIST_HEAD(&codec->conn_list);
1494
David Henningsson26a6cb62012-10-09 15:04:21 +02001495 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001496 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001497 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Takashi Iwai83012a72012-08-24 18:38:08 +02001499#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001500 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001501 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1502 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1503 * the caller has to power down appropriatley after initialization
1504 * phase.
1505 */
1506 hda_keep_power_on(codec);
1507#endif
1508
Takashi Iwai648a8d22014-02-25 10:38:13 +01001509 snd_hda_sysfs_init(codec);
1510
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001511 if (codec->bus->modelname) {
1512 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1513 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001514 err = -ENODEV;
1515 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001516 }
1517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001520 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 bus->caddr_tbl[codec_addr] = codec;
1523
Takashi Iwai0ba21762007-04-16 11:29:14 +02001524 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1525 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001526 if (codec->vendor_id == -1)
1527 /* read again, hopefully the access method was corrected
1528 * in the last read...
1529 */
1530 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1531 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001532 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1533 AC_PAR_SUBSYSTEM_ID);
1534 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1535 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001537 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001538 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001539 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001540 err = -ENODEV;
1541 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Takashi Iwaid8193872012-08-31 07:54:38 -07001544 fg = codec->afg ? codec->afg : codec->mfg;
1545 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001546 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001547 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001548 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001549 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001550 err = read_pin_defaults(codec);
1551 if (err < 0)
1552 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001553
Takashi Iwai0ba21762007-04-16 11:29:14 +02001554 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001555 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001556 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001557 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001558 }
1559
Takashi Iwai83012a72012-08-24 18:38:08 +02001560#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001561 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001562 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001563#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001564 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001565 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001566#ifdef CONFIG_PM
1567 if (!codec->d3_stop_clk || !codec->epss)
1568 bus->power_keep_link_on = 1;
1569#endif
1570
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001571
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001572 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001573 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001574
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001575 snd_hda_codec_proc_new(codec);
1576
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001577 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001578
1579 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1580 codec->subsystem_id, codec->revision_id);
1581 snd_component_add(codec->bus->card, component);
1582
Takashi Iwai2565c892014-02-19 11:41:09 +01001583 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1584 if (err < 0)
1585 goto error;
1586
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001587 if (codecp)
1588 *codecp = codec;
1589 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001590
1591 error:
1592 snd_hda_codec_free(codec);
1593 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001594}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001595EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001596
Takashi Iwai95a962c2014-10-29 16:03:58 +01001597/**
1598 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1599 * @codec: the HDA codec
1600 *
1601 * Forcibly refresh the all widget caps and the init pin configurations of
1602 * the given codec.
1603 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001604int snd_hda_codec_update_widgets(struct hda_codec *codec)
1605{
1606 hda_nid_t fg;
1607 int err;
1608
1609 /* Assume the function group node does not change,
1610 * only the widget nodes may change.
1611 */
1612 kfree(codec->wcaps);
1613 fg = codec->afg ? codec->afg : codec->mfg;
1614 err = read_widget_caps(codec, fg);
1615 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001616 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001617 return err;
1618 }
1619
1620 snd_array_free(&codec->init_pins);
1621 err = read_pin_defaults(codec);
1622
1623 return err;
1624}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001625EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001626
1627
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001628#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001629/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1630static bool is_likely_hdmi_codec(struct hda_codec *codec)
1631{
1632 hda_nid_t nid = codec->start_nid;
1633 int i;
1634
1635 for (i = 0; i < codec->num_nodes; i++, nid++) {
1636 unsigned int wcaps = get_wcaps(codec, nid);
1637 switch (get_wcaps_type(wcaps)) {
1638 case AC_WID_AUD_IN:
1639 return false; /* HDMI parser supports only HDMI out */
1640 case AC_WID_AUD_OUT:
1641 if (!(wcaps & AC_WCAP_DIGITAL))
1642 return false;
1643 break;
1644 }
1645 }
1646 return true;
1647}
1648#else
1649/* no HDMI codec parser support */
1650#define is_likely_hdmi_codec(codec) false
1651#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1652
Takashi Iwaid5191e52009-11-16 14:58:17 +01001653/**
1654 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1655 * @codec: the HDA codec
1656 *
1657 * Start parsing of the given codec tree and (re-)initialize the whole
1658 * patch instance.
1659 *
1660 * Returns 0 if successful or a negative error code.
1661 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001662int snd_hda_codec_configure(struct hda_codec *codec)
1663{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001664 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001665 int err;
1666
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001667 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001668 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001669 err = get_codec_name(codec);
1670 if (err < 0)
1671 return err;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001674 if (!is_generic_config(codec) && codec->preset)
1675 patch = codec->preset->patch;
1676 if (!patch) {
1677 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001678 if (is_likely_hdmi_codec(codec)) {
1679#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001680 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001681#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1682 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001683#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001684 }
1685 if (!patch) {
1686#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1687 patch = load_parser(codec, snd_hda_parse_generic_codec);
1688#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1689 patch = snd_hda_parse_generic_codec;
1690#endif
1691 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001692 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001693 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001694 return -ENODEV;
1695 }
Takashi Iwai82467612007-07-27 19:15:54 +02001696 }
1697
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001698 err = patch(codec);
1699 if (err < 0) {
1700 unload_parser(codec);
1701 return err;
1702 }
Takashi Iwai82467612007-07-27 19:15:54 +02001703
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001704 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001705 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001706 if (err < 0)
1707 return err;
1708 }
1709
Takashi Iwaif62faed2009-12-23 09:27:51 +01001710 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001711 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001712 snprintf(codec->bus->card->mixername,
1713 sizeof(codec->bus->card->mixername),
1714 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001717EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Takashi Iwaied360812012-08-08 17:12:52 +02001719/* update the stream-id if changed */
1720static void update_pcm_stream_id(struct hda_codec *codec,
1721 struct hda_cvt_setup *p, hda_nid_t nid,
1722 u32 stream_tag, int channel_id)
1723{
1724 unsigned int oldval, newval;
1725
1726 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1727 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1728 newval = (stream_tag << 4) | channel_id;
1729 if (oldval != newval)
1730 snd_hda_codec_write(codec, nid, 0,
1731 AC_VERB_SET_CHANNEL_STREAMID,
1732 newval);
1733 p->stream_tag = stream_tag;
1734 p->channel_id = channel_id;
1735 }
1736}
1737
1738/* update the format-id if changed */
1739static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1740 hda_nid_t nid, int format)
1741{
1742 unsigned int oldval;
1743
1744 if (p->format_id != format) {
1745 oldval = snd_hda_codec_read(codec, nid, 0,
1746 AC_VERB_GET_STREAM_FORMAT, 0);
1747 if (oldval != format) {
1748 msleep(1);
1749 snd_hda_codec_write(codec, nid, 0,
1750 AC_VERB_SET_STREAM_FORMAT,
1751 format);
1752 }
1753 p->format_id = format;
1754 }
1755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/**
1758 * snd_hda_codec_setup_stream - set up the codec for streaming
1759 * @codec: the CODEC to set up
1760 * @nid: the NID to set up
1761 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1762 * @channel_id: channel id to pass, zero based.
1763 * @format: stream format.
1764 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001765void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1766 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int channel_id, int format)
1768{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001769 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001770 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001771 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001772 int i;
1773
Takashi Iwai0ba21762007-04-16 11:29:14 +02001774 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001775 return;
1776
Takashi Iwai4e76a882014-02-25 12:21:03 +01001777 codec_dbg(codec,
1778 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1779 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001780 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001781 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001782 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001783
1784 if (codec->pcm_format_first)
1785 update_pcm_format(codec, p, nid, format);
1786 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1787 if (!codec->pcm_format_first)
1788 update_pcm_format(codec, p, nid, format);
1789
Takashi Iwaieb541332010-08-06 13:48:11 +02001790 p->active = 1;
1791 p->dirty = 0;
1792
1793 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001794 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001795 list_for_each_entry(c, &codec->bus->codec_list, list) {
1796 for (i = 0; i < c->cvt_setups.used; i++) {
1797 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001798 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001799 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001800 p->dirty = 1;
1801 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001804EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Takashi Iwaif0cea792010-08-13 11:56:53 +02001806static void really_cleanup_stream(struct hda_codec *codec,
1807 struct hda_cvt_setup *q);
1808
Takashi Iwaid5191e52009-11-16 14:58:17 +01001809/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001810 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001811 * @codec: the CODEC to clean up
1812 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001813 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001814 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001815void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1816 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001817{
Takashi Iwaieb541332010-08-06 13:48:11 +02001818 struct hda_cvt_setup *p;
1819
Takashi Iwai888afa12008-03-18 09:57:50 +01001820 if (!nid)
1821 return;
1822
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001823 if (codec->no_sticky_stream)
1824 do_now = 1;
1825
Takashi Iwai4e76a882014-02-25 12:21:03 +01001826 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001827 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001828 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001829 /* here we just clear the active flag when do_now isn't set;
1830 * actual clean-ups will be done later in
1831 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1832 */
1833 if (do_now)
1834 really_cleanup_stream(codec, p);
1835 else
1836 p->active = 0;
1837 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001838}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001839EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001840
Takashi Iwaieb541332010-08-06 13:48:11 +02001841static void really_cleanup_stream(struct hda_codec *codec,
1842 struct hda_cvt_setup *q)
1843{
1844 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001845 if (q->stream_tag || q->channel_id)
1846 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1847 if (q->format_id)
1848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1849);
Takashi Iwaieb541332010-08-06 13:48:11 +02001850 memset(q, 0, sizeof(*q));
1851 q->nid = nid;
1852}
1853
1854/* clean up the all conflicting obsolete streams */
1855static void purify_inactive_streams(struct hda_codec *codec)
1856{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001857 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001858 int i;
1859
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001860 list_for_each_entry(c, &codec->bus->codec_list, list) {
1861 for (i = 0; i < c->cvt_setups.used; i++) {
1862 struct hda_cvt_setup *p;
1863 p = snd_array_elem(&c->cvt_setups, i);
1864 if (p->dirty)
1865 really_cleanup_stream(c, p);
1866 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001867 }
1868}
1869
Takashi Iwai2a439522011-07-26 09:52:50 +02001870#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001871/* clean up all streams; called from suspend */
1872static void hda_cleanup_all_streams(struct hda_codec *codec)
1873{
1874 int i;
1875
1876 for (i = 0; i < codec->cvt_setups.used; i++) {
1877 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1878 if (p->stream_tag)
1879 really_cleanup_stream(codec, p);
1880 }
1881}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001882#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/*
1885 * amp access functions
1886 */
1887
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001888/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001889#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001890#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001891#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1892#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001894#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001897static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001898 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai01751f52007-08-10 16:59:39 +02001900 memset(cache, 0, sizeof(*cache));
1901 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001902 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001903}
1904
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001905static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001906{
Takashi Iwai603c4012008-07-30 15:01:44 +02001907 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001911static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai01751f52007-08-10 16:59:39 +02001913 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1914 u16 cur = cache->hash[idx];
1915 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001918 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (info->key == key)
1920 return info;
1921 cur = info->next;
1922 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001923 return NULL;
1924}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001926/* query the hash. allocate an entry if not found. */
1927static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1928 u32 key)
1929{
1930 struct hda_cache_head *info = get_hash(cache, key);
1931 if (!info) {
1932 u16 idx, cur;
1933 /* add a new hash entry */
1934 info = snd_array_new(&cache->buf);
1935 if (!info)
1936 return NULL;
1937 cur = snd_array_index(&cache->buf, info);
1938 info->key = key;
1939 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001940 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001941 idx = key % (u16)ARRAY_SIZE(cache->hash);
1942 info->next = cache->hash[idx];
1943 cache->hash[idx] = cur;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return info;
1946}
1947
Takashi Iwai01751f52007-08-10 16:59:39 +02001948/* query and allocate an amp hash entry */
1949static inline struct hda_amp_info *
1950get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1951{
1952 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1953}
1954
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001955/* overwrite the value with the key in the caps hash */
1956static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1957{
1958 struct hda_amp_info *info;
1959
1960 mutex_lock(&codec->hash_mutex);
1961 info = get_alloc_amp_hash(codec, key);
1962 if (!info) {
1963 mutex_unlock(&codec->hash_mutex);
1964 return -EINVAL;
1965 }
1966 info->amp_caps = val;
1967 info->head.val |= INFO_AMP_CAPS;
1968 mutex_unlock(&codec->hash_mutex);
1969 return 0;
1970}
1971
1972/* query the value from the caps hash; if not found, fetch the current
1973 * value from the given function and store in the hash
1974 */
1975static unsigned int
1976query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1977 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1978{
1979 struct hda_amp_info *info;
1980 unsigned int val;
1981
1982 mutex_lock(&codec->hash_mutex);
1983 info = get_alloc_amp_hash(codec, key);
1984 if (!info) {
1985 mutex_unlock(&codec->hash_mutex);
1986 return 0;
1987 }
1988 if (!(info->head.val & INFO_AMP_CAPS)) {
1989 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1990 val = func(codec, nid, dir);
1991 write_caps_hash(codec, key, val);
1992 } else {
1993 val = info->amp_caps;
1994 mutex_unlock(&codec->hash_mutex);
1995 }
1996 return val;
1997}
1998
1999static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
2000 int direction)
2001{
2002 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
2003 nid = codec->afg;
2004 return snd_hda_param_read(codec, nid,
2005 direction == HDA_OUTPUT ?
2006 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
2007}
2008
Takashi Iwaid5191e52009-11-16 14:58:17 +01002009/**
2010 * query_amp_caps - query AMP capabilities
2011 * @codec: the HD-auio codec
2012 * @nid: the NID to query
2013 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2014 *
2015 * Query AMP capabilities for the given widget and direction.
2016 * Returns the obtained capability bits.
2017 *
2018 * When cap bits have been already read, this doesn't read again but
2019 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01002021u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002023 return query_caps_hash(codec, nid, direction,
2024 HDA_HASH_KEY(nid, direction, 0),
2025 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002027EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Takashi Iwaid5191e52009-11-16 14:58:17 +01002029/**
David Henningsson861a04e2014-09-23 10:38:17 +02002030 * snd_hda_check_amp_caps - query AMP capabilities
2031 * @codec: the HD-audio codec
2032 * @nid: the NID to query
2033 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002034 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02002035 *
2036 * Check whether the widget has the given amp capability for the direction.
2037 */
2038bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2039 int dir, unsigned int bits)
2040{
2041 if (!nid)
2042 return false;
2043 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2044 if (query_amp_caps(codec, nid, dir) & bits)
2045 return true;
2046 return false;
2047}
2048EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2049
2050/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002051 * snd_hda_override_amp_caps - Override the AMP capabilities
2052 * @codec: the CODEC to clean up
2053 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002054 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01002055 * @caps: the capability bits to set
2056 *
2057 * Override the cached AMP caps bits value by the given one.
2058 * This function is useful if the driver needs to adjust the AMP ranges,
2059 * e.g. limit to 0dB, etc.
2060 *
2061 * Returns zero if successful or a negative error code.
2062 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002063int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2064 unsigned int caps)
2065{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002066 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002067}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002068EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002069
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002070static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2071 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002072{
2073 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2074}
2075
Takashi Iwaid5191e52009-11-16 14:58:17 +01002076/**
2077 * snd_hda_query_pin_caps - Query PIN capabilities
2078 * @codec: the HD-auio codec
2079 * @nid: the NID to query
2080 *
2081 * Query PIN capabilities for the given widget.
2082 * Returns the obtained capability bits.
2083 *
2084 * When cap bits have been already read, this doesn't read again but
2085 * returns the cached value.
2086 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002087u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2088{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002089 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002090 read_pin_cap);
2091}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002092EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002093
Wu Fengguang864f92b2009-11-18 12:38:02 +08002094/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002095 * snd_hda_override_pin_caps - Override the pin capabilities
2096 * @codec: the CODEC
2097 * @nid: the NID to override
2098 * @caps: the capability bits to set
2099 *
2100 * Override the cached PIN capabilitiy bits value by the given one.
2101 *
2102 * Returns zero if successful or a negative error code.
2103 */
2104int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2105 unsigned int caps)
2106{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002107 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002108}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002109EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002110
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002111/* read or sync the hash value with the current value;
2112 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002114static struct hda_amp_info *
2115update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002116 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002118 struct hda_amp_info *info;
2119 unsigned int parm, val = 0;
2120 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002122 retry:
2123 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2124 if (!info)
2125 return NULL;
2126 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2127 if (!val_read) {
2128 mutex_unlock(&codec->hash_mutex);
2129 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2130 parm |= direction == HDA_OUTPUT ?
2131 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2132 parm |= index;
2133 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002134 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002135 val &= 0xff;
2136 val_read = true;
2137 mutex_lock(&codec->hash_mutex);
2138 goto retry;
2139 }
2140 info->vol[ch] = val;
2141 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002142 } else if (init_only)
2143 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002144 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002148 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002150static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002151 hda_nid_t nid, int ch, int direction, int index,
2152 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 u32 parm;
2155
2156 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2157 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2158 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002159 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2160 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002161 ; /* set the zero value as a fake mute */
2162 else
2163 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2165}
2166
Takashi Iwaid5191e52009-11-16 14:58:17 +01002167/**
2168 * snd_hda_codec_amp_read - Read AMP value
2169 * @codec: HD-audio codec
2170 * @nid: NID to read the AMP value
2171 * @ch: channel (left=0 or right=1)
2172 * @direction: #HDA_INPUT or #HDA_OUTPUT
2173 * @index: the index value (only for input direction)
2174 *
2175 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Takashi Iwai834be882006-03-01 14:16:17 +01002177int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2178 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002180 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002181 unsigned int val = 0;
2182
2183 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002184 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002185 if (info)
2186 val = info->vol[ch];
2187 mutex_unlock(&codec->hash_mutex);
2188 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002190EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Takashi Iwai280e57d2012-12-14 10:32:21 +01002192static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2193 int direction, int idx, int mask, int val,
2194 bool init_only)
2195{
2196 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002197 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002198 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002199
2200 if (snd_BUG_ON(mask & ~0xff))
2201 mask &= 0xff;
2202 val &= mask;
2203
2204 mutex_lock(&codec->hash_mutex);
2205 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2206 if (!info) {
2207 mutex_unlock(&codec->hash_mutex);
2208 return 0;
2209 }
2210 val |= info->vol[ch] & ~mask;
2211 if (info->vol[ch] == val) {
2212 mutex_unlock(&codec->hash_mutex);
2213 return 0;
2214 }
2215 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002216 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002217 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002218 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002219 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002220 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002221 return 1;
2222}
2223
Takashi Iwaid5191e52009-11-16 14:58:17 +01002224/**
2225 * snd_hda_codec_amp_update - update the AMP value
2226 * @codec: HD-audio codec
2227 * @nid: NID to read the AMP value
2228 * @ch: channel (left=0 or right=1)
2229 * @direction: #HDA_INPUT or #HDA_OUTPUT
2230 * @idx: the index value (only for input direction)
2231 * @mask: bit mask to set
2232 * @val: the bits value to set
2233 *
2234 * Update the AMP value with a bit mask.
2235 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002236 */
Takashi Iwai834be882006-03-01 14:16:17 +01002237int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2238 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002240 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002242EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Takashi Iwaid5191e52009-11-16 14:58:17 +01002244/**
2245 * snd_hda_codec_amp_stereo - update the AMP stereo values
2246 * @codec: HD-audio codec
2247 * @nid: NID to read the AMP value
2248 * @direction: #HDA_INPUT or #HDA_OUTPUT
2249 * @idx: the index value (only for input direction)
2250 * @mask: bit mask to set
2251 * @val: the bits value to set
2252 *
2253 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2254 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002255 */
2256int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2257 int direction, int idx, int mask, int val)
2258{
2259 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002260
2261 if (snd_BUG_ON(mask & ~0xff))
2262 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002263 for (ch = 0; ch < 2; ch++)
2264 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2265 idx, mask, val);
2266 return ret;
2267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002268EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002269
Takashi Iwai95a962c2014-10-29 16:03:58 +01002270/**
2271 * snd_hda_codec_amp_init - initialize the AMP value
2272 * @codec: the HDA codec
2273 * @nid: NID to read the AMP value
2274 * @ch: channel (left=0 or right=1)
2275 * @dir: #HDA_INPUT or #HDA_OUTPUT
2276 * @idx: the index value (only for input direction)
2277 * @mask: bit mask to set
2278 * @val: the bits value to set
2279 *
2280 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002281 * the first access. If the amp was already initialized / updated beforehand,
2282 * this does nothing.
2283 */
2284int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2285 int dir, int idx, int mask, int val)
2286{
2287 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2288}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002289EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002290
Takashi Iwai95a962c2014-10-29 16:03:58 +01002291/**
2292 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2293 * @codec: the HDA codec
2294 * @nid: NID to read the AMP value
2295 * @dir: #HDA_INPUT or #HDA_OUTPUT
2296 * @idx: the index value (only for input direction)
2297 * @mask: bit mask to set
2298 * @val: the bits value to set
2299 *
2300 * Call snd_hda_codec_amp_init() for both stereo channels.
2301 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002302int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2303 int dir, int idx, int mask, int val)
2304{
2305 int ch, ret = 0;
2306
2307 if (snd_BUG_ON(mask & ~0xff))
2308 mask &= 0xff;
2309 for (ch = 0; ch < 2; ch++)
2310 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2311 idx, mask, val);
2312 return ret;
2313}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002314EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002315
Takashi Iwaid5191e52009-11-16 14:58:17 +01002316/**
2317 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2318 * @codec: HD-audio codec
2319 *
2320 * Resume the all amp commands from the cache.
2321 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002322void snd_hda_codec_resume_amp(struct hda_codec *codec)
2323{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002324 int i;
2325
Takashi Iwaic370dd62012-12-13 18:30:04 +01002326 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002327 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002328 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2329 struct hda_amp_info *buffer;
2330 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002331 hda_nid_t nid;
2332 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002333 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002334
2335 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002336 if (!buffer->head.dirty)
2337 continue;
2338 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002339 info = *buffer;
2340 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002341 if (!key)
2342 continue;
2343 nid = key & 0xff;
2344 idx = (key >> 16) & 0xff;
2345 dir = (key >> 24) & 0xff;
2346 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002347 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002348 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002349 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002350 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2351 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002352 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002353 }
2354 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002355 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002356}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002357EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002359static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2360 unsigned int ofs)
2361{
2362 u32 caps = query_amp_caps(codec, nid, dir);
2363 /* get num steps */
2364 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2365 if (ofs < caps)
2366 caps -= ofs;
2367 return caps;
2368}
2369
Takashi Iwaid5191e52009-11-16 14:58:17 +01002370/**
2371 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002372 * @kcontrol: referred ctl element
2373 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002374 *
2375 * The control element is supposed to have the private_value field
2376 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2377 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002378int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 u16 nid = get_amp_nid(kcontrol);
2383 u8 chs = get_amp_channels(kcontrol);
2384 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002385 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002387 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2388 uinfo->count = chs == 3 ? 2 : 1;
2389 uinfo->value.integer.min = 0;
2390 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2391 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002392 codec_warn(codec,
2393 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2394 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return -EINVAL;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return 0;
2398}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002399EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002401
2402static inline unsigned int
2403read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2404 int ch, int dir, int idx, unsigned int ofs)
2405{
2406 unsigned int val;
2407 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2408 val &= HDA_AMP_VOLMASK;
2409 if (val >= ofs)
2410 val -= ofs;
2411 else
2412 val = 0;
2413 return val;
2414}
2415
2416static inline int
2417update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2418 int ch, int dir, int idx, unsigned int ofs,
2419 unsigned int val)
2420{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002421 unsigned int maxval;
2422
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002423 if (val > 0)
2424 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002425 /* ofs = 0: raw max value */
2426 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002427 if (val > maxval)
2428 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002429 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2430 HDA_AMP_VOLMASK, val);
2431}
2432
Takashi Iwaid5191e52009-11-16 14:58:17 +01002433/**
2434 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002435 * @kcontrol: ctl element
2436 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002437 *
2438 * The control element is supposed to have the private_value field
2439 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2440 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002441int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2445 hda_nid_t nid = get_amp_nid(kcontrol);
2446 int chs = get_amp_channels(kcontrol);
2447 int dir = get_amp_direction(kcontrol);
2448 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002449 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 long *valp = ucontrol->value.integer.value;
2451
2452 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002453 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002455 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 0;
2457}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002458EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Takashi Iwaid5191e52009-11-16 14:58:17 +01002460/**
2461 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002462 * @kcontrol: ctl element
2463 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002464 *
2465 * The control element is supposed to have the private_value field
2466 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2467 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002468int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2472 hda_nid_t nid = get_amp_nid(kcontrol);
2473 int chs = get_amp_channels(kcontrol);
2474 int dir = get_amp_direction(kcontrol);
2475 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002476 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 long *valp = ucontrol->value.integer.value;
2478 int change = 0;
2479
Takashi Iwaicb53c622007-08-10 17:21:45 +02002480 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002481 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002482 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002483 valp++;
2484 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002485 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002486 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002487 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return change;
2489}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002490EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Takashi Iwaid5191e52009-11-16 14:58:17 +01002492/**
2493 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002494 * @kcontrol: ctl element
2495 * @op_flag: operation flag
2496 * @size: byte size of input TLV
2497 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002498 *
2499 * The control element is supposed to have the private_value field
2500 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2501 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002502int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2503 unsigned int size, unsigned int __user *_tlv)
2504{
2505 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2506 hda_nid_t nid = get_amp_nid(kcontrol);
2507 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002508 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002509 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002510 u32 caps, val1, val2;
2511
2512 if (size < 4 * sizeof(unsigned int))
2513 return -ENOMEM;
2514 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002515 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2516 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002517 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002518 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002519 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002520 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002521 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002522 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2523 return -EFAULT;
2524 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2525 return -EFAULT;
2526 if (put_user(val1, _tlv + 2))
2527 return -EFAULT;
2528 if (put_user(val2, _tlv + 3))
2529 return -EFAULT;
2530 return 0;
2531}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002532EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002533
Takashi Iwaid5191e52009-11-16 14:58:17 +01002534/**
2535 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2536 * @codec: HD-audio codec
2537 * @nid: NID of a reference widget
2538 * @dir: #HDA_INPUT or #HDA_OUTPUT
2539 * @tlv: TLV data to be stored, at least 4 elements
2540 *
2541 * Set (static) TLV data for a virtual master volume using the AMP caps
2542 * obtained from the reference NID.
2543 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002544 */
2545void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2546 unsigned int *tlv)
2547{
2548 u32 caps;
2549 int nums, step;
2550
2551 caps = query_amp_caps(codec, nid, dir);
2552 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2553 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2554 step = (step + 1) * 25;
2555 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2556 tlv[1] = 2 * sizeof(unsigned int);
2557 tlv[2] = -nums * step;
2558 tlv[3] = step;
2559}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002560EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002561
2562/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002563static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002564find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002565{
2566 struct snd_ctl_elem_id id;
2567 memset(&id, 0, sizeof(id));
2568 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002569 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002570 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002571 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2572 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002573 strcpy(id.name, name);
2574 return snd_ctl_find_id(codec->bus->card, &id);
2575}
2576
Takashi Iwaid5191e52009-11-16 14:58:17 +01002577/**
2578 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2579 * @codec: HD-audio codec
2580 * @name: ctl id name string
2581 *
2582 * Get the control element with the given id string and IFACE_MIXER.
2583 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002584struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2585 const char *name)
2586{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002587 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002588}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002589EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002590
Takashi Iwaidcda5802012-10-12 17:24:51 +02002591static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002592 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002593{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002594 int i, idx;
2595 /* 16 ctlrs should be large enough */
2596 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2597 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002598 return idx;
2599 }
2600 return -EBUSY;
2601}
2602
Takashi Iwaid5191e52009-11-16 14:58:17 +01002603/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002604 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002605 * @codec: HD-audio codec
2606 * @nid: corresponding NID (optional)
2607 * @kctl: the control element to assign
2608 *
2609 * Add the given control element to an array inside the codec instance.
2610 * All control elements belonging to a codec are supposed to be added
2611 * by this function so that a proper clean-up works at the free or
2612 * reconfiguration time.
2613 *
2614 * If non-zero @nid is passed, the NID is assigned to the control element.
2615 * The assignment is shown in the codec proc file.
2616 *
2617 * snd_hda_ctl_add() checks the control subdev id field whether
2618 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002619 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2620 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002621 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002622int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2623 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002624{
2625 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002626 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002627 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002628
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002629 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002630 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002631 if (nid == 0)
2632 nid = get_amp_nid_(kctl->private_value);
2633 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002634 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2635 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002636 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002637 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002638 err = snd_ctl_add(codec->bus->card, kctl);
2639 if (err < 0)
2640 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002641 item = snd_array_new(&codec->mixers);
2642 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002643 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002644 item->kctl = kctl;
2645 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002646 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002647 return 0;
2648}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002649EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002650
Takashi Iwaid5191e52009-11-16 14:58:17 +01002651/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002652 * snd_hda_add_nid - Assign a NID to a control element
2653 * @codec: HD-audio codec
2654 * @nid: corresponding NID (optional)
2655 * @kctl: the control element to assign
2656 * @index: index to kctl
2657 *
2658 * Add the given control element to an array inside the codec instance.
2659 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2660 * NID:KCTL mapping - for example "Capture Source" selector.
2661 */
2662int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2663 unsigned int index, hda_nid_t nid)
2664{
2665 struct hda_nid_item *item;
2666
2667 if (nid > 0) {
2668 item = snd_array_new(&codec->nids);
2669 if (!item)
2670 return -ENOMEM;
2671 item->kctl = kctl;
2672 item->index = index;
2673 item->nid = nid;
2674 return 0;
2675 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002676 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2677 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002678 return -EINVAL;
2679}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002680EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002681
2682/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002683 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2684 * @codec: HD-audio codec
2685 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002686void snd_hda_ctls_clear(struct hda_codec *codec)
2687{
2688 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002689 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002690 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002691 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002692 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002693 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002694}
2695
Takashi Iwai95a962c2014-10-29 16:03:58 +01002696/**
2697 * snd_hda_lock_devices - pseudo device locking
2698 * @bus: the BUS
2699 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002700 * toggle card->shutdown to allow/disallow the device access (as a hack)
2701 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002702int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002703{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002704 struct snd_card *card = bus->card;
2705 struct hda_codec *codec;
2706
Takashi Iwaia65d6292009-02-23 16:57:04 +01002707 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002708 if (card->shutdown)
2709 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002710 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002711 if (!list_empty(&card->ctl_files))
2712 goto err_clear;
2713
2714 list_for_each_entry(codec, &bus->codec_list, list) {
2715 int pcm;
2716 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2717 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2718 if (!cpcm->pcm)
2719 continue;
2720 if (cpcm->pcm->streams[0].substream_opened ||
2721 cpcm->pcm->streams[1].substream_opened)
2722 goto err_clear;
2723 }
2724 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002725 spin_unlock(&card->files_lock);
2726 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002727
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002728 err_clear:
2729 card->shutdown = 0;
2730 err_unlock:
2731 spin_unlock(&card->files_lock);
2732 return -EINVAL;
2733}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002734EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002735
Takashi Iwai95a962c2014-10-29 16:03:58 +01002736/**
2737 * snd_hda_unlock_devices - pseudo device unlocking
2738 * @bus: the BUS
2739 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002740void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002741{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002742 struct snd_card *card = bus->card;
2743
2744 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002745 spin_lock(&card->files_lock);
2746 card->shutdown = 0;
2747 spin_unlock(&card->files_lock);
2748}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002749EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002750
Takashi Iwaid5191e52009-11-16 14:58:17 +01002751/**
2752 * snd_hda_codec_reset - Clear all objects assigned to the codec
2753 * @codec: HD-audio codec
2754 *
2755 * This frees the all PCM and control elements assigned to the codec, and
2756 * clears the caches and restores the pin default configurations.
2757 *
2758 * When a device is being used, it returns -EBSY. If successfully freed,
2759 * returns zero.
2760 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002761int snd_hda_codec_reset(struct hda_codec *codec)
2762{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002763 struct hda_bus *bus = codec->bus;
2764 struct snd_card *card = bus->card;
2765 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002766
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002767 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002768 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002769
2770 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002771 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002772#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002773 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002774 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002775#endif
2776 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002777 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002778 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002779 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002780 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002781 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002782 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002783 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002784 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002785 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002786 if (codec->patch_ops.free)
2787 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002788 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002789 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002790 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002791 codec->spec = NULL;
2792 free_hda_cache(&codec->amp_cache);
2793 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002794 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2795 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002796 /* free only driver_pins so that init_pins + user_pins are restored */
2797 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002798 snd_array_free(&codec->cvt_setups);
2799 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002800 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002801 codec->num_pcms = 0;
2802 codec->pcm_info = NULL;
2803 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002804 codec->slave_dig_outs = NULL;
2805 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002806 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002807 module_put(codec->owner);
2808 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002809
2810 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002811 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002812 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002813}
2814
Takashi Iwai6194b992014-06-06 18:12:16 +02002815typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002816
2817/* apply the function to all matching slave ctls in the mixer list */
2818static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002819 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002820{
2821 struct hda_nid_item *items;
2822 const char * const *s;
2823 int i, err;
2824
2825 items = codec->mixers.list;
2826 for (i = 0; i < codec->mixers.used; i++) {
2827 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002828 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002829 continue;
2830 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002831 char tmpname[sizeof(sctl->id.name)];
2832 const char *name = *s;
2833 if (suffix) {
2834 snprintf(tmpname, sizeof(tmpname), "%s %s",
2835 name, suffix);
2836 name = tmpname;
2837 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002838 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002839 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002840 if (err)
2841 return err;
2842 break;
2843 }
2844 }
2845 }
2846 return 0;
2847}
2848
Takashi Iwai6194b992014-06-06 18:12:16 +02002849static int check_slave_present(struct hda_codec *codec,
2850 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002851{
2852 return 1;
2853}
2854
Takashi Iwai18478e82012-03-09 17:51:10 +01002855/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002856static int get_kctl_0dB_offset(struct hda_codec *codec,
2857 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002858{
2859 int _tlv[4];
2860 const int *tlv = NULL;
2861 int val = -1;
2862
2863 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2864 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2865 mm_segment_t fs = get_fs();
2866 set_fs(get_ds());
2867 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2868 tlv = _tlv;
2869 set_fs(fs);
2870 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2871 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002872 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2873 int step = tlv[3];
2874 step &= ~TLV_DB_SCALE_MUTE;
2875 if (!step)
2876 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002877 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002878 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002879- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002880 return -1;
2881 }
2882 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002883 val = -tlv[2] / step;
2884 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002885 return val;
2886}
2887
2888/* call kctl->put with the given value(s) */
2889static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2890{
2891 struct snd_ctl_elem_value *ucontrol;
2892 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2893 if (!ucontrol)
2894 return -ENOMEM;
2895 ucontrol->value.integer.value[0] = val;
2896 ucontrol->value.integer.value[1] = val;
2897 kctl->put(kctl, ucontrol);
2898 kfree(ucontrol);
2899 return 0;
2900}
2901
2902/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002903static int init_slave_0dB(struct hda_codec *codec,
2904 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002905{
Takashi Iwai6194b992014-06-06 18:12:16 +02002906 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002907 if (offset > 0)
2908 put_kctl_with_value(slave, offset);
2909 return 0;
2910}
2911
2912/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002913static int init_slave_unmute(struct hda_codec *codec,
2914 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002915{
2916 return put_kctl_with_value(slave, 1);
2917}
2918
Takashi Iwaie8750942014-06-30 14:02:39 +02002919static int add_slave(struct hda_codec *codec,
2920 void *data, struct snd_kcontrol *slave)
2921{
2922 return snd_ctl_add_slave(data, slave);
2923}
2924
Takashi Iwaid5191e52009-11-16 14:58:17 +01002925/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002926 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002927 * @codec: HD-audio codec
2928 * @name: vmaster control name
2929 * @tlv: TLV data (optional)
2930 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002931 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002932 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002933 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002934 *
2935 * Create a virtual master control with the given name. The TLV data
2936 * must be either NULL or a valid data.
2937 *
2938 * @slaves is a NULL-terminated array of strings, each of which is a
2939 * slave control name. All controls with these names are assigned to
2940 * the new virtual master control.
2941 *
2942 * This function returns zero if successful or a negative error code.
2943 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002944int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002945 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002946 const char *suffix, bool init_slave_vol,
2947 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002948{
2949 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002950 int err;
2951
Takashi Iwai29e58532012-03-12 12:25:03 +01002952 if (ctl_ret)
2953 *ctl_ret = NULL;
2954
Takashi Iwai9322ca52012-02-03 14:28:01 +01002955 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002956 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002957 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002958 return 0;
2959 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002960 kctl = snd_ctl_make_virtual_master(name, tlv);
2961 if (!kctl)
2962 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002963 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002964 if (err < 0)
2965 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002966
Takashi Iwaie8750942014-06-30 14:02:39 +02002967 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002968 if (err < 0)
2969 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002970
2971 /* init with master mute & zero volume */
2972 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002973 if (init_slave_vol) {
2974 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002975 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002976 tlv ? init_slave_0dB : init_slave_unmute, &step);
2977 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002978
Takashi Iwai29e58532012-03-12 12:25:03 +01002979 if (ctl_ret)
2980 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002981 return 0;
2982}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002983EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002984
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002985/*
2986 * mute-LED control using vmaster
2987 */
2988static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_info *uinfo)
2990{
2991 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002992 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002993 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002994
Takashi Iwai3ff72212014-10-20 18:17:28 +02002995 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002996}
2997
2998static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2999 struct snd_ctl_elem_value *ucontrol)
3000{
3001 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3002 ucontrol->value.enumerated.item[0] = hook->mute_mode;
3003 return 0;
3004}
3005
3006static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
3007 struct snd_ctl_elem_value *ucontrol)
3008{
3009 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3010 unsigned int old_mode = hook->mute_mode;
3011
3012 hook->mute_mode = ucontrol->value.enumerated.item[0];
3013 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
3014 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3015 if (old_mode == hook->mute_mode)
3016 return 0;
3017 snd_hda_sync_vmaster_hook(hook);
3018 return 1;
3019}
3020
3021static struct snd_kcontrol_new vmaster_mute_mode = {
3022 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3023 .name = "Mute-LED Mode",
3024 .info = vmaster_mute_mode_info,
3025 .get = vmaster_mute_mode_get,
3026 .put = vmaster_mute_mode_put,
3027};
3028
Takashi Iwai95a962c2014-10-29 16:03:58 +01003029/**
3030 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3031 * @codec: the HDA codec
3032 * @hook: the vmaster hook object
3033 * @expose_enum_ctl: flag to create an enum ctl
3034 *
3035 * Add a mute-LED hook with the given vmaster switch kctl.
3036 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3037 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003038 */
3039int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01003040 struct hda_vmaster_mute_hook *hook,
3041 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003042{
3043 struct snd_kcontrol *kctl;
3044
3045 if (!hook->hook || !hook->sw_kctl)
3046 return 0;
3047 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3048 hook->codec = codec;
3049 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01003050 if (!expose_enum_ctl)
3051 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003052 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3053 if (!kctl)
3054 return -ENOMEM;
3055 return snd_hda_ctl_add(codec, 0, kctl);
3056}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003057EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003058
Takashi Iwai95a962c2014-10-29 16:03:58 +01003059/**
3060 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3061 * @hook: the vmaster hook
3062 *
3063 * Call the hook with the current value for synchronization.
3064 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003065 */
3066void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3067{
3068 if (!hook->hook || !hook->codec)
3069 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003070 /* don't call vmaster hook in the destructor since it might have
3071 * been already destroyed
3072 */
3073 if (hook->codec->bus->shutdown)
3074 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003075 switch (hook->mute_mode) {
3076 case HDA_VMUTE_FOLLOW_MASTER:
3077 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3078 break;
3079 default:
3080 hook->hook(hook->codec, hook->mute_mode);
3081 break;
3082 }
3083}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003084EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003085
3086
Takashi Iwaid5191e52009-11-16 14:58:17 +01003087/**
3088 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003089 * @kcontrol: referred ctl element
3090 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003091 *
3092 * The control element is supposed to have the private_value field
3093 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3096 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 int chs = get_amp_channels(kcontrol);
3099
3100 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3101 uinfo->count = chs == 3 ? 2 : 1;
3102 uinfo->value.integer.min = 0;
3103 uinfo->value.integer.max = 1;
3104 return 0;
3105}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003106EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Takashi Iwaid5191e52009-11-16 14:58:17 +01003108/**
3109 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003110 * @kcontrol: ctl element
3111 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003112 *
3113 * The control element is supposed to have the private_value field
3114 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3115 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003116int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3120 hda_nid_t nid = get_amp_nid(kcontrol);
3121 int chs = get_amp_channels(kcontrol);
3122 int dir = get_amp_direction(kcontrol);
3123 int idx = get_amp_index(kcontrol);
3124 long *valp = ucontrol->value.integer.value;
3125
3126 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003127 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003128 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003131 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return 0;
3133}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003134EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Takashi Iwaid5191e52009-11-16 14:58:17 +01003136/**
3137 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003138 * @kcontrol: ctl element
3139 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003140 *
3141 * The control element is supposed to have the private_value field
3142 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3143 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003144int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3145 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3148 hda_nid_t nid = get_amp_nid(kcontrol);
3149 int chs = get_amp_channels(kcontrol);
3150 int dir = get_amp_direction(kcontrol);
3151 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 long *valp = ucontrol->value.integer.value;
3153 int change = 0;
3154
Takashi Iwaicb53c622007-08-10 17:21:45 +02003155 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003156 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003157 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003158 HDA_AMP_MUTE,
3159 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003160 valp++;
3161 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003162 if (chs & 2)
3163 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003164 HDA_AMP_MUTE,
3165 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003166 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003167 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return change;
3169}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003170EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172/*
Takashi Iwai985be542005-11-02 18:26:49 +01003173 * bound volume controls
3174 *
3175 * bind multiple volumes (# indices, from 0)
3176 */
3177
3178#define AMP_VAL_IDX_SHIFT 19
3179#define AMP_VAL_IDX_MASK (0x0f<<19)
3180
Takashi Iwaid5191e52009-11-16 14:58:17 +01003181/**
3182 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003183 * @kcontrol: ctl element
3184 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003185 *
3186 * The control element is supposed to have the private_value field
3187 * set up via HDA_BIND_MUTE*() macros.
3188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003189int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3190 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003191{
3192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3193 unsigned long pval;
3194 int err;
3195
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003196 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003197 pval = kcontrol->private_value;
3198 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3199 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3200 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003201 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003202 return err;
3203}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003204EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003205
Takashi Iwaid5191e52009-11-16 14:58:17 +01003206/**
3207 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003208 * @kcontrol: ctl element
3209 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003210 *
3211 * The control element is supposed to have the private_value field
3212 * set up via HDA_BIND_MUTE*() macros.
3213 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003214int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3215 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003216{
3217 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3218 unsigned long pval;
3219 int i, indices, err = 0, change = 0;
3220
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003221 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003222 pval = kcontrol->private_value;
3223 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3224 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003225 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3226 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003227 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3228 if (err < 0)
3229 break;
3230 change |= err;
3231 }
3232 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003233 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003234 return err < 0 ? err : change;
3235}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003236EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003237
Takashi Iwaid5191e52009-11-16 14:58:17 +01003238/**
3239 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003240 * @kcontrol: referred ctl element
3241 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003242 *
3243 * The control element is supposed to have the private_value field
3244 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003245 */
3246int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_info *uinfo)
3248{
3249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3250 struct hda_bind_ctls *c;
3251 int err;
3252
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003253 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003254 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003255 kcontrol->private_value = *c->values;
3256 err = c->ops->info(kcontrol, uinfo);
3257 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003258 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003259 return err;
3260}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003261EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003262
Takashi Iwaid5191e52009-11-16 14:58:17 +01003263/**
3264 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003265 * @kcontrol: ctl element
3266 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003267 *
3268 * The control element is supposed to have the private_value field
3269 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3270 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003271int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3272 struct snd_ctl_elem_value *ucontrol)
3273{
3274 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3275 struct hda_bind_ctls *c;
3276 int err;
3277
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003278 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003279 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003280 kcontrol->private_value = *c->values;
3281 err = c->ops->get(kcontrol, ucontrol);
3282 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003283 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003284 return err;
3285}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003286EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003287
Takashi Iwaid5191e52009-11-16 14:58:17 +01003288/**
3289 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003290 * @kcontrol: ctl element
3291 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003292 *
3293 * The control element is supposed to have the private_value field
3294 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3295 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003296int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3297 struct snd_ctl_elem_value *ucontrol)
3298{
3299 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3300 struct hda_bind_ctls *c;
3301 unsigned long *vals;
3302 int err = 0, change = 0;
3303
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003304 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003305 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003306 for (vals = c->values; *vals; vals++) {
3307 kcontrol->private_value = *vals;
3308 err = c->ops->put(kcontrol, ucontrol);
3309 if (err < 0)
3310 break;
3311 change |= err;
3312 }
3313 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003314 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003315 return err < 0 ? err : change;
3316}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003317EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003318
Takashi Iwaid5191e52009-11-16 14:58:17 +01003319/**
3320 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003321 * @kcontrol: ctl element
3322 * @op_flag: operation flag
3323 * @size: byte size of input TLV
3324 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003325 *
3326 * The control element is supposed to have the private_value field
3327 * set up via HDA_BIND_VOL() macro.
3328 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003329int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3330 unsigned int size, unsigned int __user *tlv)
3331{
3332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3333 struct hda_bind_ctls *c;
3334 int err;
3335
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003336 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003337 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003338 kcontrol->private_value = *c->values;
3339 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3340 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003341 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003342 return err;
3343}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003344EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003345
3346struct hda_ctl_ops snd_hda_bind_vol = {
3347 .info = snd_hda_mixer_amp_volume_info,
3348 .get = snd_hda_mixer_amp_volume_get,
3349 .put = snd_hda_mixer_amp_volume_put,
3350 .tlv = snd_hda_mixer_amp_tlv
3351};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003352EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003353
3354struct hda_ctl_ops snd_hda_bind_sw = {
3355 .info = snd_hda_mixer_amp_switch_info,
3356 .get = snd_hda_mixer_amp_switch_get,
3357 .put = snd_hda_mixer_amp_switch_put,
3358 .tlv = snd_hda_mixer_amp_tlv
3359};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003360EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003361
3362/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 * SPDIF out controls
3364 */
3365
Takashi Iwai0ba21762007-04-16 11:29:14 +02003366static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3367 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
3369 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3370 uinfo->count = 1;
3371 return 0;
3372}
3373
Takashi Iwai0ba21762007-04-16 11:29:14 +02003374static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
3377 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3378 IEC958_AES0_NONAUDIO |
3379 IEC958_AES0_CON_EMPHASIS_5015 |
3380 IEC958_AES0_CON_NOT_COPYRIGHT;
3381 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3382 IEC958_AES1_CON_ORIGINAL;
3383 return 0;
3384}
3385
Takashi Iwai0ba21762007-04-16 11:29:14 +02003386static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3387 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
3389 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3390 IEC958_AES0_NONAUDIO |
3391 IEC958_AES0_PRO_EMPHASIS_5015;
3392 return 0;
3393}
3394
Takashi Iwai0ba21762007-04-16 11:29:14 +02003395static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003399 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003400 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003402 mutex_lock(&codec->spdif_mutex);
3403 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003404 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3405 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3406 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3407 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003408 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 return 0;
3411}
3412
3413/* convert from SPDIF status bits to HDA SPDIF bits
3414 * bit 0 (DigEn) is always set zero (to be filled later)
3415 */
3416static unsigned short convert_from_spdif_status(unsigned int sbits)
3417{
3418 unsigned short val = 0;
3419
3420 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003421 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003423 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003425 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3426 IEC958_AES0_PRO_EMPHASIS_5015)
3427 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3430 IEC958_AES0_CON_EMPHASIS_5015)
3431 val |= AC_DIG1_EMPHASIS;
3432 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3433 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003435 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3437 }
3438 return val;
3439}
3440
3441/* convert to SPDIF status bits from HDA SPDIF bits
3442 */
3443static unsigned int convert_to_spdif_status(unsigned short val)
3444{
3445 unsigned int sbits = 0;
3446
Takashi Iwai0ba21762007-04-16 11:29:14 +02003447 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003449 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 sbits |= IEC958_AES0_PROFESSIONAL;
3451 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003452 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3454 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003455 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003457 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003459 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3461 sbits |= val & (0x7f << 8);
3462 }
3463 return sbits;
3464}
3465
Takashi Iwai2f728532008-09-25 16:32:41 +02003466/* set digital convert verbs both for the given NID and its slaves */
3467static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3468 int verb, int val)
3469{
Takashi Iwaidda14412011-05-02 11:29:30 +02003470 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003471
Takashi Iwai9e976972008-11-25 08:17:20 +01003472 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003473 d = codec->slave_dig_outs;
3474 if (!d)
3475 return;
3476 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003477 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003478}
3479
3480static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3481 int dig1, int dig2)
3482{
3483 if (dig1 != -1)
3484 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3485 if (dig2 != -1)
3486 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3487}
3488
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3490 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003493 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003494 struct hda_spdif_out *spdif;
3495 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 unsigned short val;
3497 int change;
3498
Ingo Molnar62932df2006-01-16 16:34:20 +01003499 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003500 spdif = snd_array_elem(&codec->spdif_out, idx);
3501 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003502 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3504 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3505 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003506 val = convert_from_spdif_status(spdif->status);
3507 val |= spdif->ctls & 1;
3508 change = spdif->ctls != val;
3509 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003510 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003511 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003512 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return change;
3514}
3515
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003516#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Takashi Iwai0ba21762007-04-16 11:29:14 +02003518static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3519 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003522 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003523 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003525 mutex_lock(&codec->spdif_mutex);
3526 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003527 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003528 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return 0;
3530}
3531
Stephen Warren74b654c2011-06-01 11:14:18 -06003532static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3533 int dig1, int dig2)
3534{
3535 set_dig_out_convert(codec, nid, dig1, dig2);
3536 /* unmute amp switch (if any) */
3537 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3538 (dig1 & AC_DIG1_ENABLE))
3539 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3540 HDA_AMP_MUTE, 0);
3541}
3542
Takashi Iwai0ba21762007-04-16 11:29:14 +02003543static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3544 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003547 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003548 struct hda_spdif_out *spdif;
3549 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 unsigned short val;
3551 int change;
3552
Ingo Molnar62932df2006-01-16 16:34:20 +01003553 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003554 spdif = snd_array_elem(&codec->spdif_out, idx);
3555 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003556 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003559 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003560 spdif->ctls = val;
3561 if (change && nid != (u16)-1)
3562 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003563 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return change;
3565}
3566
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003567static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 {
3569 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3570 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003571 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 .info = snd_hda_spdif_mask_info,
3573 .get = snd_hda_spdif_cmask_get,
3574 },
3575 {
3576 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003578 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 .info = snd_hda_spdif_mask_info,
3580 .get = snd_hda_spdif_pmask_get,
3581 },
3582 {
3583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003584 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 .info = snd_hda_spdif_mask_info,
3586 .get = snd_hda_spdif_default_get,
3587 .put = snd_hda_spdif_default_put,
3588 },
3589 {
3590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003591 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 .info = snd_hda_spdif_out_switch_info,
3593 .get = snd_hda_spdif_out_switch_get,
3594 .put = snd_hda_spdif_out_switch_put,
3595 },
3596 { } /* end */
3597};
3598
3599/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003600 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003602 * @associated_nid: NID that new ctls associated with
3603 * @cvt_nid: converter NID
3604 * @type: HDA_PCM_TYPE_*
3605 * Creates controls related with the digital output.
3606 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 *
3608 * Returns 0 if successful, or a negative error code.
3609 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003610int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3611 hda_nid_t associated_nid,
3612 hda_nid_t cvt_nid,
3613 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003616 struct snd_kcontrol *kctl;
3617 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003618 int idx = 0;
3619 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003620 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003621 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003623 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003624 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003625 idx = spdif_index;
3626 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003627 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003628 /* suppose a single SPDIF device */
3629 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3630 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3631 if (!kctl)
3632 break;
3633 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003634 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003635 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003636 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003637 if (!bus->primary_dig_out_type)
3638 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003639
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003640 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003641 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003642 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003643 return -EBUSY;
3644 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003645 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003646 if (!spdif)
3647 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3649 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003650 if (!kctl)
3651 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003652 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003653 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003654 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003655 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return err;
3657 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003658 spdif->nid = cvt_nid;
3659 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003660 AC_VERB_GET_DIGI_CONVERT_1, 0);
3661 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return 0;
3663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003664EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Takashi Iwai95a962c2014-10-29 16:03:58 +01003666/**
3667 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3668 * @codec: the HDA codec
3669 * @nid: widget NID
3670 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003671 * call within spdif_mutex lock
3672 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003673struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3674 hda_nid_t nid)
3675{
3676 int i;
3677 for (i = 0; i < codec->spdif_out.used; i++) {
3678 struct hda_spdif_out *spdif =
3679 snd_array_elem(&codec->spdif_out, i);
3680 if (spdif->nid == nid)
3681 return spdif;
3682 }
3683 return NULL;
3684}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003685EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003686
Takashi Iwai95a962c2014-10-29 16:03:58 +01003687/**
3688 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3689 * @codec: the HDA codec
3690 * @idx: the SPDIF ctl index
3691 *
3692 * Unassign the widget from the given SPDIF control.
3693 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003694void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3695{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003696 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003697
3698 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003699 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003700 spdif->nid = (u16)-1;
3701 mutex_unlock(&codec->spdif_mutex);
3702}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003703EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003704
Takashi Iwai95a962c2014-10-29 16:03:58 +01003705/**
3706 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3707 * @codec: the HDA codec
3708 * @idx: the SPDIF ctl idx
3709 * @nid: widget NID
3710 *
3711 * Assign the widget to the SPDIF control with the given index.
3712 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003713void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3714{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003715 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003716 unsigned short val;
3717
3718 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003719 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003720 if (spdif->nid != nid) {
3721 spdif->nid = nid;
3722 val = spdif->ctls;
3723 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3724 }
3725 mutex_unlock(&codec->spdif_mutex);
3726}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003727EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003730 * SPDIF sharing with analog output
3731 */
3732static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3733 struct snd_ctl_elem_value *ucontrol)
3734{
3735 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3736 ucontrol->value.integer.value[0] = mout->share_spdif;
3737 return 0;
3738}
3739
3740static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3741 struct snd_ctl_elem_value *ucontrol)
3742{
3743 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3744 mout->share_spdif = !!ucontrol->value.integer.value[0];
3745 return 0;
3746}
3747
3748static struct snd_kcontrol_new spdif_share_sw = {
3749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3750 .name = "IEC958 Default PCM Playback Switch",
3751 .info = snd_ctl_boolean_mono_info,
3752 .get = spdif_share_sw_get,
3753 .put = spdif_share_sw_put,
3754};
3755
Takashi Iwaid5191e52009-11-16 14:58:17 +01003756/**
3757 * snd_hda_create_spdif_share_sw - create Default PCM switch
3758 * @codec: the HDA codec
3759 * @mout: multi-out instance
3760 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003761int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3762 struct hda_multi_out *mout)
3763{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003764 struct snd_kcontrol *kctl;
3765
Takashi Iwai9a081602008-02-12 18:37:26 +01003766 if (!mout->dig_out_nid)
3767 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003768
3769 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3770 if (!kctl)
3771 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003772 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003773 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003774}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003775EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003776
3777/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 * SPDIF input
3779 */
3780
3781#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3782
Takashi Iwai0ba21762007-04-16 11:29:14 +02003783static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3784 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3787
3788 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3789 return 0;
3790}
3791
Takashi Iwai0ba21762007-04-16 11:29:14 +02003792static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3793 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
3795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3796 hda_nid_t nid = kcontrol->private_value;
3797 unsigned int val = !!ucontrol->value.integer.value[0];
3798 int change;
3799
Ingo Molnar62932df2006-01-16 16:34:20 +01003800 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003802 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003804 snd_hda_codec_write_cache(codec, nid, 0,
3805 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003807 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return change;
3809}
3810
Takashi Iwai0ba21762007-04-16 11:29:14 +02003811static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3812 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3815 hda_nid_t nid = kcontrol->private_value;
3816 unsigned short val;
3817 unsigned int sbits;
3818
Andrew Paprocki3982d172007-12-19 12:13:44 +01003819 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 sbits = convert_to_spdif_status(val);
3821 ucontrol->value.iec958.status[0] = sbits;
3822 ucontrol->value.iec958.status[1] = sbits >> 8;
3823 ucontrol->value.iec958.status[2] = sbits >> 16;
3824 ucontrol->value.iec958.status[3] = sbits >> 24;
3825 return 0;
3826}
3827
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003828static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 {
3830 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003831 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 .info = snd_hda_spdif_in_switch_info,
3833 .get = snd_hda_spdif_in_switch_get,
3834 .put = snd_hda_spdif_in_switch_put,
3835 },
3836 {
3837 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3838 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003839 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 .info = snd_hda_spdif_mask_info,
3841 .get = snd_hda_spdif_in_status_get,
3842 },
3843 { } /* end */
3844};
3845
3846/**
3847 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3848 * @codec: the HDA codec
3849 * @nid: audio in widget NID
3850 *
3851 * Creates controls related with the SPDIF input.
3852 * Called from each patch supporting the SPDIF in.
3853 *
3854 * Returns 0 if successful, or a negative error code.
3855 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003856int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003859 struct snd_kcontrol *kctl;
3860 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003861 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Takashi Iwaidcda5802012-10-12 17:24:51 +02003863 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003864 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003865 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003866 return -EBUSY;
3867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3869 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003870 if (!kctl)
3871 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003873 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003874 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return err;
3876 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003877 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003878 snd_hda_codec_read(codec, nid, 0,
3879 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003880 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 return 0;
3882}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003883EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003885/*
3886 * command cache
3887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Takashi Iwaic370dd62012-12-13 18:30:04 +01003889/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003890#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3891#define get_cmd_cache_nid(key) ((key) & 0xff)
3892#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3893
3894/**
3895 * snd_hda_codec_write_cache - send a single command with caching
3896 * @codec: the HDA codec
3897 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003898 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003899 * @verb: the verb to send
3900 * @parm: the parameter for the verb
3901 *
3902 * Send a single command without waiting for response.
3903 *
3904 * Returns 0 if successful, or a negative error code.
3905 */
3906int snd_hda_codec_write_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 Iwaib3ac5632007-08-10 17:03:40 +02003908{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003909 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003910 struct hda_cache_head *c;
3911 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003912 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003913
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003914 cache_only = codec->cached_write;
3915 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003916 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003917 if (err < 0)
3918 return err;
3919 }
3920
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003921 /* parm may contain the verb stuff for get/set amp */
3922 verb = verb | (parm >> 8);
3923 parm &= 0xff;
3924 key = build_cmd_cache_key(nid, verb);
3925 mutex_lock(&codec->bus->cmd_mutex);
3926 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003927 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003928 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003929 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003930 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003931 mutex_unlock(&codec->bus->cmd_mutex);
3932 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003933}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003934EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003935
Takashi Iwaid5191e52009-11-16 14:58:17 +01003936/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003937 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3938 * @codec: the HDA codec
3939 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003940 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003941 * @verb: the verb to send
3942 * @parm: the parameter for the verb
3943 *
3944 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3945 * command if the parameter is already identical with the cached value.
3946 * If not, it sends the command and refreshes the cache.
3947 *
3948 * Returns 0 if successful, or a negative error code.
3949 */
3950int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003951 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003952{
3953 struct hda_cache_head *c;
3954 u32 key;
3955
3956 /* parm may contain the verb stuff for get/set amp */
3957 verb = verb | (parm >> 8);
3958 parm &= 0xff;
3959 key = build_cmd_cache_key(nid, verb);
3960 mutex_lock(&codec->bus->cmd_mutex);
3961 c = get_hash(&codec->cmd_cache, key);
3962 if (c && c->val == parm) {
3963 mutex_unlock(&codec->bus->cmd_mutex);
3964 return 0;
3965 }
3966 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003967 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003968}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003969EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003970
3971/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003972 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3973 * @codec: HD-audio codec
3974 *
3975 * Execute all verbs recorded in the command caches to resume.
3976 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003977void snd_hda_codec_resume_cache(struct hda_codec *codec)
3978{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003979 int i;
3980
Takashi Iwaic370dd62012-12-13 18:30:04 +01003981 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003982 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003983 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3984 struct hda_cache_head *buffer;
3985 u32 key;
3986
3987 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3988 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003989 if (!key)
3990 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003991 if (!buffer->dirty)
3992 continue;
3993 buffer->dirty = 0;
3994 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003995 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3996 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003997 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003998 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003999 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004000}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004001EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004002
4003/**
4004 * snd_hda_sequence_write_cache - sequence writes with caching
4005 * @codec: the HDA codec
4006 * @seq: VERB array to send
4007 *
4008 * Send the commands sequentially from the given array.
4009 * Thte commands are recorded on cache for power-save and resume.
4010 * The array must be terminated with NID=0.
4011 */
4012void snd_hda_sequence_write_cache(struct hda_codec *codec,
4013 const struct hda_verb *seq)
4014{
4015 for (; seq->nid; seq++)
4016 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
4017 seq->param);
4018}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004019EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004020
Takashi Iwaidc870f32013-01-22 15:24:30 +01004021/**
4022 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4023 * @codec: HD-audio codec
4024 */
4025void snd_hda_codec_flush_cache(struct hda_codec *codec)
4026{
4027 snd_hda_codec_resume_amp(codec);
4028 snd_hda_codec_resume_cache(codec);
4029}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004030EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01004031
Takashi Iwai95a962c2014-10-29 16:03:58 +01004032/**
4033 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4034 * @codec: the HDA codec
4035 * @fg: function group (not used now)
4036 * @power_state: the power state to set (AC_PWRST_*)
4037 *
4038 * Set the given power state to all widgets that have the power control.
4039 * If the codec has power_filter set, it evaluates the power state and
4040 * filter out if it's unchanged as D3.
4041 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004042void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004043 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01004044{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004045 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004046 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01004047
Takashi Iwaicb53c622007-08-10 17:21:45 +02004048 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01004049 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004050 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004051 if (!(wcaps & AC_WCAP_POWER))
4052 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004053 if (codec->power_filter) {
4054 state = codec->power_filter(codec, nid, power_state);
4055 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004056 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02004057 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004058 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004059 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01004060 }
Takashi Iwai54d17402005-11-21 16:33:22 +01004061}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004062EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004063
4064/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004065 * supported power states check
4066 */
4067static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4068 unsigned int power_state)
4069{
4070 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4071
Mengdong Line037cb42012-08-10 14:11:58 +02004072 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004073 return false;
4074 if (sup & power_state)
4075 return true;
4076 else
4077 return false;
4078}
4079
4080/*
Takashi Iwai432c6412012-08-28 09:59:20 -07004081 * wait until the state is reached, returns the current state
4082 */
4083static unsigned int hda_sync_power_state(struct hda_codec *codec,
4084 hda_nid_t fg,
4085 unsigned int power_state)
4086{
4087 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4088 unsigned int state, actual_state;
4089
4090 for (;;) {
4091 state = snd_hda_codec_read(codec, fg, 0,
4092 AC_VERB_GET_POWER_STATE, 0);
4093 if (state & AC_PWRST_ERROR)
4094 break;
4095 actual_state = (state >> 4) & 0x0f;
4096 if (actual_state == power_state)
4097 break;
4098 if (time_after_eq(jiffies, end_time))
4099 break;
4100 /* wait until the codec reachs to the target state */
4101 msleep(1);
4102 }
4103 return state;
4104}
4105
Takashi Iwai95a962c2014-10-29 16:03:58 +01004106/**
4107 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4108 * @codec: the HDA codec
4109 * @nid: widget NID
4110 * @power_state: power state to evalue
4111 *
4112 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4113 * This can be used a codec power_filter callback.
4114 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004115unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4116 hda_nid_t nid,
4117 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004118{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004119 if (nid == codec->afg || nid == codec->mfg)
4120 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004121 if (power_state == AC_PWRST_D3 &&
4122 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4123 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4124 int eapd = snd_hda_codec_read(codec, nid, 0,
4125 AC_VERB_GET_EAPD_BTLENABLE, 0);
4126 if (eapd & 0x02)
4127 return AC_PWRST_D0;
4128 }
4129 return power_state;
4130}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004131EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004132
Takashi Iwai432c6412012-08-28 09:59:20 -07004133/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004134 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004135 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004136static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004137 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004138{
Takashi Iwaid8193872012-08-31 07:54:38 -07004139 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004140 int count;
4141 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004142 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004143
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004144 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004145 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004146 if (codec->depop_delay < 0)
4147 msleep(codec->epss ? 10 : 100);
4148 else if (codec->depop_delay > 0)
4149 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004150 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004151 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004152
4153 /* repeat power states setting at most 10 times*/
4154 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004155 if (codec->patch_ops.set_power_state)
4156 codec->patch_ops.set_power_state(codec, fg,
4157 power_state);
4158 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004159 state = power_state;
4160 if (codec->power_filter)
4161 state = codec->power_filter(codec, fg, state);
4162 if (state == power_state || power_state != AC_PWRST_D3)
4163 snd_hda_codec_read(codec, fg, flags,
4164 AC_VERB_SET_POWER_STATE,
4165 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004166 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004167 }
4168 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004169 if (!(state & AC_PWRST_ERROR))
4170 break;
4171 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004172
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004173 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004174}
Takashi Iwai54d17402005-11-21 16:33:22 +01004175
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004176/* sync power states of all widgets;
4177 * this is called at the end of codec parsing
4178 */
4179static void sync_power_up_states(struct hda_codec *codec)
4180{
4181 hda_nid_t nid = codec->start_nid;
4182 int i;
4183
Takashi Iwaiba615b82013-03-13 14:47:21 +01004184 /* don't care if no filter is used */
4185 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004186 return;
4187
4188 for (i = 0; i < codec->num_nodes; i++, nid++) {
4189 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004190 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004191 if (!(wcaps & AC_WCAP_POWER))
4192 continue;
4193 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4194 if (target == AC_PWRST_D0)
4195 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004196 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004197 snd_hda_codec_write(codec, nid, 0,
4198 AC_VERB_SET_POWER_STATE, target);
4199 }
4200}
4201
Takashi Iwai648a8d22014-02-25 10:38:13 +01004202#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004203/* execute additional init verbs */
4204static void hda_exec_init_verbs(struct hda_codec *codec)
4205{
4206 if (codec->init_verbs.list)
4207 snd_hda_sequence_write(codec, codec->init_verbs.list);
4208}
4209#else
4210static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4211#endif
4212
Takashi Iwai2a439522011-07-26 09:52:50 +02004213#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004214/*
4215 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004216 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004217 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004218static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004219{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004220 unsigned int state;
4221
Takashi Iwai989c3182012-11-19 14:14:58 +01004222 codec->in_pm = 1;
4223
Takashi Iwaicb53c622007-08-10 17:21:45 +02004224 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004225 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004226 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004227 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004228 /* Cancel delayed work if we aren't currently running from it. */
4229 if (!in_wq)
4230 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004231 spin_lock(&codec->power_lock);
4232 snd_hda_update_power_acct(codec);
4233 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004234 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004235 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004236 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004237 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004238 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004239 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004240}
4241
Takashi Iwaic370dd62012-12-13 18:30:04 +01004242/* mark all entries of cmd and amp caches dirty */
4243static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4244{
4245 int i;
4246 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4247 struct hda_cache_head *cmd;
4248 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4249 cmd->dirty = 1;
4250 }
4251 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4252 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004253 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004254 amp->head.dirty = 1;
4255 }
4256}
4257
Takashi Iwaicb53c622007-08-10 17:21:45 +02004258/*
4259 * kick up codec; used both from PM and power-save
4260 */
4261static void hda_call_codec_resume(struct hda_codec *codec)
4262{
Takashi Iwai989c3182012-11-19 14:14:58 +01004263 codec->in_pm = 1;
4264
Takashi Iwaic370dd62012-12-13 18:30:04 +01004265 hda_mark_cmd_cache_dirty(codec);
4266
Takashi Iwai7f308302012-05-08 16:52:23 +02004267 /* set as if powered on for avoiding re-entering the resume
4268 * in the resume / power-save sequence
4269 */
4270 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004271 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004272 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004273 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004274 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004275 if (codec->patch_ops.resume)
4276 codec->patch_ops.resume(codec);
4277 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004278 if (codec->patch_ops.init)
4279 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004280 snd_hda_codec_resume_amp(codec);
4281 snd_hda_codec_resume_cache(codec);
4282 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004283
4284 if (codec->jackpoll_interval)
4285 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004286 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004287 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004288
4289 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004290 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004291}
Takashi Iwai2a439522011-07-26 09:52:50 +02004292#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004293
Takashi Iwai54d17402005-11-21 16:33:22 +01004294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/**
4296 * snd_hda_build_controls - build mixer controls
4297 * @bus: the BUS
4298 *
4299 * Creates mixer controls for each codec included in the bus.
4300 *
4301 * Returns 0 if successful, otherwise a negative error code.
4302 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004303int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004305 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Takashi Iwai0ba21762007-04-16 11:29:14 +02004307 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004308 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004309 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004310 codec_err(codec,
4311 "cannot build controls for #%d (error %d)\n",
4312 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004313 err = snd_hda_codec_reset(codec);
4314 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004315 codec_err(codec,
4316 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004317 return err;
4318 }
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004321 return 0;
4322}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004323EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004325/*
4326 * add standard channel maps if not specified
4327 */
4328static int add_std_chmaps(struct hda_codec *codec)
4329{
4330 int i, str, err;
4331
4332 for (i = 0; i < codec->num_pcms; i++) {
4333 for (str = 0; str < 2; str++) {
4334 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4335 struct hda_pcm_stream *hinfo =
4336 &codec->pcm_info[i].stream[str];
4337 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004338 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004339
4340 if (codec->pcm_info[i].own_chmap)
4341 continue;
4342 if (!pcm || !hinfo->substreams)
4343 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004344 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4345 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004346 hinfo->channels_max,
4347 0, &chmap);
4348 if (err < 0)
4349 return err;
4350 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4351 }
4352 }
4353 return 0;
4354}
4355
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004356/* default channel maps for 2.1 speakers;
4357 * since HD-audio supports only stereo, odd number channels are omitted
4358 */
4359const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4360 { .channels = 2,
4361 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4362 { .channels = 4,
4363 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4364 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4365 { }
4366};
4367EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4368
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004369int snd_hda_codec_build_controls(struct hda_codec *codec)
4370{
4371 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004372 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004373 /* continue to initialize... */
4374 if (codec->patch_ops.init)
4375 err = codec->patch_ops.init(codec);
4376 if (!err && codec->patch_ops.build_controls)
4377 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004378 if (err < 0)
4379 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004380
4381 /* we create chmaps here instead of build_pcms */
4382 err = add_std_chmaps(codec);
4383 if (err < 0)
4384 return err;
4385
David Henningsson26a6cb62012-10-09 15:04:21 +02004386 if (codec->jackpoll_interval)
4387 hda_jackpoll_work(&codec->jackpoll_work.work);
4388 else
4389 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004390 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392}
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394/*
4395 * stream formats
4396 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004397struct hda_rate_tbl {
4398 unsigned int hz;
4399 unsigned int alsa_bits;
4400 unsigned int hda_fmt;
4401};
4402
Takashi Iwai92f10b32010-08-03 14:21:00 +02004403/* rate = base * mult / div */
4404#define HDA_RATE(base, mult, div) \
4405 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4406 (((div) - 1) << AC_FMT_DIV_SHIFT))
4407
Takashi Iwaibefdf312005-08-22 13:57:55 +02004408static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004410
4411 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004412 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4413 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4414 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4415 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4416 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4417 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4418 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4419 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4420 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4421 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4422 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004423#define AC_PAR_PCM_RATE_BITS 11
4424 /* up to bits 10, 384kHZ isn't supported properly */
4425
4426 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004427 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004428
Takashi Iwaibefdf312005-08-22 13:57:55 +02004429 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430};
4431
4432/**
4433 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004434 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 * @rate: the sample rate
4436 * @channels: the number of channels
4437 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4438 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004439 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 *
4441 * Calculate the format bitset from the given rate, channels and th PCM format.
4442 *
4443 * Return zero if invalid.
4444 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004445unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4446 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 unsigned int channels,
4448 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004449 unsigned int maxbps,
4450 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 int i;
4453 unsigned int val = 0;
4454
Takashi Iwaibefdf312005-08-22 13:57:55 +02004455 for (i = 0; rate_bits[i].hz; i++)
4456 if (rate_bits[i].hz == rate) {
4457 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 break;
4459 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004460 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004461 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return 0;
4463 }
4464
4465 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004466 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 return 0;
4468 }
4469 val |= channels - 1;
4470
4471 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004472 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004473 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004474 break;
4475 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004476 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 case 20:
4479 case 24:
4480 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004481 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004482 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004484 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004486 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 break;
4488 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004489 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004490 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return 0;
4492 }
4493
Anssi Hannula32c168c2010-08-03 13:28:57 +03004494 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004495 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return val;
4498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004499EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004501static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4502 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004503{
4504 unsigned int val = 0;
4505 if (nid != codec->afg &&
4506 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4507 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4508 if (!val || val == -1)
4509 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4510 if (!val || val == -1)
4511 return 0;
4512 return val;
4513}
4514
4515static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4516{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004517 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004518 get_pcm_param);
4519}
4520
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004521static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4522 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004523{
4524 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4525 if (!streams || streams == -1)
4526 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4527 if (!streams || streams == -1)
4528 return 0;
4529 return streams;
4530}
4531
4532static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4533{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004534 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004535 get_stream_param);
4536}
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538/**
4539 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4540 * @codec: the HDA codec
4541 * @nid: NID to query
4542 * @ratesp: the pointer to store the detected rate bitflags
4543 * @formatsp: the pointer to store the detected formats
4544 * @bpsp: the pointer to store the detected format widths
4545 *
4546 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4547 * or @bsps argument is ignored.
4548 *
4549 * Returns 0 if successful, otherwise a negative error code.
4550 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004551int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4553{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004554 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004556 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004557 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 if (ratesp) {
4560 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004561 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004563 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004565 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004566 codec_err(codec,
4567 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4568 nid, val,
4569 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004570 return -EIO;
4571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 *ratesp = rates;
4573 }
4574
4575 if (formatsp || bpsp) {
4576 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004577 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004579 streams = query_stream_param(codec, nid);
4580 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 bps = 0;
4584 if (streams & AC_SUPFMT_PCM) {
4585 if (val & AC_SUPPCM_BITS_8) {
4586 formats |= SNDRV_PCM_FMTBIT_U8;
4587 bps = 8;
4588 }
4589 if (val & AC_SUPPCM_BITS_16) {
4590 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4591 bps = 16;
4592 }
4593 if (wcaps & AC_WCAP_DIGITAL) {
4594 if (val & AC_SUPPCM_BITS_32)
4595 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4596 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4597 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4598 if (val & AC_SUPPCM_BITS_24)
4599 bps = 24;
4600 else if (val & AC_SUPPCM_BITS_20)
4601 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004602 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4603 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4605 if (val & AC_SUPPCM_BITS_32)
4606 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 else if (val & AC_SUPPCM_BITS_24)
4608 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004609 else if (val & AC_SUPPCM_BITS_20)
4610 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
4612 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004613#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004614 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004616 if (!bps)
4617 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004618 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004619#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004620 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004621 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /* temporary hack: we have still no proper support
4623 * for the direct AC3 stream...
4624 */
4625 formats |= SNDRV_PCM_FMTBIT_U8;
4626 bps = 8;
4627 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004628 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004629 codec_err(codec,
4630 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4631 nid, val,
4632 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4633 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004634 return -EIO;
4635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (formatsp)
4637 *formatsp = formats;
4638 if (bpsp)
4639 *bpsp = bps;
4640 }
4641
4642 return 0;
4643}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004644EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
4646/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004647 * snd_hda_is_supported_format - Check the validity of the format
4648 * @codec: HD-audio codec
4649 * @nid: NID to check
4650 * @format: the HD-audio format value to check
4651 *
4652 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 *
4654 * Returns 1 if supported, 0 if not.
4655 */
4656int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4657 unsigned int format)
4658{
4659 int i;
4660 unsigned int val = 0, rate, stream;
4661
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004662 val = query_pcm_param(codec, nid);
4663 if (!val)
4664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004667 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004668 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (val & (1 << i))
4670 break;
4671 return 0;
4672 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004673 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return 0;
4675
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004676 stream = query_stream_param(codec, nid);
4677 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return 0;
4679
4680 if (stream & AC_SUPFMT_PCM) {
4681 switch (format & 0xf0) {
4682 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004683 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return 0;
4685 break;
4686 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004687 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return 0;
4689 break;
4690 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004691 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return 0;
4693 break;
4694 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004695 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return 0;
4697 break;
4698 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004699 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 return 0;
4701 break;
4702 default:
4703 return 0;
4704 }
4705 } else {
4706 /* FIXME: check for float32 and AC3? */
4707 }
4708
4709 return 1;
4710}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004711EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713/*
4714 * PCM stuff
4715 */
4716static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4717 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004718 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
4720 return 0;
4721}
4722
4723static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4724 struct hda_codec *codec,
4725 unsigned int stream_tag,
4726 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004727 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
4729 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4730 return 0;
4731}
4732
4733static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4734 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004735 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Takashi Iwai888afa12008-03-18 09:57:50 +01004737 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 return 0;
4739}
4740
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004741static int set_pcm_default_values(struct hda_codec *codec,
4742 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004744 int err;
4745
Takashi Iwai0ba21762007-04-16 11:29:14 +02004746 /* query support PCM information from the given NID */
4747 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004748 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004749 info->rates ? NULL : &info->rates,
4750 info->formats ? NULL : &info->formats,
4751 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004752 if (err < 0)
4753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
4755 if (info->ops.open == NULL)
4756 info->ops.open = hda_pcm_default_open_close;
4757 if (info->ops.close == NULL)
4758 info->ops.close = hda_pcm_default_open_close;
4759 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004760 if (snd_BUG_ON(!info->nid))
4761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 info->ops.prepare = hda_pcm_default_prepare;
4763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004765 if (snd_BUG_ON(!info->nid))
4766 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 info->ops.cleanup = hda_pcm_default_cleanup;
4768 }
4769 return 0;
4770}
4771
Takashi Iwaieb541332010-08-06 13:48:11 +02004772/*
4773 * codec prepare/cleanup entries
4774 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004775/**
4776 * snd_hda_codec_prepare - Prepare a stream
4777 * @codec: the HDA codec
4778 * @hinfo: PCM information
4779 * @stream: stream tag to assign
4780 * @format: format id to assign
4781 * @substream: PCM substream to assign
4782 *
4783 * Calls the prepare callback set by the codec with the given arguments.
4784 * Clean up the inactive streams when successful.
4785 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004786int snd_hda_codec_prepare(struct hda_codec *codec,
4787 struct hda_pcm_stream *hinfo,
4788 unsigned int stream,
4789 unsigned int format,
4790 struct snd_pcm_substream *substream)
4791{
4792 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004793 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004794 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4795 if (ret >= 0)
4796 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004797 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004798 return ret;
4799}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004800EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004801
Takashi Iwai95a962c2014-10-29 16:03:58 +01004802/**
4803 * snd_hda_codec_cleanup - Prepare a stream
4804 * @codec: the HDA codec
4805 * @hinfo: PCM information
4806 * @substream: PCM substream
4807 *
4808 * Calls the cleanup callback set by the codec with the given arguments.
4809 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004810void snd_hda_codec_cleanup(struct hda_codec *codec,
4811 struct hda_pcm_stream *hinfo,
4812 struct snd_pcm_substream *substream)
4813{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004814 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004815 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004816 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004817}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004818EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004819
Takashi Iwaid5191e52009-11-16 14:58:17 +01004820/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004821const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4822 "Audio", "SPDIF", "HDMI", "Modem"
4823};
4824
Takashi Iwai176d5332008-07-30 15:01:44 +02004825/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004826 * get the empty PCM device number to assign
4827 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004828static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004829{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004830 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004831 /* assigned to static slots up to dev#10; if more needed, assign
4832 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4833 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004834 static int audio_idx[HDA_PCM_NTYPES][5] = {
4835 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4836 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004837 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004838 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004839 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004840 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004841
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004842 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004843 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004844 return -EINVAL;
4845 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004846
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004847 for (i = 0; audio_idx[type][i] >= 0; i++) {
4848#ifndef CONFIG_SND_DYNAMIC_MINORS
4849 if (audio_idx[type][i] >= 8)
4850 break;
4851#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004852 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4853 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004854 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004855
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004856#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004857 /* non-fixed slots starting from 10 */
4858 for (i = 10; i < 32; i++) {
4859 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4860 return i;
4861 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004862#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004863
Takashi Iwai4e76a882014-02-25 12:21:03 +01004864 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004865 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004866#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004867 dev_warn(bus->card->dev,
4868 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004869#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004870 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004871}
4872
4873/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004874 * attach a new PCM stream
4875 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004876static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004877{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004878 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004879 struct hda_pcm_stream *info;
4880 int stream, err;
4881
Takashi Iwaib91f0802008-11-04 08:43:08 +01004882 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004883 return -EINVAL;
4884 for (stream = 0; stream < 2; stream++) {
4885 info = &pcm->stream[stream];
4886 if (info->substreams) {
4887 err = set_pcm_default_values(codec, info);
4888 if (err < 0)
4889 return err;
4890 }
4891 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004892 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004893}
4894
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004895/* assign all PCMs of the given codec */
4896int snd_hda_codec_build_pcms(struct hda_codec *codec)
4897{
4898 unsigned int pcm;
4899 int err;
4900
4901 if (!codec->num_pcms) {
4902 if (!codec->patch_ops.build_pcms)
4903 return 0;
4904 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004905 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004906 codec_err(codec,
4907 "cannot build PCMs for #%d (error %d)\n",
4908 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004909 err = snd_hda_codec_reset(codec);
4910 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004911 codec_err(codec,
4912 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004913 return err;
4914 }
4915 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004916 }
4917 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4918 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4919 int dev;
4920
4921 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004922 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004923
4924 if (!cpcm->pcm) {
4925 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4926 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004927 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004928 cpcm->device = dev;
4929 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004930 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004931 codec_err(codec,
4932 "cannot attach PCM stream %d for codec #%d\n",
4933 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004934 continue; /* no fatal error */
4935 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004936 }
4937 }
4938 return 0;
4939}
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941/**
4942 * snd_hda_build_pcms - build PCM information
4943 * @bus: the BUS
4944 *
4945 * Create PCM information for each codec included in the bus.
4946 *
4947 * The build_pcms codec patch is requested to set up codec->num_pcms and
4948 * codec->pcm_info properly. The array is referred by the top-level driver
4949 * to create its PCM instances.
4950 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4951 * callback.
4952 *
4953 * At least, substreams, channels_min and channels_max must be filled for
4954 * each stream. substreams = 0 indicates that the stream doesn't exist.
4955 * When rates and/or formats are zero, the supported values are queried
4956 * from the given nid. The nid is used also by the default ops.prepare
4957 * and ops.cleanup callbacks.
4958 *
4959 * The driver needs to call ops.open in its open callback. Similarly,
4960 * ops.close is supposed to be called in the close callback.
4961 * ops.prepare should be called in the prepare or hw_params callback
4962 * with the proper parameters for set up.
4963 * ops.cleanup should be called in hw_free for clean up of streams.
4964 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004965 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004967int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004969 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Takashi Iwai0ba21762007-04-16 11:29:14 +02004971 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004972 int err = snd_hda_codec_build_pcms(codec);
4973 if (err < 0)
4974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 }
4976 return 0;
4977}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004978EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 * snd_hda_add_new_ctls - create controls from the array
4982 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004983 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 *
4985 * This helper function creates and add new controls in the given array.
4986 * The array must be terminated with an empty entry as terminator.
4987 *
4988 * Returns 0 if successful, or a negative error code.
4989 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004990int snd_hda_add_new_ctls(struct hda_codec *codec,
4991 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004993 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
4995 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004996 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004997 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004998 if (knew->iface == -1) /* skip this codec private value */
4999 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01005000 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01005001 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02005002 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01005003 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01005004 if (addr > 0)
5005 kctl->id.device = addr;
5006 if (idx > 0)
5007 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01005008 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005009 if (!err)
5010 break;
5011 /* try first with another device index corresponding to
5012 * the codec addr; if it still fails (or it's the
5013 * primary codec), then try another control index
5014 */
5015 if (!addr && codec->addr)
5016 addr = codec->addr;
5017 else if (!idx && !knew->index) {
5018 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02005019 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005020 if (idx <= 0)
5021 return err;
5022 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01005023 return err;
5024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 }
5026 return 0;
5027}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005028EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Takashi Iwai83012a72012-08-24 18:38:08 +02005030#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02005031static void hda_power_work(struct work_struct *work)
5032{
5033 struct hda_codec *codec =
5034 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01005035 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07005036 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005037
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005038 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005039 if (codec->power_transition > 0) { /* during power-up sequence? */
5040 spin_unlock(&codec->power_lock);
5041 return;
5042 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02005043 if (!codec->power_on || codec->power_count) {
5044 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005045 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005046 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02005047 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005048 spin_unlock(&codec->power_lock);
5049
Dylan Reidd17344b2012-09-28 15:57:01 -07005050 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005051 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5052 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005053}
5054
5055static void hda_keep_power_on(struct hda_codec *codec)
5056{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005057 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005058 codec->power_count++;
5059 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005060 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005061 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005062 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005063}
5064
Takashi Iwaid5191e52009-11-16 14:58:17 +01005065/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005066void snd_hda_update_power_acct(struct hda_codec *codec)
5067{
5068 unsigned long delta = jiffies - codec->power_jiffies;
5069 if (codec->power_on)
5070 codec->power_on_acct += delta;
5071 else
5072 codec->power_off_acct += delta;
5073 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005074}
5075
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005076/* Transition to powered up, if wait_power_down then wait for a pending
5077 * transition to D3 to complete. A pending D3 transition is indicated
5078 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005079/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005080static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005081{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005082 /* Return if power_on or transitioning to power_on, unless currently
5083 * powering down. */
5084 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005085 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005086 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005087 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005088
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005089 cancel_delayed_work_sync(&codec->power_work);
5090
5091 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005092 /* If the power down delayed work was cancelled above before starting,
5093 * then there is no need to go through power up here.
5094 */
5095 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005096 if (codec->power_transition < 0)
5097 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005098 return;
5099 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005100
Takashi Iwaid66fee52011-08-02 15:39:31 +02005101 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005102 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005103 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005104 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005105 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005106 spin_unlock(&codec->power_lock);
5107
Takashi Iwaicb53c622007-08-10 17:21:45 +02005108 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005109
5110 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005111 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005112}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005113
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005114#define power_save(codec) \
5115 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005116
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005117/* Transition to powered down */
5118static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005119{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005120 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005121 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005122
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005123 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005124 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005125 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005126 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005127 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005128}
5129
5130/**
5131 * snd_hda_power_save - Power-up/down/sync the codec
5132 * @codec: HD-audio codec
5133 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005134 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005135 *
5136 * Change the power-up counter via @delta, and power up or down the hardware
5137 * appropriately. For the power-down, queue to the delayed action.
5138 * Passing zero to @delta means to synchronize the power state.
5139 */
5140void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5141{
5142 spin_lock(&codec->power_lock);
5143 codec->power_count += delta;
5144 trace_hda_power_count(codec);
5145 if (delta > 0)
5146 __snd_hda_power_up(codec, d3wait);
5147 else
5148 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005149 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005150}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005151EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005152
Takashi Iwaid5191e52009-11-16 14:58:17 +01005153/**
5154 * snd_hda_check_amp_list_power - Check the amp list and update the power
5155 * @codec: HD-audio codec
5156 * @check: the object containing an AMP list and the status
5157 * @nid: NID to check / update
5158 *
5159 * Check whether the given NID is in the amp list. If it's in the list,
5160 * check the current AMP status, and update the the power-status according
5161 * to the mute status.
5162 *
5163 * This function is supposed to be set or called from the check_power_status
5164 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005165 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005166int snd_hda_check_amp_list_power(struct hda_codec *codec,
5167 struct hda_loopback_check *check,
5168 hda_nid_t nid)
5169{
Takashi Iwai031024e2011-05-02 11:29:30 +02005170 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005171 int ch, v;
5172
5173 if (!check->amplist)
5174 return 0;
5175 for (p = check->amplist; p->nid; p++) {
5176 if (p->nid == nid)
5177 break;
5178 }
5179 if (!p->nid)
5180 return 0; /* nothing changed */
5181
5182 for (p = check->amplist; p->nid; p++) {
5183 for (ch = 0; ch < 2; ch++) {
5184 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5185 p->idx);
5186 if (!(v & HDA_AMP_MUTE) && v > 0) {
5187 if (!check->power_on) {
5188 check->power_on = 1;
5189 snd_hda_power_up(codec);
5190 }
5191 return 1;
5192 }
5193 }
5194 }
5195 if (check->power_on) {
5196 check->power_on = 0;
5197 snd_hda_power_down(codec);
5198 }
5199 return 0;
5200}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005201EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005204/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005205 * Channel mode helper
5206 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005207
5208/**
5209 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005210 * @codec: the HDA codec
5211 * @uinfo: pointer to get/store the data
5212 * @chmode: channel mode array
5213 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005214 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005215int snd_hda_ch_mode_info(struct hda_codec *codec,
5216 struct snd_ctl_elem_info *uinfo,
5217 const struct hda_channel_mode *chmode,
5218 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005219{
5220 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5221 uinfo->count = 1;
5222 uinfo->value.enumerated.items = num_chmodes;
5223 if (uinfo->value.enumerated.item >= num_chmodes)
5224 uinfo->value.enumerated.item = num_chmodes - 1;
5225 sprintf(uinfo->value.enumerated.name, "%dch",
5226 chmode[uinfo->value.enumerated.item].channels);
5227 return 0;
5228}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005229EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005230
Takashi Iwaid5191e52009-11-16 14:58:17 +01005231/**
5232 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005233 * @codec: the HDA codec
5234 * @ucontrol: pointer to get/store the data
5235 * @chmode: channel mode array
5236 * @num_chmodes: channel mode array size
5237 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005239int snd_hda_ch_mode_get(struct hda_codec *codec,
5240 struct snd_ctl_elem_value *ucontrol,
5241 const struct hda_channel_mode *chmode,
5242 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005243 int max_channels)
5244{
5245 int i;
5246
5247 for (i = 0; i < num_chmodes; i++) {
5248 if (max_channels == chmode[i].channels) {
5249 ucontrol->value.enumerated.item[0] = i;
5250 break;
5251 }
5252 }
5253 return 0;
5254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005255EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005256
Takashi Iwaid5191e52009-11-16 14:58:17 +01005257/**
5258 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005259 * @codec: the HDA codec
5260 * @ucontrol: pointer to get/store the data
5261 * @chmode: channel mode array
5262 * @num_chmodes: channel mode array size
5263 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005264 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005265int snd_hda_ch_mode_put(struct hda_codec *codec,
5266 struct snd_ctl_elem_value *ucontrol,
5267 const struct hda_channel_mode *chmode,
5268 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005269 int *max_channelsp)
5270{
5271 unsigned int mode;
5272
5273 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005274 if (mode >= num_chmodes)
5275 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005276 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005277 return 0;
5278 /* change the current channel setting */
5279 *max_channelsp = chmode[mode].channels;
5280 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005281 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005282 return 1;
5283}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005284EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286/*
5287 * input MUX helper
5288 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005289
5290/**
5291 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005292 * @imux: imux helper object
5293 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005294 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005295int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5296 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
5298 unsigned int index;
5299
5300 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5301 uinfo->count = 1;
5302 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005303 if (!imux->num_items)
5304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 index = uinfo->value.enumerated.item;
5306 if (index >= imux->num_items)
5307 index = imux->num_items - 1;
5308 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5309 return 0;
5310}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005311EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Takashi Iwaid5191e52009-11-16 14:58:17 +01005313/**
5314 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005315 * @codec: the HDA codec
5316 * @imux: imux helper object
5317 * @ucontrol: pointer to get/store the data
5318 * @nid: input mux NID
5319 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005320 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005321int snd_hda_input_mux_put(struct hda_codec *codec,
5322 const struct hda_input_mux *imux,
5323 struct snd_ctl_elem_value *ucontrol,
5324 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 unsigned int *cur_val)
5326{
5327 unsigned int idx;
5328
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005329 if (!imux->num_items)
5330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 idx = ucontrol->value.enumerated.item[0];
5332 if (idx >= imux->num_items)
5333 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005334 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005336 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5337 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 *cur_val = idx;
5339 return 1;
5340}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005341EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
5343
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005344/**
5345 * snd_hda_enum_helper_info - Helper for simple enum ctls
5346 * @kcontrol: ctl element
5347 * @uinfo: pointer to get/store the data
5348 * @num_items: number of enum items
5349 * @texts: enum item string array
5350 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005351 * process kcontrol info callback of a simple string enum array
5352 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5353 */
5354int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5355 struct snd_ctl_elem_info *uinfo,
5356 int num_items, const char * const *texts)
5357{
5358 static const char * const texts_default[] = {
5359 "Disabled", "Enabled"
5360 };
5361
5362 if (!texts || !num_items) {
5363 num_items = 2;
5364 texts = texts_default;
5365 }
5366
Takashi Iwai3ff72212014-10-20 18:17:28 +02005367 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005369EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005370
5371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 * Multi-channel / digital-out PCM helper functions
5373 */
5374
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005375/* setup SPDIF output stream */
5376static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5377 unsigned int stream_tag, unsigned int format)
5378{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005379 struct hda_spdif_out *spdif;
5380 unsigned int curr_fmt;
5381 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005382
Laurence Darby3bef1c32012-11-03 17:00:06 +00005383 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5384 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5385 AC_VERB_GET_STREAM_FORMAT, 0);
5386 reset = codec->spdif_status_reset &&
5387 (spdif->ctls & AC_DIG1_ENABLE) &&
5388 curr_fmt != format;
5389
5390 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5391 updated */
5392 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005393 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005394 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005395 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005396 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005397 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005398 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005399 for (d = codec->slave_dig_outs; *d; d++)
5400 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5401 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005402 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005403 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005404 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005405 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005406 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005407}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005408
Takashi Iwai2f728532008-09-25 16:32:41 +02005409static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5410{
5411 snd_hda_codec_cleanup_stream(codec, nid);
5412 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005413 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005414 for (d = codec->slave_dig_outs; *d; d++)
5415 snd_hda_codec_cleanup_stream(codec, *d);
5416 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005417}
5418
Takashi Iwaid5191e52009-11-16 14:58:17 +01005419/**
5420 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5421 * @bus: HD-audio bus
5422 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005423void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5424{
5425 struct hda_codec *codec;
5426
5427 if (!bus)
5428 return;
5429 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005430 if (hda_codec_is_power_on(codec) &&
5431 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005432 codec->patch_ops.reboot_notify(codec);
5433 }
5434}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005435EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005436
Takashi Iwaid5191e52009-11-16 14:58:17 +01005437/**
5438 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005439 * @codec: the HDA codec
5440 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005442int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5443 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
Ingo Molnar62932df2006-01-16 16:34:20 +01005445 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005446 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5447 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005448 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005450 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 return 0;
5452}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005453EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Takashi Iwaid5191e52009-11-16 14:58:17 +01005455/**
5456 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005457 * @codec: the HDA codec
5458 * @mout: hda_multi_out object
5459 * @stream_tag: stream tag to assign
5460 * @format: format id to assign
5461 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005462 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005463int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5464 struct hda_multi_out *mout,
5465 unsigned int stream_tag,
5466 unsigned int format,
5467 struct snd_pcm_substream *substream)
5468{
5469 mutex_lock(&codec->spdif_mutex);
5470 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5471 mutex_unlock(&codec->spdif_mutex);
5472 return 0;
5473}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005474EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005475
Takashi Iwaid5191e52009-11-16 14:58:17 +01005476/**
5477 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005478 * @codec: the HDA codec
5479 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005480 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005481int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5482 struct hda_multi_out *mout)
5483{
5484 mutex_lock(&codec->spdif_mutex);
5485 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5486 mutex_unlock(&codec->spdif_mutex);
5487 return 0;
5488}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005489EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005490
Takashi Iwaid5191e52009-11-16 14:58:17 +01005491/**
5492 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005493 * @codec: the HDA codec
5494 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005496int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5497 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
Ingo Molnar62932df2006-01-16 16:34:20 +01005499 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005501 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 return 0;
5503}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005504EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Takashi Iwaid5191e52009-11-16 14:58:17 +01005506/**
5507 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005508 * @codec: the HDA codec
5509 * @mout: hda_multi_out object
5510 * @substream: PCM substream to assign
5511 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005512 *
5513 * Open analog outputs and set up the hw-constraints.
5514 * If the digital outputs can be opened as slave, open the digital
5515 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005517int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5518 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005519 struct snd_pcm_substream *substream,
5520 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521{
Takashi Iwai9a081602008-02-12 18:37:26 +01005522 struct snd_pcm_runtime *runtime = substream->runtime;
5523 runtime->hw.channels_max = mout->max_channels;
5524 if (mout->dig_out_nid) {
5525 if (!mout->analog_rates) {
5526 mout->analog_rates = hinfo->rates;
5527 mout->analog_formats = hinfo->formats;
5528 mout->analog_maxbps = hinfo->maxbps;
5529 } else {
5530 runtime->hw.rates = mout->analog_rates;
5531 runtime->hw.formats = mout->analog_formats;
5532 hinfo->maxbps = mout->analog_maxbps;
5533 }
5534 if (!mout->spdif_rates) {
5535 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5536 &mout->spdif_rates,
5537 &mout->spdif_formats,
5538 &mout->spdif_maxbps);
5539 }
5540 mutex_lock(&codec->spdif_mutex);
5541 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005542 if ((runtime->hw.rates & mout->spdif_rates) &&
5543 (runtime->hw.formats & mout->spdif_formats)) {
5544 runtime->hw.rates &= mout->spdif_rates;
5545 runtime->hw.formats &= mout->spdif_formats;
5546 if (mout->spdif_maxbps < hinfo->maxbps)
5547 hinfo->maxbps = mout->spdif_maxbps;
5548 } else {
5549 mout->share_spdif = 0;
5550 /* FIXME: need notify? */
5551 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005552 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005553 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5556 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5557}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005558EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Takashi Iwaid5191e52009-11-16 14:58:17 +01005560/**
5561 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005562 * @codec: the HDA codec
5563 * @mout: hda_multi_out object
5564 * @stream_tag: stream tag to assign
5565 * @format: format id to assign
5566 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005567 *
5568 * Set up the i/o for analog out.
5569 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005571int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5572 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 unsigned int stream_tag,
5574 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005575 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
Takashi Iwaidda14412011-05-02 11:29:30 +02005577 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005579 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 int i;
5581
Ingo Molnar62932df2006-01-16 16:34:20 +01005582 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005583 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005584 if (mout->dig_out_nid && mout->share_spdif &&
5585 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005587 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5588 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005589 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005591 setup_dig_out_stream(codec, mout->dig_out_nid,
5592 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 } else {
5594 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005595 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
5597 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005598 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
5600 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005601 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5602 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005603 if (!mout->no_share_stream &&
5604 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005606 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5607 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005608 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005609 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5610 if (!mout->no_share_stream && mout->hp_out_nid[i])
5611 snd_hda_codec_setup_stream(codec,
5612 mout->hp_out_nid[i],
5613 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 /* surrounds */
5616 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005617 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005618 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5619 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005620 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005621 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5622 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005624
5625 /* extra surrounds */
5626 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5627 int ch = 0;
5628 if (!mout->extra_out_nid[i])
5629 break;
5630 if (chs >= (i + 1) * 2)
5631 ch = i * 2;
5632 else if (!mout->no_share_stream)
5633 break;
5634 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5635 stream_tag, ch, format);
5636 }
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 return 0;
5639}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005640EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Takashi Iwaid5191e52009-11-16 14:58:17 +01005642/**
5643 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005644 * @codec: the HDA codec
5645 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005647int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5648 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
Takashi Iwaidda14412011-05-02 11:29:30 +02005650 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 int i;
5652
5653 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005654 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005656 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005657 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5658 if (mout->hp_out_nid[i])
5659 snd_hda_codec_cleanup_stream(codec,
5660 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005661 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5662 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005663 snd_hda_codec_cleanup_stream(codec,
5664 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005665 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005667 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 mout->dig_out_used = 0;
5669 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005670 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 return 0;
5672}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005673EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Takashi Iwai47408602012-04-20 13:06:53 +02005675/**
5676 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005677 * @codec: the HDA codec
5678 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005679 *
5680 * Guess the suitable VREF pin bits to be set as the pin-control value.
5681 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5682 */
5683unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5684{
5685 unsigned int pincap;
5686 unsigned int oldval;
5687 oldval = snd_hda_codec_read(codec, pin, 0,
5688 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5689 pincap = snd_hda_query_pin_caps(codec, pin);
5690 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5691 /* Exception: if the default pin setup is vref50, we give it priority */
5692 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5693 return AC_PINCTL_VREF_80;
5694 else if (pincap & AC_PINCAP_VREF_50)
5695 return AC_PINCTL_VREF_50;
5696 else if (pincap & AC_PINCAP_VREF_100)
5697 return AC_PINCTL_VREF_100;
5698 else if (pincap & AC_PINCAP_VREF_GRD)
5699 return AC_PINCTL_VREF_GRD;
5700 return AC_PINCTL_VREF_HIZ;
5701}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005702EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005703
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005704/**
5705 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5706 * @codec: the HDA codec
5707 * @pin: referred pin NID
5708 * @val: pin ctl value to audit
5709 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005710unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5711 hda_nid_t pin, unsigned int val)
5712{
5713 static unsigned int cap_lists[][2] = {
5714 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5715 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5716 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5717 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5718 };
5719 unsigned int cap;
5720
5721 if (!val)
5722 return 0;
5723 cap = snd_hda_query_pin_caps(codec, pin);
5724 if (!cap)
5725 return val; /* don't know what to do... */
5726
5727 if (val & AC_PINCTL_OUT_EN) {
5728 if (!(cap & AC_PINCAP_OUT))
5729 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5730 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5731 val &= ~AC_PINCTL_HP_EN;
5732 }
5733
5734 if (val & AC_PINCTL_IN_EN) {
5735 if (!(cap & AC_PINCAP_IN))
5736 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5737 else {
5738 unsigned int vcap, vref;
5739 int i;
5740 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5741 vref = val & AC_PINCTL_VREFEN;
5742 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5743 if (vref == cap_lists[i][0] &&
5744 !(vcap & cap_lists[i][1])) {
5745 if (i == ARRAY_SIZE(cap_lists) - 1)
5746 vref = AC_PINCTL_VREF_HIZ;
5747 else
5748 vref = cap_lists[i + 1][0];
5749 }
5750 }
5751 val &= ~AC_PINCTL_VREFEN;
5752 val |= vref;
5753 }
5754 }
5755
5756 return val;
5757}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005758EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005759
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005760/**
5761 * _snd_hda_pin_ctl - Helper to set pin ctl value
5762 * @codec: the HDA codec
5763 * @pin: referred pin NID
5764 * @val: pin control value to set
5765 * @cached: access over codec pinctl cache or direct write
5766 *
5767 * This function is a helper to set a pin ctl value more safely.
5768 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5769 * value in pin target array via snd_hda_codec_set_pin_target(), then
5770 * actually writes the value via either snd_hda_codec_update_cache() or
5771 * snd_hda_codec_write() depending on @cached flag.
5772 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005773int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5774 unsigned int val, bool cached)
5775{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005776 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005777 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005778 if (cached)
5779 return snd_hda_codec_update_cache(codec, pin, 0,
5780 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5781 else
5782 return snd_hda_codec_write(codec, pin, 0,
5783 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5784}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005785EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005786
Takashi Iwai990061c2010-09-09 22:08:44 +02005787/**
5788 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005789 * @codec: the HDA codec
5790 * @imux: imux helper object
5791 * @label: the name of imux item to assign
5792 * @index: index number of imux item to assign
5793 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005794 *
5795 * When the same label is used already in the existing items, the number
5796 * suffix is appended to the label. This label index number is stored
5797 * to type_idx when non-NULL pointer is given.
5798 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005799int snd_hda_add_imux_item(struct hda_codec *codec,
5800 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005801 int index, int *type_idx)
5802{
5803 int i, label_idx = 0;
5804 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005805 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005806 return -EINVAL;
5807 }
5808 for (i = 0; i < imux->num_items; i++) {
5809 if (!strncmp(label, imux->items[i].label, strlen(label)))
5810 label_idx++;
5811 }
5812 if (type_idx)
5813 *type_idx = label_idx;
5814 if (label_idx > 0)
5815 snprintf(imux->items[imux->num_items].label,
5816 sizeof(imux->items[imux->num_items].label),
5817 "%s %d", label, label_idx);
5818 else
5819 strlcpy(imux->items[imux->num_items].label, label,
5820 sizeof(imux->items[imux->num_items].label));
5821 imux->items[imux->num_items].index = index;
5822 imux->num_items++;
5823 return 0;
5824}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005825EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005826
Takashi Iwai4a471b72005-12-07 13:56:29 +01005827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828#ifdef CONFIG_PM
5829/*
5830 * power management
5831 */
5832
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005833
5834static void hda_async_suspend(void *data, async_cookie_t cookie)
5835{
5836 hda_call_codec_suspend(data, false);
5837}
5838
5839static void hda_async_resume(void *data, async_cookie_t cookie)
5840{
5841 hda_call_codec_resume(data);
5842}
5843
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844/**
5845 * snd_hda_suspend - suspend the codecs
5846 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 *
5848 * Returns 0 if successful.
5849 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005850int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005852 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005853 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Takashi Iwai0ba21762007-04-16 11:29:14 +02005855 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005856 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005857 if (hda_codec_is_power_on(codec)) {
5858 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005859 async_schedule_domain(hda_async_suspend, codec,
5860 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005861 else
5862 hda_call_codec_suspend(codec, false);
5863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005865
5866 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005867 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 return 0;
5870}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005871EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873/**
5874 * snd_hda_resume - resume the codecs
5875 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 *
5877 * Returns 0 if successful.
5878 */
5879int snd_hda_resume(struct hda_bus *bus)
5880{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005881 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005882 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Takashi Iwai0ba21762007-04-16 11:29:14 +02005884 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005885 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005886 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005887 else
5888 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005890
5891 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005892 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 return 0;
5895}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005896EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005897#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005898
5899/*
5900 * generic arrays
5901 */
5902
Takashi Iwaid5191e52009-11-16 14:58:17 +01005903/**
5904 * snd_array_new - get a new element from the given array
5905 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005906 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005907 * Get a new element from the given array. If it exceeds the
5908 * pre-allocated array size, re-allocate the array.
5909 *
5910 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005911 */
5912void *snd_array_new(struct snd_array *array)
5913{
Takashi Iwai12f17712012-10-10 08:59:14 +02005914 if (snd_BUG_ON(!array->elem_size))
5915 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005916 if (array->used >= array->alloced) {
5917 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005918 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005919 void *nlist;
5920 if (snd_BUG_ON(num >= 4096))
5921 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005922 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005923 if (!nlist)
5924 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005925 array->list = nlist;
5926 array->alloced = num;
5927 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005928 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005929}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005930EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005931
Takashi Iwaid5191e52009-11-16 14:58:17 +01005932/**
5933 * snd_array_free - free the given array elements
5934 * @array: the array object
5935 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005936void snd_array_free(struct snd_array *array)
5937{
5938 kfree(array->list);
5939 array->used = 0;
5940 array->alloced = 0;
5941 array->list = NULL;
5942}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005943EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005944
Takashi Iwaid5191e52009-11-16 14:58:17 +01005945/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005946 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5947 * @pcm: PCM caps bits
5948 * @buf: the string buffer to write
5949 * @buflen: the max buffer length
5950 *
5951 * used by hda_proc.c and hda_eld.c
5952 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005953void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5954{
5955 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5956 int i, j;
5957
5958 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5959 if (pcm & (AC_SUPPCM_BITS_8 << i))
5960 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5961
5962 buf[j] = '\0'; /* necessary when j == 0 */
5963}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005964EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005965
5966MODULE_DESCRIPTION("HDA codec core");
5967MODULE_LICENSE("GPL");