blob: 36cebe0e76b2f937747b176662b750f35c580bac [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 Iwaid66fee52011-08-02 15:39:31 +020042#define CREATE_TRACE_POINTS
43#include "hda_trace.h"
44
Takashi Iwai83012a72012-08-24 18:38:08 +020045#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +010046#define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
47#define hda_codec_is_power_on(codec) \
48 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaia40e0a82013-11-20 12:41:20 +010049
50static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -070051{
Takashi Iwaia40e0a82013-11-20 12:41:20 +010052 struct hda_bus *bus = codec->bus;
53
54 if ((power_up && codec->pm_up_notified) ||
55 (!power_up && !codec->pm_up_notified))
56 return;
Takashi Iwai68467f52012-08-28 09:14:29 -070057 if (bus->ops.pm_notify)
58 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +010059 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -070060}
Takashi Iwaia40e0a82013-11-20 12:41:20 +010061
Takashi Iwaicb53c622007-08-10 17:21:45 +020062#else
Takashi Iwaid846b172012-11-24 11:58:24 +010063#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020064#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +010065#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +020066#endif
67
Takashi Iwaid5191e52009-11-16 14:58:17 +010068/**
69 * snd_hda_get_jack_location - Give a location string of the jack
70 * @cfg: pin default config value
71 *
72 * Parse the pin default config value and returns the string of the
73 * jack location, e.g. "Rear", "Front", etc.
74 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040075const char *snd_hda_get_jack_location(u32 cfg)
76{
77 static char *bases[7] = {
78 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
79 };
80 static unsigned char specials_idx[] = {
81 0x07, 0x08,
82 0x17, 0x18, 0x19,
83 0x37, 0x38
84 };
85 static char *specials[] = {
86 "Rear Panel", "Drive Bar",
87 "Riser", "HDMI", "ATAPI",
88 "Mobile-In", "Mobile-Out"
89 };
90 int i;
91 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
92 if ((cfg & 0x0f) < 7)
93 return bases[cfg & 0x0f];
94 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
95 if (cfg == specials_idx[i])
96 return specials[i];
97 }
98 return "UNKNOWN";
99}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100100EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400101
Takashi Iwaid5191e52009-11-16 14:58:17 +0100102/**
103 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
104 * @cfg: pin default config value
105 *
106 * Parse the pin default config value and returns the string of the
107 * jack connectivity, i.e. external or internal connection.
108 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400109const char *snd_hda_get_jack_connectivity(u32 cfg)
110{
111 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
112
113 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
114}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100115EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400116
Takashi Iwaid5191e52009-11-16 14:58:17 +0100117/**
118 * snd_hda_get_jack_type - Give a type string of the jack
119 * @cfg: pin default config value
120 *
121 * Parse the pin default config value and returns the string of the
122 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
123 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400124const char *snd_hda_get_jack_type(u32 cfg)
125{
126 static char *jack_types[16] = {
127 "Line Out", "Speaker", "HP Out", "CD",
128 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
129 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200130 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131 };
132
133 return jack_types[(cfg & AC_DEFCFG_DEVICE)
134 >> AC_DEFCFG_DEVICE_SHIFT];
135}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100136EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400137
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100138/*
139 * Compose a 32bit command word to be sent to the HD-audio controller
140 */
141static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200142make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100143 unsigned int verb, unsigned int parm)
144{
145 u32 val;
146
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200147 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200148 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100149 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200150 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800151 return ~0;
152 }
153
154 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100155 val |= (u32)nid << 20;
156 val |= verb << 8;
157 val |= parm;
158 return val;
159}
160
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200161/*
162 * Send and receive a verb
163 */
164static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200165 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200166{
167 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200168 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200169
Wu Fengguang6430aee2009-07-17 16:49:19 +0800170 if (cmd == ~0)
171 return -1;
172
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200173 if (res)
174 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200175 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200176 snd_hda_power_up(codec);
177 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200178 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
179 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100180 for (;;) {
181 trace_hda_send_cmd(codec, cmd);
182 err = bus->ops.command(bus, cmd);
183 if (err != -EAGAIN)
184 break;
185 /* process pending verbs */
186 bus->ops.get_response(bus, codec->addr);
187 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200188 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800189 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200190 trace_hda_get_response(codec, *res);
191 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200192 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200193 mutex_unlock(&bus->cmd_mutex);
194 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100195 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200196 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100197 codec_dbg(codec,
198 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200199 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200200 bus->ops.bus_reset(bus);
201 }
202 goto again;
203 }
204 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100205 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 return err;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
211 * snd_hda_codec_read - send a command and get the response
212 * @codec: the HDA codec
213 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200214 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @verb: the verb to send
216 * @parm: the parameter for the verb
217 *
218 * Send a single command and read the corresponding response.
219 *
220 * Returns the obtained response value, or -1 for an error.
221 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200222unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200223 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned int verb, unsigned int parm)
225{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200226 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200227 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200228 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return res;
231}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100232EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/**
235 * snd_hda_codec_write - send a single command without waiting for response
236 * @codec: the HDA codec
237 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200238 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @verb: the verb to send
240 * @parm: the parameter for the verb
241 *
242 * Send a single command without waiting for response.
243 *
244 * Returns 0 if successful, or a negative error code.
245 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200246int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
247 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200249 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100250 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200251 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200252 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100254EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256/**
257 * snd_hda_sequence_write - sequence writes
258 * @codec: the HDA codec
259 * @seq: VERB array to send
260 *
261 * Send the commands sequentially from the given array.
262 * The array must be terminated with NID=0.
263 */
264void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
265{
266 for (; seq->nid; seq++)
267 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
268}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100269EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/**
272 * snd_hda_get_sub_nodes - get the range of sub nodes
273 * @codec: the HDA codec
274 * @nid: NID to parse
275 * @start_id: the pointer to store the start NID
276 *
277 * Parse the NID and store the start NID of its sub-nodes.
278 * Returns the number of sub-nodes.
279 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200280int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
281 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned int parm;
284
285 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300286 if (parm == -1) {
287 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200288 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *start_id = (parm >> 16) & 0x7fff;
291 return (int)(parm & 0x7fff);
292}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100293EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100295/* connection list element */
296struct hda_conn_list {
297 struct list_head list;
298 int len;
299 hda_nid_t nid;
300 hda_nid_t conns[0];
301};
302
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200303/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100304static struct hda_conn_list *
305lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200306{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100307 struct hda_conn_list *p;
308 list_for_each_entry(p, &codec->conn_list, list) {
309 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200310 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200311 }
312 return NULL;
313}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200314
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100315static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
316 const hda_nid_t *list)
317{
318 struct hda_conn_list *p;
319
320 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
321 if (!p)
322 return -ENOMEM;
323 p->len = len;
324 p->nid = nid;
325 memcpy(p->conns, list, len * sizeof(hda_nid_t));
326 list_add(&p->list, &codec->conn_list);
327 return 0;
328}
329
330static void remove_conn_list(struct hda_codec *codec)
331{
332 while (!list_empty(&codec->conn_list)) {
333 struct hda_conn_list *p;
334 p = list_first_entry(&codec->conn_list, typeof(*p), list);
335 list_del(&p->list);
336 kfree(p);
337 }
338}
339
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200340/* read the connection and add to the cache */
341static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100343 hda_nid_t list[32];
344 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200345 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200347 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100348 if (len == -ENOSPC) {
349 len = snd_hda_get_num_raw_conns(codec, nid);
350 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
351 if (!result)
352 return -ENOMEM;
353 len = snd_hda_get_raw_connections(codec, nid, result, len);
354 }
355 if (len >= 0)
356 len = snd_hda_override_conn_list(codec, nid, len, result);
357 if (result != list)
358 kfree(result);
359 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200360}
Takashi Iwaidce20792011-06-24 14:10:28 +0200361
362/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100363 * snd_hda_get_conn_list - get connection list
364 * @codec: the HDA codec
365 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100366 * @listp: the pointer to store NID list
367 *
368 * Parses the connection list of the given widget and stores the pointer
369 * to the list of NIDs.
370 *
371 * Returns the number of connections, or a negative error code.
372 *
373 * Note that the returned pointer isn't protected against the list
374 * modification. If snd_hda_override_conn_list() might be called
375 * concurrently, protect with a mutex appropriately.
376 */
377int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
378 const hda_nid_t **listp)
379{
380 bool added = false;
381
382 for (;;) {
383 int err;
384 const struct hda_conn_list *p;
385
386 /* if the connection-list is already cached, read it */
387 p = lookup_conn_list(codec, nid);
388 if (p) {
389 if (listp)
390 *listp = p->conns;
391 return p->len;
392 }
393 if (snd_BUG_ON(added))
394 return -EINVAL;
395
396 err = read_and_add_raw_conns(codec, nid);
397 if (err < 0)
398 return err;
399 added = true;
400 }
401}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100402EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403
404/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200405 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * @codec: the HDA codec
407 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * @max_conns: max. number of connections to store
410 *
411 * Parses the connection list of the given widget and stores the list
412 * of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 */
416int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200417 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200418{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100419 const hda_nid_t *list;
420 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200421
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100422 if (len > 0 && conn_list) {
423 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100424 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200425 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200426 return -EINVAL;
427 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100428 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200429 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200430
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100431 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200432}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100433EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200434
Takashi Iwai4eea3092013-02-07 18:18:19 +0100435/* return CONNLIST_LEN parameter of the given widget */
436static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
437{
438 unsigned int wcaps = get_wcaps(codec, nid);
439 unsigned int parm;
440
441 if (!(wcaps & AC_WCAP_CONN_LIST) &&
442 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
443 return 0;
444
445 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
446 if (parm == -1)
447 parm = 0;
448 return parm;
449}
450
451int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
452{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100453 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100454}
455
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200456/**
457 * snd_hda_get_raw_connections - copy connection list without cache
458 * @codec: the HDA codec
459 * @nid: NID to parse
460 * @conn_list: connection list array
461 * @max_conns: max. number of connections to store
462 *
463 * Like snd_hda_get_connections(), copy the connection list but without
464 * checking through the connection-list cache.
465 * Currently called only from hda_proc.c, so not exported.
466 */
467int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
468 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100474 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Takashi Iwai4eea3092013-02-07 18:18:19 +0100476 parm = get_num_conns(codec, nid);
477 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200478 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (parm & AC_CLIST_LONG) {
481 /* long form */
482 shift = 16;
483 num_elems = 2;
484 } else {
485 /* short form */
486 shift = 8;
487 num_elems = 4;
488 }
489 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 mask = (1 << (shift-1)) - 1;
491
Takashi Iwai0ba21762007-04-16 11:29:14 +0200492 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0; /* no connection */
494
495 if (conn_len == 1) {
496 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200497 parm = snd_hda_codec_read(codec, nid, 0,
498 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200499 if (parm == -1 && codec->bus->rirb_error)
500 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100501 if (conn_list)
502 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 1;
504 }
505
506 /* multi connection */
507 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 prev_nid = 0;
509 for (i = 0; i < conn_len; i++) {
510 int range_val;
511 hda_nid_t val, n;
512
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200513 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100514 parm = snd_hda_codec_read(codec, nid, 0,
515 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200516 if (parm == -1 && codec->bus->rirb_error)
517 return -EIO;
518 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200519 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100520 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100521 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100522 codec_dbg(codec,
523 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200524 nid, i, parm);
525 return 0;
526 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100527 parm >>= shift;
528 if (range_val) {
529 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200530 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100531 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200532 "invalid dep_range_val %x:%x\n",
533 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100536 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100537 if (conn_list) {
538 if (conns >= max_conns)
539 return -ENOSPC;
540 conn_list[conns] = n;
541 }
542 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100543 }
544 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list) {
546 if (conns >= max_conns)
547 return -ENOSPC;
548 conn_list[conns] = val;
549 }
550 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 return conns;
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200558 * snd_hda_override_conn_list - add/modify the connection-list to cache
559 * @codec: the HDA codec
560 * @nid: NID to parse
561 * @len: number of connection list entries
562 * @list: the list of connection entries
563 *
564 * Add or modify the given connection-list to the cache. If the corresponding
565 * cache already exists, invalidate it and append a new one.
566 *
567 * Returns zero or a negative error code.
568 */
569int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
570 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100572 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200573
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100574 p = lookup_conn_list(codec, nid);
575 if (p) {
576 list_del(&p->list);
577 kfree(p);
578 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200579
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100580 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100582EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200583
584/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 * snd_hda_get_conn_index - get the connection index of the given NID
586 * @codec: the HDA codec
587 * @mux: NID containing the list
588 * @nid: NID to select
589 * @recursive: 1 when searching NID recursively, otherwise 0
590 *
591 * Parses the connection list of the widget @mux and checks whether the
592 * widget @nid is present. If it is, return the connection index.
593 * Otherwise it returns -1.
594 */
595int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
596 hda_nid_t nid, int recursive)
597{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100598 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 int i, nums;
600
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100601 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200602 for (i = 0; i < nums; i++)
603 if (conn[i] == nid)
604 return i;
605 if (!recursive)
606 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100607 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100608 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200609 return -1;
610 }
611 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200612 for (i = 0; i < nums; i++) {
613 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
614 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
615 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200616 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
617 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200618 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200619 return -1;
620}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100621EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mengdong Linf1aa0682013-08-26 21:35:21 -0400623
624/* return DEVLIST_LEN parameter of the given widget */
625static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
626{
627 unsigned int wcaps = get_wcaps(codec, nid);
628 unsigned int parm;
629
630 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
631 get_wcaps_type(wcaps) != AC_WID_PIN)
632 return 0;
633
634 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
635 if (parm == -1 && codec->bus->rirb_error)
636 parm = 0;
637 return parm & AC_DEV_LIST_LEN_MASK;
638}
639
640/**
641 * snd_hda_get_devices - copy device list without cache
642 * @codec: the HDA codec
643 * @nid: NID of the pin to parse
644 * @dev_list: device list array
645 * @max_devices: max. number of devices to store
646 *
647 * Copy the device list. This info is dynamic and so not cached.
648 * Currently called only from hda_proc.c, so not exported.
649 */
650int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
651 u8 *dev_list, int max_devices)
652{
653 unsigned int parm;
654 int i, dev_len, devices;
655
656 parm = get_num_devices(codec, nid);
657 if (!parm) /* not multi-stream capable */
658 return 0;
659
660 dev_len = parm + 1;
661 dev_len = dev_len < max_devices ? dev_len : max_devices;
662
663 devices = 0;
664 while (devices < dev_len) {
665 parm = snd_hda_codec_read(codec, nid, 0,
666 AC_VERB_GET_DEVICE_LIST, devices);
667 if (parm == -1 && codec->bus->rirb_error)
668 break;
669
670 for (i = 0; i < 8; i++) {
671 dev_list[devices] = (u8)parm;
672 parm >>= 4;
673 devices++;
674 if (devices >= dev_len)
675 break;
676 }
677 }
678 return devices;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * snd_hda_queue_unsol_event - add an unsolicited event to queue
683 * @bus: the BUS
684 * @res: unsolicited event (lower 32bit of RIRB entry)
685 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
686 *
687 * Adds the given event to the queue. The events are processed in
688 * the workqueue asynchronously. Call this function in the interrupt
689 * hanlder when RIRB receives an unsolicited event.
690 *
691 * Returns 0 if successful, or a negative error code.
692 */
693int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
694{
695 struct hda_bus_unsolicited *unsol;
696 unsigned int wp;
697
Wang YanQing2195b062013-05-07 11:27:33 +0800698 if (!bus || !bus->workq)
699 return 0;
700
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200701 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100702 unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
704 unsol->wp = wp;
705
706 wp <<= 1;
707 unsol->queue[wp] = res;
708 unsol->queue[wp + 1] = res_ex;
709
Takashi Iwai6acaed32009-01-12 10:09:24 +0100710 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return 0;
713}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100714EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800717 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
David Howellsc4028952006-11-22 14:57:56 +0000719static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Takashi Iwai922c88a2015-02-17 14:46:40 +0100721 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
722 struct hda_bus_unsolicited *unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct hda_codec *codec;
724 unsigned int rp, caddr, res;
725
726 while (unsol->rp != unsol->wp) {
727 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
728 unsol->rp = rp;
729 rp <<= 1;
730 res = unsol->queue[rp];
731 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200732 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 continue;
734 codec = bus->caddr_tbl[caddr & 0x0f];
735 if (codec && codec->patch_ops.unsol_event)
736 codec->patch_ops.unsol_event(codec, res);
737 }
738}
739
740/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * destructor
742 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100743static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200745 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100746 return;
747
748 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100749 if (bus->workq)
750 flush_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (bus->ops.private_free)
752 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100753 if (bus->workq)
754 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100759static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai2565c892014-02-19 11:41:09 +0100761 snd_hda_bus_free(device->device_data);
762 return 0;
763}
764
765static int snd_hda_bus_dev_disconnect(struct snd_device *device)
766{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100768 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772/**
773 * snd_hda_bus_new - create a HDA bus
774 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * @busp: the pointer to store the created bus instance
776 *
777 * Returns 0 if successful, or a negative error code.
778 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100779int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100780 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct hda_bus *bus;
783 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100784 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100785 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .dev_free = snd_hda_bus_dev_free,
787 };
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (busp)
790 *busp = NULL;
791
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200792 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100794 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
797
798 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100799 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200800 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100802 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100804 snprintf(bus->workq_name, sizeof(bus->workq_name),
805 "hd-audio%d", card->number);
806 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100807 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100808 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100809 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100810 kfree(bus);
811 return -ENOMEM;
812 }
813
Takashi Iwai0ba21762007-04-16 11:29:14 +0200814 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
815 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 snd_hda_bus_free(bus);
817 return err;
818 }
819 if (busp)
820 *busp = bus;
821 return 0;
822}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100823EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200826 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100828static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200830 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 hda_nid_t nid;
832
833 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
834 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200835 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200836 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200837 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200838 case AC_GRP_AUDIO_FUNCTION:
839 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200840 codec->afg_function_id = function_id & 0xff;
841 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200842 break;
843 case AC_GRP_MODEM_FUNCTION:
844 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200845 codec->mfg_function_id = function_id & 0xff;
846 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200847 break;
848 default:
849 break;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100855 * read widget caps for each widget and store in cache
856 */
857static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
858{
859 int i;
860 hda_nid_t nid;
861
862 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
863 &codec->start_nid);
864 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200865 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100866 return -ENOMEM;
867 nid = codec->start_nid;
868 for (i = 0; i < codec->num_nodes; i++, nid++)
869 codec->wcaps[i] = snd_hda_param_read(codec, nid,
870 AC_PAR_AUDIO_WIDGET_CAP);
871 return 0;
872}
873
Takashi Iwai3be14142009-02-20 14:11:16 +0100874/* read all pin default configurations and save codec->init_pins */
875static int read_pin_defaults(struct hda_codec *codec)
876{
877 int i;
878 hda_nid_t nid = codec->start_nid;
879
880 for (i = 0; i < codec->num_nodes; i++, nid++) {
881 struct hda_pincfg *pin;
882 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200883 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100884 if (wid_type != AC_WID_PIN)
885 continue;
886 pin = snd_array_new(&codec->init_pins);
887 if (!pin)
888 return -ENOMEM;
889 pin->nid = nid;
890 pin->cfg = snd_hda_codec_read(codec, nid, 0,
891 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200892 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
893 AC_VERB_GET_PIN_WIDGET_CONTROL,
894 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100895 }
896 return 0;
897}
898
899/* look up the given pin config list and return the item matching with NID */
900static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
901 struct snd_array *array,
902 hda_nid_t nid)
903{
904 int i;
905 for (i = 0; i < array->used; i++) {
906 struct hda_pincfg *pin = snd_array_elem(array, i);
907 if (pin->nid == nid)
908 return pin;
909 }
910 return NULL;
911}
912
Takashi Iwai3be14142009-02-20 14:11:16 +0100913/* set the current pin config value for the given NID.
914 * the value is cached, and read via snd_hda_codec_get_pincfg()
915 */
916int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
917 hda_nid_t nid, unsigned int cfg)
918{
919 struct hda_pincfg *pin;
920
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200921 /* the check below may be invalid when pins are added by a fixup
922 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
923 * for now
924 */
925 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100926 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
927 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200928 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100929
Takashi Iwai3be14142009-02-20 14:11:16 +0100930 pin = look_up_pincfg(codec, list, nid);
931 if (!pin) {
932 pin = snd_array_new(list);
933 if (!pin)
934 return -ENOMEM;
935 pin->nid = nid;
936 }
937 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100938 return 0;
939}
940
Takashi Iwaid5191e52009-11-16 14:58:17 +0100941/**
942 * snd_hda_codec_set_pincfg - Override a pin default configuration
943 * @codec: the HDA codec
944 * @nid: NID to set the pin config
945 * @cfg: the pin default config value
946 *
947 * Override a pin default configuration value in the cache.
948 * This value can be read by snd_hda_codec_get_pincfg() in a higher
949 * priority than the real hardware value.
950 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100951int snd_hda_codec_set_pincfg(struct hda_codec *codec,
952 hda_nid_t nid, unsigned int cfg)
953{
Takashi Iwai346ff702009-02-23 09:42:57 +0100954 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100955}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100956EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100957
Takashi Iwaid5191e52009-11-16 14:58:17 +0100958/**
959 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
960 * @codec: the HDA codec
961 * @nid: NID to get the pin config
962 *
963 * Get the current pin config value of the given pin NID.
964 * If the pincfg value is cached or overridden via sysfs or driver,
965 * returns the cached value.
966 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100967unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
968{
969 struct hda_pincfg *pin;
970
Takashi Iwai648a8d22014-02-25 10:38:13 +0100971#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100972 {
973 unsigned int cfg = 0;
974 mutex_lock(&codec->user_mutex);
975 pin = look_up_pincfg(codec, &codec->user_pins, nid);
976 if (pin)
977 cfg = pin->cfg;
978 mutex_unlock(&codec->user_mutex);
979 if (cfg)
980 return cfg;
981 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100982#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100983 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
984 if (pin)
985 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100986 pin = look_up_pincfg(codec, &codec->init_pins, nid);
987 if (pin)
988 return pin->cfg;
989 return 0;
990}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100991EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100992
Takashi Iwai95a962c2014-10-29 16:03:58 +0100993/**
994 * snd_hda_codec_set_pin_target - remember the current pinctl target value
995 * @codec: the HDA codec
996 * @nid: pin NID
997 * @val: assigned pinctl value
998 *
999 * This function stores the given value to a pinctl target value in the
1000 * pincfg table. This isn't always as same as the actually written value
1001 * but can be referred at any time via snd_hda_codec_get_pin_target().
1002 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001003int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1004 unsigned int val)
1005{
1006 struct hda_pincfg *pin;
1007
1008 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1009 if (!pin)
1010 return -EINVAL;
1011 pin->target = val;
1012 return 0;
1013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001014EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001015
Takashi Iwai95a962c2014-10-29 16:03:58 +01001016/**
1017 * snd_hda_codec_get_pin_target - return the current pinctl target value
1018 * @codec: the HDA codec
1019 * @nid: pin NID
1020 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001021int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1022{
1023 struct hda_pincfg *pin;
1024
1025 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1026 if (!pin)
1027 return 0;
1028 return pin->target;
1029}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001030EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001031
Takashi Iwai92ee6162009-12-27 11:18:59 +01001032/**
1033 * snd_hda_shutup_pins - Shut up all pins
1034 * @codec: the HDA codec
1035 *
1036 * Clear all pin controls to shup up before suspend for avoiding click noise.
1037 * The controls aren't cached so that they can be resumed properly.
1038 */
1039void snd_hda_shutup_pins(struct hda_codec *codec)
1040{
1041 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001042 /* don't shut up pins when unloading the driver; otherwise it breaks
1043 * the default pin setup at the next load of the driver
1044 */
1045 if (codec->bus->shutdown)
1046 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001047 for (i = 0; i < codec->init_pins.used; i++) {
1048 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1049 /* use read here for syncing after issuing each verb */
1050 snd_hda_codec_read(codec, pin->nid, 0,
1051 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1052 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001053 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001054}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001055EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001056
Takashi Iwai2a439522011-07-26 09:52:50 +02001057#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001058/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1059static void restore_shutup_pins(struct hda_codec *codec)
1060{
1061 int i;
1062 if (!codec->pins_shutup)
1063 return;
1064 if (codec->bus->shutdown)
1065 return;
1066 for (i = 0; i < codec->init_pins.used; i++) {
1067 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1068 snd_hda_codec_write(codec, pin->nid, 0,
1069 AC_VERB_SET_PIN_WIDGET_CONTROL,
1070 pin->ctrl);
1071 }
1072 codec->pins_shutup = 0;
1073}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001074#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001075
David Henningsson26a6cb62012-10-09 15:04:21 +02001076static void hda_jackpoll_work(struct work_struct *work)
1077{
1078 struct hda_codec *codec =
1079 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001080
1081 snd_hda_jack_set_dirty_all(codec);
1082 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001083
1084 if (!codec->jackpoll_interval)
1085 return;
1086
David Henningsson26a6cb62012-10-09 15:04:21 +02001087 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1088 codec->jackpoll_interval);
1089}
1090
Takashi Iwai01751f52007-08-10 16:59:39 +02001091static void init_hda_cache(struct hda_cache_rec *cache,
1092 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001093static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001094
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001095/* release all pincfg lists */
1096static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001097{
Takashi Iwai346ff702009-02-23 09:42:57 +01001098 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001099#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001100 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001101#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001102 snd_array_free(&codec->init_pins);
1103}
1104
Takashi Iwai54d17402005-11-21 16:33:22 +01001105/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001106 * audio-converter setup caches
1107 */
1108struct hda_cvt_setup {
1109 hda_nid_t nid;
1110 u8 stream_tag;
1111 u8 channel_id;
1112 u16 format_id;
1113 unsigned char active; /* cvt is currently used */
1114 unsigned char dirty; /* setups should be cleared */
1115};
1116
1117/* get or create a cache entry for the given audio converter NID */
1118static struct hda_cvt_setup *
1119get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1120{
1121 struct hda_cvt_setup *p;
1122 int i;
1123
1124 for (i = 0; i < codec->cvt_setups.used; i++) {
1125 p = snd_array_elem(&codec->cvt_setups, i);
1126 if (p->nid == nid)
1127 return p;
1128 }
1129 p = snd_array_new(&codec->cvt_setups);
1130 if (p)
1131 p->nid = nid;
1132 return p;
1133}
1134
1135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * codec destructor
1137 */
1138static void snd_hda_codec_free(struct hda_codec *codec)
1139{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001140 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001142 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001143 if (device_is_registered(hda_codec_dev(codec)))
1144 device_del(hda_codec_dev(codec));
Takashi Iwai59cad162012-06-26 15:00:20 +02001145 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001146 free_init_pincfgs(codec);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001147 flush_workqueue(codec->bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001149 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001150 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001151 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001152 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001153 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 codec->bus->caddr_tbl[codec->addr] = NULL;
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001155 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001156 snd_hda_sysfs_clear(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +02001157 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001158 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001159 kfree(codec->vendor_name);
1160 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001161 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001162 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001163 codec->bus->num_codecs--;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001164 put_device(hda_codec_dev(codec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001167static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1168 hda_nid_t fg, unsigned int power_state);
1169
Takashi Iwaid8193872012-08-31 07:54:38 -07001170static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001171 unsigned int power_state);
1172
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001173static int snd_hda_codec_dev_register(struct snd_device *device)
1174{
1175 struct hda_codec *codec = device->device_data;
1176
Takashi Iwaid604b392014-02-28 13:42:09 +01001177 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001178 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001179 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwaid604b392014-02-28 13:42:09 +01001180 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001181}
1182
1183static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1184{
1185 struct hda_codec *codec = device->device_data;
1186
Takashi Iwaid604b392014-02-28 13:42:09 +01001187 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001188 return 0;
1189}
1190
Takashi Iwai2565c892014-02-19 11:41:09 +01001191static int snd_hda_codec_dev_free(struct snd_device *device)
1192{
1193 snd_hda_codec_free(device->device_data);
1194 return 0;
1195}
1196
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001197/* just free the container */
1198static void snd_hda_codec_dev_release(struct device *dev)
1199{
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001200 kfree(dev_to_hda_codec(dev));
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/**
1204 * snd_hda_codec_new - create a HDA codec
1205 * @bus: the bus to assign
1206 * @codec_addr: the codec address
1207 * @codecp: the pointer to store the generated codec
1208 *
1209 * Returns 0 if successful, or a negative error code.
1210 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001211int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001212 unsigned int codec_addr,
1213 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001216 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001217 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001218 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001220 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001221 .dev_register = snd_hda_codec_dev_register,
1222 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001223 .dev_free = snd_hda_codec_dev_free,
1224 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Takashi Iwaida3cec32008-08-08 17:12:14 +02001226 if (snd_BUG_ON(!bus))
1227 return -EINVAL;
1228 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001232 dev_err(bus->card->dev,
1233 "address 0x%x is already occupied\n",
1234 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -EBUSY;
1236 }
1237
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001238 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001240 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -ENOMEM;
1242 }
1243
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001244 dev = hda_codec_dev(codec);
1245 device_initialize(dev);
1246 dev->parent = bus->card->dev;
1247 dev->bus = &snd_hda_bus_type;
1248 dev->release = snd_hda_codec_dev_release;
1249 dev->groups = snd_hda_dev_attr_groups;
1250 dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr);
1251 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001252 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001258 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001259 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001260 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001261 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1262 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001263 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001264 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001265 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001267 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001268 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001269 INIT_LIST_HEAD(&codec->conn_list);
1270
David Henningsson26a6cb62012-10-09 15:04:21 +02001271 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001272 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001273 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Takashi Iwai83012a72012-08-24 18:38:08 +02001275#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001276 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1277 * the caller has to power down appropriatley after initialization
1278 * phase.
1279 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01001280 pm_runtime_set_active(hda_codec_dev(codec));
1281 pm_runtime_get_noresume(hda_codec_dev(codec));
1282 codec->power_jiffies = jiffies;
1283 hda_call_pm_notify(codec, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001284#endif
1285
Takashi Iwai648a8d22014-02-25 10:38:13 +01001286 snd_hda_sysfs_init(codec);
1287
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001288 if (codec->bus->modelname) {
1289 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1290 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001291 err = -ENODEV;
1292 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001293 }
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001297 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 bus->caddr_tbl[codec_addr] = codec;
1300
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1302 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001303 if (codec->vendor_id == -1)
1304 /* read again, hopefully the access method was corrected
1305 * in the last read...
1306 */
1307 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1308 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001309 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1310 AC_PAR_SUBSYSTEM_ID);
1311 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1312 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001314 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001316 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001317 err = -ENODEV;
1318 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Takashi Iwaid8193872012-08-31 07:54:38 -07001321 fg = codec->afg ? codec->afg : codec->mfg;
1322 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001323 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001324 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001325 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001326 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001327 err = read_pin_defaults(codec);
1328 if (err < 0)
1329 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001330
Takashi Iwai0ba21762007-04-16 11:29:14 +02001331 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001332 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001333 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001334 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001335 }
1336
Takashi Iwai83012a72012-08-24 18:38:08 +02001337#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001338 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001339 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001340#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001341 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001342 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001343#ifdef CONFIG_PM
1344 if (!codec->d3_stop_clk || !codec->epss)
1345 bus->power_keep_link_on = 1;
1346#endif
1347
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001348
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001349 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001350 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001351
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352 snd_hda_codec_proc_new(codec);
1353
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001354 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001355
1356 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1357 codec->subsystem_id, codec->revision_id);
1358 snd_component_add(codec->bus->card, component);
1359
Takashi Iwai2565c892014-02-19 11:41:09 +01001360 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1361 if (err < 0)
1362 goto error;
1363
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001364 if (codecp)
1365 *codecp = codec;
1366 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001367
1368 error:
1369 snd_hda_codec_free(codec);
1370 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001371}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001372EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001373
Takashi Iwai95a962c2014-10-29 16:03:58 +01001374/**
1375 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1376 * @codec: the HDA codec
1377 *
1378 * Forcibly refresh the all widget caps and the init pin configurations of
1379 * the given codec.
1380 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001381int snd_hda_codec_update_widgets(struct hda_codec *codec)
1382{
1383 hda_nid_t fg;
1384 int err;
1385
1386 /* Assume the function group node does not change,
1387 * only the widget nodes may change.
1388 */
1389 kfree(codec->wcaps);
1390 fg = codec->afg ? codec->afg : codec->mfg;
1391 err = read_widget_caps(codec, fg);
1392 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001393 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001394 return err;
1395 }
1396
1397 snd_array_free(&codec->init_pins);
1398 err = read_pin_defaults(codec);
1399
1400 return err;
1401}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001402EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001403
Takashi Iwaied360812012-08-08 17:12:52 +02001404/* update the stream-id if changed */
1405static void update_pcm_stream_id(struct hda_codec *codec,
1406 struct hda_cvt_setup *p, hda_nid_t nid,
1407 u32 stream_tag, int channel_id)
1408{
1409 unsigned int oldval, newval;
1410
1411 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1412 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1413 newval = (stream_tag << 4) | channel_id;
1414 if (oldval != newval)
1415 snd_hda_codec_write(codec, nid, 0,
1416 AC_VERB_SET_CHANNEL_STREAMID,
1417 newval);
1418 p->stream_tag = stream_tag;
1419 p->channel_id = channel_id;
1420 }
1421}
1422
1423/* update the format-id if changed */
1424static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1425 hda_nid_t nid, int format)
1426{
1427 unsigned int oldval;
1428
1429 if (p->format_id != format) {
1430 oldval = snd_hda_codec_read(codec, nid, 0,
1431 AC_VERB_GET_STREAM_FORMAT, 0);
1432 if (oldval != format) {
1433 msleep(1);
1434 snd_hda_codec_write(codec, nid, 0,
1435 AC_VERB_SET_STREAM_FORMAT,
1436 format);
1437 }
1438 p->format_id = format;
1439 }
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/**
1443 * snd_hda_codec_setup_stream - set up the codec for streaming
1444 * @codec: the CODEC to set up
1445 * @nid: the NID to set up
1446 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1447 * @channel_id: channel id to pass, zero based.
1448 * @format: stream format.
1449 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001450void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1451 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int channel_id, int format)
1453{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001454 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001455 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001456 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001457 int i;
1458
Takashi Iwai0ba21762007-04-16 11:29:14 +02001459 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001460 return;
1461
Takashi Iwai4e76a882014-02-25 12:21:03 +01001462 codec_dbg(codec,
1463 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1464 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001465 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001466 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001467 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001468
1469 if (codec->pcm_format_first)
1470 update_pcm_format(codec, p, nid, format);
1471 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1472 if (!codec->pcm_format_first)
1473 update_pcm_format(codec, p, nid, format);
1474
Takashi Iwaieb541332010-08-06 13:48:11 +02001475 p->active = 1;
1476 p->dirty = 0;
1477
1478 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001479 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001480 list_for_each_entry(c, &codec->bus->codec_list, list) {
1481 for (i = 0; i < c->cvt_setups.used; i++) {
1482 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001483 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001484 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001485 p->dirty = 1;
1486 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001489EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Takashi Iwaif0cea792010-08-13 11:56:53 +02001491static void really_cleanup_stream(struct hda_codec *codec,
1492 struct hda_cvt_setup *q);
1493
Takashi Iwaid5191e52009-11-16 14:58:17 +01001494/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001495 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001496 * @codec: the CODEC to clean up
1497 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001498 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001499 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001500void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1501 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001502{
Takashi Iwaieb541332010-08-06 13:48:11 +02001503 struct hda_cvt_setup *p;
1504
Takashi Iwai888afa12008-03-18 09:57:50 +01001505 if (!nid)
1506 return;
1507
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001508 if (codec->no_sticky_stream)
1509 do_now = 1;
1510
Takashi Iwai4e76a882014-02-25 12:21:03 +01001511 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001512 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001513 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001514 /* here we just clear the active flag when do_now isn't set;
1515 * actual clean-ups will be done later in
1516 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1517 */
1518 if (do_now)
1519 really_cleanup_stream(codec, p);
1520 else
1521 p->active = 0;
1522 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001523}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001524EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001525
Takashi Iwaieb541332010-08-06 13:48:11 +02001526static void really_cleanup_stream(struct hda_codec *codec,
1527 struct hda_cvt_setup *q)
1528{
1529 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001530 if (q->stream_tag || q->channel_id)
1531 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1532 if (q->format_id)
1533 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1534);
Takashi Iwaieb541332010-08-06 13:48:11 +02001535 memset(q, 0, sizeof(*q));
1536 q->nid = nid;
1537}
1538
1539/* clean up the all conflicting obsolete streams */
1540static void purify_inactive_streams(struct hda_codec *codec)
1541{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001542 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001543 int i;
1544
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001545 list_for_each_entry(c, &codec->bus->codec_list, list) {
1546 for (i = 0; i < c->cvt_setups.used; i++) {
1547 struct hda_cvt_setup *p;
1548 p = snd_array_elem(&c->cvt_setups, i);
1549 if (p->dirty)
1550 really_cleanup_stream(c, p);
1551 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001552 }
1553}
1554
Takashi Iwai2a439522011-07-26 09:52:50 +02001555#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001556/* clean up all streams; called from suspend */
1557static void hda_cleanup_all_streams(struct hda_codec *codec)
1558{
1559 int i;
1560
1561 for (i = 0; i < codec->cvt_setups.used; i++) {
1562 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1563 if (p->stream_tag)
1564 really_cleanup_stream(codec, p);
1565 }
1566}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001567#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/*
1570 * amp access functions
1571 */
1572
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001573/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001574#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001575#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001576#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1577#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001579#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001582static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001583 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Takashi Iwai01751f52007-08-10 16:59:39 +02001585 memset(cache, 0, sizeof(*cache));
1586 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001587 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001588}
1589
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001590static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001591{
Takashi Iwai603c4012008-07-30 15:01:44 +02001592 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001596static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai01751f52007-08-10 16:59:39 +02001598 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1599 u16 cur = cache->hash[idx];
1600 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001603 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (info->key == key)
1605 return info;
1606 cur = info->next;
1607 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001608 return NULL;
1609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001611/* query the hash. allocate an entry if not found. */
1612static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1613 u32 key)
1614{
1615 struct hda_cache_head *info = get_hash(cache, key);
1616 if (!info) {
1617 u16 idx, cur;
1618 /* add a new hash entry */
1619 info = snd_array_new(&cache->buf);
1620 if (!info)
1621 return NULL;
1622 cur = snd_array_index(&cache->buf, info);
1623 info->key = key;
1624 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001625 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001626 idx = key % (u16)ARRAY_SIZE(cache->hash);
1627 info->next = cache->hash[idx];
1628 cache->hash[idx] = cur;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return info;
1631}
1632
Takashi Iwai01751f52007-08-10 16:59:39 +02001633/* query and allocate an amp hash entry */
1634static inline struct hda_amp_info *
1635get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1636{
1637 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1638}
1639
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001640/* overwrite the value with the key in the caps hash */
1641static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1642{
1643 struct hda_amp_info *info;
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 -EINVAL;
1650 }
1651 info->amp_caps = val;
1652 info->head.val |= INFO_AMP_CAPS;
1653 mutex_unlock(&codec->hash_mutex);
1654 return 0;
1655}
1656
1657/* query the value from the caps hash; if not found, fetch the current
1658 * value from the given function and store in the hash
1659 */
1660static unsigned int
1661query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1662 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1663{
1664 struct hda_amp_info *info;
1665 unsigned int val;
1666
1667 mutex_lock(&codec->hash_mutex);
1668 info = get_alloc_amp_hash(codec, key);
1669 if (!info) {
1670 mutex_unlock(&codec->hash_mutex);
1671 return 0;
1672 }
1673 if (!(info->head.val & INFO_AMP_CAPS)) {
1674 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1675 val = func(codec, nid, dir);
1676 write_caps_hash(codec, key, val);
1677 } else {
1678 val = info->amp_caps;
1679 mutex_unlock(&codec->hash_mutex);
1680 }
1681 return val;
1682}
1683
1684static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1685 int direction)
1686{
1687 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1688 nid = codec->afg;
1689 return snd_hda_param_read(codec, nid,
1690 direction == HDA_OUTPUT ?
1691 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1692}
1693
Takashi Iwaid5191e52009-11-16 14:58:17 +01001694/**
1695 * query_amp_caps - query AMP capabilities
1696 * @codec: the HD-auio codec
1697 * @nid: the NID to query
1698 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1699 *
1700 * Query AMP capabilities for the given widget and direction.
1701 * Returns the obtained capability bits.
1702 *
1703 * When cap bits have been already read, this doesn't read again but
1704 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001706u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001708 return query_caps_hash(codec, nid, direction,
1709 HDA_HASH_KEY(nid, direction, 0),
1710 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001712EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Takashi Iwaid5191e52009-11-16 14:58:17 +01001714/**
David Henningsson861a04e2014-09-23 10:38:17 +02001715 * snd_hda_check_amp_caps - query AMP capabilities
1716 * @codec: the HD-audio codec
1717 * @nid: the NID to query
1718 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001719 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001720 *
1721 * Check whether the widget has the given amp capability for the direction.
1722 */
1723bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1724 int dir, unsigned int bits)
1725{
1726 if (!nid)
1727 return false;
1728 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1729 if (query_amp_caps(codec, nid, dir) & bits)
1730 return true;
1731 return false;
1732}
1733EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1734
1735/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736 * snd_hda_override_amp_caps - Override the AMP capabilities
1737 * @codec: the CODEC to clean up
1738 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001739 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001740 * @caps: the capability bits to set
1741 *
1742 * Override the cached AMP caps bits value by the given one.
1743 * This function is useful if the driver needs to adjust the AMP ranges,
1744 * e.g. limit to 0dB, etc.
1745 *
1746 * Returns zero if successful or a negative error code.
1747 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001748int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1749 unsigned int caps)
1750{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001751 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001752}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001753EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001754
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1756 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001757{
1758 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1759}
1760
Takashi Iwaid5191e52009-11-16 14:58:17 +01001761/**
1762 * snd_hda_query_pin_caps - Query PIN capabilities
1763 * @codec: the HD-auio codec
1764 * @nid: the NID to query
1765 *
1766 * Query PIN capabilities for the given widget.
1767 * Returns the obtained capability bits.
1768 *
1769 * When cap bits have been already read, this doesn't read again but
1770 * returns the cached value.
1771 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001772u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1773{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001775 read_pin_cap);
1776}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001777EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001778
Wu Fengguang864f92b2009-11-18 12:38:02 +08001779/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001780 * snd_hda_override_pin_caps - Override the pin capabilities
1781 * @codec: the CODEC
1782 * @nid: the NID to override
1783 * @caps: the capability bits to set
1784 *
1785 * Override the cached PIN capabilitiy bits value by the given one.
1786 *
1787 * Returns zero if successful or a negative error code.
1788 */
1789int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1790 unsigned int caps)
1791{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001792 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001793}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001794EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001795
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001796/* read or sync the hash value with the current value;
1797 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001799static struct hda_amp_info *
1800update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001801 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001803 struct hda_amp_info *info;
1804 unsigned int parm, val = 0;
1805 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001807 retry:
1808 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1809 if (!info)
1810 return NULL;
1811 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1812 if (!val_read) {
1813 mutex_unlock(&codec->hash_mutex);
1814 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1815 parm |= direction == HDA_OUTPUT ?
1816 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1817 parm |= index;
1818 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001819 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001820 val &= 0xff;
1821 val_read = true;
1822 mutex_lock(&codec->hash_mutex);
1823 goto retry;
1824 }
1825 info->vol[ch] = val;
1826 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001827 } else if (init_only)
1828 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001829 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001833 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001835static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001836 hda_nid_t nid, int ch, int direction, int index,
1837 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 u32 parm;
1840
1841 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1842 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1843 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001844 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1845 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001846 ; /* set the zero value as a fake mute */
1847 else
1848 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1850}
1851
Takashi Iwaid5191e52009-11-16 14:58:17 +01001852/**
1853 * snd_hda_codec_amp_read - Read AMP value
1854 * @codec: HD-audio codec
1855 * @nid: NID to read the AMP value
1856 * @ch: channel (left=0 or right=1)
1857 * @direction: #HDA_INPUT or #HDA_OUTPUT
1858 * @index: the index value (only for input direction)
1859 *
1860 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
Takashi Iwai834be882006-03-01 14:16:17 +01001862int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1863 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001865 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001866 unsigned int val = 0;
1867
1868 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001869 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001870 if (info)
1871 val = info->vol[ch];
1872 mutex_unlock(&codec->hash_mutex);
1873 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001875EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai280e57d2012-12-14 10:32:21 +01001877static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1878 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001879 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001880{
1881 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001882 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001883
1884 if (snd_BUG_ON(mask & ~0xff))
1885 mask &= 0xff;
1886 val &= mask;
1887
1888 mutex_lock(&codec->hash_mutex);
1889 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1890 if (!info) {
1891 mutex_unlock(&codec->hash_mutex);
1892 return 0;
1893 }
1894 val |= info->vol[ch] & ~mask;
1895 if (info->vol[ch] == val) {
1896 mutex_unlock(&codec->hash_mutex);
1897 return 0;
1898 }
1899 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001900 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001901 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001902 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001903 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001904 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001905 return 1;
1906}
1907
Takashi Iwaid5191e52009-11-16 14:58:17 +01001908/**
1909 * snd_hda_codec_amp_update - update the AMP value
1910 * @codec: HD-audio codec
1911 * @nid: NID to read the AMP value
1912 * @ch: channel (left=0 or right=1)
1913 * @direction: #HDA_INPUT or #HDA_OUTPUT
1914 * @idx: the index value (only for input direction)
1915 * @mask: bit mask to set
1916 * @val: the bits value to set
1917 *
1918 * Update the AMP value with a bit mask.
1919 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001920 */
Takashi Iwai834be882006-03-01 14:16:17 +01001921int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1922 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001924 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1925 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001927EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Takashi Iwaid5191e52009-11-16 14:58:17 +01001929/**
1930 * snd_hda_codec_amp_stereo - update the AMP stereo values
1931 * @codec: HD-audio codec
1932 * @nid: NID to read the AMP value
1933 * @direction: #HDA_INPUT or #HDA_OUTPUT
1934 * @idx: the index value (only for input direction)
1935 * @mask: bit mask to set
1936 * @val: the bits value to set
1937 *
1938 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1939 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001940 */
1941int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1942 int direction, int idx, int mask, int val)
1943{
1944 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001945
1946 if (snd_BUG_ON(mask & ~0xff))
1947 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001948 for (ch = 0; ch < 2; ch++)
1949 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1950 idx, mask, val);
1951 return ret;
1952}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001953EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001954
Takashi Iwai95a962c2014-10-29 16:03:58 +01001955/**
1956 * snd_hda_codec_amp_init - initialize the AMP value
1957 * @codec: the HDA codec
1958 * @nid: NID to read the AMP value
1959 * @ch: channel (left=0 or right=1)
1960 * @dir: #HDA_INPUT or #HDA_OUTPUT
1961 * @idx: the index value (only for input direction)
1962 * @mask: bit mask to set
1963 * @val: the bits value to set
1964 *
1965 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001966 * the first access. If the amp was already initialized / updated beforehand,
1967 * this does nothing.
1968 */
1969int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1970 int dir, int idx, int mask, int val)
1971{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001972 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1973 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001974}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001975EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001976
Takashi Iwai95a962c2014-10-29 16:03:58 +01001977/**
1978 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1979 * @codec: the HDA codec
1980 * @nid: NID to read the AMP value
1981 * @dir: #HDA_INPUT or #HDA_OUTPUT
1982 * @idx: the index value (only for input direction)
1983 * @mask: bit mask to set
1984 * @val: the bits value to set
1985 *
1986 * Call snd_hda_codec_amp_init() for both stereo channels.
1987 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001988int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1989 int dir, int idx, int mask, int val)
1990{
1991 int ch, ret = 0;
1992
1993 if (snd_BUG_ON(mask & ~0xff))
1994 mask &= 0xff;
1995 for (ch = 0; ch < 2; ch++)
1996 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1997 idx, mask, val);
1998 return ret;
1999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002000EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002001
Takashi Iwaid5191e52009-11-16 14:58:17 +01002002/**
2003 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2004 * @codec: HD-audio codec
2005 *
2006 * Resume the all amp commands from the cache.
2007 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002008void snd_hda_codec_resume_amp(struct hda_codec *codec)
2009{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002010 int i;
2011
Takashi Iwaic370dd62012-12-13 18:30:04 +01002012 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002013 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002014 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2015 struct hda_amp_info *buffer;
2016 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002017 hda_nid_t nid;
2018 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002019 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002020
2021 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002022 if (!buffer->head.dirty)
2023 continue;
2024 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002025 info = *buffer;
2026 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002027 if (!key)
2028 continue;
2029 nid = key & 0xff;
2030 idx = (key >> 16) & 0xff;
2031 dir = (key >> 24) & 0xff;
2032 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002033 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002034 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002035 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002036 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2037 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002038 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002039 }
2040 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002041 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002042}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002043EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002045static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2046 unsigned int ofs)
2047{
2048 u32 caps = query_amp_caps(codec, nid, dir);
2049 /* get num steps */
2050 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2051 if (ofs < caps)
2052 caps -= ofs;
2053 return caps;
2054}
2055
Takashi Iwaid5191e52009-11-16 14:58:17 +01002056/**
2057 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002058 * @kcontrol: referred ctl element
2059 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002060 *
2061 * The control element is supposed to have the private_value field
2062 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2063 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002064int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2068 u16 nid = get_amp_nid(kcontrol);
2069 u8 chs = get_amp_channels(kcontrol);
2070 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002071 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002073 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2074 uinfo->count = chs == 3 ? 2 : 1;
2075 uinfo->value.integer.min = 0;
2076 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2077 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002078 codec_warn(codec,
2079 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2080 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return -EINVAL;
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 0;
2084}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002085EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002087
2088static inline unsigned int
2089read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2090 int ch, int dir, int idx, unsigned int ofs)
2091{
2092 unsigned int val;
2093 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2094 val &= HDA_AMP_VOLMASK;
2095 if (val >= ofs)
2096 val -= ofs;
2097 else
2098 val = 0;
2099 return val;
2100}
2101
2102static inline int
2103update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2104 int ch, int dir, int idx, unsigned int ofs,
2105 unsigned int val)
2106{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002107 unsigned int maxval;
2108
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002109 if (val > 0)
2110 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002111 /* ofs = 0: raw max value */
2112 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002113 if (val > maxval)
2114 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002115 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2116 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002117}
2118
Takashi Iwaid5191e52009-11-16 14:58:17 +01002119/**
2120 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002121 * @kcontrol: ctl element
2122 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002123 *
2124 * The control element is supposed to have the private_value field
2125 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2126 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002127int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2128 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2131 hda_nid_t nid = get_amp_nid(kcontrol);
2132 int chs = get_amp_channels(kcontrol);
2133 int dir = get_amp_direction(kcontrol);
2134 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002135 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 long *valp = ucontrol->value.integer.value;
2137
2138 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002139 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002141 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002144EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Takashi Iwaid5191e52009-11-16 14:58:17 +01002146/**
2147 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002148 * @kcontrol: ctl element
2149 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150 *
2151 * The control element is supposed to have the private_value field
2152 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2153 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2158 hda_nid_t nid = get_amp_nid(kcontrol);
2159 int chs = get_amp_channels(kcontrol);
2160 int dir = get_amp_direction(kcontrol);
2161 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002162 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 long *valp = ucontrol->value.integer.value;
2164 int change = 0;
2165
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002166 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002167 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002168 valp++;
2169 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002170 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002171 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return change;
2173}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002174EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Takashi Iwaid5191e52009-11-16 14:58:17 +01002176/**
2177 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002178 * @kcontrol: ctl element
2179 * @op_flag: operation flag
2180 * @size: byte size of input TLV
2181 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002182 *
2183 * The control element is supposed to have the private_value field
2184 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2185 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002186int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2187 unsigned int size, unsigned int __user *_tlv)
2188{
2189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2190 hda_nid_t nid = get_amp_nid(kcontrol);
2191 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002192 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002193 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002194 u32 caps, val1, val2;
2195
2196 if (size < 4 * sizeof(unsigned int))
2197 return -ENOMEM;
2198 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002199 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2200 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002201 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002202 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002203 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002204 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002205 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002206 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2207 return -EFAULT;
2208 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2209 return -EFAULT;
2210 if (put_user(val1, _tlv + 2))
2211 return -EFAULT;
2212 if (put_user(val2, _tlv + 3))
2213 return -EFAULT;
2214 return 0;
2215}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002216EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
2219 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2220 * @codec: HD-audio codec
2221 * @nid: NID of a reference widget
2222 * @dir: #HDA_INPUT or #HDA_OUTPUT
2223 * @tlv: TLV data to be stored, at least 4 elements
2224 *
2225 * Set (static) TLV data for a virtual master volume using the AMP caps
2226 * obtained from the reference NID.
2227 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002228 */
2229void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2230 unsigned int *tlv)
2231{
2232 u32 caps;
2233 int nums, step;
2234
2235 caps = query_amp_caps(codec, nid, dir);
2236 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2237 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2238 step = (step + 1) * 25;
2239 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2240 tlv[1] = 2 * sizeof(unsigned int);
2241 tlv[2] = -nums * step;
2242 tlv[3] = step;
2243}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002244EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002245
2246/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002247static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002248find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002249{
2250 struct snd_ctl_elem_id id;
2251 memset(&id, 0, sizeof(id));
2252 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002253 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002254 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002255 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2256 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002257 strcpy(id.name, name);
2258 return snd_ctl_find_id(codec->bus->card, &id);
2259}
2260
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261/**
2262 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2263 * @codec: HD-audio codec
2264 * @name: ctl id name string
2265 *
2266 * Get the control element with the given id string and IFACE_MIXER.
2267 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002268struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2269 const char *name)
2270{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002271 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002272}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002273EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002274
Takashi Iwaidcda5802012-10-12 17:24:51 +02002275static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002276 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002277{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002278 int i, idx;
2279 /* 16 ctlrs should be large enough */
2280 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2281 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002282 return idx;
2283 }
2284 return -EBUSY;
2285}
2286
Takashi Iwaid5191e52009-11-16 14:58:17 +01002287/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002288 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002289 * @codec: HD-audio codec
2290 * @nid: corresponding NID (optional)
2291 * @kctl: the control element to assign
2292 *
2293 * Add the given control element to an array inside the codec instance.
2294 * All control elements belonging to a codec are supposed to be added
2295 * by this function so that a proper clean-up works at the free or
2296 * reconfiguration time.
2297 *
2298 * If non-zero @nid is passed, the NID is assigned to the control element.
2299 * The assignment is shown in the codec proc file.
2300 *
2301 * snd_hda_ctl_add() checks the control subdev id field whether
2302 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002303 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2304 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002305 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2307 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002308{
2309 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002310 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002311 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002313 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002314 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002315 if (nid == 0)
2316 nid = get_amp_nid_(kctl->private_value);
2317 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002318 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2319 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002320 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002321 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002322 err = snd_ctl_add(codec->bus->card, kctl);
2323 if (err < 0)
2324 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002325 item = snd_array_new(&codec->mixers);
2326 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002327 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002328 item->kctl = kctl;
2329 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002330 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002331 return 0;
2332}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002333EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002334
Takashi Iwaid5191e52009-11-16 14:58:17 +01002335/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002336 * snd_hda_add_nid - Assign a NID to a control element
2337 * @codec: HD-audio codec
2338 * @nid: corresponding NID (optional)
2339 * @kctl: the control element to assign
2340 * @index: index to kctl
2341 *
2342 * Add the given control element to an array inside the codec instance.
2343 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2344 * NID:KCTL mapping - for example "Capture Source" selector.
2345 */
2346int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2347 unsigned int index, hda_nid_t nid)
2348{
2349 struct hda_nid_item *item;
2350
2351 if (nid > 0) {
2352 item = snd_array_new(&codec->nids);
2353 if (!item)
2354 return -ENOMEM;
2355 item->kctl = kctl;
2356 item->index = index;
2357 item->nid = nid;
2358 return 0;
2359 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002360 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2361 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002362 return -EINVAL;
2363}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002364EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002365
2366/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002367 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2368 * @codec: HD-audio codec
2369 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002370void snd_hda_ctls_clear(struct hda_codec *codec)
2371{
2372 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002373 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002374 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002375 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002376 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002377 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002378}
2379
Takashi Iwai95a962c2014-10-29 16:03:58 +01002380/**
2381 * snd_hda_lock_devices - pseudo device locking
2382 * @bus: the BUS
2383 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002384 * toggle card->shutdown to allow/disallow the device access (as a hack)
2385 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002386int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002387{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002388 struct snd_card *card = bus->card;
2389 struct hda_codec *codec;
2390
Takashi Iwaia65d6292009-02-23 16:57:04 +01002391 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002392 if (card->shutdown)
2393 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002394 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002395 if (!list_empty(&card->ctl_files))
2396 goto err_clear;
2397
2398 list_for_each_entry(codec, &bus->codec_list, list) {
2399 int pcm;
2400 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2401 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2402 if (!cpcm->pcm)
2403 continue;
2404 if (cpcm->pcm->streams[0].substream_opened ||
2405 cpcm->pcm->streams[1].substream_opened)
2406 goto err_clear;
2407 }
2408 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002409 spin_unlock(&card->files_lock);
2410 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002411
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002412 err_clear:
2413 card->shutdown = 0;
2414 err_unlock:
2415 spin_unlock(&card->files_lock);
2416 return -EINVAL;
2417}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002418EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002419
Takashi Iwai95a962c2014-10-29 16:03:58 +01002420/**
2421 * snd_hda_unlock_devices - pseudo device unlocking
2422 * @bus: the BUS
2423 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002424void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002425{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002426 struct snd_card *card = bus->card;
2427
2428 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002429 spin_lock(&card->files_lock);
2430 card->shutdown = 0;
2431 spin_unlock(&card->files_lock);
2432}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002433EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434
Takashi Iwaid5191e52009-11-16 14:58:17 +01002435/**
2436 * snd_hda_codec_reset - Clear all objects assigned to the codec
2437 * @codec: HD-audio codec
2438 *
2439 * This frees the all PCM and control elements assigned to the codec, and
2440 * clears the caches and restores the pin default configurations.
2441 *
2442 * When a device is being used, it returns -EBSY. If successfully freed,
2443 * returns zero.
2444 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002445int snd_hda_codec_reset(struct hda_codec *codec)
2446{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002447 struct hda_bus *bus = codec->bus;
2448 struct snd_card *card = bus->card;
2449 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002450
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002451 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002452 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002453
2454 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002455 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002456 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002457 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002458 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002459 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002460 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002461 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002462 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002463 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002464 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002465 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002466 snd_hda_detach_beep_device(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002467 if (device_is_registered(hda_codec_dev(codec)))
2468 device_del(hda_codec_dev(codec));
2469
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002470 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002471 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002472 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002473 codec->spec = NULL;
2474 free_hda_cache(&codec->amp_cache);
2475 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002476 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2477 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002478 /* free only driver_pins so that init_pins + user_pins are restored */
2479 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002480 snd_array_free(&codec->cvt_setups);
2481 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002482 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002483 codec->num_pcms = 0;
2484 codec->pcm_info = NULL;
2485 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002486 codec->slave_dig_outs = NULL;
2487 codec->spdif_status_reset = 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002488
2489 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002490 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002491 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002492}
2493
Takashi Iwai6194b992014-06-06 18:12:16 +02002494typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002495
2496/* apply the function to all matching slave ctls in the mixer list */
2497static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002498 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002499{
2500 struct hda_nid_item *items;
2501 const char * const *s;
2502 int i, err;
2503
2504 items = codec->mixers.list;
2505 for (i = 0; i < codec->mixers.used; i++) {
2506 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002507 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002508 continue;
2509 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002510 char tmpname[sizeof(sctl->id.name)];
2511 const char *name = *s;
2512 if (suffix) {
2513 snprintf(tmpname, sizeof(tmpname), "%s %s",
2514 name, suffix);
2515 name = tmpname;
2516 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002517 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002518 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002519 if (err)
2520 return err;
2521 break;
2522 }
2523 }
2524 }
2525 return 0;
2526}
2527
Takashi Iwai6194b992014-06-06 18:12:16 +02002528static int check_slave_present(struct hda_codec *codec,
2529 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002530{
2531 return 1;
2532}
2533
Takashi Iwai18478e82012-03-09 17:51:10 +01002534/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002535static int get_kctl_0dB_offset(struct hda_codec *codec,
2536 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002537{
2538 int _tlv[4];
2539 const int *tlv = NULL;
2540 int val = -1;
2541
2542 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2543 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2544 mm_segment_t fs = get_fs();
2545 set_fs(get_ds());
2546 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2547 tlv = _tlv;
2548 set_fs(fs);
2549 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2550 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002551 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2552 int step = tlv[3];
2553 step &= ~TLV_DB_SCALE_MUTE;
2554 if (!step)
2555 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002556 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002557 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002558- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002559 return -1;
2560 }
2561 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002562 val = -tlv[2] / step;
2563 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002564 return val;
2565}
2566
2567/* call kctl->put with the given value(s) */
2568static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2569{
2570 struct snd_ctl_elem_value *ucontrol;
2571 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2572 if (!ucontrol)
2573 return -ENOMEM;
2574 ucontrol->value.integer.value[0] = val;
2575 ucontrol->value.integer.value[1] = val;
2576 kctl->put(kctl, ucontrol);
2577 kfree(ucontrol);
2578 return 0;
2579}
2580
2581/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002582static int init_slave_0dB(struct hda_codec *codec,
2583 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002584{
Takashi Iwai6194b992014-06-06 18:12:16 +02002585 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002586 if (offset > 0)
2587 put_kctl_with_value(slave, offset);
2588 return 0;
2589}
2590
2591/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002592static int init_slave_unmute(struct hda_codec *codec,
2593 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002594{
2595 return put_kctl_with_value(slave, 1);
2596}
2597
Takashi Iwaie8750942014-06-30 14:02:39 +02002598static int add_slave(struct hda_codec *codec,
2599 void *data, struct snd_kcontrol *slave)
2600{
2601 return snd_ctl_add_slave(data, slave);
2602}
2603
Takashi Iwaid5191e52009-11-16 14:58:17 +01002604/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002605 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002606 * @codec: HD-audio codec
2607 * @name: vmaster control name
2608 * @tlv: TLV data (optional)
2609 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002610 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002611 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002612 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002613 *
2614 * Create a virtual master control with the given name. The TLV data
2615 * must be either NULL or a valid data.
2616 *
2617 * @slaves is a NULL-terminated array of strings, each of which is a
2618 * slave control name. All controls with these names are assigned to
2619 * the new virtual master control.
2620 *
2621 * This function returns zero if successful or a negative error code.
2622 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002623int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002624 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002625 const char *suffix, bool init_slave_vol,
2626 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002627{
2628 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002629 int err;
2630
Takashi Iwai29e58532012-03-12 12:25:03 +01002631 if (ctl_ret)
2632 *ctl_ret = NULL;
2633
Takashi Iwai9322ca52012-02-03 14:28:01 +01002634 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002635 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002636 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002637 return 0;
2638 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002639 kctl = snd_ctl_make_virtual_master(name, tlv);
2640 if (!kctl)
2641 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002642 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002643 if (err < 0)
2644 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002645
Takashi Iwaie8750942014-06-30 14:02:39 +02002646 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002647 if (err < 0)
2648 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002649
2650 /* init with master mute & zero volume */
2651 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002652 if (init_slave_vol) {
2653 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002654 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002655 tlv ? init_slave_0dB : init_slave_unmute, &step);
2656 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002657
Takashi Iwai29e58532012-03-12 12:25:03 +01002658 if (ctl_ret)
2659 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002660 return 0;
2661}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002662EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002663
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002664/*
2665 * mute-LED control using vmaster
2666 */
2667static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_info *uinfo)
2669{
2670 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002671 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002672 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002673
Takashi Iwai3ff72212014-10-20 18:17:28 +02002674 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002675}
2676
2677static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2681 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2682 return 0;
2683}
2684
2685static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2687{
2688 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2689 unsigned int old_mode = hook->mute_mode;
2690
2691 hook->mute_mode = ucontrol->value.enumerated.item[0];
2692 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2693 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2694 if (old_mode == hook->mute_mode)
2695 return 0;
2696 snd_hda_sync_vmaster_hook(hook);
2697 return 1;
2698}
2699
2700static struct snd_kcontrol_new vmaster_mute_mode = {
2701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2702 .name = "Mute-LED Mode",
2703 .info = vmaster_mute_mode_info,
2704 .get = vmaster_mute_mode_get,
2705 .put = vmaster_mute_mode_put,
2706};
2707
Takashi Iwai95a962c2014-10-29 16:03:58 +01002708/**
2709 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2710 * @codec: the HDA codec
2711 * @hook: the vmaster hook object
2712 * @expose_enum_ctl: flag to create an enum ctl
2713 *
2714 * Add a mute-LED hook with the given vmaster switch kctl.
2715 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2716 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002717 */
2718int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002719 struct hda_vmaster_mute_hook *hook,
2720 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002721{
2722 struct snd_kcontrol *kctl;
2723
2724 if (!hook->hook || !hook->sw_kctl)
2725 return 0;
2726 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2727 hook->codec = codec;
2728 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002729 if (!expose_enum_ctl)
2730 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002731 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2732 if (!kctl)
2733 return -ENOMEM;
2734 return snd_hda_ctl_add(codec, 0, kctl);
2735}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002736EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002737
Takashi Iwai95a962c2014-10-29 16:03:58 +01002738/**
2739 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2740 * @hook: the vmaster hook
2741 *
2742 * Call the hook with the current value for synchronization.
2743 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002744 */
2745void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2746{
2747 if (!hook->hook || !hook->codec)
2748 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002749 /* don't call vmaster hook in the destructor since it might have
2750 * been already destroyed
2751 */
2752 if (hook->codec->bus->shutdown)
2753 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002754 switch (hook->mute_mode) {
2755 case HDA_VMUTE_FOLLOW_MASTER:
2756 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2757 break;
2758 default:
2759 hook->hook(hook->codec, hook->mute_mode);
2760 break;
2761 }
2762}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002763EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002764
2765
Takashi Iwaid5191e52009-11-16 14:58:17 +01002766/**
2767 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002768 * @kcontrol: referred ctl element
2769 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002770 *
2771 * The control element is supposed to have the private_value field
2772 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2773 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002774int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2775 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 int chs = get_amp_channels(kcontrol);
2778
2779 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2780 uinfo->count = chs == 3 ? 2 : 1;
2781 uinfo->value.integer.min = 0;
2782 uinfo->value.integer.max = 1;
2783 return 0;
2784}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002785EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Takashi Iwaid5191e52009-11-16 14:58:17 +01002787/**
2788 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002789 * @kcontrol: ctl element
2790 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002791 *
2792 * The control element is supposed to have the private_value field
2793 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2794 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002795int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2796 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2799 hda_nid_t nid = get_amp_nid(kcontrol);
2800 int chs = get_amp_channels(kcontrol);
2801 int dir = get_amp_direction(kcontrol);
2802 int idx = get_amp_index(kcontrol);
2803 long *valp = ucontrol->value.integer.value;
2804
2805 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002806 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002807 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002809 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002810 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return 0;
2812}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002813EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Takashi Iwaid5191e52009-11-16 14:58:17 +01002815/**
2816 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002817 * @kcontrol: ctl element
2818 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002819 *
2820 * The control element is supposed to have the private_value field
2821 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2822 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002823int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 hda_nid_t nid = get_amp_nid(kcontrol);
2828 int chs = get_amp_channels(kcontrol);
2829 int dir = get_amp_direction(kcontrol);
2830 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 long *valp = ucontrol->value.integer.value;
2832 int change = 0;
2833
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002834 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002835 change = codec_amp_update(codec, nid, 0, dir, idx,
2836 HDA_AMP_MUTE,
2837 *valp ? 0 : HDA_AMP_MUTE, false,
2838 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002839 valp++;
2840 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002841 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002842 change |= codec_amp_update(codec, nid, 1, dir, idx,
2843 HDA_AMP_MUTE,
2844 *valp ? 0 : HDA_AMP_MUTE, false,
2845 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002846 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return change;
2848}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002849EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851/*
Takashi Iwai985be542005-11-02 18:26:49 +01002852 * bound volume controls
2853 *
2854 * bind multiple volumes (# indices, from 0)
2855 */
2856
2857#define AMP_VAL_IDX_SHIFT 19
2858#define AMP_VAL_IDX_MASK (0x0f<<19)
2859
Takashi Iwaid5191e52009-11-16 14:58:17 +01002860/**
2861 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002862 * @kcontrol: ctl element
2863 * @ucontrol: 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_MUTE*() macros.
2867 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002868int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002870{
2871 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2872 unsigned long pval;
2873 int err;
2874
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002875 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002876 pval = kcontrol->private_value;
2877 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2878 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2879 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002881 return err;
2882}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002883EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002884
Takashi Iwaid5191e52009-11-16 14:58:17 +01002885/**
2886 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume 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_MUTE*() macros.
2892 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002893int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002895{
2896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2897 unsigned long pval;
2898 int i, indices, err = 0, change = 0;
2899
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002900 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002901 pval = kcontrol->private_value;
2902 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2903 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002904 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2905 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002906 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2907 if (err < 0)
2908 break;
2909 change |= err;
2910 }
2911 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002912 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002913 return err < 0 ? err : change;
2914}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002915EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002916
Takashi Iwaid5191e52009-11-16 14:58:17 +01002917/**
2918 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002919 * @kcontrol: referred ctl element
2920 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002921 *
2922 * The control element is supposed to have the private_value field
2923 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002924 */
2925int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_info *uinfo)
2927{
2928 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2929 struct hda_bind_ctls *c;
2930 int err;
2931
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002932 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002933 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002934 kcontrol->private_value = *c->values;
2935 err = c->ops->info(kcontrol, uinfo);
2936 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002937 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002938 return err;
2939}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002940EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002941
Takashi Iwaid5191e52009-11-16 14:58:17 +01002942/**
2943 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002944 * @kcontrol: ctl element
2945 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002946 *
2947 * The control element is supposed to have the private_value field
2948 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2949 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002950int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
2952{
2953 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2954 struct hda_bind_ctls *c;
2955 int err;
2956
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002957 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002958 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002959 kcontrol->private_value = *c->values;
2960 err = c->ops->get(kcontrol, ucontrol);
2961 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002962 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002963 return err;
2964}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002965EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002966
Takashi Iwaid5191e52009-11-16 14:58:17 +01002967/**
2968 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002969 * @kcontrol: ctl element
2970 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002971 *
2972 * The control element is supposed to have the private_value field
2973 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2974 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002975int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2977{
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979 struct hda_bind_ctls *c;
2980 unsigned long *vals;
2981 int err = 0, change = 0;
2982
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002983 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002984 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002985 for (vals = c->values; *vals; vals++) {
2986 kcontrol->private_value = *vals;
2987 err = c->ops->put(kcontrol, ucontrol);
2988 if (err < 0)
2989 break;
2990 change |= err;
2991 }
2992 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002993 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002994 return err < 0 ? err : change;
2995}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002996EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002997
Takashi Iwaid5191e52009-11-16 14:58:17 +01002998/**
2999 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003000 * @kcontrol: ctl element
3001 * @op_flag: operation flag
3002 * @size: byte size of input TLV
3003 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003004 *
3005 * The control element is supposed to have the private_value field
3006 * set up via HDA_BIND_VOL() macro.
3007 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003008int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3009 unsigned int size, unsigned int __user *tlv)
3010{
3011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3012 struct hda_bind_ctls *c;
3013 int err;
3014
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003015 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003016 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003017 kcontrol->private_value = *c->values;
3018 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3019 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003020 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003021 return err;
3022}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003023EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003024
3025struct hda_ctl_ops snd_hda_bind_vol = {
3026 .info = snd_hda_mixer_amp_volume_info,
3027 .get = snd_hda_mixer_amp_volume_get,
3028 .put = snd_hda_mixer_amp_volume_put,
3029 .tlv = snd_hda_mixer_amp_tlv
3030};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003031EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003032
3033struct hda_ctl_ops snd_hda_bind_sw = {
3034 .info = snd_hda_mixer_amp_switch_info,
3035 .get = snd_hda_mixer_amp_switch_get,
3036 .put = snd_hda_mixer_amp_switch_put,
3037 .tlv = snd_hda_mixer_amp_tlv
3038};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003039EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003040
3041/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * SPDIF out controls
3043 */
3044
Takashi Iwai0ba21762007-04-16 11:29:14 +02003045static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3046 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
3048 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3049 uinfo->count = 1;
3050 return 0;
3051}
3052
Takashi Iwai0ba21762007-04-16 11:29:14 +02003053static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3054 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3057 IEC958_AES0_NONAUDIO |
3058 IEC958_AES0_CON_EMPHASIS_5015 |
3059 IEC958_AES0_CON_NOT_COPYRIGHT;
3060 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3061 IEC958_AES1_CON_ORIGINAL;
3062 return 0;
3063}
3064
Takashi Iwai0ba21762007-04-16 11:29:14 +02003065static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3069 IEC958_AES0_NONAUDIO |
3070 IEC958_AES0_PRO_EMPHASIS_5015;
3071 return 0;
3072}
3073
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003078 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003079 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003081 mutex_lock(&codec->spdif_mutex);
3082 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003083 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3084 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3085 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3086 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003087 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 return 0;
3090}
3091
3092/* convert from SPDIF status bits to HDA SPDIF bits
3093 * bit 0 (DigEn) is always set zero (to be filled later)
3094 */
3095static unsigned short convert_from_spdif_status(unsigned int sbits)
3096{
3097 unsigned short val = 0;
3098
3099 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003104 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3105 IEC958_AES0_PRO_EMPHASIS_5015)
3106 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3109 IEC958_AES0_CON_EMPHASIS_5015)
3110 val |= AC_DIG1_EMPHASIS;
3111 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3112 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003114 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3116 }
3117 return val;
3118}
3119
3120/* convert to SPDIF status bits from HDA SPDIF bits
3121 */
3122static unsigned int convert_to_spdif_status(unsigned short val)
3123{
3124 unsigned int sbits = 0;
3125
Takashi Iwai0ba21762007-04-16 11:29:14 +02003126 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 sbits |= IEC958_AES0_PROFESSIONAL;
3130 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003131 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3133 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003134 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003136 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003138 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3140 sbits |= val & (0x7f << 8);
3141 }
3142 return sbits;
3143}
3144
Takashi Iwai2f728532008-09-25 16:32:41 +02003145/* set digital convert verbs both for the given NID and its slaves */
3146static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3147 int verb, int val)
3148{
Takashi Iwaidda14412011-05-02 11:29:30 +02003149 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003150
Takashi Iwai9e976972008-11-25 08:17:20 +01003151 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003152 d = codec->slave_dig_outs;
3153 if (!d)
3154 return;
3155 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003156 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003157}
3158
3159static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3160 int dig1, int dig2)
3161{
3162 if (dig1 != -1)
3163 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3164 if (dig2 != -1)
3165 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3166}
3167
Takashi Iwai0ba21762007-04-16 11:29:14 +02003168static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003172 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003173 struct hda_spdif_out *spdif;
3174 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 unsigned short val;
3176 int change;
3177
Ingo Molnar62932df2006-01-16 16:34:20 +01003178 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003179 spdif = snd_array_elem(&codec->spdif_out, idx);
3180 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003181 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3183 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3184 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003185 val = convert_from_spdif_status(spdif->status);
3186 val |= spdif->ctls & 1;
3187 change = spdif->ctls != val;
3188 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003189 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003190 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003191 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return change;
3193}
3194
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003195#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003201 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003202 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003204 mutex_lock(&codec->spdif_mutex);
3205 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003206 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003207 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return 0;
3209}
3210
Stephen Warren74b654c2011-06-01 11:14:18 -06003211static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3212 int dig1, int dig2)
3213{
3214 set_dig_out_convert(codec, nid, dig1, dig2);
3215 /* unmute amp switch (if any) */
3216 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3217 (dig1 & AC_DIG1_ENABLE))
3218 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3219 HDA_AMP_MUTE, 0);
3220}
3221
Takashi Iwai0ba21762007-04-16 11:29:14 +02003222static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
3225 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003226 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003227 struct hda_spdif_out *spdif;
3228 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 unsigned short val;
3230 int change;
3231
Ingo Molnar62932df2006-01-16 16:34:20 +01003232 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003233 spdif = snd_array_elem(&codec->spdif_out, idx);
3234 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003235 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003237 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003238 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003239 spdif->ctls = val;
3240 if (change && nid != (u16)-1)
3241 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003242 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return change;
3244}
3245
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003246static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 {
3248 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3249 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003250 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 .info = snd_hda_spdif_mask_info,
3252 .get = snd_hda_spdif_cmask_get,
3253 },
3254 {
3255 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003257 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 .info = snd_hda_spdif_mask_info,
3259 .get = snd_hda_spdif_pmask_get,
3260 },
3261 {
3262 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003263 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 .info = snd_hda_spdif_mask_info,
3265 .get = snd_hda_spdif_default_get,
3266 .put = snd_hda_spdif_default_put,
3267 },
3268 {
3269 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003270 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 .info = snd_hda_spdif_out_switch_info,
3272 .get = snd_hda_spdif_out_switch_get,
3273 .put = snd_hda_spdif_out_switch_put,
3274 },
3275 { } /* end */
3276};
3277
3278/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003279 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003281 * @associated_nid: NID that new ctls associated with
3282 * @cvt_nid: converter NID
3283 * @type: HDA_PCM_TYPE_*
3284 * Creates controls related with the digital output.
3285 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 *
3287 * Returns 0 if successful, or a negative error code.
3288 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003289int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3290 hda_nid_t associated_nid,
3291 hda_nid_t cvt_nid,
3292 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003295 struct snd_kcontrol *kctl;
3296 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003297 int idx = 0;
3298 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003299 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003300 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003302 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003303 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003304 idx = spdif_index;
3305 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003306 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003307 /* suppose a single SPDIF device */
3308 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3309 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3310 if (!kctl)
3311 break;
3312 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003313 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003314 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003315 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003316 if (!bus->primary_dig_out_type)
3317 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003318
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003319 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003320 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003321 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003322 return -EBUSY;
3323 }
Stephen Warren7c935972011-06-01 11:14:17 -06003324 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003325 if (!spdif)
3326 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3328 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003329 if (!kctl)
3330 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003331 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003332 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003333 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003334 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return err;
3336 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003337 spdif->nid = cvt_nid;
3338 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003339 AC_VERB_GET_DIGI_CONVERT_1, 0);
3340 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return 0;
3342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003343EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Takashi Iwai95a962c2014-10-29 16:03:58 +01003345/**
3346 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3347 * @codec: the HDA codec
3348 * @nid: widget NID
3349 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003350 * call within spdif_mutex lock
3351 */
Stephen Warren7c935972011-06-01 11:14:17 -06003352struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3353 hda_nid_t nid)
3354{
3355 int i;
3356 for (i = 0; i < codec->spdif_out.used; i++) {
3357 struct hda_spdif_out *spdif =
3358 snd_array_elem(&codec->spdif_out, i);
3359 if (spdif->nid == nid)
3360 return spdif;
3361 }
3362 return NULL;
3363}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003364EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003365
Takashi Iwai95a962c2014-10-29 16:03:58 +01003366/**
3367 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3368 * @codec: the HDA codec
3369 * @idx: the SPDIF ctl index
3370 *
3371 * Unassign the widget from the given SPDIF control.
3372 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003373void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3374{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003375 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003376
3377 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003378 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003379 spdif->nid = (u16)-1;
3380 mutex_unlock(&codec->spdif_mutex);
3381}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003382EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003383
Takashi Iwai95a962c2014-10-29 16:03:58 +01003384/**
3385 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3386 * @codec: the HDA codec
3387 * @idx: the SPDIF ctl idx
3388 * @nid: widget NID
3389 *
3390 * Assign the widget to the SPDIF control with the given index.
3391 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003392void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3393{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003394 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003395 unsigned short val;
3396
3397 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003398 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003399 if (spdif->nid != nid) {
3400 spdif->nid = nid;
3401 val = spdif->ctls;
3402 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3403 }
3404 mutex_unlock(&codec->spdif_mutex);
3405}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003406EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003409 * SPDIF sharing with analog output
3410 */
3411static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3412 struct snd_ctl_elem_value *ucontrol)
3413{
3414 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3415 ucontrol->value.integer.value[0] = mout->share_spdif;
3416 return 0;
3417}
3418
3419static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3420 struct snd_ctl_elem_value *ucontrol)
3421{
3422 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3423 mout->share_spdif = !!ucontrol->value.integer.value[0];
3424 return 0;
3425}
3426
3427static struct snd_kcontrol_new spdif_share_sw = {
3428 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3429 .name = "IEC958 Default PCM Playback Switch",
3430 .info = snd_ctl_boolean_mono_info,
3431 .get = spdif_share_sw_get,
3432 .put = spdif_share_sw_put,
3433};
3434
Takashi Iwaid5191e52009-11-16 14:58:17 +01003435/**
3436 * snd_hda_create_spdif_share_sw - create Default PCM switch
3437 * @codec: the HDA codec
3438 * @mout: multi-out instance
3439 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003440int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3441 struct hda_multi_out *mout)
3442{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003443 struct snd_kcontrol *kctl;
3444
Takashi Iwai9a081602008-02-12 18:37:26 +01003445 if (!mout->dig_out_nid)
3446 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003447
3448 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3449 if (!kctl)
3450 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003451 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003452 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003454EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003455
3456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 * SPDIF input
3458 */
3459
3460#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3461
Takashi Iwai0ba21762007-04-16 11:29:14 +02003462static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3463 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3466
3467 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3468 return 0;
3469}
3470
Takashi Iwai0ba21762007-04-16 11:29:14 +02003471static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3472 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
3474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3475 hda_nid_t nid = kcontrol->private_value;
3476 unsigned int val = !!ucontrol->value.integer.value[0];
3477 int change;
3478
Ingo Molnar62932df2006-01-16 16:34:20 +01003479 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003481 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003483 snd_hda_codec_write_cache(codec, nid, 0,
3484 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003486 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return change;
3488}
3489
Takashi Iwai0ba21762007-04-16 11:29:14 +02003490static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3494 hda_nid_t nid = kcontrol->private_value;
3495 unsigned short val;
3496 unsigned int sbits;
3497
Andrew Paprocki3982d172007-12-19 12:13:44 +01003498 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 sbits = convert_to_spdif_status(val);
3500 ucontrol->value.iec958.status[0] = sbits;
3501 ucontrol->value.iec958.status[1] = sbits >> 8;
3502 ucontrol->value.iec958.status[2] = sbits >> 16;
3503 ucontrol->value.iec958.status[3] = sbits >> 24;
3504 return 0;
3505}
3506
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003507static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 {
3509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003510 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 .info = snd_hda_spdif_in_switch_info,
3512 .get = snd_hda_spdif_in_switch_get,
3513 .put = snd_hda_spdif_in_switch_put,
3514 },
3515 {
3516 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3517 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003518 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 .info = snd_hda_spdif_mask_info,
3520 .get = snd_hda_spdif_in_status_get,
3521 },
3522 { } /* end */
3523};
3524
3525/**
3526 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3527 * @codec: the HDA codec
3528 * @nid: audio in widget NID
3529 *
3530 * Creates controls related with the SPDIF input.
3531 * Called from each patch supporting the SPDIF in.
3532 *
3533 * Returns 0 if successful, or a negative error code.
3534 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003535int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
3537 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003538 struct snd_kcontrol *kctl;
3539 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003540 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Takashi Iwaidcda5802012-10-12 17:24:51 +02003542 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003543 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003544 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003545 return -EBUSY;
3546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3548 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003549 if (!kctl)
3550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003552 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003553 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return err;
3555 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003556 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003557 snd_hda_codec_read(codec, nid, 0,
3558 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return 0;
3561}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003562EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003564/*
3565 * command cache
3566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Takashi Iwaic370dd62012-12-13 18:30:04 +01003568/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003569#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3570#define get_cmd_cache_nid(key) ((key) & 0xff)
3571#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3572
3573/**
3574 * snd_hda_codec_write_cache - send a single command with caching
3575 * @codec: the HDA codec
3576 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003577 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003578 * @verb: the verb to send
3579 * @parm: the parameter for the verb
3580 *
3581 * Send a single command without waiting for response.
3582 *
3583 * Returns 0 if successful, or a negative error code.
3584 */
3585int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003586 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003587{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003588 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003589 struct hda_cache_head *c;
3590 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003591 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003592
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003593 cache_only = codec->cached_write;
3594 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003595 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003596 if (err < 0)
3597 return err;
3598 }
3599
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003600 /* parm may contain the verb stuff for get/set amp */
3601 verb = verb | (parm >> 8);
3602 parm &= 0xff;
3603 key = build_cmd_cache_key(nid, verb);
3604 mutex_lock(&codec->bus->cmd_mutex);
3605 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003606 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003607 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003608 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003609 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003610 mutex_unlock(&codec->bus->cmd_mutex);
3611 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003612}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003613EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003614
Takashi Iwaid5191e52009-11-16 14:58:17 +01003615/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003616 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3617 * @codec: the HDA codec
3618 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003619 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003620 * @verb: the verb to send
3621 * @parm: the parameter for the verb
3622 *
3623 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3624 * command if the parameter is already identical with the cached value.
3625 * If not, it sends the command and refreshes the cache.
3626 *
3627 * Returns 0 if successful, or a negative error code.
3628 */
3629int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003630 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003631{
3632 struct hda_cache_head *c;
3633 u32 key;
3634
3635 /* parm may contain the verb stuff for get/set amp */
3636 verb = verb | (parm >> 8);
3637 parm &= 0xff;
3638 key = build_cmd_cache_key(nid, verb);
3639 mutex_lock(&codec->bus->cmd_mutex);
3640 c = get_hash(&codec->cmd_cache, key);
3641 if (c && c->val == parm) {
3642 mutex_unlock(&codec->bus->cmd_mutex);
3643 return 0;
3644 }
3645 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003646 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003647}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003648EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003649
3650/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003651 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3652 * @codec: HD-audio codec
3653 *
3654 * Execute all verbs recorded in the command caches to resume.
3655 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003656void snd_hda_codec_resume_cache(struct hda_codec *codec)
3657{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003658 int i;
3659
Takashi Iwaic370dd62012-12-13 18:30:04 +01003660 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003661 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003662 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3663 struct hda_cache_head *buffer;
3664 u32 key;
3665
3666 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3667 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003668 if (!key)
3669 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003670 if (!buffer->dirty)
3671 continue;
3672 buffer->dirty = 0;
3673 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003674 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3675 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003676 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003677 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003678 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003679}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003680EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003681
3682/**
3683 * snd_hda_sequence_write_cache - sequence writes with caching
3684 * @codec: the HDA codec
3685 * @seq: VERB array to send
3686 *
3687 * Send the commands sequentially from the given array.
3688 * Thte commands are recorded on cache for power-save and resume.
3689 * The array must be terminated with NID=0.
3690 */
3691void snd_hda_sequence_write_cache(struct hda_codec *codec,
3692 const struct hda_verb *seq)
3693{
3694 for (; seq->nid; seq++)
3695 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3696 seq->param);
3697}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003698EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003699
Takashi Iwaidc870f32013-01-22 15:24:30 +01003700/**
3701 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3702 * @codec: HD-audio codec
3703 */
3704void snd_hda_codec_flush_cache(struct hda_codec *codec)
3705{
3706 snd_hda_codec_resume_amp(codec);
3707 snd_hda_codec_resume_cache(codec);
3708}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003709EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003710
Takashi Iwai95a962c2014-10-29 16:03:58 +01003711/**
3712 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3713 * @codec: the HDA codec
3714 * @fg: function group (not used now)
3715 * @power_state: the power state to set (AC_PWRST_*)
3716 *
3717 * Set the given power state to all widgets that have the power control.
3718 * If the codec has power_filter set, it evaluates the power state and
3719 * filter out if it's unchanged as D3.
3720 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003721void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003722 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003723{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003726
Takashi Iwaicb53c622007-08-10 17:21:45 +02003727 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003728 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003729 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003730 if (!(wcaps & AC_WCAP_POWER))
3731 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003732 if (codec->power_filter) {
3733 state = codec->power_filter(codec, nid, power_state);
3734 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003736 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003738 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003739 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003740}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003741EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003742
3743/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003744 * supported power states check
3745 */
3746static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3747 unsigned int power_state)
3748{
3749 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3750
Mengdong Line037cb42012-08-10 14:11:58 +02003751 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003752 return false;
3753 if (sup & power_state)
3754 return true;
3755 else
3756 return false;
3757}
3758
3759/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003760 * wait until the state is reached, returns the current state
3761 */
3762static unsigned int hda_sync_power_state(struct hda_codec *codec,
3763 hda_nid_t fg,
3764 unsigned int power_state)
3765{
3766 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3767 unsigned int state, actual_state;
3768
3769 for (;;) {
3770 state = snd_hda_codec_read(codec, fg, 0,
3771 AC_VERB_GET_POWER_STATE, 0);
3772 if (state & AC_PWRST_ERROR)
3773 break;
3774 actual_state = (state >> 4) & 0x0f;
3775 if (actual_state == power_state)
3776 break;
3777 if (time_after_eq(jiffies, end_time))
3778 break;
3779 /* wait until the codec reachs to the target state */
3780 msleep(1);
3781 }
3782 return state;
3783}
3784
Takashi Iwai95a962c2014-10-29 16:03:58 +01003785/**
3786 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3787 * @codec: the HDA codec
3788 * @nid: widget NID
3789 * @power_state: power state to evalue
3790 *
3791 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3792 * This can be used a codec power_filter callback.
3793 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003794unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3795 hda_nid_t nid,
3796 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003797{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003798 if (nid == codec->afg || nid == codec->mfg)
3799 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003800 if (power_state == AC_PWRST_D3 &&
3801 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3802 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3803 int eapd = snd_hda_codec_read(codec, nid, 0,
3804 AC_VERB_GET_EAPD_BTLENABLE, 0);
3805 if (eapd & 0x02)
3806 return AC_PWRST_D0;
3807 }
3808 return power_state;
3809}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003810EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003811
Takashi Iwai432c6412012-08-28 09:59:20 -07003812/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003813 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003814 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003815static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003816 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003817{
Takashi Iwaid8193872012-08-31 07:54:38 -07003818 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003819 int count;
3820 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003821 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003822
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003823 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003824 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003825 if (codec->depop_delay < 0)
3826 msleep(codec->epss ? 10 : 100);
3827 else if (codec->depop_delay > 0)
3828 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003829 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003830 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003831
3832 /* repeat power states setting at most 10 times*/
3833 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003834 if (codec->patch_ops.set_power_state)
3835 codec->patch_ops.set_power_state(codec, fg,
3836 power_state);
3837 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003838 state = power_state;
3839 if (codec->power_filter)
3840 state = codec->power_filter(codec, fg, state);
3841 if (state == power_state || power_state != AC_PWRST_D3)
3842 snd_hda_codec_read(codec, fg, flags,
3843 AC_VERB_SET_POWER_STATE,
3844 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003845 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003846 }
3847 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003848 if (!(state & AC_PWRST_ERROR))
3849 break;
3850 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003851
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003852 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003853}
Takashi Iwai54d17402005-11-21 16:33:22 +01003854
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003855/* sync power states of all widgets;
3856 * this is called at the end of codec parsing
3857 */
3858static void sync_power_up_states(struct hda_codec *codec)
3859{
3860 hda_nid_t nid = codec->start_nid;
3861 int i;
3862
Takashi Iwaiba615b82013-03-13 14:47:21 +01003863 /* don't care if no filter is used */
3864 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003865 return;
3866
3867 for (i = 0; i < codec->num_nodes; i++, nid++) {
3868 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003869 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003870 if (!(wcaps & AC_WCAP_POWER))
3871 continue;
3872 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3873 if (target == AC_PWRST_D0)
3874 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003875 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003876 snd_hda_codec_write(codec, nid, 0,
3877 AC_VERB_SET_POWER_STATE, target);
3878 }
3879}
3880
Takashi Iwai648a8d22014-02-25 10:38:13 +01003881#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003882/* execute additional init verbs */
3883static void hda_exec_init_verbs(struct hda_codec *codec)
3884{
3885 if (codec->init_verbs.list)
3886 snd_hda_sequence_write(codec, codec->init_verbs.list);
3887}
3888#else
3889static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3890#endif
3891
Takashi Iwai2a439522011-07-26 09:52:50 +02003892#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003893/* update the power on/off account with the current jiffies */
3894static void update_power_acct(struct hda_codec *codec, bool on)
3895{
3896 unsigned long delta = jiffies - codec->power_jiffies;
3897
3898 if (on)
3899 codec->power_on_acct += delta;
3900 else
3901 codec->power_off_acct += delta;
3902 codec->power_jiffies += delta;
3903}
3904
3905void snd_hda_update_power_acct(struct hda_codec *codec)
3906{
3907 update_power_acct(codec, hda_codec_is_power_on(codec));
3908}
3909
Takashi Iwaicb53c622007-08-10 17:21:45 +02003910/*
3911 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003912 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003913 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003914static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003915{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003916 unsigned int state;
3917
Takashi Iwaicc72da72015-02-19 16:00:22 +01003918 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003919
Takashi Iwaicb53c622007-08-10 17:21:45 +02003920 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003921 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003922 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003923 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003924 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003925 update_power_acct(codec, true);
3926 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003927 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003928}
3929
Takashi Iwaic370dd62012-12-13 18:30:04 +01003930/* mark all entries of cmd and amp caches dirty */
3931static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3932{
3933 int i;
3934 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3935 struct hda_cache_head *cmd;
3936 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3937 cmd->dirty = 1;
3938 }
3939 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3940 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003941 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003942 amp->head.dirty = 1;
3943 }
3944}
3945
Takashi Iwaicb53c622007-08-10 17:21:45 +02003946/*
3947 * kick up codec; used both from PM and power-save
3948 */
3949static void hda_call_codec_resume(struct hda_codec *codec)
3950{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003951 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003952
Takashi Iwaicc72da72015-02-19 16:00:22 +01003953 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003954 hda_mark_cmd_cache_dirty(codec);
3955
Takashi Iwaicc72da72015-02-19 16:00:22 +01003956 codec->power_jiffies = jiffies;
3957 hda_call_pm_notify(codec, true);
3958
Takashi Iwaid8193872012-08-31 07:54:38 -07003959 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003960 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003961 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003962 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003963 if (codec->patch_ops.resume)
3964 codec->patch_ops.resume(codec);
3965 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003966 if (codec->patch_ops.init)
3967 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003968 snd_hda_codec_resume_amp(codec);
3969 snd_hda_codec_resume_cache(codec);
3970 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003971
3972 if (codec->jackpoll_interval)
3973 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003974 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003975 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003976 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003977}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003978
Takashi Iwaicc72da72015-02-19 16:00:22 +01003979static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003980{
3981 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003982 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003983 int i;
3984
3985 cancel_delayed_work_sync(&codec->jackpoll_work);
3986 for (i = 0; i < codec->num_pcms; i++)
3987 snd_pcm_suspend_all(codec->pcm_info[i].pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003988 state = hda_call_codec_suspend(codec);
3989 if (!codec->bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
3990 hda_call_pm_notify(codec, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003991 return 0;
3992}
3993
Takashi Iwaicc72da72015-02-19 16:00:22 +01003994static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003995{
3996 hda_call_codec_resume(dev_to_hda_codec(dev));
Takashi Iwaicc72da72015-02-19 16:00:22 +01003997 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003998 return 0;
3999}
Takashi Iwai2a439522011-07-26 09:52:50 +02004000#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004001
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004002/* referred in hda_bind.c */
4003const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004004 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4005 pm_runtime_force_resume)
4006 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
4007 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004008};
Takashi Iwai54d17402005-11-21 16:33:22 +01004009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010/**
4011 * snd_hda_build_controls - build mixer controls
4012 * @bus: the BUS
4013 *
4014 * Creates mixer controls for each codec included in the bus.
4015 *
4016 * Returns 0 if successful, otherwise a negative error code.
4017 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004018int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004020 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Takashi Iwai0ba21762007-04-16 11:29:14 +02004022 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004023 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004024 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004025 codec_err(codec,
4026 "cannot build controls for #%d (error %d)\n",
4027 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004028 err = snd_hda_codec_reset(codec);
4029 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004030 codec_err(codec,
4031 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004032 return err;
4033 }
4034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004036 return 0;
4037}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004038EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004040/*
4041 * add standard channel maps if not specified
4042 */
4043static int add_std_chmaps(struct hda_codec *codec)
4044{
4045 int i, str, err;
4046
4047 for (i = 0; i < codec->num_pcms; i++) {
4048 for (str = 0; str < 2; str++) {
4049 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4050 struct hda_pcm_stream *hinfo =
4051 &codec->pcm_info[i].stream[str];
4052 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004053 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004054
4055 if (codec->pcm_info[i].own_chmap)
4056 continue;
4057 if (!pcm || !hinfo->substreams)
4058 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004059 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4060 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004061 hinfo->channels_max,
4062 0, &chmap);
4063 if (err < 0)
4064 return err;
4065 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4066 }
4067 }
4068 return 0;
4069}
4070
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004071/* default channel maps for 2.1 speakers;
4072 * since HD-audio supports only stereo, odd number channels are omitted
4073 */
4074const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4075 { .channels = 2,
4076 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4077 { .channels = 4,
4078 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4079 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4080 { }
4081};
4082EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4083
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004084int snd_hda_codec_build_controls(struct hda_codec *codec)
4085{
4086 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004087 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004088 /* continue to initialize... */
4089 if (codec->patch_ops.init)
4090 err = codec->patch_ops.init(codec);
4091 if (!err && codec->patch_ops.build_controls)
4092 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004093 if (err < 0)
4094 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004095
4096 /* we create chmaps here instead of build_pcms */
4097 err = add_std_chmaps(codec);
4098 if (err < 0)
4099 return err;
4100
David Henningsson26a6cb62012-10-09 15:04:21 +02004101 if (codec->jackpoll_interval)
4102 hda_jackpoll_work(&codec->jackpoll_work.work);
4103 else
4104 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004105 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return 0;
4107}
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109/*
4110 * stream formats
4111 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004112struct hda_rate_tbl {
4113 unsigned int hz;
4114 unsigned int alsa_bits;
4115 unsigned int hda_fmt;
4116};
4117
Takashi Iwai92f10b32010-08-03 14:21:00 +02004118/* rate = base * mult / div */
4119#define HDA_RATE(base, mult, div) \
4120 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4121 (((div) - 1) << AC_FMT_DIV_SHIFT))
4122
Takashi Iwaibefdf312005-08-22 13:57:55 +02004123static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004125
4126 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004127 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4128 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4129 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4130 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4131 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4132 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4133 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4134 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4135 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4136 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4137 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004138#define AC_PAR_PCM_RATE_BITS 11
4139 /* up to bits 10, 384kHZ isn't supported properly */
4140
4141 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004142 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004143
Takashi Iwaibefdf312005-08-22 13:57:55 +02004144 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145};
4146
4147/**
4148 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004149 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 * @rate: the sample rate
4151 * @channels: the number of channels
4152 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4153 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004154 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 *
4156 * Calculate the format bitset from the given rate, channels and th PCM format.
4157 *
4158 * Return zero if invalid.
4159 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004160unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4161 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 unsigned int channels,
4163 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004164 unsigned int maxbps,
4165 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
4167 int i;
4168 unsigned int val = 0;
4169
Takashi Iwaibefdf312005-08-22 13:57:55 +02004170 for (i = 0; rate_bits[i].hz; i++)
4171 if (rate_bits[i].hz == rate) {
4172 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 break;
4174 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004175 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004176 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 return 0;
4178 }
4179
4180 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004181 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return 0;
4183 }
4184 val |= channels - 1;
4185
4186 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004187 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004188 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004189 break;
4190 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004191 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 case 20:
4194 case 24:
4195 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004196 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004197 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004199 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004201 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 break;
4203 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004204 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004205 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return 0;
4207 }
4208
Anssi Hannula32c168c2010-08-03 13:28:57 +03004209 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004210 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return val;
4213}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004214EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004216static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4217 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004218{
4219 unsigned int val = 0;
4220 if (nid != codec->afg &&
4221 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4222 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4223 if (!val || val == -1)
4224 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4225 if (!val || val == -1)
4226 return 0;
4227 return val;
4228}
4229
4230static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4231{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004232 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004233 get_pcm_param);
4234}
4235
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004236static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4237 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004238{
4239 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4240 if (!streams || streams == -1)
4241 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4242 if (!streams || streams == -1)
4243 return 0;
4244 return streams;
4245}
4246
4247static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4248{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004249 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004250 get_stream_param);
4251}
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253/**
4254 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4255 * @codec: the HDA codec
4256 * @nid: NID to query
4257 * @ratesp: the pointer to store the detected rate bitflags
4258 * @formatsp: the pointer to store the detected formats
4259 * @bpsp: the pointer to store the detected format widths
4260 *
4261 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4262 * or @bsps argument is ignored.
4263 *
4264 * Returns 0 if successful, otherwise a negative error code.
4265 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004266int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4268{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004269 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004271 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004272 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 if (ratesp) {
4275 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004276 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004278 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004280 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004281 codec_err(codec,
4282 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4283 nid, val,
4284 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004285 return -EIO;
4286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 *ratesp = rates;
4288 }
4289
4290 if (formatsp || bpsp) {
4291 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004292 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004294 streams = query_stream_param(codec, nid);
4295 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298 bps = 0;
4299 if (streams & AC_SUPFMT_PCM) {
4300 if (val & AC_SUPPCM_BITS_8) {
4301 formats |= SNDRV_PCM_FMTBIT_U8;
4302 bps = 8;
4303 }
4304 if (val & AC_SUPPCM_BITS_16) {
4305 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4306 bps = 16;
4307 }
4308 if (wcaps & AC_WCAP_DIGITAL) {
4309 if (val & AC_SUPPCM_BITS_32)
4310 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4311 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4312 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4313 if (val & AC_SUPPCM_BITS_24)
4314 bps = 24;
4315 else if (val & AC_SUPPCM_BITS_20)
4316 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004317 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4318 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4320 if (val & AC_SUPPCM_BITS_32)
4321 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 else if (val & AC_SUPPCM_BITS_24)
4323 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004324 else if (val & AC_SUPPCM_BITS_20)
4325 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 }
4327 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004328#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004329 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004331 if (!bps)
4332 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004333 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004334#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004335 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004336 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 /* temporary hack: we have still no proper support
4338 * for the direct AC3 stream...
4339 */
4340 formats |= SNDRV_PCM_FMTBIT_U8;
4341 bps = 8;
4342 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004343 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004344 codec_err(codec,
4345 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4346 nid, val,
4347 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4348 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004349 return -EIO;
4350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (formatsp)
4352 *formatsp = formats;
4353 if (bpsp)
4354 *bpsp = bps;
4355 }
4356
4357 return 0;
4358}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004359EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004362 * snd_hda_is_supported_format - Check the validity of the format
4363 * @codec: HD-audio codec
4364 * @nid: NID to check
4365 * @format: the HD-audio format value to check
4366 *
4367 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 *
4369 * Returns 1 if supported, 0 if not.
4370 */
4371int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4372 unsigned int format)
4373{
4374 int i;
4375 unsigned int val = 0, rate, stream;
4376
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004377 val = query_pcm_param(codec, nid);
4378 if (!val)
4379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
4381 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004382 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004383 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (val & (1 << i))
4385 break;
4386 return 0;
4387 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004388 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return 0;
4390
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004391 stream = query_stream_param(codec, nid);
4392 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394
4395 if (stream & AC_SUPFMT_PCM) {
4396 switch (format & 0xf0) {
4397 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004398 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return 0;
4400 break;
4401 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004402 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return 0;
4404 break;
4405 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004406 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return 0;
4408 break;
4409 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004410 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return 0;
4412 break;
4413 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004414 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 return 0;
4416 break;
4417 default:
4418 return 0;
4419 }
4420 } else {
4421 /* FIXME: check for float32 and AC3? */
4422 }
4423
4424 return 1;
4425}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004426EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428/*
4429 * PCM stuff
4430 */
4431static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4432 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004433 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
4435 return 0;
4436}
4437
4438static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4439 struct hda_codec *codec,
4440 unsigned int stream_tag,
4441 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004442 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
4444 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4445 return 0;
4446}
4447
4448static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4449 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004450 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
Takashi Iwai888afa12008-03-18 09:57:50 +01004452 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return 0;
4454}
4455
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004456static int set_pcm_default_values(struct hda_codec *codec,
4457 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004459 int err;
4460
Takashi Iwai0ba21762007-04-16 11:29:14 +02004461 /* query support PCM information from the given NID */
4462 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004463 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004464 info->rates ? NULL : &info->rates,
4465 info->formats ? NULL : &info->formats,
4466 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004467 if (err < 0)
4468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470 if (info->ops.open == NULL)
4471 info->ops.open = hda_pcm_default_open_close;
4472 if (info->ops.close == NULL)
4473 info->ops.close = hda_pcm_default_open_close;
4474 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004475 if (snd_BUG_ON(!info->nid))
4476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 info->ops.prepare = hda_pcm_default_prepare;
4478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004480 if (snd_BUG_ON(!info->nid))
4481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 info->ops.cleanup = hda_pcm_default_cleanup;
4483 }
4484 return 0;
4485}
4486
Takashi Iwaieb541332010-08-06 13:48:11 +02004487/*
4488 * codec prepare/cleanup entries
4489 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004490/**
4491 * snd_hda_codec_prepare - Prepare a stream
4492 * @codec: the HDA codec
4493 * @hinfo: PCM information
4494 * @stream: stream tag to assign
4495 * @format: format id to assign
4496 * @substream: PCM substream to assign
4497 *
4498 * Calls the prepare callback set by the codec with the given arguments.
4499 * Clean up the inactive streams when successful.
4500 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004501int snd_hda_codec_prepare(struct hda_codec *codec,
4502 struct hda_pcm_stream *hinfo,
4503 unsigned int stream,
4504 unsigned int format,
4505 struct snd_pcm_substream *substream)
4506{
4507 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004508 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004509 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4510 if (ret >= 0)
4511 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004512 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004513 return ret;
4514}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004515EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004516
Takashi Iwai95a962c2014-10-29 16:03:58 +01004517/**
4518 * snd_hda_codec_cleanup - Prepare a stream
4519 * @codec: the HDA codec
4520 * @hinfo: PCM information
4521 * @substream: PCM substream
4522 *
4523 * Calls the cleanup callback set by the codec with the given arguments.
4524 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004525void snd_hda_codec_cleanup(struct hda_codec *codec,
4526 struct hda_pcm_stream *hinfo,
4527 struct snd_pcm_substream *substream)
4528{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004529 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004530 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004531 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004532}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004533EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004534
Takashi Iwaid5191e52009-11-16 14:58:17 +01004535/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004536const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4537 "Audio", "SPDIF", "HDMI", "Modem"
4538};
4539
Takashi Iwai176d5332008-07-30 15:01:44 +02004540/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004541 * get the empty PCM device number to assign
4542 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004543static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004544{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004545 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004546 /* assigned to static slots up to dev#10; if more needed, assign
4547 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4548 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004549 static int audio_idx[HDA_PCM_NTYPES][5] = {
4550 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4551 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004552 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004553 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004554 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004555 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004556
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004557 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004558 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004559 return -EINVAL;
4560 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004561
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004562 for (i = 0; audio_idx[type][i] >= 0; i++) {
4563#ifndef CONFIG_SND_DYNAMIC_MINORS
4564 if (audio_idx[type][i] >= 8)
4565 break;
4566#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004567 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4568 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004569 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004570
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004571#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004572 /* non-fixed slots starting from 10 */
4573 for (i = 10; i < 32; i++) {
4574 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4575 return i;
4576 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004577#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004578
Takashi Iwai4e76a882014-02-25 12:21:03 +01004579 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004580 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004581#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004582 dev_warn(bus->card->dev,
4583 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004584#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004585 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004586}
4587
4588/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004589 * attach a new PCM stream
4590 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004591static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004592{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004593 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004594 struct hda_pcm_stream *info;
4595 int stream, err;
4596
Takashi Iwaib91f0802008-11-04 08:43:08 +01004597 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004598 return -EINVAL;
4599 for (stream = 0; stream < 2; stream++) {
4600 info = &pcm->stream[stream];
4601 if (info->substreams) {
4602 err = set_pcm_default_values(codec, info);
4603 if (err < 0)
4604 return err;
4605 }
4606 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004607 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004608}
4609
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004610/* assign all PCMs of the given codec */
4611int snd_hda_codec_build_pcms(struct hda_codec *codec)
4612{
4613 unsigned int pcm;
4614 int err;
4615
4616 if (!codec->num_pcms) {
4617 if (!codec->patch_ops.build_pcms)
4618 return 0;
4619 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004620 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004621 codec_err(codec,
4622 "cannot build PCMs for #%d (error %d)\n",
4623 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004624 err = snd_hda_codec_reset(codec);
4625 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004626 codec_err(codec,
4627 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004628 return err;
4629 }
4630 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004631 }
4632 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4633 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4634 int dev;
4635
4636 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004637 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004638
4639 if (!cpcm->pcm) {
4640 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4641 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004642 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004643 cpcm->device = dev;
4644 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004645 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004646 codec_err(codec,
4647 "cannot attach PCM stream %d for codec #%d\n",
4648 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004649 continue; /* no fatal error */
4650 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004651 }
4652 }
4653 return 0;
4654}
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656/**
4657 * snd_hda_build_pcms - build PCM information
4658 * @bus: the BUS
4659 *
4660 * Create PCM information for each codec included in the bus.
4661 *
4662 * The build_pcms codec patch is requested to set up codec->num_pcms and
4663 * codec->pcm_info properly. The array is referred by the top-level driver
4664 * to create its PCM instances.
4665 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4666 * callback.
4667 *
4668 * At least, substreams, channels_min and channels_max must be filled for
4669 * each stream. substreams = 0 indicates that the stream doesn't exist.
4670 * When rates and/or formats are zero, the supported values are queried
4671 * from the given nid. The nid is used also by the default ops.prepare
4672 * and ops.cleanup callbacks.
4673 *
4674 * The driver needs to call ops.open in its open callback. Similarly,
4675 * ops.close is supposed to be called in the close callback.
4676 * ops.prepare should be called in the prepare or hw_params callback
4677 * with the proper parameters for set up.
4678 * ops.cleanup should be called in hw_free for clean up of streams.
4679 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004680 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004682int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004684 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Takashi Iwai0ba21762007-04-16 11:29:14 +02004686 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004687 int err = snd_hda_codec_build_pcms(codec);
4688 if (err < 0)
4689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
4691 return 0;
4692}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004693EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 * snd_hda_add_new_ctls - create controls from the array
4697 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004698 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 *
4700 * This helper function creates and add new controls in the given array.
4701 * The array must be terminated with an empty entry as terminator.
4702 *
4703 * Returns 0 if successful, or a negative error code.
4704 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004705int snd_hda_add_new_ctls(struct hda_codec *codec,
4706 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004711 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004712 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004713 if (knew->iface == -1) /* skip this codec private value */
4714 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004715 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004716 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004717 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004718 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004719 if (addr > 0)
4720 kctl->id.device = addr;
4721 if (idx > 0)
4722 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004723 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004724 if (!err)
4725 break;
4726 /* try first with another device index corresponding to
4727 * the codec addr; if it still fails (or it's the
4728 * primary codec), then try another control index
4729 */
4730 if (!addr && codec->addr)
4731 addr = codec->addr;
4732 else if (!idx && !knew->index) {
4733 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004734 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004735 if (idx <= 0)
4736 return err;
4737 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004738 return err;
4739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
4741 return 0;
4742}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004743EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Takashi Iwai83012a72012-08-24 18:38:08 +02004745#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004746/**
4747 * snd_hda_power_up - Power-up the codec
4748 * @codec: HD-audio codec
4749 *
4750 * Increment the usage counter and resume the device if not done yet.
4751 */
4752void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004753{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004754 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004755
Takashi Iwaicc72da72015-02-19 16:00:22 +01004756 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004757 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004758 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004760EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004761
4762/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004763 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004764 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004765 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004766 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004767 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004768void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004769{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004770 struct device *dev = hda_codec_dev(codec);
4771
4772 if (codec_in_pm(codec))
4773 return;
4774 pm_runtime_mark_last_busy(dev);
4775 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004776}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004777EXPORT_SYMBOL_GPL(snd_hda_power_down);
4778
Takashi Iwaibb573922015-02-20 09:26:04 +01004779static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004780{
4781 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004782
Takashi Iwaicc72da72015-02-19 16:00:22 +01004783 if (delay > 0) {
4784 pm_runtime_set_autosuspend_delay(dev, delay);
4785 pm_runtime_use_autosuspend(dev);
4786 pm_runtime_allow(dev);
4787 if (!pm_runtime_suspended(dev))
4788 pm_runtime_mark_last_busy(dev);
4789 } else {
4790 pm_runtime_dont_use_autosuspend(dev);
4791 pm_runtime_forbid(dev);
4792 }
4793}
Takashi Iwaibb573922015-02-20 09:26:04 +01004794
4795/**
4796 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4797 * @bus: HD-audio bus
4798 * @delay: autosuspend delay in msec, 0 = off
4799 *
4800 * Synchronize the runtime PM autosuspend state from the power_save option.
4801 */
4802void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4803{
4804 struct hda_codec *c;
4805
4806 list_for_each_entry(c, &bus->codec_list, list)
4807 codec_set_power_save(c, delay);
4808}
4809EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004810
Takashi Iwaid5191e52009-11-16 14:58:17 +01004811/**
4812 * snd_hda_check_amp_list_power - Check the amp list and update the power
4813 * @codec: HD-audio codec
4814 * @check: the object containing an AMP list and the status
4815 * @nid: NID to check / update
4816 *
4817 * Check whether the given NID is in the amp list. If it's in the list,
4818 * check the current AMP status, and update the the power-status according
4819 * to the mute status.
4820 *
4821 * This function is supposed to be set or called from the check_power_status
4822 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004823 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824int snd_hda_check_amp_list_power(struct hda_codec *codec,
4825 struct hda_loopback_check *check,
4826 hda_nid_t nid)
4827{
Takashi Iwai031024e2011-05-02 11:29:30 +02004828 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004829 int ch, v;
4830
4831 if (!check->amplist)
4832 return 0;
4833 for (p = check->amplist; p->nid; p++) {
4834 if (p->nid == nid)
4835 break;
4836 }
4837 if (!p->nid)
4838 return 0; /* nothing changed */
4839
4840 for (p = check->amplist; p->nid; p++) {
4841 for (ch = 0; ch < 2; ch++) {
4842 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4843 p->idx);
4844 if (!(v & HDA_AMP_MUTE) && v > 0) {
4845 if (!check->power_on) {
4846 check->power_on = 1;
4847 snd_hda_power_up(codec);
4848 }
4849 return 1;
4850 }
4851 }
4852 }
4853 if (check->power_on) {
4854 check->power_on = 0;
4855 snd_hda_power_down(codec);
4856 }
4857 return 0;
4858}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004859EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004862/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004863 * Channel mode helper
4864 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004865
4866/**
4867 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004868 * @codec: the HDA codec
4869 * @uinfo: pointer to get/store the data
4870 * @chmode: channel mode array
4871 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01004872 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004873int snd_hda_ch_mode_info(struct hda_codec *codec,
4874 struct snd_ctl_elem_info *uinfo,
4875 const struct hda_channel_mode *chmode,
4876 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004877{
4878 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4879 uinfo->count = 1;
4880 uinfo->value.enumerated.items = num_chmodes;
4881 if (uinfo->value.enumerated.item >= num_chmodes)
4882 uinfo->value.enumerated.item = num_chmodes - 1;
4883 sprintf(uinfo->value.enumerated.name, "%dch",
4884 chmode[uinfo->value.enumerated.item].channels);
4885 return 0;
4886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004887EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004888
Takashi Iwaid5191e52009-11-16 14:58:17 +01004889/**
4890 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004891 * @codec: the HDA codec
4892 * @ucontrol: pointer to get/store the data
4893 * @chmode: channel mode array
4894 * @num_chmodes: channel mode array size
4895 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004896 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004897int snd_hda_ch_mode_get(struct hda_codec *codec,
4898 struct snd_ctl_elem_value *ucontrol,
4899 const struct hda_channel_mode *chmode,
4900 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004901 int max_channels)
4902{
4903 int i;
4904
4905 for (i = 0; i < num_chmodes; i++) {
4906 if (max_channels == chmode[i].channels) {
4907 ucontrol->value.enumerated.item[0] = i;
4908 break;
4909 }
4910 }
4911 return 0;
4912}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004913EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004914
Takashi Iwaid5191e52009-11-16 14:58:17 +01004915/**
4916 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004917 * @codec: the HDA codec
4918 * @ucontrol: pointer to get/store the data
4919 * @chmode: channel mode array
4920 * @num_chmodes: channel mode array size
4921 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004922 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004923int snd_hda_ch_mode_put(struct hda_codec *codec,
4924 struct snd_ctl_elem_value *ucontrol,
4925 const struct hda_channel_mode *chmode,
4926 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004927 int *max_channelsp)
4928{
4929 unsigned int mode;
4930
4931 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004932 if (mode >= num_chmodes)
4933 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004934 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004935 return 0;
4936 /* change the current channel setting */
4937 *max_channelsp = chmode[mode].channels;
4938 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004939 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004940 return 1;
4941}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004942EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944/*
4945 * input MUX helper
4946 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004947
4948/**
4949 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004950 * @imux: imux helper object
4951 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004952 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004953int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4954 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
4956 unsigned int index;
4957
4958 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4959 uinfo->count = 1;
4960 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004961 if (!imux->num_items)
4962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 index = uinfo->value.enumerated.item;
4964 if (index >= imux->num_items)
4965 index = imux->num_items - 1;
4966 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4967 return 0;
4968}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004969EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Takashi Iwaid5191e52009-11-16 14:58:17 +01004971/**
4972 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004973 * @codec: the HDA codec
4974 * @imux: imux helper object
4975 * @ucontrol: pointer to get/store the data
4976 * @nid: input mux NID
4977 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004978 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004979int snd_hda_input_mux_put(struct hda_codec *codec,
4980 const struct hda_input_mux *imux,
4981 struct snd_ctl_elem_value *ucontrol,
4982 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 unsigned int *cur_val)
4984{
4985 unsigned int idx;
4986
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004987 if (!imux->num_items)
4988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 idx = ucontrol->value.enumerated.item[0];
4990 if (idx >= imux->num_items)
4991 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004992 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004994 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4995 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 *cur_val = idx;
4997 return 1;
4998}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004999EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
5001
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005002/**
5003 * snd_hda_enum_helper_info - Helper for simple enum ctls
5004 * @kcontrol: ctl element
5005 * @uinfo: pointer to get/store the data
5006 * @num_items: number of enum items
5007 * @texts: enum item string array
5008 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005009 * process kcontrol info callback of a simple string enum array
5010 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5011 */
5012int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5013 struct snd_ctl_elem_info *uinfo,
5014 int num_items, const char * const *texts)
5015{
5016 static const char * const texts_default[] = {
5017 "Disabled", "Enabled"
5018 };
5019
5020 if (!texts || !num_items) {
5021 num_items = 2;
5022 texts = texts_default;
5023 }
5024
Takashi Iwai3ff72212014-10-20 18:17:28 +02005025 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005027EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005028
5029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 * Multi-channel / digital-out PCM helper functions
5031 */
5032
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005033/* setup SPDIF output stream */
5034static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5035 unsigned int stream_tag, unsigned int format)
5036{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005037 struct hda_spdif_out *spdif;
5038 unsigned int curr_fmt;
5039 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005040
Laurence Darby3bef1c32012-11-03 17:00:06 +00005041 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5042 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5043 AC_VERB_GET_STREAM_FORMAT, 0);
5044 reset = codec->spdif_status_reset &&
5045 (spdif->ctls & AC_DIG1_ENABLE) &&
5046 curr_fmt != format;
5047
5048 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5049 updated */
5050 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005051 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005052 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005053 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005054 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005055 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005056 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005057 for (d = codec->slave_dig_outs; *d; d++)
5058 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5059 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005060 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005061 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005062 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005063 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005064 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005065}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005066
Takashi Iwai2f728532008-09-25 16:32:41 +02005067static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5068{
5069 snd_hda_codec_cleanup_stream(codec, nid);
5070 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005071 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005072 for (d = codec->slave_dig_outs; *d; d++)
5073 snd_hda_codec_cleanup_stream(codec, *d);
5074 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005075}
5076
Takashi Iwaid5191e52009-11-16 14:58:17 +01005077/**
5078 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5079 * @bus: HD-audio bus
5080 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005081void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5082{
5083 struct hda_codec *codec;
5084
5085 if (!bus)
5086 return;
5087 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005088 if (hda_codec_is_power_on(codec) &&
5089 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005090 codec->patch_ops.reboot_notify(codec);
5091 }
5092}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005093EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005094
Takashi Iwaid5191e52009-11-16 14:58:17 +01005095/**
5096 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005097 * @codec: the HDA codec
5098 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005100int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5101 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102{
Ingo Molnar62932df2006-01-16 16:34:20 +01005103 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005104 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5105 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005106 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005108 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return 0;
5110}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005111EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Takashi Iwaid5191e52009-11-16 14:58:17 +01005113/**
5114 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005115 * @codec: the HDA codec
5116 * @mout: hda_multi_out object
5117 * @stream_tag: stream tag to assign
5118 * @format: format id to assign
5119 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005120 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005121int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5122 struct hda_multi_out *mout,
5123 unsigned int stream_tag,
5124 unsigned int format,
5125 struct snd_pcm_substream *substream)
5126{
5127 mutex_lock(&codec->spdif_mutex);
5128 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5129 mutex_unlock(&codec->spdif_mutex);
5130 return 0;
5131}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005132EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005133
Takashi Iwaid5191e52009-11-16 14:58:17 +01005134/**
5135 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005136 * @codec: the HDA codec
5137 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005138 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005139int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5140 struct hda_multi_out *mout)
5141{
5142 mutex_lock(&codec->spdif_mutex);
5143 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5144 mutex_unlock(&codec->spdif_mutex);
5145 return 0;
5146}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005147EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005148
Takashi Iwaid5191e52009-11-16 14:58:17 +01005149/**
5150 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005151 * @codec: the HDA codec
5152 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005154int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5155 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
Ingo Molnar62932df2006-01-16 16:34:20 +01005157 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005159 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 return 0;
5161}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005162EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164/**
5165 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005166 * @codec: the HDA codec
5167 * @mout: hda_multi_out object
5168 * @substream: PCM substream to assign
5169 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005170 *
5171 * Open analog outputs and set up the hw-constraints.
5172 * If the digital outputs can be opened as slave, open the digital
5173 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005175int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5176 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005177 struct snd_pcm_substream *substream,
5178 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
Takashi Iwai9a081602008-02-12 18:37:26 +01005180 struct snd_pcm_runtime *runtime = substream->runtime;
5181 runtime->hw.channels_max = mout->max_channels;
5182 if (mout->dig_out_nid) {
5183 if (!mout->analog_rates) {
5184 mout->analog_rates = hinfo->rates;
5185 mout->analog_formats = hinfo->formats;
5186 mout->analog_maxbps = hinfo->maxbps;
5187 } else {
5188 runtime->hw.rates = mout->analog_rates;
5189 runtime->hw.formats = mout->analog_formats;
5190 hinfo->maxbps = mout->analog_maxbps;
5191 }
5192 if (!mout->spdif_rates) {
5193 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5194 &mout->spdif_rates,
5195 &mout->spdif_formats,
5196 &mout->spdif_maxbps);
5197 }
5198 mutex_lock(&codec->spdif_mutex);
5199 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005200 if ((runtime->hw.rates & mout->spdif_rates) &&
5201 (runtime->hw.formats & mout->spdif_formats)) {
5202 runtime->hw.rates &= mout->spdif_rates;
5203 runtime->hw.formats &= mout->spdif_formats;
5204 if (mout->spdif_maxbps < hinfo->maxbps)
5205 hinfo->maxbps = mout->spdif_maxbps;
5206 } else {
5207 mout->share_spdif = 0;
5208 /* FIXME: need notify? */
5209 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005210 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005211 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5214 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5215}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005216EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Takashi Iwaid5191e52009-11-16 14:58:17 +01005218/**
5219 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005220 * @codec: the HDA codec
5221 * @mout: hda_multi_out object
5222 * @stream_tag: stream tag to assign
5223 * @format: format id to assign
5224 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005225 *
5226 * Set up the i/o for analog out.
5227 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005229int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5230 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 unsigned int stream_tag,
5232 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005233 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
Takashi Iwaidda14412011-05-02 11:29:30 +02005235 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005237 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 int i;
5239
Ingo Molnar62932df2006-01-16 16:34:20 +01005240 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005241 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005242 if (mout->dig_out_nid && mout->share_spdif &&
5243 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005245 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5246 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005247 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005249 setup_dig_out_stream(codec, mout->dig_out_nid,
5250 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 } else {
5252 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005253 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
5255 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005256 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005259 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5260 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005261 if (!mout->no_share_stream &&
5262 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005264 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5265 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005266 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005267 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5268 if (!mout->no_share_stream && mout->hp_out_nid[i])
5269 snd_hda_codec_setup_stream(codec,
5270 mout->hp_out_nid[i],
5271 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 /* surrounds */
5274 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005275 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005276 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5277 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005278 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005279 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5280 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005282
5283 /* extra surrounds */
5284 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5285 int ch = 0;
5286 if (!mout->extra_out_nid[i])
5287 break;
5288 if (chs >= (i + 1) * 2)
5289 ch = i * 2;
5290 else if (!mout->no_share_stream)
5291 break;
5292 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5293 stream_tag, ch, format);
5294 }
5295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return 0;
5297}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005298EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Takashi Iwaid5191e52009-11-16 14:58:17 +01005300/**
5301 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005302 * @codec: the HDA codec
5303 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005305int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5306 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307{
Takashi Iwaidda14412011-05-02 11:29:30 +02005308 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 int i;
5310
5311 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005312 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005314 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005315 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5316 if (mout->hp_out_nid[i])
5317 snd_hda_codec_cleanup_stream(codec,
5318 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005319 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5320 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005321 snd_hda_codec_cleanup_stream(codec,
5322 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005323 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005325 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 mout->dig_out_used = 0;
5327 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005328 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 return 0;
5330}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005331EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Takashi Iwai47408602012-04-20 13:06:53 +02005333/**
5334 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005335 * @codec: the HDA codec
5336 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005337 *
5338 * Guess the suitable VREF pin bits to be set as the pin-control value.
5339 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5340 */
5341unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5342{
5343 unsigned int pincap;
5344 unsigned int oldval;
5345 oldval = snd_hda_codec_read(codec, pin, 0,
5346 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5347 pincap = snd_hda_query_pin_caps(codec, pin);
5348 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5349 /* Exception: if the default pin setup is vref50, we give it priority */
5350 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5351 return AC_PINCTL_VREF_80;
5352 else if (pincap & AC_PINCAP_VREF_50)
5353 return AC_PINCTL_VREF_50;
5354 else if (pincap & AC_PINCAP_VREF_100)
5355 return AC_PINCTL_VREF_100;
5356 else if (pincap & AC_PINCAP_VREF_GRD)
5357 return AC_PINCTL_VREF_GRD;
5358 return AC_PINCTL_VREF_HIZ;
5359}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005360EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005361
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005362/**
5363 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5364 * @codec: the HDA codec
5365 * @pin: referred pin NID
5366 * @val: pin ctl value to audit
5367 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005368unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5369 hda_nid_t pin, unsigned int val)
5370{
5371 static unsigned int cap_lists[][2] = {
5372 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5373 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5374 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5375 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5376 };
5377 unsigned int cap;
5378
5379 if (!val)
5380 return 0;
5381 cap = snd_hda_query_pin_caps(codec, pin);
5382 if (!cap)
5383 return val; /* don't know what to do... */
5384
5385 if (val & AC_PINCTL_OUT_EN) {
5386 if (!(cap & AC_PINCAP_OUT))
5387 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5388 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5389 val &= ~AC_PINCTL_HP_EN;
5390 }
5391
5392 if (val & AC_PINCTL_IN_EN) {
5393 if (!(cap & AC_PINCAP_IN))
5394 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5395 else {
5396 unsigned int vcap, vref;
5397 int i;
5398 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5399 vref = val & AC_PINCTL_VREFEN;
5400 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5401 if (vref == cap_lists[i][0] &&
5402 !(vcap & cap_lists[i][1])) {
5403 if (i == ARRAY_SIZE(cap_lists) - 1)
5404 vref = AC_PINCTL_VREF_HIZ;
5405 else
5406 vref = cap_lists[i + 1][0];
5407 }
5408 }
5409 val &= ~AC_PINCTL_VREFEN;
5410 val |= vref;
5411 }
5412 }
5413
5414 return val;
5415}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005416EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005417
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005418/**
5419 * _snd_hda_pin_ctl - Helper to set pin ctl value
5420 * @codec: the HDA codec
5421 * @pin: referred pin NID
5422 * @val: pin control value to set
5423 * @cached: access over codec pinctl cache or direct write
5424 *
5425 * This function is a helper to set a pin ctl value more safely.
5426 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5427 * value in pin target array via snd_hda_codec_set_pin_target(), then
5428 * actually writes the value via either snd_hda_codec_update_cache() or
5429 * snd_hda_codec_write() depending on @cached flag.
5430 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005431int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5432 unsigned int val, bool cached)
5433{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005434 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005435 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005436 if (cached)
5437 return snd_hda_codec_update_cache(codec, pin, 0,
5438 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5439 else
5440 return snd_hda_codec_write(codec, pin, 0,
5441 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5442}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005443EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005444
Takashi Iwai990061c2010-09-09 22:08:44 +02005445/**
5446 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005447 * @codec: the HDA codec
5448 * @imux: imux helper object
5449 * @label: the name of imux item to assign
5450 * @index: index number of imux item to assign
5451 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005452 *
5453 * When the same label is used already in the existing items, the number
5454 * suffix is appended to the label. This label index number is stored
5455 * to type_idx when non-NULL pointer is given.
5456 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005457int snd_hda_add_imux_item(struct hda_codec *codec,
5458 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005459 int index, int *type_idx)
5460{
5461 int i, label_idx = 0;
5462 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005463 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005464 return -EINVAL;
5465 }
5466 for (i = 0; i < imux->num_items; i++) {
5467 if (!strncmp(label, imux->items[i].label, strlen(label)))
5468 label_idx++;
5469 }
5470 if (type_idx)
5471 *type_idx = label_idx;
5472 if (label_idx > 0)
5473 snprintf(imux->items[imux->num_items].label,
5474 sizeof(imux->items[imux->num_items].label),
5475 "%s %d", label, label_idx);
5476 else
5477 strlcpy(imux->items[imux->num_items].label, label,
5478 sizeof(imux->items[imux->num_items].label));
5479 imux->items[imux->num_items].index = index;
5480 imux->num_items++;
5481 return 0;
5482}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005483EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005486 * snd_hda_bus_reset - Reset the bus
5487 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005489void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005491 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Takashi Iwai0ba21762007-04-16 11:29:14 +02005493 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005494 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005495 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005496#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005497 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005498 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005499 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005500 }
5501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005504EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005505
5506/*
5507 * generic arrays
5508 */
5509
Takashi Iwaid5191e52009-11-16 14:58:17 +01005510/**
5511 * snd_array_new - get a new element from the given array
5512 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005513 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005514 * Get a new element from the given array. If it exceeds the
5515 * pre-allocated array size, re-allocate the array.
5516 *
5517 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005518 */
5519void *snd_array_new(struct snd_array *array)
5520{
Takashi Iwai12f17712012-10-10 08:59:14 +02005521 if (snd_BUG_ON(!array->elem_size))
5522 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005523 if (array->used >= array->alloced) {
5524 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005525 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005526 void *nlist;
5527 if (snd_BUG_ON(num >= 4096))
5528 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005529 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005530 if (!nlist)
5531 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005532 array->list = nlist;
5533 array->alloced = num;
5534 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005535 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005536}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005537EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005538
Takashi Iwaid5191e52009-11-16 14:58:17 +01005539/**
5540 * snd_array_free - free the given array elements
5541 * @array: the array object
5542 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005543void snd_array_free(struct snd_array *array)
5544{
5545 kfree(array->list);
5546 array->used = 0;
5547 array->alloced = 0;
5548 array->list = NULL;
5549}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005550EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005551
Takashi Iwaid5191e52009-11-16 14:58:17 +01005552/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005553 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5554 * @pcm: PCM caps bits
5555 * @buf: the string buffer to write
5556 * @buflen: the max buffer length
5557 *
5558 * used by hda_proc.c and hda_eld.c
5559 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005560void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5561{
5562 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5563 int i, j;
5564
5565 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5566 if (pcm & (AC_SUPPCM_BITS_8 << i))
5567 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5568
5569 buf[j] = '\0'; /* necessary when j == 0 */
5570}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005571EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005572
5573MODULE_DESCRIPTION("HDA codec core");
5574MODULE_LICENSE("GPL");