blob: 7e38d6f7314b7d1830e2dee1d0aa5862ff08557d [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 Iwaicb53c622007-08-10 17:21:45 +020049#else
Takashi Iwaid846b172012-11-24 11:58:24 +010050#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020051#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020052#endif
53
Takashi Iwaid5191e52009-11-16 14:58:17 +010054/**
55 * snd_hda_get_jack_location - Give a location string of the jack
56 * @cfg: pin default config value
57 *
58 * Parse the pin default config value and returns the string of the
59 * jack location, e.g. "Rear", "Front", etc.
60 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040061const char *snd_hda_get_jack_location(u32 cfg)
62{
63 static char *bases[7] = {
64 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
65 };
66 static unsigned char specials_idx[] = {
67 0x07, 0x08,
68 0x17, 0x18, 0x19,
69 0x37, 0x38
70 };
71 static char *specials[] = {
72 "Rear Panel", "Drive Bar",
73 "Riser", "HDMI", "ATAPI",
74 "Mobile-In", "Mobile-Out"
75 };
76 int i;
77 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
78 if ((cfg & 0x0f) < 7)
79 return bases[cfg & 0x0f];
80 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
81 if (cfg == specials_idx[i])
82 return specials[i];
83 }
84 return "UNKNOWN";
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040087
Takashi Iwaid5191e52009-11-16 14:58:17 +010088/**
89 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
90 * @cfg: pin default config value
91 *
92 * Parse the pin default config value and returns the string of the
93 * jack connectivity, i.e. external or internal connection.
94 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040095const char *snd_hda_get_jack_connectivity(u32 cfg)
96{
97 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
98
99 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
100}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100101EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400102
Takashi Iwaid5191e52009-11-16 14:58:17 +0100103/**
104 * snd_hda_get_jack_type - Give a type string of the jack
105 * @cfg: pin default config value
106 *
107 * Parse the pin default config value and returns the string of the
108 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
109 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400110const char *snd_hda_get_jack_type(u32 cfg)
111{
112 static char *jack_types[16] = {
113 "Line Out", "Speaker", "HP Out", "CD",
114 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
115 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200116 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400117 };
118
119 return jack_types[(cfg & AC_DEFCFG_DEVICE)
120 >> AC_DEFCFG_DEVICE_SHIFT];
121}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100122EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400123
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100124/*
125 * Compose a 32bit command word to be sent to the HD-audio controller
126 */
127static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200128make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100129 unsigned int verb, unsigned int parm)
130{
131 u32 val;
132
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200133 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200134 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100135 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200136 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800137 return ~0;
138 }
139
140 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100141 val |= (u32)nid << 20;
142 val |= verb << 8;
143 val |= parm;
144 return val;
145}
146
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200147/*
148 * Send and receive a verb
149 */
150static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200151 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200152{
153 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200154 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200155
Wu Fengguang6430aee2009-07-17 16:49:19 +0800156 if (cmd == ~0)
157 return -1;
158
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200159 if (res)
160 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200161 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200162 snd_hda_power_up(codec);
163 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200164 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
165 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100166 for (;;) {
167 trace_hda_send_cmd(codec, cmd);
168 err = bus->ops.command(bus, cmd);
169 if (err != -EAGAIN)
170 break;
171 /* process pending verbs */
172 bus->ops.get_response(bus, codec->addr);
173 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200174 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800175 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200176 trace_hda_get_response(codec, *res);
177 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200178 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200179 mutex_unlock(&bus->cmd_mutex);
180 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100181 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200182 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100183 codec_dbg(codec,
184 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200185 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200186 bus->ops.bus_reset(bus);
187 }
188 goto again;
189 }
190 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100191 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200192 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200193 return err;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
197 * snd_hda_codec_read - send a command and get the response
198 * @codec: the HDA codec
199 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200200 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * @verb: the verb to send
202 * @parm: the parameter for the verb
203 *
204 * Send a single command and read the corresponding response.
205 *
206 * Returns the obtained response value, or -1 for an error.
207 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200208unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200209 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int verb, unsigned int parm)
211{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200212 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200213 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200214 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700215 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return res;
217}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100218EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/**
221 * snd_hda_codec_write - send a single command without waiting for response
222 * @codec: the HDA codec
223 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200224 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * @verb: the verb to send
226 * @parm: the parameter for the verb
227 *
228 * Send a single command without waiting for response.
229 *
230 * Returns 0 if successful, or a negative error code.
231 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200232int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
233 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200235 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100236 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200237 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200238 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100240EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/**
243 * snd_hda_sequence_write - sequence writes
244 * @codec: the HDA codec
245 * @seq: VERB array to send
246 *
247 * Send the commands sequentially from the given array.
248 * The array must be terminated with NID=0.
249 */
250void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
251{
252 for (; seq->nid; seq++)
253 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
254}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100255EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/**
258 * snd_hda_get_sub_nodes - get the range of sub nodes
259 * @codec: the HDA codec
260 * @nid: NID to parse
261 * @start_id: the pointer to store the start NID
262 *
263 * Parse the NID and store the start NID of its sub-nodes.
264 * Returns the number of sub-nodes.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
267 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 unsigned int parm;
270
271 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300272 if (parm == -1) {
273 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200274 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *start_id = (parm >> 16) & 0x7fff;
277 return (int)(parm & 0x7fff);
278}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100279EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100281/* connection list element */
282struct hda_conn_list {
283 struct list_head list;
284 int len;
285 hda_nid_t nid;
286 hda_nid_t conns[0];
287};
288
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200289/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290static struct hda_conn_list *
291lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200292{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 struct hda_conn_list *p;
294 list_for_each_entry(p, &codec->conn_list, list) {
295 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200296 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200297 }
298 return NULL;
299}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200300
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100301static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
302 const hda_nid_t *list)
303{
304 struct hda_conn_list *p;
305
306 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
307 if (!p)
308 return -ENOMEM;
309 p->len = len;
310 p->nid = nid;
311 memcpy(p->conns, list, len * sizeof(hda_nid_t));
312 list_add(&p->list, &codec->conn_list);
313 return 0;
314}
315
316static void remove_conn_list(struct hda_codec *codec)
317{
318 while (!list_empty(&codec->conn_list)) {
319 struct hda_conn_list *p;
320 p = list_first_entry(&codec->conn_list, typeof(*p), list);
321 list_del(&p->list);
322 kfree(p);
323 }
324}
325
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200326/* read the connection and add to the cache */
327static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200328{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100329 hda_nid_t list[32];
330 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200331 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200332
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200333 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100334 if (len == -ENOSPC) {
335 len = snd_hda_get_num_raw_conns(codec, nid);
336 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
337 if (!result)
338 return -ENOMEM;
339 len = snd_hda_get_raw_connections(codec, nid, result, len);
340 }
341 if (len >= 0)
342 len = snd_hda_override_conn_list(codec, nid, len, result);
343 if (result != list)
344 kfree(result);
345 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346}
Takashi Iwaidce20792011-06-24 14:10:28 +0200347
348/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 * snd_hda_get_conn_list - get connection list
350 * @codec: the HDA codec
351 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100352 * @listp: the pointer to store NID list
353 *
354 * Parses the connection list of the given widget and stores the pointer
355 * to the list of NIDs.
356 *
357 * Returns the number of connections, or a negative error code.
358 *
359 * Note that the returned pointer isn't protected against the list
360 * modification. If snd_hda_override_conn_list() might be called
361 * concurrently, protect with a mutex appropriately.
362 */
363int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
364 const hda_nid_t **listp)
365{
366 bool added = false;
367
368 for (;;) {
369 int err;
370 const struct hda_conn_list *p;
371
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(codec, nid);
374 if (p) {
375 if (listp)
376 *listp = p->conns;
377 return p->len;
378 }
379 if (snd_BUG_ON(added))
380 return -EINVAL;
381
382 err = read_and_add_raw_conns(codec, nid);
383 if (err < 0)
384 return err;
385 added = true;
386 }
387}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100388EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100389
390/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * @codec: the HDA codec
393 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @max_conns: max. number of connections to store
396 *
397 * Parses the connection list of the given widget and stores the list
398 * of NIDs.
399 *
400 * Returns the number of connections, or a negative error code.
401 */
402int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200403 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 const hda_nid_t *list;
406 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100408 if (len > 0 && conn_list) {
409 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100410 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200411 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200412 return -EINVAL;
413 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100414 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200415 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200416
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100417 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200418}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100419EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200420
Takashi Iwai4eea3092013-02-07 18:18:19 +0100421/* return CONNLIST_LEN parameter of the given widget */
422static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
423{
424 unsigned int wcaps = get_wcaps(codec, nid);
425 unsigned int parm;
426
427 if (!(wcaps & AC_WCAP_CONN_LIST) &&
428 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
429 return 0;
430
431 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
432 if (parm == -1)
433 parm = 0;
434 return parm;
435}
436
437int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
438{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100439 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100440}
441
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200442/**
443 * snd_hda_get_raw_connections - copy connection list without cache
444 * @codec: the HDA codec
445 * @nid: NID to parse
446 * @conn_list: connection list array
447 * @max_conns: max. number of connections to store
448 *
449 * Like snd_hda_get_connections(), copy the connection list but without
450 * checking through the connection-list cache.
451 * Currently called only from hda_proc.c, so not exported.
452 */
453int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
454 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100457 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100460 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Takashi Iwai4eea3092013-02-07 18:18:19 +0100462 parm = get_num_conns(codec, nid);
463 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200464 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (parm & AC_CLIST_LONG) {
467 /* long form */
468 shift = 16;
469 num_elems = 2;
470 } else {
471 /* short form */
472 shift = 8;
473 num_elems = 4;
474 }
475 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 mask = (1 << (shift-1)) - 1;
477
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0; /* no connection */
480
481 if (conn_len == 1) {
482 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200483 parm = snd_hda_codec_read(codec, nid, 0,
484 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200485 if (parm == -1 && codec->bus->rirb_error)
486 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100487 if (conn_list)
488 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 1;
490 }
491
492 /* multi connection */
493 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100494 prev_nid = 0;
495 for (i = 0; i < conn_len; i++) {
496 int range_val;
497 hda_nid_t val, n;
498
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200499 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 parm = snd_hda_codec_read(codec, nid, 0,
501 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200502 if (parm == -1 && codec->bus->rirb_error)
503 return -EIO;
504 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200505 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100507 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100508 codec_dbg(codec,
509 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200510 nid, i, parm);
511 return 0;
512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 parm >>= shift;
514 if (range_val) {
515 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100517 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 "invalid dep_range_val %x:%x\n",
519 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100520 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100522 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100523 if (conn_list) {
524 if (conns >= max_conns)
525 return -ENOSPC;
526 conn_list[conns] = n;
527 }
528 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100529 }
530 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100531 if (conn_list) {
532 if (conns >= max_conns)
533 return -ENOSPC;
534 conn_list[conns] = val;
535 }
536 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100538 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 return conns;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200544 * snd_hda_override_conn_list - add/modify the connection-list to cache
545 * @codec: the HDA codec
546 * @nid: NID to parse
547 * @len: number of connection list entries
548 * @list: the list of connection entries
549 *
550 * Add or modify the given connection-list to the cache. If the corresponding
551 * cache already exists, invalidate it and append a new one.
552 *
553 * Returns zero or a negative error code.
554 */
555int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
556 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100558 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200559
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100560 p = lookup_conn_list(codec, nid);
561 if (p) {
562 list_del(&p->list);
563 kfree(p);
564 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200565
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100566 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100568EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200569
570/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200571 * snd_hda_get_conn_index - get the connection index of the given NID
572 * @codec: the HDA codec
573 * @mux: NID containing the list
574 * @nid: NID to select
575 * @recursive: 1 when searching NID recursively, otherwise 0
576 *
577 * Parses the connection list of the widget @mux and checks whether the
578 * widget @nid is present. If it is, return the connection index.
579 * Otherwise it returns -1.
580 */
581int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
582 hda_nid_t nid, int recursive)
583{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100584 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 int i, nums;
586
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100587 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 for (i = 0; i < nums; i++)
589 if (conn[i] == nid)
590 return i;
591 if (!recursive)
592 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100593 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200595 return -1;
596 }
597 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 for (i = 0; i < nums; i++) {
599 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
600 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
601 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200602 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
603 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200604 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200605 return -1;
606}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100607EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Mengdong Linf1aa0682013-08-26 21:35:21 -0400609
610/* return DEVLIST_LEN parameter of the given widget */
611static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
612{
613 unsigned int wcaps = get_wcaps(codec, nid);
614 unsigned int parm;
615
616 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
617 get_wcaps_type(wcaps) != AC_WID_PIN)
618 return 0;
619
620 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
621 if (parm == -1 && codec->bus->rirb_error)
622 parm = 0;
623 return parm & AC_DEV_LIST_LEN_MASK;
624}
625
626/**
627 * snd_hda_get_devices - copy device list without cache
628 * @codec: the HDA codec
629 * @nid: NID of the pin to parse
630 * @dev_list: device list array
631 * @max_devices: max. number of devices to store
632 *
633 * Copy the device list. This info is dynamic and so not cached.
634 * Currently called only from hda_proc.c, so not exported.
635 */
636int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
637 u8 *dev_list, int max_devices)
638{
639 unsigned int parm;
640 int i, dev_len, devices;
641
642 parm = get_num_devices(codec, nid);
643 if (!parm) /* not multi-stream capable */
644 return 0;
645
646 dev_len = parm + 1;
647 dev_len = dev_len < max_devices ? dev_len : max_devices;
648
649 devices = 0;
650 while (devices < dev_len) {
651 parm = snd_hda_codec_read(codec, nid, 0,
652 AC_VERB_GET_DEVICE_LIST, devices);
653 if (parm == -1 && codec->bus->rirb_error)
654 break;
655
656 for (i = 0; i < 8; i++) {
657 dev_list[devices] = (u8)parm;
658 parm >>= 4;
659 devices++;
660 if (devices >= dev_len)
661 break;
662 }
663 }
664 return devices;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Takashi Iwai2f35c632015-02-27 22:43:26 +0100684 if (!bus)
Wang YanQing2195b062013-05-07 11:27:33 +0800685 return 0;
686
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200687 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100688 unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
690 unsol->wp = wp;
691
692 wp <<= 1;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
695
Takashi Iwai2f35c632015-02-27 22:43:26 +0100696 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100700EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800703 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Howellsc4028952006-11-22 14:57:56 +0000705static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai922c88a2015-02-17 14:46:40 +0100707 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
708 struct hda_bus_unsolicited *unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct hda_codec *codec;
710 unsigned int rp, caddr, res;
711
712 while (unsol->rp != unsol->wp) {
713 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
714 unsol->rp = rp;
715 rp <<= 1;
716 res = unsol->queue[rp];
717 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200718 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
720 codec = bus->caddr_tbl[caddr & 0x0f];
721 if (codec && codec->patch_ops.unsol_event)
722 codec->patch_ops.unsol_event(codec, res);
723 }
724}
725
726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * destructor
728 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100729static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200731 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100732 return;
733
734 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai2f35c632015-02-27 22:43:26 +0100735 cancel_work_sync(&bus->unsol.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (bus->ops.private_free)
737 bus->ops.private_free(bus);
738 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100741static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Takashi Iwai2565c892014-02-19 11:41:09 +0100743 snd_hda_bus_free(device->device_data);
744 return 0;
745}
746
747static int snd_hda_bus_dev_disconnect(struct snd_device *device)
748{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100750 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/**
755 * snd_hda_bus_new - create a HDA bus
756 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * @busp: the pointer to store the created bus instance
758 *
759 * Returns 0 if successful, or a negative error code.
760 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100761int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100762 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct hda_bus *bus;
765 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100766 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100767 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 .dev_free = snd_hda_bus_dev_free,
769 };
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (busp)
772 *busp = NULL;
773
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200774 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100775 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100779 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200780 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100782 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Takashi Iwai0ba21762007-04-16 11:29:14 +0200784 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
785 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 snd_hda_bus_free(bus);
787 return err;
788 }
789 if (busp)
790 *busp = bus;
791 return 0;
792}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100793EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200796 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100798static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200800 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 hda_nid_t nid;
802
803 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
804 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200805 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200806 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200807 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200808 case AC_GRP_AUDIO_FUNCTION:
809 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200810 codec->afg_function_id = function_id & 0xff;
811 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200812 break;
813 case AC_GRP_MODEM_FUNCTION:
814 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200815 codec->mfg_function_id = function_id & 0xff;
816 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200817 break;
818 default:
819 break;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100825 * read widget caps for each widget and store in cache
826 */
827static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
828{
829 int i;
830 hda_nid_t nid;
831
832 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
833 &codec->start_nid);
834 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100836 return -ENOMEM;
837 nid = codec->start_nid;
838 for (i = 0; i < codec->num_nodes; i++, nid++)
839 codec->wcaps[i] = snd_hda_param_read(codec, nid,
840 AC_PAR_AUDIO_WIDGET_CAP);
841 return 0;
842}
843
Takashi Iwai3be14142009-02-20 14:11:16 +0100844/* read all pin default configurations and save codec->init_pins */
845static int read_pin_defaults(struct hda_codec *codec)
846{
847 int i;
848 hda_nid_t nid = codec->start_nid;
849
850 for (i = 0; i < codec->num_nodes; i++, nid++) {
851 struct hda_pincfg *pin;
852 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200853 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100854 if (wid_type != AC_WID_PIN)
855 continue;
856 pin = snd_array_new(&codec->init_pins);
857 if (!pin)
858 return -ENOMEM;
859 pin->nid = nid;
860 pin->cfg = snd_hda_codec_read(codec, nid, 0,
861 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200862 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
863 AC_VERB_GET_PIN_WIDGET_CONTROL,
864 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100865 }
866 return 0;
867}
868
869/* look up the given pin config list and return the item matching with NID */
870static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
871 struct snd_array *array,
872 hda_nid_t nid)
873{
874 int i;
875 for (i = 0; i < array->used; i++) {
876 struct hda_pincfg *pin = snd_array_elem(array, i);
877 if (pin->nid == nid)
878 return pin;
879 }
880 return NULL;
881}
882
Takashi Iwai3be14142009-02-20 14:11:16 +0100883/* set the current pin config value for the given NID.
884 * the value is cached, and read via snd_hda_codec_get_pincfg()
885 */
886int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
887 hda_nid_t nid, unsigned int cfg)
888{
889 struct hda_pincfg *pin;
890
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200891 /* the check below may be invalid when pins are added by a fixup
892 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
893 * for now
894 */
895 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100896 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
897 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200898 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100899
Takashi Iwai3be14142009-02-20 14:11:16 +0100900 pin = look_up_pincfg(codec, list, nid);
901 if (!pin) {
902 pin = snd_array_new(list);
903 if (!pin)
904 return -ENOMEM;
905 pin->nid = nid;
906 }
907 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100908 return 0;
909}
910
Takashi Iwaid5191e52009-11-16 14:58:17 +0100911/**
912 * snd_hda_codec_set_pincfg - Override a pin default configuration
913 * @codec: the HDA codec
914 * @nid: NID to set the pin config
915 * @cfg: the pin default config value
916 *
917 * Override a pin default configuration value in the cache.
918 * This value can be read by snd_hda_codec_get_pincfg() in a higher
919 * priority than the real hardware value.
920 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100921int snd_hda_codec_set_pincfg(struct hda_codec *codec,
922 hda_nid_t nid, unsigned int cfg)
923{
Takashi Iwai346ff702009-02-23 09:42:57 +0100924 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100925}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100926EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100927
Takashi Iwaid5191e52009-11-16 14:58:17 +0100928/**
929 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
930 * @codec: the HDA codec
931 * @nid: NID to get the pin config
932 *
933 * Get the current pin config value of the given pin NID.
934 * If the pincfg value is cached or overridden via sysfs or driver,
935 * returns the cached value.
936 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100937unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
938{
939 struct hda_pincfg *pin;
940
Takashi Iwai648a8d22014-02-25 10:38:13 +0100941#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100942 {
943 unsigned int cfg = 0;
944 mutex_lock(&codec->user_mutex);
945 pin = look_up_pincfg(codec, &codec->user_pins, nid);
946 if (pin)
947 cfg = pin->cfg;
948 mutex_unlock(&codec->user_mutex);
949 if (cfg)
950 return cfg;
951 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100952#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100953 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
954 if (pin)
955 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100956 pin = look_up_pincfg(codec, &codec->init_pins, nid);
957 if (pin)
958 return pin->cfg;
959 return 0;
960}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100961EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100962
Takashi Iwai95a962c2014-10-29 16:03:58 +0100963/**
964 * snd_hda_codec_set_pin_target - remember the current pinctl target value
965 * @codec: the HDA codec
966 * @nid: pin NID
967 * @val: assigned pinctl value
968 *
969 * This function stores the given value to a pinctl target value in the
970 * pincfg table. This isn't always as same as the actually written value
971 * but can be referred at any time via snd_hda_codec_get_pin_target().
972 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100973int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
974 unsigned int val)
975{
976 struct hda_pincfg *pin;
977
978 pin = look_up_pincfg(codec, &codec->init_pins, nid);
979 if (!pin)
980 return -EINVAL;
981 pin->target = val;
982 return 0;
983}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100984EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100985
Takashi Iwai95a962c2014-10-29 16:03:58 +0100986/**
987 * snd_hda_codec_get_pin_target - return the current pinctl target value
988 * @codec: the HDA codec
989 * @nid: pin NID
990 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100991int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
992{
993 struct hda_pincfg *pin;
994
995 pin = look_up_pincfg(codec, &codec->init_pins, nid);
996 if (!pin)
997 return 0;
998 return pin->target;
999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001000EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001001
Takashi Iwai92ee6162009-12-27 11:18:59 +01001002/**
1003 * snd_hda_shutup_pins - Shut up all pins
1004 * @codec: the HDA codec
1005 *
1006 * Clear all pin controls to shup up before suspend for avoiding click noise.
1007 * The controls aren't cached so that they can be resumed properly.
1008 */
1009void snd_hda_shutup_pins(struct hda_codec *codec)
1010{
1011 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001012 /* don't shut up pins when unloading the driver; otherwise it breaks
1013 * the default pin setup at the next load of the driver
1014 */
1015 if (codec->bus->shutdown)
1016 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001017 for (i = 0; i < codec->init_pins.used; i++) {
1018 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1019 /* use read here for syncing after issuing each verb */
1020 snd_hda_codec_read(codec, pin->nid, 0,
1021 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1022 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001023 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001025EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001026
Takashi Iwai2a439522011-07-26 09:52:50 +02001027#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001028/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1029static void restore_shutup_pins(struct hda_codec *codec)
1030{
1031 int i;
1032 if (!codec->pins_shutup)
1033 return;
1034 if (codec->bus->shutdown)
1035 return;
1036 for (i = 0; i < codec->init_pins.used; i++) {
1037 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1038 snd_hda_codec_write(codec, pin->nid, 0,
1039 AC_VERB_SET_PIN_WIDGET_CONTROL,
1040 pin->ctrl);
1041 }
1042 codec->pins_shutup = 0;
1043}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001044#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001045
David Henningsson26a6cb62012-10-09 15:04:21 +02001046static void hda_jackpoll_work(struct work_struct *work)
1047{
1048 struct hda_codec *codec =
1049 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001050
1051 snd_hda_jack_set_dirty_all(codec);
1052 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001053
1054 if (!codec->jackpoll_interval)
1055 return;
1056
Takashi Iwai2f35c632015-02-27 22:43:26 +01001057 schedule_delayed_work(&codec->jackpoll_work,
1058 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +02001059}
1060
Takashi Iwai01751f52007-08-10 16:59:39 +02001061static void init_hda_cache(struct hda_cache_rec *cache,
1062 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001063static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001064
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001065/* release all pincfg lists */
1066static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001067{
Takashi Iwai346ff702009-02-23 09:42:57 +01001068 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001069#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001070 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001071#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 snd_array_free(&codec->init_pins);
1073}
1074
Takashi Iwai54d17402005-11-21 16:33:22 +01001075/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001076 * audio-converter setup caches
1077 */
1078struct hda_cvt_setup {
1079 hda_nid_t nid;
1080 u8 stream_tag;
1081 u8 channel_id;
1082 u16 format_id;
1083 unsigned char active; /* cvt is currently used */
1084 unsigned char dirty; /* setups should be cleared */
1085};
1086
1087/* get or create a cache entry for the given audio converter NID */
1088static struct hda_cvt_setup *
1089get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1090{
1091 struct hda_cvt_setup *p;
1092 int i;
1093
1094 for (i = 0; i < codec->cvt_setups.used; i++) {
1095 p = snd_array_elem(&codec->cvt_setups, i);
1096 if (p->nid == nid)
1097 return p;
1098 }
1099 p = snd_array_new(&codec->cvt_setups);
1100 if (p)
1101 p->nid = nid;
1102 return p;
1103}
1104
1105/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001106 * PCM device
1107 */
1108static void release_pcm(struct kref *kref)
1109{
1110 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
1111
1112 if (pcm->pcm)
1113 snd_device_free(pcm->codec->card, pcm->pcm);
1114 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
1115 kfree(pcm->name);
1116 kfree(pcm);
1117}
1118
1119void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
1120{
1121 kref_put(&pcm->kref, release_pcm);
1122}
1123EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
1124
1125struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
1126 const char *fmt, ...)
1127{
1128 struct hda_pcm *pcm;
1129 va_list args;
1130
1131 va_start(args, fmt);
1132 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1133 if (!pcm)
1134 return NULL;
1135
1136 pcm->codec = codec;
1137 kref_init(&pcm->kref);
1138 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
1139 if (!pcm->name) {
1140 kfree(pcm);
1141 return NULL;
1142 }
1143
1144 list_add_tail(&pcm->list, &codec->pcm_list_head);
1145 return pcm;
1146}
1147EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
1148
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001149/*
1150 * codec destructor
1151 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001152static void codec_release_pcms(struct hda_codec *codec)
1153{
1154 struct hda_pcm *pcm, *n;
1155
1156 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
1157 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001158 if (pcm->pcm)
1159 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001160 snd_hda_codec_pcm_put(pcm);
1161 }
1162}
1163
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001164void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
1165{
1166 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +01001167 if (!codec->in_freeing)
1168 snd_hda_ctls_clear(codec);
1169 codec_release_pcms(codec);
1170 snd_hda_detach_beep_device(codec);
1171 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1172 snd_hda_jack_tbl_clear(codec);
1173 codec->proc_widget_hook = NULL;
1174 codec->spec = NULL;
1175
1176 free_hda_cache(&codec->amp_cache);
1177 free_hda_cache(&codec->cmd_cache);
1178 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1179 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1180
1181 /* free only driver_pins so that init_pins + user_pins are restored */
1182 snd_array_free(&codec->driver_pins);
1183 snd_array_free(&codec->cvt_setups);
1184 snd_array_free(&codec->spdif_out);
1185 snd_array_free(&codec->verbs);
1186 codec->preset = NULL;
1187 codec->slave_dig_outs = NULL;
1188 codec->spdif_status_reset = 0;
1189 snd_array_free(&codec->mixers);
1190 snd_array_free(&codec->nids);
1191 remove_conn_list(codec);
1192}
1193
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001194static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1195 hda_nid_t fg, unsigned int power_state);
1196
Takashi Iwaid8193872012-08-31 07:54:38 -07001197static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001198 unsigned int power_state);
1199
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001200static int snd_hda_codec_dev_register(struct snd_device *device)
1201{
1202 struct hda_codec *codec = device->device_data;
1203
Takashi Iwaid604b392014-02-28 13:42:09 +01001204 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001205 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001206 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwai709949f2015-02-20 09:58:14 +01001207 /* it was powered up in snd_hda_codec_new(), now all done */
1208 snd_hda_power_down(codec);
Takashi Iwaid604b392014-02-28 13:42:09 +01001209 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001210}
1211
1212static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1213{
1214 struct hda_codec *codec = device->device_data;
1215
Takashi Iwaid604b392014-02-28 13:42:09 +01001216 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001217 return 0;
1218}
1219
Takashi Iwai2565c892014-02-19 11:41:09 +01001220static int snd_hda_codec_dev_free(struct snd_device *device)
1221{
Takashi Iwaid56db742015-02-27 23:25:35 +01001222 struct hda_codec *codec = device->device_data;
1223
1224 codec->in_freeing = 1;
1225 if (device_is_registered(hda_codec_dev(codec)))
1226 device_del(hda_codec_dev(codec));
1227 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +01001228 return 0;
1229}
1230
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001231static void snd_hda_codec_dev_release(struct device *dev)
1232{
Takashi Iwaid56db742015-02-27 23:25:35 +01001233 struct hda_codec *codec = dev_to_hda_codec(dev);
1234
1235 free_init_pincfgs(codec);
1236 list_del(&codec->list);
1237 codec->bus->caddr_tbl[codec->addr] = NULL;
1238 clear_bit(codec->addr, &codec->bus->codec_powered);
1239 snd_hda_sysfs_clear(codec);
1240 free_hda_cache(&codec->amp_cache);
1241 free_hda_cache(&codec->cmd_cache);
1242 kfree(codec->vendor_name);
1243 kfree(codec->chip_name);
1244 kfree(codec->modelname);
1245 kfree(codec->wcaps);
1246 codec->bus->num_codecs--;
1247 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/**
1251 * snd_hda_codec_new - create a HDA codec
1252 * @bus: the bus to assign
1253 * @codec_addr: the codec address
1254 * @codecp: the pointer to store the generated codec
1255 *
1256 * Returns 0 if successful, or a negative error code.
1257 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001258int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1259 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001262 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001263 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001264 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001266 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001267 .dev_register = snd_hda_codec_dev_register,
1268 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001269 .dev_free = snd_hda_codec_dev_free,
1270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Takashi Iwaida3cec32008-08-08 17:12:14 +02001272 if (snd_BUG_ON(!bus))
1273 return -EINVAL;
1274 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001278 dev_err(card->dev,
Takashi Iwai4e76a882014-02-25 12:21:03 +01001279 "address 0x%x is already occupied\n",
1280 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EBUSY;
1282 }
1283
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001284 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001285 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001288 dev = hda_codec_dev(codec);
1289 device_initialize(dev);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001290 dev->parent = card->dev;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001291 dev->bus = &snd_hda_bus_type;
1292 dev->release = snd_hda_codec_dev_release;
1293 dev->groups = snd_hda_dev_attr_groups;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001294 dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001295 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001296 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001299 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001301 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001302 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001303 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001304 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001305 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001306 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1307 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001309 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001310 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001311 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001312 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001313 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001314 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001315 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001316
David Henningsson26a6cb62012-10-09 15:04:21 +02001317 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001318 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001319 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Takashi Iwai83012a72012-08-24 18:38:08 +02001321#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001322 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
Takashi Iwai709949f2015-02-20 09:58:14 +01001323 * it's powered down later in snd_hda_codec_dev_register().
Takashi Iwaicb53c622007-08-10 17:21:45 +02001324 */
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001325 set_bit(codec->addr, &bus->codec_powered);
Takashi Iwaicc72da72015-02-19 16:00:22 +01001326 pm_runtime_set_active(hda_codec_dev(codec));
1327 pm_runtime_get_noresume(hda_codec_dev(codec));
1328 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001329#endif
1330
Takashi Iwai648a8d22014-02-25 10:38:13 +01001331 snd_hda_sysfs_init(codec);
1332
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001333 if (codec->bus->modelname) {
1334 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1335 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001336 err = -ENODEV;
1337 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001338 }
1339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001342 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 bus->caddr_tbl[codec_addr] = codec;
1345
Takashi Iwai0ba21762007-04-16 11:29:14 +02001346 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1347 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001348 if (codec->vendor_id == -1)
1349 /* read again, hopefully the access method was corrected
1350 * in the last read...
1351 */
1352 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1353 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001354 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1355 AC_PAR_SUBSYSTEM_ID);
1356 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1357 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001359 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001360 if (!codec->afg && !codec->mfg) {
Takashi Iwaid56db742015-02-27 23:25:35 +01001361 codec_err(codec, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001362 err = -ENODEV;
1363 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Takashi Iwaid8193872012-08-31 07:54:38 -07001366 fg = codec->afg ? codec->afg : codec->mfg;
1367 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001368 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001369 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001370 err = read_pin_defaults(codec);
1371 if (err < 0)
1372 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001373
Takashi Iwai0ba21762007-04-16 11:29:14 +02001374 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001375 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001376 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001377 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001378 }
1379
Takashi Iwai83012a72012-08-24 18:38:08 +02001380#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001381 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001382 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001383#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001384 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001385 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001386
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001387 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001388 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001389
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 snd_hda_codec_proc_new(codec);
1391
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001392 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001393
1394 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1395 codec->subsystem_id, codec->revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001396 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001397
Takashi Iwai6efdd852015-02-27 16:09:22 +01001398 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001399 if (err < 0)
1400 goto error;
1401
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001402 if (codecp)
1403 *codecp = codec;
1404 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001405
1406 error:
Takashi Iwaid56db742015-02-27 23:25:35 +01001407 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +01001408 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001410EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001411
Takashi Iwai95a962c2014-10-29 16:03:58 +01001412/**
1413 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1414 * @codec: the HDA codec
1415 *
1416 * Forcibly refresh the all widget caps and the init pin configurations of
1417 * the given codec.
1418 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001419int snd_hda_codec_update_widgets(struct hda_codec *codec)
1420{
1421 hda_nid_t fg;
1422 int err;
1423
1424 /* Assume the function group node does not change,
1425 * only the widget nodes may change.
1426 */
1427 kfree(codec->wcaps);
1428 fg = codec->afg ? codec->afg : codec->mfg;
1429 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001430 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001431 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001432
1433 snd_array_free(&codec->init_pins);
1434 err = read_pin_defaults(codec);
1435
1436 return err;
1437}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001438EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001439
Takashi Iwaied360812012-08-08 17:12:52 +02001440/* update the stream-id if changed */
1441static void update_pcm_stream_id(struct hda_codec *codec,
1442 struct hda_cvt_setup *p, hda_nid_t nid,
1443 u32 stream_tag, int channel_id)
1444{
1445 unsigned int oldval, newval;
1446
1447 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1448 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1449 newval = (stream_tag << 4) | channel_id;
1450 if (oldval != newval)
1451 snd_hda_codec_write(codec, nid, 0,
1452 AC_VERB_SET_CHANNEL_STREAMID,
1453 newval);
1454 p->stream_tag = stream_tag;
1455 p->channel_id = channel_id;
1456 }
1457}
1458
1459/* update the format-id if changed */
1460static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1461 hda_nid_t nid, int format)
1462{
1463 unsigned int oldval;
1464
1465 if (p->format_id != format) {
1466 oldval = snd_hda_codec_read(codec, nid, 0,
1467 AC_VERB_GET_STREAM_FORMAT, 0);
1468 if (oldval != format) {
1469 msleep(1);
1470 snd_hda_codec_write(codec, nid, 0,
1471 AC_VERB_SET_STREAM_FORMAT,
1472 format);
1473 }
1474 p->format_id = format;
1475 }
1476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/**
1479 * snd_hda_codec_setup_stream - set up the codec for streaming
1480 * @codec: the CODEC to set up
1481 * @nid: the NID to set up
1482 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1483 * @channel_id: channel id to pass, zero based.
1484 * @format: stream format.
1485 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001486void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1487 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 int channel_id, int format)
1489{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001490 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001491 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001492 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001493 int i;
1494
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001496 return;
1497
Takashi Iwai4e76a882014-02-25 12:21:03 +01001498 codec_dbg(codec,
1499 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1500 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001501 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001502 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001503 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001504
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001505 if (codec->patch_ops.stream_pm)
1506 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001507 if (codec->pcm_format_first)
1508 update_pcm_format(codec, p, nid, format);
1509 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1510 if (!codec->pcm_format_first)
1511 update_pcm_format(codec, p, nid, format);
1512
Takashi Iwaieb541332010-08-06 13:48:11 +02001513 p->active = 1;
1514 p->dirty = 0;
1515
1516 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001517 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001518 list_for_each_entry(c, &codec->bus->codec_list, list) {
1519 for (i = 0; i < c->cvt_setups.used; i++) {
1520 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001521 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001522 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001523 p->dirty = 1;
1524 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001527EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Takashi Iwaif0cea792010-08-13 11:56:53 +02001529static void really_cleanup_stream(struct hda_codec *codec,
1530 struct hda_cvt_setup *q);
1531
Takashi Iwaid5191e52009-11-16 14:58:17 +01001532/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001533 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001534 * @codec: the CODEC to clean up
1535 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001536 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001537 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001538void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1539 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001540{
Takashi Iwaieb541332010-08-06 13:48:11 +02001541 struct hda_cvt_setup *p;
1542
Takashi Iwai888afa12008-03-18 09:57:50 +01001543 if (!nid)
1544 return;
1545
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001546 if (codec->no_sticky_stream)
1547 do_now = 1;
1548
Takashi Iwai4e76a882014-02-25 12:21:03 +01001549 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001550 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001551 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001552 /* here we just clear the active flag when do_now isn't set;
1553 * actual clean-ups will be done later in
1554 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1555 */
1556 if (do_now)
1557 really_cleanup_stream(codec, p);
1558 else
1559 p->active = 0;
1560 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001561}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001562EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001563
Takashi Iwaieb541332010-08-06 13:48:11 +02001564static void really_cleanup_stream(struct hda_codec *codec,
1565 struct hda_cvt_setup *q)
1566{
1567 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001568 if (q->stream_tag || q->channel_id)
1569 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1570 if (q->format_id)
1571 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1572);
Takashi Iwaieb541332010-08-06 13:48:11 +02001573 memset(q, 0, sizeof(*q));
1574 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001575 if (codec->patch_ops.stream_pm)
1576 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001577}
1578
1579/* clean up the all conflicting obsolete streams */
1580static void purify_inactive_streams(struct hda_codec *codec)
1581{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001582 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001583 int i;
1584
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001585 list_for_each_entry(c, &codec->bus->codec_list, list) {
1586 for (i = 0; i < c->cvt_setups.used; i++) {
1587 struct hda_cvt_setup *p;
1588 p = snd_array_elem(&c->cvt_setups, i);
1589 if (p->dirty)
1590 really_cleanup_stream(c, p);
1591 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001592 }
1593}
1594
Takashi Iwai2a439522011-07-26 09:52:50 +02001595#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001596/* clean up all streams; called from suspend */
1597static void hda_cleanup_all_streams(struct hda_codec *codec)
1598{
1599 int i;
1600
1601 for (i = 0; i < codec->cvt_setups.used; i++) {
1602 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1603 if (p->stream_tag)
1604 really_cleanup_stream(codec, p);
1605 }
1606}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001607#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609/*
1610 * amp access functions
1611 */
1612
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001613/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001614#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001615#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001616#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1617#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001619#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001622static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001623 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Takashi Iwai01751f52007-08-10 16:59:39 +02001625 memset(cache, 0, sizeof(*cache));
1626 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001627 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001628}
1629
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001630static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001631{
Takashi Iwai603c4012008-07-30 15:01:44 +02001632 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001636static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Takashi Iwai01751f52007-08-10 16:59:39 +02001638 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1639 u16 cur = cache->hash[idx];
1640 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001643 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (info->key == key)
1645 return info;
1646 cur = info->next;
1647 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001648 return NULL;
1649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001651/* query the hash. allocate an entry if not found. */
1652static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1653 u32 key)
1654{
1655 struct hda_cache_head *info = get_hash(cache, key);
1656 if (!info) {
1657 u16 idx, cur;
1658 /* add a new hash entry */
1659 info = snd_array_new(&cache->buf);
1660 if (!info)
1661 return NULL;
1662 cur = snd_array_index(&cache->buf, info);
1663 info->key = key;
1664 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001665 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001666 idx = key % (u16)ARRAY_SIZE(cache->hash);
1667 info->next = cache->hash[idx];
1668 cache->hash[idx] = cur;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return info;
1671}
1672
Takashi Iwai01751f52007-08-10 16:59:39 +02001673/* query and allocate an amp hash entry */
1674static inline struct hda_amp_info *
1675get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1676{
1677 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1678}
1679
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001680/* overwrite the value with the key in the caps hash */
1681static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1682{
1683 struct hda_amp_info *info;
1684
1685 mutex_lock(&codec->hash_mutex);
1686 info = get_alloc_amp_hash(codec, key);
1687 if (!info) {
1688 mutex_unlock(&codec->hash_mutex);
1689 return -EINVAL;
1690 }
1691 info->amp_caps = val;
1692 info->head.val |= INFO_AMP_CAPS;
1693 mutex_unlock(&codec->hash_mutex);
1694 return 0;
1695}
1696
1697/* query the value from the caps hash; if not found, fetch the current
1698 * value from the given function and store in the hash
1699 */
1700static unsigned int
1701query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1702 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1703{
1704 struct hda_amp_info *info;
1705 unsigned int val;
1706
1707 mutex_lock(&codec->hash_mutex);
1708 info = get_alloc_amp_hash(codec, key);
1709 if (!info) {
1710 mutex_unlock(&codec->hash_mutex);
1711 return 0;
1712 }
1713 if (!(info->head.val & INFO_AMP_CAPS)) {
1714 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1715 val = func(codec, nid, dir);
1716 write_caps_hash(codec, key, val);
1717 } else {
1718 val = info->amp_caps;
1719 mutex_unlock(&codec->hash_mutex);
1720 }
1721 return val;
1722}
1723
1724static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1725 int direction)
1726{
1727 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1728 nid = codec->afg;
1729 return snd_hda_param_read(codec, nid,
1730 direction == HDA_OUTPUT ?
1731 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1732}
1733
Takashi Iwaid5191e52009-11-16 14:58:17 +01001734/**
1735 * query_amp_caps - query AMP capabilities
1736 * @codec: the HD-auio codec
1737 * @nid: the NID to query
1738 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1739 *
1740 * Query AMP capabilities for the given widget and direction.
1741 * Returns the obtained capability bits.
1742 *
1743 * When cap bits have been already read, this doesn't read again but
1744 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001746u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001748 return query_caps_hash(codec, nid, direction,
1749 HDA_HASH_KEY(nid, direction, 0),
1750 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001752EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Takashi Iwaid5191e52009-11-16 14:58:17 +01001754/**
David Henningsson861a04e2014-09-23 10:38:17 +02001755 * snd_hda_check_amp_caps - query AMP capabilities
1756 * @codec: the HD-audio codec
1757 * @nid: the NID to query
1758 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001759 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001760 *
1761 * Check whether the widget has the given amp capability for the direction.
1762 */
1763bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1764 int dir, unsigned int bits)
1765{
1766 if (!nid)
1767 return false;
1768 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1769 if (query_amp_caps(codec, nid, dir) & bits)
1770 return true;
1771 return false;
1772}
1773EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1774
1775/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001776 * snd_hda_override_amp_caps - Override the AMP capabilities
1777 * @codec: the CODEC to clean up
1778 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001779 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001780 * @caps: the capability bits to set
1781 *
1782 * Override the cached AMP caps bits value by the given one.
1783 * This function is useful if the driver needs to adjust the AMP ranges,
1784 * e.g. limit to 0dB, etc.
1785 *
1786 * Returns zero if successful or a negative error code.
1787 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001788int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1789 unsigned int caps)
1790{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001791 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001792}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001793EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001794
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1796 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001797{
1798 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1799}
1800
Takashi Iwaid5191e52009-11-16 14:58:17 +01001801/**
1802 * snd_hda_query_pin_caps - Query PIN capabilities
1803 * @codec: the HD-auio codec
1804 * @nid: the NID to query
1805 *
1806 * Query PIN capabilities for the given widget.
1807 * Returns the obtained capability bits.
1808 *
1809 * When cap bits have been already read, this doesn't read again but
1810 * returns the cached value.
1811 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001812u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1813{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001814 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001815 read_pin_cap);
1816}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001817EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001818
Wu Fengguang864f92b2009-11-18 12:38:02 +08001819/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001820 * snd_hda_override_pin_caps - Override the pin capabilities
1821 * @codec: the CODEC
1822 * @nid: the NID to override
1823 * @caps: the capability bits to set
1824 *
1825 * Override the cached PIN capabilitiy bits value by the given one.
1826 *
1827 * Returns zero if successful or a negative error code.
1828 */
1829int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1830 unsigned int caps)
1831{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001832 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001833}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001834EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001835
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001836/* read or sync the hash value with the current value;
1837 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839static struct hda_amp_info *
1840update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001841 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001843 struct hda_amp_info *info;
1844 unsigned int parm, val = 0;
1845 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001847 retry:
1848 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1849 if (!info)
1850 return NULL;
1851 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1852 if (!val_read) {
1853 mutex_unlock(&codec->hash_mutex);
1854 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1855 parm |= direction == HDA_OUTPUT ?
1856 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1857 parm |= index;
1858 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001859 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001860 val &= 0xff;
1861 val_read = true;
1862 mutex_lock(&codec->hash_mutex);
1863 goto retry;
1864 }
1865 info->vol[ch] = val;
1866 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001867 } else if (init_only)
1868 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001869 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001873 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001875static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001876 hda_nid_t nid, int ch, int direction, int index,
1877 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 u32 parm;
1880
1881 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1882 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1883 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001884 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1885 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001886 ; /* set the zero value as a fake mute */
1887 else
1888 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1890}
1891
Takashi Iwaid5191e52009-11-16 14:58:17 +01001892/**
1893 * snd_hda_codec_amp_read - Read AMP value
1894 * @codec: HD-audio codec
1895 * @nid: NID to read the AMP value
1896 * @ch: channel (left=0 or right=1)
1897 * @direction: #HDA_INPUT or #HDA_OUTPUT
1898 * @index: the index value (only for input direction)
1899 *
1900 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Takashi Iwai834be882006-03-01 14:16:17 +01001902int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1903 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001905 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001906 unsigned int val = 0;
1907
1908 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001909 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001910 if (info)
1911 val = info->vol[ch];
1912 mutex_unlock(&codec->hash_mutex);
1913 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001915EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Takashi Iwai280e57d2012-12-14 10:32:21 +01001917static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1918 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001919 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001920{
1921 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001922 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001923
1924 if (snd_BUG_ON(mask & ~0xff))
1925 mask &= 0xff;
1926 val &= mask;
1927
1928 mutex_lock(&codec->hash_mutex);
1929 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1930 if (!info) {
1931 mutex_unlock(&codec->hash_mutex);
1932 return 0;
1933 }
1934 val |= info->vol[ch] & ~mask;
1935 if (info->vol[ch] == val) {
1936 mutex_unlock(&codec->hash_mutex);
1937 return 0;
1938 }
1939 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001940 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001941 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001942 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001943 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001944 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001945 return 1;
1946}
1947
Takashi Iwaid5191e52009-11-16 14:58:17 +01001948/**
1949 * snd_hda_codec_amp_update - update the AMP value
1950 * @codec: HD-audio codec
1951 * @nid: NID to read the AMP value
1952 * @ch: channel (left=0 or right=1)
1953 * @direction: #HDA_INPUT or #HDA_OUTPUT
1954 * @idx: the index value (only for input direction)
1955 * @mask: bit mask to set
1956 * @val: the bits value to set
1957 *
1958 * Update the AMP value with a bit mask.
1959 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001960 */
Takashi Iwai834be882006-03-01 14:16:17 +01001961int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1962 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001964 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1965 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001967EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Takashi Iwaid5191e52009-11-16 14:58:17 +01001969/**
1970 * snd_hda_codec_amp_stereo - update the AMP stereo values
1971 * @codec: HD-audio codec
1972 * @nid: NID to read the AMP value
1973 * @direction: #HDA_INPUT or #HDA_OUTPUT
1974 * @idx: the index value (only for input direction)
1975 * @mask: bit mask to set
1976 * @val: the bits value to set
1977 *
1978 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1979 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001980 */
1981int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1982 int direction, int idx, int mask, int val)
1983{
1984 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001985
1986 if (snd_BUG_ON(mask & ~0xff))
1987 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001988 for (ch = 0; ch < 2; ch++)
1989 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1990 idx, mask, val);
1991 return ret;
1992}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001993EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001994
Takashi Iwai95a962c2014-10-29 16:03:58 +01001995/**
1996 * snd_hda_codec_amp_init - initialize the AMP value
1997 * @codec: the HDA codec
1998 * @nid: NID to read the AMP value
1999 * @ch: channel (left=0 or right=1)
2000 * @dir: #HDA_INPUT or #HDA_OUTPUT
2001 * @idx: the index value (only for input direction)
2002 * @mask: bit mask to set
2003 * @val: the bits value to set
2004 *
2005 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002006 * the first access. If the amp was already initialized / updated beforehand,
2007 * this does nothing.
2008 */
2009int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2010 int dir, int idx, int mask, int val)
2011{
Takashi Iwai781c7b92015-02-20 10:26:25 +01002012 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
2013 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002014}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002015EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002016
Takashi Iwai95a962c2014-10-29 16:03:58 +01002017/**
2018 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2019 * @codec: the HDA codec
2020 * @nid: NID to read the AMP value
2021 * @dir: #HDA_INPUT or #HDA_OUTPUT
2022 * @idx: the index value (only for input direction)
2023 * @mask: bit mask to set
2024 * @val: the bits value to set
2025 *
2026 * Call snd_hda_codec_amp_init() for both stereo channels.
2027 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002028int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2029 int dir, int idx, int mask, int val)
2030{
2031 int ch, ret = 0;
2032
2033 if (snd_BUG_ON(mask & ~0xff))
2034 mask &= 0xff;
2035 for (ch = 0; ch < 2; ch++)
2036 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2037 idx, mask, val);
2038 return ret;
2039}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002040EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002041
Takashi Iwaid5191e52009-11-16 14:58:17 +01002042/**
2043 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2044 * @codec: HD-audio codec
2045 *
2046 * Resume the all amp commands from the cache.
2047 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002048void snd_hda_codec_resume_amp(struct hda_codec *codec)
2049{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002050 int i;
2051
Takashi Iwaic370dd62012-12-13 18:30:04 +01002052 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002053 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002054 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2055 struct hda_amp_info *buffer;
2056 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002057 hda_nid_t nid;
2058 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002059 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002060
2061 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002062 if (!buffer->head.dirty)
2063 continue;
2064 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002065 info = *buffer;
2066 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002067 if (!key)
2068 continue;
2069 nid = key & 0xff;
2070 idx = (key >> 16) & 0xff;
2071 dir = (key >> 24) & 0xff;
2072 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002073 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002074 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002075 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002076 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2077 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002078 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002079 }
2080 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002081 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002083EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002085static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2086 unsigned int ofs)
2087{
2088 u32 caps = query_amp_caps(codec, nid, dir);
2089 /* get num steps */
2090 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2091 if (ofs < caps)
2092 caps -= ofs;
2093 return caps;
2094}
2095
Takashi Iwaid5191e52009-11-16 14:58:17 +01002096/**
2097 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002098 * @kcontrol: referred ctl element
2099 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002100 *
2101 * The control element is supposed to have the private_value field
2102 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2103 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002104int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2105 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2108 u16 nid = get_amp_nid(kcontrol);
2109 u8 chs = get_amp_channels(kcontrol);
2110 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002111 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002113 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2114 uinfo->count = chs == 3 ? 2 : 1;
2115 uinfo->value.integer.min = 0;
2116 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2117 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002118 codec_warn(codec,
2119 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2120 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -EINVAL;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002125EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002127
2128static inline unsigned int
2129read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2130 int ch, int dir, int idx, unsigned int ofs)
2131{
2132 unsigned int val;
2133 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2134 val &= HDA_AMP_VOLMASK;
2135 if (val >= ofs)
2136 val -= ofs;
2137 else
2138 val = 0;
2139 return val;
2140}
2141
2142static inline int
2143update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2144 int ch, int dir, int idx, unsigned int ofs,
2145 unsigned int val)
2146{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002147 unsigned int maxval;
2148
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002149 if (val > 0)
2150 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002151 /* ofs = 0: raw max value */
2152 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002153 if (val > maxval)
2154 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002155 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2156 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002157}
2158
Takashi Iwaid5191e52009-11-16 14:58:17 +01002159/**
2160 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002161 * @kcontrol: ctl element
2162 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163 *
2164 * The control element is supposed to have the private_value field
2165 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2166 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002167int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2171 hda_nid_t nid = get_amp_nid(kcontrol);
2172 int chs = get_amp_channels(kcontrol);
2173 int dir = get_amp_direction(kcontrol);
2174 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002175 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 long *valp = ucontrol->value.integer.value;
2177
2178 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002179 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002181 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
2183}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002184EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Takashi Iwaid5191e52009-11-16 14:58:17 +01002186/**
2187 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002188 * @kcontrol: ctl element
2189 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002190 *
2191 * The control element is supposed to have the private_value field
2192 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2193 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 hda_nid_t nid = get_amp_nid(kcontrol);
2199 int chs = get_amp_channels(kcontrol);
2200 int dir = get_amp_direction(kcontrol);
2201 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002202 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 long *valp = ucontrol->value.integer.value;
2204 int change = 0;
2205
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002206 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002207 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002208 valp++;
2209 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002210 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002211 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return change;
2213}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002214EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Takashi Iwaid5191e52009-11-16 14:58:17 +01002216/**
2217 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002218 * @kcontrol: ctl element
2219 * @op_flag: operation flag
2220 * @size: byte size of input TLV
2221 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002222 *
2223 * The control element is supposed to have the private_value field
2224 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2225 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002226int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2227 unsigned int size, unsigned int __user *_tlv)
2228{
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 hda_nid_t nid = get_amp_nid(kcontrol);
2231 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002232 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002233 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002234 u32 caps, val1, val2;
2235
2236 if (size < 4 * sizeof(unsigned int))
2237 return -ENOMEM;
2238 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002239 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2240 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002241 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002242 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002243 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002244 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002245 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002246 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2247 return -EFAULT;
2248 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2249 return -EFAULT;
2250 if (put_user(val1, _tlv + 2))
2251 return -EFAULT;
2252 if (put_user(val2, _tlv + 3))
2253 return -EFAULT;
2254 return 0;
2255}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002256EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002257
Takashi Iwaid5191e52009-11-16 14:58:17 +01002258/**
2259 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2260 * @codec: HD-audio codec
2261 * @nid: NID of a reference widget
2262 * @dir: #HDA_INPUT or #HDA_OUTPUT
2263 * @tlv: TLV data to be stored, at least 4 elements
2264 *
2265 * Set (static) TLV data for a virtual master volume using the AMP caps
2266 * obtained from the reference NID.
2267 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002268 */
2269void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2270 unsigned int *tlv)
2271{
2272 u32 caps;
2273 int nums, step;
2274
2275 caps = query_amp_caps(codec, nid, dir);
2276 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2277 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2278 step = (step + 1) * 25;
2279 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2280 tlv[1] = 2 * sizeof(unsigned int);
2281 tlv[2] = -nums * step;
2282 tlv[3] = step;
2283}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002284EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002285
2286/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002287static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002288find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002289{
2290 struct snd_ctl_elem_id id;
2291 memset(&id, 0, sizeof(id));
2292 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002293 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002294 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002295 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2296 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002297 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002298 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002299}
2300
Takashi Iwaid5191e52009-11-16 14:58:17 +01002301/**
2302 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2303 * @codec: HD-audio codec
2304 * @name: ctl id name string
2305 *
2306 * Get the control element with the given id string and IFACE_MIXER.
2307 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002308struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2309 const char *name)
2310{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002311 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002312}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002313EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002314
Takashi Iwaidcda5802012-10-12 17:24:51 +02002315static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002316 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002317{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002318 int i, idx;
2319 /* 16 ctlrs should be large enough */
2320 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2321 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002322 return idx;
2323 }
2324 return -EBUSY;
2325}
2326
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002328 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002329 * @codec: HD-audio codec
2330 * @nid: corresponding NID (optional)
2331 * @kctl: the control element to assign
2332 *
2333 * Add the given control element to an array inside the codec instance.
2334 * All control elements belonging to a codec are supposed to be added
2335 * by this function so that a proper clean-up works at the free or
2336 * reconfiguration time.
2337 *
2338 * If non-zero @nid is passed, the NID is assigned to the control element.
2339 * The assignment is shown in the codec proc file.
2340 *
2341 * snd_hda_ctl_add() checks the control subdev id field whether
2342 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002343 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2344 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002346int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2347 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348{
2349 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002350 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002351 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002352
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002353 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002354 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002355 if (nid == 0)
2356 nid = get_amp_nid_(kctl->private_value);
2357 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002358 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2359 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002360 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002361 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002362 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002363 if (err < 0)
2364 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002365 item = snd_array_new(&codec->mixers);
2366 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002367 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002368 item->kctl = kctl;
2369 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002370 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002371 return 0;
2372}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002373EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002374
Takashi Iwaid5191e52009-11-16 14:58:17 +01002375/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002376 * snd_hda_add_nid - Assign a NID to a control element
2377 * @codec: HD-audio codec
2378 * @nid: corresponding NID (optional)
2379 * @kctl: the control element to assign
2380 * @index: index to kctl
2381 *
2382 * Add the given control element to an array inside the codec instance.
2383 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2384 * NID:KCTL mapping - for example "Capture Source" selector.
2385 */
2386int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2387 unsigned int index, hda_nid_t nid)
2388{
2389 struct hda_nid_item *item;
2390
2391 if (nid > 0) {
2392 item = snd_array_new(&codec->nids);
2393 if (!item)
2394 return -ENOMEM;
2395 item->kctl = kctl;
2396 item->index = index;
2397 item->nid = nid;
2398 return 0;
2399 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002400 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2401 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002402 return -EINVAL;
2403}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002404EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002405
2406/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002407 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2408 * @codec: HD-audio codec
2409 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002410void snd_hda_ctls_clear(struct hda_codec *codec)
2411{
2412 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002413 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002414 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002415 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002416 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002417 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002418}
2419
Takashi Iwai95a962c2014-10-29 16:03:58 +01002420/**
2421 * snd_hda_lock_devices - pseudo device locking
2422 * @bus: the BUS
2423 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424 * toggle card->shutdown to allow/disallow the device access (as a hack)
2425 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002426int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002427{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002428 struct snd_card *card = bus->card;
2429 struct hda_codec *codec;
2430
Takashi Iwaia65d6292009-02-23 16:57:04 +01002431 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002432 if (card->shutdown)
2433 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002435 if (!list_empty(&card->ctl_files))
2436 goto err_clear;
2437
2438 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002439 struct hda_pcm *cpcm;
2440 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002441 if (!cpcm->pcm)
2442 continue;
2443 if (cpcm->pcm->streams[0].substream_opened ||
2444 cpcm->pcm->streams[1].substream_opened)
2445 goto err_clear;
2446 }
2447 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002448 spin_unlock(&card->files_lock);
2449 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002450
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002451 err_clear:
2452 card->shutdown = 0;
2453 err_unlock:
2454 spin_unlock(&card->files_lock);
2455 return -EINVAL;
2456}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002457EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002458
Takashi Iwai95a962c2014-10-29 16:03:58 +01002459/**
2460 * snd_hda_unlock_devices - pseudo device unlocking
2461 * @bus: the BUS
2462 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002463void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002464{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002465 struct snd_card *card = bus->card;
2466
Takashi Iwaia65d6292009-02-23 16:57:04 +01002467 spin_lock(&card->files_lock);
2468 card->shutdown = 0;
2469 spin_unlock(&card->files_lock);
2470}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002471EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002472
Takashi Iwaid5191e52009-11-16 14:58:17 +01002473/**
2474 * snd_hda_codec_reset - Clear all objects assigned to the codec
2475 * @codec: HD-audio codec
2476 *
2477 * This frees the all PCM and control elements assigned to the codec, and
2478 * clears the caches and restores the pin default configurations.
2479 *
2480 * When a device is being used, it returns -EBSY. If successfully freed,
2481 * returns zero.
2482 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002483int snd_hda_codec_reset(struct hda_codec *codec)
2484{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002485 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002486
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002487 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002488 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002489
2490 /* OK, let it free */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002491 if (device_is_registered(hda_codec_dev(codec)))
2492 device_del(hda_codec_dev(codec));
2493
Takashi Iwaia65d6292009-02-23 16:57:04 +01002494 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002495 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002496 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002497}
2498
Takashi Iwai6194b992014-06-06 18:12:16 +02002499typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002500
2501/* apply the function to all matching slave ctls in the mixer list */
2502static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002503 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002504{
2505 struct hda_nid_item *items;
2506 const char * const *s;
2507 int i, err;
2508
2509 items = codec->mixers.list;
2510 for (i = 0; i < codec->mixers.used; i++) {
2511 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002512 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002513 continue;
2514 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002515 char tmpname[sizeof(sctl->id.name)];
2516 const char *name = *s;
2517 if (suffix) {
2518 snprintf(tmpname, sizeof(tmpname), "%s %s",
2519 name, suffix);
2520 name = tmpname;
2521 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002522 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002523 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002524 if (err)
2525 return err;
2526 break;
2527 }
2528 }
2529 }
2530 return 0;
2531}
2532
Takashi Iwai6194b992014-06-06 18:12:16 +02002533static int check_slave_present(struct hda_codec *codec,
2534 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002535{
2536 return 1;
2537}
2538
Takashi Iwai18478e82012-03-09 17:51:10 +01002539/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002540static int get_kctl_0dB_offset(struct hda_codec *codec,
2541 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002542{
2543 int _tlv[4];
2544 const int *tlv = NULL;
2545 int val = -1;
2546
2547 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2548 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2549 mm_segment_t fs = get_fs();
2550 set_fs(get_ds());
2551 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2552 tlv = _tlv;
2553 set_fs(fs);
2554 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2555 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002556 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2557 int step = tlv[3];
2558 step &= ~TLV_DB_SCALE_MUTE;
2559 if (!step)
2560 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002561 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002562 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002563- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002564 return -1;
2565 }
2566 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002567 val = -tlv[2] / step;
2568 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002569 return val;
2570}
2571
2572/* call kctl->put with the given value(s) */
2573static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2574{
2575 struct snd_ctl_elem_value *ucontrol;
2576 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2577 if (!ucontrol)
2578 return -ENOMEM;
2579 ucontrol->value.integer.value[0] = val;
2580 ucontrol->value.integer.value[1] = val;
2581 kctl->put(kctl, ucontrol);
2582 kfree(ucontrol);
2583 return 0;
2584}
2585
2586/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002587static int init_slave_0dB(struct hda_codec *codec,
2588 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002589{
Takashi Iwai6194b992014-06-06 18:12:16 +02002590 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002591 if (offset > 0)
2592 put_kctl_with_value(slave, offset);
2593 return 0;
2594}
2595
2596/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002597static int init_slave_unmute(struct hda_codec *codec,
2598 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002599{
2600 return put_kctl_with_value(slave, 1);
2601}
2602
Takashi Iwaie8750942014-06-30 14:02:39 +02002603static int add_slave(struct hda_codec *codec,
2604 void *data, struct snd_kcontrol *slave)
2605{
2606 return snd_ctl_add_slave(data, slave);
2607}
2608
Takashi Iwaid5191e52009-11-16 14:58:17 +01002609/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002610 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002611 * @codec: HD-audio codec
2612 * @name: vmaster control name
2613 * @tlv: TLV data (optional)
2614 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002615 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002616 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002617 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002618 *
2619 * Create a virtual master control with the given name. The TLV data
2620 * must be either NULL or a valid data.
2621 *
2622 * @slaves is a NULL-terminated array of strings, each of which is a
2623 * slave control name. All controls with these names are assigned to
2624 * the new virtual master control.
2625 *
2626 * This function returns zero if successful or a negative error code.
2627 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002628int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002629 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002630 const char *suffix, bool init_slave_vol,
2631 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002632{
2633 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002634 int err;
2635
Takashi Iwai29e58532012-03-12 12:25:03 +01002636 if (ctl_ret)
2637 *ctl_ret = NULL;
2638
Takashi Iwai9322ca52012-02-03 14:28:01 +01002639 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002640 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002641 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002642 return 0;
2643 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002644 kctl = snd_ctl_make_virtual_master(name, tlv);
2645 if (!kctl)
2646 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002647 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002648 if (err < 0)
2649 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002650
Takashi Iwaie8750942014-06-30 14:02:39 +02002651 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002652 if (err < 0)
2653 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002654
2655 /* init with master mute & zero volume */
2656 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002657 if (init_slave_vol) {
2658 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002659 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002660 tlv ? init_slave_0dB : init_slave_unmute, &step);
2661 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002662
Takashi Iwai29e58532012-03-12 12:25:03 +01002663 if (ctl_ret)
2664 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002665 return 0;
2666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002667EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002668
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002669/*
2670 * mute-LED control using vmaster
2671 */
2672static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_info *uinfo)
2674{
2675 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002676 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002677 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002678
Takashi Iwai3ff72212014-10-20 18:17:28 +02002679 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002680}
2681
2682static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2684{
2685 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2686 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2687 return 0;
2688}
2689
2690static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2692{
2693 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2694 unsigned int old_mode = hook->mute_mode;
2695
2696 hook->mute_mode = ucontrol->value.enumerated.item[0];
2697 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2698 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2699 if (old_mode == hook->mute_mode)
2700 return 0;
2701 snd_hda_sync_vmaster_hook(hook);
2702 return 1;
2703}
2704
2705static struct snd_kcontrol_new vmaster_mute_mode = {
2706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2707 .name = "Mute-LED Mode",
2708 .info = vmaster_mute_mode_info,
2709 .get = vmaster_mute_mode_get,
2710 .put = vmaster_mute_mode_put,
2711};
2712
Takashi Iwai95a962c2014-10-29 16:03:58 +01002713/**
2714 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2715 * @codec: the HDA codec
2716 * @hook: the vmaster hook object
2717 * @expose_enum_ctl: flag to create an enum ctl
2718 *
2719 * Add a mute-LED hook with the given vmaster switch kctl.
2720 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2721 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002722 */
2723int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002724 struct hda_vmaster_mute_hook *hook,
2725 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002726{
2727 struct snd_kcontrol *kctl;
2728
2729 if (!hook->hook || !hook->sw_kctl)
2730 return 0;
2731 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2732 hook->codec = codec;
2733 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002734 if (!expose_enum_ctl)
2735 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002736 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2737 if (!kctl)
2738 return -ENOMEM;
2739 return snd_hda_ctl_add(codec, 0, kctl);
2740}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002741EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002742
Takashi Iwai95a962c2014-10-29 16:03:58 +01002743/**
2744 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2745 * @hook: the vmaster hook
2746 *
2747 * Call the hook with the current value for synchronization.
2748 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002749 */
2750void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2751{
2752 if (!hook->hook || !hook->codec)
2753 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002754 /* don't call vmaster hook in the destructor since it might have
2755 * been already destroyed
2756 */
2757 if (hook->codec->bus->shutdown)
2758 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002759 switch (hook->mute_mode) {
2760 case HDA_VMUTE_FOLLOW_MASTER:
2761 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2762 break;
2763 default:
2764 hook->hook(hook->codec, hook->mute_mode);
2765 break;
2766 }
2767}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002768EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002769
2770
Takashi Iwaid5191e52009-11-16 14:58:17 +01002771/**
2772 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002773 * @kcontrol: referred ctl element
2774 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002775 *
2776 * The control element is supposed to have the private_value field
2777 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2778 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002779int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 int chs = get_amp_channels(kcontrol);
2783
2784 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2785 uinfo->count = chs == 3 ? 2 : 1;
2786 uinfo->value.integer.min = 0;
2787 uinfo->value.integer.max = 1;
2788 return 0;
2789}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002790EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Takashi Iwaid5191e52009-11-16 14:58:17 +01002792/**
2793 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002794 * @kcontrol: ctl element
2795 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002796 *
2797 * The control element is supposed to have the private_value field
2798 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2799 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002800int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2801 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
2803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2804 hda_nid_t nid = get_amp_nid(kcontrol);
2805 int chs = get_amp_channels(kcontrol);
2806 int dir = get_amp_direction(kcontrol);
2807 int idx = get_amp_index(kcontrol);
2808 long *valp = ucontrol->value.integer.value;
2809
2810 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002812 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002814 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002815 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 0;
2817}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002818EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Takashi Iwaid5191e52009-11-16 14:58:17 +01002820/**
2821 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002822 * @kcontrol: ctl element
2823 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002824 *
2825 * The control element is supposed to have the private_value field
2826 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2827 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002828int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2832 hda_nid_t nid = get_amp_nid(kcontrol);
2833 int chs = get_amp_channels(kcontrol);
2834 int dir = get_amp_direction(kcontrol);
2835 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 long *valp = ucontrol->value.integer.value;
2837 int change = 0;
2838
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002839 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002840 change = codec_amp_update(codec, nid, 0, dir, idx,
2841 HDA_AMP_MUTE,
2842 *valp ? 0 : HDA_AMP_MUTE, false,
2843 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002844 valp++;
2845 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002846 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002847 change |= codec_amp_update(codec, nid, 1, dir, idx,
2848 HDA_AMP_MUTE,
2849 *valp ? 0 : HDA_AMP_MUTE, false,
2850 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002851 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return change;
2853}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002854EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856/*
Takashi Iwai985be542005-11-02 18:26:49 +01002857 * bound volume controls
2858 *
2859 * bind multiple volumes (# indices, from 0)
2860 */
2861
2862#define AMP_VAL_IDX_SHIFT 19
2863#define AMP_VAL_IDX_MASK (0x0f<<19)
2864
Takashi Iwaid5191e52009-11-16 14:58:17 +01002865/**
2866 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002867 * @kcontrol: ctl element
2868 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002869 *
2870 * The control element is supposed to have the private_value field
2871 * set up via HDA_BIND_MUTE*() macros.
2872 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002873int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002875{
2876 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2877 unsigned long pval;
2878 int err;
2879
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002881 pval = kcontrol->private_value;
2882 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2883 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2884 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002885 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002886 return err;
2887}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002888EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002889
Takashi Iwaid5191e52009-11-16 14:58:17 +01002890/**
2891 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002892 * @kcontrol: ctl element
2893 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002894 *
2895 * The control element is supposed to have the private_value field
2896 * set up via HDA_BIND_MUTE*() macros.
2897 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002898int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002900{
2901 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2902 unsigned long pval;
2903 int i, indices, err = 0, change = 0;
2904
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002905 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002906 pval = kcontrol->private_value;
2907 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2908 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2910 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002911 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2912 if (err < 0)
2913 break;
2914 change |= err;
2915 }
2916 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002917 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002918 return err < 0 ? err : change;
2919}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002920EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002921
Takashi Iwaid5191e52009-11-16 14:58:17 +01002922/**
2923 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002924 * @kcontrol: referred ctl element
2925 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002926 *
2927 * The control element is supposed to have the private_value field
2928 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002929 */
2930int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_info *uinfo)
2932{
2933 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2934 struct hda_bind_ctls *c;
2935 int err;
2936
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002937 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002938 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002939 kcontrol->private_value = *c->values;
2940 err = c->ops->info(kcontrol, uinfo);
2941 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002942 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002943 return err;
2944}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002945EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002946
Takashi Iwaid5191e52009-11-16 14:58:17 +01002947/**
2948 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002949 * @kcontrol: ctl element
2950 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002951 *
2952 * The control element is supposed to have the private_value field
2953 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2954 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002955int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2957{
2958 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2959 struct hda_bind_ctls *c;
2960 int err;
2961
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002962 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002963 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002964 kcontrol->private_value = *c->values;
2965 err = c->ops->get(kcontrol, ucontrol);
2966 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002967 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002968 return err;
2969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002970EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002971
Takashi Iwaid5191e52009-11-16 14:58:17 +01002972/**
2973 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002974 * @kcontrol: ctl element
2975 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002976 *
2977 * The control element is supposed to have the private_value field
2978 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2979 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002980int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_value *ucontrol)
2982{
2983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2984 struct hda_bind_ctls *c;
2985 unsigned long *vals;
2986 int err = 0, change = 0;
2987
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002988 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002989 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002990 for (vals = c->values; *vals; vals++) {
2991 kcontrol->private_value = *vals;
2992 err = c->ops->put(kcontrol, ucontrol);
2993 if (err < 0)
2994 break;
2995 change |= err;
2996 }
2997 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002998 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002999 return err < 0 ? err : change;
3000}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003001EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003002
Takashi Iwaid5191e52009-11-16 14:58:17 +01003003/**
3004 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003005 * @kcontrol: ctl element
3006 * @op_flag: operation flag
3007 * @size: byte size of input TLV
3008 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003009 *
3010 * The control element is supposed to have the private_value field
3011 * set up via HDA_BIND_VOL() macro.
3012 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003013int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3014 unsigned int size, unsigned int __user *tlv)
3015{
3016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3017 struct hda_bind_ctls *c;
3018 int err;
3019
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003020 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003021 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003022 kcontrol->private_value = *c->values;
3023 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3024 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003025 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003026 return err;
3027}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003028EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003029
3030struct hda_ctl_ops snd_hda_bind_vol = {
3031 .info = snd_hda_mixer_amp_volume_info,
3032 .get = snd_hda_mixer_amp_volume_get,
3033 .put = snd_hda_mixer_amp_volume_put,
3034 .tlv = snd_hda_mixer_amp_tlv
3035};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003036EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003037
3038struct hda_ctl_ops snd_hda_bind_sw = {
3039 .info = snd_hda_mixer_amp_switch_info,
3040 .get = snd_hda_mixer_amp_switch_get,
3041 .put = snd_hda_mixer_amp_switch_put,
3042 .tlv = snd_hda_mixer_amp_tlv
3043};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003044EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003045
3046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 * SPDIF out controls
3048 */
3049
Takashi Iwai0ba21762007-04-16 11:29:14 +02003050static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3051 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
3053 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3054 uinfo->count = 1;
3055 return 0;
3056}
3057
Takashi Iwai0ba21762007-04-16 11:29:14 +02003058static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3059 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3062 IEC958_AES0_NONAUDIO |
3063 IEC958_AES0_CON_EMPHASIS_5015 |
3064 IEC958_AES0_CON_NOT_COPYRIGHT;
3065 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3066 IEC958_AES1_CON_ORIGINAL;
3067 return 0;
3068}
3069
Takashi Iwai0ba21762007-04-16 11:29:14 +02003070static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3074 IEC958_AES0_NONAUDIO |
3075 IEC958_AES0_PRO_EMPHASIS_5015;
3076 return 0;
3077}
3078
Takashi Iwai0ba21762007-04-16 11:29:14 +02003079static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003083 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003084 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003086 mutex_lock(&codec->spdif_mutex);
3087 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003088 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3089 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3090 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3091 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003092 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 return 0;
3095}
3096
3097/* convert from SPDIF status bits to HDA SPDIF bits
3098 * bit 0 (DigEn) is always set zero (to be filled later)
3099 */
3100static unsigned short convert_from_spdif_status(unsigned int sbits)
3101{
3102 unsigned short val = 0;
3103
3104 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3110 IEC958_AES0_PRO_EMPHASIS_5015)
3111 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003113 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3114 IEC958_AES0_CON_EMPHASIS_5015)
3115 val |= AC_DIG1_EMPHASIS;
3116 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3117 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003119 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3121 }
3122 return val;
3123}
3124
3125/* convert to SPDIF status bits from HDA SPDIF bits
3126 */
3127static unsigned int convert_to_spdif_status(unsigned short val)
3128{
3129 unsigned int sbits = 0;
3130
Takashi Iwai0ba21762007-04-16 11:29:14 +02003131 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 sbits |= IEC958_AES0_PROFESSIONAL;
3135 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003136 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3138 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003139 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003141 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3145 sbits |= val & (0x7f << 8);
3146 }
3147 return sbits;
3148}
3149
Takashi Iwai2f728532008-09-25 16:32:41 +02003150/* set digital convert verbs both for the given NID and its slaves */
3151static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3152 int verb, int val)
3153{
Takashi Iwaidda14412011-05-02 11:29:30 +02003154 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003155
Takashi Iwai9e976972008-11-25 08:17:20 +01003156 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003157 d = codec->slave_dig_outs;
3158 if (!d)
3159 return;
3160 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003161 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003162}
3163
3164static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3165 int dig1, int dig2)
3166{
3167 if (dig1 != -1)
3168 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3169 if (dig2 != -1)
3170 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3171}
3172
Takashi Iwai0ba21762007-04-16 11:29:14 +02003173static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3174 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003177 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003178 struct hda_spdif_out *spdif;
3179 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 unsigned short val;
3181 int change;
3182
Ingo Molnar62932df2006-01-16 16:34:20 +01003183 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003184 spdif = snd_array_elem(&codec->spdif_out, idx);
3185 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003186 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3188 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3189 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003190 val = convert_from_spdif_status(spdif->status);
3191 val |= spdif->ctls & 1;
3192 change = spdif->ctls != val;
3193 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003194 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003195 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003196 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return change;
3198}
3199
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003200#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Takashi Iwai0ba21762007-04-16 11:29:14 +02003202static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003206 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003207 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003209 mutex_lock(&codec->spdif_mutex);
3210 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003211 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003212 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return 0;
3214}
3215
Stephen Warren74b654c2011-06-01 11:14:18 -06003216static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3217 int dig1, int dig2)
3218{
3219 set_dig_out_convert(codec, nid, dig1, dig2);
3220 /* unmute amp switch (if any) */
3221 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3222 (dig1 & AC_DIG1_ENABLE))
3223 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3224 HDA_AMP_MUTE, 0);
3225}
3226
Takashi Iwai0ba21762007-04-16 11:29:14 +02003227static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003231 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003232 struct hda_spdif_out *spdif;
3233 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 unsigned short val;
3235 int change;
3236
Ingo Molnar62932df2006-01-16 16:34:20 +01003237 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003238 spdif = snd_array_elem(&codec->spdif_out, idx);
3239 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003240 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003242 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003243 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003244 spdif->ctls = val;
3245 if (change && nid != (u16)-1)
3246 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003247 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return change;
3249}
3250
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003251static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 {
3253 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003255 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 .info = snd_hda_spdif_mask_info,
3257 .get = snd_hda_spdif_cmask_get,
3258 },
3259 {
3260 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3261 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003262 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .info = snd_hda_spdif_mask_info,
3264 .get = snd_hda_spdif_pmask_get,
3265 },
3266 {
3267 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003268 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 .info = snd_hda_spdif_mask_info,
3270 .get = snd_hda_spdif_default_get,
3271 .put = snd_hda_spdif_default_put,
3272 },
3273 {
3274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003275 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .info = snd_hda_spdif_out_switch_info,
3277 .get = snd_hda_spdif_out_switch_get,
3278 .put = snd_hda_spdif_out_switch_put,
3279 },
3280 { } /* end */
3281};
3282
3283/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003284 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003286 * @associated_nid: NID that new ctls associated with
3287 * @cvt_nid: converter NID
3288 * @type: HDA_PCM_TYPE_*
3289 * Creates controls related with the digital output.
3290 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 *
3292 * Returns 0 if successful, or a negative error code.
3293 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003294int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3295 hda_nid_t associated_nid,
3296 hda_nid_t cvt_nid,
3297 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003300 struct snd_kcontrol *kctl;
3301 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003302 int idx = 0;
3303 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003304 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003305 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003307 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003308 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003309 idx = spdif_index;
3310 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003311 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003312 /* suppose a single SPDIF device */
3313 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3314 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3315 if (!kctl)
3316 break;
3317 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003318 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003319 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003320 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003321 if (!bus->primary_dig_out_type)
3322 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003323
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003324 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003325 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003326 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003327 return -EBUSY;
3328 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003329 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003330 if (!spdif)
3331 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3333 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003334 if (!kctl)
3335 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003336 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003337 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003338 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003339 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return err;
3341 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003342 spdif->nid = cvt_nid;
3343 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003344 AC_VERB_GET_DIGI_CONVERT_1, 0);
3345 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return 0;
3347}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003348EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Takashi Iwai95a962c2014-10-29 16:03:58 +01003350/**
3351 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3352 * @codec: the HDA codec
3353 * @nid: widget NID
3354 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003355 * call within spdif_mutex lock
3356 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003357struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3358 hda_nid_t nid)
3359{
3360 int i;
3361 for (i = 0; i < codec->spdif_out.used; i++) {
3362 struct hda_spdif_out *spdif =
3363 snd_array_elem(&codec->spdif_out, i);
3364 if (spdif->nid == nid)
3365 return spdif;
3366 }
3367 return NULL;
3368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003369EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003370
Takashi Iwai95a962c2014-10-29 16:03:58 +01003371/**
3372 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3373 * @codec: the HDA codec
3374 * @idx: the SPDIF ctl index
3375 *
3376 * Unassign the widget from the given SPDIF control.
3377 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003378void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3379{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003380 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003381
3382 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003383 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003384 spdif->nid = (u16)-1;
3385 mutex_unlock(&codec->spdif_mutex);
3386}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003387EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003388
Takashi Iwai95a962c2014-10-29 16:03:58 +01003389/**
3390 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3391 * @codec: the HDA codec
3392 * @idx: the SPDIF ctl idx
3393 * @nid: widget NID
3394 *
3395 * Assign the widget to the SPDIF control with the given index.
3396 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003397void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3398{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003399 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003400 unsigned short val;
3401
3402 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003403 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003404 if (spdif->nid != nid) {
3405 spdif->nid = nid;
3406 val = spdif->ctls;
3407 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3408 }
3409 mutex_unlock(&codec->spdif_mutex);
3410}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003411EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003414 * SPDIF sharing with analog output
3415 */
3416static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3420 ucontrol->value.integer.value[0] = mout->share_spdif;
3421 return 0;
3422}
3423
3424static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_value *ucontrol)
3426{
3427 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3428 mout->share_spdif = !!ucontrol->value.integer.value[0];
3429 return 0;
3430}
3431
3432static struct snd_kcontrol_new spdif_share_sw = {
3433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3434 .name = "IEC958 Default PCM Playback Switch",
3435 .info = snd_ctl_boolean_mono_info,
3436 .get = spdif_share_sw_get,
3437 .put = spdif_share_sw_put,
3438};
3439
Takashi Iwaid5191e52009-11-16 14:58:17 +01003440/**
3441 * snd_hda_create_spdif_share_sw - create Default PCM switch
3442 * @codec: the HDA codec
3443 * @mout: multi-out instance
3444 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003445int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3446 struct hda_multi_out *mout)
3447{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003448 struct snd_kcontrol *kctl;
3449
Takashi Iwai9a081602008-02-12 18:37:26 +01003450 if (!mout->dig_out_nid)
3451 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003452
3453 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3454 if (!kctl)
3455 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003456 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003457 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003458}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003459EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003460
3461/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 * SPDIF input
3463 */
3464
3465#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3466
Takashi Iwai0ba21762007-04-16 11:29:14 +02003467static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3471
3472 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3473 return 0;
3474}
3475
Takashi Iwai0ba21762007-04-16 11:29:14 +02003476static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3477 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
3479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3480 hda_nid_t nid = kcontrol->private_value;
3481 unsigned int val = !!ucontrol->value.integer.value[0];
3482 int change;
3483
Ingo Molnar62932df2006-01-16 16:34:20 +01003484 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003486 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003488 snd_hda_codec_write_cache(codec, nid, 0,
3489 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003491 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return change;
3493}
3494
Takashi Iwai0ba21762007-04-16 11:29:14 +02003495static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3496 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3499 hda_nid_t nid = kcontrol->private_value;
3500 unsigned short val;
3501 unsigned int sbits;
3502
Andrew Paprocki3982d172007-12-19 12:13:44 +01003503 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 sbits = convert_to_spdif_status(val);
3505 ucontrol->value.iec958.status[0] = sbits;
3506 ucontrol->value.iec958.status[1] = sbits >> 8;
3507 ucontrol->value.iec958.status[2] = sbits >> 16;
3508 ucontrol->value.iec958.status[3] = sbits >> 24;
3509 return 0;
3510}
3511
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003512static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 {
3514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003515 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 .info = snd_hda_spdif_in_switch_info,
3517 .get = snd_hda_spdif_in_switch_get,
3518 .put = snd_hda_spdif_in_switch_put,
3519 },
3520 {
3521 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003523 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 .info = snd_hda_spdif_mask_info,
3525 .get = snd_hda_spdif_in_status_get,
3526 },
3527 { } /* end */
3528};
3529
3530/**
3531 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3532 * @codec: the HDA codec
3533 * @nid: audio in widget NID
3534 *
3535 * Creates controls related with the SPDIF input.
3536 * Called from each patch supporting the SPDIF in.
3537 *
3538 * Returns 0 if successful, or a negative error code.
3539 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003540int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
3542 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003543 struct snd_kcontrol *kctl;
3544 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003545 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Takashi Iwaidcda5802012-10-12 17:24:51 +02003547 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003548 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003549 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003550 return -EBUSY;
3551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3553 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003554 if (!kctl)
3555 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003557 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return err;
3560 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003561 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003562 snd_hda_codec_read(codec, nid, 0,
3563 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003564 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return 0;
3566}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003567EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003569/*
3570 * command cache
3571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Takashi Iwaic370dd62012-12-13 18:30:04 +01003573/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003574#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3575#define get_cmd_cache_nid(key) ((key) & 0xff)
3576#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3577
3578/**
3579 * snd_hda_codec_write_cache - send a single command with caching
3580 * @codec: the HDA codec
3581 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003582 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003583 * @verb: the verb to send
3584 * @parm: the parameter for the verb
3585 *
3586 * Send a single command without waiting for response.
3587 *
3588 * Returns 0 if successful, or a negative error code.
3589 */
3590int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003591 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003592{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003593 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003594 struct hda_cache_head *c;
3595 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003596 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003597
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003598 cache_only = codec->cached_write;
3599 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003600 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003601 if (err < 0)
3602 return err;
3603 }
3604
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003605 /* parm may contain the verb stuff for get/set amp */
3606 verb = verb | (parm >> 8);
3607 parm &= 0xff;
3608 key = build_cmd_cache_key(nid, verb);
3609 mutex_lock(&codec->bus->cmd_mutex);
3610 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003611 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003612 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003613 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003614 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003615 mutex_unlock(&codec->bus->cmd_mutex);
3616 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003617}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003618EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003619
Takashi Iwaid5191e52009-11-16 14:58:17 +01003620/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003621 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3622 * @codec: the HDA codec
3623 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003624 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003625 * @verb: the verb to send
3626 * @parm: the parameter for the verb
3627 *
3628 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3629 * command if the parameter is already identical with the cached value.
3630 * If not, it sends the command and refreshes the cache.
3631 *
3632 * Returns 0 if successful, or a negative error code.
3633 */
3634int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003635 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003636{
3637 struct hda_cache_head *c;
3638 u32 key;
3639
3640 /* parm may contain the verb stuff for get/set amp */
3641 verb = verb | (parm >> 8);
3642 parm &= 0xff;
3643 key = build_cmd_cache_key(nid, verb);
3644 mutex_lock(&codec->bus->cmd_mutex);
3645 c = get_hash(&codec->cmd_cache, key);
3646 if (c && c->val == parm) {
3647 mutex_unlock(&codec->bus->cmd_mutex);
3648 return 0;
3649 }
3650 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003651 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003652}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003653EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003654
3655/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003656 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3657 * @codec: HD-audio codec
3658 *
3659 * Execute all verbs recorded in the command caches to resume.
3660 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003661void snd_hda_codec_resume_cache(struct hda_codec *codec)
3662{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003663 int i;
3664
Takashi Iwaic370dd62012-12-13 18:30:04 +01003665 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003666 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003667 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3668 struct hda_cache_head *buffer;
3669 u32 key;
3670
3671 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3672 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003673 if (!key)
3674 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003675 if (!buffer->dirty)
3676 continue;
3677 buffer->dirty = 0;
3678 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003679 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3680 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003681 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003682 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003683 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003684}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003685EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003686
3687/**
3688 * snd_hda_sequence_write_cache - sequence writes with caching
3689 * @codec: the HDA codec
3690 * @seq: VERB array to send
3691 *
3692 * Send the commands sequentially from the given array.
3693 * Thte commands are recorded on cache for power-save and resume.
3694 * The array must be terminated with NID=0.
3695 */
3696void snd_hda_sequence_write_cache(struct hda_codec *codec,
3697 const struct hda_verb *seq)
3698{
3699 for (; seq->nid; seq++)
3700 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3701 seq->param);
3702}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003703EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003704
Takashi Iwaidc870f32013-01-22 15:24:30 +01003705/**
3706 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3707 * @codec: HD-audio codec
3708 */
3709void snd_hda_codec_flush_cache(struct hda_codec *codec)
3710{
3711 snd_hda_codec_resume_amp(codec);
3712 snd_hda_codec_resume_cache(codec);
3713}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003714EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003715
Takashi Iwai95a962c2014-10-29 16:03:58 +01003716/**
3717 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3718 * @codec: the HDA codec
3719 * @fg: function group (not used now)
3720 * @power_state: the power state to set (AC_PWRST_*)
3721 *
3722 * Set the given power state to all widgets that have the power control.
3723 * If the codec has power_filter set, it evaluates the power state and
3724 * filter out if it's unchanged as D3.
3725 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003726void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003727 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003728{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003729 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003730 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003731
Takashi Iwaicb53c622007-08-10 17:21:45 +02003732 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003733 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003734 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 if (!(wcaps & AC_WCAP_POWER))
3736 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003737 if (codec->power_filter) {
3738 state = codec->power_filter(codec, nid, power_state);
3739 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003740 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003741 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003742 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003743 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003744 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003745}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003746EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003747
3748/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003749 * supported power states check
3750 */
3751static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3752 unsigned int power_state)
3753{
3754 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3755
Mengdong Line037cb42012-08-10 14:11:58 +02003756 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003757 return false;
3758 if (sup & power_state)
3759 return true;
3760 else
3761 return false;
3762}
3763
3764/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003765 * wait until the state is reached, returns the current state
3766 */
3767static unsigned int hda_sync_power_state(struct hda_codec *codec,
3768 hda_nid_t fg,
3769 unsigned int power_state)
3770{
3771 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3772 unsigned int state, actual_state;
3773
3774 for (;;) {
3775 state = snd_hda_codec_read(codec, fg, 0,
3776 AC_VERB_GET_POWER_STATE, 0);
3777 if (state & AC_PWRST_ERROR)
3778 break;
3779 actual_state = (state >> 4) & 0x0f;
3780 if (actual_state == power_state)
3781 break;
3782 if (time_after_eq(jiffies, end_time))
3783 break;
3784 /* wait until the codec reachs to the target state */
3785 msleep(1);
3786 }
3787 return state;
3788}
3789
Takashi Iwai95a962c2014-10-29 16:03:58 +01003790/**
3791 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3792 * @codec: the HDA codec
3793 * @nid: widget NID
3794 * @power_state: power state to evalue
3795 *
3796 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3797 * This can be used a codec power_filter callback.
3798 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003799unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3800 hda_nid_t nid,
3801 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003802{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003803 if (nid == codec->afg || nid == codec->mfg)
3804 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003805 if (power_state == AC_PWRST_D3 &&
3806 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3807 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3808 int eapd = snd_hda_codec_read(codec, nid, 0,
3809 AC_VERB_GET_EAPD_BTLENABLE, 0);
3810 if (eapd & 0x02)
3811 return AC_PWRST_D0;
3812 }
3813 return power_state;
3814}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003815EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003816
Takashi Iwai432c6412012-08-28 09:59:20 -07003817/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003818 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003819 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003820static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003821 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003822{
Takashi Iwaid8193872012-08-31 07:54:38 -07003823 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003824 int count;
3825 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003826 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003827
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003828 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003829 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003830 if (codec->depop_delay < 0)
3831 msleep(codec->epss ? 10 : 100);
3832 else if (codec->depop_delay > 0)
3833 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003834 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003835 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003836
3837 /* repeat power states setting at most 10 times*/
3838 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003839 if (codec->patch_ops.set_power_state)
3840 codec->patch_ops.set_power_state(codec, fg,
3841 power_state);
3842 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003843 state = power_state;
3844 if (codec->power_filter)
3845 state = codec->power_filter(codec, fg, state);
3846 if (state == power_state || power_state != AC_PWRST_D3)
3847 snd_hda_codec_read(codec, fg, flags,
3848 AC_VERB_SET_POWER_STATE,
3849 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003850 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003851 }
3852 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003853 if (!(state & AC_PWRST_ERROR))
3854 break;
3855 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003856
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003857 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003858}
Takashi Iwai54d17402005-11-21 16:33:22 +01003859
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003860/* sync power states of all widgets;
3861 * this is called at the end of codec parsing
3862 */
3863static void sync_power_up_states(struct hda_codec *codec)
3864{
3865 hda_nid_t nid = codec->start_nid;
3866 int i;
3867
Takashi Iwaiba615b82013-03-13 14:47:21 +01003868 /* don't care if no filter is used */
3869 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003870 return;
3871
3872 for (i = 0; i < codec->num_nodes; i++, nid++) {
3873 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003874 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003875 if (!(wcaps & AC_WCAP_POWER))
3876 continue;
3877 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3878 if (target == AC_PWRST_D0)
3879 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003880 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003881 snd_hda_codec_write(codec, nid, 0,
3882 AC_VERB_SET_POWER_STATE, target);
3883 }
3884}
3885
Takashi Iwai648a8d22014-02-25 10:38:13 +01003886#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003887/* execute additional init verbs */
3888static void hda_exec_init_verbs(struct hda_codec *codec)
3889{
3890 if (codec->init_verbs.list)
3891 snd_hda_sequence_write(codec, codec->init_verbs.list);
3892}
3893#else
3894static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3895#endif
3896
Takashi Iwai2a439522011-07-26 09:52:50 +02003897#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003898/* update the power on/off account with the current jiffies */
3899static void update_power_acct(struct hda_codec *codec, bool on)
3900{
3901 unsigned long delta = jiffies - codec->power_jiffies;
3902
3903 if (on)
3904 codec->power_on_acct += delta;
3905 else
3906 codec->power_off_acct += delta;
3907 codec->power_jiffies += delta;
3908}
3909
3910void snd_hda_update_power_acct(struct hda_codec *codec)
3911{
3912 update_power_acct(codec, hda_codec_is_power_on(codec));
3913}
3914
Takashi Iwaicb53c622007-08-10 17:21:45 +02003915/*
3916 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003917 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003918 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003919static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003920{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003921 unsigned int state;
3922
Takashi Iwaicc72da72015-02-19 16:00:22 +01003923 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003924
Takashi Iwaicb53c622007-08-10 17:21:45 +02003925 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003926 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003927 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003928 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003929 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003930 update_power_acct(codec, true);
3931 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003932 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003933}
3934
Takashi Iwaic370dd62012-12-13 18:30:04 +01003935/* mark all entries of cmd and amp caches dirty */
3936static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3937{
3938 int i;
3939 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3940 struct hda_cache_head *cmd;
3941 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3942 cmd->dirty = 1;
3943 }
3944 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3945 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003946 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003947 amp->head.dirty = 1;
3948 }
3949}
3950
Takashi Iwaicb53c622007-08-10 17:21:45 +02003951/*
3952 * kick up codec; used both from PM and power-save
3953 */
3954static void hda_call_codec_resume(struct hda_codec *codec)
3955{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003956 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003957
Takashi Iwaicc72da72015-02-19 16:00:22 +01003958 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003959 hda_mark_cmd_cache_dirty(codec);
3960
Takashi Iwaicc72da72015-02-19 16:00:22 +01003961 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003962
Takashi Iwaid8193872012-08-31 07:54:38 -07003963 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003964 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003965 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003966 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003967 if (codec->patch_ops.resume)
3968 codec->patch_ops.resume(codec);
3969 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003970 if (codec->patch_ops.init)
3971 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003972 snd_hda_codec_resume_amp(codec);
3973 snd_hda_codec_resume_cache(codec);
3974 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003975
3976 if (codec->jackpoll_interval)
3977 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003978 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003979 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003980 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003981}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003982
Takashi Iwaicc72da72015-02-19 16:00:22 +01003983static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003984{
3985 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003986 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003987 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003988
3989 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003990 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3991 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003992 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003993 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
3994 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003995 return 0;
3996}
3997
Takashi Iwaicc72da72015-02-19 16:00:22 +01003998static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003999{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01004000 struct hda_codec *codec = dev_to_hda_codec(dev);
4001
4002 set_bit(codec->addr, &codec->bus->codec_powered);
4003 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004004 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004005 return 0;
4006}
Takashi Iwai2a439522011-07-26 09:52:50 +02004007#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004008
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004009/* referred in hda_bind.c */
4010const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004011 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4012 pm_runtime_force_resume)
4013 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
4014 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004015};
Takashi Iwai54d17402005-11-21 16:33:22 +01004016
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004017/*
4018 * add standard channel maps if not specified
4019 */
4020static int add_std_chmaps(struct hda_codec *codec)
4021{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004022 struct hda_pcm *pcm;
4023 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004024
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004025 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004026 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004027 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004028 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004029 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004030
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004031 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004032 continue;
4033 if (!pcm || !hinfo->substreams)
4034 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004035 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004036 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004037 hinfo->channels_max,
4038 0, &chmap);
4039 if (err < 0)
4040 return err;
4041 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4042 }
4043 }
4044 return 0;
4045}
4046
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004047/* default channel maps for 2.1 speakers;
4048 * since HD-audio supports only stereo, odd number channels are omitted
4049 */
4050const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4051 { .channels = 2,
4052 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4053 { .channels = 4,
4054 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4055 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4056 { }
4057};
4058EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4059
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004060int snd_hda_codec_build_controls(struct hda_codec *codec)
4061{
4062 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004063 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004064 /* continue to initialize... */
4065 if (codec->patch_ops.init)
4066 err = codec->patch_ops.init(codec);
4067 if (!err && codec->patch_ops.build_controls)
4068 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004069 if (err < 0)
4070 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004071
4072 /* we create chmaps here instead of build_pcms */
4073 err = add_std_chmaps(codec);
4074 if (err < 0)
4075 return err;
4076
David Henningsson26a6cb62012-10-09 15:04:21 +02004077 if (codec->jackpoll_interval)
4078 hda_jackpoll_work(&codec->jackpoll_work.work);
4079 else
4080 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004081 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return 0;
4083}
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085/*
4086 * stream formats
4087 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004088struct hda_rate_tbl {
4089 unsigned int hz;
4090 unsigned int alsa_bits;
4091 unsigned int hda_fmt;
4092};
4093
Takashi Iwai92f10b32010-08-03 14:21:00 +02004094/* rate = base * mult / div */
4095#define HDA_RATE(base, mult, div) \
4096 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4097 (((div) - 1) << AC_FMT_DIV_SHIFT))
4098
Takashi Iwaibefdf312005-08-22 13:57:55 +02004099static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004101
4102 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004103 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4104 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4105 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4106 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4107 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4108 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4109 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4110 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4111 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4112 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4113 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004114#define AC_PAR_PCM_RATE_BITS 11
4115 /* up to bits 10, 384kHZ isn't supported properly */
4116
4117 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004118 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004119
Takashi Iwaibefdf312005-08-22 13:57:55 +02004120 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121};
4122
4123/**
4124 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004125 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 * @rate: the sample rate
4127 * @channels: the number of channels
4128 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4129 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004130 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 *
4132 * Calculate the format bitset from the given rate, channels and th PCM format.
4133 *
4134 * Return zero if invalid.
4135 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004136unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4137 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 unsigned int channels,
4139 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004140 unsigned int maxbps,
4141 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 int i;
4144 unsigned int val = 0;
4145
Takashi Iwaibefdf312005-08-22 13:57:55 +02004146 for (i = 0; rate_bits[i].hz; i++)
4147 if (rate_bits[i].hz == rate) {
4148 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 break;
4150 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004151 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004152 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 return 0;
4154 }
4155
4156 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004157 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 }
4160 val |= channels - 1;
4161
4162 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004163 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004164 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004165 break;
4166 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004167 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 case 20:
4170 case 24:
4171 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004172 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004173 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004175 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004177 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004180 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004181 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return 0;
4183 }
4184
Anssi Hannula32c168c2010-08-03 13:28:57 +03004185 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004186 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return val;
4189}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004190EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004192static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4193 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004194{
4195 unsigned int val = 0;
4196 if (nid != codec->afg &&
4197 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4198 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4199 if (!val || val == -1)
4200 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4201 if (!val || val == -1)
4202 return 0;
4203 return val;
4204}
4205
4206static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4207{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004208 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004209 get_pcm_param);
4210}
4211
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004212static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4213 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004214{
4215 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4216 if (!streams || streams == -1)
4217 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4218 if (!streams || streams == -1)
4219 return 0;
4220 return streams;
4221}
4222
4223static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4224{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004225 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004226 get_stream_param);
4227}
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229/**
4230 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4231 * @codec: the HDA codec
4232 * @nid: NID to query
4233 * @ratesp: the pointer to store the detected rate bitflags
4234 * @formatsp: the pointer to store the detected formats
4235 * @bpsp: the pointer to store the detected format widths
4236 *
4237 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4238 * or @bsps argument is ignored.
4239 *
4240 * Returns 0 if successful, otherwise a negative error code.
4241 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004242int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4244{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004245 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004247 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004248 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 if (ratesp) {
4251 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004252 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004254 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004256 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004257 codec_err(codec,
4258 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4259 nid, val,
4260 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004261 return -EIO;
4262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 *ratesp = rates;
4264 }
4265
4266 if (formatsp || bpsp) {
4267 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004268 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004270 streams = query_stream_param(codec, nid);
4271 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 bps = 0;
4275 if (streams & AC_SUPFMT_PCM) {
4276 if (val & AC_SUPPCM_BITS_8) {
4277 formats |= SNDRV_PCM_FMTBIT_U8;
4278 bps = 8;
4279 }
4280 if (val & AC_SUPPCM_BITS_16) {
4281 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4282 bps = 16;
4283 }
4284 if (wcaps & AC_WCAP_DIGITAL) {
4285 if (val & AC_SUPPCM_BITS_32)
4286 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4287 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4288 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4289 if (val & AC_SUPPCM_BITS_24)
4290 bps = 24;
4291 else if (val & AC_SUPPCM_BITS_20)
4292 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004293 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4294 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4296 if (val & AC_SUPPCM_BITS_32)
4297 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 else if (val & AC_SUPPCM_BITS_24)
4299 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004300 else if (val & AC_SUPPCM_BITS_20)
4301 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
4303 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004304#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004305 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004307 if (!bps)
4308 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004309 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004310#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004311 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004312 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 /* temporary hack: we have still no proper support
4314 * for the direct AC3 stream...
4315 */
4316 formats |= SNDRV_PCM_FMTBIT_U8;
4317 bps = 8;
4318 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004319 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004320 codec_err(codec,
4321 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4322 nid, val,
4323 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4324 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004325 return -EIO;
4326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (formatsp)
4328 *formatsp = formats;
4329 if (bpsp)
4330 *bpsp = bps;
4331 }
4332
4333 return 0;
4334}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004335EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004338 * snd_hda_is_supported_format - Check the validity of the format
4339 * @codec: HD-audio codec
4340 * @nid: NID to check
4341 * @format: the HD-audio format value to check
4342 *
4343 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 *
4345 * Returns 1 if supported, 0 if not.
4346 */
4347int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4348 unsigned int format)
4349{
4350 int i;
4351 unsigned int val = 0, rate, stream;
4352
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004353 val = query_pcm_param(codec, nid);
4354 if (!val)
4355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
4357 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004358 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004359 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (val & (1 << i))
4361 break;
4362 return 0;
4363 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004364 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004367 stream = query_stream_param(codec, nid);
4368 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return 0;
4370
4371 if (stream & AC_SUPFMT_PCM) {
4372 switch (format & 0xf0) {
4373 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004374 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return 0;
4376 break;
4377 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004378 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return 0;
4380 break;
4381 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004382 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return 0;
4384 break;
4385 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004386 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388 break;
4389 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004390 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392 break;
4393 default:
4394 return 0;
4395 }
4396 } else {
4397 /* FIXME: check for float32 and AC3? */
4398 }
4399
4400 return 1;
4401}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004402EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404/*
4405 * PCM stuff
4406 */
4407static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4408 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004409 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411 return 0;
4412}
4413
4414static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
4416 unsigned int stream_tag,
4417 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004418 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
4420 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4421 return 0;
4422}
4423
4424static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4425 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004426 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Takashi Iwai888afa12008-03-18 09:57:50 +01004428 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return 0;
4430}
4431
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004432static int set_pcm_default_values(struct hda_codec *codec,
4433 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004435 int err;
4436
Takashi Iwai0ba21762007-04-16 11:29:14 +02004437 /* query support PCM information from the given NID */
4438 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004439 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004440 info->rates ? NULL : &info->rates,
4441 info->formats ? NULL : &info->formats,
4442 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004443 if (err < 0)
4444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446 if (info->ops.open == NULL)
4447 info->ops.open = hda_pcm_default_open_close;
4448 if (info->ops.close == NULL)
4449 info->ops.close = hda_pcm_default_open_close;
4450 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004451 if (snd_BUG_ON(!info->nid))
4452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 info->ops.prepare = hda_pcm_default_prepare;
4454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004456 if (snd_BUG_ON(!info->nid))
4457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 info->ops.cleanup = hda_pcm_default_cleanup;
4459 }
4460 return 0;
4461}
4462
Takashi Iwaieb541332010-08-06 13:48:11 +02004463/*
4464 * codec prepare/cleanup entries
4465 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004466/**
4467 * snd_hda_codec_prepare - Prepare a stream
4468 * @codec: the HDA codec
4469 * @hinfo: PCM information
4470 * @stream: stream tag to assign
4471 * @format: format id to assign
4472 * @substream: PCM substream to assign
4473 *
4474 * Calls the prepare callback set by the codec with the given arguments.
4475 * Clean up the inactive streams when successful.
4476 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004477int snd_hda_codec_prepare(struct hda_codec *codec,
4478 struct hda_pcm_stream *hinfo,
4479 unsigned int stream,
4480 unsigned int format,
4481 struct snd_pcm_substream *substream)
4482{
4483 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004484 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004485 if (hinfo->ops.prepare)
4486 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4487 substream);
4488 else
4489 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004490 if (ret >= 0)
4491 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004492 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004493 return ret;
4494}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004495EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004496
Takashi Iwai95a962c2014-10-29 16:03:58 +01004497/**
4498 * snd_hda_codec_cleanup - Prepare a stream
4499 * @codec: the HDA codec
4500 * @hinfo: PCM information
4501 * @substream: PCM substream
4502 *
4503 * Calls the cleanup callback set by the codec with the given arguments.
4504 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004505void snd_hda_codec_cleanup(struct hda_codec *codec,
4506 struct hda_pcm_stream *hinfo,
4507 struct snd_pcm_substream *substream)
4508{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004509 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004510 if (hinfo->ops.cleanup)
4511 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004512 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004513}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004514EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004515
Takashi Iwaid5191e52009-11-16 14:58:17 +01004516/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004517const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4518 "Audio", "SPDIF", "HDMI", "Modem"
4519};
4520
Takashi Iwai176d5332008-07-30 15:01:44 +02004521/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004522 * get the empty PCM device number to assign
4523 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004524static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004525{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004526 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004527 /* assigned to static slots up to dev#10; if more needed, assign
4528 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4529 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004530 static int audio_idx[HDA_PCM_NTYPES][5] = {
4531 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4532 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004533 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004534 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004535 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004536 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004537
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004538 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004539 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004540 return -EINVAL;
4541 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004542
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004543 for (i = 0; audio_idx[type][i] >= 0; i++) {
4544#ifndef CONFIG_SND_DYNAMIC_MINORS
4545 if (audio_idx[type][i] >= 8)
4546 break;
4547#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004548 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4549 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004550 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004551
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004552#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004553 /* non-fixed slots starting from 10 */
4554 for (i = 10; i < 32; i++) {
4555 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4556 return i;
4557 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004558#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004559
Takashi Iwai4e76a882014-02-25 12:21:03 +01004560 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004561 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004562#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004563 dev_warn(bus->card->dev,
4564 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004565#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004566 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004567}
4568
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004569/* call build_pcms ops of the given codec and set up the default parameters */
4570int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004571{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004572 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004573 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004574
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004575 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004576 return 0; /* already parsed */
4577
4578 if (!codec->patch_ops.build_pcms)
4579 return 0;
4580
4581 err = codec->patch_ops.build_pcms(codec);
4582 if (err < 0) {
4583 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4584 codec->addr, err);
4585 return err;
4586 }
4587
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004588 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004589 int stream;
4590
4591 for (stream = 0; stream < 2; stream++) {
4592 struct hda_pcm_stream *info = &cpcm->stream[stream];
4593
4594 if (!info->substreams)
4595 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004596 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004597 if (err < 0) {
4598 codec_warn(codec,
4599 "fail to setup default for PCM %s\n",
4600 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004601 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004602 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004603 }
4604 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004605
4606 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004607}
4608
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004609/* assign all PCMs of the given codec */
4610int snd_hda_codec_build_pcms(struct hda_codec *codec)
4611{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004612 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004613 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004614 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004615
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004616 if (snd_BUG_ON(!bus->ops.attach_pcm))
4617 return -EINVAL;
4618
4619 err = snd_hda_codec_parse_pcms(codec);
4620 if (err < 0) {
4621 snd_hda_codec_reset(codec);
4622 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004623 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004624
4625 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004626 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004627 if (cpcm->pcm)
4628 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004629 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004630 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004631
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004632 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4633 if (dev < 0)
4634 continue; /* no fatal error */
4635 cpcm->device = dev;
4636 err = bus->ops.attach_pcm(bus, codec, cpcm);
4637 if (err < 0) {
4638 codec_err(codec,
4639 "cannot attach PCM stream %d for codec #%d\n",
4640 dev, codec->addr);
4641 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004642 }
4643 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004644
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004645 return 0;
4646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 * snd_hda_add_new_ctls - create controls from the array
4650 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004651 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 *
4653 * This helper function creates and add new controls in the given array.
4654 * The array must be terminated with an empty entry as terminator.
4655 *
4656 * Returns 0 if successful, or a negative error code.
4657 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004658int snd_hda_add_new_ctls(struct hda_codec *codec,
4659 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004661 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004664 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004665 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004666 if (knew->iface == -1) /* skip this codec private value */
4667 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004668 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004669 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004670 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004671 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004672 if (addr > 0)
4673 kctl->id.device = addr;
4674 if (idx > 0)
4675 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004676 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004677 if (!err)
4678 break;
4679 /* try first with another device index corresponding to
4680 * the codec addr; if it still fails (or it's the
4681 * primary codec), then try another control index
4682 */
4683 if (!addr && codec->addr)
4684 addr = codec->addr;
4685 else if (!idx && !knew->index) {
4686 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004687 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004688 if (idx <= 0)
4689 return err;
4690 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004691 return err;
4692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
4694 return 0;
4695}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004696EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Takashi Iwai83012a72012-08-24 18:38:08 +02004698#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004699/**
4700 * snd_hda_power_up - Power-up the codec
4701 * @codec: HD-audio codec
4702 *
4703 * Increment the usage counter and resume the device if not done yet.
4704 */
4705void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004706{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004707 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004708
Takashi Iwaicc72da72015-02-19 16:00:22 +01004709 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004710 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004711 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004712}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004713EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004714
4715/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004716 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004717 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004718 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004719 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004720 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004721void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004722{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004723 struct device *dev = hda_codec_dev(codec);
4724
4725 if (codec_in_pm(codec))
4726 return;
4727 pm_runtime_mark_last_busy(dev);
4728 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004729}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004730EXPORT_SYMBOL_GPL(snd_hda_power_down);
4731
Takashi Iwaibb573922015-02-20 09:26:04 +01004732static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004733{
4734 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004735
Takashi Iwaicc72da72015-02-19 16:00:22 +01004736 if (delay > 0) {
4737 pm_runtime_set_autosuspend_delay(dev, delay);
4738 pm_runtime_use_autosuspend(dev);
4739 pm_runtime_allow(dev);
4740 if (!pm_runtime_suspended(dev))
4741 pm_runtime_mark_last_busy(dev);
4742 } else {
4743 pm_runtime_dont_use_autosuspend(dev);
4744 pm_runtime_forbid(dev);
4745 }
4746}
Takashi Iwaibb573922015-02-20 09:26:04 +01004747
4748/**
4749 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4750 * @bus: HD-audio bus
4751 * @delay: autosuspend delay in msec, 0 = off
4752 *
4753 * Synchronize the runtime PM autosuspend state from the power_save option.
4754 */
4755void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4756{
4757 struct hda_codec *c;
4758
4759 list_for_each_entry(c, &bus->codec_list, list)
4760 codec_set_power_save(c, delay);
4761}
4762EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004763
Takashi Iwaid5191e52009-11-16 14:58:17 +01004764/**
4765 * snd_hda_check_amp_list_power - Check the amp list and update the power
4766 * @codec: HD-audio codec
4767 * @check: the object containing an AMP list and the status
4768 * @nid: NID to check / update
4769 *
4770 * Check whether the given NID is in the amp list. If it's in the list,
4771 * check the current AMP status, and update the the power-status according
4772 * to the mute status.
4773 *
4774 * This function is supposed to be set or called from the check_power_status
4775 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004776 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004777int snd_hda_check_amp_list_power(struct hda_codec *codec,
4778 struct hda_loopback_check *check,
4779 hda_nid_t nid)
4780{
Takashi Iwai031024e2011-05-02 11:29:30 +02004781 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004782 int ch, v;
4783
4784 if (!check->amplist)
4785 return 0;
4786 for (p = check->amplist; p->nid; p++) {
4787 if (p->nid == nid)
4788 break;
4789 }
4790 if (!p->nid)
4791 return 0; /* nothing changed */
4792
4793 for (p = check->amplist; p->nid; p++) {
4794 for (ch = 0; ch < 2; ch++) {
4795 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4796 p->idx);
4797 if (!(v & HDA_AMP_MUTE) && v > 0) {
4798 if (!check->power_on) {
4799 check->power_on = 1;
4800 snd_hda_power_up(codec);
4801 }
4802 return 1;
4803 }
4804 }
4805 }
4806 if (check->power_on) {
4807 check->power_on = 0;
4808 snd_hda_power_down(codec);
4809 }
4810 return 0;
4811}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004812EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 * input MUX helper
4817 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004818
4819/**
4820 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004821 * @imux: imux helper object
4822 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004823 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004824int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4825 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 unsigned int index;
4828
4829 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4830 uinfo->count = 1;
4831 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004832 if (!imux->num_items)
4833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 index = uinfo->value.enumerated.item;
4835 if (index >= imux->num_items)
4836 index = imux->num_items - 1;
4837 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4838 return 0;
4839}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004840EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Takashi Iwaid5191e52009-11-16 14:58:17 +01004842/**
4843 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004844 * @codec: the HDA codec
4845 * @imux: imux helper object
4846 * @ucontrol: pointer to get/store the data
4847 * @nid: input mux NID
4848 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004849 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004850int snd_hda_input_mux_put(struct hda_codec *codec,
4851 const struct hda_input_mux *imux,
4852 struct snd_ctl_elem_value *ucontrol,
4853 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 unsigned int *cur_val)
4855{
4856 unsigned int idx;
4857
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004858 if (!imux->num_items)
4859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 idx = ucontrol->value.enumerated.item[0];
4861 if (idx >= imux->num_items)
4862 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004863 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004865 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4866 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 *cur_val = idx;
4868 return 1;
4869}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004870EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004873/**
4874 * snd_hda_enum_helper_info - Helper for simple enum ctls
4875 * @kcontrol: ctl element
4876 * @uinfo: pointer to get/store the data
4877 * @num_items: number of enum items
4878 * @texts: enum item string array
4879 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004880 * process kcontrol info callback of a simple string enum array
4881 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4882 */
4883int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4884 struct snd_ctl_elem_info *uinfo,
4885 int num_items, const char * const *texts)
4886{
4887 static const char * const texts_default[] = {
4888 "Disabled", "Enabled"
4889 };
4890
4891 if (!texts || !num_items) {
4892 num_items = 2;
4893 texts = texts_default;
4894 }
4895
Takashi Iwai3ff72212014-10-20 18:17:28 +02004896 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004897}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004898EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004899
4900/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 * Multi-channel / digital-out PCM helper functions
4902 */
4903
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004904/* setup SPDIF output stream */
4905static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4906 unsigned int stream_tag, unsigned int format)
4907{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004908 struct hda_spdif_out *spdif;
4909 unsigned int curr_fmt;
4910 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004911
Laurence Darby3bef1c32012-11-03 17:00:06 +00004912 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4913 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4914 AC_VERB_GET_STREAM_FORMAT, 0);
4915 reset = codec->spdif_status_reset &&
4916 (spdif->ctls & AC_DIG1_ENABLE) &&
4917 curr_fmt != format;
4918
4919 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4920 updated */
4921 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004922 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004923 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004924 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004925 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004926 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004927 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004928 for (d = codec->slave_dig_outs; *d; d++)
4929 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4930 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004931 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004932 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004933 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004934 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004935 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004936}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004937
Takashi Iwai2f728532008-09-25 16:32:41 +02004938static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4939{
4940 snd_hda_codec_cleanup_stream(codec, nid);
4941 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004942 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004943 for (d = codec->slave_dig_outs; *d; d++)
4944 snd_hda_codec_cleanup_stream(codec, *d);
4945 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004946}
4947
Takashi Iwaid5191e52009-11-16 14:58:17 +01004948/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004949 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004950 * @codec: the HDA codec
4951 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004953int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4954 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Ingo Molnar62932df2006-01-16 16:34:20 +01004956 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004957 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4958 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004959 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004961 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return 0;
4963}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004964EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
Takashi Iwaid5191e52009-11-16 14:58:17 +01004966/**
4967 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004968 * @codec: the HDA codec
4969 * @mout: hda_multi_out object
4970 * @stream_tag: stream tag to assign
4971 * @format: format id to assign
4972 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004973 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004974int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4975 struct hda_multi_out *mout,
4976 unsigned int stream_tag,
4977 unsigned int format,
4978 struct snd_pcm_substream *substream)
4979{
4980 mutex_lock(&codec->spdif_mutex);
4981 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4982 mutex_unlock(&codec->spdif_mutex);
4983 return 0;
4984}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004985EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004986
Takashi Iwaid5191e52009-11-16 14:58:17 +01004987/**
4988 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004989 * @codec: the HDA codec
4990 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01004991 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004992int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4993 struct hda_multi_out *mout)
4994{
4995 mutex_lock(&codec->spdif_mutex);
4996 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4997 mutex_unlock(&codec->spdif_mutex);
4998 return 0;
4999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005000EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005001
Takashi Iwaid5191e52009-11-16 14:58:17 +01005002/**
5003 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005004 * @codec: the HDA codec
5005 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005007int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5008 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009{
Ingo Molnar62932df2006-01-16 16:34:20 +01005010 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005012 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 return 0;
5014}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005015EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Takashi Iwaid5191e52009-11-16 14:58:17 +01005017/**
5018 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005019 * @codec: the HDA codec
5020 * @mout: hda_multi_out object
5021 * @substream: PCM substream to assign
5022 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005023 *
5024 * Open analog outputs and set up the hw-constraints.
5025 * If the digital outputs can be opened as slave, open the digital
5026 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005028int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5029 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005030 struct snd_pcm_substream *substream,
5031 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032{
Takashi Iwai9a081602008-02-12 18:37:26 +01005033 struct snd_pcm_runtime *runtime = substream->runtime;
5034 runtime->hw.channels_max = mout->max_channels;
5035 if (mout->dig_out_nid) {
5036 if (!mout->analog_rates) {
5037 mout->analog_rates = hinfo->rates;
5038 mout->analog_formats = hinfo->formats;
5039 mout->analog_maxbps = hinfo->maxbps;
5040 } else {
5041 runtime->hw.rates = mout->analog_rates;
5042 runtime->hw.formats = mout->analog_formats;
5043 hinfo->maxbps = mout->analog_maxbps;
5044 }
5045 if (!mout->spdif_rates) {
5046 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5047 &mout->spdif_rates,
5048 &mout->spdif_formats,
5049 &mout->spdif_maxbps);
5050 }
5051 mutex_lock(&codec->spdif_mutex);
5052 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005053 if ((runtime->hw.rates & mout->spdif_rates) &&
5054 (runtime->hw.formats & mout->spdif_formats)) {
5055 runtime->hw.rates &= mout->spdif_rates;
5056 runtime->hw.formats &= mout->spdif_formats;
5057 if (mout->spdif_maxbps < hinfo->maxbps)
5058 hinfo->maxbps = mout->spdif_maxbps;
5059 } else {
5060 mout->share_spdif = 0;
5061 /* FIXME: need notify? */
5062 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005063 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005064 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5067 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5068}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005069EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Takashi Iwaid5191e52009-11-16 14:58:17 +01005071/**
5072 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005073 * @codec: the HDA codec
5074 * @mout: hda_multi_out object
5075 * @stream_tag: stream tag to assign
5076 * @format: format id to assign
5077 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005078 *
5079 * Set up the i/o for analog out.
5080 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005082int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5083 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 unsigned int stream_tag,
5085 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005086 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Takashi Iwaidda14412011-05-02 11:29:30 +02005088 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005090 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 int i;
5092
Ingo Molnar62932df2006-01-16 16:34:20 +01005093 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005094 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005095 if (mout->dig_out_nid && mout->share_spdif &&
5096 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005098 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5099 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005100 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005102 setup_dig_out_stream(codec, mout->dig_out_nid,
5103 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 } else {
5105 mout->dig_out_used = 0;
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 }
5108 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005109 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
5111 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005112 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5113 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005114 if (!mout->no_share_stream &&
5115 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005117 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5118 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005119 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005120 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5121 if (!mout->no_share_stream && mout->hp_out_nid[i])
5122 snd_hda_codec_setup_stream(codec,
5123 mout->hp_out_nid[i],
5124 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 /* surrounds */
5127 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005128 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005129 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5130 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005131 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005132 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5133 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005135
5136 /* extra surrounds */
5137 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5138 int ch = 0;
5139 if (!mout->extra_out_nid[i])
5140 break;
5141 if (chs >= (i + 1) * 2)
5142 ch = i * 2;
5143 else if (!mout->no_share_stream)
5144 break;
5145 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5146 stream_tag, ch, format);
5147 }
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 return 0;
5150}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005151EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Takashi Iwaid5191e52009-11-16 14:58:17 +01005153/**
5154 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005155 * @codec: the HDA codec
5156 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005158int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5159 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
Takashi Iwaidda14412011-05-02 11:29:30 +02005161 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 int i;
5163
5164 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005165 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005167 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005168 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5169 if (mout->hp_out_nid[i])
5170 snd_hda_codec_cleanup_stream(codec,
5171 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005172 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5173 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005174 snd_hda_codec_cleanup_stream(codec,
5175 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005176 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005178 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 mout->dig_out_used = 0;
5180 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005181 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 return 0;
5183}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005184EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Takashi Iwai47408602012-04-20 13:06:53 +02005186/**
5187 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005188 * @codec: the HDA codec
5189 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005190 *
5191 * Guess the suitable VREF pin bits to be set as the pin-control value.
5192 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5193 */
5194unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5195{
5196 unsigned int pincap;
5197 unsigned int oldval;
5198 oldval = snd_hda_codec_read(codec, pin, 0,
5199 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5200 pincap = snd_hda_query_pin_caps(codec, pin);
5201 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5202 /* Exception: if the default pin setup is vref50, we give it priority */
5203 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5204 return AC_PINCTL_VREF_80;
5205 else if (pincap & AC_PINCAP_VREF_50)
5206 return AC_PINCTL_VREF_50;
5207 else if (pincap & AC_PINCAP_VREF_100)
5208 return AC_PINCTL_VREF_100;
5209 else if (pincap & AC_PINCAP_VREF_GRD)
5210 return AC_PINCTL_VREF_GRD;
5211 return AC_PINCTL_VREF_HIZ;
5212}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005213EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005214
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005215/**
5216 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5217 * @codec: the HDA codec
5218 * @pin: referred pin NID
5219 * @val: pin ctl value to audit
5220 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005221unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5222 hda_nid_t pin, unsigned int val)
5223{
5224 static unsigned int cap_lists[][2] = {
5225 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5226 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5227 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5228 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5229 };
5230 unsigned int cap;
5231
5232 if (!val)
5233 return 0;
5234 cap = snd_hda_query_pin_caps(codec, pin);
5235 if (!cap)
5236 return val; /* don't know what to do... */
5237
5238 if (val & AC_PINCTL_OUT_EN) {
5239 if (!(cap & AC_PINCAP_OUT))
5240 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5241 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5242 val &= ~AC_PINCTL_HP_EN;
5243 }
5244
5245 if (val & AC_PINCTL_IN_EN) {
5246 if (!(cap & AC_PINCAP_IN))
5247 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5248 else {
5249 unsigned int vcap, vref;
5250 int i;
5251 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5252 vref = val & AC_PINCTL_VREFEN;
5253 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5254 if (vref == cap_lists[i][0] &&
5255 !(vcap & cap_lists[i][1])) {
5256 if (i == ARRAY_SIZE(cap_lists) - 1)
5257 vref = AC_PINCTL_VREF_HIZ;
5258 else
5259 vref = cap_lists[i + 1][0];
5260 }
5261 }
5262 val &= ~AC_PINCTL_VREFEN;
5263 val |= vref;
5264 }
5265 }
5266
5267 return val;
5268}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005269EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005270
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005271/**
5272 * _snd_hda_pin_ctl - Helper to set pin ctl value
5273 * @codec: the HDA codec
5274 * @pin: referred pin NID
5275 * @val: pin control value to set
5276 * @cached: access over codec pinctl cache or direct write
5277 *
5278 * This function is a helper to set a pin ctl value more safely.
5279 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5280 * value in pin target array via snd_hda_codec_set_pin_target(), then
5281 * actually writes the value via either snd_hda_codec_update_cache() or
5282 * snd_hda_codec_write() depending on @cached flag.
5283 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005284int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5285 unsigned int val, bool cached)
5286{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005287 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005288 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005289 if (cached)
5290 return snd_hda_codec_update_cache(codec, pin, 0,
5291 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5292 else
5293 return snd_hda_codec_write(codec, pin, 0,
5294 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5295}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005296EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005297
Takashi Iwai990061c2010-09-09 22:08:44 +02005298/**
5299 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005300 * @codec: the HDA codec
5301 * @imux: imux helper object
5302 * @label: the name of imux item to assign
5303 * @index: index number of imux item to assign
5304 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005305 *
5306 * When the same label is used already in the existing items, the number
5307 * suffix is appended to the label. This label index number is stored
5308 * to type_idx when non-NULL pointer is given.
5309 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005310int snd_hda_add_imux_item(struct hda_codec *codec,
5311 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005312 int index, int *type_idx)
5313{
5314 int i, label_idx = 0;
5315 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005316 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005317 return -EINVAL;
5318 }
5319 for (i = 0; i < imux->num_items; i++) {
5320 if (!strncmp(label, imux->items[i].label, strlen(label)))
5321 label_idx++;
5322 }
5323 if (type_idx)
5324 *type_idx = label_idx;
5325 if (label_idx > 0)
5326 snprintf(imux->items[imux->num_items].label,
5327 sizeof(imux->items[imux->num_items].label),
5328 "%s %d", label, label_idx);
5329 else
5330 strlcpy(imux->items[imux->num_items].label, label,
5331 sizeof(imux->items[imux->num_items].label));
5332 imux->items[imux->num_items].index = index;
5333 imux->num_items++;
5334 return 0;
5335}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005336EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005339 * snd_hda_bus_reset - Reset the bus
5340 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005342void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005344 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Takashi Iwai0ba21762007-04-16 11:29:14 +02005346 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005347 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005348 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005349#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005350 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005351 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005352 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005353 }
5354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005357EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005358
5359/*
5360 * generic arrays
5361 */
5362
Takashi Iwaid5191e52009-11-16 14:58:17 +01005363/**
5364 * snd_array_new - get a new element from the given array
5365 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005366 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005367 * Get a new element from the given array. If it exceeds the
5368 * pre-allocated array size, re-allocate the array.
5369 *
5370 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005371 */
5372void *snd_array_new(struct snd_array *array)
5373{
Takashi Iwai12f17712012-10-10 08:59:14 +02005374 if (snd_BUG_ON(!array->elem_size))
5375 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005376 if (array->used >= array->alloced) {
5377 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005378 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005379 void *nlist;
5380 if (snd_BUG_ON(num >= 4096))
5381 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005382 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005383 if (!nlist)
5384 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005385 array->list = nlist;
5386 array->alloced = num;
5387 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005388 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005389}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005390EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005391
Takashi Iwaid5191e52009-11-16 14:58:17 +01005392/**
5393 * snd_array_free - free the given array elements
5394 * @array: the array object
5395 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005396void snd_array_free(struct snd_array *array)
5397{
5398 kfree(array->list);
5399 array->used = 0;
5400 array->alloced = 0;
5401 array->list = NULL;
5402}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005403EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005404
Takashi Iwaid5191e52009-11-16 14:58:17 +01005405/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005406 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5407 * @pcm: PCM caps bits
5408 * @buf: the string buffer to write
5409 * @buflen: the max buffer length
5410 *
5411 * used by hda_proc.c and hda_eld.c
5412 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005413void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5414{
5415 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5416 int i, j;
5417
5418 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5419 if (pcm & (AC_SUPPCM_BITS_8 << i))
5420 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5421
5422 buf[j] = '\0'; /* necessary when j == 0 */
5423}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005424EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005425
5426MODULE_DESCRIPTION("HDA codec core");
5427MODULE_LICENSE("GPL");