blob: f96bff37c78719e179993cd728203797f7221978 [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 Iwaicc72da72015-02-19 16:00:22 +010043#define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
44#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 Iwaid5191e52009-11-16 14:58:17 +010051/**
52 * snd_hda_get_jack_location - Give a location string of the jack
53 * @cfg: pin default config value
54 *
55 * Parse the pin default config value and returns the string of the
56 * jack location, e.g. "Rear", "Front", etc.
57 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040058const char *snd_hda_get_jack_location(u32 cfg)
59{
60 static char *bases[7] = {
61 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
62 };
63 static unsigned char specials_idx[] = {
64 0x07, 0x08,
65 0x17, 0x18, 0x19,
66 0x37, 0x38
67 };
68 static char *specials[] = {
69 "Rear Panel", "Drive Bar",
70 "Riser", "HDMI", "ATAPI",
71 "Mobile-In", "Mobile-Out"
72 };
73 int i;
74 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
75 if ((cfg & 0x0f) < 7)
76 return bases[cfg & 0x0f];
77 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
78 if (cfg == specials_idx[i])
79 return specials[i];
80 }
81 return "UNKNOWN";
82}
Takashi Iwai2698ea92013-12-18 07:45:52 +010083EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040084
Takashi Iwaid5191e52009-11-16 14:58:17 +010085/**
86 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
87 * @cfg: pin default config value
88 *
89 * Parse the pin default config value and returns the string of the
90 * jack connectivity, i.e. external or internal connection.
91 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040092const char *snd_hda_get_jack_connectivity(u32 cfg)
93{
94 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
95
96 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
97}
Takashi Iwai2698ea92013-12-18 07:45:52 +010098EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040099
Takashi Iwaid5191e52009-11-16 14:58:17 +0100100/**
101 * snd_hda_get_jack_type - Give a type string of the jack
102 * @cfg: pin default config value
103 *
104 * Parse the pin default config value and returns the string of the
105 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
106 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400107const char *snd_hda_get_jack_type(u32 cfg)
108{
109 static char *jack_types[16] = {
110 "Line Out", "Speaker", "HP Out", "CD",
111 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
112 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200113 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400114 };
115
116 return jack_types[(cfg & AC_DEFCFG_DEVICE)
117 >> AC_DEFCFG_DEVICE_SHIFT];
118}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100119EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400120
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100121/*
122 * Compose a 32bit command word to be sent to the HD-audio controller
123 */
124static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200125make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100126 unsigned int verb, unsigned int parm)
127{
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100128 unsigned int addr = codec->core.addr;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100129 u32 val;
130
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100131 if ((addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200132 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100133 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100134 addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800135 return ~0;
136 }
137
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100138 val = (u32)addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100139 val |= (u32)nid << 20;
140 val |= verb << 8;
141 val |= parm;
142 return val;
143}
144
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200145/*
146 * Send and receive a verb
147 */
148static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200149 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200150{
151 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200152 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200153
Wu Fengguang6430aee2009-07-17 16:49:19 +0800154 if (cmd == ~0)
155 return -1;
156
Takashi Iwai8dd78332009-06-02 01:16:07 +0200157 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200158 snd_hda_power_up(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100159 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200160 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
161 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100162 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
163 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200164 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100165 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200166 snd_hda_power_down(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100167 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200168 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100169 codec_dbg(codec,
170 "resetting BUS due to fatal communication error\n");
Takashi Iwai8dd78332009-06-02 01:16:07 +0200171 bus->ops.bus_reset(bus);
172 }
173 goto again;
174 }
175 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100176 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200177 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200178 return err;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * snd_hda_codec_read - send a command and get the response
183 * @codec: the HDA codec
184 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200185 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * @verb: the verb to send
187 * @parm: the parameter for the verb
188 *
189 * Send a single command and read the corresponding response.
190 *
191 * Returns the obtained response value, or -1 for an error.
192 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200193unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200194 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int verb, unsigned int parm)
196{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200197 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200198 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200199 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700200 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return res;
202}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100203EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/**
206 * snd_hda_codec_write - send a single command without waiting for response
207 * @codec: the HDA codec
208 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200209 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * @verb: the verb to send
211 * @parm: the parameter for the verb
212 *
213 * Send a single command without waiting for response.
214 *
215 * Returns 0 if successful, or a negative error code.
216 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200217int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
218 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200220 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100221 return codec_exec_verb(codec, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100223EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/**
226 * snd_hda_sequence_write - sequence writes
227 * @codec: the HDA codec
228 * @seq: VERB array to send
229 *
230 * Send the commands sequentially from the given array.
231 * The array must be terminated with NID=0.
232 */
233void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
234{
235 for (; seq->nid; seq++)
236 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
237}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100238EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/**
241 * snd_hda_get_sub_nodes - get the range of sub nodes
242 * @codec: the HDA codec
243 * @nid: NID to parse
244 * @start_id: the pointer to store the start NID
245 *
246 * Parse the NID and store the start NID of its sub-nodes.
247 * Returns the number of sub-nodes.
248 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200249int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
250 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unsigned int parm;
253
254 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300255 if (parm == -1) {
256 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200257 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *start_id = (parm >> 16) & 0x7fff;
260 return (int)(parm & 0x7fff);
261}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100262EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100264/* connection list element */
265struct hda_conn_list {
266 struct list_head list;
267 int len;
268 hda_nid_t nid;
269 hda_nid_t conns[0];
270};
271
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200272/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100273static struct hda_conn_list *
274lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200275{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100276 struct hda_conn_list *p;
277 list_for_each_entry(p, &codec->conn_list, list) {
278 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200279 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200280 }
281 return NULL;
282}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200283
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100284static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
285 const hda_nid_t *list)
286{
287 struct hda_conn_list *p;
288
289 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
290 if (!p)
291 return -ENOMEM;
292 p->len = len;
293 p->nid = nid;
294 memcpy(p->conns, list, len * sizeof(hda_nid_t));
295 list_add(&p->list, &codec->conn_list);
296 return 0;
297}
298
299static void remove_conn_list(struct hda_codec *codec)
300{
301 while (!list_empty(&codec->conn_list)) {
302 struct hda_conn_list *p;
303 p = list_first_entry(&codec->conn_list, typeof(*p), list);
304 list_del(&p->list);
305 kfree(p);
306 }
307}
308
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200309/* read the connection and add to the cache */
310static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200311{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100312 hda_nid_t list[32];
313 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200314 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200315
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200316 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100317 if (len == -ENOSPC) {
318 len = snd_hda_get_num_raw_conns(codec, nid);
319 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
320 if (!result)
321 return -ENOMEM;
322 len = snd_hda_get_raw_connections(codec, nid, result, len);
323 }
324 if (len >= 0)
325 len = snd_hda_override_conn_list(codec, nid, len, result);
326 if (result != list)
327 kfree(result);
328 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200329}
Takashi Iwaidce20792011-06-24 14:10:28 +0200330
331/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100332 * snd_hda_get_conn_list - get connection list
333 * @codec: the HDA codec
334 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100335 * @listp: the pointer to store NID list
336 *
337 * Parses the connection list of the given widget and stores the pointer
338 * to the list of NIDs.
339 *
340 * Returns the number of connections, or a negative error code.
341 *
342 * Note that the returned pointer isn't protected against the list
343 * modification. If snd_hda_override_conn_list() might be called
344 * concurrently, protect with a mutex appropriately.
345 */
346int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
347 const hda_nid_t **listp)
348{
349 bool added = false;
350
351 for (;;) {
352 int err;
353 const struct hda_conn_list *p;
354
355 /* if the connection-list is already cached, read it */
356 p = lookup_conn_list(codec, nid);
357 if (p) {
358 if (listp)
359 *listp = p->conns;
360 return p->len;
361 }
362 if (snd_BUG_ON(added))
363 return -EINVAL;
364
365 err = read_and_add_raw_conns(codec, nid);
366 if (err < 0)
367 return err;
368 added = true;
369 }
370}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100371EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372
373/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200374 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * @codec: the HDA codec
376 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200377 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * @max_conns: max. number of connections to store
379 *
380 * Parses the connection list of the given widget and stores the list
381 * of NIDs.
382 *
383 * Returns the number of connections, or a negative error code.
384 */
385int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100388 const hda_nid_t *list;
389 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100391 if (len > 0 && conn_list) {
392 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100393 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200395 return -EINVAL;
396 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100397 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200399
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100400 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200401}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100402EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403
Takashi Iwai4eea3092013-02-07 18:18:19 +0100404/* return CONNLIST_LEN parameter of the given widget */
405static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
406{
407 unsigned int wcaps = get_wcaps(codec, nid);
408 unsigned int parm;
409
410 if (!(wcaps & AC_WCAP_CONN_LIST) &&
411 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
412 return 0;
413
414 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
415 if (parm == -1)
416 parm = 0;
417 return parm;
418}
419
420int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
421{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100422 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100423}
424
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200425/**
426 * snd_hda_get_raw_connections - copy connection list without cache
427 * @codec: the HDA codec
428 * @nid: NID to parse
429 * @conn_list: connection list array
430 * @max_conns: max. number of connections to store
431 *
432 * Like snd_hda_get_connections(), copy the connection list but without
433 * checking through the connection-list cache.
434 * Currently called only from hda_proc.c, so not exported.
435 */
436int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
437 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100440 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100442 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100443 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Takashi Iwai4eea3092013-02-07 18:18:19 +0100445 parm = get_num_conns(codec, nid);
446 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200447 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (parm & AC_CLIST_LONG) {
450 /* long form */
451 shift = 16;
452 num_elems = 2;
453 } else {
454 /* short form */
455 shift = 8;
456 num_elems = 4;
457 }
458 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 mask = (1 << (shift-1)) - 1;
460
Takashi Iwai0ba21762007-04-16 11:29:14 +0200461 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0; /* no connection */
463
464 if (conn_len == 1) {
465 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200466 parm = snd_hda_codec_read(codec, nid, 0,
467 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200468 if (parm == -1 && codec->bus->rirb_error)
469 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100470 if (conn_list)
471 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 1;
473 }
474
475 /* multi connection */
476 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 prev_nid = 0;
478 for (i = 0; i < conn_len; i++) {
479 int range_val;
480 hda_nid_t val, n;
481
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200482 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 parm = snd_hda_codec_read(codec, nid, 0,
484 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200485 if (parm == -1 && codec->bus->rirb_error)
486 return -EIO;
487 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200488 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100489 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100490 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100491 codec_dbg(codec,
492 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200493 nid, i, parm);
494 return 0;
495 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100496 parm >>= shift;
497 if (range_val) {
498 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200499 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100500 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200501 "invalid dep_range_val %x:%x\n",
502 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100503 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100505 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100506 if (conn_list) {
507 if (conns >= max_conns)
508 return -ENOSPC;
509 conn_list[conns] = n;
510 }
511 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100512 }
513 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100514 if (conn_list) {
515 if (conns >= max_conns)
516 return -ENOSPC;
517 conn_list[conns] = val;
518 }
519 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100521 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 return conns;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200527 * snd_hda_override_conn_list - add/modify the connection-list to cache
528 * @codec: the HDA codec
529 * @nid: NID to parse
530 * @len: number of connection list entries
531 * @list: the list of connection entries
532 *
533 * Add or modify the given connection-list to the cache. If the corresponding
534 * cache already exists, invalidate it and append a new one.
535 *
536 * Returns zero or a negative error code.
537 */
538int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
539 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100541 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100543 p = lookup_conn_list(codec, nid);
544 if (p) {
545 list_del(&p->list);
546 kfree(p);
547 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200548
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100549 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100551EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200552
553/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200554 * snd_hda_get_conn_index - get the connection index of the given NID
555 * @codec: the HDA codec
556 * @mux: NID containing the list
557 * @nid: NID to select
558 * @recursive: 1 when searching NID recursively, otherwise 0
559 *
560 * Parses the connection list of the widget @mux and checks whether the
561 * widget @nid is present. If it is, return the connection index.
562 * Otherwise it returns -1.
563 */
564int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
565 hda_nid_t nid, int recursive)
566{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100567 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200568 int i, nums;
569
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100570 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200571 for (i = 0; i < nums; i++)
572 if (conn[i] == nid)
573 return i;
574 if (!recursive)
575 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100576 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100577 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200578 return -1;
579 }
580 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200581 for (i = 0; i < nums; i++) {
582 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
583 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
584 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
586 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200587 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 return -1;
589}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100590EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Mengdong Linf1aa0682013-08-26 21:35:21 -0400592
593/* return DEVLIST_LEN parameter of the given widget */
594static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
595{
596 unsigned int wcaps = get_wcaps(codec, nid);
597 unsigned int parm;
598
599 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
600 get_wcaps_type(wcaps) != AC_WID_PIN)
601 return 0;
602
603 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
604 if (parm == -1 && codec->bus->rirb_error)
605 parm = 0;
606 return parm & AC_DEV_LIST_LEN_MASK;
607}
608
609/**
610 * snd_hda_get_devices - copy device list without cache
611 * @codec: the HDA codec
612 * @nid: NID of the pin to parse
613 * @dev_list: device list array
614 * @max_devices: max. number of devices to store
615 *
616 * Copy the device list. This info is dynamic and so not cached.
617 * Currently called only from hda_proc.c, so not exported.
618 */
619int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
620 u8 *dev_list, int max_devices)
621{
622 unsigned int parm;
623 int i, dev_len, devices;
624
625 parm = get_num_devices(codec, nid);
626 if (!parm) /* not multi-stream capable */
627 return 0;
628
629 dev_len = parm + 1;
630 dev_len = dev_len < max_devices ? dev_len : max_devices;
631
632 devices = 0;
633 while (devices < dev_len) {
634 parm = snd_hda_codec_read(codec, nid, 0,
635 AC_VERB_GET_DEVICE_LIST, devices);
636 if (parm == -1 && codec->bus->rirb_error)
637 break;
638
639 for (i = 0; i < 8; i++) {
640 dev_list[devices] = (u8)parm;
641 parm >>= 4;
642 devices++;
643 if (devices >= dev_len)
644 break;
645 }
646 }
647 return devices;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * destructor
652 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100653static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (bus->ops.private_free)
658 bus->ops.private_free(bus);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100659 snd_hdac_bus_exit(&bus->core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100663static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Takashi Iwai2565c892014-02-19 11:41:09 +0100665 snd_hda_bus_free(device->device_data);
666 return 0;
667}
668
669static int snd_hda_bus_dev_disconnect(struct snd_device *device)
670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100672 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100676/* hdac_bus_ops translations */
677static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
678{
679 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
680 return bus->ops.command(bus, cmd);
681}
682
683static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
684 unsigned int *res)
685{
686 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
687 *res = bus->ops.get_response(bus, addr);
688 return bus->rirb_error ? -EIO : 0;
689}
690
691static const struct hdac_bus_ops bus_ops = {
692 .command = _hda_bus_command,
693 .get_response = _hda_bus_get_response,
694};
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/**
697 * snd_hda_bus_new - create a HDA bus
698 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * @busp: the pointer to store the created bus instance
700 *
701 * Returns 0 if successful, or a negative error code.
702 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100703int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100704 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct hda_bus *bus;
707 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100708 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100709 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .dev_free = snd_hda_bus_dev_free,
711 };
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (busp)
714 *busp = NULL;
715
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200716 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100717 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100720 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
721 if (err < 0) {
722 kfree(bus);
723 return err;
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 bus->card = card;
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200727 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Takashi Iwai0ba21762007-04-16 11:29:14 +0200729 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
730 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 snd_hda_bus_free(bus);
732 return err;
733 }
734 if (busp)
735 *busp = bus;
736 return 0;
737}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100738EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200741 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100743static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200745 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 hda_nid_t nid;
747
748 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
749 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200750 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200751 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200752 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200753 case AC_GRP_AUDIO_FUNCTION:
754 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200755 codec->afg_function_id = function_id & 0xff;
756 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200757 break;
758 case AC_GRP_MODEM_FUNCTION:
759 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200760 codec->mfg_function_id = function_id & 0xff;
761 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200762 break;
763 default:
764 break;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100770 * read widget caps for each widget and store in cache
771 */
772static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
773{
774 int i;
775 hda_nid_t nid;
776
777 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
778 &codec->start_nid);
779 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200780 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100781 return -ENOMEM;
782 nid = codec->start_nid;
783 for (i = 0; i < codec->num_nodes; i++, nid++)
784 codec->wcaps[i] = snd_hda_param_read(codec, nid,
785 AC_PAR_AUDIO_WIDGET_CAP);
786 return 0;
787}
788
Takashi Iwai3be14142009-02-20 14:11:16 +0100789/* read all pin default configurations and save codec->init_pins */
790static int read_pin_defaults(struct hda_codec *codec)
791{
792 int i;
793 hda_nid_t nid = codec->start_nid;
794
795 for (i = 0; i < codec->num_nodes; i++, nid++) {
796 struct hda_pincfg *pin;
797 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200798 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100799 if (wid_type != AC_WID_PIN)
800 continue;
801 pin = snd_array_new(&codec->init_pins);
802 if (!pin)
803 return -ENOMEM;
804 pin->nid = nid;
805 pin->cfg = snd_hda_codec_read(codec, nid, 0,
806 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200807 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
808 AC_VERB_GET_PIN_WIDGET_CONTROL,
809 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100810 }
811 return 0;
812}
813
814/* look up the given pin config list and return the item matching with NID */
815static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
816 struct snd_array *array,
817 hda_nid_t nid)
818{
819 int i;
820 for (i = 0; i < array->used; i++) {
821 struct hda_pincfg *pin = snd_array_elem(array, i);
822 if (pin->nid == nid)
823 return pin;
824 }
825 return NULL;
826}
827
Takashi Iwai3be14142009-02-20 14:11:16 +0100828/* set the current pin config value for the given NID.
829 * the value is cached, and read via snd_hda_codec_get_pincfg()
830 */
831int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
832 hda_nid_t nid, unsigned int cfg)
833{
834 struct hda_pincfg *pin;
835
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200836 /* the check below may be invalid when pins are added by a fixup
837 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
838 * for now
839 */
840 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100841 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
842 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200843 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100844
Takashi Iwai3be14142009-02-20 14:11:16 +0100845 pin = look_up_pincfg(codec, list, nid);
846 if (!pin) {
847 pin = snd_array_new(list);
848 if (!pin)
849 return -ENOMEM;
850 pin->nid = nid;
851 }
852 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100853 return 0;
854}
855
Takashi Iwaid5191e52009-11-16 14:58:17 +0100856/**
857 * snd_hda_codec_set_pincfg - Override a pin default configuration
858 * @codec: the HDA codec
859 * @nid: NID to set the pin config
860 * @cfg: the pin default config value
861 *
862 * Override a pin default configuration value in the cache.
863 * This value can be read by snd_hda_codec_get_pincfg() in a higher
864 * priority than the real hardware value.
865 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100866int snd_hda_codec_set_pincfg(struct hda_codec *codec,
867 hda_nid_t nid, unsigned int cfg)
868{
Takashi Iwai346ff702009-02-23 09:42:57 +0100869 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100870}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100871EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100872
Takashi Iwaid5191e52009-11-16 14:58:17 +0100873/**
874 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
875 * @codec: the HDA codec
876 * @nid: NID to get the pin config
877 *
878 * Get the current pin config value of the given pin NID.
879 * If the pincfg value is cached or overridden via sysfs or driver,
880 * returns the cached value.
881 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100882unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
883{
884 struct hda_pincfg *pin;
885
Takashi Iwai648a8d22014-02-25 10:38:13 +0100886#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100887 {
888 unsigned int cfg = 0;
889 mutex_lock(&codec->user_mutex);
890 pin = look_up_pincfg(codec, &codec->user_pins, nid);
891 if (pin)
892 cfg = pin->cfg;
893 mutex_unlock(&codec->user_mutex);
894 if (cfg)
895 return cfg;
896 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100897#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100898 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
899 if (pin)
900 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100901 pin = look_up_pincfg(codec, &codec->init_pins, nid);
902 if (pin)
903 return pin->cfg;
904 return 0;
905}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100906EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100907
Takashi Iwai95a962c2014-10-29 16:03:58 +0100908/**
909 * snd_hda_codec_set_pin_target - remember the current pinctl target value
910 * @codec: the HDA codec
911 * @nid: pin NID
912 * @val: assigned pinctl value
913 *
914 * This function stores the given value to a pinctl target value in the
915 * pincfg table. This isn't always as same as the actually written value
916 * but can be referred at any time via snd_hda_codec_get_pin_target().
917 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100918int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
919 unsigned int val)
920{
921 struct hda_pincfg *pin;
922
923 pin = look_up_pincfg(codec, &codec->init_pins, nid);
924 if (!pin)
925 return -EINVAL;
926 pin->target = val;
927 return 0;
928}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100929EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100930
Takashi Iwai95a962c2014-10-29 16:03:58 +0100931/**
932 * snd_hda_codec_get_pin_target - return the current pinctl target value
933 * @codec: the HDA codec
934 * @nid: pin NID
935 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100936int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
937{
938 struct hda_pincfg *pin;
939
940 pin = look_up_pincfg(codec, &codec->init_pins, nid);
941 if (!pin)
942 return 0;
943 return pin->target;
944}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100945EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100946
Takashi Iwai92ee6162009-12-27 11:18:59 +0100947/**
948 * snd_hda_shutup_pins - Shut up all pins
949 * @codec: the HDA codec
950 *
951 * Clear all pin controls to shup up before suspend for avoiding click noise.
952 * The controls aren't cached so that they can be resumed properly.
953 */
954void snd_hda_shutup_pins(struct hda_codec *codec)
955{
956 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200957 /* don't shut up pins when unloading the driver; otherwise it breaks
958 * the default pin setup at the next load of the driver
959 */
960 if (codec->bus->shutdown)
961 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100962 for (i = 0; i < codec->init_pins.used; i++) {
963 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
964 /* use read here for syncing after issuing each verb */
965 snd_hda_codec_read(codec, pin->nid, 0,
966 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
967 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200968 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100969}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100970EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100971
Takashi Iwai2a439522011-07-26 09:52:50 +0200972#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200973/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
974static void restore_shutup_pins(struct hda_codec *codec)
975{
976 int i;
977 if (!codec->pins_shutup)
978 return;
979 if (codec->bus->shutdown)
980 return;
981 for (i = 0; i < codec->init_pins.used; i++) {
982 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
983 snd_hda_codec_write(codec, pin->nid, 0,
984 AC_VERB_SET_PIN_WIDGET_CONTROL,
985 pin->ctrl);
986 }
987 codec->pins_shutup = 0;
988}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700989#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200990
David Henningsson26a6cb62012-10-09 15:04:21 +0200991static void hda_jackpoll_work(struct work_struct *work)
992{
993 struct hda_codec *codec =
994 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200995
996 snd_hda_jack_set_dirty_all(codec);
997 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400998
999 if (!codec->jackpoll_interval)
1000 return;
1001
Takashi Iwai2f35c632015-02-27 22:43:26 +01001002 schedule_delayed_work(&codec->jackpoll_work,
1003 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +02001004}
1005
Takashi Iwai01751f52007-08-10 16:59:39 +02001006static void init_hda_cache(struct hda_cache_rec *cache,
1007 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001008static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001009
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001010/* release all pincfg lists */
1011static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001012{
Takashi Iwai346ff702009-02-23 09:42:57 +01001013 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001014#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001015 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001016#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001017 snd_array_free(&codec->init_pins);
1018}
1019
Takashi Iwai54d17402005-11-21 16:33:22 +01001020/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001021 * audio-converter setup caches
1022 */
1023struct hda_cvt_setup {
1024 hda_nid_t nid;
1025 u8 stream_tag;
1026 u8 channel_id;
1027 u16 format_id;
1028 unsigned char active; /* cvt is currently used */
1029 unsigned char dirty; /* setups should be cleared */
1030};
1031
1032/* get or create a cache entry for the given audio converter NID */
1033static struct hda_cvt_setup *
1034get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1035{
1036 struct hda_cvt_setup *p;
1037 int i;
1038
1039 for (i = 0; i < codec->cvt_setups.used; i++) {
1040 p = snd_array_elem(&codec->cvt_setups, i);
1041 if (p->nid == nid)
1042 return p;
1043 }
1044 p = snd_array_new(&codec->cvt_setups);
1045 if (p)
1046 p->nid = nid;
1047 return p;
1048}
1049
1050/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001051 * PCM device
1052 */
1053static void release_pcm(struct kref *kref)
1054{
1055 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
1056
1057 if (pcm->pcm)
1058 snd_device_free(pcm->codec->card, pcm->pcm);
1059 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
1060 kfree(pcm->name);
1061 kfree(pcm);
1062}
1063
1064void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
1065{
1066 kref_put(&pcm->kref, release_pcm);
1067}
1068EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
1069
1070struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
1071 const char *fmt, ...)
1072{
1073 struct hda_pcm *pcm;
1074 va_list args;
1075
1076 va_start(args, fmt);
1077 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1078 if (!pcm)
1079 return NULL;
1080
1081 pcm->codec = codec;
1082 kref_init(&pcm->kref);
1083 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
1084 if (!pcm->name) {
1085 kfree(pcm);
1086 return NULL;
1087 }
1088
1089 list_add_tail(&pcm->list, &codec->pcm_list_head);
1090 return pcm;
1091}
1092EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
1093
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001094/*
1095 * codec destructor
1096 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001097static void codec_release_pcms(struct hda_codec *codec)
1098{
1099 struct hda_pcm *pcm, *n;
1100
1101 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
1102 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001103 if (pcm->pcm)
1104 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001105 snd_hda_codec_pcm_put(pcm);
1106 }
1107}
1108
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001109void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
1110{
1111 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001112 if (!codec->in_freeing)
1113 snd_hda_ctls_clear(codec);
1114 codec_release_pcms(codec);
1115 snd_hda_detach_beep_device(codec);
1116 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1117 snd_hda_jack_tbl_clear(codec);
1118 codec->proc_widget_hook = NULL;
1119 codec->spec = NULL;
1120
1121 free_hda_cache(&codec->amp_cache);
1122 free_hda_cache(&codec->cmd_cache);
1123 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1124 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1125
1126 /* free only driver_pins so that init_pins + user_pins are restored */
1127 snd_array_free(&codec->driver_pins);
1128 snd_array_free(&codec->cvt_setups);
1129 snd_array_free(&codec->spdif_out);
1130 snd_array_free(&codec->verbs);
1131 codec->preset = NULL;
1132 codec->slave_dig_outs = NULL;
1133 codec->spdif_status_reset = 0;
1134 snd_array_free(&codec->mixers);
1135 snd_array_free(&codec->nids);
1136 remove_conn_list(codec);
1137}
1138
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001139static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1140 hda_nid_t fg, unsigned int power_state);
1141
Takashi Iwaid8193872012-08-31 07:54:38 -07001142static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001143 unsigned int power_state);
1144
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001145static int snd_hda_codec_dev_register(struct snd_device *device)
1146{
1147 struct hda_codec *codec = device->device_data;
1148
Takashi Iwaid604b392014-02-28 13:42:09 +01001149 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001150 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001151 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwai709949f2015-02-20 09:58:14 +01001152 /* it was powered up in snd_hda_codec_new(), now all done */
1153 snd_hda_power_down(codec);
Takashi Iwaid604b392014-02-28 13:42:09 +01001154 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001155}
1156
1157static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1158{
1159 struct hda_codec *codec = device->device_data;
1160
Takashi Iwaid604b392014-02-28 13:42:09 +01001161 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001162 return 0;
1163}
1164
Takashi Iwai2565c892014-02-19 11:41:09 +01001165static int snd_hda_codec_dev_free(struct snd_device *device)
1166{
Takashi Iwaid56db742015-02-27 23:25:35 +01001167 struct hda_codec *codec = device->device_data;
1168
1169 codec->in_freeing = 1;
1170 if (device_is_registered(hda_codec_dev(codec)))
1171 device_del(hda_codec_dev(codec));
1172 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +01001173 return 0;
1174}
1175
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001176static void snd_hda_codec_dev_release(struct device *dev)
1177{
Takashi Iwaid56db742015-02-27 23:25:35 +01001178 struct hda_codec *codec = dev_to_hda_codec(dev);
1179
1180 free_init_pincfgs(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001181 snd_hdac_bus_remove_device(&codec->bus->core, &codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +01001182 snd_hda_sysfs_clear(codec);
1183 free_hda_cache(&codec->amp_cache);
1184 free_hda_cache(&codec->cmd_cache);
1185 kfree(codec->vendor_name);
1186 kfree(codec->chip_name);
1187 kfree(codec->modelname);
1188 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +01001189 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/**
1193 * snd_hda_codec_new - create a HDA codec
1194 * @bus: the bus to assign
1195 * @codec_addr: the codec address
1196 * @codecp: the pointer to store the generated codec
1197 *
1198 * Returns 0 if successful, or a negative error code.
1199 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001200int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1201 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001204 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001205 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001206 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001208 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001209 .dev_register = snd_hda_codec_dev_register,
1210 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001211 .dev_free = snd_hda_codec_dev_free,
1212 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Takashi Iwaida3cec32008-08-08 17:12:14 +02001214 if (snd_BUG_ON(!bus))
1215 return -EINVAL;
1216 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001219 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001220 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001223 codec->core.bus = &bus->core;
1224 codec->core.addr = codec_addr;
1225 codec->core.type = HDA_DEV_LEGACY;
1226
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001227 dev = hda_codec_dev(codec);
1228 device_initialize(dev);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001229 dev->parent = bus->core.dev;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001230 dev->bus = &snd_hda_bus_type;
1231 dev->release = snd_hda_codec_dev_release;
1232 dev->groups = snd_hda_dev_attr_groups;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001233 dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001234 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001235 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001238 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001240 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001241 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001242 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001243 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001244 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001245 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1246 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001247 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001248 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001249 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001250 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001251 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001252 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001253 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001254 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001255
David Henningsson26a6cb62012-10-09 15:04:21 +02001256 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001257 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001258 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Takashi Iwai83012a72012-08-24 18:38:08 +02001260#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001261 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
Takashi Iwai709949f2015-02-20 09:58:14 +01001262 * it's powered down later in snd_hda_codec_dev_register().
Takashi Iwaicb53c622007-08-10 17:21:45 +02001263 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001264 set_bit(codec->core.addr, &bus->core.codec_powered);
Takashi Iwaicc72da72015-02-19 16:00:22 +01001265 pm_runtime_set_active(hda_codec_dev(codec));
1266 pm_runtime_get_noresume(hda_codec_dev(codec));
1267 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#endif
1269
Takashi Iwai648a8d22014-02-25 10:38:13 +01001270 snd_hda_sysfs_init(codec);
1271
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001272 if (codec->bus->modelname) {
1273 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1274 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001275 err = -ENODEV;
1276 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001277 }
1278 }
1279
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001280 err = snd_hdac_bus_add_device(&bus->core, &codec->core);
1281 if (err < 0)
1282 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001286 if (codec->vendor_id == -1)
1287 /* read again, hopefully the access method was corrected
1288 * in the last read...
1289 */
1290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_SUBSYSTEM_ID);
1294 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 if (!codec->afg && !codec->mfg) {
Takashi Iwaid56db742015-02-27 23:25:35 +01001299 codec_err(codec, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = -ENODEV;
1301 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Takashi Iwaid8193872012-08-31 07:54:38 -07001304 fg = codec->afg ? codec->afg : codec->mfg;
1305 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001306 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = read_pin_defaults(codec);
1309 if (err < 0)
1310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 }
1317
Takashi Iwai83012a72012-08-24 18:38:08 +02001318#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001320 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001321#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001322 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001323 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001326 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_codec_proc_new(codec);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331
1332 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1333 codec->subsystem_id, codec->revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001334 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001335
Takashi Iwai6efdd852015-02-27 16:09:22 +01001336 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001337 if (err < 0)
1338 goto error;
1339
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340 if (codecp)
1341 *codecp = codec;
1342 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001343
1344 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001345 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001346 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001348EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001349
Takashi Iwai95a962c2014-10-29 16:03:58 +01001350/**
1351 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1352 * @codec: the HDA codec
1353 *
1354 * Forcibly refresh the all widget caps and the init pin configurations of
1355 * the given codec.
1356 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001357int snd_hda_codec_update_widgets(struct hda_codec *codec)
1358{
1359 hda_nid_t fg;
1360 int err;
1361
1362 /* Assume the function group node does not change,
1363 * only the widget nodes may change.
1364 */
1365 kfree(codec->wcaps);
1366 fg = codec->afg ? codec->afg : codec->mfg;
1367 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001368 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001369 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001370
1371 snd_array_free(&codec->init_pins);
1372 err = read_pin_defaults(codec);
1373
1374 return err;
1375}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001376EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001377
Takashi Iwaied360812012-08-08 17:12:52 +02001378/* update the stream-id if changed */
1379static void update_pcm_stream_id(struct hda_codec *codec,
1380 struct hda_cvt_setup *p, hda_nid_t nid,
1381 u32 stream_tag, int channel_id)
1382{
1383 unsigned int oldval, newval;
1384
1385 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1386 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1387 newval = (stream_tag << 4) | channel_id;
1388 if (oldval != newval)
1389 snd_hda_codec_write(codec, nid, 0,
1390 AC_VERB_SET_CHANNEL_STREAMID,
1391 newval);
1392 p->stream_tag = stream_tag;
1393 p->channel_id = channel_id;
1394 }
1395}
1396
1397/* update the format-id if changed */
1398static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1399 hda_nid_t nid, int format)
1400{
1401 unsigned int oldval;
1402
1403 if (p->format_id != format) {
1404 oldval = snd_hda_codec_read(codec, nid, 0,
1405 AC_VERB_GET_STREAM_FORMAT, 0);
1406 if (oldval != format) {
1407 msleep(1);
1408 snd_hda_codec_write(codec, nid, 0,
1409 AC_VERB_SET_STREAM_FORMAT,
1410 format);
1411 }
1412 p->format_id = format;
1413 }
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/**
1417 * snd_hda_codec_setup_stream - set up the codec for streaming
1418 * @codec: the CODEC to set up
1419 * @nid: the NID to set up
1420 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1421 * @channel_id: channel id to pass, zero based.
1422 * @format: stream format.
1423 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1425 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int channel_id, int format)
1427{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001428 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001429 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001430 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001431 int i;
1432
Takashi Iwai0ba21762007-04-16 11:29:14 +02001433 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001434 return;
1435
Takashi Iwai4e76a882014-02-25 12:21:03 +01001436 codec_dbg(codec,
1437 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1438 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001439 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001440 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001441 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001442
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001443 if (codec->patch_ops.stream_pm)
1444 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001445 if (codec->pcm_format_first)
1446 update_pcm_format(codec, p, nid, format);
1447 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1448 if (!codec->pcm_format_first)
1449 update_pcm_format(codec, p, nid, format);
1450
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 p->active = 1;
1452 p->dirty = 0;
1453
1454 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001455 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001456 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001457 for (i = 0; i < c->cvt_setups.used; i++) {
1458 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001459 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001460 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001461 p->dirty = 1;
1462 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001465EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467static void really_cleanup_stream(struct hda_codec *codec,
1468 struct hda_cvt_setup *q);
1469
Takashi Iwaid5191e52009-11-16 14:58:17 +01001470/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001471 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001472 * @codec: the CODEC to clean up
1473 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001474 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001475 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001476void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1477 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001478{
Takashi Iwaieb541332010-08-06 13:48:11 +02001479 struct hda_cvt_setup *p;
1480
Takashi Iwai888afa12008-03-18 09:57:50 +01001481 if (!nid)
1482 return;
1483
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001484 if (codec->no_sticky_stream)
1485 do_now = 1;
1486
Takashi Iwai4e76a882014-02-25 12:21:03 +01001487 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001488 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001489 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001490 /* here we just clear the active flag when do_now isn't set;
1491 * actual clean-ups will be done later in
1492 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1493 */
1494 if (do_now)
1495 really_cleanup_stream(codec, p);
1496 else
1497 p->active = 0;
1498 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001499}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001500EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001501
Takashi Iwaieb541332010-08-06 13:48:11 +02001502static void really_cleanup_stream(struct hda_codec *codec,
1503 struct hda_cvt_setup *q)
1504{
1505 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001506 if (q->stream_tag || q->channel_id)
1507 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1508 if (q->format_id)
1509 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1510);
Takashi Iwaieb541332010-08-06 13:48:11 +02001511 memset(q, 0, sizeof(*q));
1512 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001513 if (codec->patch_ops.stream_pm)
1514 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001515}
1516
1517/* clean up the all conflicting obsolete streams */
1518static void purify_inactive_streams(struct hda_codec *codec)
1519{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001520 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 int i;
1522
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001523 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001524 for (i = 0; i < c->cvt_setups.used; i++) {
1525 struct hda_cvt_setup *p;
1526 p = snd_array_elem(&c->cvt_setups, i);
1527 if (p->dirty)
1528 really_cleanup_stream(c, p);
1529 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001530 }
1531}
1532
Takashi Iwai2a439522011-07-26 09:52:50 +02001533#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001534/* clean up all streams; called from suspend */
1535static void hda_cleanup_all_streams(struct hda_codec *codec)
1536{
1537 int i;
1538
1539 for (i = 0; i < codec->cvt_setups.used; i++) {
1540 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1541 if (p->stream_tag)
1542 really_cleanup_stream(codec, p);
1543 }
1544}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001545#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
1548 * amp access functions
1549 */
1550
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001551/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001552#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001553#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001554#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1555#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001557#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001560static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001561 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwai01751f52007-08-10 16:59:39 +02001563 memset(cache, 0, sizeof(*cache));
1564 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001565 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001566}
1567
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001568static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001569{
Takashi Iwai603c4012008-07-30 15:01:44 +02001570 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001574static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwai01751f52007-08-10 16:59:39 +02001576 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1577 u16 cur = cache->hash[idx];
1578 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001581 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (info->key == key)
1583 return info;
1584 cur = info->next;
1585 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001586 return NULL;
1587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001589/* query the hash. allocate an entry if not found. */
1590static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1591 u32 key)
1592{
1593 struct hda_cache_head *info = get_hash(cache, key);
1594 if (!info) {
1595 u16 idx, cur;
1596 /* add a new hash entry */
1597 info = snd_array_new(&cache->buf);
1598 if (!info)
1599 return NULL;
1600 cur = snd_array_index(&cache->buf, info);
1601 info->key = key;
1602 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001603 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001604 idx = key % (u16)ARRAY_SIZE(cache->hash);
1605 info->next = cache->hash[idx];
1606 cache->hash[idx] = cur;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return info;
1609}
1610
Takashi Iwai01751f52007-08-10 16:59:39 +02001611/* query and allocate an amp hash entry */
1612static inline struct hda_amp_info *
1613get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1614{
1615 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1616}
1617
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001618/* overwrite the value with the key in the caps hash */
1619static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1620{
1621 struct hda_amp_info *info;
1622
1623 mutex_lock(&codec->hash_mutex);
1624 info = get_alloc_amp_hash(codec, key);
1625 if (!info) {
1626 mutex_unlock(&codec->hash_mutex);
1627 return -EINVAL;
1628 }
1629 info->amp_caps = val;
1630 info->head.val |= INFO_AMP_CAPS;
1631 mutex_unlock(&codec->hash_mutex);
1632 return 0;
1633}
1634
1635/* query the value from the caps hash; if not found, fetch the current
1636 * value from the given function and store in the hash
1637 */
1638static unsigned int
1639query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1640 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1641{
1642 struct hda_amp_info *info;
1643 unsigned int val;
1644
1645 mutex_lock(&codec->hash_mutex);
1646 info = get_alloc_amp_hash(codec, key);
1647 if (!info) {
1648 mutex_unlock(&codec->hash_mutex);
1649 return 0;
1650 }
1651 if (!(info->head.val & INFO_AMP_CAPS)) {
1652 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1653 val = func(codec, nid, dir);
1654 write_caps_hash(codec, key, val);
1655 } else {
1656 val = info->amp_caps;
1657 mutex_unlock(&codec->hash_mutex);
1658 }
1659 return val;
1660}
1661
1662static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1663 int direction)
1664{
1665 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1666 nid = codec->afg;
1667 return snd_hda_param_read(codec, nid,
1668 direction == HDA_OUTPUT ?
1669 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1670}
1671
Takashi Iwaid5191e52009-11-16 14:58:17 +01001672/**
1673 * query_amp_caps - query AMP capabilities
1674 * @codec: the HD-auio codec
1675 * @nid: the NID to query
1676 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1677 *
1678 * Query AMP capabilities for the given widget and direction.
1679 * Returns the obtained capability bits.
1680 *
1681 * When cap bits have been already read, this doesn't read again but
1682 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001684u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001686 return query_caps_hash(codec, nid, direction,
1687 HDA_HASH_KEY(nid, direction, 0),
1688 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001690EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Takashi Iwaid5191e52009-11-16 14:58:17 +01001692/**
David Henningsson861a04e2014-09-23 10:38:17 +02001693 * snd_hda_check_amp_caps - query AMP capabilities
1694 * @codec: the HD-audio codec
1695 * @nid: the NID to query
1696 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001697 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001698 *
1699 * Check whether the widget has the given amp capability for the direction.
1700 */
1701bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1702 int dir, unsigned int bits)
1703{
1704 if (!nid)
1705 return false;
1706 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1707 if (query_amp_caps(codec, nid, dir) & bits)
1708 return true;
1709 return false;
1710}
1711EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1712
1713/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001714 * snd_hda_override_amp_caps - Override the AMP capabilities
1715 * @codec: the CODEC to clean up
1716 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001717 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001718 * @caps: the capability bits to set
1719 *
1720 * Override the cached AMP caps bits value by the given one.
1721 * This function is useful if the driver needs to adjust the AMP ranges,
1722 * e.g. limit to 0dB, etc.
1723 *
1724 * Returns zero if successful or a negative error code.
1725 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001726int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1727 unsigned int caps)
1728{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001729 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001730}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001731EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001732
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001733static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1734 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001735{
1736 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1737}
1738
Takashi Iwaid5191e52009-11-16 14:58:17 +01001739/**
1740 * snd_hda_query_pin_caps - Query PIN capabilities
1741 * @codec: the HD-auio codec
1742 * @nid: the NID to query
1743 *
1744 * Query PIN capabilities for the given widget.
1745 * Returns the obtained capability bits.
1746 *
1747 * When cap bits have been already read, this doesn't read again but
1748 * returns the cached value.
1749 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001750u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1751{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001752 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001753 read_pin_cap);
1754}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001755EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001756
Wu Fengguang864f92b2009-11-18 12:38:02 +08001757/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001758 * snd_hda_override_pin_caps - Override the pin capabilities
1759 * @codec: the CODEC
1760 * @nid: the NID to override
1761 * @caps: the capability bits to set
1762 *
1763 * Override the cached PIN capabilitiy bits value by the given one.
1764 *
1765 * Returns zero if successful or a negative error code.
1766 */
1767int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1768 unsigned int caps)
1769{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001770 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001771}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001772EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001773
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774/* read or sync the hash value with the current value;
1775 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001777static struct hda_amp_info *
1778update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001779 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001781 struct hda_amp_info *info;
1782 unsigned int parm, val = 0;
1783 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001785 retry:
1786 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1787 if (!info)
1788 return NULL;
1789 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1790 if (!val_read) {
1791 mutex_unlock(&codec->hash_mutex);
1792 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1793 parm |= direction == HDA_OUTPUT ?
1794 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1795 parm |= index;
1796 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001798 val &= 0xff;
1799 val_read = true;
1800 mutex_lock(&codec->hash_mutex);
1801 goto retry;
1802 }
1803 info->vol[ch] = val;
1804 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001805 } else if (init_only)
1806 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001807 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
1810/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001811 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001813static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001814 hda_nid_t nid, int ch, int direction, int index,
1815 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 u32 parm;
1818
1819 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1820 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1821 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001822 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1823 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001824 ; /* set the zero value as a fake mute */
1825 else
1826 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1828}
1829
Takashi Iwaid5191e52009-11-16 14:58:17 +01001830/**
1831 * snd_hda_codec_amp_read - Read AMP value
1832 * @codec: HD-audio codec
1833 * @nid: NID to read the AMP value
1834 * @ch: channel (left=0 or right=1)
1835 * @direction: #HDA_INPUT or #HDA_OUTPUT
1836 * @index: the index value (only for input direction)
1837 *
1838 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Takashi Iwai834be882006-03-01 14:16:17 +01001840int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1841 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001843 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001844 unsigned int val = 0;
1845
1846 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001847 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001848 if (info)
1849 val = info->vol[ch];
1850 mutex_unlock(&codec->hash_mutex);
1851 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001853EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Takashi Iwai280e57d2012-12-14 10:32:21 +01001855static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1856 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001857 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001858{
1859 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001860 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001861
1862 if (snd_BUG_ON(mask & ~0xff))
1863 mask &= 0xff;
1864 val &= mask;
1865
1866 mutex_lock(&codec->hash_mutex);
1867 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1868 if (!info) {
1869 mutex_unlock(&codec->hash_mutex);
1870 return 0;
1871 }
1872 val |= info->vol[ch] & ~mask;
1873 if (info->vol[ch] == val) {
1874 mutex_unlock(&codec->hash_mutex);
1875 return 0;
1876 }
1877 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001878 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001879 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001880 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001881 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001882 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001883 return 1;
1884}
1885
Takashi Iwaid5191e52009-11-16 14:58:17 +01001886/**
1887 * snd_hda_codec_amp_update - update the AMP value
1888 * @codec: HD-audio codec
1889 * @nid: NID to read the AMP value
1890 * @ch: channel (left=0 or right=1)
1891 * @direction: #HDA_INPUT or #HDA_OUTPUT
1892 * @idx: the index value (only for input direction)
1893 * @mask: bit mask to set
1894 * @val: the bits value to set
1895 *
1896 * Update the AMP value with a bit mask.
1897 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001898 */
Takashi Iwai834be882006-03-01 14:16:17 +01001899int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1900 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001902 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1903 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001905EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Takashi Iwaid5191e52009-11-16 14:58:17 +01001907/**
1908 * snd_hda_codec_amp_stereo - update the AMP stereo values
1909 * @codec: HD-audio codec
1910 * @nid: NID to read the AMP value
1911 * @direction: #HDA_INPUT or #HDA_OUTPUT
1912 * @idx: the index value (only for input direction)
1913 * @mask: bit mask to set
1914 * @val: the bits value to set
1915 *
1916 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1917 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001918 */
1919int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1920 int direction, int idx, int mask, int val)
1921{
1922 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001923
1924 if (snd_BUG_ON(mask & ~0xff))
1925 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001926 for (ch = 0; ch < 2; ch++)
1927 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1928 idx, mask, val);
1929 return ret;
1930}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001931EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001932
Takashi Iwai95a962c2014-10-29 16:03:58 +01001933/**
1934 * snd_hda_codec_amp_init - initialize the AMP value
1935 * @codec: the HDA codec
1936 * @nid: NID to read the AMP value
1937 * @ch: channel (left=0 or right=1)
1938 * @dir: #HDA_INPUT or #HDA_OUTPUT
1939 * @idx: the index value (only for input direction)
1940 * @mask: bit mask to set
1941 * @val: the bits value to set
1942 *
1943 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001944 * the first access. If the amp was already initialized / updated beforehand,
1945 * this does nothing.
1946 */
1947int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1948 int dir, int idx, int mask, int val)
1949{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001950 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1951 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001952}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001953EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001954
Takashi Iwai95a962c2014-10-29 16:03:58 +01001955/**
1956 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1957 * @codec: the HDA codec
1958 * @nid: NID to read the AMP value
1959 * @dir: #HDA_INPUT or #HDA_OUTPUT
1960 * @idx: the index value (only for input direction)
1961 * @mask: bit mask to set
1962 * @val: the bits value to set
1963 *
1964 * Call snd_hda_codec_amp_init() for both stereo channels.
1965 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001966int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1967 int dir, int idx, int mask, int val)
1968{
1969 int ch, ret = 0;
1970
1971 if (snd_BUG_ON(mask & ~0xff))
1972 mask &= 0xff;
1973 for (ch = 0; ch < 2; ch++)
1974 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1975 idx, mask, val);
1976 return ret;
1977}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001978EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001979
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980/**
1981 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1982 * @codec: HD-audio codec
1983 *
1984 * Resume the all amp commands from the cache.
1985 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001986void snd_hda_codec_resume_amp(struct hda_codec *codec)
1987{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001988 int i;
1989
Takashi Iwaic370dd62012-12-13 18:30:04 +01001990 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001991 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001992 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1993 struct hda_amp_info *buffer;
1994 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001995 hda_nid_t nid;
1996 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001997 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001998
1999 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002000 if (!buffer->head.dirty)
2001 continue;
2002 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002003 info = *buffer;
2004 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002005 if (!key)
2006 continue;
2007 nid = key & 0xff;
2008 idx = (key >> 16) & 0xff;
2009 dir = (key >> 24) & 0xff;
2010 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002011 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002012 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002013 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002014 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2015 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002016 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002017 }
2018 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002019 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002021EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002023static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2024 unsigned int ofs)
2025{
2026 u32 caps = query_amp_caps(codec, nid, dir);
2027 /* get num steps */
2028 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2029 if (ofs < caps)
2030 caps -= ofs;
2031 return caps;
2032}
2033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002036 * @kcontrol: referred ctl element
2037 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038 *
2039 * The control element is supposed to have the private_value field
2040 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2041 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002042int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2046 u16 nid = get_amp_nid(kcontrol);
2047 u8 chs = get_amp_channels(kcontrol);
2048 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002049 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002051 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2052 uinfo->count = chs == 3 ? 2 : 1;
2053 uinfo->value.integer.min = 0;
2054 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2055 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002056 codec_warn(codec,
2057 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2058 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return -EINVAL;
2060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return 0;
2062}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002063EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002065
2066static inline unsigned int
2067read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2068 int ch, int dir, int idx, unsigned int ofs)
2069{
2070 unsigned int val;
2071 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2072 val &= HDA_AMP_VOLMASK;
2073 if (val >= ofs)
2074 val -= ofs;
2075 else
2076 val = 0;
2077 return val;
2078}
2079
2080static inline int
2081update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2082 int ch, int dir, int idx, unsigned int ofs,
2083 unsigned int val)
2084{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002085 unsigned int maxval;
2086
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002087 if (val > 0)
2088 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002089 /* ofs = 0: raw max value */
2090 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002091 if (val > maxval)
2092 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002093 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2094 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002095}
2096
Takashi Iwaid5191e52009-11-16 14:58:17 +01002097/**
2098 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002099 * @kcontrol: ctl element
2100 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002101 *
2102 * The control element is supposed to have the private_value field
2103 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2104 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002105int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 hda_nid_t nid = get_amp_nid(kcontrol);
2110 int chs = get_amp_channels(kcontrol);
2111 int dir = get_amp_direction(kcontrol);
2112 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002113 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 long *valp = ucontrol->value.integer.value;
2115
2116 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002117 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002119 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return 0;
2121}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002122EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124/**
2125 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002126 * @kcontrol: ctl element
2127 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002128 *
2129 * The control element is supposed to have the private_value field
2130 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2131 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002132int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2133 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2136 hda_nid_t nid = get_amp_nid(kcontrol);
2137 int chs = get_amp_channels(kcontrol);
2138 int dir = get_amp_direction(kcontrol);
2139 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002140 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 long *valp = ucontrol->value.integer.value;
2142 int change = 0;
2143
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002144 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002145 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002146 valp++;
2147 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002148 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002149 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return change;
2151}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002152EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154/**
2155 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002156 * @kcontrol: ctl element
2157 * @op_flag: operation flag
2158 * @size: byte size of input TLV
2159 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160 *
2161 * The control element is supposed to have the private_value field
2162 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2163 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002164int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2165 unsigned int size, unsigned int __user *_tlv)
2166{
2167 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2168 hda_nid_t nid = get_amp_nid(kcontrol);
2169 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002170 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002171 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002172 u32 caps, val1, val2;
2173
2174 if (size < 4 * sizeof(unsigned int))
2175 return -ENOMEM;
2176 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002177 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2178 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002179 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002180 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002181 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002182 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002183 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002184 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2185 return -EFAULT;
2186 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2187 return -EFAULT;
2188 if (put_user(val1, _tlv + 2))
2189 return -EFAULT;
2190 if (put_user(val2, _tlv + 3))
2191 return -EFAULT;
2192 return 0;
2193}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002194EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2198 * @codec: HD-audio codec
2199 * @nid: NID of a reference widget
2200 * @dir: #HDA_INPUT or #HDA_OUTPUT
2201 * @tlv: TLV data to be stored, at least 4 elements
2202 *
2203 * Set (static) TLV data for a virtual master volume using the AMP caps
2204 * obtained from the reference NID.
2205 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002206 */
2207void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2208 unsigned int *tlv)
2209{
2210 u32 caps;
2211 int nums, step;
2212
2213 caps = query_amp_caps(codec, nid, dir);
2214 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2215 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2216 step = (step + 1) * 25;
2217 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2218 tlv[1] = 2 * sizeof(unsigned int);
2219 tlv[2] = -nums * step;
2220 tlv[3] = step;
2221}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002222EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002223
2224/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002225static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002226find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002227{
2228 struct snd_ctl_elem_id id;
2229 memset(&id, 0, sizeof(id));
2230 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002231 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002232 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002233 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2234 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002235 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002236 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002237}
2238
Takashi Iwaid5191e52009-11-16 14:58:17 +01002239/**
2240 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2241 * @codec: HD-audio codec
2242 * @name: ctl id name string
2243 *
2244 * Get the control element with the given id string and IFACE_MIXER.
2245 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002246struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2247 const char *name)
2248{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002249 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002250}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002251EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002252
Takashi Iwaidcda5802012-10-12 17:24:51 +02002253static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002254 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002255{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002256 int i, idx;
2257 /* 16 ctlrs should be large enough */
2258 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2259 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002260 return idx;
2261 }
2262 return -EBUSY;
2263}
2264
Takashi Iwaid5191e52009-11-16 14:58:17 +01002265/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002266 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267 * @codec: HD-audio codec
2268 * @nid: corresponding NID (optional)
2269 * @kctl: the control element to assign
2270 *
2271 * Add the given control element to an array inside the codec instance.
2272 * All control elements belonging to a codec are supposed to be added
2273 * by this function so that a proper clean-up works at the free or
2274 * reconfiguration time.
2275 *
2276 * If non-zero @nid is passed, the NID is assigned to the control element.
2277 * The assignment is shown in the codec proc file.
2278 *
2279 * snd_hda_ctl_add() checks the control subdev id field whether
2280 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002281 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2282 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002283 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002284int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2285 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002286{
2287 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002288 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002289 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002290
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002291 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002292 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002293 if (nid == 0)
2294 nid = get_amp_nid_(kctl->private_value);
2295 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002296 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2297 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002298 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002299 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002300 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002301 if (err < 0)
2302 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002303 item = snd_array_new(&codec->mixers);
2304 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002305 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306 item->kctl = kctl;
2307 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002308 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002309 return 0;
2310}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002311EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312
Takashi Iwaid5191e52009-11-16 14:58:17 +01002313/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002314 * snd_hda_add_nid - Assign a NID to a control element
2315 * @codec: HD-audio codec
2316 * @nid: corresponding NID (optional)
2317 * @kctl: the control element to assign
2318 * @index: index to kctl
2319 *
2320 * Add the given control element to an array inside the codec instance.
2321 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2322 * NID:KCTL mapping - for example "Capture Source" selector.
2323 */
2324int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2325 unsigned int index, hda_nid_t nid)
2326{
2327 struct hda_nid_item *item;
2328
2329 if (nid > 0) {
2330 item = snd_array_new(&codec->nids);
2331 if (!item)
2332 return -ENOMEM;
2333 item->kctl = kctl;
2334 item->index = index;
2335 item->nid = nid;
2336 return 0;
2337 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002338 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2339 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002340 return -EINVAL;
2341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002342EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002343
2344/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2346 * @codec: HD-audio codec
2347 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348void snd_hda_ctls_clear(struct hda_codec *codec)
2349{
2350 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002351 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002352 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002353 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002354 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002355 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002356}
2357
Takashi Iwai95a962c2014-10-29 16:03:58 +01002358/**
2359 * snd_hda_lock_devices - pseudo device locking
2360 * @bus: the BUS
2361 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002362 * toggle card->shutdown to allow/disallow the device access (as a hack)
2363 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002364int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002365{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002366 struct snd_card *card = bus->card;
2367 struct hda_codec *codec;
2368
Takashi Iwaia65d6292009-02-23 16:57:04 +01002369 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002370 if (card->shutdown)
2371 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002372 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002373 if (!list_empty(&card->ctl_files))
2374 goto err_clear;
2375
Takashi Iwaid068ebc2015-03-02 23:22:59 +01002376 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002377 struct hda_pcm *cpcm;
2378 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002379 if (!cpcm->pcm)
2380 continue;
2381 if (cpcm->pcm->streams[0].substream_opened ||
2382 cpcm->pcm->streams[1].substream_opened)
2383 goto err_clear;
2384 }
2385 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002386 spin_unlock(&card->files_lock);
2387 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002388
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002389 err_clear:
2390 card->shutdown = 0;
2391 err_unlock:
2392 spin_unlock(&card->files_lock);
2393 return -EINVAL;
2394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002395EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002396
Takashi Iwai95a962c2014-10-29 16:03:58 +01002397/**
2398 * snd_hda_unlock_devices - pseudo device unlocking
2399 * @bus: the BUS
2400 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002401void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002402{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002403 struct snd_card *card = bus->card;
2404
Takashi Iwaia65d6292009-02-23 16:57:04 +01002405 spin_lock(&card->files_lock);
2406 card->shutdown = 0;
2407 spin_unlock(&card->files_lock);
2408}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002409EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002410
Takashi Iwaid5191e52009-11-16 14:58:17 +01002411/**
2412 * snd_hda_codec_reset - Clear all objects assigned to the codec
2413 * @codec: HD-audio codec
2414 *
2415 * This frees the all PCM and control elements assigned to the codec, and
2416 * clears the caches and restores the pin default configurations.
2417 *
2418 * When a device is being used, it returns -EBSY. If successfully freed,
2419 * returns zero.
2420 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002421int snd_hda_codec_reset(struct hda_codec *codec)
2422{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002425 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002426 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002427
2428 /* OK, let it free */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002429 if (device_is_registered(hda_codec_dev(codec)))
2430 device_del(hda_codec_dev(codec));
2431
Takashi Iwaia65d6292009-02-23 16:57:04 +01002432 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002433 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002435}
2436
Takashi Iwai6194b992014-06-06 18:12:16 +02002437typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002438
2439/* apply the function to all matching slave ctls in the mixer list */
2440static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002441 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002442{
2443 struct hda_nid_item *items;
2444 const char * const *s;
2445 int i, err;
2446
2447 items = codec->mixers.list;
2448 for (i = 0; i < codec->mixers.used; i++) {
2449 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002450 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002451 continue;
2452 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002453 char tmpname[sizeof(sctl->id.name)];
2454 const char *name = *s;
2455 if (suffix) {
2456 snprintf(tmpname, sizeof(tmpname), "%s %s",
2457 name, suffix);
2458 name = tmpname;
2459 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002460 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002461 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002462 if (err)
2463 return err;
2464 break;
2465 }
2466 }
2467 }
2468 return 0;
2469}
2470
Takashi Iwai6194b992014-06-06 18:12:16 +02002471static int check_slave_present(struct hda_codec *codec,
2472 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002473{
2474 return 1;
2475}
2476
Takashi Iwai18478e82012-03-09 17:51:10 +01002477/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002478static int get_kctl_0dB_offset(struct hda_codec *codec,
2479 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002480{
2481 int _tlv[4];
2482 const int *tlv = NULL;
2483 int val = -1;
2484
2485 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2486 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2487 mm_segment_t fs = get_fs();
2488 set_fs(get_ds());
2489 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2490 tlv = _tlv;
2491 set_fs(fs);
2492 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2493 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002494 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2495 int step = tlv[3];
2496 step &= ~TLV_DB_SCALE_MUTE;
2497 if (!step)
2498 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002499 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002500 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002501- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002502 return -1;
2503 }
2504 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002505 val = -tlv[2] / step;
2506 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002507 return val;
2508}
2509
2510/* call kctl->put with the given value(s) */
2511static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2512{
2513 struct snd_ctl_elem_value *ucontrol;
2514 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2515 if (!ucontrol)
2516 return -ENOMEM;
2517 ucontrol->value.integer.value[0] = val;
2518 ucontrol->value.integer.value[1] = val;
2519 kctl->put(kctl, ucontrol);
2520 kfree(ucontrol);
2521 return 0;
2522}
2523
2524/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002525static int init_slave_0dB(struct hda_codec *codec,
2526 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002527{
Takashi Iwai6194b992014-06-06 18:12:16 +02002528 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002529 if (offset > 0)
2530 put_kctl_with_value(slave, offset);
2531 return 0;
2532}
2533
2534/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002535static int init_slave_unmute(struct hda_codec *codec,
2536 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002537{
2538 return put_kctl_with_value(slave, 1);
2539}
2540
Takashi Iwaie8750942014-06-30 14:02:39 +02002541static int add_slave(struct hda_codec *codec,
2542 void *data, struct snd_kcontrol *slave)
2543{
2544 return snd_ctl_add_slave(data, slave);
2545}
2546
Takashi Iwaid5191e52009-11-16 14:58:17 +01002547/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002548 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002549 * @codec: HD-audio codec
2550 * @name: vmaster control name
2551 * @tlv: TLV data (optional)
2552 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002553 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002554 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002555 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002556 *
2557 * Create a virtual master control with the given name. The TLV data
2558 * must be either NULL or a valid data.
2559 *
2560 * @slaves is a NULL-terminated array of strings, each of which is a
2561 * slave control name. All controls with these names are assigned to
2562 * the new virtual master control.
2563 *
2564 * This function returns zero if successful or a negative error code.
2565 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002566int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002567 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002568 const char *suffix, bool init_slave_vol,
2569 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002570{
2571 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002572 int err;
2573
Takashi Iwai29e58532012-03-12 12:25:03 +01002574 if (ctl_ret)
2575 *ctl_ret = NULL;
2576
Takashi Iwai9322ca52012-02-03 14:28:01 +01002577 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002578 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002579 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002580 return 0;
2581 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002582 kctl = snd_ctl_make_virtual_master(name, tlv);
2583 if (!kctl)
2584 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002585 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002586 if (err < 0)
2587 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002588
Takashi Iwaie8750942014-06-30 14:02:39 +02002589 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002590 if (err < 0)
2591 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002592
2593 /* init with master mute & zero volume */
2594 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002595 if (init_slave_vol) {
2596 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002597 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002598 tlv ? init_slave_0dB : init_slave_unmute, &step);
2599 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002600
Takashi Iwai29e58532012-03-12 12:25:03 +01002601 if (ctl_ret)
2602 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002603 return 0;
2604}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002605EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002606
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002607/*
2608 * mute-LED control using vmaster
2609 */
2610static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_info *uinfo)
2612{
2613 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002614 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002615 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002616
Takashi Iwai3ff72212014-10-20 18:17:28 +02002617 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002618}
2619
2620static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
2622{
2623 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2624 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2625 return 0;
2626}
2627
2628static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2630{
2631 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2632 unsigned int old_mode = hook->mute_mode;
2633
2634 hook->mute_mode = ucontrol->value.enumerated.item[0];
2635 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2636 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2637 if (old_mode == hook->mute_mode)
2638 return 0;
2639 snd_hda_sync_vmaster_hook(hook);
2640 return 1;
2641}
2642
2643static struct snd_kcontrol_new vmaster_mute_mode = {
2644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2645 .name = "Mute-LED Mode",
2646 .info = vmaster_mute_mode_info,
2647 .get = vmaster_mute_mode_get,
2648 .put = vmaster_mute_mode_put,
2649};
2650
Takashi Iwai95a962c2014-10-29 16:03:58 +01002651/**
2652 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2653 * @codec: the HDA codec
2654 * @hook: the vmaster hook object
2655 * @expose_enum_ctl: flag to create an enum ctl
2656 *
2657 * Add a mute-LED hook with the given vmaster switch kctl.
2658 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2659 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002660 */
2661int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002662 struct hda_vmaster_mute_hook *hook,
2663 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002664{
2665 struct snd_kcontrol *kctl;
2666
2667 if (!hook->hook || !hook->sw_kctl)
2668 return 0;
2669 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2670 hook->codec = codec;
2671 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002672 if (!expose_enum_ctl)
2673 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002674 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2675 if (!kctl)
2676 return -ENOMEM;
2677 return snd_hda_ctl_add(codec, 0, kctl);
2678}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002679EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002680
Takashi Iwai95a962c2014-10-29 16:03:58 +01002681/**
2682 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2683 * @hook: the vmaster hook
2684 *
2685 * Call the hook with the current value for synchronization.
2686 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002687 */
2688void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2689{
2690 if (!hook->hook || !hook->codec)
2691 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002692 /* don't call vmaster hook in the destructor since it might have
2693 * been already destroyed
2694 */
2695 if (hook->codec->bus->shutdown)
2696 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002697 switch (hook->mute_mode) {
2698 case HDA_VMUTE_FOLLOW_MASTER:
2699 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2700 break;
2701 default:
2702 hook->hook(hook->codec, hook->mute_mode);
2703 break;
2704 }
2705}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002706EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002707
2708
Takashi Iwaid5191e52009-11-16 14:58:17 +01002709/**
2710 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002711 * @kcontrol: referred ctl element
2712 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002713 *
2714 * The control element is supposed to have the private_value field
2715 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2716 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 int chs = get_amp_channels(kcontrol);
2721
2722 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2723 uinfo->count = chs == 3 ? 2 : 1;
2724 uinfo->value.integer.min = 0;
2725 uinfo->value.integer.max = 1;
2726 return 0;
2727}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002728EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Takashi Iwaid5191e52009-11-16 14:58:17 +01002730/**
2731 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002732 * @kcontrol: ctl element
2733 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002734 *
2735 * The control element is supposed to have the private_value field
2736 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2737 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2742 hda_nid_t nid = get_amp_nid(kcontrol);
2743 int chs = get_amp_channels(kcontrol);
2744 int dir = get_amp_direction(kcontrol);
2745 int idx = get_amp_index(kcontrol);
2746 long *valp = ucontrol->value.integer.value;
2747
2748 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002750 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002752 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002753 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return 0;
2755}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002756EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Takashi Iwaid5191e52009-11-16 14:58:17 +01002758/**
2759 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002760 * @kcontrol: ctl element
2761 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002762 *
2763 * The control element is supposed to have the private_value field
2764 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2765 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 hda_nid_t nid = get_amp_nid(kcontrol);
2771 int chs = get_amp_channels(kcontrol);
2772 int dir = get_amp_direction(kcontrol);
2773 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 long *valp = ucontrol->value.integer.value;
2775 int change = 0;
2776
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002777 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002778 change = codec_amp_update(codec, nid, 0, dir, idx,
2779 HDA_AMP_MUTE,
2780 *valp ? 0 : HDA_AMP_MUTE, false,
2781 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002782 valp++;
2783 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002784 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002785 change |= codec_amp_update(codec, nid, 1, dir, idx,
2786 HDA_AMP_MUTE,
2787 *valp ? 0 : HDA_AMP_MUTE, false,
2788 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002789 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return change;
2791}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002792EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794/*
Takashi Iwai985be542005-11-02 18:26:49 +01002795 * bound volume controls
2796 *
2797 * bind multiple volumes (# indices, from 0)
2798 */
2799
2800#define AMP_VAL_IDX_SHIFT 19
2801#define AMP_VAL_IDX_MASK (0x0f<<19)
2802
Takashi Iwaid5191e52009-11-16 14:58:17 +01002803/**
2804 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002805 * @kcontrol: ctl element
2806 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002807 *
2808 * The control element is supposed to have the private_value field
2809 * set up via HDA_BIND_MUTE*() macros.
2810 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002813{
2814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2815 unsigned long pval;
2816 int err;
2817
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002818 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002819 pval = kcontrol->private_value;
2820 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2821 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2822 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002823 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002824 return err;
2825}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002826EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002827
Takashi Iwaid5191e52009-11-16 14:58:17 +01002828/**
2829 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002830 * @kcontrol: ctl element
2831 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002832 *
2833 * The control element is supposed to have the private_value field
2834 * set up via HDA_BIND_MUTE*() macros.
2835 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002836int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002838{
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 unsigned long pval;
2841 int i, indices, err = 0, change = 0;
2842
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002843 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002844 pval = kcontrol->private_value;
2845 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2846 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2848 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002849 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2850 if (err < 0)
2851 break;
2852 change |= err;
2853 }
2854 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002855 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002856 return err < 0 ? err : change;
2857}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002858EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002859
Takashi Iwaid5191e52009-11-16 14:58:17 +01002860/**
2861 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002862 * @kcontrol: referred ctl element
2863 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002864 *
2865 * The control element is supposed to have the private_value field
2866 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002867 */
2868int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_info *uinfo)
2870{
2871 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2872 struct hda_bind_ctls *c;
2873 int err;
2874
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002875 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002876 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002877 kcontrol->private_value = *c->values;
2878 err = c->ops->info(kcontrol, uinfo);
2879 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002881 return err;
2882}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002883EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002884
Takashi Iwaid5191e52009-11-16 14:58:17 +01002885/**
2886 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002887 * @kcontrol: ctl element
2888 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002889 *
2890 * The control element is supposed to have the private_value field
2891 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2892 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002893int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_value *ucontrol)
2895{
2896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2897 struct hda_bind_ctls *c;
2898 int err;
2899
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002900 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002901 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002902 kcontrol->private_value = *c->values;
2903 err = c->ops->get(kcontrol, ucontrol);
2904 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002905 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002906 return err;
2907}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002908EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002909
Takashi Iwaid5191e52009-11-16 14:58:17 +01002910/**
2911 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002912 * @kcontrol: ctl element
2913 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002914 *
2915 * The control element is supposed to have the private_value field
2916 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2917 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002918int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
2921 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2922 struct hda_bind_ctls *c;
2923 unsigned long *vals;
2924 int err = 0, change = 0;
2925
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002926 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002927 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002928 for (vals = c->values; *vals; vals++) {
2929 kcontrol->private_value = *vals;
2930 err = c->ops->put(kcontrol, ucontrol);
2931 if (err < 0)
2932 break;
2933 change |= err;
2934 }
2935 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002936 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002937 return err < 0 ? err : change;
2938}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002939EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002940
Takashi Iwaid5191e52009-11-16 14:58:17 +01002941/**
2942 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002943 * @kcontrol: ctl element
2944 * @op_flag: operation flag
2945 * @size: byte size of input TLV
2946 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002947 *
2948 * The control element is supposed to have the private_value field
2949 * set up via HDA_BIND_VOL() macro.
2950 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002951int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2952 unsigned int size, unsigned int __user *tlv)
2953{
2954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2955 struct hda_bind_ctls *c;
2956 int err;
2957
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002958 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002959 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002960 kcontrol->private_value = *c->values;
2961 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2962 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002963 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002964 return err;
2965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002966EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002967
2968struct hda_ctl_ops snd_hda_bind_vol = {
2969 .info = snd_hda_mixer_amp_volume_info,
2970 .get = snd_hda_mixer_amp_volume_get,
2971 .put = snd_hda_mixer_amp_volume_put,
2972 .tlv = snd_hda_mixer_amp_tlv
2973};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002974EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002975
2976struct hda_ctl_ops snd_hda_bind_sw = {
2977 .info = snd_hda_mixer_amp_switch_info,
2978 .get = snd_hda_mixer_amp_switch_get,
2979 .put = snd_hda_mixer_amp_switch_put,
2980 .tlv = snd_hda_mixer_amp_tlv
2981};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002982EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002983
2984/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * SPDIF out controls
2986 */
2987
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2992 uinfo->count = 1;
2993 return 0;
2994}
2995
Takashi Iwai0ba21762007-04-16 11:29:14 +02002996static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3000 IEC958_AES0_NONAUDIO |
3001 IEC958_AES0_CON_EMPHASIS_5015 |
3002 IEC958_AES0_CON_NOT_COPYRIGHT;
3003 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3004 IEC958_AES1_CON_ORIGINAL;
3005 return 0;
3006}
3007
Takashi Iwai0ba21762007-04-16 11:29:14 +02003008static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3012 IEC958_AES0_NONAUDIO |
3013 IEC958_AES0_PRO_EMPHASIS_5015;
3014 return 0;
3015}
3016
Takashi Iwai0ba21762007-04-16 11:29:14 +02003017static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003021 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003022 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003024 mutex_lock(&codec->spdif_mutex);
3025 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003026 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3027 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3028 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3029 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003030 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 return 0;
3033}
3034
3035/* convert from SPDIF status bits to HDA SPDIF bits
3036 * bit 0 (DigEn) is always set zero (to be filled later)
3037 */
3038static unsigned short convert_from_spdif_status(unsigned int sbits)
3039{
3040 unsigned short val = 0;
3041
3042 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003043 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003045 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003047 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3048 IEC958_AES0_PRO_EMPHASIS_5015)
3049 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003051 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3052 IEC958_AES0_CON_EMPHASIS_5015)
3053 val |= AC_DIG1_EMPHASIS;
3054 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3055 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3059 }
3060 return val;
3061}
3062
3063/* convert to SPDIF status bits from HDA SPDIF bits
3064 */
3065static unsigned int convert_to_spdif_status(unsigned short val)
3066{
3067 unsigned int sbits = 0;
3068
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003071 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 sbits |= IEC958_AES0_PROFESSIONAL;
3073 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003074 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3076 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003077 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003079 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3083 sbits |= val & (0x7f << 8);
3084 }
3085 return sbits;
3086}
3087
Takashi Iwai2f728532008-09-25 16:32:41 +02003088/* set digital convert verbs both for the given NID and its slaves */
3089static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3090 int verb, int val)
3091{
Takashi Iwaidda14412011-05-02 11:29:30 +02003092 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003093
Takashi Iwai9e976972008-11-25 08:17:20 +01003094 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003095 d = codec->slave_dig_outs;
3096 if (!d)
3097 return;
3098 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003099 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003100}
3101
3102static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3103 int dig1, int dig2)
3104{
3105 if (dig1 != -1)
3106 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3107 if (dig2 != -1)
3108 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3109}
3110
Takashi Iwai0ba21762007-04-16 11:29:14 +02003111static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3112 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003115 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003116 struct hda_spdif_out *spdif;
3117 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 unsigned short val;
3119 int change;
3120
Ingo Molnar62932df2006-01-16 16:34:20 +01003121 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003122 spdif = snd_array_elem(&codec->spdif_out, idx);
3123 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003124 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3126 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3127 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003128 val = convert_from_spdif_status(spdif->status);
3129 val |= spdif->ctls & 1;
3130 change = spdif->ctls != val;
3131 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003132 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003133 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003134 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return change;
3136}
3137
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003138#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Takashi Iwai0ba21762007-04-16 11:29:14 +02003140static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003144 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003145 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003147 mutex_lock(&codec->spdif_mutex);
3148 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003149 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003150 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return 0;
3152}
3153
Stephen Warren74b654c2011-06-01 11:14:18 -06003154static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3155 int dig1, int dig2)
3156{
3157 set_dig_out_convert(codec, nid, dig1, dig2);
3158 /* unmute amp switch (if any) */
3159 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3160 (dig1 & AC_DIG1_ENABLE))
3161 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3162 HDA_AMP_MUTE, 0);
3163}
3164
Takashi Iwai0ba21762007-04-16 11:29:14 +02003165static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3166 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
3168 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003169 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003170 struct hda_spdif_out *spdif;
3171 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 unsigned short val;
3173 int change;
3174
Ingo Molnar62932df2006-01-16 16:34:20 +01003175 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003176 spdif = snd_array_elem(&codec->spdif_out, idx);
3177 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003178 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003180 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003181 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003182 spdif->ctls = val;
3183 if (change && nid != (u16)-1)
3184 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003185 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return change;
3187}
3188
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003189static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 {
3191 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3192 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003193 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 .info = snd_hda_spdif_mask_info,
3195 .get = snd_hda_spdif_cmask_get,
3196 },
3197 {
3198 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3199 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003200 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 .info = snd_hda_spdif_mask_info,
3202 .get = snd_hda_spdif_pmask_get,
3203 },
3204 {
3205 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003206 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 .info = snd_hda_spdif_mask_info,
3208 .get = snd_hda_spdif_default_get,
3209 .put = snd_hda_spdif_default_put,
3210 },
3211 {
3212 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003213 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 .info = snd_hda_spdif_out_switch_info,
3215 .get = snd_hda_spdif_out_switch_get,
3216 .put = snd_hda_spdif_out_switch_put,
3217 },
3218 { } /* end */
3219};
3220
3221/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003222 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003224 * @associated_nid: NID that new ctls associated with
3225 * @cvt_nid: converter NID
3226 * @type: HDA_PCM_TYPE_*
3227 * Creates controls related with the digital output.
3228 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 *
3230 * Returns 0 if successful, or a negative error code.
3231 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003232int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3233 hda_nid_t associated_nid,
3234 hda_nid_t cvt_nid,
3235 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003238 struct snd_kcontrol *kctl;
3239 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003240 int idx = 0;
3241 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003242 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003243 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003245 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003246 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003247 idx = spdif_index;
3248 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003249 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003250 /* suppose a single SPDIF device */
3251 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3252 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3253 if (!kctl)
3254 break;
3255 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003256 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003257 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003258 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003259 if (!bus->primary_dig_out_type)
3260 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003261
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003262 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003263 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003264 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003265 return -EBUSY;
3266 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003267 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003268 if (!spdif)
3269 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3271 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003272 if (!kctl)
3273 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003274 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003275 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003276 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003277 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return err;
3279 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003280 spdif->nid = cvt_nid;
3281 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003282 AC_VERB_GET_DIGI_CONVERT_1, 0);
3283 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return 0;
3285}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003286EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Takashi Iwai95a962c2014-10-29 16:03:58 +01003288/**
3289 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3290 * @codec: the HDA codec
3291 * @nid: widget NID
3292 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003293 * call within spdif_mutex lock
3294 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003295struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3296 hda_nid_t nid)
3297{
3298 int i;
3299 for (i = 0; i < codec->spdif_out.used; i++) {
3300 struct hda_spdif_out *spdif =
3301 snd_array_elem(&codec->spdif_out, i);
3302 if (spdif->nid == nid)
3303 return spdif;
3304 }
3305 return NULL;
3306}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003307EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003308
Takashi Iwai95a962c2014-10-29 16:03:58 +01003309/**
3310 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3311 * @codec: the HDA codec
3312 * @idx: the SPDIF ctl index
3313 *
3314 * Unassign the widget from the given SPDIF control.
3315 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003316void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3317{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003318 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003319
3320 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003321 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003322 spdif->nid = (u16)-1;
3323 mutex_unlock(&codec->spdif_mutex);
3324}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003325EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003326
Takashi Iwai95a962c2014-10-29 16:03:58 +01003327/**
3328 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3329 * @codec: the HDA codec
3330 * @idx: the SPDIF ctl idx
3331 * @nid: widget NID
3332 *
3333 * Assign the widget to the SPDIF control with the given index.
3334 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003335void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3336{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003337 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003338 unsigned short val;
3339
3340 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003341 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003342 if (spdif->nid != nid) {
3343 spdif->nid = nid;
3344 val = spdif->ctls;
3345 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3346 }
3347 mutex_unlock(&codec->spdif_mutex);
3348}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003349EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003352 * SPDIF sharing with analog output
3353 */
3354static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3355 struct snd_ctl_elem_value *ucontrol)
3356{
3357 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3358 ucontrol->value.integer.value[0] = mout->share_spdif;
3359 return 0;
3360}
3361
3362static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3363 struct snd_ctl_elem_value *ucontrol)
3364{
3365 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3366 mout->share_spdif = !!ucontrol->value.integer.value[0];
3367 return 0;
3368}
3369
3370static struct snd_kcontrol_new spdif_share_sw = {
3371 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3372 .name = "IEC958 Default PCM Playback Switch",
3373 .info = snd_ctl_boolean_mono_info,
3374 .get = spdif_share_sw_get,
3375 .put = spdif_share_sw_put,
3376};
3377
Takashi Iwaid5191e52009-11-16 14:58:17 +01003378/**
3379 * snd_hda_create_spdif_share_sw - create Default PCM switch
3380 * @codec: the HDA codec
3381 * @mout: multi-out instance
3382 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003383int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3384 struct hda_multi_out *mout)
3385{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003386 struct snd_kcontrol *kctl;
3387
Takashi Iwai9a081602008-02-12 18:37:26 +01003388 if (!mout->dig_out_nid)
3389 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003390
3391 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3392 if (!kctl)
3393 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003394 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003395 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003396}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003397EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003398
3399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 * SPDIF input
3401 */
3402
3403#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3404
Takashi Iwai0ba21762007-04-16 11:29:14 +02003405static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3409
3410 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3411 return 0;
3412}
3413
Takashi Iwai0ba21762007-04-16 11:29:14 +02003414static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3418 hda_nid_t nid = kcontrol->private_value;
3419 unsigned int val = !!ucontrol->value.integer.value[0];
3420 int change;
3421
Ingo Molnar62932df2006-01-16 16:34:20 +01003422 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003424 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003426 snd_hda_codec_write_cache(codec, nid, 0,
3427 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003429 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return change;
3431}
3432
Takashi Iwai0ba21762007-04-16 11:29:14 +02003433static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
3436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3437 hda_nid_t nid = kcontrol->private_value;
3438 unsigned short val;
3439 unsigned int sbits;
3440
Andrew Paprocki3982d172007-12-19 12:13:44 +01003441 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 sbits = convert_to_spdif_status(val);
3443 ucontrol->value.iec958.status[0] = sbits;
3444 ucontrol->value.iec958.status[1] = sbits >> 8;
3445 ucontrol->value.iec958.status[2] = sbits >> 16;
3446 ucontrol->value.iec958.status[3] = sbits >> 24;
3447 return 0;
3448}
3449
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003450static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 {
3452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003453 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 .info = snd_hda_spdif_in_switch_info,
3455 .get = snd_hda_spdif_in_switch_get,
3456 .put = snd_hda_spdif_in_switch_put,
3457 },
3458 {
3459 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003461 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 .info = snd_hda_spdif_mask_info,
3463 .get = snd_hda_spdif_in_status_get,
3464 },
3465 { } /* end */
3466};
3467
3468/**
3469 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3470 * @codec: the HDA codec
3471 * @nid: audio in widget NID
3472 *
3473 * Creates controls related with the SPDIF input.
3474 * Called from each patch supporting the SPDIF in.
3475 *
3476 * Returns 0 if successful, or a negative error code.
3477 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003478int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003481 struct snd_kcontrol *kctl;
3482 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003483 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Takashi Iwaidcda5802012-10-12 17:24:51 +02003485 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003486 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003487 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003488 return -EBUSY;
3489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3491 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003492 if (!kctl)
3493 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003495 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return err;
3498 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003499 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003500 snd_hda_codec_read(codec, nid, 0,
3501 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003502 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return 0;
3504}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003505EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003507/*
3508 * command cache
3509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Takashi Iwaic370dd62012-12-13 18:30:04 +01003511/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003512#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3513#define get_cmd_cache_nid(key) ((key) & 0xff)
3514#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3515
3516/**
3517 * snd_hda_codec_write_cache - send a single command with caching
3518 * @codec: the HDA codec
3519 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003520 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003521 * @verb: the verb to send
3522 * @parm: the parameter for the verb
3523 *
3524 * Send a single command without waiting for response.
3525 *
3526 * Returns 0 if successful, or a negative error code.
3527 */
3528int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003529 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003530{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003531 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003532 struct hda_cache_head *c;
3533 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003534 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003535
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003536 cache_only = codec->cached_write;
3537 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003538 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003539 if (err < 0)
3540 return err;
3541 }
3542
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003543 /* parm may contain the verb stuff for get/set amp */
3544 verb = verb | (parm >> 8);
3545 parm &= 0xff;
3546 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003547 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003548 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003549 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003550 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003551 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003552 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003553 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003554 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003555}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003556EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003557
Takashi Iwaid5191e52009-11-16 14:58:17 +01003558/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003559 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3560 * @codec: the HDA codec
3561 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003562 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003563 * @verb: the verb to send
3564 * @parm: the parameter for the verb
3565 *
3566 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3567 * command if the parameter is already identical with the cached value.
3568 * If not, it sends the command and refreshes the cache.
3569 *
3570 * Returns 0 if successful, or a negative error code.
3571 */
3572int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003573 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003574{
3575 struct hda_cache_head *c;
3576 u32 key;
3577
3578 /* parm may contain the verb stuff for get/set amp */
3579 verb = verb | (parm >> 8);
3580 parm &= 0xff;
3581 key = build_cmd_cache_key(nid, verb);
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003582 mutex_lock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003583 c = get_hash(&codec->cmd_cache, key);
3584 if (c && c->val == parm) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003585 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003586 return 0;
3587 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003588 mutex_unlock(&codec->bus->core.cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003589 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003590}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003591EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003592
3593/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003594 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3595 * @codec: HD-audio codec
3596 *
3597 * Execute all verbs recorded in the command caches to resume.
3598 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003599void snd_hda_codec_resume_cache(struct hda_codec *codec)
3600{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003601 int i;
3602
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003604 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003605 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3606 struct hda_cache_head *buffer;
3607 u32 key;
3608
3609 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3610 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003611 if (!key)
3612 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003613 if (!buffer->dirty)
3614 continue;
3615 buffer->dirty = 0;
3616 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003617 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3618 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003619 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003620 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003621 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003622}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003623EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003624
3625/**
3626 * snd_hda_sequence_write_cache - sequence writes with caching
3627 * @codec: the HDA codec
3628 * @seq: VERB array to send
3629 *
3630 * Send the commands sequentially from the given array.
3631 * Thte commands are recorded on cache for power-save and resume.
3632 * The array must be terminated with NID=0.
3633 */
3634void snd_hda_sequence_write_cache(struct hda_codec *codec,
3635 const struct hda_verb *seq)
3636{
3637 for (; seq->nid; seq++)
3638 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3639 seq->param);
3640}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003641EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003642
Takashi Iwaidc870f32013-01-22 15:24:30 +01003643/**
3644 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3645 * @codec: HD-audio codec
3646 */
3647void snd_hda_codec_flush_cache(struct hda_codec *codec)
3648{
3649 snd_hda_codec_resume_amp(codec);
3650 snd_hda_codec_resume_cache(codec);
3651}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003652EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003653
Takashi Iwai95a962c2014-10-29 16:03:58 +01003654/**
3655 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3656 * @codec: the HDA codec
3657 * @fg: function group (not used now)
3658 * @power_state: the power state to set (AC_PWRST_*)
3659 *
3660 * Set the given power state to all widgets that have the power control.
3661 * If the codec has power_filter set, it evaluates the power state and
3662 * filter out if it's unchanged as D3.
3663 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003664void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003665 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003666{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003667 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003668 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003669
Takashi Iwaicb53c622007-08-10 17:21:45 +02003670 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003671 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003672 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003673 if (!(wcaps & AC_WCAP_POWER))
3674 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003675 if (codec->power_filter) {
3676 state = codec->power_filter(codec, nid, power_state);
3677 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003678 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003679 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003680 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003681 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003682 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003683}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003684EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003685
3686/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003687 * supported power states check
3688 */
3689static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3690 unsigned int power_state)
3691{
3692 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3693
Mengdong Line037cb42012-08-10 14:11:58 +02003694 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003695 return false;
3696 if (sup & power_state)
3697 return true;
3698 else
3699 return false;
3700}
3701
3702/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003703 * wait until the state is reached, returns the current state
3704 */
3705static unsigned int hda_sync_power_state(struct hda_codec *codec,
3706 hda_nid_t fg,
3707 unsigned int power_state)
3708{
3709 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3710 unsigned int state, actual_state;
3711
3712 for (;;) {
3713 state = snd_hda_codec_read(codec, fg, 0,
3714 AC_VERB_GET_POWER_STATE, 0);
3715 if (state & AC_PWRST_ERROR)
3716 break;
3717 actual_state = (state >> 4) & 0x0f;
3718 if (actual_state == power_state)
3719 break;
3720 if (time_after_eq(jiffies, end_time))
3721 break;
3722 /* wait until the codec reachs to the target state */
3723 msleep(1);
3724 }
3725 return state;
3726}
3727
Takashi Iwai95a962c2014-10-29 16:03:58 +01003728/**
3729 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3730 * @codec: the HDA codec
3731 * @nid: widget NID
3732 * @power_state: power state to evalue
3733 *
3734 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3735 * This can be used a codec power_filter callback.
3736 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003737unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3738 hda_nid_t nid,
3739 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003740{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003741 if (nid == codec->afg || nid == codec->mfg)
3742 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003743 if (power_state == AC_PWRST_D3 &&
3744 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3745 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3746 int eapd = snd_hda_codec_read(codec, nid, 0,
3747 AC_VERB_GET_EAPD_BTLENABLE, 0);
3748 if (eapd & 0x02)
3749 return AC_PWRST_D0;
3750 }
3751 return power_state;
3752}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003753EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003754
Takashi Iwai432c6412012-08-28 09:59:20 -07003755/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003756 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003757 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003758static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003759 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003760{
Takashi Iwaid8193872012-08-31 07:54:38 -07003761 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003762 int count;
3763 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003764 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003765
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003766 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003767 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003768 if (codec->depop_delay < 0)
3769 msleep(codec->epss ? 10 : 100);
3770 else if (codec->depop_delay > 0)
3771 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003772 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003773 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003774
3775 /* repeat power states setting at most 10 times*/
3776 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003777 if (codec->patch_ops.set_power_state)
3778 codec->patch_ops.set_power_state(codec, fg,
3779 power_state);
3780 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003781 state = power_state;
3782 if (codec->power_filter)
3783 state = codec->power_filter(codec, fg, state);
3784 if (state == power_state || power_state != AC_PWRST_D3)
3785 snd_hda_codec_read(codec, fg, flags,
3786 AC_VERB_SET_POWER_STATE,
3787 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003788 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003789 }
3790 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003791 if (!(state & AC_PWRST_ERROR))
3792 break;
3793 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003794
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003795 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003796}
Takashi Iwai54d17402005-11-21 16:33:22 +01003797
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003798/* sync power states of all widgets;
3799 * this is called at the end of codec parsing
3800 */
3801static void sync_power_up_states(struct hda_codec *codec)
3802{
3803 hda_nid_t nid = codec->start_nid;
3804 int i;
3805
Takashi Iwaiba615b82013-03-13 14:47:21 +01003806 /* don't care if no filter is used */
3807 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003808 return;
3809
3810 for (i = 0; i < codec->num_nodes; i++, nid++) {
3811 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003812 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003813 if (!(wcaps & AC_WCAP_POWER))
3814 continue;
3815 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3816 if (target == AC_PWRST_D0)
3817 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003818 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003819 snd_hda_codec_write(codec, nid, 0,
3820 AC_VERB_SET_POWER_STATE, target);
3821 }
3822}
3823
Takashi Iwai648a8d22014-02-25 10:38:13 +01003824#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003825/* execute additional init verbs */
3826static void hda_exec_init_verbs(struct hda_codec *codec)
3827{
3828 if (codec->init_verbs.list)
3829 snd_hda_sequence_write(codec, codec->init_verbs.list);
3830}
3831#else
3832static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3833#endif
3834
Takashi Iwai2a439522011-07-26 09:52:50 +02003835#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003836/* update the power on/off account with the current jiffies */
3837static void update_power_acct(struct hda_codec *codec, bool on)
3838{
3839 unsigned long delta = jiffies - codec->power_jiffies;
3840
3841 if (on)
3842 codec->power_on_acct += delta;
3843 else
3844 codec->power_off_acct += delta;
3845 codec->power_jiffies += delta;
3846}
3847
3848void snd_hda_update_power_acct(struct hda_codec *codec)
3849{
3850 update_power_acct(codec, hda_codec_is_power_on(codec));
3851}
3852
Takashi Iwaicb53c622007-08-10 17:21:45 +02003853/*
3854 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003855 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003856 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003857static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003858{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003859 unsigned int state;
3860
Takashi Iwaicc72da72015-02-19 16:00:22 +01003861 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003862
Takashi Iwaicb53c622007-08-10 17:21:45 +02003863 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003864 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003865 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003866 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003867 update_power_acct(codec, true);
3868 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003869 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003870}
3871
Takashi Iwaic370dd62012-12-13 18:30:04 +01003872/* mark all entries of cmd and amp caches dirty */
3873static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3874{
3875 int i;
3876 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3877 struct hda_cache_head *cmd;
3878 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3879 cmd->dirty = 1;
3880 }
3881 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3882 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003883 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003884 amp->head.dirty = 1;
3885 }
3886}
3887
Takashi Iwaicb53c622007-08-10 17:21:45 +02003888/*
3889 * kick up codec; used both from PM and power-save
3890 */
3891static void hda_call_codec_resume(struct hda_codec *codec)
3892{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003893 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003894
Takashi Iwaic370dd62012-12-13 18:30:04 +01003895 hda_mark_cmd_cache_dirty(codec);
3896
Takashi Iwaicc72da72015-02-19 16:00:22 +01003897 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003898
Takashi Iwaid8193872012-08-31 07:54:38 -07003899 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003900 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003901 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003902 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003903 if (codec->patch_ops.resume)
3904 codec->patch_ops.resume(codec);
3905 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003906 if (codec->patch_ops.init)
3907 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003908 snd_hda_codec_resume_amp(codec);
3909 snd_hda_codec_resume_cache(codec);
3910 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003911
3912 if (codec->jackpoll_interval)
3913 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003914 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003915 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003916 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003917}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003918
Takashi Iwaicc72da72015-02-19 16:00:22 +01003919static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003920{
3921 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003922 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003923 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003924
3925 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003926 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3927 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003928 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003929 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003930 clear_bit(codec->core.addr, &codec->bus->core.codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003931 return 0;
3932}
3933
Takashi Iwaicc72da72015-02-19 16:00:22 +01003934static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003935{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003936 struct hda_codec *codec = dev_to_hda_codec(dev);
3937
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003938 set_bit(codec->core.addr, &codec->bus->core.codec_powered);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003939 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003940 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003941 return 0;
3942}
Takashi Iwai2a439522011-07-26 09:52:50 +02003943#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003944
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003945/* referred in hda_bind.c */
3946const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003947 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3948 pm_runtime_force_resume)
3949 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3950 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003951};
Takashi Iwai54d17402005-11-21 16:33:22 +01003952
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003953/*
3954 * add standard channel maps if not specified
3955 */
3956static int add_std_chmaps(struct hda_codec *codec)
3957{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003958 struct hda_pcm *pcm;
3959 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003960
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003961 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003962 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003963 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003964 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003965 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003966
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003967 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003968 continue;
3969 if (!pcm || !hinfo->substreams)
3970 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003971 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003972 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003973 hinfo->channels_max,
3974 0, &chmap);
3975 if (err < 0)
3976 return err;
3977 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3978 }
3979 }
3980 return 0;
3981}
3982
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003983/* default channel maps for 2.1 speakers;
3984 * since HD-audio supports only stereo, odd number channels are omitted
3985 */
3986const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3987 { .channels = 2,
3988 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3989 { .channels = 4,
3990 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3991 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3992 { }
3993};
3994EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3995
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003996int snd_hda_codec_build_controls(struct hda_codec *codec)
3997{
3998 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003999 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004000 /* continue to initialize... */
4001 if (codec->patch_ops.init)
4002 err = codec->patch_ops.init(codec);
4003 if (!err && codec->patch_ops.build_controls)
4004 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004005 if (err < 0)
4006 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004007
4008 /* we create chmaps here instead of build_pcms */
4009 err = add_std_chmaps(codec);
4010 if (err < 0)
4011 return err;
4012
David Henningsson26a6cb62012-10-09 15:04:21 +02004013 if (codec->jackpoll_interval)
4014 hda_jackpoll_work(&codec->jackpoll_work.work);
4015 else
4016 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004017 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return 0;
4019}
4020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021/*
4022 * stream formats
4023 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004024struct hda_rate_tbl {
4025 unsigned int hz;
4026 unsigned int alsa_bits;
4027 unsigned int hda_fmt;
4028};
4029
Takashi Iwai92f10b32010-08-03 14:21:00 +02004030/* rate = base * mult / div */
4031#define HDA_RATE(base, mult, div) \
4032 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4033 (((div) - 1) << AC_FMT_DIV_SHIFT))
4034
Takashi Iwaibefdf312005-08-22 13:57:55 +02004035static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004037
4038 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004039 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4040 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4041 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4042 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4043 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4044 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4045 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4046 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4047 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4048 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4049 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004050#define AC_PAR_PCM_RATE_BITS 11
4051 /* up to bits 10, 384kHZ isn't supported properly */
4052
4053 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004054 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004055
Takashi Iwaibefdf312005-08-22 13:57:55 +02004056 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057};
4058
4059/**
4060 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004061 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 * @rate: the sample rate
4063 * @channels: the number of channels
4064 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4065 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004066 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 *
4068 * Calculate the format bitset from the given rate, channels and th PCM format.
4069 *
4070 * Return zero if invalid.
4071 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004072unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4073 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 unsigned int channels,
4075 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004076 unsigned int maxbps,
4077 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 int i;
4080 unsigned int val = 0;
4081
Takashi Iwaibefdf312005-08-22 13:57:55 +02004082 for (i = 0; rate_bits[i].hz; i++)
4083 if (rate_bits[i].hz == rate) {
4084 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 break;
4086 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004087 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004088 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return 0;
4090 }
4091
4092 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004093 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 return 0;
4095 }
4096 val |= channels - 1;
4097
4098 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004099 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004100 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004101 break;
4102 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004103 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 case 20:
4106 case 24:
4107 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004108 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004109 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004111 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004113 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 break;
4115 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004116 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004117 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return 0;
4119 }
4120
Anssi Hannula32c168c2010-08-03 13:28:57 +03004121 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004122 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 return val;
4125}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004126EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004128static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4129 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004130{
4131 unsigned int val = 0;
4132 if (nid != codec->afg &&
4133 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4134 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4135 if (!val || val == -1)
4136 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4137 if (!val || val == -1)
4138 return 0;
4139 return val;
4140}
4141
4142static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4143{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004144 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004145 get_pcm_param);
4146}
4147
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004148static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4149 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004150{
4151 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4152 if (!streams || streams == -1)
4153 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4154 if (!streams || streams == -1)
4155 return 0;
4156 return streams;
4157}
4158
4159static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4160{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004161 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004162 get_stream_param);
4163}
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165/**
4166 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4167 * @codec: the HDA codec
4168 * @nid: NID to query
4169 * @ratesp: the pointer to store the detected rate bitflags
4170 * @formatsp: the pointer to store the detected formats
4171 * @bpsp: the pointer to store the detected format widths
4172 *
4173 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4174 * or @bsps argument is ignored.
4175 *
4176 * Returns 0 if successful, otherwise a negative error code.
4177 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004178int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4180{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004181 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004183 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004184 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
4186 if (ratesp) {
4187 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004188 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004190 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004192 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004193 codec_err(codec,
4194 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4195 nid, val,
4196 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004197 return -EIO;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 *ratesp = rates;
4200 }
4201
4202 if (formatsp || bpsp) {
4203 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004204 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004206 streams = query_stream_param(codec, nid);
4207 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 bps = 0;
4211 if (streams & AC_SUPFMT_PCM) {
4212 if (val & AC_SUPPCM_BITS_8) {
4213 formats |= SNDRV_PCM_FMTBIT_U8;
4214 bps = 8;
4215 }
4216 if (val & AC_SUPPCM_BITS_16) {
4217 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4218 bps = 16;
4219 }
4220 if (wcaps & AC_WCAP_DIGITAL) {
4221 if (val & AC_SUPPCM_BITS_32)
4222 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4223 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4224 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4225 if (val & AC_SUPPCM_BITS_24)
4226 bps = 24;
4227 else if (val & AC_SUPPCM_BITS_20)
4228 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004229 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4230 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4232 if (val & AC_SUPPCM_BITS_32)
4233 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 else if (val & AC_SUPPCM_BITS_24)
4235 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004236 else if (val & AC_SUPPCM_BITS_20)
4237 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004240#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004241 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004243 if (!bps)
4244 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004245 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004246#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004247 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004248 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 /* temporary hack: we have still no proper support
4250 * for the direct AC3 stream...
4251 */
4252 formats |= SNDRV_PCM_FMTBIT_U8;
4253 bps = 8;
4254 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004255 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004256 codec_err(codec,
4257 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4258 nid, val,
4259 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4260 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004261 return -EIO;
4262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (formatsp)
4264 *formatsp = formats;
4265 if (bpsp)
4266 *bpsp = bps;
4267 }
4268
4269 return 0;
4270}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004271EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004274 * snd_hda_is_supported_format - Check the validity of the format
4275 * @codec: HD-audio codec
4276 * @nid: NID to check
4277 * @format: the HD-audio format value to check
4278 *
4279 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 *
4281 * Returns 1 if supported, 0 if not.
4282 */
4283int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4284 unsigned int format)
4285{
4286 int i;
4287 unsigned int val = 0, rate, stream;
4288
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004289 val = query_pcm_param(codec, nid);
4290 if (!val)
4291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004294 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004295 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 if (val & (1 << i))
4297 break;
4298 return 0;
4299 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004300 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return 0;
4302
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004303 stream = query_stream_param(codec, nid);
4304 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return 0;
4306
4307 if (stream & AC_SUPFMT_PCM) {
4308 switch (format & 0xf0) {
4309 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004310 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return 0;
4312 break;
4313 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004314 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return 0;
4316 break;
4317 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004318 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 0;
4320 break;
4321 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004322 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return 0;
4324 break;
4325 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004326 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return 0;
4328 break;
4329 default:
4330 return 0;
4331 }
4332 } else {
4333 /* FIXME: check for float32 and AC3? */
4334 }
4335
4336 return 1;
4337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004338EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340/*
4341 * PCM stuff
4342 */
4343static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4344 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004345 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 return 0;
4348}
4349
4350static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4351 struct hda_codec *codec,
4352 unsigned int stream_tag,
4353 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004354 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4357 return 0;
4358}
4359
4360static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4361 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004362 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
Takashi Iwai888afa12008-03-18 09:57:50 +01004364 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366}
4367
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004368static int set_pcm_default_values(struct hda_codec *codec,
4369 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004371 int err;
4372
Takashi Iwai0ba21762007-04-16 11:29:14 +02004373 /* query support PCM information from the given NID */
4374 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004375 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004376 info->rates ? NULL : &info->rates,
4377 info->formats ? NULL : &info->formats,
4378 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004379 if (err < 0)
4380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
4382 if (info->ops.open == NULL)
4383 info->ops.open = hda_pcm_default_open_close;
4384 if (info->ops.close == NULL)
4385 info->ops.close = hda_pcm_default_open_close;
4386 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004387 if (snd_BUG_ON(!info->nid))
4388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 info->ops.prepare = hda_pcm_default_prepare;
4390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004392 if (snd_BUG_ON(!info->nid))
4393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 info->ops.cleanup = hda_pcm_default_cleanup;
4395 }
4396 return 0;
4397}
4398
Takashi Iwaieb541332010-08-06 13:48:11 +02004399/*
4400 * codec prepare/cleanup entries
4401 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004402/**
4403 * snd_hda_codec_prepare - Prepare a stream
4404 * @codec: the HDA codec
4405 * @hinfo: PCM information
4406 * @stream: stream tag to assign
4407 * @format: format id to assign
4408 * @substream: PCM substream to assign
4409 *
4410 * Calls the prepare callback set by the codec with the given arguments.
4411 * Clean up the inactive streams when successful.
4412 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004413int snd_hda_codec_prepare(struct hda_codec *codec,
4414 struct hda_pcm_stream *hinfo,
4415 unsigned int stream,
4416 unsigned int format,
4417 struct snd_pcm_substream *substream)
4418{
4419 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004420 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004421 if (hinfo->ops.prepare)
4422 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4423 substream);
4424 else
4425 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004426 if (ret >= 0)
4427 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004428 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004429 return ret;
4430}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004431EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004432
Takashi Iwai95a962c2014-10-29 16:03:58 +01004433/**
4434 * snd_hda_codec_cleanup - Prepare a stream
4435 * @codec: the HDA codec
4436 * @hinfo: PCM information
4437 * @substream: PCM substream
4438 *
4439 * Calls the cleanup callback set by the codec with the given arguments.
4440 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004441void snd_hda_codec_cleanup(struct hda_codec *codec,
4442 struct hda_pcm_stream *hinfo,
4443 struct snd_pcm_substream *substream)
4444{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004445 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004446 if (hinfo->ops.cleanup)
4447 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004448 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004449}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004450EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004451
Takashi Iwaid5191e52009-11-16 14:58:17 +01004452/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004453const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4454 "Audio", "SPDIF", "HDMI", "Modem"
4455};
4456
Takashi Iwai176d5332008-07-30 15:01:44 +02004457/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004458 * get the empty PCM device number to assign
4459 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004460static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004461{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004462 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004463 /* assigned to static slots up to dev#10; if more needed, assign
4464 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4465 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004466 static int audio_idx[HDA_PCM_NTYPES][5] = {
4467 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4468 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004469 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004470 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004471 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004472 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004473
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004474 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004475 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004476 return -EINVAL;
4477 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004478
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004479 for (i = 0; audio_idx[type][i] >= 0; i++) {
4480#ifndef CONFIG_SND_DYNAMIC_MINORS
4481 if (audio_idx[type][i] >= 8)
4482 break;
4483#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004484 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4485 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004486 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004487
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004488#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004489 /* non-fixed slots starting from 10 */
4490 for (i = 10; i < 32; i++) {
4491 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4492 return i;
4493 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004494#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004495
Takashi Iwai4e76a882014-02-25 12:21:03 +01004496 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004497 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004498#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004499 dev_warn(bus->card->dev,
4500 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004501#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004502 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004503}
4504
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004505/* call build_pcms ops of the given codec and set up the default parameters */
4506int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004507{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004508 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004509 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004510
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004511 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004512 return 0; /* already parsed */
4513
4514 if (!codec->patch_ops.build_pcms)
4515 return 0;
4516
4517 err = codec->patch_ops.build_pcms(codec);
4518 if (err < 0) {
4519 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004520 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004521 return err;
4522 }
4523
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004524 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004525 int stream;
4526
4527 for (stream = 0; stream < 2; stream++) {
4528 struct hda_pcm_stream *info = &cpcm->stream[stream];
4529
4530 if (!info->substreams)
4531 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004532 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004533 if (err < 0) {
4534 codec_warn(codec,
4535 "fail to setup default for PCM %s\n",
4536 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004537 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004538 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004539 }
4540 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004541
4542 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004543}
4544
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004545/* assign all PCMs of the given codec */
4546int snd_hda_codec_build_pcms(struct hda_codec *codec)
4547{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004548 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004549 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004550 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004551
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004552 if (snd_BUG_ON(!bus->ops.attach_pcm))
4553 return -EINVAL;
4554
4555 err = snd_hda_codec_parse_pcms(codec);
4556 if (err < 0) {
4557 snd_hda_codec_reset(codec);
4558 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004559 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004560
4561 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004562 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004563 if (cpcm->pcm)
4564 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004565 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004566 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004567
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004568 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4569 if (dev < 0)
4570 continue; /* no fatal error */
4571 cpcm->device = dev;
4572 err = bus->ops.attach_pcm(bus, codec, cpcm);
4573 if (err < 0) {
4574 codec_err(codec,
4575 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004576 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004577 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004578 }
4579 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004580
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004581 return 0;
4582}
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 * snd_hda_add_new_ctls - create controls from the array
4586 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004587 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 *
4589 * This helper function creates and add new controls in the given array.
4590 * The array must be terminated with an empty entry as terminator.
4591 *
4592 * Returns 0 if successful, or a negative error code.
4593 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004594int snd_hda_add_new_ctls(struct hda_codec *codec,
4595 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
4599 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004600 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004601 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004602 if (knew->iface == -1) /* skip this codec private value */
4603 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004604 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004605 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004606 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004607 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004608 if (addr > 0)
4609 kctl->id.device = addr;
4610 if (idx > 0)
4611 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004612 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004613 if (!err)
4614 break;
4615 /* try first with another device index corresponding to
4616 * the codec addr; if it still fails (or it's the
4617 * primary codec), then try another control index
4618 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004619 if (!addr && codec->core.addr)
4620 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004621 else if (!idx && !knew->index) {
4622 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004623 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004624 if (idx <= 0)
4625 return err;
4626 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004627 return err;
4628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
4630 return 0;
4631}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004632EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Takashi Iwai83012a72012-08-24 18:38:08 +02004634#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004635/**
4636 * snd_hda_power_up - Power-up the codec
4637 * @codec: HD-audio codec
4638 *
4639 * Increment the usage counter and resume the device if not done yet.
4640 */
4641void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004642{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004643 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004644
Takashi Iwaicc72da72015-02-19 16:00:22 +01004645 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004646 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004647 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004648}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004649EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004650
4651/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004652 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004653 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004654 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004655 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004656 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004657void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004658{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004659 struct device *dev = hda_codec_dev(codec);
4660
4661 if (codec_in_pm(codec))
4662 return;
4663 pm_runtime_mark_last_busy(dev);
4664 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004665}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004666EXPORT_SYMBOL_GPL(snd_hda_power_down);
4667
Takashi Iwaibb573922015-02-20 09:26:04 +01004668static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004669{
4670 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004671
Takashi Iwaicc72da72015-02-19 16:00:22 +01004672 if (delay > 0) {
4673 pm_runtime_set_autosuspend_delay(dev, delay);
4674 pm_runtime_use_autosuspend(dev);
4675 pm_runtime_allow(dev);
4676 if (!pm_runtime_suspended(dev))
4677 pm_runtime_mark_last_busy(dev);
4678 } else {
4679 pm_runtime_dont_use_autosuspend(dev);
4680 pm_runtime_forbid(dev);
4681 }
4682}
Takashi Iwaibb573922015-02-20 09:26:04 +01004683
4684/**
4685 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4686 * @bus: HD-audio bus
4687 * @delay: autosuspend delay in msec, 0 = off
4688 *
4689 * Synchronize the runtime PM autosuspend state from the power_save option.
4690 */
4691void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4692{
4693 struct hda_codec *c;
4694
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004695 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01004696 codec_set_power_save(c, delay);
4697}
4698EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004699
Takashi Iwaid5191e52009-11-16 14:58:17 +01004700/**
4701 * snd_hda_check_amp_list_power - Check the amp list and update the power
4702 * @codec: HD-audio codec
4703 * @check: the object containing an AMP list and the status
4704 * @nid: NID to check / update
4705 *
4706 * Check whether the given NID is in the amp list. If it's in the list,
4707 * check the current AMP status, and update the the power-status according
4708 * to the mute status.
4709 *
4710 * This function is supposed to be set or called from the check_power_status
4711 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004712 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004713int snd_hda_check_amp_list_power(struct hda_codec *codec,
4714 struct hda_loopback_check *check,
4715 hda_nid_t nid)
4716{
Takashi Iwai031024e2011-05-02 11:29:30 +02004717 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004718 int ch, v;
4719
4720 if (!check->amplist)
4721 return 0;
4722 for (p = check->amplist; p->nid; p++) {
4723 if (p->nid == nid)
4724 break;
4725 }
4726 if (!p->nid)
4727 return 0; /* nothing changed */
4728
4729 for (p = check->amplist; p->nid; p++) {
4730 for (ch = 0; ch < 2; ch++) {
4731 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4732 p->idx);
4733 if (!(v & HDA_AMP_MUTE) && v > 0) {
4734 if (!check->power_on) {
4735 check->power_on = 1;
4736 snd_hda_power_up(codec);
4737 }
4738 return 1;
4739 }
4740 }
4741 }
4742 if (check->power_on) {
4743 check->power_on = 0;
4744 snd_hda_power_down(codec);
4745 }
4746 return 0;
4747}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004748EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004751/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 * input MUX helper
4753 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004754
4755/**
4756 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004757 * @imux: imux helper object
4758 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004759 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004760int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4761 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
4763 unsigned int index;
4764
4765 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4766 uinfo->count = 1;
4767 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004768 if (!imux->num_items)
4769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 index = uinfo->value.enumerated.item;
4771 if (index >= imux->num_items)
4772 index = imux->num_items - 1;
4773 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4774 return 0;
4775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004776EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Takashi Iwaid5191e52009-11-16 14:58:17 +01004778/**
4779 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004780 * @codec: the HDA codec
4781 * @imux: imux helper object
4782 * @ucontrol: pointer to get/store the data
4783 * @nid: input mux NID
4784 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004785 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004786int snd_hda_input_mux_put(struct hda_codec *codec,
4787 const struct hda_input_mux *imux,
4788 struct snd_ctl_elem_value *ucontrol,
4789 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 unsigned int *cur_val)
4791{
4792 unsigned int idx;
4793
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004794 if (!imux->num_items)
4795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 idx = ucontrol->value.enumerated.item[0];
4797 if (idx >= imux->num_items)
4798 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004799 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004801 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4802 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 *cur_val = idx;
4804 return 1;
4805}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004806EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004809/**
4810 * snd_hda_enum_helper_info - Helper for simple enum ctls
4811 * @kcontrol: ctl element
4812 * @uinfo: pointer to get/store the data
4813 * @num_items: number of enum items
4814 * @texts: enum item string array
4815 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004816 * process kcontrol info callback of a simple string enum array
4817 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4818 */
4819int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4820 struct snd_ctl_elem_info *uinfo,
4821 int num_items, const char * const *texts)
4822{
4823 static const char * const texts_default[] = {
4824 "Disabled", "Enabled"
4825 };
4826
4827 if (!texts || !num_items) {
4828 num_items = 2;
4829 texts = texts_default;
4830 }
4831
Takashi Iwai3ff72212014-10-20 18:17:28 +02004832 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004833}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004834EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004835
4836/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 * Multi-channel / digital-out PCM helper functions
4838 */
4839
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004840/* setup SPDIF output stream */
4841static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4842 unsigned int stream_tag, unsigned int format)
4843{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004844 struct hda_spdif_out *spdif;
4845 unsigned int curr_fmt;
4846 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004847
Laurence Darby3bef1c32012-11-03 17:00:06 +00004848 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4849 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4850 AC_VERB_GET_STREAM_FORMAT, 0);
4851 reset = codec->spdif_status_reset &&
4852 (spdif->ctls & AC_DIG1_ENABLE) &&
4853 curr_fmt != format;
4854
4855 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4856 updated */
4857 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004858 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004859 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004860 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004861 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004862 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004863 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004864 for (d = codec->slave_dig_outs; *d; d++)
4865 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4866 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004867 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004868 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004869 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004870 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004871 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004872}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004873
Takashi Iwai2f728532008-09-25 16:32:41 +02004874static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4875{
4876 snd_hda_codec_cleanup_stream(codec, nid);
4877 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004878 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004879 for (d = codec->slave_dig_outs; *d; d++)
4880 snd_hda_codec_cleanup_stream(codec, *d);
4881 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004882}
4883
Takashi Iwaid5191e52009-11-16 14:58:17 +01004884/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004885 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004886 * @codec: the HDA codec
4887 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004889int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4890 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Ingo Molnar62932df2006-01-16 16:34:20 +01004892 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004893 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4894 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004895 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004897 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 return 0;
4899}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004900EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Takashi Iwaid5191e52009-11-16 14:58:17 +01004902/**
4903 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004904 * @codec: the HDA codec
4905 * @mout: hda_multi_out object
4906 * @stream_tag: stream tag to assign
4907 * @format: format id to assign
4908 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004909 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004910int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4911 struct hda_multi_out *mout,
4912 unsigned int stream_tag,
4913 unsigned int format,
4914 struct snd_pcm_substream *substream)
4915{
4916 mutex_lock(&codec->spdif_mutex);
4917 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4918 mutex_unlock(&codec->spdif_mutex);
4919 return 0;
4920}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004921EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004922
Takashi Iwaid5191e52009-11-16 14:58:17 +01004923/**
4924 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004925 * @codec: the HDA codec
4926 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004927 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004928int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4929 struct hda_multi_out *mout)
4930{
4931 mutex_lock(&codec->spdif_mutex);
4932 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4933 mutex_unlock(&codec->spdif_mutex);
4934 return 0;
4935}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004936EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01004937
Takashi Iwaid5191e52009-11-16 14:58:17 +01004938/**
4939 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004940 * @codec: the HDA codec
4941 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004943int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4944 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
Ingo Molnar62932df2006-01-16 16:34:20 +01004946 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004948 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
4950}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004951EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Takashi Iwaid5191e52009-11-16 14:58:17 +01004953/**
4954 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004955 * @codec: the HDA codec
4956 * @mout: hda_multi_out object
4957 * @substream: PCM substream to assign
4958 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004959 *
4960 * Open analog outputs and set up the hw-constraints.
4961 * If the digital outputs can be opened as slave, open the digital
4962 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004964int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4965 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004966 struct snd_pcm_substream *substream,
4967 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968{
Takashi Iwai9a081602008-02-12 18:37:26 +01004969 struct snd_pcm_runtime *runtime = substream->runtime;
4970 runtime->hw.channels_max = mout->max_channels;
4971 if (mout->dig_out_nid) {
4972 if (!mout->analog_rates) {
4973 mout->analog_rates = hinfo->rates;
4974 mout->analog_formats = hinfo->formats;
4975 mout->analog_maxbps = hinfo->maxbps;
4976 } else {
4977 runtime->hw.rates = mout->analog_rates;
4978 runtime->hw.formats = mout->analog_formats;
4979 hinfo->maxbps = mout->analog_maxbps;
4980 }
4981 if (!mout->spdif_rates) {
4982 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4983 &mout->spdif_rates,
4984 &mout->spdif_formats,
4985 &mout->spdif_maxbps);
4986 }
4987 mutex_lock(&codec->spdif_mutex);
4988 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004989 if ((runtime->hw.rates & mout->spdif_rates) &&
4990 (runtime->hw.formats & mout->spdif_formats)) {
4991 runtime->hw.rates &= mout->spdif_rates;
4992 runtime->hw.formats &= mout->spdif_formats;
4993 if (mout->spdif_maxbps < hinfo->maxbps)
4994 hinfo->maxbps = mout->spdif_maxbps;
4995 } else {
4996 mout->share_spdif = 0;
4997 /* FIXME: need notify? */
4998 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004999 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005000 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5003 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5004}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005005EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Takashi Iwaid5191e52009-11-16 14:58:17 +01005007/**
5008 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005009 * @codec: the HDA codec
5010 * @mout: hda_multi_out object
5011 * @stream_tag: stream tag to assign
5012 * @format: format id to assign
5013 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005014 *
5015 * Set up the i/o for analog out.
5016 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005018int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5019 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 unsigned int stream_tag,
5021 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005022 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Takashi Iwaidda14412011-05-02 11:29:30 +02005024 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005026 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 int i;
5028
Ingo Molnar62932df2006-01-16 16:34:20 +01005029 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005030 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005031 if (mout->dig_out_nid && mout->share_spdif &&
5032 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005034 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5035 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005036 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005038 setup_dig_out_stream(codec, mout->dig_out_nid,
5039 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 } else {
5041 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005042 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005045 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005048 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5049 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005050 if (!mout->no_share_stream &&
5051 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005053 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5054 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005055 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005056 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5057 if (!mout->no_share_stream && mout->hp_out_nid[i])
5058 snd_hda_codec_setup_stream(codec,
5059 mout->hp_out_nid[i],
5060 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /* surrounds */
5063 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005064 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005065 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5066 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005067 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005068 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5069 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005071
5072 /* extra surrounds */
5073 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5074 int ch = 0;
5075 if (!mout->extra_out_nid[i])
5076 break;
5077 if (chs >= (i + 1) * 2)
5078 ch = i * 2;
5079 else if (!mout->no_share_stream)
5080 break;
5081 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5082 stream_tag, ch, format);
5083 }
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return 0;
5086}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005087EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Takashi Iwaid5191e52009-11-16 14:58:17 +01005089/**
5090 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005091 * @codec: the HDA codec
5092 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005094int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5095 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096{
Takashi Iwaidda14412011-05-02 11:29:30 +02005097 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 int i;
5099
5100 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005101 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005103 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005104 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5105 if (mout->hp_out_nid[i])
5106 snd_hda_codec_cleanup_stream(codec,
5107 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005108 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5109 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005110 snd_hda_codec_cleanup_stream(codec,
5111 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005112 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005114 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 mout->dig_out_used = 0;
5116 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005117 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return 0;
5119}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005120EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
Takashi Iwai47408602012-04-20 13:06:53 +02005122/**
5123 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005124 * @codec: the HDA codec
5125 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005126 *
5127 * Guess the suitable VREF pin bits to be set as the pin-control value.
5128 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5129 */
5130unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5131{
5132 unsigned int pincap;
5133 unsigned int oldval;
5134 oldval = snd_hda_codec_read(codec, pin, 0,
5135 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5136 pincap = snd_hda_query_pin_caps(codec, pin);
5137 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5138 /* Exception: if the default pin setup is vref50, we give it priority */
5139 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5140 return AC_PINCTL_VREF_80;
5141 else if (pincap & AC_PINCAP_VREF_50)
5142 return AC_PINCTL_VREF_50;
5143 else if (pincap & AC_PINCAP_VREF_100)
5144 return AC_PINCTL_VREF_100;
5145 else if (pincap & AC_PINCAP_VREF_GRD)
5146 return AC_PINCTL_VREF_GRD;
5147 return AC_PINCTL_VREF_HIZ;
5148}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005149EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005150
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005151/**
5152 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5153 * @codec: the HDA codec
5154 * @pin: referred pin NID
5155 * @val: pin ctl value to audit
5156 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005157unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5158 hda_nid_t pin, unsigned int val)
5159{
5160 static unsigned int cap_lists[][2] = {
5161 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5162 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5163 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5164 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5165 };
5166 unsigned int cap;
5167
5168 if (!val)
5169 return 0;
5170 cap = snd_hda_query_pin_caps(codec, pin);
5171 if (!cap)
5172 return val; /* don't know what to do... */
5173
5174 if (val & AC_PINCTL_OUT_EN) {
5175 if (!(cap & AC_PINCAP_OUT))
5176 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5177 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5178 val &= ~AC_PINCTL_HP_EN;
5179 }
5180
5181 if (val & AC_PINCTL_IN_EN) {
5182 if (!(cap & AC_PINCAP_IN))
5183 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5184 else {
5185 unsigned int vcap, vref;
5186 int i;
5187 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5188 vref = val & AC_PINCTL_VREFEN;
5189 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5190 if (vref == cap_lists[i][0] &&
5191 !(vcap & cap_lists[i][1])) {
5192 if (i == ARRAY_SIZE(cap_lists) - 1)
5193 vref = AC_PINCTL_VREF_HIZ;
5194 else
5195 vref = cap_lists[i + 1][0];
5196 }
5197 }
5198 val &= ~AC_PINCTL_VREFEN;
5199 val |= vref;
5200 }
5201 }
5202
5203 return val;
5204}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005205EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005206
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005207/**
5208 * _snd_hda_pin_ctl - Helper to set pin ctl value
5209 * @codec: the HDA codec
5210 * @pin: referred pin NID
5211 * @val: pin control value to set
5212 * @cached: access over codec pinctl cache or direct write
5213 *
5214 * This function is a helper to set a pin ctl value more safely.
5215 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5216 * value in pin target array via snd_hda_codec_set_pin_target(), then
5217 * actually writes the value via either snd_hda_codec_update_cache() or
5218 * snd_hda_codec_write() depending on @cached flag.
5219 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005220int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5221 unsigned int val, bool cached)
5222{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005223 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005224 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005225 if (cached)
5226 return snd_hda_codec_update_cache(codec, pin, 0,
5227 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5228 else
5229 return snd_hda_codec_write(codec, pin, 0,
5230 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5231}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005232EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005233
Takashi Iwai990061c2010-09-09 22:08:44 +02005234/**
5235 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005236 * @codec: the HDA codec
5237 * @imux: imux helper object
5238 * @label: the name of imux item to assign
5239 * @index: index number of imux item to assign
5240 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005241 *
5242 * When the same label is used already in the existing items, the number
5243 * suffix is appended to the label. This label index number is stored
5244 * to type_idx when non-NULL pointer is given.
5245 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005246int snd_hda_add_imux_item(struct hda_codec *codec,
5247 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005248 int index, int *type_idx)
5249{
5250 int i, label_idx = 0;
5251 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005252 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005253 return -EINVAL;
5254 }
5255 for (i = 0; i < imux->num_items; i++) {
5256 if (!strncmp(label, imux->items[i].label, strlen(label)))
5257 label_idx++;
5258 }
5259 if (type_idx)
5260 *type_idx = label_idx;
5261 if (label_idx > 0)
5262 snprintf(imux->items[imux->num_items].label,
5263 sizeof(imux->items[imux->num_items].label),
5264 "%s %d", label, label_idx);
5265 else
5266 strlcpy(imux->items[imux->num_items].label, label,
5267 sizeof(imux->items[imux->num_items].label));
5268 imux->items[imux->num_items].index = index;
5269 imux->num_items++;
5270 return 0;
5271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005272EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005275 * snd_hda_bus_reset - Reset the bus
5276 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005278void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005280 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
Takashi Iwaid068ebc2015-03-02 23:22:59 +01005282 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005283 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005284 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005285#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005286 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005287 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005288 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005289 }
5290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005293EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005294
5295/*
5296 * generic arrays
5297 */
5298
Takashi Iwaid5191e52009-11-16 14:58:17 +01005299/**
5300 * snd_array_new - get a new element from the given array
5301 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005302 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005303 * Get a new element from the given array. If it exceeds the
5304 * pre-allocated array size, re-allocate the array.
5305 *
5306 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005307 */
5308void *snd_array_new(struct snd_array *array)
5309{
Takashi Iwai12f17712012-10-10 08:59:14 +02005310 if (snd_BUG_ON(!array->elem_size))
5311 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005312 if (array->used >= array->alloced) {
5313 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005314 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005315 void *nlist;
5316 if (snd_BUG_ON(num >= 4096))
5317 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005318 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005319 if (!nlist)
5320 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005321 array->list = nlist;
5322 array->alloced = num;
5323 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005324 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005325}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005326EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005327
Takashi Iwaid5191e52009-11-16 14:58:17 +01005328/**
5329 * snd_array_free - free the given array elements
5330 * @array: the array object
5331 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005332void snd_array_free(struct snd_array *array)
5333{
5334 kfree(array->list);
5335 array->used = 0;
5336 array->alloced = 0;
5337 array->list = NULL;
5338}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005339EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005340
Takashi Iwaid5191e52009-11-16 14:58:17 +01005341/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005342 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5343 * @pcm: PCM caps bits
5344 * @buf: the string buffer to write
5345 * @buflen: the max buffer length
5346 *
5347 * used by hda_proc.c and hda_eld.c
5348 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005349void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5350{
5351 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5352 int i, j;
5353
5354 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5355 if (pcm & (AC_SUPPCM_BITS_8 << i))
5356 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5357
5358 buf[j] = '\0'; /* necessary when j == 0 */
5359}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005360EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005361
5362MODULE_DESCRIPTION("HDA codec core");
5363MODULE_LICENSE("GPL");