blob: 52962f697825b4297a87447c9e39305b37b7a667 [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 Warren7c935972011-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 Iwaid5191e52009-11-16 14:58:17 +01001546/**
1547 * snd_hda_codec_amp_stereo - update the AMP stereo values
1548 * @codec: HD-audio codec
1549 * @nid: NID to read the AMP value
1550 * @direction: #HDA_INPUT or #HDA_OUTPUT
1551 * @idx: the index value (only for input direction)
1552 * @mask: bit mask to set
1553 * @val: the bits value to set
1554 *
1555 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1556 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001557 */
1558int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1559 int direction, int idx, int mask, int val)
1560{
1561 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001562
1563 if (snd_BUG_ON(mask & ~0xff))
1564 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001565 for (ch = 0; ch < 2; ch++)
1566 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1567 idx, mask, val);
1568 return ret;
1569}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001570EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001571
Takashi Iwai95a962c2014-10-29 16:03:58 +01001572/**
1573 * snd_hda_codec_amp_init - initialize the AMP value
1574 * @codec: the HDA codec
1575 * @nid: NID to read the AMP value
1576 * @ch: channel (left=0 or right=1)
1577 * @dir: #HDA_INPUT or #HDA_OUTPUT
1578 * @idx: the index value (only for input direction)
1579 * @mask: bit mask to set
1580 * @val: the bits value to set
1581 *
1582 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001583 * the first access. If the amp was already initialized / updated beforehand,
1584 * this does nothing.
1585 */
1586int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1587 int dir, int idx, int mask, int val)
1588{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001589 int orig;
1590
1591 if (!codec->core.regmap)
1592 return -EINVAL;
1593 regcache_cache_only(codec->core.regmap, true);
1594 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1595 regcache_cache_only(codec->core.regmap, false);
1596 if (orig >= 0)
1597 return 0;
1598 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001600EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001601
Takashi Iwai95a962c2014-10-29 16:03:58 +01001602/**
1603 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1604 * @codec: the HDA codec
1605 * @nid: NID to read the AMP value
1606 * @dir: #HDA_INPUT or #HDA_OUTPUT
1607 * @idx: the index value (only for input direction)
1608 * @mask: bit mask to set
1609 * @val: the bits value to set
1610 *
1611 * Call snd_hda_codec_amp_init() for both stereo channels.
1612 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001613int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1614 int dir, int idx, int mask, int val)
1615{
1616 int ch, ret = 0;
1617
1618 if (snd_BUG_ON(mask & ~0xff))
1619 mask &= 0xff;
1620 for (ch = 0; ch < 2; ch++)
1621 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1622 idx, mask, val);
1623 return ret;
1624}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001625EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001626
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001627static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1628 unsigned int ofs)
1629{
1630 u32 caps = query_amp_caps(codec, nid, dir);
1631 /* get num steps */
1632 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1633 if (ofs < caps)
1634 caps -= ofs;
1635 return caps;
1636}
1637
Takashi Iwaid5191e52009-11-16 14:58:17 +01001638/**
1639 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001640 * @kcontrol: referred ctl element
1641 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001642 *
1643 * The control element is supposed to have the private_value field
1644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001646int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 u16 nid = get_amp_nid(kcontrol);
1651 u8 chs = get_amp_channels(kcontrol);
1652 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001653 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001655 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1656 uinfo->count = chs == 3 ? 2 : 1;
1657 uinfo->value.integer.min = 0;
1658 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1659 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001660 codec_warn(codec,
1661 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1662 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return -EINVAL;
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
1666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001667EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001669
1670static inline unsigned int
1671read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1672 int ch, int dir, int idx, unsigned int ofs)
1673{
1674 unsigned int val;
1675 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1676 val &= HDA_AMP_VOLMASK;
1677 if (val >= ofs)
1678 val -= ofs;
1679 else
1680 val = 0;
1681 return val;
1682}
1683
1684static inline int
1685update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1686 int ch, int dir, int idx, unsigned int ofs,
1687 unsigned int val)
1688{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001689 unsigned int maxval;
1690
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001691 if (val > 0)
1692 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001693 /* ofs = 0: raw max value */
1694 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001695 if (val > maxval)
1696 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001697 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1698 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001699}
1700
Takashi Iwaid5191e52009-11-16 14:58:17 +01001701/**
1702 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001703 * @kcontrol: ctl element
1704 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001705 *
1706 * The control element is supposed to have the private_value field
1707 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1708 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001709int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1713 hda_nid_t nid = get_amp_nid(kcontrol);
1714 int chs = get_amp_channels(kcontrol);
1715 int dir = get_amp_direction(kcontrol);
1716 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001717 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 long *valp = ucontrol->value.integer.value;
1719
1720 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001721 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001723 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001726EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Takashi Iwaid5191e52009-11-16 14:58:17 +01001728/**
1729 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001730 * @kcontrol: ctl element
1731 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001732 *
1733 * The control element is supposed to have the private_value field
1734 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1735 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001736int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1740 hda_nid_t nid = get_amp_nid(kcontrol);
1741 int chs = get_amp_channels(kcontrol);
1742 int dir = get_amp_direction(kcontrol);
1743 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001744 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 long *valp = ucontrol->value.integer.value;
1746 int change = 0;
1747
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001748 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001749 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001750 valp++;
1751 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001752 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001753 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return change;
1755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001756EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Takashi Iwaid5191e52009-11-16 14:58:17 +01001758/**
1759 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001760 * @kcontrol: ctl element
1761 * @op_flag: operation flag
1762 * @size: byte size of input TLV
1763 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001764 *
1765 * The control element is supposed to have the private_value field
1766 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1767 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001768int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1769 unsigned int size, unsigned int __user *_tlv)
1770{
1771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1772 hda_nid_t nid = get_amp_nid(kcontrol);
1773 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001774 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001775 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001776 u32 caps, val1, val2;
1777
1778 if (size < 4 * sizeof(unsigned int))
1779 return -ENOMEM;
1780 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001781 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1782 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001783 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001784 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001785 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001786 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001787 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001788 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1789 return -EFAULT;
1790 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1791 return -EFAULT;
1792 if (put_user(val1, _tlv + 2))
1793 return -EFAULT;
1794 if (put_user(val2, _tlv + 3))
1795 return -EFAULT;
1796 return 0;
1797}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001798EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001799
Takashi Iwaid5191e52009-11-16 14:58:17 +01001800/**
1801 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1802 * @codec: HD-audio codec
1803 * @nid: NID of a reference widget
1804 * @dir: #HDA_INPUT or #HDA_OUTPUT
1805 * @tlv: TLV data to be stored, at least 4 elements
1806 *
1807 * Set (static) TLV data for a virtual master volume using the AMP caps
1808 * obtained from the reference NID.
1809 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001810 */
1811void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1812 unsigned int *tlv)
1813{
1814 u32 caps;
1815 int nums, step;
1816
1817 caps = query_amp_caps(codec, nid, dir);
1818 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1819 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1820 step = (step + 1) * 25;
1821 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1822 tlv[1] = 2 * sizeof(unsigned int);
1823 tlv[2] = -nums * step;
1824 tlv[3] = step;
1825}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001826EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001827
1828/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001829static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001830find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001831{
1832 struct snd_ctl_elem_id id;
1833 memset(&id, 0, sizeof(id));
1834 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001835 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001836 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001837 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1838 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001839 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001840 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001841}
1842
Takashi Iwaid5191e52009-11-16 14:58:17 +01001843/**
1844 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1845 * @codec: HD-audio codec
1846 * @name: ctl id name string
1847 *
1848 * Get the control element with the given id string and IFACE_MIXER.
1849 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001850struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1851 const char *name)
1852{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001853 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001854}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001855EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001856
Takashi Iwaidcda5802012-10-12 17:24:51 +02001857static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001858 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001859{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001860 int i, idx;
1861 /* 16 ctlrs should be large enough */
1862 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1863 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001864 return idx;
1865 }
1866 return -EBUSY;
1867}
1868
Takashi Iwaid5191e52009-11-16 14:58:17 +01001869/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001870 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001871 * @codec: HD-audio codec
1872 * @nid: corresponding NID (optional)
1873 * @kctl: the control element to assign
1874 *
1875 * Add the given control element to an array inside the codec instance.
1876 * All control elements belonging to a codec are supposed to be added
1877 * by this function so that a proper clean-up works at the free or
1878 * reconfiguration time.
1879 *
1880 * If non-zero @nid is passed, the NID is assigned to the control element.
1881 * The assignment is shown in the codec proc file.
1882 *
1883 * snd_hda_ctl_add() checks the control subdev id field whether
1884 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001885 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1886 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001887 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001888int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1889 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001890{
1891 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001892 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001893 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001894
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001895 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001896 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001897 if (nid == 0)
1898 nid = get_amp_nid_(kctl->private_value);
1899 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001900 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1901 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001902 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001903 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001904 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001905 if (err < 0)
1906 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001907 item = snd_array_new(&codec->mixers);
1908 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001909 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001910 item->kctl = kctl;
1911 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001912 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001913 return 0;
1914}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001915EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001916
Takashi Iwaid5191e52009-11-16 14:58:17 +01001917/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001918 * snd_hda_add_nid - Assign a NID to a control element
1919 * @codec: HD-audio codec
1920 * @nid: corresponding NID (optional)
1921 * @kctl: the control element to assign
1922 * @index: index to kctl
1923 *
1924 * Add the given control element to an array inside the codec instance.
1925 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1926 * NID:KCTL mapping - for example "Capture Source" selector.
1927 */
1928int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1929 unsigned int index, hda_nid_t nid)
1930{
1931 struct hda_nid_item *item;
1932
1933 if (nid > 0) {
1934 item = snd_array_new(&codec->nids);
1935 if (!item)
1936 return -ENOMEM;
1937 item->kctl = kctl;
1938 item->index = index;
1939 item->nid = nid;
1940 return 0;
1941 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001942 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1943 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001944 return -EINVAL;
1945}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001946EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001947
1948/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001949 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1950 * @codec: HD-audio codec
1951 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001952void snd_hda_ctls_clear(struct hda_codec *codec)
1953{
1954 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001955 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001956 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001957 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001958 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001959 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001960}
1961
Takashi Iwai95a962c2014-10-29 16:03:58 +01001962/**
1963 * snd_hda_lock_devices - pseudo device locking
1964 * @bus: the BUS
1965 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001966 * toggle card->shutdown to allow/disallow the device access (as a hack)
1967 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001968int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001969{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001970 struct snd_card *card = bus->card;
1971 struct hda_codec *codec;
1972
Takashi Iwaia65d6292009-02-23 16:57:04 +01001973 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001974 if (card->shutdown)
1975 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001976 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001977 if (!list_empty(&card->ctl_files))
1978 goto err_clear;
1979
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001980 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001981 struct hda_pcm *cpcm;
1982 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001983 if (!cpcm->pcm)
1984 continue;
1985 if (cpcm->pcm->streams[0].substream_opened ||
1986 cpcm->pcm->streams[1].substream_opened)
1987 goto err_clear;
1988 }
1989 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001990 spin_unlock(&card->files_lock);
1991 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001992
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001993 err_clear:
1994 card->shutdown = 0;
1995 err_unlock:
1996 spin_unlock(&card->files_lock);
1997 return -EINVAL;
1998}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001999EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002000
Takashi Iwai95a962c2014-10-29 16:03:58 +01002001/**
2002 * snd_hda_unlock_devices - pseudo device unlocking
2003 * @bus: the BUS
2004 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002005void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002006{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002007 struct snd_card *card = bus->card;
2008
Takashi Iwaia65d6292009-02-23 16:57:04 +01002009 spin_lock(&card->files_lock);
2010 card->shutdown = 0;
2011 spin_unlock(&card->files_lock);
2012}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002013EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002014
Takashi Iwaid5191e52009-11-16 14:58:17 +01002015/**
2016 * snd_hda_codec_reset - Clear all objects assigned to the codec
2017 * @codec: HD-audio codec
2018 *
2019 * This frees the all PCM and control elements assigned to the codec, and
2020 * clears the caches and restores the pin default configurations.
2021 *
2022 * When a device is being used, it returns -EBSY. If successfully freed,
2023 * returns zero.
2024 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002025int snd_hda_codec_reset(struct hda_codec *codec)
2026{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002027 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002028
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002029 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002030 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002031
2032 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01002033 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002034
Takashi Iwaia65d6292009-02-23 16:57:04 +01002035 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002036 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002037 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002038}
2039
Takashi Iwai6194b992014-06-06 18:12:16 +02002040typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002041
2042/* apply the function to all matching slave ctls in the mixer list */
2043static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002044 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002045{
2046 struct hda_nid_item *items;
2047 const char * const *s;
2048 int i, err;
2049
2050 items = codec->mixers.list;
2051 for (i = 0; i < codec->mixers.used; i++) {
2052 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002053 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002054 continue;
2055 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002056 char tmpname[sizeof(sctl->id.name)];
2057 const char *name = *s;
2058 if (suffix) {
2059 snprintf(tmpname, sizeof(tmpname), "%s %s",
2060 name, suffix);
2061 name = tmpname;
2062 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002063 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002064 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002065 if (err)
2066 return err;
2067 break;
2068 }
2069 }
2070 }
2071 return 0;
2072}
2073
Takashi Iwai6194b992014-06-06 18:12:16 +02002074static int check_slave_present(struct hda_codec *codec,
2075 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002076{
2077 return 1;
2078}
2079
Takashi Iwai18478e82012-03-09 17:51:10 +01002080/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002081static int get_kctl_0dB_offset(struct hda_codec *codec,
2082 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002083{
2084 int _tlv[4];
2085 const int *tlv = NULL;
2086 int val = -1;
2087
2088 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2089 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2090 mm_segment_t fs = get_fs();
2091 set_fs(get_ds());
2092 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2093 tlv = _tlv;
2094 set_fs(fs);
2095 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2096 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002097 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2098 int step = tlv[3];
2099 step &= ~TLV_DB_SCALE_MUTE;
2100 if (!step)
2101 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002102 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002103 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002104- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002105 return -1;
2106 }
2107 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002108 val = -tlv[2] / step;
2109 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002110 return val;
2111}
2112
2113/* call kctl->put with the given value(s) */
2114static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2115{
2116 struct snd_ctl_elem_value *ucontrol;
2117 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2118 if (!ucontrol)
2119 return -ENOMEM;
2120 ucontrol->value.integer.value[0] = val;
2121 ucontrol->value.integer.value[1] = val;
2122 kctl->put(kctl, ucontrol);
2123 kfree(ucontrol);
2124 return 0;
2125}
2126
2127/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002128static int init_slave_0dB(struct hda_codec *codec,
2129 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002130{
Takashi Iwai6194b992014-06-06 18:12:16 +02002131 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002132 if (offset > 0)
2133 put_kctl_with_value(slave, offset);
2134 return 0;
2135}
2136
2137/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002138static int init_slave_unmute(struct hda_codec *codec,
2139 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002140{
2141 return put_kctl_with_value(slave, 1);
2142}
2143
Takashi Iwaie8750942014-06-30 14:02:39 +02002144static int add_slave(struct hda_codec *codec,
2145 void *data, struct snd_kcontrol *slave)
2146{
2147 return snd_ctl_add_slave(data, slave);
2148}
2149
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002151 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002152 * @codec: HD-audio codec
2153 * @name: vmaster control name
2154 * @tlv: TLV data (optional)
2155 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002156 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002157 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002158 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002159 *
2160 * Create a virtual master control with the given name. The TLV data
2161 * must be either NULL or a valid data.
2162 *
2163 * @slaves is a NULL-terminated array of strings, each of which is a
2164 * slave control name. All controls with these names are assigned to
2165 * the new virtual master control.
2166 *
2167 * This function returns zero if successful or a negative error code.
2168 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002169int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002170 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002171 const char *suffix, bool init_slave_vol,
2172 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002173{
2174 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002175 int err;
2176
Takashi Iwai29e58532012-03-12 12:25:03 +01002177 if (ctl_ret)
2178 *ctl_ret = NULL;
2179
Takashi Iwai9322ca52012-02-03 14:28:01 +01002180 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002181 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002182 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002183 return 0;
2184 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002185 kctl = snd_ctl_make_virtual_master(name, tlv);
2186 if (!kctl)
2187 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002188 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002189 if (err < 0)
2190 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002191
Takashi Iwaie8750942014-06-30 14:02:39 +02002192 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002193 if (err < 0)
2194 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002195
2196 /* init with master mute & zero volume */
2197 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002198 if (init_slave_vol) {
2199 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002200 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002201 tlv ? init_slave_0dB : init_slave_unmute, &step);
2202 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002203
Takashi Iwai29e58532012-03-12 12:25:03 +01002204 if (ctl_ret)
2205 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002206 return 0;
2207}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002208EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002209
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002210/*
2211 * mute-LED control using vmaster
2212 */
2213static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_info *uinfo)
2215{
2216 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002217 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002218 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002219
Takashi Iwai3ff72212014-10-20 18:17:28 +02002220 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002221}
2222
2223static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2224 struct snd_ctl_elem_value *ucontrol)
2225{
2226 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2227 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2228 return 0;
2229}
2230
2231static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2235 unsigned int old_mode = hook->mute_mode;
2236
2237 hook->mute_mode = ucontrol->value.enumerated.item[0];
2238 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2239 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2240 if (old_mode == hook->mute_mode)
2241 return 0;
2242 snd_hda_sync_vmaster_hook(hook);
2243 return 1;
2244}
2245
2246static struct snd_kcontrol_new vmaster_mute_mode = {
2247 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2248 .name = "Mute-LED Mode",
2249 .info = vmaster_mute_mode_info,
2250 .get = vmaster_mute_mode_get,
2251 .put = vmaster_mute_mode_put,
2252};
2253
Takashi Iwai95a962c2014-10-29 16:03:58 +01002254/**
2255 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2256 * @codec: the HDA codec
2257 * @hook: the vmaster hook object
2258 * @expose_enum_ctl: flag to create an enum ctl
2259 *
2260 * Add a mute-LED hook with the given vmaster switch kctl.
2261 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2262 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002263 */
2264int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002265 struct hda_vmaster_mute_hook *hook,
2266 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002267{
2268 struct snd_kcontrol *kctl;
2269
2270 if (!hook->hook || !hook->sw_kctl)
2271 return 0;
2272 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2273 hook->codec = codec;
2274 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002275 if (!expose_enum_ctl)
2276 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002277 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2278 if (!kctl)
2279 return -ENOMEM;
2280 return snd_hda_ctl_add(codec, 0, kctl);
2281}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002282EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002283
Takashi Iwai95a962c2014-10-29 16:03:58 +01002284/**
2285 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2286 * @hook: the vmaster hook
2287 *
2288 * Call the hook with the current value for synchronization.
2289 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002290 */
2291void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2292{
2293 if (!hook->hook || !hook->codec)
2294 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002295 /* don't call vmaster hook in the destructor since it might have
2296 * been already destroyed
2297 */
2298 if (hook->codec->bus->shutdown)
2299 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002300 switch (hook->mute_mode) {
2301 case HDA_VMUTE_FOLLOW_MASTER:
2302 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2303 break;
2304 default:
2305 hook->hook(hook->codec, hook->mute_mode);
2306 break;
2307 }
2308}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002309EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002310
2311
Takashi Iwaid5191e52009-11-16 14:58:17 +01002312/**
2313 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002314 * @kcontrol: referred ctl element
2315 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002316 *
2317 * The control element is supposed to have the private_value field
2318 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2319 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002320int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 int chs = get_amp_channels(kcontrol);
2324
2325 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2326 uinfo->count = chs == 3 ? 2 : 1;
2327 uinfo->value.integer.min = 0;
2328 uinfo->value.integer.max = 1;
2329 return 0;
2330}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002331EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Takashi Iwaid5191e52009-11-16 14:58:17 +01002333/**
2334 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002335 * @kcontrol: ctl element
2336 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002337 *
2338 * The control element is supposed to have the private_value field
2339 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2340 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002341int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2342 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2345 hda_nid_t nid = get_amp_nid(kcontrol);
2346 int chs = get_amp_channels(kcontrol);
2347 int dir = get_amp_direction(kcontrol);
2348 int idx = get_amp_index(kcontrol);
2349 long *valp = ucontrol->value.integer.value;
2350
2351 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002352 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002353 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002355 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002356 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return 0;
2358}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002359EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Takashi Iwaid5191e52009-11-16 14:58:17 +01002361/**
2362 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002363 * @kcontrol: ctl element
2364 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002365 *
2366 * The control element is supposed to have the private_value field
2367 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2368 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002369int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 hda_nid_t nid = get_amp_nid(kcontrol);
2374 int chs = get_amp_channels(kcontrol);
2375 int dir = get_amp_direction(kcontrol);
2376 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 long *valp = ucontrol->value.integer.value;
2378 int change = 0;
2379
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002380 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002381 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2382 HDA_AMP_MUTE,
2383 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002384 valp++;
2385 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002386 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002387 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2388 HDA_AMP_MUTE,
2389 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002390 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return change;
2392}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002393EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395/*
Takashi Iwai985be542005-11-02 18:26:49 +01002396 * bound volume controls
2397 *
2398 * bind multiple volumes (# indices, from 0)
2399 */
2400
2401#define AMP_VAL_IDX_SHIFT 19
2402#define AMP_VAL_IDX_MASK (0x0f<<19)
2403
Takashi Iwaid5191e52009-11-16 14:58:17 +01002404/**
2405 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002406 * @kcontrol: ctl element
2407 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002408 *
2409 * The control element is supposed to have the private_value field
2410 * set up via HDA_BIND_MUTE*() macros.
2411 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002412int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002414{
2415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2416 unsigned long pval;
2417 int err;
2418
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002419 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002420 pval = kcontrol->private_value;
2421 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2422 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2423 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002424 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002425 return err;
2426}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002427EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002428
Takashi Iwaid5191e52009-11-16 14:58:17 +01002429/**
2430 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002431 * @kcontrol: ctl element
2432 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002433 *
2434 * The control element is supposed to have the private_value field
2435 * set up via HDA_BIND_MUTE*() macros.
2436 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002437int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002439{
2440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2441 unsigned long pval;
2442 int i, indices, err = 0, change = 0;
2443
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002444 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002445 pval = kcontrol->private_value;
2446 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2447 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2449 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002450 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2451 if (err < 0)
2452 break;
2453 change |= err;
2454 }
2455 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002456 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002457 return err < 0 ? err : change;
2458}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002459EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002460
Takashi Iwaid5191e52009-11-16 14:58:17 +01002461/**
2462 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002463 * @kcontrol: referred ctl element
2464 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002465 *
2466 * The control element is supposed to have the private_value field
2467 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002468 */
2469int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_info *uinfo)
2471{
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 struct hda_bind_ctls *c;
2474 int err;
2475
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002476 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002477 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002478 kcontrol->private_value = *c->values;
2479 err = c->ops->info(kcontrol, uinfo);
2480 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002481 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002482 return err;
2483}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002484EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002485
Takashi Iwaid5191e52009-11-16 14:58:17 +01002486/**
2487 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002488 * @kcontrol: ctl element
2489 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002490 *
2491 * The control element is supposed to have the private_value field
2492 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2493 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002494int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2496{
2497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2498 struct hda_bind_ctls *c;
2499 int err;
2500
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002501 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002502 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002503 kcontrol->private_value = *c->values;
2504 err = c->ops->get(kcontrol, ucontrol);
2505 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002506 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002507 return err;
2508}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002509EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002510
Takashi Iwaid5191e52009-11-16 14:58:17 +01002511/**
2512 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002513 * @kcontrol: ctl element
2514 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002515 *
2516 * The control element is supposed to have the private_value field
2517 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2518 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002519int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
2521{
2522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2523 struct hda_bind_ctls *c;
2524 unsigned long *vals;
2525 int err = 0, change = 0;
2526
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002527 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002528 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002529 for (vals = c->values; *vals; vals++) {
2530 kcontrol->private_value = *vals;
2531 err = c->ops->put(kcontrol, ucontrol);
2532 if (err < 0)
2533 break;
2534 change |= err;
2535 }
2536 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002537 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002538 return err < 0 ? err : change;
2539}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002540EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002541
Takashi Iwaid5191e52009-11-16 14:58:17 +01002542/**
2543 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002544 * @kcontrol: ctl element
2545 * @op_flag: operation flag
2546 * @size: byte size of input TLV
2547 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002548 *
2549 * The control element is supposed to have the private_value field
2550 * set up via HDA_BIND_VOL() macro.
2551 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002552int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2553 unsigned int size, unsigned int __user *tlv)
2554{
2555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2556 struct hda_bind_ctls *c;
2557 int err;
2558
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002559 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002560 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002561 kcontrol->private_value = *c->values;
2562 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2563 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002564 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002565 return err;
2566}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002567EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002568
2569struct hda_ctl_ops snd_hda_bind_vol = {
2570 .info = snd_hda_mixer_amp_volume_info,
2571 .get = snd_hda_mixer_amp_volume_get,
2572 .put = snd_hda_mixer_amp_volume_put,
2573 .tlv = snd_hda_mixer_amp_tlv
2574};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002575EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002576
2577struct hda_ctl_ops snd_hda_bind_sw = {
2578 .info = snd_hda_mixer_amp_switch_info,
2579 .get = snd_hda_mixer_amp_switch_get,
2580 .put = snd_hda_mixer_amp_switch_put,
2581 .tlv = snd_hda_mixer_amp_tlv
2582};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002583EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002584
2585/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 * SPDIF out controls
2587 */
2588
Takashi Iwai0ba21762007-04-16 11:29:14 +02002589static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2593 uinfo->count = 1;
2594 return 0;
2595}
2596
Takashi Iwai0ba21762007-04-16 11:29:14 +02002597static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2601 IEC958_AES0_NONAUDIO |
2602 IEC958_AES0_CON_EMPHASIS_5015 |
2603 IEC958_AES0_CON_NOT_COPYRIGHT;
2604 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2605 IEC958_AES1_CON_ORIGINAL;
2606 return 0;
2607}
2608
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2613 IEC958_AES0_NONAUDIO |
2614 IEC958_AES0_PRO_EMPHASIS_5015;
2615 return 0;
2616}
2617
Takashi Iwai0ba21762007-04-16 11:29:14 +02002618static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2619 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
2621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002622 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002623 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002625 mutex_lock(&codec->spdif_mutex);
2626 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002627 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2628 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2629 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2630 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002631 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 return 0;
2634}
2635
2636/* convert from SPDIF status bits to HDA SPDIF bits
2637 * bit 0 (DigEn) is always set zero (to be filled later)
2638 */
2639static unsigned short convert_from_spdif_status(unsigned int sbits)
2640{
2641 unsigned short val = 0;
2642
2643 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002644 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2649 IEC958_AES0_PRO_EMPHASIS_5015)
2650 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002652 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2653 IEC958_AES0_CON_EMPHASIS_5015)
2654 val |= AC_DIG1_EMPHASIS;
2655 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2656 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002658 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2660 }
2661 return val;
2662}
2663
2664/* convert to SPDIF status bits from HDA SPDIF bits
2665 */
2666static unsigned int convert_to_spdif_status(unsigned short val)
2667{
2668 unsigned int sbits = 0;
2669
Takashi Iwai0ba21762007-04-16 11:29:14 +02002670 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 sbits |= IEC958_AES0_PROFESSIONAL;
2674 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002675 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2677 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002678 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002680 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2684 sbits |= val & (0x7f << 8);
2685 }
2686 return sbits;
2687}
2688
Takashi Iwai2f728532008-09-25 16:32:41 +02002689/* set digital convert verbs both for the given NID and its slaves */
2690static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2691 int verb, int val)
2692{
Takashi Iwaidda14412011-05-02 11:29:30 +02002693 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002694
Takashi Iwai9e976972008-11-25 08:17:20 +01002695 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002696 d = codec->slave_dig_outs;
2697 if (!d)
2698 return;
2699 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002700 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002701}
2702
2703static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2704 int dig1, int dig2)
2705{
2706 if (dig1 != -1)
2707 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2708 if (dig2 != -1)
2709 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2710}
2711
Takashi Iwai0ba21762007-04-16 11:29:14 +02002712static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2713 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002716 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002717 struct hda_spdif_out *spdif;
2718 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 unsigned short val;
2720 int change;
2721
Ingo Molnar62932df2006-01-16 16:34:20 +01002722 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002723 spdif = snd_array_elem(&codec->spdif_out, idx);
2724 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002725 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2727 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2728 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002729 val = convert_from_spdif_status(spdif->status);
2730 val |= spdif->ctls & 1;
2731 change = spdif->ctls != val;
2732 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002733 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002734 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002735 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return change;
2737}
2738
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002739#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Takashi Iwai0ba21762007-04-16 11:29:14 +02002741static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002745 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002746 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002748 mutex_lock(&codec->spdif_mutex);
2749 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002750 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002751 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return 0;
2753}
2754
Stephen Warren74b654c2011-06-01 11:14:18 -06002755static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2756 int dig1, int dig2)
2757{
2758 set_dig_out_convert(codec, nid, dig1, dig2);
2759 /* unmute amp switch (if any) */
2760 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2761 (dig1 & AC_DIG1_ENABLE))
2762 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2763 HDA_AMP_MUTE, 0);
2764}
2765
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002770 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002771 struct hda_spdif_out *spdif;
2772 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 unsigned short val;
2774 int change;
2775
Ingo Molnar62932df2006-01-16 16:34:20 +01002776 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002777 spdif = snd_array_elem(&codec->spdif_out, idx);
2778 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002779 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002781 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002782 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002783 spdif->ctls = val;
2784 if (change && nid != (u16)-1)
2785 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002786 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return change;
2788}
2789
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002790static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 {
2792 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2793 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002794 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .info = snd_hda_spdif_mask_info,
2796 .get = snd_hda_spdif_cmask_get,
2797 },
2798 {
2799 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2800 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002801 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 .info = snd_hda_spdif_mask_info,
2803 .get = snd_hda_spdif_pmask_get,
2804 },
2805 {
2806 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002807 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .info = snd_hda_spdif_mask_info,
2809 .get = snd_hda_spdif_default_get,
2810 .put = snd_hda_spdif_default_put,
2811 },
2812 {
2813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002814 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 .info = snd_hda_spdif_out_switch_info,
2816 .get = snd_hda_spdif_out_switch_get,
2817 .put = snd_hda_spdif_out_switch_put,
2818 },
2819 { } /* end */
2820};
2821
2822/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002823 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002825 * @associated_nid: NID that new ctls associated with
2826 * @cvt_nid: converter NID
2827 * @type: HDA_PCM_TYPE_*
2828 * Creates controls related with the digital output.
2829 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 *
2831 * Returns 0 if successful, or a negative error code.
2832 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002833int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2834 hda_nid_t associated_nid,
2835 hda_nid_t cvt_nid,
2836 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002839 struct snd_kcontrol *kctl;
2840 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002841 int idx = 0;
2842 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002843 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002844 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002846 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002847 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002848 idx = spdif_index;
2849 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002850 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002851 /* suppose a single SPDIF device */
2852 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2853 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2854 if (!kctl)
2855 break;
2856 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002857 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002858 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002859 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002860 if (!bus->primary_dig_out_type)
2861 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002862
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002863 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002864 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002865 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002866 return -EBUSY;
2867 }
Stephen Warren7c935972011-06-01 11:14:17 -06002868 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002869 if (!spdif)
2870 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2872 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002873 if (!kctl)
2874 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002875 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002876 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002877 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return err;
2880 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002881 spdif->nid = cvt_nid;
2882 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06002883 AC_VERB_GET_DIGI_CONVERT_1, 0);
2884 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return 0;
2886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002887EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Takashi Iwai95a962c2014-10-29 16:03:58 +01002889/**
2890 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2891 * @codec: the HDA codec
2892 * @nid: widget NID
2893 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002894 * call within spdif_mutex lock
2895 */
Stephen Warren7c935972011-06-01 11:14:17 -06002896struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2897 hda_nid_t nid)
2898{
2899 int i;
2900 for (i = 0; i < codec->spdif_out.used; i++) {
2901 struct hda_spdif_out *spdif =
2902 snd_array_elem(&codec->spdif_out, i);
2903 if (spdif->nid == nid)
2904 return spdif;
2905 }
2906 return NULL;
2907}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002908EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002909
Takashi Iwai95a962c2014-10-29 16:03:58 +01002910/**
2911 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2912 * @codec: the HDA codec
2913 * @idx: the SPDIF ctl index
2914 *
2915 * Unassign the widget from the given SPDIF control.
2916 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002917void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2918{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002919 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002920
2921 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002922 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002923 spdif->nid = (u16)-1;
2924 mutex_unlock(&codec->spdif_mutex);
2925}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002926EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002927
Takashi Iwai95a962c2014-10-29 16:03:58 +01002928/**
2929 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2930 * @codec: the HDA codec
2931 * @idx: the SPDIF ctl idx
2932 * @nid: widget NID
2933 *
2934 * Assign the widget to the SPDIF control with the given index.
2935 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002936void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2937{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002938 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002939 unsigned short val;
2940
2941 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002942 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002943 if (spdif->nid != nid) {
2944 spdif->nid = nid;
2945 val = spdif->ctls;
2946 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2947 }
2948 mutex_unlock(&codec->spdif_mutex);
2949}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002950EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002953 * SPDIF sharing with analog output
2954 */
2955static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2957{
2958 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2959 ucontrol->value.integer.value[0] = mout->share_spdif;
2960 return 0;
2961}
2962
2963static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
2965{
2966 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2967 mout->share_spdif = !!ucontrol->value.integer.value[0];
2968 return 0;
2969}
2970
2971static struct snd_kcontrol_new spdif_share_sw = {
2972 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2973 .name = "IEC958 Default PCM Playback Switch",
2974 .info = snd_ctl_boolean_mono_info,
2975 .get = spdif_share_sw_get,
2976 .put = spdif_share_sw_put,
2977};
2978
Takashi Iwaid5191e52009-11-16 14:58:17 +01002979/**
2980 * snd_hda_create_spdif_share_sw - create Default PCM switch
2981 * @codec: the HDA codec
2982 * @mout: multi-out instance
2983 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002984int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2985 struct hda_multi_out *mout)
2986{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002987 struct snd_kcontrol *kctl;
2988
Takashi Iwai9a081602008-02-12 18:37:26 +01002989 if (!mout->dig_out_nid)
2990 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002991
2992 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2993 if (!kctl)
2994 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002995 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002996 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002997}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002998EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002999
3000/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 * SPDIF input
3002 */
3003
3004#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3005
Takashi Iwai0ba21762007-04-16 11:29:14 +02003006static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3007 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
3009 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3010
3011 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3012 return 0;
3013}
3014
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3019 hda_nid_t nid = kcontrol->private_value;
3020 unsigned int val = !!ucontrol->value.integer.value[0];
3021 int change;
3022
Ingo Molnar62932df2006-01-16 16:34:20 +01003023 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003025 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003027 snd_hda_codec_write_cache(codec, nid, 0,
3028 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003030 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return change;
3032}
3033
Takashi Iwai0ba21762007-04-16 11:29:14 +02003034static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3038 hda_nid_t nid = kcontrol->private_value;
3039 unsigned short val;
3040 unsigned int sbits;
3041
Andrew Paprocki3982d172007-12-19 12:13:44 +01003042 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 sbits = convert_to_spdif_status(val);
3044 ucontrol->value.iec958.status[0] = sbits;
3045 ucontrol->value.iec958.status[1] = sbits >> 8;
3046 ucontrol->value.iec958.status[2] = sbits >> 16;
3047 ucontrol->value.iec958.status[3] = sbits >> 24;
3048 return 0;
3049}
3050
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003051static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 {
3053 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003054 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 .info = snd_hda_spdif_in_switch_info,
3056 .get = snd_hda_spdif_in_switch_get,
3057 .put = snd_hda_spdif_in_switch_put,
3058 },
3059 {
3060 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3061 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003062 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .info = snd_hda_spdif_mask_info,
3064 .get = snd_hda_spdif_in_status_get,
3065 },
3066 { } /* end */
3067};
3068
3069/**
3070 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3071 * @codec: the HDA codec
3072 * @nid: audio in widget NID
3073 *
3074 * Creates controls related with the SPDIF input.
3075 * Called from each patch supporting the SPDIF in.
3076 *
3077 * Returns 0 if successful, or a negative error code.
3078 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003079int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003082 struct snd_kcontrol *kctl;
3083 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003084 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Takashi Iwaidcda5802012-10-12 17:24:51 +02003086 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003087 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003088 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003089 return -EBUSY;
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3092 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003093 if (!kctl)
3094 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003096 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003097 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return err;
3099 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003101 snd_hda_codec_read(codec, nid, 0,
3102 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return 0;
3105}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003106EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003108/*
3109 * command cache
3110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Takashi Iwaic370dd62012-12-13 18:30:04 +01003112/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003113#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3114#define get_cmd_cache_nid(key) ((key) & 0xff)
3115#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3116
3117/**
3118 * snd_hda_codec_write_cache - send a single command with caching
3119 * @codec: the HDA codec
3120 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003121 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003122 * @verb: the verb to send
3123 * @parm: the parameter for the verb
3124 *
3125 * Send a single command without waiting for response.
3126 *
3127 * Returns 0 if successful, or a negative error code.
3128 */
3129int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003130 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003131{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003132 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003133 struct hda_cache_head *c;
3134 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003135 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003136
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003137 cache_only = codec->cached_write;
3138 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003139 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003140 if (err < 0)
3141 return err;
3142 }
3143
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003144 /* parm may contain the verb stuff for get/set amp */
3145 verb = verb | (parm >> 8);
3146 parm &= 0xff;
3147 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003148 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003149 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003150 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003151 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003152 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003153 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003154 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003155 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003156}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003157EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003158
Takashi Iwaid5191e52009-11-16 14:58:17 +01003159/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003160 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3161 * @codec: the HDA codec
3162 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003163 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003164 * @verb: the verb to send
3165 * @parm: the parameter for the verb
3166 *
3167 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3168 * command if the parameter is already identical with the cached value.
3169 * If not, it sends the command and refreshes the cache.
3170 *
3171 * Returns 0 if successful, or a negative error code.
3172 */
3173int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003174 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003175{
3176 struct hda_cache_head *c;
3177 u32 key;
3178
3179 /* parm may contain the verb stuff for get/set amp */
3180 verb = verb | (parm >> 8);
3181 parm &= 0xff;
3182 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003183 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003184 c = get_hash(&codec->cmd_cache, key);
3185 if (c && c->val == parm) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003186 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003187 return 0;
3188 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003189 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003190 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003191}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003192EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003193
3194/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003195 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3196 * @codec: HD-audio codec
3197 *
3198 * Execute all verbs recorded in the command caches to resume.
3199 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003200void snd_hda_codec_resume_cache(struct hda_codec *codec)
3201{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003202 int i;
3203
Takashi Iwaic370dd62012-12-13 18:30:04 +01003204 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003205 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003206 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3207 struct hda_cache_head *buffer;
3208 u32 key;
3209
3210 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3211 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003212 if (!key)
3213 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003214 if (!buffer->dirty)
3215 continue;
3216 buffer->dirty = 0;
3217 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003218 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3219 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003220 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003221 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003222 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003223}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003224EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003225
3226/**
3227 * snd_hda_sequence_write_cache - sequence writes with caching
3228 * @codec: the HDA codec
3229 * @seq: VERB array to send
3230 *
3231 * Send the commands sequentially from the given array.
3232 * Thte commands are recorded on cache for power-save and resume.
3233 * The array must be terminated with NID=0.
3234 */
3235void snd_hda_sequence_write_cache(struct hda_codec *codec,
3236 const struct hda_verb *seq)
3237{
3238 for (; seq->nid; seq++)
3239 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3240 seq->param);
3241}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003242EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003243
Takashi Iwaidc870f32013-01-22 15:24:30 +01003244/**
3245 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3246 * @codec: HD-audio codec
3247 */
3248void snd_hda_codec_flush_cache(struct hda_codec *codec)
3249{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003250 if (codec->core.regmap)
3251 regcache_sync(codec->core.regmap);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003252 snd_hda_codec_resume_cache(codec);
3253}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003254EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003255
Takashi Iwai95a962c2014-10-29 16:03:58 +01003256/**
3257 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3258 * @codec: the HDA codec
3259 * @fg: function group (not used now)
3260 * @power_state: the power state to set (AC_PWRST_*)
3261 *
3262 * Set the given power state to all widgets that have the power control.
3263 * If the codec has power_filter set, it evaluates the power state and
3264 * filter out if it's unchanged as D3.
3265 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003266void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003267 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003268{
Takashi Iwai7639a062015-03-03 10:07:24 +01003269 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01003270
Takashi Iwai7639a062015-03-03 10:07:24 +01003271 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003272 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003273 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003274 if (!(wcaps & AC_WCAP_POWER))
3275 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003276 if (codec->power_filter) {
3277 state = codec->power_filter(codec, nid, power_state);
3278 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003279 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003280 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003281 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003282 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003283 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003284}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003285EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003286
3287/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003288 * wait until the state is reached, returns the current state
3289 */
3290static unsigned int hda_sync_power_state(struct hda_codec *codec,
3291 hda_nid_t fg,
3292 unsigned int power_state)
3293{
3294 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3295 unsigned int state, actual_state;
3296
3297 for (;;) {
3298 state = snd_hda_codec_read(codec, fg, 0,
3299 AC_VERB_GET_POWER_STATE, 0);
3300 if (state & AC_PWRST_ERROR)
3301 break;
3302 actual_state = (state >> 4) & 0x0f;
3303 if (actual_state == power_state)
3304 break;
3305 if (time_after_eq(jiffies, end_time))
3306 break;
3307 /* wait until the codec reachs to the target state */
3308 msleep(1);
3309 }
3310 return state;
3311}
3312
Takashi Iwai95a962c2014-10-29 16:03:58 +01003313/**
3314 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3315 * @codec: the HDA codec
3316 * @nid: widget NID
3317 * @power_state: power state to evalue
3318 *
3319 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3320 * This can be used a codec power_filter callback.
3321 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003322unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3323 hda_nid_t nid,
3324 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003325{
Takashi Iwai7639a062015-03-03 10:07:24 +01003326 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003327 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003328 if (power_state == AC_PWRST_D3 &&
3329 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3330 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3331 int eapd = snd_hda_codec_read(codec, nid, 0,
3332 AC_VERB_GET_EAPD_BTLENABLE, 0);
3333 if (eapd & 0x02)
3334 return AC_PWRST_D0;
3335 }
3336 return power_state;
3337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003338EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003339
Takashi Iwai432c6412012-08-28 09:59:20 -07003340/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003341 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003342 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003343static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003344 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003345{
Takashi Iwai7639a062015-03-03 10:07:24 +01003346 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003347 int count;
3348 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003349 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003350
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003351 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003352 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003353 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003354 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003355 else if (codec->depop_delay > 0)
3356 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003357 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003358 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003359
3360 /* repeat power states setting at most 10 times*/
3361 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003362 if (codec->patch_ops.set_power_state)
3363 codec->patch_ops.set_power_state(codec, fg,
3364 power_state);
3365 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003366 state = power_state;
3367 if (codec->power_filter)
3368 state = codec->power_filter(codec, fg, state);
3369 if (state == power_state || power_state != AC_PWRST_D3)
3370 snd_hda_codec_read(codec, fg, flags,
3371 AC_VERB_SET_POWER_STATE,
3372 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003373 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003374 }
3375 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003376 if (!(state & AC_PWRST_ERROR))
3377 break;
3378 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003379
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003380 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003381}
Takashi Iwai54d17402005-11-21 16:33:22 +01003382
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003383/* sync power states of all widgets;
3384 * this is called at the end of codec parsing
3385 */
3386static void sync_power_up_states(struct hda_codec *codec)
3387{
Takashi Iwai7639a062015-03-03 10:07:24 +01003388 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003389
Takashi Iwaiba615b82013-03-13 14:47:21 +01003390 /* don't care if no filter is used */
3391 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003392 return;
3393
Takashi Iwai7639a062015-03-03 10:07:24 +01003394 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003395 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003396 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003397 if (!(wcaps & AC_WCAP_POWER))
3398 continue;
3399 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3400 if (target == AC_PWRST_D0)
3401 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003402 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003403 snd_hda_codec_write(codec, nid, 0,
3404 AC_VERB_SET_POWER_STATE, target);
3405 }
3406}
3407
Takashi Iwai648a8d22014-02-25 10:38:13 +01003408#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003409/* execute additional init verbs */
3410static void hda_exec_init_verbs(struct hda_codec *codec)
3411{
3412 if (codec->init_verbs.list)
3413 snd_hda_sequence_write(codec, codec->init_verbs.list);
3414}
3415#else
3416static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3417#endif
3418
Takashi Iwai2a439522011-07-26 09:52:50 +02003419#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003420/* update the power on/off account with the current jiffies */
3421static void update_power_acct(struct hda_codec *codec, bool on)
3422{
3423 unsigned long delta = jiffies - codec->power_jiffies;
3424
3425 if (on)
3426 codec->power_on_acct += delta;
3427 else
3428 codec->power_off_acct += delta;
3429 codec->power_jiffies += delta;
3430}
3431
3432void snd_hda_update_power_acct(struct hda_codec *codec)
3433{
3434 update_power_acct(codec, hda_codec_is_power_on(codec));
3435}
3436
Takashi Iwaicb53c622007-08-10 17:21:45 +02003437/*
3438 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003439 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003440 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003441static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003442{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003443 unsigned int state;
3444
Takashi Iwai7639a062015-03-03 10:07:24 +01003445 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003446
Takashi Iwaicb53c622007-08-10 17:21:45 +02003447 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003448 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003449 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003450 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003451 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003452 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003453 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003454}
3455
Takashi Iwaic370dd62012-12-13 18:30:04 +01003456/* mark all entries of cmd and amp caches dirty */
3457static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3458{
3459 int i;
3460 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3461 struct hda_cache_head *cmd;
3462 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3463 cmd->dirty = 1;
3464 }
3465 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3466 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003467 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003468 amp->head.dirty = 1;
3469 }
3470}
3471
Takashi Iwaicb53c622007-08-10 17:21:45 +02003472/*
3473 * kick up codec; used both from PM and power-save
3474 */
3475static void hda_call_codec_resume(struct hda_codec *codec)
3476{
Takashi Iwai7639a062015-03-03 10:07:24 +01003477 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003478
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003479 if (codec->core.regmap)
3480 regcache_mark_dirty(codec->core.regmap);
3481
Takashi Iwaic370dd62012-12-13 18:30:04 +01003482 hda_mark_cmd_cache_dirty(codec);
3483
Takashi Iwaicc72da72015-02-19 16:00:22 +01003484 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003485
Takashi Iwaid8193872012-08-31 07:54:38 -07003486 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003487 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003488 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003489 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003490 if (codec->patch_ops.resume)
3491 codec->patch_ops.resume(codec);
3492 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003493 if (codec->patch_ops.init)
3494 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003495 if (codec->core.regmap)
3496 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003497 snd_hda_codec_resume_cache(codec);
3498 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003499
3500 if (codec->jackpoll_interval)
3501 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003502 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003503 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003504 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003505}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003506
Takashi Iwaicc72da72015-02-19 16:00:22 +01003507static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003508{
3509 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003510 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003511 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003512
3513 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003514 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3515 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003516 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003517 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3518 (state & AC_PWRST_CLK_STOP_OK))
3519 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003520 return 0;
3521}
3522
Takashi Iwaicc72da72015-02-19 16:00:22 +01003523static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003524{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003525 struct hda_codec *codec = dev_to_hda_codec(dev);
3526
Takashi Iwai7639a062015-03-03 10:07:24 +01003527 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003528 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003529 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003530 return 0;
3531}
Takashi Iwai2a439522011-07-26 09:52:50 +02003532#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003533
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003534/* referred in hda_bind.c */
3535const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003536 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3537 pm_runtime_force_resume)
3538 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3539 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003540};
Takashi Iwai54d17402005-11-21 16:33:22 +01003541
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003542/*
3543 * add standard channel maps if not specified
3544 */
3545static int add_std_chmaps(struct hda_codec *codec)
3546{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003547 struct hda_pcm *pcm;
3548 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003549
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003550 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003551 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003552 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003553 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003554 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003555
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003556 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003557 continue;
3558 if (!pcm || !hinfo->substreams)
3559 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003560 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003561 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003562 hinfo->channels_max,
3563 0, &chmap);
3564 if (err < 0)
3565 return err;
3566 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3567 }
3568 }
3569 return 0;
3570}
3571
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003572/* default channel maps for 2.1 speakers;
3573 * since HD-audio supports only stereo, odd number channels are omitted
3574 */
3575const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3576 { .channels = 2,
3577 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3578 { .channels = 4,
3579 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3580 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3581 { }
3582};
3583EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3584
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003585int snd_hda_codec_build_controls(struct hda_codec *codec)
3586{
3587 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003588 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003589 /* continue to initialize... */
3590 if (codec->patch_ops.init)
3591 err = codec->patch_ops.init(codec);
3592 if (!err && codec->patch_ops.build_controls)
3593 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003594 if (err < 0)
3595 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003596
3597 /* we create chmaps here instead of build_pcms */
3598 err = add_std_chmaps(codec);
3599 if (err < 0)
3600 return err;
3601
David Henningsson26a6cb62012-10-09 15:04:21 +02003602 if (codec->jackpoll_interval)
3603 hda_jackpoll_work(&codec->jackpoll_work.work);
3604 else
3605 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003606 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return 0;
3608}
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/*
3611 * stream formats
3612 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003613struct hda_rate_tbl {
3614 unsigned int hz;
3615 unsigned int alsa_bits;
3616 unsigned int hda_fmt;
3617};
3618
Takashi Iwai92f10b32010-08-03 14:21:00 +02003619/* rate = base * mult / div */
3620#define HDA_RATE(base, mult, div) \
3621 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3622 (((div) - 1) << AC_FMT_DIV_SHIFT))
3623
Takashi Iwaibefdf312005-08-22 13:57:55 +02003624static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003626
3627 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003628 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3629 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3630 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3631 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3632 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3633 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3634 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3635 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3636 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3637 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3638 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003639#define AC_PAR_PCM_RATE_BITS 11
3640 /* up to bits 10, 384kHZ isn't supported properly */
3641
3642 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003643 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003644
Takashi Iwaibefdf312005-08-22 13:57:55 +02003645 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646};
3647
3648/**
3649 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003650 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 * @rate: the sample rate
3652 * @channels: the number of channels
3653 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3654 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003655 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 *
3657 * Calculate the format bitset from the given rate, channels and th PCM format.
3658 *
3659 * Return zero if invalid.
3660 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003661unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3662 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 unsigned int channels,
3664 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003665 unsigned int maxbps,
3666 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
3668 int i;
3669 unsigned int val = 0;
3670
Takashi Iwaibefdf312005-08-22 13:57:55 +02003671 for (i = 0; rate_bits[i].hz; i++)
3672 if (rate_bits[i].hz == rate) {
3673 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 break;
3675 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003676 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003677 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
3679 }
3680
3681 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003682 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return 0;
3684 }
3685 val |= channels - 1;
3686
3687 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003688 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003689 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003690 break;
3691 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003692 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 case 20:
3695 case 24:
3696 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003697 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003698 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003700 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003702 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 break;
3704 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003705 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003706 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return 0;
3708 }
3709
Anssi Hannula32c168c2010-08-03 13:28:57 +03003710 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003711 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return val;
3714}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003715EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003717static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3718 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003719{
3720 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003721 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003722 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3723 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3724 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003725 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003726 if (!val || val == -1)
3727 return 0;
3728 return val;
3729}
3730
3731static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3732{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003733 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003734 get_pcm_param);
3735}
3736
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003737static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3738 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003739{
3740 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3741 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003742 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003743 if (!streams || streams == -1)
3744 return 0;
3745 return streams;
3746}
3747
3748static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3749{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003750 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003751 get_stream_param);
3752}
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754/**
3755 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3756 * @codec: the HDA codec
3757 * @nid: NID to query
3758 * @ratesp: the pointer to store the detected rate bitflags
3759 * @formatsp: the pointer to store the detected formats
3760 * @bpsp: the pointer to store the detected format widths
3761 *
3762 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3763 * or @bsps argument is ignored.
3764 *
3765 * Returns 0 if successful, otherwise a negative error code.
3766 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003767int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3769{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003770 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003772 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003773 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 if (ratesp) {
3776 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003777 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003779 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003781 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003782 codec_err(codec,
3783 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3784 nid, val,
3785 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003786 return -EIO;
3787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 *ratesp = rates;
3789 }
3790
3791 if (formatsp || bpsp) {
3792 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003793 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003795 streams = query_stream_param(codec, nid);
3796 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 bps = 0;
3800 if (streams & AC_SUPFMT_PCM) {
3801 if (val & AC_SUPPCM_BITS_8) {
3802 formats |= SNDRV_PCM_FMTBIT_U8;
3803 bps = 8;
3804 }
3805 if (val & AC_SUPPCM_BITS_16) {
3806 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3807 bps = 16;
3808 }
3809 if (wcaps & AC_WCAP_DIGITAL) {
3810 if (val & AC_SUPPCM_BITS_32)
3811 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3812 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3813 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3814 if (val & AC_SUPPCM_BITS_24)
3815 bps = 24;
3816 else if (val & AC_SUPPCM_BITS_20)
3817 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003818 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3819 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3821 if (val & AC_SUPPCM_BITS_32)
3822 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 else if (val & AC_SUPPCM_BITS_24)
3824 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003825 else if (val & AC_SUPPCM_BITS_20)
3826 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003829#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003830 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003832 if (!bps)
3833 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003834 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003835#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003836 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003837 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 /* temporary hack: we have still no proper support
3839 * for the direct AC3 stream...
3840 */
3841 formats |= SNDRV_PCM_FMTBIT_U8;
3842 bps = 8;
3843 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003844 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003845 codec_err(codec,
3846 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3847 nid, val,
3848 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3849 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003850 return -EIO;
3851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 if (formatsp)
3853 *formatsp = formats;
3854 if (bpsp)
3855 *bpsp = bps;
3856 }
3857
3858 return 0;
3859}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003860EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003863 * snd_hda_is_supported_format - Check the validity of the format
3864 * @codec: HD-audio codec
3865 * @nid: NID to check
3866 * @format: the HD-audio format value to check
3867 *
3868 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 *
3870 * Returns 1 if supported, 0 if not.
3871 */
3872int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3873 unsigned int format)
3874{
3875 int i;
3876 unsigned int val = 0, rate, stream;
3877
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003878 val = query_pcm_param(codec, nid);
3879 if (!val)
3880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003883 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003884 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 if (val & (1 << i))
3886 break;
3887 return 0;
3888 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003889 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return 0;
3891
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003892 stream = query_stream_param(codec, nid);
3893 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return 0;
3895
3896 if (stream & AC_SUPFMT_PCM) {
3897 switch (format & 0xf0) {
3898 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003899 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return 0;
3901 break;
3902 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003903 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return 0;
3905 break;
3906 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003907 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 return 0;
3909 break;
3910 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003911 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return 0;
3913 break;
3914 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003915 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return 0;
3917 break;
3918 default:
3919 return 0;
3920 }
3921 } else {
3922 /* FIXME: check for float32 and AC3? */
3923 }
3924
3925 return 1;
3926}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003927EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929/*
3930 * PCM stuff
3931 */
3932static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3933 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003934 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 return 0;
3937}
3938
3939static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3940 struct hda_codec *codec,
3941 unsigned int stream_tag,
3942 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003943 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3946 return 0;
3947}
3948
3949static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3950 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003951 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
Takashi Iwai888afa12008-03-18 09:57:50 +01003953 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return 0;
3955}
3956
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003957static int set_pcm_default_values(struct hda_codec *codec,
3958 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003960 int err;
3961
Takashi Iwai0ba21762007-04-16 11:29:14 +02003962 /* query support PCM information from the given NID */
3963 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003964 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003965 info->rates ? NULL : &info->rates,
3966 info->formats ? NULL : &info->formats,
3967 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003968 if (err < 0)
3969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971 if (info->ops.open == NULL)
3972 info->ops.open = hda_pcm_default_open_close;
3973 if (info->ops.close == NULL)
3974 info->ops.close = hda_pcm_default_open_close;
3975 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003976 if (snd_BUG_ON(!info->nid))
3977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 info->ops.prepare = hda_pcm_default_prepare;
3979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003981 if (snd_BUG_ON(!info->nid))
3982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 info->ops.cleanup = hda_pcm_default_cleanup;
3984 }
3985 return 0;
3986}
3987
Takashi Iwaieb541332010-08-06 13:48:11 +02003988/*
3989 * codec prepare/cleanup entries
3990 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003991/**
3992 * snd_hda_codec_prepare - Prepare a stream
3993 * @codec: the HDA codec
3994 * @hinfo: PCM information
3995 * @stream: stream tag to assign
3996 * @format: format id to assign
3997 * @substream: PCM substream to assign
3998 *
3999 * Calls the prepare callback set by the codec with the given arguments.
4000 * Clean up the inactive streams when successful.
4001 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004002int snd_hda_codec_prepare(struct hda_codec *codec,
4003 struct hda_pcm_stream *hinfo,
4004 unsigned int stream,
4005 unsigned int format,
4006 struct snd_pcm_substream *substream)
4007{
4008 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004009 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004010 if (hinfo->ops.prepare)
4011 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4012 substream);
4013 else
4014 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004015 if (ret >= 0)
4016 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004017 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004018 return ret;
4019}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004020EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004021
Takashi Iwai95a962c2014-10-29 16:03:58 +01004022/**
4023 * snd_hda_codec_cleanup - Prepare a stream
4024 * @codec: the HDA codec
4025 * @hinfo: PCM information
4026 * @substream: PCM substream
4027 *
4028 * Calls the cleanup callback set by the codec with the given arguments.
4029 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004030void snd_hda_codec_cleanup(struct hda_codec *codec,
4031 struct hda_pcm_stream *hinfo,
4032 struct snd_pcm_substream *substream)
4033{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004034 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004035 if (hinfo->ops.cleanup)
4036 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004037 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004038}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004039EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004040
Takashi Iwaid5191e52009-11-16 14:58:17 +01004041/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004042const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4043 "Audio", "SPDIF", "HDMI", "Modem"
4044};
4045
Takashi Iwai176d5332008-07-30 15:01:44 +02004046/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004047 * get the empty PCM device number to assign
4048 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004049static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004050{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004051 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004052 /* assigned to static slots up to dev#10; if more needed, assign
4053 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4054 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004055 static int audio_idx[HDA_PCM_NTYPES][5] = {
4056 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4057 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004058 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004059 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004060 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004061 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004062
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004063 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004064 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004065 return -EINVAL;
4066 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004067
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004068 for (i = 0; audio_idx[type][i] >= 0; i++) {
4069#ifndef CONFIG_SND_DYNAMIC_MINORS
4070 if (audio_idx[type][i] >= 8)
4071 break;
4072#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004073 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4074 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004075 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004076
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004077#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004078 /* non-fixed slots starting from 10 */
4079 for (i = 10; i < 32; i++) {
4080 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4081 return i;
4082 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004083#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004084
Takashi Iwai4e76a882014-02-25 12:21:03 +01004085 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004086 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004087#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004088 dev_warn(bus->card->dev,
4089 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004090#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004091 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004092}
4093
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004094/* call build_pcms ops of the given codec and set up the default parameters */
4095int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004096{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004097 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004098 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004099
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004100 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004101 return 0; /* already parsed */
4102
4103 if (!codec->patch_ops.build_pcms)
4104 return 0;
4105
4106 err = codec->patch_ops.build_pcms(codec);
4107 if (err < 0) {
4108 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004109 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004110 return err;
4111 }
4112
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004113 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004114 int stream;
4115
4116 for (stream = 0; stream < 2; stream++) {
4117 struct hda_pcm_stream *info = &cpcm->stream[stream];
4118
4119 if (!info->substreams)
4120 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004121 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004122 if (err < 0) {
4123 codec_warn(codec,
4124 "fail to setup default for PCM %s\n",
4125 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004126 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004127 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004128 }
4129 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004130
4131 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004132}
4133
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004134/* assign all PCMs of the given codec */
4135int snd_hda_codec_build_pcms(struct hda_codec *codec)
4136{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004137 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004138 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004139 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004140
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004141 if (snd_BUG_ON(!bus->ops.attach_pcm))
4142 return -EINVAL;
4143
4144 err = snd_hda_codec_parse_pcms(codec);
4145 if (err < 0) {
4146 snd_hda_codec_reset(codec);
4147 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004148 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004149
4150 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004151 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004152 if (cpcm->pcm)
4153 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004154 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004155 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004156
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004157 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4158 if (dev < 0)
4159 continue; /* no fatal error */
4160 cpcm->device = dev;
4161 err = bus->ops.attach_pcm(bus, codec, cpcm);
4162 if (err < 0) {
4163 codec_err(codec,
4164 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004165 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004166 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004167 }
4168 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004169
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004170 return 0;
4171}
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 * snd_hda_add_new_ctls - create controls from the array
4175 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004176 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 *
4178 * This helper function creates and add new controls in the given array.
4179 * The array must be terminated with an empty entry as terminator.
4180 *
4181 * Returns 0 if successful, or a negative error code.
4182 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004183int snd_hda_add_new_ctls(struct hda_codec *codec,
4184 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004189 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004190 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004191 if (knew->iface == -1) /* skip this codec private value */
4192 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004193 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004194 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004195 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004196 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004197 if (addr > 0)
4198 kctl->id.device = addr;
4199 if (idx > 0)
4200 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004201 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004202 if (!err)
4203 break;
4204 /* try first with another device index corresponding to
4205 * the codec addr; if it still fails (or it's the
4206 * primary codec), then try another control index
4207 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004208 if (!addr && codec->core.addr)
4209 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004210 else if (!idx && !knew->index) {
4211 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004212 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004213 if (idx <= 0)
4214 return err;
4215 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004216 return err;
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
4219 return 0;
4220}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004221EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Takashi Iwai83012a72012-08-24 18:38:08 +02004223#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01004224static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004225{
4226 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004227
Takashi Iwaicc72da72015-02-19 16:00:22 +01004228 if (delay > 0) {
4229 pm_runtime_set_autosuspend_delay(dev, delay);
4230 pm_runtime_use_autosuspend(dev);
4231 pm_runtime_allow(dev);
4232 if (!pm_runtime_suspended(dev))
4233 pm_runtime_mark_last_busy(dev);
4234 } else {
4235 pm_runtime_dont_use_autosuspend(dev);
4236 pm_runtime_forbid(dev);
4237 }
4238}
Takashi Iwaibb573922015-02-20 09:26:04 +01004239
4240/**
4241 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4242 * @bus: HD-audio bus
4243 * @delay: autosuspend delay in msec, 0 = off
4244 *
4245 * Synchronize the runtime PM autosuspend state from the power_save option.
4246 */
4247void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4248{
4249 struct hda_codec *c;
4250
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004251 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01004252 codec_set_power_save(c, delay);
4253}
4254EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004255
Takashi Iwaid5191e52009-11-16 14:58:17 +01004256/**
4257 * snd_hda_check_amp_list_power - Check the amp list and update the power
4258 * @codec: HD-audio codec
4259 * @check: the object containing an AMP list and the status
4260 * @nid: NID to check / update
4261 *
4262 * Check whether the given NID is in the amp list. If it's in the list,
4263 * check the current AMP status, and update the the power-status according
4264 * to the mute status.
4265 *
4266 * This function is supposed to be set or called from the check_power_status
4267 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004268 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004269int snd_hda_check_amp_list_power(struct hda_codec *codec,
4270 struct hda_loopback_check *check,
4271 hda_nid_t nid)
4272{
Takashi Iwai031024e2011-05-02 11:29:30 +02004273 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004274 int ch, v;
4275
4276 if (!check->amplist)
4277 return 0;
4278 for (p = check->amplist; p->nid; p++) {
4279 if (p->nid == nid)
4280 break;
4281 }
4282 if (!p->nid)
4283 return 0; /* nothing changed */
4284
4285 for (p = check->amplist; p->nid; p++) {
4286 for (ch = 0; ch < 2; ch++) {
4287 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4288 p->idx);
4289 if (!(v & HDA_AMP_MUTE) && v > 0) {
4290 if (!check->power_on) {
4291 check->power_on = 1;
4292 snd_hda_power_up(codec);
4293 }
4294 return 1;
4295 }
4296 }
4297 }
4298 if (check->power_on) {
4299 check->power_on = 0;
4300 snd_hda_power_down(codec);
4301 }
4302 return 0;
4303}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004304EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 * input MUX helper
4309 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004310
4311/**
4312 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004313 * @imux: imux helper object
4314 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004316int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4317 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
4319 unsigned int index;
4320
4321 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4322 uinfo->count = 1;
4323 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004324 if (!imux->num_items)
4325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 index = uinfo->value.enumerated.item;
4327 if (index >= imux->num_items)
4328 index = imux->num_items - 1;
4329 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4330 return 0;
4331}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004332EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Takashi Iwaid5191e52009-11-16 14:58:17 +01004334/**
4335 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004336 * @codec: the HDA codec
4337 * @imux: imux helper object
4338 * @ucontrol: pointer to get/store the data
4339 * @nid: input mux NID
4340 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004341 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004342int snd_hda_input_mux_put(struct hda_codec *codec,
4343 const struct hda_input_mux *imux,
4344 struct snd_ctl_elem_value *ucontrol,
4345 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 unsigned int *cur_val)
4347{
4348 unsigned int idx;
4349
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004350 if (!imux->num_items)
4351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 idx = ucontrol->value.enumerated.item[0];
4353 if (idx >= imux->num_items)
4354 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004355 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004357 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4358 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 *cur_val = idx;
4360 return 1;
4361}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004362EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004365/**
4366 * snd_hda_enum_helper_info - Helper for simple enum ctls
4367 * @kcontrol: ctl element
4368 * @uinfo: pointer to get/store the data
4369 * @num_items: number of enum items
4370 * @texts: enum item string array
4371 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004372 * process kcontrol info callback of a simple string enum array
4373 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4374 */
4375int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4376 struct snd_ctl_elem_info *uinfo,
4377 int num_items, const char * const *texts)
4378{
4379 static const char * const texts_default[] = {
4380 "Disabled", "Enabled"
4381 };
4382
4383 if (!texts || !num_items) {
4384 num_items = 2;
4385 texts = texts_default;
4386 }
4387
Takashi Iwai3ff72212014-10-20 18:17:28 +02004388 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004389}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004390EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004391
4392/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 * Multi-channel / digital-out PCM helper functions
4394 */
4395
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004396/* setup SPDIF output stream */
4397static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4398 unsigned int stream_tag, unsigned int format)
4399{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004400 struct hda_spdif_out *spdif;
4401 unsigned int curr_fmt;
4402 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004403
Laurence Darby3bef1c32012-11-03 17:00:06 +00004404 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4405 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4406 AC_VERB_GET_STREAM_FORMAT, 0);
4407 reset = codec->spdif_status_reset &&
4408 (spdif->ctls & AC_DIG1_ENABLE) &&
4409 curr_fmt != format;
4410
4411 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4412 updated */
4413 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004414 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004415 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004416 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004417 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004418 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004419 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004420 for (d = codec->slave_dig_outs; *d; d++)
4421 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4422 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004423 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004424 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004425 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004426 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004427 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004428}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004429
Takashi Iwai2f728532008-09-25 16:32:41 +02004430static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4431{
4432 snd_hda_codec_cleanup_stream(codec, nid);
4433 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004434 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004435 for (d = codec->slave_dig_outs; *d; d++)
4436 snd_hda_codec_cleanup_stream(codec, *d);
4437 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004438}
4439
Takashi Iwaid5191e52009-11-16 14:58:17 +01004440/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004441 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004442 * @codec: the HDA codec
4443 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004445int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4446 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Ingo Molnar62932df2006-01-16 16:34:20 +01004448 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004449 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4450 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004451 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004453 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return 0;
4455}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004456EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Takashi Iwaid5191e52009-11-16 14:58:17 +01004458/**
4459 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004460 * @codec: the HDA codec
4461 * @mout: hda_multi_out object
4462 * @stream_tag: stream tag to assign
4463 * @format: format id to assign
4464 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004465 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004466int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4467 struct hda_multi_out *mout,
4468 unsigned int stream_tag,
4469 unsigned int format,
4470 struct snd_pcm_substream *substream)
4471{
4472 mutex_lock(&codec->spdif_mutex);
4473 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4474 mutex_unlock(&codec->spdif_mutex);
4475 return 0;
4476}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004477EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004478
Takashi Iwaid5191e52009-11-16 14:58:17 +01004479/**
4480 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004481 * @codec: the HDA codec
4482 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004483 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004484int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4485 struct hda_multi_out *mout)
4486{
4487 mutex_lock(&codec->spdif_mutex);
4488 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4489 mutex_unlock(&codec->spdif_mutex);
4490 return 0;
4491}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004492EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004493
Takashi Iwaid5191e52009-11-16 14:58:17 +01004494/**
4495 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004496 * @codec: the HDA codec
4497 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004499int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4500 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Ingo Molnar62932df2006-01-16 16:34:20 +01004502 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004504 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return 0;
4506}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004507EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Takashi Iwaid5191e52009-11-16 14:58:17 +01004509/**
4510 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004511 * @codec: the HDA codec
4512 * @mout: hda_multi_out object
4513 * @substream: PCM substream to assign
4514 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004515 *
4516 * Open analog outputs and set up the hw-constraints.
4517 * If the digital outputs can be opened as slave, open the digital
4518 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004520int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4521 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004522 struct snd_pcm_substream *substream,
4523 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Takashi Iwai9a081602008-02-12 18:37:26 +01004525 struct snd_pcm_runtime *runtime = substream->runtime;
4526 runtime->hw.channels_max = mout->max_channels;
4527 if (mout->dig_out_nid) {
4528 if (!mout->analog_rates) {
4529 mout->analog_rates = hinfo->rates;
4530 mout->analog_formats = hinfo->formats;
4531 mout->analog_maxbps = hinfo->maxbps;
4532 } else {
4533 runtime->hw.rates = mout->analog_rates;
4534 runtime->hw.formats = mout->analog_formats;
4535 hinfo->maxbps = mout->analog_maxbps;
4536 }
4537 if (!mout->spdif_rates) {
4538 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4539 &mout->spdif_rates,
4540 &mout->spdif_formats,
4541 &mout->spdif_maxbps);
4542 }
4543 mutex_lock(&codec->spdif_mutex);
4544 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004545 if ((runtime->hw.rates & mout->spdif_rates) &&
4546 (runtime->hw.formats & mout->spdif_formats)) {
4547 runtime->hw.rates &= mout->spdif_rates;
4548 runtime->hw.formats &= mout->spdif_formats;
4549 if (mout->spdif_maxbps < hinfo->maxbps)
4550 hinfo->maxbps = mout->spdif_maxbps;
4551 } else {
4552 mout->share_spdif = 0;
4553 /* FIXME: need notify? */
4554 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004555 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004556 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4559 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4560}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004561EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Takashi Iwaid5191e52009-11-16 14:58:17 +01004563/**
4564 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004565 * @codec: the HDA codec
4566 * @mout: hda_multi_out object
4567 * @stream_tag: stream tag to assign
4568 * @format: format id to assign
4569 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004570 *
4571 * Set up the i/o for analog out.
4572 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004574int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4575 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 unsigned int stream_tag,
4577 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004578 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Takashi Iwaidda14412011-05-02 11:29:30 +02004580 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004582 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 int i;
4584
Ingo Molnar62932df2006-01-16 16:34:20 +01004585 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004586 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004587 if (mout->dig_out_nid && mout->share_spdif &&
4588 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4591 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004592 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004594 setup_dig_out_stream(codec, mout->dig_out_nid,
4595 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 } else {
4597 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004598 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004601 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004604 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4605 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004606 if (!mout->no_share_stream &&
4607 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004609 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4610 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004611 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004612 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4613 if (!mout->no_share_stream && mout->hp_out_nid[i])
4614 snd_hda_codec_setup_stream(codec,
4615 mout->hp_out_nid[i],
4616 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 /* surrounds */
4619 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004620 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004621 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4622 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004623 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004624 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4625 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004627
4628 /* extra surrounds */
4629 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4630 int ch = 0;
4631 if (!mout->extra_out_nid[i])
4632 break;
4633 if (chs >= (i + 1) * 2)
4634 ch = i * 2;
4635 else if (!mout->no_share_stream)
4636 break;
4637 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4638 stream_tag, ch, format);
4639 }
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return 0;
4642}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004643EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Takashi Iwaid5191e52009-11-16 14:58:17 +01004645/**
4646 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004647 * @codec: the HDA codec
4648 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004650int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4651 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Takashi Iwaidda14412011-05-02 11:29:30 +02004653 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 int i;
4655
4656 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004657 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004659 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004660 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4661 if (mout->hp_out_nid[i])
4662 snd_hda_codec_cleanup_stream(codec,
4663 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004664 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4665 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004666 snd_hda_codec_cleanup_stream(codec,
4667 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004668 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004670 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 mout->dig_out_used = 0;
4672 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004673 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return 0;
4675}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004676EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Takashi Iwai47408602012-04-20 13:06:53 +02004678/**
4679 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004680 * @codec: the HDA codec
4681 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004682 *
4683 * Guess the suitable VREF pin bits to be set as the pin-control value.
4684 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4685 */
4686unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4687{
4688 unsigned int pincap;
4689 unsigned int oldval;
4690 oldval = snd_hda_codec_read(codec, pin, 0,
4691 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4692 pincap = snd_hda_query_pin_caps(codec, pin);
4693 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4694 /* Exception: if the default pin setup is vref50, we give it priority */
4695 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4696 return AC_PINCTL_VREF_80;
4697 else if (pincap & AC_PINCAP_VREF_50)
4698 return AC_PINCTL_VREF_50;
4699 else if (pincap & AC_PINCAP_VREF_100)
4700 return AC_PINCTL_VREF_100;
4701 else if (pincap & AC_PINCAP_VREF_GRD)
4702 return AC_PINCTL_VREF_GRD;
4703 return AC_PINCTL_VREF_HIZ;
4704}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004705EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004706
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004707/**
4708 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4709 * @codec: the HDA codec
4710 * @pin: referred pin NID
4711 * @val: pin ctl value to audit
4712 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004713unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4714 hda_nid_t pin, unsigned int val)
4715{
4716 static unsigned int cap_lists[][2] = {
4717 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4718 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4719 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4720 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4721 };
4722 unsigned int cap;
4723
4724 if (!val)
4725 return 0;
4726 cap = snd_hda_query_pin_caps(codec, pin);
4727 if (!cap)
4728 return val; /* don't know what to do... */
4729
4730 if (val & AC_PINCTL_OUT_EN) {
4731 if (!(cap & AC_PINCAP_OUT))
4732 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4733 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4734 val &= ~AC_PINCTL_HP_EN;
4735 }
4736
4737 if (val & AC_PINCTL_IN_EN) {
4738 if (!(cap & AC_PINCAP_IN))
4739 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4740 else {
4741 unsigned int vcap, vref;
4742 int i;
4743 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4744 vref = val & AC_PINCTL_VREFEN;
4745 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4746 if (vref == cap_lists[i][0] &&
4747 !(vcap & cap_lists[i][1])) {
4748 if (i == ARRAY_SIZE(cap_lists) - 1)
4749 vref = AC_PINCTL_VREF_HIZ;
4750 else
4751 vref = cap_lists[i + 1][0];
4752 }
4753 }
4754 val &= ~AC_PINCTL_VREFEN;
4755 val |= vref;
4756 }
4757 }
4758
4759 return val;
4760}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004761EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004762
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004763/**
4764 * _snd_hda_pin_ctl - Helper to set pin ctl value
4765 * @codec: the HDA codec
4766 * @pin: referred pin NID
4767 * @val: pin control value to set
4768 * @cached: access over codec pinctl cache or direct write
4769 *
4770 * This function is a helper to set a pin ctl value more safely.
4771 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4772 * value in pin target array via snd_hda_codec_set_pin_target(), then
4773 * actually writes the value via either snd_hda_codec_update_cache() or
4774 * snd_hda_codec_write() depending on @cached flag.
4775 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004776int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4777 unsigned int val, bool cached)
4778{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004779 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004780 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004781 if (cached)
4782 return snd_hda_codec_update_cache(codec, pin, 0,
4783 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4784 else
4785 return snd_hda_codec_write(codec, pin, 0,
4786 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4787}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004788EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004789
Takashi Iwai990061c2010-09-09 22:08:44 +02004790/**
4791 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004792 * @codec: the HDA codec
4793 * @imux: imux helper object
4794 * @label: the name of imux item to assign
4795 * @index: index number of imux item to assign
4796 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004797 *
4798 * When the same label is used already in the existing items, the number
4799 * suffix is appended to the label. This label index number is stored
4800 * to type_idx when non-NULL pointer is given.
4801 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004802int snd_hda_add_imux_item(struct hda_codec *codec,
4803 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004804 int index, int *type_idx)
4805{
4806 int i, label_idx = 0;
4807 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004808 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004809 return -EINVAL;
4810 }
4811 for (i = 0; i < imux->num_items; i++) {
4812 if (!strncmp(label, imux->items[i].label, strlen(label)))
4813 label_idx++;
4814 }
4815 if (type_idx)
4816 *type_idx = label_idx;
4817 if (label_idx > 0)
4818 snprintf(imux->items[imux->num_items].label,
4819 sizeof(imux->items[imux->num_items].label),
4820 "%s %d", label, label_idx);
4821 else
4822 strlcpy(imux->items[imux->num_items].label, label,
4823 sizeof(imux->items[imux->num_items].label));
4824 imux->items[imux->num_items].index = index;
4825 imux->num_items++;
4826 return 0;
4827}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004828EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004831 * snd_hda_bus_reset - Reset the bus
4832 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004834void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004836 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004838 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004839 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004840 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004841#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004842 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004843 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004844 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004845 }
4846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004849EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004850
Takashi Iwaid5191e52009-11-16 14:58:17 +01004851/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004852 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4853 * @pcm: PCM caps bits
4854 * @buf: the string buffer to write
4855 * @buflen: the max buffer length
4856 *
4857 * used by hda_proc.c and hda_eld.c
4858 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004859void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4860{
4861 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4862 int i, j;
4863
4864 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4865 if (pcm & (AC_SUPPCM_BITS_8 << i))
4866 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4867
4868 buf[j] = '\0'; /* necessary when j == 0 */
4869}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004870EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004871
4872MODULE_DESCRIPTION("HDA codec core");
4873MODULE_LICENSE("GPL");