blob: b27f250088c15dc65df763f3407914add6e64f1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwaid5191e52009-11-16 14:58:17 +010056/**
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
59 *
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
62 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040063const char *snd_hda_get_jack_location(u32 cfg)
64{
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
67 };
68 static unsigned char specials_idx[] = {
69 0x07, 0x08,
70 0x17, 0x18, 0x19,
71 0x37, 0x38
72 };
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
77 };
78 int i;
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
80 if ((cfg & 0x0f) < 7)
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
84 return specials[i];
85 }
86 return "UNKNOWN";
87}
Takashi Iwai2698ea92013-12-18 07:45:52 +010088EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040089
Takashi Iwaid5191e52009-11-16 14:58:17 +010090/**
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
93 *
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
96 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040097const char *snd_hda_get_jack_connectivity(u32 cfg)
98{
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
100
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
102}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100103EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400104
Takashi Iwaid5191e52009-11-16 14:58:17 +0100105/**
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
111 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400112const char *snd_hda_get_jack_type(u32 cfg)
113{
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200118 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119 };
120
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
123}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100124EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400125
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100126/*
Takashi Iwai05852442015-03-03 15:40:08 +0100127 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200128 */
Takashi Iwai05852442015-03-03 15:40:08 +0100129static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200131{
Takashi Iwai05852442015-03-03 15:40:08 +0100132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200133 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200134 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200135
Wu Fengguang6430aee2009-07-17 16:49:19 +0800136 if (cmd == ~0)
137 return -1;
138
Takashi Iwai8dd78332009-06-02 01:16:07 +0200139 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200140 snd_hda_power_up(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100141 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
145 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200146 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100147 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200148 snd_hda_power_down(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100149 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200150 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100151 codec_dbg(codec,
152 "resetting BUS due to fatal communication error\n");
Takashi Iwai8dd78332009-06-02 01:16:07 +0200153 bus->ops.bus_reset(bus);
154 }
155 goto again;
156 }
157 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100158 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200159 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200160 return err;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200167 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @verb: the verb to send
169 * @parm: the parameter for the verb
170 *
171 * Send a single command and read the corresponding response.
172 *
173 * Returns the obtained response value, or -1 for an error.
174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200175unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200176 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int verb, unsigned int parm)
178{
Takashi Iwai7639a062015-03-03 10:07:24 +0100179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200180 unsigned int res;
Takashi Iwai05852442015-03-03 15:40:08 +0100181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700182 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return res;
184}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100185EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/**
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200191 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @verb: the verb to send
193 * @parm: the parameter for the verb
194 *
195 * Send a single command without waiting for response.
196 *
197 * Returns 0 if successful, or a negative error code.
198 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200199int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Takashi Iwai7639a062015-03-03 10:07:24 +0100202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwai05852442015-03-03 15:40:08 +0100203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100205EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
211 *
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
214 */
215void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
216{
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
219}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100220EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100222/* connection list element */
223struct hda_conn_list {
224 struct list_head list;
225 int len;
226 hda_nid_t nid;
227 hda_nid_t conns[0];
228};
229
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200230/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100231static struct hda_conn_list *
232lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200233{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
236 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200237 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200238 }
239 return NULL;
240}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200241
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100242static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
244{
245 struct hda_conn_list *p;
246
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
248 if (!p)
249 return -ENOMEM;
250 p->len = len;
251 p->nid = nid;
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
254 return 0;
255}
256
257static void remove_conn_list(struct hda_codec *codec)
258{
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
262 list_del(&p->list);
263 kfree(p);
264 }
265}
266
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200267/* read the connection and add to the cache */
268static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200269{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100270 hda_nid_t list[32];
271 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200272 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200273
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
278 if (!result)
279 return -ENOMEM;
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
281 }
282 if (len >= 0)
283 len = snd_hda_override_conn_list(codec, nid, len, result);
284 if (result != list)
285 kfree(result);
286 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200287}
Takashi Iwaidce20792011-06-24 14:10:28 +0200288
289/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
292 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 * @listp: the pointer to store NID list
294 *
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
297 *
298 * Returns the number of connections, or a negative error code.
299 *
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
303 */
304int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
306{
307 bool added = false;
308
309 for (;;) {
310 int err;
311 const struct hda_conn_list *p;
312
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
315 if (p) {
316 if (listp)
317 *listp = p->conns;
318 return p->len;
319 }
320 if (snd_BUG_ON(added))
321 return -EINVAL;
322
323 err = read_and_add_raw_conns(codec, nid);
324 if (err < 0)
325 return err;
326 added = true;
327 }
328}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100329EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100330
331/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200332 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @codec: the HDA codec
334 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200335 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @max_conns: max. number of connections to store
337 *
338 * Parses the connection list of the given widget and stores the list
339 * of NIDs.
340 *
341 * Returns the number of connections, or a negative error code.
342 */
343int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200344 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 return -EINVAL;
354 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200356 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200357
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100358 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200359}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100360EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
365 * @nid: NID to parse
366 * @len: number of connection list entries
367 * @list: the list of connection entries
368 *
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
371 *
372 * Returns zero or a negative error code.
373 */
374int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100377 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 p = lookup_conn_list(codec, nid);
380 if (p) {
381 list_del(&p->list);
382 kfree(p);
383 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200384
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100385 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100387EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200388
389/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
395 *
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
399 */
400int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
402{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200404 int i, nums;
405
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100406 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200407 for (i = 0; i < nums; i++)
408 if (conn[i] == nid)
409 return i;
410 if (!recursive)
411 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100412 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200414 return -1;
415 }
416 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
420 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
422 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200423 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200424 return -1;
425}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100426EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Mengdong Linf1aa0682013-08-26 21:35:21 -0400428
429/* return DEVLIST_LEN parameter of the given widget */
430static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
437 return 0;
438
439 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
440 if (parm == -1 && codec->bus->rirb_error)
441 parm = 0;
442 return parm & AC_DEV_LIST_LEN_MASK;
443}
444
445/**
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
451 *
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
454 */
455int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
457{
458 unsigned int parm;
459 int i, dev_len, devices;
460
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
463 return 0;
464
465 dev_len = parm + 1;
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
467
468 devices = 0;
469 while (devices < dev_len) {
470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_DEVICE_LIST, devices);
472 if (parm == -1 && codec->bus->rirb_error)
473 break;
474
475 for (i = 0; i < 8; i++) {
476 dev_list[devices] = (u8)parm;
477 parm >>= 4;
478 devices++;
479 if (devices >= dev_len)
480 break;
481 }
482 }
483 return devices;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * destructor
488 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100489static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200491 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (bus->ops.private_free)
494 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100495 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100499static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Takashi Iwai2565c892014-02-19 11:41:09 +0100501 snd_hda_bus_free(device->device_data);
502 return 0;
503}
504
505static int snd_hda_bus_dev_disconnect(struct snd_device *device)
506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100508 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100512/* hdac_bus_ops translations */
513static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
514{
515 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
516 return bus->ops.command(bus, cmd);
517}
518
519static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
520 unsigned int *res)
521{
522 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
523 *res = bus->ops.get_response(bus, addr);
524 return bus->rirb_error ? -EIO : 0;
525}
526
527static const struct hdac_bus_ops bus_ops = {
528 .command = _hda_bus_command,
529 .get_response = _hda_bus_get_response,
530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/**
533 * snd_hda_bus_new - create a HDA bus
534 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * @busp: the pointer to store the created bus instance
536 *
537 * Returns 0 if successful, or a negative error code.
538 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100539int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100540 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct hda_bus *bus;
543 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100544 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100545 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 .dev_free = snd_hda_bus_dev_free,
547 };
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (busp)
550 *busp = NULL;
551
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200552 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100553 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100556 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
557 if (err < 0) {
558 kfree(bus);
559 return err;
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200563 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
566 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 snd_hda_bus_free(bus);
568 return err;
569 }
570 if (busp)
571 *busp = bus;
572 return 0;
573}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100574EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 * read widget caps for each widget and store in cache
578 */
579static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
580{
581 int i;
582 hda_nid_t nid;
583
Takashi Iwai7639a062015-03-03 10:07:24 +0100584 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200585 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100586 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100587 nid = codec->core.start_nid;
588 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100589 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
590 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100591 return 0;
592}
593
Takashi Iwai3be14142009-02-20 14:11:16 +0100594/* read all pin default configurations and save codec->init_pins */
595static int read_pin_defaults(struct hda_codec *codec)
596{
Takashi Iwai7639a062015-03-03 10:07:24 +0100597 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100598
Takashi Iwai7639a062015-03-03 10:07:24 +0100599 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100600 struct hda_pincfg *pin;
601 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200602 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100603 if (wid_type != AC_WID_PIN)
604 continue;
605 pin = snd_array_new(&codec->init_pins);
606 if (!pin)
607 return -ENOMEM;
608 pin->nid = nid;
609 pin->cfg = snd_hda_codec_read(codec, nid, 0,
610 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200611 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
612 AC_VERB_GET_PIN_WIDGET_CONTROL,
613 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100614 }
615 return 0;
616}
617
618/* look up the given pin config list and return the item matching with NID */
619static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
620 struct snd_array *array,
621 hda_nid_t nid)
622{
623 int i;
624 for (i = 0; i < array->used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(array, i);
626 if (pin->nid == nid)
627 return pin;
628 }
629 return NULL;
630}
631
Takashi Iwai3be14142009-02-20 14:11:16 +0100632/* set the current pin config value for the given NID.
633 * the value is cached, and read via snd_hda_codec_get_pincfg()
634 */
635int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
636 hda_nid_t nid, unsigned int cfg)
637{
638 struct hda_pincfg *pin;
639
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200640 /* the check below may be invalid when pins are added by a fixup
641 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
642 * for now
643 */
644 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100645 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
646 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200647 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100648
Takashi Iwai3be14142009-02-20 14:11:16 +0100649 pin = look_up_pincfg(codec, list, nid);
650 if (!pin) {
651 pin = snd_array_new(list);
652 if (!pin)
653 return -ENOMEM;
654 pin->nid = nid;
655 }
656 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100657 return 0;
658}
659
Takashi Iwaid5191e52009-11-16 14:58:17 +0100660/**
661 * snd_hda_codec_set_pincfg - Override a pin default configuration
662 * @codec: the HDA codec
663 * @nid: NID to set the pin config
664 * @cfg: the pin default config value
665 *
666 * Override a pin default configuration value in the cache.
667 * This value can be read by snd_hda_codec_get_pincfg() in a higher
668 * priority than the real hardware value.
669 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100670int snd_hda_codec_set_pincfg(struct hda_codec *codec,
671 hda_nid_t nid, unsigned int cfg)
672{
Takashi Iwai346ff702009-02-23 09:42:57 +0100673 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100674}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100675EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100676
Takashi Iwaid5191e52009-11-16 14:58:17 +0100677/**
678 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
679 * @codec: the HDA codec
680 * @nid: NID to get the pin config
681 *
682 * Get the current pin config value of the given pin NID.
683 * If the pincfg value is cached or overridden via sysfs or driver,
684 * returns the cached value.
685 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100686unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
687{
688 struct hda_pincfg *pin;
689
Takashi Iwai648a8d22014-02-25 10:38:13 +0100690#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100691 {
692 unsigned int cfg = 0;
693 mutex_lock(&codec->user_mutex);
694 pin = look_up_pincfg(codec, &codec->user_pins, nid);
695 if (pin)
696 cfg = pin->cfg;
697 mutex_unlock(&codec->user_mutex);
698 if (cfg)
699 return cfg;
700 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100701#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100702 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
703 if (pin)
704 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100705 pin = look_up_pincfg(codec, &codec->init_pins, nid);
706 if (pin)
707 return pin->cfg;
708 return 0;
709}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100710EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100711
Takashi Iwai95a962c2014-10-29 16:03:58 +0100712/**
713 * snd_hda_codec_set_pin_target - remember the current pinctl target value
714 * @codec: the HDA codec
715 * @nid: pin NID
716 * @val: assigned pinctl value
717 *
718 * This function stores the given value to a pinctl target value in the
719 * pincfg table. This isn't always as same as the actually written value
720 * but can be referred at any time via snd_hda_codec_get_pin_target().
721 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100722int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
723 unsigned int val)
724{
725 struct hda_pincfg *pin;
726
727 pin = look_up_pincfg(codec, &codec->init_pins, nid);
728 if (!pin)
729 return -EINVAL;
730 pin->target = val;
731 return 0;
732}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100733EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100734
Takashi Iwai95a962c2014-10-29 16:03:58 +0100735/**
736 * snd_hda_codec_get_pin_target - return the current pinctl target value
737 * @codec: the HDA codec
738 * @nid: pin NID
739 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100740int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
741{
742 struct hda_pincfg *pin;
743
744 pin = look_up_pincfg(codec, &codec->init_pins, nid);
745 if (!pin)
746 return 0;
747 return pin->target;
748}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100749EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100750
Takashi Iwai92ee6162009-12-27 11:18:59 +0100751/**
752 * snd_hda_shutup_pins - Shut up all pins
753 * @codec: the HDA codec
754 *
755 * Clear all pin controls to shup up before suspend for avoiding click noise.
756 * The controls aren't cached so that they can be resumed properly.
757 */
758void snd_hda_shutup_pins(struct hda_codec *codec)
759{
760 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200761 /* don't shut up pins when unloading the driver; otherwise it breaks
762 * the default pin setup at the next load of the driver
763 */
764 if (codec->bus->shutdown)
765 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100766 for (i = 0; i < codec->init_pins.used; i++) {
767 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
768 /* use read here for syncing after issuing each verb */
769 snd_hda_codec_read(codec, pin->nid, 0,
770 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
771 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200772 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100773}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100774EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100775
Takashi Iwai2a439522011-07-26 09:52:50 +0200776#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200777/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
778static void restore_shutup_pins(struct hda_codec *codec)
779{
780 int i;
781 if (!codec->pins_shutup)
782 return;
783 if (codec->bus->shutdown)
784 return;
785 for (i = 0; i < codec->init_pins.used; i++) {
786 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
787 snd_hda_codec_write(codec, pin->nid, 0,
788 AC_VERB_SET_PIN_WIDGET_CONTROL,
789 pin->ctrl);
790 }
791 codec->pins_shutup = 0;
792}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700793#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200794
David Henningsson26a6cb62012-10-09 15:04:21 +0200795static void hda_jackpoll_work(struct work_struct *work)
796{
797 struct hda_codec *codec =
798 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200799
800 snd_hda_jack_set_dirty_all(codec);
801 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400802
803 if (!codec->jackpoll_interval)
804 return;
805
Takashi Iwai2f35c632015-02-27 22:43:26 +0100806 schedule_delayed_work(&codec->jackpoll_work,
807 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200808}
809
Takashi Iwai01751f52007-08-10 16:59:39 +0200810static void init_hda_cache(struct hda_cache_rec *cache,
811 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200812static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200813
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100814/* release all pincfg lists */
815static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100816{
Takashi Iwai346ff702009-02-23 09:42:57 +0100817 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100818#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100819 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100820#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100821 snd_array_free(&codec->init_pins);
822}
823
Takashi Iwai54d17402005-11-21 16:33:22 +0100824/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200825 * audio-converter setup caches
826 */
827struct hda_cvt_setup {
828 hda_nid_t nid;
829 u8 stream_tag;
830 u8 channel_id;
831 u16 format_id;
832 unsigned char active; /* cvt is currently used */
833 unsigned char dirty; /* setups should be cleared */
834};
835
836/* get or create a cache entry for the given audio converter NID */
837static struct hda_cvt_setup *
838get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
839{
840 struct hda_cvt_setup *p;
841 int i;
842
843 for (i = 0; i < codec->cvt_setups.used; i++) {
844 p = snd_array_elem(&codec->cvt_setups, i);
845 if (p->nid == nid)
846 return p;
847 }
848 p = snd_array_new(&codec->cvt_setups);
849 if (p)
850 p->nid = nid;
851 return p;
852}
853
854/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100855 * PCM device
856 */
857static void release_pcm(struct kref *kref)
858{
859 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
860
861 if (pcm->pcm)
862 snd_device_free(pcm->codec->card, pcm->pcm);
863 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
864 kfree(pcm->name);
865 kfree(pcm);
866}
867
868void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
869{
870 kref_put(&pcm->kref, release_pcm);
871}
872EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
873
874struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
875 const char *fmt, ...)
876{
877 struct hda_pcm *pcm;
878 va_list args;
879
880 va_start(args, fmt);
881 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
882 if (!pcm)
883 return NULL;
884
885 pcm->codec = codec;
886 kref_init(&pcm->kref);
887 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
888 if (!pcm->name) {
889 kfree(pcm);
890 return NULL;
891 }
892
893 list_add_tail(&pcm->list, &codec->pcm_list_head);
894 return pcm;
895}
896EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
897
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100898/*
899 * codec destructor
900 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100901static void codec_release_pcms(struct hda_codec *codec)
902{
903 struct hda_pcm *pcm, *n;
904
905 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
906 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100907 if (pcm->pcm)
908 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100909 snd_hda_codec_pcm_put(pcm);
910 }
911}
912
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100913void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
914{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100915 if (codec->registered) {
916 /* pm_runtime_put() is called in snd_hdac_device_exit() */
917 pm_runtime_get_noresume(hda_codec_dev(codec));
918 pm_runtime_disable(hda_codec_dev(codec));
919 codec->registered = 0;
920 }
921
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100922 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100923 if (!codec->in_freeing)
924 snd_hda_ctls_clear(codec);
925 codec_release_pcms(codec);
926 snd_hda_detach_beep_device(codec);
927 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
928 snd_hda_jack_tbl_clear(codec);
929 codec->proc_widget_hook = NULL;
930 codec->spec = NULL;
931
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100932 free_hda_cache(&codec->cmd_cache);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100933 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
934
935 /* free only driver_pins so that init_pins + user_pins are restored */
936 snd_array_free(&codec->driver_pins);
937 snd_array_free(&codec->cvt_setups);
938 snd_array_free(&codec->spdif_out);
939 snd_array_free(&codec->verbs);
940 codec->preset = NULL;
941 codec->slave_dig_outs = NULL;
942 codec->spdif_status_reset = 0;
943 snd_array_free(&codec->mixers);
944 snd_array_free(&codec->nids);
945 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100946 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100947}
948
Takashi Iwaid8193872012-08-31 07:54:38 -0700949static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100950 unsigned int power_state);
951
Takashi Iwaic4c25332015-03-03 17:22:12 +0100952/* also called from hda_bind.c */
953void snd_hda_codec_register(struct hda_codec *codec)
954{
955 if (codec->registered)
956 return;
957 if (device_is_registered(hda_codec_dev(codec))) {
958 snd_hda_register_beep_device(codec);
959 pm_runtime_enable(hda_codec_dev(codec));
960 /* it was powered up in snd_hda_codec_new(), now all done */
961 snd_hda_power_down(codec);
962 codec->registered = 1;
963 }
964}
965
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100966static int snd_hda_codec_dev_register(struct snd_device *device)
967{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100968 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100969 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100970}
971
972static int snd_hda_codec_dev_disconnect(struct snd_device *device)
973{
974 struct hda_codec *codec = device->device_data;
975
Takashi Iwaid604b392014-02-28 13:42:09 +0100976 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100977 return 0;
978}
979
Takashi Iwai2565c892014-02-19 11:41:09 +0100980static int snd_hda_codec_dev_free(struct snd_device *device)
981{
Takashi Iwaid56db742015-02-27 23:25:35 +0100982 struct hda_codec *codec = device->device_data;
983
984 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100985 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100986 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100987 return 0;
988}
989
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100990static void snd_hda_codec_dev_release(struct device *dev)
991{
Takashi Iwaid56db742015-02-27 23:25:35 +0100992 struct hda_codec *codec = dev_to_hda_codec(dev);
993
994 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100995 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100996 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100997 free_hda_cache(&codec->cmd_cache);
Takashi Iwaid56db742015-02-27 23:25:35 +0100998 kfree(codec->modelname);
999 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +01001000 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/**
1004 * snd_hda_codec_new - create a HDA codec
1005 * @bus: the bus to assign
1006 * @codec_addr: the codec address
1007 * @codecp: the pointer to store the generated codec
1008 *
1009 * Returns 0 if successful, or a negative error code.
1010 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001011int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1012 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001015 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001016 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001018 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001019 .dev_register = snd_hda_codec_dev_register,
1020 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001021 .dev_free = snd_hda_codec_dev_free,
1022 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Takashi Iwaida3cec32008-08-08 17:12:14 +02001024 if (snd_BUG_ON(!bus))
1025 return -EINVAL;
1026 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001029 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001030 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Takashi Iwai7639a062015-03-03 10:07:24 +01001033 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1034 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1035 codec_addr);
1036 if (err < 0) {
1037 kfree(codec);
1038 return err;
1039 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001040
Takashi Iwai7639a062015-03-03 10:07:24 +01001041 codec->core.dev.release = snd_hda_codec_dev_release;
1042 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +01001043 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001046 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001048 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001049 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001050 mutex_init(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001051 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001052 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1053 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001054 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001055 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001056 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001057 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001058 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001059 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001060 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001061 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001062
David Henningsson26a6cb62012-10-09 15:04:21 +02001063 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001064 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001065 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Takashi Iwai83012a72012-08-24 18:38:08 +02001067#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01001068 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001069#endif
1070
Takashi Iwai648a8d22014-02-25 10:38:13 +01001071 snd_hda_sysfs_init(codec);
1072
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001073 if (codec->bus->modelname) {
1074 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1075 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001076 err = -ENODEV;
1077 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001078 }
1079 }
1080
Takashi Iwai7639a062015-03-03 10:07:24 +01001081 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -07001082 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001083 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001084 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001085 err = read_pin_defaults(codec);
1086 if (err < 0)
1087 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001088
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001089 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001090 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001091
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001092 snd_hda_codec_proc_new(codec);
1093
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001094 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001095
Takashi Iwai7639a062015-03-03 10:07:24 +01001096 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1097 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001098 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001099
Takashi Iwai6efdd852015-02-27 16:09:22 +01001100 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001101 if (err < 0)
1102 goto error;
1103
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001104 if (codecp)
1105 *codecp = codec;
1106 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001107
1108 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001109 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001110 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001111}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001112EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001113
Takashi Iwai95a962c2014-10-29 16:03:58 +01001114/**
1115 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1116 * @codec: the HDA codec
1117 *
1118 * Forcibly refresh the all widget caps and the init pin configurations of
1119 * the given codec.
1120 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001121int snd_hda_codec_update_widgets(struct hda_codec *codec)
1122{
1123 hda_nid_t fg;
1124 int err;
1125
Takashi Iwai7639a062015-03-03 10:07:24 +01001126 err = snd_hdac_refresh_widgets(&codec->core);
1127 if (err < 0)
1128 return err;
1129
Mengdong Lina15d05d2013-02-08 17:09:31 -05001130 /* Assume the function group node does not change,
1131 * only the widget nodes may change.
1132 */
1133 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +01001134 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001135 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001136 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001137 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001138
1139 snd_array_free(&codec->init_pins);
1140 err = read_pin_defaults(codec);
1141
1142 return err;
1143}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001144EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001145
Takashi Iwaied360812012-08-08 17:12:52 +02001146/* update the stream-id if changed */
1147static void update_pcm_stream_id(struct hda_codec *codec,
1148 struct hda_cvt_setup *p, hda_nid_t nid,
1149 u32 stream_tag, int channel_id)
1150{
1151 unsigned int oldval, newval;
1152
1153 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1154 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1155 newval = (stream_tag << 4) | channel_id;
1156 if (oldval != newval)
1157 snd_hda_codec_write(codec, nid, 0,
1158 AC_VERB_SET_CHANNEL_STREAMID,
1159 newval);
1160 p->stream_tag = stream_tag;
1161 p->channel_id = channel_id;
1162 }
1163}
1164
1165/* update the format-id if changed */
1166static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1167 hda_nid_t nid, int format)
1168{
1169 unsigned int oldval;
1170
1171 if (p->format_id != format) {
1172 oldval = snd_hda_codec_read(codec, nid, 0,
1173 AC_VERB_GET_STREAM_FORMAT, 0);
1174 if (oldval != format) {
1175 msleep(1);
1176 snd_hda_codec_write(codec, nid, 0,
1177 AC_VERB_SET_STREAM_FORMAT,
1178 format);
1179 }
1180 p->format_id = format;
1181 }
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184/**
1185 * snd_hda_codec_setup_stream - set up the codec for streaming
1186 * @codec: the CODEC to set up
1187 * @nid: the NID to set up
1188 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1189 * @channel_id: channel id to pass, zero based.
1190 * @format: stream format.
1191 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001192void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1193 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int channel_id, int format)
1195{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001196 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001197 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001198 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001199 int i;
1200
Takashi Iwai0ba21762007-04-16 11:29:14 +02001201 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001202 return;
1203
Takashi Iwai4e76a882014-02-25 12:21:03 +01001204 codec_dbg(codec,
1205 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1206 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001207 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001208 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001209 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001210
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001211 if (codec->patch_ops.stream_pm)
1212 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001213 if (codec->pcm_format_first)
1214 update_pcm_format(codec, p, nid, format);
1215 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1216 if (!codec->pcm_format_first)
1217 update_pcm_format(codec, p, nid, format);
1218
Takashi Iwaieb541332010-08-06 13:48:11 +02001219 p->active = 1;
1220 p->dirty = 0;
1221
1222 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001223 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001224 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001225 for (i = 0; i < c->cvt_setups.used; i++) {
1226 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001227 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001228 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001229 p->dirty = 1;
1230 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001233EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Takashi Iwaif0cea792010-08-13 11:56:53 +02001235static void really_cleanup_stream(struct hda_codec *codec,
1236 struct hda_cvt_setup *q);
1237
Takashi Iwaid5191e52009-11-16 14:58:17 +01001238/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001239 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001240 * @codec: the CODEC to clean up
1241 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001242 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001243 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001244void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1245 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001246{
Takashi Iwaieb541332010-08-06 13:48:11 +02001247 struct hda_cvt_setup *p;
1248
Takashi Iwai888afa12008-03-18 09:57:50 +01001249 if (!nid)
1250 return;
1251
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001252 if (codec->no_sticky_stream)
1253 do_now = 1;
1254
Takashi Iwai4e76a882014-02-25 12:21:03 +01001255 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001256 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001257 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001258 /* here we just clear the active flag when do_now isn't set;
1259 * actual clean-ups will be done later in
1260 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1261 */
1262 if (do_now)
1263 really_cleanup_stream(codec, p);
1264 else
1265 p->active = 0;
1266 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001268EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001269
Takashi Iwaieb541332010-08-06 13:48:11 +02001270static void really_cleanup_stream(struct hda_codec *codec,
1271 struct hda_cvt_setup *q)
1272{
1273 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001274 if (q->stream_tag || q->channel_id)
1275 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1276 if (q->format_id)
1277 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1278);
Takashi Iwaieb541332010-08-06 13:48:11 +02001279 memset(q, 0, sizeof(*q));
1280 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001281 if (codec->patch_ops.stream_pm)
1282 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001283}
1284
1285/* clean up the all conflicting obsolete streams */
1286static void purify_inactive_streams(struct hda_codec *codec)
1287{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001288 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001289 int i;
1290
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001291 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001292 for (i = 0; i < c->cvt_setups.used; i++) {
1293 struct hda_cvt_setup *p;
1294 p = snd_array_elem(&c->cvt_setups, i);
1295 if (p->dirty)
1296 really_cleanup_stream(c, p);
1297 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001298 }
1299}
1300
Takashi Iwai2a439522011-07-26 09:52:50 +02001301#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001302/* clean up all streams; called from suspend */
1303static void hda_cleanup_all_streams(struct hda_codec *codec)
1304{
1305 int i;
1306
1307 for (i = 0; i < codec->cvt_setups.used; i++) {
1308 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1309 if (p->stream_tag)
1310 really_cleanup_stream(codec, p);
1311 }
1312}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001313#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
1316 * amp access functions
1317 */
1318
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001319/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001320#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001321#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001322#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1323#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001325#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001328static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001329 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Takashi Iwai01751f52007-08-10 16:59:39 +02001331 memset(cache, 0, sizeof(*cache));
1332 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001333 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001334}
1335
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001336static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001337{
Takashi Iwai603c4012008-07-30 15:01:44 +02001338 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001342static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwai01751f52007-08-10 16:59:39 +02001344 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1345 u16 cur = cache->hash[idx];
1346 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001349 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (info->key == key)
1351 return info;
1352 cur = info->next;
1353 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001354 return NULL;
1355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001357/* query the hash. allocate an entry if not found. */
1358static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1359 u32 key)
1360{
1361 struct hda_cache_head *info = get_hash(cache, key);
1362 if (!info) {
1363 u16 idx, cur;
1364 /* add a new hash entry */
1365 info = snd_array_new(&cache->buf);
1366 if (!info)
1367 return NULL;
1368 cur = snd_array_index(&cache->buf, info);
1369 info->key = key;
1370 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001371 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001372 idx = key % (u16)ARRAY_SIZE(cache->hash);
1373 info->next = cache->hash[idx];
1374 cache->hash[idx] = cur;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return info;
1377}
1378
Takashi Iwaid5191e52009-11-16 14:58:17 +01001379/**
1380 * query_amp_caps - query AMP capabilities
1381 * @codec: the HD-auio codec
1382 * @nid: the NID to query
1383 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1384 *
1385 * Query AMP capabilities for the given widget and direction.
1386 * Returns the obtained capability bits.
1387 *
1388 * When cap bits have been already read, this doesn't read again but
1389 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001391u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001393 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1394 nid = codec->core.afg;
1395 return snd_hda_param_read(codec, nid,
1396 direction == HDA_OUTPUT ?
1397 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001399EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Takashi Iwaid5191e52009-11-16 14:58:17 +01001401/**
David Henningsson861a04e2014-09-23 10:38:17 +02001402 * snd_hda_check_amp_caps - query AMP capabilities
1403 * @codec: the HD-audio codec
1404 * @nid: the NID to query
1405 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001406 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001407 *
1408 * Check whether the widget has the given amp capability for the direction.
1409 */
1410bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1411 int dir, unsigned int bits)
1412{
1413 if (!nid)
1414 return false;
1415 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1416 if (query_amp_caps(codec, nid, dir) & bits)
1417 return true;
1418 return false;
1419}
1420EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1421
1422/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001423 * snd_hda_override_amp_caps - Override the AMP capabilities
1424 * @codec: the CODEC to clean up
1425 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001426 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001427 * @caps: the capability bits to set
1428 *
1429 * Override the cached AMP caps bits value by the given one.
1430 * This function is useful if the driver needs to adjust the AMP ranges,
1431 * e.g. limit to 0dB, etc.
1432 *
1433 * Returns zero if successful or a negative error code.
1434 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001435int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1436 unsigned int caps)
1437{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001438 unsigned int parm;
1439
1440 snd_hda_override_wcaps(codec, nid,
1441 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1442 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1443 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001444}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001445EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001446
Takashi Iwaid5191e52009-11-16 14:58:17 +01001447/**
1448 * snd_hda_codec_amp_stereo - update the AMP stereo values
1449 * @codec: HD-audio codec
1450 * @nid: NID to read the AMP value
1451 * @direction: #HDA_INPUT or #HDA_OUTPUT
1452 * @idx: the index value (only for input direction)
1453 * @mask: bit mask to set
1454 * @val: the bits value to set
1455 *
1456 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1457 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001458 */
1459int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1460 int direction, int idx, int mask, int val)
1461{
1462 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001463
1464 if (snd_BUG_ON(mask & ~0xff))
1465 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001466 for (ch = 0; ch < 2; ch++)
1467 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1468 idx, mask, val);
1469 return ret;
1470}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001471EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001472
Takashi Iwai95a962c2014-10-29 16:03:58 +01001473/**
1474 * snd_hda_codec_amp_init - initialize the AMP value
1475 * @codec: the HDA codec
1476 * @nid: NID to read the AMP value
1477 * @ch: channel (left=0 or right=1)
1478 * @dir: #HDA_INPUT or #HDA_OUTPUT
1479 * @idx: the index value (only for input direction)
1480 * @mask: bit mask to set
1481 * @val: the bits value to set
1482 *
1483 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001484 * the first access. If the amp was already initialized / updated beforehand,
1485 * this does nothing.
1486 */
1487int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1488 int dir, int idx, int mask, int val)
1489{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001490 int orig;
1491
1492 if (!codec->core.regmap)
1493 return -EINVAL;
1494 regcache_cache_only(codec->core.regmap, true);
1495 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1496 regcache_cache_only(codec->core.regmap, false);
1497 if (orig >= 0)
1498 return 0;
1499 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001500}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001501EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001502
Takashi Iwai95a962c2014-10-29 16:03:58 +01001503/**
1504 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1505 * @codec: the HDA codec
1506 * @nid: NID to read the AMP value
1507 * @dir: #HDA_INPUT or #HDA_OUTPUT
1508 * @idx: the index value (only for input direction)
1509 * @mask: bit mask to set
1510 * @val: the bits value to set
1511 *
1512 * Call snd_hda_codec_amp_init() for both stereo channels.
1513 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001514int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1515 int dir, int idx, int mask, int val)
1516{
1517 int ch, ret = 0;
1518
1519 if (snd_BUG_ON(mask & ~0xff))
1520 mask &= 0xff;
1521 for (ch = 0; ch < 2; ch++)
1522 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1523 idx, mask, val);
1524 return ret;
1525}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001526EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001527
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001528static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1529 unsigned int ofs)
1530{
1531 u32 caps = query_amp_caps(codec, nid, dir);
1532 /* get num steps */
1533 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1534 if (ofs < caps)
1535 caps -= ofs;
1536 return caps;
1537}
1538
Takashi Iwaid5191e52009-11-16 14:58:17 +01001539/**
1540 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001541 * @kcontrol: referred ctl element
1542 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001543 *
1544 * The control element is supposed to have the private_value field
1545 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1546 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001547int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1551 u16 nid = get_amp_nid(kcontrol);
1552 u8 chs = get_amp_channels(kcontrol);
1553 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001554 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001556 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1557 uinfo->count = chs == 3 ? 2 : 1;
1558 uinfo->value.integer.min = 0;
1559 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1560 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001561 codec_warn(codec,
1562 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1563 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return -EINVAL;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 0;
1567}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001568EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001570
1571static inline unsigned int
1572read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1573 int ch, int dir, int idx, unsigned int ofs)
1574{
1575 unsigned int val;
1576 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1577 val &= HDA_AMP_VOLMASK;
1578 if (val >= ofs)
1579 val -= ofs;
1580 else
1581 val = 0;
1582 return val;
1583}
1584
1585static inline int
1586update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1587 int ch, int dir, int idx, unsigned int ofs,
1588 unsigned int val)
1589{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001590 unsigned int maxval;
1591
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001592 if (val > 0)
1593 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001594 /* ofs = 0: raw max value */
1595 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001596 if (val > maxval)
1597 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001598 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1599 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001600}
1601
Takashi Iwaid5191e52009-11-16 14:58:17 +01001602/**
1603 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001604 * @kcontrol: ctl element
1605 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001606 *
1607 * The control element is supposed to have the private_value field
1608 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1609 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001610int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1611 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1614 hda_nid_t nid = get_amp_nid(kcontrol);
1615 int chs = get_amp_channels(kcontrol);
1616 int dir = get_amp_direction(kcontrol);
1617 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001618 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 long *valp = ucontrol->value.integer.value;
1620
1621 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001622 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001624 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return 0;
1626}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001627EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Takashi Iwaid5191e52009-11-16 14:58:17 +01001629/**
1630 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001631 * @kcontrol: ctl element
1632 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001633 *
1634 * The control element is supposed to have the private_value field
1635 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1636 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001637int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1641 hda_nid_t nid = get_amp_nid(kcontrol);
1642 int chs = get_amp_channels(kcontrol);
1643 int dir = get_amp_direction(kcontrol);
1644 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001645 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 long *valp = ucontrol->value.integer.value;
1647 int change = 0;
1648
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001649 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001650 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001651 valp++;
1652 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001653 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001654 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return change;
1656}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001657EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Takashi Iwaid5191e52009-11-16 14:58:17 +01001659/**
1660 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001661 * @kcontrol: ctl element
1662 * @op_flag: operation flag
1663 * @size: byte size of input TLV
1664 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001665 *
1666 * The control element is supposed to have the private_value field
1667 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1668 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001669int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1670 unsigned int size, unsigned int __user *_tlv)
1671{
1672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1673 hda_nid_t nid = get_amp_nid(kcontrol);
1674 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001675 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001676 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001677 u32 caps, val1, val2;
1678
1679 if (size < 4 * sizeof(unsigned int))
1680 return -ENOMEM;
1681 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001682 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1683 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001684 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001685 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001686 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001687 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001688 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001689 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1690 return -EFAULT;
1691 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1692 return -EFAULT;
1693 if (put_user(val1, _tlv + 2))
1694 return -EFAULT;
1695 if (put_user(val2, _tlv + 3))
1696 return -EFAULT;
1697 return 0;
1698}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001699EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001700
Takashi Iwaid5191e52009-11-16 14:58:17 +01001701/**
1702 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1703 * @codec: HD-audio codec
1704 * @nid: NID of a reference widget
1705 * @dir: #HDA_INPUT or #HDA_OUTPUT
1706 * @tlv: TLV data to be stored, at least 4 elements
1707 *
1708 * Set (static) TLV data for a virtual master volume using the AMP caps
1709 * obtained from the reference NID.
1710 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001711 */
1712void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1713 unsigned int *tlv)
1714{
1715 u32 caps;
1716 int nums, step;
1717
1718 caps = query_amp_caps(codec, nid, dir);
1719 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1720 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1721 step = (step + 1) * 25;
1722 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1723 tlv[1] = 2 * sizeof(unsigned int);
1724 tlv[2] = -nums * step;
1725 tlv[3] = step;
1726}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001727EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001728
1729/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001730static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001731find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001732{
1733 struct snd_ctl_elem_id id;
1734 memset(&id, 0, sizeof(id));
1735 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001736 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001737 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001738 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1739 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001740 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001741 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001742}
1743
Takashi Iwaid5191e52009-11-16 14:58:17 +01001744/**
1745 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1746 * @codec: HD-audio codec
1747 * @name: ctl id name string
1748 *
1749 * Get the control element with the given id string and IFACE_MIXER.
1750 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001751struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1752 const char *name)
1753{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001754 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001756EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001757
Takashi Iwaidcda5802012-10-12 17:24:51 +02001758static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001759 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001760{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001761 int i, idx;
1762 /* 16 ctlrs should be large enough */
1763 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1764 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001765 return idx;
1766 }
1767 return -EBUSY;
1768}
1769
Takashi Iwaid5191e52009-11-16 14:58:17 +01001770/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001771 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772 * @codec: HD-audio codec
1773 * @nid: corresponding NID (optional)
1774 * @kctl: the control element to assign
1775 *
1776 * Add the given control element to an array inside the codec instance.
1777 * All control elements belonging to a codec are supposed to be added
1778 * by this function so that a proper clean-up works at the free or
1779 * reconfiguration time.
1780 *
1781 * If non-zero @nid is passed, the NID is assigned to the control element.
1782 * The assignment is shown in the codec proc file.
1783 *
1784 * snd_hda_ctl_add() checks the control subdev id field whether
1785 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001786 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1787 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001788 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001789int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1790 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001791{
1792 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001793 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001794 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001795
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001796 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001797 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001798 if (nid == 0)
1799 nid = get_amp_nid_(kctl->private_value);
1800 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001801 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1802 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001803 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001804 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001805 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001806 if (err < 0)
1807 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001808 item = snd_array_new(&codec->mixers);
1809 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001810 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001811 item->kctl = kctl;
1812 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001813 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001814 return 0;
1815}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001816EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001817
Takashi Iwaid5191e52009-11-16 14:58:17 +01001818/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001819 * snd_hda_add_nid - Assign a NID to a control element
1820 * @codec: HD-audio codec
1821 * @nid: corresponding NID (optional)
1822 * @kctl: the control element to assign
1823 * @index: index to kctl
1824 *
1825 * Add the given control element to an array inside the codec instance.
1826 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1827 * NID:KCTL mapping - for example "Capture Source" selector.
1828 */
1829int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1830 unsigned int index, hda_nid_t nid)
1831{
1832 struct hda_nid_item *item;
1833
1834 if (nid > 0) {
1835 item = snd_array_new(&codec->nids);
1836 if (!item)
1837 return -ENOMEM;
1838 item->kctl = kctl;
1839 item->index = index;
1840 item->nid = nid;
1841 return 0;
1842 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001843 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1844 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001845 return -EINVAL;
1846}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001847EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001848
1849/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001850 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1851 * @codec: HD-audio codec
1852 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001853void snd_hda_ctls_clear(struct hda_codec *codec)
1854{
1855 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001856 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001857 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001858 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001859 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001860 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001861}
1862
Takashi Iwai95a962c2014-10-29 16:03:58 +01001863/**
1864 * snd_hda_lock_devices - pseudo device locking
1865 * @bus: the BUS
1866 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001867 * toggle card->shutdown to allow/disallow the device access (as a hack)
1868 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001869int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001870{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001871 struct snd_card *card = bus->card;
1872 struct hda_codec *codec;
1873
Takashi Iwaia65d6292009-02-23 16:57:04 +01001874 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001875 if (card->shutdown)
1876 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001877 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001878 if (!list_empty(&card->ctl_files))
1879 goto err_clear;
1880
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001881 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001882 struct hda_pcm *cpcm;
1883 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001884 if (!cpcm->pcm)
1885 continue;
1886 if (cpcm->pcm->streams[0].substream_opened ||
1887 cpcm->pcm->streams[1].substream_opened)
1888 goto err_clear;
1889 }
1890 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001891 spin_unlock(&card->files_lock);
1892 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001893
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001894 err_clear:
1895 card->shutdown = 0;
1896 err_unlock:
1897 spin_unlock(&card->files_lock);
1898 return -EINVAL;
1899}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001900EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001901
Takashi Iwai95a962c2014-10-29 16:03:58 +01001902/**
1903 * snd_hda_unlock_devices - pseudo device unlocking
1904 * @bus: the BUS
1905 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001906void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001907{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001908 struct snd_card *card = bus->card;
1909
Takashi Iwaia65d6292009-02-23 16:57:04 +01001910 spin_lock(&card->files_lock);
1911 card->shutdown = 0;
1912 spin_unlock(&card->files_lock);
1913}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001914EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001915
Takashi Iwaid5191e52009-11-16 14:58:17 +01001916/**
1917 * snd_hda_codec_reset - Clear all objects assigned to the codec
1918 * @codec: HD-audio codec
1919 *
1920 * This frees the all PCM and control elements assigned to the codec, and
1921 * clears the caches and restores the pin default configurations.
1922 *
1923 * When a device is being used, it returns -EBSY. If successfully freed,
1924 * returns zero.
1925 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001926int snd_hda_codec_reset(struct hda_codec *codec)
1927{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001928 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001929
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001930 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001931 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001932
1933 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001934 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001935
Takashi Iwaia65d6292009-02-23 16:57:04 +01001936 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001937 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001938 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001939}
1940
Takashi Iwai6194b992014-06-06 18:12:16 +02001941typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001942
1943/* apply the function to all matching slave ctls in the mixer list */
1944static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001945 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001946{
1947 struct hda_nid_item *items;
1948 const char * const *s;
1949 int i, err;
1950
1951 items = codec->mixers.list;
1952 for (i = 0; i < codec->mixers.used; i++) {
1953 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001954 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001955 continue;
1956 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001957 char tmpname[sizeof(sctl->id.name)];
1958 const char *name = *s;
1959 if (suffix) {
1960 snprintf(tmpname, sizeof(tmpname), "%s %s",
1961 name, suffix);
1962 name = tmpname;
1963 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001964 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001965 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001966 if (err)
1967 return err;
1968 break;
1969 }
1970 }
1971 }
1972 return 0;
1973}
1974
Takashi Iwai6194b992014-06-06 18:12:16 +02001975static int check_slave_present(struct hda_codec *codec,
1976 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001977{
1978 return 1;
1979}
1980
Takashi Iwai18478e82012-03-09 17:51:10 +01001981/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001982static int get_kctl_0dB_offset(struct hda_codec *codec,
1983 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001984{
1985 int _tlv[4];
1986 const int *tlv = NULL;
1987 int val = -1;
1988
1989 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1990 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1991 mm_segment_t fs = get_fs();
1992 set_fs(get_ds());
1993 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1994 tlv = _tlv;
1995 set_fs(fs);
1996 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1997 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001998 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1999 int step = tlv[3];
2000 step &= ~TLV_DB_SCALE_MUTE;
2001 if (!step)
2002 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002003 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002004 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002005- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002006 return -1;
2007 }
2008 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002009 val = -tlv[2] / step;
2010 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002011 return val;
2012}
2013
2014/* call kctl->put with the given value(s) */
2015static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2016{
2017 struct snd_ctl_elem_value *ucontrol;
2018 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2019 if (!ucontrol)
2020 return -ENOMEM;
2021 ucontrol->value.integer.value[0] = val;
2022 ucontrol->value.integer.value[1] = val;
2023 kctl->put(kctl, ucontrol);
2024 kfree(ucontrol);
2025 return 0;
2026}
2027
2028/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002029static int init_slave_0dB(struct hda_codec *codec,
2030 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002031{
Takashi Iwai6194b992014-06-06 18:12:16 +02002032 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002033 if (offset > 0)
2034 put_kctl_with_value(slave, offset);
2035 return 0;
2036}
2037
2038/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002039static int init_slave_unmute(struct hda_codec *codec,
2040 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002041{
2042 return put_kctl_with_value(slave, 1);
2043}
2044
Takashi Iwaie8750942014-06-30 14:02:39 +02002045static int add_slave(struct hda_codec *codec,
2046 void *data, struct snd_kcontrol *slave)
2047{
2048 return snd_ctl_add_slave(data, slave);
2049}
2050
Takashi Iwaid5191e52009-11-16 14:58:17 +01002051/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002052 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002053 * @codec: HD-audio codec
2054 * @name: vmaster control name
2055 * @tlv: TLV data (optional)
2056 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002057 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002058 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002059 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002060 *
2061 * Create a virtual master control with the given name. The TLV data
2062 * must be either NULL or a valid data.
2063 *
2064 * @slaves is a NULL-terminated array of strings, each of which is a
2065 * slave control name. All controls with these names are assigned to
2066 * the new virtual master control.
2067 *
2068 * This function returns zero if successful or a negative error code.
2069 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002070int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002071 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002072 const char *suffix, bool init_slave_vol,
2073 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002074{
2075 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002076 int err;
2077
Takashi Iwai29e58532012-03-12 12:25:03 +01002078 if (ctl_ret)
2079 *ctl_ret = NULL;
2080
Takashi Iwai9322ca52012-02-03 14:28:01 +01002081 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002082 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002083 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002084 return 0;
2085 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002086 kctl = snd_ctl_make_virtual_master(name, tlv);
2087 if (!kctl)
2088 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002089 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002090 if (err < 0)
2091 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002092
Takashi Iwaie8750942014-06-30 14:02:39 +02002093 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002094 if (err < 0)
2095 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002096
2097 /* init with master mute & zero volume */
2098 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002099 if (init_slave_vol) {
2100 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002101 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002102 tlv ? init_slave_0dB : init_slave_unmute, &step);
2103 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002104
Takashi Iwai29e58532012-03-12 12:25:03 +01002105 if (ctl_ret)
2106 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002107 return 0;
2108}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002109EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002110
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002111/*
2112 * mute-LED control using vmaster
2113 */
2114static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_info *uinfo)
2116{
2117 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002118 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002119 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002120
Takashi Iwai3ff72212014-10-20 18:17:28 +02002121 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002122}
2123
2124static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2126{
2127 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2128 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2129 return 0;
2130}
2131
2132static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2133 struct snd_ctl_elem_value *ucontrol)
2134{
2135 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2136 unsigned int old_mode = hook->mute_mode;
2137
2138 hook->mute_mode = ucontrol->value.enumerated.item[0];
2139 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2140 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2141 if (old_mode == hook->mute_mode)
2142 return 0;
2143 snd_hda_sync_vmaster_hook(hook);
2144 return 1;
2145}
2146
2147static struct snd_kcontrol_new vmaster_mute_mode = {
2148 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2149 .name = "Mute-LED Mode",
2150 .info = vmaster_mute_mode_info,
2151 .get = vmaster_mute_mode_get,
2152 .put = vmaster_mute_mode_put,
2153};
2154
Takashi Iwai95a962c2014-10-29 16:03:58 +01002155/**
2156 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2157 * @codec: the HDA codec
2158 * @hook: the vmaster hook object
2159 * @expose_enum_ctl: flag to create an enum ctl
2160 *
2161 * Add a mute-LED hook with the given vmaster switch kctl.
2162 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2163 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002164 */
2165int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002166 struct hda_vmaster_mute_hook *hook,
2167 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002168{
2169 struct snd_kcontrol *kctl;
2170
2171 if (!hook->hook || !hook->sw_kctl)
2172 return 0;
2173 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2174 hook->codec = codec;
2175 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002176 if (!expose_enum_ctl)
2177 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002178 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2179 if (!kctl)
2180 return -ENOMEM;
2181 return snd_hda_ctl_add(codec, 0, kctl);
2182}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002183EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002184
Takashi Iwai95a962c2014-10-29 16:03:58 +01002185/**
2186 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2187 * @hook: the vmaster hook
2188 *
2189 * Call the hook with the current value for synchronization.
2190 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002191 */
2192void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2193{
2194 if (!hook->hook || !hook->codec)
2195 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002196 /* don't call vmaster hook in the destructor since it might have
2197 * been already destroyed
2198 */
2199 if (hook->codec->bus->shutdown)
2200 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002201 switch (hook->mute_mode) {
2202 case HDA_VMUTE_FOLLOW_MASTER:
2203 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2204 break;
2205 default:
2206 hook->hook(hook->codec, hook->mute_mode);
2207 break;
2208 }
2209}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002210EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002211
2212
Takashi Iwaid5191e52009-11-16 14:58:17 +01002213/**
2214 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002215 * @kcontrol: referred ctl element
2216 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002217 *
2218 * The control element is supposed to have the private_value field
2219 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2220 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002221int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2222 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int chs = get_amp_channels(kcontrol);
2225
2226 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2227 uinfo->count = chs == 3 ? 2 : 1;
2228 uinfo->value.integer.min = 0;
2229 uinfo->value.integer.max = 1;
2230 return 0;
2231}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002232EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Takashi Iwaid5191e52009-11-16 14:58:17 +01002234/**
2235 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002236 * @kcontrol: ctl element
2237 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002238 *
2239 * The control element is supposed to have the private_value field
2240 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2241 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002242int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2243 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2246 hda_nid_t nid = get_amp_nid(kcontrol);
2247 int chs = get_amp_channels(kcontrol);
2248 int dir = get_amp_direction(kcontrol);
2249 int idx = get_amp_index(kcontrol);
2250 long *valp = ucontrol->value.integer.value;
2251
2252 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002253 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002254 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002256 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002257 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
2259}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002260EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Takashi Iwaid5191e52009-11-16 14:58:17 +01002262/**
2263 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002264 * @kcontrol: ctl element
2265 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002266 *
2267 * The control element is supposed to have the private_value field
2268 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2269 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002270int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2274 hda_nid_t nid = get_amp_nid(kcontrol);
2275 int chs = get_amp_channels(kcontrol);
2276 int dir = get_amp_direction(kcontrol);
2277 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 long *valp = ucontrol->value.integer.value;
2279 int change = 0;
2280
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002281 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002282 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2283 HDA_AMP_MUTE,
2284 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002285 valp++;
2286 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002287 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002288 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2289 HDA_AMP_MUTE,
2290 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002291 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return change;
2293}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002294EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296/*
Takashi Iwai985be542005-11-02 18:26:49 +01002297 * bound volume controls
2298 *
2299 * bind multiple volumes (# indices, from 0)
2300 */
2301
2302#define AMP_VAL_IDX_SHIFT 19
2303#define AMP_VAL_IDX_MASK (0x0f<<19)
2304
Takashi Iwaid5191e52009-11-16 14:58:17 +01002305/**
2306 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002307 * @kcontrol: ctl element
2308 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002309 *
2310 * The control element is supposed to have the private_value field
2311 * set up via HDA_BIND_MUTE*() macros.
2312 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002313int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002315{
2316 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2317 unsigned long pval;
2318 int err;
2319
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002320 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002321 pval = kcontrol->private_value;
2322 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2323 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2324 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002325 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002326 return err;
2327}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002328EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002329
Takashi Iwaid5191e52009-11-16 14:58:17 +01002330/**
2331 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002332 * @kcontrol: ctl element
2333 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002334 *
2335 * The control element is supposed to have the private_value field
2336 * set up via HDA_BIND_MUTE*() macros.
2337 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002338int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002340{
2341 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2342 unsigned long pval;
2343 int i, indices, err = 0, change = 0;
2344
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002345 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002346 pval = kcontrol->private_value;
2347 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2348 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002349 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2350 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002351 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2352 if (err < 0)
2353 break;
2354 change |= err;
2355 }
2356 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002357 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002358 return err < 0 ? err : change;
2359}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002360EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002361
Takashi Iwaid5191e52009-11-16 14:58:17 +01002362/**
2363 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002364 * @kcontrol: referred ctl element
2365 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002366 *
2367 * The control element is supposed to have the private_value field
2368 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002369 */
2370int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_info *uinfo)
2372{
2373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2374 struct hda_bind_ctls *c;
2375 int err;
2376
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002377 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002378 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002379 kcontrol->private_value = *c->values;
2380 err = c->ops->info(kcontrol, uinfo);
2381 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002382 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002383 return err;
2384}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002385EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002386
Takashi Iwaid5191e52009-11-16 14:58:17 +01002387/**
2388 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002389 * @kcontrol: ctl element
2390 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002391 *
2392 * The control element is supposed to have the private_value field
2393 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2394 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002395int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_value *ucontrol)
2397{
2398 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2399 struct hda_bind_ctls *c;
2400 int err;
2401
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002402 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002403 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002404 kcontrol->private_value = *c->values;
2405 err = c->ops->get(kcontrol, ucontrol);
2406 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002407 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002408 return err;
2409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002410EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002411
Takashi Iwaid5191e52009-11-16 14:58:17 +01002412/**
2413 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002414 * @kcontrol: ctl element
2415 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002416 *
2417 * The control element is supposed to have the private_value field
2418 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2419 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002420int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2424 struct hda_bind_ctls *c;
2425 unsigned long *vals;
2426 int err = 0, change = 0;
2427
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002428 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002429 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002430 for (vals = c->values; *vals; vals++) {
2431 kcontrol->private_value = *vals;
2432 err = c->ops->put(kcontrol, ucontrol);
2433 if (err < 0)
2434 break;
2435 change |= err;
2436 }
2437 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002438 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002439 return err < 0 ? err : change;
2440}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002441EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002442
Takashi Iwaid5191e52009-11-16 14:58:17 +01002443/**
2444 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002445 * @kcontrol: ctl element
2446 * @op_flag: operation flag
2447 * @size: byte size of input TLV
2448 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002449 *
2450 * The control element is supposed to have the private_value field
2451 * set up via HDA_BIND_VOL() macro.
2452 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002453int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2454 unsigned int size, unsigned int __user *tlv)
2455{
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2457 struct hda_bind_ctls *c;
2458 int err;
2459
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002460 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002461 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002462 kcontrol->private_value = *c->values;
2463 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2464 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002465 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002466 return err;
2467}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002468EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002469
2470struct hda_ctl_ops snd_hda_bind_vol = {
2471 .info = snd_hda_mixer_amp_volume_info,
2472 .get = snd_hda_mixer_amp_volume_get,
2473 .put = snd_hda_mixer_amp_volume_put,
2474 .tlv = snd_hda_mixer_amp_tlv
2475};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002476EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002477
2478struct hda_ctl_ops snd_hda_bind_sw = {
2479 .info = snd_hda_mixer_amp_switch_info,
2480 .get = snd_hda_mixer_amp_switch_get,
2481 .put = snd_hda_mixer_amp_switch_put,
2482 .tlv = snd_hda_mixer_amp_tlv
2483};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002484EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002485
2486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 * SPDIF out controls
2488 */
2489
Takashi Iwai0ba21762007-04-16 11:29:14 +02002490static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
2493 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2494 uinfo->count = 1;
2495 return 0;
2496}
2497
Takashi Iwai0ba21762007-04-16 11:29:14 +02002498static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2499 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2502 IEC958_AES0_NONAUDIO |
2503 IEC958_AES0_CON_EMPHASIS_5015 |
2504 IEC958_AES0_CON_NOT_COPYRIGHT;
2505 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2506 IEC958_AES1_CON_ORIGINAL;
2507 return 0;
2508}
2509
Takashi Iwai0ba21762007-04-16 11:29:14 +02002510static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2514 IEC958_AES0_NONAUDIO |
2515 IEC958_AES0_PRO_EMPHASIS_5015;
2516 return 0;
2517}
2518
Takashi Iwai0ba21762007-04-16 11:29:14 +02002519static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002523 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002524 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002526 mutex_lock(&codec->spdif_mutex);
2527 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002528 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2529 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2530 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2531 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002532 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 return 0;
2535}
2536
2537/* convert from SPDIF status bits to HDA SPDIF bits
2538 * bit 0 (DigEn) is always set zero (to be filled later)
2539 */
2540static unsigned short convert_from_spdif_status(unsigned int sbits)
2541{
2542 unsigned short val = 0;
2543
2544 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002545 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002547 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002549 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2550 IEC958_AES0_PRO_EMPHASIS_5015)
2551 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002553 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2554 IEC958_AES0_CON_EMPHASIS_5015)
2555 val |= AC_DIG1_EMPHASIS;
2556 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2557 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002559 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2561 }
2562 return val;
2563}
2564
2565/* convert to SPDIF status bits from HDA SPDIF bits
2566 */
2567static unsigned int convert_to_spdif_status(unsigned short val)
2568{
2569 unsigned int sbits = 0;
2570
Takashi Iwai0ba21762007-04-16 11:29:14 +02002571 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002573 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 sbits |= IEC958_AES0_PROFESSIONAL;
2575 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002576 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2578 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002579 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002581 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2585 sbits |= val & (0x7f << 8);
2586 }
2587 return sbits;
2588}
2589
Takashi Iwai2f728532008-09-25 16:32:41 +02002590/* set digital convert verbs both for the given NID and its slaves */
2591static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2592 int verb, int val)
2593{
Takashi Iwaidda14412011-05-02 11:29:30 +02002594 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002595
Takashi Iwai9e976972008-11-25 08:17:20 +01002596 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002597 d = codec->slave_dig_outs;
2598 if (!d)
2599 return;
2600 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002601 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002602}
2603
2604static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2605 int dig1, int dig2)
2606{
2607 if (dig1 != -1)
2608 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2609 if (dig2 != -1)
2610 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2611}
2612
Takashi Iwai0ba21762007-04-16 11:29:14 +02002613static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002617 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002618 struct hda_spdif_out *spdif;
2619 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 unsigned short val;
2621 int change;
2622
Ingo Molnar62932df2006-01-16 16:34:20 +01002623 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002624 spdif = snd_array_elem(&codec->spdif_out, idx);
2625 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002626 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2628 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2629 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002630 val = convert_from_spdif_status(spdif->status);
2631 val |= spdif->ctls & 1;
2632 change = spdif->ctls != val;
2633 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002634 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002635 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002636 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return change;
2638}
2639
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002640#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Takashi Iwai0ba21762007-04-16 11:29:14 +02002642static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2643 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002646 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002647 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002649 mutex_lock(&codec->spdif_mutex);
2650 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002651 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002652 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
2654}
2655
Stephen Warren74b654c2011-06-01 11:14:18 -06002656static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2657 int dig1, int dig2)
2658{
2659 set_dig_out_convert(codec, nid, dig1, dig2);
2660 /* unmute amp switch (if any) */
2661 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2662 (dig1 & AC_DIG1_ENABLE))
2663 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2664 HDA_AMP_MUTE, 0);
2665}
2666
Takashi Iwai0ba21762007-04-16 11:29:14 +02002667static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002671 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002672 struct hda_spdif_out *spdif;
2673 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 unsigned short val;
2675 int change;
2676
Ingo Molnar62932df2006-01-16 16:34:20 +01002677 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002678 spdif = snd_array_elem(&codec->spdif_out, idx);
2679 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002680 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002683 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002684 spdif->ctls = val;
2685 if (change && nid != (u16)-1)
2686 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002687 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return change;
2689}
2690
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002691static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 {
2693 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002695 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 .info = snd_hda_spdif_mask_info,
2697 .get = snd_hda_spdif_cmask_get,
2698 },
2699 {
2700 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002702 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .info = snd_hda_spdif_mask_info,
2704 .get = snd_hda_spdif_pmask_get,
2705 },
2706 {
2707 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002708 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 .info = snd_hda_spdif_mask_info,
2710 .get = snd_hda_spdif_default_get,
2711 .put = snd_hda_spdif_default_put,
2712 },
2713 {
2714 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002715 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .info = snd_hda_spdif_out_switch_info,
2717 .get = snd_hda_spdif_out_switch_get,
2718 .put = snd_hda_spdif_out_switch_put,
2719 },
2720 { } /* end */
2721};
2722
2723/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002724 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002726 * @associated_nid: NID that new ctls associated with
2727 * @cvt_nid: converter NID
2728 * @type: HDA_PCM_TYPE_*
2729 * Creates controls related with the digital output.
2730 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 *
2732 * Returns 0 if successful, or a negative error code.
2733 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002734int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2735 hda_nid_t associated_nid,
2736 hda_nid_t cvt_nid,
2737 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002740 struct snd_kcontrol *kctl;
2741 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002742 int idx = 0;
2743 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06002744 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002745 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002747 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002748 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002749 idx = spdif_index;
2750 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002751 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002752 /* suppose a single SPDIF device */
2753 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2754 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2755 if (!kctl)
2756 break;
2757 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002758 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002759 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002760 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002761 if (!bus->primary_dig_out_type)
2762 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002763
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002764 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002765 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002766 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002767 return -EBUSY;
2768 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002769 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002770 if (!spdif)
2771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2773 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002774 if (!kctl)
2775 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002776 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002777 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002778 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002779 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return err;
2781 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002782 spdif->nid = cvt_nid;
2783 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002784 AC_VERB_GET_DIGI_CONVERT_1, 0);
2785 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002788EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Takashi Iwai95a962c2014-10-29 16:03:58 +01002790/**
2791 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2792 * @codec: the HDA codec
2793 * @nid: widget NID
2794 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002795 * call within spdif_mutex lock
2796 */
Stephen Warren7c9359762011-06-01 11:14:17 -06002797struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2798 hda_nid_t nid)
2799{
2800 int i;
2801 for (i = 0; i < codec->spdif_out.used; i++) {
2802 struct hda_spdif_out *spdif =
2803 snd_array_elem(&codec->spdif_out, i);
2804 if (spdif->nid == nid)
2805 return spdif;
2806 }
2807 return NULL;
2808}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002809EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06002810
Takashi Iwai95a962c2014-10-29 16:03:58 +01002811/**
2812 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2813 * @codec: the HDA codec
2814 * @idx: the SPDIF ctl index
2815 *
2816 * Unassign the widget from the given SPDIF control.
2817 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002818void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2819{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002820 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002821
2822 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002823 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002824 spdif->nid = (u16)-1;
2825 mutex_unlock(&codec->spdif_mutex);
2826}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002827EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002828
Takashi Iwai95a962c2014-10-29 16:03:58 +01002829/**
2830 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2831 * @codec: the HDA codec
2832 * @idx: the SPDIF ctl idx
2833 * @nid: widget NID
2834 *
2835 * Assign the widget to the SPDIF control with the given index.
2836 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002837void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2838{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002839 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002840 unsigned short val;
2841
2842 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002843 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002844 if (spdif->nid != nid) {
2845 spdif->nid = nid;
2846 val = spdif->ctls;
2847 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2848 }
2849 mutex_unlock(&codec->spdif_mutex);
2850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002851EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002854 * SPDIF sharing with analog output
2855 */
2856static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
2859 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2860 ucontrol->value.integer.value[0] = mout->share_spdif;
2861 return 0;
2862}
2863
2864static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2866{
2867 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2868 mout->share_spdif = !!ucontrol->value.integer.value[0];
2869 return 0;
2870}
2871
2872static struct snd_kcontrol_new spdif_share_sw = {
2873 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2874 .name = "IEC958 Default PCM Playback Switch",
2875 .info = snd_ctl_boolean_mono_info,
2876 .get = spdif_share_sw_get,
2877 .put = spdif_share_sw_put,
2878};
2879
Takashi Iwaid5191e52009-11-16 14:58:17 +01002880/**
2881 * snd_hda_create_spdif_share_sw - create Default PCM switch
2882 * @codec: the HDA codec
2883 * @mout: multi-out instance
2884 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002885int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2886 struct hda_multi_out *mout)
2887{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002888 struct snd_kcontrol *kctl;
2889
Takashi Iwai9a081602008-02-12 18:37:26 +01002890 if (!mout->dig_out_nid)
2891 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002892
2893 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2894 if (!kctl)
2895 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002896 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002897 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002898}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002899EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002900
2901/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 * SPDIF input
2903 */
2904
2905#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2906
Takashi Iwai0ba21762007-04-16 11:29:14 +02002907static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2911
2912 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2913 return 0;
2914}
2915
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2917 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2920 hda_nid_t nid = kcontrol->private_value;
2921 unsigned int val = !!ucontrol->value.integer.value[0];
2922 int change;
2923
Ingo Molnar62932df2006-01-16 16:34:20 +01002924 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002926 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002928 snd_hda_codec_write_cache(codec, nid, 0,
2929 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002931 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return change;
2933}
2934
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2936 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2939 hda_nid_t nid = kcontrol->private_value;
2940 unsigned short val;
2941 unsigned int sbits;
2942
Andrew Paprocki3982d172007-12-19 12:13:44 +01002943 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 sbits = convert_to_spdif_status(val);
2945 ucontrol->value.iec958.status[0] = sbits;
2946 ucontrol->value.iec958.status[1] = sbits >> 8;
2947 ucontrol->value.iec958.status[2] = sbits >> 16;
2948 ucontrol->value.iec958.status[3] = sbits >> 24;
2949 return 0;
2950}
2951
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002952static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 {
2954 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002955 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 .info = snd_hda_spdif_in_switch_info,
2957 .get = snd_hda_spdif_in_switch_get,
2958 .put = snd_hda_spdif_in_switch_put,
2959 },
2960 {
2961 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002963 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .info = snd_hda_spdif_mask_info,
2965 .get = snd_hda_spdif_in_status_get,
2966 },
2967 { } /* end */
2968};
2969
2970/**
2971 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2972 * @codec: the HDA codec
2973 * @nid: audio in widget NID
2974 *
2975 * Creates controls related with the SPDIF input.
2976 * Called from each patch supporting the SPDIF in.
2977 *
2978 * Returns 0 if successful, or a negative error code.
2979 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002980int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002983 struct snd_kcontrol *kctl;
2984 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002985 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Takashi Iwaidcda5802012-10-12 17:24:51 +02002987 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002988 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002989 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002990 return -EBUSY;
2991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2993 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002994 if (!kctl)
2995 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002997 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002998 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return err;
3000 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003002 snd_hda_codec_read(codec, nid, 0,
3003 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003004 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return 0;
3006}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003007EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003009/*
3010 * command cache
3011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Takashi Iwaic370dd62012-12-13 18:30:04 +01003013/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003014#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3015#define get_cmd_cache_nid(key) ((key) & 0xff)
3016#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3017
3018/**
3019 * snd_hda_codec_write_cache - send a single command with caching
3020 * @codec: the HDA codec
3021 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003022 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003023 * @verb: the verb to send
3024 * @parm: the parameter for the verb
3025 *
3026 * Send a single command without waiting for response.
3027 *
3028 * Returns 0 if successful, or a negative error code.
3029 */
3030int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003031 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003032{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003033 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003034 struct hda_cache_head *c;
3035 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003036 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003037
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003038 cache_only = codec->cached_write;
3039 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003040 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003041 if (err < 0)
3042 return err;
3043 }
3044
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003045 /* parm may contain the verb stuff for get/set amp */
3046 verb = verb | (parm >> 8);
3047 parm &= 0xff;
3048 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003049 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003050 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003051 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003052 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003053 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003054 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003055 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003056 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003057}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003058EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003059
Takashi Iwaid5191e52009-11-16 14:58:17 +01003060/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003061 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3062 * @codec: the HDA codec
3063 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003064 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003065 * @verb: the verb to send
3066 * @parm: the parameter for the verb
3067 *
3068 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3069 * command if the parameter is already identical with the cached value.
3070 * If not, it sends the command and refreshes the cache.
3071 *
3072 * Returns 0 if successful, or a negative error code.
3073 */
3074int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003075 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003076{
3077 struct hda_cache_head *c;
3078 u32 key;
3079
3080 /* parm may contain the verb stuff for get/set amp */
3081 verb = verb | (parm >> 8);
3082 parm &= 0xff;
3083 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003084 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003085 c = get_hash(&codec->cmd_cache, key);
3086 if (c && c->val == parm) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003087 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003088 return 0;
3089 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003090 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003091 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003092}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003093EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003094
3095/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003096 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3097 * @codec: HD-audio codec
3098 *
3099 * Execute all verbs recorded in the command caches to resume.
3100 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003101void snd_hda_codec_resume_cache(struct hda_codec *codec)
3102{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003103 int i;
3104
Takashi Iwaic370dd62012-12-13 18:30:04 +01003105 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003106 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003107 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3108 struct hda_cache_head *buffer;
3109 u32 key;
3110
3111 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3112 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003113 if (!key)
3114 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003115 if (!buffer->dirty)
3116 continue;
3117 buffer->dirty = 0;
3118 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003119 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3120 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003121 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003122 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003123 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003124}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003125EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003126
3127/**
3128 * snd_hda_sequence_write_cache - sequence writes with caching
3129 * @codec: the HDA codec
3130 * @seq: VERB array to send
3131 *
3132 * Send the commands sequentially from the given array.
3133 * Thte commands are recorded on cache for power-save and resume.
3134 * The array must be terminated with NID=0.
3135 */
3136void snd_hda_sequence_write_cache(struct hda_codec *codec,
3137 const struct hda_verb *seq)
3138{
3139 for (; seq->nid; seq++)
3140 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3141 seq->param);
3142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003143EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003144
Takashi Iwaidc870f32013-01-22 15:24:30 +01003145/**
3146 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3147 * @codec: HD-audio codec
3148 */
3149void snd_hda_codec_flush_cache(struct hda_codec *codec)
3150{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003151 if (codec->core.regmap)
3152 regcache_sync(codec->core.regmap);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003153 snd_hda_codec_resume_cache(codec);
3154}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003155EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003156
Takashi Iwai95a962c2014-10-29 16:03:58 +01003157/**
3158 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3159 * @codec: the HDA codec
3160 * @fg: function group (not used now)
3161 * @power_state: the power state to set (AC_PWRST_*)
3162 *
3163 * Set the given power state to all widgets that have the power control.
3164 * If the codec has power_filter set, it evaluates the power state and
3165 * filter out if it's unchanged as D3.
3166 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003167void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003168 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003169{
Takashi Iwai7639a062015-03-03 10:07:24 +01003170 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01003171
Takashi Iwai7639a062015-03-03 10:07:24 +01003172 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003173 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003174 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003175 if (!(wcaps & AC_WCAP_POWER))
3176 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003177 if (codec->power_filter) {
3178 state = codec->power_filter(codec, nid, power_state);
3179 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003180 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003181 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003182 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003183 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003184 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003185}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003186EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003187
3188/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003189 * wait until the state is reached, returns the current state
3190 */
3191static unsigned int hda_sync_power_state(struct hda_codec *codec,
3192 hda_nid_t fg,
3193 unsigned int power_state)
3194{
3195 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3196 unsigned int state, actual_state;
3197
3198 for (;;) {
3199 state = snd_hda_codec_read(codec, fg, 0,
3200 AC_VERB_GET_POWER_STATE, 0);
3201 if (state & AC_PWRST_ERROR)
3202 break;
3203 actual_state = (state >> 4) & 0x0f;
3204 if (actual_state == power_state)
3205 break;
3206 if (time_after_eq(jiffies, end_time))
3207 break;
3208 /* wait until the codec reachs to the target state */
3209 msleep(1);
3210 }
3211 return state;
3212}
3213
Takashi Iwai95a962c2014-10-29 16:03:58 +01003214/**
3215 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3216 * @codec: the HDA codec
3217 * @nid: widget NID
3218 * @power_state: power state to evalue
3219 *
3220 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3221 * This can be used a codec power_filter callback.
3222 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003223unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3224 hda_nid_t nid,
3225 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003226{
Takashi Iwai7639a062015-03-03 10:07:24 +01003227 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003228 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003229 if (power_state == AC_PWRST_D3 &&
3230 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3231 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3232 int eapd = snd_hda_codec_read(codec, nid, 0,
3233 AC_VERB_GET_EAPD_BTLENABLE, 0);
3234 if (eapd & 0x02)
3235 return AC_PWRST_D0;
3236 }
3237 return power_state;
3238}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003239EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003240
Takashi Iwai432c6412012-08-28 09:59:20 -07003241/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003242 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003243 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003244static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003245 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003246{
Takashi Iwai7639a062015-03-03 10:07:24 +01003247 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003248 int count;
3249 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003250 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003251
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003252 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003253 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003254 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01003255 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05003256 else if (codec->depop_delay > 0)
3257 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003258 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003259 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003260
3261 /* repeat power states setting at most 10 times*/
3262 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003263 if (codec->patch_ops.set_power_state)
3264 codec->patch_ops.set_power_state(codec, fg,
3265 power_state);
3266 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003267 state = power_state;
3268 if (codec->power_filter)
3269 state = codec->power_filter(codec, fg, state);
3270 if (state == power_state || power_state != AC_PWRST_D3)
3271 snd_hda_codec_read(codec, fg, flags,
3272 AC_VERB_SET_POWER_STATE,
3273 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003274 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003275 }
3276 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003277 if (!(state & AC_PWRST_ERROR))
3278 break;
3279 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003280
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003281 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003282}
Takashi Iwai54d17402005-11-21 16:33:22 +01003283
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003284/* sync power states of all widgets;
3285 * this is called at the end of codec parsing
3286 */
3287static void sync_power_up_states(struct hda_codec *codec)
3288{
Takashi Iwai7639a062015-03-03 10:07:24 +01003289 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003290
Takashi Iwaiba615b82013-03-13 14:47:21 +01003291 /* don't care if no filter is used */
3292 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003293 return;
3294
Takashi Iwai7639a062015-03-03 10:07:24 +01003295 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003296 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003297 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003298 if (!(wcaps & AC_WCAP_POWER))
3299 continue;
3300 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3301 if (target == AC_PWRST_D0)
3302 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003303 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003304 snd_hda_codec_write(codec, nid, 0,
3305 AC_VERB_SET_POWER_STATE, target);
3306 }
3307}
3308
Takashi Iwai648a8d22014-02-25 10:38:13 +01003309#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003310/* execute additional init verbs */
3311static void hda_exec_init_verbs(struct hda_codec *codec)
3312{
3313 if (codec->init_verbs.list)
3314 snd_hda_sequence_write(codec, codec->init_verbs.list);
3315}
3316#else
3317static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3318#endif
3319
Takashi Iwai2a439522011-07-26 09:52:50 +02003320#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003321/* update the power on/off account with the current jiffies */
3322static void update_power_acct(struct hda_codec *codec, bool on)
3323{
3324 unsigned long delta = jiffies - codec->power_jiffies;
3325
3326 if (on)
3327 codec->power_on_acct += delta;
3328 else
3329 codec->power_off_acct += delta;
3330 codec->power_jiffies += delta;
3331}
3332
3333void snd_hda_update_power_acct(struct hda_codec *codec)
3334{
3335 update_power_acct(codec, hda_codec_is_power_on(codec));
3336}
3337
Takashi Iwaicb53c622007-08-10 17:21:45 +02003338/*
3339 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003340 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003341 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003342static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003343{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003344 unsigned int state;
3345
Takashi Iwai7639a062015-03-03 10:07:24 +01003346 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003347
Takashi Iwaicb53c622007-08-10 17:21:45 +02003348 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003349 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003350 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003351 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003352 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003353 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003354 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003355}
3356
Takashi Iwaic370dd62012-12-13 18:30:04 +01003357/* mark all entries of cmd and amp caches dirty */
3358static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3359{
3360 int i;
3361 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3362 struct hda_cache_head *cmd;
3363 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3364 cmd->dirty = 1;
3365 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003366}
3367
Takashi Iwaicb53c622007-08-10 17:21:45 +02003368/*
3369 * kick up codec; used both from PM and power-save
3370 */
3371static void hda_call_codec_resume(struct hda_codec *codec)
3372{
Takashi Iwai7639a062015-03-03 10:07:24 +01003373 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003374
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003375 if (codec->core.regmap)
3376 regcache_mark_dirty(codec->core.regmap);
3377
Takashi Iwaic370dd62012-12-13 18:30:04 +01003378 hda_mark_cmd_cache_dirty(codec);
3379
Takashi Iwaicc72da72015-02-19 16:00:22 +01003380 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003381
Takashi Iwaid8193872012-08-31 07:54:38 -07003382 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003383 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003384 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003385 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003386 if (codec->patch_ops.resume)
3387 codec->patch_ops.resume(codec);
3388 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003389 if (codec->patch_ops.init)
3390 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003391 if (codec->core.regmap)
3392 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003393 snd_hda_codec_resume_cache(codec);
3394 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003395
3396 if (codec->jackpoll_interval)
3397 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003398 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003399 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003400 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003401}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003402
Takashi Iwaicc72da72015-02-19 16:00:22 +01003403static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003404{
3405 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003406 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003407 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003408
3409 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003410 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3411 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003412 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003413 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3414 (state & AC_PWRST_CLK_STOP_OK))
3415 snd_hdac_codec_link_down(&codec->core);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003416 return 0;
3417}
3418
Takashi Iwaicc72da72015-02-19 16:00:22 +01003419static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003420{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003421 struct hda_codec *codec = dev_to_hda_codec(dev);
3422
Takashi Iwai7639a062015-03-03 10:07:24 +01003423 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003424 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003425 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003426 return 0;
3427}
Takashi Iwai2a439522011-07-26 09:52:50 +02003428#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003429
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003430/* referred in hda_bind.c */
3431const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003432 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3433 pm_runtime_force_resume)
3434 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3435 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003436};
Takashi Iwai54d17402005-11-21 16:33:22 +01003437
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003438/*
3439 * add standard channel maps if not specified
3440 */
3441static int add_std_chmaps(struct hda_codec *codec)
3442{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003443 struct hda_pcm *pcm;
3444 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003445
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003446 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003447 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003448 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003449 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003450 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003451
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003452 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003453 continue;
3454 if (!pcm || !hinfo->substreams)
3455 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003456 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003457 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003458 hinfo->channels_max,
3459 0, &chmap);
3460 if (err < 0)
3461 return err;
3462 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3463 }
3464 }
3465 return 0;
3466}
3467
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003468/* default channel maps for 2.1 speakers;
3469 * since HD-audio supports only stereo, odd number channels are omitted
3470 */
3471const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3472 { .channels = 2,
3473 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3474 { .channels = 4,
3475 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3476 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3477 { }
3478};
3479EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3480
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003481int snd_hda_codec_build_controls(struct hda_codec *codec)
3482{
3483 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003484 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003485 /* continue to initialize... */
3486 if (codec->patch_ops.init)
3487 err = codec->patch_ops.init(codec);
3488 if (!err && codec->patch_ops.build_controls)
3489 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003490 if (err < 0)
3491 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003492
3493 /* we create chmaps here instead of build_pcms */
3494 err = add_std_chmaps(codec);
3495 if (err < 0)
3496 return err;
3497
David Henningsson26a6cb62012-10-09 15:04:21 +02003498 if (codec->jackpoll_interval)
3499 hda_jackpoll_work(&codec->jackpoll_work.work);
3500 else
3501 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003502 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return 0;
3504}
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506/*
3507 * stream formats
3508 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003509struct hda_rate_tbl {
3510 unsigned int hz;
3511 unsigned int alsa_bits;
3512 unsigned int hda_fmt;
3513};
3514
Takashi Iwai92f10b32010-08-03 14:21:00 +02003515/* rate = base * mult / div */
3516#define HDA_RATE(base, mult, div) \
3517 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3518 (((div) - 1) << AC_FMT_DIV_SHIFT))
3519
Takashi Iwaibefdf312005-08-22 13:57:55 +02003520static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003522
3523 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003524 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3525 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3526 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3527 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3528 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3529 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3530 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3531 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3532 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3533 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3534 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003535#define AC_PAR_PCM_RATE_BITS 11
3536 /* up to bits 10, 384kHZ isn't supported properly */
3537
3538 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003539 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003540
Takashi Iwaibefdf312005-08-22 13:57:55 +02003541 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542};
3543
3544/**
3545 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02003546 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 * @rate: the sample rate
3548 * @channels: the number of channels
3549 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3550 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003551 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 *
3553 * Calculate the format bitset from the given rate, channels and th PCM format.
3554 *
3555 * Return zero if invalid.
3556 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003557unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3558 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 unsigned int channels,
3560 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003561 unsigned int maxbps,
3562 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 int i;
3565 unsigned int val = 0;
3566
Takashi Iwaibefdf312005-08-22 13:57:55 +02003567 for (i = 0; rate_bits[i].hz; i++)
3568 if (rate_bits[i].hz == rate) {
3569 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 break;
3571 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003572 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003573 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return 0;
3575 }
3576
3577 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003578 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return 0;
3580 }
3581 val |= channels - 1;
3582
3583 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003584 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003585 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003586 break;
3587 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003588 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 case 20:
3591 case 24:
3592 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003593 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003594 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003596 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003598 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 break;
3600 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02003601 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01003602 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return 0;
3604 }
3605
Anssi Hannula32c168c2010-08-03 13:28:57 +03003606 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003607 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return val;
3610}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003611EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003613static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003614{
3615 unsigned int val = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +01003616 if (nid != codec->core.afg &&
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003617 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3618 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3619 if (!val || val == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003620 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003621 if (!val || val == -1)
3622 return 0;
3623 return val;
3624}
3625
Takashi Iwaifaa75f82015-02-26 08:54:56 +01003626static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003627{
3628 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3629 if (!streams || streams == -1)
Takashi Iwai7639a062015-03-03 10:07:24 +01003630 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003631 if (!streams || streams == -1)
3632 return 0;
3633 return streams;
3634}
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636/**
3637 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3638 * @codec: the HDA codec
3639 * @nid: NID to query
3640 * @ratesp: the pointer to store the detected rate bitflags
3641 * @formatsp: the pointer to store the detected formats
3642 * @bpsp: the pointer to store the detected format widths
3643 *
3644 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3645 * or @bsps argument is ignored.
3646 *
3647 * Returns 0 if successful, otherwise a negative error code.
3648 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003649int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3651{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003652 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003654 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003655 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 if (ratesp) {
3658 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003659 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003661 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003663 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003664 codec_err(codec,
3665 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3666 nid, val,
3667 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003668 return -EIO;
3669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 *ratesp = rates;
3671 }
3672
3673 if (formatsp || bpsp) {
3674 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003675 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003677 streams = query_stream_param(codec, nid);
3678 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 bps = 0;
3682 if (streams & AC_SUPFMT_PCM) {
3683 if (val & AC_SUPPCM_BITS_8) {
3684 formats |= SNDRV_PCM_FMTBIT_U8;
3685 bps = 8;
3686 }
3687 if (val & AC_SUPPCM_BITS_16) {
3688 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3689 bps = 16;
3690 }
3691 if (wcaps & AC_WCAP_DIGITAL) {
3692 if (val & AC_SUPPCM_BITS_32)
3693 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3694 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3695 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3696 if (val & AC_SUPPCM_BITS_24)
3697 bps = 24;
3698 else if (val & AC_SUPPCM_BITS_20)
3699 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003700 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3701 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3703 if (val & AC_SUPPCM_BITS_32)
3704 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 else if (val & AC_SUPPCM_BITS_24)
3706 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003707 else if (val & AC_SUPPCM_BITS_20)
3708 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 }
3710 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003711#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003712 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003714 if (!bps)
3715 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003716 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003717#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003718 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003719 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 /* temporary hack: we have still no proper support
3721 * for the direct AC3 stream...
3722 */
3723 formats |= SNDRV_PCM_FMTBIT_U8;
3724 bps = 8;
3725 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003726 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003727 codec_err(codec,
3728 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3729 nid, val,
3730 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3731 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003732 return -EIO;
3733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (formatsp)
3735 *formatsp = formats;
3736 if (bpsp)
3737 *bpsp = bps;
3738 }
3739
3740 return 0;
3741}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003742EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003745 * snd_hda_is_supported_format - Check the validity of the format
3746 * @codec: HD-audio codec
3747 * @nid: NID to check
3748 * @format: the HD-audio format value to check
3749 *
3750 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 *
3752 * Returns 1 if supported, 0 if not.
3753 */
3754int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3755 unsigned int format)
3756{
3757 int i;
3758 unsigned int val = 0, rate, stream;
3759
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003760 val = query_pcm_param(codec, nid);
3761 if (!val)
3762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003765 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003766 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (val & (1 << i))
3768 break;
3769 return 0;
3770 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003771 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return 0;
3773
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003774 stream = query_stream_param(codec, nid);
3775 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 return 0;
3777
3778 if (stream & AC_SUPFMT_PCM) {
3779 switch (format & 0xf0) {
3780 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003781 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return 0;
3783 break;
3784 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003785 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return 0;
3787 break;
3788 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003789 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return 0;
3791 break;
3792 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003793 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return 0;
3795 break;
3796 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003797 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return 0;
3799 break;
3800 default:
3801 return 0;
3802 }
3803 } else {
3804 /* FIXME: check for float32 and AC3? */
3805 }
3806
3807 return 1;
3808}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003809EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811/*
3812 * PCM stuff
3813 */
3814static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3815 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003816 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 return 0;
3819}
3820
3821static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3822 struct hda_codec *codec,
3823 unsigned int stream_tag,
3824 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003825 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
3827 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3828 return 0;
3829}
3830
3831static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3832 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003833 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Takashi Iwai888afa12008-03-18 09:57:50 +01003835 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return 0;
3837}
3838
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003839static int set_pcm_default_values(struct hda_codec *codec,
3840 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003842 int err;
3843
Takashi Iwai0ba21762007-04-16 11:29:14 +02003844 /* query support PCM information from the given NID */
3845 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003846 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003847 info->rates ? NULL : &info->rates,
3848 info->formats ? NULL : &info->formats,
3849 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003850 if (err < 0)
3851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853 if (info->ops.open == NULL)
3854 info->ops.open = hda_pcm_default_open_close;
3855 if (info->ops.close == NULL)
3856 info->ops.close = hda_pcm_default_open_close;
3857 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003858 if (snd_BUG_ON(!info->nid))
3859 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 info->ops.prepare = hda_pcm_default_prepare;
3861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003863 if (snd_BUG_ON(!info->nid))
3864 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 info->ops.cleanup = hda_pcm_default_cleanup;
3866 }
3867 return 0;
3868}
3869
Takashi Iwaieb541332010-08-06 13:48:11 +02003870/*
3871 * codec prepare/cleanup entries
3872 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003873/**
3874 * snd_hda_codec_prepare - Prepare a stream
3875 * @codec: the HDA codec
3876 * @hinfo: PCM information
3877 * @stream: stream tag to assign
3878 * @format: format id to assign
3879 * @substream: PCM substream to assign
3880 *
3881 * Calls the prepare callback set by the codec with the given arguments.
3882 * Clean up the inactive streams when successful.
3883 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003884int snd_hda_codec_prepare(struct hda_codec *codec,
3885 struct hda_pcm_stream *hinfo,
3886 unsigned int stream,
3887 unsigned int format,
3888 struct snd_pcm_substream *substream)
3889{
3890 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003891 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003892 if (hinfo->ops.prepare)
3893 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3894 substream);
3895 else
3896 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003897 if (ret >= 0)
3898 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003899 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003900 return ret;
3901}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003902EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003903
Takashi Iwai95a962c2014-10-29 16:03:58 +01003904/**
3905 * snd_hda_codec_cleanup - Prepare a stream
3906 * @codec: the HDA codec
3907 * @hinfo: PCM information
3908 * @substream: PCM substream
3909 *
3910 * Calls the cleanup callback set by the codec with the given arguments.
3911 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003912void snd_hda_codec_cleanup(struct hda_codec *codec,
3913 struct hda_pcm_stream *hinfo,
3914 struct snd_pcm_substream *substream)
3915{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003916 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003917 if (hinfo->ops.cleanup)
3918 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003919 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003920}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003921EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003922
Takashi Iwaid5191e52009-11-16 14:58:17 +01003923/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003924const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3925 "Audio", "SPDIF", "HDMI", "Modem"
3926};
3927
Takashi Iwai176d5332008-07-30 15:01:44 +02003928/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003929 * get the empty PCM device number to assign
3930 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003931static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003932{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003933 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003934 /* assigned to static slots up to dev#10; if more needed, assign
3935 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3936 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003937 static int audio_idx[HDA_PCM_NTYPES][5] = {
3938 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3939 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003940 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003941 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003942 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003943 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003944
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003945 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003946 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003947 return -EINVAL;
3948 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003949
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003950 for (i = 0; audio_idx[type][i] >= 0; i++) {
3951#ifndef CONFIG_SND_DYNAMIC_MINORS
3952 if (audio_idx[type][i] >= 8)
3953 break;
3954#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003955 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3956 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003957 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003958
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003959#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003960 /* non-fixed slots starting from 10 */
3961 for (i = 10; i < 32; i++) {
3962 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3963 return i;
3964 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003965#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003966
Takashi Iwai4e76a882014-02-25 12:21:03 +01003967 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003968 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003969#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003970 dev_warn(bus->card->dev,
3971 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003972#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003973 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003974}
3975
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003976/* call build_pcms ops of the given codec and set up the default parameters */
3977int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003978{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003979 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003980 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003981
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003982 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003983 return 0; /* already parsed */
3984
3985 if (!codec->patch_ops.build_pcms)
3986 return 0;
3987
3988 err = codec->patch_ops.build_pcms(codec);
3989 if (err < 0) {
3990 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003991 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003992 return err;
3993 }
3994
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003995 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003996 int stream;
3997
3998 for (stream = 0; stream < 2; stream++) {
3999 struct hda_pcm_stream *info = &cpcm->stream[stream];
4000
4001 if (!info->substreams)
4002 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004003 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004004 if (err < 0) {
4005 codec_warn(codec,
4006 "fail to setup default for PCM %s\n",
4007 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004008 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004009 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004010 }
4011 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004012
4013 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004014}
4015
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004016/* assign all PCMs of the given codec */
4017int snd_hda_codec_build_pcms(struct hda_codec *codec)
4018{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004019 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004020 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004021 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004022
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004023 if (snd_BUG_ON(!bus->ops.attach_pcm))
4024 return -EINVAL;
4025
4026 err = snd_hda_codec_parse_pcms(codec);
4027 if (err < 0) {
4028 snd_hda_codec_reset(codec);
4029 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004030 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004031
4032 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004033 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004034 if (cpcm->pcm)
4035 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004036 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004037 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004038
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004039 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4040 if (dev < 0)
4041 continue; /* no fatal error */
4042 cpcm->device = dev;
4043 err = bus->ops.attach_pcm(bus, codec, cpcm);
4044 if (err < 0) {
4045 codec_err(codec,
4046 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004047 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004048 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004049 }
4050 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004051
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004052 return 0;
4053}
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 * snd_hda_add_new_ctls - create controls from the array
4057 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004058 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 *
4060 * This helper function creates and add new controls in the given array.
4061 * The array must be terminated with an empty entry as terminator.
4062 *
4063 * Returns 0 if successful, or a negative error code.
4064 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004065int snd_hda_add_new_ctls(struct hda_codec *codec,
4066 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004068 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004071 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004072 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004073 if (knew->iface == -1) /* skip this codec private value */
4074 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004075 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004076 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004077 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004078 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004079 if (addr > 0)
4080 kctl->id.device = addr;
4081 if (idx > 0)
4082 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004083 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004084 if (!err)
4085 break;
4086 /* try first with another device index corresponding to
4087 * the codec addr; if it still fails (or it's the
4088 * primary codec), then try another control index
4089 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004090 if (!addr && codec->core.addr)
4091 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004092 else if (!idx && !knew->index) {
4093 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004094 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004095 if (idx <= 0)
4096 return err;
4097 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004098 return err;
4099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 }
4101 return 0;
4102}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004103EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Takashi Iwai83012a72012-08-24 18:38:08 +02004105#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01004106static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004107{
4108 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004109
Takashi Iwaicc72da72015-02-19 16:00:22 +01004110 if (delay > 0) {
4111 pm_runtime_set_autosuspend_delay(dev, delay);
4112 pm_runtime_use_autosuspend(dev);
4113 pm_runtime_allow(dev);
4114 if (!pm_runtime_suspended(dev))
4115 pm_runtime_mark_last_busy(dev);
4116 } else {
4117 pm_runtime_dont_use_autosuspend(dev);
4118 pm_runtime_forbid(dev);
4119 }
4120}
Takashi Iwaibb573922015-02-20 09:26:04 +01004121
4122/**
4123 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4124 * @bus: HD-audio bus
4125 * @delay: autosuspend delay in msec, 0 = off
4126 *
4127 * Synchronize the runtime PM autosuspend state from the power_save option.
4128 */
4129void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4130{
4131 struct hda_codec *c;
4132
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004133 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01004134 codec_set_power_save(c, delay);
4135}
4136EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004137
Takashi Iwaid5191e52009-11-16 14:58:17 +01004138/**
4139 * snd_hda_check_amp_list_power - Check the amp list and update the power
4140 * @codec: HD-audio codec
4141 * @check: the object containing an AMP list and the status
4142 * @nid: NID to check / update
4143 *
4144 * Check whether the given NID is in the amp list. If it's in the list,
4145 * check the current AMP status, and update the the power-status according
4146 * to the mute status.
4147 *
4148 * This function is supposed to be set or called from the check_power_status
4149 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004150 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004151int snd_hda_check_amp_list_power(struct hda_codec *codec,
4152 struct hda_loopback_check *check,
4153 hda_nid_t nid)
4154{
Takashi Iwai031024e2011-05-02 11:29:30 +02004155 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004156 int ch, v;
4157
4158 if (!check->amplist)
4159 return 0;
4160 for (p = check->amplist; p->nid; p++) {
4161 if (p->nid == nid)
4162 break;
4163 }
4164 if (!p->nid)
4165 return 0; /* nothing changed */
4166
4167 for (p = check->amplist; p->nid; p++) {
4168 for (ch = 0; ch < 2; ch++) {
4169 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4170 p->idx);
4171 if (!(v & HDA_AMP_MUTE) && v > 0) {
4172 if (!check->power_on) {
4173 check->power_on = 1;
4174 snd_hda_power_up(codec);
4175 }
4176 return 1;
4177 }
4178 }
4179 }
4180 if (check->power_on) {
4181 check->power_on = 0;
4182 snd_hda_power_down(codec);
4183 }
4184 return 0;
4185}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004186EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 * input MUX helper
4191 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004192
4193/**
4194 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004195 * @imux: imux helper object
4196 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004197 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004198int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4199 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
4201 unsigned int index;
4202
4203 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4204 uinfo->count = 1;
4205 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004206 if (!imux->num_items)
4207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 index = uinfo->value.enumerated.item;
4209 if (index >= imux->num_items)
4210 index = imux->num_items - 1;
4211 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4212 return 0;
4213}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004214EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Takashi Iwaid5191e52009-11-16 14:58:17 +01004216/**
4217 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004218 * @codec: the HDA codec
4219 * @imux: imux helper object
4220 * @ucontrol: pointer to get/store the data
4221 * @nid: input mux NID
4222 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004223 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004224int snd_hda_input_mux_put(struct hda_codec *codec,
4225 const struct hda_input_mux *imux,
4226 struct snd_ctl_elem_value *ucontrol,
4227 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 unsigned int *cur_val)
4229{
4230 unsigned int idx;
4231
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004232 if (!imux->num_items)
4233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 idx = ucontrol->value.enumerated.item[0];
4235 if (idx >= imux->num_items)
4236 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004237 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004239 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4240 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 *cur_val = idx;
4242 return 1;
4243}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004244EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004247/**
4248 * snd_hda_enum_helper_info - Helper for simple enum ctls
4249 * @kcontrol: ctl element
4250 * @uinfo: pointer to get/store the data
4251 * @num_items: number of enum items
4252 * @texts: enum item string array
4253 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004254 * process kcontrol info callback of a simple string enum array
4255 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4256 */
4257int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4258 struct snd_ctl_elem_info *uinfo,
4259 int num_items, const char * const *texts)
4260{
4261 static const char * const texts_default[] = {
4262 "Disabled", "Enabled"
4263 };
4264
4265 if (!texts || !num_items) {
4266 num_items = 2;
4267 texts = texts_default;
4268 }
4269
Takashi Iwai3ff72212014-10-20 18:17:28 +02004270 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004272EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004273
4274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 * Multi-channel / digital-out PCM helper functions
4276 */
4277
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004278/* setup SPDIF output stream */
4279static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4280 unsigned int stream_tag, unsigned int format)
4281{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004282 struct hda_spdif_out *spdif;
4283 unsigned int curr_fmt;
4284 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004285
Laurence Darby3bef1c32012-11-03 17:00:06 +00004286 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4287 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4288 AC_VERB_GET_STREAM_FORMAT, 0);
4289 reset = codec->spdif_status_reset &&
4290 (spdif->ctls & AC_DIG1_ENABLE) &&
4291 curr_fmt != format;
4292
4293 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4294 updated */
4295 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004296 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004297 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004298 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004299 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004300 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004301 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004302 for (d = codec->slave_dig_outs; *d; d++)
4303 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4304 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004305 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004306 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004307 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004308 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004309 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004310}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004311
Takashi Iwai2f728532008-09-25 16:32:41 +02004312static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4313{
4314 snd_hda_codec_cleanup_stream(codec, nid);
4315 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004316 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004317 for (d = codec->slave_dig_outs; *d; d++)
4318 snd_hda_codec_cleanup_stream(codec, *d);
4319 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004320}
4321
Takashi Iwaid5191e52009-11-16 14:58:17 +01004322/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004323 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004324 * @codec: the HDA codec
4325 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004327int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4328 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Ingo Molnar62932df2006-01-16 16:34:20 +01004330 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004331 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4332 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004333 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004335 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004338EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Takashi Iwaid5191e52009-11-16 14:58:17 +01004340/**
4341 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004342 * @codec: the HDA codec
4343 * @mout: hda_multi_out object
4344 * @stream_tag: stream tag to assign
4345 * @format: format id to assign
4346 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004347 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004348int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4349 struct hda_multi_out *mout,
4350 unsigned int stream_tag,
4351 unsigned int format,
4352 struct snd_pcm_substream *substream)
4353{
4354 mutex_lock(&codec->spdif_mutex);
4355 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4356 mutex_unlock(&codec->spdif_mutex);
4357 return 0;
4358}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004359EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004360
Takashi Iwaid5191e52009-11-16 14:58:17 +01004361/**
4362 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004363 * @codec: the HDA codec
4364 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004365 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004366int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4367 struct hda_multi_out *mout)
4368{
4369 mutex_lock(&codec->spdif_mutex);
4370 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4371 mutex_unlock(&codec->spdif_mutex);
4372 return 0;
4373}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004374EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004375
Takashi Iwaid5191e52009-11-16 14:58:17 +01004376/**
4377 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004378 * @codec: the HDA codec
4379 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004381int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4382 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Ingo Molnar62932df2006-01-16 16:34:20 +01004384 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004386 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004389EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Takashi Iwaid5191e52009-11-16 14:58:17 +01004391/**
4392 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004393 * @codec: the HDA codec
4394 * @mout: hda_multi_out object
4395 * @substream: PCM substream to assign
4396 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004397 *
4398 * Open analog outputs and set up the hw-constraints.
4399 * If the digital outputs can be opened as slave, open the digital
4400 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004402int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4403 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004404 struct snd_pcm_substream *substream,
4405 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
Takashi Iwai9a081602008-02-12 18:37:26 +01004407 struct snd_pcm_runtime *runtime = substream->runtime;
4408 runtime->hw.channels_max = mout->max_channels;
4409 if (mout->dig_out_nid) {
4410 if (!mout->analog_rates) {
4411 mout->analog_rates = hinfo->rates;
4412 mout->analog_formats = hinfo->formats;
4413 mout->analog_maxbps = hinfo->maxbps;
4414 } else {
4415 runtime->hw.rates = mout->analog_rates;
4416 runtime->hw.formats = mout->analog_formats;
4417 hinfo->maxbps = mout->analog_maxbps;
4418 }
4419 if (!mout->spdif_rates) {
4420 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4421 &mout->spdif_rates,
4422 &mout->spdif_formats,
4423 &mout->spdif_maxbps);
4424 }
4425 mutex_lock(&codec->spdif_mutex);
4426 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004427 if ((runtime->hw.rates & mout->spdif_rates) &&
4428 (runtime->hw.formats & mout->spdif_formats)) {
4429 runtime->hw.rates &= mout->spdif_rates;
4430 runtime->hw.formats &= mout->spdif_formats;
4431 if (mout->spdif_maxbps < hinfo->maxbps)
4432 hinfo->maxbps = mout->spdif_maxbps;
4433 } else {
4434 mout->share_spdif = 0;
4435 /* FIXME: need notify? */
4436 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004437 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004438 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4441 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4442}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004443EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Takashi Iwaid5191e52009-11-16 14:58:17 +01004445/**
4446 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004447 * @codec: the HDA codec
4448 * @mout: hda_multi_out object
4449 * @stream_tag: stream tag to assign
4450 * @format: format id to assign
4451 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004452 *
4453 * Set up the i/o for analog out.
4454 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004456int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4457 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 unsigned int stream_tag,
4459 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004460 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
Takashi Iwaidda14412011-05-02 11:29:30 +02004462 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004464 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 int i;
4466
Ingo Molnar62932df2006-01-16 16:34:20 +01004467 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004468 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004469 if (mout->dig_out_nid && mout->share_spdif &&
4470 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004472 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4473 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004474 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004476 setup_dig_out_stream(codec, mout->dig_out_nid,
4477 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 } else {
4479 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004480 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004483 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004486 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4487 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004488 if (!mout->no_share_stream &&
4489 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004491 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4492 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004493 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004494 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4495 if (!mout->no_share_stream && mout->hp_out_nid[i])
4496 snd_hda_codec_setup_stream(codec,
4497 mout->hp_out_nid[i],
4498 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /* surrounds */
4501 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004502 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004503 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4504 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004505 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004506 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4507 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
David Henningssoncd4035e2013-10-10 09:01:25 +02004509
4510 /* extra surrounds */
4511 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4512 int ch = 0;
4513 if (!mout->extra_out_nid[i])
4514 break;
4515 if (chs >= (i + 1) * 2)
4516 ch = i * 2;
4517 else if (!mout->no_share_stream)
4518 break;
4519 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4520 stream_tag, ch, format);
4521 }
4522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return 0;
4524}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004525EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Takashi Iwaid5191e52009-11-16 14:58:17 +01004527/**
4528 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004529 * @codec: the HDA codec
4530 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004532int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4533 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Takashi Iwaidda14412011-05-02 11:29:30 +02004535 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 int i;
4537
4538 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004539 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004541 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004542 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4543 if (mout->hp_out_nid[i])
4544 snd_hda_codec_cleanup_stream(codec,
4545 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004546 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4547 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004548 snd_hda_codec_cleanup_stream(codec,
4549 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004550 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004552 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 mout->dig_out_used = 0;
4554 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004555 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return 0;
4557}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004558EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Takashi Iwai47408602012-04-20 13:06:53 +02004560/**
4561 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004562 * @codec: the HDA codec
4563 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02004564 *
4565 * Guess the suitable VREF pin bits to be set as the pin-control value.
4566 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4567 */
4568unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4569{
4570 unsigned int pincap;
4571 unsigned int oldval;
4572 oldval = snd_hda_codec_read(codec, pin, 0,
4573 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4574 pincap = snd_hda_query_pin_caps(codec, pin);
4575 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4576 /* Exception: if the default pin setup is vref50, we give it priority */
4577 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4578 return AC_PINCTL_VREF_80;
4579 else if (pincap & AC_PINCAP_VREF_50)
4580 return AC_PINCTL_VREF_50;
4581 else if (pincap & AC_PINCAP_VREF_100)
4582 return AC_PINCTL_VREF_100;
4583 else if (pincap & AC_PINCAP_VREF_GRD)
4584 return AC_PINCTL_VREF_GRD;
4585 return AC_PINCTL_VREF_HIZ;
4586}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004587EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02004588
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004589/**
4590 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4591 * @codec: the HDA codec
4592 * @pin: referred pin NID
4593 * @val: pin ctl value to audit
4594 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004595unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4596 hda_nid_t pin, unsigned int val)
4597{
4598 static unsigned int cap_lists[][2] = {
4599 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4600 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4601 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4602 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4603 };
4604 unsigned int cap;
4605
4606 if (!val)
4607 return 0;
4608 cap = snd_hda_query_pin_caps(codec, pin);
4609 if (!cap)
4610 return val; /* don't know what to do... */
4611
4612 if (val & AC_PINCTL_OUT_EN) {
4613 if (!(cap & AC_PINCAP_OUT))
4614 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4615 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4616 val &= ~AC_PINCTL_HP_EN;
4617 }
4618
4619 if (val & AC_PINCTL_IN_EN) {
4620 if (!(cap & AC_PINCAP_IN))
4621 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4622 else {
4623 unsigned int vcap, vref;
4624 int i;
4625 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4626 vref = val & AC_PINCTL_VREFEN;
4627 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4628 if (vref == cap_lists[i][0] &&
4629 !(vcap & cap_lists[i][1])) {
4630 if (i == ARRAY_SIZE(cap_lists) - 1)
4631 vref = AC_PINCTL_VREF_HIZ;
4632 else
4633 vref = cap_lists[i + 1][0];
4634 }
4635 }
4636 val &= ~AC_PINCTL_VREFEN;
4637 val |= vref;
4638 }
4639 }
4640
4641 return val;
4642}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004643EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004644
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004645/**
4646 * _snd_hda_pin_ctl - Helper to set pin ctl value
4647 * @codec: the HDA codec
4648 * @pin: referred pin NID
4649 * @val: pin control value to set
4650 * @cached: access over codec pinctl cache or direct write
4651 *
4652 * This function is a helper to set a pin ctl value more safely.
4653 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4654 * value in pin target array via snd_hda_codec_set_pin_target(), then
4655 * actually writes the value via either snd_hda_codec_update_cache() or
4656 * snd_hda_codec_write() depending on @cached flag.
4657 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004658int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4659 unsigned int val, bool cached)
4660{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004661 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004662 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004663 if (cached)
4664 return snd_hda_codec_update_cache(codec, pin, 0,
4665 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4666 else
4667 return snd_hda_codec_write(codec, pin, 0,
4668 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4669}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004670EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004671
Takashi Iwai990061c2010-09-09 22:08:44 +02004672/**
4673 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004674 * @codec: the HDA codec
4675 * @imux: imux helper object
4676 * @label: the name of imux item to assign
4677 * @index: index number of imux item to assign
4678 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004679 *
4680 * When the same label is used already in the existing items, the number
4681 * suffix is appended to the label. This label index number is stored
4682 * to type_idx when non-NULL pointer is given.
4683 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004684int snd_hda_add_imux_item(struct hda_codec *codec,
4685 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004686 int index, int *type_idx)
4687{
4688 int i, label_idx = 0;
4689 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004690 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004691 return -EINVAL;
4692 }
4693 for (i = 0; i < imux->num_items; i++) {
4694 if (!strncmp(label, imux->items[i].label, strlen(label)))
4695 label_idx++;
4696 }
4697 if (type_idx)
4698 *type_idx = label_idx;
4699 if (label_idx > 0)
4700 snprintf(imux->items[imux->num_items].label,
4701 sizeof(imux->items[imux->num_items].label),
4702 "%s %d", label, label_idx);
4703 else
4704 strlcpy(imux->items[imux->num_items].label, label,
4705 sizeof(imux->items[imux->num_items].label));
4706 imux->items[imux->num_items].index = index;
4707 imux->num_items++;
4708 return 0;
4709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004710EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004713 * snd_hda_bus_reset - Reset the bus
4714 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004716void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004718 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004720 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004721 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004722 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004723#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004724 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004725 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004726 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004727 }
4728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004731EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02004732
Takashi Iwaid5191e52009-11-16 14:58:17 +01004733/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004734 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4735 * @pcm: PCM caps bits
4736 * @buf: the string buffer to write
4737 * @buflen: the max buffer length
4738 *
4739 * used by hda_proc.c and hda_eld.c
4740 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004741void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4742{
4743 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4744 int i, j;
4745
4746 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4747 if (pcm & (AC_SUPPCM_BITS_8 << i))
4748 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4749
4750 buf[j] = '\0'; /* necessary when j == 0 */
4751}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004752EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004753
4754MODULE_DESCRIPTION("HDA codec core");
4755MODULE_LICENSE("GPL");