blob: b49feff0a31982e7c22071c08e8d088e91a97727 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwaid5191e52009-11-16 14:58:17 +010056/**
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
59 *
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
62 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040063const char *snd_hda_get_jack_location(u32 cfg)
64{
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
67 };
68 static unsigned char specials_idx[] = {
69 0x07, 0x08,
70 0x17, 0x18, 0x19,
71 0x37, 0x38
72 };
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
77 };
78 int i;
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
80 if ((cfg & 0x0f) < 7)
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
84 return specials[i];
85 }
86 return "UNKNOWN";
87}
Takashi Iwai2698ea92013-12-18 07:45:52 +010088EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040089
Takashi Iwaid5191e52009-11-16 14:58:17 +010090/**
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
93 *
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
96 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040097const char *snd_hda_get_jack_connectivity(u32 cfg)
98{
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
100
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
102}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100103EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104
Takashi Iwaid5191e52009-11-16 14:58:17 +0100105/**
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
111 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400112const char *snd_hda_get_jack_type(u32 cfg)
113{
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200118 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119 };
120
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
123}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100124EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400125
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100126/*
Takashi Iwai05852442015-03-03 15:40:08 +0100127 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200128 */
Takashi Iwai05852442015-03-03 15:40:08 +0100129static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200131{
Takashi Iwai05852442015-03-03 15:40:08 +0100132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200133 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200134 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200135
Wu Fengguang6430aee2009-07-17 16:49:19 +0800136 if (cmd == ~0)
137 return -1;
138
Takashi Iwai8dd78332009-06-02 01:16:07 +0200139 again:
Takashi Iwai664c7152015-04-08 11:43:14 +0200140 snd_hda_power_up_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100141 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
145 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200146 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100147 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwai664c7152015-04-08 11:43:14 +0200148 snd_hda_power_down_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100149 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200150 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100151 codec_dbg(codec,
152 "resetting BUS due to fatal communication error\n");
Takashi Iwai8dd78332009-06-02 01:16:07 +0200153 bus->ops.bus_reset(bus);
154 }
155 goto again;
156 }
157 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100158 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200159 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200160 return err;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200167 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @verb: the verb to send
169 * @parm: the parameter for the verb
170 *
171 * Send a single command and read the corresponding response.
172 *
173 * Returns the obtained response value, or -1 for an error.
174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200175unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200176 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int verb, unsigned int parm)
178{
Takashi Iwai7639a062015-03-03 10:07:24 +0100179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200180 unsigned int res;
Takashi Iwai05852442015-03-03 15:40:08 +0100181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700182 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return res;
184}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100185EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/**
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200191 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @verb: the verb to send
193 * @parm: the parameter for the verb
194 *
195 * Send a single command without waiting for response.
196 *
197 * Returns 0 if successful, or a negative error code.
198 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200199int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Takashi Iwai7639a062015-03-03 10:07:24 +0100202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwai05852442015-03-03 15:40:08 +0100203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100205EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
211 *
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
214 */
215void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
216{
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
219}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100220EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100222/* connection list element */
223struct hda_conn_list {
224 struct list_head list;
225 int len;
226 hda_nid_t nid;
227 hda_nid_t conns[0];
228};
229
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200230/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100231static struct hda_conn_list *
232lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200233{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
236 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200237 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200238 }
239 return NULL;
240}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200241
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100242static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
244{
245 struct hda_conn_list *p;
246
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
248 if (!p)
249 return -ENOMEM;
250 p->len = len;
251 p->nid = nid;
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
254 return 0;
255}
256
257static void remove_conn_list(struct hda_codec *codec)
258{
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
262 list_del(&p->list);
263 kfree(p);
264 }
265}
266
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200267/* read the connection and add to the cache */
268static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200269{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100270 hda_nid_t list[32];
271 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200272 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200273
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
278 if (!result)
279 return -ENOMEM;
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
281 }
282 if (len >= 0)
283 len = snd_hda_override_conn_list(codec, nid, len, result);
284 if (result != list)
285 kfree(result);
286 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200287}
Takashi Iwaidce20792011-06-24 14:10:28 +0200288
289/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
292 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 * @listp: the pointer to store NID list
294 *
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
297 *
298 * Returns the number of connections, or a negative error code.
299 *
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
303 */
304int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
306{
307 bool added = false;
308
309 for (;;) {
310 int err;
311 const struct hda_conn_list *p;
312
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
315 if (p) {
316 if (listp)
317 *listp = p->conns;
318 return p->len;
319 }
320 if (snd_BUG_ON(added))
321 return -EINVAL;
322
323 err = read_and_add_raw_conns(codec, nid);
324 if (err < 0)
325 return err;
326 added = true;
327 }
328}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100329EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100330
331/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @codec: the HDA codec
334 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200335 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @max_conns: max. number of connections to store
337 *
338 * Parses the connection list of the given widget and stores the list
339 * of NIDs.
340 *
341 * Returns the number of connections, or a negative error code.
342 */
343int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200344 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 return -EINVAL;
354 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200356 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200359}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100360EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
365 * @nid: NID to parse
366 * @len: number of connection list entries
367 * @list: the list of connection entries
368 *
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
371 *
372 * Returns zero or a negative error code.
373 */
374int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100377 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 p = lookup_conn_list(codec, nid);
380 if (p) {
381 list_del(&p->list);
382 kfree(p);
383 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200384
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100385 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100387EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200388
389/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
395 *
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
399 */
400int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
402{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200404 int i, nums;
405
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100406 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200407 for (i = 0; i < nums; i++)
408 if (conn[i] == nid)
409 return i;
410 if (!recursive)
411 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100412 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200414 return -1;
415 }
416 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
420 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
422 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200423 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200424 return -1;
425}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100426EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Mengdong Linf1aa0682013-08-26 21:35:21 -0400428
429/* return DEVLIST_LEN parameter of the given widget */
430static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
437 return 0;
438
439 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
440 if (parm == -1 && codec->bus->rirb_error)
441 parm = 0;
442 return parm & AC_DEV_LIST_LEN_MASK;
443}
444
445/**
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
451 *
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
454 */
455int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
457{
458 unsigned int parm;
459 int i, dev_len, devices;
460
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
463 return 0;
464
465 dev_len = parm + 1;
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
467
468 devices = 0;
469 while (devices < dev_len) {
470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_DEVICE_LIST, devices);
472 if (parm == -1 && codec->bus->rirb_error)
473 break;
474
475 for (i = 0; i < 8; i++) {
476 dev_list[devices] = (u8)parm;
477 parm >>= 4;
478 devices++;
479 if (devices >= dev_len)
480 break;
481 }
482 }
483 return devices;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * destructor
488 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100489static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200491 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (bus->ops.private_free)
494 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100495 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100499static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Takashi Iwai2565c892014-02-19 11:41:09 +0100501 snd_hda_bus_free(device->device_data);
502 return 0;
503}
504
505static int snd_hda_bus_dev_disconnect(struct snd_device *device)
506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100508 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100512/* hdac_bus_ops translations */
513static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
514{
515 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
516 return bus->ops.command(bus, cmd);
517}
518
519static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
520 unsigned int *res)
521{
522 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
523 *res = bus->ops.get_response(bus, addr);
524 return bus->rirb_error ? -EIO : 0;
525}
526
527static const struct hdac_bus_ops bus_ops = {
528 .command = _hda_bus_command,
529 .get_response = _hda_bus_get_response,
530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/**
533 * snd_hda_bus_new - create a HDA bus
534 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * @busp: the pointer to store the created bus instance
536 *
537 * Returns 0 if successful, or a negative error code.
538 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100539int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100540 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct hda_bus *bus;
543 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100544 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100545 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 .dev_free = snd_hda_bus_dev_free,
547 };
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (busp)
550 *busp = NULL;
551
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200552 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100553 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100556 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
557 if (err < 0) {
558 kfree(bus);
559 return err;
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200563 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
566 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 snd_hda_bus_free(bus);
568 return err;
569 }
570 if (busp)
571 *busp = bus;
572 return 0;
573}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100574EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 * read widget caps for each widget and store in cache
578 */
579static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
580{
581 int i;
582 hda_nid_t nid;
583
Takashi Iwai7639a062015-03-03 10:07:24 +0100584 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200585 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100586 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100587 nid = codec->core.start_nid;
588 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100589 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
590 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100591 return 0;
592}
593
Takashi Iwai3be14142009-02-20 14:11:16 +0100594/* read all pin default configurations and save codec->init_pins */
595static int read_pin_defaults(struct hda_codec *codec)
596{
Takashi Iwai7639a062015-03-03 10:07:24 +0100597 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100598
Takashi Iwai7639a062015-03-03 10:07:24 +0100599 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100600 struct hda_pincfg *pin;
601 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200602 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100603 if (wid_type != AC_WID_PIN)
604 continue;
605 pin = snd_array_new(&codec->init_pins);
606 if (!pin)
607 return -ENOMEM;
608 pin->nid = nid;
609 pin->cfg = snd_hda_codec_read(codec, nid, 0,
610 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200611 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
612 AC_VERB_GET_PIN_WIDGET_CONTROL,
613 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100614 }
615 return 0;
616}
617
618/* look up the given pin config list and return the item matching with NID */
619static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
620 struct snd_array *array,
621 hda_nid_t nid)
622{
623 int i;
624 for (i = 0; i < array->used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(array, i);
626 if (pin->nid == nid)
627 return pin;
628 }
629 return NULL;
630}
631
Takashi Iwai3be14142009-02-20 14:11:16 +0100632/* set the current pin config value for the given NID.
633 * the value is cached, and read via snd_hda_codec_get_pincfg()
634 */
635int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
636 hda_nid_t nid, unsigned int cfg)
637{
638 struct hda_pincfg *pin;
639
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200640 /* the check below may be invalid when pins are added by a fixup
641 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
642 * for now
643 */
644 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100645 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
646 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200647 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100648
Takashi Iwai3be14142009-02-20 14:11:16 +0100649 pin = look_up_pincfg(codec, list, nid);
650 if (!pin) {
651 pin = snd_array_new(list);
652 if (!pin)
653 return -ENOMEM;
654 pin->nid = nid;
655 }
656 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100657 return 0;
658}
659
Takashi Iwaid5191e52009-11-16 14:58:17 +0100660/**
661 * snd_hda_codec_set_pincfg - Override a pin default configuration
662 * @codec: the HDA codec
663 * @nid: NID to set the pin config
664 * @cfg: the pin default config value
665 *
666 * Override a pin default configuration value in the cache.
667 * This value can be read by snd_hda_codec_get_pincfg() in a higher
668 * priority than the real hardware value.
669 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100670int snd_hda_codec_set_pincfg(struct hda_codec *codec,
671 hda_nid_t nid, unsigned int cfg)
672{
Takashi Iwai346ff702009-02-23 09:42:57 +0100673 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100674}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100675EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100676
Takashi Iwaid5191e52009-11-16 14:58:17 +0100677/**
678 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
679 * @codec: the HDA codec
680 * @nid: NID to get the pin config
681 *
682 * Get the current pin config value of the given pin NID.
683 * If the pincfg value is cached or overridden via sysfs or driver,
684 * returns the cached value.
685 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100686unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
687{
688 struct hda_pincfg *pin;
689
Takashi Iwai648a8d22014-02-25 10:38:13 +0100690#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100691 {
692 unsigned int cfg = 0;
693 mutex_lock(&codec->user_mutex);
694 pin = look_up_pincfg(codec, &codec->user_pins, nid);
695 if (pin)
696 cfg = pin->cfg;
697 mutex_unlock(&codec->user_mutex);
698 if (cfg)
699 return cfg;
700 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100701#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100702 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
703 if (pin)
704 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100705 pin = look_up_pincfg(codec, &codec->init_pins, nid);
706 if (pin)
707 return pin->cfg;
708 return 0;
709}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100710EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100711
Takashi Iwai95a962c2014-10-29 16:03:58 +0100712/**
713 * snd_hda_codec_set_pin_target - remember the current pinctl target value
714 * @codec: the HDA codec
715 * @nid: pin NID
716 * @val: assigned pinctl value
717 *
718 * This function stores the given value to a pinctl target value in the
719 * pincfg table. This isn't always as same as the actually written value
720 * but can be referred at any time via snd_hda_codec_get_pin_target().
721 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100722int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
723 unsigned int val)
724{
725 struct hda_pincfg *pin;
726
727 pin = look_up_pincfg(codec, &codec->init_pins, nid);
728 if (!pin)
729 return -EINVAL;
730 pin->target = val;
731 return 0;
732}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100733EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100734
Takashi Iwai95a962c2014-10-29 16:03:58 +0100735/**
736 * snd_hda_codec_get_pin_target - return the current pinctl target value
737 * @codec: the HDA codec
738 * @nid: pin NID
739 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100740int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
741{
742 struct hda_pincfg *pin;
743
744 pin = look_up_pincfg(codec, &codec->init_pins, nid);
745 if (!pin)
746 return 0;
747 return pin->target;
748}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100749EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100750
Takashi Iwai92ee6162009-12-27 11:18:59 +0100751/**
752 * snd_hda_shutup_pins - Shut up all pins
753 * @codec: the HDA codec
754 *
755 * Clear all pin controls to shup up before suspend for avoiding click noise.
756 * The controls aren't cached so that they can be resumed properly.
757 */
758void snd_hda_shutup_pins(struct hda_codec *codec)
759{
760 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200761 /* don't shut up pins when unloading the driver; otherwise it breaks
762 * the default pin setup at the next load of the driver
763 */
764 if (codec->bus->shutdown)
765 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100766 for (i = 0; i < codec->init_pins.used; i++) {
767 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
768 /* use read here for syncing after issuing each verb */
769 snd_hda_codec_read(codec, pin->nid, 0,
770 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
771 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200772 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100773}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100774EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100775
Takashi Iwai2a439522011-07-26 09:52:50 +0200776#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200777/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
778static void restore_shutup_pins(struct hda_codec *codec)
779{
780 int i;
781 if (!codec->pins_shutup)
782 return;
783 if (codec->bus->shutdown)
784 return;
785 for (i = 0; i < codec->init_pins.used; i++) {
786 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
787 snd_hda_codec_write(codec, pin->nid, 0,
788 AC_VERB_SET_PIN_WIDGET_CONTROL,
789 pin->ctrl);
790 }
791 codec->pins_shutup = 0;
792}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700793#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200794
David Henningsson26a6cb62012-10-09 15:04:21 +0200795static void hda_jackpoll_work(struct work_struct *work)
796{
797 struct hda_codec *codec =
798 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200799
800 snd_hda_jack_set_dirty_all(codec);
801 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400802
803 if (!codec->jackpoll_interval)
804 return;
805
Takashi Iwai2f35c632015-02-27 22:43:26 +0100806 schedule_delayed_work(&codec->jackpoll_work,
807 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200808}
809
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100810/* release all pincfg lists */
811static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100812{
Takashi Iwai346ff702009-02-23 09:42:57 +0100813 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100814#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100815 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100816#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100817 snd_array_free(&codec->init_pins);
818}
819
Takashi Iwai54d17402005-11-21 16:33:22 +0100820/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200821 * audio-converter setup caches
822 */
823struct hda_cvt_setup {
824 hda_nid_t nid;
825 u8 stream_tag;
826 u8 channel_id;
827 u16 format_id;
828 unsigned char active; /* cvt is currently used */
829 unsigned char dirty; /* setups should be cleared */
830};
831
832/* get or create a cache entry for the given audio converter NID */
833static struct hda_cvt_setup *
834get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
835{
836 struct hda_cvt_setup *p;
837 int i;
838
839 for (i = 0; i < codec->cvt_setups.used; i++) {
840 p = snd_array_elem(&codec->cvt_setups, i);
841 if (p->nid == nid)
842 return p;
843 }
844 p = snd_array_new(&codec->cvt_setups);
845 if (p)
846 p->nid = nid;
847 return p;
848}
849
850/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100851 * PCM device
852 */
853static void release_pcm(struct kref *kref)
854{
855 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
856
857 if (pcm->pcm)
858 snd_device_free(pcm->codec->card, pcm->pcm);
859 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
860 kfree(pcm->name);
861 kfree(pcm);
862}
863
864void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
865{
866 kref_put(&pcm->kref, release_pcm);
867}
868EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
869
870struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
871 const char *fmt, ...)
872{
873 struct hda_pcm *pcm;
874 va_list args;
875
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100876 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
877 if (!pcm)
878 return NULL;
879
880 pcm->codec = codec;
881 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200882 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100883 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200884 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100885 if (!pcm->name) {
886 kfree(pcm);
887 return NULL;
888 }
889
890 list_add_tail(&pcm->list, &codec->pcm_list_head);
891 return pcm;
892}
893EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
894
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100895/*
896 * codec destructor
897 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100898static void codec_release_pcms(struct hda_codec *codec)
899{
900 struct hda_pcm *pcm, *n;
901
902 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
903 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100904 if (pcm->pcm)
905 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100906 snd_hda_codec_pcm_put(pcm);
907 }
908}
909
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100910void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
911{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100912 if (codec->registered) {
913 /* pm_runtime_put() is called in snd_hdac_device_exit() */
914 pm_runtime_get_noresume(hda_codec_dev(codec));
915 pm_runtime_disable(hda_codec_dev(codec));
916 codec->registered = 0;
917 }
918
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100919 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100920 if (!codec->in_freeing)
921 snd_hda_ctls_clear(codec);
922 codec_release_pcms(codec);
923 snd_hda_detach_beep_device(codec);
924 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
925 snd_hda_jack_tbl_clear(codec);
926 codec->proc_widget_hook = NULL;
927 codec->spec = NULL;
928
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100929 /* free only driver_pins so that init_pins + user_pins are restored */
930 snd_array_free(&codec->driver_pins);
931 snd_array_free(&codec->cvt_setups);
932 snd_array_free(&codec->spdif_out);
933 snd_array_free(&codec->verbs);
934 codec->preset = NULL;
935 codec->slave_dig_outs = NULL;
936 codec->spdif_status_reset = 0;
937 snd_array_free(&codec->mixers);
938 snd_array_free(&codec->nids);
939 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100940 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100941}
942
Takashi Iwaid8193872012-08-31 07:54:38 -0700943static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100944 unsigned int power_state);
945
Takashi Iwaic4c25332015-03-03 17:22:12 +0100946/* also called from hda_bind.c */
947void snd_hda_codec_register(struct hda_codec *codec)
948{
949 if (codec->registered)
950 return;
951 if (device_is_registered(hda_codec_dev(codec))) {
952 snd_hda_register_beep_device(codec);
953 pm_runtime_enable(hda_codec_dev(codec));
954 /* it was powered up in snd_hda_codec_new(), now all done */
955 snd_hda_power_down(codec);
956 codec->registered = 1;
957 }
958}
959
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100960static int snd_hda_codec_dev_register(struct snd_device *device)
961{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100962 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100963 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100964}
965
966static int snd_hda_codec_dev_disconnect(struct snd_device *device)
967{
968 struct hda_codec *codec = device->device_data;
969
Takashi Iwaid604b392014-02-28 13:42:09 +0100970 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100971 return 0;
972}
973
Takashi Iwai2565c892014-02-19 11:41:09 +0100974static int snd_hda_codec_dev_free(struct snd_device *device)
975{
Takashi Iwaid56db742015-02-27 23:25:35 +0100976 struct hda_codec *codec = device->device_data;
977
978 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100979 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100980 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100981 return 0;
982}
983
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100984static void snd_hda_codec_dev_release(struct device *dev)
985{
Takashi Iwaid56db742015-02-27 23:25:35 +0100986 struct hda_codec *codec = dev_to_hda_codec(dev);
987
988 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100989 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100990 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100991 kfree(codec->modelname);
992 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100993 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/**
997 * snd_hda_codec_new - create a HDA codec
998 * @bus: the bus to assign
999 * @codec_addr: the codec address
1000 * @codecp: the pointer to store the generated codec
1001 *
1002 * Returns 0 if successful, or a negative error code.
1003 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001004int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1005 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001008 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001009 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001011 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001012 .dev_register = snd_hda_codec_dev_register,
1013 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001014 .dev_free = snd_hda_codec_dev_free,
1015 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Takashi Iwaida3cec32008-08-08 17:12:14 +02001017 if (snd_BUG_ON(!bus))
1018 return -EINVAL;
1019 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1020 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001022 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001023 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Takashi Iwai7639a062015-03-03 10:07:24 +01001026 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1027 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1028 codec_addr);
1029 if (err < 0) {
1030 kfree(codec);
1031 return err;
1032 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001033
Takashi Iwai7639a062015-03-03 10:07:24 +01001034 codec->core.dev.release = snd_hda_codec_dev_release;
1035 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +01001036 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001039 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001041 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001042 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001043 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1044 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001045 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001046 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001047 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001048 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001049 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001050 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001051 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001052 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001053
David Henningsson26a6cb62012-10-09 15:04:21 +02001054 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001055 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001056 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Takashi Iwai83012a72012-08-24 18:38:08 +02001058#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001059 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001060#endif
1061
Takashi Iwai648a8d22014-02-25 10:38:13 +01001062 snd_hda_sysfs_init(codec);
1063
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001064 if (codec->bus->modelname) {
1065 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1066 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001067 err = -ENODEV;
1068 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001069 }
1070 }
1071
Takashi Iwai7639a062015-03-03 10:07:24 +01001072 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001073 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001074 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 err = read_pin_defaults(codec);
1077 if (err < 0)
1078 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001079
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001080 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001081 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001082
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001083 snd_hda_codec_proc_new(codec);
1084
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001085 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001086
Takashi Iwai7639a062015-03-03 10:07:24 +01001087 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1088 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001089 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001090
Takashi Iwai6efdd852015-02-27 16:09:22 +01001091 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001092 if (err < 0)
1093 goto error;
1094
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001095 if (codecp)
1096 *codecp = codec;
1097 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001098
1099 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001100 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001101 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001102}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001103EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001104
Takashi Iwai95a962c2014-10-29 16:03:58 +01001105/**
1106 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1107 * @codec: the HDA codec
1108 *
1109 * Forcibly refresh the all widget caps and the init pin configurations of
1110 * the given codec.
1111 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001112int snd_hda_codec_update_widgets(struct hda_codec *codec)
1113{
1114 hda_nid_t fg;
1115 int err;
1116
Takashi Iwai7639a062015-03-03 10:07:24 +01001117 err = snd_hdac_refresh_widgets(&codec->core);
1118 if (err < 0)
1119 return err;
1120
Mengdong Lina15d05d2013-02-08 17:09:31 -05001121 /* Assume the function group node does not change,
1122 * only the widget nodes may change.
1123 */
1124 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001125 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001126 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001127 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001128 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001129
1130 snd_array_free(&codec->init_pins);
1131 err = read_pin_defaults(codec);
1132
1133 return err;
1134}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001135EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001136
Takashi Iwaied360812012-08-08 17:12:52 +02001137/* update the stream-id if changed */
1138static void update_pcm_stream_id(struct hda_codec *codec,
1139 struct hda_cvt_setup *p, hda_nid_t nid,
1140 u32 stream_tag, int channel_id)
1141{
1142 unsigned int oldval, newval;
1143
1144 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1145 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1146 newval = (stream_tag << 4) | channel_id;
1147 if (oldval != newval)
1148 snd_hda_codec_write(codec, nid, 0,
1149 AC_VERB_SET_CHANNEL_STREAMID,
1150 newval);
1151 p->stream_tag = stream_tag;
1152 p->channel_id = channel_id;
1153 }
1154}
1155
1156/* update the format-id if changed */
1157static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1158 hda_nid_t nid, int format)
1159{
1160 unsigned int oldval;
1161
1162 if (p->format_id != format) {
1163 oldval = snd_hda_codec_read(codec, nid, 0,
1164 AC_VERB_GET_STREAM_FORMAT, 0);
1165 if (oldval != format) {
1166 msleep(1);
1167 snd_hda_codec_write(codec, nid, 0,
1168 AC_VERB_SET_STREAM_FORMAT,
1169 format);
1170 }
1171 p->format_id = format;
1172 }
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/**
1176 * snd_hda_codec_setup_stream - set up the codec for streaming
1177 * @codec: the CODEC to set up
1178 * @nid: the NID to set up
1179 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1180 * @channel_id: channel id to pass, zero based.
1181 * @format: stream format.
1182 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001183void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1184 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int channel_id, int format)
1186{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001187 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001188 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001189 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001190 int i;
1191
Takashi Iwai0ba21762007-04-16 11:29:14 +02001192 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001193 return;
1194
Takashi Iwai4e76a882014-02-25 12:21:03 +01001195 codec_dbg(codec,
1196 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1197 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001198 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001199 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001200 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001201
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001202 if (codec->patch_ops.stream_pm)
1203 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001204 if (codec->pcm_format_first)
1205 update_pcm_format(codec, p, nid, format);
1206 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1207 if (!codec->pcm_format_first)
1208 update_pcm_format(codec, p, nid, format);
1209
Takashi Iwaieb541332010-08-06 13:48:11 +02001210 p->active = 1;
1211 p->dirty = 0;
1212
1213 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001214 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001215 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001216 for (i = 0; i < c->cvt_setups.used; i++) {
1217 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001218 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001219 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001220 p->dirty = 1;
1221 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001224EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Takashi Iwaif0cea792010-08-13 11:56:53 +02001226static void really_cleanup_stream(struct hda_codec *codec,
1227 struct hda_cvt_setup *q);
1228
Takashi Iwaid5191e52009-11-16 14:58:17 +01001229/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001230 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001231 * @codec: the CODEC to clean up
1232 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001233 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001234 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001235void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1236 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001237{
Takashi Iwaieb541332010-08-06 13:48:11 +02001238 struct hda_cvt_setup *p;
1239
Takashi Iwai888afa12008-03-18 09:57:50 +01001240 if (!nid)
1241 return;
1242
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001243 if (codec->no_sticky_stream)
1244 do_now = 1;
1245
Takashi Iwai4e76a882014-02-25 12:21:03 +01001246 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001247 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001248 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001249 /* here we just clear the active flag when do_now isn't set;
1250 * actual clean-ups will be done later in
1251 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1252 */
1253 if (do_now)
1254 really_cleanup_stream(codec, p);
1255 else
1256 p->active = 0;
1257 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001258}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001259EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001260
Takashi Iwaieb541332010-08-06 13:48:11 +02001261static void really_cleanup_stream(struct hda_codec *codec,
1262 struct hda_cvt_setup *q)
1263{
1264 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001265 if (q->stream_tag || q->channel_id)
1266 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1267 if (q->format_id)
1268 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1269);
Takashi Iwaieb541332010-08-06 13:48:11 +02001270 memset(q, 0, sizeof(*q));
1271 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001272 if (codec->patch_ops.stream_pm)
1273 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001274}
1275
1276/* clean up the all conflicting obsolete streams */
1277static void purify_inactive_streams(struct hda_codec *codec)
1278{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001279 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001280 int i;
1281
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001282 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001283 for (i = 0; i < c->cvt_setups.used; i++) {
1284 struct hda_cvt_setup *p;
1285 p = snd_array_elem(&c->cvt_setups, i);
1286 if (p->dirty)
1287 really_cleanup_stream(c, p);
1288 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001289 }
1290}
1291
Takashi Iwai2a439522011-07-26 09:52:50 +02001292#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001293/* clean up all streams; called from suspend */
1294static void hda_cleanup_all_streams(struct hda_codec *codec)
1295{
1296 int i;
1297
1298 for (i = 0; i < codec->cvt_setups.used; i++) {
1299 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1300 if (p->stream_tag)
1301 really_cleanup_stream(codec, p);
1302 }
1303}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001304#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306/*
1307 * amp access functions
1308 */
1309
Takashi Iwaid5191e52009-11-16 14:58:17 +01001310/**
1311 * query_amp_caps - query AMP capabilities
1312 * @codec: the HD-auio codec
1313 * @nid: the NID to query
1314 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1315 *
1316 * Query AMP capabilities for the given widget and direction.
1317 * Returns the obtained capability bits.
1318 *
1319 * When cap bits have been already read, this doesn't read again but
1320 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001322u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001324 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1325 nid = codec->core.afg;
1326 return snd_hda_param_read(codec, nid,
1327 direction == HDA_OUTPUT ?
1328 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001330EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Takashi Iwaid5191e52009-11-16 14:58:17 +01001332/**
David Henningsson861a04e2014-09-23 10:38:17 +02001333 * snd_hda_check_amp_caps - query AMP capabilities
1334 * @codec: the HD-audio codec
1335 * @nid: the NID to query
1336 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001337 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001338 *
1339 * Check whether the widget has the given amp capability for the direction.
1340 */
1341bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1342 int dir, unsigned int bits)
1343{
1344 if (!nid)
1345 return false;
1346 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1347 if (query_amp_caps(codec, nid, dir) & bits)
1348 return true;
1349 return false;
1350}
1351EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1352
1353/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001354 * snd_hda_override_amp_caps - Override the AMP capabilities
1355 * @codec: the CODEC to clean up
1356 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001357 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001358 * @caps: the capability bits to set
1359 *
1360 * Override the cached AMP caps bits value by the given one.
1361 * This function is useful if the driver needs to adjust the AMP ranges,
1362 * e.g. limit to 0dB, etc.
1363 *
1364 * Returns zero if successful or a negative error code.
1365 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001366int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1367 unsigned int caps)
1368{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001369 unsigned int parm;
1370
1371 snd_hda_override_wcaps(codec, nid,
1372 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1373 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1374 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001375}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001376EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001377
Takashi Iwaid5191e52009-11-16 14:58:17 +01001378/**
1379 * snd_hda_codec_amp_stereo - update the AMP stereo values
1380 * @codec: HD-audio codec
1381 * @nid: NID to read the AMP value
1382 * @direction: #HDA_INPUT or #HDA_OUTPUT
1383 * @idx: the index value (only for input direction)
1384 * @mask: bit mask to set
1385 * @val: the bits value to set
1386 *
1387 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1388 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001389 */
1390int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1391 int direction, int idx, int mask, int val)
1392{
1393 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001394
1395 if (snd_BUG_ON(mask & ~0xff))
1396 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001397 for (ch = 0; ch < 2; ch++)
1398 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1399 idx, mask, val);
1400 return ret;
1401}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001402EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001403
Takashi Iwai95a962c2014-10-29 16:03:58 +01001404/**
1405 * snd_hda_codec_amp_init - initialize the AMP value
1406 * @codec: the HDA codec
1407 * @nid: NID to read the AMP value
1408 * @ch: channel (left=0 or right=1)
1409 * @dir: #HDA_INPUT or #HDA_OUTPUT
1410 * @idx: the index value (only for input direction)
1411 * @mask: bit mask to set
1412 * @val: the bits value to set
1413 *
1414 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001415 * the first access. If the amp was already initialized / updated beforehand,
1416 * this does nothing.
1417 */
1418int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1419 int dir, int idx, int mask, int val)
1420{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001421 int orig;
1422
1423 if (!codec->core.regmap)
1424 return -EINVAL;
1425 regcache_cache_only(codec->core.regmap, true);
1426 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1427 regcache_cache_only(codec->core.regmap, false);
1428 if (orig >= 0)
1429 return 0;
1430 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001431}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001432EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001433
Takashi Iwai95a962c2014-10-29 16:03:58 +01001434/**
1435 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1436 * @codec: the HDA codec
1437 * @nid: NID to read the AMP value
1438 * @dir: #HDA_INPUT or #HDA_OUTPUT
1439 * @idx: the index value (only for input direction)
1440 * @mask: bit mask to set
1441 * @val: the bits value to set
1442 *
1443 * Call snd_hda_codec_amp_init() for both stereo channels.
1444 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001445int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1446 int dir, int idx, int mask, int val)
1447{
1448 int ch, ret = 0;
1449
1450 if (snd_BUG_ON(mask & ~0xff))
1451 mask &= 0xff;
1452 for (ch = 0; ch < 2; ch++)
1453 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1454 idx, mask, val);
1455 return ret;
1456}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001457EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001458
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001459static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1460 unsigned int ofs)
1461{
1462 u32 caps = query_amp_caps(codec, nid, dir);
1463 /* get num steps */
1464 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1465 if (ofs < caps)
1466 caps -= ofs;
1467 return caps;
1468}
1469
Takashi Iwaid5191e52009-11-16 14:58:17 +01001470/**
1471 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001472 * @kcontrol: referred ctl element
1473 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001474 *
1475 * The control element is supposed to have the private_value field
1476 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1477 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001478int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1482 u16 nid = get_amp_nid(kcontrol);
1483 u8 chs = get_amp_channels(kcontrol);
1484 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001485 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1488 uinfo->count = chs == 3 ? 2 : 1;
1489 uinfo->value.integer.min = 0;
1490 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1491 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001492 codec_warn(codec,
1493 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1494 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EINVAL;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001499EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001501
1502static inline unsigned int
1503read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1504 int ch, int dir, int idx, unsigned int ofs)
1505{
1506 unsigned int val;
1507 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1508 val &= HDA_AMP_VOLMASK;
1509 if (val >= ofs)
1510 val -= ofs;
1511 else
1512 val = 0;
1513 return val;
1514}
1515
1516static inline int
1517update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1518 int ch, int dir, int idx, unsigned int ofs,
1519 unsigned int val)
1520{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001521 unsigned int maxval;
1522
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001523 if (val > 0)
1524 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001525 /* ofs = 0: raw max value */
1526 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001527 if (val > maxval)
1528 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001529 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1530 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001531}
1532
Takashi Iwaid5191e52009-11-16 14:58:17 +01001533/**
1534 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001535 * @kcontrol: ctl element
1536 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001537 *
1538 * The control element is supposed to have the private_value field
1539 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1540 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001541int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1545 hda_nid_t nid = get_amp_nid(kcontrol);
1546 int chs = get_amp_channels(kcontrol);
1547 int dir = get_amp_direction(kcontrol);
1548 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001549 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 long *valp = ucontrol->value.integer.value;
1551
1552 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001553 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001555 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001558EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Takashi Iwaid5191e52009-11-16 14:58:17 +01001560/**
1561 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001562 * @kcontrol: ctl element
1563 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001564 *
1565 * The control element is supposed to have the private_value field
1566 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1567 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001568int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1572 hda_nid_t nid = get_amp_nid(kcontrol);
1573 int chs = get_amp_channels(kcontrol);
1574 int dir = get_amp_direction(kcontrol);
1575 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001576 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 long *valp = ucontrol->value.integer.value;
1578 int change = 0;
1579
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001580 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001581 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001582 valp++;
1583 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001584 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001585 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return change;
1587}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001588EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Takashi Iwaid5191e52009-11-16 14:58:17 +01001590/**
1591 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001592 * @kcontrol: ctl element
1593 * @op_flag: operation flag
1594 * @size: byte size of input TLV
1595 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001596 *
1597 * The control element is supposed to have the private_value field
1598 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1599 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001600int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1601 unsigned int size, unsigned int __user *_tlv)
1602{
1603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1604 hda_nid_t nid = get_amp_nid(kcontrol);
1605 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001606 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001607 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001608 u32 caps, val1, val2;
1609
1610 if (size < 4 * sizeof(unsigned int))
1611 return -ENOMEM;
1612 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001613 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1614 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001615 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001616 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001617 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001618 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001619 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001620 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1621 return -EFAULT;
1622 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1623 return -EFAULT;
1624 if (put_user(val1, _tlv + 2))
1625 return -EFAULT;
1626 if (put_user(val2, _tlv + 3))
1627 return -EFAULT;
1628 return 0;
1629}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001630EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001631
Takashi Iwaid5191e52009-11-16 14:58:17 +01001632/**
1633 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1634 * @codec: HD-audio codec
1635 * @nid: NID of a reference widget
1636 * @dir: #HDA_INPUT or #HDA_OUTPUT
1637 * @tlv: TLV data to be stored, at least 4 elements
1638 *
1639 * Set (static) TLV data for a virtual master volume using the AMP caps
1640 * obtained from the reference NID.
1641 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001642 */
1643void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1644 unsigned int *tlv)
1645{
1646 u32 caps;
1647 int nums, step;
1648
1649 caps = query_amp_caps(codec, nid, dir);
1650 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1651 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1652 step = (step + 1) * 25;
1653 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1654 tlv[1] = 2 * sizeof(unsigned int);
1655 tlv[2] = -nums * step;
1656 tlv[3] = step;
1657}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001658EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001659
1660/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001661static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001662find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001663{
1664 struct snd_ctl_elem_id id;
1665 memset(&id, 0, sizeof(id));
1666 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001667 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001668 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001669 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1670 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001671 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001672 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001673}
1674
Takashi Iwaid5191e52009-11-16 14:58:17 +01001675/**
1676 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1677 * @codec: HD-audio codec
1678 * @name: ctl id name string
1679 *
1680 * Get the control element with the given id string and IFACE_MIXER.
1681 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001682struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1683 const char *name)
1684{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001685 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001686}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001687EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001688
Takashi Iwaidcda5802012-10-12 17:24:51 +02001689static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001690 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001691{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001692 int i, idx;
1693 /* 16 ctlrs should be large enough */
1694 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1695 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001696 return idx;
1697 }
1698 return -EBUSY;
1699}
1700
Takashi Iwaid5191e52009-11-16 14:58:17 +01001701/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001702 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001703 * @codec: HD-audio codec
1704 * @nid: corresponding NID (optional)
1705 * @kctl: the control element to assign
1706 *
1707 * Add the given control element to an array inside the codec instance.
1708 * All control elements belonging to a codec are supposed to be added
1709 * by this function so that a proper clean-up works at the free or
1710 * reconfiguration time.
1711 *
1712 * If non-zero @nid is passed, the NID is assigned to the control element.
1713 * The assignment is shown in the codec proc file.
1714 *
1715 * snd_hda_ctl_add() checks the control subdev id field whether
1716 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001717 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1718 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001719 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001720int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1721 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001722{
1723 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001724 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001725 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001726
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001727 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001728 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001729 if (nid == 0)
1730 nid = get_amp_nid_(kctl->private_value);
1731 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001732 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1733 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001734 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001735 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001736 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001737 if (err < 0)
1738 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001739 item = snd_array_new(&codec->mixers);
1740 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001741 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001742 item->kctl = kctl;
1743 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001744 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001745 return 0;
1746}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001747EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001748
Takashi Iwaid5191e52009-11-16 14:58:17 +01001749/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001750 * snd_hda_add_nid - Assign a NID to a control element
1751 * @codec: HD-audio codec
1752 * @nid: corresponding NID (optional)
1753 * @kctl: the control element to assign
1754 * @index: index to kctl
1755 *
1756 * Add the given control element to an array inside the codec instance.
1757 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1758 * NID:KCTL mapping - for example "Capture Source" selector.
1759 */
1760int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1761 unsigned int index, hda_nid_t nid)
1762{
1763 struct hda_nid_item *item;
1764
1765 if (nid > 0) {
1766 item = snd_array_new(&codec->nids);
1767 if (!item)
1768 return -ENOMEM;
1769 item->kctl = kctl;
1770 item->index = index;
1771 item->nid = nid;
1772 return 0;
1773 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001774 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1775 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001776 return -EINVAL;
1777}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001778EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001779
1780/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001781 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1782 * @codec: HD-audio codec
1783 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001784void snd_hda_ctls_clear(struct hda_codec *codec)
1785{
1786 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001787 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001788 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001789 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001790 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001791 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001792}
1793
Takashi Iwai95a962c2014-10-29 16:03:58 +01001794/**
1795 * snd_hda_lock_devices - pseudo device locking
1796 * @bus: the BUS
1797 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001798 * toggle card->shutdown to allow/disallow the device access (as a hack)
1799 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001800int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001801{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001802 struct snd_card *card = bus->card;
1803 struct hda_codec *codec;
1804
Takashi Iwaia65d6292009-02-23 16:57:04 +01001805 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001806 if (card->shutdown)
1807 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001808 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001809 if (!list_empty(&card->ctl_files))
1810 goto err_clear;
1811
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001812 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001813 struct hda_pcm *cpcm;
1814 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001815 if (!cpcm->pcm)
1816 continue;
1817 if (cpcm->pcm->streams[0].substream_opened ||
1818 cpcm->pcm->streams[1].substream_opened)
1819 goto err_clear;
1820 }
1821 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001822 spin_unlock(&card->files_lock);
1823 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001824
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001825 err_clear:
1826 card->shutdown = 0;
1827 err_unlock:
1828 spin_unlock(&card->files_lock);
1829 return -EINVAL;
1830}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001831EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001832
Takashi Iwai95a962c2014-10-29 16:03:58 +01001833/**
1834 * snd_hda_unlock_devices - pseudo device unlocking
1835 * @bus: the BUS
1836 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001837void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001838{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001839 struct snd_card *card = bus->card;
1840
Takashi Iwaia65d6292009-02-23 16:57:04 +01001841 spin_lock(&card->files_lock);
1842 card->shutdown = 0;
1843 spin_unlock(&card->files_lock);
1844}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001845EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001846
Takashi Iwaid5191e52009-11-16 14:58:17 +01001847/**
1848 * snd_hda_codec_reset - Clear all objects assigned to the codec
1849 * @codec: HD-audio codec
1850 *
1851 * This frees the all PCM and control elements assigned to the codec, and
1852 * clears the caches and restores the pin default configurations.
1853 *
1854 * When a device is being used, it returns -EBSY. If successfully freed,
1855 * returns zero.
1856 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001857int snd_hda_codec_reset(struct hda_codec *codec)
1858{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001859 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001860
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001861 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001862 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001863
1864 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001865 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001866
Takashi Iwaia65d6292009-02-23 16:57:04 +01001867 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001868 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001869 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001870}
1871
Takashi Iwai6194b992014-06-06 18:12:16 +02001872typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001873
1874/* apply the function to all matching slave ctls in the mixer list */
1875static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001876 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001877{
1878 struct hda_nid_item *items;
1879 const char * const *s;
1880 int i, err;
1881
1882 items = codec->mixers.list;
1883 for (i = 0; i < codec->mixers.used; i++) {
1884 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001885 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001886 continue;
1887 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001888 char tmpname[sizeof(sctl->id.name)];
1889 const char *name = *s;
1890 if (suffix) {
1891 snprintf(tmpname, sizeof(tmpname), "%s %s",
1892 name, suffix);
1893 name = tmpname;
1894 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001895 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001896 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001897 if (err)
1898 return err;
1899 break;
1900 }
1901 }
1902 }
1903 return 0;
1904}
1905
Takashi Iwai6194b992014-06-06 18:12:16 +02001906static int check_slave_present(struct hda_codec *codec,
1907 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001908{
1909 return 1;
1910}
1911
Takashi Iwai18478e82012-03-09 17:51:10 +01001912/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001913static int get_kctl_0dB_offset(struct hda_codec *codec,
1914 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001915{
1916 int _tlv[4];
1917 const int *tlv = NULL;
1918 int val = -1;
1919
1920 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1921 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1922 mm_segment_t fs = get_fs();
1923 set_fs(get_ds());
1924 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1925 tlv = _tlv;
1926 set_fs(fs);
1927 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1928 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001929 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1930 int step = tlv[3];
1931 step &= ~TLV_DB_SCALE_MUTE;
1932 if (!step)
1933 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01001934 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001935 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001936- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001937 return -1;
1938 }
1939 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001940 val = -tlv[2] / step;
1941 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001942 return val;
1943}
1944
1945/* call kctl->put with the given value(s) */
1946static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1947{
1948 struct snd_ctl_elem_value *ucontrol;
1949 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1950 if (!ucontrol)
1951 return -ENOMEM;
1952 ucontrol->value.integer.value[0] = val;
1953 ucontrol->value.integer.value[1] = val;
1954 kctl->put(kctl, ucontrol);
1955 kfree(ucontrol);
1956 return 0;
1957}
1958
1959/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001960static int init_slave_0dB(struct hda_codec *codec,
1961 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001962{
Takashi Iwai6194b992014-06-06 18:12:16 +02001963 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001964 if (offset > 0)
1965 put_kctl_with_value(slave, offset);
1966 return 0;
1967}
1968
1969/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001970static int init_slave_unmute(struct hda_codec *codec,
1971 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001972{
1973 return put_kctl_with_value(slave, 1);
1974}
1975
Takashi Iwaie8750942014-06-30 14:02:39 +02001976static int add_slave(struct hda_codec *codec,
1977 void *data, struct snd_kcontrol *slave)
1978{
1979 return snd_ctl_add_slave(data, slave);
1980}
1981
Takashi Iwaid5191e52009-11-16 14:58:17 +01001982/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001983 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001984 * @codec: HD-audio codec
1985 * @name: vmaster control name
1986 * @tlv: TLV data (optional)
1987 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001988 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001989 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001990 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001991 *
1992 * Create a virtual master control with the given name. The TLV data
1993 * must be either NULL or a valid data.
1994 *
1995 * @slaves is a NULL-terminated array of strings, each of which is a
1996 * slave control name. All controls with these names are assigned to
1997 * the new virtual master control.
1998 *
1999 * This function returns zero if successful or a negative error code.
2000 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002001int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002002 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002003 const char *suffix, bool init_slave_vol,
2004 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002005{
2006 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002007 int err;
2008
Takashi Iwai29e58532012-03-12 12:25:03 +01002009 if (ctl_ret)
2010 *ctl_ret = NULL;
2011
Takashi Iwai9322ca52012-02-03 14:28:01 +01002012 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002013 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002014 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002015 return 0;
2016 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002017 kctl = snd_ctl_make_virtual_master(name, tlv);
2018 if (!kctl)
2019 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002020 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002021 if (err < 0)
2022 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002023
Takashi Iwaie8750942014-06-30 14:02:39 +02002024 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002025 if (err < 0)
2026 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002027
2028 /* init with master mute & zero volume */
2029 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002030 if (init_slave_vol) {
2031 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002032 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002033 tlv ? init_slave_0dB : init_slave_unmute, &step);
2034 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002035
Takashi Iwai29e58532012-03-12 12:25:03 +01002036 if (ctl_ret)
2037 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002038 return 0;
2039}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002040EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002041
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002042/*
2043 * mute-LED control using vmaster
2044 */
2045static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
2047{
2048 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002049 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002050 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002051
Takashi Iwai3ff72212014-10-20 18:17:28 +02002052 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002053}
2054
2055static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2059 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2060 return 0;
2061}
2062
2063static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2065{
2066 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2067 unsigned int old_mode = hook->mute_mode;
2068
2069 hook->mute_mode = ucontrol->value.enumerated.item[0];
2070 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2071 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2072 if (old_mode == hook->mute_mode)
2073 return 0;
2074 snd_hda_sync_vmaster_hook(hook);
2075 return 1;
2076}
2077
2078static struct snd_kcontrol_new vmaster_mute_mode = {
2079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2080 .name = "Mute-LED Mode",
2081 .info = vmaster_mute_mode_info,
2082 .get = vmaster_mute_mode_get,
2083 .put = vmaster_mute_mode_put,
2084};
2085
Takashi Iwaiee52e562015-04-27 10:36:11 +02002086/* meta hook to call each driver's vmaster hook */
2087static void vmaster_hook(void *private_data, int enabled)
2088{
2089 struct hda_vmaster_mute_hook *hook = private_data;
2090
2091 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2092 enabled = hook->mute_mode;
2093 hook->hook(hook->codec, enabled);
2094}
2095
Takashi Iwai95a962c2014-10-29 16:03:58 +01002096/**
2097 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2098 * @codec: the HDA codec
2099 * @hook: the vmaster hook object
2100 * @expose_enum_ctl: flag to create an enum ctl
2101 *
2102 * Add a mute-LED hook with the given vmaster switch kctl.
2103 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2104 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002105 */
2106int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002107 struct hda_vmaster_mute_hook *hook,
2108 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002109{
2110 struct snd_kcontrol *kctl;
2111
2112 if (!hook->hook || !hook->sw_kctl)
2113 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002114 hook->codec = codec;
2115 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002116 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735c2012-03-13 07:55:10 +01002117 if (!expose_enum_ctl)
2118 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002119 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2120 if (!kctl)
2121 return -ENOMEM;
2122 return snd_hda_ctl_add(codec, 0, kctl);
2123}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002124EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002125
Takashi Iwai95a962c2014-10-29 16:03:58 +01002126/**
2127 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2128 * @hook: the vmaster hook
2129 *
2130 * Call the hook with the current value for synchronization.
2131 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002132 */
2133void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2134{
2135 if (!hook->hook || !hook->codec)
2136 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002137 /* don't call vmaster hook in the destructor since it might have
2138 * been already destroyed
2139 */
2140 if (hook->codec->bus->shutdown)
2141 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002142 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002143}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002144EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002145
2146
Takashi Iwaid5191e52009-11-16 14:58:17 +01002147/**
2148 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002149 * @kcontrol: referred ctl element
2150 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002151 *
2152 * The control element is supposed to have the private_value field
2153 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2154 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 int chs = get_amp_channels(kcontrol);
2159
2160 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2161 uinfo->count = chs == 3 ? 2 : 1;
2162 uinfo->value.integer.min = 0;
2163 uinfo->value.integer.max = 1;
2164 return 0;
2165}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002166EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
2169 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002170 * @kcontrol: ctl element
2171 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002172 *
2173 * The control element is supposed to have the private_value field
2174 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2175 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002176int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 hda_nid_t nid = get_amp_nid(kcontrol);
2181 int chs = get_amp_channels(kcontrol);
2182 int dir = get_amp_direction(kcontrol);
2183 int idx = get_amp_index(kcontrol);
2184 long *valp = ucontrol->value.integer.value;
2185
2186 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002187 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002188 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002190 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002191 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002194EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002198 * @kcontrol: ctl element
2199 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002200 *
2201 * The control element is supposed to have the private_value field
2202 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2203 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002204int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2205 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2208 hda_nid_t nid = get_amp_nid(kcontrol);
2209 int chs = get_amp_channels(kcontrol);
2210 int dir = get_amp_direction(kcontrol);
2211 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 long *valp = ucontrol->value.integer.value;
2213 int change = 0;
2214
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002215 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002216 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2217 HDA_AMP_MUTE,
2218 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002219 valp++;
2220 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002221 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002222 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2223 HDA_AMP_MUTE,
2224 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002225 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return change;
2227}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002228EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230/*
Takashi Iwai985be542005-11-02 18:26:49 +01002231 * bound volume controls
2232 *
2233 * bind multiple volumes (# indices, from 0)
2234 */
2235
2236#define AMP_VAL_IDX_SHIFT 19
2237#define AMP_VAL_IDX_MASK (0x0f<<19)
2238
Takashi Iwaid5191e52009-11-16 14:58:17 +01002239/**
2240 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002241 * @kcontrol: ctl element
2242 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243 *
2244 * The control element is supposed to have the private_value field
2245 * set up via HDA_BIND_MUTE*() macros.
2246 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002247int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002249{
2250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2251 unsigned long pval;
2252 int err;
2253
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002254 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002255 pval = kcontrol->private_value;
2256 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2257 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2258 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002259 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002260 return err;
2261}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002262EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002263
Takashi Iwaid5191e52009-11-16 14:58:17 +01002264/**
2265 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002266 * @kcontrol: ctl element
2267 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002268 *
2269 * The control element is supposed to have the private_value field
2270 * set up via HDA_BIND_MUTE*() macros.
2271 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002272int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002274{
2275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2276 unsigned long pval;
2277 int i, indices, err = 0, change = 0;
2278
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002279 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002280 pval = kcontrol->private_value;
2281 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2282 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002283 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2284 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002285 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2286 if (err < 0)
2287 break;
2288 change |= err;
2289 }
2290 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002291 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002292 return err < 0 ? err : change;
2293}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002294EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002295
Takashi Iwaid5191e52009-11-16 14:58:17 +01002296/**
2297 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002298 * @kcontrol: referred ctl element
2299 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002300 *
2301 * The control element is supposed to have the private_value field
2302 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002303 */
2304int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_info *uinfo)
2306{
2307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2308 struct hda_bind_ctls *c;
2309 int err;
2310
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002311 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002312 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002313 kcontrol->private_value = *c->values;
2314 err = c->ops->info(kcontrol, uinfo);
2315 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002316 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002317 return err;
2318}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002319EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002320
Takashi Iwaid5191e52009-11-16 14:58:17 +01002321/**
2322 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002323 * @kcontrol: ctl element
2324 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002325 *
2326 * The control element is supposed to have the private_value field
2327 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2328 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002329int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2331{
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 struct hda_bind_ctls *c;
2334 int err;
2335
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002336 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002337 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002338 kcontrol->private_value = *c->values;
2339 err = c->ops->get(kcontrol, ucontrol);
2340 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002341 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002342 return err;
2343}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002344EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002345
Takashi Iwaid5191e52009-11-16 14:58:17 +01002346/**
2347 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002348 * @kcontrol: ctl element
2349 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350 *
2351 * The control element is supposed to have the private_value field
2352 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2353 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002354int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2356{
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 struct hda_bind_ctls *c;
2359 unsigned long *vals;
2360 int err = 0, change = 0;
2361
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002362 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002363 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002364 for (vals = c->values; *vals; vals++) {
2365 kcontrol->private_value = *vals;
2366 err = c->ops->put(kcontrol, ucontrol);
2367 if (err < 0)
2368 break;
2369 change |= err;
2370 }
2371 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002372 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002373 return err < 0 ? err : change;
2374}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002375EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002376
Takashi Iwaid5191e52009-11-16 14:58:17 +01002377/**
2378 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002379 * @kcontrol: ctl element
2380 * @op_flag: operation flag
2381 * @size: byte size of input TLV
2382 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002383 *
2384 * The control element is supposed to have the private_value field
2385 * set up via HDA_BIND_VOL() macro.
2386 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002387int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2388 unsigned int size, unsigned int __user *tlv)
2389{
2390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2391 struct hda_bind_ctls *c;
2392 int err;
2393
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002394 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002395 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002396 kcontrol->private_value = *c->values;
2397 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2398 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002399 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002400 return err;
2401}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002402EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002403
2404struct hda_ctl_ops snd_hda_bind_vol = {
2405 .info = snd_hda_mixer_amp_volume_info,
2406 .get = snd_hda_mixer_amp_volume_get,
2407 .put = snd_hda_mixer_amp_volume_put,
2408 .tlv = snd_hda_mixer_amp_tlv
2409};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002410EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002411
2412struct hda_ctl_ops snd_hda_bind_sw = {
2413 .info = snd_hda_mixer_amp_switch_info,
2414 .get = snd_hda_mixer_amp_switch_get,
2415 .put = snd_hda_mixer_amp_switch_put,
2416 .tlv = snd_hda_mixer_amp_tlv
2417};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002418EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002419
2420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 * SPDIF out controls
2422 */
2423
Takashi Iwai0ba21762007-04-16 11:29:14 +02002424static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
2427 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2428 uinfo->count = 1;
2429 return 0;
2430}
2431
Takashi Iwai0ba21762007-04-16 11:29:14 +02002432static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2436 IEC958_AES0_NONAUDIO |
2437 IEC958_AES0_CON_EMPHASIS_5015 |
2438 IEC958_AES0_CON_NOT_COPYRIGHT;
2439 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2440 IEC958_AES1_CON_ORIGINAL;
2441 return 0;
2442}
2443
Takashi Iwai0ba21762007-04-16 11:29:14 +02002444static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2448 IEC958_AES0_NONAUDIO |
2449 IEC958_AES0_PRO_EMPHASIS_5015;
2450 return 0;
2451}
2452
Takashi Iwai0ba21762007-04-16 11:29:14 +02002453static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002457 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002458 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002460 mutex_lock(&codec->spdif_mutex);
2461 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002462 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2463 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2464 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2465 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002466 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 return 0;
2469}
2470
2471/* convert from SPDIF status bits to HDA SPDIF bits
2472 * bit 0 (DigEn) is always set zero (to be filled later)
2473 */
2474static unsigned short convert_from_spdif_status(unsigned int sbits)
2475{
2476 unsigned short val = 0;
2477
2478 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002479 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002481 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002483 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2484 IEC958_AES0_PRO_EMPHASIS_5015)
2485 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002487 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2488 IEC958_AES0_CON_EMPHASIS_5015)
2489 val |= AC_DIG1_EMPHASIS;
2490 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2491 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002493 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2495 }
2496 return val;
2497}
2498
2499/* convert to SPDIF status bits from HDA SPDIF bits
2500 */
2501static unsigned int convert_to_spdif_status(unsigned short val)
2502{
2503 unsigned int sbits = 0;
2504
Takashi Iwai0ba21762007-04-16 11:29:14 +02002505 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002507 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 sbits |= IEC958_AES0_PROFESSIONAL;
2509 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002510 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2512 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002513 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002515 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002517 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2519 sbits |= val & (0x7f << 8);
2520 }
2521 return sbits;
2522}
2523
Takashi Iwai2f728532008-09-25 16:32:41 +02002524/* set digital convert verbs both for the given NID and its slaves */
2525static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002526 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002527{
Takashi Iwaidda14412011-05-02 11:29:30 +02002528 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002529
Takashi Iwaia551d912015-02-26 12:34:49 +01002530 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2531 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002532 d = codec->slave_dig_outs;
2533 if (!d)
2534 return;
2535 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002536 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002537 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002538}
2539
2540static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2541 int dig1, int dig2)
2542{
Takashi Iwaia551d912015-02-26 12:34:49 +01002543 unsigned int mask = 0;
2544 unsigned int val = 0;
2545
2546 if (dig1 != -1) {
2547 mask |= 0xff;
2548 val = dig1;
2549 }
2550 if (dig2 != -1) {
2551 mask |= 0xff00;
2552 val |= dig2 << 8;
2553 }
2554 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002555}
2556
Takashi Iwai0ba21762007-04-16 11:29:14 +02002557static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002561 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002562 struct hda_spdif_out *spdif;
2563 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 unsigned short val;
2565 int change;
2566
Ingo Molnar62932df2006-01-16 16:34:20 +01002567 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002568 spdif = snd_array_elem(&codec->spdif_out, idx);
2569 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002570 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2572 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2573 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002574 val = convert_from_spdif_status(spdif->status);
2575 val |= spdif->ctls & 1;
2576 change = spdif->ctls != val;
2577 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002578 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002579 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002580 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return change;
2582}
2583
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002584#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Takashi Iwai0ba21762007-04-16 11:29:14 +02002586static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002590 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002591 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002593 mutex_lock(&codec->spdif_mutex);
2594 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002595 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002596 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
Stephen Warren74b654c2011-06-01 11:14:18 -06002600static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2601 int dig1, int dig2)
2602{
2603 set_dig_out_convert(codec, nid, dig1, dig2);
2604 /* unmute amp switch (if any) */
2605 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2606 (dig1 & AC_DIG1_ENABLE))
2607 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2608 HDA_AMP_MUTE, 0);
2609}
2610
Takashi Iwai0ba21762007-04-16 11:29:14 +02002611static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002615 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002616 struct hda_spdif_out *spdif;
2617 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 unsigned short val;
2619 int change;
2620
Ingo Molnar62932df2006-01-16 16:34:20 +01002621 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002622 spdif = snd_array_elem(&codec->spdif_out, idx);
2623 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002624 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002626 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002627 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002628 spdif->ctls = val;
2629 if (change && nid != (u16)-1)
2630 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002631 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return change;
2633}
2634
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002635static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 {
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, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_cmask_get,
2642 },
2643 {
2644 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002646 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .info = snd_hda_spdif_mask_info,
2648 .get = snd_hda_spdif_pmask_get,
2649 },
2650 {
2651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002652 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 .info = snd_hda_spdif_mask_info,
2654 .get = snd_hda_spdif_default_get,
2655 .put = snd_hda_spdif_default_put,
2656 },
2657 {
2658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002659 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .info = snd_hda_spdif_out_switch_info,
2661 .get = snd_hda_spdif_out_switch_get,
2662 .put = snd_hda_spdif_out_switch_put,
2663 },
2664 { } /* end */
2665};
2666
2667/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002668 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002670 * @associated_nid: NID that new ctls associated with
2671 * @cvt_nid: converter NID
2672 * @type: HDA_PCM_TYPE_*
2673 * Creates controls related with the digital output.
2674 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 *
2676 * Returns 0 if successful, or a negative error code.
2677 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002678int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2679 hda_nid_t associated_nid,
2680 hda_nid_t cvt_nid,
2681 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
2683 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002684 struct snd_kcontrol *kctl;
2685 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002686 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002687 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002688 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002689 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002690 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002692 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002693 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002694 idx = spdif_index;
2695 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002696 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002697 /* suppose a single SPDIF device */
2698 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2699 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2700 if (!kctl)
2701 break;
2702 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002703 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002704 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002705 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002706 if (!bus->primary_dig_out_type)
2707 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002708
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002709 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002710 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002711 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002712 return -EBUSY;
2713 }
Stephen Warren7c935972011-06-01 11:14:17 -06002714 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002715 if (!spdif)
2716 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2718 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002719 if (!kctl)
2720 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002721 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002722 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002723 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002724 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return err;
2726 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002727 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002728 snd_hdac_regmap_read(&codec->core, cvt_nid,
2729 AC_VERB_GET_DIGI_CONVERT_1, &val);
2730 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002731 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return 0;
2733}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002734EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Takashi Iwai95a962c2014-10-29 16:03:58 +01002736/**
2737 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2738 * @codec: the HDA codec
2739 * @nid: widget NID
2740 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002741 * call within spdif_mutex lock
2742 */
Stephen Warren7c935972011-06-01 11:14:17 -06002743struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2744 hda_nid_t nid)
2745{
2746 int i;
2747 for (i = 0; i < codec->spdif_out.used; i++) {
2748 struct hda_spdif_out *spdif =
2749 snd_array_elem(&codec->spdif_out, i);
2750 if (spdif->nid == nid)
2751 return spdif;
2752 }
2753 return NULL;
2754}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002755EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002756
Takashi Iwai95a962c2014-10-29 16:03:58 +01002757/**
2758 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2759 * @codec: the HDA codec
2760 * @idx: the SPDIF ctl index
2761 *
2762 * Unassign the widget from the given SPDIF control.
2763 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002764void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2765{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002766 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002767
2768 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002769 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002770 spdif->nid = (u16)-1;
2771 mutex_unlock(&codec->spdif_mutex);
2772}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002773EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002774
Takashi Iwai95a962c2014-10-29 16:03:58 +01002775/**
2776 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2777 * @codec: the HDA codec
2778 * @idx: the SPDIF ctl idx
2779 * @nid: widget NID
2780 *
2781 * Assign the widget to the SPDIF control with the given index.
2782 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002783void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2784{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002785 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002786 unsigned short val;
2787
2788 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002789 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002790 if (spdif->nid != nid) {
2791 spdif->nid = nid;
2792 val = spdif->ctls;
2793 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2794 }
2795 mutex_unlock(&codec->spdif_mutex);
2796}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002797EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002800 * SPDIF sharing with analog output
2801 */
2802static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804{
2805 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2806 ucontrol->value.integer.value[0] = mout->share_spdif;
2807 return 0;
2808}
2809
2810static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2812{
2813 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2814 mout->share_spdif = !!ucontrol->value.integer.value[0];
2815 return 0;
2816}
2817
2818static struct snd_kcontrol_new spdif_share_sw = {
2819 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2820 .name = "IEC958 Default PCM Playback Switch",
2821 .info = snd_ctl_boolean_mono_info,
2822 .get = spdif_share_sw_get,
2823 .put = spdif_share_sw_put,
2824};
2825
Takashi Iwaid5191e52009-11-16 14:58:17 +01002826/**
2827 * snd_hda_create_spdif_share_sw - create Default PCM switch
2828 * @codec: the HDA codec
2829 * @mout: multi-out instance
2830 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002831int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2832 struct hda_multi_out *mout)
2833{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002834 struct snd_kcontrol *kctl;
2835
Takashi Iwai9a081602008-02-12 18:37:26 +01002836 if (!mout->dig_out_nid)
2837 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002838
2839 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2840 if (!kctl)
2841 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002842 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002843 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002844}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002845EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002846
2847/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 * SPDIF input
2849 */
2850
2851#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2852
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857
2858 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2859 return 0;
2860}
2861
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2866 hda_nid_t nid = kcontrol->private_value;
2867 unsigned int val = !!ucontrol->value.integer.value[0];
2868 int change;
2869
Ingo Molnar62932df2006-01-16 16:34:20 +01002870 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002872 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002874 snd_hdac_regmap_write(&codec->core, nid,
2875 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002877 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return change;
2879}
2880
Takashi Iwai0ba21762007-04-16 11:29:14 +02002881static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2882 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2885 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002886 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 unsigned int sbits;
2888
Takashi Iwaia551d912015-02-26 12:34:49 +01002889 snd_hdac_regmap_read(&codec->core, nid,
2890 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 sbits = convert_to_spdif_status(val);
2892 ucontrol->value.iec958.status[0] = sbits;
2893 ucontrol->value.iec958.status[1] = sbits >> 8;
2894 ucontrol->value.iec958.status[2] = sbits >> 16;
2895 ucontrol->value.iec958.status[3] = sbits >> 24;
2896 return 0;
2897}
2898
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002899static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 {
2901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002902 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 .info = snd_hda_spdif_in_switch_info,
2904 .get = snd_hda_spdif_in_switch_get,
2905 .put = snd_hda_spdif_in_switch_put,
2906 },
2907 {
2908 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002910 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 .info = snd_hda_spdif_mask_info,
2912 .get = snd_hda_spdif_in_status_get,
2913 },
2914 { } /* end */
2915};
2916
2917/**
2918 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2919 * @codec: the HDA codec
2920 * @nid: audio in widget NID
2921 *
2922 * Creates controls related with the SPDIF input.
2923 * Called from each patch supporting the SPDIF in.
2924 *
2925 * Returns 0 if successful, or a negative error code.
2926 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002927int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002930 struct snd_kcontrol *kctl;
2931 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002932 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Takashi Iwaidcda5802012-10-12 17:24:51 +02002934 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002935 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002936 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002937 return -EBUSY;
2938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2940 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002941 if (!kctl)
2942 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002944 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return err;
2947 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002948 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002949 snd_hda_codec_read(codec, nid, 0,
2950 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return 0;
2953}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002954EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Takashi Iwai95a962c2014-10-29 16:03:58 +01002956/**
2957 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2958 * @codec: the HDA codec
2959 * @fg: function group (not used now)
2960 * @power_state: the power state to set (AC_PWRST_*)
2961 *
2962 * Set the given power state to all widgets that have the power control.
2963 * If the codec has power_filter set, it evaluates the power state and
2964 * filter out if it's unchanged as D3.
2965 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002966void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002967 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002968{
Takashi Iwai7639a062015-03-03 10:07:24 +01002969 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002970
Takashi Iwai7639a062015-03-03 10:07:24 +01002971 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002972 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002973 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002974 if (!(wcaps & AC_WCAP_POWER))
2975 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002976 if (codec->power_filter) {
2977 state = codec->power_filter(codec, nid, power_state);
2978 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002979 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002980 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002981 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002982 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002983 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002984}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002985EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002986
2987/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002988 * wait until the state is reached, returns the current state
2989 */
2990static unsigned int hda_sync_power_state(struct hda_codec *codec,
2991 hda_nid_t fg,
2992 unsigned int power_state)
2993{
2994 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2995 unsigned int state, actual_state;
2996
2997 for (;;) {
2998 state = snd_hda_codec_read(codec, fg, 0,
2999 AC_VERB_GET_POWER_STATE, 0);
3000 if (state & AC_PWRST_ERROR)
3001 break;
3002 actual_state = (state >> 4) & 0x0f;
3003 if (actual_state == power_state)
3004 break;
3005 if (time_after_eq(jiffies, end_time))
3006 break;
3007 /* wait until the codec reachs to the target state */
3008 msleep(1);
3009 }
3010 return state;
3011}
3012
Takashi Iwai95a962c2014-10-29 16:03:58 +01003013/**
3014 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3015 * @codec: the HDA codec
3016 * @nid: widget NID
3017 * @power_state: power state to evalue
3018 *
3019 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3020 * This can be used a codec power_filter callback.
3021 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003022unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3023 hda_nid_t nid,
3024 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003025{
Takashi Iwai7639a062015-03-03 10:07:24 +01003026 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003027 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003028 if (power_state == AC_PWRST_D3 &&
3029 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3030 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3031 int eapd = snd_hda_codec_read(codec, nid, 0,
3032 AC_VERB_GET_EAPD_BTLENABLE, 0);
3033 if (eapd & 0x02)
3034 return AC_PWRST_D0;
3035 }
3036 return power_state;
3037}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003038EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003039
Takashi Iwai432c6412012-08-28 09:59:20 -07003040/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003041 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003042 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003043static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003044 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003045{
Takashi Iwai7639a062015-03-03 10:07:24 +01003046 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003047 int count;
3048 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003049 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003050
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003051 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003052 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003053 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003054 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003055 else if (codec->depop_delay > 0)
3056 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003057 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003058 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003059
3060 /* repeat power states setting at most 10 times*/
3061 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003062 if (codec->patch_ops.set_power_state)
3063 codec->patch_ops.set_power_state(codec, fg,
3064 power_state);
3065 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003066 state = power_state;
3067 if (codec->power_filter)
3068 state = codec->power_filter(codec, fg, state);
3069 if (state == power_state || power_state != AC_PWRST_D3)
3070 snd_hda_codec_read(codec, fg, flags,
3071 AC_VERB_SET_POWER_STATE,
3072 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003073 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003074 }
3075 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003076 if (!(state & AC_PWRST_ERROR))
3077 break;
3078 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003079
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003080 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003081}
Takashi Iwai54d17402005-11-21 16:33:22 +01003082
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003083/* sync power states of all widgets;
3084 * this is called at the end of codec parsing
3085 */
3086static void sync_power_up_states(struct hda_codec *codec)
3087{
Takashi Iwai7639a062015-03-03 10:07:24 +01003088 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003089
Takashi Iwaiba615b82013-03-13 14:47:21 +01003090 /* don't care if no filter is used */
3091 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003092 return;
3093
Takashi Iwai7639a062015-03-03 10:07:24 +01003094 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003095 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003096 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003097 if (!(wcaps & AC_WCAP_POWER))
3098 continue;
3099 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3100 if (target == AC_PWRST_D0)
3101 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003102 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003103 snd_hda_codec_write(codec, nid, 0,
3104 AC_VERB_SET_POWER_STATE, target);
3105 }
3106}
3107
Takashi Iwai648a8d22014-02-25 10:38:13 +01003108#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003109/* execute additional init verbs */
3110static void hda_exec_init_verbs(struct hda_codec *codec)
3111{
3112 if (codec->init_verbs.list)
3113 snd_hda_sequence_write(codec, codec->init_verbs.list);
3114}
3115#else
3116static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3117#endif
3118
Takashi Iwai2a439522011-07-26 09:52:50 +02003119#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003120/* update the power on/off account with the current jiffies */
3121static void update_power_acct(struct hda_codec *codec, bool on)
3122{
3123 unsigned long delta = jiffies - codec->power_jiffies;
3124
3125 if (on)
3126 codec->power_on_acct += delta;
3127 else
3128 codec->power_off_acct += delta;
3129 codec->power_jiffies += delta;
3130}
3131
3132void snd_hda_update_power_acct(struct hda_codec *codec)
3133{
3134 update_power_acct(codec, hda_codec_is_power_on(codec));
3135}
3136
Takashi Iwaicb53c622007-08-10 17:21:45 +02003137/*
3138 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003139 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003140 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003141static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003142{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003143 unsigned int state;
3144
Takashi Iwai7639a062015-03-03 10:07:24 +01003145 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003146
Takashi Iwaicb53c622007-08-10 17:21:45 +02003147 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003148 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003149 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003150 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003151 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003152 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003153 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003154}
3155
3156/*
3157 * kick up codec; used both from PM and power-save
3158 */
3159static void hda_call_codec_resume(struct hda_codec *codec)
3160{
Takashi Iwai7639a062015-03-03 10:07:24 +01003161 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003162
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003163 if (codec->core.regmap)
3164 regcache_mark_dirty(codec->core.regmap);
3165
Takashi Iwaicc72da72015-02-19 16:00:22 +01003166 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003167
Takashi Iwaid8193872012-08-31 07:54:38 -07003168 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003169 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003170 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003171 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003172 if (codec->patch_ops.resume)
3173 codec->patch_ops.resume(codec);
3174 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003175 if (codec->patch_ops.init)
3176 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003177 if (codec->core.regmap)
3178 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003179 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003180
3181 if (codec->jackpoll_interval)
3182 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003183 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003184 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003185 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003186}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003187
Takashi Iwaicc72da72015-02-19 16:00:22 +01003188static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003189{
3190 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003191 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003192 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003193
3194 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003195 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3196 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003197 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003198 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3199 (state & AC_PWRST_CLK_STOP_OK))
3200 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003201 return 0;
3202}
3203
Takashi Iwaicc72da72015-02-19 16:00:22 +01003204static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003205{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003206 struct hda_codec *codec = dev_to_hda_codec(dev);
3207
Takashi Iwai7639a062015-03-03 10:07:24 +01003208 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003209 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003210 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003211 return 0;
3212}
Takashi Iwai2a439522011-07-26 09:52:50 +02003213#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003214
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003215/* referred in hda_bind.c */
3216const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003217 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3218 pm_runtime_force_resume)
3219 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3220 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003221};
Takashi Iwai54d17402005-11-21 16:33:22 +01003222
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003223/*
3224 * add standard channel maps if not specified
3225 */
3226static int add_std_chmaps(struct hda_codec *codec)
3227{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003228 struct hda_pcm *pcm;
3229 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003230
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003231 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003232 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003233 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003234 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003235 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003236
Sudip Mukherjee751e2212015-04-02 16:51:43 +05303237 if (!pcm || pcm->own_chmap ||
3238 !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003239 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003240 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003241 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003242 hinfo->channels_max,
3243 0, &chmap);
3244 if (err < 0)
3245 return err;
3246 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3247 }
3248 }
3249 return 0;
3250}
3251
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003252/* default channel maps for 2.1 speakers;
3253 * since HD-audio supports only stereo, odd number channels are omitted
3254 */
3255const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3256 { .channels = 2,
3257 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3258 { .channels = 4,
3259 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3260 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3261 { }
3262};
3263EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3264
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003265int snd_hda_codec_build_controls(struct hda_codec *codec)
3266{
3267 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003268 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003269 /* continue to initialize... */
3270 if (codec->patch_ops.init)
3271 err = codec->patch_ops.init(codec);
3272 if (!err && codec->patch_ops.build_controls)
3273 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003274 if (err < 0)
3275 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003276
3277 /* we create chmaps here instead of build_pcms */
3278 err = add_std_chmaps(codec);
3279 if (err < 0)
3280 return err;
3281
David Henningsson26a6cb62012-10-09 15:04:21 +02003282 if (codec->jackpoll_interval)
3283 hda_jackpoll_work(&codec->jackpoll_work.work);
3284 else
3285 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003286 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return 0;
3288}
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290/*
3291 * stream formats
3292 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003293struct hda_rate_tbl {
3294 unsigned int hz;
3295 unsigned int alsa_bits;
3296 unsigned int hda_fmt;
3297};
3298
Takashi Iwai92f10b32010-08-03 14:21:00 +02003299/* rate = base * mult / div */
3300#define HDA_RATE(base, mult, div) \
3301 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3302 (((div) - 1) << AC_FMT_DIV_SHIFT))
3303
Takashi Iwaibefdf312005-08-22 13:57:55 +02003304static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003306
3307 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003308 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3309 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3310 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3311 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3312 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3313 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3314 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3315 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3316 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3317 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3318 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003319#define AC_PAR_PCM_RATE_BITS 11
3320 /* up to bits 10, 384kHZ isn't supported properly */
3321
3322 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003323 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003324
Takashi Iwaibefdf312005-08-22 13:57:55 +02003325 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326};
3327
3328/**
3329 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003330 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 * @rate: the sample rate
3332 * @channels: the number of channels
3333 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3334 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003335 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 *
3337 * Calculate the format bitset from the given rate, channels and th PCM format.
3338 *
3339 * Return zero if invalid.
3340 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003341unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3342 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 unsigned int channels,
3344 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003345 unsigned int maxbps,
3346 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
3348 int i;
3349 unsigned int val = 0;
3350
Takashi Iwaibefdf312005-08-22 13:57:55 +02003351 for (i = 0; rate_bits[i].hz; i++)
3352 if (rate_bits[i].hz == rate) {
3353 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 break;
3355 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003356 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003357 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return 0;
3359 }
3360
3361 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003362 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 0;
3364 }
3365 val |= channels - 1;
3366
3367 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003368 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003369 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003370 break;
3371 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003372 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 case 20:
3375 case 24:
3376 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003377 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003378 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003380 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003382 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 break;
3384 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003385 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003386 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return 0;
3388 }
3389
Anssi Hannula32c168c2010-08-03 13:28:57 +03003390 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003391 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return val;
3394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003395EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003397static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003398{
3399 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003400 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003401 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3402 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3403 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003404 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003405 if (!val || val == -1)
3406 return 0;
3407 return val;
3408}
3409
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003410static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003411{
3412 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3413 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003414 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003415 if (!streams || streams == -1)
3416 return 0;
3417 return streams;
3418}
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/**
3421 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3422 * @codec: the HDA codec
3423 * @nid: NID to query
3424 * @ratesp: the pointer to store the detected rate bitflags
3425 * @formatsp: the pointer to store the detected formats
3426 * @bpsp: the pointer to store the detected format widths
3427 *
3428 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3429 * or @bsps argument is ignored.
3430 *
3431 * Returns 0 if successful, otherwise a negative error code.
3432 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003433int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3435{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003436 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003438 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003439 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 if (ratesp) {
3442 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003443 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003445 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003447 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003448 codec_err(codec,
3449 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3450 nid, val,
3451 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003452 return -EIO;
3453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 *ratesp = rates;
3455 }
3456
3457 if (formatsp || bpsp) {
3458 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003459 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003461 streams = query_stream_param(codec, nid);
3462 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 bps = 0;
3466 if (streams & AC_SUPFMT_PCM) {
3467 if (val & AC_SUPPCM_BITS_8) {
3468 formats |= SNDRV_PCM_FMTBIT_U8;
3469 bps = 8;
3470 }
3471 if (val & AC_SUPPCM_BITS_16) {
3472 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3473 bps = 16;
3474 }
3475 if (wcaps & AC_WCAP_DIGITAL) {
3476 if (val & AC_SUPPCM_BITS_32)
3477 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3478 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3479 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3480 if (val & AC_SUPPCM_BITS_24)
3481 bps = 24;
3482 else if (val & AC_SUPPCM_BITS_20)
3483 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003484 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3485 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3487 if (val & AC_SUPPCM_BITS_32)
3488 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 else if (val & AC_SUPPCM_BITS_24)
3490 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003491 else if (val & AC_SUPPCM_BITS_20)
3492 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003495#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003496 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003498 if (!bps)
3499 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003500 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003501#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003502 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003503 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 /* temporary hack: we have still no proper support
3505 * for the direct AC3 stream...
3506 */
3507 formats |= SNDRV_PCM_FMTBIT_U8;
3508 bps = 8;
3509 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003510 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003511 codec_err(codec,
3512 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3513 nid, val,
3514 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3515 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003516 return -EIO;
3517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 if (formatsp)
3519 *formatsp = formats;
3520 if (bpsp)
3521 *bpsp = bps;
3522 }
3523
3524 return 0;
3525}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003526EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003529 * snd_hda_is_supported_format - Check the validity of the format
3530 * @codec: HD-audio codec
3531 * @nid: NID to check
3532 * @format: the HD-audio format value to check
3533 *
3534 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 *
3536 * Returns 1 if supported, 0 if not.
3537 */
3538int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3539 unsigned int format)
3540{
3541 int i;
3542 unsigned int val = 0, rate, stream;
3543
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003544 val = query_pcm_param(codec, nid);
3545 if (!val)
3546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003549 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003550 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 if (val & (1 << i))
3552 break;
3553 return 0;
3554 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003555 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return 0;
3557
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003558 stream = query_stream_param(codec, nid);
3559 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return 0;
3561
3562 if (stream & AC_SUPFMT_PCM) {
3563 switch (format & 0xf0) {
3564 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003565 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return 0;
3567 break;
3568 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003569 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571 break;
3572 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003573 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return 0;
3575 break;
3576 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003577 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return 0;
3579 break;
3580 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003581 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return 0;
3583 break;
3584 default:
3585 return 0;
3586 }
3587 } else {
3588 /* FIXME: check for float32 and AC3? */
3589 }
3590
3591 return 1;
3592}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003593EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595/*
3596 * PCM stuff
3597 */
3598static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3599 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003600 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 return 0;
3603}
3604
3605static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3606 struct hda_codec *codec,
3607 unsigned int stream_tag,
3608 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003609 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3612 return 0;
3613}
3614
3615static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3616 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003617 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Takashi Iwai888afa12008-03-18 09:57:50 +01003619 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return 0;
3621}
3622
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003623static int set_pcm_default_values(struct hda_codec *codec,
3624 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003626 int err;
3627
Takashi Iwai0ba21762007-04-16 11:29:14 +02003628 /* query support PCM information from the given NID */
3629 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003630 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003631 info->rates ? NULL : &info->rates,
3632 info->formats ? NULL : &info->formats,
3633 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003634 if (err < 0)
3635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637 if (info->ops.open == NULL)
3638 info->ops.open = hda_pcm_default_open_close;
3639 if (info->ops.close == NULL)
3640 info->ops.close = hda_pcm_default_open_close;
3641 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003642 if (snd_BUG_ON(!info->nid))
3643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 info->ops.prepare = hda_pcm_default_prepare;
3645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003647 if (snd_BUG_ON(!info->nid))
3648 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 info->ops.cleanup = hda_pcm_default_cleanup;
3650 }
3651 return 0;
3652}
3653
Takashi Iwaieb541332010-08-06 13:48:11 +02003654/*
3655 * codec prepare/cleanup entries
3656 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003657/**
3658 * snd_hda_codec_prepare - Prepare a stream
3659 * @codec: the HDA codec
3660 * @hinfo: PCM information
3661 * @stream: stream tag to assign
3662 * @format: format id to assign
3663 * @substream: PCM substream to assign
3664 *
3665 * Calls the prepare callback set by the codec with the given arguments.
3666 * Clean up the inactive streams when successful.
3667 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003668int snd_hda_codec_prepare(struct hda_codec *codec,
3669 struct hda_pcm_stream *hinfo,
3670 unsigned int stream,
3671 unsigned int format,
3672 struct snd_pcm_substream *substream)
3673{
3674 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003675 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003676 if (hinfo->ops.prepare)
3677 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3678 substream);
3679 else
3680 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003681 if (ret >= 0)
3682 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003683 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003684 return ret;
3685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003686EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003687
Takashi Iwai95a962c2014-10-29 16:03:58 +01003688/**
3689 * snd_hda_codec_cleanup - Prepare a stream
3690 * @codec: the HDA codec
3691 * @hinfo: PCM information
3692 * @substream: PCM substream
3693 *
3694 * Calls the cleanup callback set by the codec with the given arguments.
3695 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003696void snd_hda_codec_cleanup(struct hda_codec *codec,
3697 struct hda_pcm_stream *hinfo,
3698 struct snd_pcm_substream *substream)
3699{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003700 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003701 if (hinfo->ops.cleanup)
3702 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003703 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003704}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003705EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003706
Takashi Iwaid5191e52009-11-16 14:58:17 +01003707/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003708const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3709 "Audio", "SPDIF", "HDMI", "Modem"
3710};
3711
Takashi Iwai176d5332008-07-30 15:01:44 +02003712/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003713 * get the empty PCM device number to assign
3714 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003715static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003716{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003717 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003718 /* assigned to static slots up to dev#10; if more needed, assign
3719 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3720 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003721 static int audio_idx[HDA_PCM_NTYPES][5] = {
3722 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3723 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003724 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003725 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003726 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003727 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003728
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003729 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003730 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003731 return -EINVAL;
3732 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003733
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003734 for (i = 0; audio_idx[type][i] >= 0; i++) {
3735#ifndef CONFIG_SND_DYNAMIC_MINORS
3736 if (audio_idx[type][i] >= 8)
3737 break;
3738#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003739 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3740 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003741 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003742
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003743#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003744 /* non-fixed slots starting from 10 */
3745 for (i = 10; i < 32; i++) {
3746 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3747 return i;
3748 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003749#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003750
Takashi Iwai4e76a882014-02-25 12:21:03 +01003751 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003752 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003753#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003754 dev_warn(bus->card->dev,
3755 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003756#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003757 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003758}
3759
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003760/* call build_pcms ops of the given codec and set up the default parameters */
3761int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003762{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003763 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003764 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003765
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003766 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003767 return 0; /* already parsed */
3768
3769 if (!codec->patch_ops.build_pcms)
3770 return 0;
3771
3772 err = codec->patch_ops.build_pcms(codec);
3773 if (err < 0) {
3774 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003775 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003776 return err;
3777 }
3778
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003779 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003780 int stream;
3781
3782 for (stream = 0; stream < 2; stream++) {
3783 struct hda_pcm_stream *info = &cpcm->stream[stream];
3784
3785 if (!info->substreams)
3786 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003787 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003788 if (err < 0) {
3789 codec_warn(codec,
3790 "fail to setup default for PCM %s\n",
3791 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003792 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003793 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003794 }
3795 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003796
3797 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003798}
3799
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003800/* assign all PCMs of the given codec */
3801int snd_hda_codec_build_pcms(struct hda_codec *codec)
3802{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003803 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003804 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003805 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003806
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003807 if (snd_BUG_ON(!bus->ops.attach_pcm))
3808 return -EINVAL;
3809
3810 err = snd_hda_codec_parse_pcms(codec);
3811 if (err < 0) {
3812 snd_hda_codec_reset(codec);
3813 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003814 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003815
3816 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003817 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003818 if (cpcm->pcm)
3819 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003820 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003821 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003822
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003823 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3824 if (dev < 0)
3825 continue; /* no fatal error */
3826 cpcm->device = dev;
3827 err = bus->ops.attach_pcm(bus, codec, cpcm);
3828 if (err < 0) {
3829 codec_err(codec,
3830 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003831 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003832 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003833 }
3834 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003835
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003836 return 0;
3837}
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 * snd_hda_add_new_ctls - create controls from the array
3841 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003842 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 *
3844 * This helper function creates and add new controls in the given array.
3845 * The array must be terminated with an empty entry as terminator.
3846 *
3847 * Returns 0 if successful, or a negative error code.
3848 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003849int snd_hda_add_new_ctls(struct hda_codec *codec,
3850 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003852 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003855 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003856 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003857 if (knew->iface == -1) /* skip this codec private value */
3858 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003859 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003860 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003861 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003862 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003863 if (addr > 0)
3864 kctl->id.device = addr;
3865 if (idx > 0)
3866 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003867 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003868 if (!err)
3869 break;
3870 /* try first with another device index corresponding to
3871 * the codec addr; if it still fails (or it's the
3872 * primary codec), then try another control index
3873 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003874 if (!addr && codec->core.addr)
3875 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003876 else if (!idx && !knew->index) {
3877 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003878 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003879 if (idx <= 0)
3880 return err;
3881 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003882 return err;
3883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
3885 return 0;
3886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003887EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Takashi Iwai83012a72012-08-24 18:38:08 +02003889#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003890static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003891{
3892 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003893
Takashi Iwaicc72da72015-02-19 16:00:22 +01003894 if (delay > 0) {
3895 pm_runtime_set_autosuspend_delay(dev, delay);
3896 pm_runtime_use_autosuspend(dev);
3897 pm_runtime_allow(dev);
3898 if (!pm_runtime_suspended(dev))
3899 pm_runtime_mark_last_busy(dev);
3900 } else {
3901 pm_runtime_dont_use_autosuspend(dev);
3902 pm_runtime_forbid(dev);
3903 }
3904}
Takashi Iwaibb573922015-02-20 09:26:04 +01003905
3906/**
3907 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3908 * @bus: HD-audio bus
3909 * @delay: autosuspend delay in msec, 0 = off
3910 *
3911 * Synchronize the runtime PM autosuspend state from the power_save option.
3912 */
3913void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3914{
3915 struct hda_codec *c;
3916
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003917 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003918 codec_set_power_save(c, delay);
3919}
3920EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003921
Takashi Iwaid5191e52009-11-16 14:58:17 +01003922/**
3923 * snd_hda_check_amp_list_power - Check the amp list and update the power
3924 * @codec: HD-audio codec
3925 * @check: the object containing an AMP list and the status
3926 * @nid: NID to check / update
3927 *
3928 * Check whether the given NID is in the amp list. If it's in the list,
3929 * check the current AMP status, and update the the power-status according
3930 * to the mute status.
3931 *
3932 * This function is supposed to be set or called from the check_power_status
3933 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003934 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003935int snd_hda_check_amp_list_power(struct hda_codec *codec,
3936 struct hda_loopback_check *check,
3937 hda_nid_t nid)
3938{
Takashi Iwai031024e2011-05-02 11:29:30 +02003939 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003940 int ch, v;
3941
3942 if (!check->amplist)
3943 return 0;
3944 for (p = check->amplist; p->nid; p++) {
3945 if (p->nid == nid)
3946 break;
3947 }
3948 if (!p->nid)
3949 return 0; /* nothing changed */
3950
3951 for (p = check->amplist; p->nid; p++) {
3952 for (ch = 0; ch < 2; ch++) {
3953 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3954 p->idx);
3955 if (!(v & HDA_AMP_MUTE) && v > 0) {
3956 if (!check->power_on) {
3957 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003958 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003959 }
3960 return 1;
3961 }
3962 }
3963 }
3964 if (check->power_on) {
3965 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003966 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003967 }
3968 return 0;
3969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003970EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003971#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003973/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 * input MUX helper
3975 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003976
3977/**
3978 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003979 * @imux: imux helper object
3980 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003981 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003982int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3983 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 unsigned int index;
3986
3987 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3988 uinfo->count = 1;
3989 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003990 if (!imux->num_items)
3991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 index = uinfo->value.enumerated.item;
3993 if (index >= imux->num_items)
3994 index = imux->num_items - 1;
3995 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3996 return 0;
3997}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003998EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Takashi Iwaid5191e52009-11-16 14:58:17 +01004000/**
4001 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004002 * @codec: the HDA codec
4003 * @imux: imux helper object
4004 * @ucontrol: pointer to get/store the data
4005 * @nid: input mux NID
4006 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004007 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004008int snd_hda_input_mux_put(struct hda_codec *codec,
4009 const struct hda_input_mux *imux,
4010 struct snd_ctl_elem_value *ucontrol,
4011 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 unsigned int *cur_val)
4013{
4014 unsigned int idx;
4015
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004016 if (!imux->num_items)
4017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 idx = ucontrol->value.enumerated.item[0];
4019 if (idx >= imux->num_items)
4020 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004021 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004023 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4024 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 *cur_val = idx;
4026 return 1;
4027}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004028EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004031/**
4032 * snd_hda_enum_helper_info - Helper for simple enum ctls
4033 * @kcontrol: ctl element
4034 * @uinfo: pointer to get/store the data
4035 * @num_items: number of enum items
4036 * @texts: enum item string array
4037 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004038 * process kcontrol info callback of a simple string enum array
4039 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4040 */
4041int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4042 struct snd_ctl_elem_info *uinfo,
4043 int num_items, const char * const *texts)
4044{
4045 static const char * const texts_default[] = {
4046 "Disabled", "Enabled"
4047 };
4048
4049 if (!texts || !num_items) {
4050 num_items = 2;
4051 texts = texts_default;
4052 }
4053
Takashi Iwai3ff72212014-10-20 18:17:28 +02004054 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004056EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004057
4058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 * Multi-channel / digital-out PCM helper functions
4060 */
4061
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004062/* setup SPDIF output stream */
4063static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4064 unsigned int stream_tag, unsigned int format)
4065{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004066 struct hda_spdif_out *spdif;
4067 unsigned int curr_fmt;
4068 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004069
Laurence Darby3bef1c32012-11-03 17:00:06 +00004070 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4071 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4072 AC_VERB_GET_STREAM_FORMAT, 0);
4073 reset = codec->spdif_status_reset &&
4074 (spdif->ctls & AC_DIG1_ENABLE) &&
4075 curr_fmt != format;
4076
4077 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4078 updated */
4079 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004080 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004081 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004082 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004083 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004084 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004085 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004086 for (d = codec->slave_dig_outs; *d; d++)
4087 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4088 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004089 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004090 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004091 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004092 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004093 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004094}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004095
Takashi Iwai2f728532008-09-25 16:32:41 +02004096static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4097{
4098 snd_hda_codec_cleanup_stream(codec, nid);
4099 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004100 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004101 for (d = codec->slave_dig_outs; *d; d++)
4102 snd_hda_codec_cleanup_stream(codec, *d);
4103 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004104}
4105
Takashi Iwaid5191e52009-11-16 14:58:17 +01004106/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004107 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004108 * @codec: the HDA codec
4109 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004111int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4112 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Ingo Molnar62932df2006-01-16 16:34:20 +01004114 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004115 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4116 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004117 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004119 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return 0;
4121}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004122EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Takashi Iwaid5191e52009-11-16 14:58:17 +01004124/**
4125 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004126 * @codec: the HDA codec
4127 * @mout: hda_multi_out object
4128 * @stream_tag: stream tag to assign
4129 * @format: format id to assign
4130 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004131 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004132int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4133 struct hda_multi_out *mout,
4134 unsigned int stream_tag,
4135 unsigned int format,
4136 struct snd_pcm_substream *substream)
4137{
4138 mutex_lock(&codec->spdif_mutex);
4139 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4140 mutex_unlock(&codec->spdif_mutex);
4141 return 0;
4142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004143EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004144
Takashi Iwaid5191e52009-11-16 14:58:17 +01004145/**
4146 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004147 * @codec: the HDA codec
4148 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004149 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004150int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4151 struct hda_multi_out *mout)
4152{
4153 mutex_lock(&codec->spdif_mutex);
4154 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4155 mutex_unlock(&codec->spdif_mutex);
4156 return 0;
4157}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004158EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004159
Takashi Iwaid5191e52009-11-16 14:58:17 +01004160/**
4161 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004162 * @codec: the HDA codec
4163 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004165int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4166 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Ingo Molnar62932df2006-01-16 16:34:20 +01004168 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004170 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return 0;
4172}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004173EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Takashi Iwaid5191e52009-11-16 14:58:17 +01004175/**
4176 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004177 * @codec: the HDA codec
4178 * @mout: hda_multi_out object
4179 * @substream: PCM substream to assign
4180 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004181 *
4182 * Open analog outputs and set up the hw-constraints.
4183 * If the digital outputs can be opened as slave, open the digital
4184 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004186int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4187 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004188 struct snd_pcm_substream *substream,
4189 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Takashi Iwai9a081602008-02-12 18:37:26 +01004191 struct snd_pcm_runtime *runtime = substream->runtime;
4192 runtime->hw.channels_max = mout->max_channels;
4193 if (mout->dig_out_nid) {
4194 if (!mout->analog_rates) {
4195 mout->analog_rates = hinfo->rates;
4196 mout->analog_formats = hinfo->formats;
4197 mout->analog_maxbps = hinfo->maxbps;
4198 } else {
4199 runtime->hw.rates = mout->analog_rates;
4200 runtime->hw.formats = mout->analog_formats;
4201 hinfo->maxbps = mout->analog_maxbps;
4202 }
4203 if (!mout->spdif_rates) {
4204 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4205 &mout->spdif_rates,
4206 &mout->spdif_formats,
4207 &mout->spdif_maxbps);
4208 }
4209 mutex_lock(&codec->spdif_mutex);
4210 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004211 if ((runtime->hw.rates & mout->spdif_rates) &&
4212 (runtime->hw.formats & mout->spdif_formats)) {
4213 runtime->hw.rates &= mout->spdif_rates;
4214 runtime->hw.formats &= mout->spdif_formats;
4215 if (mout->spdif_maxbps < hinfo->maxbps)
4216 hinfo->maxbps = mout->spdif_maxbps;
4217 } else {
4218 mout->share_spdif = 0;
4219 /* FIXME: need notify? */
4220 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004221 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004222 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4225 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4226}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004227EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Takashi Iwaid5191e52009-11-16 14:58:17 +01004229/**
4230 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004231 * @codec: the HDA codec
4232 * @mout: hda_multi_out object
4233 * @stream_tag: stream tag to assign
4234 * @format: format id to assign
4235 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004236 *
4237 * Set up the i/o for analog out.
4238 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004240int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4241 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 unsigned int stream_tag,
4243 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004244 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Takashi Iwaidda14412011-05-02 11:29:30 +02004246 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004248 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 int i;
4250
Ingo Molnar62932df2006-01-16 16:34:20 +01004251 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004252 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004253 if (mout->dig_out_nid && mout->share_spdif &&
4254 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004256 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4257 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004258 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004260 setup_dig_out_stream(codec, mout->dig_out_nid,
4261 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 } else {
4263 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004264 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004267 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004270 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4271 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004272 if (!mout->no_share_stream &&
4273 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004275 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4276 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004277 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004278 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4279 if (!mout->no_share_stream && mout->hp_out_nid[i])
4280 snd_hda_codec_setup_stream(codec,
4281 mout->hp_out_nid[i],
4282 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 /* surrounds */
4285 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004286 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004287 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4288 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004289 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004290 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4291 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004293
4294 /* extra surrounds */
4295 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4296 int ch = 0;
4297 if (!mout->extra_out_nid[i])
4298 break;
4299 if (chs >= (i + 1) * 2)
4300 ch = i * 2;
4301 else if (!mout->no_share_stream)
4302 break;
4303 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4304 stream_tag, ch, format);
4305 }
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return 0;
4308}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004309EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Takashi Iwaid5191e52009-11-16 14:58:17 +01004311/**
4312 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004313 * @codec: the HDA codec
4314 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004316int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4317 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Takashi Iwaidda14412011-05-02 11:29:30 +02004319 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 int i;
4321
4322 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004323 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004325 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004326 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4327 if (mout->hp_out_nid[i])
4328 snd_hda_codec_cleanup_stream(codec,
4329 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004330 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4331 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004332 snd_hda_codec_cleanup_stream(codec,
4333 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004334 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004336 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 mout->dig_out_used = 0;
4338 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004339 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return 0;
4341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004342EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Takashi Iwai47408602012-04-20 13:06:53 +02004344/**
4345 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004346 * @codec: the HDA codec
4347 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004348 *
4349 * Guess the suitable VREF pin bits to be set as the pin-control value.
4350 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4351 */
4352unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4353{
4354 unsigned int pincap;
4355 unsigned int oldval;
4356 oldval = snd_hda_codec_read(codec, pin, 0,
4357 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4358 pincap = snd_hda_query_pin_caps(codec, pin);
4359 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4360 /* Exception: if the default pin setup is vref50, we give it priority */
4361 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4362 return AC_PINCTL_VREF_80;
4363 else if (pincap & AC_PINCAP_VREF_50)
4364 return AC_PINCTL_VREF_50;
4365 else if (pincap & AC_PINCAP_VREF_100)
4366 return AC_PINCTL_VREF_100;
4367 else if (pincap & AC_PINCAP_VREF_GRD)
4368 return AC_PINCTL_VREF_GRD;
4369 return AC_PINCTL_VREF_HIZ;
4370}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004371EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004372
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004373/**
4374 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4375 * @codec: the HDA codec
4376 * @pin: referred pin NID
4377 * @val: pin ctl value to audit
4378 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004379unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4380 hda_nid_t pin, unsigned int val)
4381{
4382 static unsigned int cap_lists[][2] = {
4383 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4384 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4385 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4386 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4387 };
4388 unsigned int cap;
4389
4390 if (!val)
4391 return 0;
4392 cap = snd_hda_query_pin_caps(codec, pin);
4393 if (!cap)
4394 return val; /* don't know what to do... */
4395
4396 if (val & AC_PINCTL_OUT_EN) {
4397 if (!(cap & AC_PINCAP_OUT))
4398 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4399 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4400 val &= ~AC_PINCTL_HP_EN;
4401 }
4402
4403 if (val & AC_PINCTL_IN_EN) {
4404 if (!(cap & AC_PINCAP_IN))
4405 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4406 else {
4407 unsigned int vcap, vref;
4408 int i;
4409 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4410 vref = val & AC_PINCTL_VREFEN;
4411 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4412 if (vref == cap_lists[i][0] &&
4413 !(vcap & cap_lists[i][1])) {
4414 if (i == ARRAY_SIZE(cap_lists) - 1)
4415 vref = AC_PINCTL_VREF_HIZ;
4416 else
4417 vref = cap_lists[i + 1][0];
4418 }
4419 }
4420 val &= ~AC_PINCTL_VREFEN;
4421 val |= vref;
4422 }
4423 }
4424
4425 return val;
4426}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004427EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004428
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004429/**
4430 * _snd_hda_pin_ctl - Helper to set pin ctl value
4431 * @codec: the HDA codec
4432 * @pin: referred pin NID
4433 * @val: pin control value to set
4434 * @cached: access over codec pinctl cache or direct write
4435 *
4436 * This function is a helper to set a pin ctl value more safely.
4437 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4438 * value in pin target array via snd_hda_codec_set_pin_target(), then
4439 * actually writes the value via either snd_hda_codec_update_cache() or
4440 * snd_hda_codec_write() depending on @cached flag.
4441 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004442int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4443 unsigned int val, bool cached)
4444{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004445 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004446 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004447 if (cached)
4448 return snd_hda_codec_update_cache(codec, pin, 0,
4449 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4450 else
4451 return snd_hda_codec_write(codec, pin, 0,
4452 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004454EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004455
Takashi Iwai990061c2010-09-09 22:08:44 +02004456/**
4457 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004458 * @codec: the HDA codec
4459 * @imux: imux helper object
4460 * @label: the name of imux item to assign
4461 * @index: index number of imux item to assign
4462 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004463 *
4464 * When the same label is used already in the existing items, the number
4465 * suffix is appended to the label. This label index number is stored
4466 * to type_idx when non-NULL pointer is given.
4467 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004468int snd_hda_add_imux_item(struct hda_codec *codec,
4469 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004470 int index, int *type_idx)
4471{
4472 int i, label_idx = 0;
4473 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004474 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004475 return -EINVAL;
4476 }
4477 for (i = 0; i < imux->num_items; i++) {
4478 if (!strncmp(label, imux->items[i].label, strlen(label)))
4479 label_idx++;
4480 }
4481 if (type_idx)
4482 *type_idx = label_idx;
4483 if (label_idx > 0)
4484 snprintf(imux->items[imux->num_items].label,
4485 sizeof(imux->items[imux->num_items].label),
4486 "%s %d", label, label_idx);
4487 else
4488 strlcpy(imux->items[imux->num_items].label, label,
4489 sizeof(imux->items[imux->num_items].label));
4490 imux->items[imux->num_items].index = index;
4491 imux->num_items++;
4492 return 0;
4493}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004494EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004497 * snd_hda_bus_reset - Reset the bus
4498 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004500void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004502 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004504 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004505 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004506 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004507#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004508 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004509 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004510 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004511 }
4512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004515EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004516
Takashi Iwaid5191e52009-11-16 14:58:17 +01004517/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004518 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4519 * @pcm: PCM caps bits
4520 * @buf: the string buffer to write
4521 * @buflen: the max buffer length
4522 *
4523 * used by hda_proc.c and hda_eld.c
4524 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004525void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4526{
4527 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4528 int i, j;
4529
4530 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4531 if (pcm & (AC_SUPPCM_BITS_8 << i))
4532 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4533
4534 buf[j] = '\0'; /* necessary when j == 0 */
4535}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004536EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004537
4538MODULE_DESCRIPTION("HDA codec core");
4539MODULE_LICENSE("GPL");