blob: b2d58998dbddbb6c99e0b93c2a9b390565c8f0a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Takashi Iwaidff86f82014-02-19 11:11:20 +010070 { 0x1af4, "QEMU" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010072 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020073 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {} /* terminator */
75};
76
Takashi Iwai1289e9e2008-11-27 15:47:11 +010077static DEFINE_MUTEX(preset_mutex);
78static LIST_HEAD(hda_preset_tables);
79
Takashi Iwai95a962c2014-10-29 16:03:58 +010080/**
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
83 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010084int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
85{
86 mutex_lock(&preset_mutex);
87 list_add_tail(&preset->list, &hda_preset_tables);
88 mutex_unlock(&preset_mutex);
89 return 0;
90}
Takashi Iwai2698ea92013-12-18 07:45:52 +010091EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010092
Takashi Iwai95a962c2014-10-29 16:03:58 +010093/**
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
96 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +010097int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
98{
99 mutex_lock(&preset_mutex);
100 list_del(&preset->list);
101 mutex_unlock(&preset_mutex);
102 return 0;
103}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100104EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai83012a72012-08-24 18:38:08 +0200106#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +0100107#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200108static void hda_power_work(struct work_struct *work);
109static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100111
112static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -0700113{
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100114 struct hda_bus *bus = codec->bus;
115
116 if ((power_up && codec->pm_up_notified) ||
117 (!power_up && !codec->pm_up_notified))
118 return;
Takashi Iwai68467f52012-08-28 09:14:29 -0700119 if (bus->ops.pm_notify)
120 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100121 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -0700122}
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100123
Takashi Iwaicb53c622007-08-10 17:21:45 +0200124#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100125#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200126static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200127#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +0100128#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200129#endif
130
Takashi Iwaid5191e52009-11-16 14:58:17 +0100131/**
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
137 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400138const char *snd_hda_get_jack_location(u32 cfg)
139{
140 static char *bases[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
142 };
143 static unsigned char specials_idx[] = {
144 0x07, 0x08,
145 0x17, 0x18, 0x19,
146 0x37, 0x38
147 };
148 static char *specials[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
152 };
153 int i;
154 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
155 if ((cfg & 0x0f) < 7)
156 return bases[cfg & 0x0f];
157 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
158 if (cfg == specials_idx[i])
159 return specials[i];
160 }
161 return "UNKNOWN";
162}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100163EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400164
Takashi Iwaid5191e52009-11-16 14:58:17 +0100165/**
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
168 *
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
171 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172const char *snd_hda_get_jack_connectivity(u32 cfg)
173{
174 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
175
176 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
177}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100178EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400179
Takashi Iwaid5191e52009-11-16 14:58:17 +0100180/**
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
183 *
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
186 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400187const char *snd_hda_get_jack_type(u32 cfg)
188{
189 static char *jack_types[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200193 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400194 };
195
196 return jack_types[(cfg & AC_DEFCFG_DEVICE)
197 >> AC_DEFCFG_DEVICE_SHIFT];
198}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100199EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400200
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201/*
202 * Compose a 32bit command word to be sent to the HD-audio controller
203 */
204static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200205make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100206 unsigned int verb, unsigned int parm)
207{
208 u32 val;
209
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200210 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200211 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100212 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200213 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800214 return ~0;
215 }
216
217 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100218 val |= (u32)nid << 20;
219 val |= verb << 8;
220 val |= parm;
221 return val;
222}
223
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224/*
225 * Send and receive a verb
226 */
227static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200228 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229{
230 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200232
Wu Fengguang6430aee2009-07-17 16:49:19 +0800233 if (cmd == ~0)
234 return -1;
235
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200236 if (res)
237 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200239 snd_hda_power_up(codec);
240 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200241 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
242 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100243 for (;;) {
244 trace_hda_send_cmd(codec, cmd);
245 err = bus->ops.command(bus, cmd);
246 if (err != -EAGAIN)
247 break;
248 /* process pending verbs */
249 bus->ops.get_response(bus, codec->addr);
250 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200251 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800252 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200253 trace_hda_get_response(codec, *res);
254 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200255 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 mutex_unlock(&bus->cmd_mutex);
257 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100258 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200259 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100260 codec_dbg(codec,
261 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200262 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200263 bus->ops.bus_reset(bus);
264 }
265 goto again;
266 }
267 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100268 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200269 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 return err;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200277 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * @verb: the verb to send
279 * @parm: the parameter for the verb
280 *
281 * Send a single command and read the corresponding response.
282 *
283 * Returns the obtained response value, or -1 for an error.
284 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200285unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200286 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned int verb, unsigned int parm)
288{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200289 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200290 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200291 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700292 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return res;
294}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100295EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200301 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * @verb: the verb to send
303 * @parm: the parameter for the verb
304 *
305 * Send a single command without waiting for response.
306 *
307 * Returns 0 if successful, or a negative error code.
308 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200309int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
310 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200312 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100313 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200314 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200315 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100317EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/**
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
323 *
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
326 */
327void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
328{
329 for (; seq->nid; seq++)
330 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
331}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100332EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/**
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
337 * @nid: NID to parse
338 * @start_id: the pointer to store the start NID
339 *
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
342 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200343int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned int parm;
347
348 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200349 if (parm == -1)
350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *start_id = (parm >> 16) & 0x7fff;
352 return (int)(parm & 0x7fff);
353}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100354EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100356/* connection list element */
357struct hda_conn_list {
358 struct list_head list;
359 int len;
360 hda_nid_t nid;
361 hda_nid_t conns[0];
362};
363
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200364/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100365static struct hda_conn_list *
366lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200367{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100368 struct hda_conn_list *p;
369 list_for_each_entry(p, &codec->conn_list, list) {
370 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200371 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 }
373 return NULL;
374}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200375
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100376static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
377 const hda_nid_t *list)
378{
379 struct hda_conn_list *p;
380
381 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
382 if (!p)
383 return -ENOMEM;
384 p->len = len;
385 p->nid = nid;
386 memcpy(p->conns, list, len * sizeof(hda_nid_t));
387 list_add(&p->list, &codec->conn_list);
388 return 0;
389}
390
391static void remove_conn_list(struct hda_codec *codec)
392{
393 while (!list_empty(&codec->conn_list)) {
394 struct hda_conn_list *p;
395 p = list_first_entry(&codec->conn_list, typeof(*p), list);
396 list_del(&p->list);
397 kfree(p);
398 }
399}
400
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200401/* read the connection and add to the cache */
402static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100404 hda_nid_t list[32];
405 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200406 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100409 if (len == -ENOSPC) {
410 len = snd_hda_get_num_raw_conns(codec, nid);
411 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
412 if (!result)
413 return -ENOMEM;
414 len = snd_hda_get_raw_connections(codec, nid, result, len);
415 }
416 if (len >= 0)
417 len = snd_hda_override_conn_list(codec, nid, len, result);
418 if (result != list)
419 kfree(result);
420 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200421}
Takashi Iwaidce20792011-06-24 14:10:28 +0200422
423/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100424 * snd_hda_get_conn_list - get connection list
425 * @codec: the HDA codec
426 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100427 * @listp: the pointer to store NID list
428 *
429 * Parses the connection list of the given widget and stores the pointer
430 * to the list of NIDs.
431 *
432 * Returns the number of connections, or a negative error code.
433 *
434 * Note that the returned pointer isn't protected against the list
435 * modification. If snd_hda_override_conn_list() might be called
436 * concurrently, protect with a mutex appropriately.
437 */
438int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
439 const hda_nid_t **listp)
440{
441 bool added = false;
442
443 for (;;) {
444 int err;
445 const struct hda_conn_list *p;
446
447 /* if the connection-list is already cached, read it */
448 p = lookup_conn_list(codec, nid);
449 if (p) {
450 if (listp)
451 *listp = p->conns;
452 return p->len;
453 }
454 if (snd_BUG_ON(added))
455 return -EINVAL;
456
457 err = read_and_add_raw_conns(codec, nid);
458 if (err < 0)
459 return err;
460 added = true;
461 }
462}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100463EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464
465/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200466 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * @codec: the HDA codec
468 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * @max_conns: max. number of connections to store
471 *
472 * Parses the connection list of the given widget and stores the list
473 * of NIDs.
474 *
475 * Returns the number of connections, or a negative error code.
476 */
477int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200478 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200479{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100480 const hda_nid_t *list;
481 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200482
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100483 if (len > 0 && conn_list) {
484 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100485 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200486 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200487 return -EINVAL;
488 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100489 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200490 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200491
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100492 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200493}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100494EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200495
Takashi Iwai4eea3092013-02-07 18:18:19 +0100496/* return CONNLIST_LEN parameter of the given widget */
497static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
498{
499 unsigned int wcaps = get_wcaps(codec, nid);
500 unsigned int parm;
501
502 if (!(wcaps & AC_WCAP_CONN_LIST) &&
503 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
504 return 0;
505
506 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
507 if (parm == -1)
508 parm = 0;
509 return parm;
510}
511
512int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
513{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100514 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100515}
516
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200517/**
518 * snd_hda_get_raw_connections - copy connection list without cache
519 * @codec: the HDA codec
520 * @nid: NID to parse
521 * @conn_list: connection list array
522 * @max_conns: max. number of connections to store
523 *
524 * Like snd_hda_get_connections(), copy the connection list but without
525 * checking through the connection-list cache.
526 * Currently called only from hda_proc.c, so not exported.
527 */
528int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
529 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100532 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100535 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Takashi Iwai4eea3092013-02-07 18:18:19 +0100537 parm = get_num_conns(codec, nid);
538 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200539 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (parm & AC_CLIST_LONG) {
542 /* long form */
543 shift = 16;
544 num_elems = 2;
545 } else {
546 /* short form */
547 shift = 8;
548 num_elems = 4;
549 }
550 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 mask = (1 << (shift-1)) - 1;
552
Takashi Iwai0ba21762007-04-16 11:29:14 +0200553 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0; /* no connection */
555
556 if (conn_len == 1) {
557 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100562 if (conn_list)
563 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 1;
565 }
566
567 /* multi connection */
568 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100569 prev_nid = 0;
570 for (i = 0; i < conn_len; i++) {
571 int range_val;
572 hda_nid_t val, n;
573
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200574 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100575 parm = snd_hda_codec_read(codec, nid, 0,
576 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200577 if (parm == -1 && codec->bus->rirb_error)
578 return -EIO;
579 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200580 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100581 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100582 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100583 codec_dbg(codec,
584 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200585 nid, i, parm);
586 return 0;
587 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 parm >>= shift;
589 if (range_val) {
590 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200591 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100592 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200593 "invalid dep_range_val %x:%x\n",
594 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100595 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100597 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100598 if (conn_list) {
599 if (conns >= max_conns)
600 return -ENOSPC;
601 conn_list[conns] = n;
602 }
603 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100604 }
605 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100606 if (conn_list) {
607 if (conns >= max_conns)
608 return -ENOSPC;
609 conn_list[conns] = val;
610 }
611 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100613 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 return conns;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619 * snd_hda_override_conn_list - add/modify the connection-list to cache
620 * @codec: the HDA codec
621 * @nid: NID to parse
622 * @len: number of connection list entries
623 * @list: the list of connection entries
624 *
625 * Add or modify the given connection-list to the cache. If the corresponding
626 * cache already exists, invalidate it and append a new one.
627 *
628 * Returns zero or a negative error code.
629 */
630int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
631 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100633 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200634
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100635 p = lookup_conn_list(codec, nid);
636 if (p) {
637 list_del(&p->list);
638 kfree(p);
639 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200640
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100643EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200644
645/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 * snd_hda_get_conn_index - get the connection index of the given NID
647 * @codec: the HDA codec
648 * @mux: NID containing the list
649 * @nid: NID to select
650 * @recursive: 1 when searching NID recursively, otherwise 0
651 *
652 * Parses the connection list of the widget @mux and checks whether the
653 * widget @nid is present. If it is, return the connection index.
654 * Otherwise it returns -1.
655 */
656int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
657 hda_nid_t nid, int recursive)
658{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100659 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 int i, nums;
661
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100662 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 for (i = 0; i < nums; i++)
664 if (conn[i] == nid)
665 return i;
666 if (!recursive)
667 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100668 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100669 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200670 return -1;
671 }
672 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200673 for (i = 0; i < nums; i++) {
674 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
675 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
676 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200677 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
678 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200679 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200680 return -1;
681}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100682EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Mengdong Linf1aa0682013-08-26 21:35:21 -0400684
685/* return DEVLIST_LEN parameter of the given widget */
686static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
687{
688 unsigned int wcaps = get_wcaps(codec, nid);
689 unsigned int parm;
690
691 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
692 get_wcaps_type(wcaps) != AC_WID_PIN)
693 return 0;
694
695 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
696 if (parm == -1 && codec->bus->rirb_error)
697 parm = 0;
698 return parm & AC_DEV_LIST_LEN_MASK;
699}
700
701/**
702 * snd_hda_get_devices - copy device list without cache
703 * @codec: the HDA codec
704 * @nid: NID of the pin to parse
705 * @dev_list: device list array
706 * @max_devices: max. number of devices to store
707 *
708 * Copy the device list. This info is dynamic and so not cached.
709 * Currently called only from hda_proc.c, so not exported.
710 */
711int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
712 u8 *dev_list, int max_devices)
713{
714 unsigned int parm;
715 int i, dev_len, devices;
716
717 parm = get_num_devices(codec, nid);
718 if (!parm) /* not multi-stream capable */
719 return 0;
720
721 dev_len = parm + 1;
722 dev_len = dev_len < max_devices ? dev_len : max_devices;
723
724 devices = 0;
725 while (devices < dev_len) {
726 parm = snd_hda_codec_read(codec, nid, 0,
727 AC_VERB_GET_DEVICE_LIST, devices);
728 if (parm == -1 && codec->bus->rirb_error)
729 break;
730
731 for (i = 0; i < 8; i++) {
732 dev_list[devices] = (u8)parm;
733 parm >>= 4;
734 devices++;
735 if (devices >= dev_len)
736 break;
737 }
738 }
739 return devices;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/**
743 * snd_hda_queue_unsol_event - add an unsolicited event to queue
744 * @bus: the BUS
745 * @res: unsolicited event (lower 32bit of RIRB entry)
746 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
747 *
748 * Adds the given event to the queue. The events are processed in
749 * the workqueue asynchronously. Call this function in the interrupt
750 * hanlder when RIRB receives an unsolicited event.
751 *
752 * Returns 0 if successful, or a negative error code.
753 */
754int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
755{
756 struct hda_bus_unsolicited *unsol;
757 unsigned int wp;
758
Wang YanQing2195b062013-05-07 11:27:33 +0800759 if (!bus || !bus->workq)
760 return 0;
761
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200762 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 unsol = bus->unsol;
764 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766
767 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
768 unsol->wp = wp;
769
770 wp <<= 1;
771 unsol->queue[wp] = res;
772 unsol->queue[wp + 1] = res_ex;
773
Takashi Iwai6acaed32009-01-12 10:09:24 +0100774 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return 0;
777}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100778EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800781 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
David Howellsc4028952006-11-22 14:57:56 +0000783static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David Howellsc4028952006-11-22 14:57:56 +0000785 struct hda_bus_unsolicited *unsol =
786 container_of(work, struct hda_bus_unsolicited, work);
787 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct hda_codec *codec;
789 unsigned int rp, caddr, res;
790
791 while (unsol->rp != unsol->wp) {
792 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
793 unsol->rp = rp;
794 rp <<= 1;
795 res = unsol->queue[rp];
796 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200797 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 continue;
799 codec = bus->caddr_tbl[caddr & 0x0f];
800 if (codec && codec->patch_ops.unsol_event)
801 codec->patch_ops.unsol_event(codec, res);
802 }
803}
804
805/*
806 * initialize unsolicited queue
807 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200808static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct hda_bus_unsolicited *unsol;
811
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100812 if (bus->unsol) /* already initialized */
813 return 0;
814
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200815 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200816 if (!unsol) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100817 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -ENOMEM;
819 }
David Howellsc4028952006-11-22 14:57:56 +0000820 INIT_WORK(&unsol->work, process_unsol_events);
821 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 bus->unsol = unsol;
823 return 0;
824}
825
826/*
827 * destructor
828 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100829static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200831 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100832 return;
833
834 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100835 if (bus->workq)
836 flush_workqueue(bus->workq);
Markus Elfringf0acd282014-11-17 10:44:33 +0100837 kfree(bus->unsol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (bus->ops.private_free)
839 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100840 if (bus->workq)
841 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100846static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Takashi Iwai2565c892014-02-19 11:41:09 +0100848 snd_hda_bus_free(device->device_data);
849 return 0;
850}
851
852static int snd_hda_bus_dev_disconnect(struct snd_device *device)
853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100855 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/**
860 * snd_hda_bus_new - create a HDA bus
861 * @card: the card entry
862 * @temp: the template for hda_bus information
863 * @busp: the pointer to store the created bus instance
864 *
865 * Returns 0 if successful, or a negative error code.
866 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100867int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200868 const struct hda_bus_template *temp,
869 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct hda_bus *bus;
872 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100873 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100874 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .dev_free = snd_hda_bus_dev_free,
876 };
877
Takashi Iwaida3cec32008-08-08 17:12:14 +0200878 if (snd_BUG_ON(!temp))
879 return -EINVAL;
880 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
881 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (busp)
884 *busp = NULL;
885
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200886 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100888 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENOMEM;
890 }
891
892 bus->card = card;
893 bus->private_data = temp->private_data;
894 bus->pci = temp->pci;
895 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100896 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 bus->ops = temp->ops;
898
Ingo Molnar62932df2006-01-16 16:34:20 +0100899 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200900 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 INIT_LIST_HEAD(&bus->codec_list);
902
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100903 snprintf(bus->workq_name, sizeof(bus->workq_name),
904 "hd-audio%d", card->number);
905 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100906 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100907 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100908 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100909 kfree(bus);
910 return -ENOMEM;
911 }
912
Takashi Iwai0ba21762007-04-16 11:29:14 +0200913 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
914 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 snd_hda_bus_free(bus);
916 return err;
917 }
918 if (busp)
919 *busp = bus;
920 return 0;
921}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100922EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Takashi Iwaif8f1bec2014-02-06 18:14:03 +0100924#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
Takashi Iwai82467612007-07-27 19:15:54 +0200925#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200926 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200927#else
928#define is_generic_config(codec) 0
929#endif
930
Takashi Iwai645f10c2008-11-28 15:07:37 +0100931#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100932#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
933#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100934#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100935#endif
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * find a matching codec preset
939 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200940static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200941find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100943 struct hda_codec_preset_list *tbl;
944 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200945 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100947 again:
948 mutex_lock(&preset_mutex);
949 list_for_each_entry(tbl, &hda_preset_tables, list) {
950 if (!try_module_get(tbl->owner)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100951 codec_err(codec, "cannot module_get\n");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100952 continue;
953 }
954 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100956 if (preset->afg && preset->afg != codec->afg)
957 continue;
958 if (preset->mfg && preset->mfg != codec->mfg)
959 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200960 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200962 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200963 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100964 preset->rev == codec->revision_id)) {
965 mutex_unlock(&preset_mutex);
966 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100970 module_put(tbl->owner);
971 }
972 mutex_unlock(&preset_mutex);
973
974 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100975 if (!mod_requested)
Takashi Iwai66797f32014-10-27 15:15:44 +0100976 request_module("snd-hda-codec-id:%08x",
977 codec->vendor_id);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100978 else
Takashi Iwai66797f32014-10-27 15:15:44 +0100979 request_module("snd-hda-codec-id:%04x*",
980 (codec->vendor_id >> 16) & 0xffff);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100981 mod_requested++;
982 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return NULL;
985}
986
987/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200988 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200990static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 const struct hda_vendor_id *c;
993 const char *vendor = NULL;
994 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200995 char tmp[16];
996
997 if (codec->vendor_name)
998 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 for (c = hda_vendor_ids; c->id; c++) {
1001 if (c->id == vendor_id) {
1002 vendor = c->name;
1003 break;
1004 }
1005 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001006 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sprintf(tmp, "Generic %04x", vendor_id);
1008 vendor = tmp;
1009 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001010 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1011 if (!codec->vendor_name)
1012 return -ENOMEM;
1013
1014 get_chip_name:
1015 if (codec->chip_name)
1016 return 0;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001019 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1020 else {
1021 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1022 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1023 }
1024 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +02001025 return -ENOMEM;
1026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001030 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001032static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001034 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 hda_nid_t nid;
1036
1037 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1038 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +02001039 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001040 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +02001041 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001042 case AC_GRP_AUDIO_FUNCTION:
1043 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001044 codec->afg_function_id = function_id & 0xff;
1045 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001046 break;
1047 case AC_GRP_MODEM_FUNCTION:
1048 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +02001049 codec->mfg_function_id = function_id & 0xff;
1050 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001051 break;
1052 default:
1053 break;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001059 * read widget caps for each widget and store in cache
1060 */
1061static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1062{
1063 int i;
1064 hda_nid_t nid;
1065
1066 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1067 &codec->start_nid);
1068 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001069 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001070 return -ENOMEM;
1071 nid = codec->start_nid;
1072 for (i = 0; i < codec->num_nodes; i++, nid++)
1073 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1074 AC_PAR_AUDIO_WIDGET_CAP);
1075 return 0;
1076}
1077
Takashi Iwai3be14142009-02-20 14:11:16 +01001078/* read all pin default configurations and save codec->init_pins */
1079static int read_pin_defaults(struct hda_codec *codec)
1080{
1081 int i;
1082 hda_nid_t nid = codec->start_nid;
1083
1084 for (i = 0; i < codec->num_nodes; i++, nid++) {
1085 struct hda_pincfg *pin;
1086 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001087 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 if (wid_type != AC_WID_PIN)
1089 continue;
1090 pin = snd_array_new(&codec->init_pins);
1091 if (!pin)
1092 return -ENOMEM;
1093 pin->nid = nid;
1094 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1095 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001096 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1097 AC_VERB_GET_PIN_WIDGET_CONTROL,
1098 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001099 }
1100 return 0;
1101}
1102
1103/* look up the given pin config list and return the item matching with NID */
1104static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1105 struct snd_array *array,
1106 hda_nid_t nid)
1107{
1108 int i;
1109 for (i = 0; i < array->used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(array, i);
1111 if (pin->nid == nid)
1112 return pin;
1113 }
1114 return NULL;
1115}
1116
Takashi Iwai3be14142009-02-20 14:11:16 +01001117/* set the current pin config value for the given NID.
1118 * the value is cached, and read via snd_hda_codec_get_pincfg()
1119 */
1120int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1121 hda_nid_t nid, unsigned int cfg)
1122{
1123 struct hda_pincfg *pin;
1124
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001125 /* the check below may be invalid when pins are added by a fixup
1126 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1127 * for now
1128 */
1129 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001130 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1131 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001132 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001133
Takashi Iwai3be14142009-02-20 14:11:16 +01001134 pin = look_up_pincfg(codec, list, nid);
1135 if (!pin) {
1136 pin = snd_array_new(list);
1137 if (!pin)
1138 return -ENOMEM;
1139 pin->nid = nid;
1140 }
1141 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001142 return 0;
1143}
1144
Takashi Iwaid5191e52009-11-16 14:58:17 +01001145/**
1146 * snd_hda_codec_set_pincfg - Override a pin default configuration
1147 * @codec: the HDA codec
1148 * @nid: NID to set the pin config
1149 * @cfg: the pin default config value
1150 *
1151 * Override a pin default configuration value in the cache.
1152 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1153 * priority than the real hardware value.
1154 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001155int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1156 hda_nid_t nid, unsigned int cfg)
1157{
Takashi Iwai346ff702009-02-23 09:42:57 +01001158 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001159}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001160EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001161
Takashi Iwaid5191e52009-11-16 14:58:17 +01001162/**
1163 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1164 * @codec: the HDA codec
1165 * @nid: NID to get the pin config
1166 *
1167 * Get the current pin config value of the given pin NID.
1168 * If the pincfg value is cached or overridden via sysfs or driver,
1169 * returns the cached value.
1170 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001171unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 struct hda_pincfg *pin;
1174
Takashi Iwai648a8d22014-02-25 10:38:13 +01001175#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +01001176 {
1177 unsigned int cfg = 0;
1178 mutex_lock(&codec->user_mutex);
1179 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1180 if (pin)
1181 cfg = pin->cfg;
1182 mutex_unlock(&codec->user_mutex);
1183 if (cfg)
1184 return cfg;
1185 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001186#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001187 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1188 if (pin)
1189 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001190 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1191 if (pin)
1192 return pin->cfg;
1193 return 0;
1194}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001195EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001196
Takashi Iwai95a962c2014-10-29 16:03:58 +01001197/**
1198 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1199 * @codec: the HDA codec
1200 * @nid: pin NID
1201 * @val: assigned pinctl value
1202 *
1203 * This function stores the given value to a pinctl target value in the
1204 * pincfg table. This isn't always as same as the actually written value
1205 * but can be referred at any time via snd_hda_codec_get_pin_target().
1206 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001207int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1208 unsigned int val)
1209{
1210 struct hda_pincfg *pin;
1211
1212 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1213 if (!pin)
1214 return -EINVAL;
1215 pin->target = val;
1216 return 0;
1217}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001218EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001219
Takashi Iwai95a962c2014-10-29 16:03:58 +01001220/**
1221 * snd_hda_codec_get_pin_target - return the current pinctl target value
1222 * @codec: the HDA codec
1223 * @nid: pin NID
1224 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001225int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1226{
1227 struct hda_pincfg *pin;
1228
1229 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1230 if (!pin)
1231 return 0;
1232 return pin->target;
1233}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001234EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001235
Takashi Iwai92ee6162009-12-27 11:18:59 +01001236/**
1237 * snd_hda_shutup_pins - Shut up all pins
1238 * @codec: the HDA codec
1239 *
1240 * Clear all pin controls to shup up before suspend for avoiding click noise.
1241 * The controls aren't cached so that they can be resumed properly.
1242 */
1243void snd_hda_shutup_pins(struct hda_codec *codec)
1244{
1245 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001246 /* don't shut up pins when unloading the driver; otherwise it breaks
1247 * the default pin setup at the next load of the driver
1248 */
1249 if (codec->bus->shutdown)
1250 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001251 for (i = 0; i < codec->init_pins.used; i++) {
1252 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1253 /* use read here for syncing after issuing each verb */
1254 snd_hda_codec_read(codec, pin->nid, 0,
1255 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1256 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001257 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001258}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001259EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001260
Takashi Iwai2a439522011-07-26 09:52:50 +02001261#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001262/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1263static void restore_shutup_pins(struct hda_codec *codec)
1264{
1265 int i;
1266 if (!codec->pins_shutup)
1267 return;
1268 if (codec->bus->shutdown)
1269 return;
1270 for (i = 0; i < codec->init_pins.used; i++) {
1271 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1272 snd_hda_codec_write(codec, pin->nid, 0,
1273 AC_VERB_SET_PIN_WIDGET_CONTROL,
1274 pin->ctrl);
1275 }
1276 codec->pins_shutup = 0;
1277}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001278#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001279
David Henningsson26a6cb62012-10-09 15:04:21 +02001280static void hda_jackpoll_work(struct work_struct *work)
1281{
1282 struct hda_codec *codec =
1283 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001284
1285 snd_hda_jack_set_dirty_all(codec);
1286 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001287
1288 if (!codec->jackpoll_interval)
1289 return;
1290
David Henningsson26a6cb62012-10-09 15:04:21 +02001291 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1292 codec->jackpoll_interval);
1293}
1294
Takashi Iwai01751f52007-08-10 16:59:39 +02001295static void init_hda_cache(struct hda_cache_rec *cache,
1296 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001297static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001298
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001299/* release all pincfg lists */
1300static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001301{
Takashi Iwai346ff702009-02-23 09:42:57 +01001302 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001303#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001304 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001305#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 snd_array_free(&codec->init_pins);
1307}
1308
Takashi Iwai54d17402005-11-21 16:33:22 +01001309/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001310 * audio-converter setup caches
1311 */
1312struct hda_cvt_setup {
1313 hda_nid_t nid;
1314 u8 stream_tag;
1315 u8 channel_id;
1316 u16 format_id;
1317 unsigned char active; /* cvt is currently used */
1318 unsigned char dirty; /* setups should be cleared */
1319};
1320
1321/* get or create a cache entry for the given audio converter NID */
1322static struct hda_cvt_setup *
1323get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1324{
1325 struct hda_cvt_setup *p;
1326 int i;
1327
1328 for (i = 0; i < codec->cvt_setups.used; i++) {
1329 p = snd_array_elem(&codec->cvt_setups, i);
1330 if (p->nid == nid)
1331 return p;
1332 }
1333 p = snd_array_new(&codec->cvt_setups);
1334 if (p)
1335 p->nid = nid;
1336 return p;
1337}
1338
1339/*
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001340 * Dynamic symbol binding for the codec parsers
1341 */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001342
1343#define load_parser(codec, sym) \
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001344 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001345
1346static void unload_parser(struct hda_codec *codec)
1347{
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001348 if (codec->parser)
1349 symbol_put_addr(codec->parser);
1350 codec->parser = NULL;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001351}
1352
1353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * codec destructor
1355 */
1356static void snd_hda_codec_free(struct hda_codec *codec)
1357{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001358 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001360 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001361 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001362 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001363#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001364 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001365 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001366#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001368 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001369 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001370 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001371 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001372 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 codec->bus->caddr_tbl[codec->addr] = NULL;
1374 if (codec->patch_ops.free)
1375 codec->patch_ops.free(codec);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001376 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001377 snd_hda_sysfs_clear(codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001378 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001379 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001380 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001381 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001382 kfree(codec->vendor_name);
1383 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001384 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001385 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001386 codec->bus->num_codecs--;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001387 put_device(&codec->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001390static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1391 hda_nid_t fg, unsigned int power_state);
1392
Takashi Iwaid8193872012-08-31 07:54:38 -07001393static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001394 unsigned int power_state);
1395
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001396static int snd_hda_codec_dev_register(struct snd_device *device)
1397{
1398 struct hda_codec *codec = device->device_data;
Takashi Iwaid604b392014-02-28 13:42:09 +01001399 int err = device_add(&codec->dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001400
Takashi Iwaid604b392014-02-28 13:42:09 +01001401 if (err < 0)
1402 return err;
1403 snd_hda_register_beep_device(codec);
1404 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001405}
1406
1407static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1408{
1409 struct hda_codec *codec = device->device_data;
1410
Takashi Iwaid604b392014-02-28 13:42:09 +01001411 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001412 device_del(&codec->dev);
1413 return 0;
1414}
1415
Takashi Iwai2565c892014-02-19 11:41:09 +01001416static int snd_hda_codec_dev_free(struct snd_device *device)
1417{
1418 snd_hda_codec_free(device->device_data);
1419 return 0;
1420}
1421
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001422/* just free the container */
1423static void snd_hda_codec_dev_release(struct device *dev)
1424{
1425 kfree(container_of(dev, struct hda_codec, dev));
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/**
1429 * snd_hda_codec_new - create a HDA codec
1430 * @bus: the bus to assign
1431 * @codec_addr: the codec address
1432 * @codecp: the pointer to store the generated codec
1433 *
1434 * Returns 0 if successful, or a negative error code.
1435 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001436int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001437 unsigned int codec_addr,
1438 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001441 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001442 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001444 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001445 .dev_register = snd_hda_codec_dev_register,
1446 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001447 .dev_free = snd_hda_codec_dev_free,
1448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Takashi Iwaida3cec32008-08-08 17:12:14 +02001450 if (snd_BUG_ON(!bus))
1451 return -EINVAL;
1452 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001456 dev_err(bus->card->dev,
1457 "address 0x%x is already occupied\n",
1458 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EBUSY;
1460 }
1461
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001462 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001464 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENOMEM;
1466 }
1467
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001468 device_initialize(&codec->dev);
1469 codec->dev.parent = &bus->card->card_dev;
1470 codec->dev.class = sound_class;
1471 codec->dev.release = snd_hda_codec_dev_release;
Takashi Iwai648a8d22014-02-25 10:38:13 +01001472 codec->dev.groups = snd_hda_dev_attr_groups;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001473 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1474 codec_addr);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001475 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 codec->bus = bus;
1478 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001479 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001480 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001481 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001482 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001483 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001484 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1485 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001486 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001487 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001488 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001489 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001490 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001491 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001492 INIT_LIST_HEAD(&codec->conn_list);
1493
David Henningsson26a6cb62012-10-09 15:04:21 +02001494 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001495 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001496 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Takashi Iwai83012a72012-08-24 18:38:08 +02001498#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001499 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001500 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1501 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1502 * the caller has to power down appropriatley after initialization
1503 * phase.
1504 */
1505 hda_keep_power_on(codec);
1506#endif
1507
Takashi Iwai648a8d22014-02-25 10:38:13 +01001508 snd_hda_sysfs_init(codec);
1509
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001510 if (codec->bus->modelname) {
1511 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1512 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001513 err = -ENODEV;
1514 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001515 }
1516 }
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001519 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 bus->caddr_tbl[codec_addr] = codec;
1522
Takashi Iwai0ba21762007-04-16 11:29:14 +02001523 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1524 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001525 if (codec->vendor_id == -1)
1526 /* read again, hopefully the access method was corrected
1527 * in the last read...
1528 */
1529 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1530 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001531 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1532 AC_PAR_SUBSYSTEM_ID);
1533 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1534 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001536 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001538 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001539 err = -ENODEV;
1540 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
Takashi Iwaid8193872012-08-31 07:54:38 -07001543 fg = codec->afg ? codec->afg : codec->mfg;
1544 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001545 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001546 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001547 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001548 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001549 err = read_pin_defaults(codec);
1550 if (err < 0)
1551 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001552
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001554 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001555 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001556 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001557 }
1558
Takashi Iwai83012a72012-08-24 18:38:08 +02001559#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001560 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001561 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001562#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001563 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001564 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001565#ifdef CONFIG_PM
1566 if (!codec->d3_stop_clk || !codec->epss)
1567 bus->power_keep_link_on = 1;
1568#endif
1569
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001570
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001571 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001572 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001573
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001574 snd_hda_codec_proc_new(codec);
1575
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001576 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001577
1578 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1579 codec->subsystem_id, codec->revision_id);
1580 snd_component_add(codec->bus->card, component);
1581
Takashi Iwai2565c892014-02-19 11:41:09 +01001582 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1583 if (err < 0)
1584 goto error;
1585
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001586 if (codecp)
1587 *codecp = codec;
1588 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001589
1590 error:
1591 snd_hda_codec_free(codec);
1592 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001593}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001594EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001595
Takashi Iwai95a962c2014-10-29 16:03:58 +01001596/**
1597 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1598 * @codec: the HDA codec
1599 *
1600 * Forcibly refresh the all widget caps and the init pin configurations of
1601 * the given codec.
1602 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001603int snd_hda_codec_update_widgets(struct hda_codec *codec)
1604{
1605 hda_nid_t fg;
1606 int err;
1607
1608 /* Assume the function group node does not change,
1609 * only the widget nodes may change.
1610 */
1611 kfree(codec->wcaps);
1612 fg = codec->afg ? codec->afg : codec->mfg;
1613 err = read_widget_caps(codec, fg);
1614 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001615 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001616 return err;
1617 }
1618
1619 snd_array_free(&codec->init_pins);
1620 err = read_pin_defaults(codec);
1621
1622 return err;
1623}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001624EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001625
1626
Takashi Iwaif8f1bec2014-02-06 18:14:03 +01001627#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001628/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1629static bool is_likely_hdmi_codec(struct hda_codec *codec)
1630{
1631 hda_nid_t nid = codec->start_nid;
1632 int i;
1633
1634 for (i = 0; i < codec->num_nodes; i++, nid++) {
1635 unsigned int wcaps = get_wcaps(codec, nid);
1636 switch (get_wcaps_type(wcaps)) {
1637 case AC_WID_AUD_IN:
1638 return false; /* HDMI parser supports only HDMI out */
1639 case AC_WID_AUD_OUT:
1640 if (!(wcaps & AC_WCAP_DIGITAL))
1641 return false;
1642 break;
1643 }
1644 }
1645 return true;
1646}
1647#else
1648/* no HDMI codec parser support */
1649#define is_likely_hdmi_codec(codec) false
1650#endif /* CONFIG_SND_HDA_CODEC_HDMI */
1651
Takashi Iwaid5191e52009-11-16 14:58:17 +01001652/**
1653 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1654 * @codec: the HDA codec
1655 *
1656 * Start parsing of the given codec tree and (re-)initialize the whole
1657 * patch instance.
1658 *
1659 * Returns 0 if successful or a negative error code.
1660 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001661int snd_hda_codec_configure(struct hda_codec *codec)
1662{
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001663 int (*patch)(struct hda_codec *) = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001664 int err;
1665
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001666 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001667 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001668 err = get_codec_name(codec);
1669 if (err < 0)
1670 return err;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001673 if (!is_generic_config(codec) && codec->preset)
1674 patch = codec->preset->patch;
1675 if (!patch) {
1676 unload_parser(codec); /* to be sure */
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001677 if (is_likely_hdmi_codec(codec)) {
1678#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
Takashi Iwaif0639272013-11-18 12:07:29 +01001679 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001680#elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1681 patch = snd_hda_parse_hdmi_codec;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001682#endif
Takashi Iwaief8e39b2014-02-10 11:43:41 +01001683 }
1684 if (!patch) {
1685#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1686 patch = load_parser(codec, snd_hda_parse_generic_codec);
1687#elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1688 patch = snd_hda_parse_generic_codec;
1689#endif
1690 }
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001691 if (!patch) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001692 codec_err(codec, "No codec parser is available\n");
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001693 return -ENODEV;
1694 }
Takashi Iwai82467612007-07-27 19:15:54 +02001695 }
1696
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001697 err = patch(codec);
1698 if (err < 0) {
1699 unload_parser(codec);
1700 return err;
1701 }
Takashi Iwai82467612007-07-27 19:15:54 +02001702
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001703 if (codec->patch_ops.unsol_event) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001704 err = init_unsol_queue(codec->bus);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001705 if (err < 0)
1706 return err;
1707 }
1708
Takashi Iwaif62faed2009-12-23 09:27:51 +01001709 /* audio codec should override the mixer name */
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001710 if (codec->afg || !*codec->bus->card->mixername)
Takashi Iwaif62faed2009-12-23 09:27:51 +01001711 snprintf(codec->bus->card->mixername,
1712 sizeof(codec->bus->card->mixername),
1713 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001716EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Takashi Iwaied360812012-08-08 17:12:52 +02001718/* update the stream-id if changed */
1719static void update_pcm_stream_id(struct hda_codec *codec,
1720 struct hda_cvt_setup *p, hda_nid_t nid,
1721 u32 stream_tag, int channel_id)
1722{
1723 unsigned int oldval, newval;
1724
1725 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1726 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1727 newval = (stream_tag << 4) | channel_id;
1728 if (oldval != newval)
1729 snd_hda_codec_write(codec, nid, 0,
1730 AC_VERB_SET_CHANNEL_STREAMID,
1731 newval);
1732 p->stream_tag = stream_tag;
1733 p->channel_id = channel_id;
1734 }
1735}
1736
1737/* update the format-id if changed */
1738static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1739 hda_nid_t nid, int format)
1740{
1741 unsigned int oldval;
1742
1743 if (p->format_id != format) {
1744 oldval = snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_STREAM_FORMAT, 0);
1746 if (oldval != format) {
1747 msleep(1);
1748 snd_hda_codec_write(codec, nid, 0,
1749 AC_VERB_SET_STREAM_FORMAT,
1750 format);
1751 }
1752 p->format_id = format;
1753 }
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/**
1757 * snd_hda_codec_setup_stream - set up the codec for streaming
1758 * @codec: the CODEC to set up
1759 * @nid: the NID to set up
1760 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1761 * @channel_id: channel id to pass, zero based.
1762 * @format: stream format.
1763 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001764void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1765 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int channel_id, int format)
1767{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001768 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001769 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001770 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001771 int i;
1772
Takashi Iwai0ba21762007-04-16 11:29:14 +02001773 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001774 return;
1775
Takashi Iwai4e76a882014-02-25 12:21:03 +01001776 codec_dbg(codec,
1777 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1778 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001779 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001780 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001781 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001782
1783 if (codec->pcm_format_first)
1784 update_pcm_format(codec, p, nid, format);
1785 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1786 if (!codec->pcm_format_first)
1787 update_pcm_format(codec, p, nid, format);
1788
Takashi Iwaieb541332010-08-06 13:48:11 +02001789 p->active = 1;
1790 p->dirty = 0;
1791
1792 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001793 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001794 list_for_each_entry(c, &codec->bus->codec_list, list) {
1795 for (i = 0; i < c->cvt_setups.used; i++) {
1796 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001797 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001798 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001799 p->dirty = 1;
1800 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001803EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Takashi Iwaif0cea792010-08-13 11:56:53 +02001805static void really_cleanup_stream(struct hda_codec *codec,
1806 struct hda_cvt_setup *q);
1807
Takashi Iwaid5191e52009-11-16 14:58:17 +01001808/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001809 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001810 * @codec: the CODEC to clean up
1811 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001812 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001813 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001814void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1815 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001816{
Takashi Iwaieb541332010-08-06 13:48:11 +02001817 struct hda_cvt_setup *p;
1818
Takashi Iwai888afa12008-03-18 09:57:50 +01001819 if (!nid)
1820 return;
1821
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001822 if (codec->no_sticky_stream)
1823 do_now = 1;
1824
Takashi Iwai4e76a882014-02-25 12:21:03 +01001825 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001826 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001827 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001828 /* here we just clear the active flag when do_now isn't set;
1829 * actual clean-ups will be done later in
1830 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1831 */
1832 if (do_now)
1833 really_cleanup_stream(codec, p);
1834 else
1835 p->active = 0;
1836 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001837}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001838EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001839
Takashi Iwaieb541332010-08-06 13:48:11 +02001840static void really_cleanup_stream(struct hda_codec *codec,
1841 struct hda_cvt_setup *q)
1842{
1843 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001844 if (q->stream_tag || q->channel_id)
1845 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1846 if (q->format_id)
1847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1848);
Takashi Iwaieb541332010-08-06 13:48:11 +02001849 memset(q, 0, sizeof(*q));
1850 q->nid = nid;
1851}
1852
1853/* clean up the all conflicting obsolete streams */
1854static void purify_inactive_streams(struct hda_codec *codec)
1855{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001856 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001857 int i;
1858
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001859 list_for_each_entry(c, &codec->bus->codec_list, list) {
1860 for (i = 0; i < c->cvt_setups.used; i++) {
1861 struct hda_cvt_setup *p;
1862 p = snd_array_elem(&c->cvt_setups, i);
1863 if (p->dirty)
1864 really_cleanup_stream(c, p);
1865 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001866 }
1867}
1868
Takashi Iwai2a439522011-07-26 09:52:50 +02001869#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001870/* clean up all streams; called from suspend */
1871static void hda_cleanup_all_streams(struct hda_codec *codec)
1872{
1873 int i;
1874
1875 for (i = 0; i < codec->cvt_setups.used; i++) {
1876 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1877 if (p->stream_tag)
1878 really_cleanup_stream(codec, p);
1879 }
1880}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001881#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883/*
1884 * amp access functions
1885 */
1886
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001887/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001888#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001889#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001890#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1891#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001893#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001896static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001897 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Takashi Iwai01751f52007-08-10 16:59:39 +02001899 memset(cache, 0, sizeof(*cache));
1900 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001901 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001902}
1903
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001904static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001905{
Takashi Iwai603c4012008-07-30 15:01:44 +02001906 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001910static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Takashi Iwai01751f52007-08-10 16:59:39 +02001912 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1913 u16 cur = cache->hash[idx];
1914 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001917 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (info->key == key)
1919 return info;
1920 cur = info->next;
1921 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001922 return NULL;
1923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001925/* query the hash. allocate an entry if not found. */
1926static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1927 u32 key)
1928{
1929 struct hda_cache_head *info = get_hash(cache, key);
1930 if (!info) {
1931 u16 idx, cur;
1932 /* add a new hash entry */
1933 info = snd_array_new(&cache->buf);
1934 if (!info)
1935 return NULL;
1936 cur = snd_array_index(&cache->buf, info);
1937 info->key = key;
1938 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001939 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001940 idx = key % (u16)ARRAY_SIZE(cache->hash);
1941 info->next = cache->hash[idx];
1942 cache->hash[idx] = cur;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return info;
1945}
1946
Takashi Iwai01751f52007-08-10 16:59:39 +02001947/* query and allocate an amp hash entry */
1948static inline struct hda_amp_info *
1949get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1950{
1951 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1952}
1953
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001954/* overwrite the value with the key in the caps hash */
1955static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1956{
1957 struct hda_amp_info *info;
1958
1959 mutex_lock(&codec->hash_mutex);
1960 info = get_alloc_amp_hash(codec, key);
1961 if (!info) {
1962 mutex_unlock(&codec->hash_mutex);
1963 return -EINVAL;
1964 }
1965 info->amp_caps = val;
1966 info->head.val |= INFO_AMP_CAPS;
1967 mutex_unlock(&codec->hash_mutex);
1968 return 0;
1969}
1970
1971/* query the value from the caps hash; if not found, fetch the current
1972 * value from the given function and store in the hash
1973 */
1974static unsigned int
1975query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1976 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1977{
1978 struct hda_amp_info *info;
1979 unsigned int val;
1980
1981 mutex_lock(&codec->hash_mutex);
1982 info = get_alloc_amp_hash(codec, key);
1983 if (!info) {
1984 mutex_unlock(&codec->hash_mutex);
1985 return 0;
1986 }
1987 if (!(info->head.val & INFO_AMP_CAPS)) {
1988 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1989 val = func(codec, nid, dir);
1990 write_caps_hash(codec, key, val);
1991 } else {
1992 val = info->amp_caps;
1993 mutex_unlock(&codec->hash_mutex);
1994 }
1995 return val;
1996}
1997
1998static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1999 int direction)
2000{
2001 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
2002 nid = codec->afg;
2003 return snd_hda_param_read(codec, nid,
2004 direction == HDA_OUTPUT ?
2005 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
2006}
2007
Takashi Iwaid5191e52009-11-16 14:58:17 +01002008/**
2009 * query_amp_caps - query AMP capabilities
2010 * @codec: the HD-auio codec
2011 * @nid: the NID to query
2012 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2013 *
2014 * Query AMP capabilities for the given widget and direction.
2015 * Returns the obtained capability bits.
2016 *
2017 * When cap bits have been already read, this doesn't read again but
2018 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01002020u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002022 return query_caps_hash(codec, nid, direction,
2023 HDA_HASH_KEY(nid, direction, 0),
2024 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002026EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Takashi Iwaid5191e52009-11-16 14:58:17 +01002028/**
David Henningsson861a04e2014-09-23 10:38:17 +02002029 * snd_hda_check_amp_caps - query AMP capabilities
2030 * @codec: the HD-audio codec
2031 * @nid: the NID to query
2032 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002033 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02002034 *
2035 * Check whether the widget has the given amp capability for the direction.
2036 */
2037bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2038 int dir, unsigned int bits)
2039{
2040 if (!nid)
2041 return false;
2042 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2043 if (query_amp_caps(codec, nid, dir) & bits)
2044 return true;
2045 return false;
2046}
2047EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2048
2049/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050 * snd_hda_override_amp_caps - Override the AMP capabilities
2051 * @codec: the CODEC to clean up
2052 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002053 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01002054 * @caps: the capability bits to set
2055 *
2056 * Override the cached AMP caps bits value by the given one.
2057 * This function is useful if the driver needs to adjust the AMP ranges,
2058 * e.g. limit to 0dB, etc.
2059 *
2060 * Returns zero if successful or a negative error code.
2061 */
Takashi Iwai897cc182007-05-29 19:01:37 +02002062int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2063 unsigned int caps)
2064{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002065 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002066}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002067EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02002068
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002069static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2070 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002071{
2072 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2073}
2074
Takashi Iwaid5191e52009-11-16 14:58:17 +01002075/**
2076 * snd_hda_query_pin_caps - Query PIN capabilities
2077 * @codec: the HD-auio codec
2078 * @nid: the NID to query
2079 *
2080 * Query PIN capabilities for the given widget.
2081 * Returns the obtained capability bits.
2082 *
2083 * When cap bits have been already read, this doesn't read again but
2084 * returns the cached value.
2085 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002086u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2087{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002088 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01002089 read_pin_cap);
2090}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002091EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01002092
Wu Fengguang864f92b2009-11-18 12:38:02 +08002093/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02002094 * snd_hda_override_pin_caps - Override the pin capabilities
2095 * @codec: the CODEC
2096 * @nid: the NID to override
2097 * @caps: the capability bits to set
2098 *
2099 * Override the cached PIN capabilitiy bits value by the given one.
2100 *
2101 * Returns zero if successful or a negative error code.
2102 */
2103int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2104 unsigned int caps)
2105{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002106 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002107}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002108EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02002109
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002110/* read or sync the hash value with the current value;
2111 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002113static struct hda_amp_info *
2114update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01002115 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002117 struct hda_amp_info *info;
2118 unsigned int parm, val = 0;
2119 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002121 retry:
2122 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2123 if (!info)
2124 return NULL;
2125 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2126 if (!val_read) {
2127 mutex_unlock(&codec->hash_mutex);
2128 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2129 parm |= direction == HDA_OUTPUT ?
2130 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2131 parm |= index;
2132 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002134 val &= 0xff;
2135 val_read = true;
2136 mutex_lock(&codec->hash_mutex);
2137 goto retry;
2138 }
2139 info->vol[ch] = val;
2140 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002141 } else if (init_only)
2142 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002143 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002147 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002149static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02002150 hda_nid_t nid, int ch, int direction, int index,
2151 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 u32 parm;
2154
2155 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2156 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2157 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002158 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2159 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01002160 ; /* set the zero value as a fake mute */
2161 else
2162 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2164}
2165
Takashi Iwaid5191e52009-11-16 14:58:17 +01002166/**
2167 * snd_hda_codec_amp_read - Read AMP value
2168 * @codec: HD-audio codec
2169 * @nid: NID to read the AMP value
2170 * @ch: channel (left=0 or right=1)
2171 * @direction: #HDA_INPUT or #HDA_OUTPUT
2172 * @index: the index value (only for input direction)
2173 *
2174 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
Takashi Iwai834be882006-03-01 14:16:17 +01002176int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2177 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002179 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002180 unsigned int val = 0;
2181
2182 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002183 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02002184 if (info)
2185 val = info->vol[ch];
2186 mutex_unlock(&codec->hash_mutex);
2187 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002189EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Takashi Iwai280e57d2012-12-14 10:32:21 +01002191static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2192 int direction, int idx, int mask, int val,
2193 bool init_only)
2194{
2195 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002196 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002197 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002198
2199 if (snd_BUG_ON(mask & ~0xff))
2200 mask &= 0xff;
2201 val &= mask;
2202
2203 mutex_lock(&codec->hash_mutex);
2204 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2205 if (!info) {
2206 mutex_unlock(&codec->hash_mutex);
2207 return 0;
2208 }
2209 val |= info->vol[ch] & ~mask;
2210 if (info->vol[ch] == val) {
2211 mutex_unlock(&codec->hash_mutex);
2212 return 0;
2213 }
2214 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002215 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002216 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002217 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002218 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002219 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002220 return 1;
2221}
2222
Takashi Iwaid5191e52009-11-16 14:58:17 +01002223/**
2224 * snd_hda_codec_amp_update - update the AMP value
2225 * @codec: HD-audio codec
2226 * @nid: NID to read the AMP value
2227 * @ch: channel (left=0 or right=1)
2228 * @direction: #HDA_INPUT or #HDA_OUTPUT
2229 * @idx: the index value (only for input direction)
2230 * @mask: bit mask to set
2231 * @val: the bits value to set
2232 *
2233 * Update the AMP value with a bit mask.
2234 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002235 */
Takashi Iwai834be882006-03-01 14:16:17 +01002236int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2237 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002239 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002241EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_codec_amp_stereo - update the AMP stereo values
2245 * @codec: HD-audio codec
2246 * @nid: NID to read the AMP value
2247 * @direction: #HDA_INPUT or #HDA_OUTPUT
2248 * @idx: the index value (only for input direction)
2249 * @mask: bit mask to set
2250 * @val: the bits value to set
2251 *
2252 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2253 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002254 */
2255int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2256 int direction, int idx, int mask, int val)
2257{
2258 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002259
2260 if (snd_BUG_ON(mask & ~0xff))
2261 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002262 for (ch = 0; ch < 2; ch++)
2263 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2264 idx, mask, val);
2265 return ret;
2266}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002267EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002268
Takashi Iwai95a962c2014-10-29 16:03:58 +01002269/**
2270 * snd_hda_codec_amp_init - initialize the AMP value
2271 * @codec: the HDA codec
2272 * @nid: NID to read the AMP value
2273 * @ch: channel (left=0 or right=1)
2274 * @dir: #HDA_INPUT or #HDA_OUTPUT
2275 * @idx: the index value (only for input direction)
2276 * @mask: bit mask to set
2277 * @val: the bits value to set
2278 *
2279 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002280 * the first access. If the amp was already initialized / updated beforehand,
2281 * this does nothing.
2282 */
2283int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2284 int dir, int idx, int mask, int val)
2285{
2286 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2287}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002288EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002289
Takashi Iwai95a962c2014-10-29 16:03:58 +01002290/**
2291 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2292 * @codec: the HDA codec
2293 * @nid: NID to read the AMP value
2294 * @dir: #HDA_INPUT or #HDA_OUTPUT
2295 * @idx: the index value (only for input direction)
2296 * @mask: bit mask to set
2297 * @val: the bits value to set
2298 *
2299 * Call snd_hda_codec_amp_init() for both stereo channels.
2300 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002301int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2302 int dir, int idx, int mask, int val)
2303{
2304 int ch, ret = 0;
2305
2306 if (snd_BUG_ON(mask & ~0xff))
2307 mask &= 0xff;
2308 for (ch = 0; ch < 2; ch++)
2309 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2310 idx, mask, val);
2311 return ret;
2312}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002313EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002314
Takashi Iwaid5191e52009-11-16 14:58:17 +01002315/**
2316 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2317 * @codec: HD-audio codec
2318 *
2319 * Resume the all amp commands from the cache.
2320 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002321void snd_hda_codec_resume_amp(struct hda_codec *codec)
2322{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002323 int i;
2324
Takashi Iwaic370dd62012-12-13 18:30:04 +01002325 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002326 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002327 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2328 struct hda_amp_info *buffer;
2329 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002330 hda_nid_t nid;
2331 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002332 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002333
2334 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002335 if (!buffer->head.dirty)
2336 continue;
2337 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002338 info = *buffer;
2339 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002340 if (!key)
2341 continue;
2342 nid = key & 0xff;
2343 idx = (key >> 16) & 0xff;
2344 dir = (key >> 24) & 0xff;
2345 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002346 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002347 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002348 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002349 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2350 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002351 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002352 }
2353 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002354 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002355}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002356EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002358static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2359 unsigned int ofs)
2360{
2361 u32 caps = query_amp_caps(codec, nid, dir);
2362 /* get num steps */
2363 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2364 if (ofs < caps)
2365 caps -= ofs;
2366 return caps;
2367}
2368
Takashi Iwaid5191e52009-11-16 14:58:17 +01002369/**
2370 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002371 * @kcontrol: referred ctl element
2372 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002373 *
2374 * The control element is supposed to have the private_value field
2375 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2376 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002377int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2381 u16 nid = get_amp_nid(kcontrol);
2382 u8 chs = get_amp_channels(kcontrol);
2383 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002384 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002386 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2387 uinfo->count = chs == 3 ? 2 : 1;
2388 uinfo->value.integer.min = 0;
2389 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2390 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002391 codec_warn(codec,
2392 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2393 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return -EINVAL;
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return 0;
2397}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002398EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002400
2401static inline unsigned int
2402read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2403 int ch, int dir, int idx, unsigned int ofs)
2404{
2405 unsigned int val;
2406 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2407 val &= HDA_AMP_VOLMASK;
2408 if (val >= ofs)
2409 val -= ofs;
2410 else
2411 val = 0;
2412 return val;
2413}
2414
2415static inline int
2416update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2417 int ch, int dir, int idx, unsigned int ofs,
2418 unsigned int val)
2419{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002420 unsigned int maxval;
2421
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002422 if (val > 0)
2423 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002424 /* ofs = 0: raw max value */
2425 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002426 if (val > maxval)
2427 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002428 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2429 HDA_AMP_VOLMASK, val);
2430}
2431
Takashi Iwaid5191e52009-11-16 14:58:17 +01002432/**
2433 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002434 * @kcontrol: ctl element
2435 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002436 *
2437 * The control element is supposed to have the private_value field
2438 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2439 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002440int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2444 hda_nid_t nid = get_amp_nid(kcontrol);
2445 int chs = get_amp_channels(kcontrol);
2446 int dir = get_amp_direction(kcontrol);
2447 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002448 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 long *valp = ucontrol->value.integer.value;
2450
2451 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002452 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002454 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 0;
2456}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002457EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Takashi Iwaid5191e52009-11-16 14:58:17 +01002459/**
2460 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002461 * @kcontrol: ctl element
2462 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002463 *
2464 * The control element is supposed to have the private_value field
2465 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2466 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002467int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2471 hda_nid_t nid = get_amp_nid(kcontrol);
2472 int chs = get_amp_channels(kcontrol);
2473 int dir = get_amp_direction(kcontrol);
2474 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002475 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 long *valp = ucontrol->value.integer.value;
2477 int change = 0;
2478
Takashi Iwaicb53c622007-08-10 17:21:45 +02002479 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002480 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002481 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002482 valp++;
2483 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002484 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002485 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002486 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return change;
2488}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002489EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Takashi Iwaid5191e52009-11-16 14:58:17 +01002491/**
2492 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002493 * @kcontrol: ctl element
2494 * @op_flag: operation flag
2495 * @size: byte size of input TLV
2496 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002497 *
2498 * The control element is supposed to have the private_value field
2499 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2500 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002501int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2502 unsigned int size, unsigned int __user *_tlv)
2503{
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 hda_nid_t nid = get_amp_nid(kcontrol);
2506 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002507 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002508 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002509 u32 caps, val1, val2;
2510
2511 if (size < 4 * sizeof(unsigned int))
2512 return -ENOMEM;
2513 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002514 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2515 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002516 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002517 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002518 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002519 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002520 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002521 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2522 return -EFAULT;
2523 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2524 return -EFAULT;
2525 if (put_user(val1, _tlv + 2))
2526 return -EFAULT;
2527 if (put_user(val2, _tlv + 3))
2528 return -EFAULT;
2529 return 0;
2530}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002531EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002532
Takashi Iwaid5191e52009-11-16 14:58:17 +01002533/**
2534 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2535 * @codec: HD-audio codec
2536 * @nid: NID of a reference widget
2537 * @dir: #HDA_INPUT or #HDA_OUTPUT
2538 * @tlv: TLV data to be stored, at least 4 elements
2539 *
2540 * Set (static) TLV data for a virtual master volume using the AMP caps
2541 * obtained from the reference NID.
2542 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002543 */
2544void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2545 unsigned int *tlv)
2546{
2547 u32 caps;
2548 int nums, step;
2549
2550 caps = query_amp_caps(codec, nid, dir);
2551 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2552 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2553 step = (step + 1) * 25;
2554 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2555 tlv[1] = 2 * sizeof(unsigned int);
2556 tlv[2] = -nums * step;
2557 tlv[3] = step;
2558}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002559EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002560
2561/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002562static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002563find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002564{
2565 struct snd_ctl_elem_id id;
2566 memset(&id, 0, sizeof(id));
2567 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002568 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002569 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002570 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2571 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002572 strcpy(id.name, name);
2573 return snd_ctl_find_id(codec->bus->card, &id);
2574}
2575
Takashi Iwaid5191e52009-11-16 14:58:17 +01002576/**
2577 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2578 * @codec: HD-audio codec
2579 * @name: ctl id name string
2580 *
2581 * Get the control element with the given id string and IFACE_MIXER.
2582 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002583struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2584 const char *name)
2585{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002586 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002587}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002588EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002589
Takashi Iwaidcda5802012-10-12 17:24:51 +02002590static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002591 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002592{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002593 int i, idx;
2594 /* 16 ctlrs should be large enough */
2595 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2596 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002597 return idx;
2598 }
2599 return -EBUSY;
2600}
2601
Takashi Iwaid5191e52009-11-16 14:58:17 +01002602/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002603 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002604 * @codec: HD-audio codec
2605 * @nid: corresponding NID (optional)
2606 * @kctl: the control element to assign
2607 *
2608 * Add the given control element to an array inside the codec instance.
2609 * All control elements belonging to a codec are supposed to be added
2610 * by this function so that a proper clean-up works at the free or
2611 * reconfiguration time.
2612 *
2613 * If non-zero @nid is passed, the NID is assigned to the control element.
2614 * The assignment is shown in the codec proc file.
2615 *
2616 * snd_hda_ctl_add() checks the control subdev id field whether
2617 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002618 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2619 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002620 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002621int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2622 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002623{
2624 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002625 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002626 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002627
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002628 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002629 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002630 if (nid == 0)
2631 nid = get_amp_nid_(kctl->private_value);
2632 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002633 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2634 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002635 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002636 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002637 err = snd_ctl_add(codec->bus->card, kctl);
2638 if (err < 0)
2639 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002640 item = snd_array_new(&codec->mixers);
2641 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002642 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002643 item->kctl = kctl;
2644 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002645 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002646 return 0;
2647}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002648EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002649
Takashi Iwaid5191e52009-11-16 14:58:17 +01002650/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002651 * snd_hda_add_nid - Assign a NID to a control element
2652 * @codec: HD-audio codec
2653 * @nid: corresponding NID (optional)
2654 * @kctl: the control element to assign
2655 * @index: index to kctl
2656 *
2657 * Add the given control element to an array inside the codec instance.
2658 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2659 * NID:KCTL mapping - for example "Capture Source" selector.
2660 */
2661int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2662 unsigned int index, hda_nid_t nid)
2663{
2664 struct hda_nid_item *item;
2665
2666 if (nid > 0) {
2667 item = snd_array_new(&codec->nids);
2668 if (!item)
2669 return -ENOMEM;
2670 item->kctl = kctl;
2671 item->index = index;
2672 item->nid = nid;
2673 return 0;
2674 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002675 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2676 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002677 return -EINVAL;
2678}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002679EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002680
2681/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002682 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2683 * @codec: HD-audio codec
2684 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002685void snd_hda_ctls_clear(struct hda_codec *codec)
2686{
2687 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002688 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002689 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002690 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002691 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002692 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002693}
2694
Takashi Iwai95a962c2014-10-29 16:03:58 +01002695/**
2696 * snd_hda_lock_devices - pseudo device locking
2697 * @bus: the BUS
2698 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002699 * toggle card->shutdown to allow/disallow the device access (as a hack)
2700 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002701int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002702{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002703 struct snd_card *card = bus->card;
2704 struct hda_codec *codec;
2705
Takashi Iwaia65d6292009-02-23 16:57:04 +01002706 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002707 if (card->shutdown)
2708 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002709 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002710 if (!list_empty(&card->ctl_files))
2711 goto err_clear;
2712
2713 list_for_each_entry(codec, &bus->codec_list, list) {
2714 int pcm;
2715 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2716 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2717 if (!cpcm->pcm)
2718 continue;
2719 if (cpcm->pcm->streams[0].substream_opened ||
2720 cpcm->pcm->streams[1].substream_opened)
2721 goto err_clear;
2722 }
2723 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002724 spin_unlock(&card->files_lock);
2725 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002726
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002727 err_clear:
2728 card->shutdown = 0;
2729 err_unlock:
2730 spin_unlock(&card->files_lock);
2731 return -EINVAL;
2732}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002733EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002734
Takashi Iwai95a962c2014-10-29 16:03:58 +01002735/**
2736 * snd_hda_unlock_devices - pseudo device unlocking
2737 * @bus: the BUS
2738 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002739void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002740{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002741 struct snd_card *card = bus->card;
2742
2743 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002744 spin_lock(&card->files_lock);
2745 card->shutdown = 0;
2746 spin_unlock(&card->files_lock);
2747}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002748EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002749
Takashi Iwaid5191e52009-11-16 14:58:17 +01002750/**
2751 * snd_hda_codec_reset - Clear all objects assigned to the codec
2752 * @codec: HD-audio codec
2753 *
2754 * This frees the all PCM and control elements assigned to the codec, and
2755 * clears the caches and restores the pin default configurations.
2756 *
2757 * When a device is being used, it returns -EBSY. If successfully freed,
2758 * returns zero.
2759 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002760int snd_hda_codec_reset(struct hda_codec *codec)
2761{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002762 struct hda_bus *bus = codec->bus;
2763 struct snd_card *card = bus->card;
2764 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002765
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002766 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002767 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002768
2769 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002770 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002771#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002772 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002773 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002774#endif
2775 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002776 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002777 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002778 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002779 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002780 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002781 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002782 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002783 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002784 snd_hda_detach_beep_device(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002785 if (codec->patch_ops.free)
2786 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002787 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002788 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002789 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002790 codec->spec = NULL;
2791 free_hda_cache(&codec->amp_cache);
2792 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002793 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2794 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002795 /* free only driver_pins so that init_pins + user_pins are restored */
2796 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002797 snd_array_free(&codec->cvt_setups);
2798 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002799 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002800 codec->num_pcms = 0;
2801 codec->pcm_info = NULL;
2802 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002803 codec->slave_dig_outs = NULL;
2804 codec->spdif_status_reset = 0;
Takashi Iwaib21bdd02013-11-18 12:03:56 +01002805 unload_parser(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002806 module_put(codec->owner);
2807 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002808
2809 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002810 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002811 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002812}
2813
Takashi Iwai6194b992014-06-06 18:12:16 +02002814typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002815
2816/* apply the function to all matching slave ctls in the mixer list */
2817static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002818 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002819{
2820 struct hda_nid_item *items;
2821 const char * const *s;
2822 int i, err;
2823
2824 items = codec->mixers.list;
2825 for (i = 0; i < codec->mixers.used; i++) {
2826 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002827 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002828 continue;
2829 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002830 char tmpname[sizeof(sctl->id.name)];
2831 const char *name = *s;
2832 if (suffix) {
2833 snprintf(tmpname, sizeof(tmpname), "%s %s",
2834 name, suffix);
2835 name = tmpname;
2836 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002837 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002838 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002839 if (err)
2840 return err;
2841 break;
2842 }
2843 }
2844 }
2845 return 0;
2846}
2847
Takashi Iwai6194b992014-06-06 18:12:16 +02002848static int check_slave_present(struct hda_codec *codec,
2849 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002850{
2851 return 1;
2852}
2853
Takashi Iwai18478e82012-03-09 17:51:10 +01002854/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002855static int get_kctl_0dB_offset(struct hda_codec *codec,
2856 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002857{
2858 int _tlv[4];
2859 const int *tlv = NULL;
2860 int val = -1;
2861
2862 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2863 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2864 mm_segment_t fs = get_fs();
2865 set_fs(get_ds());
2866 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2867 tlv = _tlv;
2868 set_fs(fs);
2869 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2870 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002871 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2872 int step = tlv[3];
2873 step &= ~TLV_DB_SCALE_MUTE;
2874 if (!step)
2875 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002876 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002877 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002878- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002879 return -1;
2880 }
2881 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002882 val = -tlv[2] / step;
2883 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002884 return val;
2885}
2886
2887/* call kctl->put with the given value(s) */
2888static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2889{
2890 struct snd_ctl_elem_value *ucontrol;
2891 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2892 if (!ucontrol)
2893 return -ENOMEM;
2894 ucontrol->value.integer.value[0] = val;
2895 ucontrol->value.integer.value[1] = val;
2896 kctl->put(kctl, ucontrol);
2897 kfree(ucontrol);
2898 return 0;
2899}
2900
2901/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002902static int init_slave_0dB(struct hda_codec *codec,
2903 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002904{
Takashi Iwai6194b992014-06-06 18:12:16 +02002905 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002906 if (offset > 0)
2907 put_kctl_with_value(slave, offset);
2908 return 0;
2909}
2910
2911/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002912static int init_slave_unmute(struct hda_codec *codec,
2913 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002914{
2915 return put_kctl_with_value(slave, 1);
2916}
2917
Takashi Iwaie8750942014-06-30 14:02:39 +02002918static int add_slave(struct hda_codec *codec,
2919 void *data, struct snd_kcontrol *slave)
2920{
2921 return snd_ctl_add_slave(data, slave);
2922}
2923
Takashi Iwaid5191e52009-11-16 14:58:17 +01002924/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002925 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002926 * @codec: HD-audio codec
2927 * @name: vmaster control name
2928 * @tlv: TLV data (optional)
2929 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002930 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002931 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002932 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002933 *
2934 * Create a virtual master control with the given name. The TLV data
2935 * must be either NULL or a valid data.
2936 *
2937 * @slaves is a NULL-terminated array of strings, each of which is a
2938 * slave control name. All controls with these names are assigned to
2939 * the new virtual master control.
2940 *
2941 * This function returns zero if successful or a negative error code.
2942 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002943int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002944 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002945 const char *suffix, bool init_slave_vol,
2946 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002947{
2948 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002949 int err;
2950
Takashi Iwai29e58532012-03-12 12:25:03 +01002951 if (ctl_ret)
2952 *ctl_ret = NULL;
2953
Takashi Iwai9322ca52012-02-03 14:28:01 +01002954 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002955 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002956 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002957 return 0;
2958 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002959 kctl = snd_ctl_make_virtual_master(name, tlv);
2960 if (!kctl)
2961 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002962 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002963 if (err < 0)
2964 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002965
Takashi Iwaie8750942014-06-30 14:02:39 +02002966 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002967 if (err < 0)
2968 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002969
2970 /* init with master mute & zero volume */
2971 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002972 if (init_slave_vol) {
2973 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002974 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002975 tlv ? init_slave_0dB : init_slave_unmute, &step);
2976 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002977
Takashi Iwai29e58532012-03-12 12:25:03 +01002978 if (ctl_ret)
2979 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002980 return 0;
2981}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002982EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002983
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002984/*
2985 * mute-LED control using vmaster
2986 */
2987static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_info *uinfo)
2989{
2990 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002991 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002992 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002993
Takashi Iwai3ff72212014-10-20 18:17:28 +02002994 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002995}
2996
2997static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
2999{
3000 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3001 ucontrol->value.enumerated.item[0] = hook->mute_mode;
3002 return 0;
3003}
3004
3005static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
3007{
3008 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3009 unsigned int old_mode = hook->mute_mode;
3010
3011 hook->mute_mode = ucontrol->value.enumerated.item[0];
3012 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
3013 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3014 if (old_mode == hook->mute_mode)
3015 return 0;
3016 snd_hda_sync_vmaster_hook(hook);
3017 return 1;
3018}
3019
3020static struct snd_kcontrol_new vmaster_mute_mode = {
3021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3022 .name = "Mute-LED Mode",
3023 .info = vmaster_mute_mode_info,
3024 .get = vmaster_mute_mode_get,
3025 .put = vmaster_mute_mode_put,
3026};
3027
Takashi Iwai95a962c2014-10-29 16:03:58 +01003028/**
3029 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3030 * @codec: the HDA codec
3031 * @hook: the vmaster hook object
3032 * @expose_enum_ctl: flag to create an enum ctl
3033 *
3034 * Add a mute-LED hook with the given vmaster switch kctl.
3035 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3036 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003037 */
3038int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01003039 struct hda_vmaster_mute_hook *hook,
3040 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003041{
3042 struct snd_kcontrol *kctl;
3043
3044 if (!hook->hook || !hook->sw_kctl)
3045 return 0;
3046 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3047 hook->codec = codec;
3048 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01003049 if (!expose_enum_ctl)
3050 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003051 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3052 if (!kctl)
3053 return -ENOMEM;
3054 return snd_hda_ctl_add(codec, 0, kctl);
3055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003056EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003057
Takashi Iwai95a962c2014-10-29 16:03:58 +01003058/**
3059 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3060 * @hook: the vmaster hook
3061 *
3062 * Call the hook with the current value for synchronization.
3063 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003064 */
3065void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3066{
3067 if (!hook->hook || !hook->codec)
3068 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02003069 /* don't call vmaster hook in the destructor since it might have
3070 * been already destroyed
3071 */
3072 if (hook->codec->bus->shutdown)
3073 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003074 switch (hook->mute_mode) {
3075 case HDA_VMUTE_FOLLOW_MASTER:
3076 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3077 break;
3078 default:
3079 hook->hook(hook->codec, hook->mute_mode);
3080 break;
3081 }
3082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003083EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01003084
3085
Takashi Iwaid5191e52009-11-16 14:58:17 +01003086/**
3087 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003088 * @kcontrol: referred ctl element
3089 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003090 *
3091 * The control element is supposed to have the private_value field
3092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003094int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 int chs = get_amp_channels(kcontrol);
3098
3099 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3100 uinfo->count = chs == 3 ? 2 : 1;
3101 uinfo->value.integer.min = 0;
3102 uinfo->value.integer.max = 1;
3103 return 0;
3104}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003105EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Takashi Iwaid5191e52009-11-16 14:58:17 +01003107/**
3108 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003109 * @kcontrol: ctl element
3110 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003111 *
3112 * The control element is supposed to have the private_value field
3113 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3114 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3116 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3119 hda_nid_t nid = get_amp_nid(kcontrol);
3120 int chs = get_amp_channels(kcontrol);
3121 int dir = get_amp_direction(kcontrol);
3122 int idx = get_amp_index(kcontrol);
3123 long *valp = ucontrol->value.integer.value;
3124
3125 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003126 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003127 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003129 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02003130 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return 0;
3132}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003133EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwaid5191e52009-11-16 14:58:17 +01003135/**
3136 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003137 * @kcontrol: ctl element
3138 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003139 *
3140 * The control element is supposed to have the private_value field
3141 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3142 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3144 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3147 hda_nid_t nid = get_amp_nid(kcontrol);
3148 int chs = get_amp_channels(kcontrol);
3149 int dir = get_amp_direction(kcontrol);
3150 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 long *valp = ucontrol->value.integer.value;
3152 int change = 0;
3153
Takashi Iwaicb53c622007-08-10 17:21:45 +02003154 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003155 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003156 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003157 HDA_AMP_MUTE,
3158 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02003159 valp++;
3160 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02003161 if (chs & 2)
3162 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02003163 HDA_AMP_MUTE,
3164 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02003165 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003166 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return change;
3168}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003169EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
3171/*
Takashi Iwai985be542005-11-02 18:26:49 +01003172 * bound volume controls
3173 *
3174 * bind multiple volumes (# indices, from 0)
3175 */
3176
3177#define AMP_VAL_IDX_SHIFT 19
3178#define AMP_VAL_IDX_MASK (0x0f<<19)
3179
Takashi Iwaid5191e52009-11-16 14:58:17 +01003180/**
3181 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003182 * @kcontrol: ctl element
3183 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003184 *
3185 * The control element is supposed to have the private_value field
3186 * set up via HDA_BIND_MUTE*() macros.
3187 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003188int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 unsigned long pval;
3193 int err;
3194
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003195 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003196 pval = kcontrol->private_value;
3197 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3198 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3199 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003200 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003201 return err;
3202}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003203EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01003204
Takashi Iwaid5191e52009-11-16 14:58:17 +01003205/**
3206 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003207 * @kcontrol: ctl element
3208 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003209 *
3210 * The control element is supposed to have the private_value field
3211 * set up via HDA_BIND_MUTE*() macros.
3212 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003213int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01003215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3217 unsigned long pval;
3218 int i, indices, err = 0, change = 0;
3219
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003220 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003221 pval = kcontrol->private_value;
3222 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3223 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003224 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3225 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01003226 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3227 if (err < 0)
3228 break;
3229 change |= err;
3230 }
3231 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003232 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01003233 return err < 0 ? err : change;
3234}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003235EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01003236
Takashi Iwaid5191e52009-11-16 14:58:17 +01003237/**
3238 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003239 * @kcontrol: referred ctl element
3240 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003241 *
3242 * The control element is supposed to have the private_value field
3243 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02003244 */
3245int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3246 struct snd_ctl_elem_info *uinfo)
3247{
3248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3249 struct hda_bind_ctls *c;
3250 int err;
3251
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003252 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003253 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003254 kcontrol->private_value = *c->values;
3255 err = c->ops->info(kcontrol, uinfo);
3256 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003257 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003258 return err;
3259}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003260EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02003261
Takashi Iwaid5191e52009-11-16 14:58:17 +01003262/**
3263 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003264 * @kcontrol: ctl element
3265 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003266 *
3267 * The control element is supposed to have the private_value field
3268 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3269 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003270int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3271 struct snd_ctl_elem_value *ucontrol)
3272{
3273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3274 struct hda_bind_ctls *c;
3275 int err;
3276
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003277 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003278 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003279 kcontrol->private_value = *c->values;
3280 err = c->ops->get(kcontrol, ucontrol);
3281 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003282 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003283 return err;
3284}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003285EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003286
Takashi Iwaid5191e52009-11-16 14:58:17 +01003287/**
3288 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003289 * @kcontrol: ctl element
3290 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003291 *
3292 * The control element is supposed to have the private_value field
3293 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3294 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003295int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3296 struct snd_ctl_elem_value *ucontrol)
3297{
3298 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3299 struct hda_bind_ctls *c;
3300 unsigned long *vals;
3301 int err = 0, change = 0;
3302
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003303 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003304 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003305 for (vals = c->values; *vals; vals++) {
3306 kcontrol->private_value = *vals;
3307 err = c->ops->put(kcontrol, ucontrol);
3308 if (err < 0)
3309 break;
3310 change |= err;
3311 }
3312 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003313 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003314 return err < 0 ? err : change;
3315}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003316EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003317
Takashi Iwaid5191e52009-11-16 14:58:17 +01003318/**
3319 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003320 * @kcontrol: ctl element
3321 * @op_flag: operation flag
3322 * @size: byte size of input TLV
3323 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003324 *
3325 * The control element is supposed to have the private_value field
3326 * set up via HDA_BIND_VOL() macro.
3327 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003328int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3329 unsigned int size, unsigned int __user *tlv)
3330{
3331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3332 struct hda_bind_ctls *c;
3333 int err;
3334
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003335 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003336 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003337 kcontrol->private_value = *c->values;
3338 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3339 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003340 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003341 return err;
3342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003343EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003344
3345struct hda_ctl_ops snd_hda_bind_vol = {
3346 .info = snd_hda_mixer_amp_volume_info,
3347 .get = snd_hda_mixer_amp_volume_get,
3348 .put = snd_hda_mixer_amp_volume_put,
3349 .tlv = snd_hda_mixer_amp_tlv
3350};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003351EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003352
3353struct hda_ctl_ops snd_hda_bind_sw = {
3354 .info = snd_hda_mixer_amp_switch_info,
3355 .get = snd_hda_mixer_amp_switch_get,
3356 .put = snd_hda_mixer_amp_switch_put,
3357 .tlv = snd_hda_mixer_amp_tlv
3358};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003359EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003360
3361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 * SPDIF out controls
3363 */
3364
Takashi Iwai0ba21762007-04-16 11:29:14 +02003365static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
3368 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3369 uinfo->count = 1;
3370 return 0;
3371}
3372
Takashi Iwai0ba21762007-04-16 11:29:14 +02003373static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3377 IEC958_AES0_NONAUDIO |
3378 IEC958_AES0_CON_EMPHASIS_5015 |
3379 IEC958_AES0_CON_NOT_COPYRIGHT;
3380 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3381 IEC958_AES1_CON_ORIGINAL;
3382 return 0;
3383}
3384
Takashi Iwai0ba21762007-04-16 11:29:14 +02003385static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
3388 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3389 IEC958_AES0_NONAUDIO |
3390 IEC958_AES0_PRO_EMPHASIS_5015;
3391 return 0;
3392}
3393
Takashi Iwai0ba21762007-04-16 11:29:14 +02003394static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3395 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003398 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003399 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003401 mutex_lock(&codec->spdif_mutex);
3402 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003403 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3404 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3405 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3406 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003407 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 return 0;
3410}
3411
3412/* convert from SPDIF status bits to HDA SPDIF bits
3413 * bit 0 (DigEn) is always set zero (to be filled later)
3414 */
3415static unsigned short convert_from_spdif_status(unsigned int sbits)
3416{
3417 unsigned short val = 0;
3418
3419 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003420 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003422 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003424 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3425 IEC958_AES0_PRO_EMPHASIS_5015)
3426 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003428 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3429 IEC958_AES0_CON_EMPHASIS_5015)
3430 val |= AC_DIG1_EMPHASIS;
3431 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3432 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003434 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3436 }
3437 return val;
3438}
3439
3440/* convert to SPDIF status bits from HDA SPDIF bits
3441 */
3442static unsigned int convert_to_spdif_status(unsigned short val)
3443{
3444 unsigned int sbits = 0;
3445
Takashi Iwai0ba21762007-04-16 11:29:14 +02003446 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003448 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 sbits |= IEC958_AES0_PROFESSIONAL;
3450 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003451 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3453 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003454 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003456 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003458 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3460 sbits |= val & (0x7f << 8);
3461 }
3462 return sbits;
3463}
3464
Takashi Iwai2f728532008-09-25 16:32:41 +02003465/* set digital convert verbs both for the given NID and its slaves */
3466static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3467 int verb, int val)
3468{
Takashi Iwaidda14412011-05-02 11:29:30 +02003469 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003470
Takashi Iwai9e976972008-11-25 08:17:20 +01003471 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003472 d = codec->slave_dig_outs;
3473 if (!d)
3474 return;
3475 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003476 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003477}
3478
3479static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3480 int dig1, int dig2)
3481{
3482 if (dig1 != -1)
3483 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3484 if (dig2 != -1)
3485 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3486}
3487
Takashi Iwai0ba21762007-04-16 11:29:14 +02003488static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3489 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
3491 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003492 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003493 struct hda_spdif_out *spdif;
3494 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 unsigned short val;
3496 int change;
3497
Ingo Molnar62932df2006-01-16 16:34:20 +01003498 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003499 spdif = snd_array_elem(&codec->spdif_out, idx);
3500 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003501 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3503 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3504 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003505 val = convert_from_spdif_status(spdif->status);
3506 val |= spdif->ctls & 1;
3507 change = spdif->ctls != val;
3508 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003509 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003510 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003511 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return change;
3513}
3514
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003515#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Takashi Iwai0ba21762007-04-16 11:29:14 +02003517static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3518 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003521 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003522 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003524 mutex_lock(&codec->spdif_mutex);
3525 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003526 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003527 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return 0;
3529}
3530
Stephen Warren74b654c2011-06-01 11:14:18 -06003531static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3532 int dig1, int dig2)
3533{
3534 set_dig_out_convert(codec, nid, dig1, dig2);
3535 /* unmute amp switch (if any) */
3536 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3537 (dig1 & AC_DIG1_ENABLE))
3538 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3539 HDA_AMP_MUTE, 0);
3540}
3541
Takashi Iwai0ba21762007-04-16 11:29:14 +02003542static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003546 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003547 struct hda_spdif_out *spdif;
3548 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 unsigned short val;
3550 int change;
3551
Ingo Molnar62932df2006-01-16 16:34:20 +01003552 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003553 spdif = snd_array_elem(&codec->spdif_out, idx);
3554 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003555 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003557 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003558 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003559 spdif->ctls = val;
3560 if (change && nid != (u16)-1)
3561 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003562 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return change;
3564}
3565
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003566static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 {
3568 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003570 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 .info = snd_hda_spdif_mask_info,
3572 .get = snd_hda_spdif_cmask_get,
3573 },
3574 {
3575 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3576 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003577 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 .info = snd_hda_spdif_mask_info,
3579 .get = snd_hda_spdif_pmask_get,
3580 },
3581 {
3582 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003583 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 .info = snd_hda_spdif_mask_info,
3585 .get = snd_hda_spdif_default_get,
3586 .put = snd_hda_spdif_default_put,
3587 },
3588 {
3589 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003590 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 .info = snd_hda_spdif_out_switch_info,
3592 .get = snd_hda_spdif_out_switch_get,
3593 .put = snd_hda_spdif_out_switch_put,
3594 },
3595 { } /* end */
3596};
3597
3598/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003599 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003601 * @associated_nid: NID that new ctls associated with
3602 * @cvt_nid: converter NID
3603 * @type: HDA_PCM_TYPE_*
3604 * Creates controls related with the digital output.
3605 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 *
3607 * Returns 0 if successful, or a negative error code.
3608 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003609int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3610 hda_nid_t associated_nid,
3611 hda_nid_t cvt_nid,
3612 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003615 struct snd_kcontrol *kctl;
3616 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003617 int idx = 0;
3618 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003619 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003620 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003622 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003623 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003624 idx = spdif_index;
3625 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003626 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003627 /* suppose a single SPDIF device */
3628 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3629 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3630 if (!kctl)
3631 break;
3632 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003633 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003634 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003635 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003636 if (!bus->primary_dig_out_type)
3637 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003638
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003639 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003640 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003641 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003642 return -EBUSY;
3643 }
Stephen Warren7c935972011-06-01 11:14:17 -06003644 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003645 if (!spdif)
3646 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3648 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003649 if (!kctl)
3650 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003651 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003652 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003653 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003654 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return err;
3656 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003657 spdif->nid = cvt_nid;
3658 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003659 AC_VERB_GET_DIGI_CONVERT_1, 0);
3660 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return 0;
3662}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003663EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Takashi Iwai95a962c2014-10-29 16:03:58 +01003665/**
3666 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3667 * @codec: the HDA codec
3668 * @nid: widget NID
3669 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003670 * call within spdif_mutex lock
3671 */
Stephen Warren7c935972011-06-01 11:14:17 -06003672struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3673 hda_nid_t nid)
3674{
3675 int i;
3676 for (i = 0; i < codec->spdif_out.used; i++) {
3677 struct hda_spdif_out *spdif =
3678 snd_array_elem(&codec->spdif_out, i);
3679 if (spdif->nid == nid)
3680 return spdif;
3681 }
3682 return NULL;
3683}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003684EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003685
Takashi Iwai95a962c2014-10-29 16:03:58 +01003686/**
3687 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3688 * @codec: the HDA codec
3689 * @idx: the SPDIF ctl index
3690 *
3691 * Unassign the widget from the given SPDIF control.
3692 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003693void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3694{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003695 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003696
3697 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003698 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003699 spdif->nid = (u16)-1;
3700 mutex_unlock(&codec->spdif_mutex);
3701}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003702EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003703
Takashi Iwai95a962c2014-10-29 16:03:58 +01003704/**
3705 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3706 * @codec: the HDA codec
3707 * @idx: the SPDIF ctl idx
3708 * @nid: widget NID
3709 *
3710 * Assign the widget to the SPDIF control with the given index.
3711 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003712void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3713{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003714 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003715 unsigned short val;
3716
3717 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003718 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003719 if (spdif->nid != nid) {
3720 spdif->nid = nid;
3721 val = spdif->ctls;
3722 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3723 }
3724 mutex_unlock(&codec->spdif_mutex);
3725}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003726EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003729 * SPDIF sharing with analog output
3730 */
3731static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3732 struct snd_ctl_elem_value *ucontrol)
3733{
3734 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3735 ucontrol->value.integer.value[0] = mout->share_spdif;
3736 return 0;
3737}
3738
3739static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3740 struct snd_ctl_elem_value *ucontrol)
3741{
3742 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3743 mout->share_spdif = !!ucontrol->value.integer.value[0];
3744 return 0;
3745}
3746
3747static struct snd_kcontrol_new spdif_share_sw = {
3748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3749 .name = "IEC958 Default PCM Playback Switch",
3750 .info = snd_ctl_boolean_mono_info,
3751 .get = spdif_share_sw_get,
3752 .put = spdif_share_sw_put,
3753};
3754
Takashi Iwaid5191e52009-11-16 14:58:17 +01003755/**
3756 * snd_hda_create_spdif_share_sw - create Default PCM switch
3757 * @codec: the HDA codec
3758 * @mout: multi-out instance
3759 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003760int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3761 struct hda_multi_out *mout)
3762{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003763 struct snd_kcontrol *kctl;
3764
Takashi Iwai9a081602008-02-12 18:37:26 +01003765 if (!mout->dig_out_nid)
3766 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003767
3768 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3769 if (!kctl)
3770 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003771 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003772 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003773}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003774EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003775
3776/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 * SPDIF input
3778 */
3779
3780#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3781
Takashi Iwai0ba21762007-04-16 11:29:14 +02003782static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3783 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3786
3787 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3788 return 0;
3789}
3790
Takashi Iwai0ba21762007-04-16 11:29:14 +02003791static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3792 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3795 hda_nid_t nid = kcontrol->private_value;
3796 unsigned int val = !!ucontrol->value.integer.value[0];
3797 int change;
3798
Ingo Molnar62932df2006-01-16 16:34:20 +01003799 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003801 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003803 snd_hda_codec_write_cache(codec, nid, 0,
3804 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003806 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return change;
3808}
3809
Takashi Iwai0ba21762007-04-16 11:29:14 +02003810static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3811 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3814 hda_nid_t nid = kcontrol->private_value;
3815 unsigned short val;
3816 unsigned int sbits;
3817
Andrew Paprocki3982d172007-12-19 12:13:44 +01003818 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 sbits = convert_to_spdif_status(val);
3820 ucontrol->value.iec958.status[0] = sbits;
3821 ucontrol->value.iec958.status[1] = sbits >> 8;
3822 ucontrol->value.iec958.status[2] = sbits >> 16;
3823 ucontrol->value.iec958.status[3] = sbits >> 24;
3824 return 0;
3825}
3826
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003827static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 {
3829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003830 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 .info = snd_hda_spdif_in_switch_info,
3832 .get = snd_hda_spdif_in_switch_get,
3833 .put = snd_hda_spdif_in_switch_put,
3834 },
3835 {
3836 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003838 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 .info = snd_hda_spdif_mask_info,
3840 .get = snd_hda_spdif_in_status_get,
3841 },
3842 { } /* end */
3843};
3844
3845/**
3846 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3847 * @codec: the HDA codec
3848 * @nid: audio in widget NID
3849 *
3850 * Creates controls related with the SPDIF input.
3851 * Called from each patch supporting the SPDIF in.
3852 *
3853 * Returns 0 if successful, or a negative error code.
3854 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003855int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
3857 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003858 struct snd_kcontrol *kctl;
3859 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003860 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Takashi Iwaidcda5802012-10-12 17:24:51 +02003862 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003863 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003864 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003865 return -EBUSY;
3866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3868 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003869 if (!kctl)
3870 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003872 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003873 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return err;
3875 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003876 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003877 snd_hda_codec_read(codec, nid, 0,
3878 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003879 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return 0;
3881}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003882EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003884/*
3885 * command cache
3886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Takashi Iwaic370dd62012-12-13 18:30:04 +01003888/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003889#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3890#define get_cmd_cache_nid(key) ((key) & 0xff)
3891#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3892
3893/**
3894 * snd_hda_codec_write_cache - send a single command with caching
3895 * @codec: the HDA codec
3896 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003897 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003898 * @verb: the verb to send
3899 * @parm: the parameter for the verb
3900 *
3901 * Send a single command without waiting for response.
3902 *
3903 * Returns 0 if successful, or a negative error code.
3904 */
3905int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003906 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003907{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003908 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003909 struct hda_cache_head *c;
3910 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003911 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003912
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003913 cache_only = codec->cached_write;
3914 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003915 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003916 if (err < 0)
3917 return err;
3918 }
3919
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003920 /* parm may contain the verb stuff for get/set amp */
3921 verb = verb | (parm >> 8);
3922 parm &= 0xff;
3923 key = build_cmd_cache_key(nid, verb);
3924 mutex_lock(&codec->bus->cmd_mutex);
3925 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003926 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003927 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003928 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003929 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003930 mutex_unlock(&codec->bus->cmd_mutex);
3931 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003932}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003933EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003934
Takashi Iwaid5191e52009-11-16 14:58:17 +01003935/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003936 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3937 * @codec: the HDA codec
3938 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003939 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003940 * @verb: the verb to send
3941 * @parm: the parameter for the verb
3942 *
3943 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3944 * command if the parameter is already identical with the cached value.
3945 * If not, it sends the command and refreshes the cache.
3946 *
3947 * Returns 0 if successful, or a negative error code.
3948 */
3949int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003950 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003951{
3952 struct hda_cache_head *c;
3953 u32 key;
3954
3955 /* parm may contain the verb stuff for get/set amp */
3956 verb = verb | (parm >> 8);
3957 parm &= 0xff;
3958 key = build_cmd_cache_key(nid, verb);
3959 mutex_lock(&codec->bus->cmd_mutex);
3960 c = get_hash(&codec->cmd_cache, key);
3961 if (c && c->val == parm) {
3962 mutex_unlock(&codec->bus->cmd_mutex);
3963 return 0;
3964 }
3965 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003966 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003967}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003968EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003969
3970/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003971 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3972 * @codec: HD-audio codec
3973 *
3974 * Execute all verbs recorded in the command caches to resume.
3975 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003976void snd_hda_codec_resume_cache(struct hda_codec *codec)
3977{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003978 int i;
3979
Takashi Iwaic370dd62012-12-13 18:30:04 +01003980 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003981 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003982 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3983 struct hda_cache_head *buffer;
3984 u32 key;
3985
3986 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3987 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003988 if (!key)
3989 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003990 if (!buffer->dirty)
3991 continue;
3992 buffer->dirty = 0;
3993 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003994 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3995 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003996 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003997 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003998 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004000EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004001
4002/**
4003 * snd_hda_sequence_write_cache - sequence writes with caching
4004 * @codec: the HDA codec
4005 * @seq: VERB array to send
4006 *
4007 * Send the commands sequentially from the given array.
4008 * Thte commands are recorded on cache for power-save and resume.
4009 * The array must be terminated with NID=0.
4010 */
4011void snd_hda_sequence_write_cache(struct hda_codec *codec,
4012 const struct hda_verb *seq)
4013{
4014 for (; seq->nid; seq++)
4015 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
4016 seq->param);
4017}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004018EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02004019
Takashi Iwaidc870f32013-01-22 15:24:30 +01004020/**
4021 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4022 * @codec: HD-audio codec
4023 */
4024void snd_hda_codec_flush_cache(struct hda_codec *codec)
4025{
4026 snd_hda_codec_resume_amp(codec);
4027 snd_hda_codec_resume_cache(codec);
4028}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004029EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01004030
Takashi Iwai95a962c2014-10-29 16:03:58 +01004031/**
4032 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4033 * @codec: the HDA codec
4034 * @fg: function group (not used now)
4035 * @power_state: the power state to set (AC_PWRST_*)
4036 *
4037 * Set the given power state to all widgets that have the power control.
4038 * If the codec has power_filter set, it evaluates the power state and
4039 * filter out if it's unchanged as D3.
4040 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004041void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004042 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01004043{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004044 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004045 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01004046
Takashi Iwaicb53c622007-08-10 17:21:45 +02004047 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01004048 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004049 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004050 if (!(wcaps & AC_WCAP_POWER))
4051 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004052 if (codec->power_filter) {
4053 state = codec->power_filter(codec, nid, power_state);
4054 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004055 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02004056 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004057 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01004058 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01004059 }
Takashi Iwai54d17402005-11-21 16:33:22 +01004060}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004061EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004062
4063/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004064 * supported power states check
4065 */
4066static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4067 unsigned int power_state)
4068{
4069 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4070
Mengdong Line037cb42012-08-10 14:11:58 +02004071 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08004072 return false;
4073 if (sup & power_state)
4074 return true;
4075 else
4076 return false;
4077}
4078
4079/*
Takashi Iwai432c6412012-08-28 09:59:20 -07004080 * wait until the state is reached, returns the current state
4081 */
4082static unsigned int hda_sync_power_state(struct hda_codec *codec,
4083 hda_nid_t fg,
4084 unsigned int power_state)
4085{
4086 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4087 unsigned int state, actual_state;
4088
4089 for (;;) {
4090 state = snd_hda_codec_read(codec, fg, 0,
4091 AC_VERB_GET_POWER_STATE, 0);
4092 if (state & AC_PWRST_ERROR)
4093 break;
4094 actual_state = (state >> 4) & 0x0f;
4095 if (actual_state == power_state)
4096 break;
4097 if (time_after_eq(jiffies, end_time))
4098 break;
4099 /* wait until the codec reachs to the target state */
4100 msleep(1);
4101 }
4102 return state;
4103}
4104
Takashi Iwai95a962c2014-10-29 16:03:58 +01004105/**
4106 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4107 * @codec: the HDA codec
4108 * @nid: widget NID
4109 * @power_state: power state to evalue
4110 *
4111 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4112 * This can be used a codec power_filter callback.
4113 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01004114unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4115 hda_nid_t nid,
4116 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01004117{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004118 if (nid == codec->afg || nid == codec->mfg)
4119 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01004120 if (power_state == AC_PWRST_D3 &&
4121 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4122 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4123 int eapd = snd_hda_codec_read(codec, nid, 0,
4124 AC_VERB_GET_EAPD_BTLENABLE, 0);
4125 if (eapd & 0x02)
4126 return AC_PWRST_D0;
4127 }
4128 return power_state;
4129}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004130EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004131
Takashi Iwai432c6412012-08-28 09:59:20 -07004132/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004133 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004134 */
Takashi Iwaid8193872012-08-31 07:54:38 -07004135static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004136 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004137{
Takashi Iwaid8193872012-08-31 07:54:38 -07004138 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004139 int count;
4140 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004141 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08004142
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004143 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004144 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05004145 if (codec->depop_delay < 0)
4146 msleep(codec->epss ? 10 : 100);
4147 else if (codec->depop_delay > 0)
4148 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02004149 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08004150 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08004151
4152 /* repeat power states setting at most 10 times*/
4153 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07004154 if (codec->patch_ops.set_power_state)
4155 codec->patch_ops.set_power_state(codec, fg,
4156 power_state);
4157 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004158 state = power_state;
4159 if (codec->power_filter)
4160 state = codec->power_filter(codec, fg, state);
4161 if (state == power_state || power_state != AC_PWRST_D3)
4162 snd_hda_codec_read(codec, fg, flags,
4163 AC_VERB_SET_POWER_STATE,
4164 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01004165 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07004166 }
4167 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08004168 if (!(state & AC_PWRST_ERROR))
4169 break;
4170 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004171
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004172 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02004173}
Takashi Iwai54d17402005-11-21 16:33:22 +01004174
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004175/* sync power states of all widgets;
4176 * this is called at the end of codec parsing
4177 */
4178static void sync_power_up_states(struct hda_codec *codec)
4179{
4180 hda_nid_t nid = codec->start_nid;
4181 int i;
4182
Takashi Iwaiba615b82013-03-13 14:47:21 +01004183 /* don't care if no filter is used */
4184 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004185 return;
4186
4187 for (i = 0; i < codec->num_nodes; i++, nid++) {
4188 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01004189 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004190 if (!(wcaps & AC_WCAP_POWER))
4191 continue;
4192 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4193 if (target == AC_PWRST_D0)
4194 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01004195 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004196 snd_hda_codec_write(codec, nid, 0,
4197 AC_VERB_SET_POWER_STATE, target);
4198 }
4199}
4200
Takashi Iwai648a8d22014-02-25 10:38:13 +01004201#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02004202/* execute additional init verbs */
4203static void hda_exec_init_verbs(struct hda_codec *codec)
4204{
4205 if (codec->init_verbs.list)
4206 snd_hda_sequence_write(codec, codec->init_verbs.list);
4207}
4208#else
4209static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4210#endif
4211
Takashi Iwai2a439522011-07-26 09:52:50 +02004212#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004213/*
4214 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004215 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02004216 */
Dylan Reidd17344b2012-09-28 15:57:01 -07004217static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004218{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004219 unsigned int state;
4220
Takashi Iwai989c3182012-11-19 14:14:58 +01004221 codec->in_pm = 1;
4222
Takashi Iwaicb53c622007-08-10 17:21:45 +02004223 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02004224 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02004225 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004226 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07004227 /* Cancel delayed work if we aren't currently running from it. */
4228 if (!in_wq)
4229 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004230 spin_lock(&codec->power_lock);
4231 snd_hda_update_power_acct(codec);
4232 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02004233 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02004234 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004235 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004236 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01004237 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004238 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004239}
4240
Takashi Iwaic370dd62012-12-13 18:30:04 +01004241/* mark all entries of cmd and amp caches dirty */
4242static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4243{
4244 int i;
4245 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4246 struct hda_cache_head *cmd;
4247 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4248 cmd->dirty = 1;
4249 }
4250 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4251 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01004252 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01004253 amp->head.dirty = 1;
4254 }
4255}
4256
Takashi Iwaicb53c622007-08-10 17:21:45 +02004257/*
4258 * kick up codec; used both from PM and power-save
4259 */
4260static void hda_call_codec_resume(struct hda_codec *codec)
4261{
Takashi Iwai989c3182012-11-19 14:14:58 +01004262 codec->in_pm = 1;
4263
Takashi Iwaic370dd62012-12-13 18:30:04 +01004264 hda_mark_cmd_cache_dirty(codec);
4265
Takashi Iwai7f308302012-05-08 16:52:23 +02004266 /* set as if powered on for avoiding re-entering the resume
4267 * in the resume / power-save sequence
4268 */
4269 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07004270 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02004271 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02004272 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004273 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004274 if (codec->patch_ops.resume)
4275 codec->patch_ops.resume(codec);
4276 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02004277 if (codec->patch_ops.init)
4278 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004279 snd_hda_codec_resume_amp(codec);
4280 snd_hda_codec_resume_cache(codec);
4281 }
David Henningsson26a6cb62012-10-09 15:04:21 +02004282
4283 if (codec->jackpoll_interval)
4284 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01004285 else
David Henningsson26a6cb62012-10-09 15:04:21 +02004286 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01004287
4288 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02004289 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004290}
Takashi Iwai2a439522011-07-26 09:52:50 +02004291#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004292
Takashi Iwai54d17402005-11-21 16:33:22 +01004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294/**
4295 * snd_hda_build_controls - build mixer controls
4296 * @bus: the BUS
4297 *
4298 * Creates mixer controls for each codec included in the bus.
4299 *
4300 * Returns 0 if successful, otherwise a negative error code.
4301 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004302int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004304 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Takashi Iwai0ba21762007-04-16 11:29:14 +02004306 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004307 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004308 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004309 codec_err(codec,
4310 "cannot build controls for #%d (error %d)\n",
4311 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004312 err = snd_hda_codec_reset(codec);
4313 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004314 codec_err(codec,
4315 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004316 return err;
4317 }
4318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004320 return 0;
4321}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004322EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004324/*
4325 * add standard channel maps if not specified
4326 */
4327static int add_std_chmaps(struct hda_codec *codec)
4328{
4329 int i, str, err;
4330
4331 for (i = 0; i < codec->num_pcms; i++) {
4332 for (str = 0; str < 2; str++) {
4333 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4334 struct hda_pcm_stream *hinfo =
4335 &codec->pcm_info[i].stream[str];
4336 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004337 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004338
4339 if (codec->pcm_info[i].own_chmap)
4340 continue;
4341 if (!pcm || !hinfo->substreams)
4342 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004343 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4344 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004345 hinfo->channels_max,
4346 0, &chmap);
4347 if (err < 0)
4348 return err;
4349 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4350 }
4351 }
4352 return 0;
4353}
4354
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004355/* default channel maps for 2.1 speakers;
4356 * since HD-audio supports only stereo, odd number channels are omitted
4357 */
4358const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4359 { .channels = 2,
4360 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4361 { .channels = 4,
4362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4363 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4364 { }
4365};
4366EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4367
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004368int snd_hda_codec_build_controls(struct hda_codec *codec)
4369{
4370 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004371 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004372 /* continue to initialize... */
4373 if (codec->patch_ops.init)
4374 err = codec->patch_ops.init(codec);
4375 if (!err && codec->patch_ops.build_controls)
4376 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004377 if (err < 0)
4378 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004379
4380 /* we create chmaps here instead of build_pcms */
4381 err = add_std_chmaps(codec);
4382 if (err < 0)
4383 return err;
4384
David Henningsson26a6cb62012-10-09 15:04:21 +02004385 if (codec->jackpoll_interval)
4386 hda_jackpoll_work(&codec->jackpoll_work.work);
4387 else
4388 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004389 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return 0;
4391}
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393/*
4394 * stream formats
4395 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004396struct hda_rate_tbl {
4397 unsigned int hz;
4398 unsigned int alsa_bits;
4399 unsigned int hda_fmt;
4400};
4401
Takashi Iwai92f10b32010-08-03 14:21:00 +02004402/* rate = base * mult / div */
4403#define HDA_RATE(base, mult, div) \
4404 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4405 (((div) - 1) << AC_FMT_DIV_SHIFT))
4406
Takashi Iwaibefdf312005-08-22 13:57:55 +02004407static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004409
4410 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004411 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4412 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4413 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4414 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4415 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4416 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4417 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4418 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4419 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4420 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4421 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004422#define AC_PAR_PCM_RATE_BITS 11
4423 /* up to bits 10, 384kHZ isn't supported properly */
4424
4425 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004426 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004427
Takashi Iwaibefdf312005-08-22 13:57:55 +02004428 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429};
4430
4431/**
4432 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004433 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 * @rate: the sample rate
4435 * @channels: the number of channels
4436 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4437 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004438 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 *
4440 * Calculate the format bitset from the given rate, channels and th PCM format.
4441 *
4442 * Return zero if invalid.
4443 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004444unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4445 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 unsigned int channels,
4447 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004448 unsigned int maxbps,
4449 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 int i;
4452 unsigned int val = 0;
4453
Takashi Iwaibefdf312005-08-22 13:57:55 +02004454 for (i = 0; rate_bits[i].hz; i++)
4455 if (rate_bits[i].hz == rate) {
4456 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 break;
4458 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004459 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004460 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return 0;
4462 }
4463
4464 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004465 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return 0;
4467 }
4468 val |= channels - 1;
4469
4470 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004471 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004472 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004473 break;
4474 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004475 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 case 20:
4478 case 24:
4479 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004480 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004481 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004483 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004485 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 break;
4487 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004488 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004489 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return 0;
4491 }
4492
Anssi Hannula32c168c2010-08-03 13:28:57 +03004493 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004494 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return val;
4497}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004498EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004500static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4501 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004502{
4503 unsigned int val = 0;
4504 if (nid != codec->afg &&
4505 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4506 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4507 if (!val || val == -1)
4508 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4509 if (!val || val == -1)
4510 return 0;
4511 return val;
4512}
4513
4514static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4515{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004516 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004517 get_pcm_param);
4518}
4519
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004520static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4521 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004522{
4523 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4524 if (!streams || streams == -1)
4525 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4526 if (!streams || streams == -1)
4527 return 0;
4528 return streams;
4529}
4530
4531static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4532{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004533 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004534 get_stream_param);
4535}
4536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537/**
4538 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4539 * @codec: the HDA codec
4540 * @nid: NID to query
4541 * @ratesp: the pointer to store the detected rate bitflags
4542 * @formatsp: the pointer to store the detected formats
4543 * @bpsp: the pointer to store the detected format widths
4544 *
4545 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4546 * or @bsps argument is ignored.
4547 *
4548 * Returns 0 if successful, otherwise a negative error code.
4549 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004550int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4552{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004553 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004555 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004556 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 if (ratesp) {
4559 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004560 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004562 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004564 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004565 codec_err(codec,
4566 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4567 nid, val,
4568 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004569 return -EIO;
4570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 *ratesp = rates;
4572 }
4573
4574 if (formatsp || bpsp) {
4575 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004576 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004578 streams = query_stream_param(codec, nid);
4579 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 bps = 0;
4583 if (streams & AC_SUPFMT_PCM) {
4584 if (val & AC_SUPPCM_BITS_8) {
4585 formats |= SNDRV_PCM_FMTBIT_U8;
4586 bps = 8;
4587 }
4588 if (val & AC_SUPPCM_BITS_16) {
4589 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4590 bps = 16;
4591 }
4592 if (wcaps & AC_WCAP_DIGITAL) {
4593 if (val & AC_SUPPCM_BITS_32)
4594 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4595 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4596 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4597 if (val & AC_SUPPCM_BITS_24)
4598 bps = 24;
4599 else if (val & AC_SUPPCM_BITS_20)
4600 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004601 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4602 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4604 if (val & AC_SUPPCM_BITS_32)
4605 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 else if (val & AC_SUPPCM_BITS_24)
4607 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004608 else if (val & AC_SUPPCM_BITS_20)
4609 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 }
4611 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004612#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004613 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004615 if (!bps)
4616 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004617 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004618#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004619 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004620 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 /* temporary hack: we have still no proper support
4622 * for the direct AC3 stream...
4623 */
4624 formats |= SNDRV_PCM_FMTBIT_U8;
4625 bps = 8;
4626 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004627 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004628 codec_err(codec,
4629 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4630 nid, val,
4631 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4632 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004633 return -EIO;
4634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (formatsp)
4636 *formatsp = formats;
4637 if (bpsp)
4638 *bpsp = bps;
4639 }
4640
4641 return 0;
4642}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004643EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004646 * snd_hda_is_supported_format - Check the validity of the format
4647 * @codec: HD-audio codec
4648 * @nid: NID to check
4649 * @format: the HD-audio format value to check
4650 *
4651 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 *
4653 * Returns 1 if supported, 0 if not.
4654 */
4655int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4656 unsigned int format)
4657{
4658 int i;
4659 unsigned int val = 0, rate, stream;
4660
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004661 val = query_pcm_param(codec, nid);
4662 if (!val)
4663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004666 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004667 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 if (val & (1 << i))
4669 break;
4670 return 0;
4671 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004672 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return 0;
4674
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004675 stream = query_stream_param(codec, nid);
4676 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 return 0;
4678
4679 if (stream & AC_SUPFMT_PCM) {
4680 switch (format & 0xf0) {
4681 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004682 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return 0;
4684 break;
4685 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004686 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return 0;
4688 break;
4689 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004690 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return 0;
4692 break;
4693 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004694 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return 0;
4696 break;
4697 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004698 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return 0;
4700 break;
4701 default:
4702 return 0;
4703 }
4704 } else {
4705 /* FIXME: check for float32 and AC3? */
4706 }
4707
4708 return 1;
4709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004710EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712/*
4713 * PCM stuff
4714 */
4715static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4716 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004717 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
4719 return 0;
4720}
4721
4722static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4723 struct hda_codec *codec,
4724 unsigned int stream_tag,
4725 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004726 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
4728 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4729 return 0;
4730}
4731
4732static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4733 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004734 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Takashi Iwai888afa12008-03-18 09:57:50 +01004736 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return 0;
4738}
4739
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004740static int set_pcm_default_values(struct hda_codec *codec,
4741 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004743 int err;
4744
Takashi Iwai0ba21762007-04-16 11:29:14 +02004745 /* query support PCM information from the given NID */
4746 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004747 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004748 info->rates ? NULL : &info->rates,
4749 info->formats ? NULL : &info->formats,
4750 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004751 if (err < 0)
4752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
4754 if (info->ops.open == NULL)
4755 info->ops.open = hda_pcm_default_open_close;
4756 if (info->ops.close == NULL)
4757 info->ops.close = hda_pcm_default_open_close;
4758 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004759 if (snd_BUG_ON(!info->nid))
4760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 info->ops.prepare = hda_pcm_default_prepare;
4762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004764 if (snd_BUG_ON(!info->nid))
4765 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 info->ops.cleanup = hda_pcm_default_cleanup;
4767 }
4768 return 0;
4769}
4770
Takashi Iwaieb541332010-08-06 13:48:11 +02004771/*
4772 * codec prepare/cleanup entries
4773 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004774/**
4775 * snd_hda_codec_prepare - Prepare a stream
4776 * @codec: the HDA codec
4777 * @hinfo: PCM information
4778 * @stream: stream tag to assign
4779 * @format: format id to assign
4780 * @substream: PCM substream to assign
4781 *
4782 * Calls the prepare callback set by the codec with the given arguments.
4783 * Clean up the inactive streams when successful.
4784 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004785int snd_hda_codec_prepare(struct hda_codec *codec,
4786 struct hda_pcm_stream *hinfo,
4787 unsigned int stream,
4788 unsigned int format,
4789 struct snd_pcm_substream *substream)
4790{
4791 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004792 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004793 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4794 if (ret >= 0)
4795 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004796 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004797 return ret;
4798}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004799EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004800
Takashi Iwai95a962c2014-10-29 16:03:58 +01004801/**
4802 * snd_hda_codec_cleanup - Prepare a stream
4803 * @codec: the HDA codec
4804 * @hinfo: PCM information
4805 * @substream: PCM substream
4806 *
4807 * Calls the cleanup callback set by the codec with the given arguments.
4808 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004809void snd_hda_codec_cleanup(struct hda_codec *codec,
4810 struct hda_pcm_stream *hinfo,
4811 struct snd_pcm_substream *substream)
4812{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004813 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004814 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004815 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004816}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004817EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004818
Takashi Iwaid5191e52009-11-16 14:58:17 +01004819/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004820const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4821 "Audio", "SPDIF", "HDMI", "Modem"
4822};
4823
Takashi Iwai176d5332008-07-30 15:01:44 +02004824/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004825 * get the empty PCM device number to assign
4826 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004827static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004828{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004829 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004830 /* assigned to static slots up to dev#10; if more needed, assign
4831 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4832 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004833 static int audio_idx[HDA_PCM_NTYPES][5] = {
4834 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4835 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004836 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004837 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004838 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004839 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004840
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004841 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004842 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004843 return -EINVAL;
4844 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004845
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004846 for (i = 0; audio_idx[type][i] >= 0; i++) {
4847#ifndef CONFIG_SND_DYNAMIC_MINORS
4848 if (audio_idx[type][i] >= 8)
4849 break;
4850#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004851 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4852 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004853 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004854
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004855#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004856 /* non-fixed slots starting from 10 */
4857 for (i = 10; i < 32; i++) {
4858 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4859 return i;
4860 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004861#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004862
Takashi Iwai4e76a882014-02-25 12:21:03 +01004863 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004864 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004865#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004866 dev_warn(bus->card->dev,
4867 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004868#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004869 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004870}
4871
4872/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004873 * attach a new PCM stream
4874 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004875static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004876{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004877 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004878 struct hda_pcm_stream *info;
4879 int stream, err;
4880
Takashi Iwaib91f0802008-11-04 08:43:08 +01004881 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004882 return -EINVAL;
4883 for (stream = 0; stream < 2; stream++) {
4884 info = &pcm->stream[stream];
4885 if (info->substreams) {
4886 err = set_pcm_default_values(codec, info);
4887 if (err < 0)
4888 return err;
4889 }
4890 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004891 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004892}
4893
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004894/* assign all PCMs of the given codec */
4895int snd_hda_codec_build_pcms(struct hda_codec *codec)
4896{
4897 unsigned int pcm;
4898 int err;
4899
4900 if (!codec->num_pcms) {
4901 if (!codec->patch_ops.build_pcms)
4902 return 0;
4903 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004904 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004905 codec_err(codec,
4906 "cannot build PCMs for #%d (error %d)\n",
4907 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004908 err = snd_hda_codec_reset(codec);
4909 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004910 codec_err(codec,
4911 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004912 return err;
4913 }
4914 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004915 }
4916 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4917 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4918 int dev;
4919
4920 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004921 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004922
4923 if (!cpcm->pcm) {
4924 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4925 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004926 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004927 cpcm->device = dev;
4928 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004929 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004930 codec_err(codec,
4931 "cannot attach PCM stream %d for codec #%d\n",
4932 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004933 continue; /* no fatal error */
4934 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004935 }
4936 }
4937 return 0;
4938}
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940/**
4941 * snd_hda_build_pcms - build PCM information
4942 * @bus: the BUS
4943 *
4944 * Create PCM information for each codec included in the bus.
4945 *
4946 * The build_pcms codec patch is requested to set up codec->num_pcms and
4947 * codec->pcm_info properly. The array is referred by the top-level driver
4948 * to create its PCM instances.
4949 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4950 * callback.
4951 *
4952 * At least, substreams, channels_min and channels_max must be filled for
4953 * each stream. substreams = 0 indicates that the stream doesn't exist.
4954 * When rates and/or formats are zero, the supported values are queried
4955 * from the given nid. The nid is used also by the default ops.prepare
4956 * and ops.cleanup callbacks.
4957 *
4958 * The driver needs to call ops.open in its open callback. Similarly,
4959 * ops.close is supposed to be called in the close callback.
4960 * ops.prepare should be called in the prepare or hw_params callback
4961 * with the proper parameters for set up.
4962 * ops.cleanup should be called in hw_free for clean up of streams.
4963 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004964 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004966int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004968 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Takashi Iwai0ba21762007-04-16 11:29:14 +02004970 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004971 int err = snd_hda_codec_build_pcms(codec);
4972 if (err < 0)
4973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 }
4975 return 0;
4976}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004977EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 * snd_hda_add_new_ctls - create controls from the array
4981 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004982 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 *
4984 * This helper function creates and add new controls in the given array.
4985 * The array must be terminated with an empty entry as terminator.
4986 *
4987 * Returns 0 if successful, or a negative error code.
4988 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004989int snd_hda_add_new_ctls(struct hda_codec *codec,
4990 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004992 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
4994 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004995 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004996 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004997 if (knew->iface == -1) /* skip this codec private value */
4998 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004999 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01005000 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02005001 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01005002 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01005003 if (addr > 0)
5004 kctl->id.device = addr;
5005 if (idx > 0)
5006 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01005007 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005008 if (!err)
5009 break;
5010 /* try first with another device index corresponding to
5011 * the codec addr; if it still fails (or it's the
5012 * primary codec), then try another control index
5013 */
5014 if (!addr && codec->addr)
5015 addr = codec->addr;
5016 else if (!idx && !knew->index) {
5017 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02005018 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01005019 if (idx <= 0)
5020 return err;
5021 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01005022 return err;
5023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
5025 return 0;
5026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005027EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
Takashi Iwai83012a72012-08-24 18:38:08 +02005029#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02005030static void hda_power_work(struct work_struct *work)
5031{
5032 struct hda_codec *codec =
5033 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01005034 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07005035 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005036
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005037 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005038 if (codec->power_transition > 0) { /* during power-up sequence? */
5039 spin_unlock(&codec->power_lock);
5040 return;
5041 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02005042 if (!codec->power_on || codec->power_count) {
5043 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005044 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005045 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02005046 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005047 spin_unlock(&codec->power_lock);
5048
Dylan Reidd17344b2012-09-28 15:57:01 -07005049 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005050 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5051 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005052}
5053
5054static void hda_keep_power_on(struct hda_codec *codec)
5055{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005056 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005057 codec->power_count++;
5058 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005059 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005060 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01005061 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005062}
5063
Takashi Iwaid5191e52009-11-16 14:58:17 +01005064/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01005065void snd_hda_update_power_acct(struct hda_codec *codec)
5066{
5067 unsigned long delta = jiffies - codec->power_jiffies;
5068 if (codec->power_on)
5069 codec->power_on_acct += delta;
5070 else
5071 codec->power_off_acct += delta;
5072 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005073}
5074
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005075/* Transition to powered up, if wait_power_down then wait for a pending
5076 * transition to D3 to complete. A pending D3 transition is indicated
5077 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005078/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005079static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005080{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005081 /* Return if power_on or transitioning to power_on, unless currently
5082 * powering down. */
5083 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005084 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02005085 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005086 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005087
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005088 cancel_delayed_work_sync(&codec->power_work);
5089
5090 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07005091 /* If the power down delayed work was cancelled above before starting,
5092 * then there is no need to go through power up here.
5093 */
5094 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02005095 if (codec->power_transition < 0)
5096 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07005097 return;
5098 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005099
Takashi Iwaid66fee52011-08-02 15:39:31 +02005100 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01005101 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005102 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01005103 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02005104 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005105 spin_unlock(&codec->power_lock);
5106
Takashi Iwaicb53c622007-08-10 17:21:45 +02005107 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005108
5109 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02005110 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005111}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07005112
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005113#define power_save(codec) \
5114 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005115
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005116/* Transition to powered down */
5117static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005118{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005119 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02005120 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005121
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005122 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02005123 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01005124 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01005125 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02005126 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005127}
5128
5129/**
5130 * snd_hda_power_save - Power-up/down/sync the codec
5131 * @codec: HD-audio codec
5132 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005133 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02005134 *
5135 * Change the power-up counter via @delta, and power up or down the hardware
5136 * appropriately. For the power-down, queue to the delayed action.
5137 * Passing zero to @delta means to synchronize the power state.
5138 */
5139void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5140{
5141 spin_lock(&codec->power_lock);
5142 codec->power_count += delta;
5143 trace_hda_power_count(codec);
5144 if (delta > 0)
5145 __snd_hda_power_up(codec, d3wait);
5146 else
5147 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02005148 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005149}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005150EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005151
Takashi Iwaid5191e52009-11-16 14:58:17 +01005152/**
5153 * snd_hda_check_amp_list_power - Check the amp list and update the power
5154 * @codec: HD-audio codec
5155 * @check: the object containing an AMP list and the status
5156 * @nid: NID to check / update
5157 *
5158 * Check whether the given NID is in the amp list. If it's in the list,
5159 * check the current AMP status, and update the the power-status according
5160 * to the mute status.
5161 *
5162 * This function is supposed to be set or called from the check_power_status
5163 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005164 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02005165int snd_hda_check_amp_list_power(struct hda_codec *codec,
5166 struct hda_loopback_check *check,
5167 hda_nid_t nid)
5168{
Takashi Iwai031024e2011-05-02 11:29:30 +02005169 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02005170 int ch, v;
5171
5172 if (!check->amplist)
5173 return 0;
5174 for (p = check->amplist; p->nid; p++) {
5175 if (p->nid == nid)
5176 break;
5177 }
5178 if (!p->nid)
5179 return 0; /* nothing changed */
5180
5181 for (p = check->amplist; p->nid; p++) {
5182 for (ch = 0; ch < 2; ch++) {
5183 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5184 p->idx);
5185 if (!(v & HDA_AMP_MUTE) && v > 0) {
5186 if (!check->power_on) {
5187 check->power_on = 1;
5188 snd_hda_power_up(codec);
5189 }
5190 return 1;
5191 }
5192 }
5193 }
5194 if (check->power_on) {
5195 check->power_on = 0;
5196 snd_hda_power_down(codec);
5197 }
5198 return 0;
5199}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005200EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02005201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005203/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005204 * Channel mode helper
5205 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005206
5207/**
5208 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005209 * @codec: the HDA codec
5210 * @uinfo: pointer to get/store the data
5211 * @chmode: channel mode array
5212 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01005213 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005214int snd_hda_ch_mode_info(struct hda_codec *codec,
5215 struct snd_ctl_elem_info *uinfo,
5216 const struct hda_channel_mode *chmode,
5217 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005218{
5219 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5220 uinfo->count = 1;
5221 uinfo->value.enumerated.items = num_chmodes;
5222 if (uinfo->value.enumerated.item >= num_chmodes)
5223 uinfo->value.enumerated.item = num_chmodes - 1;
5224 sprintf(uinfo->value.enumerated.name, "%dch",
5225 chmode[uinfo->value.enumerated.item].channels);
5226 return 0;
5227}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005228EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005229
Takashi Iwaid5191e52009-11-16 14:58:17 +01005230/**
5231 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005232 * @codec: the HDA codec
5233 * @ucontrol: pointer to get/store the data
5234 * @chmode: channel mode array
5235 * @num_chmodes: channel mode array size
5236 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005237 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005238int snd_hda_ch_mode_get(struct hda_codec *codec,
5239 struct snd_ctl_elem_value *ucontrol,
5240 const struct hda_channel_mode *chmode,
5241 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005242 int max_channels)
5243{
5244 int i;
5245
5246 for (i = 0; i < num_chmodes; i++) {
5247 if (max_channels == chmode[i].channels) {
5248 ucontrol->value.enumerated.item[0] = i;
5249 break;
5250 }
5251 }
5252 return 0;
5253}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005254EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005255
Takashi Iwaid5191e52009-11-16 14:58:17 +01005256/**
5257 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005258 * @codec: the HDA codec
5259 * @ucontrol: pointer to get/store the data
5260 * @chmode: channel mode array
5261 * @num_chmodes: channel mode array size
5262 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01005263 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005264int snd_hda_ch_mode_put(struct hda_codec *codec,
5265 struct snd_ctl_elem_value *ucontrol,
5266 const struct hda_channel_mode *chmode,
5267 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005268 int *max_channelsp)
5269{
5270 unsigned int mode;
5271
5272 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005273 if (mode >= num_chmodes)
5274 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005275 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005276 return 0;
5277 /* change the current channel setting */
5278 *max_channelsp = chmode[mode].channels;
5279 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005280 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005281 return 1;
5282}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005283EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005284
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285/*
5286 * input MUX helper
5287 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005288
5289/**
5290 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005291 * @imux: imux helper object
5292 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01005293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005294int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5295 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296{
5297 unsigned int index;
5298
5299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5300 uinfo->count = 1;
5301 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005302 if (!imux->num_items)
5303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 index = uinfo->value.enumerated.item;
5305 if (index >= imux->num_items)
5306 index = imux->num_items - 1;
5307 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5308 return 0;
5309}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005310EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
Takashi Iwaid5191e52009-11-16 14:58:17 +01005312/**
5313 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005314 * @codec: the HDA codec
5315 * @imux: imux helper object
5316 * @ucontrol: pointer to get/store the data
5317 * @nid: input mux NID
5318 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005319 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005320int snd_hda_input_mux_put(struct hda_codec *codec,
5321 const struct hda_input_mux *imux,
5322 struct snd_ctl_elem_value *ucontrol,
5323 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 unsigned int *cur_val)
5325{
5326 unsigned int idx;
5327
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005328 if (!imux->num_items)
5329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 idx = ucontrol->value.enumerated.item[0];
5331 if (idx >= imux->num_items)
5332 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005333 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005335 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5336 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 *cur_val = idx;
5338 return 1;
5339}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005340EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005343/**
5344 * snd_hda_enum_helper_info - Helper for simple enum ctls
5345 * @kcontrol: ctl element
5346 * @uinfo: pointer to get/store the data
5347 * @num_items: number of enum items
5348 * @texts: enum item string array
5349 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005350 * process kcontrol info callback of a simple string enum array
5351 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5352 */
5353int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5354 struct snd_ctl_elem_info *uinfo,
5355 int num_items, const char * const *texts)
5356{
5357 static const char * const texts_default[] = {
5358 "Disabled", "Enabled"
5359 };
5360
5361 if (!texts || !num_items) {
5362 num_items = 2;
5363 texts = texts_default;
5364 }
5365
Takashi Iwai3ff72212014-10-20 18:17:28 +02005366 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005367}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005368EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005369
5370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 * Multi-channel / digital-out PCM helper functions
5372 */
5373
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005374/* setup SPDIF output stream */
5375static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5376 unsigned int stream_tag, unsigned int format)
5377{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005378 struct hda_spdif_out *spdif;
5379 unsigned int curr_fmt;
5380 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005381
Laurence Darby3bef1c32012-11-03 17:00:06 +00005382 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5383 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5384 AC_VERB_GET_STREAM_FORMAT, 0);
5385 reset = codec->spdif_status_reset &&
5386 (spdif->ctls & AC_DIG1_ENABLE) &&
5387 curr_fmt != format;
5388
5389 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5390 updated */
5391 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005392 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005393 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005394 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005395 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005396 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005397 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005398 for (d = codec->slave_dig_outs; *d; d++)
5399 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5400 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005401 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005402 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005403 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005404 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005405 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005406}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005407
Takashi Iwai2f728532008-09-25 16:32:41 +02005408static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5409{
5410 snd_hda_codec_cleanup_stream(codec, nid);
5411 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005412 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005413 for (d = codec->slave_dig_outs; *d; d++)
5414 snd_hda_codec_cleanup_stream(codec, *d);
5415 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005416}
5417
Takashi Iwaid5191e52009-11-16 14:58:17 +01005418/**
5419 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5420 * @bus: HD-audio bus
5421 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005422void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5423{
5424 struct hda_codec *codec;
5425
5426 if (!bus)
5427 return;
5428 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005429 if (hda_codec_is_power_on(codec) &&
5430 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005431 codec->patch_ops.reboot_notify(codec);
5432 }
5433}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005434EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005435
Takashi Iwaid5191e52009-11-16 14:58:17 +01005436/**
5437 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005438 * @codec: the HDA codec
5439 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005441int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5442 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
Ingo Molnar62932df2006-01-16 16:34:20 +01005444 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005445 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5446 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005447 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005449 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 return 0;
5451}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005452EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Takashi Iwaid5191e52009-11-16 14:58:17 +01005454/**
5455 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005456 * @codec: the HDA codec
5457 * @mout: hda_multi_out object
5458 * @stream_tag: stream tag to assign
5459 * @format: format id to assign
5460 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005461 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005462int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5463 struct hda_multi_out *mout,
5464 unsigned int stream_tag,
5465 unsigned int format,
5466 struct snd_pcm_substream *substream)
5467{
5468 mutex_lock(&codec->spdif_mutex);
5469 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5470 mutex_unlock(&codec->spdif_mutex);
5471 return 0;
5472}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005473EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005474
Takashi Iwaid5191e52009-11-16 14:58:17 +01005475/**
5476 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005477 * @codec: the HDA codec
5478 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005479 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005480int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5481 struct hda_multi_out *mout)
5482{
5483 mutex_lock(&codec->spdif_mutex);
5484 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5485 mutex_unlock(&codec->spdif_mutex);
5486 return 0;
5487}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005488EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005489
Takashi Iwaid5191e52009-11-16 14:58:17 +01005490/**
5491 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005492 * @codec: the HDA codec
5493 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005495int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5496 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
Ingo Molnar62932df2006-01-16 16:34:20 +01005498 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005500 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 return 0;
5502}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005503EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Takashi Iwaid5191e52009-11-16 14:58:17 +01005505/**
5506 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005507 * @codec: the HDA codec
5508 * @mout: hda_multi_out object
5509 * @substream: PCM substream to assign
5510 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005511 *
5512 * Open analog outputs and set up the hw-constraints.
5513 * If the digital outputs can be opened as slave, open the digital
5514 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005516int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5517 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005518 struct snd_pcm_substream *substream,
5519 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520{
Takashi Iwai9a081602008-02-12 18:37:26 +01005521 struct snd_pcm_runtime *runtime = substream->runtime;
5522 runtime->hw.channels_max = mout->max_channels;
5523 if (mout->dig_out_nid) {
5524 if (!mout->analog_rates) {
5525 mout->analog_rates = hinfo->rates;
5526 mout->analog_formats = hinfo->formats;
5527 mout->analog_maxbps = hinfo->maxbps;
5528 } else {
5529 runtime->hw.rates = mout->analog_rates;
5530 runtime->hw.formats = mout->analog_formats;
5531 hinfo->maxbps = mout->analog_maxbps;
5532 }
5533 if (!mout->spdif_rates) {
5534 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5535 &mout->spdif_rates,
5536 &mout->spdif_formats,
5537 &mout->spdif_maxbps);
5538 }
5539 mutex_lock(&codec->spdif_mutex);
5540 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005541 if ((runtime->hw.rates & mout->spdif_rates) &&
5542 (runtime->hw.formats & mout->spdif_formats)) {
5543 runtime->hw.rates &= mout->spdif_rates;
5544 runtime->hw.formats &= mout->spdif_formats;
5545 if (mout->spdif_maxbps < hinfo->maxbps)
5546 hinfo->maxbps = mout->spdif_maxbps;
5547 } else {
5548 mout->share_spdif = 0;
5549 /* FIXME: need notify? */
5550 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005551 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005552 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5555 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5556}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005557EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Takashi Iwaid5191e52009-11-16 14:58:17 +01005559/**
5560 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005561 * @codec: the HDA codec
5562 * @mout: hda_multi_out object
5563 * @stream_tag: stream tag to assign
5564 * @format: format id to assign
5565 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005566 *
5567 * Set up the i/o for analog out.
5568 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005570int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5571 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 unsigned int stream_tag,
5573 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005574 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
Takashi Iwaidda14412011-05-02 11:29:30 +02005576 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005578 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 int i;
5580
Ingo Molnar62932df2006-01-16 16:34:20 +01005581 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005582 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005583 if (mout->dig_out_nid && mout->share_spdif &&
5584 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005586 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5587 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005588 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005590 setup_dig_out_stream(codec, mout->dig_out_nid,
5591 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 } else {
5593 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005594 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 }
5596 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005597 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005600 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5601 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005602 if (!mout->no_share_stream &&
5603 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005605 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5606 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005607 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005608 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5609 if (!mout->no_share_stream && mout->hp_out_nid[i])
5610 snd_hda_codec_setup_stream(codec,
5611 mout->hp_out_nid[i],
5612 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 /* surrounds */
5615 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005616 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005617 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5618 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005619 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005620 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5621 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005623
5624 /* extra surrounds */
5625 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5626 int ch = 0;
5627 if (!mout->extra_out_nid[i])
5628 break;
5629 if (chs >= (i + 1) * 2)
5630 ch = i * 2;
5631 else if (!mout->no_share_stream)
5632 break;
5633 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5634 stream_tag, ch, format);
5635 }
5636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 return 0;
5638}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005639EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Takashi Iwaid5191e52009-11-16 14:58:17 +01005641/**
5642 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005643 * @codec: the HDA codec
5644 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005646int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5647 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
Takashi Iwaidda14412011-05-02 11:29:30 +02005649 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 int i;
5651
5652 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005653 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005655 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005656 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5657 if (mout->hp_out_nid[i])
5658 snd_hda_codec_cleanup_stream(codec,
5659 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005660 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5661 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005662 snd_hda_codec_cleanup_stream(codec,
5663 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005664 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005666 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 mout->dig_out_used = 0;
5668 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005669 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 return 0;
5671}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005672EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Takashi Iwai47408602012-04-20 13:06:53 +02005674/**
5675 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005676 * @codec: the HDA codec
5677 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005678 *
5679 * Guess the suitable VREF pin bits to be set as the pin-control value.
5680 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5681 */
5682unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5683{
5684 unsigned int pincap;
5685 unsigned int oldval;
5686 oldval = snd_hda_codec_read(codec, pin, 0,
5687 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5688 pincap = snd_hda_query_pin_caps(codec, pin);
5689 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5690 /* Exception: if the default pin setup is vref50, we give it priority */
5691 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5692 return AC_PINCTL_VREF_80;
5693 else if (pincap & AC_PINCAP_VREF_50)
5694 return AC_PINCTL_VREF_50;
5695 else if (pincap & AC_PINCAP_VREF_100)
5696 return AC_PINCTL_VREF_100;
5697 else if (pincap & AC_PINCAP_VREF_GRD)
5698 return AC_PINCTL_VREF_GRD;
5699 return AC_PINCTL_VREF_HIZ;
5700}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005701EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005702
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005703/**
5704 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5705 * @codec: the HDA codec
5706 * @pin: referred pin NID
5707 * @val: pin ctl value to audit
5708 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005709unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5710 hda_nid_t pin, unsigned int val)
5711{
5712 static unsigned int cap_lists[][2] = {
5713 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5714 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5715 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5716 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5717 };
5718 unsigned int cap;
5719
5720 if (!val)
5721 return 0;
5722 cap = snd_hda_query_pin_caps(codec, pin);
5723 if (!cap)
5724 return val; /* don't know what to do... */
5725
5726 if (val & AC_PINCTL_OUT_EN) {
5727 if (!(cap & AC_PINCAP_OUT))
5728 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5729 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5730 val &= ~AC_PINCTL_HP_EN;
5731 }
5732
5733 if (val & AC_PINCTL_IN_EN) {
5734 if (!(cap & AC_PINCAP_IN))
5735 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5736 else {
5737 unsigned int vcap, vref;
5738 int i;
5739 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5740 vref = val & AC_PINCTL_VREFEN;
5741 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5742 if (vref == cap_lists[i][0] &&
5743 !(vcap & cap_lists[i][1])) {
5744 if (i == ARRAY_SIZE(cap_lists) - 1)
5745 vref = AC_PINCTL_VREF_HIZ;
5746 else
5747 vref = cap_lists[i + 1][0];
5748 }
5749 }
5750 val &= ~AC_PINCTL_VREFEN;
5751 val |= vref;
5752 }
5753 }
5754
5755 return val;
5756}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005757EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005758
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005759/**
5760 * _snd_hda_pin_ctl - Helper to set pin ctl value
5761 * @codec: the HDA codec
5762 * @pin: referred pin NID
5763 * @val: pin control value to set
5764 * @cached: access over codec pinctl cache or direct write
5765 *
5766 * This function is a helper to set a pin ctl value more safely.
5767 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5768 * value in pin target array via snd_hda_codec_set_pin_target(), then
5769 * actually writes the value via either snd_hda_codec_update_cache() or
5770 * snd_hda_codec_write() depending on @cached flag.
5771 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005772int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5773 unsigned int val, bool cached)
5774{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005775 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005776 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005777 if (cached)
5778 return snd_hda_codec_update_cache(codec, pin, 0,
5779 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5780 else
5781 return snd_hda_codec_write(codec, pin, 0,
5782 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5783}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005784EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005785
Takashi Iwai990061c2010-09-09 22:08:44 +02005786/**
5787 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005788 * @codec: the HDA codec
5789 * @imux: imux helper object
5790 * @label: the name of imux item to assign
5791 * @index: index number of imux item to assign
5792 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005793 *
5794 * When the same label is used already in the existing items, the number
5795 * suffix is appended to the label. This label index number is stored
5796 * to type_idx when non-NULL pointer is given.
5797 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005798int snd_hda_add_imux_item(struct hda_codec *codec,
5799 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005800 int index, int *type_idx)
5801{
5802 int i, label_idx = 0;
5803 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005804 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005805 return -EINVAL;
5806 }
5807 for (i = 0; i < imux->num_items; i++) {
5808 if (!strncmp(label, imux->items[i].label, strlen(label)))
5809 label_idx++;
5810 }
5811 if (type_idx)
5812 *type_idx = label_idx;
5813 if (label_idx > 0)
5814 snprintf(imux->items[imux->num_items].label,
5815 sizeof(imux->items[imux->num_items].label),
5816 "%s %d", label, label_idx);
5817 else
5818 strlcpy(imux->items[imux->num_items].label, label,
5819 sizeof(imux->items[imux->num_items].label));
5820 imux->items[imux->num_items].index = index;
5821 imux->num_items++;
5822 return 0;
5823}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005824EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005825
Takashi Iwai4a471b72005-12-07 13:56:29 +01005826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827#ifdef CONFIG_PM
5828/*
5829 * power management
5830 */
5831
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005832
5833static void hda_async_suspend(void *data, async_cookie_t cookie)
5834{
5835 hda_call_codec_suspend(data, false);
5836}
5837
5838static void hda_async_resume(void *data, async_cookie_t cookie)
5839{
5840 hda_call_codec_resume(data);
5841}
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843/**
5844 * snd_hda_suspend - suspend the codecs
5845 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 *
5847 * Returns 0 if successful.
5848 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005849int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005851 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005852 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Takashi Iwai0ba21762007-04-16 11:29:14 +02005854 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005855 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005856 if (hda_codec_is_power_on(codec)) {
5857 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005858 async_schedule_domain(hda_async_suspend, codec,
5859 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005860 else
5861 hda_call_codec_suspend(codec, false);
5862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005864
5865 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005866 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 return 0;
5869}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005870EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872/**
5873 * snd_hda_resume - resume the codecs
5874 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 *
5876 * Returns 0 if successful.
5877 */
5878int snd_hda_resume(struct hda_bus *bus)
5879{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005880 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005881 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Takashi Iwai0ba21762007-04-16 11:29:14 +02005883 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005884 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005885 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005886 else
5887 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005889
5890 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005891 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 return 0;
5894}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005895EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005896#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005897
5898/*
5899 * generic arrays
5900 */
5901
Takashi Iwaid5191e52009-11-16 14:58:17 +01005902/**
5903 * snd_array_new - get a new element from the given array
5904 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005905 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005906 * Get a new element from the given array. If it exceeds the
5907 * pre-allocated array size, re-allocate the array.
5908 *
5909 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005910 */
5911void *snd_array_new(struct snd_array *array)
5912{
Takashi Iwai12f17712012-10-10 08:59:14 +02005913 if (snd_BUG_ON(!array->elem_size))
5914 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005915 if (array->used >= array->alloced) {
5916 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005917 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005918 void *nlist;
5919 if (snd_BUG_ON(num >= 4096))
5920 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005921 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005922 if (!nlist)
5923 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005924 array->list = nlist;
5925 array->alloced = num;
5926 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005927 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005928}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005929EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005930
Takashi Iwaid5191e52009-11-16 14:58:17 +01005931/**
5932 * snd_array_free - free the given array elements
5933 * @array: the array object
5934 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005935void snd_array_free(struct snd_array *array)
5936{
5937 kfree(array->list);
5938 array->used = 0;
5939 array->alloced = 0;
5940 array->list = NULL;
5941}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005942EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005943
Takashi Iwaid5191e52009-11-16 14:58:17 +01005944/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005945 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5946 * @pcm: PCM caps bits
5947 * @buf: the string buffer to write
5948 * @buflen: the max buffer length
5949 *
5950 * used by hda_proc.c and hda_eld.c
5951 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005952void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5953{
5954 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5955 int i, j;
5956
5957 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5958 if (pcm & (AC_SUPPCM_BITS_8 << i))
5959 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5960
5961 buf[j] = '\0'; /* necessary when j == 0 */
5962}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005963EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005964
5965MODULE_DESCRIPTION("HDA codec core");
5966MODULE_LICENSE("GPL");