blob: b162fc40348f4aa8d02136be328a011508b1fc87 [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>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwaid5191e52009-11-16 14:58:17 +010056/**
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
59 *
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
62 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040063const char *snd_hda_get_jack_location(u32 cfg)
64{
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
67 };
68 static unsigned char specials_idx[] = {
69 0x07, 0x08,
70 0x17, 0x18, 0x19,
71 0x37, 0x38
72 };
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
77 };
78 int i;
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
80 if ((cfg & 0x0f) < 7)
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
84 return specials[i];
85 }
86 return "UNKNOWN";
87}
Takashi Iwai2698ea92013-12-18 07:45:52 +010088EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040089
Takashi Iwaid5191e52009-11-16 14:58:17 +010090/**
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
93 *
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
96 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040097const char *snd_hda_get_jack_connectivity(u32 cfg)
98{
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
100
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
102}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100103EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104
Takashi Iwaid5191e52009-11-16 14:58:17 +0100105/**
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
111 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400112const char *snd_hda_get_jack_type(u32 cfg)
113{
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200118 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119 };
120
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
123}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100124EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400125
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100126/*
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200127 * Send and receive a verb
128 */
129static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200130 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200131{
132 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200133 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200134
Wu Fengguang6430aee2009-07-17 16:49:19 +0800135 if (cmd == ~0)
136 return -1;
137
Takashi Iwai8dd78332009-06-02 01:16:07 +0200138 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200139 snd_hda_power_up(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100140 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200141 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
142 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100143 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
144 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200145 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100146 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200147 snd_hda_power_down(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100148 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200149 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100150 codec_dbg(codec,
151 "resetting BUS due to fatal communication error\n");
Takashi Iwai8dd78332009-06-02 01:16:07 +0200152 bus->ops.bus_reset(bus);
153 }
154 goto again;
155 }
156 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100157 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200158 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200159 return err;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * snd_hda_codec_read - send a command and get the response
164 * @codec: the HDA codec
165 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200166 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * @verb: the verb to send
168 * @parm: the parameter for the verb
169 *
170 * Send a single command and read the corresponding response.
171 *
172 * Returns the obtained response value, or -1 for an error.
173 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200174unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200175 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int verb, unsigned int parm)
177{
Takashi Iwai7639a062015-03-03 10:07:24 +0100178 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200179 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200180 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700181 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return res;
183}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100184EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/**
187 * snd_hda_codec_write - send a single command without waiting for response
188 * @codec: the HDA codec
189 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200190 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * @verb: the verb to send
192 * @parm: the parameter for the verb
193 *
194 * Send a single command without waiting for response.
195 *
196 * Returns 0 if successful, or a negative error code.
197 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200198int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
199 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Takashi Iwai7639a062015-03-03 10:07:24 +0100201 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100202 return codec_exec_verb(codec, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100204EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/**
207 * snd_hda_sequence_write - sequence writes
208 * @codec: the HDA codec
209 * @seq: VERB array to send
210 *
211 * Send the commands sequentially from the given array.
212 * The array must be terminated with NID=0.
213 */
214void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
215{
216 for (; seq->nid; seq++)
217 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
218}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100219EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100221/* connection list element */
222struct hda_conn_list {
223 struct list_head list;
224 int len;
225 hda_nid_t nid;
226 hda_nid_t conns[0];
227};
228
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200229/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100230static struct hda_conn_list *
231lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200232{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100233 struct hda_conn_list *p;
234 list_for_each_entry(p, &codec->conn_list, list) {
235 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200236 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200237 }
238 return NULL;
239}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200240
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100241static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
242 const hda_nid_t *list)
243{
244 struct hda_conn_list *p;
245
246 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
247 if (!p)
248 return -ENOMEM;
249 p->len = len;
250 p->nid = nid;
251 memcpy(p->conns, list, len * sizeof(hda_nid_t));
252 list_add(&p->list, &codec->conn_list);
253 return 0;
254}
255
256static void remove_conn_list(struct hda_codec *codec)
257{
258 while (!list_empty(&codec->conn_list)) {
259 struct hda_conn_list *p;
260 p = list_first_entry(&codec->conn_list, typeof(*p), list);
261 list_del(&p->list);
262 kfree(p);
263 }
264}
265
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200266/* read the connection and add to the cache */
267static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200268{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100269 hda_nid_t list[32];
270 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200271 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200272
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200273 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100274 if (len == -ENOSPC) {
275 len = snd_hda_get_num_raw_conns(codec, nid);
276 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
277 if (!result)
278 return -ENOMEM;
279 len = snd_hda_get_raw_connections(codec, nid, result, len);
280 }
281 if (len >= 0)
282 len = snd_hda_override_conn_list(codec, nid, len, result);
283 if (result != list)
284 kfree(result);
285 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200286}
Takashi Iwaidce20792011-06-24 14:10:28 +0200287
288/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100289 * snd_hda_get_conn_list - get connection list
290 * @codec: the HDA codec
291 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100292 * @listp: the pointer to store NID list
293 *
294 * Parses the connection list of the given widget and stores the pointer
295 * to the list of NIDs.
296 *
297 * Returns the number of connections, or a negative error code.
298 *
299 * Note that the returned pointer isn't protected against the list
300 * modification. If snd_hda_override_conn_list() might be called
301 * concurrently, protect with a mutex appropriately.
302 */
303int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
304 const hda_nid_t **listp)
305{
306 bool added = false;
307
308 for (;;) {
309 int err;
310 const struct hda_conn_list *p;
311
312 /* if the connection-list is already cached, read it */
313 p = lookup_conn_list(codec, nid);
314 if (p) {
315 if (listp)
316 *listp = p->conns;
317 return p->len;
318 }
319 if (snd_BUG_ON(added))
320 return -EINVAL;
321
322 err = read_and_add_raw_conns(codec, nid);
323 if (err < 0)
324 return err;
325 added = true;
326 }
327}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100328EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100329
330/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200331 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * @codec: the HDA codec
333 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200334 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * @max_conns: max. number of connections to store
336 *
337 * Parses the connection list of the given widget and stores the list
338 * of NIDs.
339 *
340 * Returns the number of connections, or a negative error code.
341 */
342int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100345 const hda_nid_t *list;
346 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348 if (len > 0 && conn_list) {
349 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100350 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200351 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 return -EINVAL;
353 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100354 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200355 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200356
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100359EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200362 * snd_hda_override_conn_list - add/modify the connection-list to cache
363 * @codec: the HDA codec
364 * @nid: NID to parse
365 * @len: number of connection list entries
366 * @list: the list of connection entries
367 *
368 * Add or modify the given connection-list to the cache. If the corresponding
369 * cache already exists, invalidate it and append a new one.
370 *
371 * Returns zero or a negative error code.
372 */
373int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
374 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100376 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200377
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100378 p = lookup_conn_list(codec, nid);
379 if (p) {
380 list_del(&p->list);
381 kfree(p);
382 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200383
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100384 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100386EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200387
388/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200389 * snd_hda_get_conn_index - get the connection index of the given NID
390 * @codec: the HDA codec
391 * @mux: NID containing the list
392 * @nid: NID to select
393 * @recursive: 1 when searching NID recursively, otherwise 0
394 *
395 * Parses the connection list of the widget @mux and checks whether the
396 * widget @nid is present. If it is, return the connection index.
397 * Otherwise it returns -1.
398 */
399int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
400 hda_nid_t nid, int recursive)
401{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100402 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200403 int i, nums;
404
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200406 for (i = 0; i < nums; i++)
407 if (conn[i] == nid)
408 return i;
409 if (!recursive)
410 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100411 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100412 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200413 return -1;
414 }
415 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200416 for (i = 0; i < nums; i++) {
417 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
418 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
419 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200420 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
421 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200422 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200423 return -1;
424}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100425EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Mengdong Linf1aa0682013-08-26 21:35:21 -0400427
428/* return DEVLIST_LEN parameter of the given widget */
429static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
430{
431 unsigned int wcaps = get_wcaps(codec, nid);
432 unsigned int parm;
433
434 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
435 get_wcaps_type(wcaps) != AC_WID_PIN)
436 return 0;
437
438 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
439 if (parm == -1 && codec->bus->rirb_error)
440 parm = 0;
441 return parm & AC_DEV_LIST_LEN_MASK;
442}
443
444/**
445 * snd_hda_get_devices - copy device list without cache
446 * @codec: the HDA codec
447 * @nid: NID of the pin to parse
448 * @dev_list: device list array
449 * @max_devices: max. number of devices to store
450 *
451 * Copy the device list. This info is dynamic and so not cached.
452 * Currently called only from hda_proc.c, so not exported.
453 */
454int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
455 u8 *dev_list, int max_devices)
456{
457 unsigned int parm;
458 int i, dev_len, devices;
459
460 parm = get_num_devices(codec, nid);
461 if (!parm) /* not multi-stream capable */
462 return 0;
463
464 dev_len = parm + 1;
465 dev_len = dev_len < max_devices ? dev_len : max_devices;
466
467 devices = 0;
468 while (devices < dev_len) {
469 parm = snd_hda_codec_read(codec, nid, 0,
470 AC_VERB_GET_DEVICE_LIST, devices);
471 if (parm == -1 && codec->bus->rirb_error)
472 break;
473
474 for (i = 0; i < 8; i++) {
475 dev_list[devices] = (u8)parm;
476 parm >>= 4;
477 devices++;
478 if (devices >= dev_len)
479 break;
480 }
481 }
482 return devices;
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * destructor
487 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100488static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200490 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (bus->ops.private_free)
493 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100494 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100498static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwai2565c892014-02-19 11:41:09 +0100500 snd_hda_bus_free(device->device_data);
501 return 0;
502}
503
504static int snd_hda_bus_dev_disconnect(struct snd_device *device)
505{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100507 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100511/* hdac_bus_ops translations */
512static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
513{
514 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
515 return bus->ops.command(bus, cmd);
516}
517
518static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
519 unsigned int *res)
520{
521 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
522 *res = bus->ops.get_response(bus, addr);
523 return bus->rirb_error ? -EIO : 0;
524}
525
526static const struct hdac_bus_ops bus_ops = {
527 .command = _hda_bus_command,
528 .get_response = _hda_bus_get_response,
529};
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/**
532 * snd_hda_bus_new - create a HDA bus
533 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * @busp: the pointer to store the created bus instance
535 *
536 * Returns 0 if successful, or a negative error code.
537 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100538int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100539 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct hda_bus *bus;
542 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100543 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100544 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 .dev_free = snd_hda_bus_dev_free,
546 };
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (busp)
549 *busp = NULL;
550
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200551 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100552 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100555 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
556 if (err < 0) {
557 kfree(bus);
558 return err;
559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200562 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Takashi Iwai0ba21762007-04-16 11:29:14 +0200564 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
565 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 snd_hda_bus_free(bus);
567 return err;
568 }
569 if (busp)
570 *busp = bus;
571 return 0;
572}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100573EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100576 * read widget caps for each widget and store in cache
577 */
578static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
579{
580 int i;
581 hda_nid_t nid;
582
Takashi Iwai7639a062015-03-03 10:07:24 +0100583 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200584 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100585 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100586 nid = codec->core.start_nid;
587 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 codec->wcaps[i] = snd_hda_param_read(codec, nid,
589 AC_PAR_AUDIO_WIDGET_CAP);
590 return 0;
591}
592
Takashi Iwai3be14142009-02-20 14:11:16 +0100593/* read all pin default configurations and save codec->init_pins */
594static int read_pin_defaults(struct hda_codec *codec)
595{
Takashi Iwai7639a062015-03-03 10:07:24 +0100596 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100597
Takashi Iwai7639a062015-03-03 10:07:24 +0100598 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100599 struct hda_pincfg *pin;
600 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200601 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100602 if (wid_type != AC_WID_PIN)
603 continue;
604 pin = snd_array_new(&codec->init_pins);
605 if (!pin)
606 return -ENOMEM;
607 pin->nid = nid;
608 pin->cfg = snd_hda_codec_read(codec, nid, 0,
609 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200610 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
611 AC_VERB_GET_PIN_WIDGET_CONTROL,
612 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100613 }
614 return 0;
615}
616
617/* look up the given pin config list and return the item matching with NID */
618static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
619 struct snd_array *array,
620 hda_nid_t nid)
621{
622 int i;
623 for (i = 0; i < array->used; i++) {
624 struct hda_pincfg *pin = snd_array_elem(array, i);
625 if (pin->nid == nid)
626 return pin;
627 }
628 return NULL;
629}
630
Takashi Iwai3be14142009-02-20 14:11:16 +0100631/* set the current pin config value for the given NID.
632 * the value is cached, and read via snd_hda_codec_get_pincfg()
633 */
634int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
635 hda_nid_t nid, unsigned int cfg)
636{
637 struct hda_pincfg *pin;
638
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200639 /* the check below may be invalid when pins are added by a fixup
640 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
641 * for now
642 */
643 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100644 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
645 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200646 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100647
Takashi Iwai3be14142009-02-20 14:11:16 +0100648 pin = look_up_pincfg(codec, list, nid);
649 if (!pin) {
650 pin = snd_array_new(list);
651 if (!pin)
652 return -ENOMEM;
653 pin->nid = nid;
654 }
655 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100656 return 0;
657}
658
Takashi Iwaid5191e52009-11-16 14:58:17 +0100659/**
660 * snd_hda_codec_set_pincfg - Override a pin default configuration
661 * @codec: the HDA codec
662 * @nid: NID to set the pin config
663 * @cfg: the pin default config value
664 *
665 * Override a pin default configuration value in the cache.
666 * This value can be read by snd_hda_codec_get_pincfg() in a higher
667 * priority than the real hardware value.
668 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100669int snd_hda_codec_set_pincfg(struct hda_codec *codec,
670 hda_nid_t nid, unsigned int cfg)
671{
Takashi Iwai346ff702009-02-23 09:42:57 +0100672 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100673}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100674EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100675
Takashi Iwaid5191e52009-11-16 14:58:17 +0100676/**
677 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
678 * @codec: the HDA codec
679 * @nid: NID to get the pin config
680 *
681 * Get the current pin config value of the given pin NID.
682 * If the pincfg value is cached or overridden via sysfs or driver,
683 * returns the cached value.
684 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100685unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
686{
687 struct hda_pincfg *pin;
688
Takashi Iwai648a8d22014-02-25 10:38:13 +0100689#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100690 {
691 unsigned int cfg = 0;
692 mutex_lock(&codec->user_mutex);
693 pin = look_up_pincfg(codec, &codec->user_pins, nid);
694 if (pin)
695 cfg = pin->cfg;
696 mutex_unlock(&codec->user_mutex);
697 if (cfg)
698 return cfg;
699 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100700#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100701 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
702 if (pin)
703 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100704 pin = look_up_pincfg(codec, &codec->init_pins, nid);
705 if (pin)
706 return pin->cfg;
707 return 0;
708}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100709EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100710
Takashi Iwai95a962c2014-10-29 16:03:58 +0100711/**
712 * snd_hda_codec_set_pin_target - remember the current pinctl target value
713 * @codec: the HDA codec
714 * @nid: pin NID
715 * @val: assigned pinctl value
716 *
717 * This function stores the given value to a pinctl target value in the
718 * pincfg table. This isn't always as same as the actually written value
719 * but can be referred at any time via snd_hda_codec_get_pin_target().
720 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100721int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
722 unsigned int val)
723{
724 struct hda_pincfg *pin;
725
726 pin = look_up_pincfg(codec, &codec->init_pins, nid);
727 if (!pin)
728 return -EINVAL;
729 pin->target = val;
730 return 0;
731}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100732EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100733
Takashi Iwai95a962c2014-10-29 16:03:58 +0100734/**
735 * snd_hda_codec_get_pin_target - return the current pinctl target value
736 * @codec: the HDA codec
737 * @nid: pin NID
738 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100739int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
740{
741 struct hda_pincfg *pin;
742
743 pin = look_up_pincfg(codec, &codec->init_pins, nid);
744 if (!pin)
745 return 0;
746 return pin->target;
747}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100748EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100749
Takashi Iwai92ee6162009-12-27 11:18:59 +0100750/**
751 * snd_hda_shutup_pins - Shut up all pins
752 * @codec: the HDA codec
753 *
754 * Clear all pin controls to shup up before suspend for avoiding click noise.
755 * The controls aren't cached so that they can be resumed properly.
756 */
757void snd_hda_shutup_pins(struct hda_codec *codec)
758{
759 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200760 /* don't shut up pins when unloading the driver; otherwise it breaks
761 * the default pin setup at the next load of the driver
762 */
763 if (codec->bus->shutdown)
764 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100765 for (i = 0; i < codec->init_pins.used; i++) {
766 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
767 /* use read here for syncing after issuing each verb */
768 snd_hda_codec_read(codec, pin->nid, 0,
769 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
770 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200771 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100772}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100773EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100774
Takashi Iwai2a439522011-07-26 09:52:50 +0200775#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200776/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
777static void restore_shutup_pins(struct hda_codec *codec)
778{
779 int i;
780 if (!codec->pins_shutup)
781 return;
782 if (codec->bus->shutdown)
783 return;
784 for (i = 0; i < codec->init_pins.used; i++) {
785 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
786 snd_hda_codec_write(codec, pin->nid, 0,
787 AC_VERB_SET_PIN_WIDGET_CONTROL,
788 pin->ctrl);
789 }
790 codec->pins_shutup = 0;
791}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700792#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200793
David Henningsson26a6cb62012-10-09 15:04:21 +0200794static void hda_jackpoll_work(struct work_struct *work)
795{
796 struct hda_codec *codec =
797 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200798
799 snd_hda_jack_set_dirty_all(codec);
800 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400801
802 if (!codec->jackpoll_interval)
803 return;
804
Takashi Iwai2f35c632015-02-27 22:43:26 +0100805 schedule_delayed_work(&codec->jackpoll_work,
806 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200807}
808
Takashi Iwai01751f52007-08-10 16:59:39 +0200809static void init_hda_cache(struct hda_cache_rec *cache,
810 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200811static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200812
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100813/* release all pincfg lists */
814static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100815{
Takashi Iwai346ff702009-02-23 09:42:57 +0100816 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100817#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100818 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100819#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100820 snd_array_free(&codec->init_pins);
821}
822
Takashi Iwai54d17402005-11-21 16:33:22 +0100823/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200824 * audio-converter setup caches
825 */
826struct hda_cvt_setup {
827 hda_nid_t nid;
828 u8 stream_tag;
829 u8 channel_id;
830 u16 format_id;
831 unsigned char active; /* cvt is currently used */
832 unsigned char dirty; /* setups should be cleared */
833};
834
835/* get or create a cache entry for the given audio converter NID */
836static struct hda_cvt_setup *
837get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
838{
839 struct hda_cvt_setup *p;
840 int i;
841
842 for (i = 0; i < codec->cvt_setups.used; i++) {
843 p = snd_array_elem(&codec->cvt_setups, i);
844 if (p->nid == nid)
845 return p;
846 }
847 p = snd_array_new(&codec->cvt_setups);
848 if (p)
849 p->nid = nid;
850 return p;
851}
852
853/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100854 * PCM device
855 */
856static void release_pcm(struct kref *kref)
857{
858 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
859
860 if (pcm->pcm)
861 snd_device_free(pcm->codec->card, pcm->pcm);
862 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
863 kfree(pcm->name);
864 kfree(pcm);
865}
866
867void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
868{
869 kref_put(&pcm->kref, release_pcm);
870}
871EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
872
873struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
874 const char *fmt, ...)
875{
876 struct hda_pcm *pcm;
877 va_list args;
878
879 va_start(args, fmt);
880 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
881 if (!pcm)
882 return NULL;
883
884 pcm->codec = codec;
885 kref_init(&pcm->kref);
886 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
887 if (!pcm->name) {
888 kfree(pcm);
889 return NULL;
890 }
891
892 list_add_tail(&pcm->list, &codec->pcm_list_head);
893 return pcm;
894}
895EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
896
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100897/*
898 * codec destructor
899 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100900static void codec_release_pcms(struct hda_codec *codec)
901{
902 struct hda_pcm *pcm, *n;
903
904 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
905 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100906 if (pcm->pcm)
907 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100908 snd_hda_codec_pcm_put(pcm);
909 }
910}
911
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100912void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
913{
914 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100915 if (!codec->in_freeing)
916 snd_hda_ctls_clear(codec);
917 codec_release_pcms(codec);
918 snd_hda_detach_beep_device(codec);
919 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
920 snd_hda_jack_tbl_clear(codec);
921 codec->proc_widget_hook = NULL;
922 codec->spec = NULL;
923
924 free_hda_cache(&codec->amp_cache);
925 free_hda_cache(&codec->cmd_cache);
926 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
927 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
928
929 /* free only driver_pins so that init_pins + user_pins are restored */
930 snd_array_free(&codec->driver_pins);
931 snd_array_free(&codec->cvt_setups);
932 snd_array_free(&codec->spdif_out);
933 snd_array_free(&codec->verbs);
934 codec->preset = NULL;
935 codec->slave_dig_outs = NULL;
936 codec->spdif_status_reset = 0;
937 snd_array_free(&codec->mixers);
938 snd_array_free(&codec->nids);
939 remove_conn_list(codec);
940}
941
Takashi Iwaid8193872012-08-31 07:54:38 -0700942static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100943 unsigned int power_state);
944
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100945static int snd_hda_codec_dev_register(struct snd_device *device)
946{
947 struct hda_codec *codec = device->device_data;
948
Takashi Iwaid604b392014-02-28 13:42:09 +0100949 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +0100950 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +0100951 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwai709949f2015-02-20 09:58:14 +0100952 /* it was powered up in snd_hda_codec_new(), now all done */
953 snd_hda_power_down(codec);
Takashi Iwaid604b392014-02-28 13:42:09 +0100954 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100955}
956
957static int snd_hda_codec_dev_disconnect(struct snd_device *device)
958{
959 struct hda_codec *codec = device->device_data;
960
Takashi Iwaid604b392014-02-28 13:42:09 +0100961 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100962 return 0;
963}
964
Takashi Iwai2565c892014-02-19 11:41:09 +0100965static int snd_hda_codec_dev_free(struct snd_device *device)
966{
Takashi Iwaid56db742015-02-27 23:25:35 +0100967 struct hda_codec *codec = device->device_data;
968
969 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100970 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100971 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100972 return 0;
973}
974
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100975static void snd_hda_codec_dev_release(struct device *dev)
976{
Takashi Iwaid56db742015-02-27 23:25:35 +0100977 struct hda_codec *codec = dev_to_hda_codec(dev);
978
979 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100980 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100981 snd_hda_sysfs_clear(codec);
982 free_hda_cache(&codec->amp_cache);
983 free_hda_cache(&codec->cmd_cache);
Takashi Iwaid56db742015-02-27 23:25:35 +0100984 kfree(codec->modelname);
985 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100986 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
990 * snd_hda_codec_new - create a HDA codec
991 * @bus: the bus to assign
992 * @codec_addr: the codec address
993 * @codecp: the pointer to store the generated codec
994 *
995 * Returns 0 if successful, or a negative error code.
996 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100997int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
998 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001001 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001002 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001004 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001005 .dev_register = snd_hda_codec_dev_register,
1006 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001007 .dev_free = snd_hda_codec_dev_free,
1008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Takashi Iwaida3cec32008-08-08 17:12:14 +02001010 if (snd_BUG_ON(!bus))
1011 return -EINVAL;
1012 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001015 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001016 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Takashi Iwai7639a062015-03-03 10:07:24 +01001019 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1020 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1021 codec_addr);
1022 if (err < 0) {
1023 kfree(codec);
1024 return err;
1025 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001026
Takashi Iwai7639a062015-03-03 10:07:24 +01001027 codec->core.dev.release = snd_hda_codec_dev_release;
1028 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001031 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001033 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001034 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001035 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001036 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001037 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001038 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1039 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001040 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001041 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001042 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001043 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001044 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001045 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001046 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001047 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001048
David Henningsson26a6cb62012-10-09 15:04:21 +02001049 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001050 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001051 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Takashi Iwai83012a72012-08-24 18:38:08 +02001053#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001054 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001055#endif
1056
Takashi Iwai648a8d22014-02-25 10:38:13 +01001057 snd_hda_sysfs_init(codec);
1058
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001059 if (codec->bus->modelname) {
1060 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1061 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001062 err = -ENODEV;
1063 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001064 }
1065 }
1066
Takashi Iwai7639a062015-03-03 10:07:24 +01001067 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001068 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001069 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 err = read_pin_defaults(codec);
1072 if (err < 0)
1073 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001074
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001075 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001076 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001077
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001078 snd_hda_codec_proc_new(codec);
1079
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001080 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001081
Takashi Iwai7639a062015-03-03 10:07:24 +01001082 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1083 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001084 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001085
Takashi Iwai6efdd852015-02-27 16:09:22 +01001086 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001087 if (err < 0)
1088 goto error;
1089
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001090 if (codecp)
1091 *codecp = codec;
1092 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001093
1094 error:
Takashi Iwai7639a062015-03-03 10:07:24 +01001095 pm_runtime_put_noidle(hda_codec_dev(codec));
Takashi Iwaid56db742015-02-27 23:25:35 +01001096 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001097 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001098}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001099EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001100
Takashi Iwai95a962c2014-10-29 16:03:58 +01001101/**
1102 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1103 * @codec: the HDA codec
1104 *
1105 * Forcibly refresh the all widget caps and the init pin configurations of
1106 * the given codec.
1107 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001108int snd_hda_codec_update_widgets(struct hda_codec *codec)
1109{
1110 hda_nid_t fg;
1111 int err;
1112
Takashi Iwai7639a062015-03-03 10:07:24 +01001113 err = snd_hdac_refresh_widgets(&codec->core);
1114 if (err < 0)
1115 return err;
1116
Mengdong Lina15d05d2013-02-08 17:09:31 -05001117 /* Assume the function group node does not change,
1118 * only the widget nodes may change.
1119 */
1120 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001121 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001122 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001123 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001124 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001125
1126 snd_array_free(&codec->init_pins);
1127 err = read_pin_defaults(codec);
1128
1129 return err;
1130}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001131EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001132
Takashi Iwaied360812012-08-08 17:12:52 +02001133/* update the stream-id if changed */
1134static void update_pcm_stream_id(struct hda_codec *codec,
1135 struct hda_cvt_setup *p, hda_nid_t nid,
1136 u32 stream_tag, int channel_id)
1137{
1138 unsigned int oldval, newval;
1139
1140 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1141 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1142 newval = (stream_tag << 4) | channel_id;
1143 if (oldval != newval)
1144 snd_hda_codec_write(codec, nid, 0,
1145 AC_VERB_SET_CHANNEL_STREAMID,
1146 newval);
1147 p->stream_tag = stream_tag;
1148 p->channel_id = channel_id;
1149 }
1150}
1151
1152/* update the format-id if changed */
1153static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1154 hda_nid_t nid, int format)
1155{
1156 unsigned int oldval;
1157
1158 if (p->format_id != format) {
1159 oldval = snd_hda_codec_read(codec, nid, 0,
1160 AC_VERB_GET_STREAM_FORMAT, 0);
1161 if (oldval != format) {
1162 msleep(1);
1163 snd_hda_codec_write(codec, nid, 0,
1164 AC_VERB_SET_STREAM_FORMAT,
1165 format);
1166 }
1167 p->format_id = format;
1168 }
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/**
1172 * snd_hda_codec_setup_stream - set up the codec for streaming
1173 * @codec: the CODEC to set up
1174 * @nid: the NID to set up
1175 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1176 * @channel_id: channel id to pass, zero based.
1177 * @format: stream format.
1178 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1180 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int channel_id, int format)
1182{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001183 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001184 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001185 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001186 int i;
1187
Takashi Iwai0ba21762007-04-16 11:29:14 +02001188 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001189 return;
1190
Takashi Iwai4e76a882014-02-25 12:21:03 +01001191 codec_dbg(codec,
1192 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1193 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001194 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001195 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001196 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001197
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001198 if (codec->patch_ops.stream_pm)
1199 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001200 if (codec->pcm_format_first)
1201 update_pcm_format(codec, p, nid, format);
1202 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1203 if (!codec->pcm_format_first)
1204 update_pcm_format(codec, p, nid, format);
1205
Takashi Iwaieb541332010-08-06 13:48:11 +02001206 p->active = 1;
1207 p->dirty = 0;
1208
1209 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001210 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001211 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001212 for (i = 0; i < c->cvt_setups.used; i++) {
1213 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001214 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001215 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001216 p->dirty = 1;
1217 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001220EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Takashi Iwaif0cea792010-08-13 11:56:53 +02001222static void really_cleanup_stream(struct hda_codec *codec,
1223 struct hda_cvt_setup *q);
1224
Takashi Iwaid5191e52009-11-16 14:58:17 +01001225/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001226 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001227 * @codec: the CODEC to clean up
1228 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001229 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001230 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001231void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1232 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001233{
Takashi Iwaieb541332010-08-06 13:48:11 +02001234 struct hda_cvt_setup *p;
1235
Takashi Iwai888afa12008-03-18 09:57:50 +01001236 if (!nid)
1237 return;
1238
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001239 if (codec->no_sticky_stream)
1240 do_now = 1;
1241
Takashi Iwai4e76a882014-02-25 12:21:03 +01001242 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001243 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001244 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001245 /* here we just clear the active flag when do_now isn't set;
1246 * actual clean-ups will be done later in
1247 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1248 */
1249 if (do_now)
1250 really_cleanup_stream(codec, p);
1251 else
1252 p->active = 0;
1253 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001255EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001256
Takashi Iwaieb541332010-08-06 13:48:11 +02001257static void really_cleanup_stream(struct hda_codec *codec,
1258 struct hda_cvt_setup *q)
1259{
1260 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001261 if (q->stream_tag || q->channel_id)
1262 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1263 if (q->format_id)
1264 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1265);
Takashi Iwaieb541332010-08-06 13:48:11 +02001266 memset(q, 0, sizeof(*q));
1267 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001268 if (codec->patch_ops.stream_pm)
1269 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001270}
1271
1272/* clean up the all conflicting obsolete streams */
1273static void purify_inactive_streams(struct hda_codec *codec)
1274{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001275 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001276 int i;
1277
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001278 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001279 for (i = 0; i < c->cvt_setups.used; i++) {
1280 struct hda_cvt_setup *p;
1281 p = snd_array_elem(&c->cvt_setups, i);
1282 if (p->dirty)
1283 really_cleanup_stream(c, p);
1284 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001285 }
1286}
1287
Takashi Iwai2a439522011-07-26 09:52:50 +02001288#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001289/* clean up all streams; called from suspend */
1290static void hda_cleanup_all_streams(struct hda_codec *codec)
1291{
1292 int i;
1293
1294 for (i = 0; i < codec->cvt_setups.used; i++) {
1295 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1296 if (p->stream_tag)
1297 really_cleanup_stream(codec, p);
1298 }
1299}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001300#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
1303 * amp access functions
1304 */
1305
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001306/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001307#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001308#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001309#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1310#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001312#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001315static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001316 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwai01751f52007-08-10 16:59:39 +02001318 memset(cache, 0, sizeof(*cache));
1319 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001320 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001321}
1322
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001323static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001324{
Takashi Iwai603c4012008-07-30 15:01:44 +02001325 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001329static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Takashi Iwai01751f52007-08-10 16:59:39 +02001331 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1332 u16 cur = cache->hash[idx];
1333 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001336 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (info->key == key)
1338 return info;
1339 cur = info->next;
1340 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001341 return NULL;
1342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001344/* query the hash. allocate an entry if not found. */
1345static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1346 u32 key)
1347{
1348 struct hda_cache_head *info = get_hash(cache, key);
1349 if (!info) {
1350 u16 idx, cur;
1351 /* add a new hash entry */
1352 info = snd_array_new(&cache->buf);
1353 if (!info)
1354 return NULL;
1355 cur = snd_array_index(&cache->buf, info);
1356 info->key = key;
1357 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001358 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001359 idx = key % (u16)ARRAY_SIZE(cache->hash);
1360 info->next = cache->hash[idx];
1361 cache->hash[idx] = cur;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return info;
1364}
1365
Takashi Iwai01751f52007-08-10 16:59:39 +02001366/* query and allocate an amp hash entry */
1367static inline struct hda_amp_info *
1368get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1369{
1370 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1371}
1372
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001373/* overwrite the value with the key in the caps hash */
1374static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1375{
1376 struct hda_amp_info *info;
1377
1378 mutex_lock(&codec->hash_mutex);
1379 info = get_alloc_amp_hash(codec, key);
1380 if (!info) {
1381 mutex_unlock(&codec->hash_mutex);
1382 return -EINVAL;
1383 }
1384 info->amp_caps = val;
1385 info->head.val |= INFO_AMP_CAPS;
1386 mutex_unlock(&codec->hash_mutex);
1387 return 0;
1388}
1389
1390/* query the value from the caps hash; if not found, fetch the current
1391 * value from the given function and store in the hash
1392 */
1393static unsigned int
1394query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1395 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1396{
1397 struct hda_amp_info *info;
1398 unsigned int val;
1399
1400 mutex_lock(&codec->hash_mutex);
1401 info = get_alloc_amp_hash(codec, key);
1402 if (!info) {
1403 mutex_unlock(&codec->hash_mutex);
1404 return 0;
1405 }
1406 if (!(info->head.val & INFO_AMP_CAPS)) {
1407 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1408 val = func(codec, nid, dir);
1409 write_caps_hash(codec, key, val);
1410 } else {
1411 val = info->amp_caps;
1412 mutex_unlock(&codec->hash_mutex);
1413 }
1414 return val;
1415}
1416
1417static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1418 int direction)
1419{
1420 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Takashi Iwai7639a062015-03-03 10:07:24 +01001421 nid = codec->core.afg;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001422 return snd_hda_param_read(codec, nid,
1423 direction == HDA_OUTPUT ?
1424 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1425}
1426
Takashi Iwaid5191e52009-11-16 14:58:17 +01001427/**
1428 * query_amp_caps - query AMP capabilities
1429 * @codec: the HD-auio codec
1430 * @nid: the NID to query
1431 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1432 *
1433 * Query AMP capabilities for the given widget and direction.
1434 * Returns the obtained capability bits.
1435 *
1436 * When cap bits have been already read, this doesn't read again but
1437 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001439u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001441 return query_caps_hash(codec, nid, direction,
1442 HDA_HASH_KEY(nid, direction, 0),
1443 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001445EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Takashi Iwaid5191e52009-11-16 14:58:17 +01001447/**
David Henningsson861a04e2014-09-23 10:38:17 +02001448 * snd_hda_check_amp_caps - query AMP capabilities
1449 * @codec: the HD-audio codec
1450 * @nid: the NID to query
1451 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001452 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001453 *
1454 * Check whether the widget has the given amp capability for the direction.
1455 */
1456bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1457 int dir, unsigned int bits)
1458{
1459 if (!nid)
1460 return false;
1461 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1462 if (query_amp_caps(codec, nid, dir) & bits)
1463 return true;
1464 return false;
1465}
1466EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1467
1468/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001469 * snd_hda_override_amp_caps - Override the AMP capabilities
1470 * @codec: the CODEC to clean up
1471 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001472 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001473 * @caps: the capability bits to set
1474 *
1475 * Override the cached AMP caps bits value by the given one.
1476 * This function is useful if the driver needs to adjust the AMP ranges,
1477 * e.g. limit to 0dB, etc.
1478 *
1479 * Returns zero if successful or a negative error code.
1480 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001481int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1482 unsigned int caps)
1483{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001484 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001485}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001486EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001487
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001488static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1489 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001490{
1491 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1492}
1493
Takashi Iwaid5191e52009-11-16 14:58:17 +01001494/**
1495 * snd_hda_query_pin_caps - Query PIN capabilities
1496 * @codec: the HD-auio codec
1497 * @nid: the NID to query
1498 *
1499 * Query PIN capabilities for the given widget.
1500 * Returns the obtained capability bits.
1501 *
1502 * When cap bits have been already read, this doesn't read again but
1503 * returns the cached value.
1504 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001505u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1506{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001507 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001508 read_pin_cap);
1509}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001510EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001511
Wu Fengguang864f92b2009-11-18 12:38:02 +08001512/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001513 * snd_hda_override_pin_caps - Override the pin capabilities
1514 * @codec: the CODEC
1515 * @nid: the NID to override
1516 * @caps: the capability bits to set
1517 *
1518 * Override the cached PIN capabilitiy bits value by the given one.
1519 *
1520 * Returns zero if successful or a negative error code.
1521 */
1522int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1523 unsigned int caps)
1524{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001525 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001527EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001528
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001529/* read or sync the hash value with the current value;
1530 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001532static struct hda_amp_info *
1533update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001534 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001536 struct hda_amp_info *info;
1537 unsigned int parm, val = 0;
1538 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001540 retry:
1541 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1542 if (!info)
1543 return NULL;
1544 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1545 if (!val_read) {
1546 mutex_unlock(&codec->hash_mutex);
1547 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1548 parm |= direction == HDA_OUTPUT ?
1549 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1550 parm |= index;
1551 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001552 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001553 val &= 0xff;
1554 val_read = true;
1555 mutex_lock(&codec->hash_mutex);
1556 goto retry;
1557 }
1558 info->vol[ch] = val;
1559 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001560 } else if (init_only)
1561 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001562 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001566 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001568static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569 hda_nid_t nid, int ch, int direction, int index,
1570 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 u32 parm;
1573
1574 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1575 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1576 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001577 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1578 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001579 ; /* set the zero value as a fake mute */
1580 else
1581 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1583}
1584
Takashi Iwaid5191e52009-11-16 14:58:17 +01001585/**
1586 * snd_hda_codec_amp_read - Read AMP value
1587 * @codec: HD-audio codec
1588 * @nid: NID to read the AMP value
1589 * @ch: channel (left=0 or right=1)
1590 * @direction: #HDA_INPUT or #HDA_OUTPUT
1591 * @index: the index value (only for input direction)
1592 *
1593 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
Takashi Iwai834be882006-03-01 14:16:17 +01001595int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1596 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001598 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001599 unsigned int val = 0;
1600
1601 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001602 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001603 if (info)
1604 val = info->vol[ch];
1605 mutex_unlock(&codec->hash_mutex);
1606 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001608EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwai280e57d2012-12-14 10:32:21 +01001610static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1611 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001612 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001613{
1614 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001615 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001616
1617 if (snd_BUG_ON(mask & ~0xff))
1618 mask &= 0xff;
1619 val &= mask;
1620
1621 mutex_lock(&codec->hash_mutex);
1622 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1623 if (!info) {
1624 mutex_unlock(&codec->hash_mutex);
1625 return 0;
1626 }
1627 val |= info->vol[ch] & ~mask;
1628 if (info->vol[ch] == val) {
1629 mutex_unlock(&codec->hash_mutex);
1630 return 0;
1631 }
1632 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001633 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001634 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001635 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001636 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001637 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001638 return 1;
1639}
1640
Takashi Iwaid5191e52009-11-16 14:58:17 +01001641/**
1642 * snd_hda_codec_amp_update - update the AMP value
1643 * @codec: HD-audio codec
1644 * @nid: NID to read the AMP value
1645 * @ch: channel (left=0 or right=1)
1646 * @direction: #HDA_INPUT or #HDA_OUTPUT
1647 * @idx: the index value (only for input direction)
1648 * @mask: bit mask to set
1649 * @val: the bits value to set
1650 *
1651 * Update the AMP value with a bit mask.
1652 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001653 */
Takashi Iwai834be882006-03-01 14:16:17 +01001654int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1655 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001657 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1658 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001660EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Takashi Iwaid5191e52009-11-16 14:58:17 +01001662/**
1663 * snd_hda_codec_amp_stereo - update the AMP stereo values
1664 * @codec: HD-audio codec
1665 * @nid: NID to read the AMP value
1666 * @direction: #HDA_INPUT or #HDA_OUTPUT
1667 * @idx: the index value (only for input direction)
1668 * @mask: bit mask to set
1669 * @val: the bits value to set
1670 *
1671 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1672 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001673 */
1674int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1675 int direction, int idx, int mask, int val)
1676{
1677 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001678
1679 if (snd_BUG_ON(mask & ~0xff))
1680 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001681 for (ch = 0; ch < 2; ch++)
1682 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1683 idx, mask, val);
1684 return ret;
1685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001686EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001687
Takashi Iwai95a962c2014-10-29 16:03:58 +01001688/**
1689 * snd_hda_codec_amp_init - initialize the AMP value
1690 * @codec: the HDA codec
1691 * @nid: NID to read the AMP value
1692 * @ch: channel (left=0 or right=1)
1693 * @dir: #HDA_INPUT or #HDA_OUTPUT
1694 * @idx: the index value (only for input direction)
1695 * @mask: bit mask to set
1696 * @val: the bits value to set
1697 *
1698 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001699 * the first access. If the amp was already initialized / updated beforehand,
1700 * this does nothing.
1701 */
1702int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1703 int dir, int idx, int mask, int val)
1704{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001705 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1706 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001707}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001708EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001709
Takashi Iwai95a962c2014-10-29 16:03:58 +01001710/**
1711 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1712 * @codec: the HDA codec
1713 * @nid: NID to read the AMP value
1714 * @dir: #HDA_INPUT or #HDA_OUTPUT
1715 * @idx: the index value (only for input direction)
1716 * @mask: bit mask to set
1717 * @val: the bits value to set
1718 *
1719 * Call snd_hda_codec_amp_init() for both stereo channels.
1720 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001721int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1722 int dir, int idx, int mask, int val)
1723{
1724 int ch, ret = 0;
1725
1726 if (snd_BUG_ON(mask & ~0xff))
1727 mask &= 0xff;
1728 for (ch = 0; ch < 2; ch++)
1729 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1730 idx, mask, val);
1731 return ret;
1732}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001733EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001734
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735/**
1736 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1737 * @codec: HD-audio codec
1738 *
1739 * Resume the all amp commands from the cache.
1740 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001741void snd_hda_codec_resume_amp(struct hda_codec *codec)
1742{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001743 int i;
1744
Takashi Iwaic370dd62012-12-13 18:30:04 +01001745 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001746 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001747 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1748 struct hda_amp_info *buffer;
1749 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001750 hda_nid_t nid;
1751 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001752 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001753
1754 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01001755 if (!buffer->head.dirty)
1756 continue;
1757 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001758 info = *buffer;
1759 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001760 if (!key)
1761 continue;
1762 nid = key & 0xff;
1763 idx = (key >> 16) & 0xff;
1764 dir = (key >> 24) & 0xff;
1765 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001766 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001767 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001768 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001769 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
1770 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001771 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001772 }
1773 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001774 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001776EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001778static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1779 unsigned int ofs)
1780{
1781 u32 caps = query_amp_caps(codec, nid, dir);
1782 /* get num steps */
1783 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1784 if (ofs < caps)
1785 caps -= ofs;
1786 return caps;
1787}
1788
Takashi Iwaid5191e52009-11-16 14:58:17 +01001789/**
1790 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001791 * @kcontrol: referred ctl element
1792 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001793 *
1794 * The control element is supposed to have the private_value field
1795 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1801 u16 nid = get_amp_nid(kcontrol);
1802 u8 chs = get_amp_channels(kcontrol);
1803 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001804 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001806 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1807 uinfo->count = chs == 3 ? 2 : 1;
1808 uinfo->value.integer.min = 0;
1809 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1810 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001811 codec_warn(codec,
1812 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1813 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return -EINVAL;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001818EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001820
1821static inline unsigned int
1822read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1823 int ch, int dir, int idx, unsigned int ofs)
1824{
1825 unsigned int val;
1826 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1827 val &= HDA_AMP_VOLMASK;
1828 if (val >= ofs)
1829 val -= ofs;
1830 else
1831 val = 0;
1832 return val;
1833}
1834
1835static inline int
1836update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1837 int ch, int dir, int idx, unsigned int ofs,
1838 unsigned int val)
1839{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001840 unsigned int maxval;
1841
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001842 if (val > 0)
1843 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001844 /* ofs = 0: raw max value */
1845 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001846 if (val > maxval)
1847 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001848 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
1849 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001850}
1851
Takashi Iwaid5191e52009-11-16 14:58:17 +01001852/**
1853 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001854 * @kcontrol: ctl element
1855 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001856 *
1857 * The control element is supposed to have the private_value field
1858 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001860int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1864 hda_nid_t nid = get_amp_nid(kcontrol);
1865 int chs = get_amp_channels(kcontrol);
1866 int dir = get_amp_direction(kcontrol);
1867 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001868 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 long *valp = ucontrol->value.integer.value;
1870
1871 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001872 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001874 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001877EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Takashi Iwaid5191e52009-11-16 14:58:17 +01001879/**
1880 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001881 * @kcontrol: ctl element
1882 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001883 *
1884 * The control element is supposed to have the private_value field
1885 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1886 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001887int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1888 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1891 hda_nid_t nid = get_amp_nid(kcontrol);
1892 int chs = get_amp_channels(kcontrol);
1893 int dir = get_amp_direction(kcontrol);
1894 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001895 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 long *valp = ucontrol->value.integer.value;
1897 int change = 0;
1898
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001899 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001900 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001901 valp++;
1902 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001903 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001904 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return change;
1906}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001907EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Takashi Iwaid5191e52009-11-16 14:58:17 +01001909/**
1910 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001911 * @kcontrol: ctl element
1912 * @op_flag: operation flag
1913 * @size: byte size of input TLV
1914 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001915 *
1916 * The control element is supposed to have the private_value field
1917 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1918 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001919int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1920 unsigned int size, unsigned int __user *_tlv)
1921{
1922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1923 hda_nid_t nid = get_amp_nid(kcontrol);
1924 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001925 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001926 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001927 u32 caps, val1, val2;
1928
1929 if (size < 4 * sizeof(unsigned int))
1930 return -ENOMEM;
1931 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001932 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1933 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001934 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001935 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001936 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001937 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001938 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001939 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1940 return -EFAULT;
1941 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1942 return -EFAULT;
1943 if (put_user(val1, _tlv + 2))
1944 return -EFAULT;
1945 if (put_user(val2, _tlv + 3))
1946 return -EFAULT;
1947 return 0;
1948}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001949EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001950
Takashi Iwaid5191e52009-11-16 14:58:17 +01001951/**
1952 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1953 * @codec: HD-audio codec
1954 * @nid: NID of a reference widget
1955 * @dir: #HDA_INPUT or #HDA_OUTPUT
1956 * @tlv: TLV data to be stored, at least 4 elements
1957 *
1958 * Set (static) TLV data for a virtual master volume using the AMP caps
1959 * obtained from the reference NID.
1960 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001961 */
1962void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1963 unsigned int *tlv)
1964{
1965 u32 caps;
1966 int nums, step;
1967
1968 caps = query_amp_caps(codec, nid, dir);
1969 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1970 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1971 step = (step + 1) * 25;
1972 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1973 tlv[1] = 2 * sizeof(unsigned int);
1974 tlv[2] = -nums * step;
1975 tlv[3] = step;
1976}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001977EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001978
1979/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001980static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001981find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001982{
1983 struct snd_ctl_elem_id id;
1984 memset(&id, 0, sizeof(id));
1985 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001986 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001987 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001988 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1989 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001990 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001991 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001992}
1993
Takashi Iwaid5191e52009-11-16 14:58:17 +01001994/**
1995 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1996 * @codec: HD-audio codec
1997 * @name: ctl id name string
1998 *
1999 * Get the control element with the given id string and IFACE_MIXER.
2000 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002001struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2002 const char *name)
2003{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002004 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002005}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002006EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002007
Takashi Iwaidcda5802012-10-12 17:24:51 +02002008static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002009 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002010{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002011 int i, idx;
2012 /* 16 ctlrs should be large enough */
2013 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2014 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002015 return idx;
2016 }
2017 return -EBUSY;
2018}
2019
Takashi Iwaid5191e52009-11-16 14:58:17 +01002020/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002021 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002022 * @codec: HD-audio codec
2023 * @nid: corresponding NID (optional)
2024 * @kctl: the control element to assign
2025 *
2026 * Add the given control element to an array inside the codec instance.
2027 * All control elements belonging to a codec are supposed to be added
2028 * by this function so that a proper clean-up works at the free or
2029 * reconfiguration time.
2030 *
2031 * If non-zero @nid is passed, the NID is assigned to the control element.
2032 * The assignment is shown in the codec proc file.
2033 *
2034 * snd_hda_ctl_add() checks the control subdev id field whether
2035 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002036 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2037 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002039int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2040 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002041{
2042 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002043 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002044 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002045
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002046 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002047 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002048 if (nid == 0)
2049 nid = get_amp_nid_(kctl->private_value);
2050 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002051 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2052 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002053 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002054 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002055 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002056 if (err < 0)
2057 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002058 item = snd_array_new(&codec->mixers);
2059 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002060 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002061 item->kctl = kctl;
2062 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002063 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002064 return 0;
2065}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002066EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002067
Takashi Iwaid5191e52009-11-16 14:58:17 +01002068/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002069 * snd_hda_add_nid - Assign a NID to a control element
2070 * @codec: HD-audio codec
2071 * @nid: corresponding NID (optional)
2072 * @kctl: the control element to assign
2073 * @index: index to kctl
2074 *
2075 * Add the given control element to an array inside the codec instance.
2076 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2077 * NID:KCTL mapping - for example "Capture Source" selector.
2078 */
2079int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2080 unsigned int index, hda_nid_t nid)
2081{
2082 struct hda_nid_item *item;
2083
2084 if (nid > 0) {
2085 item = snd_array_new(&codec->nids);
2086 if (!item)
2087 return -ENOMEM;
2088 item->kctl = kctl;
2089 item->index = index;
2090 item->nid = nid;
2091 return 0;
2092 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002093 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2094 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002095 return -EINVAL;
2096}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002097EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002098
2099/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002100 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2101 * @codec: HD-audio codec
2102 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002103void snd_hda_ctls_clear(struct hda_codec *codec)
2104{
2105 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002106 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002107 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002108 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002109 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002110 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002111}
2112
Takashi Iwai95a962c2014-10-29 16:03:58 +01002113/**
2114 * snd_hda_lock_devices - pseudo device locking
2115 * @bus: the BUS
2116 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002117 * toggle card->shutdown to allow/disallow the device access (as a hack)
2118 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002119int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002120{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002121 struct snd_card *card = bus->card;
2122 struct hda_codec *codec;
2123
Takashi Iwaia65d6292009-02-23 16:57:04 +01002124 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002125 if (card->shutdown)
2126 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002127 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002128 if (!list_empty(&card->ctl_files))
2129 goto err_clear;
2130
Takashi Iwaid068ebc2015-03-02 23:22:59 +01002131 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002132 struct hda_pcm *cpcm;
2133 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002134 if (!cpcm->pcm)
2135 continue;
2136 if (cpcm->pcm->streams[0].substream_opened ||
2137 cpcm->pcm->streams[1].substream_opened)
2138 goto err_clear;
2139 }
2140 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002141 spin_unlock(&card->files_lock);
2142 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002143
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002144 err_clear:
2145 card->shutdown = 0;
2146 err_unlock:
2147 spin_unlock(&card->files_lock);
2148 return -EINVAL;
2149}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002150EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002151
Takashi Iwai95a962c2014-10-29 16:03:58 +01002152/**
2153 * snd_hda_unlock_devices - pseudo device unlocking
2154 * @bus: the BUS
2155 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002156void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002157{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002158 struct snd_card *card = bus->card;
2159
Takashi Iwaia65d6292009-02-23 16:57:04 +01002160 spin_lock(&card->files_lock);
2161 card->shutdown = 0;
2162 spin_unlock(&card->files_lock);
2163}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002164EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002165
Takashi Iwaid5191e52009-11-16 14:58:17 +01002166/**
2167 * snd_hda_codec_reset - Clear all objects assigned to the codec
2168 * @codec: HD-audio codec
2169 *
2170 * This frees the all PCM and control elements assigned to the codec, and
2171 * clears the caches and restores the pin default configurations.
2172 *
2173 * When a device is being used, it returns -EBSY. If successfully freed,
2174 * returns zero.
2175 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002176int snd_hda_codec_reset(struct hda_codec *codec)
2177{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002178 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002179
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002180 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002181 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002182
2183 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01002184 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002185
Takashi Iwaia65d6292009-02-23 16:57:04 +01002186 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002187 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002188 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002189}
2190
Takashi Iwai6194b992014-06-06 18:12:16 +02002191typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002192
2193/* apply the function to all matching slave ctls in the mixer list */
2194static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002195 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002196{
2197 struct hda_nid_item *items;
2198 const char * const *s;
2199 int i, err;
2200
2201 items = codec->mixers.list;
2202 for (i = 0; i < codec->mixers.used; i++) {
2203 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002204 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002205 continue;
2206 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002207 char tmpname[sizeof(sctl->id.name)];
2208 const char *name = *s;
2209 if (suffix) {
2210 snprintf(tmpname, sizeof(tmpname), "%s %s",
2211 name, suffix);
2212 name = tmpname;
2213 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002214 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002215 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002216 if (err)
2217 return err;
2218 break;
2219 }
2220 }
2221 }
2222 return 0;
2223}
2224
Takashi Iwai6194b992014-06-06 18:12:16 +02002225static int check_slave_present(struct hda_codec *codec,
2226 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002227{
2228 return 1;
2229}
2230
Takashi Iwai18478e82012-03-09 17:51:10 +01002231/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002232static int get_kctl_0dB_offset(struct hda_codec *codec,
2233 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002234{
2235 int _tlv[4];
2236 const int *tlv = NULL;
2237 int val = -1;
2238
2239 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2240 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2241 mm_segment_t fs = get_fs();
2242 set_fs(get_ds());
2243 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2244 tlv = _tlv;
2245 set_fs(fs);
2246 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2247 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002248 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2249 int step = tlv[3];
2250 step &= ~TLV_DB_SCALE_MUTE;
2251 if (!step)
2252 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002253 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002254 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002255- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002256 return -1;
2257 }
2258 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002259 val = -tlv[2] / step;
2260 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002261 return val;
2262}
2263
2264/* call kctl->put with the given value(s) */
2265static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2266{
2267 struct snd_ctl_elem_value *ucontrol;
2268 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2269 if (!ucontrol)
2270 return -ENOMEM;
2271 ucontrol->value.integer.value[0] = val;
2272 ucontrol->value.integer.value[1] = val;
2273 kctl->put(kctl, ucontrol);
2274 kfree(ucontrol);
2275 return 0;
2276}
2277
2278/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002279static int init_slave_0dB(struct hda_codec *codec,
2280 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002281{
Takashi Iwai6194b992014-06-06 18:12:16 +02002282 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002283 if (offset > 0)
2284 put_kctl_with_value(slave, offset);
2285 return 0;
2286}
2287
2288/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002289static int init_slave_unmute(struct hda_codec *codec,
2290 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002291{
2292 return put_kctl_with_value(slave, 1);
2293}
2294
Takashi Iwaie8750942014-06-30 14:02:39 +02002295static int add_slave(struct hda_codec *codec,
2296 void *data, struct snd_kcontrol *slave)
2297{
2298 return snd_ctl_add_slave(data, slave);
2299}
2300
Takashi Iwaid5191e52009-11-16 14:58:17 +01002301/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002302 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002303 * @codec: HD-audio codec
2304 * @name: vmaster control name
2305 * @tlv: TLV data (optional)
2306 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002307 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002308 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002309 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002310 *
2311 * Create a virtual master control with the given name. The TLV data
2312 * must be either NULL or a valid data.
2313 *
2314 * @slaves is a NULL-terminated array of strings, each of which is a
2315 * slave control name. All controls with these names are assigned to
2316 * the new virtual master control.
2317 *
2318 * This function returns zero if successful or a negative error code.
2319 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002320int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002321 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002322 const char *suffix, bool init_slave_vol,
2323 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002324{
2325 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002326 int err;
2327
Takashi Iwai29e58532012-03-12 12:25:03 +01002328 if (ctl_ret)
2329 *ctl_ret = NULL;
2330
Takashi Iwai9322ca52012-02-03 14:28:01 +01002331 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002332 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002333 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002334 return 0;
2335 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002336 kctl = snd_ctl_make_virtual_master(name, tlv);
2337 if (!kctl)
2338 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002339 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002340 if (err < 0)
2341 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002342
Takashi Iwaie8750942014-06-30 14:02:39 +02002343 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002344 if (err < 0)
2345 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002346
2347 /* init with master mute & zero volume */
2348 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002349 if (init_slave_vol) {
2350 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002351 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002352 tlv ? init_slave_0dB : init_slave_unmute, &step);
2353 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002354
Takashi Iwai29e58532012-03-12 12:25:03 +01002355 if (ctl_ret)
2356 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002357 return 0;
2358}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002359EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002360
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002361/*
2362 * mute-LED control using vmaster
2363 */
2364static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_info *uinfo)
2366{
2367 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002368 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002369 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002370
Takashi Iwai3ff72212014-10-20 18:17:28 +02002371 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002372}
2373
2374static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_value *ucontrol)
2376{
2377 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2378 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2379 return 0;
2380}
2381
2382static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_value *ucontrol)
2384{
2385 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2386 unsigned int old_mode = hook->mute_mode;
2387
2388 hook->mute_mode = ucontrol->value.enumerated.item[0];
2389 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2390 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2391 if (old_mode == hook->mute_mode)
2392 return 0;
2393 snd_hda_sync_vmaster_hook(hook);
2394 return 1;
2395}
2396
2397static struct snd_kcontrol_new vmaster_mute_mode = {
2398 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2399 .name = "Mute-LED Mode",
2400 .info = vmaster_mute_mode_info,
2401 .get = vmaster_mute_mode_get,
2402 .put = vmaster_mute_mode_put,
2403};
2404
Takashi Iwai95a962c2014-10-29 16:03:58 +01002405/**
2406 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2407 * @codec: the HDA codec
2408 * @hook: the vmaster hook object
2409 * @expose_enum_ctl: flag to create an enum ctl
2410 *
2411 * Add a mute-LED hook with the given vmaster switch kctl.
2412 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2413 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002414 */
2415int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002416 struct hda_vmaster_mute_hook *hook,
2417 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002418{
2419 struct snd_kcontrol *kctl;
2420
2421 if (!hook->hook || !hook->sw_kctl)
2422 return 0;
2423 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2424 hook->codec = codec;
2425 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002426 if (!expose_enum_ctl)
2427 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002428 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2429 if (!kctl)
2430 return -ENOMEM;
2431 return snd_hda_ctl_add(codec, 0, kctl);
2432}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002433EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002434
Takashi Iwai95a962c2014-10-29 16:03:58 +01002435/**
2436 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2437 * @hook: the vmaster hook
2438 *
2439 * Call the hook with the current value for synchronization.
2440 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002441 */
2442void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2443{
2444 if (!hook->hook || !hook->codec)
2445 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002446 /* don't call vmaster hook in the destructor since it might have
2447 * been already destroyed
2448 */
2449 if (hook->codec->bus->shutdown)
2450 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002451 switch (hook->mute_mode) {
2452 case HDA_VMUTE_FOLLOW_MASTER:
2453 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2454 break;
2455 default:
2456 hook->hook(hook->codec, hook->mute_mode);
2457 break;
2458 }
2459}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002460EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002461
2462
Takashi Iwaid5191e52009-11-16 14:58:17 +01002463/**
2464 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002465 * @kcontrol: referred ctl element
2466 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002467 *
2468 * The control element is supposed to have the private_value field
2469 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2470 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002471int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 int chs = get_amp_channels(kcontrol);
2475
2476 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2477 uinfo->count = chs == 3 ? 2 : 1;
2478 uinfo->value.integer.min = 0;
2479 uinfo->value.integer.max = 1;
2480 return 0;
2481}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002482EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Takashi Iwaid5191e52009-11-16 14:58:17 +01002484/**
2485 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002486 * @kcontrol: ctl element
2487 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002488 *
2489 * The control element is supposed to have the private_value field
2490 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2491 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002492int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2493 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
2495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2496 hda_nid_t nid = get_amp_nid(kcontrol);
2497 int chs = get_amp_channels(kcontrol);
2498 int dir = get_amp_direction(kcontrol);
2499 int idx = get_amp_index(kcontrol);
2500 long *valp = ucontrol->value.integer.value;
2501
2502 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002503 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002504 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002507 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return 0;
2509}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002510EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Takashi Iwaid5191e52009-11-16 14:58:17 +01002512/**
2513 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002514 * @kcontrol: ctl element
2515 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002516 *
2517 * The control element is supposed to have the private_value field
2518 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2519 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002520int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2524 hda_nid_t nid = get_amp_nid(kcontrol);
2525 int chs = get_amp_channels(kcontrol);
2526 int dir = get_amp_direction(kcontrol);
2527 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 long *valp = ucontrol->value.integer.value;
2529 int change = 0;
2530
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002531 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002532 change = codec_amp_update(codec, nid, 0, dir, idx,
2533 HDA_AMP_MUTE,
2534 *valp ? 0 : HDA_AMP_MUTE, false,
2535 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002536 valp++;
2537 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002538 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002539 change |= codec_amp_update(codec, nid, 1, dir, idx,
2540 HDA_AMP_MUTE,
2541 *valp ? 0 : HDA_AMP_MUTE, false,
2542 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002543 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return change;
2545}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002546EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548/*
Takashi Iwai985be542005-11-02 18:26:49 +01002549 * bound volume controls
2550 *
2551 * bind multiple volumes (# indices, from 0)
2552 */
2553
2554#define AMP_VAL_IDX_SHIFT 19
2555#define AMP_VAL_IDX_MASK (0x0f<<19)
2556
Takashi Iwaid5191e52009-11-16 14:58:17 +01002557/**
2558 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002559 * @kcontrol: ctl element
2560 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002561 *
2562 * The control element is supposed to have the private_value field
2563 * set up via HDA_BIND_MUTE*() macros.
2564 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002565int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002567{
2568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2569 unsigned long pval;
2570 int err;
2571
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002572 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002573 pval = kcontrol->private_value;
2574 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2575 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2576 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002577 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002578 return err;
2579}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002580EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002581
Takashi Iwaid5191e52009-11-16 14:58:17 +01002582/**
2583 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002584 * @kcontrol: ctl element
2585 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002586 *
2587 * The control element is supposed to have the private_value field
2588 * set up via HDA_BIND_MUTE*() macros.
2589 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002590int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002592{
2593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2594 unsigned long pval;
2595 int i, indices, err = 0, change = 0;
2596
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002597 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002598 pval = kcontrol->private_value;
2599 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2600 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2602 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002603 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2604 if (err < 0)
2605 break;
2606 change |= err;
2607 }
2608 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002609 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002610 return err < 0 ? err : change;
2611}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002612EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002613
Takashi Iwaid5191e52009-11-16 14:58:17 +01002614/**
2615 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002616 * @kcontrol: referred ctl element
2617 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002618 *
2619 * The control element is supposed to have the private_value field
2620 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002621 */
2622int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_info *uinfo)
2624{
2625 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2626 struct hda_bind_ctls *c;
2627 int err;
2628
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002629 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002630 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002631 kcontrol->private_value = *c->values;
2632 err = c->ops->info(kcontrol, uinfo);
2633 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002634 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002635 return err;
2636}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002637EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002638
Takashi Iwaid5191e52009-11-16 14:58:17 +01002639/**
2640 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002641 * @kcontrol: ctl element
2642 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002643 *
2644 * The control element is supposed to have the private_value field
2645 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2646 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002647int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
2649{
2650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2651 struct hda_bind_ctls *c;
2652 int err;
2653
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002654 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002655 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002656 kcontrol->private_value = *c->values;
2657 err = c->ops->get(kcontrol, ucontrol);
2658 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002659 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002660 return err;
2661}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002662EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002663
Takashi Iwaid5191e52009-11-16 14:58:17 +01002664/**
2665 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002666 * @kcontrol: ctl element
2667 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002668 *
2669 * The control element is supposed to have the private_value field
2670 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2671 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002672int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2674{
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 struct hda_bind_ctls *c;
2677 unsigned long *vals;
2678 int err = 0, change = 0;
2679
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002680 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002681 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002682 for (vals = c->values; *vals; vals++) {
2683 kcontrol->private_value = *vals;
2684 err = c->ops->put(kcontrol, ucontrol);
2685 if (err < 0)
2686 break;
2687 change |= err;
2688 }
2689 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002690 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002691 return err < 0 ? err : change;
2692}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002693EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002694
Takashi Iwaid5191e52009-11-16 14:58:17 +01002695/**
2696 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002697 * @kcontrol: ctl element
2698 * @op_flag: operation flag
2699 * @size: byte size of input TLV
2700 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002701 *
2702 * The control element is supposed to have the private_value field
2703 * set up via HDA_BIND_VOL() macro.
2704 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002705int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2706 unsigned int size, unsigned int __user *tlv)
2707{
2708 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2709 struct hda_bind_ctls *c;
2710 int err;
2711
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002712 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002713 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002714 kcontrol->private_value = *c->values;
2715 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2716 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002717 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002718 return err;
2719}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002720EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002721
2722struct hda_ctl_ops snd_hda_bind_vol = {
2723 .info = snd_hda_mixer_amp_volume_info,
2724 .get = snd_hda_mixer_amp_volume_get,
2725 .put = snd_hda_mixer_amp_volume_put,
2726 .tlv = snd_hda_mixer_amp_tlv
2727};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002728EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002729
2730struct hda_ctl_ops snd_hda_bind_sw = {
2731 .info = snd_hda_mixer_amp_switch_info,
2732 .get = snd_hda_mixer_amp_switch_get,
2733 .put = snd_hda_mixer_amp_switch_put,
2734 .tlv = snd_hda_mixer_amp_tlv
2735};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002736EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002737
2738/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 * SPDIF out controls
2740 */
2741
Takashi Iwai0ba21762007-04-16 11:29:14 +02002742static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2746 uinfo->count = 1;
2747 return 0;
2748}
2749
Takashi Iwai0ba21762007-04-16 11:29:14 +02002750static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2751 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2754 IEC958_AES0_NONAUDIO |
2755 IEC958_AES0_CON_EMPHASIS_5015 |
2756 IEC958_AES0_CON_NOT_COPYRIGHT;
2757 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2758 IEC958_AES1_CON_ORIGINAL;
2759 return 0;
2760}
2761
Takashi Iwai0ba21762007-04-16 11:29:14 +02002762static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2763 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2766 IEC958_AES0_NONAUDIO |
2767 IEC958_AES0_PRO_EMPHASIS_5015;
2768 return 0;
2769}
2770
Takashi Iwai0ba21762007-04-16 11:29:14 +02002771static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002775 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002776 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002778 mutex_lock(&codec->spdif_mutex);
2779 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002780 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2781 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2782 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2783 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002784 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 return 0;
2787}
2788
2789/* convert from SPDIF status bits to HDA SPDIF bits
2790 * bit 0 (DigEn) is always set zero (to be filled later)
2791 */
2792static unsigned short convert_from_spdif_status(unsigned int sbits)
2793{
2794 unsigned short val = 0;
2795
2796 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002799 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002801 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2802 IEC958_AES0_PRO_EMPHASIS_5015)
2803 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002805 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2806 IEC958_AES0_CON_EMPHASIS_5015)
2807 val |= AC_DIG1_EMPHASIS;
2808 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2809 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2813 }
2814 return val;
2815}
2816
2817/* convert to SPDIF status bits from HDA SPDIF bits
2818 */
2819static unsigned int convert_to_spdif_status(unsigned short val)
2820{
2821 unsigned int sbits = 0;
2822
Takashi Iwai0ba21762007-04-16 11:29:14 +02002823 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002825 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 sbits |= IEC958_AES0_PROFESSIONAL;
2827 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002828 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2830 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002831 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002835 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2837 sbits |= val & (0x7f << 8);
2838 }
2839 return sbits;
2840}
2841
Takashi Iwai2f728532008-09-25 16:32:41 +02002842/* set digital convert verbs both for the given NID and its slaves */
2843static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2844 int verb, int val)
2845{
Takashi Iwaidda14412011-05-02 11:29:30 +02002846 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002847
Takashi Iwai9e976972008-11-25 08:17:20 +01002848 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002849 d = codec->slave_dig_outs;
2850 if (!d)
2851 return;
2852 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002853 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002854}
2855
2856static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2857 int dig1, int dig2)
2858{
2859 if (dig1 != -1)
2860 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2861 if (dig2 != -1)
2862 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2863}
2864
Takashi Iwai0ba21762007-04-16 11:29:14 +02002865static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002869 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002870 struct hda_spdif_out *spdif;
2871 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 unsigned short val;
2873 int change;
2874
Ingo Molnar62932df2006-01-16 16:34:20 +01002875 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002876 spdif = snd_array_elem(&codec->spdif_out, idx);
2877 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002878 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2880 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2881 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002882 val = convert_from_spdif_status(spdif->status);
2883 val |= spdif->ctls & 1;
2884 change = spdif->ctls != val;
2885 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002886 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002887 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002888 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return change;
2890}
2891
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002892#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Takashi Iwai0ba21762007-04-16 11:29:14 +02002894static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002898 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002899 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002901 mutex_lock(&codec->spdif_mutex);
2902 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002903 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002904 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return 0;
2906}
2907
Stephen Warren74b654c2011-06-01 11:14:18 -06002908static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2909 int dig1, int dig2)
2910{
2911 set_dig_out_convert(codec, nid, dig1, dig2);
2912 /* unmute amp switch (if any) */
2913 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2914 (dig1 & AC_DIG1_ENABLE))
2915 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2916 HDA_AMP_MUTE, 0);
2917}
2918
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2920 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002923 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002924 struct hda_spdif_out *spdif;
2925 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 unsigned short val;
2927 int change;
2928
Ingo Molnar62932df2006-01-16 16:34:20 +01002929 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002930 spdif = snd_array_elem(&codec->spdif_out, idx);
2931 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002932 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002934 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002935 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002936 spdif->ctls = val;
2937 if (change && nid != (u16)-1)
2938 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002939 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return change;
2941}
2942
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002943static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 {
2945 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2946 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002947 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 .info = snd_hda_spdif_mask_info,
2949 .get = snd_hda_spdif_cmask_get,
2950 },
2951 {
2952 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2953 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002954 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 .info = snd_hda_spdif_mask_info,
2956 .get = snd_hda_spdif_pmask_get,
2957 },
2958 {
2959 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002960 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 .info = snd_hda_spdif_mask_info,
2962 .get = snd_hda_spdif_default_get,
2963 .put = snd_hda_spdif_default_put,
2964 },
2965 {
2966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002967 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 .info = snd_hda_spdif_out_switch_info,
2969 .get = snd_hda_spdif_out_switch_get,
2970 .put = snd_hda_spdif_out_switch_put,
2971 },
2972 { } /* end */
2973};
2974
2975/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002976 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002978 * @associated_nid: NID that new ctls associated with
2979 * @cvt_nid: converter NID
2980 * @type: HDA_PCM_TYPE_*
2981 * Creates controls related with the digital output.
2982 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 *
2984 * Returns 0 if successful, or a negative error code.
2985 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002986int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2987 hda_nid_t associated_nid,
2988 hda_nid_t cvt_nid,
2989 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002992 struct snd_kcontrol *kctl;
2993 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002994 int idx = 0;
2995 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06002996 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002997 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002999 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003000 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003001 idx = spdif_index;
3002 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003003 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003004 /* suppose a single SPDIF device */
3005 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3006 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3007 if (!kctl)
3008 break;
3009 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003010 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003011 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003012 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003013 if (!bus->primary_dig_out_type)
3014 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003015
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003016 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003017 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003018 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003019 return -EBUSY;
3020 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003021 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003022 if (!spdif)
3023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3025 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003026 if (!kctl)
3027 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003028 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003029 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003030 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return err;
3033 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003034 spdif->nid = cvt_nid;
3035 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003036 AC_VERB_GET_DIGI_CONVERT_1, 0);
3037 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003040EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Takashi Iwai95a962c2014-10-29 16:03:58 +01003042/**
3043 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3044 * @codec: the HDA codec
3045 * @nid: widget NID
3046 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003047 * call within spdif_mutex lock
3048 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003049struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3050 hda_nid_t nid)
3051{
3052 int i;
3053 for (i = 0; i < codec->spdif_out.used; i++) {
3054 struct hda_spdif_out *spdif =
3055 snd_array_elem(&codec->spdif_out, i);
3056 if (spdif->nid == nid)
3057 return spdif;
3058 }
3059 return NULL;
3060}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003061EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003062
Takashi Iwai95a962c2014-10-29 16:03:58 +01003063/**
3064 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3065 * @codec: the HDA codec
3066 * @idx: the SPDIF ctl index
3067 *
3068 * Unassign the widget from the given SPDIF control.
3069 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003070void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3071{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003072 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003073
3074 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003075 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003076 spdif->nid = (u16)-1;
3077 mutex_unlock(&codec->spdif_mutex);
3078}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003079EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003080
Takashi Iwai95a962c2014-10-29 16:03:58 +01003081/**
3082 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3083 * @codec: the HDA codec
3084 * @idx: the SPDIF ctl idx
3085 * @nid: widget NID
3086 *
3087 * Assign the widget to the SPDIF control with the given index.
3088 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003089void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3090{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003091 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003092 unsigned short val;
3093
3094 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003095 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003096 if (spdif->nid != nid) {
3097 spdif->nid = nid;
3098 val = spdif->ctls;
3099 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3100 }
3101 mutex_unlock(&codec->spdif_mutex);
3102}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003103EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003106 * SPDIF sharing with analog output
3107 */
3108static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3109 struct snd_ctl_elem_value *ucontrol)
3110{
3111 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3112 ucontrol->value.integer.value[0] = mout->share_spdif;
3113 return 0;
3114}
3115
3116static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol)
3118{
3119 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3120 mout->share_spdif = !!ucontrol->value.integer.value[0];
3121 return 0;
3122}
3123
3124static struct snd_kcontrol_new spdif_share_sw = {
3125 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3126 .name = "IEC958 Default PCM Playback Switch",
3127 .info = snd_ctl_boolean_mono_info,
3128 .get = spdif_share_sw_get,
3129 .put = spdif_share_sw_put,
3130};
3131
Takashi Iwaid5191e52009-11-16 14:58:17 +01003132/**
3133 * snd_hda_create_spdif_share_sw - create Default PCM switch
3134 * @codec: the HDA codec
3135 * @mout: multi-out instance
3136 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003137int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3138 struct hda_multi_out *mout)
3139{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003140 struct snd_kcontrol *kctl;
3141
Takashi Iwai9a081602008-02-12 18:37:26 +01003142 if (!mout->dig_out_nid)
3143 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003144
3145 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3146 if (!kctl)
3147 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003148 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003149 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003150}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003151EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003152
3153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 * SPDIF input
3155 */
3156
3157#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3158
Takashi Iwai0ba21762007-04-16 11:29:14 +02003159static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3160 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3163
3164 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3165 return 0;
3166}
3167
Takashi Iwai0ba21762007-04-16 11:29:14 +02003168static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3172 hda_nid_t nid = kcontrol->private_value;
3173 unsigned int val = !!ucontrol->value.integer.value[0];
3174 int change;
3175
Ingo Molnar62932df2006-01-16 16:34:20 +01003176 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003178 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003180 snd_hda_codec_write_cache(codec, nid, 0,
3181 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003183 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return change;
3185}
3186
Takashi Iwai0ba21762007-04-16 11:29:14 +02003187static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3188 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3191 hda_nid_t nid = kcontrol->private_value;
3192 unsigned short val;
3193 unsigned int sbits;
3194
Andrew Paprocki3982d172007-12-19 12:13:44 +01003195 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 sbits = convert_to_spdif_status(val);
3197 ucontrol->value.iec958.status[0] = sbits;
3198 ucontrol->value.iec958.status[1] = sbits >> 8;
3199 ucontrol->value.iec958.status[2] = sbits >> 16;
3200 ucontrol->value.iec958.status[3] = sbits >> 24;
3201 return 0;
3202}
3203
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003204static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 {
3206 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003207 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 .info = snd_hda_spdif_in_switch_info,
3209 .get = snd_hda_spdif_in_switch_get,
3210 .put = snd_hda_spdif_in_switch_put,
3211 },
3212 {
3213 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3214 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003215 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 .info = snd_hda_spdif_mask_info,
3217 .get = snd_hda_spdif_in_status_get,
3218 },
3219 { } /* end */
3220};
3221
3222/**
3223 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3224 * @codec: the HDA codec
3225 * @nid: audio in widget NID
3226 *
3227 * Creates controls related with the SPDIF input.
3228 * Called from each patch supporting the SPDIF in.
3229 *
3230 * Returns 0 if successful, or a negative error code.
3231 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003232int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003235 struct snd_kcontrol *kctl;
3236 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003237 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Takashi Iwaidcda5802012-10-12 17:24:51 +02003239 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003240 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003241 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003242 return -EBUSY;
3243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3245 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003246 if (!kctl)
3247 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003249 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003250 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return err;
3252 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003254 snd_hda_codec_read(codec, nid, 0,
3255 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return 0;
3258}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003259EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003261/*
3262 * command cache
3263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Takashi Iwaic370dd62012-12-13 18:30:04 +01003265/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003266#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3267#define get_cmd_cache_nid(key) ((key) & 0xff)
3268#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3269
3270/**
3271 * snd_hda_codec_write_cache - send a single command with caching
3272 * @codec: the HDA codec
3273 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003274 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003275 * @verb: the verb to send
3276 * @parm: the parameter for the verb
3277 *
3278 * Send a single command without waiting for response.
3279 *
3280 * Returns 0 if successful, or a negative error code.
3281 */
3282int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003283 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003284{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003285 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003286 struct hda_cache_head *c;
3287 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003288 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003289
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003290 cache_only = codec->cached_write;
3291 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003292 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003293 if (err < 0)
3294 return err;
3295 }
3296
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003297 /* parm may contain the verb stuff for get/set amp */
3298 verb = verb | (parm >> 8);
3299 parm &= 0xff;
3300 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003301 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003302 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003303 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003304 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003305 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003306 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003307 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003308 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003309}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003310EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003311
Takashi Iwaid5191e52009-11-16 14:58:17 +01003312/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003313 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3314 * @codec: the HDA codec
3315 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003316 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003317 * @verb: the verb to send
3318 * @parm: the parameter for the verb
3319 *
3320 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3321 * command if the parameter is already identical with the cached value.
3322 * If not, it sends the command and refreshes the cache.
3323 *
3324 * Returns 0 if successful, or a negative error code.
3325 */
3326int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003327 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003328{
3329 struct hda_cache_head *c;
3330 u32 key;
3331
3332 /* parm may contain the verb stuff for get/set amp */
3333 verb = verb | (parm >> 8);
3334 parm &= 0xff;
3335 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003336 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003337 c = get_hash(&codec->cmd_cache, key);
3338 if (c && c->val == parm) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003339 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003340 return 0;
3341 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003342 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003343 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003345EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003346
3347/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003348 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3349 * @codec: HD-audio codec
3350 *
3351 * Execute all verbs recorded in the command caches to resume.
3352 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003353void snd_hda_codec_resume_cache(struct hda_codec *codec)
3354{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003355 int i;
3356
Takashi Iwaic370dd62012-12-13 18:30:04 +01003357 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003358 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003359 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3360 struct hda_cache_head *buffer;
3361 u32 key;
3362
3363 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3364 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003365 if (!key)
3366 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003367 if (!buffer->dirty)
3368 continue;
3369 buffer->dirty = 0;
3370 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003371 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3372 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003373 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003374 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003375 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003376}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003377EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003378
3379/**
3380 * snd_hda_sequence_write_cache - sequence writes with caching
3381 * @codec: the HDA codec
3382 * @seq: VERB array to send
3383 *
3384 * Send the commands sequentially from the given array.
3385 * Thte commands are recorded on cache for power-save and resume.
3386 * The array must be terminated with NID=0.
3387 */
3388void snd_hda_sequence_write_cache(struct hda_codec *codec,
3389 const struct hda_verb *seq)
3390{
3391 for (; seq->nid; seq++)
3392 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3393 seq->param);
3394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003395EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003396
Takashi Iwaidc870f32013-01-22 15:24:30 +01003397/**
3398 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3399 * @codec: HD-audio codec
3400 */
3401void snd_hda_codec_flush_cache(struct hda_codec *codec)
3402{
3403 snd_hda_codec_resume_amp(codec);
3404 snd_hda_codec_resume_cache(codec);
3405}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003406EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003407
Takashi Iwai95a962c2014-10-29 16:03:58 +01003408/**
3409 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3410 * @codec: the HDA codec
3411 * @fg: function group (not used now)
3412 * @power_state: the power state to set (AC_PWRST_*)
3413 *
3414 * Set the given power state to all widgets that have the power control.
3415 * If the codec has power_filter set, it evaluates the power state and
3416 * filter out if it's unchanged as D3.
3417 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003418void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003419 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003420{
Takashi Iwai7639a062015-03-03 10:07:24 +01003421 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01003422
Takashi Iwai7639a062015-03-03 10:07:24 +01003423 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003424 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003425 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003426 if (!(wcaps & AC_WCAP_POWER))
3427 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003428 if (codec->power_filter) {
3429 state = codec->power_filter(codec, nid, power_state);
3430 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003431 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003432 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003433 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003434 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003435 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003436}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003437EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003438
3439/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003440 * wait until the state is reached, returns the current state
3441 */
3442static unsigned int hda_sync_power_state(struct hda_codec *codec,
3443 hda_nid_t fg,
3444 unsigned int power_state)
3445{
3446 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3447 unsigned int state, actual_state;
3448
3449 for (;;) {
3450 state = snd_hda_codec_read(codec, fg, 0,
3451 AC_VERB_GET_POWER_STATE, 0);
3452 if (state & AC_PWRST_ERROR)
3453 break;
3454 actual_state = (state >> 4) & 0x0f;
3455 if (actual_state == power_state)
3456 break;
3457 if (time_after_eq(jiffies, end_time))
3458 break;
3459 /* wait until the codec reachs to the target state */
3460 msleep(1);
3461 }
3462 return state;
3463}
3464
Takashi Iwai95a962c2014-10-29 16:03:58 +01003465/**
3466 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3467 * @codec: the HDA codec
3468 * @nid: widget NID
3469 * @power_state: power state to evalue
3470 *
3471 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3472 * This can be used a codec power_filter callback.
3473 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003474unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3475 hda_nid_t nid,
3476 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003477{
Takashi Iwai7639a062015-03-03 10:07:24 +01003478 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003479 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003480 if (power_state == AC_PWRST_D3 &&
3481 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3482 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3483 int eapd = snd_hda_codec_read(codec, nid, 0,
3484 AC_VERB_GET_EAPD_BTLENABLE, 0);
3485 if (eapd & 0x02)
3486 return AC_PWRST_D0;
3487 }
3488 return power_state;
3489}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003490EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003491
Takashi Iwai432c6412012-08-28 09:59:20 -07003492/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003493 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003494 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003495static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003496 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003497{
Takashi Iwai7639a062015-03-03 10:07:24 +01003498 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003499 int count;
3500 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003501 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003502
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003503 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003504 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003505 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003506 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003507 else if (codec->depop_delay > 0)
3508 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003509 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003510 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003511
3512 /* repeat power states setting at most 10 times*/
3513 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003514 if (codec->patch_ops.set_power_state)
3515 codec->patch_ops.set_power_state(codec, fg,
3516 power_state);
3517 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003518 state = power_state;
3519 if (codec->power_filter)
3520 state = codec->power_filter(codec, fg, state);
3521 if (state == power_state || power_state != AC_PWRST_D3)
3522 snd_hda_codec_read(codec, fg, flags,
3523 AC_VERB_SET_POWER_STATE,
3524 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003525 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003526 }
3527 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003528 if (!(state & AC_PWRST_ERROR))
3529 break;
3530 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003531
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003532 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003533}
Takashi Iwai54d17402005-11-21 16:33:22 +01003534
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003535/* sync power states of all widgets;
3536 * this is called at the end of codec parsing
3537 */
3538static void sync_power_up_states(struct hda_codec *codec)
3539{
Takashi Iwai7639a062015-03-03 10:07:24 +01003540 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003541
Takashi Iwaiba615b82013-03-13 14:47:21 +01003542 /* don't care if no filter is used */
3543 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003544 return;
3545
Takashi Iwai7639a062015-03-03 10:07:24 +01003546 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003547 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003548 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003549 if (!(wcaps & AC_WCAP_POWER))
3550 continue;
3551 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3552 if (target == AC_PWRST_D0)
3553 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003554 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003555 snd_hda_codec_write(codec, nid, 0,
3556 AC_VERB_SET_POWER_STATE, target);
3557 }
3558}
3559
Takashi Iwai648a8d22014-02-25 10:38:13 +01003560#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003561/* execute additional init verbs */
3562static void hda_exec_init_verbs(struct hda_codec *codec)
3563{
3564 if (codec->init_verbs.list)
3565 snd_hda_sequence_write(codec, codec->init_verbs.list);
3566}
3567#else
3568static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3569#endif
3570
Takashi Iwai2a439522011-07-26 09:52:50 +02003571#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003572/* update the power on/off account with the current jiffies */
3573static void update_power_acct(struct hda_codec *codec, bool on)
3574{
3575 unsigned long delta = jiffies - codec->power_jiffies;
3576
3577 if (on)
3578 codec->power_on_acct += delta;
3579 else
3580 codec->power_off_acct += delta;
3581 codec->power_jiffies += delta;
3582}
3583
3584void snd_hda_update_power_acct(struct hda_codec *codec)
3585{
3586 update_power_acct(codec, hda_codec_is_power_on(codec));
3587}
3588
Takashi Iwaicb53c622007-08-10 17:21:45 +02003589/*
3590 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003591 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003592 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003593static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003594{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003595 unsigned int state;
3596
Takashi Iwai7639a062015-03-03 10:07:24 +01003597 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003598
Takashi Iwaicb53c622007-08-10 17:21:45 +02003599 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003600 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003601 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003602 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003603 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003604 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003605 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003606}
3607
Takashi Iwaic370dd62012-12-13 18:30:04 +01003608/* mark all entries of cmd and amp caches dirty */
3609static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3610{
3611 int i;
3612 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3613 struct hda_cache_head *cmd;
3614 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3615 cmd->dirty = 1;
3616 }
3617 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3618 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003619 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003620 amp->head.dirty = 1;
3621 }
3622}
3623
Takashi Iwaicb53c622007-08-10 17:21:45 +02003624/*
3625 * kick up codec; used both from PM and power-save
3626 */
3627static void hda_call_codec_resume(struct hda_codec *codec)
3628{
Takashi Iwai7639a062015-03-03 10:07:24 +01003629 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003630
Takashi Iwaic370dd62012-12-13 18:30:04 +01003631 hda_mark_cmd_cache_dirty(codec);
3632
Takashi Iwaicc72da72015-02-19 16:00:22 +01003633 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003634
Takashi Iwaid8193872012-08-31 07:54:38 -07003635 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003636 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003637 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003638 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003639 if (codec->patch_ops.resume)
3640 codec->patch_ops.resume(codec);
3641 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003642 if (codec->patch_ops.init)
3643 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003644 snd_hda_codec_resume_amp(codec);
3645 snd_hda_codec_resume_cache(codec);
3646 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003647
3648 if (codec->jackpoll_interval)
3649 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003650 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003651 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003652 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003653}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003654
Takashi Iwaicc72da72015-02-19 16:00:22 +01003655static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003656{
3657 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003658 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003659 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003660
3661 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003662 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3663 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003664 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003665 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3666 (state & AC_PWRST_CLK_STOP_OK))
3667 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003668 return 0;
3669}
3670
Takashi Iwaicc72da72015-02-19 16:00:22 +01003671static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003672{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003673 struct hda_codec *codec = dev_to_hda_codec(dev);
3674
Takashi Iwai7639a062015-03-03 10:07:24 +01003675 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003676 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003677 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003678 return 0;
3679}
Takashi Iwai2a439522011-07-26 09:52:50 +02003680#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003681
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003682/* referred in hda_bind.c */
3683const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003684 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3685 pm_runtime_force_resume)
3686 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3687 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003688};
Takashi Iwai54d17402005-11-21 16:33:22 +01003689
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003690/*
3691 * add standard channel maps if not specified
3692 */
3693static int add_std_chmaps(struct hda_codec *codec)
3694{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003695 struct hda_pcm *pcm;
3696 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003697
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003698 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003699 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003700 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003701 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003702 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003703
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003704 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003705 continue;
3706 if (!pcm || !hinfo->substreams)
3707 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003708 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003709 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003710 hinfo->channels_max,
3711 0, &chmap);
3712 if (err < 0)
3713 return err;
3714 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3715 }
3716 }
3717 return 0;
3718}
3719
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003720/* default channel maps for 2.1 speakers;
3721 * since HD-audio supports only stereo, odd number channels are omitted
3722 */
3723const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3724 { .channels = 2,
3725 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3726 { .channels = 4,
3727 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3728 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3729 { }
3730};
3731EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3732
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003733int snd_hda_codec_build_controls(struct hda_codec *codec)
3734{
3735 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003736 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003737 /* continue to initialize... */
3738 if (codec->patch_ops.init)
3739 err = codec->patch_ops.init(codec);
3740 if (!err && codec->patch_ops.build_controls)
3741 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003742 if (err < 0)
3743 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003744
3745 /* we create chmaps here instead of build_pcms */
3746 err = add_std_chmaps(codec);
3747 if (err < 0)
3748 return err;
3749
David Henningsson26a6cb62012-10-09 15:04:21 +02003750 if (codec->jackpoll_interval)
3751 hda_jackpoll_work(&codec->jackpoll_work.work);
3752 else
3753 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003754 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return 0;
3756}
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758/*
3759 * stream formats
3760 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003761struct hda_rate_tbl {
3762 unsigned int hz;
3763 unsigned int alsa_bits;
3764 unsigned int hda_fmt;
3765};
3766
Takashi Iwai92f10b32010-08-03 14:21:00 +02003767/* rate = base * mult / div */
3768#define HDA_RATE(base, mult, div) \
3769 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3770 (((div) - 1) << AC_FMT_DIV_SHIFT))
3771
Takashi Iwaibefdf312005-08-22 13:57:55 +02003772static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003774
3775 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003776 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3777 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3778 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3779 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3780 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3781 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3782 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3783 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3784 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3785 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3786 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003787#define AC_PAR_PCM_RATE_BITS 11
3788 /* up to bits 10, 384kHZ isn't supported properly */
3789
3790 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003791 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003792
Takashi Iwaibefdf312005-08-22 13:57:55 +02003793 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794};
3795
3796/**
3797 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003798 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 * @rate: the sample rate
3800 * @channels: the number of channels
3801 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3802 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003803 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 *
3805 * Calculate the format bitset from the given rate, channels and th PCM format.
3806 *
3807 * Return zero if invalid.
3808 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003809unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3810 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 unsigned int channels,
3812 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003813 unsigned int maxbps,
3814 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
3816 int i;
3817 unsigned int val = 0;
3818
Takashi Iwaibefdf312005-08-22 13:57:55 +02003819 for (i = 0; rate_bits[i].hz; i++)
3820 if (rate_bits[i].hz == rate) {
3821 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 break;
3823 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003824 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003825 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return 0;
3827 }
3828
3829 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003830 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return 0;
3832 }
3833 val |= channels - 1;
3834
3835 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003836 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003837 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003838 break;
3839 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003840 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 case 20:
3843 case 24:
3844 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003845 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003846 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003848 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003850 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 break;
3852 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003853 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003854 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return 0;
3856 }
3857
Anssi Hannula32c168c2010-08-03 13:28:57 +03003858 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003859 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return val;
3862}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003863EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003865static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3866 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003867{
3868 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003869 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003870 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3871 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3872 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003873 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003874 if (!val || val == -1)
3875 return 0;
3876 return val;
3877}
3878
3879static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3880{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003881 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003882 get_pcm_param);
3883}
3884
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003885static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3886 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003887{
3888 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3889 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003890 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003891 if (!streams || streams == -1)
3892 return 0;
3893 return streams;
3894}
3895
3896static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3897{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003898 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003899 get_stream_param);
3900}
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902/**
3903 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3904 * @codec: the HDA codec
3905 * @nid: NID to query
3906 * @ratesp: the pointer to store the detected rate bitflags
3907 * @formatsp: the pointer to store the detected formats
3908 * @bpsp: the pointer to store the detected format widths
3909 *
3910 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3911 * or @bsps argument is ignored.
3912 *
3913 * Returns 0 if successful, otherwise a negative error code.
3914 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003915int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3917{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003918 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003920 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003921 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
3923 if (ratesp) {
3924 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003925 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003927 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003929 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003930 codec_err(codec,
3931 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3932 nid, val,
3933 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003934 return -EIO;
3935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 *ratesp = rates;
3937 }
3938
3939 if (formatsp || bpsp) {
3940 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003941 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003943 streams = query_stream_param(codec, nid);
3944 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 bps = 0;
3948 if (streams & AC_SUPFMT_PCM) {
3949 if (val & AC_SUPPCM_BITS_8) {
3950 formats |= SNDRV_PCM_FMTBIT_U8;
3951 bps = 8;
3952 }
3953 if (val & AC_SUPPCM_BITS_16) {
3954 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3955 bps = 16;
3956 }
3957 if (wcaps & AC_WCAP_DIGITAL) {
3958 if (val & AC_SUPPCM_BITS_32)
3959 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3960 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3961 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3962 if (val & AC_SUPPCM_BITS_24)
3963 bps = 24;
3964 else if (val & AC_SUPPCM_BITS_20)
3965 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003966 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3967 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3969 if (val & AC_SUPPCM_BITS_32)
3970 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 else if (val & AC_SUPPCM_BITS_24)
3972 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003973 else if (val & AC_SUPPCM_BITS_20)
3974 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003977#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003978 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003980 if (!bps)
3981 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003982 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003983#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003984 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003985 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 /* temporary hack: we have still no proper support
3987 * for the direct AC3 stream...
3988 */
3989 formats |= SNDRV_PCM_FMTBIT_U8;
3990 bps = 8;
3991 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003992 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003993 codec_err(codec,
3994 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3995 nid, val,
3996 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3997 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003998 return -EIO;
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (formatsp)
4001 *formatsp = formats;
4002 if (bpsp)
4003 *bpsp = bps;
4004 }
4005
4006 return 0;
4007}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004008EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
4010/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004011 * snd_hda_is_supported_format - Check the validity of the format
4012 * @codec: HD-audio codec
4013 * @nid: NID to check
4014 * @format: the HD-audio format value to check
4015 *
4016 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 *
4018 * Returns 1 if supported, 0 if not.
4019 */
4020int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4021 unsigned int format)
4022{
4023 int i;
4024 unsigned int val = 0, rate, stream;
4025
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004026 val = query_pcm_param(codec, nid);
4027 if (!val)
4028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004031 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004032 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 if (val & (1 << i))
4034 break;
4035 return 0;
4036 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004037 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return 0;
4039
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004040 stream = query_stream_param(codec, nid);
4041 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return 0;
4043
4044 if (stream & AC_SUPFMT_PCM) {
4045 switch (format & 0xf0) {
4046 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004047 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return 0;
4049 break;
4050 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004051 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 return 0;
4053 break;
4054 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004055 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return 0;
4057 break;
4058 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004059 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 return 0;
4061 break;
4062 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004063 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return 0;
4065 break;
4066 default:
4067 return 0;
4068 }
4069 } else {
4070 /* FIXME: check for float32 and AC3? */
4071 }
4072
4073 return 1;
4074}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004075EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077/*
4078 * PCM stuff
4079 */
4080static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4081 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004082 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
4084 return 0;
4085}
4086
4087static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4088 struct hda_codec *codec,
4089 unsigned int stream_tag,
4090 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004091 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
4093 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4094 return 0;
4095}
4096
4097static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4098 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004099 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{
Takashi Iwai888afa12008-03-18 09:57:50 +01004101 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 return 0;
4103}
4104
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004105static int set_pcm_default_values(struct hda_codec *codec,
4106 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004108 int err;
4109
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110 /* query support PCM information from the given NID */
4111 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004112 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004113 info->rates ? NULL : &info->rates,
4114 info->formats ? NULL : &info->formats,
4115 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004116 if (err < 0)
4117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 }
4119 if (info->ops.open == NULL)
4120 info->ops.open = hda_pcm_default_open_close;
4121 if (info->ops.close == NULL)
4122 info->ops.close = hda_pcm_default_open_close;
4123 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004124 if (snd_BUG_ON(!info->nid))
4125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 info->ops.prepare = hda_pcm_default_prepare;
4127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004129 if (snd_BUG_ON(!info->nid))
4130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 info->ops.cleanup = hda_pcm_default_cleanup;
4132 }
4133 return 0;
4134}
4135
Takashi Iwaieb541332010-08-06 13:48:11 +02004136/*
4137 * codec prepare/cleanup entries
4138 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004139/**
4140 * snd_hda_codec_prepare - Prepare a stream
4141 * @codec: the HDA codec
4142 * @hinfo: PCM information
4143 * @stream: stream tag to assign
4144 * @format: format id to assign
4145 * @substream: PCM substream to assign
4146 *
4147 * Calls the prepare callback set by the codec with the given arguments.
4148 * Clean up the inactive streams when successful.
4149 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004150int snd_hda_codec_prepare(struct hda_codec *codec,
4151 struct hda_pcm_stream *hinfo,
4152 unsigned int stream,
4153 unsigned int format,
4154 struct snd_pcm_substream *substream)
4155{
4156 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004157 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004158 if (hinfo->ops.prepare)
4159 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4160 substream);
4161 else
4162 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004163 if (ret >= 0)
4164 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004165 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004166 return ret;
4167}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004168EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004169
Takashi Iwai95a962c2014-10-29 16:03:58 +01004170/**
4171 * snd_hda_codec_cleanup - Prepare a stream
4172 * @codec: the HDA codec
4173 * @hinfo: PCM information
4174 * @substream: PCM substream
4175 *
4176 * Calls the cleanup callback set by the codec with the given arguments.
4177 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004178void snd_hda_codec_cleanup(struct hda_codec *codec,
4179 struct hda_pcm_stream *hinfo,
4180 struct snd_pcm_substream *substream)
4181{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004182 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004183 if (hinfo->ops.cleanup)
4184 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004185 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004186}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004187EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004188
Takashi Iwaid5191e52009-11-16 14:58:17 +01004189/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004190const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4191 "Audio", "SPDIF", "HDMI", "Modem"
4192};
4193
Takashi Iwai176d5332008-07-30 15:01:44 +02004194/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004195 * get the empty PCM device number to assign
4196 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004197static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004198{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004199 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004200 /* assigned to static slots up to dev#10; if more needed, assign
4201 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4202 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004203 static int audio_idx[HDA_PCM_NTYPES][5] = {
4204 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4205 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004206 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004207 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004208 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004209 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004210
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004211 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004212 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004213 return -EINVAL;
4214 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004215
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004216 for (i = 0; audio_idx[type][i] >= 0; i++) {
4217#ifndef CONFIG_SND_DYNAMIC_MINORS
4218 if (audio_idx[type][i] >= 8)
4219 break;
4220#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004221 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4222 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004223 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004224
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004225#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004226 /* non-fixed slots starting from 10 */
4227 for (i = 10; i < 32; i++) {
4228 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4229 return i;
4230 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004231#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004232
Takashi Iwai4e76a882014-02-25 12:21:03 +01004233 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004234 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004235#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004236 dev_warn(bus->card->dev,
4237 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004238#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004239 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004240}
4241
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004242/* call build_pcms ops of the given codec and set up the default parameters */
4243int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004244{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004245 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004246 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004247
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004248 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004249 return 0; /* already parsed */
4250
4251 if (!codec->patch_ops.build_pcms)
4252 return 0;
4253
4254 err = codec->patch_ops.build_pcms(codec);
4255 if (err < 0) {
4256 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004257 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004258 return err;
4259 }
4260
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004261 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004262 int stream;
4263
4264 for (stream = 0; stream < 2; stream++) {
4265 struct hda_pcm_stream *info = &cpcm->stream[stream];
4266
4267 if (!info->substreams)
4268 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004269 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004270 if (err < 0) {
4271 codec_warn(codec,
4272 "fail to setup default for PCM %s\n",
4273 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004274 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004275 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004276 }
4277 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004278
4279 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004280}
4281
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004282/* assign all PCMs of the given codec */
4283int snd_hda_codec_build_pcms(struct hda_codec *codec)
4284{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004285 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004286 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004287 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004288
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004289 if (snd_BUG_ON(!bus->ops.attach_pcm))
4290 return -EINVAL;
4291
4292 err = snd_hda_codec_parse_pcms(codec);
4293 if (err < 0) {
4294 snd_hda_codec_reset(codec);
4295 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004296 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004297
4298 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004299 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004300 if (cpcm->pcm)
4301 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004302 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004303 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004304
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004305 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4306 if (dev < 0)
4307 continue; /* no fatal error */
4308 cpcm->device = dev;
4309 err = bus->ops.attach_pcm(bus, codec, cpcm);
4310 if (err < 0) {
4311 codec_err(codec,
4312 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004313 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004314 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004315 }
4316 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004317
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004318 return 0;
4319}
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 * snd_hda_add_new_ctls - create controls from the array
4323 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004324 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 *
4326 * This helper function creates and add new controls in the given array.
4327 * The array must be terminated with an empty entry as terminator.
4328 *
4329 * Returns 0 if successful, or a negative error code.
4330 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004331int snd_hda_add_new_ctls(struct hda_codec *codec,
4332 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004337 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004338 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004339 if (knew->iface == -1) /* skip this codec private value */
4340 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004341 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004342 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004343 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004344 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004345 if (addr > 0)
4346 kctl->id.device = addr;
4347 if (idx > 0)
4348 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004349 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004350 if (!err)
4351 break;
4352 /* try first with another device index corresponding to
4353 * the codec addr; if it still fails (or it's the
4354 * primary codec), then try another control index
4355 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004356 if (!addr && codec->core.addr)
4357 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004358 else if (!idx && !knew->index) {
4359 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004360 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004361 if (idx <= 0)
4362 return err;
4363 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004364 return err;
4365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367 return 0;
4368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004369EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Takashi Iwai83012a72012-08-24 18:38:08 +02004371#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01004372static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004373{
4374 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004375
Takashi Iwaicc72da72015-02-19 16:00:22 +01004376 if (delay > 0) {
4377 pm_runtime_set_autosuspend_delay(dev, delay);
4378 pm_runtime_use_autosuspend(dev);
4379 pm_runtime_allow(dev);
4380 if (!pm_runtime_suspended(dev))
4381 pm_runtime_mark_last_busy(dev);
4382 } else {
4383 pm_runtime_dont_use_autosuspend(dev);
4384 pm_runtime_forbid(dev);
4385 }
4386}
Takashi Iwaibb573922015-02-20 09:26:04 +01004387
4388/**
4389 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4390 * @bus: HD-audio bus
4391 * @delay: autosuspend delay in msec, 0 = off
4392 *
4393 * Synchronize the runtime PM autosuspend state from the power_save option.
4394 */
4395void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4396{
4397 struct hda_codec *c;
4398
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004399 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01004400 codec_set_power_save(c, delay);
4401}
4402EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004403
Takashi Iwaid5191e52009-11-16 14:58:17 +01004404/**
4405 * snd_hda_check_amp_list_power - Check the amp list and update the power
4406 * @codec: HD-audio codec
4407 * @check: the object containing an AMP list and the status
4408 * @nid: NID to check / update
4409 *
4410 * Check whether the given NID is in the amp list. If it's in the list,
4411 * check the current AMP status, and update the the power-status according
4412 * to the mute status.
4413 *
4414 * This function is supposed to be set or called from the check_power_status
4415 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004416 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004417int snd_hda_check_amp_list_power(struct hda_codec *codec,
4418 struct hda_loopback_check *check,
4419 hda_nid_t nid)
4420{
Takashi Iwai031024e2011-05-02 11:29:30 +02004421 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004422 int ch, v;
4423
4424 if (!check->amplist)
4425 return 0;
4426 for (p = check->amplist; p->nid; p++) {
4427 if (p->nid == nid)
4428 break;
4429 }
4430 if (!p->nid)
4431 return 0; /* nothing changed */
4432
4433 for (p = check->amplist; p->nid; p++) {
4434 for (ch = 0; ch < 2; ch++) {
4435 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4436 p->idx);
4437 if (!(v & HDA_AMP_MUTE) && v > 0) {
4438 if (!check->power_on) {
4439 check->power_on = 1;
4440 snd_hda_power_up(codec);
4441 }
4442 return 1;
4443 }
4444 }
4445 }
4446 if (check->power_on) {
4447 check->power_on = 0;
4448 snd_hda_power_down(codec);
4449 }
4450 return 0;
4451}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004452EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 * input MUX helper
4457 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004458
4459/**
4460 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004461 * @imux: imux helper object
4462 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004463 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004464int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4465 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
4467 unsigned int index;
4468
4469 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4470 uinfo->count = 1;
4471 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004472 if (!imux->num_items)
4473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 index = uinfo->value.enumerated.item;
4475 if (index >= imux->num_items)
4476 index = imux->num_items - 1;
4477 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4478 return 0;
4479}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004480EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Takashi Iwaid5191e52009-11-16 14:58:17 +01004482/**
4483 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004484 * @codec: the HDA codec
4485 * @imux: imux helper object
4486 * @ucontrol: pointer to get/store the data
4487 * @nid: input mux NID
4488 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004489 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004490int snd_hda_input_mux_put(struct hda_codec *codec,
4491 const struct hda_input_mux *imux,
4492 struct snd_ctl_elem_value *ucontrol,
4493 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 unsigned int *cur_val)
4495{
4496 unsigned int idx;
4497
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004498 if (!imux->num_items)
4499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 idx = ucontrol->value.enumerated.item[0];
4501 if (idx >= imux->num_items)
4502 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004503 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004505 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4506 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 *cur_val = idx;
4508 return 1;
4509}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004510EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004513/**
4514 * snd_hda_enum_helper_info - Helper for simple enum ctls
4515 * @kcontrol: ctl element
4516 * @uinfo: pointer to get/store the data
4517 * @num_items: number of enum items
4518 * @texts: enum item string array
4519 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004520 * process kcontrol info callback of a simple string enum array
4521 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4522 */
4523int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4524 struct snd_ctl_elem_info *uinfo,
4525 int num_items, const char * const *texts)
4526{
4527 static const char * const texts_default[] = {
4528 "Disabled", "Enabled"
4529 };
4530
4531 if (!texts || !num_items) {
4532 num_items = 2;
4533 texts = texts_default;
4534 }
4535
Takashi Iwai3ff72212014-10-20 18:17:28 +02004536 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004537}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004538EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004539
4540/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 * Multi-channel / digital-out PCM helper functions
4542 */
4543
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004544/* setup SPDIF output stream */
4545static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4546 unsigned int stream_tag, unsigned int format)
4547{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004548 struct hda_spdif_out *spdif;
4549 unsigned int curr_fmt;
4550 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004551
Laurence Darby3bef1c32012-11-03 17:00:06 +00004552 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4553 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4554 AC_VERB_GET_STREAM_FORMAT, 0);
4555 reset = codec->spdif_status_reset &&
4556 (spdif->ctls & AC_DIG1_ENABLE) &&
4557 curr_fmt != format;
4558
4559 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4560 updated */
4561 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004562 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004563 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004564 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004565 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004566 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004567 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004568 for (d = codec->slave_dig_outs; *d; d++)
4569 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4570 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004571 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004572 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004573 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004574 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004575 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004576}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004577
Takashi Iwai2f728532008-09-25 16:32:41 +02004578static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4579{
4580 snd_hda_codec_cleanup_stream(codec, nid);
4581 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004582 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004583 for (d = codec->slave_dig_outs; *d; d++)
4584 snd_hda_codec_cleanup_stream(codec, *d);
4585 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004586}
4587
Takashi Iwaid5191e52009-11-16 14:58:17 +01004588/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004589 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004590 * @codec: the HDA codec
4591 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004593int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4594 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Ingo Molnar62932df2006-01-16 16:34:20 +01004596 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004597 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4598 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004599 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004601 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 return 0;
4603}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004604EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Takashi Iwaid5191e52009-11-16 14:58:17 +01004606/**
4607 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004608 * @codec: the HDA codec
4609 * @mout: hda_multi_out object
4610 * @stream_tag: stream tag to assign
4611 * @format: format id to assign
4612 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004613 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004614int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4615 struct hda_multi_out *mout,
4616 unsigned int stream_tag,
4617 unsigned int format,
4618 struct snd_pcm_substream *substream)
4619{
4620 mutex_lock(&codec->spdif_mutex);
4621 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4622 mutex_unlock(&codec->spdif_mutex);
4623 return 0;
4624}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004625EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004626
Takashi Iwaid5191e52009-11-16 14:58:17 +01004627/**
4628 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004629 * @codec: the HDA codec
4630 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004631 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004632int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4633 struct hda_multi_out *mout)
4634{
4635 mutex_lock(&codec->spdif_mutex);
4636 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4637 mutex_unlock(&codec->spdif_mutex);
4638 return 0;
4639}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004640EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004641
Takashi Iwaid5191e52009-11-16 14:58:17 +01004642/**
4643 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004644 * @codec: the HDA codec
4645 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004647int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4648 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Ingo Molnar62932df2006-01-16 16:34:20 +01004650 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004652 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 return 0;
4654}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004655EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Takashi Iwaid5191e52009-11-16 14:58:17 +01004657/**
4658 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004659 * @codec: the HDA codec
4660 * @mout: hda_multi_out object
4661 * @substream: PCM substream to assign
4662 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004663 *
4664 * Open analog outputs and set up the hw-constraints.
4665 * If the digital outputs can be opened as slave, open the digital
4666 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004668int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4669 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004670 struct snd_pcm_substream *substream,
4671 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
Takashi Iwai9a081602008-02-12 18:37:26 +01004673 struct snd_pcm_runtime *runtime = substream->runtime;
4674 runtime->hw.channels_max = mout->max_channels;
4675 if (mout->dig_out_nid) {
4676 if (!mout->analog_rates) {
4677 mout->analog_rates = hinfo->rates;
4678 mout->analog_formats = hinfo->formats;
4679 mout->analog_maxbps = hinfo->maxbps;
4680 } else {
4681 runtime->hw.rates = mout->analog_rates;
4682 runtime->hw.formats = mout->analog_formats;
4683 hinfo->maxbps = mout->analog_maxbps;
4684 }
4685 if (!mout->spdif_rates) {
4686 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4687 &mout->spdif_rates,
4688 &mout->spdif_formats,
4689 &mout->spdif_maxbps);
4690 }
4691 mutex_lock(&codec->spdif_mutex);
4692 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004693 if ((runtime->hw.rates & mout->spdif_rates) &&
4694 (runtime->hw.formats & mout->spdif_formats)) {
4695 runtime->hw.rates &= mout->spdif_rates;
4696 runtime->hw.formats &= mout->spdif_formats;
4697 if (mout->spdif_maxbps < hinfo->maxbps)
4698 hinfo->maxbps = mout->spdif_maxbps;
4699 } else {
4700 mout->share_spdif = 0;
4701 /* FIXME: need notify? */
4702 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004703 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004704 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4707 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4708}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004709EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Takashi Iwaid5191e52009-11-16 14:58:17 +01004711/**
4712 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004713 * @codec: the HDA codec
4714 * @mout: hda_multi_out object
4715 * @stream_tag: stream tag to assign
4716 * @format: format id to assign
4717 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004718 *
4719 * Set up the i/o for analog out.
4720 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004722int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4723 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 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{
Takashi Iwaidda14412011-05-02 11:29:30 +02004728 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004730 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 int i;
4732
Ingo Molnar62932df2006-01-16 16:34:20 +01004733 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004734 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004735 if (mout->dig_out_nid && mout->share_spdif &&
4736 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004738 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4739 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004740 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004742 setup_dig_out_stream(codec, mout->dig_out_nid,
4743 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 } else {
4745 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004746 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
4748 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004749 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004752 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4753 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004754 if (!mout->no_share_stream &&
4755 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004757 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4758 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004759 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004760 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4761 if (!mout->no_share_stream && mout->hp_out_nid[i])
4762 snd_hda_codec_setup_stream(codec,
4763 mout->hp_out_nid[i],
4764 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004765
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 /* surrounds */
4767 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004768 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004769 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4770 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004771 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004772 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4773 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004775
4776 /* extra surrounds */
4777 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4778 int ch = 0;
4779 if (!mout->extra_out_nid[i])
4780 break;
4781 if (chs >= (i + 1) * 2)
4782 ch = i * 2;
4783 else if (!mout->no_share_stream)
4784 break;
4785 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4786 stream_tag, ch, format);
4787 }
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 return 0;
4790}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004791EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Takashi Iwaid5191e52009-11-16 14:58:17 +01004793/**
4794 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004795 * @codec: the HDA codec
4796 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004798int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4799 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Takashi Iwaidda14412011-05-02 11:29:30 +02004801 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 int i;
4803
4804 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004805 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004807 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004808 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4809 if (mout->hp_out_nid[i])
4810 snd_hda_codec_cleanup_stream(codec,
4811 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004812 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4813 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004814 snd_hda_codec_cleanup_stream(codec,
4815 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004816 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004818 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 mout->dig_out_used = 0;
4820 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004821 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return 0;
4823}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004824EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Takashi Iwai47408602012-04-20 13:06:53 +02004826/**
4827 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004828 * @codec: the HDA codec
4829 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004830 *
4831 * Guess the suitable VREF pin bits to be set as the pin-control value.
4832 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4833 */
4834unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4835{
4836 unsigned int pincap;
4837 unsigned int oldval;
4838 oldval = snd_hda_codec_read(codec, pin, 0,
4839 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4840 pincap = snd_hda_query_pin_caps(codec, pin);
4841 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4842 /* Exception: if the default pin setup is vref50, we give it priority */
4843 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4844 return AC_PINCTL_VREF_80;
4845 else if (pincap & AC_PINCAP_VREF_50)
4846 return AC_PINCTL_VREF_50;
4847 else if (pincap & AC_PINCAP_VREF_100)
4848 return AC_PINCTL_VREF_100;
4849 else if (pincap & AC_PINCAP_VREF_GRD)
4850 return AC_PINCTL_VREF_GRD;
4851 return AC_PINCTL_VREF_HIZ;
4852}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004853EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004854
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004855/**
4856 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4857 * @codec: the HDA codec
4858 * @pin: referred pin NID
4859 * @val: pin ctl value to audit
4860 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004861unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4862 hda_nid_t pin, unsigned int val)
4863{
4864 static unsigned int cap_lists[][2] = {
4865 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4866 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4867 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4868 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4869 };
4870 unsigned int cap;
4871
4872 if (!val)
4873 return 0;
4874 cap = snd_hda_query_pin_caps(codec, pin);
4875 if (!cap)
4876 return val; /* don't know what to do... */
4877
4878 if (val & AC_PINCTL_OUT_EN) {
4879 if (!(cap & AC_PINCAP_OUT))
4880 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4881 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4882 val &= ~AC_PINCTL_HP_EN;
4883 }
4884
4885 if (val & AC_PINCTL_IN_EN) {
4886 if (!(cap & AC_PINCAP_IN))
4887 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4888 else {
4889 unsigned int vcap, vref;
4890 int i;
4891 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4892 vref = val & AC_PINCTL_VREFEN;
4893 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4894 if (vref == cap_lists[i][0] &&
4895 !(vcap & cap_lists[i][1])) {
4896 if (i == ARRAY_SIZE(cap_lists) - 1)
4897 vref = AC_PINCTL_VREF_HIZ;
4898 else
4899 vref = cap_lists[i + 1][0];
4900 }
4901 }
4902 val &= ~AC_PINCTL_VREFEN;
4903 val |= vref;
4904 }
4905 }
4906
4907 return val;
4908}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004909EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004910
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004911/**
4912 * _snd_hda_pin_ctl - Helper to set pin ctl value
4913 * @codec: the HDA codec
4914 * @pin: referred pin NID
4915 * @val: pin control value to set
4916 * @cached: access over codec pinctl cache or direct write
4917 *
4918 * This function is a helper to set a pin ctl value more safely.
4919 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4920 * value in pin target array via snd_hda_codec_set_pin_target(), then
4921 * actually writes the value via either snd_hda_codec_update_cache() or
4922 * snd_hda_codec_write() depending on @cached flag.
4923 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004924int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4925 unsigned int val, bool cached)
4926{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004927 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004928 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004929 if (cached)
4930 return snd_hda_codec_update_cache(codec, pin, 0,
4931 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4932 else
4933 return snd_hda_codec_write(codec, pin, 0,
4934 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4935}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004936EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004937
Takashi Iwai990061c2010-09-09 22:08:44 +02004938/**
4939 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004940 * @codec: the HDA codec
4941 * @imux: imux helper object
4942 * @label: the name of imux item to assign
4943 * @index: index number of imux item to assign
4944 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004945 *
4946 * When the same label is used already in the existing items, the number
4947 * suffix is appended to the label. This label index number is stored
4948 * to type_idx when non-NULL pointer is given.
4949 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004950int snd_hda_add_imux_item(struct hda_codec *codec,
4951 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004952 int index, int *type_idx)
4953{
4954 int i, label_idx = 0;
4955 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004956 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004957 return -EINVAL;
4958 }
4959 for (i = 0; i < imux->num_items; i++) {
4960 if (!strncmp(label, imux->items[i].label, strlen(label)))
4961 label_idx++;
4962 }
4963 if (type_idx)
4964 *type_idx = label_idx;
4965 if (label_idx > 0)
4966 snprintf(imux->items[imux->num_items].label,
4967 sizeof(imux->items[imux->num_items].label),
4968 "%s %d", label, label_idx);
4969 else
4970 strlcpy(imux->items[imux->num_items].label, label,
4971 sizeof(imux->items[imux->num_items].label));
4972 imux->items[imux->num_items].index = index;
4973 imux->num_items++;
4974 return 0;
4975}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004976EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004979 * snd_hda_bus_reset - Reset the bus
4980 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004982void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004984 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004986 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004987 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004988 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004989#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004990 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004991 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004992 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004993 }
4994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004997EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004998
4999/*
5000 * generic arrays
5001 */
5002
Takashi Iwaid5191e52009-11-16 14:58:17 +01005003/**
5004 * snd_array_new - get a new element from the given array
5005 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005006 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005007 * Get a new element from the given array. If it exceeds the
5008 * pre-allocated array size, re-allocate the array.
5009 *
5010 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005011 */
5012void *snd_array_new(struct snd_array *array)
5013{
Takashi Iwai12f17712012-10-10 08:59:14 +02005014 if (snd_BUG_ON(!array->elem_size))
5015 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005016 if (array->used >= array->alloced) {
5017 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005018 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005019 void *nlist;
5020 if (snd_BUG_ON(num >= 4096))
5021 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005022 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005023 if (!nlist)
5024 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005025 array->list = nlist;
5026 array->alloced = num;
5027 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005028 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005029}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005030EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005031
Takashi Iwaid5191e52009-11-16 14:58:17 +01005032/**
5033 * snd_array_free - free the given array elements
5034 * @array: the array object
5035 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005036void snd_array_free(struct snd_array *array)
5037{
5038 kfree(array->list);
5039 array->used = 0;
5040 array->alloced = 0;
5041 array->list = NULL;
5042}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005043EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005044
Takashi Iwaid5191e52009-11-16 14:58:17 +01005045/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005046 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5047 * @pcm: PCM caps bits
5048 * @buf: the string buffer to write
5049 * @buflen: the max buffer length
5050 *
5051 * used by hda_proc.c and hda_eld.c
5052 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005053void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5054{
5055 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5056 int i, j;
5057
5058 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5059 if (pcm & (AC_SUPPCM_BITS_8 << i))
5060 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5061
5062 buf[j] = '\0'; /* necessary when j == 0 */
5063}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005064EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005065
5066MODULE_DESCRIPTION("HDA codec core");
5067MODULE_LICENSE("GPL");