blob: 6fecf57c8d7c5a8f43bdf61d57f0005029bd48cd [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 Iwai63e51fd72013-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 Iwai63e51fd72013-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 Warren7c935972011-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
1505 if (codec->pcm_format_first)
1506 update_pcm_format(codec, p, nid, format);
1507 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1508 if (!codec->pcm_format_first)
1509 update_pcm_format(codec, p, nid, format);
1510
Takashi Iwaieb541332010-08-06 13:48:11 +02001511 p->active = 1;
1512 p->dirty = 0;
1513
1514 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001515 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001516 list_for_each_entry(c, &codec->bus->codec_list, list) {
1517 for (i = 0; i < c->cvt_setups.used; i++) {
1518 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001519 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001520 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001521 p->dirty = 1;
1522 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001525EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Takashi Iwaif0cea792010-08-13 11:56:53 +02001527static void really_cleanup_stream(struct hda_codec *codec,
1528 struct hda_cvt_setup *q);
1529
Takashi Iwaid5191e52009-11-16 14:58:17 +01001530/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001531 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001532 * @codec: the CODEC to clean up
1533 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001534 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001535 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001536void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1537 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001538{
Takashi Iwaieb541332010-08-06 13:48:11 +02001539 struct hda_cvt_setup *p;
1540
Takashi Iwai888afa12008-03-18 09:57:50 +01001541 if (!nid)
1542 return;
1543
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001544 if (codec->no_sticky_stream)
1545 do_now = 1;
1546
Takashi Iwai4e76a882014-02-25 12:21:03 +01001547 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001548 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001549 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001550 /* here we just clear the active flag when do_now isn't set;
1551 * actual clean-ups will be done later in
1552 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1553 */
1554 if (do_now)
1555 really_cleanup_stream(codec, p);
1556 else
1557 p->active = 0;
1558 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001559}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001560EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001561
Takashi Iwaieb541332010-08-06 13:48:11 +02001562static void really_cleanup_stream(struct hda_codec *codec,
1563 struct hda_cvt_setup *q)
1564{
1565 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001566 if (q->stream_tag || q->channel_id)
1567 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1568 if (q->format_id)
1569 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1570);
Takashi Iwaieb541332010-08-06 13:48:11 +02001571 memset(q, 0, sizeof(*q));
1572 q->nid = nid;
1573}
1574
1575/* clean up the all conflicting obsolete streams */
1576static void purify_inactive_streams(struct hda_codec *codec)
1577{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001578 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001579 int i;
1580
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001581 list_for_each_entry(c, &codec->bus->codec_list, list) {
1582 for (i = 0; i < c->cvt_setups.used; i++) {
1583 struct hda_cvt_setup *p;
1584 p = snd_array_elem(&c->cvt_setups, i);
1585 if (p->dirty)
1586 really_cleanup_stream(c, p);
1587 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001588 }
1589}
1590
Takashi Iwai2a439522011-07-26 09:52:50 +02001591#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001592/* clean up all streams; called from suspend */
1593static void hda_cleanup_all_streams(struct hda_codec *codec)
1594{
1595 int i;
1596
1597 for (i = 0; i < codec->cvt_setups.used; i++) {
1598 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1599 if (p->stream_tag)
1600 really_cleanup_stream(codec, p);
1601 }
1602}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001603#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605/*
1606 * amp access functions
1607 */
1608
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001609/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001610#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001611#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001612#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1613#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001615#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001618static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001619 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwai01751f52007-08-10 16:59:39 +02001621 memset(cache, 0, sizeof(*cache));
1622 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001623 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001624}
1625
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001626static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001627{
Takashi Iwai603c4012008-07-30 15:01:44 +02001628 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001632static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Takashi Iwai01751f52007-08-10 16:59:39 +02001634 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1635 u16 cur = cache->hash[idx];
1636 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001639 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (info->key == key)
1641 return info;
1642 cur = info->next;
1643 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001644 return NULL;
1645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001647/* query the hash. allocate an entry if not found. */
1648static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1649 u32 key)
1650{
1651 struct hda_cache_head *info = get_hash(cache, key);
1652 if (!info) {
1653 u16 idx, cur;
1654 /* add a new hash entry */
1655 info = snd_array_new(&cache->buf);
1656 if (!info)
1657 return NULL;
1658 cur = snd_array_index(&cache->buf, info);
1659 info->key = key;
1660 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001661 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001662 idx = key % (u16)ARRAY_SIZE(cache->hash);
1663 info->next = cache->hash[idx];
1664 cache->hash[idx] = cur;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return info;
1667}
1668
Takashi Iwai01751f52007-08-10 16:59:39 +02001669/* query and allocate an amp hash entry */
1670static inline struct hda_amp_info *
1671get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1672{
1673 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1674}
1675
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001676/* overwrite the value with the key in the caps hash */
1677static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1678{
1679 struct hda_amp_info *info;
1680
1681 mutex_lock(&codec->hash_mutex);
1682 info = get_alloc_amp_hash(codec, key);
1683 if (!info) {
1684 mutex_unlock(&codec->hash_mutex);
1685 return -EINVAL;
1686 }
1687 info->amp_caps = val;
1688 info->head.val |= INFO_AMP_CAPS;
1689 mutex_unlock(&codec->hash_mutex);
1690 return 0;
1691}
1692
1693/* query the value from the caps hash; if not found, fetch the current
1694 * value from the given function and store in the hash
1695 */
1696static unsigned int
1697query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1698 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1699{
1700 struct hda_amp_info *info;
1701 unsigned int val;
1702
1703 mutex_lock(&codec->hash_mutex);
1704 info = get_alloc_amp_hash(codec, key);
1705 if (!info) {
1706 mutex_unlock(&codec->hash_mutex);
1707 return 0;
1708 }
1709 if (!(info->head.val & INFO_AMP_CAPS)) {
1710 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1711 val = func(codec, nid, dir);
1712 write_caps_hash(codec, key, val);
1713 } else {
1714 val = info->amp_caps;
1715 mutex_unlock(&codec->hash_mutex);
1716 }
1717 return val;
1718}
1719
1720static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1721 int direction)
1722{
1723 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1724 nid = codec->afg;
1725 return snd_hda_param_read(codec, nid,
1726 direction == HDA_OUTPUT ?
1727 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1728}
1729
Takashi Iwaid5191e52009-11-16 14:58:17 +01001730/**
1731 * query_amp_caps - query AMP capabilities
1732 * @codec: the HD-auio codec
1733 * @nid: the NID to query
1734 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1735 *
1736 * Query AMP capabilities for the given widget and direction.
1737 * Returns the obtained capability bits.
1738 *
1739 * When cap bits have been already read, this doesn't read again but
1740 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001742u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001744 return query_caps_hash(codec, nid, direction,
1745 HDA_HASH_KEY(nid, direction, 0),
1746 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001748EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Takashi Iwaid5191e52009-11-16 14:58:17 +01001750/**
David Henningsson861a04e2014-09-23 10:38:17 +02001751 * snd_hda_check_amp_caps - query AMP capabilities
1752 * @codec: the HD-audio codec
1753 * @nid: the NID to query
1754 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001755 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001756 *
1757 * Check whether the widget has the given amp capability for the direction.
1758 */
1759bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1760 int dir, unsigned int bits)
1761{
1762 if (!nid)
1763 return false;
1764 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1765 if (query_amp_caps(codec, nid, dir) & bits)
1766 return true;
1767 return false;
1768}
1769EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1770
1771/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772 * snd_hda_override_amp_caps - Override the AMP capabilities
1773 * @codec: the CODEC to clean up
1774 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001775 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001776 * @caps: the capability bits to set
1777 *
1778 * Override the cached AMP caps bits value by the given one.
1779 * This function is useful if the driver needs to adjust the AMP ranges,
1780 * e.g. limit to 0dB, etc.
1781 *
1782 * Returns zero if successful or a negative error code.
1783 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001784int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1785 unsigned int caps)
1786{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001787 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001788}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001789EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001790
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001791static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1792 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001793{
1794 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1795}
1796
Takashi Iwaid5191e52009-11-16 14:58:17 +01001797/**
1798 * snd_hda_query_pin_caps - Query PIN capabilities
1799 * @codec: the HD-auio codec
1800 * @nid: the NID to query
1801 *
1802 * Query PIN capabilities for the given widget.
1803 * Returns the obtained capability bits.
1804 *
1805 * When cap bits have been already read, this doesn't read again but
1806 * returns the cached value.
1807 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001808u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1809{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001810 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001811 read_pin_cap);
1812}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001813EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001814
Wu Fengguang864f92b2009-11-18 12:38:02 +08001815/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001816 * snd_hda_override_pin_caps - Override the pin capabilities
1817 * @codec: the CODEC
1818 * @nid: the NID to override
1819 * @caps: the capability bits to set
1820 *
1821 * Override the cached PIN capabilitiy bits value by the given one.
1822 *
1823 * Returns zero if successful or a negative error code.
1824 */
1825int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1826 unsigned int caps)
1827{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001828 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001829}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001830EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001831
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001832/* read or sync the hash value with the current value;
1833 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001835static struct hda_amp_info *
1836update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001837 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839 struct hda_amp_info *info;
1840 unsigned int parm, val = 0;
1841 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001843 retry:
1844 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1845 if (!info)
1846 return NULL;
1847 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1848 if (!val_read) {
1849 mutex_unlock(&codec->hash_mutex);
1850 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1851 parm |= direction == HDA_OUTPUT ?
1852 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1853 parm |= index;
1854 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001855 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001856 val &= 0xff;
1857 val_read = true;
1858 mutex_lock(&codec->hash_mutex);
1859 goto retry;
1860 }
1861 info->vol[ch] = val;
1862 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001863 } else if (init_only)
1864 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001865 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001869 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001871static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001872 hda_nid_t nid, int ch, int direction, int index,
1873 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 u32 parm;
1876
1877 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1878 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1879 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001880 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1881 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001882 ; /* set the zero value as a fake mute */
1883 else
1884 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1886}
1887
Takashi Iwaid5191e52009-11-16 14:58:17 +01001888/**
1889 * snd_hda_codec_amp_read - Read AMP value
1890 * @codec: HD-audio codec
1891 * @nid: NID to read the AMP value
1892 * @ch: channel (left=0 or right=1)
1893 * @direction: #HDA_INPUT or #HDA_OUTPUT
1894 * @index: the index value (only for input direction)
1895 *
1896 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
Takashi Iwai834be882006-03-01 14:16:17 +01001898int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1899 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001901 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001902 unsigned int val = 0;
1903
1904 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001905 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001906 if (info)
1907 val = info->vol[ch];
1908 mutex_unlock(&codec->hash_mutex);
1909 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001911EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Takashi Iwai280e57d2012-12-14 10:32:21 +01001913static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1914 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001915 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001916{
1917 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001918 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001919
1920 if (snd_BUG_ON(mask & ~0xff))
1921 mask &= 0xff;
1922 val &= mask;
1923
1924 mutex_lock(&codec->hash_mutex);
1925 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1926 if (!info) {
1927 mutex_unlock(&codec->hash_mutex);
1928 return 0;
1929 }
1930 val |= info->vol[ch] & ~mask;
1931 if (info->vol[ch] == val) {
1932 mutex_unlock(&codec->hash_mutex);
1933 return 0;
1934 }
1935 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001936 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001937 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001938 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001939 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001940 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001941 return 1;
1942}
1943
Takashi Iwaid5191e52009-11-16 14:58:17 +01001944/**
1945 * snd_hda_codec_amp_update - update the AMP value
1946 * @codec: HD-audio codec
1947 * @nid: NID to read the AMP value
1948 * @ch: channel (left=0 or right=1)
1949 * @direction: #HDA_INPUT or #HDA_OUTPUT
1950 * @idx: the index value (only for input direction)
1951 * @mask: bit mask to set
1952 * @val: the bits value to set
1953 *
1954 * Update the AMP value with a bit mask.
1955 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001956 */
Takashi Iwai834be882006-03-01 14:16:17 +01001957int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1958 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001960 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1961 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001963EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Takashi Iwaid5191e52009-11-16 14:58:17 +01001965/**
1966 * snd_hda_codec_amp_stereo - update the AMP stereo values
1967 * @codec: HD-audio codec
1968 * @nid: NID to read the AMP value
1969 * @direction: #HDA_INPUT or #HDA_OUTPUT
1970 * @idx: the index value (only for input direction)
1971 * @mask: bit mask to set
1972 * @val: the bits value to set
1973 *
1974 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1975 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001976 */
1977int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1978 int direction, int idx, int mask, int val)
1979{
1980 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001981
1982 if (snd_BUG_ON(mask & ~0xff))
1983 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001984 for (ch = 0; ch < 2; ch++)
1985 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1986 idx, mask, val);
1987 return ret;
1988}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001989EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001990
Takashi Iwai95a962c2014-10-29 16:03:58 +01001991/**
1992 * snd_hda_codec_amp_init - initialize the AMP value
1993 * @codec: the HDA codec
1994 * @nid: NID to read the AMP value
1995 * @ch: channel (left=0 or right=1)
1996 * @dir: #HDA_INPUT or #HDA_OUTPUT
1997 * @idx: the index value (only for input direction)
1998 * @mask: bit mask to set
1999 * @val: the bits value to set
2000 *
2001 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01002002 * the first access. If the amp was already initialized / updated beforehand,
2003 * this does nothing.
2004 */
2005int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2006 int dir, int idx, int mask, int val)
2007{
Takashi Iwai781c7b92015-02-20 10:26:25 +01002008 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
2009 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002010}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002011EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002012
Takashi Iwai95a962c2014-10-29 16:03:58 +01002013/**
2014 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2015 * @codec: the HDA codec
2016 * @nid: NID to read the AMP value
2017 * @dir: #HDA_INPUT or #HDA_OUTPUT
2018 * @idx: the index value (only for input direction)
2019 * @mask: bit mask to set
2020 * @val: the bits value to set
2021 *
2022 * Call snd_hda_codec_amp_init() for both stereo channels.
2023 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01002024int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2025 int dir, int idx, int mask, int val)
2026{
2027 int ch, ret = 0;
2028
2029 if (snd_BUG_ON(mask & ~0xff))
2030 mask &= 0xff;
2031 for (ch = 0; ch < 2; ch++)
2032 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2033 idx, mask, val);
2034 return ret;
2035}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002036EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002037
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038/**
2039 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2040 * @codec: HD-audio codec
2041 *
2042 * Resume the all amp commands from the cache.
2043 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002044void snd_hda_codec_resume_amp(struct hda_codec *codec)
2045{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002046 int i;
2047
Takashi Iwaic370dd62012-12-13 18:30:04 +01002048 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002049 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002050 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2051 struct hda_amp_info *buffer;
2052 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002053 hda_nid_t nid;
2054 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002055 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002056
2057 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002058 if (!buffer->head.dirty)
2059 continue;
2060 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002061 info = *buffer;
2062 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002063 if (!key)
2064 continue;
2065 nid = key & 0xff;
2066 idx = (key >> 16) & 0xff;
2067 dir = (key >> 24) & 0xff;
2068 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002069 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002070 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002071 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002072 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2073 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002074 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002075 }
2076 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002077 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002078}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002079EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002081static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2082 unsigned int ofs)
2083{
2084 u32 caps = query_amp_caps(codec, nid, dir);
2085 /* get num steps */
2086 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2087 if (ofs < caps)
2088 caps -= ofs;
2089 return caps;
2090}
2091
Takashi Iwaid5191e52009-11-16 14:58:17 +01002092/**
2093 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002094 * @kcontrol: referred ctl element
2095 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002096 *
2097 * The control element is supposed to have the private_value field
2098 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2099 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002100int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2101 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2104 u16 nid = get_amp_nid(kcontrol);
2105 u8 chs = get_amp_channels(kcontrol);
2106 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002107 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002109 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2110 uinfo->count = chs == 3 ? 2 : 1;
2111 uinfo->value.integer.min = 0;
2112 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2113 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002114 codec_warn(codec,
2115 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2116 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -EINVAL;
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 0;
2120}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002121EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002123
2124static inline unsigned int
2125read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2126 int ch, int dir, int idx, unsigned int ofs)
2127{
2128 unsigned int val;
2129 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2130 val &= HDA_AMP_VOLMASK;
2131 if (val >= ofs)
2132 val -= ofs;
2133 else
2134 val = 0;
2135 return val;
2136}
2137
2138static inline int
2139update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2140 int ch, int dir, int idx, unsigned int ofs,
2141 unsigned int val)
2142{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002143 unsigned int maxval;
2144
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002145 if (val > 0)
2146 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002147 /* ofs = 0: raw max value */
2148 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002149 if (val > maxval)
2150 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002151 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2152 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002153}
2154
Takashi Iwaid5191e52009-11-16 14:58:17 +01002155/**
2156 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002157 * @kcontrol: ctl element
2158 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002159 *
2160 * The control element is supposed to have the private_value field
2161 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2162 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002163int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2164 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2167 hda_nid_t nid = get_amp_nid(kcontrol);
2168 int chs = get_amp_channels(kcontrol);
2169 int dir = get_amp_direction(kcontrol);
2170 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002171 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 long *valp = ucontrol->value.integer.value;
2173
2174 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002175 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002177 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return 0;
2179}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002180EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Takashi Iwaid5191e52009-11-16 14:58:17 +01002182/**
2183 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002184 * @kcontrol: ctl element
2185 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002186 *
2187 * The control element is supposed to have the private_value field
2188 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2189 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002190int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2194 hda_nid_t nid = get_amp_nid(kcontrol);
2195 int chs = get_amp_channels(kcontrol);
2196 int dir = get_amp_direction(kcontrol);
2197 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002198 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 long *valp = ucontrol->value.integer.value;
2200 int change = 0;
2201
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002202 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002203 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002204 valp++;
2205 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002206 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002207 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return change;
2209}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002210EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Takashi Iwaid5191e52009-11-16 14:58:17 +01002212/**
2213 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002214 * @kcontrol: ctl element
2215 * @op_flag: operation flag
2216 * @size: byte size of input TLV
2217 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218 *
2219 * The control element is supposed to have the private_value field
2220 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2221 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002222int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2223 unsigned int size, unsigned int __user *_tlv)
2224{
2225 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2226 hda_nid_t nid = get_amp_nid(kcontrol);
2227 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002228 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002229 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002230 u32 caps, val1, val2;
2231
2232 if (size < 4 * sizeof(unsigned int))
2233 return -ENOMEM;
2234 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002235 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2236 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002237 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002238 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002239 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002240 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002241 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002242 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2243 return -EFAULT;
2244 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2245 return -EFAULT;
2246 if (put_user(val1, _tlv + 2))
2247 return -EFAULT;
2248 if (put_user(val2, _tlv + 3))
2249 return -EFAULT;
2250 return 0;
2251}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002252EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002253
Takashi Iwaid5191e52009-11-16 14:58:17 +01002254/**
2255 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2256 * @codec: HD-audio codec
2257 * @nid: NID of a reference widget
2258 * @dir: #HDA_INPUT or #HDA_OUTPUT
2259 * @tlv: TLV data to be stored, at least 4 elements
2260 *
2261 * Set (static) TLV data for a virtual master volume using the AMP caps
2262 * obtained from the reference NID.
2263 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002264 */
2265void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2266 unsigned int *tlv)
2267{
2268 u32 caps;
2269 int nums, step;
2270
2271 caps = query_amp_caps(codec, nid, dir);
2272 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2273 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2274 step = (step + 1) * 25;
2275 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2276 tlv[1] = 2 * sizeof(unsigned int);
2277 tlv[2] = -nums * step;
2278 tlv[3] = step;
2279}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002280EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002281
2282/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002283static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002284find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002285{
2286 struct snd_ctl_elem_id id;
2287 memset(&id, 0, sizeof(id));
2288 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002289 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002290 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002291 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2292 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002293 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002294 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002295}
2296
Takashi Iwaid5191e52009-11-16 14:58:17 +01002297/**
2298 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2299 * @codec: HD-audio codec
2300 * @name: ctl id name string
2301 *
2302 * Get the control element with the given id string and IFACE_MIXER.
2303 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002304struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2305 const char *name)
2306{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002307 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002308}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002309EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002310
Takashi Iwaidcda5802012-10-12 17:24:51 +02002311static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002312 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002313{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002314 int i, idx;
2315 /* 16 ctlrs should be large enough */
2316 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2317 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002318 return idx;
2319 }
2320 return -EBUSY;
2321}
2322
Takashi Iwaid5191e52009-11-16 14:58:17 +01002323/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002324 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002325 * @codec: HD-audio codec
2326 * @nid: corresponding NID (optional)
2327 * @kctl: the control element to assign
2328 *
2329 * Add the given control element to an array inside the codec instance.
2330 * All control elements belonging to a codec are supposed to be added
2331 * by this function so that a proper clean-up works at the free or
2332 * reconfiguration time.
2333 *
2334 * If non-zero @nid is passed, the NID is assigned to the control element.
2335 * The assignment is shown in the codec proc file.
2336 *
2337 * snd_hda_ctl_add() checks the control subdev id field whether
2338 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002339 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2340 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002341 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002342int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2343 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002344{
2345 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002346 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002347 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002349 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002350 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002351 if (nid == 0)
2352 nid = get_amp_nid_(kctl->private_value);
2353 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002354 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2355 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002356 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002357 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002358 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002359 if (err < 0)
2360 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002361 item = snd_array_new(&codec->mixers);
2362 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002363 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002364 item->kctl = kctl;
2365 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002366 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002367 return 0;
2368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002369EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002370
Takashi Iwaid5191e52009-11-16 14:58:17 +01002371/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002372 * snd_hda_add_nid - Assign a NID to a control element
2373 * @codec: HD-audio codec
2374 * @nid: corresponding NID (optional)
2375 * @kctl: the control element to assign
2376 * @index: index to kctl
2377 *
2378 * Add the given control element to an array inside the codec instance.
2379 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2380 * NID:KCTL mapping - for example "Capture Source" selector.
2381 */
2382int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2383 unsigned int index, hda_nid_t nid)
2384{
2385 struct hda_nid_item *item;
2386
2387 if (nid > 0) {
2388 item = snd_array_new(&codec->nids);
2389 if (!item)
2390 return -ENOMEM;
2391 item->kctl = kctl;
2392 item->index = index;
2393 item->nid = nid;
2394 return 0;
2395 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002396 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2397 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002398 return -EINVAL;
2399}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002400EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002401
2402/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002403 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2404 * @codec: HD-audio codec
2405 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002406void snd_hda_ctls_clear(struct hda_codec *codec)
2407{
2408 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002409 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002410 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002411 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002412 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002413 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002414}
2415
Takashi Iwai95a962c2014-10-29 16:03:58 +01002416/**
2417 * snd_hda_lock_devices - pseudo device locking
2418 * @bus: the BUS
2419 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002420 * toggle card->shutdown to allow/disallow the device access (as a hack)
2421 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002422int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002423{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002424 struct snd_card *card = bus->card;
2425 struct hda_codec *codec;
2426
Takashi Iwaia65d6292009-02-23 16:57:04 +01002427 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002428 if (card->shutdown)
2429 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002430 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002431 if (!list_empty(&card->ctl_files))
2432 goto err_clear;
2433
2434 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002435 struct hda_pcm *cpcm;
2436 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002437 if (!cpcm->pcm)
2438 continue;
2439 if (cpcm->pcm->streams[0].substream_opened ||
2440 cpcm->pcm->streams[1].substream_opened)
2441 goto err_clear;
2442 }
2443 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002444 spin_unlock(&card->files_lock);
2445 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002446
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002447 err_clear:
2448 card->shutdown = 0;
2449 err_unlock:
2450 spin_unlock(&card->files_lock);
2451 return -EINVAL;
2452}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002453EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002454
Takashi Iwai95a962c2014-10-29 16:03:58 +01002455/**
2456 * snd_hda_unlock_devices - pseudo device unlocking
2457 * @bus: the BUS
2458 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002459void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002460{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002461 struct snd_card *card = bus->card;
2462
Takashi Iwaia65d6292009-02-23 16:57:04 +01002463 spin_lock(&card->files_lock);
2464 card->shutdown = 0;
2465 spin_unlock(&card->files_lock);
2466}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002467EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002468
Takashi Iwaid5191e52009-11-16 14:58:17 +01002469/**
2470 * snd_hda_codec_reset - Clear all objects assigned to the codec
2471 * @codec: HD-audio codec
2472 *
2473 * This frees the all PCM and control elements assigned to the codec, and
2474 * clears the caches and restores the pin default configurations.
2475 *
2476 * When a device is being used, it returns -EBSY. If successfully freed,
2477 * returns zero.
2478 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002479int snd_hda_codec_reset(struct hda_codec *codec)
2480{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002481 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002482
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002483 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002484 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002485
2486 /* OK, let it free */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002487 if (device_is_registered(hda_codec_dev(codec)))
2488 device_del(hda_codec_dev(codec));
2489
Takashi Iwaia65d6292009-02-23 16:57:04 +01002490 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002491 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002492 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002493}
2494
Takashi Iwai6194b992014-06-06 18:12:16 +02002495typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002496
2497/* apply the function to all matching slave ctls in the mixer list */
2498static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002499 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002500{
2501 struct hda_nid_item *items;
2502 const char * const *s;
2503 int i, err;
2504
2505 items = codec->mixers.list;
2506 for (i = 0; i < codec->mixers.used; i++) {
2507 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002508 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002509 continue;
2510 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002511 char tmpname[sizeof(sctl->id.name)];
2512 const char *name = *s;
2513 if (suffix) {
2514 snprintf(tmpname, sizeof(tmpname), "%s %s",
2515 name, suffix);
2516 name = tmpname;
2517 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002518 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002519 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002520 if (err)
2521 return err;
2522 break;
2523 }
2524 }
2525 }
2526 return 0;
2527}
2528
Takashi Iwai6194b992014-06-06 18:12:16 +02002529static int check_slave_present(struct hda_codec *codec,
2530 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002531{
2532 return 1;
2533}
2534
Takashi Iwai18478e82012-03-09 17:51:10 +01002535/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002536static int get_kctl_0dB_offset(struct hda_codec *codec,
2537 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002538{
2539 int _tlv[4];
2540 const int *tlv = NULL;
2541 int val = -1;
2542
2543 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2544 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2545 mm_segment_t fs = get_fs();
2546 set_fs(get_ds());
2547 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2548 tlv = _tlv;
2549 set_fs(fs);
2550 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2551 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002552 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2553 int step = tlv[3];
2554 step &= ~TLV_DB_SCALE_MUTE;
2555 if (!step)
2556 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002557 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002558 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002559- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002560 return -1;
2561 }
2562 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002563 val = -tlv[2] / step;
2564 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002565 return val;
2566}
2567
2568/* call kctl->put with the given value(s) */
2569static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2570{
2571 struct snd_ctl_elem_value *ucontrol;
2572 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2573 if (!ucontrol)
2574 return -ENOMEM;
2575 ucontrol->value.integer.value[0] = val;
2576 ucontrol->value.integer.value[1] = val;
2577 kctl->put(kctl, ucontrol);
2578 kfree(ucontrol);
2579 return 0;
2580}
2581
2582/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002583static int init_slave_0dB(struct hda_codec *codec,
2584 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002585{
Takashi Iwai6194b992014-06-06 18:12:16 +02002586 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002587 if (offset > 0)
2588 put_kctl_with_value(slave, offset);
2589 return 0;
2590}
2591
2592/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002593static int init_slave_unmute(struct hda_codec *codec,
2594 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002595{
2596 return put_kctl_with_value(slave, 1);
2597}
2598
Takashi Iwaie8750942014-06-30 14:02:39 +02002599static int add_slave(struct hda_codec *codec,
2600 void *data, struct snd_kcontrol *slave)
2601{
2602 return snd_ctl_add_slave(data, slave);
2603}
2604
Takashi Iwaid5191e52009-11-16 14:58:17 +01002605/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002606 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002607 * @codec: HD-audio codec
2608 * @name: vmaster control name
2609 * @tlv: TLV data (optional)
2610 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002611 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002612 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002613 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002614 *
2615 * Create a virtual master control with the given name. The TLV data
2616 * must be either NULL or a valid data.
2617 *
2618 * @slaves is a NULL-terminated array of strings, each of which is a
2619 * slave control name. All controls with these names are assigned to
2620 * the new virtual master control.
2621 *
2622 * This function returns zero if successful or a negative error code.
2623 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002624int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002625 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002626 const char *suffix, bool init_slave_vol,
2627 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002628{
2629 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002630 int err;
2631
Takashi Iwai29e58532012-03-12 12:25:03 +01002632 if (ctl_ret)
2633 *ctl_ret = NULL;
2634
Takashi Iwai9322ca52012-02-03 14:28:01 +01002635 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002636 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002637 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002638 return 0;
2639 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002640 kctl = snd_ctl_make_virtual_master(name, tlv);
2641 if (!kctl)
2642 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002643 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002644 if (err < 0)
2645 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002646
Takashi Iwaie8750942014-06-30 14:02:39 +02002647 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002648 if (err < 0)
2649 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002650
2651 /* init with master mute & zero volume */
2652 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002653 if (init_slave_vol) {
2654 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002655 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002656 tlv ? init_slave_0dB : init_slave_unmute, &step);
2657 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002658
Takashi Iwai29e58532012-03-12 12:25:03 +01002659 if (ctl_ret)
2660 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002661 return 0;
2662}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002663EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002664
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002665/*
2666 * mute-LED control using vmaster
2667 */
2668static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_info *uinfo)
2670{
2671 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002672 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002673 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002674
Takashi Iwai3ff72212014-10-20 18:17:28 +02002675 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002676}
2677
2678static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_value *ucontrol)
2680{
2681 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2682 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2683 return 0;
2684}
2685
2686static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2688{
2689 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2690 unsigned int old_mode = hook->mute_mode;
2691
2692 hook->mute_mode = ucontrol->value.enumerated.item[0];
2693 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2694 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2695 if (old_mode == hook->mute_mode)
2696 return 0;
2697 snd_hda_sync_vmaster_hook(hook);
2698 return 1;
2699}
2700
2701static struct snd_kcontrol_new vmaster_mute_mode = {
2702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2703 .name = "Mute-LED Mode",
2704 .info = vmaster_mute_mode_info,
2705 .get = vmaster_mute_mode_get,
2706 .put = vmaster_mute_mode_put,
2707};
2708
Takashi Iwai95a962c2014-10-29 16:03:58 +01002709/**
2710 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2711 * @codec: the HDA codec
2712 * @hook: the vmaster hook object
2713 * @expose_enum_ctl: flag to create an enum ctl
2714 *
2715 * Add a mute-LED hook with the given vmaster switch kctl.
2716 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2717 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002718 */
2719int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002720 struct hda_vmaster_mute_hook *hook,
2721 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002722{
2723 struct snd_kcontrol *kctl;
2724
2725 if (!hook->hook || !hook->sw_kctl)
2726 return 0;
2727 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2728 hook->codec = codec;
2729 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002730 if (!expose_enum_ctl)
2731 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002732 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2733 if (!kctl)
2734 return -ENOMEM;
2735 return snd_hda_ctl_add(codec, 0, kctl);
2736}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002737EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002738
Takashi Iwai95a962c2014-10-29 16:03:58 +01002739/**
2740 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2741 * @hook: the vmaster hook
2742 *
2743 * Call the hook with the current value for synchronization.
2744 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002745 */
2746void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2747{
2748 if (!hook->hook || !hook->codec)
2749 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002750 /* don't call vmaster hook in the destructor since it might have
2751 * been already destroyed
2752 */
2753 if (hook->codec->bus->shutdown)
2754 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002755 switch (hook->mute_mode) {
2756 case HDA_VMUTE_FOLLOW_MASTER:
2757 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2758 break;
2759 default:
2760 hook->hook(hook->codec, hook->mute_mode);
2761 break;
2762 }
2763}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002764EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002765
2766
Takashi Iwaid5191e52009-11-16 14:58:17 +01002767/**
2768 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002769 * @kcontrol: referred ctl element
2770 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002771 *
2772 * The control element is supposed to have the private_value field
2773 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002775int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 int chs = get_amp_channels(kcontrol);
2779
2780 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2781 uinfo->count = chs == 3 ? 2 : 1;
2782 uinfo->value.integer.min = 0;
2783 uinfo->value.integer.max = 1;
2784 return 0;
2785}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002786EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Takashi Iwaid5191e52009-11-16 14:58:17 +01002788/**
2789 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002790 * @kcontrol: ctl element
2791 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002792 *
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2795 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002796int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 hda_nid_t nid = get_amp_nid(kcontrol);
2801 int chs = get_amp_channels(kcontrol);
2802 int dir = get_amp_direction(kcontrol);
2803 int idx = get_amp_index(kcontrol);
2804 long *valp = ucontrol->value.integer.value;
2805
2806 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002808 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002810 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002811 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return 0;
2813}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002814EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Takashi Iwaid5191e52009-11-16 14:58:17 +01002816/**
2817 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002818 * @kcontrol: ctl element
2819 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002820 *
2821 * The control element is supposed to have the private_value field
2822 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2823 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002824int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2828 hda_nid_t nid = get_amp_nid(kcontrol);
2829 int chs = get_amp_channels(kcontrol);
2830 int dir = get_amp_direction(kcontrol);
2831 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 long *valp = ucontrol->value.integer.value;
2833 int change = 0;
2834
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002835 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002836 change = codec_amp_update(codec, nid, 0, dir, idx,
2837 HDA_AMP_MUTE,
2838 *valp ? 0 : HDA_AMP_MUTE, false,
2839 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002840 valp++;
2841 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002842 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002843 change |= codec_amp_update(codec, nid, 1, dir, idx,
2844 HDA_AMP_MUTE,
2845 *valp ? 0 : HDA_AMP_MUTE, false,
2846 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002847 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return change;
2849}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002850EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852/*
Takashi Iwai985be542005-11-02 18:26:49 +01002853 * bound volume controls
2854 *
2855 * bind multiple volumes (# indices, from 0)
2856 */
2857
2858#define AMP_VAL_IDX_SHIFT 19
2859#define AMP_VAL_IDX_MASK (0x0f<<19)
2860
Takashi Iwaid5191e52009-11-16 14:58:17 +01002861/**
2862 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002863 * @kcontrol: ctl element
2864 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002865 *
2866 * The control element is supposed to have the private_value field
2867 * set up via HDA_BIND_MUTE*() macros.
2868 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002869int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002871{
2872 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2873 unsigned long pval;
2874 int err;
2875
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002876 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002877 pval = kcontrol->private_value;
2878 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2879 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2880 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002881 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002882 return err;
2883}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002884EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002885
Takashi Iwaid5191e52009-11-16 14:58:17 +01002886/**
2887 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002888 * @kcontrol: ctl element
2889 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002890 *
2891 * The control element is supposed to have the private_value field
2892 * set up via HDA_BIND_MUTE*() macros.
2893 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002894int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002896{
2897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2898 unsigned long pval;
2899 int i, indices, err = 0, change = 0;
2900
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002901 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002902 pval = kcontrol->private_value;
2903 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2904 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002905 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2906 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002907 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2908 if (err < 0)
2909 break;
2910 change |= err;
2911 }
2912 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002913 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002914 return err < 0 ? err : change;
2915}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002916EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002917
Takashi Iwaid5191e52009-11-16 14:58:17 +01002918/**
2919 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002920 * @kcontrol: referred ctl element
2921 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002922 *
2923 * The control element is supposed to have the private_value field
2924 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002925 */
2926int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2927 struct snd_ctl_elem_info *uinfo)
2928{
2929 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2930 struct hda_bind_ctls *c;
2931 int err;
2932
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002933 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002934 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002935 kcontrol->private_value = *c->values;
2936 err = c->ops->info(kcontrol, uinfo);
2937 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002938 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002939 return err;
2940}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002941EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002942
Takashi Iwaid5191e52009-11-16 14:58:17 +01002943/**
2944 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002945 * @kcontrol: ctl element
2946 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002947 *
2948 * The control element is supposed to have the private_value field
2949 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2950 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002951int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2955 struct hda_bind_ctls *c;
2956 int err;
2957
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002958 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002959 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002960 kcontrol->private_value = *c->values;
2961 err = c->ops->get(kcontrol, ucontrol);
2962 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002963 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002964 return err;
2965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002966EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002967
Takashi Iwaid5191e52009-11-16 14:58:17 +01002968/**
2969 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002970 * @kcontrol: ctl element
2971 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002972 *
2973 * The control element is supposed to have the private_value field
2974 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2975 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002976int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2977 struct snd_ctl_elem_value *ucontrol)
2978{
2979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2980 struct hda_bind_ctls *c;
2981 unsigned long *vals;
2982 int err = 0, change = 0;
2983
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002984 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002985 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002986 for (vals = c->values; *vals; vals++) {
2987 kcontrol->private_value = *vals;
2988 err = c->ops->put(kcontrol, ucontrol);
2989 if (err < 0)
2990 break;
2991 change |= err;
2992 }
2993 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002994 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002995 return err < 0 ? err : change;
2996}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002997EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002998
Takashi Iwaid5191e52009-11-16 14:58:17 +01002999/**
3000 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003001 * @kcontrol: ctl element
3002 * @op_flag: operation flag
3003 * @size: byte size of input TLV
3004 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003005 *
3006 * The control element is supposed to have the private_value field
3007 * set up via HDA_BIND_VOL() macro.
3008 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003009int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3010 unsigned int size, unsigned int __user *tlv)
3011{
3012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3013 struct hda_bind_ctls *c;
3014 int err;
3015
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003016 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003017 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003018 kcontrol->private_value = *c->values;
3019 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3020 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003021 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003022 return err;
3023}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003024EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003025
3026struct hda_ctl_ops snd_hda_bind_vol = {
3027 .info = snd_hda_mixer_amp_volume_info,
3028 .get = snd_hda_mixer_amp_volume_get,
3029 .put = snd_hda_mixer_amp_volume_put,
3030 .tlv = snd_hda_mixer_amp_tlv
3031};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003032EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003033
3034struct hda_ctl_ops snd_hda_bind_sw = {
3035 .info = snd_hda_mixer_amp_switch_info,
3036 .get = snd_hda_mixer_amp_switch_get,
3037 .put = snd_hda_mixer_amp_switch_put,
3038 .tlv = snd_hda_mixer_amp_tlv
3039};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003040EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003041
3042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 * SPDIF out controls
3044 */
3045
Takashi Iwai0ba21762007-04-16 11:29:14 +02003046static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3047 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3050 uinfo->count = 1;
3051 return 0;
3052}
3053
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3055 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3058 IEC958_AES0_NONAUDIO |
3059 IEC958_AES0_CON_EMPHASIS_5015 |
3060 IEC958_AES0_CON_NOT_COPYRIGHT;
3061 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3062 IEC958_AES1_CON_ORIGINAL;
3063 return 0;
3064}
3065
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3070 IEC958_AES0_NONAUDIO |
3071 IEC958_AES0_PRO_EMPHASIS_5015;
3072 return 0;
3073}
3074
Takashi Iwai0ba21762007-04-16 11:29:14 +02003075static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003079 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003080 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003082 mutex_lock(&codec->spdif_mutex);
3083 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003084 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3085 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3086 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3087 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003088 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 return 0;
3091}
3092
3093/* convert from SPDIF status bits to HDA SPDIF bits
3094 * bit 0 (DigEn) is always set zero (to be filled later)
3095 */
3096static unsigned short convert_from_spdif_status(unsigned int sbits)
3097{
3098 unsigned short val = 0;
3099
3100 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3106 IEC958_AES0_PRO_EMPHASIS_5015)
3107 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3110 IEC958_AES0_CON_EMPHASIS_5015)
3111 val |= AC_DIG1_EMPHASIS;
3112 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3113 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3117 }
3118 return val;
3119}
3120
3121/* convert to SPDIF status bits from HDA SPDIF bits
3122 */
3123static unsigned int convert_to_spdif_status(unsigned short val)
3124{
3125 unsigned int sbits = 0;
3126
Takashi Iwai0ba21762007-04-16 11:29:14 +02003127 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003129 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 sbits |= IEC958_AES0_PROFESSIONAL;
3131 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003132 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3134 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003135 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003137 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003139 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3141 sbits |= val & (0x7f << 8);
3142 }
3143 return sbits;
3144}
3145
Takashi Iwai2f728532008-09-25 16:32:41 +02003146/* set digital convert verbs both for the given NID and its slaves */
3147static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3148 int verb, int val)
3149{
Takashi Iwaidda14412011-05-02 11:29:30 +02003150 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003151
Takashi Iwai9e976972008-11-25 08:17:20 +01003152 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003153 d = codec->slave_dig_outs;
3154 if (!d)
3155 return;
3156 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003157 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003158}
3159
3160static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3161 int dig1, int dig2)
3162{
3163 if (dig1 != -1)
3164 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3165 if (dig2 != -1)
3166 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3167}
3168
Takashi Iwai0ba21762007-04-16 11:29:14 +02003169static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3170 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003173 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003174 struct hda_spdif_out *spdif;
3175 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 unsigned short val;
3177 int change;
3178
Ingo Molnar62932df2006-01-16 16:34:20 +01003179 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003180 spdif = snd_array_elem(&codec->spdif_out, idx);
3181 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003182 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3184 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3185 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003186 val = convert_from_spdif_status(spdif->status);
3187 val |= spdif->ctls & 1;
3188 change = spdif->ctls != val;
3189 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003190 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003191 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003192 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return change;
3194}
3195
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003196#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Takashi Iwai0ba21762007-04-16 11:29:14 +02003198static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003202 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003203 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003205 mutex_lock(&codec->spdif_mutex);
3206 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003207 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003208 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return 0;
3210}
3211
Stephen Warren74b654c2011-06-01 11:14:18 -06003212static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3213 int dig1, int dig2)
3214{
3215 set_dig_out_convert(codec, nid, dig1, dig2);
3216 /* unmute amp switch (if any) */
3217 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3218 (dig1 & AC_DIG1_ENABLE))
3219 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3220 HDA_AMP_MUTE, 0);
3221}
3222
Takashi Iwai0ba21762007-04-16 11:29:14 +02003223static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003227 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003228 struct hda_spdif_out *spdif;
3229 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 unsigned short val;
3231 int change;
3232
Ingo Molnar62932df2006-01-16 16:34:20 +01003233 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003234 spdif = snd_array_elem(&codec->spdif_out, idx);
3235 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003236 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003238 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003239 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003240 spdif->ctls = val;
3241 if (change && nid != (u16)-1)
3242 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003243 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return change;
3245}
3246
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003247static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 {
3249 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3250 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003251 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 .info = snd_hda_spdif_mask_info,
3253 .get = snd_hda_spdif_cmask_get,
3254 },
3255 {
3256 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3257 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003258 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 .info = snd_hda_spdif_mask_info,
3260 .get = snd_hda_spdif_pmask_get,
3261 },
3262 {
3263 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003264 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 .info = snd_hda_spdif_mask_info,
3266 .get = snd_hda_spdif_default_get,
3267 .put = snd_hda_spdif_default_put,
3268 },
3269 {
3270 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003271 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .info = snd_hda_spdif_out_switch_info,
3273 .get = snd_hda_spdif_out_switch_get,
3274 .put = snd_hda_spdif_out_switch_put,
3275 },
3276 { } /* end */
3277};
3278
3279/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003280 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003282 * @associated_nid: NID that new ctls associated with
3283 * @cvt_nid: converter NID
3284 * @type: HDA_PCM_TYPE_*
3285 * Creates controls related with the digital output.
3286 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *
3288 * Returns 0 if successful, or a negative error code.
3289 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003290int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3291 hda_nid_t associated_nid,
3292 hda_nid_t cvt_nid,
3293 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
3295 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003296 struct snd_kcontrol *kctl;
3297 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003298 int idx = 0;
3299 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003300 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003301 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003303 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003304 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003305 idx = spdif_index;
3306 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003307 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003308 /* suppose a single SPDIF device */
3309 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3310 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3311 if (!kctl)
3312 break;
3313 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003314 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003315 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003316 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003317 if (!bus->primary_dig_out_type)
3318 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003319
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003320 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003321 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003322 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003323 return -EBUSY;
3324 }
Stephen Warren7c935972011-06-01 11:14:17 -06003325 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003326 if (!spdif)
3327 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3329 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003330 if (!kctl)
3331 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003332 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003333 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003334 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003335 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return err;
3337 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003338 spdif->nid = cvt_nid;
3339 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003340 AC_VERB_GET_DIGI_CONVERT_1, 0);
3341 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return 0;
3343}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003344EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Takashi Iwai95a962c2014-10-29 16:03:58 +01003346/**
3347 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3348 * @codec: the HDA codec
3349 * @nid: widget NID
3350 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003351 * call within spdif_mutex lock
3352 */
Stephen Warren7c935972011-06-01 11:14:17 -06003353struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3354 hda_nid_t nid)
3355{
3356 int i;
3357 for (i = 0; i < codec->spdif_out.used; i++) {
3358 struct hda_spdif_out *spdif =
3359 snd_array_elem(&codec->spdif_out, i);
3360 if (spdif->nid == nid)
3361 return spdif;
3362 }
3363 return NULL;
3364}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003365EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003366
Takashi Iwai95a962c2014-10-29 16:03:58 +01003367/**
3368 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3369 * @codec: the HDA codec
3370 * @idx: the SPDIF ctl index
3371 *
3372 * Unassign the widget from the given SPDIF control.
3373 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003374void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3375{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003376 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003377
3378 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003379 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003380 spdif->nid = (u16)-1;
3381 mutex_unlock(&codec->spdif_mutex);
3382}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003383EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003384
Takashi Iwai95a962c2014-10-29 16:03:58 +01003385/**
3386 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3387 * @codec: the HDA codec
3388 * @idx: the SPDIF ctl idx
3389 * @nid: widget NID
3390 *
3391 * Assign the widget to the SPDIF control with the given index.
3392 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003393void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3394{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003395 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003396 unsigned short val;
3397
3398 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003399 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003400 if (spdif->nid != nid) {
3401 spdif->nid = nid;
3402 val = spdif->ctls;
3403 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3404 }
3405 mutex_unlock(&codec->spdif_mutex);
3406}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003407EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003410 * SPDIF sharing with analog output
3411 */
3412static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
3415 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3416 ucontrol->value.integer.value[0] = mout->share_spdif;
3417 return 0;
3418}
3419
3420static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3422{
3423 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3424 mout->share_spdif = !!ucontrol->value.integer.value[0];
3425 return 0;
3426}
3427
3428static struct snd_kcontrol_new spdif_share_sw = {
3429 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3430 .name = "IEC958 Default PCM Playback Switch",
3431 .info = snd_ctl_boolean_mono_info,
3432 .get = spdif_share_sw_get,
3433 .put = spdif_share_sw_put,
3434};
3435
Takashi Iwaid5191e52009-11-16 14:58:17 +01003436/**
3437 * snd_hda_create_spdif_share_sw - create Default PCM switch
3438 * @codec: the HDA codec
3439 * @mout: multi-out instance
3440 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003441int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3442 struct hda_multi_out *mout)
3443{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003444 struct snd_kcontrol *kctl;
3445
Takashi Iwai9a081602008-02-12 18:37:26 +01003446 if (!mout->dig_out_nid)
3447 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003448
3449 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3450 if (!kctl)
3451 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003452 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003453 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003454}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003455EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003456
3457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 * SPDIF input
3459 */
3460
3461#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3462
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3464 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
3466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3467
3468 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3469 return 0;
3470}
3471
Takashi Iwai0ba21762007-04-16 11:29:14 +02003472static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3476 hda_nid_t nid = kcontrol->private_value;
3477 unsigned int val = !!ucontrol->value.integer.value[0];
3478 int change;
3479
Ingo Molnar62932df2006-01-16 16:34:20 +01003480 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003482 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003484 snd_hda_codec_write_cache(codec, nid, 0,
3485 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003487 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return change;
3489}
3490
Takashi Iwai0ba21762007-04-16 11:29:14 +02003491static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3492 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3495 hda_nid_t nid = kcontrol->private_value;
3496 unsigned short val;
3497 unsigned int sbits;
3498
Andrew Paprocki3982d172007-12-19 12:13:44 +01003499 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 sbits = convert_to_spdif_status(val);
3501 ucontrol->value.iec958.status[0] = sbits;
3502 ucontrol->value.iec958.status[1] = sbits >> 8;
3503 ucontrol->value.iec958.status[2] = sbits >> 16;
3504 ucontrol->value.iec958.status[3] = sbits >> 24;
3505 return 0;
3506}
3507
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003508static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 {
3510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003511 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 .info = snd_hda_spdif_in_switch_info,
3513 .get = snd_hda_spdif_in_switch_get,
3514 .put = snd_hda_spdif_in_switch_put,
3515 },
3516 {
3517 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003519 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 .info = snd_hda_spdif_mask_info,
3521 .get = snd_hda_spdif_in_status_get,
3522 },
3523 { } /* end */
3524};
3525
3526/**
3527 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3528 * @codec: the HDA codec
3529 * @nid: audio in widget NID
3530 *
3531 * Creates controls related with the SPDIF input.
3532 * Called from each patch supporting the SPDIF in.
3533 *
3534 * Returns 0 if successful, or a negative error code.
3535 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003536int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003539 struct snd_kcontrol *kctl;
3540 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003541 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Takashi Iwaidcda5802012-10-12 17:24:51 +02003543 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003544 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003545 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003546 return -EBUSY;
3547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3549 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003550 if (!kctl)
3551 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003553 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003554 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return err;
3556 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003557 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003558 snd_hda_codec_read(codec, nid, 0,
3559 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003560 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return 0;
3562}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003563EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003565/*
3566 * command cache
3567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Takashi Iwaic370dd62012-12-13 18:30:04 +01003569/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003570#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3571#define get_cmd_cache_nid(key) ((key) & 0xff)
3572#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3573
3574/**
3575 * snd_hda_codec_write_cache - send a single command with caching
3576 * @codec: the HDA codec
3577 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003578 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003579 * @verb: the verb to send
3580 * @parm: the parameter for the verb
3581 *
3582 * Send a single command without waiting for response.
3583 *
3584 * Returns 0 if successful, or a negative error code.
3585 */
3586int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003587 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003588{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003589 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003590 struct hda_cache_head *c;
3591 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003592 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003593
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003594 cache_only = codec->cached_write;
3595 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003596 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003597 if (err < 0)
3598 return err;
3599 }
3600
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003601 /* parm may contain the verb stuff for get/set amp */
3602 verb = verb | (parm >> 8);
3603 parm &= 0xff;
3604 key = build_cmd_cache_key(nid, verb);
3605 mutex_lock(&codec->bus->cmd_mutex);
3606 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003607 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003608 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003609 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003610 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003611 mutex_unlock(&codec->bus->cmd_mutex);
3612 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003613}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003614EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003615
Takashi Iwaid5191e52009-11-16 14:58:17 +01003616/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003617 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3618 * @codec: the HDA codec
3619 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003620 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003621 * @verb: the verb to send
3622 * @parm: the parameter for the verb
3623 *
3624 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3625 * command if the parameter is already identical with the cached value.
3626 * If not, it sends the command and refreshes the cache.
3627 *
3628 * Returns 0 if successful, or a negative error code.
3629 */
3630int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003631 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003632{
3633 struct hda_cache_head *c;
3634 u32 key;
3635
3636 /* parm may contain the verb stuff for get/set amp */
3637 verb = verb | (parm >> 8);
3638 parm &= 0xff;
3639 key = build_cmd_cache_key(nid, verb);
3640 mutex_lock(&codec->bus->cmd_mutex);
3641 c = get_hash(&codec->cmd_cache, key);
3642 if (c && c->val == parm) {
3643 mutex_unlock(&codec->bus->cmd_mutex);
3644 return 0;
3645 }
3646 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003647 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003648}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003649EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003650
3651/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003652 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3653 * @codec: HD-audio codec
3654 *
3655 * Execute all verbs recorded in the command caches to resume.
3656 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003657void snd_hda_codec_resume_cache(struct hda_codec *codec)
3658{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003659 int i;
3660
Takashi Iwaic370dd62012-12-13 18:30:04 +01003661 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003662 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003663 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3664 struct hda_cache_head *buffer;
3665 u32 key;
3666
3667 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3668 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003669 if (!key)
3670 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003671 if (!buffer->dirty)
3672 continue;
3673 buffer->dirty = 0;
3674 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003675 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3676 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003677 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003678 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003679 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003680}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003681EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003682
3683/**
3684 * snd_hda_sequence_write_cache - sequence writes with caching
3685 * @codec: the HDA codec
3686 * @seq: VERB array to send
3687 *
3688 * Send the commands sequentially from the given array.
3689 * Thte commands are recorded on cache for power-save and resume.
3690 * The array must be terminated with NID=0.
3691 */
3692void snd_hda_sequence_write_cache(struct hda_codec *codec,
3693 const struct hda_verb *seq)
3694{
3695 for (; seq->nid; seq++)
3696 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3697 seq->param);
3698}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003699EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003700
Takashi Iwaidc870f32013-01-22 15:24:30 +01003701/**
3702 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3703 * @codec: HD-audio codec
3704 */
3705void snd_hda_codec_flush_cache(struct hda_codec *codec)
3706{
3707 snd_hda_codec_resume_amp(codec);
3708 snd_hda_codec_resume_cache(codec);
3709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003710EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003711
Takashi Iwai95a962c2014-10-29 16:03:58 +01003712/**
3713 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3714 * @codec: the HDA codec
3715 * @fg: function group (not used now)
3716 * @power_state: the power state to set (AC_PWRST_*)
3717 *
3718 * Set the given power state to all widgets that have the power control.
3719 * If the codec has power_filter set, it evaluates the power state and
3720 * filter out if it's unchanged as D3.
3721 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003722void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003723 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003724{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003725 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003726 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003727
Takashi Iwaicb53c622007-08-10 17:21:45 +02003728 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003729 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003730 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003731 if (!(wcaps & AC_WCAP_POWER))
3732 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003733 if (codec->power_filter) {
3734 state = codec->power_filter(codec, nid, power_state);
3735 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003736 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003737 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003738 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003739 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003740 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003741}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003742EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003743
3744/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003745 * supported power states check
3746 */
3747static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3748 unsigned int power_state)
3749{
3750 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3751
Mengdong Line037cb42012-08-10 14:11:58 +02003752 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003753 return false;
3754 if (sup & power_state)
3755 return true;
3756 else
3757 return false;
3758}
3759
3760/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003761 * wait until the state is reached, returns the current state
3762 */
3763static unsigned int hda_sync_power_state(struct hda_codec *codec,
3764 hda_nid_t fg,
3765 unsigned int power_state)
3766{
3767 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3768 unsigned int state, actual_state;
3769
3770 for (;;) {
3771 state = snd_hda_codec_read(codec, fg, 0,
3772 AC_VERB_GET_POWER_STATE, 0);
3773 if (state & AC_PWRST_ERROR)
3774 break;
3775 actual_state = (state >> 4) & 0x0f;
3776 if (actual_state == power_state)
3777 break;
3778 if (time_after_eq(jiffies, end_time))
3779 break;
3780 /* wait until the codec reachs to the target state */
3781 msleep(1);
3782 }
3783 return state;
3784}
3785
Takashi Iwai95a962c2014-10-29 16:03:58 +01003786/**
3787 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3788 * @codec: the HDA codec
3789 * @nid: widget NID
3790 * @power_state: power state to evalue
3791 *
3792 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3793 * This can be used a codec power_filter callback.
3794 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003795unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3796 hda_nid_t nid,
3797 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003798{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003799 if (nid == codec->afg || nid == codec->mfg)
3800 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003801 if (power_state == AC_PWRST_D3 &&
3802 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3803 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3804 int eapd = snd_hda_codec_read(codec, nid, 0,
3805 AC_VERB_GET_EAPD_BTLENABLE, 0);
3806 if (eapd & 0x02)
3807 return AC_PWRST_D0;
3808 }
3809 return power_state;
3810}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003811EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003812
Takashi Iwai432c6412012-08-28 09:59:20 -07003813/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003814 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003815 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003816static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003817 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003818{
Takashi Iwaid8193872012-08-31 07:54:38 -07003819 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003820 int count;
3821 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003822 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003823
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003824 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003825 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003826 if (codec->depop_delay < 0)
3827 msleep(codec->epss ? 10 : 100);
3828 else if (codec->depop_delay > 0)
3829 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003830 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003831 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003832
3833 /* repeat power states setting at most 10 times*/
3834 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003835 if (codec->patch_ops.set_power_state)
3836 codec->patch_ops.set_power_state(codec, fg,
3837 power_state);
3838 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003839 state = power_state;
3840 if (codec->power_filter)
3841 state = codec->power_filter(codec, fg, state);
3842 if (state == power_state || power_state != AC_PWRST_D3)
3843 snd_hda_codec_read(codec, fg, flags,
3844 AC_VERB_SET_POWER_STATE,
3845 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003846 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003847 }
3848 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003849 if (!(state & AC_PWRST_ERROR))
3850 break;
3851 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003852
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003853 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003854}
Takashi Iwai54d17402005-11-21 16:33:22 +01003855
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003856/* sync power states of all widgets;
3857 * this is called at the end of codec parsing
3858 */
3859static void sync_power_up_states(struct hda_codec *codec)
3860{
3861 hda_nid_t nid = codec->start_nid;
3862 int i;
3863
Takashi Iwaiba615b82013-03-13 14:47:21 +01003864 /* don't care if no filter is used */
3865 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003866 return;
3867
3868 for (i = 0; i < codec->num_nodes; i++, nid++) {
3869 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003870 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003871 if (!(wcaps & AC_WCAP_POWER))
3872 continue;
3873 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3874 if (target == AC_PWRST_D0)
3875 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003876 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003877 snd_hda_codec_write(codec, nid, 0,
3878 AC_VERB_SET_POWER_STATE, target);
3879 }
3880}
3881
Takashi Iwai648a8d22014-02-25 10:38:13 +01003882#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003883/* execute additional init verbs */
3884static void hda_exec_init_verbs(struct hda_codec *codec)
3885{
3886 if (codec->init_verbs.list)
3887 snd_hda_sequence_write(codec, codec->init_verbs.list);
3888}
3889#else
3890static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3891#endif
3892
Takashi Iwai2a439522011-07-26 09:52:50 +02003893#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003894/* update the power on/off account with the current jiffies */
3895static void update_power_acct(struct hda_codec *codec, bool on)
3896{
3897 unsigned long delta = jiffies - codec->power_jiffies;
3898
3899 if (on)
3900 codec->power_on_acct += delta;
3901 else
3902 codec->power_off_acct += delta;
3903 codec->power_jiffies += delta;
3904}
3905
3906void snd_hda_update_power_acct(struct hda_codec *codec)
3907{
3908 update_power_acct(codec, hda_codec_is_power_on(codec));
3909}
3910
Takashi Iwaicb53c622007-08-10 17:21:45 +02003911/*
3912 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003913 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003915static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003916{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003917 unsigned int state;
3918
Takashi Iwaicc72da72015-02-19 16:00:22 +01003919 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003920
Takashi Iwaicb53c622007-08-10 17:21:45 +02003921 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003922 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003923 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003924 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003925 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003926 update_power_acct(codec, true);
3927 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003928 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003929}
3930
Takashi Iwaic370dd62012-12-13 18:30:04 +01003931/* mark all entries of cmd and amp caches dirty */
3932static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3933{
3934 int i;
3935 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3936 struct hda_cache_head *cmd;
3937 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3938 cmd->dirty = 1;
3939 }
3940 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3941 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003942 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003943 amp->head.dirty = 1;
3944 }
3945}
3946
Takashi Iwaicb53c622007-08-10 17:21:45 +02003947/*
3948 * kick up codec; used both from PM and power-save
3949 */
3950static void hda_call_codec_resume(struct hda_codec *codec)
3951{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003952 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003953
Takashi Iwaicc72da72015-02-19 16:00:22 +01003954 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003955 hda_mark_cmd_cache_dirty(codec);
3956
Takashi Iwaicc72da72015-02-19 16:00:22 +01003957 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003958
Takashi Iwaid8193872012-08-31 07:54:38 -07003959 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003960 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003961 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003962 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003963 if (codec->patch_ops.resume)
3964 codec->patch_ops.resume(codec);
3965 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003966 if (codec->patch_ops.init)
3967 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003968 snd_hda_codec_resume_amp(codec);
3969 snd_hda_codec_resume_cache(codec);
3970 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003971
3972 if (codec->jackpoll_interval)
3973 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003974 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003975 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003976 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003977}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003978
Takashi Iwaicc72da72015-02-19 16:00:22 +01003979static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003980{
3981 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003982 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003983 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003984
3985 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003986 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3987 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003988 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003989 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
3990 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003991 return 0;
3992}
3993
Takashi Iwaicc72da72015-02-19 16:00:22 +01003994static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003995{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003996 struct hda_codec *codec = dev_to_hda_codec(dev);
3997
3998 set_bit(codec->addr, &codec->bus->codec_powered);
3999 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004000 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004001 return 0;
4002}
Takashi Iwai2a439522011-07-26 09:52:50 +02004003#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004004
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004005/* referred in hda_bind.c */
4006const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004007 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4008 pm_runtime_force_resume)
4009 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
4010 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004011};
Takashi Iwai54d17402005-11-21 16:33:22 +01004012
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004013/*
4014 * add standard channel maps if not specified
4015 */
4016static int add_std_chmaps(struct hda_codec *codec)
4017{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004018 struct hda_pcm *pcm;
4019 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004020
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004021 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004022 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004023 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004024 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004025 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004026
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004027 if (pcm->own_chmap)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004028 continue;
4029 if (!pcm || !hinfo->substreams)
4030 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004031 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004032 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004033 hinfo->channels_max,
4034 0, &chmap);
4035 if (err < 0)
4036 return err;
4037 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4038 }
4039 }
4040 return 0;
4041}
4042
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004043/* default channel maps for 2.1 speakers;
4044 * since HD-audio supports only stereo, odd number channels are omitted
4045 */
4046const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4047 { .channels = 2,
4048 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4049 { .channels = 4,
4050 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4051 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4052 { }
4053};
4054EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4055
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004056int snd_hda_codec_build_controls(struct hda_codec *codec)
4057{
4058 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004059 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004060 /* continue to initialize... */
4061 if (codec->patch_ops.init)
4062 err = codec->patch_ops.init(codec);
4063 if (!err && codec->patch_ops.build_controls)
4064 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004065 if (err < 0)
4066 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004067
4068 /* we create chmaps here instead of build_pcms */
4069 err = add_std_chmaps(codec);
4070 if (err < 0)
4071 return err;
4072
David Henningsson26a6cb62012-10-09 15:04:21 +02004073 if (codec->jackpoll_interval)
4074 hda_jackpoll_work(&codec->jackpoll_work.work);
4075 else
4076 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004077 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return 0;
4079}
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081/*
4082 * stream formats
4083 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004084struct hda_rate_tbl {
4085 unsigned int hz;
4086 unsigned int alsa_bits;
4087 unsigned int hda_fmt;
4088};
4089
Takashi Iwai92f10b32010-08-03 14:21:00 +02004090/* rate = base * mult / div */
4091#define HDA_RATE(base, mult, div) \
4092 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4093 (((div) - 1) << AC_FMT_DIV_SHIFT))
4094
Takashi Iwaibefdf312005-08-22 13:57:55 +02004095static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004097
4098 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004099 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4100 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4101 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4102 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4103 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4104 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4105 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4106 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4107 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4108 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4109 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004110#define AC_PAR_PCM_RATE_BITS 11
4111 /* up to bits 10, 384kHZ isn't supported properly */
4112
4113 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004114 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004115
Takashi Iwaibefdf312005-08-22 13:57:55 +02004116 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117};
4118
4119/**
4120 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004121 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * @rate: the sample rate
4123 * @channels: the number of channels
4124 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4125 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004126 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 *
4128 * Calculate the format bitset from the given rate, channels and th PCM format.
4129 *
4130 * Return zero if invalid.
4131 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004132unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4133 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 unsigned int channels,
4135 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004136 unsigned int maxbps,
4137 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
4139 int i;
4140 unsigned int val = 0;
4141
Takashi Iwaibefdf312005-08-22 13:57:55 +02004142 for (i = 0; rate_bits[i].hz; i++)
4143 if (rate_bits[i].hz == rate) {
4144 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
4146 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004147 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004148 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return 0;
4150 }
4151
4152 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004153 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return 0;
4155 }
4156 val |= channels - 1;
4157
4158 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004159 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004160 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004161 break;
4162 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004163 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 case 20:
4166 case 24:
4167 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004168 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004169 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004171 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004173 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 break;
4175 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004176 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004177 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return 0;
4179 }
4180
Anssi Hannula32c168c2010-08-03 13:28:57 +03004181 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004182 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return val;
4185}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004186EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004188static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4189 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004190{
4191 unsigned int val = 0;
4192 if (nid != codec->afg &&
4193 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4194 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4195 if (!val || val == -1)
4196 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4197 if (!val || val == -1)
4198 return 0;
4199 return val;
4200}
4201
4202static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4203{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004204 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004205 get_pcm_param);
4206}
4207
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004208static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4209 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004210{
4211 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4212 if (!streams || streams == -1)
4213 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4214 if (!streams || streams == -1)
4215 return 0;
4216 return streams;
4217}
4218
4219static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4220{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004221 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004222 get_stream_param);
4223}
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225/**
4226 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4227 * @codec: the HDA codec
4228 * @nid: NID to query
4229 * @ratesp: the pointer to store the detected rate bitflags
4230 * @formatsp: the pointer to store the detected formats
4231 * @bpsp: the pointer to store the detected format widths
4232 *
4233 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4234 * or @bsps argument is ignored.
4235 *
4236 * Returns 0 if successful, otherwise a negative error code.
4237 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004238int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4240{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004241 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004243 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004244 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 if (ratesp) {
4247 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004248 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004250 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004252 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004253 codec_err(codec,
4254 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4255 nid, val,
4256 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004257 return -EIO;
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 *ratesp = rates;
4260 }
4261
4262 if (formatsp || bpsp) {
4263 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004264 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004266 streams = query_stream_param(codec, nid);
4267 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 bps = 0;
4271 if (streams & AC_SUPFMT_PCM) {
4272 if (val & AC_SUPPCM_BITS_8) {
4273 formats |= SNDRV_PCM_FMTBIT_U8;
4274 bps = 8;
4275 }
4276 if (val & AC_SUPPCM_BITS_16) {
4277 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4278 bps = 16;
4279 }
4280 if (wcaps & AC_WCAP_DIGITAL) {
4281 if (val & AC_SUPPCM_BITS_32)
4282 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4283 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4284 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4285 if (val & AC_SUPPCM_BITS_24)
4286 bps = 24;
4287 else if (val & AC_SUPPCM_BITS_20)
4288 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004289 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4290 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4292 if (val & AC_SUPPCM_BITS_32)
4293 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 else if (val & AC_SUPPCM_BITS_24)
4295 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004296 else if (val & AC_SUPPCM_BITS_20)
4297 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 }
4299 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004300#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004301 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004303 if (!bps)
4304 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004305 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004306#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004307 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004308 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 /* temporary hack: we have still no proper support
4310 * for the direct AC3 stream...
4311 */
4312 formats |= SNDRV_PCM_FMTBIT_U8;
4313 bps = 8;
4314 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004315 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004316 codec_err(codec,
4317 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4318 nid, val,
4319 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4320 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004321 return -EIO;
4322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (formatsp)
4324 *formatsp = formats;
4325 if (bpsp)
4326 *bpsp = bps;
4327 }
4328
4329 return 0;
4330}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004331EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
4333/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004334 * snd_hda_is_supported_format - Check the validity of the format
4335 * @codec: HD-audio codec
4336 * @nid: NID to check
4337 * @format: the HD-audio format value to check
4338 *
4339 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 *
4341 * Returns 1 if supported, 0 if not.
4342 */
4343int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4344 unsigned int format)
4345{
4346 int i;
4347 unsigned int val = 0, rate, stream;
4348
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004349 val = query_pcm_param(codec, nid);
4350 if (!val)
4351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004354 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004355 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (val & (1 << i))
4357 break;
4358 return 0;
4359 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004360 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return 0;
4362
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004363 stream = query_stream_param(codec, nid);
4364 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366
4367 if (stream & AC_SUPFMT_PCM) {
4368 switch (format & 0xf0) {
4369 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004370 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return 0;
4372 break;
4373 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004374 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return 0;
4376 break;
4377 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004378 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return 0;
4380 break;
4381 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004382 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return 0;
4384 break;
4385 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004386 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388 break;
4389 default:
4390 return 0;
4391 }
4392 } else {
4393 /* FIXME: check for float32 and AC3? */
4394 }
4395
4396 return 1;
4397}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004398EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
4400/*
4401 * PCM stuff
4402 */
4403static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4404 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004405 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
4407 return 0;
4408}
4409
4410static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4411 struct hda_codec *codec,
4412 unsigned int stream_tag,
4413 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004414 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
4416 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4417 return 0;
4418}
4419
4420static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4421 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004422 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Takashi Iwai888afa12008-03-18 09:57:50 +01004424 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 return 0;
4426}
4427
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004428static int set_pcm_default_values(struct hda_codec *codec,
4429 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004431 int err;
4432
Takashi Iwai0ba21762007-04-16 11:29:14 +02004433 /* query support PCM information from the given NID */
4434 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004435 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004436 info->rates ? NULL : &info->rates,
4437 info->formats ? NULL : &info->formats,
4438 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004439 if (err < 0)
4440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 }
4442 if (info->ops.open == NULL)
4443 info->ops.open = hda_pcm_default_open_close;
4444 if (info->ops.close == NULL)
4445 info->ops.close = hda_pcm_default_open_close;
4446 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004447 if (snd_BUG_ON(!info->nid))
4448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 info->ops.prepare = hda_pcm_default_prepare;
4450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004452 if (snd_BUG_ON(!info->nid))
4453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 info->ops.cleanup = hda_pcm_default_cleanup;
4455 }
4456 return 0;
4457}
4458
Takashi Iwaieb541332010-08-06 13:48:11 +02004459/*
4460 * codec prepare/cleanup entries
4461 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004462/**
4463 * snd_hda_codec_prepare - Prepare a stream
4464 * @codec: the HDA codec
4465 * @hinfo: PCM information
4466 * @stream: stream tag to assign
4467 * @format: format id to assign
4468 * @substream: PCM substream to assign
4469 *
4470 * Calls the prepare callback set by the codec with the given arguments.
4471 * Clean up the inactive streams when successful.
4472 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004473int snd_hda_codec_prepare(struct hda_codec *codec,
4474 struct hda_pcm_stream *hinfo,
4475 unsigned int stream,
4476 unsigned int format,
4477 struct snd_pcm_substream *substream)
4478{
4479 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004480 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004481 if (hinfo->ops.prepare)
4482 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4483 substream);
4484 else
4485 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02004486 if (ret >= 0)
4487 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004488 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004489 return ret;
4490}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004491EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004492
Takashi Iwai95a962c2014-10-29 16:03:58 +01004493/**
4494 * snd_hda_codec_cleanup - Prepare a stream
4495 * @codec: the HDA codec
4496 * @hinfo: PCM information
4497 * @substream: PCM substream
4498 *
4499 * Calls the cleanup callback set by the codec with the given arguments.
4500 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004501void snd_hda_codec_cleanup(struct hda_codec *codec,
4502 struct hda_pcm_stream *hinfo,
4503 struct snd_pcm_substream *substream)
4504{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004505 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01004506 if (hinfo->ops.cleanup)
4507 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004508 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004509}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004510EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004511
Takashi Iwaid5191e52009-11-16 14:58:17 +01004512/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004513const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4514 "Audio", "SPDIF", "HDMI", "Modem"
4515};
4516
Takashi Iwai176d5332008-07-30 15:01:44 +02004517/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004518 * get the empty PCM device number to assign
4519 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004520static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004521{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004522 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004523 /* assigned to static slots up to dev#10; if more needed, assign
4524 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4525 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004526 static int audio_idx[HDA_PCM_NTYPES][5] = {
4527 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4528 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004529 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004530 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004531 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004532 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004533
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004534 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004535 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004536 return -EINVAL;
4537 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004538
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004539 for (i = 0; audio_idx[type][i] >= 0; i++) {
4540#ifndef CONFIG_SND_DYNAMIC_MINORS
4541 if (audio_idx[type][i] >= 8)
4542 break;
4543#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004544 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4545 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004546 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004547
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004548#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004549 /* non-fixed slots starting from 10 */
4550 for (i = 10; i < 32; i++) {
4551 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4552 return i;
4553 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004554#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004555
Takashi Iwai4e76a882014-02-25 12:21:03 +01004556 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004557 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004558#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004559 dev_warn(bus->card->dev,
4560 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004561#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004562 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004563}
4564
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004565/* call build_pcms ops of the given codec and set up the default parameters */
4566int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004567{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004568 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004569 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004570
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004571 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004572 return 0; /* already parsed */
4573
4574 if (!codec->patch_ops.build_pcms)
4575 return 0;
4576
4577 err = codec->patch_ops.build_pcms(codec);
4578 if (err < 0) {
4579 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4580 codec->addr, err);
4581 return err;
4582 }
4583
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004584 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004585 int stream;
4586
4587 for (stream = 0; stream < 2; stream++) {
4588 struct hda_pcm_stream *info = &cpcm->stream[stream];
4589
4590 if (!info->substreams)
4591 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02004592 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004593 if (err < 0) {
4594 codec_warn(codec,
4595 "fail to setup default for PCM %s\n",
4596 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004597 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004598 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004599 }
4600 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004601
4602 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004603}
4604
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004605/* assign all PCMs of the given codec */
4606int snd_hda_codec_build_pcms(struct hda_codec *codec)
4607{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004608 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004609 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004610 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004611
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004612 if (snd_BUG_ON(!bus->ops.attach_pcm))
4613 return -EINVAL;
4614
4615 err = snd_hda_codec_parse_pcms(codec);
4616 if (err < 0) {
4617 snd_hda_codec_reset(codec);
4618 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004619 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004620
4621 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004622 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004623 if (cpcm->pcm)
4624 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004625 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004626 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004627
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004628 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4629 if (dev < 0)
4630 continue; /* no fatal error */
4631 cpcm->device = dev;
4632 err = bus->ops.attach_pcm(bus, codec, cpcm);
4633 if (err < 0) {
4634 codec_err(codec,
4635 "cannot attach PCM stream %d for codec #%d\n",
4636 dev, codec->addr);
4637 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004638 }
4639 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004640
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004641 return 0;
4642}
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 * snd_hda_add_new_ctls - create controls from the array
4646 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004647 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 *
4649 * This helper function creates and add new controls in the given array.
4650 * The array must be terminated with an empty entry as terminator.
4651 *
4652 * Returns 0 if successful, or a negative error code.
4653 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004654int snd_hda_add_new_ctls(struct hda_codec *codec,
4655 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004657 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004660 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004661 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004662 if (knew->iface == -1) /* skip this codec private value */
4663 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004664 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004665 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004666 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004667 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004668 if (addr > 0)
4669 kctl->id.device = addr;
4670 if (idx > 0)
4671 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004672 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004673 if (!err)
4674 break;
4675 /* try first with another device index corresponding to
4676 * the codec addr; if it still fails (or it's the
4677 * primary codec), then try another control index
4678 */
4679 if (!addr && codec->addr)
4680 addr = codec->addr;
4681 else if (!idx && !knew->index) {
4682 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004683 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004684 if (idx <= 0)
4685 return err;
4686 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004687 return err;
4688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690 return 0;
4691}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004692EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Takashi Iwai83012a72012-08-24 18:38:08 +02004694#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004695/**
4696 * snd_hda_power_up - Power-up the codec
4697 * @codec: HD-audio codec
4698 *
4699 * Increment the usage counter and resume the device if not done yet.
4700 */
4701void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004702{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004703 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004704
Takashi Iwaicc72da72015-02-19 16:00:22 +01004705 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004706 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004707 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004708}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004709EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004710
4711/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004712 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004713 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004714 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004715 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004716 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004717void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004718{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004719 struct device *dev = hda_codec_dev(codec);
4720
4721 if (codec_in_pm(codec))
4722 return;
4723 pm_runtime_mark_last_busy(dev);
4724 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004725}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004726EXPORT_SYMBOL_GPL(snd_hda_power_down);
4727
Takashi Iwaibb573922015-02-20 09:26:04 +01004728static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004729{
4730 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004731
Takashi Iwaicc72da72015-02-19 16:00:22 +01004732 if (delay > 0) {
4733 pm_runtime_set_autosuspend_delay(dev, delay);
4734 pm_runtime_use_autosuspend(dev);
4735 pm_runtime_allow(dev);
4736 if (!pm_runtime_suspended(dev))
4737 pm_runtime_mark_last_busy(dev);
4738 } else {
4739 pm_runtime_dont_use_autosuspend(dev);
4740 pm_runtime_forbid(dev);
4741 }
4742}
Takashi Iwaibb573922015-02-20 09:26:04 +01004743
4744/**
4745 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4746 * @bus: HD-audio bus
4747 * @delay: autosuspend delay in msec, 0 = off
4748 *
4749 * Synchronize the runtime PM autosuspend state from the power_save option.
4750 */
4751void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4752{
4753 struct hda_codec *c;
4754
4755 list_for_each_entry(c, &bus->codec_list, list)
4756 codec_set_power_save(c, delay);
4757}
4758EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759
Takashi Iwaid5191e52009-11-16 14:58:17 +01004760/**
4761 * snd_hda_check_amp_list_power - Check the amp list and update the power
4762 * @codec: HD-audio codec
4763 * @check: the object containing an AMP list and the status
4764 * @nid: NID to check / update
4765 *
4766 * Check whether the given NID is in the amp list. If it's in the list,
4767 * check the current AMP status, and update the the power-status according
4768 * to the mute status.
4769 *
4770 * This function is supposed to be set or called from the check_power_status
4771 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004772 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004773int snd_hda_check_amp_list_power(struct hda_codec *codec,
4774 struct hda_loopback_check *check,
4775 hda_nid_t nid)
4776{
Takashi Iwai031024e2011-05-02 11:29:30 +02004777 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004778 int ch, v;
4779
4780 if (!check->amplist)
4781 return 0;
4782 for (p = check->amplist; p->nid; p++) {
4783 if (p->nid == nid)
4784 break;
4785 }
4786 if (!p->nid)
4787 return 0; /* nothing changed */
4788
4789 for (p = check->amplist; p->nid; p++) {
4790 for (ch = 0; ch < 2; ch++) {
4791 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4792 p->idx);
4793 if (!(v & HDA_AMP_MUTE) && v > 0) {
4794 if (!check->power_on) {
4795 check->power_on = 1;
4796 snd_hda_power_up(codec);
4797 }
4798 return 1;
4799 }
4800 }
4801 }
4802 if (check->power_on) {
4803 check->power_on = 0;
4804 snd_hda_power_down(codec);
4805 }
4806 return 0;
4807}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004808EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004811/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 * input MUX helper
4813 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004814
4815/**
4816 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004817 * @imux: imux helper object
4818 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004819 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004820int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4821 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
4823 unsigned int index;
4824
4825 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4826 uinfo->count = 1;
4827 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004828 if (!imux->num_items)
4829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 index = uinfo->value.enumerated.item;
4831 if (index >= imux->num_items)
4832 index = imux->num_items - 1;
4833 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4834 return 0;
4835}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004836EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Takashi Iwaid5191e52009-11-16 14:58:17 +01004838/**
4839 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004840 * @codec: the HDA codec
4841 * @imux: imux helper object
4842 * @ucontrol: pointer to get/store the data
4843 * @nid: input mux NID
4844 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004845 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004846int snd_hda_input_mux_put(struct hda_codec *codec,
4847 const struct hda_input_mux *imux,
4848 struct snd_ctl_elem_value *ucontrol,
4849 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 unsigned int *cur_val)
4851{
4852 unsigned int idx;
4853
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004854 if (!imux->num_items)
4855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 idx = ucontrol->value.enumerated.item[0];
4857 if (idx >= imux->num_items)
4858 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004859 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004861 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4862 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 *cur_val = idx;
4864 return 1;
4865}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004866EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004869/**
4870 * snd_hda_enum_helper_info - Helper for simple enum ctls
4871 * @kcontrol: ctl element
4872 * @uinfo: pointer to get/store the data
4873 * @num_items: number of enum items
4874 * @texts: enum item string array
4875 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004876 * process kcontrol info callback of a simple string enum array
4877 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4878 */
4879int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4880 struct snd_ctl_elem_info *uinfo,
4881 int num_items, const char * const *texts)
4882{
4883 static const char * const texts_default[] = {
4884 "Disabled", "Enabled"
4885 };
4886
4887 if (!texts || !num_items) {
4888 num_items = 2;
4889 texts = texts_default;
4890 }
4891
Takashi Iwai3ff72212014-10-20 18:17:28 +02004892 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004893}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004894EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004895
4896/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 * Multi-channel / digital-out PCM helper functions
4898 */
4899
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004900/* setup SPDIF output stream */
4901static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4902 unsigned int stream_tag, unsigned int format)
4903{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004904 struct hda_spdif_out *spdif;
4905 unsigned int curr_fmt;
4906 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004907
Laurence Darby3bef1c32012-11-03 17:00:06 +00004908 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4909 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4910 AC_VERB_GET_STREAM_FORMAT, 0);
4911 reset = codec->spdif_status_reset &&
4912 (spdif->ctls & AC_DIG1_ENABLE) &&
4913 curr_fmt != format;
4914
4915 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4916 updated */
4917 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004918 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004919 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004920 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004921 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004922 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004923 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004924 for (d = codec->slave_dig_outs; *d; d++)
4925 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4926 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004927 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004928 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004929 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004930 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004931 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004932}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004933
Takashi Iwai2f728532008-09-25 16:32:41 +02004934static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4935{
4936 snd_hda_codec_cleanup_stream(codec, nid);
4937 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004938 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004939 for (d = codec->slave_dig_outs; *d; d++)
4940 snd_hda_codec_cleanup_stream(codec, *d);
4941 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004942}
4943
Takashi Iwaid5191e52009-11-16 14:58:17 +01004944/**
4945 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4946 * @bus: HD-audio bus
4947 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004948void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4949{
4950 struct hda_codec *codec;
4951
4952 if (!bus)
4953 return;
4954 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004955 if (hda_codec_is_power_on(codec) &&
4956 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004957 codec->patch_ops.reboot_notify(codec);
4958 }
4959}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004960EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004961
Takashi Iwaid5191e52009-11-16 14:58:17 +01004962/**
4963 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004964 * @codec: the HDA codec
4965 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004967int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4968 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
Ingo Molnar62932df2006-01-16 16:34:20 +01004970 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004971 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4972 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004973 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004975 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 return 0;
4977}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004978EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Takashi Iwaid5191e52009-11-16 14:58:17 +01004980/**
4981 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004982 * @codec: the HDA codec
4983 * @mout: hda_multi_out object
4984 * @stream_tag: stream tag to assign
4985 * @format: format id to assign
4986 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01004987 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004988int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4989 struct hda_multi_out *mout,
4990 unsigned int stream_tag,
4991 unsigned int format,
4992 struct snd_pcm_substream *substream)
4993{
4994 mutex_lock(&codec->spdif_mutex);
4995 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4996 mutex_unlock(&codec->spdif_mutex);
4997 return 0;
4998}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004999EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005000
Takashi Iwaid5191e52009-11-16 14:58:17 +01005001/**
5002 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005003 * @codec: the HDA codec
5004 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005005 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005006int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5007 struct hda_multi_out *mout)
5008{
5009 mutex_lock(&codec->spdif_mutex);
5010 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5011 mutex_unlock(&codec->spdif_mutex);
5012 return 0;
5013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005014EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005015
Takashi Iwaid5191e52009-11-16 14:58:17 +01005016/**
5017 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005018 * @codec: the HDA codec
5019 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005021int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5022 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Ingo Molnar62932df2006-01-16 16:34:20 +01005024 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005026 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 return 0;
5028}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005029EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Takashi Iwaid5191e52009-11-16 14:58:17 +01005031/**
5032 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005033 * @codec: the HDA codec
5034 * @mout: hda_multi_out object
5035 * @substream: PCM substream to assign
5036 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005037 *
5038 * Open analog outputs and set up the hw-constraints.
5039 * If the digital outputs can be opened as slave, open the digital
5040 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005042int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5043 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005044 struct snd_pcm_substream *substream,
5045 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
Takashi Iwai9a081602008-02-12 18:37:26 +01005047 struct snd_pcm_runtime *runtime = substream->runtime;
5048 runtime->hw.channels_max = mout->max_channels;
5049 if (mout->dig_out_nid) {
5050 if (!mout->analog_rates) {
5051 mout->analog_rates = hinfo->rates;
5052 mout->analog_formats = hinfo->formats;
5053 mout->analog_maxbps = hinfo->maxbps;
5054 } else {
5055 runtime->hw.rates = mout->analog_rates;
5056 runtime->hw.formats = mout->analog_formats;
5057 hinfo->maxbps = mout->analog_maxbps;
5058 }
5059 if (!mout->spdif_rates) {
5060 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5061 &mout->spdif_rates,
5062 &mout->spdif_formats,
5063 &mout->spdif_maxbps);
5064 }
5065 mutex_lock(&codec->spdif_mutex);
5066 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005067 if ((runtime->hw.rates & mout->spdif_rates) &&
5068 (runtime->hw.formats & mout->spdif_formats)) {
5069 runtime->hw.rates &= mout->spdif_rates;
5070 runtime->hw.formats &= mout->spdif_formats;
5071 if (mout->spdif_maxbps < hinfo->maxbps)
5072 hinfo->maxbps = mout->spdif_maxbps;
5073 } else {
5074 mout->share_spdif = 0;
5075 /* FIXME: need notify? */
5076 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005077 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005078 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5081 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005083EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Takashi Iwaid5191e52009-11-16 14:58:17 +01005085/**
5086 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005087 * @codec: the HDA codec
5088 * @mout: hda_multi_out object
5089 * @stream_tag: stream tag to assign
5090 * @format: format id to assign
5091 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005092 *
5093 * Set up the i/o for analog out.
5094 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005096int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5097 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 unsigned int stream_tag,
5099 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005100 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
Takashi Iwaidda14412011-05-02 11:29:30 +02005102 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005104 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 int i;
5106
Ingo Molnar62932df2006-01-16 16:34:20 +01005107 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005108 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005109 if (mout->dig_out_nid && mout->share_spdif &&
5110 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005112 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5113 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005114 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005116 setup_dig_out_stream(codec, mout->dig_out_nid,
5117 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 } else {
5119 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005120 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
5122 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005123 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
5125 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005126 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5127 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005128 if (!mout->no_share_stream &&
5129 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005131 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5132 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005133 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005134 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5135 if (!mout->no_share_stream && mout->hp_out_nid[i])
5136 snd_hda_codec_setup_stream(codec,
5137 mout->hp_out_nid[i],
5138 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 /* surrounds */
5141 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005142 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005143 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5144 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005145 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005146 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5147 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005149
5150 /* extra surrounds */
5151 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5152 int ch = 0;
5153 if (!mout->extra_out_nid[i])
5154 break;
5155 if (chs >= (i + 1) * 2)
5156 ch = i * 2;
5157 else if (!mout->no_share_stream)
5158 break;
5159 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5160 stream_tag, ch, format);
5161 }
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 return 0;
5164}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005165EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Takashi Iwaid5191e52009-11-16 14:58:17 +01005167/**
5168 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005169 * @codec: the HDA codec
5170 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005172int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5173 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
Takashi Iwaidda14412011-05-02 11:29:30 +02005175 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 int i;
5177
5178 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005179 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005181 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005182 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5183 if (mout->hp_out_nid[i])
5184 snd_hda_codec_cleanup_stream(codec,
5185 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005186 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5187 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005188 snd_hda_codec_cleanup_stream(codec,
5189 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005190 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005192 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 mout->dig_out_used = 0;
5194 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005195 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return 0;
5197}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005198EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Takashi Iwai47408602012-04-20 13:06:53 +02005200/**
5201 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005202 * @codec: the HDA codec
5203 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005204 *
5205 * Guess the suitable VREF pin bits to be set as the pin-control value.
5206 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5207 */
5208unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5209{
5210 unsigned int pincap;
5211 unsigned int oldval;
5212 oldval = snd_hda_codec_read(codec, pin, 0,
5213 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5214 pincap = snd_hda_query_pin_caps(codec, pin);
5215 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5216 /* Exception: if the default pin setup is vref50, we give it priority */
5217 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5218 return AC_PINCTL_VREF_80;
5219 else if (pincap & AC_PINCAP_VREF_50)
5220 return AC_PINCTL_VREF_50;
5221 else if (pincap & AC_PINCAP_VREF_100)
5222 return AC_PINCTL_VREF_100;
5223 else if (pincap & AC_PINCAP_VREF_GRD)
5224 return AC_PINCTL_VREF_GRD;
5225 return AC_PINCTL_VREF_HIZ;
5226}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005227EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005228
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005229/**
5230 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5231 * @codec: the HDA codec
5232 * @pin: referred pin NID
5233 * @val: pin ctl value to audit
5234 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005235unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5236 hda_nid_t pin, unsigned int val)
5237{
5238 static unsigned int cap_lists[][2] = {
5239 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5240 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5241 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5242 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5243 };
5244 unsigned int cap;
5245
5246 if (!val)
5247 return 0;
5248 cap = snd_hda_query_pin_caps(codec, pin);
5249 if (!cap)
5250 return val; /* don't know what to do... */
5251
5252 if (val & AC_PINCTL_OUT_EN) {
5253 if (!(cap & AC_PINCAP_OUT))
5254 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5255 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5256 val &= ~AC_PINCTL_HP_EN;
5257 }
5258
5259 if (val & AC_PINCTL_IN_EN) {
5260 if (!(cap & AC_PINCAP_IN))
5261 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5262 else {
5263 unsigned int vcap, vref;
5264 int i;
5265 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5266 vref = val & AC_PINCTL_VREFEN;
5267 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5268 if (vref == cap_lists[i][0] &&
5269 !(vcap & cap_lists[i][1])) {
5270 if (i == ARRAY_SIZE(cap_lists) - 1)
5271 vref = AC_PINCTL_VREF_HIZ;
5272 else
5273 vref = cap_lists[i + 1][0];
5274 }
5275 }
5276 val &= ~AC_PINCTL_VREFEN;
5277 val |= vref;
5278 }
5279 }
5280
5281 return val;
5282}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005283EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005284
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005285/**
5286 * _snd_hda_pin_ctl - Helper to set pin ctl value
5287 * @codec: the HDA codec
5288 * @pin: referred pin NID
5289 * @val: pin control value to set
5290 * @cached: access over codec pinctl cache or direct write
5291 *
5292 * This function is a helper to set a pin ctl value more safely.
5293 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5294 * value in pin target array via snd_hda_codec_set_pin_target(), then
5295 * actually writes the value via either snd_hda_codec_update_cache() or
5296 * snd_hda_codec_write() depending on @cached flag.
5297 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005298int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5299 unsigned int val, bool cached)
5300{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005301 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005302 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005303 if (cached)
5304 return snd_hda_codec_update_cache(codec, pin, 0,
5305 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5306 else
5307 return snd_hda_codec_write(codec, pin, 0,
5308 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5309}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005310EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005311
Takashi Iwai990061c2010-09-09 22:08:44 +02005312/**
5313 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005314 * @codec: the HDA codec
5315 * @imux: imux helper object
5316 * @label: the name of imux item to assign
5317 * @index: index number of imux item to assign
5318 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005319 *
5320 * When the same label is used already in the existing items, the number
5321 * suffix is appended to the label. This label index number is stored
5322 * to type_idx when non-NULL pointer is given.
5323 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005324int snd_hda_add_imux_item(struct hda_codec *codec,
5325 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005326 int index, int *type_idx)
5327{
5328 int i, label_idx = 0;
5329 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005330 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005331 return -EINVAL;
5332 }
5333 for (i = 0; i < imux->num_items; i++) {
5334 if (!strncmp(label, imux->items[i].label, strlen(label)))
5335 label_idx++;
5336 }
5337 if (type_idx)
5338 *type_idx = label_idx;
5339 if (label_idx > 0)
5340 snprintf(imux->items[imux->num_items].label,
5341 sizeof(imux->items[imux->num_items].label),
5342 "%s %d", label, label_idx);
5343 else
5344 strlcpy(imux->items[imux->num_items].label, label,
5345 sizeof(imux->items[imux->num_items].label));
5346 imux->items[imux->num_items].index = index;
5347 imux->num_items++;
5348 return 0;
5349}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005350EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005353 * snd_hda_bus_reset - Reset the bus
5354 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005356void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005358 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Takashi Iwai0ba21762007-04-16 11:29:14 +02005360 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005361 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005362 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005363#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005364 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005365 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005366 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005367 }
5368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005371EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005372
5373/*
5374 * generic arrays
5375 */
5376
Takashi Iwaid5191e52009-11-16 14:58:17 +01005377/**
5378 * snd_array_new - get a new element from the given array
5379 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005380 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005381 * Get a new element from the given array. If it exceeds the
5382 * pre-allocated array size, re-allocate the array.
5383 *
5384 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005385 */
5386void *snd_array_new(struct snd_array *array)
5387{
Takashi Iwai12f17712012-10-10 08:59:14 +02005388 if (snd_BUG_ON(!array->elem_size))
5389 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005390 if (array->used >= array->alloced) {
5391 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005392 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005393 void *nlist;
5394 if (snd_BUG_ON(num >= 4096))
5395 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005396 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005397 if (!nlist)
5398 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005399 array->list = nlist;
5400 array->alloced = num;
5401 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005402 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005403}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005404EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005405
Takashi Iwaid5191e52009-11-16 14:58:17 +01005406/**
5407 * snd_array_free - free the given array elements
5408 * @array: the array object
5409 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005410void snd_array_free(struct snd_array *array)
5411{
5412 kfree(array->list);
5413 array->used = 0;
5414 array->alloced = 0;
5415 array->list = NULL;
5416}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005417EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005418
Takashi Iwaid5191e52009-11-16 14:58:17 +01005419/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005420 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5421 * @pcm: PCM caps bits
5422 * @buf: the string buffer to write
5423 * @buflen: the max buffer length
5424 *
5425 * used by hda_proc.c and hda_eld.c
5426 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005427void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5428{
5429 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5430 int i, j;
5431
5432 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5433 if (pcm & (AC_SUPPCM_BITS_8 << i))
5434 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5435
5436 buf[j] = '\0'; /* necessary when j == 0 */
5437}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005438EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005439
5440MODULE_DESCRIPTION("HDA codec core");
5441MODULE_LICENSE("GPL");