blob: c13d5c3e1d03f468edd01592062cae0635706422 [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 Iwai058524482015-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 Iwai058524482015-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 Iwai058524482015-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 Iwai63e51fd72013-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 Iwai63e51fd72013-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 Iwaicad372f2015-03-25 17:57:00 +0100149 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
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 Iwai058524482015-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 Iwai058524482015-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
Takashi Iwaicad372f2015-03-25 17:57:00 +0100439 if (_snd_hdac_read_parm(&codec->core, nid, AC_PAR_DEVLIST_LEN, &parm))
440 return 0; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400441 return parm & AC_DEV_LIST_LEN_MASK;
442}
443
444/**
445 * snd_hda_get_devices - copy device list without cache
446 * @codec: the HDA codec
447 * @nid: NID of the pin to parse
448 * @dev_list: device list array
449 * @max_devices: max. number of devices to store
450 *
451 * Copy the device list. This info is dynamic and so not cached.
452 * Currently called only from hda_proc.c, so not exported.
453 */
454int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
455 u8 *dev_list, int max_devices)
456{
457 unsigned int parm;
458 int i, dev_len, devices;
459
460 parm = get_num_devices(codec, nid);
461 if (!parm) /* not multi-stream capable */
462 return 0;
463
464 dev_len = parm + 1;
465 dev_len = dev_len < max_devices ? dev_len : max_devices;
466
467 devices = 0;
468 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100469 if (snd_hdac_read(&codec->core, nid,
470 AC_VERB_GET_DEVICE_LIST, devices, &parm))
471 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400472
473 for (i = 0; i < 8; i++) {
474 dev_list[devices] = (u8)parm;
475 parm >>= 4;
476 devices++;
477 if (devices >= dev_len)
478 break;
479 }
480 }
481 return devices;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * destructor
486 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100487static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (bus->ops.private_free)
492 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100493 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100497static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Takashi Iwai2565c892014-02-19 11:41:09 +0100499 snd_hda_bus_free(device->device_data);
500 return 0;
501}
502
503static int snd_hda_bus_dev_disconnect(struct snd_device *device)
504{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100506 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100510/* hdac_bus_ops translations */
511static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
512{
513 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
514 return bus->ops.command(bus, cmd);
515}
516
517static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
518 unsigned int *res)
519{
520 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
Takashi Iwaicad372f2015-03-25 17:57:00 +0100521 return bus->ops.get_response(bus, addr, res);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100522}
523
524static const struct hdac_bus_ops bus_ops = {
525 .command = _hda_bus_command,
526 .get_response = _hda_bus_get_response,
527};
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/**
530 * snd_hda_bus_new - create a HDA bus
531 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * @busp: the pointer to store the created bus instance
533 *
534 * Returns 0 if successful, or a negative error code.
535 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100536int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100537 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct hda_bus *bus;
540 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100541 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100542 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .dev_free = snd_hda_bus_dev_free,
544 };
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (busp)
547 *busp = NULL;
548
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200549 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100550 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100553 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
554 if (err < 0) {
555 kfree(bus);
556 return err;
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200560 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Takashi Iwai0ba21762007-04-16 11:29:14 +0200562 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
563 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 snd_hda_bus_free(bus);
565 return err;
566 }
567 if (busp)
568 *busp = bus;
569 return 0;
570}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100571EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100574 * read widget caps for each widget and store in cache
575 */
576static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
577{
578 int i;
579 hda_nid_t nid;
580
Takashi Iwai7639a062015-03-03 10:07:24 +0100581 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100583 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100584 nid = codec->core.start_nid;
585 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100586 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
587 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100588 return 0;
589}
590
Takashi Iwai3be14142009-02-20 14:11:16 +0100591/* read all pin default configurations and save codec->init_pins */
592static int read_pin_defaults(struct hda_codec *codec)
593{
Takashi Iwai7639a062015-03-03 10:07:24 +0100594 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100595
Takashi Iwai7639a062015-03-03 10:07:24 +0100596 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100597 struct hda_pincfg *pin;
598 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200599 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100600 if (wid_type != AC_WID_PIN)
601 continue;
602 pin = snd_array_new(&codec->init_pins);
603 if (!pin)
604 return -ENOMEM;
605 pin->nid = nid;
606 pin->cfg = snd_hda_codec_read(codec, nid, 0,
607 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200608 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
609 AC_VERB_GET_PIN_WIDGET_CONTROL,
610 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100611 }
612 return 0;
613}
614
615/* look up the given pin config list and return the item matching with NID */
616static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
617 struct snd_array *array,
618 hda_nid_t nid)
619{
620 int i;
621 for (i = 0; i < array->used; i++) {
622 struct hda_pincfg *pin = snd_array_elem(array, i);
623 if (pin->nid == nid)
624 return pin;
625 }
626 return NULL;
627}
628
Takashi Iwai3be14142009-02-20 14:11:16 +0100629/* set the current pin config value for the given NID.
630 * the value is cached, and read via snd_hda_codec_get_pincfg()
631 */
632int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
633 hda_nid_t nid, unsigned int cfg)
634{
635 struct hda_pincfg *pin;
636
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200637 /* the check below may be invalid when pins are added by a fixup
638 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
639 * for now
640 */
641 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100642 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
643 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200644 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100645
Takashi Iwai3be14142009-02-20 14:11:16 +0100646 pin = look_up_pincfg(codec, list, nid);
647 if (!pin) {
648 pin = snd_array_new(list);
649 if (!pin)
650 return -ENOMEM;
651 pin->nid = nid;
652 }
653 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100654 return 0;
655}
656
Takashi Iwaid5191e52009-11-16 14:58:17 +0100657/**
658 * snd_hda_codec_set_pincfg - Override a pin default configuration
659 * @codec: the HDA codec
660 * @nid: NID to set the pin config
661 * @cfg: the pin default config value
662 *
663 * Override a pin default configuration value in the cache.
664 * This value can be read by snd_hda_codec_get_pincfg() in a higher
665 * priority than the real hardware value.
666 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100667int snd_hda_codec_set_pincfg(struct hda_codec *codec,
668 hda_nid_t nid, unsigned int cfg)
669{
Takashi Iwai346ff702009-02-23 09:42:57 +0100670 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100671}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100672EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100673
Takashi Iwaid5191e52009-11-16 14:58:17 +0100674/**
675 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
676 * @codec: the HDA codec
677 * @nid: NID to get the pin config
678 *
679 * Get the current pin config value of the given pin NID.
680 * If the pincfg value is cached or overridden via sysfs or driver,
681 * returns the cached value.
682 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100683unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
684{
685 struct hda_pincfg *pin;
686
Takashi Iwai648a8d22014-02-25 10:38:13 +0100687#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100688 {
689 unsigned int cfg = 0;
690 mutex_lock(&codec->user_mutex);
691 pin = look_up_pincfg(codec, &codec->user_pins, nid);
692 if (pin)
693 cfg = pin->cfg;
694 mutex_unlock(&codec->user_mutex);
695 if (cfg)
696 return cfg;
697 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100698#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100699 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
700 if (pin)
701 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100702 pin = look_up_pincfg(codec, &codec->init_pins, nid);
703 if (pin)
704 return pin->cfg;
705 return 0;
706}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100707EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100708
Takashi Iwai95a962c2014-10-29 16:03:58 +0100709/**
710 * snd_hda_codec_set_pin_target - remember the current pinctl target value
711 * @codec: the HDA codec
712 * @nid: pin NID
713 * @val: assigned pinctl value
714 *
715 * This function stores the given value to a pinctl target value in the
716 * pincfg table. This isn't always as same as the actually written value
717 * but can be referred at any time via snd_hda_codec_get_pin_target().
718 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100719int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
720 unsigned int val)
721{
722 struct hda_pincfg *pin;
723
724 pin = look_up_pincfg(codec, &codec->init_pins, nid);
725 if (!pin)
726 return -EINVAL;
727 pin->target = val;
728 return 0;
729}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100730EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100731
Takashi Iwai95a962c2014-10-29 16:03:58 +0100732/**
733 * snd_hda_codec_get_pin_target - return the current pinctl target value
734 * @codec: the HDA codec
735 * @nid: pin NID
736 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100737int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
738{
739 struct hda_pincfg *pin;
740
741 pin = look_up_pincfg(codec, &codec->init_pins, nid);
742 if (!pin)
743 return 0;
744 return pin->target;
745}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100746EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100747
Takashi Iwai92ee6162009-12-27 11:18:59 +0100748/**
749 * snd_hda_shutup_pins - Shut up all pins
750 * @codec: the HDA codec
751 *
752 * Clear all pin controls to shup up before suspend for avoiding click noise.
753 * The controls aren't cached so that they can be resumed properly.
754 */
755void snd_hda_shutup_pins(struct hda_codec *codec)
756{
757 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200758 /* don't shut up pins when unloading the driver; otherwise it breaks
759 * the default pin setup at the next load of the driver
760 */
761 if (codec->bus->shutdown)
762 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100763 for (i = 0; i < codec->init_pins.used; i++) {
764 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
765 /* use read here for syncing after issuing each verb */
766 snd_hda_codec_read(codec, pin->nid, 0,
767 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
768 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200769 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100770}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100771EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100772
Takashi Iwai2a439522011-07-26 09:52:50 +0200773#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200774/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
775static void restore_shutup_pins(struct hda_codec *codec)
776{
777 int i;
778 if (!codec->pins_shutup)
779 return;
780 if (codec->bus->shutdown)
781 return;
782 for (i = 0; i < codec->init_pins.used; i++) {
783 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
784 snd_hda_codec_write(codec, pin->nid, 0,
785 AC_VERB_SET_PIN_WIDGET_CONTROL,
786 pin->ctrl);
787 }
788 codec->pins_shutup = 0;
789}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700790#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200791
David Henningsson26a6cb62012-10-09 15:04:21 +0200792static void hda_jackpoll_work(struct work_struct *work)
793{
794 struct hda_codec *codec =
795 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200796
797 snd_hda_jack_set_dirty_all(codec);
798 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400799
800 if (!codec->jackpoll_interval)
801 return;
802
Takashi Iwai2f35c632015-02-27 22:43:26 +0100803 schedule_delayed_work(&codec->jackpoll_work,
804 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200805}
806
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100807/* release all pincfg lists */
808static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100809{
Takashi Iwai346ff702009-02-23 09:42:57 +0100810 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100811#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100812 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100813#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100814 snd_array_free(&codec->init_pins);
815}
816
Takashi Iwai54d17402005-11-21 16:33:22 +0100817/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200818 * audio-converter setup caches
819 */
820struct hda_cvt_setup {
821 hda_nid_t nid;
822 u8 stream_tag;
823 u8 channel_id;
824 u16 format_id;
825 unsigned char active; /* cvt is currently used */
826 unsigned char dirty; /* setups should be cleared */
827};
828
829/* get or create a cache entry for the given audio converter NID */
830static struct hda_cvt_setup *
831get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
832{
833 struct hda_cvt_setup *p;
834 int i;
835
836 for (i = 0; i < codec->cvt_setups.used; i++) {
837 p = snd_array_elem(&codec->cvt_setups, i);
838 if (p->nid == nid)
839 return p;
840 }
841 p = snd_array_new(&codec->cvt_setups);
842 if (p)
843 p->nid = nid;
844 return p;
845}
846
847/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100848 * PCM device
849 */
850static void release_pcm(struct kref *kref)
851{
852 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
853
854 if (pcm->pcm)
855 snd_device_free(pcm->codec->card, pcm->pcm);
856 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
857 kfree(pcm->name);
858 kfree(pcm);
859}
860
861void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
862{
863 kref_put(&pcm->kref, release_pcm);
864}
865EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
866
867struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
868 const char *fmt, ...)
869{
870 struct hda_pcm *pcm;
871 va_list args;
872
873 va_start(args, fmt);
874 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
875 if (!pcm)
876 return NULL;
877
878 pcm->codec = codec;
879 kref_init(&pcm->kref);
880 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
881 if (!pcm->name) {
882 kfree(pcm);
883 return NULL;
884 }
885
886 list_add_tail(&pcm->list, &codec->pcm_list_head);
887 return pcm;
888}
889EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
890
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100891/*
892 * codec destructor
893 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100894static void codec_release_pcms(struct hda_codec *codec)
895{
896 struct hda_pcm *pcm, *n;
897
898 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
899 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100900 if (pcm->pcm)
901 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100902 snd_hda_codec_pcm_put(pcm);
903 }
904}
905
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100906void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
907{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100908 if (codec->registered) {
909 /* pm_runtime_put() is called in snd_hdac_device_exit() */
910 pm_runtime_get_noresume(hda_codec_dev(codec));
911 pm_runtime_disable(hda_codec_dev(codec));
912 codec->registered = 0;
913 }
914
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100915 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100916 if (!codec->in_freeing)
917 snd_hda_ctls_clear(codec);
918 codec_release_pcms(codec);
919 snd_hda_detach_beep_device(codec);
920 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
921 snd_hda_jack_tbl_clear(codec);
922 codec->proc_widget_hook = NULL;
923 codec->spec = NULL;
924
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100925 /* free only driver_pins so that init_pins + user_pins are restored */
926 snd_array_free(&codec->driver_pins);
927 snd_array_free(&codec->cvt_setups);
928 snd_array_free(&codec->spdif_out);
929 snd_array_free(&codec->verbs);
930 codec->preset = NULL;
931 codec->slave_dig_outs = NULL;
932 codec->spdif_status_reset = 0;
933 snd_array_free(&codec->mixers);
934 snd_array_free(&codec->nids);
935 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100936 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100937}
938
Takashi Iwaid8193872012-08-31 07:54:38 -0700939static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100940 unsigned int power_state);
941
Takashi Iwaic4c25332015-03-03 17:22:12 +0100942/* also called from hda_bind.c */
943void snd_hda_codec_register(struct hda_codec *codec)
944{
945 if (codec->registered)
946 return;
947 if (device_is_registered(hda_codec_dev(codec))) {
948 snd_hda_register_beep_device(codec);
949 pm_runtime_enable(hda_codec_dev(codec));
950 /* it was powered up in snd_hda_codec_new(), now all done */
951 snd_hda_power_down(codec);
952 codec->registered = 1;
953 }
954}
955
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100956static int snd_hda_codec_dev_register(struct snd_device *device)
957{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100958 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100959 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100960}
961
962static int snd_hda_codec_dev_disconnect(struct snd_device *device)
963{
964 struct hda_codec *codec = device->device_data;
965
Takashi Iwaid604b392014-02-28 13:42:09 +0100966 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100967 return 0;
968}
969
Takashi Iwai2565c892014-02-19 11:41:09 +0100970static int snd_hda_codec_dev_free(struct snd_device *device)
971{
Takashi Iwaid56db742015-02-27 23:25:35 +0100972 struct hda_codec *codec = device->device_data;
973
974 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100975 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100976 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100977 return 0;
978}
979
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100980static void snd_hda_codec_dev_release(struct device *dev)
981{
Takashi Iwaid56db742015-02-27 23:25:35 +0100982 struct hda_codec *codec = dev_to_hda_codec(dev);
983
984 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100985 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100986 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100987 kfree(codec->modelname);
988 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100989 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/**
993 * snd_hda_codec_new - create a HDA codec
994 * @bus: the bus to assign
995 * @codec_addr: the codec address
996 * @codecp: the pointer to store the generated codec
997 *
998 * Returns 0 if successful, or a negative error code.
999 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001000int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1001 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001004 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001005 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001007 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001008 .dev_register = snd_hda_codec_dev_register,
1009 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001010 .dev_free = snd_hda_codec_dev_free,
1011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Takashi Iwaida3cec32008-08-08 17:12:14 +02001013 if (snd_BUG_ON(!bus))
1014 return -EINVAL;
1015 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1016 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001018 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001019 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Takashi Iwai7639a062015-03-03 10:07:24 +01001022 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1023 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1024 codec_addr);
1025 if (err < 0) {
1026 kfree(codec);
1027 return err;
1028 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001029
Takashi Iwai7639a062015-03-03 10:07:24 +01001030 codec->core.dev.release = snd_hda_codec_dev_release;
1031 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai058524482015-03-03 15:40:08 +01001032 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001035 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001037 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001038 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001039 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1040 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001041 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001042 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001043 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001044 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001045 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001046 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001047 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001048 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001049
David Henningsson26a6cb62012-10-09 15:04:21 +02001050 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001051 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001052 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Takashi Iwai83012a72012-08-24 18:38:08 +02001054#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001055 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001056#endif
1057
Takashi Iwai648a8d22014-02-25 10:38:13 +01001058 snd_hda_sysfs_init(codec);
1059
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001060 if (codec->bus->modelname) {
1061 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1062 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001063 err = -ENODEV;
1064 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001065 }
1066 }
1067
Takashi Iwai7639a062015-03-03 10:07:24 +01001068 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001069 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001070 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 err = read_pin_defaults(codec);
1073 if (err < 0)
1074 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001075
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001076 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001077 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001078
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001079 snd_hda_codec_proc_new(codec);
1080
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001081 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001082
Takashi Iwai7639a062015-03-03 10:07:24 +01001083 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1084 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001085 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001086
Takashi Iwai6efdd852015-02-27 16:09:22 +01001087 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001088 if (err < 0)
1089 goto error;
1090
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001091 if (codecp)
1092 *codecp = codec;
1093 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001094
1095 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001096 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001097 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001098}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001099EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001100
Takashi Iwai95a962c2014-10-29 16:03:58 +01001101/**
1102 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1103 * @codec: the HDA codec
1104 *
1105 * Forcibly refresh the all widget caps and the init pin configurations of
1106 * the given codec.
1107 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001108int snd_hda_codec_update_widgets(struct hda_codec *codec)
1109{
1110 hda_nid_t fg;
1111 int err;
1112
Takashi Iwai7639a062015-03-03 10:07:24 +01001113 err = snd_hdac_refresh_widgets(&codec->core);
1114 if (err < 0)
1115 return err;
1116
Mengdong Lina15d05d2013-02-08 17:09:31 -05001117 /* Assume the function group node does not change,
1118 * only the widget nodes may change.
1119 */
1120 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001121 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001122 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001123 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001124 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001125
1126 snd_array_free(&codec->init_pins);
1127 err = read_pin_defaults(codec);
1128
1129 return err;
1130}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001131EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001132
Takashi Iwaied360812012-08-08 17:12:52 +02001133/* update the stream-id if changed */
1134static void update_pcm_stream_id(struct hda_codec *codec,
1135 struct hda_cvt_setup *p, hda_nid_t nid,
1136 u32 stream_tag, int channel_id)
1137{
1138 unsigned int oldval, newval;
1139
1140 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1141 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1142 newval = (stream_tag << 4) | channel_id;
1143 if (oldval != newval)
1144 snd_hda_codec_write(codec, nid, 0,
1145 AC_VERB_SET_CHANNEL_STREAMID,
1146 newval);
1147 p->stream_tag = stream_tag;
1148 p->channel_id = channel_id;
1149 }
1150}
1151
1152/* update the format-id if changed */
1153static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1154 hda_nid_t nid, int format)
1155{
1156 unsigned int oldval;
1157
1158 if (p->format_id != format) {
1159 oldval = snd_hda_codec_read(codec, nid, 0,
1160 AC_VERB_GET_STREAM_FORMAT, 0);
1161 if (oldval != format) {
1162 msleep(1);
1163 snd_hda_codec_write(codec, nid, 0,
1164 AC_VERB_SET_STREAM_FORMAT,
1165 format);
1166 }
1167 p->format_id = format;
1168 }
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/**
1172 * snd_hda_codec_setup_stream - set up the codec for streaming
1173 * @codec: the CODEC to set up
1174 * @nid: the NID to set up
1175 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1176 * @channel_id: channel id to pass, zero based.
1177 * @format: stream format.
1178 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001179void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1180 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int channel_id, int format)
1182{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001183 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001184 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001185 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001186 int i;
1187
Takashi Iwai0ba21762007-04-16 11:29:14 +02001188 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001189 return;
1190
Takashi Iwai4e76a882014-02-25 12:21:03 +01001191 codec_dbg(codec,
1192 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1193 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001194 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001195 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001196 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001197
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001198 if (codec->patch_ops.stream_pm)
1199 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001200 if (codec->pcm_format_first)
1201 update_pcm_format(codec, p, nid, format);
1202 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1203 if (!codec->pcm_format_first)
1204 update_pcm_format(codec, p, nid, format);
1205
Takashi Iwaieb541332010-08-06 13:48:11 +02001206 p->active = 1;
1207 p->dirty = 0;
1208
1209 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001210 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001211 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001212 for (i = 0; i < c->cvt_setups.used; i++) {
1213 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001214 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001215 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001216 p->dirty = 1;
1217 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001220EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Takashi Iwaif0cea792010-08-13 11:56:53 +02001222static void really_cleanup_stream(struct hda_codec *codec,
1223 struct hda_cvt_setup *q);
1224
Takashi Iwaid5191e52009-11-16 14:58:17 +01001225/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001226 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001227 * @codec: the CODEC to clean up
1228 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001229 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001230 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001231void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1232 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001233{
Takashi Iwaieb541332010-08-06 13:48:11 +02001234 struct hda_cvt_setup *p;
1235
Takashi Iwai888afa12008-03-18 09:57:50 +01001236 if (!nid)
1237 return;
1238
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001239 if (codec->no_sticky_stream)
1240 do_now = 1;
1241
Takashi Iwai4e76a882014-02-25 12:21:03 +01001242 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001243 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001244 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001245 /* here we just clear the active flag when do_now isn't set;
1246 * actual clean-ups will be done later in
1247 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1248 */
1249 if (do_now)
1250 really_cleanup_stream(codec, p);
1251 else
1252 p->active = 0;
1253 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001255EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001256
Takashi Iwaieb541332010-08-06 13:48:11 +02001257static void really_cleanup_stream(struct hda_codec *codec,
1258 struct hda_cvt_setup *q)
1259{
1260 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001261 if (q->stream_tag || q->channel_id)
1262 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1263 if (q->format_id)
1264 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1265);
Takashi Iwaieb541332010-08-06 13:48:11 +02001266 memset(q, 0, sizeof(*q));
1267 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001268 if (codec->patch_ops.stream_pm)
1269 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001270}
1271
1272/* clean up the all conflicting obsolete streams */
1273static void purify_inactive_streams(struct hda_codec *codec)
1274{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001275 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001276 int i;
1277
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001278 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001279 for (i = 0; i < c->cvt_setups.used; i++) {
1280 struct hda_cvt_setup *p;
1281 p = snd_array_elem(&c->cvt_setups, i);
1282 if (p->dirty)
1283 really_cleanup_stream(c, p);
1284 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001285 }
1286}
1287
Takashi Iwai2a439522011-07-26 09:52:50 +02001288#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001289/* clean up all streams; called from suspend */
1290static void hda_cleanup_all_streams(struct hda_codec *codec)
1291{
1292 int i;
1293
1294 for (i = 0; i < codec->cvt_setups.used; i++) {
1295 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1296 if (p->stream_tag)
1297 really_cleanup_stream(codec, p);
1298 }
1299}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001300#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
1303 * amp access functions
1304 */
1305
Takashi Iwaid5191e52009-11-16 14:58:17 +01001306/**
1307 * query_amp_caps - query AMP capabilities
1308 * @codec: the HD-auio codec
1309 * @nid: the NID to query
1310 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1311 *
1312 * Query AMP capabilities for the given widget and direction.
1313 * Returns the obtained capability bits.
1314 *
1315 * When cap bits have been already read, this doesn't read again but
1316 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001318u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001320 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1321 nid = codec->core.afg;
1322 return snd_hda_param_read(codec, nid,
1323 direction == HDA_OUTPUT ?
1324 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001326EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Takashi Iwaid5191e52009-11-16 14:58:17 +01001328/**
David Henningsson861a04e2014-09-23 10:38:17 +02001329 * snd_hda_check_amp_caps - query AMP capabilities
1330 * @codec: the HD-audio codec
1331 * @nid: the NID to query
1332 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001333 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001334 *
1335 * Check whether the widget has the given amp capability for the direction.
1336 */
1337bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1338 int dir, unsigned int bits)
1339{
1340 if (!nid)
1341 return false;
1342 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1343 if (query_amp_caps(codec, nid, dir) & bits)
1344 return true;
1345 return false;
1346}
1347EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1348
1349/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001350 * snd_hda_override_amp_caps - Override the AMP capabilities
1351 * @codec: the CODEC to clean up
1352 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001353 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001354 * @caps: the capability bits to set
1355 *
1356 * Override the cached AMP caps bits value by the given one.
1357 * This function is useful if the driver needs to adjust the AMP ranges,
1358 * e.g. limit to 0dB, etc.
1359 *
1360 * Returns zero if successful or a negative error code.
1361 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001362int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1363 unsigned int caps)
1364{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001365 unsigned int parm;
1366
1367 snd_hda_override_wcaps(codec, nid,
1368 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1369 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1370 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001371}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001372EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001373
Takashi Iwaid5191e52009-11-16 14:58:17 +01001374/**
1375 * snd_hda_codec_amp_stereo - update the AMP stereo values
1376 * @codec: HD-audio codec
1377 * @nid: NID to read the AMP value
1378 * @direction: #HDA_INPUT or #HDA_OUTPUT
1379 * @idx: the index value (only for input direction)
1380 * @mask: bit mask to set
1381 * @val: the bits value to set
1382 *
1383 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1384 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001385 */
1386int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1387 int direction, int idx, int mask, int val)
1388{
1389 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001390
1391 if (snd_BUG_ON(mask & ~0xff))
1392 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001393 for (ch = 0; ch < 2; ch++)
1394 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1395 idx, mask, val);
1396 return ret;
1397}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001398EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001399
Takashi Iwai95a962c2014-10-29 16:03:58 +01001400/**
1401 * snd_hda_codec_amp_init - initialize the AMP value
1402 * @codec: the HDA codec
1403 * @nid: NID to read the AMP value
1404 * @ch: channel (left=0 or right=1)
1405 * @dir: #HDA_INPUT or #HDA_OUTPUT
1406 * @idx: the index value (only for input direction)
1407 * @mask: bit mask to set
1408 * @val: the bits value to set
1409 *
1410 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001411 * the first access. If the amp was already initialized / updated beforehand,
1412 * this does nothing.
1413 */
1414int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1415 int dir, int idx, int mask, int val)
1416{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001417 int orig;
1418
1419 if (!codec->core.regmap)
1420 return -EINVAL;
1421 regcache_cache_only(codec->core.regmap, true);
1422 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1423 regcache_cache_only(codec->core.regmap, false);
1424 if (orig >= 0)
1425 return 0;
1426 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001427}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001428EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001429
Takashi Iwai95a962c2014-10-29 16:03:58 +01001430/**
1431 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1432 * @codec: the HDA codec
1433 * @nid: NID to read the AMP value
1434 * @dir: #HDA_INPUT or #HDA_OUTPUT
1435 * @idx: the index value (only for input direction)
1436 * @mask: bit mask to set
1437 * @val: the bits value to set
1438 *
1439 * Call snd_hda_codec_amp_init() for both stereo channels.
1440 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001441int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1442 int dir, int idx, int mask, int val)
1443{
1444 int ch, ret = 0;
1445
1446 if (snd_BUG_ON(mask & ~0xff))
1447 mask &= 0xff;
1448 for (ch = 0; ch < 2; ch++)
1449 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1450 idx, mask, val);
1451 return ret;
1452}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001453EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001454
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001455static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1456 unsigned int ofs)
1457{
1458 u32 caps = query_amp_caps(codec, nid, dir);
1459 /* get num steps */
1460 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1461 if (ofs < caps)
1462 caps -= ofs;
1463 return caps;
1464}
1465
Takashi Iwaid5191e52009-11-16 14:58:17 +01001466/**
1467 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001468 * @kcontrol: referred ctl element
1469 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001470 *
1471 * The control element is supposed to have the private_value field
1472 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1473 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001474int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1475 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1478 u16 nid = get_amp_nid(kcontrol);
1479 u8 chs = get_amp_channels(kcontrol);
1480 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001481 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001483 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1484 uinfo->count = chs == 3 ? 2 : 1;
1485 uinfo->value.integer.min = 0;
1486 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1487 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001488 codec_warn(codec,
1489 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1490 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EINVAL;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001495EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001497
1498static inline unsigned int
1499read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1500 int ch, int dir, int idx, unsigned int ofs)
1501{
1502 unsigned int val;
1503 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1504 val &= HDA_AMP_VOLMASK;
1505 if (val >= ofs)
1506 val -= ofs;
1507 else
1508 val = 0;
1509 return val;
1510}
1511
1512static inline int
1513update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1514 int ch, int dir, int idx, unsigned int ofs,
1515 unsigned int val)
1516{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001517 unsigned int maxval;
1518
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001519 if (val > 0)
1520 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001521 /* ofs = 0: raw max value */
1522 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001523 if (val > maxval)
1524 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001525 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1526 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001527}
1528
Takashi Iwaid5191e52009-11-16 14:58:17 +01001529/**
1530 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001531 * @kcontrol: ctl element
1532 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001533 *
1534 * The control element is supposed to have the private_value field
1535 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1536 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1541 hda_nid_t nid = get_amp_nid(kcontrol);
1542 int chs = get_amp_channels(kcontrol);
1543 int dir = get_amp_direction(kcontrol);
1544 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001545 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 long *valp = ucontrol->value.integer.value;
1547
1548 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001549 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001551 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 0;
1553}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001554EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Takashi Iwaid5191e52009-11-16 14:58:17 +01001556/**
1557 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001558 * @kcontrol: ctl element
1559 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001560 *
1561 * The control element is supposed to have the private_value field
1562 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1563 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001564int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1565 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1568 hda_nid_t nid = get_amp_nid(kcontrol);
1569 int chs = get_amp_channels(kcontrol);
1570 int dir = get_amp_direction(kcontrol);
1571 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001572 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 long *valp = ucontrol->value.integer.value;
1574 int change = 0;
1575
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001576 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001577 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001578 valp++;
1579 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001580 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001581 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return change;
1583}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001584EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Takashi Iwaid5191e52009-11-16 14:58:17 +01001586/**
1587 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001588 * @kcontrol: ctl element
1589 * @op_flag: operation flag
1590 * @size: byte size of input TLV
1591 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001592 *
1593 * The control element is supposed to have the private_value field
1594 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1595 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001596int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1597 unsigned int size, unsigned int __user *_tlv)
1598{
1599 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1600 hda_nid_t nid = get_amp_nid(kcontrol);
1601 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001602 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001603 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001604 u32 caps, val1, val2;
1605
1606 if (size < 4 * sizeof(unsigned int))
1607 return -ENOMEM;
1608 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001609 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1610 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001611 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001612 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001613 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001614 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001615 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001616 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1617 return -EFAULT;
1618 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1619 return -EFAULT;
1620 if (put_user(val1, _tlv + 2))
1621 return -EFAULT;
1622 if (put_user(val2, _tlv + 3))
1623 return -EFAULT;
1624 return 0;
1625}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001626EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001627
Takashi Iwaid5191e52009-11-16 14:58:17 +01001628/**
1629 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1630 * @codec: HD-audio codec
1631 * @nid: NID of a reference widget
1632 * @dir: #HDA_INPUT or #HDA_OUTPUT
1633 * @tlv: TLV data to be stored, at least 4 elements
1634 *
1635 * Set (static) TLV data for a virtual master volume using the AMP caps
1636 * obtained from the reference NID.
1637 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001638 */
1639void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1640 unsigned int *tlv)
1641{
1642 u32 caps;
1643 int nums, step;
1644
1645 caps = query_amp_caps(codec, nid, dir);
1646 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1647 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1648 step = (step + 1) * 25;
1649 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1650 tlv[1] = 2 * sizeof(unsigned int);
1651 tlv[2] = -nums * step;
1652 tlv[3] = step;
1653}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001654EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001655
1656/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001657static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001658find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001659{
1660 struct snd_ctl_elem_id id;
1661 memset(&id, 0, sizeof(id));
1662 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001663 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001664 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001665 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1666 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001667 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001668 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001669}
1670
Takashi Iwaid5191e52009-11-16 14:58:17 +01001671/**
1672 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1673 * @codec: HD-audio codec
1674 * @name: ctl id name string
1675 *
1676 * Get the control element with the given id string and IFACE_MIXER.
1677 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001678struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1679 const char *name)
1680{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001681 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001682}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001683EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001684
Takashi Iwaidcda5802012-10-12 17:24:51 +02001685static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001686 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001687{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001688 int i, idx;
1689 /* 16 ctlrs should be large enough */
1690 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1691 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001692 return idx;
1693 }
1694 return -EBUSY;
1695}
1696
Takashi Iwaid5191e52009-11-16 14:58:17 +01001697/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001698 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001699 * @codec: HD-audio codec
1700 * @nid: corresponding NID (optional)
1701 * @kctl: the control element to assign
1702 *
1703 * Add the given control element to an array inside the codec instance.
1704 * All control elements belonging to a codec are supposed to be added
1705 * by this function so that a proper clean-up works at the free or
1706 * reconfiguration time.
1707 *
1708 * If non-zero @nid is passed, the NID is assigned to the control element.
1709 * The assignment is shown in the codec proc file.
1710 *
1711 * snd_hda_ctl_add() checks the control subdev id field whether
1712 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001713 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1714 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001715 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001716int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1717 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001718{
1719 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001720 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001721 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001722
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001723 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001724 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001725 if (nid == 0)
1726 nid = get_amp_nid_(kctl->private_value);
1727 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001728 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1729 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001730 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001731 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001732 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001733 if (err < 0)
1734 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001735 item = snd_array_new(&codec->mixers);
1736 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001737 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001738 item->kctl = kctl;
1739 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001740 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001741 return 0;
1742}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001743EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001744
Takashi Iwaid5191e52009-11-16 14:58:17 +01001745/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001746 * snd_hda_add_nid - Assign a NID to a control element
1747 * @codec: HD-audio codec
1748 * @nid: corresponding NID (optional)
1749 * @kctl: the control element to assign
1750 * @index: index to kctl
1751 *
1752 * Add the given control element to an array inside the codec instance.
1753 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1754 * NID:KCTL mapping - for example "Capture Source" selector.
1755 */
1756int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1757 unsigned int index, hda_nid_t nid)
1758{
1759 struct hda_nid_item *item;
1760
1761 if (nid > 0) {
1762 item = snd_array_new(&codec->nids);
1763 if (!item)
1764 return -ENOMEM;
1765 item->kctl = kctl;
1766 item->index = index;
1767 item->nid = nid;
1768 return 0;
1769 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001770 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1771 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001772 return -EINVAL;
1773}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001774EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001775
1776/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001777 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1778 * @codec: HD-audio codec
1779 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001780void snd_hda_ctls_clear(struct hda_codec *codec)
1781{
1782 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001783 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001784 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001785 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001786 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001787 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001788}
1789
Takashi Iwai95a962c2014-10-29 16:03:58 +01001790/**
1791 * snd_hda_lock_devices - pseudo device locking
1792 * @bus: the BUS
1793 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001794 * toggle card->shutdown to allow/disallow the device access (as a hack)
1795 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001796int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001797{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001798 struct snd_card *card = bus->card;
1799 struct hda_codec *codec;
1800
Takashi Iwaia65d6292009-02-23 16:57:04 +01001801 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001802 if (card->shutdown)
1803 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001804 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001805 if (!list_empty(&card->ctl_files))
1806 goto err_clear;
1807
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001808 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001809 struct hda_pcm *cpcm;
1810 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001811 if (!cpcm->pcm)
1812 continue;
1813 if (cpcm->pcm->streams[0].substream_opened ||
1814 cpcm->pcm->streams[1].substream_opened)
1815 goto err_clear;
1816 }
1817 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001818 spin_unlock(&card->files_lock);
1819 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001820
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001821 err_clear:
1822 card->shutdown = 0;
1823 err_unlock:
1824 spin_unlock(&card->files_lock);
1825 return -EINVAL;
1826}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001827EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001828
Takashi Iwai95a962c2014-10-29 16:03:58 +01001829/**
1830 * snd_hda_unlock_devices - pseudo device unlocking
1831 * @bus: the BUS
1832 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001833void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001834{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001835 struct snd_card *card = bus->card;
1836
Takashi Iwaia65d6292009-02-23 16:57:04 +01001837 spin_lock(&card->files_lock);
1838 card->shutdown = 0;
1839 spin_unlock(&card->files_lock);
1840}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001841EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001842
Takashi Iwaid5191e52009-11-16 14:58:17 +01001843/**
1844 * snd_hda_codec_reset - Clear all objects assigned to the codec
1845 * @codec: HD-audio codec
1846 *
1847 * This frees the all PCM and control elements assigned to the codec, and
1848 * clears the caches and restores the pin default configurations.
1849 *
1850 * When a device is being used, it returns -EBSY. If successfully freed,
1851 * returns zero.
1852 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001853int snd_hda_codec_reset(struct hda_codec *codec)
1854{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001855 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001856
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001857 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001858 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001859
1860 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001861 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001862
Takashi Iwaia65d6292009-02-23 16:57:04 +01001863 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001864 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001865 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001866}
1867
Takashi Iwai6194b992014-06-06 18:12:16 +02001868typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001869
1870/* apply the function to all matching slave ctls in the mixer list */
1871static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001872 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001873{
1874 struct hda_nid_item *items;
1875 const char * const *s;
1876 int i, err;
1877
1878 items = codec->mixers.list;
1879 for (i = 0; i < codec->mixers.used; i++) {
1880 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001881 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001882 continue;
1883 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001884 char tmpname[sizeof(sctl->id.name)];
1885 const char *name = *s;
1886 if (suffix) {
1887 snprintf(tmpname, sizeof(tmpname), "%s %s",
1888 name, suffix);
1889 name = tmpname;
1890 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001891 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001892 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001893 if (err)
1894 return err;
1895 break;
1896 }
1897 }
1898 }
1899 return 0;
1900}
1901
Takashi Iwai6194b992014-06-06 18:12:16 +02001902static int check_slave_present(struct hda_codec *codec,
1903 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001904{
1905 return 1;
1906}
1907
Takashi Iwai18478e82012-03-09 17:51:10 +01001908/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001909static int get_kctl_0dB_offset(struct hda_codec *codec,
1910 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001911{
1912 int _tlv[4];
1913 const int *tlv = NULL;
1914 int val = -1;
1915
1916 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1917 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1918 mm_segment_t fs = get_fs();
1919 set_fs(get_ds());
1920 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1921 tlv = _tlv;
1922 set_fs(fs);
1923 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1924 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001925 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1926 int step = tlv[3];
1927 step &= ~TLV_DB_SCALE_MUTE;
1928 if (!step)
1929 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001930 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001931 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001932- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001933 return -1;
1934 }
1935 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001936 val = -tlv[2] / step;
1937 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001938 return val;
1939}
1940
1941/* call kctl->put with the given value(s) */
1942static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1943{
1944 struct snd_ctl_elem_value *ucontrol;
1945 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1946 if (!ucontrol)
1947 return -ENOMEM;
1948 ucontrol->value.integer.value[0] = val;
1949 ucontrol->value.integer.value[1] = val;
1950 kctl->put(kctl, ucontrol);
1951 kfree(ucontrol);
1952 return 0;
1953}
1954
1955/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001956static int init_slave_0dB(struct hda_codec *codec,
1957 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001958{
Takashi Iwai6194b992014-06-06 18:12:16 +02001959 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001960 if (offset > 0)
1961 put_kctl_with_value(slave, offset);
1962 return 0;
1963}
1964
1965/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001966static int init_slave_unmute(struct hda_codec *codec,
1967 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001968{
1969 return put_kctl_with_value(slave, 1);
1970}
1971
Takashi Iwaie8750942014-06-30 14:02:39 +02001972static int add_slave(struct hda_codec *codec,
1973 void *data, struct snd_kcontrol *slave)
1974{
1975 return snd_ctl_add_slave(data, slave);
1976}
1977
Takashi Iwaid5191e52009-11-16 14:58:17 +01001978/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001979 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980 * @codec: HD-audio codec
1981 * @name: vmaster control name
1982 * @tlv: TLV data (optional)
1983 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001984 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001985 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001986 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001987 *
1988 * Create a virtual master control with the given name. The TLV data
1989 * must be either NULL or a valid data.
1990 *
1991 * @slaves is a NULL-terminated array of strings, each of which is a
1992 * slave control name. All controls with these names are assigned to
1993 * the new virtual master control.
1994 *
1995 * This function returns zero if successful or a negative error code.
1996 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001997int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001998 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001999 const char *suffix, bool init_slave_vol,
2000 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002001{
2002 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002003 int err;
2004
Takashi Iwai29e58532012-03-12 12:25:03 +01002005 if (ctl_ret)
2006 *ctl_ret = NULL;
2007
Takashi Iwai9322ca52012-02-03 14:28:01 +01002008 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002009 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002010 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002011 return 0;
2012 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002013 kctl = snd_ctl_make_virtual_master(name, tlv);
2014 if (!kctl)
2015 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002016 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002017 if (err < 0)
2018 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002019
Takashi Iwaie8750942014-06-30 14:02:39 +02002020 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002021 if (err < 0)
2022 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002023
2024 /* init with master mute & zero volume */
2025 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002026 if (init_slave_vol) {
2027 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002028 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002029 tlv ? init_slave_0dB : init_slave_unmute, &step);
2030 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002031
Takashi Iwai29e58532012-03-12 12:25:03 +01002032 if (ctl_ret)
2033 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002034 return 0;
2035}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002036EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002037
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002038/*
2039 * mute-LED control using vmaster
2040 */
2041static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_info *uinfo)
2043{
2044 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002045 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002046 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002047
Takashi Iwai3ff72212014-10-20 18:17:28 +02002048 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002049}
2050
2051static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_value *ucontrol)
2053{
2054 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2055 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2056 return 0;
2057}
2058
2059static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
2061{
2062 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2063 unsigned int old_mode = hook->mute_mode;
2064
2065 hook->mute_mode = ucontrol->value.enumerated.item[0];
2066 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2067 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2068 if (old_mode == hook->mute_mode)
2069 return 0;
2070 snd_hda_sync_vmaster_hook(hook);
2071 return 1;
2072}
2073
2074static struct snd_kcontrol_new vmaster_mute_mode = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .name = "Mute-LED Mode",
2077 .info = vmaster_mute_mode_info,
2078 .get = vmaster_mute_mode_get,
2079 .put = vmaster_mute_mode_put,
2080};
2081
Takashi Iwai95a962c2014-10-29 16:03:58 +01002082/**
2083 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2084 * @codec: the HDA codec
2085 * @hook: the vmaster hook object
2086 * @expose_enum_ctl: flag to create an enum ctl
2087 *
2088 * Add a mute-LED hook with the given vmaster switch kctl.
2089 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2090 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002091 */
2092int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002093 struct hda_vmaster_mute_hook *hook,
2094 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002095{
2096 struct snd_kcontrol *kctl;
2097
2098 if (!hook->hook || !hook->sw_kctl)
2099 return 0;
2100 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2101 hook->codec = codec;
2102 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002103 if (!expose_enum_ctl)
2104 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002105 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2106 if (!kctl)
2107 return -ENOMEM;
2108 return snd_hda_ctl_add(codec, 0, kctl);
2109}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002110EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002111
Takashi Iwai95a962c2014-10-29 16:03:58 +01002112/**
2113 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2114 * @hook: the vmaster hook
2115 *
2116 * Call the hook with the current value for synchronization.
2117 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002118 */
2119void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2120{
2121 if (!hook->hook || !hook->codec)
2122 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002123 /* don't call vmaster hook in the destructor since it might have
2124 * been already destroyed
2125 */
2126 if (hook->codec->bus->shutdown)
2127 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002128 switch (hook->mute_mode) {
2129 case HDA_VMUTE_FOLLOW_MASTER:
2130 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2131 break;
2132 default:
2133 hook->hook(hook->codec, hook->mute_mode);
2134 break;
2135 }
2136}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002137EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002138
2139
Takashi Iwaid5191e52009-11-16 14:58:17 +01002140/**
2141 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002142 * @kcontrol: referred ctl element
2143 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002144 *
2145 * The control element is supposed to have the private_value field
2146 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2147 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002148int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2149 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 int chs = get_amp_channels(kcontrol);
2152
2153 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2154 uinfo->count = chs == 3 ? 2 : 1;
2155 uinfo->value.integer.min = 0;
2156 uinfo->value.integer.max = 1;
2157 return 0;
2158}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002159EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Takashi Iwaid5191e52009-11-16 14:58:17 +01002161/**
2162 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002163 * @kcontrol: ctl element
2164 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165 *
2166 * The control element is supposed to have the private_value field
2167 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2168 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002169int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2173 hda_nid_t nid = get_amp_nid(kcontrol);
2174 int chs = get_amp_channels(kcontrol);
2175 int dir = get_amp_direction(kcontrol);
2176 int idx = get_amp_index(kcontrol);
2177 long *valp = ucontrol->value.integer.value;
2178
2179 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002180 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002181 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002184 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return 0;
2186}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002187EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Takashi Iwaid5191e52009-11-16 14:58:17 +01002189/**
2190 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002191 * @kcontrol: ctl element
2192 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002193 *
2194 * The control element is supposed to have the private_value field
2195 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2196 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002197int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2201 hda_nid_t nid = get_amp_nid(kcontrol);
2202 int chs = get_amp_channels(kcontrol);
2203 int dir = get_amp_direction(kcontrol);
2204 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 long *valp = ucontrol->value.integer.value;
2206 int change = 0;
2207
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002208 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002209 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2210 HDA_AMP_MUTE,
2211 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002212 valp++;
2213 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002214 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002215 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2216 HDA_AMP_MUTE,
2217 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002218 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return change;
2220}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002221EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223/*
Takashi Iwai985be542005-11-02 18:26:49 +01002224 * bound volume controls
2225 *
2226 * bind multiple volumes (# indices, from 0)
2227 */
2228
2229#define AMP_VAL_IDX_SHIFT 19
2230#define AMP_VAL_IDX_MASK (0x0f<<19)
2231
Takashi Iwaid5191e52009-11-16 14:58:17 +01002232/**
2233 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002234 * @kcontrol: ctl element
2235 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002236 *
2237 * The control element is supposed to have the private_value field
2238 * set up via HDA_BIND_MUTE*() macros.
2239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002240int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002242{
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 unsigned long pval;
2245 int err;
2246
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002247 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002248 pval = kcontrol->private_value;
2249 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2250 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2251 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002252 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002253 return err;
2254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002255EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002256
Takashi Iwaid5191e52009-11-16 14:58:17 +01002257/**
2258 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002259 * @kcontrol: ctl element
2260 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261 *
2262 * The control element is supposed to have the private_value field
2263 * set up via HDA_BIND_MUTE*() macros.
2264 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002265int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002267{
2268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2269 unsigned long pval;
2270 int i, indices, err = 0, change = 0;
2271
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002272 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002273 pval = kcontrol->private_value;
2274 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2275 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002276 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2277 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002278 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2279 if (err < 0)
2280 break;
2281 change |= err;
2282 }
2283 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002284 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002285 return err < 0 ? err : change;
2286}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002287EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002288
Takashi Iwaid5191e52009-11-16 14:58:17 +01002289/**
2290 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002291 * @kcontrol: referred ctl element
2292 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002293 *
2294 * The control element is supposed to have the private_value field
2295 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002296 */
2297int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2298 struct snd_ctl_elem_info *uinfo)
2299{
2300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2301 struct hda_bind_ctls *c;
2302 int err;
2303
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002304 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002305 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002306 kcontrol->private_value = *c->values;
2307 err = c->ops->info(kcontrol, uinfo);
2308 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002309 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002310 return err;
2311}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002312EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002313
Takashi Iwaid5191e52009-11-16 14:58:17 +01002314/**
2315 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002316 * @kcontrol: ctl element
2317 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002318 *
2319 * The control element is supposed to have the private_value field
2320 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2321 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002322int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324{
2325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2326 struct hda_bind_ctls *c;
2327 int err;
2328
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002329 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002330 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002331 kcontrol->private_value = *c->values;
2332 err = c->ops->get(kcontrol, ucontrol);
2333 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002334 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002335 return err;
2336}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002337EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002338
Takashi Iwaid5191e52009-11-16 14:58:17 +01002339/**
2340 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002341 * @kcontrol: ctl element
2342 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002343 *
2344 * The control element is supposed to have the private_value field
2345 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2346 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002347int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2348 struct snd_ctl_elem_value *ucontrol)
2349{
2350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2351 struct hda_bind_ctls *c;
2352 unsigned long *vals;
2353 int err = 0, change = 0;
2354
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002355 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002356 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002357 for (vals = c->values; *vals; vals++) {
2358 kcontrol->private_value = *vals;
2359 err = c->ops->put(kcontrol, ucontrol);
2360 if (err < 0)
2361 break;
2362 change |= err;
2363 }
2364 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002365 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002366 return err < 0 ? err : change;
2367}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002368EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002369
Takashi Iwaid5191e52009-11-16 14:58:17 +01002370/**
2371 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002372 * @kcontrol: ctl element
2373 * @op_flag: operation flag
2374 * @size: byte size of input TLV
2375 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002376 *
2377 * The control element is supposed to have the private_value field
2378 * set up via HDA_BIND_VOL() macro.
2379 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002380int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2381 unsigned int size, unsigned int __user *tlv)
2382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 struct hda_bind_ctls *c;
2385 int err;
2386
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002387 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002388 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002389 kcontrol->private_value = *c->values;
2390 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2391 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002392 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002393 return err;
2394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002395EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002396
2397struct hda_ctl_ops snd_hda_bind_vol = {
2398 .info = snd_hda_mixer_amp_volume_info,
2399 .get = snd_hda_mixer_amp_volume_get,
2400 .put = snd_hda_mixer_amp_volume_put,
2401 .tlv = snd_hda_mixer_amp_tlv
2402};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002403EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002404
2405struct hda_ctl_ops snd_hda_bind_sw = {
2406 .info = snd_hda_mixer_amp_switch_info,
2407 .get = snd_hda_mixer_amp_switch_get,
2408 .put = snd_hda_mixer_amp_switch_put,
2409 .tlv = snd_hda_mixer_amp_tlv
2410};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002411EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002412
2413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * SPDIF out controls
2415 */
2416
Takashi Iwai0ba21762007-04-16 11:29:14 +02002417static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2418 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2421 uinfo->count = 1;
2422 return 0;
2423}
2424
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2429 IEC958_AES0_NONAUDIO |
2430 IEC958_AES0_CON_EMPHASIS_5015 |
2431 IEC958_AES0_CON_NOT_COPYRIGHT;
2432 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2433 IEC958_AES1_CON_ORIGINAL;
2434 return 0;
2435}
2436
Takashi Iwai0ba21762007-04-16 11:29:14 +02002437static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2441 IEC958_AES0_NONAUDIO |
2442 IEC958_AES0_PRO_EMPHASIS_5015;
2443 return 0;
2444}
2445
Takashi Iwai0ba21762007-04-16 11:29:14 +02002446static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002450 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002451 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002453 mutex_lock(&codec->spdif_mutex);
2454 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002455 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2456 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2457 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2458 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002459 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 return 0;
2462}
2463
2464/* convert from SPDIF status bits to HDA SPDIF bits
2465 * bit 0 (DigEn) is always set zero (to be filled later)
2466 */
2467static unsigned short convert_from_spdif_status(unsigned int sbits)
2468{
2469 unsigned short val = 0;
2470
2471 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002472 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002474 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002476 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2477 IEC958_AES0_PRO_EMPHASIS_5015)
2478 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002480 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2481 IEC958_AES0_CON_EMPHASIS_5015)
2482 val |= AC_DIG1_EMPHASIS;
2483 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2484 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2488 }
2489 return val;
2490}
2491
2492/* convert to SPDIF status bits from HDA SPDIF bits
2493 */
2494static unsigned int convert_to_spdif_status(unsigned short val)
2495{
2496 unsigned int sbits = 0;
2497
Takashi Iwai0ba21762007-04-16 11:29:14 +02002498 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002500 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 sbits |= IEC958_AES0_PROFESSIONAL;
2502 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002503 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2505 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002508 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002510 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2512 sbits |= val & (0x7f << 8);
2513 }
2514 return sbits;
2515}
2516
Takashi Iwai2f728532008-09-25 16:32:41 +02002517/* set digital convert verbs both for the given NID and its slaves */
2518static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002519 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002520{
Takashi Iwaidda14412011-05-02 11:29:30 +02002521 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002522
Takashi Iwaia551d912015-02-26 12:34:49 +01002523 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2524 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002525 d = codec->slave_dig_outs;
2526 if (!d)
2527 return;
2528 for (; *d; d++)
Takashi Iwaia551d912015-02-26 12:34:49 +01002529 snd_hdac_regmap_update(&codec->core, nid,
2530 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002531}
2532
2533static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2534 int dig1, int dig2)
2535{
Takashi Iwaia551d912015-02-26 12:34:49 +01002536 unsigned int mask = 0;
2537 unsigned int val = 0;
2538
2539 if (dig1 != -1) {
2540 mask |= 0xff;
2541 val = dig1;
2542 }
2543 if (dig2 != -1) {
2544 mask |= 0xff00;
2545 val |= dig2 << 8;
2546 }
2547 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002548}
2549
Takashi Iwai0ba21762007-04-16 11:29:14 +02002550static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2551 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002554 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002555 struct hda_spdif_out *spdif;
2556 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 unsigned short val;
2558 int change;
2559
Ingo Molnar62932df2006-01-16 16:34:20 +01002560 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002561 spdif = snd_array_elem(&codec->spdif_out, idx);
2562 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002563 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2565 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2566 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002567 val = convert_from_spdif_status(spdif->status);
2568 val |= spdif->ctls & 1;
2569 change = spdif->ctls != val;
2570 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002571 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002572 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002573 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return change;
2575}
2576
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002577#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002583 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002584 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002586 mutex_lock(&codec->spdif_mutex);
2587 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002588 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002589 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return 0;
2591}
2592
Stephen Warren74b654c2011-06-01 11:14:18 -06002593static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2594 int dig1, int dig2)
2595{
2596 set_dig_out_convert(codec, nid, dig1, dig2);
2597 /* unmute amp switch (if any) */
2598 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2599 (dig1 & AC_DIG1_ENABLE))
2600 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2601 HDA_AMP_MUTE, 0);
2602}
2603
Takashi Iwai0ba21762007-04-16 11:29:14 +02002604static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002608 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002609 struct hda_spdif_out *spdif;
2610 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 unsigned short val;
2612 int change;
2613
Ingo Molnar62932df2006-01-16 16:34:20 +01002614 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002615 spdif = snd_array_elem(&codec->spdif_out, idx);
2616 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002617 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002619 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002620 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002621 spdif->ctls = val;
2622 if (change && nid != (u16)-1)
2623 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002624 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return change;
2626}
2627
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002628static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 {
2630 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002632 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 .info = snd_hda_spdif_mask_info,
2634 .get = snd_hda_spdif_cmask_get,
2635 },
2636 {
2637 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002639 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_pmask_get,
2642 },
2643 {
2644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002645 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .info = snd_hda_spdif_mask_info,
2647 .get = snd_hda_spdif_default_get,
2648 .put = snd_hda_spdif_default_put,
2649 },
2650 {
2651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002652 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 .info = snd_hda_spdif_out_switch_info,
2654 .get = snd_hda_spdif_out_switch_get,
2655 .put = snd_hda_spdif_out_switch_put,
2656 },
2657 { } /* end */
2658};
2659
2660/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002661 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002663 * @associated_nid: NID that new ctls associated with
2664 * @cvt_nid: converter NID
2665 * @type: HDA_PCM_TYPE_*
2666 * Creates controls related with the digital output.
2667 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 *
2669 * Returns 0 if successful, or a negative error code.
2670 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002671int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2672 hda_nid_t associated_nid,
2673 hda_nid_t cvt_nid,
2674 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002677 struct snd_kcontrol *kctl;
2678 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002679 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002680 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002681 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002682 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002683 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002685 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002686 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002687 idx = spdif_index;
2688 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002689 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002690 /* suppose a single SPDIF device */
2691 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2692 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2693 if (!kctl)
2694 break;
2695 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002696 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002697 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002698 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002699 if (!bus->primary_dig_out_type)
2700 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002701
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002702 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002703 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002704 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002705 return -EBUSY;
2706 }
Stephen Warren7c935972011-06-01 11:14:17 -06002707 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002708 if (!spdif)
2709 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2711 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002712 if (!kctl)
2713 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002714 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002715 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002716 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return err;
2719 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002720 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002721 snd_hdac_regmap_read(&codec->core, cvt_nid,
2722 AC_VERB_GET_DIGI_CONVERT_1, &val);
2723 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002724 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return 0;
2726}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002727EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Takashi Iwai95a962c2014-10-29 16:03:58 +01002729/**
2730 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2731 * @codec: the HDA codec
2732 * @nid: widget NID
2733 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002734 * call within spdif_mutex lock
2735 */
Stephen Warren7c935972011-06-01 11:14:17 -06002736struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2737 hda_nid_t nid)
2738{
2739 int i;
2740 for (i = 0; i < codec->spdif_out.used; i++) {
2741 struct hda_spdif_out *spdif =
2742 snd_array_elem(&codec->spdif_out, i);
2743 if (spdif->nid == nid)
2744 return spdif;
2745 }
2746 return NULL;
2747}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002748EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002749
Takashi Iwai95a962c2014-10-29 16:03:58 +01002750/**
2751 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2752 * @codec: the HDA codec
2753 * @idx: the SPDIF ctl index
2754 *
2755 * Unassign the widget from the given SPDIF control.
2756 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002757void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2758{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002759 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002760
2761 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002762 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002763 spdif->nid = (u16)-1;
2764 mutex_unlock(&codec->spdif_mutex);
2765}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002766EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002767
Takashi Iwai95a962c2014-10-29 16:03:58 +01002768/**
2769 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2770 * @codec: the HDA codec
2771 * @idx: the SPDIF ctl idx
2772 * @nid: widget NID
2773 *
2774 * Assign the widget to the SPDIF control with the given index.
2775 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002776void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2777{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002778 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002779 unsigned short val;
2780
2781 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002782 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002783 if (spdif->nid != nid) {
2784 spdif->nid = nid;
2785 val = spdif->ctls;
2786 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2787 }
2788 mutex_unlock(&codec->spdif_mutex);
2789}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002790EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002793 * SPDIF sharing with analog output
2794 */
2795static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2796 struct snd_ctl_elem_value *ucontrol)
2797{
2798 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2799 ucontrol->value.integer.value[0] = mout->share_spdif;
2800 return 0;
2801}
2802
2803static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2805{
2806 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2807 mout->share_spdif = !!ucontrol->value.integer.value[0];
2808 return 0;
2809}
2810
2811static struct snd_kcontrol_new spdif_share_sw = {
2812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2813 .name = "IEC958 Default PCM Playback Switch",
2814 .info = snd_ctl_boolean_mono_info,
2815 .get = spdif_share_sw_get,
2816 .put = spdif_share_sw_put,
2817};
2818
Takashi Iwaid5191e52009-11-16 14:58:17 +01002819/**
2820 * snd_hda_create_spdif_share_sw - create Default PCM switch
2821 * @codec: the HDA codec
2822 * @mout: multi-out instance
2823 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002824int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2825 struct hda_multi_out *mout)
2826{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002827 struct snd_kcontrol *kctl;
2828
Takashi Iwai9a081602008-02-12 18:37:26 +01002829 if (!mout->dig_out_nid)
2830 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002831
2832 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2833 if (!kctl)
2834 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002835 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002836 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002837}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002838EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002839
2840/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * SPDIF input
2842 */
2843
2844#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2845
Takashi Iwai0ba21762007-04-16 11:29:14 +02002846static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2850
2851 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2852 return 0;
2853}
2854
Takashi Iwai0ba21762007-04-16 11:29:14 +02002855static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2856 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2859 hda_nid_t nid = kcontrol->private_value;
2860 unsigned int val = !!ucontrol->value.integer.value[0];
2861 int change;
2862
Ingo Molnar62932df2006-01-16 16:34:20 +01002863 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002865 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002867 snd_hdac_regmap_write(&codec->core, nid,
2868 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002870 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return change;
2872}
2873
Takashi Iwai0ba21762007-04-16 11:29:14 +02002874static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
2877 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2878 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002879 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 unsigned int sbits;
2881
Takashi Iwaia551d912015-02-26 12:34:49 +01002882 snd_hdac_regmap_read(&codec->core, nid,
2883 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 sbits = convert_to_spdif_status(val);
2885 ucontrol->value.iec958.status[0] = sbits;
2886 ucontrol->value.iec958.status[1] = sbits >> 8;
2887 ucontrol->value.iec958.status[2] = sbits >> 16;
2888 ucontrol->value.iec958.status[3] = sbits >> 24;
2889 return 0;
2890}
2891
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002892static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 {
2894 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002895 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .info = snd_hda_spdif_in_switch_info,
2897 .get = snd_hda_spdif_in_switch_get,
2898 .put = snd_hda_spdif_in_switch_put,
2899 },
2900 {
2901 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2902 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002903 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 .info = snd_hda_spdif_mask_info,
2905 .get = snd_hda_spdif_in_status_get,
2906 },
2907 { } /* end */
2908};
2909
2910/**
2911 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2912 * @codec: the HDA codec
2913 * @nid: audio in widget NID
2914 *
2915 * Creates controls related with the SPDIF input.
2916 * Called from each patch supporting the SPDIF in.
2917 *
2918 * Returns 0 if successful, or a negative error code.
2919 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002920int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002923 struct snd_kcontrol *kctl;
2924 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002925 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Takashi Iwaidcda5802012-10-12 17:24:51 +02002927 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002928 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002929 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002930 return -EBUSY;
2931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2933 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002934 if (!kctl)
2935 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002937 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return err;
2940 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002941 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002942 snd_hda_codec_read(codec, nid, 0,
2943 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002944 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return 0;
2946}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002947EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Takashi Iwai95a962c2014-10-29 16:03:58 +01002949/**
2950 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2951 * @codec: the HDA codec
2952 * @fg: function group (not used now)
2953 * @power_state: the power state to set (AC_PWRST_*)
2954 *
2955 * Set the given power state to all widgets that have the power control.
2956 * If the codec has power_filter set, it evaluates the power state and
2957 * filter out if it's unchanged as D3.
2958 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002959void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002960 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002961{
Takashi Iwai7639a062015-03-03 10:07:24 +01002962 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002963
Takashi Iwai7639a062015-03-03 10:07:24 +01002964 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002966 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002967 if (!(wcaps & AC_WCAP_POWER))
2968 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002969 if (codec->power_filter) {
2970 state = codec->power_filter(codec, nid, power_state);
2971 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002972 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002973 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002974 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002975 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002976 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002977}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002978EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002979
2980/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002981 * wait until the state is reached, returns the current state
2982 */
2983static unsigned int hda_sync_power_state(struct hda_codec *codec,
2984 hda_nid_t fg,
2985 unsigned int power_state)
2986{
2987 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2988 unsigned int state, actual_state;
2989
2990 for (;;) {
2991 state = snd_hda_codec_read(codec, fg, 0,
2992 AC_VERB_GET_POWER_STATE, 0);
2993 if (state & AC_PWRST_ERROR)
2994 break;
2995 actual_state = (state >> 4) & 0x0f;
2996 if (actual_state == power_state)
2997 break;
2998 if (time_after_eq(jiffies, end_time))
2999 break;
3000 /* wait until the codec reachs to the target state */
3001 msleep(1);
3002 }
3003 return state;
3004}
3005
Takashi Iwai95a962c2014-10-29 16:03:58 +01003006/**
3007 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3008 * @codec: the HDA codec
3009 * @nid: widget NID
3010 * @power_state: power state to evalue
3011 *
3012 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3013 * This can be used a codec power_filter callback.
3014 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003015unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3016 hda_nid_t nid,
3017 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003018{
Takashi Iwai7639a062015-03-03 10:07:24 +01003019 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003020 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003021 if (power_state == AC_PWRST_D3 &&
3022 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3023 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3024 int eapd = snd_hda_codec_read(codec, nid, 0,
3025 AC_VERB_GET_EAPD_BTLENABLE, 0);
3026 if (eapd & 0x02)
3027 return AC_PWRST_D0;
3028 }
3029 return power_state;
3030}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003031EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003032
Takashi Iwai432c6412012-08-28 09:59:20 -07003033/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003034 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003035 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003036static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003037 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003038{
Takashi Iwai7639a062015-03-03 10:07:24 +01003039 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003040 int count;
3041 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003042 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003043
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003044 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003045 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003046 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003047 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003048 else if (codec->depop_delay > 0)
3049 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003050 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003051 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003052
3053 /* repeat power states setting at most 10 times*/
3054 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003055 if (codec->patch_ops.set_power_state)
3056 codec->patch_ops.set_power_state(codec, fg,
3057 power_state);
3058 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003059 state = power_state;
3060 if (codec->power_filter)
3061 state = codec->power_filter(codec, fg, state);
3062 if (state == power_state || power_state != AC_PWRST_D3)
3063 snd_hda_codec_read(codec, fg, flags,
3064 AC_VERB_SET_POWER_STATE,
3065 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003066 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003067 }
3068 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003069 if (!(state & AC_PWRST_ERROR))
3070 break;
3071 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003072
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003073 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003074}
Takashi Iwai54d17402005-11-21 16:33:22 +01003075
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003076/* sync power states of all widgets;
3077 * this is called at the end of codec parsing
3078 */
3079static void sync_power_up_states(struct hda_codec *codec)
3080{
Takashi Iwai7639a062015-03-03 10:07:24 +01003081 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003082
Takashi Iwaiba615b82013-03-13 14:47:21 +01003083 /* don't care if no filter is used */
3084 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003085 return;
3086
Takashi Iwai7639a062015-03-03 10:07:24 +01003087 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003088 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003089 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003090 if (!(wcaps & AC_WCAP_POWER))
3091 continue;
3092 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3093 if (target == AC_PWRST_D0)
3094 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003095 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003096 snd_hda_codec_write(codec, nid, 0,
3097 AC_VERB_SET_POWER_STATE, target);
3098 }
3099}
3100
Takashi Iwai648a8d22014-02-25 10:38:13 +01003101#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003102/* execute additional init verbs */
3103static void hda_exec_init_verbs(struct hda_codec *codec)
3104{
3105 if (codec->init_verbs.list)
3106 snd_hda_sequence_write(codec, codec->init_verbs.list);
3107}
3108#else
3109static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3110#endif
3111
Takashi Iwai2a439522011-07-26 09:52:50 +02003112#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003113/* update the power on/off account with the current jiffies */
3114static void update_power_acct(struct hda_codec *codec, bool on)
3115{
3116 unsigned long delta = jiffies - codec->power_jiffies;
3117
3118 if (on)
3119 codec->power_on_acct += delta;
3120 else
3121 codec->power_off_acct += delta;
3122 codec->power_jiffies += delta;
3123}
3124
3125void snd_hda_update_power_acct(struct hda_codec *codec)
3126{
3127 update_power_acct(codec, hda_codec_is_power_on(codec));
3128}
3129
Takashi Iwaicb53c622007-08-10 17:21:45 +02003130/*
3131 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003132 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003133 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003134static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003135{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003136 unsigned int state;
3137
Takashi Iwai7639a062015-03-03 10:07:24 +01003138 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003139
Takashi Iwaicb53c622007-08-10 17:21:45 +02003140 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003141 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003142 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003143 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003144 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003145 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003146 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003147}
3148
3149/*
3150 * kick up codec; used both from PM and power-save
3151 */
3152static void hda_call_codec_resume(struct hda_codec *codec)
3153{
Takashi Iwai7639a062015-03-03 10:07:24 +01003154 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003155
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003156 if (codec->core.regmap)
3157 regcache_mark_dirty(codec->core.regmap);
3158
Takashi Iwaicc72da72015-02-19 16:00:22 +01003159 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003160
Takashi Iwaid8193872012-08-31 07:54:38 -07003161 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003162 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003163 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003164 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003165 if (codec->patch_ops.resume)
3166 codec->patch_ops.resume(codec);
3167 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003168 if (codec->patch_ops.init)
3169 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003170 if (codec->core.regmap)
3171 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003172 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003173
3174 if (codec->jackpoll_interval)
3175 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003176 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003177 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003178 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003179}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003180
Takashi Iwaicc72da72015-02-19 16:00:22 +01003181static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003182{
3183 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003184 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003185 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003186
3187 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003188 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3189 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003190 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003191 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3192 (state & AC_PWRST_CLK_STOP_OK))
3193 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003194 return 0;
3195}
3196
Takashi Iwaicc72da72015-02-19 16:00:22 +01003197static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003198{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003199 struct hda_codec *codec = dev_to_hda_codec(dev);
3200
Takashi Iwai7639a062015-03-03 10:07:24 +01003201 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003202 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003203 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003204 return 0;
3205}
Takashi Iwai2a439522011-07-26 09:52:50 +02003206#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003207
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003208/* referred in hda_bind.c */
3209const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003210 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3211 pm_runtime_force_resume)
3212 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3213 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003214};
Takashi Iwai54d17402005-11-21 16:33:22 +01003215
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003216/*
3217 * add standard channel maps if not specified
3218 */
3219static int add_std_chmaps(struct hda_codec *codec)
3220{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003221 struct hda_pcm *pcm;
3222 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003223
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003224 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003225 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003226 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003227 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003228 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003229
Sudip Mukherjee751e2212015-04-02 16:51:43 +05303230 if (!pcm || pcm->own_chmap ||
3231 !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003232 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003233 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003234 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003235 hinfo->channels_max,
3236 0, &chmap);
3237 if (err < 0)
3238 return err;
3239 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3240 }
3241 }
3242 return 0;
3243}
3244
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003245/* default channel maps for 2.1 speakers;
3246 * since HD-audio supports only stereo, odd number channels are omitted
3247 */
3248const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3249 { .channels = 2,
3250 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3251 { .channels = 4,
3252 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3253 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3254 { }
3255};
3256EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3257
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003258int snd_hda_codec_build_controls(struct hda_codec *codec)
3259{
3260 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003261 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003262 /* continue to initialize... */
3263 if (codec->patch_ops.init)
3264 err = codec->patch_ops.init(codec);
3265 if (!err && codec->patch_ops.build_controls)
3266 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003267 if (err < 0)
3268 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003269
3270 /* we create chmaps here instead of build_pcms */
3271 err = add_std_chmaps(codec);
3272 if (err < 0)
3273 return err;
3274
David Henningsson26a6cb62012-10-09 15:04:21 +02003275 if (codec->jackpoll_interval)
3276 hda_jackpoll_work(&codec->jackpoll_work.work);
3277 else
3278 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003279 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return 0;
3281}
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283/*
3284 * stream formats
3285 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003286struct hda_rate_tbl {
3287 unsigned int hz;
3288 unsigned int alsa_bits;
3289 unsigned int hda_fmt;
3290};
3291
Takashi Iwai92f10b32010-08-03 14:21:00 +02003292/* rate = base * mult / div */
3293#define HDA_RATE(base, mult, div) \
3294 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3295 (((div) - 1) << AC_FMT_DIV_SHIFT))
3296
Takashi Iwaibefdf312005-08-22 13:57:55 +02003297static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003299
3300 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003301 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3302 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3303 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3304 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3305 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3306 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3307 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3308 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3309 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3310 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3311 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003312#define AC_PAR_PCM_RATE_BITS 11
3313 /* up to bits 10, 384kHZ isn't supported properly */
3314
3315 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003316 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003317
Takashi Iwaibefdf312005-08-22 13:57:55 +02003318 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319};
3320
3321/**
3322 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003323 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 * @rate: the sample rate
3325 * @channels: the number of channels
3326 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3327 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003328 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 *
3330 * Calculate the format bitset from the given rate, channels and th PCM format.
3331 *
3332 * Return zero if invalid.
3333 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003334unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3335 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 unsigned int channels,
3337 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003338 unsigned int maxbps,
3339 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
3341 int i;
3342 unsigned int val = 0;
3343
Takashi Iwaibefdf312005-08-22 13:57:55 +02003344 for (i = 0; rate_bits[i].hz; i++)
3345 if (rate_bits[i].hz == rate) {
3346 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 break;
3348 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003349 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003350 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return 0;
3352 }
3353
3354 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003355 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return 0;
3357 }
3358 val |= channels - 1;
3359
3360 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003361 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003362 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003363 break;
3364 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003365 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 case 20:
3368 case 24:
3369 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003370 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003371 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003373 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003375 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 break;
3377 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003378 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003379 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return 0;
3381 }
3382
Anssi Hannula32c168c2010-08-03 13:28:57 +03003383 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003384 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return val;
3387}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003388EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003390static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003391{
3392 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003393 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003394 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3395 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3396 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003397 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003398 if (!val || val == -1)
3399 return 0;
3400 return val;
3401}
3402
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003403static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003404{
3405 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3406 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003407 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003408 if (!streams || streams == -1)
3409 return 0;
3410 return streams;
3411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/**
3414 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3415 * @codec: the HDA codec
3416 * @nid: NID to query
3417 * @ratesp: the pointer to store the detected rate bitflags
3418 * @formatsp: the pointer to store the detected formats
3419 * @bpsp: the pointer to store the detected format widths
3420 *
3421 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3422 * or @bsps argument is ignored.
3423 *
3424 * Returns 0 if successful, otherwise a negative error code.
3425 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003426int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3428{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003429 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003431 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003432 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
3434 if (ratesp) {
3435 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003436 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003438 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003440 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003441 codec_err(codec,
3442 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3443 nid, val,
3444 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003445 return -EIO;
3446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 *ratesp = rates;
3448 }
3449
3450 if (formatsp || bpsp) {
3451 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003452 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003454 streams = query_stream_param(codec, nid);
3455 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 bps = 0;
3459 if (streams & AC_SUPFMT_PCM) {
3460 if (val & AC_SUPPCM_BITS_8) {
3461 formats |= SNDRV_PCM_FMTBIT_U8;
3462 bps = 8;
3463 }
3464 if (val & AC_SUPPCM_BITS_16) {
3465 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3466 bps = 16;
3467 }
3468 if (wcaps & AC_WCAP_DIGITAL) {
3469 if (val & AC_SUPPCM_BITS_32)
3470 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3471 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3472 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3473 if (val & AC_SUPPCM_BITS_24)
3474 bps = 24;
3475 else if (val & AC_SUPPCM_BITS_20)
3476 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003477 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3478 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3480 if (val & AC_SUPPCM_BITS_32)
3481 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 else if (val & AC_SUPPCM_BITS_24)
3483 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003484 else if (val & AC_SUPPCM_BITS_20)
3485 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
3487 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003488#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003489 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003491 if (!bps)
3492 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003493 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003494#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003495 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 /* temporary hack: we have still no proper support
3498 * for the direct AC3 stream...
3499 */
3500 formats |= SNDRV_PCM_FMTBIT_U8;
3501 bps = 8;
3502 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003503 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003504 codec_err(codec,
3505 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3506 nid, val,
3507 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3508 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003509 return -EIO;
3510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (formatsp)
3512 *formatsp = formats;
3513 if (bpsp)
3514 *bpsp = bps;
3515 }
3516
3517 return 0;
3518}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003519EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003522 * snd_hda_is_supported_format - Check the validity of the format
3523 * @codec: HD-audio codec
3524 * @nid: NID to check
3525 * @format: the HD-audio format value to check
3526 *
3527 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 *
3529 * Returns 1 if supported, 0 if not.
3530 */
3531int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3532 unsigned int format)
3533{
3534 int i;
3535 unsigned int val = 0, rate, stream;
3536
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003537 val = query_pcm_param(codec, nid);
3538 if (!val)
3539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003542 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003543 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (val & (1 << i))
3545 break;
3546 return 0;
3547 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003548 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 return 0;
3550
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003551 stream = query_stream_param(codec, nid);
3552 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return 0;
3554
3555 if (stream & AC_SUPFMT_PCM) {
3556 switch (format & 0xf0) {
3557 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return 0;
3560 break;
3561 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003562 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return 0;
3564 break;
3565 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003566 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return 0;
3568 break;
3569 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003570 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return 0;
3572 break;
3573 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003574 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return 0;
3576 break;
3577 default:
3578 return 0;
3579 }
3580 } else {
3581 /* FIXME: check for float32 and AC3? */
3582 }
3583
3584 return 1;
3585}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003586EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588/*
3589 * PCM stuff
3590 */
3591static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3592 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003593 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
3595 return 0;
3596}
3597
3598static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3599 struct hda_codec *codec,
3600 unsigned int stream_tag,
3601 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003602 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3605 return 0;
3606}
3607
3608static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3609 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003610 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
Takashi Iwai888afa12008-03-18 09:57:50 +01003612 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return 0;
3614}
3615
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003616static int set_pcm_default_values(struct hda_codec *codec,
3617 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003619 int err;
3620
Takashi Iwai0ba21762007-04-16 11:29:14 +02003621 /* query support PCM information from the given NID */
3622 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003623 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003624 info->rates ? NULL : &info->rates,
3625 info->formats ? NULL : &info->formats,
3626 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003627 if (err < 0)
3628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
3630 if (info->ops.open == NULL)
3631 info->ops.open = hda_pcm_default_open_close;
3632 if (info->ops.close == NULL)
3633 info->ops.close = hda_pcm_default_open_close;
3634 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003635 if (snd_BUG_ON(!info->nid))
3636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 info->ops.prepare = hda_pcm_default_prepare;
3638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003640 if (snd_BUG_ON(!info->nid))
3641 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 info->ops.cleanup = hda_pcm_default_cleanup;
3643 }
3644 return 0;
3645}
3646
Takashi Iwaieb541332010-08-06 13:48:11 +02003647/*
3648 * codec prepare/cleanup entries
3649 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003650/**
3651 * snd_hda_codec_prepare - Prepare a stream
3652 * @codec: the HDA codec
3653 * @hinfo: PCM information
3654 * @stream: stream tag to assign
3655 * @format: format id to assign
3656 * @substream: PCM substream to assign
3657 *
3658 * Calls the prepare callback set by the codec with the given arguments.
3659 * Clean up the inactive streams when successful.
3660 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003661int snd_hda_codec_prepare(struct hda_codec *codec,
3662 struct hda_pcm_stream *hinfo,
3663 unsigned int stream,
3664 unsigned int format,
3665 struct snd_pcm_substream *substream)
3666{
3667 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003668 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003669 if (hinfo->ops.prepare)
3670 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3671 substream);
3672 else
3673 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003674 if (ret >= 0)
3675 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003676 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003677 return ret;
3678}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003679EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003680
Takashi Iwai95a962c2014-10-29 16:03:58 +01003681/**
3682 * snd_hda_codec_cleanup - Prepare a stream
3683 * @codec: the HDA codec
3684 * @hinfo: PCM information
3685 * @substream: PCM substream
3686 *
3687 * Calls the cleanup callback set by the codec with the given arguments.
3688 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003689void snd_hda_codec_cleanup(struct hda_codec *codec,
3690 struct hda_pcm_stream *hinfo,
3691 struct snd_pcm_substream *substream)
3692{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003693 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003694 if (hinfo->ops.cleanup)
3695 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003696 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003697}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003698EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003699
Takashi Iwaid5191e52009-11-16 14:58:17 +01003700/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003701const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3702 "Audio", "SPDIF", "HDMI", "Modem"
3703};
3704
Takashi Iwai176d5332008-07-30 15:01:44 +02003705/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003706 * get the empty PCM device number to assign
3707 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003708static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003709{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003710 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003711 /* assigned to static slots up to dev#10; if more needed, assign
3712 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3713 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003714 static int audio_idx[HDA_PCM_NTYPES][5] = {
3715 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3716 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003717 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003718 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003719 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003720 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003721
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003722 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003723 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003724 return -EINVAL;
3725 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003726
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003727 for (i = 0; audio_idx[type][i] >= 0; i++) {
3728#ifndef CONFIG_SND_DYNAMIC_MINORS
3729 if (audio_idx[type][i] >= 8)
3730 break;
3731#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003732 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3733 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003734 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003735
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003736#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003737 /* non-fixed slots starting from 10 */
3738 for (i = 10; i < 32; i++) {
3739 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3740 return i;
3741 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003742#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003743
Takashi Iwai4e76a882014-02-25 12:21:03 +01003744 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003745 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003746#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003747 dev_warn(bus->card->dev,
3748 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003749#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003750 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003751}
3752
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003753/* call build_pcms ops of the given codec and set up the default parameters */
3754int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003755{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003756 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003757 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003758
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003759 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003760 return 0; /* already parsed */
3761
3762 if (!codec->patch_ops.build_pcms)
3763 return 0;
3764
3765 err = codec->patch_ops.build_pcms(codec);
3766 if (err < 0) {
3767 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003768 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003769 return err;
3770 }
3771
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003772 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003773 int stream;
3774
3775 for (stream = 0; stream < 2; stream++) {
3776 struct hda_pcm_stream *info = &cpcm->stream[stream];
3777
3778 if (!info->substreams)
3779 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003780 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003781 if (err < 0) {
3782 codec_warn(codec,
3783 "fail to setup default for PCM %s\n",
3784 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003785 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003786 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003787 }
3788 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003789
3790 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003791}
3792
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003793/* assign all PCMs of the given codec */
3794int snd_hda_codec_build_pcms(struct hda_codec *codec)
3795{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003796 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003797 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003798 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003799
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003800 if (snd_BUG_ON(!bus->ops.attach_pcm))
3801 return -EINVAL;
3802
3803 err = snd_hda_codec_parse_pcms(codec);
3804 if (err < 0) {
3805 snd_hda_codec_reset(codec);
3806 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003807 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003808
3809 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003810 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003811 if (cpcm->pcm)
3812 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003813 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003814 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003815
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003816 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3817 if (dev < 0)
3818 continue; /* no fatal error */
3819 cpcm->device = dev;
3820 err = bus->ops.attach_pcm(bus, codec, cpcm);
3821 if (err < 0) {
3822 codec_err(codec,
3823 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003824 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003825 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003826 }
3827 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003828
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003829 return 0;
3830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * snd_hda_add_new_ctls - create controls from the array
3834 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003835 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 *
3837 * This helper function creates and add new controls in the given array.
3838 * The array must be terminated with an empty entry as terminator.
3839 *
3840 * Returns 0 if successful, or a negative error code.
3841 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003842int snd_hda_add_new_ctls(struct hda_codec *codec,
3843 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003848 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003849 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003850 if (knew->iface == -1) /* skip this codec private value */
3851 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003852 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003853 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003854 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003855 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003856 if (addr > 0)
3857 kctl->id.device = addr;
3858 if (idx > 0)
3859 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003860 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003861 if (!err)
3862 break;
3863 /* try first with another device index corresponding to
3864 * the codec addr; if it still fails (or it's the
3865 * primary codec), then try another control index
3866 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003867 if (!addr && codec->core.addr)
3868 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003869 else if (!idx && !knew->index) {
3870 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003871 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003872 if (idx <= 0)
3873 return err;
3874 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003875 return err;
3876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
3878 return 0;
3879}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003880EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Takashi Iwai83012a72012-08-24 18:38:08 +02003882#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003883static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003884{
3885 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003886
Takashi Iwaicc72da72015-02-19 16:00:22 +01003887 if (delay > 0) {
3888 pm_runtime_set_autosuspend_delay(dev, delay);
3889 pm_runtime_use_autosuspend(dev);
3890 pm_runtime_allow(dev);
3891 if (!pm_runtime_suspended(dev))
3892 pm_runtime_mark_last_busy(dev);
3893 } else {
3894 pm_runtime_dont_use_autosuspend(dev);
3895 pm_runtime_forbid(dev);
3896 }
3897}
Takashi Iwaibb573922015-02-20 09:26:04 +01003898
3899/**
3900 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3901 * @bus: HD-audio bus
3902 * @delay: autosuspend delay in msec, 0 = off
3903 *
3904 * Synchronize the runtime PM autosuspend state from the power_save option.
3905 */
3906void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3907{
3908 struct hda_codec *c;
3909
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003910 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003911 codec_set_power_save(c, delay);
3912}
3913EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914
Takashi Iwaid5191e52009-11-16 14:58:17 +01003915/**
3916 * snd_hda_check_amp_list_power - Check the amp list and update the power
3917 * @codec: HD-audio codec
3918 * @check: the object containing an AMP list and the status
3919 * @nid: NID to check / update
3920 *
3921 * Check whether the given NID is in the amp list. If it's in the list,
3922 * check the current AMP status, and update the the power-status according
3923 * to the mute status.
3924 *
3925 * This function is supposed to be set or called from the check_power_status
3926 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003927 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003928int snd_hda_check_amp_list_power(struct hda_codec *codec,
3929 struct hda_loopback_check *check,
3930 hda_nid_t nid)
3931{
Takashi Iwai031024e2011-05-02 11:29:30 +02003932 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003933 int ch, v;
3934
3935 if (!check->amplist)
3936 return 0;
3937 for (p = check->amplist; p->nid; p++) {
3938 if (p->nid == nid)
3939 break;
3940 }
3941 if (!p->nid)
3942 return 0; /* nothing changed */
3943
3944 for (p = check->amplist; p->nid; p++) {
3945 for (ch = 0; ch < 2; ch++) {
3946 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3947 p->idx);
3948 if (!(v & HDA_AMP_MUTE) && v > 0) {
3949 if (!check->power_on) {
3950 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003951 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003952 }
3953 return 1;
3954 }
3955 }
3956 }
3957 if (check->power_on) {
3958 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003959 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003960 }
3961 return 0;
3962}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003963EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 * input MUX helper
3968 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003969
3970/**
3971 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003972 * @imux: imux helper object
3973 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003975int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3976 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
3978 unsigned int index;
3979
3980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3981 uinfo->count = 1;
3982 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003983 if (!imux->num_items)
3984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 index = uinfo->value.enumerated.item;
3986 if (index >= imux->num_items)
3987 index = imux->num_items - 1;
3988 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3989 return 0;
3990}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003991EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Takashi Iwaid5191e52009-11-16 14:58:17 +01003993/**
3994 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003995 * @codec: the HDA codec
3996 * @imux: imux helper object
3997 * @ucontrol: pointer to get/store the data
3998 * @nid: input mux NID
3999 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004000 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004001int snd_hda_input_mux_put(struct hda_codec *codec,
4002 const struct hda_input_mux *imux,
4003 struct snd_ctl_elem_value *ucontrol,
4004 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 unsigned int *cur_val)
4006{
4007 unsigned int idx;
4008
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004009 if (!imux->num_items)
4010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 idx = ucontrol->value.enumerated.item[0];
4012 if (idx >= imux->num_items)
4013 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004014 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004016 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4017 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 *cur_val = idx;
4019 return 1;
4020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004021EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004024/**
4025 * snd_hda_enum_helper_info - Helper for simple enum ctls
4026 * @kcontrol: ctl element
4027 * @uinfo: pointer to get/store the data
4028 * @num_items: number of enum items
4029 * @texts: enum item string array
4030 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004031 * process kcontrol info callback of a simple string enum array
4032 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4033 */
4034int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4035 struct snd_ctl_elem_info *uinfo,
4036 int num_items, const char * const *texts)
4037{
4038 static const char * const texts_default[] = {
4039 "Disabled", "Enabled"
4040 };
4041
4042 if (!texts || !num_items) {
4043 num_items = 2;
4044 texts = texts_default;
4045 }
4046
Takashi Iwai3ff72212014-10-20 18:17:28 +02004047 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004048}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004049EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004050
4051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 * Multi-channel / digital-out PCM helper functions
4053 */
4054
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004055/* setup SPDIF output stream */
4056static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4057 unsigned int stream_tag, unsigned int format)
4058{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004059 struct hda_spdif_out *spdif;
4060 unsigned int curr_fmt;
4061 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004062
Laurence Darby3bef1c32012-11-03 17:00:06 +00004063 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4064 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4065 AC_VERB_GET_STREAM_FORMAT, 0);
4066 reset = codec->spdif_status_reset &&
4067 (spdif->ctls & AC_DIG1_ENABLE) &&
4068 curr_fmt != format;
4069
4070 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4071 updated */
4072 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004073 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004074 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004075 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004076 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004077 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004078 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004079 for (d = codec->slave_dig_outs; *d; d++)
4080 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4081 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004082 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004083 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004084 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004085 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004086 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004087}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004088
Takashi Iwai2f728532008-09-25 16:32:41 +02004089static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4090{
4091 snd_hda_codec_cleanup_stream(codec, nid);
4092 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004093 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004094 for (d = codec->slave_dig_outs; *d; d++)
4095 snd_hda_codec_cleanup_stream(codec, *d);
4096 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004097}
4098
Takashi Iwaid5191e52009-11-16 14:58:17 +01004099/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004100 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004101 * @codec: the HDA codec
4102 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004104int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4105 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
Ingo Molnar62932df2006-01-16 16:34:20 +01004107 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004108 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4109 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004110 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004112 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return 0;
4114}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004115EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Takashi Iwaid5191e52009-11-16 14:58:17 +01004117/**
4118 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004119 * @codec: the HDA codec
4120 * @mout: hda_multi_out object
4121 * @stream_tag: stream tag to assign
4122 * @format: format id to assign
4123 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004124 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004125int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4126 struct hda_multi_out *mout,
4127 unsigned int stream_tag,
4128 unsigned int format,
4129 struct snd_pcm_substream *substream)
4130{
4131 mutex_lock(&codec->spdif_mutex);
4132 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4133 mutex_unlock(&codec->spdif_mutex);
4134 return 0;
4135}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004136EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004137
Takashi Iwaid5191e52009-11-16 14:58:17 +01004138/**
4139 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004140 * @codec: the HDA codec
4141 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004142 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004143int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4144 struct hda_multi_out *mout)
4145{
4146 mutex_lock(&codec->spdif_mutex);
4147 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4148 mutex_unlock(&codec->spdif_mutex);
4149 return 0;
4150}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004151EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004152
Takashi Iwaid5191e52009-11-16 14:58:17 +01004153/**
4154 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004155 * @codec: the HDA codec
4156 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004158int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4159 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
Ingo Molnar62932df2006-01-16 16:34:20 +01004161 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004163 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return 0;
4165}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004166EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Takashi Iwaid5191e52009-11-16 14:58:17 +01004168/**
4169 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004170 * @codec: the HDA codec
4171 * @mout: hda_multi_out object
4172 * @substream: PCM substream to assign
4173 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004174 *
4175 * Open analog outputs and set up the hw-constraints.
4176 * If the digital outputs can be opened as slave, open the digital
4177 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004179int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4180 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004181 struct snd_pcm_substream *substream,
4182 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Takashi Iwai9a081602008-02-12 18:37:26 +01004184 struct snd_pcm_runtime *runtime = substream->runtime;
4185 runtime->hw.channels_max = mout->max_channels;
4186 if (mout->dig_out_nid) {
4187 if (!mout->analog_rates) {
4188 mout->analog_rates = hinfo->rates;
4189 mout->analog_formats = hinfo->formats;
4190 mout->analog_maxbps = hinfo->maxbps;
4191 } else {
4192 runtime->hw.rates = mout->analog_rates;
4193 runtime->hw.formats = mout->analog_formats;
4194 hinfo->maxbps = mout->analog_maxbps;
4195 }
4196 if (!mout->spdif_rates) {
4197 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4198 &mout->spdif_rates,
4199 &mout->spdif_formats,
4200 &mout->spdif_maxbps);
4201 }
4202 mutex_lock(&codec->spdif_mutex);
4203 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004204 if ((runtime->hw.rates & mout->spdif_rates) &&
4205 (runtime->hw.formats & mout->spdif_formats)) {
4206 runtime->hw.rates &= mout->spdif_rates;
4207 runtime->hw.formats &= mout->spdif_formats;
4208 if (mout->spdif_maxbps < hinfo->maxbps)
4209 hinfo->maxbps = mout->spdif_maxbps;
4210 } else {
4211 mout->share_spdif = 0;
4212 /* FIXME: need notify? */
4213 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004214 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004215 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4218 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4219}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004220EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Takashi Iwaid5191e52009-11-16 14:58:17 +01004222/**
4223 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004224 * @codec: the HDA codec
4225 * @mout: hda_multi_out object
4226 * @stream_tag: stream tag to assign
4227 * @format: format id to assign
4228 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004229 *
4230 * Set up the i/o for analog out.
4231 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004233int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4234 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 unsigned int stream_tag,
4236 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004237 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Takashi Iwaidda14412011-05-02 11:29:30 +02004239 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004241 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 int i;
4243
Ingo Molnar62932df2006-01-16 16:34:20 +01004244 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004245 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004246 if (mout->dig_out_nid && mout->share_spdif &&
4247 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004249 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4250 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004251 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004253 setup_dig_out_stream(codec, mout->dig_out_nid,
4254 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 } else {
4256 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004257 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
4259 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004260 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004263 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4264 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004265 if (!mout->no_share_stream &&
4266 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004268 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4269 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004270 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004271 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4272 if (!mout->no_share_stream && mout->hp_out_nid[i])
4273 snd_hda_codec_setup_stream(codec,
4274 mout->hp_out_nid[i],
4275 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 /* surrounds */
4278 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004279 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004280 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4281 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004282 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004283 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4284 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004286
4287 /* extra surrounds */
4288 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4289 int ch = 0;
4290 if (!mout->extra_out_nid[i])
4291 break;
4292 if (chs >= (i + 1) * 2)
4293 ch = i * 2;
4294 else if (!mout->no_share_stream)
4295 break;
4296 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4297 stream_tag, ch, format);
4298 }
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return 0;
4301}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004302EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Takashi Iwaid5191e52009-11-16 14:58:17 +01004304/**
4305 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004306 * @codec: the HDA codec
4307 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004309int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4310 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
Takashi Iwaidda14412011-05-02 11:29:30 +02004312 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 int i;
4314
4315 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004316 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004318 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004319 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4320 if (mout->hp_out_nid[i])
4321 snd_hda_codec_cleanup_stream(codec,
4322 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004323 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4324 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004325 snd_hda_codec_cleanup_stream(codec,
4326 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004327 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004329 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 mout->dig_out_used = 0;
4331 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004332 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return 0;
4334}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004335EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Takashi Iwai47408602012-04-20 13:06:53 +02004337/**
4338 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004339 * @codec: the HDA codec
4340 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004341 *
4342 * Guess the suitable VREF pin bits to be set as the pin-control value.
4343 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4344 */
4345unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4346{
4347 unsigned int pincap;
4348 unsigned int oldval;
4349 oldval = snd_hda_codec_read(codec, pin, 0,
4350 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4351 pincap = snd_hda_query_pin_caps(codec, pin);
4352 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4353 /* Exception: if the default pin setup is vref50, we give it priority */
4354 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4355 return AC_PINCTL_VREF_80;
4356 else if (pincap & AC_PINCAP_VREF_50)
4357 return AC_PINCTL_VREF_50;
4358 else if (pincap & AC_PINCAP_VREF_100)
4359 return AC_PINCTL_VREF_100;
4360 else if (pincap & AC_PINCAP_VREF_GRD)
4361 return AC_PINCTL_VREF_GRD;
4362 return AC_PINCTL_VREF_HIZ;
4363}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004364EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004365
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004366/**
4367 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4368 * @codec: the HDA codec
4369 * @pin: referred pin NID
4370 * @val: pin ctl value to audit
4371 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004372unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4373 hda_nid_t pin, unsigned int val)
4374{
4375 static unsigned int cap_lists[][2] = {
4376 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4377 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4378 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4379 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4380 };
4381 unsigned int cap;
4382
4383 if (!val)
4384 return 0;
4385 cap = snd_hda_query_pin_caps(codec, pin);
4386 if (!cap)
4387 return val; /* don't know what to do... */
4388
4389 if (val & AC_PINCTL_OUT_EN) {
4390 if (!(cap & AC_PINCAP_OUT))
4391 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4392 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4393 val &= ~AC_PINCTL_HP_EN;
4394 }
4395
4396 if (val & AC_PINCTL_IN_EN) {
4397 if (!(cap & AC_PINCAP_IN))
4398 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4399 else {
4400 unsigned int vcap, vref;
4401 int i;
4402 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4403 vref = val & AC_PINCTL_VREFEN;
4404 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4405 if (vref == cap_lists[i][0] &&
4406 !(vcap & cap_lists[i][1])) {
4407 if (i == ARRAY_SIZE(cap_lists) - 1)
4408 vref = AC_PINCTL_VREF_HIZ;
4409 else
4410 vref = cap_lists[i + 1][0];
4411 }
4412 }
4413 val &= ~AC_PINCTL_VREFEN;
4414 val |= vref;
4415 }
4416 }
4417
4418 return val;
4419}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004420EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004421
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004422/**
4423 * _snd_hda_pin_ctl - Helper to set pin ctl value
4424 * @codec: the HDA codec
4425 * @pin: referred pin NID
4426 * @val: pin control value to set
4427 * @cached: access over codec pinctl cache or direct write
4428 *
4429 * This function is a helper to set a pin ctl value more safely.
4430 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4431 * value in pin target array via snd_hda_codec_set_pin_target(), then
4432 * actually writes the value via either snd_hda_codec_update_cache() or
4433 * snd_hda_codec_write() depending on @cached flag.
4434 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004435int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4436 unsigned int val, bool cached)
4437{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004438 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004439 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004440 if (cached)
4441 return snd_hda_codec_update_cache(codec, pin, 0,
4442 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4443 else
4444 return snd_hda_codec_write(codec, pin, 0,
4445 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4446}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004447EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004448
Takashi Iwai990061c2010-09-09 22:08:44 +02004449/**
4450 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004451 * @codec: the HDA codec
4452 * @imux: imux helper object
4453 * @label: the name of imux item to assign
4454 * @index: index number of imux item to assign
4455 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004456 *
4457 * When the same label is used already in the existing items, the number
4458 * suffix is appended to the label. This label index number is stored
4459 * to type_idx when non-NULL pointer is given.
4460 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004461int snd_hda_add_imux_item(struct hda_codec *codec,
4462 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004463 int index, int *type_idx)
4464{
4465 int i, label_idx = 0;
4466 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004467 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004468 return -EINVAL;
4469 }
4470 for (i = 0; i < imux->num_items; i++) {
4471 if (!strncmp(label, imux->items[i].label, strlen(label)))
4472 label_idx++;
4473 }
4474 if (type_idx)
4475 *type_idx = label_idx;
4476 if (label_idx > 0)
4477 snprintf(imux->items[imux->num_items].label,
4478 sizeof(imux->items[imux->num_items].label),
4479 "%s %d", label, label_idx);
4480 else
4481 strlcpy(imux->items[imux->num_items].label, label,
4482 sizeof(imux->items[imux->num_items].label));
4483 imux->items[imux->num_items].index = index;
4484 imux->num_items++;
4485 return 0;
4486}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004487EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004490 * snd_hda_bus_reset - Reset the bus
4491 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004493void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004495 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004497 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004498 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004499 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004500#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004501 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004502 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004503 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004504 }
4505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004508EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004509
Takashi Iwaid5191e52009-11-16 14:58:17 +01004510/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004511 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4512 * @pcm: PCM caps bits
4513 * @buf: the string buffer to write
4514 * @buflen: the max buffer length
4515 *
4516 * used by hda_proc.c and hda_eld.c
4517 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004518void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4519{
4520 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4521 int i, j;
4522
4523 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4524 if (pcm & (AC_SUPPCM_BITS_8 << i))
4525 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4526
4527 buf[j] = '\0'; /* necessary when j == 0 */
4528}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004529EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004530
4531MODULE_DESCRIPTION("HDA codec core");
4532MODULE_LICENSE("GPL");