blob: 2fe86d2e1b09dfc6f5d8f095a3fc3ad4792680f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Takashi Iwaidff86f82014-02-19 11:11:20 +010070 { 0x1af4, "QEMU" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010072 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020073 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {} /* terminator */
75};
76
Takashi Iwai1289e9e2008-11-27 15:47:11 +010077static DEFINE_MUTEX(preset_mutex);
78static LIST_HEAD(hda_preset_tables);
79
Takashi Iwai95a962c2014-10-29 16:03:58 +010080/**
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
83 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010084int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
85{
86 mutex_lock(&preset_mutex);
87 list_add_tail(&preset->list, &hda_preset_tables);
88 mutex_unlock(&preset_mutex);
89 return 0;
90}
Takashi Iwai2698ea92013-12-18 07:45:52 +010091EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010092
Takashi Iwai95a962c2014-10-29 16:03:58 +010093/**
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
96 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010097int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
98{
99 mutex_lock(&preset_mutex);
100 list_del(&preset->list);
101 mutex_unlock(&preset_mutex);
102 return 0;
103}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100104EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai83012a72012-08-24 18:38:08 +0200106#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +0100107#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200108static void hda_power_work(struct work_struct *work);
109static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100111
112static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700113{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100114 struct hda_bus *bus = codec->bus;
115
116 if ((power_up && codec->pm_up_notified) ||
117 (!power_up && !codec->pm_up_notified))
118 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700119 if (bus->ops.pm_notify)
120 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100121 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700122}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100123
Takashi Iwaicb53c622007-08-10 17:21:45 +0200124#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100125#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200126static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200127#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100128#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200129#endif
130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_location(u32 cfg)
139{
140 static char *bases[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
142 };
143 static unsigned char specials_idx[] = {
144 0x07, 0x08,
145 0x17, 0x18, 0x19,
146 0x37, 0x38
147 };
148 static char *specials[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
152 };
153 int i;
154 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
155 if ((cfg & 0x0f) < 7)
156 return bases[cfg & 0x0f];
157 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
158 if (cfg == specials_idx[i])
159 return specials[i];
160 }
161 return "UNKNOWN";
162}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100163EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400164
Takashi Iwaid5191e52009-11-16 14:58:17 +0100165/**
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
168 *
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
171 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172const char *snd_hda_get_jack_connectivity(u32 cfg)
173{
174 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
175
176 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
177}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100178EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400179
Takashi Iwaid5191e52009-11-16 14:58:17 +0100180/**
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
183 *
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
186 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400187const char *snd_hda_get_jack_type(u32 cfg)
188{
189 static char *jack_types[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200193 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400194 };
195
196 return jack_types[(cfg & AC_DEFCFG_DEVICE)
197 >> AC_DEFCFG_DEVICE_SHIFT];
198}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100199EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400200
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201/*
202 * Compose a 32bit command word to be sent to the HD-audio controller
203 */
204static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200205make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100206 unsigned int verb, unsigned int parm)
207{
208 u32 val;
209
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200210 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200211 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100212 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200213 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800214 return ~0;
215 }
216
217 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100218 val |= (u32)nid << 20;
219 val |= verb << 8;
220 val |= parm;
221 return val;
222}
223
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224/*
225 * Send and receive a verb
226 */
227static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200228 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229{
230 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200232
Wu Fengguang6430aee2009-07-17 16:49:19 +0800233 if (cmd == ~0)
234 return -1;
235
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200236 if (res)
237 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200239 snd_hda_power_up(codec);
240 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200241 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
242 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100243 for (;;) {
244 trace_hda_send_cmd(codec, cmd);
245 err = bus->ops.command(bus, cmd);
246 if (err != -EAGAIN)
247 break;
248 /* process pending verbs */
249 bus->ops.get_response(bus, codec->addr);
250 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200251 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800252 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200253 trace_hda_get_response(codec, *res);
254 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200255 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 mutex_unlock(&bus->cmd_mutex);
257 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100258 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200259 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100260 codec_dbg(codec,
261 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200262 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200263 bus->ops.bus_reset(bus);
264 }
265 goto again;
266 }
267 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100268 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200269 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 return err;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200277 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * @verb: the verb to send
279 * @parm: the parameter for the verb
280 *
281 * Send a single command and read the corresponding response.
282 *
283 * Returns the obtained response value, or -1 for an error.
284 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200285unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200286 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned int verb, unsigned int parm)
288{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200289 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200290 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200291 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700292 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return res;
294}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100295EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200301 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * @verb: the verb to send
303 * @parm: the parameter for the verb
304 *
305 * Send a single command without waiting for response.
306 *
307 * Returns 0 if successful, or a negative error code.
308 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200309int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
310 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200312 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100313 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200314 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200315 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100317EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/**
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
323 *
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
326 */
327void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
328{
329 for (; seq->nid; seq++)
330 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
331}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100332EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/**
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
337 * @nid: NID to parse
338 * @start_id: the pointer to store the start NID
339 *
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
342 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200343int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned int parm;
347
348 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300349 if (parm == -1) {
350 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200351 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *start_id = (parm >> 16) & 0x7fff;
354 return (int)(parm & 0x7fff);
355}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100356EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358/* connection list element */
359struct hda_conn_list {
360 struct list_head list;
361 int len;
362 hda_nid_t nid;
363 hda_nid_t conns[0];
364};
365
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200366/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100367static struct hda_conn_list *
368lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200369{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100370 struct hda_conn_list *p;
371 list_for_each_entry(p, &codec->conn_list, list) {
372 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200373 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200374 }
375 return NULL;
376}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100378static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
379 const hda_nid_t *list)
380{
381 struct hda_conn_list *p;
382
383 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
384 if (!p)
385 return -ENOMEM;
386 p->len = len;
387 p->nid = nid;
388 memcpy(p->conns, list, len * sizeof(hda_nid_t));
389 list_add(&p->list, &codec->conn_list);
390 return 0;
391}
392
393static void remove_conn_list(struct hda_codec *codec)
394{
395 while (!list_empty(&codec->conn_list)) {
396 struct hda_conn_list *p;
397 p = list_first_entry(&codec->conn_list, typeof(*p), list);
398 list_del(&p->list);
399 kfree(p);
400 }
401}
402
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200403/* read the connection and add to the cache */
404static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100406 hda_nid_t list[32];
407 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200409
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200410 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100411 if (len == -ENOSPC) {
412 len = snd_hda_get_num_raw_conns(codec, nid);
413 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
414 if (!result)
415 return -ENOMEM;
416 len = snd_hda_get_raw_connections(codec, nid, result, len);
417 }
418 if (len >= 0)
419 len = snd_hda_override_conn_list(codec, nid, len, result);
420 if (result != list)
421 kfree(result);
422 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200423}
Takashi Iwaidce20792011-06-24 14:10:28 +0200424
425/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100426 * snd_hda_get_conn_list - get connection list
427 * @codec: the HDA codec
428 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100429 * @listp: the pointer to store NID list
430 *
431 * Parses the connection list of the given widget and stores the pointer
432 * to the list of NIDs.
433 *
434 * Returns the number of connections, or a negative error code.
435 *
436 * Note that the returned pointer isn't protected against the list
437 * modification. If snd_hda_override_conn_list() might be called
438 * concurrently, protect with a mutex appropriately.
439 */
440int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
441 const hda_nid_t **listp)
442{
443 bool added = false;
444
445 for (;;) {
446 int err;
447 const struct hda_conn_list *p;
448
449 /* if the connection-list is already cached, read it */
450 p = lookup_conn_list(codec, nid);
451 if (p) {
452 if (listp)
453 *listp = p->conns;
454 return p->len;
455 }
456 if (snd_BUG_ON(added))
457 return -EINVAL;
458
459 err = read_and_add_raw_conns(codec, nid);
460 if (err < 0)
461 return err;
462 added = true;
463 }
464}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100465EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100466
467/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200468 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * @codec: the HDA codec
470 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200471 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * @max_conns: max. number of connections to store
473 *
474 * Parses the connection list of the given widget and stores the list
475 * of NIDs.
476 *
477 * Returns the number of connections, or a negative error code.
478 */
479int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200480 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200481{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100482 const hda_nid_t *list;
483 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200484
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100485 if (len > 0 && conn_list) {
486 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100487 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200488 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200489 return -EINVAL;
490 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100491 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200492 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200493
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100494 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200495}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100496EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200497
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498/* return CONNLIST_LEN parameter of the given widget */
499static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
500{
501 unsigned int wcaps = get_wcaps(codec, nid);
502 unsigned int parm;
503
504 if (!(wcaps & AC_WCAP_CONN_LIST) &&
505 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
506 return 0;
507
508 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
509 if (parm == -1)
510 parm = 0;
511 return parm;
512}
513
514int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
515{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100516 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100517}
518
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200519/**
520 * snd_hda_get_raw_connections - copy connection list without cache
521 * @codec: the HDA codec
522 * @nid: NID to parse
523 * @conn_list: connection list array
524 * @max_conns: max. number of connections to store
525 *
526 * Like snd_hda_get_connections(), copy the connection list but without
527 * checking through the connection-list cache.
528 * Currently called only from hda_proc.c, so not exported.
529 */
530int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
531 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100536 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100537 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Takashi Iwai4eea3092013-02-07 18:18:19 +0100539 parm = get_num_conns(codec, nid);
540 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200541 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (parm & AC_CLIST_LONG) {
544 /* long form */
545 shift = 16;
546 num_elems = 2;
547 } else {
548 /* short form */
549 shift = 8;
550 num_elems = 4;
551 }
552 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 mask = (1 << (shift-1)) - 1;
554
Takashi Iwai0ba21762007-04-16 11:29:14 +0200555 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0; /* no connection */
557
558 if (conn_len == 1) {
559 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100564 if (conn_list)
565 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 1;
567 }
568
569 /* multi connection */
570 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 prev_nid = 0;
572 for (i = 0; i < conn_len; i++) {
573 int range_val;
574 hda_nid_t val, n;
575
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200576 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 parm = snd_hda_codec_read(codec, nid, 0,
578 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200579 if (parm == -1 && codec->bus->rirb_error)
580 return -EIO;
581 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100583 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100584 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100585 codec_dbg(codec,
586 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200587 nid, i, parm);
588 return 0;
589 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 parm >>= shift;
591 if (range_val) {
592 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200593 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200595 "invalid dep_range_val %x:%x\n",
596 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100599 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100600 if (conn_list) {
601 if (conns >= max_conns)
602 return -ENOSPC;
603 conn_list[conns] = n;
604 }
605 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100606 }
607 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100608 if (conn_list) {
609 if (conns >= max_conns)
610 return -ENOSPC;
611 conn_list[conns] = val;
612 }
613 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100615 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 return conns;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200621 * snd_hda_override_conn_list - add/modify the connection-list to cache
622 * @codec: the HDA codec
623 * @nid: NID to parse
624 * @len: number of connection list entries
625 * @list: the list of connection entries
626 *
627 * Add or modify the given connection-list to the cache. If the corresponding
628 * cache already exists, invalidate it and append a new one.
629 *
630 * Returns zero or a negative error code.
631 */
632int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
633 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100635 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200636
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100637 p = lookup_conn_list(codec, nid);
638 if (p) {
639 list_del(&p->list);
640 kfree(p);
641 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200642
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100643 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100645EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200646
647/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 * snd_hda_get_conn_index - get the connection index of the given NID
649 * @codec: the HDA codec
650 * @mux: NID containing the list
651 * @nid: NID to select
652 * @recursive: 1 when searching NID recursively, otherwise 0
653 *
654 * Parses the connection list of the widget @mux and checks whether the
655 * widget @nid is present. If it is, return the connection index.
656 * Otherwise it returns -1.
657 */
658int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
659 hda_nid_t nid, int recursive)
660{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100661 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 int i, nums;
663
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100664 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200665 for (i = 0; i < nums; i++)
666 if (conn[i] == nid)
667 return i;
668 if (!recursive)
669 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100670 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100671 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200672 return -1;
673 }
674 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200675 for (i = 0; i < nums; i++) {
676 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
677 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
678 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200679 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
680 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200681 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200682 return -1;
683}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100684EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Mengdong Linf1aa0682013-08-26 21:35:21 -0400686
687/* return DEVLIST_LEN parameter of the given widget */
688static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
689{
690 unsigned int wcaps = get_wcaps(codec, nid);
691 unsigned int parm;
692
693 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
694 get_wcaps_type(wcaps) != AC_WID_PIN)
695 return 0;
696
697 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
698 if (parm == -1 && codec->bus->rirb_error)
699 parm = 0;
700 return parm & AC_DEV_LIST_LEN_MASK;
701}
702
703/**
704 * snd_hda_get_devices - copy device list without cache
705 * @codec: the HDA codec
706 * @nid: NID of the pin to parse
707 * @dev_list: device list array
708 * @max_devices: max. number of devices to store
709 *
710 * Copy the device list. This info is dynamic and so not cached.
711 * Currently called only from hda_proc.c, so not exported.
712 */
713int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
714 u8 *dev_list, int max_devices)
715{
716 unsigned int parm;
717 int i, dev_len, devices;
718
719 parm = get_num_devices(codec, nid);
720 if (!parm) /* not multi-stream capable */
721 return 0;
722
723 dev_len = parm + 1;
724 dev_len = dev_len < max_devices ? dev_len : max_devices;
725
726 devices = 0;
727 while (devices < dev_len) {
728 parm = snd_hda_codec_read(codec, nid, 0,
729 AC_VERB_GET_DEVICE_LIST, devices);
730 if (parm == -1 && codec->bus->rirb_error)
731 break;
732
733 for (i = 0; i < 8; i++) {
734 dev_list[devices] = (u8)parm;
735 parm >>= 4;
736 devices++;
737 if (devices >= dev_len)
738 break;
739 }
740 }
741 return devices;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/**
745 * snd_hda_queue_unsol_event - add an unsolicited event to queue
746 * @bus: the BUS
747 * @res: unsolicited event (lower 32bit of RIRB entry)
748 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
749 *
750 * Adds the given event to the queue. The events are processed in
751 * the workqueue asynchronously. Call this function in the interrupt
752 * hanlder when RIRB receives an unsolicited event.
753 *
754 * Returns 0 if successful, or a negative error code.
755 */
756int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
757{
758 struct hda_bus_unsolicited *unsol;
759 unsigned int wp;
760
Wang YanQing2195b062013-05-07 11:27:33 +0800761 if (!bus || !bus->workq)
762 return 0;
763
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200764 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200765 unsol = bus->unsol;
766 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768
769 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
770 unsol->wp = wp;
771
772 wp <<= 1;
773 unsol->queue[wp] = res;
774 unsol->queue[wp + 1] = res_ex;
775
Takashi Iwai6acaed32009-01-12 10:09:24 +0100776 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return 0;
779}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100780EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800783 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
David Howellsc4028952006-11-22 14:57:56 +0000785static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
David Howellsc4028952006-11-22 14:57:56 +0000787 struct hda_bus_unsolicited *unsol =
788 container_of(work, struct hda_bus_unsolicited, work);
789 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct hda_codec *codec;
791 unsigned int rp, caddr, res;
792
793 while (unsol->rp != unsol->wp) {
794 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
795 unsol->rp = rp;
796 rp <<= 1;
797 res = unsol->queue[rp];
798 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200799 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 continue;
801 codec = bus->caddr_tbl[caddr & 0x0f];
802 if (codec && codec->patch_ops.unsol_event)
803 codec->patch_ops.unsol_event(codec, res);
804 }
805}
806
807/*
808 * initialize unsolicited queue
809 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200810static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct hda_bus_unsolicited *unsol;
813
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100814 if (bus->unsol) /* already initialized */
815 return 0;
816
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200817 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200818 if (!unsol) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100819 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -ENOMEM;
821 }
David Howellsc4028952006-11-22 14:57:56 +0000822 INIT_WORK(&unsol->work, process_unsol_events);
823 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 bus->unsol = unsol;
825 return 0;
826}
827
828/*
829 * destructor
830 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100831static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200833 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100834 return;
835
836 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100837 if (bus->workq)
838 flush_workqueue(bus->workq);
Markus Elfringf0acd282014-11-17 10:44:33 +0100839 kfree(bus->unsol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (bus->ops.private_free)
841 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100842 if (bus->workq)
843 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100848static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai2565c892014-02-19 11:41:09 +0100850 snd_hda_bus_free(device->device_data);
851 return 0;
852}
853
854static int snd_hda_bus_dev_disconnect(struct snd_device *device)
855{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100857 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861/**
862 * snd_hda_bus_new - create a HDA bus
863 * @card: the card entry
864 * @temp: the template for hda_bus information
865 * @busp: the pointer to store the created bus instance
866 *
867 * Returns 0 if successful, or a negative error code.
868 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100869int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200870 const struct hda_bus_template *temp,
871 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct hda_bus *bus;
874 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100875 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100876 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .dev_free = snd_hda_bus_dev_free,
878 };
879
Takashi Iwaida3cec32008-08-08 17:12:14 +0200880 if (snd_BUG_ON(!temp))
881 return -EINVAL;
882 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (busp)
886 *busp = NULL;
887
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200888 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100890 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENOMEM;
892 }
893
894 bus->card = card;
895 bus->private_data = temp->private_data;
896 bus->pci = temp->pci;
897 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100898 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 bus->ops = temp->ops;
900
Ingo Molnar62932df2006-01-16 16:34:20 +0100901 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200902 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 INIT_LIST_HEAD(&bus->codec_list);
904
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100905 snprintf(bus->workq_name, sizeof(bus->workq_name),
906 "hd-audio%d", card->number);
907 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100908 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100909 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100910 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100911 kfree(bus);
912 return -ENOMEM;
913 }
914
Takashi Iwai0ba21762007-04-16 11:29:14 +0200915 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
916 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 snd_hda_bus_free(bus);
918 return err;
919 }
920 if (busp)
921 *busp = bus;
922 return 0;
923}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100924EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100926#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200927#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200928 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200929#else
930#define is_generic_config(codec) 0
931#endif
932
Takashi Iwai645f10c2008-11-28 15:07:37 +0100933#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100934#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
935#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100936#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100937#endif
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/*
940 * find a matching codec preset
941 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200942static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200943find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100945 struct hda_codec_preset_list *tbl;
946 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200947 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100949 again:
950 mutex_lock(&preset_mutex);
951 list_for_each_entry(tbl, &hda_preset_tables, list) {
952 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100953 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100954 continue;
955 }
956 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100958 if (preset->afg && preset->afg != codec->afg)
959 continue;
960 if (preset->mfg && preset->mfg != codec->mfg)
961 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200962 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200964 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200965 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100966 preset->rev == codec->revision_id)) {
967 mutex_unlock(&preset_mutex);
968 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100972 module_put(tbl->owner);
973 }
974 mutex_unlock(&preset_mutex);
975
976 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100977 if (!mod_requested)
Takashi Iwai66797f32014-10-27 15:15:44 +0100978 request_module("snd-hda-codec-id:%08x",
979 codec->vendor_id);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100980 else
Takashi Iwai66797f32014-10-27 15:15:44 +0100981 request_module("snd-hda-codec-id:%04x*",
982 (codec->vendor_id >> 16) & 0xffff);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100983 mod_requested++;
984 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 return NULL;
987}
988
989/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200990 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200992static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 const struct hda_vendor_id *c;
995 const char *vendor = NULL;
996 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200997 char tmp[16];
998
999 if (codec->vendor_name)
1000 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 for (c = hda_vendor_ids; c->id; c++) {
1003 if (c->id == vendor_id) {
1004 vendor = c->name;
1005 break;
1006 }
1007 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001008 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 sprintf(tmp, "Generic %04x", vendor_id);
1010 vendor = tmp;
1011 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001012 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1013 if (!codec->vendor_name)
1014 return -ENOMEM;
1015
1016 get_chip_name:
1017 if (codec->chip_name)
1018 return 0;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001021 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1022 else {
1023 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1024 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1025 }
1026 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001027 return -ENOMEM;
1028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001032 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001034static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001036 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 hda_nid_t nid;
1038
1039 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1040 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001041 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001042 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001043 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001044 case AC_GRP_AUDIO_FUNCTION:
1045 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001046 codec->afg_function_id = function_id & 0xff;
1047 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001048 break;
1049 case AC_GRP_MODEM_FUNCTION:
1050 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001051 codec->mfg_function_id = function_id & 0xff;
1052 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001053 break;
1054 default:
1055 break;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001061 * read widget caps for each widget and store in cache
1062 */
1063static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1064{
1065 int i;
1066 hda_nid_t nid;
1067
1068 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1069 &codec->start_nid);
1070 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001071 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001072 return -ENOMEM;
1073 nid = codec->start_nid;
1074 for (i = 0; i < codec->num_nodes; i++, nid++)
1075 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1076 AC_PAR_AUDIO_WIDGET_CAP);
1077 return 0;
1078}
1079
Takashi Iwai3be14142009-02-20 14:11:16 +01001080/* read all pin default configurations and save codec->init_pins */
1081static int read_pin_defaults(struct hda_codec *codec)
1082{
1083 int i;
1084 hda_nid_t nid = codec->start_nid;
1085
1086 for (i = 0; i < codec->num_nodes; i++, nid++) {
1087 struct hda_pincfg *pin;
1088 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001089 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 if (wid_type != AC_WID_PIN)
1091 continue;
1092 pin = snd_array_new(&codec->init_pins);
1093 if (!pin)
1094 return -ENOMEM;
1095 pin->nid = nid;
1096 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1097 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001098 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1099 AC_VERB_GET_PIN_WIDGET_CONTROL,
1100 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001101 }
1102 return 0;
1103}
1104
1105/* look up the given pin config list and return the item matching with NID */
1106static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1107 struct snd_array *array,
1108 hda_nid_t nid)
1109{
1110 int i;
1111 for (i = 0; i < array->used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(array, i);
1113 if (pin->nid == nid)
1114 return pin;
1115 }
1116 return NULL;
1117}
1118
Takashi Iwai3be14142009-02-20 14:11:16 +01001119/* set the current pin config value for the given NID.
1120 * the value is cached, and read via snd_hda_codec_get_pincfg()
1121 */
1122int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1123 hda_nid_t nid, unsigned int cfg)
1124{
1125 struct hda_pincfg *pin;
1126
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001127 /* the check below may be invalid when pins are added by a fixup
1128 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1129 * for now
1130 */
1131 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001132 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1133 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001134 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001135
Takashi Iwai3be14142009-02-20 14:11:16 +01001136 pin = look_up_pincfg(codec, list, nid);
1137 if (!pin) {
1138 pin = snd_array_new(list);
1139 if (!pin)
1140 return -ENOMEM;
1141 pin->nid = nid;
1142 }
1143 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001144 return 0;
1145}
1146
Takashi Iwaid5191e52009-11-16 14:58:17 +01001147/**
1148 * snd_hda_codec_set_pincfg - Override a pin default configuration
1149 * @codec: the HDA codec
1150 * @nid: NID to set the pin config
1151 * @cfg: the pin default config value
1152 *
1153 * Override a pin default configuration value in the cache.
1154 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1155 * priority than the real hardware value.
1156 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001157int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1158 hda_nid_t nid, unsigned int cfg)
1159{
Takashi Iwai346ff702009-02-23 09:42:57 +01001160 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001161}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001162EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001163
Takashi Iwaid5191e52009-11-16 14:58:17 +01001164/**
1165 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1166 * @codec: the HDA codec
1167 * @nid: NID to get the pin config
1168 *
1169 * Get the current pin config value of the given pin NID.
1170 * If the pincfg value is cached or overridden via sysfs or driver,
1171 * returns the cached value.
1172 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001173unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1174{
1175 struct hda_pincfg *pin;
1176
Takashi Iwai648a8d22014-02-25 10:38:13 +01001177#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001178 {
1179 unsigned int cfg = 0;
1180 mutex_lock(&codec->user_mutex);
1181 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1182 if (pin)
1183 cfg = pin->cfg;
1184 mutex_unlock(&codec->user_mutex);
1185 if (cfg)
1186 return cfg;
1187 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001188#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001189 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1190 if (pin)
1191 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001192 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1193 if (pin)
1194 return pin->cfg;
1195 return 0;
1196}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001197EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001198
Takashi Iwai95a962c2014-10-29 16:03:58 +01001199/**
1200 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1201 * @codec: the HDA codec
1202 * @nid: pin NID
1203 * @val: assigned pinctl value
1204 *
1205 * This function stores the given value to a pinctl target value in the
1206 * pincfg table. This isn't always as same as the actually written value
1207 * but can be referred at any time via snd_hda_codec_get_pin_target().
1208 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001209int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1210 unsigned int val)
1211{
1212 struct hda_pincfg *pin;
1213
1214 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1215 if (!pin)
1216 return -EINVAL;
1217 pin->target = val;
1218 return 0;
1219}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001220EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001221
Takashi Iwai95a962c2014-10-29 16:03:58 +01001222/**
1223 * snd_hda_codec_get_pin_target - return the current pinctl target value
1224 * @codec: the HDA codec
1225 * @nid: pin NID
1226 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001227int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1228{
1229 struct hda_pincfg *pin;
1230
1231 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1232 if (!pin)
1233 return 0;
1234 return pin->target;
1235}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001236EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001237
Takashi Iwai92ee6162009-12-27 11:18:59 +01001238/**
1239 * snd_hda_shutup_pins - Shut up all pins
1240 * @codec: the HDA codec
1241 *
1242 * Clear all pin controls to shup up before suspend for avoiding click noise.
1243 * The controls aren't cached so that they can be resumed properly.
1244 */
1245void snd_hda_shutup_pins(struct hda_codec *codec)
1246{
1247 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001248 /* don't shut up pins when unloading the driver; otherwise it breaks
1249 * the default pin setup at the next load of the driver
1250 */
1251 if (codec->bus->shutdown)
1252 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001253 for (i = 0; i < codec->init_pins.used; i++) {
1254 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1255 /* use read here for syncing after issuing each verb */
1256 snd_hda_codec_read(codec, pin->nid, 0,
1257 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1258 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001259 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001260}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001261EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001262
Takashi Iwai2a439522011-07-26 09:52:50 +02001263#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001264/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1265static void restore_shutup_pins(struct hda_codec *codec)
1266{
1267 int i;
1268 if (!codec->pins_shutup)
1269 return;
1270 if (codec->bus->shutdown)
1271 return;
1272 for (i = 0; i < codec->init_pins.used; i++) {
1273 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1274 snd_hda_codec_write(codec, pin->nid, 0,
1275 AC_VERB_SET_PIN_WIDGET_CONTROL,
1276 pin->ctrl);
1277 }
1278 codec->pins_shutup = 0;
1279}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001280#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001281
David Henningsson26a6cb62012-10-09 15:04:21 +02001282static void hda_jackpoll_work(struct work_struct *work)
1283{
1284 struct hda_codec *codec =
1285 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001286
1287 snd_hda_jack_set_dirty_all(codec);
1288 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001289
1290 if (!codec->jackpoll_interval)
1291 return;
1292
David Henningsson26a6cb62012-10-09 15:04:21 +02001293 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1294 codec->jackpoll_interval);
1295}
1296
Takashi Iwai01751f52007-08-10 16:59:39 +02001297static void init_hda_cache(struct hda_cache_rec *cache,
1298 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001299static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001300
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001301/* release all pincfg lists */
1302static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001303{
Takashi Iwai346ff702009-02-23 09:42:57 +01001304 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001305#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001306 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001307#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 snd_array_free(&codec->init_pins);
1309}
1310
Takashi Iwai54d17402005-11-21 16:33:22 +01001311/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001312 * audio-converter setup caches
1313 */
1314struct hda_cvt_setup {
1315 hda_nid_t nid;
1316 u8 stream_tag;
1317 u8 channel_id;
1318 u16 format_id;
1319 unsigned char active; /* cvt is currently used */
1320 unsigned char dirty; /* setups should be cleared */
1321};
1322
1323/* get or create a cache entry for the given audio converter NID */
1324static struct hda_cvt_setup *
1325get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1326{
1327 struct hda_cvt_setup *p;
1328 int i;
1329
1330 for (i = 0; i < codec->cvt_setups.used; i++) {
1331 p = snd_array_elem(&codec->cvt_setups, i);
1332 if (p->nid == nid)
1333 return p;
1334 }
1335 p = snd_array_new(&codec->cvt_setups);
1336 if (p)
1337 p->nid = nid;
1338 return p;
1339}
1340
1341/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001342 * Dynamic symbol binding for the codec parsers
1343 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001344
1345#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001346 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001347
1348static void unload_parser(struct hda_codec *codec)
1349{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001350 if (codec->parser)
1351 symbol_put_addr(codec->parser);
1352 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001353}
1354
1355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 * codec destructor
1357 */
1358static void snd_hda_codec_free(struct hda_codec *codec)
1359{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001360 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001362 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001363 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001364 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001365#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001366 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001367 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001370 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001371 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001372 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001373 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001374 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 codec->bus->caddr_tbl[codec->addr] = NULL;
1376 if (codec->patch_ops.free)
1377 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001378 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001379 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001380 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001381 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001382 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001383 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001384 kfree(codec->vendor_name);
1385 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001386 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001387 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001388 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001389 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001392static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1393 hda_nid_t fg, unsigned int power_state);
1394
Takashi Iwaid8193872012-08-31 07:54:38 -07001395static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001396 unsigned int power_state);
1397
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001398static int snd_hda_codec_dev_register(struct snd_device *device)
1399{
1400 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001401 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001402
Takashi Iwaid604b392014-02-28 13:42:09 +01001403 if (err < 0)
1404 return err;
1405 snd_hda_register_beep_device(codec);
1406 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001407}
1408
1409static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1410{
1411 struct hda_codec *codec = device->device_data;
1412
Takashi Iwaid604b392014-02-28 13:42:09 +01001413 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001414 device_del(&codec->dev);
1415 return 0;
1416}
1417
Takashi Iwai2565c892014-02-19 11:41:09 +01001418static int snd_hda_codec_dev_free(struct snd_device *device)
1419{
1420 snd_hda_codec_free(device->device_data);
1421 return 0;
1422}
1423
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001424/* just free the container */
1425static void snd_hda_codec_dev_release(struct device *dev)
1426{
1427 kfree(container_of(dev, struct hda_codec, dev));
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/**
1431 * snd_hda_codec_new - create a HDA codec
1432 * @bus: the bus to assign
1433 * @codec_addr: the codec address
1434 * @codecp: the pointer to store the generated codec
1435 *
1436 * Returns 0 if successful, or a negative error code.
1437 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001438int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001439 unsigned int codec_addr,
1440 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001443 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001444 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001446 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001447 .dev_register = snd_hda_codec_dev_register,
1448 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001449 .dev_free = snd_hda_codec_dev_free,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Takashi Iwaida3cec32008-08-08 17:12:14 +02001452 if (snd_BUG_ON(!bus))
1453 return -EINVAL;
1454 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001458 dev_err(bus->card->dev,
1459 "address 0x%x is already occupied\n",
1460 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EBUSY;
1462 }
1463
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001464 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001466 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -ENOMEM;
1468 }
1469
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001470 device_initialize(&codec->dev);
1471 codec->dev.parent = &bus->card->card_dev;
1472 codec->dev.class = sound_class;
1473 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001474 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001475 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1476 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001477 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 codec->bus = bus;
1480 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001481 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001482 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001483 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001484 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001485 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001486 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1487 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001488 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001489 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001490 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001491 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001492 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001493 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001494 INIT_LIST_HEAD(&codec->conn_list);
1495
David Henningsson26a6cb62012-10-09 15:04:21 +02001496 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001497 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001498 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Takashi Iwai83012a72012-08-24 18:38:08 +02001500#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001501 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001502 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1503 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1504 * the caller has to power down appropriatley after initialization
1505 * phase.
1506 */
1507 hda_keep_power_on(codec);
1508#endif
1509
Takashi Iwai648a8d22014-02-25 10:38:13 +01001510 snd_hda_sysfs_init(codec);
1511
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001512 if (codec->bus->modelname) {
1513 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1514 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001515 err = -ENODEV;
1516 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001517 }
1518 }
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001521 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 bus->caddr_tbl[codec_addr] = codec;
1524
Takashi Iwai0ba21762007-04-16 11:29:14 +02001525 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1526 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001527 if (codec->vendor_id == -1)
1528 /* read again, hopefully the access method was corrected
1529 * in the last read...
1530 */
1531 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1532 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001533 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1534 AC_PAR_SUBSYSTEM_ID);
1535 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1536 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001538 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001539 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001540 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001541 err = -ENODEV;
1542 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
Takashi Iwaid8193872012-08-31 07:54:38 -07001545 fg = codec->afg ? codec->afg : codec->mfg;
1546 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001547 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001548 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001549 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001550 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001551 err = read_pin_defaults(codec);
1552 if (err < 0)
1553 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001554
Takashi Iwai0ba21762007-04-16 11:29:14 +02001555 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001556 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001557 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001558 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001559 }
1560
Takashi Iwai83012a72012-08-24 18:38:08 +02001561#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001562 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001563 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001564#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001565 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001566 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001567#ifdef CONFIG_PM
1568 if (!codec->d3_stop_clk || !codec->epss)
1569 bus->power_keep_link_on = 1;
1570#endif
1571
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001572
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001573 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001574 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001575
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001576 snd_hda_codec_proc_new(codec);
1577
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001578 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001579
1580 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1581 codec->subsystem_id, codec->revision_id);
1582 snd_component_add(codec->bus->card, component);
1583
Takashi Iwai2565c892014-02-19 11:41:09 +01001584 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1585 if (err < 0)
1586 goto error;
1587
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001588 if (codecp)
1589 *codecp = codec;
1590 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001591
1592 error:
1593 snd_hda_codec_free(codec);
1594 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001595}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001596EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001597
Takashi Iwai95a962c2014-10-29 16:03:58 +01001598/**
1599 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1600 * @codec: the HDA codec
1601 *
1602 * Forcibly refresh the all widget caps and the init pin configurations of
1603 * the given codec.
1604 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001605int snd_hda_codec_update_widgets(struct hda_codec *codec)
1606{
1607 hda_nid_t fg;
1608 int err;
1609
1610 /* Assume the function group node does not change,
1611 * only the widget nodes may change.
1612 */
1613 kfree(codec->wcaps);
1614 fg = codec->afg ? codec->afg : codec->mfg;
1615 err = read_widget_caps(codec, fg);
1616 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001617 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001618 return err;
1619 }
1620
1621 snd_array_free(&codec->init_pins);
1622 err = read_pin_defaults(codec);
1623
1624 return err;
1625}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001626EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001627
1628
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001629#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001630/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1631static bool is_likely_hdmi_codec(struct hda_codec *codec)
1632{
1633 hda_nid_t nid = codec->start_nid;
1634 int i;
1635
1636 for (i = 0; i < codec->num_nodes; i++, nid++) {
1637 unsigned int wcaps = get_wcaps(codec, nid);
1638 switch (get_wcaps_type(wcaps)) {
1639 case AC_WID_AUD_IN:
1640 return false; /* HDMI parser supports only HDMI out */
1641 case AC_WID_AUD_OUT:
1642 if (!(wcaps & AC_WCAP_DIGITAL))
1643 return false;
1644 break;
1645 }
1646 }
1647 return true;
1648}
1649#else
1650/* no HDMI codec parser support */
1651#define is_likely_hdmi_codec(codec) false
1652#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1653
Takashi Iwaid5191e52009-11-16 14:58:17 +01001654/**
1655 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1656 * @codec: the HDA codec
1657 *
1658 * Start parsing of the given codec tree and (re-)initialize the whole
1659 * patch instance.
1660 *
1661 * Returns 0 if successful or a negative error code.
1662 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001663int snd_hda_codec_configure(struct hda_codec *codec)
1664{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001665 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001666 int err;
1667
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001668 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001669 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001670 err = get_codec_name(codec);
1671 if (err < 0)
1672 return err;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001675 if (!is_generic_config(codec) && codec->preset)
1676 patch = codec->preset->patch;
1677 if (!patch) {
1678 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001679 if (is_likely_hdmi_codec(codec)) {
1680#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001681 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001682#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1683 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001684#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001685 }
1686 if (!patch) {
1687#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1688 patch = load_parser(codec, snd_hda_parse_generic_codec);
1689#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1690 patch = snd_hda_parse_generic_codec;
1691#endif
1692 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001693 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001694 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001695 return -ENODEV;
1696 }
Takashi Iwai82467612007-07-27 19:15:54 +02001697 }
1698
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001699 err = patch(codec);
1700 if (err < 0) {
1701 unload_parser(codec);
1702 return err;
1703 }
Takashi Iwai82467612007-07-27 19:15:54 +02001704
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001705 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001706 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001707 if (err < 0)
1708 return err;
1709 }
1710
Takashi Iwaif62faed2009-12-23 09:27:51 +01001711 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001712 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001713 snprintf(codec->bus->card->mixername,
1714 sizeof(codec->bus->card->mixername),
1715 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001718EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Takashi Iwaied360812012-08-08 17:12:52 +02001720/* update the stream-id if changed */
1721static void update_pcm_stream_id(struct hda_codec *codec,
1722 struct hda_cvt_setup *p, hda_nid_t nid,
1723 u32 stream_tag, int channel_id)
1724{
1725 unsigned int oldval, newval;
1726
1727 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1728 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1729 newval = (stream_tag << 4) | channel_id;
1730 if (oldval != newval)
1731 snd_hda_codec_write(codec, nid, 0,
1732 AC_VERB_SET_CHANNEL_STREAMID,
1733 newval);
1734 p->stream_tag = stream_tag;
1735 p->channel_id = channel_id;
1736 }
1737}
1738
1739/* update the format-id if changed */
1740static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1741 hda_nid_t nid, int format)
1742{
1743 unsigned int oldval;
1744
1745 if (p->format_id != format) {
1746 oldval = snd_hda_codec_read(codec, nid, 0,
1747 AC_VERB_GET_STREAM_FORMAT, 0);
1748 if (oldval != format) {
1749 msleep(1);
1750 snd_hda_codec_write(codec, nid, 0,
1751 AC_VERB_SET_STREAM_FORMAT,
1752 format);
1753 }
1754 p->format_id = format;
1755 }
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/**
1759 * snd_hda_codec_setup_stream - set up the codec for streaming
1760 * @codec: the CODEC to set up
1761 * @nid: the NID to set up
1762 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1763 * @channel_id: channel id to pass, zero based.
1764 * @format: stream format.
1765 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001766void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1767 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int channel_id, int format)
1769{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001770 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001771 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001772 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001773 int i;
1774
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001776 return;
1777
Takashi Iwai4e76a882014-02-25 12:21:03 +01001778 codec_dbg(codec,
1779 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1780 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001781 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001782 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001783 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001784
1785 if (codec->pcm_format_first)
1786 update_pcm_format(codec, p, nid, format);
1787 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1788 if (!codec->pcm_format_first)
1789 update_pcm_format(codec, p, nid, format);
1790
Takashi Iwaieb541332010-08-06 13:48:11 +02001791 p->active = 1;
1792 p->dirty = 0;
1793
1794 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001795 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001796 list_for_each_entry(c, &codec->bus->codec_list, list) {
1797 for (i = 0; i < c->cvt_setups.used; i++) {
1798 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001799 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001800 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001801 p->dirty = 1;
1802 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001805EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Takashi Iwaif0cea792010-08-13 11:56:53 +02001807static void really_cleanup_stream(struct hda_codec *codec,
1808 struct hda_cvt_setup *q);
1809
Takashi Iwaid5191e52009-11-16 14:58:17 +01001810/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001811 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001812 * @codec: the CODEC to clean up
1813 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001814 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001815 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001816void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1817 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001818{
Takashi Iwaieb541332010-08-06 13:48:11 +02001819 struct hda_cvt_setup *p;
1820
Takashi Iwai888afa12008-03-18 09:57:50 +01001821 if (!nid)
1822 return;
1823
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001824 if (codec->no_sticky_stream)
1825 do_now = 1;
1826
Takashi Iwai4e76a882014-02-25 12:21:03 +01001827 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001828 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001829 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001830 /* here we just clear the active flag when do_now isn't set;
1831 * actual clean-ups will be done later in
1832 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1833 */
1834 if (do_now)
1835 really_cleanup_stream(codec, p);
1836 else
1837 p->active = 0;
1838 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001839}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001840EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001841
Takashi Iwaieb541332010-08-06 13:48:11 +02001842static void really_cleanup_stream(struct hda_codec *codec,
1843 struct hda_cvt_setup *q)
1844{
1845 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001846 if (q->stream_tag || q->channel_id)
1847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1848 if (q->format_id)
1849 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1850);
Takashi Iwaieb541332010-08-06 13:48:11 +02001851 memset(q, 0, sizeof(*q));
1852 q->nid = nid;
1853}
1854
1855/* clean up the all conflicting obsolete streams */
1856static void purify_inactive_streams(struct hda_codec *codec)
1857{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001858 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001859 int i;
1860
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001861 list_for_each_entry(c, &codec->bus->codec_list, list) {
1862 for (i = 0; i < c->cvt_setups.used; i++) {
1863 struct hda_cvt_setup *p;
1864 p = snd_array_elem(&c->cvt_setups, i);
1865 if (p->dirty)
1866 really_cleanup_stream(c, p);
1867 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001868 }
1869}
1870
Takashi Iwai2a439522011-07-26 09:52:50 +02001871#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001872/* clean up all streams; called from suspend */
1873static void hda_cleanup_all_streams(struct hda_codec *codec)
1874{
1875 int i;
1876
1877 for (i = 0; i < codec->cvt_setups.used; i++) {
1878 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1879 if (p->stream_tag)
1880 really_cleanup_stream(codec, p);
1881 }
1882}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001883#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/*
1886 * amp access functions
1887 */
1888
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001889/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001890#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001891#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001892#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1893#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001895#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001898static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001899 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwai01751f52007-08-10 16:59:39 +02001901 memset(cache, 0, sizeof(*cache));
1902 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001903 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001904}
1905
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001906static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001907{
Takashi Iwai603c4012008-07-30 15:01:44 +02001908 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001912static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Takashi Iwai01751f52007-08-10 16:59:39 +02001914 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1915 u16 cur = cache->hash[idx];
1916 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001919 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (info->key == key)
1921 return info;
1922 cur = info->next;
1923 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001924 return NULL;
1925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001927/* query the hash. allocate an entry if not found. */
1928static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1929 u32 key)
1930{
1931 struct hda_cache_head *info = get_hash(cache, key);
1932 if (!info) {
1933 u16 idx, cur;
1934 /* add a new hash entry */
1935 info = snd_array_new(&cache->buf);
1936 if (!info)
1937 return NULL;
1938 cur = snd_array_index(&cache->buf, info);
1939 info->key = key;
1940 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001941 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001942 idx = key % (u16)ARRAY_SIZE(cache->hash);
1943 info->next = cache->hash[idx];
1944 cache->hash[idx] = cur;
1945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return info;
1947}
1948
Takashi Iwai01751f52007-08-10 16:59:39 +02001949/* query and allocate an amp hash entry */
1950static inline struct hda_amp_info *
1951get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1952{
1953 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1954}
1955
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001956/* overwrite the value with the key in the caps hash */
1957static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1958{
1959 struct hda_amp_info *info;
1960
1961 mutex_lock(&codec->hash_mutex);
1962 info = get_alloc_amp_hash(codec, key);
1963 if (!info) {
1964 mutex_unlock(&codec->hash_mutex);
1965 return -EINVAL;
1966 }
1967 info->amp_caps = val;
1968 info->head.val |= INFO_AMP_CAPS;
1969 mutex_unlock(&codec->hash_mutex);
1970 return 0;
1971}
1972
1973/* query the value from the caps hash; if not found, fetch the current
1974 * value from the given function and store in the hash
1975 */
1976static unsigned int
1977query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1978 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1979{
1980 struct hda_amp_info *info;
1981 unsigned int val;
1982
1983 mutex_lock(&codec->hash_mutex);
1984 info = get_alloc_amp_hash(codec, key);
1985 if (!info) {
1986 mutex_unlock(&codec->hash_mutex);
1987 return 0;
1988 }
1989 if (!(info->head.val & INFO_AMP_CAPS)) {
1990 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1991 val = func(codec, nid, dir);
1992 write_caps_hash(codec, key, val);
1993 } else {
1994 val = info->amp_caps;
1995 mutex_unlock(&codec->hash_mutex);
1996 }
1997 return val;
1998}
1999
2000static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
2001 int direction)
2002{
2003 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
2004 nid = codec->afg;
2005 return snd_hda_param_read(codec, nid,
2006 direction == HDA_OUTPUT ?
2007 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
2008}
2009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * query_amp_caps - query AMP capabilities
2012 * @codec: the HD-auio codec
2013 * @nid: the NID to query
2014 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2015 *
2016 * Query AMP capabilities for the given widget and direction.
2017 * Returns the obtained capability bits.
2018 *
2019 * When cap bits have been already read, this doesn't read again but
2020 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01002022u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002024 return query_caps_hash(codec, nid, direction,
2025 HDA_HASH_KEY(nid, direction, 0),
2026 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002028EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Takashi Iwaid5191e52009-11-16 14:58:17 +01002030/**
David Henningsson861a04e2014-09-23 10:38:17 +02002031 * snd_hda_check_amp_caps - query AMP capabilities
2032 * @codec: the HD-audio codec
2033 * @nid: the NID to query
2034 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002035 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02002036 *
2037 * Check whether the widget has the given amp capability for the direction.
2038 */
2039bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2040 int dir, unsigned int bits)
2041{
2042 if (!nid)
2043 return false;
2044 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2045 if (query_amp_caps(codec, nid, dir) & bits)
2046 return true;
2047 return false;
2048}
2049EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2050
2051/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002052 * snd_hda_override_amp_caps - Override the AMP capabilities
2053 * @codec: the CODEC to clean up
2054 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002055 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01002056 * @caps: the capability bits to set
2057 *
2058 * Override the cached AMP caps bits value by the given one.
2059 * This function is useful if the driver needs to adjust the AMP ranges,
2060 * e.g. limit to 0dB, etc.
2061 *
2062 * Returns zero if successful or a negative error code.
2063 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002064int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2065 unsigned int caps)
2066{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002067 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002068}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002069EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002070
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002071static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2072 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002073{
2074 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2075}
2076
Takashi Iwaid5191e52009-11-16 14:58:17 +01002077/**
2078 * snd_hda_query_pin_caps - Query PIN capabilities
2079 * @codec: the HD-auio codec
2080 * @nid: the NID to query
2081 *
2082 * Query PIN capabilities for the given widget.
2083 * Returns the obtained capability bits.
2084 *
2085 * When cap bits have been already read, this doesn't read again but
2086 * returns the cached value.
2087 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002088u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2089{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002090 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002091 read_pin_cap);
2092}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002093EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002094
Wu Fengguang864f92b2009-11-18 12:38:02 +08002095/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002096 * snd_hda_override_pin_caps - Override the pin capabilities
2097 * @codec: the CODEC
2098 * @nid: the NID to override
2099 * @caps: the capability bits to set
2100 *
2101 * Override the cached PIN capabilitiy bits value by the given one.
2102 *
2103 * Returns zero if successful or a negative error code.
2104 */
2105int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2106 unsigned int caps)
2107{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002108 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002109}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002110EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002111
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002112/* read or sync the hash value with the current value;
2113 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002115static struct hda_amp_info *
2116update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002117 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002119 struct hda_amp_info *info;
2120 unsigned int parm, val = 0;
2121 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002123 retry:
2124 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2125 if (!info)
2126 return NULL;
2127 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2128 if (!val_read) {
2129 mutex_unlock(&codec->hash_mutex);
2130 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2131 parm |= direction == HDA_OUTPUT ?
2132 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2133 parm |= index;
2134 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002135 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002136 val &= 0xff;
2137 val_read = true;
2138 mutex_lock(&codec->hash_mutex);
2139 goto retry;
2140 }
2141 info->vol[ch] = val;
2142 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002143 } else if (init_only)
2144 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002145 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002149 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002151static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002152 hda_nid_t nid, int ch, int direction, int index,
2153 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 u32 parm;
2156
2157 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2158 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2159 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002160 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2161 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002162 ; /* set the zero value as a fake mute */
2163 else
2164 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2166}
2167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
2169 * snd_hda_codec_amp_read - Read AMP value
2170 * @codec: HD-audio codec
2171 * @nid: NID to read the AMP value
2172 * @ch: channel (left=0 or right=1)
2173 * @direction: #HDA_INPUT or #HDA_OUTPUT
2174 * @index: the index value (only for input direction)
2175 *
2176 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 */
Takashi Iwai834be882006-03-01 14:16:17 +01002178int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2179 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002181 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002182 unsigned int val = 0;
2183
2184 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002185 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002186 if (info)
2187 val = info->vol[ch];
2188 mutex_unlock(&codec->hash_mutex);
2189 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002191EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Takashi Iwai280e57d2012-12-14 10:32:21 +01002193static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2194 int direction, int idx, int mask, int val,
2195 bool init_only)
2196{
2197 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002198 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002199 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002200
2201 if (snd_BUG_ON(mask & ~0xff))
2202 mask &= 0xff;
2203 val &= mask;
2204
2205 mutex_lock(&codec->hash_mutex);
2206 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2207 if (!info) {
2208 mutex_unlock(&codec->hash_mutex);
2209 return 0;
2210 }
2211 val |= info->vol[ch] & ~mask;
2212 if (info->vol[ch] == val) {
2213 mutex_unlock(&codec->hash_mutex);
2214 return 0;
2215 }
2216 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002217 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002218 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002219 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002220 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002221 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002222 return 1;
2223}
2224
Takashi Iwaid5191e52009-11-16 14:58:17 +01002225/**
2226 * snd_hda_codec_amp_update - update the AMP value
2227 * @codec: HD-audio codec
2228 * @nid: NID to read the AMP value
2229 * @ch: channel (left=0 or right=1)
2230 * @direction: #HDA_INPUT or #HDA_OUTPUT
2231 * @idx: the index value (only for input direction)
2232 * @mask: bit mask to set
2233 * @val: the bits value to set
2234 *
2235 * Update the AMP value with a bit mask.
2236 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002237 */
Takashi Iwai834be882006-03-01 14:16:17 +01002238int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2239 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002241 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002243EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Takashi Iwaid5191e52009-11-16 14:58:17 +01002245/**
2246 * snd_hda_codec_amp_stereo - update the AMP stereo values
2247 * @codec: HD-audio codec
2248 * @nid: NID to read the AMP value
2249 * @direction: #HDA_INPUT or #HDA_OUTPUT
2250 * @idx: the index value (only for input direction)
2251 * @mask: bit mask to set
2252 * @val: the bits value to set
2253 *
2254 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2255 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002256 */
2257int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2258 int direction, int idx, int mask, int val)
2259{
2260 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002261
2262 if (snd_BUG_ON(mask & ~0xff))
2263 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002264 for (ch = 0; ch < 2; ch++)
2265 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2266 idx, mask, val);
2267 return ret;
2268}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002269EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002270
Takashi Iwai95a962c2014-10-29 16:03:58 +01002271/**
2272 * snd_hda_codec_amp_init - initialize the AMP value
2273 * @codec: the HDA codec
2274 * @nid: NID to read the AMP value
2275 * @ch: channel (left=0 or right=1)
2276 * @dir: #HDA_INPUT or #HDA_OUTPUT
2277 * @idx: the index value (only for input direction)
2278 * @mask: bit mask to set
2279 * @val: the bits value to set
2280 *
2281 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002282 * the first access. If the amp was already initialized / updated beforehand,
2283 * this does nothing.
2284 */
2285int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2286 int dir, int idx, int mask, int val)
2287{
2288 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2289}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002290EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002291
Takashi Iwai95a962c2014-10-29 16:03:58 +01002292/**
2293 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2294 * @codec: the HDA codec
2295 * @nid: NID to read the AMP value
2296 * @dir: #HDA_INPUT or #HDA_OUTPUT
2297 * @idx: the index value (only for input direction)
2298 * @mask: bit mask to set
2299 * @val: the bits value to set
2300 *
2301 * Call snd_hda_codec_amp_init() for both stereo channels.
2302 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002303int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2304 int dir, int idx, int mask, int val)
2305{
2306 int ch, ret = 0;
2307
2308 if (snd_BUG_ON(mask & ~0xff))
2309 mask &= 0xff;
2310 for (ch = 0; ch < 2; ch++)
2311 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2312 idx, mask, val);
2313 return ret;
2314}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002315EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002316
Takashi Iwaid5191e52009-11-16 14:58:17 +01002317/**
2318 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2319 * @codec: HD-audio codec
2320 *
2321 * Resume the all amp commands from the cache.
2322 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002323void snd_hda_codec_resume_amp(struct hda_codec *codec)
2324{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002325 int i;
2326
Takashi Iwaic370dd62012-12-13 18:30:04 +01002327 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002328 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002329 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2330 struct hda_amp_info *buffer;
2331 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002332 hda_nid_t nid;
2333 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002334 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002335
2336 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002337 if (!buffer->head.dirty)
2338 continue;
2339 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002340 info = *buffer;
2341 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002342 if (!key)
2343 continue;
2344 nid = key & 0xff;
2345 idx = (key >> 16) & 0xff;
2346 dir = (key >> 24) & 0xff;
2347 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002348 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002349 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002350 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002351 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2352 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002353 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002354 }
2355 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002356 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002357}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002358EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002360static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2361 unsigned int ofs)
2362{
2363 u32 caps = query_amp_caps(codec, nid, dir);
2364 /* get num steps */
2365 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2366 if (ofs < caps)
2367 caps -= ofs;
2368 return caps;
2369}
2370
Takashi Iwaid5191e52009-11-16 14:58:17 +01002371/**
2372 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002373 * @kcontrol: referred ctl element
2374 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002375 *
2376 * The control element is supposed to have the private_value field
2377 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2378 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002379int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2383 u16 nid = get_amp_nid(kcontrol);
2384 u8 chs = get_amp_channels(kcontrol);
2385 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002386 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002388 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2389 uinfo->count = chs == 3 ? 2 : 1;
2390 uinfo->value.integer.min = 0;
2391 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2392 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002393 codec_warn(codec,
2394 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2395 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return -EINVAL;
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return 0;
2399}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002400EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002402
2403static inline unsigned int
2404read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2405 int ch, int dir, int idx, unsigned int ofs)
2406{
2407 unsigned int val;
2408 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2409 val &= HDA_AMP_VOLMASK;
2410 if (val >= ofs)
2411 val -= ofs;
2412 else
2413 val = 0;
2414 return val;
2415}
2416
2417static inline int
2418update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2419 int ch, int dir, int idx, unsigned int ofs,
2420 unsigned int val)
2421{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002422 unsigned int maxval;
2423
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002424 if (val > 0)
2425 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002426 /* ofs = 0: raw max value */
2427 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002428 if (val > maxval)
2429 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002430 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2431 HDA_AMP_VOLMASK, val);
2432}
2433
Takashi Iwaid5191e52009-11-16 14:58:17 +01002434/**
2435 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002436 * @kcontrol: ctl element
2437 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002438 *
2439 * The control element is supposed to have the private_value field
2440 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2441 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002442int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446 hda_nid_t nid = get_amp_nid(kcontrol);
2447 int chs = get_amp_channels(kcontrol);
2448 int dir = get_amp_direction(kcontrol);
2449 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002450 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 long *valp = ucontrol->value.integer.value;
2452
2453 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002454 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002456 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 0;
2458}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002459EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Takashi Iwaid5191e52009-11-16 14:58:17 +01002461/**
2462 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002463 * @kcontrol: ctl element
2464 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002465 *
2466 * The control element is supposed to have the private_value field
2467 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2468 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002469int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 hda_nid_t nid = get_amp_nid(kcontrol);
2474 int chs = get_amp_channels(kcontrol);
2475 int dir = get_amp_direction(kcontrol);
2476 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002477 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 long *valp = ucontrol->value.integer.value;
2479 int change = 0;
2480
Takashi Iwaicb53c622007-08-10 17:21:45 +02002481 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002482 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002483 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002484 valp++;
2485 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002486 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002487 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002488 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return change;
2490}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002491EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Takashi Iwaid5191e52009-11-16 14:58:17 +01002493/**
2494 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002495 * @kcontrol: ctl element
2496 * @op_flag: operation flag
2497 * @size: byte size of input TLV
2498 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002499 *
2500 * The control element is supposed to have the private_value field
2501 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2502 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002503int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2504 unsigned int size, unsigned int __user *_tlv)
2505{
2506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2507 hda_nid_t nid = get_amp_nid(kcontrol);
2508 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002509 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002510 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002511 u32 caps, val1, val2;
2512
2513 if (size < 4 * sizeof(unsigned int))
2514 return -ENOMEM;
2515 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002516 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2517 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002518 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002519 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002520 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002521 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002522 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002523 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2524 return -EFAULT;
2525 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2526 return -EFAULT;
2527 if (put_user(val1, _tlv + 2))
2528 return -EFAULT;
2529 if (put_user(val2, _tlv + 3))
2530 return -EFAULT;
2531 return 0;
2532}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002533EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002534
Takashi Iwaid5191e52009-11-16 14:58:17 +01002535/**
2536 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2537 * @codec: HD-audio codec
2538 * @nid: NID of a reference widget
2539 * @dir: #HDA_INPUT or #HDA_OUTPUT
2540 * @tlv: TLV data to be stored, at least 4 elements
2541 *
2542 * Set (static) TLV data for a virtual master volume using the AMP caps
2543 * obtained from the reference NID.
2544 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002545 */
2546void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2547 unsigned int *tlv)
2548{
2549 u32 caps;
2550 int nums, step;
2551
2552 caps = query_amp_caps(codec, nid, dir);
2553 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2554 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2555 step = (step + 1) * 25;
2556 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2557 tlv[1] = 2 * sizeof(unsigned int);
2558 tlv[2] = -nums * step;
2559 tlv[3] = step;
2560}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002561EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002562
2563/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002564static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002565find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002566{
2567 struct snd_ctl_elem_id id;
2568 memset(&id, 0, sizeof(id));
2569 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002570 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002571 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002572 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2573 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002574 strcpy(id.name, name);
2575 return snd_ctl_find_id(codec->bus->card, &id);
2576}
2577
Takashi Iwaid5191e52009-11-16 14:58:17 +01002578/**
2579 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2580 * @codec: HD-audio codec
2581 * @name: ctl id name string
2582 *
2583 * Get the control element with the given id string and IFACE_MIXER.
2584 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002585struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2586 const char *name)
2587{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002588 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002589}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002590EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002591
Takashi Iwaidcda5802012-10-12 17:24:51 +02002592static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002593 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002594{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002595 int i, idx;
2596 /* 16 ctlrs should be large enough */
2597 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2598 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002599 return idx;
2600 }
2601 return -EBUSY;
2602}
2603
Takashi Iwaid5191e52009-11-16 14:58:17 +01002604/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002605 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002606 * @codec: HD-audio codec
2607 * @nid: corresponding NID (optional)
2608 * @kctl: the control element to assign
2609 *
2610 * Add the given control element to an array inside the codec instance.
2611 * All control elements belonging to a codec are supposed to be added
2612 * by this function so that a proper clean-up works at the free or
2613 * reconfiguration time.
2614 *
2615 * If non-zero @nid is passed, the NID is assigned to the control element.
2616 * The assignment is shown in the codec proc file.
2617 *
2618 * snd_hda_ctl_add() checks the control subdev id field whether
2619 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002620 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2621 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002622 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002623int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2624 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002625{
2626 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002627 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002628 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002629
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002630 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002631 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002632 if (nid == 0)
2633 nid = get_amp_nid_(kctl->private_value);
2634 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002635 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2636 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002637 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002638 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002639 err = snd_ctl_add(codec->bus->card, kctl);
2640 if (err < 0)
2641 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002642 item = snd_array_new(&codec->mixers);
2643 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002644 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002645 item->kctl = kctl;
2646 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002647 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002648 return 0;
2649}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002650EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002651
Takashi Iwaid5191e52009-11-16 14:58:17 +01002652/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002653 * snd_hda_add_nid - Assign a NID to a control element
2654 * @codec: HD-audio codec
2655 * @nid: corresponding NID (optional)
2656 * @kctl: the control element to assign
2657 * @index: index to kctl
2658 *
2659 * Add the given control element to an array inside the codec instance.
2660 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2661 * NID:KCTL mapping - for example "Capture Source" selector.
2662 */
2663int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2664 unsigned int index, hda_nid_t nid)
2665{
2666 struct hda_nid_item *item;
2667
2668 if (nid > 0) {
2669 item = snd_array_new(&codec->nids);
2670 if (!item)
2671 return -ENOMEM;
2672 item->kctl = kctl;
2673 item->index = index;
2674 item->nid = nid;
2675 return 0;
2676 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002677 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2678 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002679 return -EINVAL;
2680}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002681EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002682
2683/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002684 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2685 * @codec: HD-audio codec
2686 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002687void snd_hda_ctls_clear(struct hda_codec *codec)
2688{
2689 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002690 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002691 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002692 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002693 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002694 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002695}
2696
Takashi Iwai95a962c2014-10-29 16:03:58 +01002697/**
2698 * snd_hda_lock_devices - pseudo device locking
2699 * @bus: the BUS
2700 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002701 * toggle card->shutdown to allow/disallow the device access (as a hack)
2702 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002703int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002704{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002705 struct snd_card *card = bus->card;
2706 struct hda_codec *codec;
2707
Takashi Iwaia65d6292009-02-23 16:57:04 +01002708 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002709 if (card->shutdown)
2710 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002711 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002712 if (!list_empty(&card->ctl_files))
2713 goto err_clear;
2714
2715 list_for_each_entry(codec, &bus->codec_list, list) {
2716 int pcm;
2717 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2718 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2719 if (!cpcm->pcm)
2720 continue;
2721 if (cpcm->pcm->streams[0].substream_opened ||
2722 cpcm->pcm->streams[1].substream_opened)
2723 goto err_clear;
2724 }
2725 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002726 spin_unlock(&card->files_lock);
2727 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002728
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002729 err_clear:
2730 card->shutdown = 0;
2731 err_unlock:
2732 spin_unlock(&card->files_lock);
2733 return -EINVAL;
2734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002735EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002736
Takashi Iwai95a962c2014-10-29 16:03:58 +01002737/**
2738 * snd_hda_unlock_devices - pseudo device unlocking
2739 * @bus: the BUS
2740 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002741void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002742{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002743 struct snd_card *card = bus->card;
2744
2745 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002746 spin_lock(&card->files_lock);
2747 card->shutdown = 0;
2748 spin_unlock(&card->files_lock);
2749}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002750EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002751
Takashi Iwaid5191e52009-11-16 14:58:17 +01002752/**
2753 * snd_hda_codec_reset - Clear all objects assigned to the codec
2754 * @codec: HD-audio codec
2755 *
2756 * This frees the all PCM and control elements assigned to the codec, and
2757 * clears the caches and restores the pin default configurations.
2758 *
2759 * When a device is being used, it returns -EBSY. If successfully freed,
2760 * returns zero.
2761 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002762int snd_hda_codec_reset(struct hda_codec *codec)
2763{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002764 struct hda_bus *bus = codec->bus;
2765 struct snd_card *card = bus->card;
2766 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002767
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002768 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002769 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002770
2771 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002772 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002773#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002774 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002775 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002776#endif
2777 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002778 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002779 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002780 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002781 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002782 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002783 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002784 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002785 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002786 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002787 if (codec->patch_ops.free)
2788 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002789 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002790 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002791 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002792 codec->spec = NULL;
2793 free_hda_cache(&codec->amp_cache);
2794 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002795 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2796 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002797 /* free only driver_pins so that init_pins + user_pins are restored */
2798 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002799 snd_array_free(&codec->cvt_setups);
2800 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002801 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002802 codec->num_pcms = 0;
2803 codec->pcm_info = NULL;
2804 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002805 codec->slave_dig_outs = NULL;
2806 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002807 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002808 module_put(codec->owner);
2809 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002810
2811 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002812 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002813 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002814}
2815
Takashi Iwai6194b992014-06-06 18:12:16 +02002816typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002817
2818/* apply the function to all matching slave ctls in the mixer list */
2819static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002820 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002821{
2822 struct hda_nid_item *items;
2823 const char * const *s;
2824 int i, err;
2825
2826 items = codec->mixers.list;
2827 for (i = 0; i < codec->mixers.used; i++) {
2828 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002829 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002830 continue;
2831 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002832 char tmpname[sizeof(sctl->id.name)];
2833 const char *name = *s;
2834 if (suffix) {
2835 snprintf(tmpname, sizeof(tmpname), "%s %s",
2836 name, suffix);
2837 name = tmpname;
2838 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002839 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002840 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002841 if (err)
2842 return err;
2843 break;
2844 }
2845 }
2846 }
2847 return 0;
2848}
2849
Takashi Iwai6194b992014-06-06 18:12:16 +02002850static int check_slave_present(struct hda_codec *codec,
2851 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002852{
2853 return 1;
2854}
2855
Takashi Iwai18478e82012-03-09 17:51:10 +01002856/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002857static int get_kctl_0dB_offset(struct hda_codec *codec,
2858 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002859{
2860 int _tlv[4];
2861 const int *tlv = NULL;
2862 int val = -1;
2863
2864 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2865 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2866 mm_segment_t fs = get_fs();
2867 set_fs(get_ds());
2868 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2869 tlv = _tlv;
2870 set_fs(fs);
2871 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2872 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002873 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2874 int step = tlv[3];
2875 step &= ~TLV_DB_SCALE_MUTE;
2876 if (!step)
2877 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002878 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002879 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002880- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002881 return -1;
2882 }
2883 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002884 val = -tlv[2] / step;
2885 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002886 return val;
2887}
2888
2889/* call kctl->put with the given value(s) */
2890static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2891{
2892 struct snd_ctl_elem_value *ucontrol;
2893 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2894 if (!ucontrol)
2895 return -ENOMEM;
2896 ucontrol->value.integer.value[0] = val;
2897 ucontrol->value.integer.value[1] = val;
2898 kctl->put(kctl, ucontrol);
2899 kfree(ucontrol);
2900 return 0;
2901}
2902
2903/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002904static int init_slave_0dB(struct hda_codec *codec,
2905 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002906{
Takashi Iwai6194b992014-06-06 18:12:16 +02002907 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002908 if (offset > 0)
2909 put_kctl_with_value(slave, offset);
2910 return 0;
2911}
2912
2913/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002914static int init_slave_unmute(struct hda_codec *codec,
2915 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002916{
2917 return put_kctl_with_value(slave, 1);
2918}
2919
Takashi Iwaie8750942014-06-30 14:02:39 +02002920static int add_slave(struct hda_codec *codec,
2921 void *data, struct snd_kcontrol *slave)
2922{
2923 return snd_ctl_add_slave(data, slave);
2924}
2925
Takashi Iwaid5191e52009-11-16 14:58:17 +01002926/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002927 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002928 * @codec: HD-audio codec
2929 * @name: vmaster control name
2930 * @tlv: TLV data (optional)
2931 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002932 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002933 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002934 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002935 *
2936 * Create a virtual master control with the given name. The TLV data
2937 * must be either NULL or a valid data.
2938 *
2939 * @slaves is a NULL-terminated array of strings, each of which is a
2940 * slave control name. All controls with these names are assigned to
2941 * the new virtual master control.
2942 *
2943 * This function returns zero if successful or a negative error code.
2944 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002945int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002946 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002947 const char *suffix, bool init_slave_vol,
2948 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002949{
2950 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002951 int err;
2952
Takashi Iwai29e58532012-03-12 12:25:03 +01002953 if (ctl_ret)
2954 *ctl_ret = NULL;
2955
Takashi Iwai9322ca52012-02-03 14:28:01 +01002956 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002957 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002958 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002959 return 0;
2960 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002961 kctl = snd_ctl_make_virtual_master(name, tlv);
2962 if (!kctl)
2963 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002964 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002965 if (err < 0)
2966 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002967
Takashi Iwaie8750942014-06-30 14:02:39 +02002968 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002969 if (err < 0)
2970 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002971
2972 /* init with master mute & zero volume */
2973 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002974 if (init_slave_vol) {
2975 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002976 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002977 tlv ? init_slave_0dB : init_slave_unmute, &step);
2978 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002979
Takashi Iwai29e58532012-03-12 12:25:03 +01002980 if (ctl_ret)
2981 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002982 return 0;
2983}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002984EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002985
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002986/*
2987 * mute-LED control using vmaster
2988 */
2989static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2990 struct snd_ctl_elem_info *uinfo)
2991{
2992 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002993 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002994 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002995
Takashi Iwai3ff72212014-10-20 18:17:28 +02002996 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002997}
2998
2999static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_value *ucontrol)
3001{
3002 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3003 ucontrol->value.enumerated.item[0] = hook->mute_mode;
3004 return 0;
3005}
3006
3007static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_value *ucontrol)
3009{
3010 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3011 unsigned int old_mode = hook->mute_mode;
3012
3013 hook->mute_mode = ucontrol->value.enumerated.item[0];
3014 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
3015 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3016 if (old_mode == hook->mute_mode)
3017 return 0;
3018 snd_hda_sync_vmaster_hook(hook);
3019 return 1;
3020}
3021
3022static struct snd_kcontrol_new vmaster_mute_mode = {
3023 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3024 .name = "Mute-LED Mode",
3025 .info = vmaster_mute_mode_info,
3026 .get = vmaster_mute_mode_get,
3027 .put = vmaster_mute_mode_put,
3028};
3029
Takashi Iwai95a962c2014-10-29 16:03:58 +01003030/**
3031 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3032 * @codec: the HDA codec
3033 * @hook: the vmaster hook object
3034 * @expose_enum_ctl: flag to create an enum ctl
3035 *
3036 * Add a mute-LED hook with the given vmaster switch kctl.
3037 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3038 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003039 */
3040int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01003041 struct hda_vmaster_mute_hook *hook,
3042 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003043{
3044 struct snd_kcontrol *kctl;
3045
3046 if (!hook->hook || !hook->sw_kctl)
3047 return 0;
3048 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3049 hook->codec = codec;
3050 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01003051 if (!expose_enum_ctl)
3052 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003053 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3054 if (!kctl)
3055 return -ENOMEM;
3056 return snd_hda_ctl_add(codec, 0, kctl);
3057}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003058EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003059
Takashi Iwai95a962c2014-10-29 16:03:58 +01003060/**
3061 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3062 * @hook: the vmaster hook
3063 *
3064 * Call the hook with the current value for synchronization.
3065 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003066 */
3067void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3068{
3069 if (!hook->hook || !hook->codec)
3070 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003071 /* don't call vmaster hook in the destructor since it might have
3072 * been already destroyed
3073 */
3074 if (hook->codec->bus->shutdown)
3075 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003076 switch (hook->mute_mode) {
3077 case HDA_VMUTE_FOLLOW_MASTER:
3078 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3079 break;
3080 default:
3081 hook->hook(hook->codec, hook->mute_mode);
3082 break;
3083 }
3084}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003085EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003086
3087
Takashi Iwaid5191e52009-11-16 14:58:17 +01003088/**
3089 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003090 * @kcontrol: referred ctl element
3091 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003092 *
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3095 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003096int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 int chs = get_amp_channels(kcontrol);
3100
3101 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3102 uinfo->count = chs == 3 ? 2 : 1;
3103 uinfo->value.integer.min = 0;
3104 uinfo->value.integer.max = 1;
3105 return 0;
3106}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003107EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Takashi Iwaid5191e52009-11-16 14:58:17 +01003109/**
3110 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003111 * @kcontrol: ctl element
3112 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003113 *
3114 * The control element is supposed to have the private_value field
3115 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3116 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3121 hda_nid_t nid = get_amp_nid(kcontrol);
3122 int chs = get_amp_channels(kcontrol);
3123 int dir = get_amp_direction(kcontrol);
3124 int idx = get_amp_index(kcontrol);
3125 long *valp = ucontrol->value.integer.value;
3126
3127 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003129 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003131 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003132 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return 0;
3134}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003135EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Takashi Iwaid5191e52009-11-16 14:58:17 +01003137/**
3138 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003139 * @kcontrol: ctl element
3140 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003141 *
3142 * The control element is supposed to have the private_value field
3143 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3144 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003145int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 hda_nid_t nid = get_amp_nid(kcontrol);
3150 int chs = get_amp_channels(kcontrol);
3151 int dir = get_amp_direction(kcontrol);
3152 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 long *valp = ucontrol->value.integer.value;
3154 int change = 0;
3155
Takashi Iwaicb53c622007-08-10 17:21:45 +02003156 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003157 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003158 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003159 HDA_AMP_MUTE,
3160 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003161 valp++;
3162 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003163 if (chs & 2)
3164 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003165 HDA_AMP_MUTE,
3166 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003167 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003168 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return change;
3170}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003171EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173/*
Takashi Iwai985be542005-11-02 18:26:49 +01003174 * bound volume controls
3175 *
3176 * bind multiple volumes (# indices, from 0)
3177 */
3178
3179#define AMP_VAL_IDX_SHIFT 19
3180#define AMP_VAL_IDX_MASK (0x0f<<19)
3181
Takashi Iwaid5191e52009-11-16 14:58:17 +01003182/**
3183 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003184 * @kcontrol: ctl element
3185 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003186 *
3187 * The control element is supposed to have the private_value field
3188 * set up via HDA_BIND_MUTE*() macros.
3189 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003190int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003192{
3193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3194 unsigned long pval;
3195 int err;
3196
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003197 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003198 pval = kcontrol->private_value;
3199 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3200 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3201 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003202 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003203 return err;
3204}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003205EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003206
Takashi Iwaid5191e52009-11-16 14:58:17 +01003207/**
3208 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003209 * @kcontrol: ctl element
3210 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003211 *
3212 * The control element is supposed to have the private_value field
3213 * set up via HDA_BIND_MUTE*() macros.
3214 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003215int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3216 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003217{
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3219 unsigned long pval;
3220 int i, indices, err = 0, change = 0;
3221
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003222 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003223 pval = kcontrol->private_value;
3224 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3225 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3227 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003228 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3229 if (err < 0)
3230 break;
3231 change |= err;
3232 }
3233 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003234 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003235 return err < 0 ? err : change;
3236}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003237EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003238
Takashi Iwaid5191e52009-11-16 14:58:17 +01003239/**
3240 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003241 * @kcontrol: referred ctl element
3242 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003243 *
3244 * The control element is supposed to have the private_value field
3245 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003246 */
3247int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3248 struct snd_ctl_elem_info *uinfo)
3249{
3250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3251 struct hda_bind_ctls *c;
3252 int err;
3253
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003254 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003255 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003256 kcontrol->private_value = *c->values;
3257 err = c->ops->info(kcontrol, uinfo);
3258 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003259 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003260 return err;
3261}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003262EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003263
Takashi Iwaid5191e52009-11-16 14:58:17 +01003264/**
3265 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003266 * @kcontrol: ctl element
3267 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003268 *
3269 * The control element is supposed to have the private_value field
3270 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3271 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003272int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
3274{
3275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3276 struct hda_bind_ctls *c;
3277 int err;
3278
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003279 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003280 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003281 kcontrol->private_value = *c->values;
3282 err = c->ops->get(kcontrol, ucontrol);
3283 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003284 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003285 return err;
3286}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003287EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003288
Takashi Iwaid5191e52009-11-16 14:58:17 +01003289/**
3290 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003291 * @kcontrol: ctl element
3292 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003293 *
3294 * The control element is supposed to have the private_value field
3295 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3296 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003297int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3299{
3300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3301 struct hda_bind_ctls *c;
3302 unsigned long *vals;
3303 int err = 0, change = 0;
3304
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003305 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003306 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003307 for (vals = c->values; *vals; vals++) {
3308 kcontrol->private_value = *vals;
3309 err = c->ops->put(kcontrol, ucontrol);
3310 if (err < 0)
3311 break;
3312 change |= err;
3313 }
3314 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003315 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003316 return err < 0 ? err : change;
3317}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003318EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003319
Takashi Iwaid5191e52009-11-16 14:58:17 +01003320/**
3321 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003322 * @kcontrol: ctl element
3323 * @op_flag: operation flag
3324 * @size: byte size of input TLV
3325 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003326 *
3327 * The control element is supposed to have the private_value field
3328 * set up via HDA_BIND_VOL() macro.
3329 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003330int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3331 unsigned int size, unsigned int __user *tlv)
3332{
3333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334 struct hda_bind_ctls *c;
3335 int err;
3336
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003337 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003338 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003339 kcontrol->private_value = *c->values;
3340 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3341 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003342 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003343 return err;
3344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003345EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003346
3347struct hda_ctl_ops snd_hda_bind_vol = {
3348 .info = snd_hda_mixer_amp_volume_info,
3349 .get = snd_hda_mixer_amp_volume_get,
3350 .put = snd_hda_mixer_amp_volume_put,
3351 .tlv = snd_hda_mixer_amp_tlv
3352};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003353EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003354
3355struct hda_ctl_ops snd_hda_bind_sw = {
3356 .info = snd_hda_mixer_amp_switch_info,
3357 .get = snd_hda_mixer_amp_switch_get,
3358 .put = snd_hda_mixer_amp_switch_put,
3359 .tlv = snd_hda_mixer_amp_tlv
3360};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003361EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003362
3363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 * SPDIF out controls
3365 */
3366
Takashi Iwai0ba21762007-04-16 11:29:14 +02003367static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3371 uinfo->count = 1;
3372 return 0;
3373}
3374
Takashi Iwai0ba21762007-04-16 11:29:14 +02003375static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3376 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3379 IEC958_AES0_NONAUDIO |
3380 IEC958_AES0_CON_EMPHASIS_5015 |
3381 IEC958_AES0_CON_NOT_COPYRIGHT;
3382 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3383 IEC958_AES1_CON_ORIGINAL;
3384 return 0;
3385}
3386
Takashi Iwai0ba21762007-04-16 11:29:14 +02003387static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3391 IEC958_AES0_NONAUDIO |
3392 IEC958_AES0_PRO_EMPHASIS_5015;
3393 return 0;
3394}
3395
Takashi Iwai0ba21762007-04-16 11:29:14 +02003396static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3397 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003400 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003401 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003403 mutex_lock(&codec->spdif_mutex);
3404 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003405 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3406 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3407 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3408 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003409 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411 return 0;
3412}
3413
3414/* convert from SPDIF status bits to HDA SPDIF bits
3415 * bit 0 (DigEn) is always set zero (to be filled later)
3416 */
3417static unsigned short convert_from_spdif_status(unsigned int sbits)
3418{
3419 unsigned short val = 0;
3420
3421 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003422 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003424 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003426 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3427 IEC958_AES0_PRO_EMPHASIS_5015)
3428 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003430 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3431 IEC958_AES0_CON_EMPHASIS_5015)
3432 val |= AC_DIG1_EMPHASIS;
3433 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3434 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003436 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3438 }
3439 return val;
3440}
3441
3442/* convert to SPDIF status bits from HDA SPDIF bits
3443 */
3444static unsigned int convert_to_spdif_status(unsigned short val)
3445{
3446 unsigned int sbits = 0;
3447
Takashi Iwai0ba21762007-04-16 11:29:14 +02003448 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003450 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 sbits |= IEC958_AES0_PROFESSIONAL;
3452 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003453 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3455 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003456 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003458 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3462 sbits |= val & (0x7f << 8);
3463 }
3464 return sbits;
3465}
3466
Takashi Iwai2f728532008-09-25 16:32:41 +02003467/* set digital convert verbs both for the given NID and its slaves */
3468static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3469 int verb, int val)
3470{
Takashi Iwaidda14412011-05-02 11:29:30 +02003471 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003472
Takashi Iwai9e976972008-11-25 08:17:20 +01003473 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003474 d = codec->slave_dig_outs;
3475 if (!d)
3476 return;
3477 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003478 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003479}
3480
3481static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3482 int dig1, int dig2)
3483{
3484 if (dig1 != -1)
3485 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3486 if (dig2 != -1)
3487 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3488}
3489
Takashi Iwai0ba21762007-04-16 11:29:14 +02003490static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003494 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003495 struct hda_spdif_out *spdif;
3496 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 unsigned short val;
3498 int change;
3499
Ingo Molnar62932df2006-01-16 16:34:20 +01003500 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003501 spdif = snd_array_elem(&codec->spdif_out, idx);
3502 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003503 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3505 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3506 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003507 val = convert_from_spdif_status(spdif->status);
3508 val |= spdif->ctls & 1;
3509 change = spdif->ctls != val;
3510 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003511 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003512 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003513 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 return change;
3515}
3516
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003517#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Takashi Iwai0ba21762007-04-16 11:29:14 +02003519static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3520 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003523 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003524 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003526 mutex_lock(&codec->spdif_mutex);
3527 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003528 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003529 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return 0;
3531}
3532
Stephen Warren74b654c2011-06-01 11:14:18 -06003533static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3534 int dig1, int dig2)
3535{
3536 set_dig_out_convert(codec, nid, dig1, dig2);
3537 /* unmute amp switch (if any) */
3538 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3539 (dig1 & AC_DIG1_ENABLE))
3540 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3541 HDA_AMP_MUTE, 0);
3542}
3543
Takashi Iwai0ba21762007-04-16 11:29:14 +02003544static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3545 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003548 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003549 struct hda_spdif_out *spdif;
3550 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 unsigned short val;
3552 int change;
3553
Ingo Molnar62932df2006-01-16 16:34:20 +01003554 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003555 spdif = snd_array_elem(&codec->spdif_out, idx);
3556 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003557 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003560 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003561 spdif->ctls = val;
3562 if (change && nid != (u16)-1)
3563 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003564 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return change;
3566}
3567
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003568static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 {
3570 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3571 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003572 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 .info = snd_hda_spdif_mask_info,
3574 .get = snd_hda_spdif_cmask_get,
3575 },
3576 {
3577 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003579 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 .info = snd_hda_spdif_mask_info,
3581 .get = snd_hda_spdif_pmask_get,
3582 },
3583 {
3584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003585 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 .info = snd_hda_spdif_mask_info,
3587 .get = snd_hda_spdif_default_get,
3588 .put = snd_hda_spdif_default_put,
3589 },
3590 {
3591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003592 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 .info = snd_hda_spdif_out_switch_info,
3594 .get = snd_hda_spdif_out_switch_get,
3595 .put = snd_hda_spdif_out_switch_put,
3596 },
3597 { } /* end */
3598};
3599
3600/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003601 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003603 * @associated_nid: NID that new ctls associated with
3604 * @cvt_nid: converter NID
3605 * @type: HDA_PCM_TYPE_*
3606 * Creates controls related with the digital output.
3607 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 *
3609 * Returns 0 if successful, or a negative error code.
3610 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003611int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3612 hda_nid_t associated_nid,
3613 hda_nid_t cvt_nid,
3614 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003617 struct snd_kcontrol *kctl;
3618 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003619 int idx = 0;
3620 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003621 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003622 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003624 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003625 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003626 idx = spdif_index;
3627 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003628 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003629 /* suppose a single SPDIF device */
3630 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3631 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3632 if (!kctl)
3633 break;
3634 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003635 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003636 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003637 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003638 if (!bus->primary_dig_out_type)
3639 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003640
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003641 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003642 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003643 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003644 return -EBUSY;
3645 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003646 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003647 if (!spdif)
3648 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3650 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003651 if (!kctl)
3652 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003653 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003654 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003655 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003656 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return err;
3658 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003659 spdif->nid = cvt_nid;
3660 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003661 AC_VERB_GET_DIGI_CONVERT_1, 0);
3662 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return 0;
3664}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003665EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Takashi Iwai95a962c2014-10-29 16:03:58 +01003667/**
3668 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3669 * @codec: the HDA codec
3670 * @nid: widget NID
3671 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003672 * call within spdif_mutex lock
3673 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003674struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3675 hda_nid_t nid)
3676{
3677 int i;
3678 for (i = 0; i < codec->spdif_out.used; i++) {
3679 struct hda_spdif_out *spdif =
3680 snd_array_elem(&codec->spdif_out, i);
3681 if (spdif->nid == nid)
3682 return spdif;
3683 }
3684 return NULL;
3685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003686EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003687
Takashi Iwai95a962c2014-10-29 16:03:58 +01003688/**
3689 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3690 * @codec: the HDA codec
3691 * @idx: the SPDIF ctl index
3692 *
3693 * Unassign the widget from the given SPDIF control.
3694 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003695void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3696{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003697 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003698
3699 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003700 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003701 spdif->nid = (u16)-1;
3702 mutex_unlock(&codec->spdif_mutex);
3703}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003704EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003705
Takashi Iwai95a962c2014-10-29 16:03:58 +01003706/**
3707 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3708 * @codec: the HDA codec
3709 * @idx: the SPDIF ctl idx
3710 * @nid: widget NID
3711 *
3712 * Assign the widget to the SPDIF control with the given index.
3713 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003714void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3715{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003716 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003717 unsigned short val;
3718
3719 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003720 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003721 if (spdif->nid != nid) {
3722 spdif->nid = nid;
3723 val = spdif->ctls;
3724 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3725 }
3726 mutex_unlock(&codec->spdif_mutex);
3727}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003728EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003731 * SPDIF sharing with analog output
3732 */
3733static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3734 struct snd_ctl_elem_value *ucontrol)
3735{
3736 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3737 ucontrol->value.integer.value[0] = mout->share_spdif;
3738 return 0;
3739}
3740
3741static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3742 struct snd_ctl_elem_value *ucontrol)
3743{
3744 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3745 mout->share_spdif = !!ucontrol->value.integer.value[0];
3746 return 0;
3747}
3748
3749static struct snd_kcontrol_new spdif_share_sw = {
3750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3751 .name = "IEC958 Default PCM Playback Switch",
3752 .info = snd_ctl_boolean_mono_info,
3753 .get = spdif_share_sw_get,
3754 .put = spdif_share_sw_put,
3755};
3756
Takashi Iwaid5191e52009-11-16 14:58:17 +01003757/**
3758 * snd_hda_create_spdif_share_sw - create Default PCM switch
3759 * @codec: the HDA codec
3760 * @mout: multi-out instance
3761 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003762int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3763 struct hda_multi_out *mout)
3764{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003765 struct snd_kcontrol *kctl;
3766
Takashi Iwai9a081602008-02-12 18:37:26 +01003767 if (!mout->dig_out_nid)
3768 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003769
3770 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3771 if (!kctl)
3772 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003773 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003774 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003776EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003777
3778/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 * SPDIF input
3780 */
3781
3782#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3783
Takashi Iwai0ba21762007-04-16 11:29:14 +02003784static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3785 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
3787 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3788
3789 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3790 return 0;
3791}
3792
Takashi Iwai0ba21762007-04-16 11:29:14 +02003793static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3794 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3797 hda_nid_t nid = kcontrol->private_value;
3798 unsigned int val = !!ucontrol->value.integer.value[0];
3799 int change;
3800
Ingo Molnar62932df2006-01-16 16:34:20 +01003801 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003803 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003805 snd_hda_codec_write_cache(codec, nid, 0,
3806 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003808 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return change;
3810}
3811
Takashi Iwai0ba21762007-04-16 11:29:14 +02003812static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3813 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3816 hda_nid_t nid = kcontrol->private_value;
3817 unsigned short val;
3818 unsigned int sbits;
3819
Andrew Paprocki3982d172007-12-19 12:13:44 +01003820 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 sbits = convert_to_spdif_status(val);
3822 ucontrol->value.iec958.status[0] = sbits;
3823 ucontrol->value.iec958.status[1] = sbits >> 8;
3824 ucontrol->value.iec958.status[2] = sbits >> 16;
3825 ucontrol->value.iec958.status[3] = sbits >> 24;
3826 return 0;
3827}
3828
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003829static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 {
3831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003832 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 .info = snd_hda_spdif_in_switch_info,
3834 .get = snd_hda_spdif_in_switch_get,
3835 .put = snd_hda_spdif_in_switch_put,
3836 },
3837 {
3838 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003840 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 .info = snd_hda_spdif_mask_info,
3842 .get = snd_hda_spdif_in_status_get,
3843 },
3844 { } /* end */
3845};
3846
3847/**
3848 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3849 * @codec: the HDA codec
3850 * @nid: audio in widget NID
3851 *
3852 * Creates controls related with the SPDIF input.
3853 * Called from each patch supporting the SPDIF in.
3854 *
3855 * Returns 0 if successful, or a negative error code.
3856 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003857int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
3859 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003860 struct snd_kcontrol *kctl;
3861 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003862 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Takashi Iwaidcda5802012-10-12 17:24:51 +02003864 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003865 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003866 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003867 return -EBUSY;
3868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3870 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003871 if (!kctl)
3872 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003874 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003875 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return err;
3877 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003878 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003879 snd_hda_codec_read(codec, nid, 0,
3880 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003881 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return 0;
3883}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003884EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003886/*
3887 * command cache
3888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwaic370dd62012-12-13 18:30:04 +01003890/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003891#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3892#define get_cmd_cache_nid(key) ((key) & 0xff)
3893#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3894
3895/**
3896 * snd_hda_codec_write_cache - send a single command with caching
3897 * @codec: the HDA codec
3898 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003899 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003900 * @verb: the verb to send
3901 * @parm: the parameter for the verb
3902 *
3903 * Send a single command without waiting for response.
3904 *
3905 * Returns 0 if successful, or a negative error code.
3906 */
3907int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003908 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003909{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003910 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003911 struct hda_cache_head *c;
3912 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003913 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003914
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003915 cache_only = codec->cached_write;
3916 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003917 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003918 if (err < 0)
3919 return err;
3920 }
3921
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003922 /* parm may contain the verb stuff for get/set amp */
3923 verb = verb | (parm >> 8);
3924 parm &= 0xff;
3925 key = build_cmd_cache_key(nid, verb);
3926 mutex_lock(&codec->bus->cmd_mutex);
3927 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003928 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003929 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003930 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003931 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003932 mutex_unlock(&codec->bus->cmd_mutex);
3933 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003934}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003935EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003936
Takashi Iwaid5191e52009-11-16 14:58:17 +01003937/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003938 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3939 * @codec: the HDA codec
3940 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003941 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003942 * @verb: the verb to send
3943 * @parm: the parameter for the verb
3944 *
3945 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3946 * command if the parameter is already identical with the cached value.
3947 * If not, it sends the command and refreshes the cache.
3948 *
3949 * Returns 0 if successful, or a negative error code.
3950 */
3951int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003952 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003953{
3954 struct hda_cache_head *c;
3955 u32 key;
3956
3957 /* parm may contain the verb stuff for get/set amp */
3958 verb = verb | (parm >> 8);
3959 parm &= 0xff;
3960 key = build_cmd_cache_key(nid, verb);
3961 mutex_lock(&codec->bus->cmd_mutex);
3962 c = get_hash(&codec->cmd_cache, key);
3963 if (c && c->val == parm) {
3964 mutex_unlock(&codec->bus->cmd_mutex);
3965 return 0;
3966 }
3967 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003968 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003970EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003971
3972/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003973 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3974 * @codec: HD-audio codec
3975 *
3976 * Execute all verbs recorded in the command caches to resume.
3977 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003978void snd_hda_codec_resume_cache(struct hda_codec *codec)
3979{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003980 int i;
3981
Takashi Iwaic370dd62012-12-13 18:30:04 +01003982 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003983 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003984 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3985 struct hda_cache_head *buffer;
3986 u32 key;
3987
3988 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3989 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003990 if (!key)
3991 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003992 if (!buffer->dirty)
3993 continue;
3994 buffer->dirty = 0;
3995 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003996 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3997 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003998 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003999 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01004000 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004001}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004002EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004003
4004/**
4005 * snd_hda_sequence_write_cache - sequence writes with caching
4006 * @codec: the HDA codec
4007 * @seq: VERB array to send
4008 *
4009 * Send the commands sequentially from the given array.
4010 * Thte commands are recorded on cache for power-save and resume.
4011 * The array must be terminated with NID=0.
4012 */
4013void snd_hda_sequence_write_cache(struct hda_codec *codec,
4014 const struct hda_verb *seq)
4015{
4016 for (; seq->nid; seq++)
4017 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
4018 seq->param);
4019}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004020EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004021
Takashi Iwaidc870f32013-01-22 15:24:30 +01004022/**
4023 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4024 * @codec: HD-audio codec
4025 */
4026void snd_hda_codec_flush_cache(struct hda_codec *codec)
4027{
4028 snd_hda_codec_resume_amp(codec);
4029 snd_hda_codec_resume_cache(codec);
4030}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004031EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01004032
Takashi Iwai95a962c2014-10-29 16:03:58 +01004033/**
4034 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4035 * @codec: the HDA codec
4036 * @fg: function group (not used now)
4037 * @power_state: the power state to set (AC_PWRST_*)
4038 *
4039 * Set the given power state to all widgets that have the power control.
4040 * If the codec has power_filter set, it evaluates the power state and
4041 * filter out if it's unchanged as D3.
4042 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004043void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004044 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01004045{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004046 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004047 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01004048
Takashi Iwaicb53c622007-08-10 17:21:45 +02004049 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01004050 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004051 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004052 if (!(wcaps & AC_WCAP_POWER))
4053 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004054 if (codec->power_filter) {
4055 state = codec->power_filter(codec, nid, power_state);
4056 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004057 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02004058 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004059 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004060 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01004061 }
Takashi Iwai54d17402005-11-21 16:33:22 +01004062}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004063EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004064
4065/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004066 * supported power states check
4067 */
4068static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4069 unsigned int power_state)
4070{
4071 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4072
Mengdong Line037cb42012-08-10 14:11:58 +02004073 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004074 return false;
4075 if (sup & power_state)
4076 return true;
4077 else
4078 return false;
4079}
4080
4081/*
Takashi Iwai432c6412012-08-28 09:59:20 -07004082 * wait until the state is reached, returns the current state
4083 */
4084static unsigned int hda_sync_power_state(struct hda_codec *codec,
4085 hda_nid_t fg,
4086 unsigned int power_state)
4087{
4088 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4089 unsigned int state, actual_state;
4090
4091 for (;;) {
4092 state = snd_hda_codec_read(codec, fg, 0,
4093 AC_VERB_GET_POWER_STATE, 0);
4094 if (state & AC_PWRST_ERROR)
4095 break;
4096 actual_state = (state >> 4) & 0x0f;
4097 if (actual_state == power_state)
4098 break;
4099 if (time_after_eq(jiffies, end_time))
4100 break;
4101 /* wait until the codec reachs to the target state */
4102 msleep(1);
4103 }
4104 return state;
4105}
4106
Takashi Iwai95a962c2014-10-29 16:03:58 +01004107/**
4108 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4109 * @codec: the HDA codec
4110 * @nid: widget NID
4111 * @power_state: power state to evalue
4112 *
4113 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4114 * This can be used a codec power_filter callback.
4115 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004116unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4117 hda_nid_t nid,
4118 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004119{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004120 if (nid == codec->afg || nid == codec->mfg)
4121 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004122 if (power_state == AC_PWRST_D3 &&
4123 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4124 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4125 int eapd = snd_hda_codec_read(codec, nid, 0,
4126 AC_VERB_GET_EAPD_BTLENABLE, 0);
4127 if (eapd & 0x02)
4128 return AC_PWRST_D0;
4129 }
4130 return power_state;
4131}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004132EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004133
Takashi Iwai432c6412012-08-28 09:59:20 -07004134/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004135 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004136 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004137static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004138 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004139{
Takashi Iwaid8193872012-08-31 07:54:38 -07004140 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004141 int count;
4142 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004143 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004144
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004145 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004146 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004147 if (codec->depop_delay < 0)
4148 msleep(codec->epss ? 10 : 100);
4149 else if (codec->depop_delay > 0)
4150 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004151 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004152 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004153
4154 /* repeat power states setting at most 10 times*/
4155 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004156 if (codec->patch_ops.set_power_state)
4157 codec->patch_ops.set_power_state(codec, fg,
4158 power_state);
4159 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004160 state = power_state;
4161 if (codec->power_filter)
4162 state = codec->power_filter(codec, fg, state);
4163 if (state == power_state || power_state != AC_PWRST_D3)
4164 snd_hda_codec_read(codec, fg, flags,
4165 AC_VERB_SET_POWER_STATE,
4166 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004167 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004168 }
4169 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004170 if (!(state & AC_PWRST_ERROR))
4171 break;
4172 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004173
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004174 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004175}
Takashi Iwai54d17402005-11-21 16:33:22 +01004176
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004177/* sync power states of all widgets;
4178 * this is called at the end of codec parsing
4179 */
4180static void sync_power_up_states(struct hda_codec *codec)
4181{
4182 hda_nid_t nid = codec->start_nid;
4183 int i;
4184
Takashi Iwaiba615b82013-03-13 14:47:21 +01004185 /* don't care if no filter is used */
4186 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004187 return;
4188
4189 for (i = 0; i < codec->num_nodes; i++, nid++) {
4190 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004191 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004192 if (!(wcaps & AC_WCAP_POWER))
4193 continue;
4194 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4195 if (target == AC_PWRST_D0)
4196 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004197 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004198 snd_hda_codec_write(codec, nid, 0,
4199 AC_VERB_SET_POWER_STATE, target);
4200 }
4201}
4202
Takashi Iwai648a8d22014-02-25 10:38:13 +01004203#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004204/* execute additional init verbs */
4205static void hda_exec_init_verbs(struct hda_codec *codec)
4206{
4207 if (codec->init_verbs.list)
4208 snd_hda_sequence_write(codec, codec->init_verbs.list);
4209}
4210#else
4211static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4212#endif
4213
Takashi Iwai2a439522011-07-26 09:52:50 +02004214#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004215/*
4216 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004217 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004218 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004219static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004220{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004221 unsigned int state;
4222
Takashi Iwai989c3182012-11-19 14:14:58 +01004223 codec->in_pm = 1;
4224
Takashi Iwaicb53c622007-08-10 17:21:45 +02004225 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004226 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004227 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004228 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004229 /* Cancel delayed work if we aren't currently running from it. */
4230 if (!in_wq)
4231 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004232 spin_lock(&codec->power_lock);
4233 snd_hda_update_power_acct(codec);
4234 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004235 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004236 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004237 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004238 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004239 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004240 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004241}
4242
Takashi Iwaic370dd62012-12-13 18:30:04 +01004243/* mark all entries of cmd and amp caches dirty */
4244static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4245{
4246 int i;
4247 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4248 struct hda_cache_head *cmd;
4249 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4250 cmd->dirty = 1;
4251 }
4252 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4253 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004254 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004255 amp->head.dirty = 1;
4256 }
4257}
4258
Takashi Iwaicb53c622007-08-10 17:21:45 +02004259/*
4260 * kick up codec; used both from PM and power-save
4261 */
4262static void hda_call_codec_resume(struct hda_codec *codec)
4263{
Takashi Iwai989c3182012-11-19 14:14:58 +01004264 codec->in_pm = 1;
4265
Takashi Iwaic370dd62012-12-13 18:30:04 +01004266 hda_mark_cmd_cache_dirty(codec);
4267
Takashi Iwai7f308302012-05-08 16:52:23 +02004268 /* set as if powered on for avoiding re-entering the resume
4269 * in the resume / power-save sequence
4270 */
4271 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004272 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004273 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004274 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004275 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004276 if (codec->patch_ops.resume)
4277 codec->patch_ops.resume(codec);
4278 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004279 if (codec->patch_ops.init)
4280 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004281 snd_hda_codec_resume_amp(codec);
4282 snd_hda_codec_resume_cache(codec);
4283 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004284
4285 if (codec->jackpoll_interval)
4286 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004287 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004288 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004289
4290 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004291 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004292}
Takashi Iwai2a439522011-07-26 09:52:50 +02004293#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004294
Takashi Iwai54d17402005-11-21 16:33:22 +01004295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296/**
4297 * snd_hda_build_controls - build mixer controls
4298 * @bus: the BUS
4299 *
4300 * Creates mixer controls for each codec included in the bus.
4301 *
4302 * Returns 0 if successful, otherwise a negative error code.
4303 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004304int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004306 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Takashi Iwai0ba21762007-04-16 11:29:14 +02004308 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004309 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004310 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004311 codec_err(codec,
4312 "cannot build controls for #%d (error %d)\n",
4313 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004314 err = snd_hda_codec_reset(codec);
4315 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004316 codec_err(codec,
4317 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004318 return err;
4319 }
4320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004322 return 0;
4323}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004324EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004326/*
4327 * add standard channel maps if not specified
4328 */
4329static int add_std_chmaps(struct hda_codec *codec)
4330{
4331 int i, str, err;
4332
4333 for (i = 0; i < codec->num_pcms; i++) {
4334 for (str = 0; str < 2; str++) {
4335 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4336 struct hda_pcm_stream *hinfo =
4337 &codec->pcm_info[i].stream[str];
4338 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004339 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004340
4341 if (codec->pcm_info[i].own_chmap)
4342 continue;
4343 if (!pcm || !hinfo->substreams)
4344 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004345 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4346 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004347 hinfo->channels_max,
4348 0, &chmap);
4349 if (err < 0)
4350 return err;
4351 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4352 }
4353 }
4354 return 0;
4355}
4356
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004357/* default channel maps for 2.1 speakers;
4358 * since HD-audio supports only stereo, odd number channels are omitted
4359 */
4360const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4361 { .channels = 2,
4362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4363 { .channels = 4,
4364 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4365 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4366 { }
4367};
4368EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4369
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004370int snd_hda_codec_build_controls(struct hda_codec *codec)
4371{
4372 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004373 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004374 /* continue to initialize... */
4375 if (codec->patch_ops.init)
4376 err = codec->patch_ops.init(codec);
4377 if (!err && codec->patch_ops.build_controls)
4378 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004379 if (err < 0)
4380 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004381
4382 /* we create chmaps here instead of build_pcms */
4383 err = add_std_chmaps(codec);
4384 if (err < 0)
4385 return err;
4386
David Henningsson26a6cb62012-10-09 15:04:21 +02004387 if (codec->jackpoll_interval)
4388 hda_jackpoll_work(&codec->jackpoll_work.work);
4389 else
4390 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004391 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return 0;
4393}
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395/*
4396 * stream formats
4397 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004398struct hda_rate_tbl {
4399 unsigned int hz;
4400 unsigned int alsa_bits;
4401 unsigned int hda_fmt;
4402};
4403
Takashi Iwai92f10b32010-08-03 14:21:00 +02004404/* rate = base * mult / div */
4405#define HDA_RATE(base, mult, div) \
4406 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4407 (((div) - 1) << AC_FMT_DIV_SHIFT))
4408
Takashi Iwaibefdf312005-08-22 13:57:55 +02004409static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004411
4412 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004413 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4414 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4415 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4416 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4417 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4418 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4419 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4420 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4421 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4422 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4423 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004424#define AC_PAR_PCM_RATE_BITS 11
4425 /* up to bits 10, 384kHZ isn't supported properly */
4426
4427 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004428 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004429
Takashi Iwaibefdf312005-08-22 13:57:55 +02004430 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431};
4432
4433/**
4434 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004435 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 * @rate: the sample rate
4437 * @channels: the number of channels
4438 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4439 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004440 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 *
4442 * Calculate the format bitset from the given rate, channels and th PCM format.
4443 *
4444 * Return zero if invalid.
4445 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004446unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4447 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 unsigned int channels,
4449 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004450 unsigned int maxbps,
4451 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 int i;
4454 unsigned int val = 0;
4455
Takashi Iwaibefdf312005-08-22 13:57:55 +02004456 for (i = 0; rate_bits[i].hz; i++)
4457 if (rate_bits[i].hz == rate) {
4458 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 break;
4460 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004461 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004462 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return 0;
4464 }
4465
4466 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004467 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return 0;
4469 }
4470 val |= channels - 1;
4471
4472 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004473 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004474 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004475 break;
4476 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004477 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 case 20:
4480 case 24:
4481 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004482 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004483 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004485 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004487 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 break;
4489 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004490 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004491 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return 0;
4493 }
4494
Anssi Hannula32c168c2010-08-03 13:28:57 +03004495 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004496 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 return val;
4499}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004500EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004502static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4503 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004504{
4505 unsigned int val = 0;
4506 if (nid != codec->afg &&
4507 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4508 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4509 if (!val || val == -1)
4510 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4511 if (!val || val == -1)
4512 return 0;
4513 return val;
4514}
4515
4516static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4517{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004518 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004519 get_pcm_param);
4520}
4521
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004522static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4523 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004524{
4525 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4526 if (!streams || streams == -1)
4527 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4528 if (!streams || streams == -1)
4529 return 0;
4530 return streams;
4531}
4532
4533static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4534{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004535 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004536 get_stream_param);
4537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539/**
4540 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4541 * @codec: the HDA codec
4542 * @nid: NID to query
4543 * @ratesp: the pointer to store the detected rate bitflags
4544 * @formatsp: the pointer to store the detected formats
4545 * @bpsp: the pointer to store the detected format widths
4546 *
4547 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4548 * or @bsps argument is ignored.
4549 *
4550 * Returns 0 if successful, otherwise a negative error code.
4551 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004552int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4554{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004555 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004557 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004558 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 if (ratesp) {
4561 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004562 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004564 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004566 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004567 codec_err(codec,
4568 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4569 nid, val,
4570 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004571 return -EIO;
4572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 *ratesp = rates;
4574 }
4575
4576 if (formatsp || bpsp) {
4577 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004578 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004580 streams = query_stream_param(codec, nid);
4581 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 bps = 0;
4585 if (streams & AC_SUPFMT_PCM) {
4586 if (val & AC_SUPPCM_BITS_8) {
4587 formats |= SNDRV_PCM_FMTBIT_U8;
4588 bps = 8;
4589 }
4590 if (val & AC_SUPPCM_BITS_16) {
4591 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4592 bps = 16;
4593 }
4594 if (wcaps & AC_WCAP_DIGITAL) {
4595 if (val & AC_SUPPCM_BITS_32)
4596 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4597 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4598 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4599 if (val & AC_SUPPCM_BITS_24)
4600 bps = 24;
4601 else if (val & AC_SUPPCM_BITS_20)
4602 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004603 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4604 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4606 if (val & AC_SUPPCM_BITS_32)
4607 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 else if (val & AC_SUPPCM_BITS_24)
4609 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004610 else if (val & AC_SUPPCM_BITS_20)
4611 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004614#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004615 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004617 if (!bps)
4618 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004619 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004620#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004621 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004622 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 /* temporary hack: we have still no proper support
4624 * for the direct AC3 stream...
4625 */
4626 formats |= SNDRV_PCM_FMTBIT_U8;
4627 bps = 8;
4628 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004629 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004630 codec_err(codec,
4631 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4632 nid, val,
4633 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4634 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004635 return -EIO;
4636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 if (formatsp)
4638 *formatsp = formats;
4639 if (bpsp)
4640 *bpsp = bps;
4641 }
4642
4643 return 0;
4644}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004645EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004648 * snd_hda_is_supported_format - Check the validity of the format
4649 * @codec: HD-audio codec
4650 * @nid: NID to check
4651 * @format: the HD-audio format value to check
4652 *
4653 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 *
4655 * Returns 1 if supported, 0 if not.
4656 */
4657int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4658 unsigned int format)
4659{
4660 int i;
4661 unsigned int val = 0, rate, stream;
4662
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004663 val = query_pcm_param(codec, nid);
4664 if (!val)
4665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004668 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004669 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (val & (1 << i))
4671 break;
4672 return 0;
4673 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004674 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 return 0;
4676
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004677 stream = query_stream_param(codec, nid);
4678 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return 0;
4680
4681 if (stream & AC_SUPFMT_PCM) {
4682 switch (format & 0xf0) {
4683 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004684 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 return 0;
4686 break;
4687 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004688 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return 0;
4690 break;
4691 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004692 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 return 0;
4694 break;
4695 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004696 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 return 0;
4698 break;
4699 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004700 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 return 0;
4702 break;
4703 default:
4704 return 0;
4705 }
4706 } else {
4707 /* FIXME: check for float32 and AC3? */
4708 }
4709
4710 return 1;
4711}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004712EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714/*
4715 * PCM stuff
4716 */
4717static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4718 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004719 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
4721 return 0;
4722}
4723
4724static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4725 struct hda_codec *codec,
4726 unsigned int stream_tag,
4727 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004728 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
4730 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4731 return 0;
4732}
4733
4734static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4735 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004736 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Takashi Iwai888afa12008-03-18 09:57:50 +01004738 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return 0;
4740}
4741
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004742static int set_pcm_default_values(struct hda_codec *codec,
4743 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004745 int err;
4746
Takashi Iwai0ba21762007-04-16 11:29:14 +02004747 /* query support PCM information from the given NID */
4748 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004749 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004750 info->rates ? NULL : &info->rates,
4751 info->formats ? NULL : &info->formats,
4752 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004753 if (err < 0)
4754 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 }
4756 if (info->ops.open == NULL)
4757 info->ops.open = hda_pcm_default_open_close;
4758 if (info->ops.close == NULL)
4759 info->ops.close = hda_pcm_default_open_close;
4760 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004761 if (snd_BUG_ON(!info->nid))
4762 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 info->ops.prepare = hda_pcm_default_prepare;
4764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004766 if (snd_BUG_ON(!info->nid))
4767 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 info->ops.cleanup = hda_pcm_default_cleanup;
4769 }
4770 return 0;
4771}
4772
Takashi Iwaieb541332010-08-06 13:48:11 +02004773/*
4774 * codec prepare/cleanup entries
4775 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004776/**
4777 * snd_hda_codec_prepare - Prepare a stream
4778 * @codec: the HDA codec
4779 * @hinfo: PCM information
4780 * @stream: stream tag to assign
4781 * @format: format id to assign
4782 * @substream: PCM substream to assign
4783 *
4784 * Calls the prepare callback set by the codec with the given arguments.
4785 * Clean up the inactive streams when successful.
4786 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004787int snd_hda_codec_prepare(struct hda_codec *codec,
4788 struct hda_pcm_stream *hinfo,
4789 unsigned int stream,
4790 unsigned int format,
4791 struct snd_pcm_substream *substream)
4792{
4793 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004794 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004795 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4796 if (ret >= 0)
4797 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004798 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004799 return ret;
4800}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004801EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004802
Takashi Iwai95a962c2014-10-29 16:03:58 +01004803/**
4804 * snd_hda_codec_cleanup - Prepare a stream
4805 * @codec: the HDA codec
4806 * @hinfo: PCM information
4807 * @substream: PCM substream
4808 *
4809 * Calls the cleanup callback set by the codec with the given arguments.
4810 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004811void snd_hda_codec_cleanup(struct hda_codec *codec,
4812 struct hda_pcm_stream *hinfo,
4813 struct snd_pcm_substream *substream)
4814{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004815 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004816 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004817 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004818}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004819EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004820
Takashi Iwaid5191e52009-11-16 14:58:17 +01004821/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004822const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4823 "Audio", "SPDIF", "HDMI", "Modem"
4824};
4825
Takashi Iwai176d5332008-07-30 15:01:44 +02004826/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004827 * get the empty PCM device number to assign
4828 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004829static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004830{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004831 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004832 /* assigned to static slots up to dev#10; if more needed, assign
4833 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4834 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004835 static int audio_idx[HDA_PCM_NTYPES][5] = {
4836 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4837 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004838 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004839 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004840 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004841 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004842
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004843 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004844 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004845 return -EINVAL;
4846 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004847
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004848 for (i = 0; audio_idx[type][i] >= 0; i++) {
4849#ifndef CONFIG_SND_DYNAMIC_MINORS
4850 if (audio_idx[type][i] >= 8)
4851 break;
4852#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004853 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4854 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004855 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004856
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004857#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004858 /* non-fixed slots starting from 10 */
4859 for (i = 10; i < 32; i++) {
4860 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4861 return i;
4862 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004863#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004864
Takashi Iwai4e76a882014-02-25 12:21:03 +01004865 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004866 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004867#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004868 dev_warn(bus->card->dev,
4869 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004870#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004871 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004872}
4873
4874/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004875 * attach a new PCM stream
4876 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004877static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004878{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004879 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004880 struct hda_pcm_stream *info;
4881 int stream, err;
4882
Takashi Iwaib91f0802008-11-04 08:43:08 +01004883 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004884 return -EINVAL;
4885 for (stream = 0; stream < 2; stream++) {
4886 info = &pcm->stream[stream];
4887 if (info->substreams) {
4888 err = set_pcm_default_values(codec, info);
4889 if (err < 0)
4890 return err;
4891 }
4892 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004893 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004894}
4895
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004896/* assign all PCMs of the given codec */
4897int snd_hda_codec_build_pcms(struct hda_codec *codec)
4898{
4899 unsigned int pcm;
4900 int err;
4901
4902 if (!codec->num_pcms) {
4903 if (!codec->patch_ops.build_pcms)
4904 return 0;
4905 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004906 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004907 codec_err(codec,
4908 "cannot build PCMs for #%d (error %d)\n",
4909 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004910 err = snd_hda_codec_reset(codec);
4911 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004912 codec_err(codec,
4913 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004914 return err;
4915 }
4916 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004917 }
4918 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4919 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4920 int dev;
4921
4922 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004923 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004924
4925 if (!cpcm->pcm) {
4926 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4927 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004928 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004929 cpcm->device = dev;
4930 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004931 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004932 codec_err(codec,
4933 "cannot attach PCM stream %d for codec #%d\n",
4934 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004935 continue; /* no fatal error */
4936 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004937 }
4938 }
4939 return 0;
4940}
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942/**
4943 * snd_hda_build_pcms - build PCM information
4944 * @bus: the BUS
4945 *
4946 * Create PCM information for each codec included in the bus.
4947 *
4948 * The build_pcms codec patch is requested to set up codec->num_pcms and
4949 * codec->pcm_info properly. The array is referred by the top-level driver
4950 * to create its PCM instances.
4951 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4952 * callback.
4953 *
4954 * At least, substreams, channels_min and channels_max must be filled for
4955 * each stream. substreams = 0 indicates that the stream doesn't exist.
4956 * When rates and/or formats are zero, the supported values are queried
4957 * from the given nid. The nid is used also by the default ops.prepare
4958 * and ops.cleanup callbacks.
4959 *
4960 * The driver needs to call ops.open in its open callback. Similarly,
4961 * ops.close is supposed to be called in the close callback.
4962 * ops.prepare should be called in the prepare or hw_params callback
4963 * with the proper parameters for set up.
4964 * ops.cleanup should be called in hw_free for clean up of streams.
4965 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004966 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004968int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004970 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Takashi Iwai0ba21762007-04-16 11:29:14 +02004972 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004973 int err = snd_hda_codec_build_pcms(codec);
4974 if (err < 0)
4975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977 return 0;
4978}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004979EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 * snd_hda_add_new_ctls - create controls from the array
4983 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004984 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 *
4986 * This helper function creates and add new controls in the given array.
4987 * The array must be terminated with an empty entry as terminator.
4988 *
4989 * Returns 0 if successful, or a negative error code.
4990 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004991int snd_hda_add_new_ctls(struct hda_codec *codec,
4992 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004994 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004997 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004998 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004999 if (knew->iface == -1) /* skip this codec private value */
5000 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01005001 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01005002 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02005003 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01005004 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01005005 if (addr > 0)
5006 kctl->id.device = addr;
5007 if (idx > 0)
5008 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01005009 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005010 if (!err)
5011 break;
5012 /* try first with another device index corresponding to
5013 * the codec addr; if it still fails (or it's the
5014 * primary codec), then try another control index
5015 */
5016 if (!addr && codec->addr)
5017 addr = codec->addr;
5018 else if (!idx && !knew->index) {
5019 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02005020 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005021 if (idx <= 0)
5022 return err;
5023 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01005024 return err;
5025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
5027 return 0;
5028}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005029EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Takashi Iwai83012a72012-08-24 18:38:08 +02005031#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02005032static void hda_power_work(struct work_struct *work)
5033{
5034 struct hda_codec *codec =
5035 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01005036 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07005037 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005038
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005039 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005040 if (codec->power_transition > 0) { /* during power-up sequence? */
5041 spin_unlock(&codec->power_lock);
5042 return;
5043 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02005044 if (!codec->power_on || codec->power_count) {
5045 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005046 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005047 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02005048 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005049 spin_unlock(&codec->power_lock);
5050
Dylan Reidd17344b2012-09-28 15:57:01 -07005051 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005052 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5053 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005054}
5055
5056static void hda_keep_power_on(struct hda_codec *codec)
5057{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005058 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005059 codec->power_count++;
5060 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005061 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005062 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005063 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005064}
5065
Takashi Iwaid5191e52009-11-16 14:58:17 +01005066/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005067void snd_hda_update_power_acct(struct hda_codec *codec)
5068{
5069 unsigned long delta = jiffies - codec->power_jiffies;
5070 if (codec->power_on)
5071 codec->power_on_acct += delta;
5072 else
5073 codec->power_off_acct += delta;
5074 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005075}
5076
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005077/* Transition to powered up, if wait_power_down then wait for a pending
5078 * transition to D3 to complete. A pending D3 transition is indicated
5079 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005080/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005081static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005082{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005083 /* Return if power_on or transitioning to power_on, unless currently
5084 * powering down. */
5085 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005086 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005087 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005088 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005089
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005090 cancel_delayed_work_sync(&codec->power_work);
5091
5092 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005093 /* If the power down delayed work was cancelled above before starting,
5094 * then there is no need to go through power up here.
5095 */
5096 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005097 if (codec->power_transition < 0)
5098 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005099 return;
5100 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005101
Takashi Iwaid66fee52011-08-02 15:39:31 +02005102 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005103 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005104 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005105 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005106 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005107 spin_unlock(&codec->power_lock);
5108
Takashi Iwaicb53c622007-08-10 17:21:45 +02005109 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005110
5111 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005112 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005113}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005114
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005115#define power_save(codec) \
5116 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005117
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005118/* Transition to powered down */
5119static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005120{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005121 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005122 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005123
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005124 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005125 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005126 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005127 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005128 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005129}
5130
5131/**
5132 * snd_hda_power_save - Power-up/down/sync the codec
5133 * @codec: HD-audio codec
5134 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005135 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005136 *
5137 * Change the power-up counter via @delta, and power up or down the hardware
5138 * appropriately. For the power-down, queue to the delayed action.
5139 * Passing zero to @delta means to synchronize the power state.
5140 */
5141void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5142{
5143 spin_lock(&codec->power_lock);
5144 codec->power_count += delta;
5145 trace_hda_power_count(codec);
5146 if (delta > 0)
5147 __snd_hda_power_up(codec, d3wait);
5148 else
5149 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005150 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005151}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005152EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005153
Takashi Iwaid5191e52009-11-16 14:58:17 +01005154/**
5155 * snd_hda_check_amp_list_power - Check the amp list and update the power
5156 * @codec: HD-audio codec
5157 * @check: the object containing an AMP list and the status
5158 * @nid: NID to check / update
5159 *
5160 * Check whether the given NID is in the amp list. If it's in the list,
5161 * check the current AMP status, and update the the power-status according
5162 * to the mute status.
5163 *
5164 * This function is supposed to be set or called from the check_power_status
5165 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005166 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005167int snd_hda_check_amp_list_power(struct hda_codec *codec,
5168 struct hda_loopback_check *check,
5169 hda_nid_t nid)
5170{
Takashi Iwai031024e2011-05-02 11:29:30 +02005171 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005172 int ch, v;
5173
5174 if (!check->amplist)
5175 return 0;
5176 for (p = check->amplist; p->nid; p++) {
5177 if (p->nid == nid)
5178 break;
5179 }
5180 if (!p->nid)
5181 return 0; /* nothing changed */
5182
5183 for (p = check->amplist; p->nid; p++) {
5184 for (ch = 0; ch < 2; ch++) {
5185 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5186 p->idx);
5187 if (!(v & HDA_AMP_MUTE) && v > 0) {
5188 if (!check->power_on) {
5189 check->power_on = 1;
5190 snd_hda_power_up(codec);
5191 }
5192 return 1;
5193 }
5194 }
5195 }
5196 if (check->power_on) {
5197 check->power_on = 0;
5198 snd_hda_power_down(codec);
5199 }
5200 return 0;
5201}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005202EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005205/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005206 * Channel mode helper
5207 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005208
5209/**
5210 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005211 * @codec: the HDA codec
5212 * @uinfo: pointer to get/store the data
5213 * @chmode: channel mode array
5214 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005215 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005216int snd_hda_ch_mode_info(struct hda_codec *codec,
5217 struct snd_ctl_elem_info *uinfo,
5218 const struct hda_channel_mode *chmode,
5219 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005220{
5221 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5222 uinfo->count = 1;
5223 uinfo->value.enumerated.items = num_chmodes;
5224 if (uinfo->value.enumerated.item >= num_chmodes)
5225 uinfo->value.enumerated.item = num_chmodes - 1;
5226 sprintf(uinfo->value.enumerated.name, "%dch",
5227 chmode[uinfo->value.enumerated.item].channels);
5228 return 0;
5229}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005230EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005231
Takashi Iwaid5191e52009-11-16 14:58:17 +01005232/**
5233 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005234 * @codec: the HDA codec
5235 * @ucontrol: pointer to get/store the data
5236 * @chmode: channel mode array
5237 * @num_chmodes: channel mode array size
5238 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005240int snd_hda_ch_mode_get(struct hda_codec *codec,
5241 struct snd_ctl_elem_value *ucontrol,
5242 const struct hda_channel_mode *chmode,
5243 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005244 int max_channels)
5245{
5246 int i;
5247
5248 for (i = 0; i < num_chmodes; i++) {
5249 if (max_channels == chmode[i].channels) {
5250 ucontrol->value.enumerated.item[0] = i;
5251 break;
5252 }
5253 }
5254 return 0;
5255}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005256EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005257
Takashi Iwaid5191e52009-11-16 14:58:17 +01005258/**
5259 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005260 * @codec: the HDA codec
5261 * @ucontrol: pointer to get/store the data
5262 * @chmode: channel mode array
5263 * @num_chmodes: channel mode array size
5264 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005266int snd_hda_ch_mode_put(struct hda_codec *codec,
5267 struct snd_ctl_elem_value *ucontrol,
5268 const struct hda_channel_mode *chmode,
5269 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005270 int *max_channelsp)
5271{
5272 unsigned int mode;
5273
5274 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005275 if (mode >= num_chmodes)
5276 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005277 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005278 return 0;
5279 /* change the current channel setting */
5280 *max_channelsp = chmode[mode].channels;
5281 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005282 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005283 return 1;
5284}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005285EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287/*
5288 * input MUX helper
5289 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005290
5291/**
5292 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005293 * @imux: imux helper object
5294 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005295 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005296int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5297 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
5299 unsigned int index;
5300
5301 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5302 uinfo->count = 1;
5303 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005304 if (!imux->num_items)
5305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 index = uinfo->value.enumerated.item;
5307 if (index >= imux->num_items)
5308 index = imux->num_items - 1;
5309 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5310 return 0;
5311}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005312EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Takashi Iwaid5191e52009-11-16 14:58:17 +01005314/**
5315 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005316 * @codec: the HDA codec
5317 * @imux: imux helper object
5318 * @ucontrol: pointer to get/store the data
5319 * @nid: input mux NID
5320 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005321 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005322int snd_hda_input_mux_put(struct hda_codec *codec,
5323 const struct hda_input_mux *imux,
5324 struct snd_ctl_elem_value *ucontrol,
5325 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 unsigned int *cur_val)
5327{
5328 unsigned int idx;
5329
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005330 if (!imux->num_items)
5331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 idx = ucontrol->value.enumerated.item[0];
5333 if (idx >= imux->num_items)
5334 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005335 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005337 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5338 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 *cur_val = idx;
5340 return 1;
5341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005342EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005345/**
5346 * snd_hda_enum_helper_info - Helper for simple enum ctls
5347 * @kcontrol: ctl element
5348 * @uinfo: pointer to get/store the data
5349 * @num_items: number of enum items
5350 * @texts: enum item string array
5351 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005352 * process kcontrol info callback of a simple string enum array
5353 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5354 */
5355int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5356 struct snd_ctl_elem_info *uinfo,
5357 int num_items, const char * const *texts)
5358{
5359 static const char * const texts_default[] = {
5360 "Disabled", "Enabled"
5361 };
5362
5363 if (!texts || !num_items) {
5364 num_items = 2;
5365 texts = texts_default;
5366 }
5367
Takashi Iwai3ff72212014-10-20 18:17:28 +02005368 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005369}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005370EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005371
5372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 * Multi-channel / digital-out PCM helper functions
5374 */
5375
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005376/* setup SPDIF output stream */
5377static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5378 unsigned int stream_tag, unsigned int format)
5379{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005380 struct hda_spdif_out *spdif;
5381 unsigned int curr_fmt;
5382 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005383
Laurence Darby3bef1c32012-11-03 17:00:06 +00005384 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5385 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5386 AC_VERB_GET_STREAM_FORMAT, 0);
5387 reset = codec->spdif_status_reset &&
5388 (spdif->ctls & AC_DIG1_ENABLE) &&
5389 curr_fmt != format;
5390
5391 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5392 updated */
5393 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005394 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005395 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005396 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005397 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005398 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005399 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005400 for (d = codec->slave_dig_outs; *d; d++)
5401 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5402 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005403 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005404 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005405 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005406 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005407 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005408}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005409
Takashi Iwai2f728532008-09-25 16:32:41 +02005410static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5411{
5412 snd_hda_codec_cleanup_stream(codec, nid);
5413 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005414 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005415 for (d = codec->slave_dig_outs; *d; d++)
5416 snd_hda_codec_cleanup_stream(codec, *d);
5417 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005418}
5419
Takashi Iwaid5191e52009-11-16 14:58:17 +01005420/**
5421 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5422 * @bus: HD-audio bus
5423 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005424void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5425{
5426 struct hda_codec *codec;
5427
5428 if (!bus)
5429 return;
5430 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005431 if (hda_codec_is_power_on(codec) &&
5432 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005433 codec->patch_ops.reboot_notify(codec);
5434 }
5435}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005436EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005437
Takashi Iwaid5191e52009-11-16 14:58:17 +01005438/**
5439 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005440 * @codec: the HDA codec
5441 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005443int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5444 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445{
Ingo Molnar62932df2006-01-16 16:34:20 +01005446 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005447 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5448 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005449 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005451 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 return 0;
5453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005454EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Takashi Iwaid5191e52009-11-16 14:58:17 +01005456/**
5457 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005458 * @codec: the HDA codec
5459 * @mout: hda_multi_out object
5460 * @stream_tag: stream tag to assign
5461 * @format: format id to assign
5462 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005463 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005464int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5465 struct hda_multi_out *mout,
5466 unsigned int stream_tag,
5467 unsigned int format,
5468 struct snd_pcm_substream *substream)
5469{
5470 mutex_lock(&codec->spdif_mutex);
5471 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5472 mutex_unlock(&codec->spdif_mutex);
5473 return 0;
5474}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005475EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005476
Takashi Iwaid5191e52009-11-16 14:58:17 +01005477/**
5478 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005479 * @codec: the HDA codec
5480 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005481 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005482int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5483 struct hda_multi_out *mout)
5484{
5485 mutex_lock(&codec->spdif_mutex);
5486 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5487 mutex_unlock(&codec->spdif_mutex);
5488 return 0;
5489}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005490EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005491
Takashi Iwaid5191e52009-11-16 14:58:17 +01005492/**
5493 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005494 * @codec: the HDA codec
5495 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005497int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5498 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Ingo Molnar62932df2006-01-16 16:34:20 +01005500 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005502 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return 0;
5504}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005505EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Takashi Iwaid5191e52009-11-16 14:58:17 +01005507/**
5508 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005509 * @codec: the HDA codec
5510 * @mout: hda_multi_out object
5511 * @substream: PCM substream to assign
5512 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005513 *
5514 * Open analog outputs and set up the hw-constraints.
5515 * If the digital outputs can be opened as slave, open the digital
5516 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005518int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5519 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005520 struct snd_pcm_substream *substream,
5521 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
Takashi Iwai9a081602008-02-12 18:37:26 +01005523 struct snd_pcm_runtime *runtime = substream->runtime;
5524 runtime->hw.channels_max = mout->max_channels;
5525 if (mout->dig_out_nid) {
5526 if (!mout->analog_rates) {
5527 mout->analog_rates = hinfo->rates;
5528 mout->analog_formats = hinfo->formats;
5529 mout->analog_maxbps = hinfo->maxbps;
5530 } else {
5531 runtime->hw.rates = mout->analog_rates;
5532 runtime->hw.formats = mout->analog_formats;
5533 hinfo->maxbps = mout->analog_maxbps;
5534 }
5535 if (!mout->spdif_rates) {
5536 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5537 &mout->spdif_rates,
5538 &mout->spdif_formats,
5539 &mout->spdif_maxbps);
5540 }
5541 mutex_lock(&codec->spdif_mutex);
5542 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005543 if ((runtime->hw.rates & mout->spdif_rates) &&
5544 (runtime->hw.formats & mout->spdif_formats)) {
5545 runtime->hw.rates &= mout->spdif_rates;
5546 runtime->hw.formats &= mout->spdif_formats;
5547 if (mout->spdif_maxbps < hinfo->maxbps)
5548 hinfo->maxbps = mout->spdif_maxbps;
5549 } else {
5550 mout->share_spdif = 0;
5551 /* FIXME: need notify? */
5552 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005553 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005554 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5557 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5558}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005559EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Takashi Iwaid5191e52009-11-16 14:58:17 +01005561/**
5562 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005563 * @codec: the HDA codec
5564 * @mout: hda_multi_out object
5565 * @stream_tag: stream tag to assign
5566 * @format: format id to assign
5567 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005568 *
5569 * Set up the i/o for analog out.
5570 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005572int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5573 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 unsigned int stream_tag,
5575 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005576 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
Takashi Iwaidda14412011-05-02 11:29:30 +02005578 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005580 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 int i;
5582
Ingo Molnar62932df2006-01-16 16:34:20 +01005583 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005584 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005585 if (mout->dig_out_nid && mout->share_spdif &&
5586 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005588 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5589 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005590 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005592 setup_dig_out_stream(codec, mout->dig_out_nid,
5593 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 } else {
5595 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005596 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 }
5598 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005599 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
5601 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005602 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5603 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005604 if (!mout->no_share_stream &&
5605 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005607 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5608 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005609 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005610 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5611 if (!mout->no_share_stream && mout->hp_out_nid[i])
5612 snd_hda_codec_setup_stream(codec,
5613 mout->hp_out_nid[i],
5614 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 /* surrounds */
5617 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005618 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005619 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5620 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005621 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005622 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5623 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005625
5626 /* extra surrounds */
5627 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5628 int ch = 0;
5629 if (!mout->extra_out_nid[i])
5630 break;
5631 if (chs >= (i + 1) * 2)
5632 ch = i * 2;
5633 else if (!mout->no_share_stream)
5634 break;
5635 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5636 stream_tag, ch, format);
5637 }
5638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 return 0;
5640}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005641EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Takashi Iwaid5191e52009-11-16 14:58:17 +01005643/**
5644 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005645 * @codec: the HDA codec
5646 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005648int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5649 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
Takashi Iwaidda14412011-05-02 11:29:30 +02005651 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 int i;
5653
5654 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005655 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005657 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005658 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5659 if (mout->hp_out_nid[i])
5660 snd_hda_codec_cleanup_stream(codec,
5661 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005662 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5663 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005664 snd_hda_codec_cleanup_stream(codec,
5665 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005666 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005668 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 mout->dig_out_used = 0;
5670 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005671 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 return 0;
5673}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005674EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
Takashi Iwai47408602012-04-20 13:06:53 +02005676/**
5677 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005678 * @codec: the HDA codec
5679 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005680 *
5681 * Guess the suitable VREF pin bits to be set as the pin-control value.
5682 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5683 */
5684unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5685{
5686 unsigned int pincap;
5687 unsigned int oldval;
5688 oldval = snd_hda_codec_read(codec, pin, 0,
5689 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5690 pincap = snd_hda_query_pin_caps(codec, pin);
5691 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5692 /* Exception: if the default pin setup is vref50, we give it priority */
5693 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5694 return AC_PINCTL_VREF_80;
5695 else if (pincap & AC_PINCAP_VREF_50)
5696 return AC_PINCTL_VREF_50;
5697 else if (pincap & AC_PINCAP_VREF_100)
5698 return AC_PINCTL_VREF_100;
5699 else if (pincap & AC_PINCAP_VREF_GRD)
5700 return AC_PINCTL_VREF_GRD;
5701 return AC_PINCTL_VREF_HIZ;
5702}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005703EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005704
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005705/**
5706 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5707 * @codec: the HDA codec
5708 * @pin: referred pin NID
5709 * @val: pin ctl value to audit
5710 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005711unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5712 hda_nid_t pin, unsigned int val)
5713{
5714 static unsigned int cap_lists[][2] = {
5715 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5716 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5717 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5718 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5719 };
5720 unsigned int cap;
5721
5722 if (!val)
5723 return 0;
5724 cap = snd_hda_query_pin_caps(codec, pin);
5725 if (!cap)
5726 return val; /* don't know what to do... */
5727
5728 if (val & AC_PINCTL_OUT_EN) {
5729 if (!(cap & AC_PINCAP_OUT))
5730 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5731 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5732 val &= ~AC_PINCTL_HP_EN;
5733 }
5734
5735 if (val & AC_PINCTL_IN_EN) {
5736 if (!(cap & AC_PINCAP_IN))
5737 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5738 else {
5739 unsigned int vcap, vref;
5740 int i;
5741 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5742 vref = val & AC_PINCTL_VREFEN;
5743 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5744 if (vref == cap_lists[i][0] &&
5745 !(vcap & cap_lists[i][1])) {
5746 if (i == ARRAY_SIZE(cap_lists) - 1)
5747 vref = AC_PINCTL_VREF_HIZ;
5748 else
5749 vref = cap_lists[i + 1][0];
5750 }
5751 }
5752 val &= ~AC_PINCTL_VREFEN;
5753 val |= vref;
5754 }
5755 }
5756
5757 return val;
5758}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005759EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005760
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005761/**
5762 * _snd_hda_pin_ctl - Helper to set pin ctl value
5763 * @codec: the HDA codec
5764 * @pin: referred pin NID
5765 * @val: pin control value to set
5766 * @cached: access over codec pinctl cache or direct write
5767 *
5768 * This function is a helper to set a pin ctl value more safely.
5769 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5770 * value in pin target array via snd_hda_codec_set_pin_target(), then
5771 * actually writes the value via either snd_hda_codec_update_cache() or
5772 * snd_hda_codec_write() depending on @cached flag.
5773 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005774int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5775 unsigned int val, bool cached)
5776{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005777 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005778 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005779 if (cached)
5780 return snd_hda_codec_update_cache(codec, pin, 0,
5781 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5782 else
5783 return snd_hda_codec_write(codec, pin, 0,
5784 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5785}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005786EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005787
Takashi Iwai990061c2010-09-09 22:08:44 +02005788/**
5789 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005790 * @codec: the HDA codec
5791 * @imux: imux helper object
5792 * @label: the name of imux item to assign
5793 * @index: index number of imux item to assign
5794 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005795 *
5796 * When the same label is used already in the existing items, the number
5797 * suffix is appended to the label. This label index number is stored
5798 * to type_idx when non-NULL pointer is given.
5799 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005800int snd_hda_add_imux_item(struct hda_codec *codec,
5801 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005802 int index, int *type_idx)
5803{
5804 int i, label_idx = 0;
5805 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005806 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005807 return -EINVAL;
5808 }
5809 for (i = 0; i < imux->num_items; i++) {
5810 if (!strncmp(label, imux->items[i].label, strlen(label)))
5811 label_idx++;
5812 }
5813 if (type_idx)
5814 *type_idx = label_idx;
5815 if (label_idx > 0)
5816 snprintf(imux->items[imux->num_items].label,
5817 sizeof(imux->items[imux->num_items].label),
5818 "%s %d", label, label_idx);
5819 else
5820 strlcpy(imux->items[imux->num_items].label, label,
5821 sizeof(imux->items[imux->num_items].label));
5822 imux->items[imux->num_items].index = index;
5823 imux->num_items++;
5824 return 0;
5825}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005826EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005827
Takashi Iwai4a471b72005-12-07 13:56:29 +01005828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829#ifdef CONFIG_PM
5830/*
5831 * power management
5832 */
5833
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005834
5835static void hda_async_suspend(void *data, async_cookie_t cookie)
5836{
5837 hda_call_codec_suspend(data, false);
5838}
5839
5840static void hda_async_resume(void *data, async_cookie_t cookie)
5841{
5842 hda_call_codec_resume(data);
5843}
5844
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845/**
5846 * snd_hda_suspend - suspend the codecs
5847 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 *
5849 * Returns 0 if successful.
5850 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005851int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005853 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005854 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Takashi Iwai0ba21762007-04-16 11:29:14 +02005856 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005857 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005858 if (hda_codec_is_power_on(codec)) {
5859 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005860 async_schedule_domain(hda_async_suspend, codec,
5861 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005862 else
5863 hda_call_codec_suspend(codec, false);
5864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005866
5867 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005868 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 return 0;
5871}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005872EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874/**
5875 * snd_hda_resume - resume the codecs
5876 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 *
5878 * Returns 0 if successful.
5879 */
5880int snd_hda_resume(struct hda_bus *bus)
5881{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005882 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005883 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
Takashi Iwai0ba21762007-04-16 11:29:14 +02005885 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005886 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005887 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005888 else
5889 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005891
5892 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005893 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 return 0;
5896}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005897EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005898#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005899
5900/*
5901 * generic arrays
5902 */
5903
Takashi Iwaid5191e52009-11-16 14:58:17 +01005904/**
5905 * snd_array_new - get a new element from the given array
5906 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005907 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005908 * Get a new element from the given array. If it exceeds the
5909 * pre-allocated array size, re-allocate the array.
5910 *
5911 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005912 */
5913void *snd_array_new(struct snd_array *array)
5914{
Takashi Iwai12f17712012-10-10 08:59:14 +02005915 if (snd_BUG_ON(!array->elem_size))
5916 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005917 if (array->used >= array->alloced) {
5918 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005919 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005920 void *nlist;
5921 if (snd_BUG_ON(num >= 4096))
5922 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005923 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005924 if (!nlist)
5925 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005926 array->list = nlist;
5927 array->alloced = num;
5928 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005929 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005930}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005931EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005932
Takashi Iwaid5191e52009-11-16 14:58:17 +01005933/**
5934 * snd_array_free - free the given array elements
5935 * @array: the array object
5936 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005937void snd_array_free(struct snd_array *array)
5938{
5939 kfree(array->list);
5940 array->used = 0;
5941 array->alloced = 0;
5942 array->list = NULL;
5943}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005944EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005945
Takashi Iwaid5191e52009-11-16 14:58:17 +01005946/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005947 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5948 * @pcm: PCM caps bits
5949 * @buf: the string buffer to write
5950 * @buflen: the max buffer length
5951 *
5952 * used by hda_proc.c and hda_eld.c
5953 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005954void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5955{
5956 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5957 int i, j;
5958
5959 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5960 if (pcm & (AC_SUPPCM_BITS_8 << i))
5961 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5962
5963 buf[j] = '\0'; /* necessary when j == 0 */
5964}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005965EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005966
5967MODULE_DESCRIPTION("HDA codec core");
5968MODULE_LICENSE("GPL");