blob: 27333e0d8ebe64de3edfc3aa6c621b5296eb9710 [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 Iwai664c7152015-04-08 11:43:14 +0200140 snd_hda_power_up_pm(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 Iwai664c7152015-04-08 11:43:14 +0200148 snd_hda_power_down_pm(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 Iwai3fdf1462012-11-22 08:16:31 +0100810/* release all pincfg lists */
811static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100812{
Takashi Iwai346ff702009-02-23 09:42:57 +0100813 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100814#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100815 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100816#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100817 snd_array_free(&codec->init_pins);
818}
819
Takashi Iwai54d17402005-11-21 16:33:22 +0100820/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200821 * audio-converter setup caches
822 */
823struct hda_cvt_setup {
824 hda_nid_t nid;
825 u8 stream_tag;
826 u8 channel_id;
827 u16 format_id;
828 unsigned char active; /* cvt is currently used */
829 unsigned char dirty; /* setups should be cleared */
830};
831
832/* get or create a cache entry for the given audio converter NID */
833static struct hda_cvt_setup *
834get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
835{
836 struct hda_cvt_setup *p;
837 int i;
838
839 for (i = 0; i < codec->cvt_setups.used; i++) {
840 p = snd_array_elem(&codec->cvt_setups, i);
841 if (p->nid == nid)
842 return p;
843 }
844 p = snd_array_new(&codec->cvt_setups);
845 if (p)
846 p->nid = nid;
847 return p;
848}
849
850/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100851 * PCM device
852 */
853static void release_pcm(struct kref *kref)
854{
855 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
856
857 if (pcm->pcm)
858 snd_device_free(pcm->codec->card, pcm->pcm);
859 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
860 kfree(pcm->name);
861 kfree(pcm);
862}
863
864void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
865{
866 kref_put(&pcm->kref, release_pcm);
867}
868EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
869
870struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
871 const char *fmt, ...)
872{
873 struct hda_pcm *pcm;
874 va_list args;
875
876 va_start(args, fmt);
877 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
878 if (!pcm)
879 return NULL;
880
881 pcm->codec = codec;
882 kref_init(&pcm->kref);
883 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
884 if (!pcm->name) {
885 kfree(pcm);
886 return NULL;
887 }
888
889 list_add_tail(&pcm->list, &codec->pcm_list_head);
890 return pcm;
891}
892EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
893
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100894/*
895 * codec destructor
896 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100897static void codec_release_pcms(struct hda_codec *codec)
898{
899 struct hda_pcm *pcm, *n;
900
901 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
902 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100903 if (pcm->pcm)
904 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100905 snd_hda_codec_pcm_put(pcm);
906 }
907}
908
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100909void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
910{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100911 if (codec->registered) {
912 /* pm_runtime_put() is called in snd_hdac_device_exit() */
913 pm_runtime_get_noresume(hda_codec_dev(codec));
914 pm_runtime_disable(hda_codec_dev(codec));
915 codec->registered = 0;
916 }
917
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100918 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100919 if (!codec->in_freeing)
920 snd_hda_ctls_clear(codec);
921 codec_release_pcms(codec);
922 snd_hda_detach_beep_device(codec);
923 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
924 snd_hda_jack_tbl_clear(codec);
925 codec->proc_widget_hook = NULL;
926 codec->spec = NULL;
927
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100928 /* free only driver_pins so that init_pins + user_pins are restored */
929 snd_array_free(&codec->driver_pins);
930 snd_array_free(&codec->cvt_setups);
931 snd_array_free(&codec->spdif_out);
932 snd_array_free(&codec->verbs);
933 codec->preset = NULL;
934 codec->slave_dig_outs = NULL;
935 codec->spdif_status_reset = 0;
936 snd_array_free(&codec->mixers);
937 snd_array_free(&codec->nids);
938 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100939 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100940}
941
Takashi Iwaid8193872012-08-31 07:54:38 -0700942static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100943 unsigned int power_state);
944
Takashi Iwaic4c25332015-03-03 17:22:12 +0100945/* also called from hda_bind.c */
946void snd_hda_codec_register(struct hda_codec *codec)
947{
948 if (codec->registered)
949 return;
950 if (device_is_registered(hda_codec_dev(codec))) {
951 snd_hda_register_beep_device(codec);
952 pm_runtime_enable(hda_codec_dev(codec));
953 /* it was powered up in snd_hda_codec_new(), now all done */
954 snd_hda_power_down(codec);
955 codec->registered = 1;
956 }
957}
958
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100959static int snd_hda_codec_dev_register(struct snd_device *device)
960{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100961 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100962 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100963}
964
965static int snd_hda_codec_dev_disconnect(struct snd_device *device)
966{
967 struct hda_codec *codec = device->device_data;
968
Takashi Iwaid604b392014-02-28 13:42:09 +0100969 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100970 return 0;
971}
972
Takashi Iwai2565c892014-02-19 11:41:09 +0100973static int snd_hda_codec_dev_free(struct snd_device *device)
974{
Takashi Iwaid56db742015-02-27 23:25:35 +0100975 struct hda_codec *codec = device->device_data;
976
977 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100978 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100979 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100980 return 0;
981}
982
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100983static void snd_hda_codec_dev_release(struct device *dev)
984{
Takashi Iwaid56db742015-02-27 23:25:35 +0100985 struct hda_codec *codec = dev_to_hda_codec(dev);
986
987 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100988 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100989 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100990 kfree(codec->modelname);
991 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100992 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/**
996 * snd_hda_codec_new - create a HDA codec
997 * @bus: the bus to assign
998 * @codec_addr: the codec address
999 * @codecp: the pointer to store the generated codec
1000 *
1001 * Returns 0 if successful, or a negative error code.
1002 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001003int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1004 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001007 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001008 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001010 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001011 .dev_register = snd_hda_codec_dev_register,
1012 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001013 .dev_free = snd_hda_codec_dev_free,
1014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Takashi Iwaida3cec32008-08-08 17:12:14 +02001016 if (snd_BUG_ON(!bus))
1017 return -EINVAL;
1018 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1019 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001021 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001022 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Takashi Iwai7639a062015-03-03 10:07:24 +01001025 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1026 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1027 codec_addr);
1028 if (err < 0) {
1029 kfree(codec);
1030 return err;
1031 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001032
Takashi Iwai7639a062015-03-03 10:07:24 +01001033 codec->core.dev.release = snd_hda_codec_dev_release;
1034 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +01001035 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001038 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001040 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001041 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001042 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1043 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001044 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001045 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001046 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001047 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001048 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001049 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001050 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001051 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001052
David Henningsson26a6cb62012-10-09 15:04:21 +02001053 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001054 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001055 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Takashi Iwai83012a72012-08-24 18:38:08 +02001057#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001058 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001059#endif
1060
Takashi Iwai648a8d22014-02-25 10:38:13 +01001061 snd_hda_sysfs_init(codec);
1062
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001063 if (codec->bus->modelname) {
1064 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1065 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001066 err = -ENODEV;
1067 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001068 }
1069 }
1070
Takashi Iwai7639a062015-03-03 10:07:24 +01001071 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001072 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001073 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001074 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 err = read_pin_defaults(codec);
1076 if (err < 0)
1077 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001078
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001079 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001080 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001081
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001082 snd_hda_codec_proc_new(codec);
1083
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001084 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001085
Takashi Iwai7639a062015-03-03 10:07:24 +01001086 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1087 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001088 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001089
Takashi Iwai6efdd852015-02-27 16:09:22 +01001090 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001091 if (err < 0)
1092 goto error;
1093
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094 if (codecp)
1095 *codecp = codec;
1096 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001097
1098 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001099 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001100 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001101}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001102EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001103
Takashi Iwai95a962c2014-10-29 16:03:58 +01001104/**
1105 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1106 * @codec: the HDA codec
1107 *
1108 * Forcibly refresh the all widget caps and the init pin configurations of
1109 * the given codec.
1110 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001111int snd_hda_codec_update_widgets(struct hda_codec *codec)
1112{
1113 hda_nid_t fg;
1114 int err;
1115
Takashi Iwai7639a062015-03-03 10:07:24 +01001116 err = snd_hdac_refresh_widgets(&codec->core);
1117 if (err < 0)
1118 return err;
1119
Mengdong Lina15d05d2013-02-08 17:09:31 -05001120 /* Assume the function group node does not change,
1121 * only the widget nodes may change.
1122 */
1123 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001124 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001125 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001126 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001127 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001128
1129 snd_array_free(&codec->init_pins);
1130 err = read_pin_defaults(codec);
1131
1132 return err;
1133}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001134EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001135
Takashi Iwaied360812012-08-08 17:12:52 +02001136/* update the stream-id if changed */
1137static void update_pcm_stream_id(struct hda_codec *codec,
1138 struct hda_cvt_setup *p, hda_nid_t nid,
1139 u32 stream_tag, int channel_id)
1140{
1141 unsigned int oldval, newval;
1142
1143 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1144 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1145 newval = (stream_tag << 4) | channel_id;
1146 if (oldval != newval)
1147 snd_hda_codec_write(codec, nid, 0,
1148 AC_VERB_SET_CHANNEL_STREAMID,
1149 newval);
1150 p->stream_tag = stream_tag;
1151 p->channel_id = channel_id;
1152 }
1153}
1154
1155/* update the format-id if changed */
1156static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1157 hda_nid_t nid, int format)
1158{
1159 unsigned int oldval;
1160
1161 if (p->format_id != format) {
1162 oldval = snd_hda_codec_read(codec, nid, 0,
1163 AC_VERB_GET_STREAM_FORMAT, 0);
1164 if (oldval != format) {
1165 msleep(1);
1166 snd_hda_codec_write(codec, nid, 0,
1167 AC_VERB_SET_STREAM_FORMAT,
1168 format);
1169 }
1170 p->format_id = format;
1171 }
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/**
1175 * snd_hda_codec_setup_stream - set up the codec for streaming
1176 * @codec: the CODEC to set up
1177 * @nid: the NID to set up
1178 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1179 * @channel_id: channel id to pass, zero based.
1180 * @format: stream format.
1181 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001182void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1183 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int channel_id, int format)
1185{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001186 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001187 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001188 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001189 int i;
1190
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001192 return;
1193
Takashi Iwai4e76a882014-02-25 12:21:03 +01001194 codec_dbg(codec,
1195 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1196 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001197 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001198 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001199 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001200
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001201 if (codec->patch_ops.stream_pm)
1202 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001203 if (codec->pcm_format_first)
1204 update_pcm_format(codec, p, nid, format);
1205 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1206 if (!codec->pcm_format_first)
1207 update_pcm_format(codec, p, nid, format);
1208
Takashi Iwaieb541332010-08-06 13:48:11 +02001209 p->active = 1;
1210 p->dirty = 0;
1211
1212 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001213 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001214 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001215 for (i = 0; i < c->cvt_setups.used; i++) {
1216 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001217 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001218 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001219 p->dirty = 1;
1220 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001223EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Takashi Iwaif0cea792010-08-13 11:56:53 +02001225static void really_cleanup_stream(struct hda_codec *codec,
1226 struct hda_cvt_setup *q);
1227
Takashi Iwaid5191e52009-11-16 14:58:17 +01001228/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001229 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001230 * @codec: the CODEC to clean up
1231 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001232 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001233 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001234void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1235 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001236{
Takashi Iwaieb541332010-08-06 13:48:11 +02001237 struct hda_cvt_setup *p;
1238
Takashi Iwai888afa12008-03-18 09:57:50 +01001239 if (!nid)
1240 return;
1241
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001242 if (codec->no_sticky_stream)
1243 do_now = 1;
1244
Takashi Iwai4e76a882014-02-25 12:21:03 +01001245 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001246 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001247 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001248 /* here we just clear the active flag when do_now isn't set;
1249 * actual clean-ups will be done later in
1250 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1251 */
1252 if (do_now)
1253 really_cleanup_stream(codec, p);
1254 else
1255 p->active = 0;
1256 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001257}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001258EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001259
Takashi Iwaieb541332010-08-06 13:48:11 +02001260static void really_cleanup_stream(struct hda_codec *codec,
1261 struct hda_cvt_setup *q)
1262{
1263 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001264 if (q->stream_tag || q->channel_id)
1265 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1266 if (q->format_id)
1267 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1268);
Takashi Iwaieb541332010-08-06 13:48:11 +02001269 memset(q, 0, sizeof(*q));
1270 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001271 if (codec->patch_ops.stream_pm)
1272 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001273}
1274
1275/* clean up the all conflicting obsolete streams */
1276static void purify_inactive_streams(struct hda_codec *codec)
1277{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001278 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001279 int i;
1280
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001281 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001282 for (i = 0; i < c->cvt_setups.used; i++) {
1283 struct hda_cvt_setup *p;
1284 p = snd_array_elem(&c->cvt_setups, i);
1285 if (p->dirty)
1286 really_cleanup_stream(c, p);
1287 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001288 }
1289}
1290
Takashi Iwai2a439522011-07-26 09:52:50 +02001291#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001292/* clean up all streams; called from suspend */
1293static void hda_cleanup_all_streams(struct hda_codec *codec)
1294{
1295 int i;
1296
1297 for (i = 0; i < codec->cvt_setups.used; i++) {
1298 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1299 if (p->stream_tag)
1300 really_cleanup_stream(codec, p);
1301 }
1302}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001303#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/*
1306 * amp access functions
1307 */
1308
Takashi Iwaid5191e52009-11-16 14:58:17 +01001309/**
1310 * query_amp_caps - query AMP capabilities
1311 * @codec: the HD-auio codec
1312 * @nid: the NID to query
1313 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1314 *
1315 * Query AMP capabilities for the given widget and direction.
1316 * Returns the obtained capability bits.
1317 *
1318 * When cap bits have been already read, this doesn't read again but
1319 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001321u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001323 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1324 nid = codec->core.afg;
1325 return snd_hda_param_read(codec, nid,
1326 direction == HDA_OUTPUT ?
1327 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001329EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Takashi Iwaid5191e52009-11-16 14:58:17 +01001331/**
David Henningsson861a04e2014-09-23 10:38:17 +02001332 * snd_hda_check_amp_caps - query AMP capabilities
1333 * @codec: the HD-audio codec
1334 * @nid: the NID to query
1335 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001336 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001337 *
1338 * Check whether the widget has the given amp capability for the direction.
1339 */
1340bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1341 int dir, unsigned int bits)
1342{
1343 if (!nid)
1344 return false;
1345 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1346 if (query_amp_caps(codec, nid, dir) & bits)
1347 return true;
1348 return false;
1349}
1350EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1351
1352/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001353 * snd_hda_override_amp_caps - Override the AMP capabilities
1354 * @codec: the CODEC to clean up
1355 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001356 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001357 * @caps: the capability bits to set
1358 *
1359 * Override the cached AMP caps bits value by the given one.
1360 * This function is useful if the driver needs to adjust the AMP ranges,
1361 * e.g. limit to 0dB, etc.
1362 *
1363 * Returns zero if successful or a negative error code.
1364 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001365int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1366 unsigned int caps)
1367{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001368 unsigned int parm;
1369
1370 snd_hda_override_wcaps(codec, nid,
1371 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1372 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1373 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001374}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001375EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001376
Takashi Iwaid5191e52009-11-16 14:58:17 +01001377/**
1378 * snd_hda_codec_amp_stereo - update the AMP stereo values
1379 * @codec: HD-audio codec
1380 * @nid: NID to read the AMP value
1381 * @direction: #HDA_INPUT or #HDA_OUTPUT
1382 * @idx: the index value (only for input direction)
1383 * @mask: bit mask to set
1384 * @val: the bits value to set
1385 *
1386 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1387 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001388 */
1389int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1390 int direction, int idx, int mask, int val)
1391{
1392 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001393
1394 if (snd_BUG_ON(mask & ~0xff))
1395 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001396 for (ch = 0; ch < 2; ch++)
1397 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1398 idx, mask, val);
1399 return ret;
1400}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001401EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001402
Takashi Iwai95a962c2014-10-29 16:03:58 +01001403/**
1404 * snd_hda_codec_amp_init - initialize the AMP value
1405 * @codec: the HDA codec
1406 * @nid: NID to read the AMP value
1407 * @ch: channel (left=0 or right=1)
1408 * @dir: #HDA_INPUT or #HDA_OUTPUT
1409 * @idx: the index value (only for input direction)
1410 * @mask: bit mask to set
1411 * @val: the bits value to set
1412 *
1413 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001414 * the first access. If the amp was already initialized / updated beforehand,
1415 * this does nothing.
1416 */
1417int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1418 int dir, int idx, int mask, int val)
1419{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001420 int orig;
1421
1422 if (!codec->core.regmap)
1423 return -EINVAL;
1424 regcache_cache_only(codec->core.regmap, true);
1425 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1426 regcache_cache_only(codec->core.regmap, false);
1427 if (orig >= 0)
1428 return 0;
1429 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001430}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001431EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001432
Takashi Iwai95a962c2014-10-29 16:03:58 +01001433/**
1434 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1435 * @codec: the HDA codec
1436 * @nid: NID to read the AMP value
1437 * @dir: #HDA_INPUT or #HDA_OUTPUT
1438 * @idx: the index value (only for input direction)
1439 * @mask: bit mask to set
1440 * @val: the bits value to set
1441 *
1442 * Call snd_hda_codec_amp_init() for both stereo channels.
1443 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001444int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1445 int dir, int idx, int mask, int val)
1446{
1447 int ch, ret = 0;
1448
1449 if (snd_BUG_ON(mask & ~0xff))
1450 mask &= 0xff;
1451 for (ch = 0; ch < 2; ch++)
1452 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1453 idx, mask, val);
1454 return ret;
1455}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001456EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001457
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001458static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1459 unsigned int ofs)
1460{
1461 u32 caps = query_amp_caps(codec, nid, dir);
1462 /* get num steps */
1463 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1464 if (ofs < caps)
1465 caps -= ofs;
1466 return caps;
1467}
1468
Takashi Iwaid5191e52009-11-16 14:58:17 +01001469/**
1470 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001471 * @kcontrol: referred ctl element
1472 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001473 *
1474 * The control element is supposed to have the private_value field
1475 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1476 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001477int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1481 u16 nid = get_amp_nid(kcontrol);
1482 u8 chs = get_amp_channels(kcontrol);
1483 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001484 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001486 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1487 uinfo->count = chs == 3 ? 2 : 1;
1488 uinfo->value.integer.min = 0;
1489 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1490 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001491 codec_warn(codec,
1492 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1493 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EINVAL;
1495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 0;
1497}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001498EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001500
1501static inline unsigned int
1502read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1503 int ch, int dir, int idx, unsigned int ofs)
1504{
1505 unsigned int val;
1506 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1507 val &= HDA_AMP_VOLMASK;
1508 if (val >= ofs)
1509 val -= ofs;
1510 else
1511 val = 0;
1512 return val;
1513}
1514
1515static inline int
1516update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1517 int ch, int dir, int idx, unsigned int ofs,
1518 unsigned int val)
1519{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001520 unsigned int maxval;
1521
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001522 if (val > 0)
1523 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001524 /* ofs = 0: raw max value */
1525 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001526 if (val > maxval)
1527 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001528 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1529 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001530}
1531
Takashi Iwaid5191e52009-11-16 14:58:17 +01001532/**
1533 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001534 * @kcontrol: ctl element
1535 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001536 *
1537 * The control element is supposed to have the private_value field
1538 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1539 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001540int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1544 hda_nid_t nid = get_amp_nid(kcontrol);
1545 int chs = get_amp_channels(kcontrol);
1546 int dir = get_amp_direction(kcontrol);
1547 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001548 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 long *valp = ucontrol->value.integer.value;
1550
1551 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001552 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001554 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
1556}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001557EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Takashi Iwaid5191e52009-11-16 14:58:17 +01001559/**
1560 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001561 * @kcontrol: ctl element
1562 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001563 *
1564 * The control element is supposed to have the private_value field
1565 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1566 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001567int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1571 hda_nid_t nid = get_amp_nid(kcontrol);
1572 int chs = get_amp_channels(kcontrol);
1573 int dir = get_amp_direction(kcontrol);
1574 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001575 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 long *valp = ucontrol->value.integer.value;
1577 int change = 0;
1578
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001579 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001580 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001581 valp++;
1582 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001583 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001584 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return change;
1586}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001587EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Takashi Iwaid5191e52009-11-16 14:58:17 +01001589/**
1590 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001591 * @kcontrol: ctl element
1592 * @op_flag: operation flag
1593 * @size: byte size of input TLV
1594 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001595 *
1596 * The control element is supposed to have the private_value field
1597 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1598 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001599int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1600 unsigned int size, unsigned int __user *_tlv)
1601{
1602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1603 hda_nid_t nid = get_amp_nid(kcontrol);
1604 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001605 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001606 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001607 u32 caps, val1, val2;
1608
1609 if (size < 4 * sizeof(unsigned int))
1610 return -ENOMEM;
1611 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001612 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1613 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001614 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001615 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001616 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001617 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001618 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001619 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1620 return -EFAULT;
1621 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1622 return -EFAULT;
1623 if (put_user(val1, _tlv + 2))
1624 return -EFAULT;
1625 if (put_user(val2, _tlv + 3))
1626 return -EFAULT;
1627 return 0;
1628}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001629EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001630
Takashi Iwaid5191e52009-11-16 14:58:17 +01001631/**
1632 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1633 * @codec: HD-audio codec
1634 * @nid: NID of a reference widget
1635 * @dir: #HDA_INPUT or #HDA_OUTPUT
1636 * @tlv: TLV data to be stored, at least 4 elements
1637 *
1638 * Set (static) TLV data for a virtual master volume using the AMP caps
1639 * obtained from the reference NID.
1640 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001641 */
1642void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1643 unsigned int *tlv)
1644{
1645 u32 caps;
1646 int nums, step;
1647
1648 caps = query_amp_caps(codec, nid, dir);
1649 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1650 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1651 step = (step + 1) * 25;
1652 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1653 tlv[1] = 2 * sizeof(unsigned int);
1654 tlv[2] = -nums * step;
1655 tlv[3] = step;
1656}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001657EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001658
1659/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001660static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001661find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001662{
1663 struct snd_ctl_elem_id id;
1664 memset(&id, 0, sizeof(id));
1665 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001666 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001667 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001668 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1669 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001670 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001671 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001672}
1673
Takashi Iwaid5191e52009-11-16 14:58:17 +01001674/**
1675 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1676 * @codec: HD-audio codec
1677 * @name: ctl id name string
1678 *
1679 * Get the control element with the given id string and IFACE_MIXER.
1680 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001681struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1682 const char *name)
1683{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001684 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001686EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001687
Takashi Iwaidcda5802012-10-12 17:24:51 +02001688static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001689 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001690{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001691 int i, idx;
1692 /* 16 ctlrs should be large enough */
1693 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1694 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001695 return idx;
1696 }
1697 return -EBUSY;
1698}
1699
Takashi Iwaid5191e52009-11-16 14:58:17 +01001700/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001701 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001702 * @codec: HD-audio codec
1703 * @nid: corresponding NID (optional)
1704 * @kctl: the control element to assign
1705 *
1706 * Add the given control element to an array inside the codec instance.
1707 * All control elements belonging to a codec are supposed to be added
1708 * by this function so that a proper clean-up works at the free or
1709 * reconfiguration time.
1710 *
1711 * If non-zero @nid is passed, the NID is assigned to the control element.
1712 * The assignment is shown in the codec proc file.
1713 *
1714 * snd_hda_ctl_add() checks the control subdev id field whether
1715 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001716 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1717 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001718 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001719int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1720 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001721{
1722 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001723 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001724 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001725
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001726 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001727 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001728 if (nid == 0)
1729 nid = get_amp_nid_(kctl->private_value);
1730 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001731 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1732 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001733 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001734 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001735 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001736 if (err < 0)
1737 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001738 item = snd_array_new(&codec->mixers);
1739 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001740 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001741 item->kctl = kctl;
1742 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001743 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001744 return 0;
1745}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001746EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001747
Takashi Iwaid5191e52009-11-16 14:58:17 +01001748/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001749 * snd_hda_add_nid - Assign a NID to a control element
1750 * @codec: HD-audio codec
1751 * @nid: corresponding NID (optional)
1752 * @kctl: the control element to assign
1753 * @index: index to kctl
1754 *
1755 * Add the given control element to an array inside the codec instance.
1756 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1757 * NID:KCTL mapping - for example "Capture Source" selector.
1758 */
1759int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1760 unsigned int index, hda_nid_t nid)
1761{
1762 struct hda_nid_item *item;
1763
1764 if (nid > 0) {
1765 item = snd_array_new(&codec->nids);
1766 if (!item)
1767 return -ENOMEM;
1768 item->kctl = kctl;
1769 item->index = index;
1770 item->nid = nid;
1771 return 0;
1772 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001773 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1774 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001775 return -EINVAL;
1776}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001777EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001778
1779/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001780 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1781 * @codec: HD-audio codec
1782 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001783void snd_hda_ctls_clear(struct hda_codec *codec)
1784{
1785 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001786 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001787 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001788 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001789 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001790 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001791}
1792
Takashi Iwai95a962c2014-10-29 16:03:58 +01001793/**
1794 * snd_hda_lock_devices - pseudo device locking
1795 * @bus: the BUS
1796 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001797 * toggle card->shutdown to allow/disallow the device access (as a hack)
1798 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001799int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001800{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001801 struct snd_card *card = bus->card;
1802 struct hda_codec *codec;
1803
Takashi Iwaia65d6292009-02-23 16:57:04 +01001804 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001805 if (card->shutdown)
1806 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001807 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001808 if (!list_empty(&card->ctl_files))
1809 goto err_clear;
1810
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001811 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001812 struct hda_pcm *cpcm;
1813 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001814 if (!cpcm->pcm)
1815 continue;
1816 if (cpcm->pcm->streams[0].substream_opened ||
1817 cpcm->pcm->streams[1].substream_opened)
1818 goto err_clear;
1819 }
1820 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001821 spin_unlock(&card->files_lock);
1822 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001823
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001824 err_clear:
1825 card->shutdown = 0;
1826 err_unlock:
1827 spin_unlock(&card->files_lock);
1828 return -EINVAL;
1829}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001830EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001831
Takashi Iwai95a962c2014-10-29 16:03:58 +01001832/**
1833 * snd_hda_unlock_devices - pseudo device unlocking
1834 * @bus: the BUS
1835 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001836void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001837{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001838 struct snd_card *card = bus->card;
1839
Takashi Iwaia65d6292009-02-23 16:57:04 +01001840 spin_lock(&card->files_lock);
1841 card->shutdown = 0;
1842 spin_unlock(&card->files_lock);
1843}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001844EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001845
Takashi Iwaid5191e52009-11-16 14:58:17 +01001846/**
1847 * snd_hda_codec_reset - Clear all objects assigned to the codec
1848 * @codec: HD-audio codec
1849 *
1850 * This frees the all PCM and control elements assigned to the codec, and
1851 * clears the caches and restores the pin default configurations.
1852 *
1853 * When a device is being used, it returns -EBSY. If successfully freed,
1854 * returns zero.
1855 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001856int snd_hda_codec_reset(struct hda_codec *codec)
1857{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001858 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001859
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001860 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001861 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001862
1863 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001864 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001865
Takashi Iwaia65d6292009-02-23 16:57:04 +01001866 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001867 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001868 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001869}
1870
Takashi Iwai6194b992014-06-06 18:12:16 +02001871typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001872
1873/* apply the function to all matching slave ctls in the mixer list */
1874static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001875 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001876{
1877 struct hda_nid_item *items;
1878 const char * const *s;
1879 int i, err;
1880
1881 items = codec->mixers.list;
1882 for (i = 0; i < codec->mixers.used; i++) {
1883 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001884 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001885 continue;
1886 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001887 char tmpname[sizeof(sctl->id.name)];
1888 const char *name = *s;
1889 if (suffix) {
1890 snprintf(tmpname, sizeof(tmpname), "%s %s",
1891 name, suffix);
1892 name = tmpname;
1893 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001894 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001895 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001896 if (err)
1897 return err;
1898 break;
1899 }
1900 }
1901 }
1902 return 0;
1903}
1904
Takashi Iwai6194b992014-06-06 18:12:16 +02001905static int check_slave_present(struct hda_codec *codec,
1906 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001907{
1908 return 1;
1909}
1910
Takashi Iwai18478e82012-03-09 17:51:10 +01001911/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001912static int get_kctl_0dB_offset(struct hda_codec *codec,
1913 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001914{
1915 int _tlv[4];
1916 const int *tlv = NULL;
1917 int val = -1;
1918
1919 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1920 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1921 mm_segment_t fs = get_fs();
1922 set_fs(get_ds());
1923 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1924 tlv = _tlv;
1925 set_fs(fs);
1926 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1927 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001928 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1929 int step = tlv[3];
1930 step &= ~TLV_DB_SCALE_MUTE;
1931 if (!step)
1932 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01001933 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001934 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001935- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001936 return -1;
1937 }
1938 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001939 val = -tlv[2] / step;
1940 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001941 return val;
1942}
1943
1944/* call kctl->put with the given value(s) */
1945static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1946{
1947 struct snd_ctl_elem_value *ucontrol;
1948 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1949 if (!ucontrol)
1950 return -ENOMEM;
1951 ucontrol->value.integer.value[0] = val;
1952 ucontrol->value.integer.value[1] = val;
1953 kctl->put(kctl, ucontrol);
1954 kfree(ucontrol);
1955 return 0;
1956}
1957
1958/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001959static int init_slave_0dB(struct hda_codec *codec,
1960 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001961{
Takashi Iwai6194b992014-06-06 18:12:16 +02001962 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001963 if (offset > 0)
1964 put_kctl_with_value(slave, offset);
1965 return 0;
1966}
1967
1968/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001969static int init_slave_unmute(struct hda_codec *codec,
1970 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001971{
1972 return put_kctl_with_value(slave, 1);
1973}
1974
Takashi Iwaie8750942014-06-30 14:02:39 +02001975static int add_slave(struct hda_codec *codec,
1976 void *data, struct snd_kcontrol *slave)
1977{
1978 return snd_ctl_add_slave(data, slave);
1979}
1980
Takashi Iwaid5191e52009-11-16 14:58:17 +01001981/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001982 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001983 * @codec: HD-audio codec
1984 * @name: vmaster control name
1985 * @tlv: TLV data (optional)
1986 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001987 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001988 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001989 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001990 *
1991 * Create a virtual master control with the given name. The TLV data
1992 * must be either NULL or a valid data.
1993 *
1994 * @slaves is a NULL-terminated array of strings, each of which is a
1995 * slave control name. All controls with these names are assigned to
1996 * the new virtual master control.
1997 *
1998 * This function returns zero if successful or a negative error code.
1999 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002000int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002001 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002002 const char *suffix, bool init_slave_vol,
2003 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002004{
2005 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002006 int err;
2007
Takashi Iwai29e58532012-03-12 12:25:03 +01002008 if (ctl_ret)
2009 *ctl_ret = NULL;
2010
Takashi Iwai9322ca52012-02-03 14:28:01 +01002011 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002012 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002013 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002014 return 0;
2015 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002016 kctl = snd_ctl_make_virtual_master(name, tlv);
2017 if (!kctl)
2018 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002019 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002020 if (err < 0)
2021 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002022
Takashi Iwaie8750942014-06-30 14:02:39 +02002023 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002024 if (err < 0)
2025 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002026
2027 /* init with master mute & zero volume */
2028 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002029 if (init_slave_vol) {
2030 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002031 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002032 tlv ? init_slave_0dB : init_slave_unmute, &step);
2033 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002034
Takashi Iwai29e58532012-03-12 12:25:03 +01002035 if (ctl_ret)
2036 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002037 return 0;
2038}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002039EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002040
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002041/*
2042 * mute-LED control using vmaster
2043 */
2044static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2045 struct snd_ctl_elem_info *uinfo)
2046{
2047 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002048 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002049 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002050
Takashi Iwai3ff72212014-10-20 18:17:28 +02002051 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002052}
2053
2054static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2055 struct snd_ctl_elem_value *ucontrol)
2056{
2057 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2058 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2059 return 0;
2060}
2061
2062static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2063 struct snd_ctl_elem_value *ucontrol)
2064{
2065 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2066 unsigned int old_mode = hook->mute_mode;
2067
2068 hook->mute_mode = ucontrol->value.enumerated.item[0];
2069 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2070 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2071 if (old_mode == hook->mute_mode)
2072 return 0;
2073 snd_hda_sync_vmaster_hook(hook);
2074 return 1;
2075}
2076
2077static struct snd_kcontrol_new vmaster_mute_mode = {
2078 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2079 .name = "Mute-LED Mode",
2080 .info = vmaster_mute_mode_info,
2081 .get = vmaster_mute_mode_get,
2082 .put = vmaster_mute_mode_put,
2083};
2084
Takashi Iwaiee52e562015-04-27 10:36:11 +02002085/* meta hook to call each driver's vmaster hook */
2086static void vmaster_hook(void *private_data, int enabled)
2087{
2088 struct hda_vmaster_mute_hook *hook = private_data;
2089
2090 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2091 enabled = hook->mute_mode;
2092 hook->hook(hook->codec, enabled);
2093}
2094
Takashi Iwai95a962c2014-10-29 16:03:58 +01002095/**
2096 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2097 * @codec: the HDA codec
2098 * @hook: the vmaster hook object
2099 * @expose_enum_ctl: flag to create an enum ctl
2100 *
2101 * Add a mute-LED hook with the given vmaster switch kctl.
2102 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2103 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002104 */
2105int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002106 struct hda_vmaster_mute_hook *hook,
2107 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002108{
2109 struct snd_kcontrol *kctl;
2110
2111 if (!hook->hook || !hook->sw_kctl)
2112 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002113 hook->codec = codec;
2114 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002115 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735c2012-03-13 07:55:10 +01002116 if (!expose_enum_ctl)
2117 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002118 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2119 if (!kctl)
2120 return -ENOMEM;
2121 return snd_hda_ctl_add(codec, 0, kctl);
2122}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002123EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002124
Takashi Iwai95a962c2014-10-29 16:03:58 +01002125/**
2126 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2127 * @hook: the vmaster hook
2128 *
2129 * Call the hook with the current value for synchronization.
2130 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002131 */
2132void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2133{
2134 if (!hook->hook || !hook->codec)
2135 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002136 /* don't call vmaster hook in the destructor since it might have
2137 * been already destroyed
2138 */
2139 if (hook->codec->bus->shutdown)
2140 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002141 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002143EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002144
2145
Takashi Iwaid5191e52009-11-16 14:58:17 +01002146/**
2147 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002148 * @kcontrol: referred ctl element
2149 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150 *
2151 * The control element is supposed to have the private_value field
2152 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2153 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 int chs = get_amp_channels(kcontrol);
2158
2159 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2160 uinfo->count = chs == 3 ? 2 : 1;
2161 uinfo->value.integer.min = 0;
2162 uinfo->value.integer.max = 1;
2163 return 0;
2164}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002165EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Takashi Iwaid5191e52009-11-16 14:58:17 +01002167/**
2168 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002169 * @kcontrol: ctl element
2170 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002171 *
2172 * The control element is supposed to have the private_value field
2173 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002175int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2179 hda_nid_t nid = get_amp_nid(kcontrol);
2180 int chs = get_amp_channels(kcontrol);
2181 int dir = get_amp_direction(kcontrol);
2182 int idx = get_amp_index(kcontrol);
2183 long *valp = ucontrol->value.integer.value;
2184
2185 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002186 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002187 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002189 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002190 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return 0;
2192}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002193EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195/**
2196 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002197 * @kcontrol: ctl element
2198 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002199 *
2200 * The control element is supposed to have the private_value field
2201 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2202 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002203int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2207 hda_nid_t nid = get_amp_nid(kcontrol);
2208 int chs = get_amp_channels(kcontrol);
2209 int dir = get_amp_direction(kcontrol);
2210 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 long *valp = ucontrol->value.integer.value;
2212 int change = 0;
2213
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002214 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002215 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2216 HDA_AMP_MUTE,
2217 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002218 valp++;
2219 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002220 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002221 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2222 HDA_AMP_MUTE,
2223 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002224 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return change;
2226}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002227EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*
Takashi Iwai985be542005-11-02 18:26:49 +01002230 * bound volume controls
2231 *
2232 * bind multiple volumes (# indices, from 0)
2233 */
2234
2235#define AMP_VAL_IDX_SHIFT 19
2236#define AMP_VAL_IDX_MASK (0x0f<<19)
2237
Takashi Iwaid5191e52009-11-16 14:58:17 +01002238/**
2239 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002240 * @kcontrol: ctl element
2241 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002242 *
2243 * The control element is supposed to have the private_value field
2244 * set up via HDA_BIND_MUTE*() macros.
2245 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002246int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002248{
2249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2250 unsigned long pval;
2251 int err;
2252
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002253 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002254 pval = kcontrol->private_value;
2255 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2256 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2257 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002258 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002259 return err;
2260}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002261EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002262
Takashi Iwaid5191e52009-11-16 14:58:17 +01002263/**
2264 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002265 * @kcontrol: ctl element
2266 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267 *
2268 * The control element is supposed to have the private_value field
2269 * set up via HDA_BIND_MUTE*() macros.
2270 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002271int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2272 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002273{
2274 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2275 unsigned long pval;
2276 int i, indices, err = 0, change = 0;
2277
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002278 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002279 pval = kcontrol->private_value;
2280 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2281 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002282 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2283 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002284 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2285 if (err < 0)
2286 break;
2287 change |= err;
2288 }
2289 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002290 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002291 return err < 0 ? err : change;
2292}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002293EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002294
Takashi Iwaid5191e52009-11-16 14:58:17 +01002295/**
2296 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002297 * @kcontrol: referred ctl element
2298 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002299 *
2300 * The control element is supposed to have the private_value field
2301 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002302 */
2303int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2304 struct snd_ctl_elem_info *uinfo)
2305{
2306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2307 struct hda_bind_ctls *c;
2308 int err;
2309
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002310 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002311 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002312 kcontrol->private_value = *c->values;
2313 err = c->ops->info(kcontrol, uinfo);
2314 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002315 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002316 return err;
2317}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002318EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002319
Takashi Iwaid5191e52009-11-16 14:58:17 +01002320/**
2321 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002322 * @kcontrol: ctl element
2323 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002324 *
2325 * The control element is supposed to have the private_value field
2326 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2327 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002328int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_value *ucontrol)
2330{
2331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2332 struct hda_bind_ctls *c;
2333 int err;
2334
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002335 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002336 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002337 kcontrol->private_value = *c->values;
2338 err = c->ops->get(kcontrol, ucontrol);
2339 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002340 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002341 return err;
2342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002343EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002344
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345/**
2346 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002347 * @kcontrol: ctl element
2348 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349 *
2350 * The control element is supposed to have the private_value field
2351 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2352 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002353int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2355{
2356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2357 struct hda_bind_ctls *c;
2358 unsigned long *vals;
2359 int err = 0, change = 0;
2360
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002361 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002362 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002363 for (vals = c->values; *vals; vals++) {
2364 kcontrol->private_value = *vals;
2365 err = c->ops->put(kcontrol, ucontrol);
2366 if (err < 0)
2367 break;
2368 change |= err;
2369 }
2370 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002371 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002372 return err < 0 ? err : change;
2373}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002374EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002375
Takashi Iwaid5191e52009-11-16 14:58:17 +01002376/**
2377 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002378 * @kcontrol: ctl element
2379 * @op_flag: operation flag
2380 * @size: byte size of input TLV
2381 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002382 *
2383 * The control element is supposed to have the private_value field
2384 * set up via HDA_BIND_VOL() macro.
2385 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002386int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2387 unsigned int size, unsigned int __user *tlv)
2388{
2389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2390 struct hda_bind_ctls *c;
2391 int err;
2392
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002393 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002394 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002395 kcontrol->private_value = *c->values;
2396 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2397 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002398 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002399 return err;
2400}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002401EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002402
2403struct hda_ctl_ops snd_hda_bind_vol = {
2404 .info = snd_hda_mixer_amp_volume_info,
2405 .get = snd_hda_mixer_amp_volume_get,
2406 .put = snd_hda_mixer_amp_volume_put,
2407 .tlv = snd_hda_mixer_amp_tlv
2408};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002409EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002410
2411struct hda_ctl_ops snd_hda_bind_sw = {
2412 .info = snd_hda_mixer_amp_switch_info,
2413 .get = snd_hda_mixer_amp_switch_get,
2414 .put = snd_hda_mixer_amp_switch_put,
2415 .tlv = snd_hda_mixer_amp_tlv
2416};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002417EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002418
2419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * SPDIF out controls
2421 */
2422
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2427 uinfo->count = 1;
2428 return 0;
2429}
2430
Takashi Iwai0ba21762007-04-16 11:29:14 +02002431static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2435 IEC958_AES0_NONAUDIO |
2436 IEC958_AES0_CON_EMPHASIS_5015 |
2437 IEC958_AES0_CON_NOT_COPYRIGHT;
2438 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2439 IEC958_AES1_CON_ORIGINAL;
2440 return 0;
2441}
2442
Takashi Iwai0ba21762007-04-16 11:29:14 +02002443static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2447 IEC958_AES0_NONAUDIO |
2448 IEC958_AES0_PRO_EMPHASIS_5015;
2449 return 0;
2450}
2451
Takashi Iwai0ba21762007-04-16 11:29:14 +02002452static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002456 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002457 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002459 mutex_lock(&codec->spdif_mutex);
2460 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002461 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2462 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2463 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2464 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002465 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 return 0;
2468}
2469
2470/* convert from SPDIF status bits to HDA SPDIF bits
2471 * bit 0 (DigEn) is always set zero (to be filled later)
2472 */
2473static unsigned short convert_from_spdif_status(unsigned int sbits)
2474{
2475 unsigned short val = 0;
2476
2477 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002478 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002480 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002482 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2483 IEC958_AES0_PRO_EMPHASIS_5015)
2484 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2487 IEC958_AES0_CON_EMPHASIS_5015)
2488 val |= AC_DIG1_EMPHASIS;
2489 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2490 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002492 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2494 }
2495 return val;
2496}
2497
2498/* convert to SPDIF status bits from HDA SPDIF bits
2499 */
2500static unsigned int convert_to_spdif_status(unsigned short val)
2501{
2502 unsigned int sbits = 0;
2503
Takashi Iwai0ba21762007-04-16 11:29:14 +02002504 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 sbits |= IEC958_AES0_PROFESSIONAL;
2508 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002509 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2511 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002512 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002514 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002516 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2518 sbits |= val & (0x7f << 8);
2519 }
2520 return sbits;
2521}
2522
Takashi Iwai2f728532008-09-25 16:32:41 +02002523/* set digital convert verbs both for the given NID and its slaves */
2524static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002525 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002526{
Takashi Iwaidda14412011-05-02 11:29:30 +02002527 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002528
Takashi Iwaia551d912015-02-26 12:34:49 +01002529 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2530 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002531 d = codec->slave_dig_outs;
2532 if (!d)
2533 return;
2534 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002535 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002536 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002537}
2538
2539static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2540 int dig1, int dig2)
2541{
Takashi Iwaia551d912015-02-26 12:34:49 +01002542 unsigned int mask = 0;
2543 unsigned int val = 0;
2544
2545 if (dig1 != -1) {
2546 mask |= 0xff;
2547 val = dig1;
2548 }
2549 if (dig2 != -1) {
2550 mask |= 0xff00;
2551 val |= dig2 << 8;
2552 }
2553 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002554}
2555
Takashi Iwai0ba21762007-04-16 11:29:14 +02002556static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2557 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002560 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002561 struct hda_spdif_out *spdif;
2562 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 unsigned short val;
2564 int change;
2565
Ingo Molnar62932df2006-01-16 16:34:20 +01002566 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002567 spdif = snd_array_elem(&codec->spdif_out, idx);
2568 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002569 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2571 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2572 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002573 val = convert_from_spdif_status(spdif->status);
2574 val |= spdif->ctls & 1;
2575 change = spdif->ctls != val;
2576 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002577 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002578 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002579 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return change;
2581}
2582
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002583#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Takashi Iwai0ba21762007-04-16 11:29:14 +02002585static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002589 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002590 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002592 mutex_lock(&codec->spdif_mutex);
2593 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002594 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002595 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598
Stephen Warren74b654c2011-06-01 11:14:18 -06002599static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2600 int dig1, int dig2)
2601{
2602 set_dig_out_convert(codec, nid, dig1, dig2);
2603 /* unmute amp switch (if any) */
2604 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2605 (dig1 & AC_DIG1_ENABLE))
2606 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2607 HDA_AMP_MUTE, 0);
2608}
2609
Takashi Iwai0ba21762007-04-16 11:29:14 +02002610static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002614 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002615 struct hda_spdif_out *spdif;
2616 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 unsigned short val;
2618 int change;
2619
Ingo Molnar62932df2006-01-16 16:34:20 +01002620 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002621 spdif = snd_array_elem(&codec->spdif_out, idx);
2622 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002623 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002625 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002626 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002627 spdif->ctls = val;
2628 if (change && nid != (u16)-1)
2629 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002630 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return change;
2632}
2633
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002634static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 {
2636 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2637 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002638 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 .info = snd_hda_spdif_mask_info,
2640 .get = snd_hda_spdif_cmask_get,
2641 },
2642 {
2643 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002645 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .info = snd_hda_spdif_mask_info,
2647 .get = snd_hda_spdif_pmask_get,
2648 },
2649 {
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002651 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 .info = snd_hda_spdif_mask_info,
2653 .get = snd_hda_spdif_default_get,
2654 .put = snd_hda_spdif_default_put,
2655 },
2656 {
2657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002658 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .info = snd_hda_spdif_out_switch_info,
2660 .get = snd_hda_spdif_out_switch_get,
2661 .put = snd_hda_spdif_out_switch_put,
2662 },
2663 { } /* end */
2664};
2665
2666/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002667 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002669 * @associated_nid: NID that new ctls associated with
2670 * @cvt_nid: converter NID
2671 * @type: HDA_PCM_TYPE_*
2672 * Creates controls related with the digital output.
2673 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 *
2675 * Returns 0 if successful, or a negative error code.
2676 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002677int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2678 hda_nid_t associated_nid,
2679 hda_nid_t cvt_nid,
2680 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002683 struct snd_kcontrol *kctl;
2684 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002685 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002686 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002687 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002688 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002689 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002691 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002692 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002693 idx = spdif_index;
2694 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002695 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002696 /* suppose a single SPDIF device */
2697 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2698 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2699 if (!kctl)
2700 break;
2701 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002702 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002703 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002704 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002705 if (!bus->primary_dig_out_type)
2706 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002707
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002708 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002709 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002710 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002711 return -EBUSY;
2712 }
Stephen Warren7c935972011-06-01 11:14:17 -06002713 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002714 if (!spdif)
2715 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2717 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002718 if (!kctl)
2719 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002720 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002721 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002722 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return err;
2725 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002726 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002727 snd_hdac_regmap_read(&codec->core, cvt_nid,
2728 AC_VERB_GET_DIGI_CONVERT_1, &val);
2729 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002730 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return 0;
2732}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002733EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Takashi Iwai95a962c2014-10-29 16:03:58 +01002735/**
2736 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2737 * @codec: the HDA codec
2738 * @nid: widget NID
2739 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002740 * call within spdif_mutex lock
2741 */
Stephen Warren7c935972011-06-01 11:14:17 -06002742struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2743 hda_nid_t nid)
2744{
2745 int i;
2746 for (i = 0; i < codec->spdif_out.used; i++) {
2747 struct hda_spdif_out *spdif =
2748 snd_array_elem(&codec->spdif_out, i);
2749 if (spdif->nid == nid)
2750 return spdif;
2751 }
2752 return NULL;
2753}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002754EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002755
Takashi Iwai95a962c2014-10-29 16:03:58 +01002756/**
2757 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2758 * @codec: the HDA codec
2759 * @idx: the SPDIF ctl index
2760 *
2761 * Unassign the widget from the given SPDIF control.
2762 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002763void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2764{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002765 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002766
2767 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002768 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002769 spdif->nid = (u16)-1;
2770 mutex_unlock(&codec->spdif_mutex);
2771}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002772EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002773
Takashi Iwai95a962c2014-10-29 16:03:58 +01002774/**
2775 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2776 * @codec: the HDA codec
2777 * @idx: the SPDIF ctl idx
2778 * @nid: widget NID
2779 *
2780 * Assign the widget to the SPDIF control with the given index.
2781 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002782void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2783{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002784 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002785 unsigned short val;
2786
2787 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002788 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002789 if (spdif->nid != nid) {
2790 spdif->nid = nid;
2791 val = spdif->ctls;
2792 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2793 }
2794 mutex_unlock(&codec->spdif_mutex);
2795}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002796EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002799 * SPDIF sharing with analog output
2800 */
2801static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2803{
2804 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2805 ucontrol->value.integer.value[0] = mout->share_spdif;
2806 return 0;
2807}
2808
2809static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_value *ucontrol)
2811{
2812 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2813 mout->share_spdif = !!ucontrol->value.integer.value[0];
2814 return 0;
2815}
2816
2817static struct snd_kcontrol_new spdif_share_sw = {
2818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2819 .name = "IEC958 Default PCM Playback Switch",
2820 .info = snd_ctl_boolean_mono_info,
2821 .get = spdif_share_sw_get,
2822 .put = spdif_share_sw_put,
2823};
2824
Takashi Iwaid5191e52009-11-16 14:58:17 +01002825/**
2826 * snd_hda_create_spdif_share_sw - create Default PCM switch
2827 * @codec: the HDA codec
2828 * @mout: multi-out instance
2829 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002830int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2831 struct hda_multi_out *mout)
2832{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002833 struct snd_kcontrol *kctl;
2834
Takashi Iwai9a081602008-02-12 18:37:26 +01002835 if (!mout->dig_out_nid)
2836 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002837
2838 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2839 if (!kctl)
2840 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002841 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002842 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002843}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002844EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002845
2846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * SPDIF input
2848 */
2849
2850#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2851
Takashi Iwai0ba21762007-04-16 11:29:14 +02002852static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2856
2857 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2858 return 0;
2859}
2860
Takashi Iwai0ba21762007-04-16 11:29:14 +02002861static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2865 hda_nid_t nid = kcontrol->private_value;
2866 unsigned int val = !!ucontrol->value.integer.value[0];
2867 int change;
2868
Ingo Molnar62932df2006-01-16 16:34:20 +01002869 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002871 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002873 snd_hdac_regmap_write(&codec->core, nid,
2874 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002876 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return change;
2878}
2879
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2884 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002885 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 unsigned int sbits;
2887
Takashi Iwaia551d912015-02-26 12:34:49 +01002888 snd_hdac_regmap_read(&codec->core, nid,
2889 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 sbits = convert_to_spdif_status(val);
2891 ucontrol->value.iec958.status[0] = sbits;
2892 ucontrol->value.iec958.status[1] = sbits >> 8;
2893 ucontrol->value.iec958.status[2] = sbits >> 16;
2894 ucontrol->value.iec958.status[3] = sbits >> 24;
2895 return 0;
2896}
2897
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002898static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 {
2900 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002901 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 .info = snd_hda_spdif_in_switch_info,
2903 .get = snd_hda_spdif_in_switch_get,
2904 .put = snd_hda_spdif_in_switch_put,
2905 },
2906 {
2907 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2908 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002909 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 .info = snd_hda_spdif_mask_info,
2911 .get = snd_hda_spdif_in_status_get,
2912 },
2913 { } /* end */
2914};
2915
2916/**
2917 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2918 * @codec: the HDA codec
2919 * @nid: audio in widget NID
2920 *
2921 * Creates controls related with the SPDIF input.
2922 * Called from each patch supporting the SPDIF in.
2923 *
2924 * Returns 0 if successful, or a negative error code.
2925 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002926int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
2928 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002929 struct snd_kcontrol *kctl;
2930 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002931 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Takashi Iwaidcda5802012-10-12 17:24:51 +02002933 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002934 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002935 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002936 return -EBUSY;
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2939 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002940 if (!kctl)
2941 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002943 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002944 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return err;
2946 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002947 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002948 snd_hda_codec_read(codec, nid, 0,
2949 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return 0;
2952}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002953EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Takashi Iwai95a962c2014-10-29 16:03:58 +01002955/**
2956 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2957 * @codec: the HDA codec
2958 * @fg: function group (not used now)
2959 * @power_state: the power state to set (AC_PWRST_*)
2960 *
2961 * Set the given power state to all widgets that have the power control.
2962 * If the codec has power_filter set, it evaluates the power state and
2963 * filter out if it's unchanged as D3.
2964 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002965void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002966 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002967{
Takashi Iwai7639a062015-03-03 10:07:24 +01002968 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002969
Takashi Iwai7639a062015-03-03 10:07:24 +01002970 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002971 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002972 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002973 if (!(wcaps & AC_WCAP_POWER))
2974 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002975 if (codec->power_filter) {
2976 state = codec->power_filter(codec, nid, power_state);
2977 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002978 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002979 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002980 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002981 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002982 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002983}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002984EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002985
2986/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002987 * wait until the state is reached, returns the current state
2988 */
2989static unsigned int hda_sync_power_state(struct hda_codec *codec,
2990 hda_nid_t fg,
2991 unsigned int power_state)
2992{
2993 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2994 unsigned int state, actual_state;
2995
2996 for (;;) {
2997 state = snd_hda_codec_read(codec, fg, 0,
2998 AC_VERB_GET_POWER_STATE, 0);
2999 if (state & AC_PWRST_ERROR)
3000 break;
3001 actual_state = (state >> 4) & 0x0f;
3002 if (actual_state == power_state)
3003 break;
3004 if (time_after_eq(jiffies, end_time))
3005 break;
3006 /* wait until the codec reachs to the target state */
3007 msleep(1);
3008 }
3009 return state;
3010}
3011
Takashi Iwai95a962c2014-10-29 16:03:58 +01003012/**
3013 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3014 * @codec: the HDA codec
3015 * @nid: widget NID
3016 * @power_state: power state to evalue
3017 *
3018 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3019 * This can be used a codec power_filter callback.
3020 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003021unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3022 hda_nid_t nid,
3023 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003024{
Takashi Iwai7639a062015-03-03 10:07:24 +01003025 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003026 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003027 if (power_state == AC_PWRST_D3 &&
3028 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3029 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3030 int eapd = snd_hda_codec_read(codec, nid, 0,
3031 AC_VERB_GET_EAPD_BTLENABLE, 0);
3032 if (eapd & 0x02)
3033 return AC_PWRST_D0;
3034 }
3035 return power_state;
3036}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003037EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003038
Takashi Iwai432c6412012-08-28 09:59:20 -07003039/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003040 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003041 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003042static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003043 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003044{
Takashi Iwai7639a062015-03-03 10:07:24 +01003045 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003046 int count;
3047 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003048 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003049
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003050 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003051 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003052 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003053 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003054 else if (codec->depop_delay > 0)
3055 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003056 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003057 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003058
3059 /* repeat power states setting at most 10 times*/
3060 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003061 if (codec->patch_ops.set_power_state)
3062 codec->patch_ops.set_power_state(codec, fg,
3063 power_state);
3064 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003065 state = power_state;
3066 if (codec->power_filter)
3067 state = codec->power_filter(codec, fg, state);
3068 if (state == power_state || power_state != AC_PWRST_D3)
3069 snd_hda_codec_read(codec, fg, flags,
3070 AC_VERB_SET_POWER_STATE,
3071 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003072 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003073 }
3074 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003075 if (!(state & AC_PWRST_ERROR))
3076 break;
3077 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003078
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003079 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003080}
Takashi Iwai54d17402005-11-21 16:33:22 +01003081
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003082/* sync power states of all widgets;
3083 * this is called at the end of codec parsing
3084 */
3085static void sync_power_up_states(struct hda_codec *codec)
3086{
Takashi Iwai7639a062015-03-03 10:07:24 +01003087 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003088
Takashi Iwaiba615b82013-03-13 14:47:21 +01003089 /* don't care if no filter is used */
3090 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003091 return;
3092
Takashi Iwai7639a062015-03-03 10:07:24 +01003093 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003094 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003095 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003096 if (!(wcaps & AC_WCAP_POWER))
3097 continue;
3098 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3099 if (target == AC_PWRST_D0)
3100 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003101 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003102 snd_hda_codec_write(codec, nid, 0,
3103 AC_VERB_SET_POWER_STATE, target);
3104 }
3105}
3106
Takashi Iwai648a8d22014-02-25 10:38:13 +01003107#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003108/* execute additional init verbs */
3109static void hda_exec_init_verbs(struct hda_codec *codec)
3110{
3111 if (codec->init_verbs.list)
3112 snd_hda_sequence_write(codec, codec->init_verbs.list);
3113}
3114#else
3115static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3116#endif
3117
Takashi Iwai2a439522011-07-26 09:52:50 +02003118#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003119/* update the power on/off account with the current jiffies */
3120static void update_power_acct(struct hda_codec *codec, bool on)
3121{
3122 unsigned long delta = jiffies - codec->power_jiffies;
3123
3124 if (on)
3125 codec->power_on_acct += delta;
3126 else
3127 codec->power_off_acct += delta;
3128 codec->power_jiffies += delta;
3129}
3130
3131void snd_hda_update_power_acct(struct hda_codec *codec)
3132{
3133 update_power_acct(codec, hda_codec_is_power_on(codec));
3134}
3135
Takashi Iwaicb53c622007-08-10 17:21:45 +02003136/*
3137 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003138 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003139 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003140static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003141{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003142 unsigned int state;
3143
Takashi Iwai7639a062015-03-03 10:07:24 +01003144 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003145
Takashi Iwaicb53c622007-08-10 17:21:45 +02003146 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003147 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003148 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003149 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003150 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003151 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003152 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003153}
3154
3155/*
3156 * kick up codec; used both from PM and power-save
3157 */
3158static void hda_call_codec_resume(struct hda_codec *codec)
3159{
Takashi Iwai7639a062015-03-03 10:07:24 +01003160 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003161
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003162 if (codec->core.regmap)
3163 regcache_mark_dirty(codec->core.regmap);
3164
Takashi Iwaicc72da72015-02-19 16:00:22 +01003165 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003166
Takashi Iwaid8193872012-08-31 07:54:38 -07003167 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003168 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003169 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003170 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003171 if (codec->patch_ops.resume)
3172 codec->patch_ops.resume(codec);
3173 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003174 if (codec->patch_ops.init)
3175 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003176 if (codec->core.regmap)
3177 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003178 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003179
3180 if (codec->jackpoll_interval)
3181 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003182 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003183 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003184 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003185}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003186
Takashi Iwaicc72da72015-02-19 16:00:22 +01003187static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003188{
3189 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003190 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003191 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003192
3193 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003194 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3195 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003196 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003197 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3198 (state & AC_PWRST_CLK_STOP_OK))
3199 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003200 return 0;
3201}
3202
Takashi Iwaicc72da72015-02-19 16:00:22 +01003203static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003204{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003205 struct hda_codec *codec = dev_to_hda_codec(dev);
3206
Takashi Iwai7639a062015-03-03 10:07:24 +01003207 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003208 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003209 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003210 return 0;
3211}
Takashi Iwai2a439522011-07-26 09:52:50 +02003212#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003213
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003214/* referred in hda_bind.c */
3215const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003216 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3217 pm_runtime_force_resume)
3218 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3219 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003220};
Takashi Iwai54d17402005-11-21 16:33:22 +01003221
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003222/*
3223 * add standard channel maps if not specified
3224 */
3225static int add_std_chmaps(struct hda_codec *codec)
3226{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003227 struct hda_pcm *pcm;
3228 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003229
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003230 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003231 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003232 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003233 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003234 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003235
Sudip Mukherjee751e2212015-04-02 16:51:43 +05303236 if (!pcm || pcm->own_chmap ||
3237 !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003238 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003239 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003240 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003241 hinfo->channels_max,
3242 0, &chmap);
3243 if (err < 0)
3244 return err;
3245 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3246 }
3247 }
3248 return 0;
3249}
3250
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003251/* default channel maps for 2.1 speakers;
3252 * since HD-audio supports only stereo, odd number channels are omitted
3253 */
3254const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3255 { .channels = 2,
3256 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3257 { .channels = 4,
3258 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3259 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3260 { }
3261};
3262EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3263
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003264int snd_hda_codec_build_controls(struct hda_codec *codec)
3265{
3266 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003267 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003268 /* continue to initialize... */
3269 if (codec->patch_ops.init)
3270 err = codec->patch_ops.init(codec);
3271 if (!err && codec->patch_ops.build_controls)
3272 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003273 if (err < 0)
3274 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003275
3276 /* we create chmaps here instead of build_pcms */
3277 err = add_std_chmaps(codec);
3278 if (err < 0)
3279 return err;
3280
David Henningsson26a6cb62012-10-09 15:04:21 +02003281 if (codec->jackpoll_interval)
3282 hda_jackpoll_work(&codec->jackpoll_work.work);
3283 else
3284 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003285 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return 0;
3287}
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289/*
3290 * stream formats
3291 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003292struct hda_rate_tbl {
3293 unsigned int hz;
3294 unsigned int alsa_bits;
3295 unsigned int hda_fmt;
3296};
3297
Takashi Iwai92f10b32010-08-03 14:21:00 +02003298/* rate = base * mult / div */
3299#define HDA_RATE(base, mult, div) \
3300 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3301 (((div) - 1) << AC_FMT_DIV_SHIFT))
3302
Takashi Iwaibefdf312005-08-22 13:57:55 +02003303static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003305
3306 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003307 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3308 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3309 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3310 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3311 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3312 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3313 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3314 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3315 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3316 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3317 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003318#define AC_PAR_PCM_RATE_BITS 11
3319 /* up to bits 10, 384kHZ isn't supported properly */
3320
3321 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003322 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003323
Takashi Iwaibefdf312005-08-22 13:57:55 +02003324 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325};
3326
3327/**
3328 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003329 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 * @rate: the sample rate
3331 * @channels: the number of channels
3332 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3333 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003334 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 *
3336 * Calculate the format bitset from the given rate, channels and th PCM format.
3337 *
3338 * Return zero if invalid.
3339 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003340unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3341 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 unsigned int channels,
3343 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003344 unsigned int maxbps,
3345 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 int i;
3348 unsigned int val = 0;
3349
Takashi Iwaibefdf312005-08-22 13:57:55 +02003350 for (i = 0; rate_bits[i].hz; i++)
3351 if (rate_bits[i].hz == rate) {
3352 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 break;
3354 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003355 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003356 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return 0;
3358 }
3359
3360 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003361 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return 0;
3363 }
3364 val |= channels - 1;
3365
3366 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003367 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003368 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003369 break;
3370 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003371 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 case 20:
3374 case 24:
3375 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003376 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003377 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003379 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003381 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 break;
3383 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003384 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003385 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return 0;
3387 }
3388
Anssi Hannula32c168c2010-08-03 13:28:57 +03003389 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003390 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return val;
3393}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003394EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003396static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003397{
3398 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003399 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003400 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3401 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3402 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003403 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003404 if (!val || val == -1)
3405 return 0;
3406 return val;
3407}
3408
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003409static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003410{
3411 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3412 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003413 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003414 if (!streams || streams == -1)
3415 return 0;
3416 return streams;
3417}
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419/**
3420 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3421 * @codec: the HDA codec
3422 * @nid: NID to query
3423 * @ratesp: the pointer to store the detected rate bitflags
3424 * @formatsp: the pointer to store the detected formats
3425 * @bpsp: the pointer to store the detected format widths
3426 *
3427 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3428 * or @bsps argument is ignored.
3429 *
3430 * Returns 0 if successful, otherwise a negative error code.
3431 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003432int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3434{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003435 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003437 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003438 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 if (ratesp) {
3441 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003442 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003444 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003446 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003447 codec_err(codec,
3448 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3449 nid, val,
3450 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003451 return -EIO;
3452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 *ratesp = rates;
3454 }
3455
3456 if (formatsp || bpsp) {
3457 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003458 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003460 streams = query_stream_param(codec, nid);
3461 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 bps = 0;
3465 if (streams & AC_SUPFMT_PCM) {
3466 if (val & AC_SUPPCM_BITS_8) {
3467 formats |= SNDRV_PCM_FMTBIT_U8;
3468 bps = 8;
3469 }
3470 if (val & AC_SUPPCM_BITS_16) {
3471 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3472 bps = 16;
3473 }
3474 if (wcaps & AC_WCAP_DIGITAL) {
3475 if (val & AC_SUPPCM_BITS_32)
3476 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3477 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3478 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3479 if (val & AC_SUPPCM_BITS_24)
3480 bps = 24;
3481 else if (val & AC_SUPPCM_BITS_20)
3482 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003483 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3484 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3486 if (val & AC_SUPPCM_BITS_32)
3487 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 else if (val & AC_SUPPCM_BITS_24)
3489 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003490 else if (val & AC_SUPPCM_BITS_20)
3491 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
3493 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003494#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003495 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003497 if (!bps)
3498 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003499 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003500#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003501 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003502 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 /* temporary hack: we have still no proper support
3504 * for the direct AC3 stream...
3505 */
3506 formats |= SNDRV_PCM_FMTBIT_U8;
3507 bps = 8;
3508 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003509 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003510 codec_err(codec,
3511 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3512 nid, val,
3513 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3514 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003515 return -EIO;
3516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 if (formatsp)
3518 *formatsp = formats;
3519 if (bpsp)
3520 *bpsp = bps;
3521 }
3522
3523 return 0;
3524}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003525EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003528 * snd_hda_is_supported_format - Check the validity of the format
3529 * @codec: HD-audio codec
3530 * @nid: NID to check
3531 * @format: the HD-audio format value to check
3532 *
3533 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 *
3535 * Returns 1 if supported, 0 if not.
3536 */
3537int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3538 unsigned int format)
3539{
3540 int i;
3541 unsigned int val = 0, rate, stream;
3542
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003543 val = query_pcm_param(codec, nid);
3544 if (!val)
3545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003548 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003549 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 if (val & (1 << i))
3551 break;
3552 return 0;
3553 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003554 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return 0;
3556
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003557 stream = query_stream_param(codec, nid);
3558 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return 0;
3560
3561 if (stream & AC_SUPFMT_PCM) {
3562 switch (format & 0xf0) {
3563 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003564 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return 0;
3566 break;
3567 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003568 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return 0;
3570 break;
3571 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003572 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return 0;
3574 break;
3575 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003576 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return 0;
3578 break;
3579 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003580 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return 0;
3582 break;
3583 default:
3584 return 0;
3585 }
3586 } else {
3587 /* FIXME: check for float32 and AC3? */
3588 }
3589
3590 return 1;
3591}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003592EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594/*
3595 * PCM stuff
3596 */
3597static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3598 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003599 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 return 0;
3602}
3603
3604static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3605 struct hda_codec *codec,
3606 unsigned int stream_tag,
3607 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003608 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3611 return 0;
3612}
3613
3614static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3615 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003616 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Takashi Iwai888afa12008-03-18 09:57:50 +01003618 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return 0;
3620}
3621
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003622static int set_pcm_default_values(struct hda_codec *codec,
3623 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003625 int err;
3626
Takashi Iwai0ba21762007-04-16 11:29:14 +02003627 /* query support PCM information from the given NID */
3628 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003629 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003630 info->rates ? NULL : &info->rates,
3631 info->formats ? NULL : &info->formats,
3632 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003633 if (err < 0)
3634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636 if (info->ops.open == NULL)
3637 info->ops.open = hda_pcm_default_open_close;
3638 if (info->ops.close == NULL)
3639 info->ops.close = hda_pcm_default_open_close;
3640 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003641 if (snd_BUG_ON(!info->nid))
3642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 info->ops.prepare = hda_pcm_default_prepare;
3644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003646 if (snd_BUG_ON(!info->nid))
3647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 info->ops.cleanup = hda_pcm_default_cleanup;
3649 }
3650 return 0;
3651}
3652
Takashi Iwaieb541332010-08-06 13:48:11 +02003653/*
3654 * codec prepare/cleanup entries
3655 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003656/**
3657 * snd_hda_codec_prepare - Prepare a stream
3658 * @codec: the HDA codec
3659 * @hinfo: PCM information
3660 * @stream: stream tag to assign
3661 * @format: format id to assign
3662 * @substream: PCM substream to assign
3663 *
3664 * Calls the prepare callback set by the codec with the given arguments.
3665 * Clean up the inactive streams when successful.
3666 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003667int snd_hda_codec_prepare(struct hda_codec *codec,
3668 struct hda_pcm_stream *hinfo,
3669 unsigned int stream,
3670 unsigned int format,
3671 struct snd_pcm_substream *substream)
3672{
3673 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003674 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003675 if (hinfo->ops.prepare)
3676 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3677 substream);
3678 else
3679 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003680 if (ret >= 0)
3681 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003682 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003683 return ret;
3684}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003685EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003686
Takashi Iwai95a962c2014-10-29 16:03:58 +01003687/**
3688 * snd_hda_codec_cleanup - Prepare a stream
3689 * @codec: the HDA codec
3690 * @hinfo: PCM information
3691 * @substream: PCM substream
3692 *
3693 * Calls the cleanup callback set by the codec with the given arguments.
3694 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003695void snd_hda_codec_cleanup(struct hda_codec *codec,
3696 struct hda_pcm_stream *hinfo,
3697 struct snd_pcm_substream *substream)
3698{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003699 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003700 if (hinfo->ops.cleanup)
3701 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003702 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003703}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003704EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003705
Takashi Iwaid5191e52009-11-16 14:58:17 +01003706/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003707const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3708 "Audio", "SPDIF", "HDMI", "Modem"
3709};
3710
Takashi Iwai176d5332008-07-30 15:01:44 +02003711/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003712 * get the empty PCM device number to assign
3713 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003714static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003715{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003716 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003717 /* assigned to static slots up to dev#10; if more needed, assign
3718 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3719 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003720 static int audio_idx[HDA_PCM_NTYPES][5] = {
3721 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3722 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003723 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003724 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003725 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003726 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003727
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003728 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003729 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003730 return -EINVAL;
3731 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003732
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003733 for (i = 0; audio_idx[type][i] >= 0; i++) {
3734#ifndef CONFIG_SND_DYNAMIC_MINORS
3735 if (audio_idx[type][i] >= 8)
3736 break;
3737#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003738 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3739 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003740 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003741
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003742#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003743 /* non-fixed slots starting from 10 */
3744 for (i = 10; i < 32; i++) {
3745 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3746 return i;
3747 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003748#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003749
Takashi Iwai4e76a882014-02-25 12:21:03 +01003750 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003751 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003752#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003753 dev_warn(bus->card->dev,
3754 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003755#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003756 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003757}
3758
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003759/* call build_pcms ops of the given codec and set up the default parameters */
3760int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003761{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003762 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003763 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003764
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003765 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003766 return 0; /* already parsed */
3767
3768 if (!codec->patch_ops.build_pcms)
3769 return 0;
3770
3771 err = codec->patch_ops.build_pcms(codec);
3772 if (err < 0) {
3773 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003774 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003775 return err;
3776 }
3777
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003778 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003779 int stream;
3780
3781 for (stream = 0; stream < 2; stream++) {
3782 struct hda_pcm_stream *info = &cpcm->stream[stream];
3783
3784 if (!info->substreams)
3785 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003786 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003787 if (err < 0) {
3788 codec_warn(codec,
3789 "fail to setup default for PCM %s\n",
3790 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003791 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003792 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003793 }
3794 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003795
3796 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003797}
3798
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003799/* assign all PCMs of the given codec */
3800int snd_hda_codec_build_pcms(struct hda_codec *codec)
3801{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003802 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003803 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003804 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003805
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003806 if (snd_BUG_ON(!bus->ops.attach_pcm))
3807 return -EINVAL;
3808
3809 err = snd_hda_codec_parse_pcms(codec);
3810 if (err < 0) {
3811 snd_hda_codec_reset(codec);
3812 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003813 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003814
3815 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003816 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003817 if (cpcm->pcm)
3818 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003819 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003820 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003821
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003822 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3823 if (dev < 0)
3824 continue; /* no fatal error */
3825 cpcm->device = dev;
3826 err = bus->ops.attach_pcm(bus, codec, cpcm);
3827 if (err < 0) {
3828 codec_err(codec,
3829 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003830 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003831 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003832 }
3833 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003834
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003835 return 0;
3836}
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 * snd_hda_add_new_ctls - create controls from the array
3840 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003841 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 *
3843 * This helper function creates and add new controls in the given array.
3844 * The array must be terminated with an empty entry as terminator.
3845 *
3846 * Returns 0 if successful, or a negative error code.
3847 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003848int snd_hda_add_new_ctls(struct hda_codec *codec,
3849 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003851 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003854 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003855 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003856 if (knew->iface == -1) /* skip this codec private value */
3857 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003858 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003859 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003861 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003862 if (addr > 0)
3863 kctl->id.device = addr;
3864 if (idx > 0)
3865 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003866 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003867 if (!err)
3868 break;
3869 /* try first with another device index corresponding to
3870 * the codec addr; if it still fails (or it's the
3871 * primary codec), then try another control index
3872 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003873 if (!addr && codec->core.addr)
3874 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003875 else if (!idx && !knew->index) {
3876 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003877 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003878 if (idx <= 0)
3879 return err;
3880 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003881 return err;
3882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
3884 return 0;
3885}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003886EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Takashi Iwai83012a72012-08-24 18:38:08 +02003888#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003889static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003890{
3891 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003892
Takashi Iwaicc72da72015-02-19 16:00:22 +01003893 if (delay > 0) {
3894 pm_runtime_set_autosuspend_delay(dev, delay);
3895 pm_runtime_use_autosuspend(dev);
3896 pm_runtime_allow(dev);
3897 if (!pm_runtime_suspended(dev))
3898 pm_runtime_mark_last_busy(dev);
3899 } else {
3900 pm_runtime_dont_use_autosuspend(dev);
3901 pm_runtime_forbid(dev);
3902 }
3903}
Takashi Iwaibb573922015-02-20 09:26:04 +01003904
3905/**
3906 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3907 * @bus: HD-audio bus
3908 * @delay: autosuspend delay in msec, 0 = off
3909 *
3910 * Synchronize the runtime PM autosuspend state from the power_save option.
3911 */
3912void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3913{
3914 struct hda_codec *c;
3915
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003916 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003917 codec_set_power_save(c, delay);
3918}
3919EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003920
Takashi Iwaid5191e52009-11-16 14:58:17 +01003921/**
3922 * snd_hda_check_amp_list_power - Check the amp list and update the power
3923 * @codec: HD-audio codec
3924 * @check: the object containing an AMP list and the status
3925 * @nid: NID to check / update
3926 *
3927 * Check whether the given NID is in the amp list. If it's in the list,
3928 * check the current AMP status, and update the the power-status according
3929 * to the mute status.
3930 *
3931 * This function is supposed to be set or called from the check_power_status
3932 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003933 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003934int snd_hda_check_amp_list_power(struct hda_codec *codec,
3935 struct hda_loopback_check *check,
3936 hda_nid_t nid)
3937{
Takashi Iwai031024e2011-05-02 11:29:30 +02003938 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003939 int ch, v;
3940
3941 if (!check->amplist)
3942 return 0;
3943 for (p = check->amplist; p->nid; p++) {
3944 if (p->nid == nid)
3945 break;
3946 }
3947 if (!p->nid)
3948 return 0; /* nothing changed */
3949
3950 for (p = check->amplist; p->nid; p++) {
3951 for (ch = 0; ch < 2; ch++) {
3952 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3953 p->idx);
3954 if (!(v & HDA_AMP_MUTE) && v > 0) {
3955 if (!check->power_on) {
3956 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003957 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003958 }
3959 return 1;
3960 }
3961 }
3962 }
3963 if (check->power_on) {
3964 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003965 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003966 }
3967 return 0;
3968}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003969EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003972/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 * input MUX helper
3974 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003975
3976/**
3977 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003978 * @imux: imux helper object
3979 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003980 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003981int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3982 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
3984 unsigned int index;
3985
3986 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3987 uinfo->count = 1;
3988 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003989 if (!imux->num_items)
3990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 index = uinfo->value.enumerated.item;
3992 if (index >= imux->num_items)
3993 index = imux->num_items - 1;
3994 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3995 return 0;
3996}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003997EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Takashi Iwaid5191e52009-11-16 14:58:17 +01003999/**
4000 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004001 * @codec: the HDA codec
4002 * @imux: imux helper object
4003 * @ucontrol: pointer to get/store the data
4004 * @nid: input mux NID
4005 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004006 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004007int snd_hda_input_mux_put(struct hda_codec *codec,
4008 const struct hda_input_mux *imux,
4009 struct snd_ctl_elem_value *ucontrol,
4010 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 unsigned int *cur_val)
4012{
4013 unsigned int idx;
4014
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004015 if (!imux->num_items)
4016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 idx = ucontrol->value.enumerated.item[0];
4018 if (idx >= imux->num_items)
4019 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004020 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004022 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4023 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 *cur_val = idx;
4025 return 1;
4026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004027EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004030/**
4031 * snd_hda_enum_helper_info - Helper for simple enum ctls
4032 * @kcontrol: ctl element
4033 * @uinfo: pointer to get/store the data
4034 * @num_items: number of enum items
4035 * @texts: enum item string array
4036 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004037 * process kcontrol info callback of a simple string enum array
4038 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4039 */
4040int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4041 struct snd_ctl_elem_info *uinfo,
4042 int num_items, const char * const *texts)
4043{
4044 static const char * const texts_default[] = {
4045 "Disabled", "Enabled"
4046 };
4047
4048 if (!texts || !num_items) {
4049 num_items = 2;
4050 texts = texts_default;
4051 }
4052
Takashi Iwai3ff72212014-10-20 18:17:28 +02004053 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004054}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004055EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004056
4057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 * Multi-channel / digital-out PCM helper functions
4059 */
4060
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004061/* setup SPDIF output stream */
4062static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4063 unsigned int stream_tag, unsigned int format)
4064{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004065 struct hda_spdif_out *spdif;
4066 unsigned int curr_fmt;
4067 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004068
Laurence Darby3bef1c32012-11-03 17:00:06 +00004069 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4070 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4071 AC_VERB_GET_STREAM_FORMAT, 0);
4072 reset = codec->spdif_status_reset &&
4073 (spdif->ctls & AC_DIG1_ENABLE) &&
4074 curr_fmt != format;
4075
4076 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4077 updated */
4078 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004079 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004080 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004081 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004082 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004083 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004084 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004085 for (d = codec->slave_dig_outs; *d; d++)
4086 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4087 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004088 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004089 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004090 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004091 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004092 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004093}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004094
Takashi Iwai2f728532008-09-25 16:32:41 +02004095static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4096{
4097 snd_hda_codec_cleanup_stream(codec, nid);
4098 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004099 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004100 for (d = codec->slave_dig_outs; *d; d++)
4101 snd_hda_codec_cleanup_stream(codec, *d);
4102 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004103}
4104
Takashi Iwaid5191e52009-11-16 14:58:17 +01004105/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004106 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004107 * @codec: the HDA codec
4108 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004110int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4111 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Ingo Molnar62932df2006-01-16 16:34:20 +01004113 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004114 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4115 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004116 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004118 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return 0;
4120}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004121EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Takashi Iwaid5191e52009-11-16 14:58:17 +01004123/**
4124 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004125 * @codec: the HDA codec
4126 * @mout: hda_multi_out object
4127 * @stream_tag: stream tag to assign
4128 * @format: format id to assign
4129 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004130 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004131int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4132 struct hda_multi_out *mout,
4133 unsigned int stream_tag,
4134 unsigned int format,
4135 struct snd_pcm_substream *substream)
4136{
4137 mutex_lock(&codec->spdif_mutex);
4138 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4139 mutex_unlock(&codec->spdif_mutex);
4140 return 0;
4141}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004142EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004143
Takashi Iwaid5191e52009-11-16 14:58:17 +01004144/**
4145 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004146 * @codec: the HDA codec
4147 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004148 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004149int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4150 struct hda_multi_out *mout)
4151{
4152 mutex_lock(&codec->spdif_mutex);
4153 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4154 mutex_unlock(&codec->spdif_mutex);
4155 return 0;
4156}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004157EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004158
Takashi Iwaid5191e52009-11-16 14:58:17 +01004159/**
4160 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004161 * @codec: the HDA codec
4162 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004164int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4165 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Ingo Molnar62932df2006-01-16 16:34:20 +01004167 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004169 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004172EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Takashi Iwaid5191e52009-11-16 14:58:17 +01004174/**
4175 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004176 * @codec: the HDA codec
4177 * @mout: hda_multi_out object
4178 * @substream: PCM substream to assign
4179 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004180 *
4181 * Open analog outputs and set up the hw-constraints.
4182 * If the digital outputs can be opened as slave, open the digital
4183 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004185int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4186 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004187 struct snd_pcm_substream *substream,
4188 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
Takashi Iwai9a081602008-02-12 18:37:26 +01004190 struct snd_pcm_runtime *runtime = substream->runtime;
4191 runtime->hw.channels_max = mout->max_channels;
4192 if (mout->dig_out_nid) {
4193 if (!mout->analog_rates) {
4194 mout->analog_rates = hinfo->rates;
4195 mout->analog_formats = hinfo->formats;
4196 mout->analog_maxbps = hinfo->maxbps;
4197 } else {
4198 runtime->hw.rates = mout->analog_rates;
4199 runtime->hw.formats = mout->analog_formats;
4200 hinfo->maxbps = mout->analog_maxbps;
4201 }
4202 if (!mout->spdif_rates) {
4203 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4204 &mout->spdif_rates,
4205 &mout->spdif_formats,
4206 &mout->spdif_maxbps);
4207 }
4208 mutex_lock(&codec->spdif_mutex);
4209 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004210 if ((runtime->hw.rates & mout->spdif_rates) &&
4211 (runtime->hw.formats & mout->spdif_formats)) {
4212 runtime->hw.rates &= mout->spdif_rates;
4213 runtime->hw.formats &= mout->spdif_formats;
4214 if (mout->spdif_maxbps < hinfo->maxbps)
4215 hinfo->maxbps = mout->spdif_maxbps;
4216 } else {
4217 mout->share_spdif = 0;
4218 /* FIXME: need notify? */
4219 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004220 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004221 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4224 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4225}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004226EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Takashi Iwaid5191e52009-11-16 14:58:17 +01004228/**
4229 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004230 * @codec: the HDA codec
4231 * @mout: hda_multi_out object
4232 * @stream_tag: stream tag to assign
4233 * @format: format id to assign
4234 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004235 *
4236 * Set up the i/o for analog out.
4237 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004239int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4240 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 unsigned int stream_tag,
4242 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004243 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Takashi Iwaidda14412011-05-02 11:29:30 +02004245 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004247 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 int i;
4249
Ingo Molnar62932df2006-01-16 16:34:20 +01004250 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004251 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004252 if (mout->dig_out_nid && mout->share_spdif &&
4253 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004255 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4256 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004257 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004259 setup_dig_out_stream(codec, mout->dig_out_nid,
4260 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 } else {
4262 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004263 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004266 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004269 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4270 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004271 if (!mout->no_share_stream &&
4272 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004274 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4275 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004276 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004277 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4278 if (!mout->no_share_stream && mout->hp_out_nid[i])
4279 snd_hda_codec_setup_stream(codec,
4280 mout->hp_out_nid[i],
4281 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* surrounds */
4284 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004285 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004286 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4287 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004288 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004289 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4290 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004292
4293 /* extra surrounds */
4294 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4295 int ch = 0;
4296 if (!mout->extra_out_nid[i])
4297 break;
4298 if (chs >= (i + 1) * 2)
4299 ch = i * 2;
4300 else if (!mout->no_share_stream)
4301 break;
4302 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4303 stream_tag, ch, format);
4304 }
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return 0;
4307}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004308EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Takashi Iwaid5191e52009-11-16 14:58:17 +01004310/**
4311 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004312 * @codec: the HDA codec
4313 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004315int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4316 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Takashi Iwaidda14412011-05-02 11:29:30 +02004318 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 int i;
4320
4321 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004322 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004324 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004325 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4326 if (mout->hp_out_nid[i])
4327 snd_hda_codec_cleanup_stream(codec,
4328 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004329 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4330 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004331 snd_hda_codec_cleanup_stream(codec,
4332 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004333 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004335 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 mout->dig_out_used = 0;
4337 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004338 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return 0;
4340}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004341EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Takashi Iwai47408602012-04-20 13:06:53 +02004343/**
4344 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004345 * @codec: the HDA codec
4346 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004347 *
4348 * Guess the suitable VREF pin bits to be set as the pin-control value.
4349 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4350 */
4351unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4352{
4353 unsigned int pincap;
4354 unsigned int oldval;
4355 oldval = snd_hda_codec_read(codec, pin, 0,
4356 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4357 pincap = snd_hda_query_pin_caps(codec, pin);
4358 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4359 /* Exception: if the default pin setup is vref50, we give it priority */
4360 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4361 return AC_PINCTL_VREF_80;
4362 else if (pincap & AC_PINCAP_VREF_50)
4363 return AC_PINCTL_VREF_50;
4364 else if (pincap & AC_PINCAP_VREF_100)
4365 return AC_PINCTL_VREF_100;
4366 else if (pincap & AC_PINCAP_VREF_GRD)
4367 return AC_PINCTL_VREF_GRD;
4368 return AC_PINCTL_VREF_HIZ;
4369}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004370EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004371
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004372/**
4373 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4374 * @codec: the HDA codec
4375 * @pin: referred pin NID
4376 * @val: pin ctl value to audit
4377 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004378unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4379 hda_nid_t pin, unsigned int val)
4380{
4381 static unsigned int cap_lists[][2] = {
4382 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4383 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4384 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4385 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4386 };
4387 unsigned int cap;
4388
4389 if (!val)
4390 return 0;
4391 cap = snd_hda_query_pin_caps(codec, pin);
4392 if (!cap)
4393 return val; /* don't know what to do... */
4394
4395 if (val & AC_PINCTL_OUT_EN) {
4396 if (!(cap & AC_PINCAP_OUT))
4397 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4398 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4399 val &= ~AC_PINCTL_HP_EN;
4400 }
4401
4402 if (val & AC_PINCTL_IN_EN) {
4403 if (!(cap & AC_PINCAP_IN))
4404 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4405 else {
4406 unsigned int vcap, vref;
4407 int i;
4408 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4409 vref = val & AC_PINCTL_VREFEN;
4410 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4411 if (vref == cap_lists[i][0] &&
4412 !(vcap & cap_lists[i][1])) {
4413 if (i == ARRAY_SIZE(cap_lists) - 1)
4414 vref = AC_PINCTL_VREF_HIZ;
4415 else
4416 vref = cap_lists[i + 1][0];
4417 }
4418 }
4419 val &= ~AC_PINCTL_VREFEN;
4420 val |= vref;
4421 }
4422 }
4423
4424 return val;
4425}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004426EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004427
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004428/**
4429 * _snd_hda_pin_ctl - Helper to set pin ctl value
4430 * @codec: the HDA codec
4431 * @pin: referred pin NID
4432 * @val: pin control value to set
4433 * @cached: access over codec pinctl cache or direct write
4434 *
4435 * This function is a helper to set a pin ctl value more safely.
4436 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4437 * value in pin target array via snd_hda_codec_set_pin_target(), then
4438 * actually writes the value via either snd_hda_codec_update_cache() or
4439 * snd_hda_codec_write() depending on @cached flag.
4440 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004441int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4442 unsigned int val, bool cached)
4443{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004444 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004445 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004446 if (cached)
4447 return snd_hda_codec_update_cache(codec, pin, 0,
4448 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4449 else
4450 return snd_hda_codec_write(codec, pin, 0,
4451 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4452}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004453EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004454
Takashi Iwai990061c2010-09-09 22:08:44 +02004455/**
4456 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004457 * @codec: the HDA codec
4458 * @imux: imux helper object
4459 * @label: the name of imux item to assign
4460 * @index: index number of imux item to assign
4461 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004462 *
4463 * When the same label is used already in the existing items, the number
4464 * suffix is appended to the label. This label index number is stored
4465 * to type_idx when non-NULL pointer is given.
4466 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004467int snd_hda_add_imux_item(struct hda_codec *codec,
4468 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004469 int index, int *type_idx)
4470{
4471 int i, label_idx = 0;
4472 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004473 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004474 return -EINVAL;
4475 }
4476 for (i = 0; i < imux->num_items; i++) {
4477 if (!strncmp(label, imux->items[i].label, strlen(label)))
4478 label_idx++;
4479 }
4480 if (type_idx)
4481 *type_idx = label_idx;
4482 if (label_idx > 0)
4483 snprintf(imux->items[imux->num_items].label,
4484 sizeof(imux->items[imux->num_items].label),
4485 "%s %d", label, label_idx);
4486 else
4487 strlcpy(imux->items[imux->num_items].label, label,
4488 sizeof(imux->items[imux->num_items].label));
4489 imux->items[imux->num_items].index = index;
4490 imux->num_items++;
4491 return 0;
4492}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004493EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004496 * snd_hda_bus_reset - Reset the bus
4497 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004499void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004501 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004503 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004504 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004505 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004506#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004507 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004508 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004509 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004510 }
4511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004514EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004515
Takashi Iwaid5191e52009-11-16 14:58:17 +01004516/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004517 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4518 * @pcm: PCM caps bits
4519 * @buf: the string buffer to write
4520 * @buflen: the max buffer length
4521 *
4522 * used by hda_proc.c and hda_eld.c
4523 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004524void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4525{
4526 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4527 int i, j;
4528
4529 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4530 if (pcm & (AC_SUPPCM_BITS_8 << i))
4531 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4532
4533 buf[j] = '\0'; /* necessary when j == 0 */
4534}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004535EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004536
4537MODULE_DESCRIPTION("HDA codec core");
4538MODULE_LICENSE("GPL");