blob: 39b5660653f0f48ab161313fd4f798d0f67a7b7a [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 Iwai05852442015-03-03 15:40:08 +0100127 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200128 */
Takashi Iwai05852442015-03-03 15:40:08 +0100129static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200131{
Takashi Iwai05852442015-03-03 15:40:08 +0100132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200133 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200134 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200135
Wu Fengguang6430aee2009-07-17 16:49:19 +0800136 if (cmd == ~0)
137 return -1;
138
Takashi Iwai8dd78332009-06-02 01:16:07 +0200139 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200140 snd_hda_power_up(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100141 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
145 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200146 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100147 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200148 snd_hda_power_down(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100149 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200150 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100151 codec_dbg(codec,
152 "resetting BUS due to fatal communication error\n");
Takashi Iwai8dd78332009-06-02 01:16:07 +0200153 bus->ops.bus_reset(bus);
154 }
155 goto again;
156 }
157 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100158 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200159 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200160 return err;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200167 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @verb: the verb to send
169 * @parm: the parameter for the verb
170 *
171 * Send a single command and read the corresponding response.
172 *
173 * Returns the obtained response value, or -1 for an error.
174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200175unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200176 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int verb, unsigned int parm)
178{
Takashi Iwai7639a062015-03-03 10:07:24 +0100179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200180 unsigned int res;
Takashi Iwai05852442015-03-03 15:40:08 +0100181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700182 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return res;
184}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100185EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/**
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200191 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @verb: the verb to send
193 * @parm: the parameter for the verb
194 *
195 * Send a single command without waiting for response.
196 *
197 * Returns 0 if successful, or a negative error code.
198 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200199int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Takashi Iwai7639a062015-03-03 10:07:24 +0100202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwai05852442015-03-03 15:40:08 +0100203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100205EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
211 *
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
214 */
215void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
216{
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
219}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100220EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100222/* connection list element */
223struct hda_conn_list {
224 struct list_head list;
225 int len;
226 hda_nid_t nid;
227 hda_nid_t conns[0];
228};
229
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200230/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100231static struct hda_conn_list *
232lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200233{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
236 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200237 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200238 }
239 return NULL;
240}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200241
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100242static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
244{
245 struct hda_conn_list *p;
246
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
248 if (!p)
249 return -ENOMEM;
250 p->len = len;
251 p->nid = nid;
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
254 return 0;
255}
256
257static void remove_conn_list(struct hda_codec *codec)
258{
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
262 list_del(&p->list);
263 kfree(p);
264 }
265}
266
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200267/* read the connection and add to the cache */
268static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200269{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100270 hda_nid_t list[32];
271 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200272 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200273
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
278 if (!result)
279 return -ENOMEM;
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
281 }
282 if (len >= 0)
283 len = snd_hda_override_conn_list(codec, nid, len, result);
284 if (result != list)
285 kfree(result);
286 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200287}
Takashi Iwaidce20792011-06-24 14:10:28 +0200288
289/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
292 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 * @listp: the pointer to store NID list
294 *
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
297 *
298 * Returns the number of connections, or a negative error code.
299 *
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
303 */
304int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
306{
307 bool added = false;
308
309 for (;;) {
310 int err;
311 const struct hda_conn_list *p;
312
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
315 if (p) {
316 if (listp)
317 *listp = p->conns;
318 return p->len;
319 }
320 if (snd_BUG_ON(added))
321 return -EINVAL;
322
323 err = read_and_add_raw_conns(codec, nid);
324 if (err < 0)
325 return err;
326 added = true;
327 }
328}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100329EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100330
331/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @codec: the HDA codec
334 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200335 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @max_conns: max. number of connections to store
337 *
338 * Parses the connection list of the given widget and stores the list
339 * of NIDs.
340 *
341 * Returns the number of connections, or a negative error code.
342 */
343int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200344 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 return -EINVAL;
354 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200356 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200359}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100360EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
365 * @nid: NID to parse
366 * @len: number of connection list entries
367 * @list: the list of connection entries
368 *
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
371 *
372 * Returns zero or a negative error code.
373 */
374int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100377 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 p = lookup_conn_list(codec, nid);
380 if (p) {
381 list_del(&p->list);
382 kfree(p);
383 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200384
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100385 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100387EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200388
389/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
395 *
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
399 */
400int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
402{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200404 int i, nums;
405
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100406 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200407 for (i = 0; i < nums; i++)
408 if (conn[i] == nid)
409 return i;
410 if (!recursive)
411 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100412 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200414 return -1;
415 }
416 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
420 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
422 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200423 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200424 return -1;
425}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100426EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Mengdong Linf1aa0682013-08-26 21:35:21 -0400428
429/* return DEVLIST_LEN parameter of the given widget */
430static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
437 return 0;
438
439 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
440 if (parm == -1 && codec->bus->rirb_error)
441 parm = 0;
442 return parm & AC_DEV_LIST_LEN_MASK;
443}
444
445/**
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
451 *
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
454 */
455int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
457{
458 unsigned int parm;
459 int i, dev_len, devices;
460
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
463 return 0;
464
465 dev_len = parm + 1;
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
467
468 devices = 0;
469 while (devices < dev_len) {
470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_DEVICE_LIST, devices);
472 if (parm == -1 && codec->bus->rirb_error)
473 break;
474
475 for (i = 0; i < 8; i++) {
476 dev_list[devices] = (u8)parm;
477 parm >>= 4;
478 devices++;
479 if (devices >= dev_len)
480 break;
481 }
482 }
483 return devices;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * destructor
488 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100489static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200491 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (bus->ops.private_free)
494 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100495 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100499static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Takashi Iwai2565c892014-02-19 11:41:09 +0100501 snd_hda_bus_free(device->device_data);
502 return 0;
503}
504
505static int snd_hda_bus_dev_disconnect(struct snd_device *device)
506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100508 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100512/* hdac_bus_ops translations */
513static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
514{
515 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
516 return bus->ops.command(bus, cmd);
517}
518
519static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
520 unsigned int *res)
521{
522 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
523 *res = bus->ops.get_response(bus, addr);
524 return bus->rirb_error ? -EIO : 0;
525}
526
527static const struct hdac_bus_ops bus_ops = {
528 .command = _hda_bus_command,
529 .get_response = _hda_bus_get_response,
530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/**
533 * snd_hda_bus_new - create a HDA bus
534 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * @busp: the pointer to store the created bus instance
536 *
537 * Returns 0 if successful, or a negative error code.
538 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100539int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100540 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct hda_bus *bus;
543 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100544 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100545 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 .dev_free = snd_hda_bus_dev_free,
547 };
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (busp)
550 *busp = NULL;
551
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200552 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100553 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100556 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
557 if (err < 0) {
558 kfree(bus);
559 return err;
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200563 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
566 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 snd_hda_bus_free(bus);
568 return err;
569 }
570 if (busp)
571 *busp = bus;
572 return 0;
573}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100574EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 * read widget caps for each widget and store in cache
578 */
579static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
580{
581 int i;
582 hda_nid_t nid;
583
Takashi Iwai7639a062015-03-03 10:07:24 +0100584 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200585 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100586 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100587 nid = codec->core.start_nid;
588 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100589 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
590 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100591 return 0;
592}
593
Takashi Iwai3be14142009-02-20 14:11:16 +0100594/* read all pin default configurations and save codec->init_pins */
595static int read_pin_defaults(struct hda_codec *codec)
596{
Takashi Iwai7639a062015-03-03 10:07:24 +0100597 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100598
Takashi Iwai7639a062015-03-03 10:07:24 +0100599 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100600 struct hda_pincfg *pin;
601 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200602 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100603 if (wid_type != AC_WID_PIN)
604 continue;
605 pin = snd_array_new(&codec->init_pins);
606 if (!pin)
607 return -ENOMEM;
608 pin->nid = nid;
609 pin->cfg = snd_hda_codec_read(codec, nid, 0,
610 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200611 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
612 AC_VERB_GET_PIN_WIDGET_CONTROL,
613 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100614 }
615 return 0;
616}
617
618/* look up the given pin config list and return the item matching with NID */
619static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
620 struct snd_array *array,
621 hda_nid_t nid)
622{
623 int i;
624 for (i = 0; i < array->used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(array, i);
626 if (pin->nid == nid)
627 return pin;
628 }
629 return NULL;
630}
631
Takashi Iwai3be14142009-02-20 14:11:16 +0100632/* set the current pin config value for the given NID.
633 * the value is cached, and read via snd_hda_codec_get_pincfg()
634 */
635int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
636 hda_nid_t nid, unsigned int cfg)
637{
638 struct hda_pincfg *pin;
639
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200640 /* the check below may be invalid when pins are added by a fixup
641 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
642 * for now
643 */
644 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100645 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
646 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200647 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100648
Takashi Iwai3be14142009-02-20 14:11:16 +0100649 pin = look_up_pincfg(codec, list, nid);
650 if (!pin) {
651 pin = snd_array_new(list);
652 if (!pin)
653 return -ENOMEM;
654 pin->nid = nid;
655 }
656 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100657 return 0;
658}
659
Takashi Iwaid5191e52009-11-16 14:58:17 +0100660/**
661 * snd_hda_codec_set_pincfg - Override a pin default configuration
662 * @codec: the HDA codec
663 * @nid: NID to set the pin config
664 * @cfg: the pin default config value
665 *
666 * Override a pin default configuration value in the cache.
667 * This value can be read by snd_hda_codec_get_pincfg() in a higher
668 * priority than the real hardware value.
669 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100670int snd_hda_codec_set_pincfg(struct hda_codec *codec,
671 hda_nid_t nid, unsigned int cfg)
672{
Takashi Iwai346ff702009-02-23 09:42:57 +0100673 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100674}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100675EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100676
Takashi Iwaid5191e52009-11-16 14:58:17 +0100677/**
678 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
679 * @codec: the HDA codec
680 * @nid: NID to get the pin config
681 *
682 * Get the current pin config value of the given pin NID.
683 * If the pincfg value is cached or overridden via sysfs or driver,
684 * returns the cached value.
685 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100686unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
687{
688 struct hda_pincfg *pin;
689
Takashi Iwai648a8d22014-02-25 10:38:13 +0100690#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100691 {
692 unsigned int cfg = 0;
693 mutex_lock(&codec->user_mutex);
694 pin = look_up_pincfg(codec, &codec->user_pins, nid);
695 if (pin)
696 cfg = pin->cfg;
697 mutex_unlock(&codec->user_mutex);
698 if (cfg)
699 return cfg;
700 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100701#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100702 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
703 if (pin)
704 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100705 pin = look_up_pincfg(codec, &codec->init_pins, nid);
706 if (pin)
707 return pin->cfg;
708 return 0;
709}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100710EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100711
Takashi Iwai95a962c2014-10-29 16:03:58 +0100712/**
713 * snd_hda_codec_set_pin_target - remember the current pinctl target value
714 * @codec: the HDA codec
715 * @nid: pin NID
716 * @val: assigned pinctl value
717 *
718 * This function stores the given value to a pinctl target value in the
719 * pincfg table. This isn't always as same as the actually written value
720 * but can be referred at any time via snd_hda_codec_get_pin_target().
721 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100722int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
723 unsigned int val)
724{
725 struct hda_pincfg *pin;
726
727 pin = look_up_pincfg(codec, &codec->init_pins, nid);
728 if (!pin)
729 return -EINVAL;
730 pin->target = val;
731 return 0;
732}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100733EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100734
Takashi Iwai95a962c2014-10-29 16:03:58 +0100735/**
736 * snd_hda_codec_get_pin_target - return the current pinctl target value
737 * @codec: the HDA codec
738 * @nid: pin NID
739 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100740int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
741{
742 struct hda_pincfg *pin;
743
744 pin = look_up_pincfg(codec, &codec->init_pins, nid);
745 if (!pin)
746 return 0;
747 return pin->target;
748}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100749EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100750
Takashi Iwai92ee6162009-12-27 11:18:59 +0100751/**
752 * snd_hda_shutup_pins - Shut up all pins
753 * @codec: the HDA codec
754 *
755 * Clear all pin controls to shup up before suspend for avoiding click noise.
756 * The controls aren't cached so that they can be resumed properly.
757 */
758void snd_hda_shutup_pins(struct hda_codec *codec)
759{
760 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200761 /* don't shut up pins when unloading the driver; otherwise it breaks
762 * the default pin setup at the next load of the driver
763 */
764 if (codec->bus->shutdown)
765 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100766 for (i = 0; i < codec->init_pins.used; i++) {
767 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
768 /* use read here for syncing after issuing each verb */
769 snd_hda_codec_read(codec, pin->nid, 0,
770 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
771 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200772 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100773}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100774EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100775
Takashi Iwai2a439522011-07-26 09:52:50 +0200776#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200777/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
778static void restore_shutup_pins(struct hda_codec *codec)
779{
780 int i;
781 if (!codec->pins_shutup)
782 return;
783 if (codec->bus->shutdown)
784 return;
785 for (i = 0; i < codec->init_pins.used; i++) {
786 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
787 snd_hda_codec_write(codec, pin->nid, 0,
788 AC_VERB_SET_PIN_WIDGET_CONTROL,
789 pin->ctrl);
790 }
791 codec->pins_shutup = 0;
792}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700793#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200794
David Henningsson26a6cb62012-10-09 15:04:21 +0200795static void hda_jackpoll_work(struct work_struct *work)
796{
797 struct hda_codec *codec =
798 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200799
800 snd_hda_jack_set_dirty_all(codec);
801 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400802
803 if (!codec->jackpoll_interval)
804 return;
805
Takashi Iwai2f35c632015-02-27 22:43:26 +0100806 schedule_delayed_work(&codec->jackpoll_work,
807 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200808}
809
Takashi Iwai01751f52007-08-10 16:59:39 +0200810static void init_hda_cache(struct hda_cache_rec *cache,
811 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200812static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200813
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100814/* release all pincfg lists */
815static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100816{
Takashi Iwai346ff702009-02-23 09:42:57 +0100817 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100818#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100819 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100820#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100821 snd_array_free(&codec->init_pins);
822}
823
Takashi Iwai54d17402005-11-21 16:33:22 +0100824/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200825 * audio-converter setup caches
826 */
827struct hda_cvt_setup {
828 hda_nid_t nid;
829 u8 stream_tag;
830 u8 channel_id;
831 u16 format_id;
832 unsigned char active; /* cvt is currently used */
833 unsigned char dirty; /* setups should be cleared */
834};
835
836/* get or create a cache entry for the given audio converter NID */
837static struct hda_cvt_setup *
838get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
839{
840 struct hda_cvt_setup *p;
841 int i;
842
843 for (i = 0; i < codec->cvt_setups.used; i++) {
844 p = snd_array_elem(&codec->cvt_setups, i);
845 if (p->nid == nid)
846 return p;
847 }
848 p = snd_array_new(&codec->cvt_setups);
849 if (p)
850 p->nid = nid;
851 return p;
852}
853
854/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100855 * PCM device
856 */
857static void release_pcm(struct kref *kref)
858{
859 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
860
861 if (pcm->pcm)
862 snd_device_free(pcm->codec->card, pcm->pcm);
863 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
864 kfree(pcm->name);
865 kfree(pcm);
866}
867
868void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
869{
870 kref_put(&pcm->kref, release_pcm);
871}
872EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
873
874struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
875 const char *fmt, ...)
876{
877 struct hda_pcm *pcm;
878 va_list args;
879
880 va_start(args, fmt);
881 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
882 if (!pcm)
883 return NULL;
884
885 pcm->codec = codec;
886 kref_init(&pcm->kref);
887 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
888 if (!pcm->name) {
889 kfree(pcm);
890 return NULL;
891 }
892
893 list_add_tail(&pcm->list, &codec->pcm_list_head);
894 return pcm;
895}
896EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
897
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100898/*
899 * codec destructor
900 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100901static void codec_release_pcms(struct hda_codec *codec)
902{
903 struct hda_pcm *pcm, *n;
904
905 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
906 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100907 if (pcm->pcm)
908 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100909 snd_hda_codec_pcm_put(pcm);
910 }
911}
912
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100913void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
914{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100915 if (codec->registered) {
916 /* pm_runtime_put() is called in snd_hdac_device_exit() */
917 pm_runtime_get_noresume(hda_codec_dev(codec));
918 pm_runtime_disable(hda_codec_dev(codec));
919 codec->registered = 0;
920 }
921
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100922 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100923 if (!codec->in_freeing)
924 snd_hda_ctls_clear(codec);
925 codec_release_pcms(codec);
926 snd_hda_detach_beep_device(codec);
927 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
928 snd_hda_jack_tbl_clear(codec);
929 codec->proc_widget_hook = NULL;
930 codec->spec = NULL;
931
932 free_hda_cache(&codec->amp_cache);
933 free_hda_cache(&codec->cmd_cache);
934 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
935 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
936
937 /* free only driver_pins so that init_pins + user_pins are restored */
938 snd_array_free(&codec->driver_pins);
939 snd_array_free(&codec->cvt_setups);
940 snd_array_free(&codec->spdif_out);
941 snd_array_free(&codec->verbs);
942 codec->preset = NULL;
943 codec->slave_dig_outs = NULL;
944 codec->spdif_status_reset = 0;
945 snd_array_free(&codec->mixers);
946 snd_array_free(&codec->nids);
947 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100948 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100949}
950
Takashi Iwaid8193872012-08-31 07:54:38 -0700951static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100952 unsigned int power_state);
953
Takashi Iwaic4c25332015-03-03 17:22:12 +0100954/* also called from hda_bind.c */
955void snd_hda_codec_register(struct hda_codec *codec)
956{
957 if (codec->registered)
958 return;
959 if (device_is_registered(hda_codec_dev(codec))) {
960 snd_hda_register_beep_device(codec);
961 pm_runtime_enable(hda_codec_dev(codec));
962 /* it was powered up in snd_hda_codec_new(), now all done */
963 snd_hda_power_down(codec);
964 codec->registered = 1;
965 }
966}
967
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100968static int snd_hda_codec_dev_register(struct snd_device *device)
969{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100970 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100971 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100972}
973
974static int snd_hda_codec_dev_disconnect(struct snd_device *device)
975{
976 struct hda_codec *codec = device->device_data;
977
Takashi Iwaid604b392014-02-28 13:42:09 +0100978 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100979 return 0;
980}
981
Takashi Iwai2565c892014-02-19 11:41:09 +0100982static int snd_hda_codec_dev_free(struct snd_device *device)
983{
Takashi Iwaid56db742015-02-27 23:25:35 +0100984 struct hda_codec *codec = device->device_data;
985
986 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100987 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100988 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100989 return 0;
990}
991
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100992static void snd_hda_codec_dev_release(struct device *dev)
993{
Takashi Iwaid56db742015-02-27 23:25:35 +0100994 struct hda_codec *codec = dev_to_hda_codec(dev);
995
996 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100997 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100998 snd_hda_sysfs_clear(codec);
999 free_hda_cache(&codec->amp_cache);
1000 free_hda_cache(&codec->cmd_cache);
Takashi Iwaid56db742015-02-27 23:25:35 +01001001 kfree(codec->modelname);
1002 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +01001003 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/**
1007 * snd_hda_codec_new - create a HDA codec
1008 * @bus: the bus to assign
1009 * @codec_addr: the codec address
1010 * @codecp: the pointer to store the generated codec
1011 *
1012 * Returns 0 if successful, or a negative error code.
1013 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001014int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1015 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001018 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001019 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001021 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001022 .dev_register = snd_hda_codec_dev_register,
1023 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001024 .dev_free = snd_hda_codec_dev_free,
1025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Takashi Iwaida3cec32008-08-08 17:12:14 +02001027 if (snd_BUG_ON(!bus))
1028 return -EINVAL;
1029 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001032 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001033 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Takashi Iwai7639a062015-03-03 10:07:24 +01001036 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1037 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1038 codec_addr);
1039 if (err < 0) {
1040 kfree(codec);
1041 return err;
1042 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001043
Takashi Iwai7639a062015-03-03 10:07:24 +01001044 codec->core.dev.release = snd_hda_codec_dev_release;
1045 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +01001046 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001049 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001051 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001052 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001053 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001054 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001055 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001056 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1057 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001058 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001059 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001060 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001061 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001062 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001063 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001064 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001065 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001066
David Henningsson26a6cb62012-10-09 15:04:21 +02001067 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001068 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001069 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Takashi Iwai83012a72012-08-24 18:38:08 +02001071#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001072 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001073#endif
1074
Takashi Iwai648a8d22014-02-25 10:38:13 +01001075 snd_hda_sysfs_init(codec);
1076
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001077 if (codec->bus->modelname) {
1078 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1079 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001080 err = -ENODEV;
1081 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001082 }
1083 }
1084
Takashi Iwai7639a062015-03-03 10:07:24 +01001085 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001086 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001087 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001089 err = read_pin_defaults(codec);
1090 if (err < 0)
1091 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001092
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001093 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001094 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001095
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001096 snd_hda_codec_proc_new(codec);
1097
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001098 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001099
Takashi Iwai7639a062015-03-03 10:07:24 +01001100 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1101 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001102 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001103
Takashi Iwai6efdd852015-02-27 16:09:22 +01001104 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001105 if (err < 0)
1106 goto error;
1107
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001108 if (codecp)
1109 *codecp = codec;
1110 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001111
1112 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001113 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001114 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001115}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001116EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001117
Takashi Iwai95a962c2014-10-29 16:03:58 +01001118/**
1119 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1120 * @codec: the HDA codec
1121 *
1122 * Forcibly refresh the all widget caps and the init pin configurations of
1123 * the given codec.
1124 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001125int snd_hda_codec_update_widgets(struct hda_codec *codec)
1126{
1127 hda_nid_t fg;
1128 int err;
1129
Takashi Iwai7639a062015-03-03 10:07:24 +01001130 err = snd_hdac_refresh_widgets(&codec->core);
1131 if (err < 0)
1132 return err;
1133
Mengdong Lina15d05d2013-02-08 17:09:31 -05001134 /* Assume the function group node does not change,
1135 * only the widget nodes may change.
1136 */
1137 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001138 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001139 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001140 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001141 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001142
1143 snd_array_free(&codec->init_pins);
1144 err = read_pin_defaults(codec);
1145
1146 return err;
1147}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001148EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001149
Takashi Iwaied360812012-08-08 17:12:52 +02001150/* update the stream-id if changed */
1151static void update_pcm_stream_id(struct hda_codec *codec,
1152 struct hda_cvt_setup *p, hda_nid_t nid,
1153 u32 stream_tag, int channel_id)
1154{
1155 unsigned int oldval, newval;
1156
1157 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1158 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1159 newval = (stream_tag << 4) | channel_id;
1160 if (oldval != newval)
1161 snd_hda_codec_write(codec, nid, 0,
1162 AC_VERB_SET_CHANNEL_STREAMID,
1163 newval);
1164 p->stream_tag = stream_tag;
1165 p->channel_id = channel_id;
1166 }
1167}
1168
1169/* update the format-id if changed */
1170static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1171 hda_nid_t nid, int format)
1172{
1173 unsigned int oldval;
1174
1175 if (p->format_id != format) {
1176 oldval = snd_hda_codec_read(codec, nid, 0,
1177 AC_VERB_GET_STREAM_FORMAT, 0);
1178 if (oldval != format) {
1179 msleep(1);
1180 snd_hda_codec_write(codec, nid, 0,
1181 AC_VERB_SET_STREAM_FORMAT,
1182 format);
1183 }
1184 p->format_id = format;
1185 }
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/**
1189 * snd_hda_codec_setup_stream - set up the codec for streaming
1190 * @codec: the CODEC to set up
1191 * @nid: the NID to set up
1192 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1193 * @channel_id: channel id to pass, zero based.
1194 * @format: stream format.
1195 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001196void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1197 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int channel_id, int format)
1199{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001200 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001201 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001202 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001203 int i;
1204
Takashi Iwai0ba21762007-04-16 11:29:14 +02001205 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001206 return;
1207
Takashi Iwai4e76a882014-02-25 12:21:03 +01001208 codec_dbg(codec,
1209 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1210 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001211 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001212 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001213 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001214
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001215 if (codec->patch_ops.stream_pm)
1216 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001217 if (codec->pcm_format_first)
1218 update_pcm_format(codec, p, nid, format);
1219 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1220 if (!codec->pcm_format_first)
1221 update_pcm_format(codec, p, nid, format);
1222
Takashi Iwaieb541332010-08-06 13:48:11 +02001223 p->active = 1;
1224 p->dirty = 0;
1225
1226 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001227 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001228 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001229 for (i = 0; i < c->cvt_setups.used; i++) {
1230 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001231 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001232 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001233 p->dirty = 1;
1234 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001237EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Takashi Iwaif0cea792010-08-13 11:56:53 +02001239static void really_cleanup_stream(struct hda_codec *codec,
1240 struct hda_cvt_setup *q);
1241
Takashi Iwaid5191e52009-11-16 14:58:17 +01001242/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001243 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001244 * @codec: the CODEC to clean up
1245 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001246 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001247 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001248void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1249 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001250{
Takashi Iwaieb541332010-08-06 13:48:11 +02001251 struct hda_cvt_setup *p;
1252
Takashi Iwai888afa12008-03-18 09:57:50 +01001253 if (!nid)
1254 return;
1255
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001256 if (codec->no_sticky_stream)
1257 do_now = 1;
1258
Takashi Iwai4e76a882014-02-25 12:21:03 +01001259 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001260 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001261 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001262 /* here we just clear the active flag when do_now isn't set;
1263 * actual clean-ups will be done later in
1264 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1265 */
1266 if (do_now)
1267 really_cleanup_stream(codec, p);
1268 else
1269 p->active = 0;
1270 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001272EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001273
Takashi Iwaieb541332010-08-06 13:48:11 +02001274static void really_cleanup_stream(struct hda_codec *codec,
1275 struct hda_cvt_setup *q)
1276{
1277 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001278 if (q->stream_tag || q->channel_id)
1279 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1280 if (q->format_id)
1281 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1282);
Takashi Iwaieb541332010-08-06 13:48:11 +02001283 memset(q, 0, sizeof(*q));
1284 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001285 if (codec->patch_ops.stream_pm)
1286 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001287}
1288
1289/* clean up the all conflicting obsolete streams */
1290static void purify_inactive_streams(struct hda_codec *codec)
1291{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001292 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001293 int i;
1294
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001295 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001296 for (i = 0; i < c->cvt_setups.used; i++) {
1297 struct hda_cvt_setup *p;
1298 p = snd_array_elem(&c->cvt_setups, i);
1299 if (p->dirty)
1300 really_cleanup_stream(c, p);
1301 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001302 }
1303}
1304
Takashi Iwai2a439522011-07-26 09:52:50 +02001305#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001306/* clean up all streams; called from suspend */
1307static void hda_cleanup_all_streams(struct hda_codec *codec)
1308{
1309 int i;
1310
1311 for (i = 0; i < codec->cvt_setups.used; i++) {
1312 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1313 if (p->stream_tag)
1314 really_cleanup_stream(codec, p);
1315 }
1316}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001317#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
1320 * amp access functions
1321 */
1322
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001323/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001324#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001325#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001326#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1327#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001329#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001332static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001333 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Takashi Iwai01751f52007-08-10 16:59:39 +02001335 memset(cache, 0, sizeof(*cache));
1336 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001337 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001338}
1339
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001340static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001341{
Takashi Iwai603c4012008-07-30 15:01:44 +02001342 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001346static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Takashi Iwai01751f52007-08-10 16:59:39 +02001348 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1349 u16 cur = cache->hash[idx];
1350 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001353 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (info->key == key)
1355 return info;
1356 cur = info->next;
1357 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001358 return NULL;
1359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001361/* query the hash. allocate an entry if not found. */
1362static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1363 u32 key)
1364{
1365 struct hda_cache_head *info = get_hash(cache, key);
1366 if (!info) {
1367 u16 idx, cur;
1368 /* add a new hash entry */
1369 info = snd_array_new(&cache->buf);
1370 if (!info)
1371 return NULL;
1372 cur = snd_array_index(&cache->buf, info);
1373 info->key = key;
1374 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001375 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001376 idx = key % (u16)ARRAY_SIZE(cache->hash);
1377 info->next = cache->hash[idx];
1378 cache->hash[idx] = cur;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return info;
1381}
1382
Takashi Iwai01751f52007-08-10 16:59:39 +02001383/* query and allocate an amp hash entry */
1384static inline struct hda_amp_info *
1385get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1386{
1387 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1388}
1389
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001390/* overwrite the value with the key in the caps hash */
1391static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1392{
1393 struct hda_amp_info *info;
1394
1395 mutex_lock(&codec->hash_mutex);
1396 info = get_alloc_amp_hash(codec, key);
1397 if (!info) {
1398 mutex_unlock(&codec->hash_mutex);
1399 return -EINVAL;
1400 }
1401 info->amp_caps = val;
1402 info->head.val |= INFO_AMP_CAPS;
1403 mutex_unlock(&codec->hash_mutex);
1404 return 0;
1405}
1406
1407/* query the value from the caps hash; if not found, fetch the current
1408 * value from the given function and store in the hash
1409 */
1410static unsigned int
1411query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1412 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1413{
1414 struct hda_amp_info *info;
1415 unsigned int val;
1416
1417 mutex_lock(&codec->hash_mutex);
1418 info = get_alloc_amp_hash(codec, key);
1419 if (!info) {
1420 mutex_unlock(&codec->hash_mutex);
1421 return 0;
1422 }
1423 if (!(info->head.val & INFO_AMP_CAPS)) {
1424 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1425 val = func(codec, nid, dir);
1426 write_caps_hash(codec, key, val);
1427 } else {
1428 val = info->amp_caps;
1429 mutex_unlock(&codec->hash_mutex);
1430 }
1431 return val;
1432}
1433
1434static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1435 int direction)
1436{
1437 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Takashi Iwai7639a062015-03-03 10:07:24 +01001438 nid = codec->core.afg;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001439 return snd_hda_param_read(codec, nid,
1440 direction == HDA_OUTPUT ?
1441 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1442}
1443
Takashi Iwaid5191e52009-11-16 14:58:17 +01001444/**
1445 * query_amp_caps - query AMP capabilities
1446 * @codec: the HD-auio codec
1447 * @nid: the NID to query
1448 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1449 *
1450 * Query AMP capabilities for the given widget and direction.
1451 * Returns the obtained capability bits.
1452 *
1453 * When cap bits have been already read, this doesn't read again but
1454 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001456u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001458 return query_caps_hash(codec, nid, direction,
1459 HDA_HASH_KEY(nid, direction, 0),
1460 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001462EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Takashi Iwaid5191e52009-11-16 14:58:17 +01001464/**
David Henningsson861a04e2014-09-23 10:38:17 +02001465 * snd_hda_check_amp_caps - query AMP capabilities
1466 * @codec: the HD-audio codec
1467 * @nid: the NID to query
1468 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001469 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001470 *
1471 * Check whether the widget has the given amp capability for the direction.
1472 */
1473bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1474 int dir, unsigned int bits)
1475{
1476 if (!nid)
1477 return false;
1478 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1479 if (query_amp_caps(codec, nid, dir) & bits)
1480 return true;
1481 return false;
1482}
1483EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1484
1485/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486 * snd_hda_override_amp_caps - Override the AMP capabilities
1487 * @codec: the CODEC to clean up
1488 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001489 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001490 * @caps: the capability bits to set
1491 *
1492 * Override the cached AMP caps bits value by the given one.
1493 * This function is useful if the driver needs to adjust the AMP ranges,
1494 * e.g. limit to 0dB, etc.
1495 *
1496 * Returns zero if successful or a negative error code.
1497 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001498int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1499 unsigned int caps)
1500{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001501 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001502}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001503EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001504
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001505static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1506 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001507{
1508 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1509}
1510
Takashi Iwaid5191e52009-11-16 14:58:17 +01001511/**
1512 * snd_hda_query_pin_caps - Query PIN capabilities
1513 * @codec: the HD-auio codec
1514 * @nid: the NID to query
1515 *
1516 * Query PIN capabilities for the given widget.
1517 * Returns the obtained capability bits.
1518 *
1519 * When cap bits have been already read, this doesn't read again but
1520 * returns the cached value.
1521 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001522u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1523{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001524 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001525 read_pin_cap);
1526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001527EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001528
Wu Fengguang864f92b2009-11-18 12:38:02 +08001529/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001530 * snd_hda_override_pin_caps - Override the pin capabilities
1531 * @codec: the CODEC
1532 * @nid: the NID to override
1533 * @caps: the capability bits to set
1534 *
1535 * Override the cached PIN capabilitiy bits value by the given one.
1536 *
1537 * Returns zero if successful or a negative error code.
1538 */
1539int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1540 unsigned int caps)
1541{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001542 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001543}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001544EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001545
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001546/* read or sync the hash value with the current value;
1547 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001549static struct hda_amp_info *
1550update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001551 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001553 struct hda_amp_info *info;
1554 unsigned int parm, val = 0;
1555 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001557 retry:
1558 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1559 if (!info)
1560 return NULL;
1561 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1562 if (!val_read) {
1563 mutex_unlock(&codec->hash_mutex);
1564 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1565 parm |= direction == HDA_OUTPUT ?
1566 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1567 parm |= index;
1568 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001570 val &= 0xff;
1571 val_read = true;
1572 mutex_lock(&codec->hash_mutex);
1573 goto retry;
1574 }
1575 info->vol[ch] = val;
1576 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001577 } else if (init_only)
1578 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001579 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001583 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001585static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001586 hda_nid_t nid, int ch, int direction, int index,
1587 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 u32 parm;
1590
1591 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1592 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1593 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001594 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1595 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001596 ; /* set the zero value as a fake mute */
1597 else
1598 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1600}
1601
Takashi Iwaid5191e52009-11-16 14:58:17 +01001602/**
1603 * snd_hda_codec_amp_read - Read AMP value
1604 * @codec: HD-audio codec
1605 * @nid: NID to read the AMP value
1606 * @ch: channel (left=0 or right=1)
1607 * @direction: #HDA_INPUT or #HDA_OUTPUT
1608 * @index: the index value (only for input direction)
1609 *
1610 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
Takashi Iwai834be882006-03-01 14:16:17 +01001612int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1613 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001615 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001616 unsigned int val = 0;
1617
1618 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001619 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001620 if (info)
1621 val = info->vol[ch];
1622 mutex_unlock(&codec->hash_mutex);
1623 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001625EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Takashi Iwai280e57d2012-12-14 10:32:21 +01001627static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1628 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001629 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001630{
1631 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001632 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001633
1634 if (snd_BUG_ON(mask & ~0xff))
1635 mask &= 0xff;
1636 val &= mask;
1637
1638 mutex_lock(&codec->hash_mutex);
1639 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1640 if (!info) {
1641 mutex_unlock(&codec->hash_mutex);
1642 return 0;
1643 }
1644 val |= info->vol[ch] & ~mask;
1645 if (info->vol[ch] == val) {
1646 mutex_unlock(&codec->hash_mutex);
1647 return 0;
1648 }
1649 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001650 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001651 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001652 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001653 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001654 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001655 return 1;
1656}
1657
Takashi Iwaid5191e52009-11-16 14:58:17 +01001658/**
1659 * snd_hda_codec_amp_update - update the AMP value
1660 * @codec: HD-audio codec
1661 * @nid: NID to read the AMP value
1662 * @ch: channel (left=0 or right=1)
1663 * @direction: #HDA_INPUT or #HDA_OUTPUT
1664 * @idx: the index value (only for input direction)
1665 * @mask: bit mask to set
1666 * @val: the bits value to set
1667 *
1668 * Update the AMP value with a bit mask.
1669 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001670 */
Takashi Iwai834be882006-03-01 14:16:17 +01001671int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1672 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001674 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1675 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001677EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Takashi Iwaid5191e52009-11-16 14:58:17 +01001679/**
1680 * snd_hda_codec_amp_stereo - update the AMP stereo values
1681 * @codec: HD-audio codec
1682 * @nid: NID to read the AMP value
1683 * @direction: #HDA_INPUT or #HDA_OUTPUT
1684 * @idx: the index value (only for input direction)
1685 * @mask: bit mask to set
1686 * @val: the bits value to set
1687 *
1688 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1689 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001690 */
1691int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1692 int direction, int idx, int mask, int val)
1693{
1694 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001695
1696 if (snd_BUG_ON(mask & ~0xff))
1697 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001698 for (ch = 0; ch < 2; ch++)
1699 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1700 idx, mask, val);
1701 return ret;
1702}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001703EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001704
Takashi Iwai95a962c2014-10-29 16:03:58 +01001705/**
1706 * snd_hda_codec_amp_init - initialize the AMP value
1707 * @codec: the HDA codec
1708 * @nid: NID to read the AMP value
1709 * @ch: channel (left=0 or right=1)
1710 * @dir: #HDA_INPUT or #HDA_OUTPUT
1711 * @idx: the index value (only for input direction)
1712 * @mask: bit mask to set
1713 * @val: the bits value to set
1714 *
1715 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001716 * the first access. If the amp was already initialized / updated beforehand,
1717 * this does nothing.
1718 */
1719int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1720 int dir, int idx, int mask, int val)
1721{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001722 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1723 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001724}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001725EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001726
Takashi Iwai95a962c2014-10-29 16:03:58 +01001727/**
1728 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1729 * @codec: the HDA codec
1730 * @nid: NID to read the AMP value
1731 * @dir: #HDA_INPUT or #HDA_OUTPUT
1732 * @idx: the index value (only for input direction)
1733 * @mask: bit mask to set
1734 * @val: the bits value to set
1735 *
1736 * Call snd_hda_codec_amp_init() for both stereo channels.
1737 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001738int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1739 int dir, int idx, int mask, int val)
1740{
1741 int ch, ret = 0;
1742
1743 if (snd_BUG_ON(mask & ~0xff))
1744 mask &= 0xff;
1745 for (ch = 0; ch < 2; ch++)
1746 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1747 idx, mask, val);
1748 return ret;
1749}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001750EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001751
Takashi Iwaid5191e52009-11-16 14:58:17 +01001752/**
1753 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1754 * @codec: HD-audio codec
1755 *
1756 * Resume the all amp commands from the cache.
1757 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001758void snd_hda_codec_resume_amp(struct hda_codec *codec)
1759{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001760 int i;
1761
Takashi Iwaic370dd62012-12-13 18:30:04 +01001762 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001763 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001764 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1765 struct hda_amp_info *buffer;
1766 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001767 hda_nid_t nid;
1768 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001769 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001770
1771 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01001772 if (!buffer->head.dirty)
1773 continue;
1774 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001775 info = *buffer;
1776 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001777 if (!key)
1778 continue;
1779 nid = key & 0xff;
1780 idx = (key >> 16) & 0xff;
1781 dir = (key >> 24) & 0xff;
1782 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001783 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001784 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001785 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001786 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
1787 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001788 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001789 }
1790 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001791 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001792}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001793EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001795static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1796 unsigned int ofs)
1797{
1798 u32 caps = query_amp_caps(codec, nid, dir);
1799 /* get num steps */
1800 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1801 if (ofs < caps)
1802 caps -= ofs;
1803 return caps;
1804}
1805
Takashi Iwaid5191e52009-11-16 14:58:17 +01001806/**
1807 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001808 * @kcontrol: referred ctl element
1809 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001810 *
1811 * The control element is supposed to have the private_value field
1812 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1813 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001814int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1818 u16 nid = get_amp_nid(kcontrol);
1819 u8 chs = get_amp_channels(kcontrol);
1820 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001821 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001823 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1824 uinfo->count = chs == 3 ? 2 : 1;
1825 uinfo->value.integer.min = 0;
1826 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1827 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001828 codec_warn(codec,
1829 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1830 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -EINVAL;
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001835EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001837
1838static inline unsigned int
1839read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1840 int ch, int dir, int idx, unsigned int ofs)
1841{
1842 unsigned int val;
1843 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1844 val &= HDA_AMP_VOLMASK;
1845 if (val >= ofs)
1846 val -= ofs;
1847 else
1848 val = 0;
1849 return val;
1850}
1851
1852static inline int
1853update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1854 int ch, int dir, int idx, unsigned int ofs,
1855 unsigned int val)
1856{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001857 unsigned int maxval;
1858
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001859 if (val > 0)
1860 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001861 /* ofs = 0: raw max value */
1862 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001863 if (val > maxval)
1864 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001865 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
1866 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001867}
1868
Takashi Iwaid5191e52009-11-16 14:58:17 +01001869/**
1870 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001871 * @kcontrol: ctl element
1872 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001873 *
1874 * The control element is supposed to have the private_value field
1875 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1876 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001877int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 hda_nid_t nid = get_amp_nid(kcontrol);
1882 int chs = get_amp_channels(kcontrol);
1883 int dir = get_amp_direction(kcontrol);
1884 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001885 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 long *valp = ucontrol->value.integer.value;
1887
1888 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001889 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001891 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001894EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001898 * @kcontrol: ctl element
1899 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001900 *
1901 * The control element is supposed to have the private_value field
1902 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1903 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001904int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1908 hda_nid_t nid = get_amp_nid(kcontrol);
1909 int chs = get_amp_channels(kcontrol);
1910 int dir = get_amp_direction(kcontrol);
1911 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001912 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 long *valp = ucontrol->value.integer.value;
1914 int change = 0;
1915
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001916 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001917 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001918 valp++;
1919 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001920 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001921 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return change;
1923}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001924EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Takashi Iwaid5191e52009-11-16 14:58:17 +01001926/**
1927 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001928 * @kcontrol: ctl element
1929 * @op_flag: operation flag
1930 * @size: byte size of input TLV
1931 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001932 *
1933 * The control element is supposed to have the private_value field
1934 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1935 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001936int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1937 unsigned int size, unsigned int __user *_tlv)
1938{
1939 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1940 hda_nid_t nid = get_amp_nid(kcontrol);
1941 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001942 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001943 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001944 u32 caps, val1, val2;
1945
1946 if (size < 4 * sizeof(unsigned int))
1947 return -ENOMEM;
1948 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001949 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1950 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001951 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001952 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001953 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001954 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001955 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001956 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1957 return -EFAULT;
1958 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1959 return -EFAULT;
1960 if (put_user(val1, _tlv + 2))
1961 return -EFAULT;
1962 if (put_user(val2, _tlv + 3))
1963 return -EFAULT;
1964 return 0;
1965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001966EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001967
Takashi Iwaid5191e52009-11-16 14:58:17 +01001968/**
1969 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1970 * @codec: HD-audio codec
1971 * @nid: NID of a reference widget
1972 * @dir: #HDA_INPUT or #HDA_OUTPUT
1973 * @tlv: TLV data to be stored, at least 4 elements
1974 *
1975 * Set (static) TLV data for a virtual master volume using the AMP caps
1976 * obtained from the reference NID.
1977 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001978 */
1979void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1980 unsigned int *tlv)
1981{
1982 u32 caps;
1983 int nums, step;
1984
1985 caps = query_amp_caps(codec, nid, dir);
1986 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1987 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1988 step = (step + 1) * 25;
1989 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1990 tlv[1] = 2 * sizeof(unsigned int);
1991 tlv[2] = -nums * step;
1992 tlv[3] = step;
1993}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001994EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001995
1996/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001997static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001998find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001999{
2000 struct snd_ctl_elem_id id;
2001 memset(&id, 0, sizeof(id));
2002 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002003 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002004 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002005 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2006 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002007 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002008 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002009}
2010
Takashi Iwaid5191e52009-11-16 14:58:17 +01002011/**
2012 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2013 * @codec: HD-audio codec
2014 * @name: ctl id name string
2015 *
2016 * Get the control element with the given id string and IFACE_MIXER.
2017 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002018struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2019 const char *name)
2020{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002021 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002022}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002023EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002024
Takashi Iwaidcda5802012-10-12 17:24:51 +02002025static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002026 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002027{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002028 int i, idx;
2029 /* 16 ctlrs should be large enough */
2030 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2031 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002032 return idx;
2033 }
2034 return -EBUSY;
2035}
2036
Takashi Iwaid5191e52009-11-16 14:58:17 +01002037/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002038 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002039 * @codec: HD-audio codec
2040 * @nid: corresponding NID (optional)
2041 * @kctl: the control element to assign
2042 *
2043 * Add the given control element to an array inside the codec instance.
2044 * All control elements belonging to a codec are supposed to be added
2045 * by this function so that a proper clean-up works at the free or
2046 * reconfiguration time.
2047 *
2048 * If non-zero @nid is passed, the NID is assigned to the control element.
2049 * The assignment is shown in the codec proc file.
2050 *
2051 * snd_hda_ctl_add() checks the control subdev id field whether
2052 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002053 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2054 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002055 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002056int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2057 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002058{
2059 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002060 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002061 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002062
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002063 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002064 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002065 if (nid == 0)
2066 nid = get_amp_nid_(kctl->private_value);
2067 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002068 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2069 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002070 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002071 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002072 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002073 if (err < 0)
2074 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002075 item = snd_array_new(&codec->mixers);
2076 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002077 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002078 item->kctl = kctl;
2079 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002080 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002081 return 0;
2082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002083EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002084
Takashi Iwaid5191e52009-11-16 14:58:17 +01002085/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002086 * snd_hda_add_nid - Assign a NID to a control element
2087 * @codec: HD-audio codec
2088 * @nid: corresponding NID (optional)
2089 * @kctl: the control element to assign
2090 * @index: index to kctl
2091 *
2092 * Add the given control element to an array inside the codec instance.
2093 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2094 * NID:KCTL mapping - for example "Capture Source" selector.
2095 */
2096int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2097 unsigned int index, hda_nid_t nid)
2098{
2099 struct hda_nid_item *item;
2100
2101 if (nid > 0) {
2102 item = snd_array_new(&codec->nids);
2103 if (!item)
2104 return -ENOMEM;
2105 item->kctl = kctl;
2106 item->index = index;
2107 item->nid = nid;
2108 return 0;
2109 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002110 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2111 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002112 return -EINVAL;
2113}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002114EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002115
2116/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2118 * @codec: HD-audio codec
2119 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002120void snd_hda_ctls_clear(struct hda_codec *codec)
2121{
2122 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002123 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002124 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002125 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002126 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002127 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002128}
2129
Takashi Iwai95a962c2014-10-29 16:03:58 +01002130/**
2131 * snd_hda_lock_devices - pseudo device locking
2132 * @bus: the BUS
2133 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002134 * toggle card->shutdown to allow/disallow the device access (as a hack)
2135 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002136int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002137{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002138 struct snd_card *card = bus->card;
2139 struct hda_codec *codec;
2140
Takashi Iwaia65d6292009-02-23 16:57:04 +01002141 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002142 if (card->shutdown)
2143 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002144 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002145 if (!list_empty(&card->ctl_files))
2146 goto err_clear;
2147
Takashi Iwaid068ebc2015-03-02 23:22:59 +01002148 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002149 struct hda_pcm *cpcm;
2150 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002151 if (!cpcm->pcm)
2152 continue;
2153 if (cpcm->pcm->streams[0].substream_opened ||
2154 cpcm->pcm->streams[1].substream_opened)
2155 goto err_clear;
2156 }
2157 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002158 spin_unlock(&card->files_lock);
2159 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002160
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002161 err_clear:
2162 card->shutdown = 0;
2163 err_unlock:
2164 spin_unlock(&card->files_lock);
2165 return -EINVAL;
2166}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002167EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002168
Takashi Iwai95a962c2014-10-29 16:03:58 +01002169/**
2170 * snd_hda_unlock_devices - pseudo device unlocking
2171 * @bus: the BUS
2172 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002173void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002174{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002175 struct snd_card *card = bus->card;
2176
Takashi Iwaia65d6292009-02-23 16:57:04 +01002177 spin_lock(&card->files_lock);
2178 card->shutdown = 0;
2179 spin_unlock(&card->files_lock);
2180}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002181EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002182
Takashi Iwaid5191e52009-11-16 14:58:17 +01002183/**
2184 * snd_hda_codec_reset - Clear all objects assigned to the codec
2185 * @codec: HD-audio codec
2186 *
2187 * This frees the all PCM and control elements assigned to the codec, and
2188 * clears the caches and restores the pin default configurations.
2189 *
2190 * When a device is being used, it returns -EBSY. If successfully freed,
2191 * returns zero.
2192 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002193int snd_hda_codec_reset(struct hda_codec *codec)
2194{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002195 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002196
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002197 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002198 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002199
2200 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01002201 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002202
Takashi Iwaia65d6292009-02-23 16:57:04 +01002203 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002204 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002205 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002206}
2207
Takashi Iwai6194b992014-06-06 18:12:16 +02002208typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002209
2210/* apply the function to all matching slave ctls in the mixer list */
2211static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002212 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002213{
2214 struct hda_nid_item *items;
2215 const char * const *s;
2216 int i, err;
2217
2218 items = codec->mixers.list;
2219 for (i = 0; i < codec->mixers.used; i++) {
2220 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002221 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002222 continue;
2223 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002224 char tmpname[sizeof(sctl->id.name)];
2225 const char *name = *s;
2226 if (suffix) {
2227 snprintf(tmpname, sizeof(tmpname), "%s %s",
2228 name, suffix);
2229 name = tmpname;
2230 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002231 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002232 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002233 if (err)
2234 return err;
2235 break;
2236 }
2237 }
2238 }
2239 return 0;
2240}
2241
Takashi Iwai6194b992014-06-06 18:12:16 +02002242static int check_slave_present(struct hda_codec *codec,
2243 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002244{
2245 return 1;
2246}
2247
Takashi Iwai18478e82012-03-09 17:51:10 +01002248/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002249static int get_kctl_0dB_offset(struct hda_codec *codec,
2250 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002251{
2252 int _tlv[4];
2253 const int *tlv = NULL;
2254 int val = -1;
2255
2256 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2257 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2258 mm_segment_t fs = get_fs();
2259 set_fs(get_ds());
2260 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2261 tlv = _tlv;
2262 set_fs(fs);
2263 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2264 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002265 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2266 int step = tlv[3];
2267 step &= ~TLV_DB_SCALE_MUTE;
2268 if (!step)
2269 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002270 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002271 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002272- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002273 return -1;
2274 }
2275 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002276 val = -tlv[2] / step;
2277 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002278 return val;
2279}
2280
2281/* call kctl->put with the given value(s) */
2282static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2283{
2284 struct snd_ctl_elem_value *ucontrol;
2285 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2286 if (!ucontrol)
2287 return -ENOMEM;
2288 ucontrol->value.integer.value[0] = val;
2289 ucontrol->value.integer.value[1] = val;
2290 kctl->put(kctl, ucontrol);
2291 kfree(ucontrol);
2292 return 0;
2293}
2294
2295/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002296static int init_slave_0dB(struct hda_codec *codec,
2297 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002298{
Takashi Iwai6194b992014-06-06 18:12:16 +02002299 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002300 if (offset > 0)
2301 put_kctl_with_value(slave, offset);
2302 return 0;
2303}
2304
2305/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002306static int init_slave_unmute(struct hda_codec *codec,
2307 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002308{
2309 return put_kctl_with_value(slave, 1);
2310}
2311
Takashi Iwaie8750942014-06-30 14:02:39 +02002312static int add_slave(struct hda_codec *codec,
2313 void *data, struct snd_kcontrol *slave)
2314{
2315 return snd_ctl_add_slave(data, slave);
2316}
2317
Takashi Iwaid5191e52009-11-16 14:58:17 +01002318/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002319 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002320 * @codec: HD-audio codec
2321 * @name: vmaster control name
2322 * @tlv: TLV data (optional)
2323 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002324 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002325 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002326 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327 *
2328 * Create a virtual master control with the given name. The TLV data
2329 * must be either NULL or a valid data.
2330 *
2331 * @slaves is a NULL-terminated array of strings, each of which is a
2332 * slave control name. All controls with these names are assigned to
2333 * the new virtual master control.
2334 *
2335 * This function returns zero if successful or a negative error code.
2336 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002337int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002338 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002339 const char *suffix, bool init_slave_vol,
2340 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002341{
2342 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002343 int err;
2344
Takashi Iwai29e58532012-03-12 12:25:03 +01002345 if (ctl_ret)
2346 *ctl_ret = NULL;
2347
Takashi Iwai9322ca52012-02-03 14:28:01 +01002348 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002349 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002350 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002351 return 0;
2352 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002353 kctl = snd_ctl_make_virtual_master(name, tlv);
2354 if (!kctl)
2355 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002356 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002357 if (err < 0)
2358 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002359
Takashi Iwaie8750942014-06-30 14:02:39 +02002360 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002361 if (err < 0)
2362 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002363
2364 /* init with master mute & zero volume */
2365 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002366 if (init_slave_vol) {
2367 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002368 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002369 tlv ? init_slave_0dB : init_slave_unmute, &step);
2370 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002371
Takashi Iwai29e58532012-03-12 12:25:03 +01002372 if (ctl_ret)
2373 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002374 return 0;
2375}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002376EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002377
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002378/*
2379 * mute-LED control using vmaster
2380 */
2381static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_info *uinfo)
2383{
2384 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002385 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002386 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002387
Takashi Iwai3ff72212014-10-20 18:17:28 +02002388 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002389}
2390
2391static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
2394 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2395 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2396 return 0;
2397}
2398
2399static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
2401{
2402 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2403 unsigned int old_mode = hook->mute_mode;
2404
2405 hook->mute_mode = ucontrol->value.enumerated.item[0];
2406 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2407 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2408 if (old_mode == hook->mute_mode)
2409 return 0;
2410 snd_hda_sync_vmaster_hook(hook);
2411 return 1;
2412}
2413
2414static struct snd_kcontrol_new vmaster_mute_mode = {
2415 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2416 .name = "Mute-LED Mode",
2417 .info = vmaster_mute_mode_info,
2418 .get = vmaster_mute_mode_get,
2419 .put = vmaster_mute_mode_put,
2420};
2421
Takashi Iwai95a962c2014-10-29 16:03:58 +01002422/**
2423 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2424 * @codec: the HDA codec
2425 * @hook: the vmaster hook object
2426 * @expose_enum_ctl: flag to create an enum ctl
2427 *
2428 * Add a mute-LED hook with the given vmaster switch kctl.
2429 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2430 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002431 */
2432int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002433 struct hda_vmaster_mute_hook *hook,
2434 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002435{
2436 struct snd_kcontrol *kctl;
2437
2438 if (!hook->hook || !hook->sw_kctl)
2439 return 0;
2440 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2441 hook->codec = codec;
2442 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002443 if (!expose_enum_ctl)
2444 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002445 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2446 if (!kctl)
2447 return -ENOMEM;
2448 return snd_hda_ctl_add(codec, 0, kctl);
2449}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002450EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002451
Takashi Iwai95a962c2014-10-29 16:03:58 +01002452/**
2453 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2454 * @hook: the vmaster hook
2455 *
2456 * Call the hook with the current value for synchronization.
2457 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002458 */
2459void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2460{
2461 if (!hook->hook || !hook->codec)
2462 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002463 /* don't call vmaster hook in the destructor since it might have
2464 * been already destroyed
2465 */
2466 if (hook->codec->bus->shutdown)
2467 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002468 switch (hook->mute_mode) {
2469 case HDA_VMUTE_FOLLOW_MASTER:
2470 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2471 break;
2472 default:
2473 hook->hook(hook->codec, hook->mute_mode);
2474 break;
2475 }
2476}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002477EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002478
2479
Takashi Iwaid5191e52009-11-16 14:58:17 +01002480/**
2481 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002482 * @kcontrol: referred ctl element
2483 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002484 *
2485 * The control element is supposed to have the private_value field
2486 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2487 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002488int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 int chs = get_amp_channels(kcontrol);
2492
2493 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2494 uinfo->count = chs == 3 ? 2 : 1;
2495 uinfo->value.integer.min = 0;
2496 uinfo->value.integer.max = 1;
2497 return 0;
2498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002499EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Takashi Iwaid5191e52009-11-16 14:58:17 +01002501/**
2502 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002503 * @kcontrol: ctl element
2504 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002505 *
2506 * The control element is supposed to have the private_value field
2507 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2508 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002509int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2510 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2513 hda_nid_t nid = get_amp_nid(kcontrol);
2514 int chs = get_amp_channels(kcontrol);
2515 int dir = get_amp_direction(kcontrol);
2516 int idx = get_amp_index(kcontrol);
2517 long *valp = ucontrol->value.integer.value;
2518
2519 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002520 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002521 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002523 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002524 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return 0;
2526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002527EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Takashi Iwaid5191e52009-11-16 14:58:17 +01002529/**
2530 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002531 * @kcontrol: ctl element
2532 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002533 *
2534 * The control element is supposed to have the private_value field
2535 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2536 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002537int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2538 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2541 hda_nid_t nid = get_amp_nid(kcontrol);
2542 int chs = get_amp_channels(kcontrol);
2543 int dir = get_amp_direction(kcontrol);
2544 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 long *valp = ucontrol->value.integer.value;
2546 int change = 0;
2547
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002548 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002549 change = codec_amp_update(codec, nid, 0, dir, idx,
2550 HDA_AMP_MUTE,
2551 *valp ? 0 : HDA_AMP_MUTE, false,
2552 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002553 valp++;
2554 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002555 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002556 change |= codec_amp_update(codec, nid, 1, dir, idx,
2557 HDA_AMP_MUTE,
2558 *valp ? 0 : HDA_AMP_MUTE, false,
2559 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002560 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return change;
2562}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002563EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565/*
Takashi Iwai985be542005-11-02 18:26:49 +01002566 * bound volume controls
2567 *
2568 * bind multiple volumes (# indices, from 0)
2569 */
2570
2571#define AMP_VAL_IDX_SHIFT 19
2572#define AMP_VAL_IDX_MASK (0x0f<<19)
2573
Takashi Iwaid5191e52009-11-16 14:58:17 +01002574/**
2575 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002576 * @kcontrol: ctl element
2577 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002578 *
2579 * The control element is supposed to have the private_value field
2580 * set up via HDA_BIND_MUTE*() macros.
2581 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002582int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002584{
2585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2586 unsigned long pval;
2587 int err;
2588
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002589 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002590 pval = kcontrol->private_value;
2591 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2592 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2593 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002594 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002595 return err;
2596}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002597EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002598
Takashi Iwaid5191e52009-11-16 14:58:17 +01002599/**
2600 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002601 * @kcontrol: ctl element
2602 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002603 *
2604 * The control element is supposed to have the private_value field
2605 * set up via HDA_BIND_MUTE*() macros.
2606 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002607int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2608 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002609{
2610 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2611 unsigned long pval;
2612 int i, indices, err = 0, change = 0;
2613
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002614 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002615 pval = kcontrol->private_value;
2616 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2617 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002618 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2619 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002620 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2621 if (err < 0)
2622 break;
2623 change |= err;
2624 }
2625 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002626 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002627 return err < 0 ? err : change;
2628}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002629EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002630
Takashi Iwaid5191e52009-11-16 14:58:17 +01002631/**
2632 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002633 * @kcontrol: referred ctl element
2634 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002635 *
2636 * The control element is supposed to have the private_value field
2637 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002638 */
2639int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2640 struct snd_ctl_elem_info *uinfo)
2641{
2642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2643 struct hda_bind_ctls *c;
2644 int err;
2645
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002646 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002647 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002648 kcontrol->private_value = *c->values;
2649 err = c->ops->info(kcontrol, uinfo);
2650 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002651 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002652 return err;
2653}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002654EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002655
Takashi Iwaid5191e52009-11-16 14:58:17 +01002656/**
2657 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002658 * @kcontrol: ctl element
2659 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002660 *
2661 * The control element is supposed to have the private_value field
2662 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2663 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002664int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_value *ucontrol)
2666{
2667 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2668 struct hda_bind_ctls *c;
2669 int err;
2670
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002671 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002672 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002673 kcontrol->private_value = *c->values;
2674 err = c->ops->get(kcontrol, ucontrol);
2675 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002676 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002677 return err;
2678}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002679EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002680
Takashi Iwaid5191e52009-11-16 14:58:17 +01002681/**
2682 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002683 * @kcontrol: ctl element
2684 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002685 *
2686 * The control element is supposed to have the private_value field
2687 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2688 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002689int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_value *ucontrol)
2691{
2692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2693 struct hda_bind_ctls *c;
2694 unsigned long *vals;
2695 int err = 0, change = 0;
2696
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002697 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002698 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002699 for (vals = c->values; *vals; vals++) {
2700 kcontrol->private_value = *vals;
2701 err = c->ops->put(kcontrol, ucontrol);
2702 if (err < 0)
2703 break;
2704 change |= err;
2705 }
2706 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002707 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002708 return err < 0 ? err : change;
2709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002710EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002711
Takashi Iwaid5191e52009-11-16 14:58:17 +01002712/**
2713 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002714 * @kcontrol: ctl element
2715 * @op_flag: operation flag
2716 * @size: byte size of input TLV
2717 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002718 *
2719 * The control element is supposed to have the private_value field
2720 * set up via HDA_BIND_VOL() macro.
2721 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002722int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2723 unsigned int size, unsigned int __user *tlv)
2724{
2725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2726 struct hda_bind_ctls *c;
2727 int err;
2728
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002729 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002730 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002731 kcontrol->private_value = *c->values;
2732 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2733 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002734 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002735 return err;
2736}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002737EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002738
2739struct hda_ctl_ops snd_hda_bind_vol = {
2740 .info = snd_hda_mixer_amp_volume_info,
2741 .get = snd_hda_mixer_amp_volume_get,
2742 .put = snd_hda_mixer_amp_volume_put,
2743 .tlv = snd_hda_mixer_amp_tlv
2744};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002745EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002746
2747struct hda_ctl_ops snd_hda_bind_sw = {
2748 .info = snd_hda_mixer_amp_switch_info,
2749 .get = snd_hda_mixer_amp_switch_get,
2750 .put = snd_hda_mixer_amp_switch_put,
2751 .tlv = snd_hda_mixer_amp_tlv
2752};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002753EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002754
2755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 * SPDIF out controls
2757 */
2758
Takashi Iwai0ba21762007-04-16 11:29:14 +02002759static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2763 uinfo->count = 1;
2764 return 0;
2765}
2766
Takashi Iwai0ba21762007-04-16 11:29:14 +02002767static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2771 IEC958_AES0_NONAUDIO |
2772 IEC958_AES0_CON_EMPHASIS_5015 |
2773 IEC958_AES0_CON_NOT_COPYRIGHT;
2774 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2775 IEC958_AES1_CON_ORIGINAL;
2776 return 0;
2777}
2778
Takashi Iwai0ba21762007-04-16 11:29:14 +02002779static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2783 IEC958_AES0_NONAUDIO |
2784 IEC958_AES0_PRO_EMPHASIS_5015;
2785 return 0;
2786}
2787
Takashi Iwai0ba21762007-04-16 11:29:14 +02002788static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002792 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002793 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002795 mutex_lock(&codec->spdif_mutex);
2796 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002797 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2798 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2799 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2800 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002801 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 return 0;
2804}
2805
2806/* convert from SPDIF status bits to HDA SPDIF bits
2807 * bit 0 (DigEn) is always set zero (to be filled later)
2808 */
2809static unsigned short convert_from_spdif_status(unsigned int sbits)
2810{
2811 unsigned short val = 0;
2812
2813 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002814 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002816 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002818 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2819 IEC958_AES0_PRO_EMPHASIS_5015)
2820 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002822 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2823 IEC958_AES0_CON_EMPHASIS_5015)
2824 val |= AC_DIG1_EMPHASIS;
2825 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2826 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002828 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2830 }
2831 return val;
2832}
2833
2834/* convert to SPDIF status bits from HDA SPDIF bits
2835 */
2836static unsigned int convert_to_spdif_status(unsigned short val)
2837{
2838 unsigned int sbits = 0;
2839
Takashi Iwai0ba21762007-04-16 11:29:14 +02002840 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002842 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 sbits |= IEC958_AES0_PROFESSIONAL;
2844 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002845 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2847 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002848 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002852 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2854 sbits |= val & (0x7f << 8);
2855 }
2856 return sbits;
2857}
2858
Takashi Iwai2f728532008-09-25 16:32:41 +02002859/* set digital convert verbs both for the given NID and its slaves */
2860static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2861 int verb, int val)
2862{
Takashi Iwaidda14412011-05-02 11:29:30 +02002863 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002864
Takashi Iwai9e976972008-11-25 08:17:20 +01002865 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002866 d = codec->slave_dig_outs;
2867 if (!d)
2868 return;
2869 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002870 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002871}
2872
2873static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2874 int dig1, int dig2)
2875{
2876 if (dig1 != -1)
2877 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2878 if (dig2 != -1)
2879 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2880}
2881
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002886 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002887 struct hda_spdif_out *spdif;
2888 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 unsigned short val;
2890 int change;
2891
Ingo Molnar62932df2006-01-16 16:34:20 +01002892 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002893 spdif = snd_array_elem(&codec->spdif_out, idx);
2894 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002895 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2897 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2898 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002899 val = convert_from_spdif_status(spdif->status);
2900 val |= spdif->ctls & 1;
2901 change = spdif->ctls != val;
2902 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002903 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002904 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002905 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return change;
2907}
2908
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002909#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002915 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002916 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002918 mutex_lock(&codec->spdif_mutex);
2919 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002920 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002921 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return 0;
2923}
2924
Stephen Warren74b654c2011-06-01 11:14:18 -06002925static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2926 int dig1, int dig2)
2927{
2928 set_dig_out_convert(codec, nid, dig1, dig2);
2929 /* unmute amp switch (if any) */
2930 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2931 (dig1 & AC_DIG1_ENABLE))
2932 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2933 HDA_AMP_MUTE, 0);
2934}
2935
Takashi Iwai0ba21762007-04-16 11:29:14 +02002936static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002940 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002941 struct hda_spdif_out *spdif;
2942 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 unsigned short val;
2944 int change;
2945
Ingo Molnar62932df2006-01-16 16:34:20 +01002946 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002947 spdif = snd_array_elem(&codec->spdif_out, idx);
2948 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002949 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002952 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002953 spdif->ctls = val;
2954 if (change && nid != (u16)-1)
2955 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002956 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return change;
2958}
2959
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002960static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 {
2962 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002964 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 .info = snd_hda_spdif_mask_info,
2966 .get = snd_hda_spdif_cmask_get,
2967 },
2968 {
2969 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002971 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 .info = snd_hda_spdif_mask_info,
2973 .get = snd_hda_spdif_pmask_get,
2974 },
2975 {
2976 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002977 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .info = snd_hda_spdif_mask_info,
2979 .get = snd_hda_spdif_default_get,
2980 .put = snd_hda_spdif_default_put,
2981 },
2982 {
2983 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002984 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .info = snd_hda_spdif_out_switch_info,
2986 .get = snd_hda_spdif_out_switch_get,
2987 .put = snd_hda_spdif_out_switch_put,
2988 },
2989 { } /* end */
2990};
2991
2992/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002993 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002995 * @associated_nid: NID that new ctls associated with
2996 * @cvt_nid: converter NID
2997 * @type: HDA_PCM_TYPE_*
2998 * Creates controls related with the digital output.
2999 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 *
3001 * Returns 0 if successful, or a negative error code.
3002 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003003int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3004 hda_nid_t associated_nid,
3005 hda_nid_t cvt_nid,
3006 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003009 struct snd_kcontrol *kctl;
3010 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003011 int idx = 0;
3012 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003013 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003014 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003016 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003017 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003018 idx = spdif_index;
3019 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003020 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003021 /* suppose a single SPDIF device */
3022 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3023 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3024 if (!kctl)
3025 break;
3026 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003027 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003028 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003029 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003030 if (!bus->primary_dig_out_type)
3031 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003032
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003033 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003034 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003035 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003036 return -EBUSY;
3037 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003038 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003039 if (!spdif)
3040 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3042 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003043 if (!kctl)
3044 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003045 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003046 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003047 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003048 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return err;
3050 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003051 spdif->nid = cvt_nid;
3052 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003053 AC_VERB_GET_DIGI_CONVERT_1, 0);
3054 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return 0;
3056}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003057EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Takashi Iwai95a962c2014-10-29 16:03:58 +01003059/**
3060 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3061 * @codec: the HDA codec
3062 * @nid: widget NID
3063 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 * call within spdif_mutex lock
3065 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003066struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3067 hda_nid_t nid)
3068{
3069 int i;
3070 for (i = 0; i < codec->spdif_out.used; i++) {
3071 struct hda_spdif_out *spdif =
3072 snd_array_elem(&codec->spdif_out, i);
3073 if (spdif->nid == nid)
3074 return spdif;
3075 }
3076 return NULL;
3077}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003078EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003079
Takashi Iwai95a962c2014-10-29 16:03:58 +01003080/**
3081 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3082 * @codec: the HDA codec
3083 * @idx: the SPDIF ctl index
3084 *
3085 * Unassign the widget from the given SPDIF control.
3086 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003087void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3088{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003089 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003090
3091 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003092 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003093 spdif->nid = (u16)-1;
3094 mutex_unlock(&codec->spdif_mutex);
3095}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003096EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003097
Takashi Iwai95a962c2014-10-29 16:03:58 +01003098/**
3099 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3100 * @codec: the HDA codec
3101 * @idx: the SPDIF ctl idx
3102 * @nid: widget NID
3103 *
3104 * Assign the widget to the SPDIF control with the given index.
3105 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003106void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3107{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003108 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003109 unsigned short val;
3110
3111 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003112 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003113 if (spdif->nid != nid) {
3114 spdif->nid = nid;
3115 val = spdif->ctls;
3116 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3117 }
3118 mutex_unlock(&codec->spdif_mutex);
3119}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003120EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003123 * SPDIF sharing with analog output
3124 */
3125static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
3127{
3128 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3129 ucontrol->value.integer.value[0] = mout->share_spdif;
3130 return 0;
3131}
3132
3133static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol)
3135{
3136 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3137 mout->share_spdif = !!ucontrol->value.integer.value[0];
3138 return 0;
3139}
3140
3141static struct snd_kcontrol_new spdif_share_sw = {
3142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3143 .name = "IEC958 Default PCM Playback Switch",
3144 .info = snd_ctl_boolean_mono_info,
3145 .get = spdif_share_sw_get,
3146 .put = spdif_share_sw_put,
3147};
3148
Takashi Iwaid5191e52009-11-16 14:58:17 +01003149/**
3150 * snd_hda_create_spdif_share_sw - create Default PCM switch
3151 * @codec: the HDA codec
3152 * @mout: multi-out instance
3153 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003154int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3155 struct hda_multi_out *mout)
3156{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003157 struct snd_kcontrol *kctl;
3158
Takashi Iwai9a081602008-02-12 18:37:26 +01003159 if (!mout->dig_out_nid)
3160 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003161
3162 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3163 if (!kctl)
3164 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003165 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003166 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003167}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003168EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003169
3170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * SPDIF input
3172 */
3173
3174#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3175
Takashi Iwai0ba21762007-04-16 11:29:14 +02003176static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3180
3181 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3182 return 0;
3183}
3184
Takashi Iwai0ba21762007-04-16 11:29:14 +02003185static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3189 hda_nid_t nid = kcontrol->private_value;
3190 unsigned int val = !!ucontrol->value.integer.value[0];
3191 int change;
3192
Ingo Molnar62932df2006-01-16 16:34:20 +01003193 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003195 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003197 snd_hda_codec_write_cache(codec, nid, 0,
3198 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003200 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return change;
3202}
3203
Takashi Iwai0ba21762007-04-16 11:29:14 +02003204static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3208 hda_nid_t nid = kcontrol->private_value;
3209 unsigned short val;
3210 unsigned int sbits;
3211
Andrew Paprocki3982d172007-12-19 12:13:44 +01003212 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 sbits = convert_to_spdif_status(val);
3214 ucontrol->value.iec958.status[0] = sbits;
3215 ucontrol->value.iec958.status[1] = sbits >> 8;
3216 ucontrol->value.iec958.status[2] = sbits >> 16;
3217 ucontrol->value.iec958.status[3] = sbits >> 24;
3218 return 0;
3219}
3220
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003221static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 {
3223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003224 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 .info = snd_hda_spdif_in_switch_info,
3226 .get = snd_hda_spdif_in_switch_get,
3227 .put = snd_hda_spdif_in_switch_put,
3228 },
3229 {
3230 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003232 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 .info = snd_hda_spdif_mask_info,
3234 .get = snd_hda_spdif_in_status_get,
3235 },
3236 { } /* end */
3237};
3238
3239/**
3240 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3241 * @codec: the HDA codec
3242 * @nid: audio in widget NID
3243 *
3244 * Creates controls related with the SPDIF input.
3245 * Called from each patch supporting the SPDIF in.
3246 *
3247 * Returns 0 if successful, or a negative error code.
3248 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003249int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003252 struct snd_kcontrol *kctl;
3253 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003254 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Takashi Iwaidcda5802012-10-12 17:24:51 +02003256 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003257 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003258 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003259 return -EBUSY;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3262 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003263 if (!kctl)
3264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003266 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003267 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return err;
3269 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003270 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003271 snd_hda_codec_read(codec, nid, 0,
3272 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003273 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return 0;
3275}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003276EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003278/*
3279 * command cache
3280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Takashi Iwaic370dd62012-12-13 18:30:04 +01003282/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003283#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3284#define get_cmd_cache_nid(key) ((key) & 0xff)
3285#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3286
3287/**
3288 * snd_hda_codec_write_cache - send a single command with caching
3289 * @codec: the HDA codec
3290 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003291 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003292 * @verb: the verb to send
3293 * @parm: the parameter for the verb
3294 *
3295 * Send a single command without waiting for response.
3296 *
3297 * Returns 0 if successful, or a negative error code.
3298 */
3299int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003300 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003301{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003302 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003303 struct hda_cache_head *c;
3304 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003305 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003306
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003307 cache_only = codec->cached_write;
3308 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003309 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003310 if (err < 0)
3311 return err;
3312 }
3313
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003314 /* parm may contain the verb stuff for get/set amp */
3315 verb = verb | (parm >> 8);
3316 parm &= 0xff;
3317 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003318 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003319 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003320 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003321 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003322 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003323 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003324 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003325 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003326}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003327EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003328
Takashi Iwaid5191e52009-11-16 14:58:17 +01003329/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003330 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3331 * @codec: the HDA codec
3332 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003333 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003334 * @verb: the verb to send
3335 * @parm: the parameter for the verb
3336 *
3337 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3338 * command if the parameter is already identical with the cached value.
3339 * If not, it sends the command and refreshes the cache.
3340 *
3341 * Returns 0 if successful, or a negative error code.
3342 */
3343int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003344 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003345{
3346 struct hda_cache_head *c;
3347 u32 key;
3348
3349 /* parm may contain the verb stuff for get/set amp */
3350 verb = verb | (parm >> 8);
3351 parm &= 0xff;
3352 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003353 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003354 c = get_hash(&codec->cmd_cache, key);
3355 if (c && c->val == parm) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003356 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003357 return 0;
3358 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003359 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003360 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003361}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003362EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003363
3364/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003365 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3366 * @codec: HD-audio codec
3367 *
3368 * Execute all verbs recorded in the command caches to resume.
3369 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003370void snd_hda_codec_resume_cache(struct hda_codec *codec)
3371{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003372 int i;
3373
Takashi Iwaic370dd62012-12-13 18:30:04 +01003374 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003375 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003376 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3377 struct hda_cache_head *buffer;
3378 u32 key;
3379
3380 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3381 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003382 if (!key)
3383 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003384 if (!buffer->dirty)
3385 continue;
3386 buffer->dirty = 0;
3387 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003388 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3389 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003390 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003391 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003392 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003393}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003394EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003395
3396/**
3397 * snd_hda_sequence_write_cache - sequence writes with caching
3398 * @codec: the HDA codec
3399 * @seq: VERB array to send
3400 *
3401 * Send the commands sequentially from the given array.
3402 * Thte commands are recorded on cache for power-save and resume.
3403 * The array must be terminated with NID=0.
3404 */
3405void snd_hda_sequence_write_cache(struct hda_codec *codec,
3406 const struct hda_verb *seq)
3407{
3408 for (; seq->nid; seq++)
3409 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3410 seq->param);
3411}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003412EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003413
Takashi Iwaidc870f32013-01-22 15:24:30 +01003414/**
3415 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3416 * @codec: HD-audio codec
3417 */
3418void snd_hda_codec_flush_cache(struct hda_codec *codec)
3419{
3420 snd_hda_codec_resume_amp(codec);
3421 snd_hda_codec_resume_cache(codec);
3422}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003423EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003424
Takashi Iwai95a962c2014-10-29 16:03:58 +01003425/**
3426 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3427 * @codec: the HDA codec
3428 * @fg: function group (not used now)
3429 * @power_state: the power state to set (AC_PWRST_*)
3430 *
3431 * Set the given power state to all widgets that have the power control.
3432 * If the codec has power_filter set, it evaluates the power state and
3433 * filter out if it's unchanged as D3.
3434 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003435void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003436 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003437{
Takashi Iwai7639a062015-03-03 10:07:24 +01003438 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01003439
Takashi Iwai7639a062015-03-03 10:07:24 +01003440 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003441 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003442 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003443 if (!(wcaps & AC_WCAP_POWER))
3444 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003445 if (codec->power_filter) {
3446 state = codec->power_filter(codec, nid, power_state);
3447 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003448 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003449 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003450 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003451 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003452 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003454EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003455
3456/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003457 * wait until the state is reached, returns the current state
3458 */
3459static unsigned int hda_sync_power_state(struct hda_codec *codec,
3460 hda_nid_t fg,
3461 unsigned int power_state)
3462{
3463 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3464 unsigned int state, actual_state;
3465
3466 for (;;) {
3467 state = snd_hda_codec_read(codec, fg, 0,
3468 AC_VERB_GET_POWER_STATE, 0);
3469 if (state & AC_PWRST_ERROR)
3470 break;
3471 actual_state = (state >> 4) & 0x0f;
3472 if (actual_state == power_state)
3473 break;
3474 if (time_after_eq(jiffies, end_time))
3475 break;
3476 /* wait until the codec reachs to the target state */
3477 msleep(1);
3478 }
3479 return state;
3480}
3481
Takashi Iwai95a962c2014-10-29 16:03:58 +01003482/**
3483 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3484 * @codec: the HDA codec
3485 * @nid: widget NID
3486 * @power_state: power state to evalue
3487 *
3488 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3489 * This can be used a codec power_filter callback.
3490 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003491unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3492 hda_nid_t nid,
3493 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003494{
Takashi Iwai7639a062015-03-03 10:07:24 +01003495 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003496 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003497 if (power_state == AC_PWRST_D3 &&
3498 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3499 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3500 int eapd = snd_hda_codec_read(codec, nid, 0,
3501 AC_VERB_GET_EAPD_BTLENABLE, 0);
3502 if (eapd & 0x02)
3503 return AC_PWRST_D0;
3504 }
3505 return power_state;
3506}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003507EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003508
Takashi Iwai432c6412012-08-28 09:59:20 -07003509/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003510 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003511 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003512static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003513 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003514{
Takashi Iwai7639a062015-03-03 10:07:24 +01003515 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003516 int count;
3517 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003518 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003519
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003520 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003521 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003522 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003523 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003524 else if (codec->depop_delay > 0)
3525 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003526 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003527 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003528
3529 /* repeat power states setting at most 10 times*/
3530 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003531 if (codec->patch_ops.set_power_state)
3532 codec->patch_ops.set_power_state(codec, fg,
3533 power_state);
3534 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003535 state = power_state;
3536 if (codec->power_filter)
3537 state = codec->power_filter(codec, fg, state);
3538 if (state == power_state || power_state != AC_PWRST_D3)
3539 snd_hda_codec_read(codec, fg, flags,
3540 AC_VERB_SET_POWER_STATE,
3541 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003542 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003543 }
3544 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003545 if (!(state & AC_PWRST_ERROR))
3546 break;
3547 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003548
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003549 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003550}
Takashi Iwai54d17402005-11-21 16:33:22 +01003551
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003552/* sync power states of all widgets;
3553 * this is called at the end of codec parsing
3554 */
3555static void sync_power_up_states(struct hda_codec *codec)
3556{
Takashi Iwai7639a062015-03-03 10:07:24 +01003557 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003558
Takashi Iwaiba615b82013-03-13 14:47:21 +01003559 /* don't care if no filter is used */
3560 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003561 return;
3562
Takashi Iwai7639a062015-03-03 10:07:24 +01003563 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003564 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003565 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003566 if (!(wcaps & AC_WCAP_POWER))
3567 continue;
3568 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3569 if (target == AC_PWRST_D0)
3570 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003571 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003572 snd_hda_codec_write(codec, nid, 0,
3573 AC_VERB_SET_POWER_STATE, target);
3574 }
3575}
3576
Takashi Iwai648a8d22014-02-25 10:38:13 +01003577#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003578/* execute additional init verbs */
3579static void hda_exec_init_verbs(struct hda_codec *codec)
3580{
3581 if (codec->init_verbs.list)
3582 snd_hda_sequence_write(codec, codec->init_verbs.list);
3583}
3584#else
3585static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3586#endif
3587
Takashi Iwai2a439522011-07-26 09:52:50 +02003588#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003589/* update the power on/off account with the current jiffies */
3590static void update_power_acct(struct hda_codec *codec, bool on)
3591{
3592 unsigned long delta = jiffies - codec->power_jiffies;
3593
3594 if (on)
3595 codec->power_on_acct += delta;
3596 else
3597 codec->power_off_acct += delta;
3598 codec->power_jiffies += delta;
3599}
3600
3601void snd_hda_update_power_acct(struct hda_codec *codec)
3602{
3603 update_power_acct(codec, hda_codec_is_power_on(codec));
3604}
3605
Takashi Iwaicb53c622007-08-10 17:21:45 +02003606/*
3607 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003608 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003609 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003610static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003611{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003612 unsigned int state;
3613
Takashi Iwai7639a062015-03-03 10:07:24 +01003614 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003615
Takashi Iwaicb53c622007-08-10 17:21:45 +02003616 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003617 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003618 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003619 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003620 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003621 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003622 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003623}
3624
Takashi Iwaic370dd62012-12-13 18:30:04 +01003625/* mark all entries of cmd and amp caches dirty */
3626static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3627{
3628 int i;
3629 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3630 struct hda_cache_head *cmd;
3631 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3632 cmd->dirty = 1;
3633 }
3634 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3635 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003636 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003637 amp->head.dirty = 1;
3638 }
3639}
3640
Takashi Iwaicb53c622007-08-10 17:21:45 +02003641/*
3642 * kick up codec; used both from PM and power-save
3643 */
3644static void hda_call_codec_resume(struct hda_codec *codec)
3645{
Takashi Iwai7639a062015-03-03 10:07:24 +01003646 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003647
Takashi Iwaic370dd62012-12-13 18:30:04 +01003648 hda_mark_cmd_cache_dirty(codec);
3649
Takashi Iwaicc72da72015-02-19 16:00:22 +01003650 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003651
Takashi Iwaid8193872012-08-31 07:54:38 -07003652 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003653 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003654 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003655 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003656 if (codec->patch_ops.resume)
3657 codec->patch_ops.resume(codec);
3658 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003659 if (codec->patch_ops.init)
3660 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003661 snd_hda_codec_resume_amp(codec);
3662 snd_hda_codec_resume_cache(codec);
3663 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003664
3665 if (codec->jackpoll_interval)
3666 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003667 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003668 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003669 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003670}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003671
Takashi Iwaicc72da72015-02-19 16:00:22 +01003672static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003673{
3674 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003675 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003676 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003677
3678 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003679 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3680 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003681 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003682 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3683 (state & AC_PWRST_CLK_STOP_OK))
3684 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003685 return 0;
3686}
3687
Takashi Iwaicc72da72015-02-19 16:00:22 +01003688static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003689{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003690 struct hda_codec *codec = dev_to_hda_codec(dev);
3691
Takashi Iwai7639a062015-03-03 10:07:24 +01003692 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003693 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003694 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003695 return 0;
3696}
Takashi Iwai2a439522011-07-26 09:52:50 +02003697#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003698
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003699/* referred in hda_bind.c */
3700const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003701 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3702 pm_runtime_force_resume)
3703 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3704 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003705};
Takashi Iwai54d17402005-11-21 16:33:22 +01003706
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003707/*
3708 * add standard channel maps if not specified
3709 */
3710static int add_std_chmaps(struct hda_codec *codec)
3711{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003712 struct hda_pcm *pcm;
3713 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003714
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003715 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003716 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003717 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003718 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003719 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003720
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003721 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003722 continue;
3723 if (!pcm || !hinfo->substreams)
3724 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003725 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003726 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003727 hinfo->channels_max,
3728 0, &chmap);
3729 if (err < 0)
3730 return err;
3731 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3732 }
3733 }
3734 return 0;
3735}
3736
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003737/* default channel maps for 2.1 speakers;
3738 * since HD-audio supports only stereo, odd number channels are omitted
3739 */
3740const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3741 { .channels = 2,
3742 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3743 { .channels = 4,
3744 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3745 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3746 { }
3747};
3748EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3749
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003750int snd_hda_codec_build_controls(struct hda_codec *codec)
3751{
3752 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003753 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003754 /* continue to initialize... */
3755 if (codec->patch_ops.init)
3756 err = codec->patch_ops.init(codec);
3757 if (!err && codec->patch_ops.build_controls)
3758 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003759 if (err < 0)
3760 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003761
3762 /* we create chmaps here instead of build_pcms */
3763 err = add_std_chmaps(codec);
3764 if (err < 0)
3765 return err;
3766
David Henningsson26a6cb62012-10-09 15:04:21 +02003767 if (codec->jackpoll_interval)
3768 hda_jackpoll_work(&codec->jackpoll_work.work);
3769 else
3770 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003771 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return 0;
3773}
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775/*
3776 * stream formats
3777 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003778struct hda_rate_tbl {
3779 unsigned int hz;
3780 unsigned int alsa_bits;
3781 unsigned int hda_fmt;
3782};
3783
Takashi Iwai92f10b32010-08-03 14:21:00 +02003784/* rate = base * mult / div */
3785#define HDA_RATE(base, mult, div) \
3786 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3787 (((div) - 1) << AC_FMT_DIV_SHIFT))
3788
Takashi Iwaibefdf312005-08-22 13:57:55 +02003789static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003791
3792 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003793 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3794 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3795 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3796 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3797 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3798 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3799 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3800 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3801 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3802 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3803 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003804#define AC_PAR_PCM_RATE_BITS 11
3805 /* up to bits 10, 384kHZ isn't supported properly */
3806
3807 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003808 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003809
Takashi Iwaibefdf312005-08-22 13:57:55 +02003810 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811};
3812
3813/**
3814 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003815 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 * @rate: the sample rate
3817 * @channels: the number of channels
3818 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3819 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003820 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 *
3822 * Calculate the format bitset from the given rate, channels and th PCM format.
3823 *
3824 * Return zero if invalid.
3825 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003826unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3827 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 unsigned int channels,
3829 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003830 unsigned int maxbps,
3831 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
3833 int i;
3834 unsigned int val = 0;
3835
Takashi Iwaibefdf312005-08-22 13:57:55 +02003836 for (i = 0; rate_bits[i].hz; i++)
3837 if (rate_bits[i].hz == rate) {
3838 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 break;
3840 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003841 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003842 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return 0;
3844 }
3845
3846 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003847 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return 0;
3849 }
3850 val |= channels - 1;
3851
3852 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003853 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003854 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003855 break;
3856 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003857 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 case 20:
3860 case 24:
3861 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003862 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003863 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003865 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003867 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 break;
3869 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003870 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003871 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return 0;
3873 }
3874
Anssi Hannula32c168c2010-08-03 13:28:57 +03003875 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003876 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 return val;
3879}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003880EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003882static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3883 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003884{
3885 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003886 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003887 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3888 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3889 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003890 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003891 if (!val || val == -1)
3892 return 0;
3893 return val;
3894}
3895
3896static unsigned int query_pcm_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_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003899 get_pcm_param);
3900}
3901
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003902static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3903 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003904{
3905 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3906 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003907 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003908 if (!streams || streams == -1)
3909 return 0;
3910 return streams;
3911}
3912
3913static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3914{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003915 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003916 get_stream_param);
3917}
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919/**
3920 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3921 * @codec: the HDA codec
3922 * @nid: NID to query
3923 * @ratesp: the pointer to store the detected rate bitflags
3924 * @formatsp: the pointer to store the detected formats
3925 * @bpsp: the pointer to store the detected format widths
3926 *
3927 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3928 * or @bsps argument is ignored.
3929 *
3930 * Returns 0 if successful, otherwise a negative error code.
3931 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003932int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3934{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003935 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003937 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003938 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 if (ratesp) {
3941 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003942 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003944 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003946 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003947 codec_err(codec,
3948 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3949 nid, val,
3950 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003951 return -EIO;
3952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 *ratesp = rates;
3954 }
3955
3956 if (formatsp || bpsp) {
3957 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003958 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003960 streams = query_stream_param(codec, nid);
3961 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 bps = 0;
3965 if (streams & AC_SUPFMT_PCM) {
3966 if (val & AC_SUPPCM_BITS_8) {
3967 formats |= SNDRV_PCM_FMTBIT_U8;
3968 bps = 8;
3969 }
3970 if (val & AC_SUPPCM_BITS_16) {
3971 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3972 bps = 16;
3973 }
3974 if (wcaps & AC_WCAP_DIGITAL) {
3975 if (val & AC_SUPPCM_BITS_32)
3976 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3977 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3978 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3979 if (val & AC_SUPPCM_BITS_24)
3980 bps = 24;
3981 else if (val & AC_SUPPCM_BITS_20)
3982 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003983 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3984 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3986 if (val & AC_SUPPCM_BITS_32)
3987 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 else if (val & AC_SUPPCM_BITS_24)
3989 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003990 else if (val & AC_SUPPCM_BITS_20)
3991 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003994#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003995 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003997 if (!bps)
3998 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003999 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004000#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004001 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004002 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 /* temporary hack: we have still no proper support
4004 * for the direct AC3 stream...
4005 */
4006 formats |= SNDRV_PCM_FMTBIT_U8;
4007 bps = 8;
4008 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004009 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004010 codec_err(codec,
4011 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4012 nid, val,
4013 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4014 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004015 return -EIO;
4016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 if (formatsp)
4018 *formatsp = formats;
4019 if (bpsp)
4020 *bpsp = bps;
4021 }
4022
4023 return 0;
4024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004025EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004028 * snd_hda_is_supported_format - Check the validity of the format
4029 * @codec: HD-audio codec
4030 * @nid: NID to check
4031 * @format: the HD-audio format value to check
4032 *
4033 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 *
4035 * Returns 1 if supported, 0 if not.
4036 */
4037int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4038 unsigned int format)
4039{
4040 int i;
4041 unsigned int val = 0, rate, stream;
4042
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004043 val = query_pcm_param(codec, nid);
4044 if (!val)
4045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
4047 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004048 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004049 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (val & (1 << i))
4051 break;
4052 return 0;
4053 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004054 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return 0;
4056
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004057 stream = query_stream_param(codec, nid);
4058 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return 0;
4060
4061 if (stream & AC_SUPFMT_PCM) {
4062 switch (format & 0xf0) {
4063 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004064 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return 0;
4066 break;
4067 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004068 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return 0;
4070 break;
4071 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004072 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return 0;
4074 break;
4075 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004076 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return 0;
4078 break;
4079 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004080 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return 0;
4082 break;
4083 default:
4084 return 0;
4085 }
4086 } else {
4087 /* FIXME: check for float32 and AC3? */
4088 }
4089
4090 return 1;
4091}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004092EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094/*
4095 * PCM stuff
4096 */
4097static int hda_pcm_default_open_close(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{
4101 return 0;
4102}
4103
4104static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4105 struct hda_codec *codec,
4106 unsigned int stream_tag,
4107 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004108 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4111 return 0;
4112}
4113
4114static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4115 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004116 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Takashi Iwai888afa12008-03-18 09:57:50 +01004118 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return 0;
4120}
4121
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004122static int set_pcm_default_values(struct hda_codec *codec,
4123 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004125 int err;
4126
Takashi Iwai0ba21762007-04-16 11:29:14 +02004127 /* query support PCM information from the given NID */
4128 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004129 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004130 info->rates ? NULL : &info->rates,
4131 info->formats ? NULL : &info->formats,
4132 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004133 if (err < 0)
4134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 }
4136 if (info->ops.open == NULL)
4137 info->ops.open = hda_pcm_default_open_close;
4138 if (info->ops.close == NULL)
4139 info->ops.close = hda_pcm_default_open_close;
4140 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004141 if (snd_BUG_ON(!info->nid))
4142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 info->ops.prepare = hda_pcm_default_prepare;
4144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004146 if (snd_BUG_ON(!info->nid))
4147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 info->ops.cleanup = hda_pcm_default_cleanup;
4149 }
4150 return 0;
4151}
4152
Takashi Iwaieb541332010-08-06 13:48:11 +02004153/*
4154 * codec prepare/cleanup entries
4155 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004156/**
4157 * snd_hda_codec_prepare - Prepare a stream
4158 * @codec: the HDA codec
4159 * @hinfo: PCM information
4160 * @stream: stream tag to assign
4161 * @format: format id to assign
4162 * @substream: PCM substream to assign
4163 *
4164 * Calls the prepare callback set by the codec with the given arguments.
4165 * Clean up the inactive streams when successful.
4166 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004167int snd_hda_codec_prepare(struct hda_codec *codec,
4168 struct hda_pcm_stream *hinfo,
4169 unsigned int stream,
4170 unsigned int format,
4171 struct snd_pcm_substream *substream)
4172{
4173 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004174 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004175 if (hinfo->ops.prepare)
4176 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4177 substream);
4178 else
4179 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004180 if (ret >= 0)
4181 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004182 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004183 return ret;
4184}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004185EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004186
Takashi Iwai95a962c2014-10-29 16:03:58 +01004187/**
4188 * snd_hda_codec_cleanup - Prepare a stream
4189 * @codec: the HDA codec
4190 * @hinfo: PCM information
4191 * @substream: PCM substream
4192 *
4193 * Calls the cleanup callback set by the codec with the given arguments.
4194 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004195void snd_hda_codec_cleanup(struct hda_codec *codec,
4196 struct hda_pcm_stream *hinfo,
4197 struct snd_pcm_substream *substream)
4198{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004199 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004200 if (hinfo->ops.cleanup)
4201 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004202 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004203}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004204EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004205
Takashi Iwaid5191e52009-11-16 14:58:17 +01004206/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004207const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4208 "Audio", "SPDIF", "HDMI", "Modem"
4209};
4210
Takashi Iwai176d5332008-07-30 15:01:44 +02004211/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004212 * get the empty PCM device number to assign
4213 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004214static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004215{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004216 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004217 /* assigned to static slots up to dev#10; if more needed, assign
4218 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4219 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004220 static int audio_idx[HDA_PCM_NTYPES][5] = {
4221 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4222 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004223 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004224 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004225 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004226 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004227
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004228 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004229 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004230 return -EINVAL;
4231 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004232
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004233 for (i = 0; audio_idx[type][i] >= 0; i++) {
4234#ifndef CONFIG_SND_DYNAMIC_MINORS
4235 if (audio_idx[type][i] >= 8)
4236 break;
4237#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004238 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4239 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004240 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004241
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004242#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004243 /* non-fixed slots starting from 10 */
4244 for (i = 10; i < 32; i++) {
4245 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4246 return i;
4247 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004248#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004249
Takashi Iwai4e76a882014-02-25 12:21:03 +01004250 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004251 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004252#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004253 dev_warn(bus->card->dev,
4254 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004255#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004256 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004257}
4258
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004259/* call build_pcms ops of the given codec and set up the default parameters */
4260int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004261{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004262 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004263 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004264
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004265 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004266 return 0; /* already parsed */
4267
4268 if (!codec->patch_ops.build_pcms)
4269 return 0;
4270
4271 err = codec->patch_ops.build_pcms(codec);
4272 if (err < 0) {
4273 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004274 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004275 return err;
4276 }
4277
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004278 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004279 int stream;
4280
4281 for (stream = 0; stream < 2; stream++) {
4282 struct hda_pcm_stream *info = &cpcm->stream[stream];
4283
4284 if (!info->substreams)
4285 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004286 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004287 if (err < 0) {
4288 codec_warn(codec,
4289 "fail to setup default for PCM %s\n",
4290 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004291 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004292 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004293 }
4294 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004295
4296 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004297}
4298
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004299/* assign all PCMs of the given codec */
4300int snd_hda_codec_build_pcms(struct hda_codec *codec)
4301{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004302 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004303 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004304 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004305
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004306 if (snd_BUG_ON(!bus->ops.attach_pcm))
4307 return -EINVAL;
4308
4309 err = snd_hda_codec_parse_pcms(codec);
4310 if (err < 0) {
4311 snd_hda_codec_reset(codec);
4312 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004313 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004314
4315 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004316 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004317 if (cpcm->pcm)
4318 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004319 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004320 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004321
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004322 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4323 if (dev < 0)
4324 continue; /* no fatal error */
4325 cpcm->device = dev;
4326 err = bus->ops.attach_pcm(bus, codec, cpcm);
4327 if (err < 0) {
4328 codec_err(codec,
4329 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004330 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004331 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004332 }
4333 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004334
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004335 return 0;
4336}
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 * snd_hda_add_new_ctls - create controls from the array
4340 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004341 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 *
4343 * This helper function creates and add new controls in the given array.
4344 * The array must be terminated with an empty entry as terminator.
4345 *
4346 * Returns 0 if successful, or a negative error code.
4347 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004348int snd_hda_add_new_ctls(struct hda_codec *codec,
4349 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004354 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004355 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004356 if (knew->iface == -1) /* skip this codec private value */
4357 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004358 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004359 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004360 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004361 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004362 if (addr > 0)
4363 kctl->id.device = addr;
4364 if (idx > 0)
4365 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004366 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004367 if (!err)
4368 break;
4369 /* try first with another device index corresponding to
4370 * the codec addr; if it still fails (or it's the
4371 * primary codec), then try another control index
4372 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004373 if (!addr && codec->core.addr)
4374 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004375 else if (!idx && !knew->index) {
4376 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004377 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004378 if (idx <= 0)
4379 return err;
4380 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004381 return err;
4382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 }
4384 return 0;
4385}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004386EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Takashi Iwai83012a72012-08-24 18:38:08 +02004388#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01004389static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004390{
4391 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004392
Takashi Iwaicc72da72015-02-19 16:00:22 +01004393 if (delay > 0) {
4394 pm_runtime_set_autosuspend_delay(dev, delay);
4395 pm_runtime_use_autosuspend(dev);
4396 pm_runtime_allow(dev);
4397 if (!pm_runtime_suspended(dev))
4398 pm_runtime_mark_last_busy(dev);
4399 } else {
4400 pm_runtime_dont_use_autosuspend(dev);
4401 pm_runtime_forbid(dev);
4402 }
4403}
Takashi Iwaibb573922015-02-20 09:26:04 +01004404
4405/**
4406 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4407 * @bus: HD-audio bus
4408 * @delay: autosuspend delay in msec, 0 = off
4409 *
4410 * Synchronize the runtime PM autosuspend state from the power_save option.
4411 */
4412void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4413{
4414 struct hda_codec *c;
4415
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004416 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01004417 codec_set_power_save(c, delay);
4418}
4419EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004420
Takashi Iwaid5191e52009-11-16 14:58:17 +01004421/**
4422 * snd_hda_check_amp_list_power - Check the amp list and update the power
4423 * @codec: HD-audio codec
4424 * @check: the object containing an AMP list and the status
4425 * @nid: NID to check / update
4426 *
4427 * Check whether the given NID is in the amp list. If it's in the list,
4428 * check the current AMP status, and update the the power-status according
4429 * to the mute status.
4430 *
4431 * This function is supposed to be set or called from the check_power_status
4432 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004433 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004434int snd_hda_check_amp_list_power(struct hda_codec *codec,
4435 struct hda_loopback_check *check,
4436 hda_nid_t nid)
4437{
Takashi Iwai031024e2011-05-02 11:29:30 +02004438 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004439 int ch, v;
4440
4441 if (!check->amplist)
4442 return 0;
4443 for (p = check->amplist; p->nid; p++) {
4444 if (p->nid == nid)
4445 break;
4446 }
4447 if (!p->nid)
4448 return 0; /* nothing changed */
4449
4450 for (p = check->amplist; p->nid; p++) {
4451 for (ch = 0; ch < 2; ch++) {
4452 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4453 p->idx);
4454 if (!(v & HDA_AMP_MUTE) && v > 0) {
4455 if (!check->power_on) {
4456 check->power_on = 1;
4457 snd_hda_power_up(codec);
4458 }
4459 return 1;
4460 }
4461 }
4462 }
4463 if (check->power_on) {
4464 check->power_on = 0;
4465 snd_hda_power_down(codec);
4466 }
4467 return 0;
4468}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004469EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 * input MUX helper
4474 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004475
4476/**
4477 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004478 * @imux: imux helper object
4479 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004480 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004481int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4482 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 unsigned int index;
4485
4486 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4487 uinfo->count = 1;
4488 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004489 if (!imux->num_items)
4490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 index = uinfo->value.enumerated.item;
4492 if (index >= imux->num_items)
4493 index = imux->num_items - 1;
4494 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4495 return 0;
4496}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004497EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Takashi Iwaid5191e52009-11-16 14:58:17 +01004499/**
4500 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004501 * @codec: the HDA codec
4502 * @imux: imux helper object
4503 * @ucontrol: pointer to get/store the data
4504 * @nid: input mux NID
4505 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004506 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004507int snd_hda_input_mux_put(struct hda_codec *codec,
4508 const struct hda_input_mux *imux,
4509 struct snd_ctl_elem_value *ucontrol,
4510 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 unsigned int *cur_val)
4512{
4513 unsigned int idx;
4514
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004515 if (!imux->num_items)
4516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 idx = ucontrol->value.enumerated.item[0];
4518 if (idx >= imux->num_items)
4519 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004520 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004522 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4523 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 *cur_val = idx;
4525 return 1;
4526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004527EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004530/**
4531 * snd_hda_enum_helper_info - Helper for simple enum ctls
4532 * @kcontrol: ctl element
4533 * @uinfo: pointer to get/store the data
4534 * @num_items: number of enum items
4535 * @texts: enum item string array
4536 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004537 * process kcontrol info callback of a simple string enum array
4538 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4539 */
4540int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4541 struct snd_ctl_elem_info *uinfo,
4542 int num_items, const char * const *texts)
4543{
4544 static const char * const texts_default[] = {
4545 "Disabled", "Enabled"
4546 };
4547
4548 if (!texts || !num_items) {
4549 num_items = 2;
4550 texts = texts_default;
4551 }
4552
Takashi Iwai3ff72212014-10-20 18:17:28 +02004553 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004554}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004555EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004556
4557/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 * Multi-channel / digital-out PCM helper functions
4559 */
4560
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004561/* setup SPDIF output stream */
4562static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4563 unsigned int stream_tag, unsigned int format)
4564{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004565 struct hda_spdif_out *spdif;
4566 unsigned int curr_fmt;
4567 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004568
Laurence Darby3bef1c32012-11-03 17:00:06 +00004569 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4570 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4571 AC_VERB_GET_STREAM_FORMAT, 0);
4572 reset = codec->spdif_status_reset &&
4573 (spdif->ctls & AC_DIG1_ENABLE) &&
4574 curr_fmt != format;
4575
4576 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4577 updated */
4578 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004579 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004580 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004581 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004582 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004583 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004584 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004585 for (d = codec->slave_dig_outs; *d; d++)
4586 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4587 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004588 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004589 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004590 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004591 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004592 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004593}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004594
Takashi Iwai2f728532008-09-25 16:32:41 +02004595static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4596{
4597 snd_hda_codec_cleanup_stream(codec, nid);
4598 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004599 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004600 for (d = codec->slave_dig_outs; *d; d++)
4601 snd_hda_codec_cleanup_stream(codec, *d);
4602 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004603}
4604
Takashi Iwaid5191e52009-11-16 14:58:17 +01004605/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004606 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004607 * @codec: the HDA codec
4608 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004610int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4611 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
Ingo Molnar62932df2006-01-16 16:34:20 +01004613 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004614 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4615 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004616 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004618 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return 0;
4620}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004621EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Takashi Iwaid5191e52009-11-16 14:58:17 +01004623/**
4624 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004625 * @codec: the HDA codec
4626 * @mout: hda_multi_out object
4627 * @stream_tag: stream tag to assign
4628 * @format: format id to assign
4629 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004630 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004631int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4632 struct hda_multi_out *mout,
4633 unsigned int stream_tag,
4634 unsigned int format,
4635 struct snd_pcm_substream *substream)
4636{
4637 mutex_lock(&codec->spdif_mutex);
4638 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4639 mutex_unlock(&codec->spdif_mutex);
4640 return 0;
4641}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004642EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004643
Takashi Iwaid5191e52009-11-16 14:58:17 +01004644/**
4645 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004646 * @codec: the HDA codec
4647 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004648 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004649int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4650 struct hda_multi_out *mout)
4651{
4652 mutex_lock(&codec->spdif_mutex);
4653 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4654 mutex_unlock(&codec->spdif_mutex);
4655 return 0;
4656}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004657EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004658
Takashi Iwaid5191e52009-11-16 14:58:17 +01004659/**
4660 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004661 * @codec: the HDA codec
4662 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004664int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4665 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
Ingo Molnar62932df2006-01-16 16:34:20 +01004667 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004669 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 return 0;
4671}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004672EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Takashi Iwaid5191e52009-11-16 14:58:17 +01004674/**
4675 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004676 * @codec: the HDA codec
4677 * @mout: hda_multi_out object
4678 * @substream: PCM substream to assign
4679 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004680 *
4681 * Open analog outputs and set up the hw-constraints.
4682 * If the digital outputs can be opened as slave, open the digital
4683 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004685int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4686 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004687 struct snd_pcm_substream *substream,
4688 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Takashi Iwai9a081602008-02-12 18:37:26 +01004690 struct snd_pcm_runtime *runtime = substream->runtime;
4691 runtime->hw.channels_max = mout->max_channels;
4692 if (mout->dig_out_nid) {
4693 if (!mout->analog_rates) {
4694 mout->analog_rates = hinfo->rates;
4695 mout->analog_formats = hinfo->formats;
4696 mout->analog_maxbps = hinfo->maxbps;
4697 } else {
4698 runtime->hw.rates = mout->analog_rates;
4699 runtime->hw.formats = mout->analog_formats;
4700 hinfo->maxbps = mout->analog_maxbps;
4701 }
4702 if (!mout->spdif_rates) {
4703 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4704 &mout->spdif_rates,
4705 &mout->spdif_formats,
4706 &mout->spdif_maxbps);
4707 }
4708 mutex_lock(&codec->spdif_mutex);
4709 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004710 if ((runtime->hw.rates & mout->spdif_rates) &&
4711 (runtime->hw.formats & mout->spdif_formats)) {
4712 runtime->hw.rates &= mout->spdif_rates;
4713 runtime->hw.formats &= mout->spdif_formats;
4714 if (mout->spdif_maxbps < hinfo->maxbps)
4715 hinfo->maxbps = mout->spdif_maxbps;
4716 } else {
4717 mout->share_spdif = 0;
4718 /* FIXME: need notify? */
4719 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004720 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004721 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4724 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4725}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004726EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Takashi Iwaid5191e52009-11-16 14:58:17 +01004728/**
4729 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004730 * @codec: the HDA codec
4731 * @mout: hda_multi_out object
4732 * @stream_tag: stream tag to assign
4733 * @format: format id to assign
4734 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004735 *
4736 * Set up the i/o for analog out.
4737 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004739int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4740 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 unsigned int stream_tag,
4742 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004743 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Takashi Iwaidda14412011-05-02 11:29:30 +02004745 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004747 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 int i;
4749
Ingo Molnar62932df2006-01-16 16:34:20 +01004750 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004751 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004752 if (mout->dig_out_nid && mout->share_spdif &&
4753 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004755 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4756 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004757 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004759 setup_dig_out_stream(codec, mout->dig_out_nid,
4760 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 } else {
4762 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004763 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
4765 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004766 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004769 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4770 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004771 if (!mout->no_share_stream &&
4772 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004774 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4775 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004776 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004777 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4778 if (!mout->no_share_stream && mout->hp_out_nid[i])
4779 snd_hda_codec_setup_stream(codec,
4780 mout->hp_out_nid[i],
4781 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 /* surrounds */
4784 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004785 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004786 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4787 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004788 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004789 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4790 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004792
4793 /* extra surrounds */
4794 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4795 int ch = 0;
4796 if (!mout->extra_out_nid[i])
4797 break;
4798 if (chs >= (i + 1) * 2)
4799 ch = i * 2;
4800 else if (!mout->no_share_stream)
4801 break;
4802 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4803 stream_tag, ch, format);
4804 }
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return 0;
4807}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004808EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Takashi Iwaid5191e52009-11-16 14:58:17 +01004810/**
4811 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004812 * @codec: the HDA codec
4813 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004815int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4816 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Takashi Iwaidda14412011-05-02 11:29:30 +02004818 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 int i;
4820
4821 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004822 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004824 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004825 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4826 if (mout->hp_out_nid[i])
4827 snd_hda_codec_cleanup_stream(codec,
4828 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004829 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4830 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004831 snd_hda_codec_cleanup_stream(codec,
4832 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004833 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004835 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 mout->dig_out_used = 0;
4837 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004838 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return 0;
4840}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004841EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Takashi Iwai47408602012-04-20 13:06:53 +02004843/**
4844 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004845 * @codec: the HDA codec
4846 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004847 *
4848 * Guess the suitable VREF pin bits to be set as the pin-control value.
4849 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4850 */
4851unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4852{
4853 unsigned int pincap;
4854 unsigned int oldval;
4855 oldval = snd_hda_codec_read(codec, pin, 0,
4856 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4857 pincap = snd_hda_query_pin_caps(codec, pin);
4858 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4859 /* Exception: if the default pin setup is vref50, we give it priority */
4860 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4861 return AC_PINCTL_VREF_80;
4862 else if (pincap & AC_PINCAP_VREF_50)
4863 return AC_PINCTL_VREF_50;
4864 else if (pincap & AC_PINCAP_VREF_100)
4865 return AC_PINCTL_VREF_100;
4866 else if (pincap & AC_PINCAP_VREF_GRD)
4867 return AC_PINCTL_VREF_GRD;
4868 return AC_PINCTL_VREF_HIZ;
4869}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004870EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004871
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004872/**
4873 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4874 * @codec: the HDA codec
4875 * @pin: referred pin NID
4876 * @val: pin ctl value to audit
4877 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004878unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4879 hda_nid_t pin, unsigned int val)
4880{
4881 static unsigned int cap_lists[][2] = {
4882 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4883 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4884 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4885 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4886 };
4887 unsigned int cap;
4888
4889 if (!val)
4890 return 0;
4891 cap = snd_hda_query_pin_caps(codec, pin);
4892 if (!cap)
4893 return val; /* don't know what to do... */
4894
4895 if (val & AC_PINCTL_OUT_EN) {
4896 if (!(cap & AC_PINCAP_OUT))
4897 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4898 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4899 val &= ~AC_PINCTL_HP_EN;
4900 }
4901
4902 if (val & AC_PINCTL_IN_EN) {
4903 if (!(cap & AC_PINCAP_IN))
4904 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4905 else {
4906 unsigned int vcap, vref;
4907 int i;
4908 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4909 vref = val & AC_PINCTL_VREFEN;
4910 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4911 if (vref == cap_lists[i][0] &&
4912 !(vcap & cap_lists[i][1])) {
4913 if (i == ARRAY_SIZE(cap_lists) - 1)
4914 vref = AC_PINCTL_VREF_HIZ;
4915 else
4916 vref = cap_lists[i + 1][0];
4917 }
4918 }
4919 val &= ~AC_PINCTL_VREFEN;
4920 val |= vref;
4921 }
4922 }
4923
4924 return val;
4925}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004926EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004927
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004928/**
4929 * _snd_hda_pin_ctl - Helper to set pin ctl value
4930 * @codec: the HDA codec
4931 * @pin: referred pin NID
4932 * @val: pin control value to set
4933 * @cached: access over codec pinctl cache or direct write
4934 *
4935 * This function is a helper to set a pin ctl value more safely.
4936 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4937 * value in pin target array via snd_hda_codec_set_pin_target(), then
4938 * actually writes the value via either snd_hda_codec_update_cache() or
4939 * snd_hda_codec_write() depending on @cached flag.
4940 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004941int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4942 unsigned int val, bool cached)
4943{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004944 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004945 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004946 if (cached)
4947 return snd_hda_codec_update_cache(codec, pin, 0,
4948 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4949 else
4950 return snd_hda_codec_write(codec, pin, 0,
4951 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4952}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004953EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004954
Takashi Iwai990061c2010-09-09 22:08:44 +02004955/**
4956 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004957 * @codec: the HDA codec
4958 * @imux: imux helper object
4959 * @label: the name of imux item to assign
4960 * @index: index number of imux item to assign
4961 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004962 *
4963 * When the same label is used already in the existing items, the number
4964 * suffix is appended to the label. This label index number is stored
4965 * to type_idx when non-NULL pointer is given.
4966 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004967int snd_hda_add_imux_item(struct hda_codec *codec,
4968 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004969 int index, int *type_idx)
4970{
4971 int i, label_idx = 0;
4972 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004973 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004974 return -EINVAL;
4975 }
4976 for (i = 0; i < imux->num_items; i++) {
4977 if (!strncmp(label, imux->items[i].label, strlen(label)))
4978 label_idx++;
4979 }
4980 if (type_idx)
4981 *type_idx = label_idx;
4982 if (label_idx > 0)
4983 snprintf(imux->items[imux->num_items].label,
4984 sizeof(imux->items[imux->num_items].label),
4985 "%s %d", label, label_idx);
4986 else
4987 strlcpy(imux->items[imux->num_items].label, label,
4988 sizeof(imux->items[imux->num_items].label));
4989 imux->items[imux->num_items].index = index;
4990 imux->num_items++;
4991 return 0;
4992}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004993EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004996 * snd_hda_bus_reset - Reset the bus
4997 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004999void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005001 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Takashi Iwaid068ebc2015-03-02 23:22:59 +01005003 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005004 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005005 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005006#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005007 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005008 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005009 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005010 }
5011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005014EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005015
Takashi Iwaid5191e52009-11-16 14:58:17 +01005016/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005017 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5018 * @pcm: PCM caps bits
5019 * @buf: the string buffer to write
5020 * @buflen: the max buffer length
5021 *
5022 * used by hda_proc.c and hda_eld.c
5023 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005024void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5025{
5026 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5027 int i, j;
5028
5029 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5030 if (pcm & (AC_SUPPCM_BITS_8 << i))
5031 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5032
5033 buf[j] = '\0'; /* necessary when j == 0 */
5034}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005035EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005036
5037MODULE_DESCRIPTION("HDA codec core");
5038MODULE_LICENSE("GPL");