blob: 33b8b71f8eaf53ec3927fe843cb79f4315e67298 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwaid66fee52011-08-02 15:39:31 +020042#define CREATE_TRACE_POINTS
43#include "hda_trace.h"
44
Takashi Iwai83012a72012-08-24 18:38:08 +020045#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +010046#define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
47#define hda_codec_is_power_on(codec) \
48 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#else
Takashi Iwaid846b172012-11-24 11:58:24 +010050#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020051#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020052#endif
53
Takashi Iwaid5191e52009-11-16 14:58:17 +010054/**
55 * snd_hda_get_jack_location - Give a location string of the jack
56 * @cfg: pin default config value
57 *
58 * Parse the pin default config value and returns the string of the
59 * jack location, e.g. "Rear", "Front", etc.
60 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040061const char *snd_hda_get_jack_location(u32 cfg)
62{
63 static char *bases[7] = {
64 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
65 };
66 static unsigned char specials_idx[] = {
67 0x07, 0x08,
68 0x17, 0x18, 0x19,
69 0x37, 0x38
70 };
71 static char *specials[] = {
72 "Rear Panel", "Drive Bar",
73 "Riser", "HDMI", "ATAPI",
74 "Mobile-In", "Mobile-Out"
75 };
76 int i;
77 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
78 if ((cfg & 0x0f) < 7)
79 return bases[cfg & 0x0f];
80 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
81 if (cfg == specials_idx[i])
82 return specials[i];
83 }
84 return "UNKNOWN";
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040087
Takashi Iwaid5191e52009-11-16 14:58:17 +010088/**
89 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
90 * @cfg: pin default config value
91 *
92 * Parse the pin default config value and returns the string of the
93 * jack connectivity, i.e. external or internal connection.
94 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040095const char *snd_hda_get_jack_connectivity(u32 cfg)
96{
97 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
98
99 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
100}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100101EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400102
Takashi Iwaid5191e52009-11-16 14:58:17 +0100103/**
104 * snd_hda_get_jack_type - Give a type string of the jack
105 * @cfg: pin default config value
106 *
107 * Parse the pin default config value and returns the string of the
108 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
109 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400110const char *snd_hda_get_jack_type(u32 cfg)
111{
112 static char *jack_types[16] = {
113 "Line Out", "Speaker", "HP Out", "CD",
114 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
115 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200116 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400117 };
118
119 return jack_types[(cfg & AC_DEFCFG_DEVICE)
120 >> AC_DEFCFG_DEVICE_SHIFT];
121}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100122EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400123
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100124/*
125 * Compose a 32bit command word to be sent to the HD-audio controller
126 */
127static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200128make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100129 unsigned int verb, unsigned int parm)
130{
131 u32 val;
132
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200133 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200134 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100135 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200136 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800137 return ~0;
138 }
139
140 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100141 val |= (u32)nid << 20;
142 val |= verb << 8;
143 val |= parm;
144 return val;
145}
146
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200147/*
148 * Send and receive a verb
149 */
150static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200151 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200152{
153 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200154 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200155
Wu Fengguang6430aee2009-07-17 16:49:19 +0800156 if (cmd == ~0)
157 return -1;
158
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200159 if (res)
160 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200161 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200162 snd_hda_power_up(codec);
163 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200164 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
165 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100166 for (;;) {
167 trace_hda_send_cmd(codec, cmd);
168 err = bus->ops.command(bus, cmd);
169 if (err != -EAGAIN)
170 break;
171 /* process pending verbs */
172 bus->ops.get_response(bus, codec->addr);
173 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200174 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800175 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200176 trace_hda_get_response(codec, *res);
177 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200178 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200179 mutex_unlock(&bus->cmd_mutex);
180 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100181 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200182 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100183 codec_dbg(codec,
184 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200185 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200186 bus->ops.bus_reset(bus);
187 }
188 goto again;
189 }
190 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100191 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200192 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200193 return err;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
197 * snd_hda_codec_read - send a command and get the response
198 * @codec: the HDA codec
199 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200200 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * @verb: the verb to send
202 * @parm: the parameter for the verb
203 *
204 * Send a single command and read the corresponding response.
205 *
206 * Returns the obtained response value, or -1 for an error.
207 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200208unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200209 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int verb, unsigned int parm)
211{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200212 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200213 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200214 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700215 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return res;
217}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100218EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/**
221 * snd_hda_codec_write - send a single command without waiting for response
222 * @codec: the HDA codec
223 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200224 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * @verb: the verb to send
226 * @parm: the parameter for the verb
227 *
228 * Send a single command without waiting for response.
229 *
230 * Returns 0 if successful, or a negative error code.
231 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200232int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
233 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200235 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100236 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200237 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200238 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100240EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/**
243 * snd_hda_sequence_write - sequence writes
244 * @codec: the HDA codec
245 * @seq: VERB array to send
246 *
247 * Send the commands sequentially from the given array.
248 * The array must be terminated with NID=0.
249 */
250void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
251{
252 for (; seq->nid; seq++)
253 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
254}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100255EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/**
258 * snd_hda_get_sub_nodes - get the range of sub nodes
259 * @codec: the HDA codec
260 * @nid: NID to parse
261 * @start_id: the pointer to store the start NID
262 *
263 * Parse the NID and store the start NID of its sub-nodes.
264 * Returns the number of sub-nodes.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
267 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 unsigned int parm;
270
271 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300272 if (parm == -1) {
273 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200274 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *start_id = (parm >> 16) & 0x7fff;
277 return (int)(parm & 0x7fff);
278}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100279EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100281/* connection list element */
282struct hda_conn_list {
283 struct list_head list;
284 int len;
285 hda_nid_t nid;
286 hda_nid_t conns[0];
287};
288
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200289/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290static struct hda_conn_list *
291lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200292{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 struct hda_conn_list *p;
294 list_for_each_entry(p, &codec->conn_list, list) {
295 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200296 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200297 }
298 return NULL;
299}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200300
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100301static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
302 const hda_nid_t *list)
303{
304 struct hda_conn_list *p;
305
306 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
307 if (!p)
308 return -ENOMEM;
309 p->len = len;
310 p->nid = nid;
311 memcpy(p->conns, list, len * sizeof(hda_nid_t));
312 list_add(&p->list, &codec->conn_list);
313 return 0;
314}
315
316static void remove_conn_list(struct hda_codec *codec)
317{
318 while (!list_empty(&codec->conn_list)) {
319 struct hda_conn_list *p;
320 p = list_first_entry(&codec->conn_list, typeof(*p), list);
321 list_del(&p->list);
322 kfree(p);
323 }
324}
325
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200326/* read the connection and add to the cache */
327static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200328{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100329 hda_nid_t list[32];
330 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200331 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200332
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200333 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100334 if (len == -ENOSPC) {
335 len = snd_hda_get_num_raw_conns(codec, nid);
336 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
337 if (!result)
338 return -ENOMEM;
339 len = snd_hda_get_raw_connections(codec, nid, result, len);
340 }
341 if (len >= 0)
342 len = snd_hda_override_conn_list(codec, nid, len, result);
343 if (result != list)
344 kfree(result);
345 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346}
Takashi Iwaidce20792011-06-24 14:10:28 +0200347
348/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 * snd_hda_get_conn_list - get connection list
350 * @codec: the HDA codec
351 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100352 * @listp: the pointer to store NID list
353 *
354 * Parses the connection list of the given widget and stores the pointer
355 * to the list of NIDs.
356 *
357 * Returns the number of connections, or a negative error code.
358 *
359 * Note that the returned pointer isn't protected against the list
360 * modification. If snd_hda_override_conn_list() might be called
361 * concurrently, protect with a mutex appropriately.
362 */
363int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
364 const hda_nid_t **listp)
365{
366 bool added = false;
367
368 for (;;) {
369 int err;
370 const struct hda_conn_list *p;
371
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(codec, nid);
374 if (p) {
375 if (listp)
376 *listp = p->conns;
377 return p->len;
378 }
379 if (snd_BUG_ON(added))
380 return -EINVAL;
381
382 err = read_and_add_raw_conns(codec, nid);
383 if (err < 0)
384 return err;
385 added = true;
386 }
387}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100388EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100389
390/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * @codec: the HDA codec
393 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @max_conns: max. number of connections to store
396 *
397 * Parses the connection list of the given widget and stores the list
398 * of NIDs.
399 *
400 * Returns the number of connections, or a negative error code.
401 */
402int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200403 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 const hda_nid_t *list;
406 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100408 if (len > 0 && conn_list) {
409 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100410 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200411 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200412 return -EINVAL;
413 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100414 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200415 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200416
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100417 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200418}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100419EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200420
Takashi Iwai4eea3092013-02-07 18:18:19 +0100421/* return CONNLIST_LEN parameter of the given widget */
422static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
423{
424 unsigned int wcaps = get_wcaps(codec, nid);
425 unsigned int parm;
426
427 if (!(wcaps & AC_WCAP_CONN_LIST) &&
428 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
429 return 0;
430
431 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
432 if (parm == -1)
433 parm = 0;
434 return parm;
435}
436
437int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
438{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100439 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100440}
441
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200442/**
443 * snd_hda_get_raw_connections - copy connection list without cache
444 * @codec: the HDA codec
445 * @nid: NID to parse
446 * @conn_list: connection list array
447 * @max_conns: max. number of connections to store
448 *
449 * Like snd_hda_get_connections(), copy the connection list but without
450 * checking through the connection-list cache.
451 * Currently called only from hda_proc.c, so not exported.
452 */
453int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
454 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100457 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100460 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Takashi Iwai4eea3092013-02-07 18:18:19 +0100462 parm = get_num_conns(codec, nid);
463 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200464 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (parm & AC_CLIST_LONG) {
467 /* long form */
468 shift = 16;
469 num_elems = 2;
470 } else {
471 /* short form */
472 shift = 8;
473 num_elems = 4;
474 }
475 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 mask = (1 << (shift-1)) - 1;
477
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0; /* no connection */
480
481 if (conn_len == 1) {
482 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200483 parm = snd_hda_codec_read(codec, nid, 0,
484 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200485 if (parm == -1 && codec->bus->rirb_error)
486 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100487 if (conn_list)
488 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 1;
490 }
491
492 /* multi connection */
493 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100494 prev_nid = 0;
495 for (i = 0; i < conn_len; i++) {
496 int range_val;
497 hda_nid_t val, n;
498
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200499 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 parm = snd_hda_codec_read(codec, nid, 0,
501 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200502 if (parm == -1 && codec->bus->rirb_error)
503 return -EIO;
504 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200505 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100507 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100508 codec_dbg(codec,
509 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200510 nid, i, parm);
511 return 0;
512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 parm >>= shift;
514 if (range_val) {
515 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100517 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 "invalid dep_range_val %x:%x\n",
519 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100520 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100522 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100523 if (conn_list) {
524 if (conns >= max_conns)
525 return -ENOSPC;
526 conn_list[conns] = n;
527 }
528 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100529 }
530 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100531 if (conn_list) {
532 if (conns >= max_conns)
533 return -ENOSPC;
534 conn_list[conns] = val;
535 }
536 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100538 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 return conns;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200544 * snd_hda_override_conn_list - add/modify the connection-list to cache
545 * @codec: the HDA codec
546 * @nid: NID to parse
547 * @len: number of connection list entries
548 * @list: the list of connection entries
549 *
550 * Add or modify the given connection-list to the cache. If the corresponding
551 * cache already exists, invalidate it and append a new one.
552 *
553 * Returns zero or a negative error code.
554 */
555int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
556 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100558 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200559
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100560 p = lookup_conn_list(codec, nid);
561 if (p) {
562 list_del(&p->list);
563 kfree(p);
564 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200565
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100566 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100568EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200569
570/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200571 * snd_hda_get_conn_index - get the connection index of the given NID
572 * @codec: the HDA codec
573 * @mux: NID containing the list
574 * @nid: NID to select
575 * @recursive: 1 when searching NID recursively, otherwise 0
576 *
577 * Parses the connection list of the widget @mux and checks whether the
578 * widget @nid is present. If it is, return the connection index.
579 * Otherwise it returns -1.
580 */
581int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
582 hda_nid_t nid, int recursive)
583{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100584 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 int i, nums;
586
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100587 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 for (i = 0; i < nums; i++)
589 if (conn[i] == nid)
590 return i;
591 if (!recursive)
592 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100593 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200595 return -1;
596 }
597 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 for (i = 0; i < nums; i++) {
599 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
600 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
601 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200602 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
603 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200604 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200605 return -1;
606}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100607EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Mengdong Linf1aa0682013-08-26 21:35:21 -0400609
610/* return DEVLIST_LEN parameter of the given widget */
611static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
612{
613 unsigned int wcaps = get_wcaps(codec, nid);
614 unsigned int parm;
615
616 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
617 get_wcaps_type(wcaps) != AC_WID_PIN)
618 return 0;
619
620 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
621 if (parm == -1 && codec->bus->rirb_error)
622 parm = 0;
623 return parm & AC_DEV_LIST_LEN_MASK;
624}
625
626/**
627 * snd_hda_get_devices - copy device list without cache
628 * @codec: the HDA codec
629 * @nid: NID of the pin to parse
630 * @dev_list: device list array
631 * @max_devices: max. number of devices to store
632 *
633 * Copy the device list. This info is dynamic and so not cached.
634 * Currently called only from hda_proc.c, so not exported.
635 */
636int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
637 u8 *dev_list, int max_devices)
638{
639 unsigned int parm;
640 int i, dev_len, devices;
641
642 parm = get_num_devices(codec, nid);
643 if (!parm) /* not multi-stream capable */
644 return 0;
645
646 dev_len = parm + 1;
647 dev_len = dev_len < max_devices ? dev_len : max_devices;
648
649 devices = 0;
650 while (devices < dev_len) {
651 parm = snd_hda_codec_read(codec, nid, 0,
652 AC_VERB_GET_DEVICE_LIST, devices);
653 if (parm == -1 && codec->bus->rirb_error)
654 break;
655
656 for (i = 0; i < 8; i++) {
657 dev_list[devices] = (u8)parm;
658 parm >>= 4;
659 devices++;
660 if (devices >= dev_len)
661 break;
662 }
663 }
664 return devices;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Wang YanQing2195b062013-05-07 11:27:33 +0800684 if (!bus || !bus->workq)
685 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 Iwai6acaed32009-01-12 10:09:24 +0100696 queue_work(bus->workq, &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 Iwai6acaed32009-01-12 10:09:24 +0100735 if (bus->workq)
736 flush_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (bus->ops.private_free)
738 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100739 if (bus->workq)
740 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwai2565c892014-02-19 11:41:09 +0100747 snd_hda_bus_free(device->device_data);
748 return 0;
749}
750
751static int snd_hda_bus_dev_disconnect(struct snd_device *device)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100754 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
759 * snd_hda_bus_new - create a HDA bus
760 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * @busp: the pointer to store the created bus instance
762 *
763 * Returns 0 if successful, or a negative error code.
764 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100765int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100766 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct hda_bus *bus;
769 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100770 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100771 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .dev_free = snd_hda_bus_dev_free,
773 };
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (busp)
776 *busp = NULL;
777
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200778 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100780 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENOMEM;
782 }
783
784 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100785 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200786 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100788 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100790 snprintf(bus->workq_name, sizeof(bus->workq_name),
791 "hd-audio%d", card->number);
792 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100793 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100794 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100795 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100796 kfree(bus);
797 return -ENOMEM;
798 }
799
Takashi Iwai0ba21762007-04-16 11:29:14 +0200800 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
801 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 snd_hda_bus_free(bus);
803 return err;
804 }
805 if (busp)
806 *busp = bus;
807 return 0;
808}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100809EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200812 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100814static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200816 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hda_nid_t nid;
818
819 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
820 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200821 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200822 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200823 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200824 case AC_GRP_AUDIO_FUNCTION:
825 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200826 codec->afg_function_id = function_id & 0xff;
827 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200828 break;
829 case AC_GRP_MODEM_FUNCTION:
830 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200831 codec->mfg_function_id = function_id & 0xff;
832 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200833 break;
834 default:
835 break;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100841 * read widget caps for each widget and store in cache
842 */
843static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
844{
845 int i;
846 hda_nid_t nid;
847
848 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
849 &codec->start_nid);
850 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200851 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100852 return -ENOMEM;
853 nid = codec->start_nid;
854 for (i = 0; i < codec->num_nodes; i++, nid++)
855 codec->wcaps[i] = snd_hda_param_read(codec, nid,
856 AC_PAR_AUDIO_WIDGET_CAP);
857 return 0;
858}
859
Takashi Iwai3be14142009-02-20 14:11:16 +0100860/* read all pin default configurations and save codec->init_pins */
861static int read_pin_defaults(struct hda_codec *codec)
862{
863 int i;
864 hda_nid_t nid = codec->start_nid;
865
866 for (i = 0; i < codec->num_nodes; i++, nid++) {
867 struct hda_pincfg *pin;
868 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200869 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100870 if (wid_type != AC_WID_PIN)
871 continue;
872 pin = snd_array_new(&codec->init_pins);
873 if (!pin)
874 return -ENOMEM;
875 pin->nid = nid;
876 pin->cfg = snd_hda_codec_read(codec, nid, 0,
877 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200878 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
879 AC_VERB_GET_PIN_WIDGET_CONTROL,
880 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 }
882 return 0;
883}
884
885/* look up the given pin config list and return the item matching with NID */
886static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
887 struct snd_array *array,
888 hda_nid_t nid)
889{
890 int i;
891 for (i = 0; i < array->used; i++) {
892 struct hda_pincfg *pin = snd_array_elem(array, i);
893 if (pin->nid == nid)
894 return pin;
895 }
896 return NULL;
897}
898
Takashi Iwai3be14142009-02-20 14:11:16 +0100899/* set the current pin config value for the given NID.
900 * the value is cached, and read via snd_hda_codec_get_pincfg()
901 */
902int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
903 hda_nid_t nid, unsigned int cfg)
904{
905 struct hda_pincfg *pin;
906
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200907 /* the check below may be invalid when pins are added by a fixup
908 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
909 * for now
910 */
911 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100912 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
913 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200914 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100915
Takashi Iwai3be14142009-02-20 14:11:16 +0100916 pin = look_up_pincfg(codec, list, nid);
917 if (!pin) {
918 pin = snd_array_new(list);
919 if (!pin)
920 return -ENOMEM;
921 pin->nid = nid;
922 }
923 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100924 return 0;
925}
926
Takashi Iwaid5191e52009-11-16 14:58:17 +0100927/**
928 * snd_hda_codec_set_pincfg - Override a pin default configuration
929 * @codec: the HDA codec
930 * @nid: NID to set the pin config
931 * @cfg: the pin default config value
932 *
933 * Override a pin default configuration value in the cache.
934 * This value can be read by snd_hda_codec_get_pincfg() in a higher
935 * priority than the real hardware value.
936 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100937int snd_hda_codec_set_pincfg(struct hda_codec *codec,
938 hda_nid_t nid, unsigned int cfg)
939{
Takashi Iwai346ff702009-02-23 09:42:57 +0100940 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100941}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100942EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100943
Takashi Iwaid5191e52009-11-16 14:58:17 +0100944/**
945 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
946 * @codec: the HDA codec
947 * @nid: NID to get the pin config
948 *
949 * Get the current pin config value of the given pin NID.
950 * If the pincfg value is cached or overridden via sysfs or driver,
951 * returns the cached value.
952 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100953unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
954{
955 struct hda_pincfg *pin;
956
Takashi Iwai648a8d22014-02-25 10:38:13 +0100957#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100958 {
959 unsigned int cfg = 0;
960 mutex_lock(&codec->user_mutex);
961 pin = look_up_pincfg(codec, &codec->user_pins, nid);
962 if (pin)
963 cfg = pin->cfg;
964 mutex_unlock(&codec->user_mutex);
965 if (cfg)
966 return cfg;
967 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100968#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100969 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
970 if (pin)
971 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100972 pin = look_up_pincfg(codec, &codec->init_pins, nid);
973 if (pin)
974 return pin->cfg;
975 return 0;
976}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100977EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978
Takashi Iwai95a962c2014-10-29 16:03:58 +0100979/**
980 * snd_hda_codec_set_pin_target - remember the current pinctl target value
981 * @codec: the HDA codec
982 * @nid: pin NID
983 * @val: assigned pinctl value
984 *
985 * This function stores the given value to a pinctl target value in the
986 * pincfg table. This isn't always as same as the actually written value
987 * but can be referred at any time via snd_hda_codec_get_pin_target().
988 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100989int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
990 unsigned int val)
991{
992 struct hda_pincfg *pin;
993
994 pin = look_up_pincfg(codec, &codec->init_pins, nid);
995 if (!pin)
996 return -EINVAL;
997 pin->target = val;
998 return 0;
999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001000EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001001
Takashi Iwai95a962c2014-10-29 16:03:58 +01001002/**
1003 * snd_hda_codec_get_pin_target - return the current pinctl target value
1004 * @codec: the HDA codec
1005 * @nid: pin NID
1006 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001007int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1008{
1009 struct hda_pincfg *pin;
1010
1011 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1012 if (!pin)
1013 return 0;
1014 return pin->target;
1015}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001016EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001017
Takashi Iwai92ee6162009-12-27 11:18:59 +01001018/**
1019 * snd_hda_shutup_pins - Shut up all pins
1020 * @codec: the HDA codec
1021 *
1022 * Clear all pin controls to shup up before suspend for avoiding click noise.
1023 * The controls aren't cached so that they can be resumed properly.
1024 */
1025void snd_hda_shutup_pins(struct hda_codec *codec)
1026{
1027 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001028 /* don't shut up pins when unloading the driver; otherwise it breaks
1029 * the default pin setup at the next load of the driver
1030 */
1031 if (codec->bus->shutdown)
1032 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001033 for (i = 0; i < codec->init_pins.used; i++) {
1034 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1035 /* use read here for syncing after issuing each verb */
1036 snd_hda_codec_read(codec, pin->nid, 0,
1037 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1038 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001039 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001040}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001041EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001042
Takashi Iwai2a439522011-07-26 09:52:50 +02001043#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001044/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1045static void restore_shutup_pins(struct hda_codec *codec)
1046{
1047 int i;
1048 if (!codec->pins_shutup)
1049 return;
1050 if (codec->bus->shutdown)
1051 return;
1052 for (i = 0; i < codec->init_pins.used; i++) {
1053 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1054 snd_hda_codec_write(codec, pin->nid, 0,
1055 AC_VERB_SET_PIN_WIDGET_CONTROL,
1056 pin->ctrl);
1057 }
1058 codec->pins_shutup = 0;
1059}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001060#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001061
David Henningsson26a6cb62012-10-09 15:04:21 +02001062static void hda_jackpoll_work(struct work_struct *work)
1063{
1064 struct hda_codec *codec =
1065 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001066
1067 snd_hda_jack_set_dirty_all(codec);
1068 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001069
1070 if (!codec->jackpoll_interval)
1071 return;
1072
David Henningsson26a6cb62012-10-09 15:04:21 +02001073 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1074 codec->jackpoll_interval);
1075}
1076
Takashi Iwai01751f52007-08-10 16:59:39 +02001077static void init_hda_cache(struct hda_cache_rec *cache,
1078 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001079static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001080
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001081/* release all pincfg lists */
1082static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001083{
Takashi Iwai346ff702009-02-23 09:42:57 +01001084 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001085#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001086 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001087#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 snd_array_free(&codec->init_pins);
1089}
1090
Takashi Iwai54d17402005-11-21 16:33:22 +01001091/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001092 * audio-converter setup caches
1093 */
1094struct hda_cvt_setup {
1095 hda_nid_t nid;
1096 u8 stream_tag;
1097 u8 channel_id;
1098 u16 format_id;
1099 unsigned char active; /* cvt is currently used */
1100 unsigned char dirty; /* setups should be cleared */
1101};
1102
1103/* get or create a cache entry for the given audio converter NID */
1104static struct hda_cvt_setup *
1105get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1106{
1107 struct hda_cvt_setup *p;
1108 int i;
1109
1110 for (i = 0; i < codec->cvt_setups.used; i++) {
1111 p = snd_array_elem(&codec->cvt_setups, i);
1112 if (p->nid == nid)
1113 return p;
1114 }
1115 p = snd_array_new(&codec->cvt_setups);
1116 if (p)
1117 p->nid = nid;
1118 return p;
1119}
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * codec destructor
1123 */
1124static void snd_hda_codec_free(struct hda_codec *codec)
1125{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001126 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001128 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001129 if (device_is_registered(hda_codec_dev(codec)))
1130 device_del(hda_codec_dev(codec));
Takashi Iwai59cad162012-06-26 15:00:20 +02001131 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001132 free_init_pincfgs(codec);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001133 flush_workqueue(codec->bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001135 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001136 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001137 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001138 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001139 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 codec->bus->caddr_tbl[codec->addr] = NULL;
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001141 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001142 snd_hda_sysfs_clear(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001144 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001145 kfree(codec->vendor_name);
1146 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001147 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001148 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001149 codec->bus->num_codecs--;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001150 put_device(hda_codec_dev(codec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001153static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1154 hda_nid_t fg, unsigned int power_state);
1155
Takashi Iwaid8193872012-08-31 07:54:38 -07001156static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001157 unsigned int power_state);
1158
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001159static int snd_hda_codec_dev_register(struct snd_device *device)
1160{
1161 struct hda_codec *codec = device->device_data;
1162
Takashi Iwaid604b392014-02-28 13:42:09 +01001163 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001164 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001165 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwaid604b392014-02-28 13:42:09 +01001166 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001167}
1168
1169static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1170{
1171 struct hda_codec *codec = device->device_data;
1172
Takashi Iwaid604b392014-02-28 13:42:09 +01001173 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001174 return 0;
1175}
1176
Takashi Iwai2565c892014-02-19 11:41:09 +01001177static int snd_hda_codec_dev_free(struct snd_device *device)
1178{
1179 snd_hda_codec_free(device->device_data);
1180 return 0;
1181}
1182
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001183/* just free the container */
1184static void snd_hda_codec_dev_release(struct device *dev)
1185{
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001186 kfree(dev_to_hda_codec(dev));
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189/**
1190 * snd_hda_codec_new - create a HDA codec
1191 * @bus: the bus to assign
1192 * @codec_addr: the codec address
1193 * @codecp: the pointer to store the generated codec
1194 *
1195 * Returns 0 if successful, or a negative error code.
1196 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001197int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001198 unsigned int codec_addr,
1199 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001202 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001203 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001204 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001206 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001207 .dev_register = snd_hda_codec_dev_register,
1208 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001209 .dev_free = snd_hda_codec_dev_free,
1210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Takashi Iwaida3cec32008-08-08 17:12:14 +02001212 if (snd_BUG_ON(!bus))
1213 return -EINVAL;
1214 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001218 dev_err(bus->card->dev,
1219 "address 0x%x is already occupied\n",
1220 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -EBUSY;
1222 }
1223
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001224 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001226 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -ENOMEM;
1228 }
1229
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001230 dev = hda_codec_dev(codec);
1231 device_initialize(dev);
1232 dev->parent = bus->card->dev;
1233 dev->bus = &snd_hda_bus_type;
1234 dev->release = snd_hda_codec_dev_release;
1235 dev->groups = snd_hda_dev_attr_groups;
1236 dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr);
1237 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001238 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 codec->bus = bus;
1241 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001242 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001243 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001244 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001245 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001246 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001247 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1248 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001249 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001250 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001251 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001252 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001253 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001254 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001255 INIT_LIST_HEAD(&codec->conn_list);
1256
David Henningsson26a6cb62012-10-09 15:04:21 +02001257 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001258 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001259 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Takashi Iwai83012a72012-08-24 18:38:08 +02001261#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001262 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1263 * the caller has to power down appropriatley after initialization
1264 * phase.
1265 */
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001266 set_bit(codec->addr, &bus->codec_powered);
Takashi Iwaicc72da72015-02-19 16:00:22 +01001267 pm_runtime_set_active(hda_codec_dev(codec));
1268 pm_runtime_get_noresume(hda_codec_dev(codec));
1269 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001270#endif
1271
Takashi Iwai648a8d22014-02-25 10:38:13 +01001272 snd_hda_sysfs_init(codec);
1273
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001274 if (codec->bus->modelname) {
1275 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1276 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001277 err = -ENODEV;
1278 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001279 }
1280 }
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001283 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 bus->caddr_tbl[codec_addr] = codec;
1286
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001289 if (codec->vendor_id == -1)
1290 /* read again, hopefully the access method was corrected
1291 * in the last read...
1292 */
1293 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1294 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_SUBSYSTEM_ID);
1297 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001300 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001302 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 err = -ENODEV;
1304 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Takashi Iwaid8193872012-08-31 07:54:38 -07001307 fg = codec->afg ? codec->afg : codec->mfg;
1308 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001310 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001311 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001312 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001313 err = read_pin_defaults(codec);
1314 if (err < 0)
1315 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001316
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001320 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001321 }
1322
Takashi Iwai83012a72012-08-24 18:38:08 +02001323#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001324 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001325 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001326#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001327 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001328 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001329
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001330 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001331 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001332
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333 snd_hda_codec_proc_new(codec);
1334
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001335 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336
1337 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1338 codec->subsystem_id, codec->revision_id);
1339 snd_component_add(codec->bus->card, component);
1340
Takashi Iwai2565c892014-02-19 11:41:09 +01001341 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1342 if (err < 0)
1343 goto error;
1344
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345 if (codecp)
1346 *codecp = codec;
1347 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001348
1349 error:
1350 snd_hda_codec_free(codec);
1351 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001353EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001354
Takashi Iwai95a962c2014-10-29 16:03:58 +01001355/**
1356 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1357 * @codec: the HDA codec
1358 *
1359 * Forcibly refresh the all widget caps and the init pin configurations of
1360 * the given codec.
1361 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001362int snd_hda_codec_update_widgets(struct hda_codec *codec)
1363{
1364 hda_nid_t fg;
1365 int err;
1366
1367 /* Assume the function group node does not change,
1368 * only the widget nodes may change.
1369 */
1370 kfree(codec->wcaps);
1371 fg = codec->afg ? codec->afg : codec->mfg;
1372 err = read_widget_caps(codec, fg);
1373 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001374 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001375 return err;
1376 }
1377
1378 snd_array_free(&codec->init_pins);
1379 err = read_pin_defaults(codec);
1380
1381 return err;
1382}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001383EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001384
Takashi Iwaied360812012-08-08 17:12:52 +02001385/* update the stream-id if changed */
1386static void update_pcm_stream_id(struct hda_codec *codec,
1387 struct hda_cvt_setup *p, hda_nid_t nid,
1388 u32 stream_tag, int channel_id)
1389{
1390 unsigned int oldval, newval;
1391
1392 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1393 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1394 newval = (stream_tag << 4) | channel_id;
1395 if (oldval != newval)
1396 snd_hda_codec_write(codec, nid, 0,
1397 AC_VERB_SET_CHANNEL_STREAMID,
1398 newval);
1399 p->stream_tag = stream_tag;
1400 p->channel_id = channel_id;
1401 }
1402}
1403
1404/* update the format-id if changed */
1405static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1406 hda_nid_t nid, int format)
1407{
1408 unsigned int oldval;
1409
1410 if (p->format_id != format) {
1411 oldval = snd_hda_codec_read(codec, nid, 0,
1412 AC_VERB_GET_STREAM_FORMAT, 0);
1413 if (oldval != format) {
1414 msleep(1);
1415 snd_hda_codec_write(codec, nid, 0,
1416 AC_VERB_SET_STREAM_FORMAT,
1417 format);
1418 }
1419 p->format_id = format;
1420 }
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/**
1424 * snd_hda_codec_setup_stream - set up the codec for streaming
1425 * @codec: the CODEC to set up
1426 * @nid: the NID to set up
1427 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1428 * @channel_id: channel id to pass, zero based.
1429 * @format: stream format.
1430 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001431void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1432 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int channel_id, int format)
1434{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001435 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001436 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001437 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001438 int i;
1439
Takashi Iwai0ba21762007-04-16 11:29:14 +02001440 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001441 return;
1442
Takashi Iwai4e76a882014-02-25 12:21:03 +01001443 codec_dbg(codec,
1444 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1445 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001446 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001447 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001448 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001449
1450 if (codec->pcm_format_first)
1451 update_pcm_format(codec, p, nid, format);
1452 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1453 if (!codec->pcm_format_first)
1454 update_pcm_format(codec, p, nid, format);
1455
Takashi Iwaieb541332010-08-06 13:48:11 +02001456 p->active = 1;
1457 p->dirty = 0;
1458
1459 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001460 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001461 list_for_each_entry(c, &codec->bus->codec_list, list) {
1462 for (i = 0; i < c->cvt_setups.used; i++) {
1463 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001464 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001465 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001466 p->dirty = 1;
1467 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001470EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Takashi Iwaif0cea792010-08-13 11:56:53 +02001472static void really_cleanup_stream(struct hda_codec *codec,
1473 struct hda_cvt_setup *q);
1474
Takashi Iwaid5191e52009-11-16 14:58:17 +01001475/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001476 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001477 * @codec: the CODEC to clean up
1478 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001479 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001480 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001481void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1482 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001483{
Takashi Iwaieb541332010-08-06 13:48:11 +02001484 struct hda_cvt_setup *p;
1485
Takashi Iwai888afa12008-03-18 09:57:50 +01001486 if (!nid)
1487 return;
1488
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001489 if (codec->no_sticky_stream)
1490 do_now = 1;
1491
Takashi Iwai4e76a882014-02-25 12:21:03 +01001492 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001493 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001494 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001495 /* here we just clear the active flag when do_now isn't set;
1496 * actual clean-ups will be done later in
1497 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1498 */
1499 if (do_now)
1500 really_cleanup_stream(codec, p);
1501 else
1502 p->active = 0;
1503 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001504}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001505EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001506
Takashi Iwaieb541332010-08-06 13:48:11 +02001507static void really_cleanup_stream(struct hda_codec *codec,
1508 struct hda_cvt_setup *q)
1509{
1510 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001511 if (q->stream_tag || q->channel_id)
1512 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1513 if (q->format_id)
1514 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1515);
Takashi Iwaieb541332010-08-06 13:48:11 +02001516 memset(q, 0, sizeof(*q));
1517 q->nid = nid;
1518}
1519
1520/* clean up the all conflicting obsolete streams */
1521static void purify_inactive_streams(struct hda_codec *codec)
1522{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001523 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001524 int i;
1525
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001526 list_for_each_entry(c, &codec->bus->codec_list, list) {
1527 for (i = 0; i < c->cvt_setups.used; i++) {
1528 struct hda_cvt_setup *p;
1529 p = snd_array_elem(&c->cvt_setups, i);
1530 if (p->dirty)
1531 really_cleanup_stream(c, p);
1532 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001533 }
1534}
1535
Takashi Iwai2a439522011-07-26 09:52:50 +02001536#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001537/* clean up all streams; called from suspend */
1538static void hda_cleanup_all_streams(struct hda_codec *codec)
1539{
1540 int i;
1541
1542 for (i = 0; i < codec->cvt_setups.used; i++) {
1543 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1544 if (p->stream_tag)
1545 really_cleanup_stream(codec, p);
1546 }
1547}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001548#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * amp access functions
1552 */
1553
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001554/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001555#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001556#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001557#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1558#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001560#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001563static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001564 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Takashi Iwai01751f52007-08-10 16:59:39 +02001566 memset(cache, 0, sizeof(*cache));
1567 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001568 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001569}
1570
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001571static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001572{
Takashi Iwai603c4012008-07-30 15:01:44 +02001573 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001577static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwai01751f52007-08-10 16:59:39 +02001579 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1580 u16 cur = cache->hash[idx];
1581 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001584 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (info->key == key)
1586 return info;
1587 cur = info->next;
1588 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001589 return NULL;
1590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001592/* query the hash. allocate an entry if not found. */
1593static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1594 u32 key)
1595{
1596 struct hda_cache_head *info = get_hash(cache, key);
1597 if (!info) {
1598 u16 idx, cur;
1599 /* add a new hash entry */
1600 info = snd_array_new(&cache->buf);
1601 if (!info)
1602 return NULL;
1603 cur = snd_array_index(&cache->buf, info);
1604 info->key = key;
1605 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001606 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001607 idx = key % (u16)ARRAY_SIZE(cache->hash);
1608 info->next = cache->hash[idx];
1609 cache->hash[idx] = cur;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return info;
1612}
1613
Takashi Iwai01751f52007-08-10 16:59:39 +02001614/* query and allocate an amp hash entry */
1615static inline struct hda_amp_info *
1616get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1617{
1618 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1619}
1620
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001621/* overwrite the value with the key in the caps hash */
1622static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1623{
1624 struct hda_amp_info *info;
1625
1626 mutex_lock(&codec->hash_mutex);
1627 info = get_alloc_amp_hash(codec, key);
1628 if (!info) {
1629 mutex_unlock(&codec->hash_mutex);
1630 return -EINVAL;
1631 }
1632 info->amp_caps = val;
1633 info->head.val |= INFO_AMP_CAPS;
1634 mutex_unlock(&codec->hash_mutex);
1635 return 0;
1636}
1637
1638/* query the value from the caps hash; if not found, fetch the current
1639 * value from the given function and store in the hash
1640 */
1641static unsigned int
1642query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1643 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1644{
1645 struct hda_amp_info *info;
1646 unsigned int val;
1647
1648 mutex_lock(&codec->hash_mutex);
1649 info = get_alloc_amp_hash(codec, key);
1650 if (!info) {
1651 mutex_unlock(&codec->hash_mutex);
1652 return 0;
1653 }
1654 if (!(info->head.val & INFO_AMP_CAPS)) {
1655 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1656 val = func(codec, nid, dir);
1657 write_caps_hash(codec, key, val);
1658 } else {
1659 val = info->amp_caps;
1660 mutex_unlock(&codec->hash_mutex);
1661 }
1662 return val;
1663}
1664
1665static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1666 int direction)
1667{
1668 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1669 nid = codec->afg;
1670 return snd_hda_param_read(codec, nid,
1671 direction == HDA_OUTPUT ?
1672 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1673}
1674
Takashi Iwaid5191e52009-11-16 14:58:17 +01001675/**
1676 * query_amp_caps - query AMP capabilities
1677 * @codec: the HD-auio codec
1678 * @nid: the NID to query
1679 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1680 *
1681 * Query AMP capabilities for the given widget and direction.
1682 * Returns the obtained capability bits.
1683 *
1684 * When cap bits have been already read, this doesn't read again but
1685 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001687u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001689 return query_caps_hash(codec, nid, direction,
1690 HDA_HASH_KEY(nid, direction, 0),
1691 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001693EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Takashi Iwaid5191e52009-11-16 14:58:17 +01001695/**
David Henningsson861a04e2014-09-23 10:38:17 +02001696 * snd_hda_check_amp_caps - query AMP capabilities
1697 * @codec: the HD-audio codec
1698 * @nid: the NID to query
1699 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001700 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001701 *
1702 * Check whether the widget has the given amp capability for the direction.
1703 */
1704bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1705 int dir, unsigned int bits)
1706{
1707 if (!nid)
1708 return false;
1709 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1710 if (query_amp_caps(codec, nid, dir) & bits)
1711 return true;
1712 return false;
1713}
1714EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1715
1716/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001717 * snd_hda_override_amp_caps - Override the AMP capabilities
1718 * @codec: the CODEC to clean up
1719 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001720 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001721 * @caps: the capability bits to set
1722 *
1723 * Override the cached AMP caps bits value by the given one.
1724 * This function is useful if the driver needs to adjust the AMP ranges,
1725 * e.g. limit to 0dB, etc.
1726 *
1727 * Returns zero if successful or a negative error code.
1728 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001729int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1730 unsigned int caps)
1731{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001732 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001733}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001734EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001735
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001736static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1737 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001738{
1739 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1740}
1741
Takashi Iwaid5191e52009-11-16 14:58:17 +01001742/**
1743 * snd_hda_query_pin_caps - Query PIN capabilities
1744 * @codec: the HD-auio codec
1745 * @nid: the NID to query
1746 *
1747 * Query PIN capabilities for the given widget.
1748 * Returns the obtained capability bits.
1749 *
1750 * When cap bits have been already read, this doesn't read again but
1751 * returns the cached value.
1752 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001753u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1754{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001756 read_pin_cap);
1757}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001758EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001759
Wu Fengguang864f92b2009-11-18 12:38:02 +08001760/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001761 * snd_hda_override_pin_caps - Override the pin capabilities
1762 * @codec: the CODEC
1763 * @nid: the NID to override
1764 * @caps: the capability bits to set
1765 *
1766 * Override the cached PIN capabilitiy bits value by the given one.
1767 *
1768 * Returns zero if successful or a negative error code.
1769 */
1770int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1771 unsigned int caps)
1772{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001773 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001774}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001775EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001776
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001777/* read or sync the hash value with the current value;
1778 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001780static struct hda_amp_info *
1781update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001782 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001784 struct hda_amp_info *info;
1785 unsigned int parm, val = 0;
1786 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001788 retry:
1789 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1790 if (!info)
1791 return NULL;
1792 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1793 if (!val_read) {
1794 mutex_unlock(&codec->hash_mutex);
1795 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1796 parm |= direction == HDA_OUTPUT ?
1797 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1798 parm |= index;
1799 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001800 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001801 val &= 0xff;
1802 val_read = true;
1803 mutex_lock(&codec->hash_mutex);
1804 goto retry;
1805 }
1806 info->vol[ch] = val;
1807 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001808 } else if (init_only)
1809 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001810 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001814 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001816static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001817 hda_nid_t nid, int ch, int direction, int index,
1818 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 u32 parm;
1821
1822 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1823 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1824 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001825 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1826 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001827 ; /* set the zero value as a fake mute */
1828 else
1829 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1831}
1832
Takashi Iwaid5191e52009-11-16 14:58:17 +01001833/**
1834 * snd_hda_codec_amp_read - Read AMP value
1835 * @codec: HD-audio codec
1836 * @nid: NID to read the AMP value
1837 * @ch: channel (left=0 or right=1)
1838 * @direction: #HDA_INPUT or #HDA_OUTPUT
1839 * @index: the index value (only for input direction)
1840 *
1841 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Takashi Iwai834be882006-03-01 14:16:17 +01001843int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1844 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001846 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001847 unsigned int val = 0;
1848
1849 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001850 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001851 if (info)
1852 val = info->vol[ch];
1853 mutex_unlock(&codec->hash_mutex);
1854 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001856EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Takashi Iwai280e57d2012-12-14 10:32:21 +01001858static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1859 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001860 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001861{
1862 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001863 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001864
1865 if (snd_BUG_ON(mask & ~0xff))
1866 mask &= 0xff;
1867 val &= mask;
1868
1869 mutex_lock(&codec->hash_mutex);
1870 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1871 if (!info) {
1872 mutex_unlock(&codec->hash_mutex);
1873 return 0;
1874 }
1875 val |= info->vol[ch] & ~mask;
1876 if (info->vol[ch] == val) {
1877 mutex_unlock(&codec->hash_mutex);
1878 return 0;
1879 }
1880 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001881 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001882 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001883 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001884 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001885 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001886 return 1;
1887}
1888
Takashi Iwaid5191e52009-11-16 14:58:17 +01001889/**
1890 * snd_hda_codec_amp_update - update the AMP value
1891 * @codec: HD-audio codec
1892 * @nid: NID to read the AMP value
1893 * @ch: channel (left=0 or right=1)
1894 * @direction: #HDA_INPUT or #HDA_OUTPUT
1895 * @idx: the index value (only for input direction)
1896 * @mask: bit mask to set
1897 * @val: the bits value to set
1898 *
1899 * Update the AMP value with a bit mask.
1900 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001901 */
Takashi Iwai834be882006-03-01 14:16:17 +01001902int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1903 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001905 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1906 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001908EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Takashi Iwaid5191e52009-11-16 14:58:17 +01001910/**
1911 * snd_hda_codec_amp_stereo - update the AMP stereo values
1912 * @codec: HD-audio codec
1913 * @nid: NID to read the AMP value
1914 * @direction: #HDA_INPUT or #HDA_OUTPUT
1915 * @idx: the index value (only for input direction)
1916 * @mask: bit mask to set
1917 * @val: the bits value to set
1918 *
1919 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1920 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001921 */
1922int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1923 int direction, int idx, int mask, int val)
1924{
1925 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001926
1927 if (snd_BUG_ON(mask & ~0xff))
1928 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001929 for (ch = 0; ch < 2; ch++)
1930 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1931 idx, mask, val);
1932 return ret;
1933}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001934EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001935
Takashi Iwai95a962c2014-10-29 16:03:58 +01001936/**
1937 * snd_hda_codec_amp_init - initialize the AMP value
1938 * @codec: the HDA codec
1939 * @nid: NID to read the AMP value
1940 * @ch: channel (left=0 or right=1)
1941 * @dir: #HDA_INPUT or #HDA_OUTPUT
1942 * @idx: the index value (only for input direction)
1943 * @mask: bit mask to set
1944 * @val: the bits value to set
1945 *
1946 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001947 * the first access. If the amp was already initialized / updated beforehand,
1948 * this does nothing.
1949 */
1950int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1951 int dir, int idx, int mask, int val)
1952{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001953 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1954 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001955}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001956EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001957
Takashi Iwai95a962c2014-10-29 16:03:58 +01001958/**
1959 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1960 * @codec: the HDA codec
1961 * @nid: NID to read the AMP value
1962 * @dir: #HDA_INPUT or #HDA_OUTPUT
1963 * @idx: the index value (only for input direction)
1964 * @mask: bit mask to set
1965 * @val: the bits value to set
1966 *
1967 * Call snd_hda_codec_amp_init() for both stereo channels.
1968 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001969int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1970 int dir, int idx, int mask, int val)
1971{
1972 int ch, ret = 0;
1973
1974 if (snd_BUG_ON(mask & ~0xff))
1975 mask &= 0xff;
1976 for (ch = 0; ch < 2; ch++)
1977 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1978 idx, mask, val);
1979 return ret;
1980}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001981EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001982
Takashi Iwaid5191e52009-11-16 14:58:17 +01001983/**
1984 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1985 * @codec: HD-audio codec
1986 *
1987 * Resume the all amp commands from the cache.
1988 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001989void snd_hda_codec_resume_amp(struct hda_codec *codec)
1990{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001991 int i;
1992
Takashi Iwaic370dd62012-12-13 18:30:04 +01001993 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001994 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001995 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1996 struct hda_amp_info *buffer;
1997 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001998 hda_nid_t nid;
1999 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002000 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002001
2002 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002003 if (!buffer->head.dirty)
2004 continue;
2005 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002006 info = *buffer;
2007 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002008 if (!key)
2009 continue;
2010 nid = key & 0xff;
2011 idx = (key >> 16) & 0xff;
2012 dir = (key >> 24) & 0xff;
2013 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002014 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002015 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002016 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002017 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2018 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002019 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002020 }
2021 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002022 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002023}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002024EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002026static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2027 unsigned int ofs)
2028{
2029 u32 caps = query_amp_caps(codec, nid, dir);
2030 /* get num steps */
2031 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2032 if (ofs < caps)
2033 caps -= ofs;
2034 return caps;
2035}
2036
Takashi Iwaid5191e52009-11-16 14:58:17 +01002037/**
2038 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002039 * @kcontrol: referred ctl element
2040 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002041 *
2042 * The control element is supposed to have the private_value field
2043 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2044 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002045int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2049 u16 nid = get_amp_nid(kcontrol);
2050 u8 chs = get_amp_channels(kcontrol);
2051 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002052 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002054 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2055 uinfo->count = chs == 3 ? 2 : 1;
2056 uinfo->value.integer.min = 0;
2057 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2058 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002059 codec_warn(codec,
2060 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2061 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return -EINVAL;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
2065}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002066EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002068
2069static inline unsigned int
2070read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2071 int ch, int dir, int idx, unsigned int ofs)
2072{
2073 unsigned int val;
2074 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2075 val &= HDA_AMP_VOLMASK;
2076 if (val >= ofs)
2077 val -= ofs;
2078 else
2079 val = 0;
2080 return val;
2081}
2082
2083static inline int
2084update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2085 int ch, int dir, int idx, unsigned int ofs,
2086 unsigned int val)
2087{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002088 unsigned int maxval;
2089
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002090 if (val > 0)
2091 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002092 /* ofs = 0: raw max value */
2093 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002094 if (val > maxval)
2095 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002096 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2097 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002098}
2099
Takashi Iwaid5191e52009-11-16 14:58:17 +01002100/**
2101 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002102 * @kcontrol: ctl element
2103 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002104 *
2105 * The control element is supposed to have the private_value field
2106 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2107 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2109 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2112 hda_nid_t nid = get_amp_nid(kcontrol);
2113 int chs = get_amp_channels(kcontrol);
2114 int dir = get_amp_direction(kcontrol);
2115 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002116 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 long *valp = ucontrol->value.integer.value;
2118
2119 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002120 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002122 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002125EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Takashi Iwaid5191e52009-11-16 14:58:17 +01002127/**
2128 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002129 * @kcontrol: ctl element
2130 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002131 *
2132 * The control element is supposed to have the private_value field
2133 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2134 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002135int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2139 hda_nid_t nid = get_amp_nid(kcontrol);
2140 int chs = get_amp_channels(kcontrol);
2141 int dir = get_amp_direction(kcontrol);
2142 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002143 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 long *valp = ucontrol->value.integer.value;
2145 int change = 0;
2146
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002147 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002148 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002149 valp++;
2150 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002151 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002152 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return change;
2154}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002155EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Takashi Iwaid5191e52009-11-16 14:58:17 +01002157/**
2158 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002159 * @kcontrol: ctl element
2160 * @op_flag: operation flag
2161 * @size: byte size of input TLV
2162 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163 *
2164 * The control element is supposed to have the private_value field
2165 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2166 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002167int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2168 unsigned int size, unsigned int __user *_tlv)
2169{
2170 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2171 hda_nid_t nid = get_amp_nid(kcontrol);
2172 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002173 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002174 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002175 u32 caps, val1, val2;
2176
2177 if (size < 4 * sizeof(unsigned int))
2178 return -ENOMEM;
2179 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002180 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2181 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002182 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002183 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002184 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002185 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002186 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002187 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2188 return -EFAULT;
2189 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2190 return -EFAULT;
2191 if (put_user(val1, _tlv + 2))
2192 return -EFAULT;
2193 if (put_user(val2, _tlv + 3))
2194 return -EFAULT;
2195 return 0;
2196}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002197EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002198
Takashi Iwaid5191e52009-11-16 14:58:17 +01002199/**
2200 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2201 * @codec: HD-audio codec
2202 * @nid: NID of a reference widget
2203 * @dir: #HDA_INPUT or #HDA_OUTPUT
2204 * @tlv: TLV data to be stored, at least 4 elements
2205 *
2206 * Set (static) TLV data for a virtual master volume using the AMP caps
2207 * obtained from the reference NID.
2208 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002209 */
2210void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2211 unsigned int *tlv)
2212{
2213 u32 caps;
2214 int nums, step;
2215
2216 caps = query_amp_caps(codec, nid, dir);
2217 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2218 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2219 step = (step + 1) * 25;
2220 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2221 tlv[1] = 2 * sizeof(unsigned int);
2222 tlv[2] = -nums * step;
2223 tlv[3] = step;
2224}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002225EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002226
2227/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002228static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002229find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002230{
2231 struct snd_ctl_elem_id id;
2232 memset(&id, 0, sizeof(id));
2233 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002234 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002235 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002236 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2237 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002238 strcpy(id.name, name);
2239 return snd_ctl_find_id(codec->bus->card, &id);
2240}
2241
Takashi Iwaid5191e52009-11-16 14:58:17 +01002242/**
2243 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2244 * @codec: HD-audio codec
2245 * @name: ctl id name string
2246 *
2247 * Get the control element with the given id string and IFACE_MIXER.
2248 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002249struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2250 const char *name)
2251{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002252 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002253}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002254EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002255
Takashi Iwaidcda5802012-10-12 17:24:51 +02002256static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002257 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002258{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002259 int i, idx;
2260 /* 16 ctlrs should be large enough */
2261 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2262 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002263 return idx;
2264 }
2265 return -EBUSY;
2266}
2267
Takashi Iwaid5191e52009-11-16 14:58:17 +01002268/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002269 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002270 * @codec: HD-audio codec
2271 * @nid: corresponding NID (optional)
2272 * @kctl: the control element to assign
2273 *
2274 * Add the given control element to an array inside the codec instance.
2275 * All control elements belonging to a codec are supposed to be added
2276 * by this function so that a proper clean-up works at the free or
2277 * reconfiguration time.
2278 *
2279 * If non-zero @nid is passed, the NID is assigned to the control element.
2280 * The assignment is shown in the codec proc file.
2281 *
2282 * snd_hda_ctl_add() checks the control subdev id field whether
2283 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002284 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2285 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002286 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002287int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2288 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002289{
2290 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002291 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002292 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002293
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002294 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002295 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002296 if (nid == 0)
2297 nid = get_amp_nid_(kctl->private_value);
2298 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002299 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2300 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002301 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002302 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002303 err = snd_ctl_add(codec->bus->card, kctl);
2304 if (err < 0)
2305 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306 item = snd_array_new(&codec->mixers);
2307 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002308 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002309 item->kctl = kctl;
2310 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002311 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312 return 0;
2313}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002314EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002315
Takashi Iwaid5191e52009-11-16 14:58:17 +01002316/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002317 * snd_hda_add_nid - Assign a NID to a control element
2318 * @codec: HD-audio codec
2319 * @nid: corresponding NID (optional)
2320 * @kctl: the control element to assign
2321 * @index: index to kctl
2322 *
2323 * Add the given control element to an array inside the codec instance.
2324 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2325 * NID:KCTL mapping - for example "Capture Source" selector.
2326 */
2327int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2328 unsigned int index, hda_nid_t nid)
2329{
2330 struct hda_nid_item *item;
2331
2332 if (nid > 0) {
2333 item = snd_array_new(&codec->nids);
2334 if (!item)
2335 return -ENOMEM;
2336 item->kctl = kctl;
2337 item->index = index;
2338 item->nid = nid;
2339 return 0;
2340 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002341 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2342 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002343 return -EINVAL;
2344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002345EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002346
2347/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002348 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2349 * @codec: HD-audio codec
2350 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002351void snd_hda_ctls_clear(struct hda_codec *codec)
2352{
2353 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002354 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002355 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002356 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002357 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002358 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002359}
2360
Takashi Iwai95a962c2014-10-29 16:03:58 +01002361/**
2362 * snd_hda_lock_devices - pseudo device locking
2363 * @bus: the BUS
2364 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002365 * toggle card->shutdown to allow/disallow the device access (as a hack)
2366 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002367int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002368{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002369 struct snd_card *card = bus->card;
2370 struct hda_codec *codec;
2371
Takashi Iwaia65d6292009-02-23 16:57:04 +01002372 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002373 if (card->shutdown)
2374 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002375 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002376 if (!list_empty(&card->ctl_files))
2377 goto err_clear;
2378
2379 list_for_each_entry(codec, &bus->codec_list, list) {
2380 int pcm;
2381 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2382 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2383 if (!cpcm->pcm)
2384 continue;
2385 if (cpcm->pcm->streams[0].substream_opened ||
2386 cpcm->pcm->streams[1].substream_opened)
2387 goto err_clear;
2388 }
2389 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002390 spin_unlock(&card->files_lock);
2391 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002392
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002393 err_clear:
2394 card->shutdown = 0;
2395 err_unlock:
2396 spin_unlock(&card->files_lock);
2397 return -EINVAL;
2398}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002399EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002400
Takashi Iwai95a962c2014-10-29 16:03:58 +01002401/**
2402 * snd_hda_unlock_devices - pseudo device unlocking
2403 * @bus: the BUS
2404 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002405void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002406{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002407 struct snd_card *card = bus->card;
2408
2409 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002410 spin_lock(&card->files_lock);
2411 card->shutdown = 0;
2412 spin_unlock(&card->files_lock);
2413}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002414EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002415
Takashi Iwaid5191e52009-11-16 14:58:17 +01002416/**
2417 * snd_hda_codec_reset - Clear all objects assigned to the codec
2418 * @codec: HD-audio codec
2419 *
2420 * This frees the all PCM and control elements assigned to the codec, and
2421 * clears the caches and restores the pin default configurations.
2422 *
2423 * When a device is being used, it returns -EBSY. If successfully freed,
2424 * returns zero.
2425 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002426int snd_hda_codec_reset(struct hda_codec *codec)
2427{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002428 struct hda_bus *bus = codec->bus;
2429 struct snd_card *card = bus->card;
2430 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002431
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002432 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002433 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434
2435 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002436 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002437 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002438 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002439 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002440 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002441 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002442 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002443 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002444 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002445 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002446 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002447 snd_hda_detach_beep_device(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002448 if (device_is_registered(hda_codec_dev(codec)))
2449 device_del(hda_codec_dev(codec));
2450
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002451 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002452 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002453 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002454 codec->spec = NULL;
2455 free_hda_cache(&codec->amp_cache);
2456 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002457 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2458 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002459 /* free only driver_pins so that init_pins + user_pins are restored */
2460 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002461 snd_array_free(&codec->cvt_setups);
2462 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002463 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002464 codec->num_pcms = 0;
2465 codec->pcm_info = NULL;
2466 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002467 codec->slave_dig_outs = NULL;
2468 codec->spdif_status_reset = 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002469
2470 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002471 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002472 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002473}
2474
Takashi Iwai6194b992014-06-06 18:12:16 +02002475typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002476
2477/* apply the function to all matching slave ctls in the mixer list */
2478static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002479 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002480{
2481 struct hda_nid_item *items;
2482 const char * const *s;
2483 int i, err;
2484
2485 items = codec->mixers.list;
2486 for (i = 0; i < codec->mixers.used; i++) {
2487 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002488 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002489 continue;
2490 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002491 char tmpname[sizeof(sctl->id.name)];
2492 const char *name = *s;
2493 if (suffix) {
2494 snprintf(tmpname, sizeof(tmpname), "%s %s",
2495 name, suffix);
2496 name = tmpname;
2497 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002498 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002499 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002500 if (err)
2501 return err;
2502 break;
2503 }
2504 }
2505 }
2506 return 0;
2507}
2508
Takashi Iwai6194b992014-06-06 18:12:16 +02002509static int check_slave_present(struct hda_codec *codec,
2510 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002511{
2512 return 1;
2513}
2514
Takashi Iwai18478e82012-03-09 17:51:10 +01002515/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002516static int get_kctl_0dB_offset(struct hda_codec *codec,
2517 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002518{
2519 int _tlv[4];
2520 const int *tlv = NULL;
2521 int val = -1;
2522
2523 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2524 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2525 mm_segment_t fs = get_fs();
2526 set_fs(get_ds());
2527 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2528 tlv = _tlv;
2529 set_fs(fs);
2530 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2531 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002532 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2533 int step = tlv[3];
2534 step &= ~TLV_DB_SCALE_MUTE;
2535 if (!step)
2536 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002537 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002538 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002539- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002540 return -1;
2541 }
2542 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002543 val = -tlv[2] / step;
2544 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002545 return val;
2546}
2547
2548/* call kctl->put with the given value(s) */
2549static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2550{
2551 struct snd_ctl_elem_value *ucontrol;
2552 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2553 if (!ucontrol)
2554 return -ENOMEM;
2555 ucontrol->value.integer.value[0] = val;
2556 ucontrol->value.integer.value[1] = val;
2557 kctl->put(kctl, ucontrol);
2558 kfree(ucontrol);
2559 return 0;
2560}
2561
2562/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002563static int init_slave_0dB(struct hda_codec *codec,
2564 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002565{
Takashi Iwai6194b992014-06-06 18:12:16 +02002566 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002567 if (offset > 0)
2568 put_kctl_with_value(slave, offset);
2569 return 0;
2570}
2571
2572/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002573static int init_slave_unmute(struct hda_codec *codec,
2574 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002575{
2576 return put_kctl_with_value(slave, 1);
2577}
2578
Takashi Iwaie8750942014-06-30 14:02:39 +02002579static int add_slave(struct hda_codec *codec,
2580 void *data, struct snd_kcontrol *slave)
2581{
2582 return snd_ctl_add_slave(data, slave);
2583}
2584
Takashi Iwaid5191e52009-11-16 14:58:17 +01002585/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002586 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002587 * @codec: HD-audio codec
2588 * @name: vmaster control name
2589 * @tlv: TLV data (optional)
2590 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002591 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002592 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002593 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002594 *
2595 * Create a virtual master control with the given name. The TLV data
2596 * must be either NULL or a valid data.
2597 *
2598 * @slaves is a NULL-terminated array of strings, each of which is a
2599 * slave control name. All controls with these names are assigned to
2600 * the new virtual master control.
2601 *
2602 * This function returns zero if successful or a negative error code.
2603 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002604int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002605 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002606 const char *suffix, bool init_slave_vol,
2607 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002608{
2609 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002610 int err;
2611
Takashi Iwai29e58532012-03-12 12:25:03 +01002612 if (ctl_ret)
2613 *ctl_ret = NULL;
2614
Takashi Iwai9322ca52012-02-03 14:28:01 +01002615 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002616 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002617 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002618 return 0;
2619 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002620 kctl = snd_ctl_make_virtual_master(name, tlv);
2621 if (!kctl)
2622 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002623 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002624 if (err < 0)
2625 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002626
Takashi Iwaie8750942014-06-30 14:02:39 +02002627 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002628 if (err < 0)
2629 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002630
2631 /* init with master mute & zero volume */
2632 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002633 if (init_slave_vol) {
2634 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002635 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002636 tlv ? init_slave_0dB : init_slave_unmute, &step);
2637 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002638
Takashi Iwai29e58532012-03-12 12:25:03 +01002639 if (ctl_ret)
2640 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002641 return 0;
2642}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002643EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002644
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002645/*
2646 * mute-LED control using vmaster
2647 */
2648static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_info *uinfo)
2650{
2651 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002652 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002653 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002654
Takashi Iwai3ff72212014-10-20 18:17:28 +02002655 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002656}
2657
2658static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2659 struct snd_ctl_elem_value *ucontrol)
2660{
2661 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2662 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2663 return 0;
2664}
2665
2666static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
2668{
2669 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2670 unsigned int old_mode = hook->mute_mode;
2671
2672 hook->mute_mode = ucontrol->value.enumerated.item[0];
2673 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2674 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2675 if (old_mode == hook->mute_mode)
2676 return 0;
2677 snd_hda_sync_vmaster_hook(hook);
2678 return 1;
2679}
2680
2681static struct snd_kcontrol_new vmaster_mute_mode = {
2682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2683 .name = "Mute-LED Mode",
2684 .info = vmaster_mute_mode_info,
2685 .get = vmaster_mute_mode_get,
2686 .put = vmaster_mute_mode_put,
2687};
2688
Takashi Iwai95a962c2014-10-29 16:03:58 +01002689/**
2690 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2691 * @codec: the HDA codec
2692 * @hook: the vmaster hook object
2693 * @expose_enum_ctl: flag to create an enum ctl
2694 *
2695 * Add a mute-LED hook with the given vmaster switch kctl.
2696 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2697 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002698 */
2699int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002700 struct hda_vmaster_mute_hook *hook,
2701 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002702{
2703 struct snd_kcontrol *kctl;
2704
2705 if (!hook->hook || !hook->sw_kctl)
2706 return 0;
2707 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2708 hook->codec = codec;
2709 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002710 if (!expose_enum_ctl)
2711 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002712 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2713 if (!kctl)
2714 return -ENOMEM;
2715 return snd_hda_ctl_add(codec, 0, kctl);
2716}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002717EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002718
Takashi Iwai95a962c2014-10-29 16:03:58 +01002719/**
2720 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2721 * @hook: the vmaster hook
2722 *
2723 * Call the hook with the current value for synchronization.
2724 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002725 */
2726void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2727{
2728 if (!hook->hook || !hook->codec)
2729 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002730 /* don't call vmaster hook in the destructor since it might have
2731 * been already destroyed
2732 */
2733 if (hook->codec->bus->shutdown)
2734 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002735 switch (hook->mute_mode) {
2736 case HDA_VMUTE_FOLLOW_MASTER:
2737 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2738 break;
2739 default:
2740 hook->hook(hook->codec, hook->mute_mode);
2741 break;
2742 }
2743}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002744EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002745
2746
Takashi Iwaid5191e52009-11-16 14:58:17 +01002747/**
2748 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002749 * @kcontrol: referred ctl element
2750 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002751 *
2752 * The control element is supposed to have the private_value field
2753 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2754 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002755int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2756 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
2758 int chs = get_amp_channels(kcontrol);
2759
2760 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2761 uinfo->count = chs == 3 ? 2 : 1;
2762 uinfo->value.integer.min = 0;
2763 uinfo->value.integer.max = 1;
2764 return 0;
2765}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002766EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Takashi Iwaid5191e52009-11-16 14:58:17 +01002768/**
2769 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002770 * @kcontrol: ctl element
2771 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002772 *
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2775 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002776int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 hda_nid_t nid = get_amp_nid(kcontrol);
2781 int chs = get_amp_channels(kcontrol);
2782 int dir = get_amp_direction(kcontrol);
2783 int idx = get_amp_index(kcontrol);
2784 long *valp = ucontrol->value.integer.value;
2785
2786 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002787 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002788 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002790 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002791 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002794EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Takashi Iwaid5191e52009-11-16 14:58:17 +01002796/**
2797 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002798 * @kcontrol: ctl element
2799 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002800 *
2801 * The control element is supposed to have the private_value field
2802 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2803 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002804int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2808 hda_nid_t nid = get_amp_nid(kcontrol);
2809 int chs = get_amp_channels(kcontrol);
2810 int dir = get_amp_direction(kcontrol);
2811 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 long *valp = ucontrol->value.integer.value;
2813 int change = 0;
2814
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002815 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002816 change = codec_amp_update(codec, nid, 0, dir, idx,
2817 HDA_AMP_MUTE,
2818 *valp ? 0 : HDA_AMP_MUTE, false,
2819 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002820 valp++;
2821 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002822 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002823 change |= codec_amp_update(codec, nid, 1, dir, idx,
2824 HDA_AMP_MUTE,
2825 *valp ? 0 : HDA_AMP_MUTE, false,
2826 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002827 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return change;
2829}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002830EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832/*
Takashi Iwai985be542005-11-02 18:26:49 +01002833 * bound volume controls
2834 *
2835 * bind multiple volumes (# indices, from 0)
2836 */
2837
2838#define AMP_VAL_IDX_SHIFT 19
2839#define AMP_VAL_IDX_MASK (0x0f<<19)
2840
Takashi Iwaid5191e52009-11-16 14:58:17 +01002841/**
2842 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002843 * @kcontrol: ctl element
2844 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002845 *
2846 * The control element is supposed to have the private_value field
2847 * set up via HDA_BIND_MUTE*() macros.
2848 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002849int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002851{
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2853 unsigned long pval;
2854 int err;
2855
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002856 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002857 pval = kcontrol->private_value;
2858 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2859 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2860 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002861 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002862 return err;
2863}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002864EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002865
Takashi Iwaid5191e52009-11-16 14:58:17 +01002866/**
2867 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002868 * @kcontrol: ctl element
2869 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002870 *
2871 * The control element is supposed to have the private_value field
2872 * set up via HDA_BIND_MUTE*() macros.
2873 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002874int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002876{
2877 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2878 unsigned long pval;
2879 int i, indices, err = 0, change = 0;
2880
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002881 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002882 pval = kcontrol->private_value;
2883 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2884 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002885 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2886 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002887 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2888 if (err < 0)
2889 break;
2890 change |= err;
2891 }
2892 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002893 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002894 return err < 0 ? err : change;
2895}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002896EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002897
Takashi Iwaid5191e52009-11-16 14:58:17 +01002898/**
2899 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002900 * @kcontrol: referred ctl element
2901 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002902 *
2903 * The control element is supposed to have the private_value field
2904 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002905 */
2906int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_info *uinfo)
2908{
2909 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2910 struct hda_bind_ctls *c;
2911 int err;
2912
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002913 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002914 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002915 kcontrol->private_value = *c->values;
2916 err = c->ops->info(kcontrol, uinfo);
2917 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002918 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002919 return err;
2920}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002921EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002922
Takashi Iwaid5191e52009-11-16 14:58:17 +01002923/**
2924 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002925 * @kcontrol: ctl element
2926 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002927 *
2928 * The control element is supposed to have the private_value field
2929 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2930 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002931int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_value *ucontrol)
2933{
2934 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2935 struct hda_bind_ctls *c;
2936 int err;
2937
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002938 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002939 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002940 kcontrol->private_value = *c->values;
2941 err = c->ops->get(kcontrol, ucontrol);
2942 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002943 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002944 return err;
2945}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002946EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002947
Takashi Iwaid5191e52009-11-16 14:58:17 +01002948/**
2949 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002950 * @kcontrol: ctl element
2951 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002952 *
2953 * The control element is supposed to have the private_value field
2954 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2955 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002956int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2958{
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2960 struct hda_bind_ctls *c;
2961 unsigned long *vals;
2962 int err = 0, change = 0;
2963
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002964 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002965 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002966 for (vals = c->values; *vals; vals++) {
2967 kcontrol->private_value = *vals;
2968 err = c->ops->put(kcontrol, ucontrol);
2969 if (err < 0)
2970 break;
2971 change |= err;
2972 }
2973 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002974 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002975 return err < 0 ? err : change;
2976}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002977EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002978
Takashi Iwaid5191e52009-11-16 14:58:17 +01002979/**
2980 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002981 * @kcontrol: ctl element
2982 * @op_flag: operation flag
2983 * @size: byte size of input TLV
2984 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002985 *
2986 * The control element is supposed to have the private_value field
2987 * set up via HDA_BIND_VOL() macro.
2988 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002989int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2990 unsigned int size, unsigned int __user *tlv)
2991{
2992 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2993 struct hda_bind_ctls *c;
2994 int err;
2995
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002996 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002997 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002998 kcontrol->private_value = *c->values;
2999 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3000 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003001 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003002 return err;
3003}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003004EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003005
3006struct hda_ctl_ops snd_hda_bind_vol = {
3007 .info = snd_hda_mixer_amp_volume_info,
3008 .get = snd_hda_mixer_amp_volume_get,
3009 .put = snd_hda_mixer_amp_volume_put,
3010 .tlv = snd_hda_mixer_amp_tlv
3011};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003012EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003013
3014struct hda_ctl_ops snd_hda_bind_sw = {
3015 .info = snd_hda_mixer_amp_switch_info,
3016 .get = snd_hda_mixer_amp_switch_get,
3017 .put = snd_hda_mixer_amp_switch_put,
3018 .tlv = snd_hda_mixer_amp_tlv
3019};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003020EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003021
3022/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 * SPDIF out controls
3024 */
3025
Takashi Iwai0ba21762007-04-16 11:29:14 +02003026static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3027 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3030 uinfo->count = 1;
3031 return 0;
3032}
3033
Takashi Iwai0ba21762007-04-16 11:29:14 +02003034static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3038 IEC958_AES0_NONAUDIO |
3039 IEC958_AES0_CON_EMPHASIS_5015 |
3040 IEC958_AES0_CON_NOT_COPYRIGHT;
3041 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3042 IEC958_AES1_CON_ORIGINAL;
3043 return 0;
3044}
3045
Takashi Iwai0ba21762007-04-16 11:29:14 +02003046static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3047 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3050 IEC958_AES0_NONAUDIO |
3051 IEC958_AES0_PRO_EMPHASIS_5015;
3052 return 0;
3053}
3054
Takashi Iwai0ba21762007-04-16 11:29:14 +02003055static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003059 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003060 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003062 mutex_lock(&codec->spdif_mutex);
3063 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003064 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3065 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3066 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3067 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003068 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 return 0;
3071}
3072
3073/* convert from SPDIF status bits to HDA SPDIF bits
3074 * bit 0 (DigEn) is always set zero (to be filled later)
3075 */
3076static unsigned short convert_from_spdif_status(unsigned int sbits)
3077{
3078 unsigned short val = 0;
3079
3080 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003085 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3086 IEC958_AES0_PRO_EMPHASIS_5015)
3087 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3090 IEC958_AES0_CON_EMPHASIS_5015)
3091 val |= AC_DIG1_EMPHASIS;
3092 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3093 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3097 }
3098 return val;
3099}
3100
3101/* convert to SPDIF status bits from HDA SPDIF bits
3102 */
3103static unsigned int convert_to_spdif_status(unsigned short val)
3104{
3105 unsigned int sbits = 0;
3106
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 sbits |= IEC958_AES0_PROFESSIONAL;
3111 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003112 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3114 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003119 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3121 sbits |= val & (0x7f << 8);
3122 }
3123 return sbits;
3124}
3125
Takashi Iwai2f728532008-09-25 16:32:41 +02003126/* set digital convert verbs both for the given NID and its slaves */
3127static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3128 int verb, int val)
3129{
Takashi Iwaidda14412011-05-02 11:29:30 +02003130 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003131
Takashi Iwai9e976972008-11-25 08:17:20 +01003132 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003133 d = codec->slave_dig_outs;
3134 if (!d)
3135 return;
3136 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003137 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003138}
3139
3140static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3141 int dig1, int dig2)
3142{
3143 if (dig1 != -1)
3144 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3145 if (dig2 != -1)
3146 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3147}
3148
Takashi Iwai0ba21762007-04-16 11:29:14 +02003149static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3150 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003153 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003154 struct hda_spdif_out *spdif;
3155 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 unsigned short val;
3157 int change;
3158
Ingo Molnar62932df2006-01-16 16:34:20 +01003159 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003160 spdif = snd_array_elem(&codec->spdif_out, idx);
3161 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003162 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3164 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3165 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003166 val = convert_from_spdif_status(spdif->status);
3167 val |= spdif->ctls & 1;
3168 change = spdif->ctls != val;
3169 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003170 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003171 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003172 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return change;
3174}
3175
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003176#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Takashi Iwai0ba21762007-04-16 11:29:14 +02003178static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3179 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003182 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003183 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003185 mutex_lock(&codec->spdif_mutex);
3186 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003187 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003188 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return 0;
3190}
3191
Stephen Warren74b654c2011-06-01 11:14:18 -06003192static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3193 int dig1, int dig2)
3194{
3195 set_dig_out_convert(codec, nid, dig1, dig2);
3196 /* unmute amp switch (if any) */
3197 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3198 (dig1 & AC_DIG1_ENABLE))
3199 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3200 HDA_AMP_MUTE, 0);
3201}
3202
Takashi Iwai0ba21762007-04-16 11:29:14 +02003203static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
3206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003207 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003208 struct hda_spdif_out *spdif;
3209 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 unsigned short val;
3211 int change;
3212
Ingo Molnar62932df2006-01-16 16:34:20 +01003213 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003214 spdif = snd_array_elem(&codec->spdif_out, idx);
3215 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003216 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003218 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003219 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003220 spdif->ctls = val;
3221 if (change && nid != (u16)-1)
3222 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003223 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return change;
3225}
3226
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003227static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 {
3229 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3230 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003231 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 .info = snd_hda_spdif_mask_info,
3233 .get = snd_hda_spdif_cmask_get,
3234 },
3235 {
3236 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003238 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 .info = snd_hda_spdif_mask_info,
3240 .get = snd_hda_spdif_pmask_get,
3241 },
3242 {
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003244 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .info = snd_hda_spdif_mask_info,
3246 .get = snd_hda_spdif_default_get,
3247 .put = snd_hda_spdif_default_put,
3248 },
3249 {
3250 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003251 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 .info = snd_hda_spdif_out_switch_info,
3253 .get = snd_hda_spdif_out_switch_get,
3254 .put = snd_hda_spdif_out_switch_put,
3255 },
3256 { } /* end */
3257};
3258
3259/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003260 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003262 * @associated_nid: NID that new ctls associated with
3263 * @cvt_nid: converter NID
3264 * @type: HDA_PCM_TYPE_*
3265 * Creates controls related with the digital output.
3266 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 *
3268 * Returns 0 if successful, or a negative error code.
3269 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003270int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3271 hda_nid_t associated_nid,
3272 hda_nid_t cvt_nid,
3273 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003276 struct snd_kcontrol *kctl;
3277 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003278 int idx = 0;
3279 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003280 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003281 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003283 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003284 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003285 idx = spdif_index;
3286 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003287 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003288 /* suppose a single SPDIF device */
3289 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3290 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3291 if (!kctl)
3292 break;
3293 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003294 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003295 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003296 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003297 if (!bus->primary_dig_out_type)
3298 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003299
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003300 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003301 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003302 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003303 return -EBUSY;
3304 }
Stephen Warren7c935972011-06-01 11:14:17 -06003305 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003306 if (!spdif)
3307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3309 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003310 if (!kctl)
3311 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003312 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003313 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003314 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003315 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return err;
3317 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003318 spdif->nid = cvt_nid;
3319 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003320 AC_VERB_GET_DIGI_CONVERT_1, 0);
3321 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return 0;
3323}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003324EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Takashi Iwai95a962c2014-10-29 16:03:58 +01003326/**
3327 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3328 * @codec: the HDA codec
3329 * @nid: widget NID
3330 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003331 * call within spdif_mutex lock
3332 */
Stephen Warren7c935972011-06-01 11:14:17 -06003333struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3334 hda_nid_t nid)
3335{
3336 int i;
3337 for (i = 0; i < codec->spdif_out.used; i++) {
3338 struct hda_spdif_out *spdif =
3339 snd_array_elem(&codec->spdif_out, i);
3340 if (spdif->nid == nid)
3341 return spdif;
3342 }
3343 return NULL;
3344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003345EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003346
Takashi Iwai95a962c2014-10-29 16:03:58 +01003347/**
3348 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3349 * @codec: the HDA codec
3350 * @idx: the SPDIF ctl index
3351 *
3352 * Unassign the widget from the given SPDIF control.
3353 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003354void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3355{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003356 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003357
3358 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003359 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003360 spdif->nid = (u16)-1;
3361 mutex_unlock(&codec->spdif_mutex);
3362}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003363EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003364
Takashi Iwai95a962c2014-10-29 16:03:58 +01003365/**
3366 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3367 * @codec: the HDA codec
3368 * @idx: the SPDIF ctl idx
3369 * @nid: widget NID
3370 *
3371 * Assign the widget to the SPDIF control with the given index.
3372 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003373void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3374{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003375 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003376 unsigned short val;
3377
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 if (spdif->nid != nid) {
3381 spdif->nid = nid;
3382 val = spdif->ctls;
3383 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3384 }
3385 mutex_unlock(&codec->spdif_mutex);
3386}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003387EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003390 * SPDIF sharing with analog output
3391 */
3392static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3393 struct snd_ctl_elem_value *ucontrol)
3394{
3395 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3396 ucontrol->value.integer.value[0] = mout->share_spdif;
3397 return 0;
3398}
3399
3400static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3401 struct snd_ctl_elem_value *ucontrol)
3402{
3403 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3404 mout->share_spdif = !!ucontrol->value.integer.value[0];
3405 return 0;
3406}
3407
3408static struct snd_kcontrol_new spdif_share_sw = {
3409 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3410 .name = "IEC958 Default PCM Playback Switch",
3411 .info = snd_ctl_boolean_mono_info,
3412 .get = spdif_share_sw_get,
3413 .put = spdif_share_sw_put,
3414};
3415
Takashi Iwaid5191e52009-11-16 14:58:17 +01003416/**
3417 * snd_hda_create_spdif_share_sw - create Default PCM switch
3418 * @codec: the HDA codec
3419 * @mout: multi-out instance
3420 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003421int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3422 struct hda_multi_out *mout)
3423{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003424 struct snd_kcontrol *kctl;
3425
Takashi Iwai9a081602008-02-12 18:37:26 +01003426 if (!mout->dig_out_nid)
3427 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003428
3429 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3430 if (!kctl)
3431 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003432 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003433 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003434}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003435EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003436
3437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 * SPDIF input
3439 */
3440
3441#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3442
Takashi Iwai0ba21762007-04-16 11:29:14 +02003443static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3444 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
3446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3447
3448 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3449 return 0;
3450}
3451
Takashi Iwai0ba21762007-04-16 11:29:14 +02003452static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3453 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3456 hda_nid_t nid = kcontrol->private_value;
3457 unsigned int val = !!ucontrol->value.integer.value[0];
3458 int change;
3459
Ingo Molnar62932df2006-01-16 16:34:20 +01003460 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003462 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003464 snd_hda_codec_write_cache(codec, nid, 0,
3465 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003467 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return change;
3469}
3470
Takashi Iwai0ba21762007-04-16 11:29:14 +02003471static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3472 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
3474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3475 hda_nid_t nid = kcontrol->private_value;
3476 unsigned short val;
3477 unsigned int sbits;
3478
Andrew Paprocki3982d172007-12-19 12:13:44 +01003479 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 sbits = convert_to_spdif_status(val);
3481 ucontrol->value.iec958.status[0] = sbits;
3482 ucontrol->value.iec958.status[1] = sbits >> 8;
3483 ucontrol->value.iec958.status[2] = sbits >> 16;
3484 ucontrol->value.iec958.status[3] = sbits >> 24;
3485 return 0;
3486}
3487
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003488static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 {
3490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003491 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 .info = snd_hda_spdif_in_switch_info,
3493 .get = snd_hda_spdif_in_switch_get,
3494 .put = snd_hda_spdif_in_switch_put,
3495 },
3496 {
3497 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003499 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 .info = snd_hda_spdif_mask_info,
3501 .get = snd_hda_spdif_in_status_get,
3502 },
3503 { } /* end */
3504};
3505
3506/**
3507 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3508 * @codec: the HDA codec
3509 * @nid: audio in widget NID
3510 *
3511 * Creates controls related with the SPDIF input.
3512 * Called from each patch supporting the SPDIF in.
3513 *
3514 * Returns 0 if successful, or a negative error code.
3515 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003516int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003519 struct snd_kcontrol *kctl;
3520 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003521 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Takashi Iwaidcda5802012-10-12 17:24:51 +02003523 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003524 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003525 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003526 return -EBUSY;
3527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3529 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003530 if (!kctl)
3531 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003533 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003534 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return err;
3536 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003537 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003538 snd_hda_codec_read(codec, nid, 0,
3539 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003540 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return 0;
3542}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003543EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003545/*
3546 * command cache
3547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Takashi Iwaic370dd62012-12-13 18:30:04 +01003549/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003550#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3551#define get_cmd_cache_nid(key) ((key) & 0xff)
3552#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3553
3554/**
3555 * snd_hda_codec_write_cache - send a single command with caching
3556 * @codec: the HDA codec
3557 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003558 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003559 * @verb: the verb to send
3560 * @parm: the parameter for the verb
3561 *
3562 * Send a single command without waiting for response.
3563 *
3564 * Returns 0 if successful, or a negative error code.
3565 */
3566int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003567 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003568{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003569 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003570 struct hda_cache_head *c;
3571 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003572 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003573
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003574 cache_only = codec->cached_write;
3575 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003576 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003577 if (err < 0)
3578 return err;
3579 }
3580
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003581 /* parm may contain the verb stuff for get/set amp */
3582 verb = verb | (parm >> 8);
3583 parm &= 0xff;
3584 key = build_cmd_cache_key(nid, verb);
3585 mutex_lock(&codec->bus->cmd_mutex);
3586 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003587 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003588 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003589 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003590 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003591 mutex_unlock(&codec->bus->cmd_mutex);
3592 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003593}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003594EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003595
Takashi Iwaid5191e52009-11-16 14:58:17 +01003596/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003597 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3598 * @codec: the HDA codec
3599 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003600 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003601 * @verb: the verb to send
3602 * @parm: the parameter for the verb
3603 *
3604 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3605 * command if the parameter is already identical with the cached value.
3606 * If not, it sends the command and refreshes the cache.
3607 *
3608 * Returns 0 if successful, or a negative error code.
3609 */
3610int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003611 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003612{
3613 struct hda_cache_head *c;
3614 u32 key;
3615
3616 /* parm may contain the verb stuff for get/set amp */
3617 verb = verb | (parm >> 8);
3618 parm &= 0xff;
3619 key = build_cmd_cache_key(nid, verb);
3620 mutex_lock(&codec->bus->cmd_mutex);
3621 c = get_hash(&codec->cmd_cache, key);
3622 if (c && c->val == parm) {
3623 mutex_unlock(&codec->bus->cmd_mutex);
3624 return 0;
3625 }
3626 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003627 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003628}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003629EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003630
3631/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003632 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3633 * @codec: HD-audio codec
3634 *
3635 * Execute all verbs recorded in the command caches to resume.
3636 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003637void snd_hda_codec_resume_cache(struct hda_codec *codec)
3638{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003639 int i;
3640
Takashi Iwaic370dd62012-12-13 18:30:04 +01003641 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003642 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003643 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3644 struct hda_cache_head *buffer;
3645 u32 key;
3646
3647 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3648 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003649 if (!key)
3650 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003651 if (!buffer->dirty)
3652 continue;
3653 buffer->dirty = 0;
3654 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003655 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3656 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003657 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003658 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003659 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003660}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003661EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003662
3663/**
3664 * snd_hda_sequence_write_cache - sequence writes with caching
3665 * @codec: the HDA codec
3666 * @seq: VERB array to send
3667 *
3668 * Send the commands sequentially from the given array.
3669 * Thte commands are recorded on cache for power-save and resume.
3670 * The array must be terminated with NID=0.
3671 */
3672void snd_hda_sequence_write_cache(struct hda_codec *codec,
3673 const struct hda_verb *seq)
3674{
3675 for (; seq->nid; seq++)
3676 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3677 seq->param);
3678}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003679EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003680
Takashi Iwaidc870f32013-01-22 15:24:30 +01003681/**
3682 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3683 * @codec: HD-audio codec
3684 */
3685void snd_hda_codec_flush_cache(struct hda_codec *codec)
3686{
3687 snd_hda_codec_resume_amp(codec);
3688 snd_hda_codec_resume_cache(codec);
3689}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003690EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003691
Takashi Iwai95a962c2014-10-29 16:03:58 +01003692/**
3693 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3694 * @codec: the HDA codec
3695 * @fg: function group (not used now)
3696 * @power_state: the power state to set (AC_PWRST_*)
3697 *
3698 * Set the given power state to all widgets that have the power control.
3699 * If the codec has power_filter set, it evaluates the power state and
3700 * filter out if it's unchanged as D3.
3701 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003702void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003703 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003704{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003705 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003706 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003707
Takashi Iwaicb53c622007-08-10 17:21:45 +02003708 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003709 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003710 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003711 if (!(wcaps & AC_WCAP_POWER))
3712 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003713 if (codec->power_filter) {
3714 state = codec->power_filter(codec, nid, power_state);
3715 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003716 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003717 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003718 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003719 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003720 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003721}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003722EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003723
3724/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003725 * supported power states check
3726 */
3727static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3728 unsigned int power_state)
3729{
3730 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3731
Mengdong Line037cb42012-08-10 14:11:58 +02003732 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003733 return false;
3734 if (sup & power_state)
3735 return true;
3736 else
3737 return false;
3738}
3739
3740/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003741 * wait until the state is reached, returns the current state
3742 */
3743static unsigned int hda_sync_power_state(struct hda_codec *codec,
3744 hda_nid_t fg,
3745 unsigned int power_state)
3746{
3747 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3748 unsigned int state, actual_state;
3749
3750 for (;;) {
3751 state = snd_hda_codec_read(codec, fg, 0,
3752 AC_VERB_GET_POWER_STATE, 0);
3753 if (state & AC_PWRST_ERROR)
3754 break;
3755 actual_state = (state >> 4) & 0x0f;
3756 if (actual_state == power_state)
3757 break;
3758 if (time_after_eq(jiffies, end_time))
3759 break;
3760 /* wait until the codec reachs to the target state */
3761 msleep(1);
3762 }
3763 return state;
3764}
3765
Takashi Iwai95a962c2014-10-29 16:03:58 +01003766/**
3767 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3768 * @codec: the HDA codec
3769 * @nid: widget NID
3770 * @power_state: power state to evalue
3771 *
3772 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3773 * This can be used a codec power_filter callback.
3774 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003775unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3776 hda_nid_t nid,
3777 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003778{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003779 if (nid == codec->afg || nid == codec->mfg)
3780 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003781 if (power_state == AC_PWRST_D3 &&
3782 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3783 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3784 int eapd = snd_hda_codec_read(codec, nid, 0,
3785 AC_VERB_GET_EAPD_BTLENABLE, 0);
3786 if (eapd & 0x02)
3787 return AC_PWRST_D0;
3788 }
3789 return power_state;
3790}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003791EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003792
Takashi Iwai432c6412012-08-28 09:59:20 -07003793/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003794 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003795 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003796static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003797 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003798{
Takashi Iwaid8193872012-08-31 07:54:38 -07003799 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003800 int count;
3801 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003802 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003803
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003804 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003805 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003806 if (codec->depop_delay < 0)
3807 msleep(codec->epss ? 10 : 100);
3808 else if (codec->depop_delay > 0)
3809 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003810 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003811 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003812
3813 /* repeat power states setting at most 10 times*/
3814 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003815 if (codec->patch_ops.set_power_state)
3816 codec->patch_ops.set_power_state(codec, fg,
3817 power_state);
3818 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003819 state = power_state;
3820 if (codec->power_filter)
3821 state = codec->power_filter(codec, fg, state);
3822 if (state == power_state || power_state != AC_PWRST_D3)
3823 snd_hda_codec_read(codec, fg, flags,
3824 AC_VERB_SET_POWER_STATE,
3825 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003826 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003827 }
3828 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003829 if (!(state & AC_PWRST_ERROR))
3830 break;
3831 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003832
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003833 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003834}
Takashi Iwai54d17402005-11-21 16:33:22 +01003835
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003836/* sync power states of all widgets;
3837 * this is called at the end of codec parsing
3838 */
3839static void sync_power_up_states(struct hda_codec *codec)
3840{
3841 hda_nid_t nid = codec->start_nid;
3842 int i;
3843
Takashi Iwaiba615b82013-03-13 14:47:21 +01003844 /* don't care if no filter is used */
3845 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003846 return;
3847
3848 for (i = 0; i < codec->num_nodes; i++, nid++) {
3849 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003850 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003851 if (!(wcaps & AC_WCAP_POWER))
3852 continue;
3853 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3854 if (target == AC_PWRST_D0)
3855 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003856 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003857 snd_hda_codec_write(codec, nid, 0,
3858 AC_VERB_SET_POWER_STATE, target);
3859 }
3860}
3861
Takashi Iwai648a8d22014-02-25 10:38:13 +01003862#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003863/* execute additional init verbs */
3864static void hda_exec_init_verbs(struct hda_codec *codec)
3865{
3866 if (codec->init_verbs.list)
3867 snd_hda_sequence_write(codec, codec->init_verbs.list);
3868}
3869#else
3870static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3871#endif
3872
Takashi Iwai2a439522011-07-26 09:52:50 +02003873#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003874/* update the power on/off account with the current jiffies */
3875static void update_power_acct(struct hda_codec *codec, bool on)
3876{
3877 unsigned long delta = jiffies - codec->power_jiffies;
3878
3879 if (on)
3880 codec->power_on_acct += delta;
3881 else
3882 codec->power_off_acct += delta;
3883 codec->power_jiffies += delta;
3884}
3885
3886void snd_hda_update_power_acct(struct hda_codec *codec)
3887{
3888 update_power_acct(codec, hda_codec_is_power_on(codec));
3889}
3890
Takashi Iwaicb53c622007-08-10 17:21:45 +02003891/*
3892 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003893 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003894 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003895static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003896{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003897 unsigned int state;
3898
Takashi Iwaicc72da72015-02-19 16:00:22 +01003899 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003900
Takashi Iwaicb53c622007-08-10 17:21:45 +02003901 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003902 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003903 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003904 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003905 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003906 update_power_acct(codec, true);
3907 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003908 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003909}
3910
Takashi Iwaic370dd62012-12-13 18:30:04 +01003911/* mark all entries of cmd and amp caches dirty */
3912static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3913{
3914 int i;
3915 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3916 struct hda_cache_head *cmd;
3917 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3918 cmd->dirty = 1;
3919 }
3920 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3921 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003922 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003923 amp->head.dirty = 1;
3924 }
3925}
3926
Takashi Iwaicb53c622007-08-10 17:21:45 +02003927/*
3928 * kick up codec; used both from PM and power-save
3929 */
3930static void hda_call_codec_resume(struct hda_codec *codec)
3931{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003932 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003933
Takashi Iwaicc72da72015-02-19 16:00:22 +01003934 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003935 hda_mark_cmd_cache_dirty(codec);
3936
Takashi Iwaicc72da72015-02-19 16:00:22 +01003937 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003938
Takashi Iwaid8193872012-08-31 07:54:38 -07003939 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003940 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003941 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003942 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003943 if (codec->patch_ops.resume)
3944 codec->patch_ops.resume(codec);
3945 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003946 if (codec->patch_ops.init)
3947 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003948 snd_hda_codec_resume_amp(codec);
3949 snd_hda_codec_resume_cache(codec);
3950 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003951
3952 if (codec->jackpoll_interval)
3953 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003954 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003955 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003956 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003957}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003958
Takashi Iwaicc72da72015-02-19 16:00:22 +01003959static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003960{
3961 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003962 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003963 int i;
3964
3965 cancel_delayed_work_sync(&codec->jackpoll_work);
3966 for (i = 0; i < codec->num_pcms; i++)
3967 snd_pcm_suspend_all(codec->pcm_info[i].pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003968 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003969 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
3970 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003971 return 0;
3972}
3973
Takashi Iwaicc72da72015-02-19 16:00:22 +01003974static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003975{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003976 struct hda_codec *codec = dev_to_hda_codec(dev);
3977
3978 set_bit(codec->addr, &codec->bus->codec_powered);
3979 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003980 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003981 return 0;
3982}
Takashi Iwai2a439522011-07-26 09:52:50 +02003983#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003984
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003985/* referred in hda_bind.c */
3986const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003987 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3988 pm_runtime_force_resume)
3989 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3990 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003991};
Takashi Iwai54d17402005-11-21 16:33:22 +01003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993/**
3994 * snd_hda_build_controls - build mixer controls
3995 * @bus: the BUS
3996 *
3997 * Creates mixer controls for each codec included in the bus.
3998 *
3999 * Returns 0 if successful, otherwise a negative error code.
4000 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004001int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004003 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Takashi Iwai0ba21762007-04-16 11:29:14 +02004005 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004006 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004007 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004008 codec_err(codec,
4009 "cannot build controls for #%d (error %d)\n",
4010 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004011 err = snd_hda_codec_reset(codec);
4012 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004013 codec_err(codec,
4014 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004015 return err;
4016 }
4017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004019 return 0;
4020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004021EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004023/*
4024 * add standard channel maps if not specified
4025 */
4026static int add_std_chmaps(struct hda_codec *codec)
4027{
4028 int i, str, err;
4029
4030 for (i = 0; i < codec->num_pcms; i++) {
4031 for (str = 0; str < 2; str++) {
4032 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4033 struct hda_pcm_stream *hinfo =
4034 &codec->pcm_info[i].stream[str];
4035 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004036 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004037
4038 if (codec->pcm_info[i].own_chmap)
4039 continue;
4040 if (!pcm || !hinfo->substreams)
4041 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004042 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4043 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004044 hinfo->channels_max,
4045 0, &chmap);
4046 if (err < 0)
4047 return err;
4048 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4049 }
4050 }
4051 return 0;
4052}
4053
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004054/* default channel maps for 2.1 speakers;
4055 * since HD-audio supports only stereo, odd number channels are omitted
4056 */
4057const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4058 { .channels = 2,
4059 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4060 { .channels = 4,
4061 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4062 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4063 { }
4064};
4065EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4066
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004067int snd_hda_codec_build_controls(struct hda_codec *codec)
4068{
4069 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004070 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004071 /* continue to initialize... */
4072 if (codec->patch_ops.init)
4073 err = codec->patch_ops.init(codec);
4074 if (!err && codec->patch_ops.build_controls)
4075 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004076 if (err < 0)
4077 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004078
4079 /* we create chmaps here instead of build_pcms */
4080 err = add_std_chmaps(codec);
4081 if (err < 0)
4082 return err;
4083
David Henningsson26a6cb62012-10-09 15:04:21 +02004084 if (codec->jackpoll_interval)
4085 hda_jackpoll_work(&codec->jackpoll_work.work);
4086 else
4087 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004088 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return 0;
4090}
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092/*
4093 * stream formats
4094 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004095struct hda_rate_tbl {
4096 unsigned int hz;
4097 unsigned int alsa_bits;
4098 unsigned int hda_fmt;
4099};
4100
Takashi Iwai92f10b32010-08-03 14:21:00 +02004101/* rate = base * mult / div */
4102#define HDA_RATE(base, mult, div) \
4103 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4104 (((div) - 1) << AC_FMT_DIV_SHIFT))
4105
Takashi Iwaibefdf312005-08-22 13:57:55 +02004106static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004108
4109 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004110 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4111 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4112 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4113 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4114 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4115 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4116 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4117 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4118 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4119 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4120 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004121#define AC_PAR_PCM_RATE_BITS 11
4122 /* up to bits 10, 384kHZ isn't supported properly */
4123
4124 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004125 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004126
Takashi Iwaibefdf312005-08-22 13:57:55 +02004127 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128};
4129
4130/**
4131 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004132 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 * @rate: the sample rate
4134 * @channels: the number of channels
4135 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4136 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004137 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 *
4139 * Calculate the format bitset from the given rate, channels and th PCM format.
4140 *
4141 * Return zero if invalid.
4142 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004143unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4144 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 unsigned int channels,
4146 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004147 unsigned int maxbps,
4148 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 int i;
4151 unsigned int val = 0;
4152
Takashi Iwaibefdf312005-08-22 13:57:55 +02004153 for (i = 0; rate_bits[i].hz; i++)
4154 if (rate_bits[i].hz == rate) {
4155 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 break;
4157 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004158 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004159 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return 0;
4161 }
4162
4163 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004164 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return 0;
4166 }
4167 val |= channels - 1;
4168
4169 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004170 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004171 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004172 break;
4173 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004174 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 case 20:
4177 case 24:
4178 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004179 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004180 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004182 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004184 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 break;
4186 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004187 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004188 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return 0;
4190 }
4191
Anssi Hannula32c168c2010-08-03 13:28:57 +03004192 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004193 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return val;
4196}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004197EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004199static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4200 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004201{
4202 unsigned int val = 0;
4203 if (nid != codec->afg &&
4204 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4205 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4206 if (!val || val == -1)
4207 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4208 if (!val || val == -1)
4209 return 0;
4210 return val;
4211}
4212
4213static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4214{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004215 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004216 get_pcm_param);
4217}
4218
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004219static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4220 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004221{
4222 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4223 if (!streams || streams == -1)
4224 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4225 if (!streams || streams == -1)
4226 return 0;
4227 return streams;
4228}
4229
4230static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4231{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004232 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004233 get_stream_param);
4234}
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236/**
4237 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4238 * @codec: the HDA codec
4239 * @nid: NID to query
4240 * @ratesp: the pointer to store the detected rate bitflags
4241 * @formatsp: the pointer to store the detected formats
4242 * @bpsp: the pointer to store the detected format widths
4243 *
4244 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4245 * or @bsps argument is ignored.
4246 *
4247 * Returns 0 if successful, otherwise a negative error code.
4248 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004249int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4251{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004252 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004254 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004255 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 if (ratesp) {
4258 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004259 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004261 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004263 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004264 codec_err(codec,
4265 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4266 nid, val,
4267 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004268 return -EIO;
4269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 *ratesp = rates;
4271 }
4272
4273 if (formatsp || bpsp) {
4274 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004275 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004277 streams = query_stream_param(codec, nid);
4278 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 bps = 0;
4282 if (streams & AC_SUPFMT_PCM) {
4283 if (val & AC_SUPPCM_BITS_8) {
4284 formats |= SNDRV_PCM_FMTBIT_U8;
4285 bps = 8;
4286 }
4287 if (val & AC_SUPPCM_BITS_16) {
4288 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4289 bps = 16;
4290 }
4291 if (wcaps & AC_WCAP_DIGITAL) {
4292 if (val & AC_SUPPCM_BITS_32)
4293 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4294 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4295 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4296 if (val & AC_SUPPCM_BITS_24)
4297 bps = 24;
4298 else if (val & AC_SUPPCM_BITS_20)
4299 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004300 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4301 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4303 if (val & AC_SUPPCM_BITS_32)
4304 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 else if (val & AC_SUPPCM_BITS_24)
4306 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004307 else if (val & AC_SUPPCM_BITS_20)
4308 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 }
4310 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004311#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004312 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004314 if (!bps)
4315 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004316 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004317#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004318 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004319 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 /* temporary hack: we have still no proper support
4321 * for the direct AC3 stream...
4322 */
4323 formats |= SNDRV_PCM_FMTBIT_U8;
4324 bps = 8;
4325 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004326 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004327 codec_err(codec,
4328 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4329 nid, val,
4330 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4331 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004332 return -EIO;
4333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (formatsp)
4335 *formatsp = formats;
4336 if (bpsp)
4337 *bpsp = bps;
4338 }
4339
4340 return 0;
4341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004342EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004345 * snd_hda_is_supported_format - Check the validity of the format
4346 * @codec: HD-audio codec
4347 * @nid: NID to check
4348 * @format: the HD-audio format value to check
4349 *
4350 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 *
4352 * Returns 1 if supported, 0 if not.
4353 */
4354int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4355 unsigned int format)
4356{
4357 int i;
4358 unsigned int val = 0, rate, stream;
4359
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004360 val = query_pcm_param(codec, nid);
4361 if (!val)
4362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004365 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004366 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 if (val & (1 << i))
4368 break;
4369 return 0;
4370 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004371 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return 0;
4373
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004374 stream = query_stream_param(codec, nid);
4375 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return 0;
4377
4378 if (stream & AC_SUPFMT_PCM) {
4379 switch (format & 0xf0) {
4380 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004381 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return 0;
4383 break;
4384 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004385 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 return 0;
4387 break;
4388 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004389 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return 0;
4391 break;
4392 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004393 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return 0;
4395 break;
4396 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004397 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return 0;
4399 break;
4400 default:
4401 return 0;
4402 }
4403 } else {
4404 /* FIXME: check for float32 and AC3? */
4405 }
4406
4407 return 1;
4408}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004409EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411/*
4412 * PCM stuff
4413 */
4414static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004416 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
4418 return 0;
4419}
4420
4421static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4422 struct hda_codec *codec,
4423 unsigned int stream_tag,
4424 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004425 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4428 return 0;
4429}
4430
4431static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4432 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004433 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Takashi Iwai888afa12008-03-18 09:57:50 +01004435 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return 0;
4437}
4438
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004439static int set_pcm_default_values(struct hda_codec *codec,
4440 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004442 int err;
4443
Takashi Iwai0ba21762007-04-16 11:29:14 +02004444 /* query support PCM information from the given NID */
4445 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004446 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004447 info->rates ? NULL : &info->rates,
4448 info->formats ? NULL : &info->formats,
4449 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004450 if (err < 0)
4451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
4453 if (info->ops.open == NULL)
4454 info->ops.open = hda_pcm_default_open_close;
4455 if (info->ops.close == NULL)
4456 info->ops.close = hda_pcm_default_open_close;
4457 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004458 if (snd_BUG_ON(!info->nid))
4459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 info->ops.prepare = hda_pcm_default_prepare;
4461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004463 if (snd_BUG_ON(!info->nid))
4464 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 info->ops.cleanup = hda_pcm_default_cleanup;
4466 }
4467 return 0;
4468}
4469
Takashi Iwaieb541332010-08-06 13:48:11 +02004470/*
4471 * codec prepare/cleanup entries
4472 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004473/**
4474 * snd_hda_codec_prepare - Prepare a stream
4475 * @codec: the HDA codec
4476 * @hinfo: PCM information
4477 * @stream: stream tag to assign
4478 * @format: format id to assign
4479 * @substream: PCM substream to assign
4480 *
4481 * Calls the prepare callback set by the codec with the given arguments.
4482 * Clean up the inactive streams when successful.
4483 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004484int snd_hda_codec_prepare(struct hda_codec *codec,
4485 struct hda_pcm_stream *hinfo,
4486 unsigned int stream,
4487 unsigned int format,
4488 struct snd_pcm_substream *substream)
4489{
4490 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004491 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004492 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4493 if (ret >= 0)
4494 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004495 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004496 return ret;
4497}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004498EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004499
Takashi Iwai95a962c2014-10-29 16:03:58 +01004500/**
4501 * snd_hda_codec_cleanup - Prepare a stream
4502 * @codec: the HDA codec
4503 * @hinfo: PCM information
4504 * @substream: PCM substream
4505 *
4506 * Calls the cleanup callback set by the codec with the given arguments.
4507 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004508void snd_hda_codec_cleanup(struct hda_codec *codec,
4509 struct hda_pcm_stream *hinfo,
4510 struct snd_pcm_substream *substream)
4511{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004512 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004513 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004514 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004515}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004516EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004517
Takashi Iwaid5191e52009-11-16 14:58:17 +01004518/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004519const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4520 "Audio", "SPDIF", "HDMI", "Modem"
4521};
4522
Takashi Iwai176d5332008-07-30 15:01:44 +02004523/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004524 * get the empty PCM device number to assign
4525 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004526static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004527{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004528 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004529 /* assigned to static slots up to dev#10; if more needed, assign
4530 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4531 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004532 static int audio_idx[HDA_PCM_NTYPES][5] = {
4533 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4534 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004535 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004536 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004537 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004538 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004539
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004540 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004541 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004542 return -EINVAL;
4543 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004544
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004545 for (i = 0; audio_idx[type][i] >= 0; i++) {
4546#ifndef CONFIG_SND_DYNAMIC_MINORS
4547 if (audio_idx[type][i] >= 8)
4548 break;
4549#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004550 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4551 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004552 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004553
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004554#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004555 /* non-fixed slots starting from 10 */
4556 for (i = 10; i < 32; i++) {
4557 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4558 return i;
4559 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004560#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004561
Takashi Iwai4e76a882014-02-25 12:21:03 +01004562 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004563 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004564#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004565 dev_warn(bus->card->dev,
4566 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004567#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004568 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004569}
4570
4571/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004572 * attach a new PCM stream
4573 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004574static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004575{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004576 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004577 struct hda_pcm_stream *info;
4578 int stream, err;
4579
Takashi Iwaib91f0802008-11-04 08:43:08 +01004580 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004581 return -EINVAL;
4582 for (stream = 0; stream < 2; stream++) {
4583 info = &pcm->stream[stream];
4584 if (info->substreams) {
4585 err = set_pcm_default_values(codec, info);
4586 if (err < 0)
4587 return err;
4588 }
4589 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004590 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004591}
4592
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004593/* assign all PCMs of the given codec */
4594int snd_hda_codec_build_pcms(struct hda_codec *codec)
4595{
4596 unsigned int pcm;
4597 int err;
4598
4599 if (!codec->num_pcms) {
4600 if (!codec->patch_ops.build_pcms)
4601 return 0;
4602 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004603 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004604 codec_err(codec,
4605 "cannot build PCMs for #%d (error %d)\n",
4606 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004607 err = snd_hda_codec_reset(codec);
4608 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004609 codec_err(codec,
4610 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004611 return err;
4612 }
4613 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004614 }
4615 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4616 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4617 int dev;
4618
4619 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004620 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004621
4622 if (!cpcm->pcm) {
4623 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4624 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004625 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004626 cpcm->device = dev;
4627 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004628 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004629 codec_err(codec,
4630 "cannot attach PCM stream %d for codec #%d\n",
4631 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004632 continue; /* no fatal error */
4633 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004634 }
4635 }
4636 return 0;
4637}
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639/**
4640 * snd_hda_build_pcms - build PCM information
4641 * @bus: the BUS
4642 *
4643 * Create PCM information for each codec included in the bus.
4644 *
4645 * The build_pcms codec patch is requested to set up codec->num_pcms and
4646 * codec->pcm_info properly. The array is referred by the top-level driver
4647 * to create its PCM instances.
4648 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4649 * callback.
4650 *
4651 * At least, substreams, channels_min and channels_max must be filled for
4652 * each stream. substreams = 0 indicates that the stream doesn't exist.
4653 * When rates and/or formats are zero, the supported values are queried
4654 * from the given nid. The nid is used also by the default ops.prepare
4655 * and ops.cleanup callbacks.
4656 *
4657 * The driver needs to call ops.open in its open callback. Similarly,
4658 * ops.close is supposed to be called in the close callback.
4659 * ops.prepare should be called in the prepare or hw_params callback
4660 * with the proper parameters for set up.
4661 * ops.cleanup should be called in hw_free for clean up of streams.
4662 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004663 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004665int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004667 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Takashi Iwai0ba21762007-04-16 11:29:14 +02004669 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004670 int err = snd_hda_codec_build_pcms(codec);
4671 if (err < 0)
4672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
4674 return 0;
4675}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004676EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 * snd_hda_add_new_ctls - create controls from the array
4680 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004681 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 *
4683 * This helper function creates and add new controls in the given array.
4684 * The array must be terminated with an empty entry as terminator.
4685 *
4686 * Returns 0 if successful, or a negative error code.
4687 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004688int snd_hda_add_new_ctls(struct hda_codec *codec,
4689 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004691 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004694 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004695 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004696 if (knew->iface == -1) /* skip this codec private value */
4697 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004698 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004699 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004700 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004701 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004702 if (addr > 0)
4703 kctl->id.device = addr;
4704 if (idx > 0)
4705 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004706 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004707 if (!err)
4708 break;
4709 /* try first with another device index corresponding to
4710 * the codec addr; if it still fails (or it's the
4711 * primary codec), then try another control index
4712 */
4713 if (!addr && codec->addr)
4714 addr = codec->addr;
4715 else if (!idx && !knew->index) {
4716 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004717 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004718 if (idx <= 0)
4719 return err;
4720 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004721 return err;
4722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 }
4724 return 0;
4725}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004726EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Takashi Iwai83012a72012-08-24 18:38:08 +02004728#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004729/**
4730 * snd_hda_power_up - Power-up the codec
4731 * @codec: HD-audio codec
4732 *
4733 * Increment the usage counter and resume the device if not done yet.
4734 */
4735void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004736{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004737 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004738
Takashi Iwaicc72da72015-02-19 16:00:22 +01004739 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004740 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004741 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004742}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004743EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004744
4745/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004746 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004747 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004748 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004749 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004750 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004751void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004752{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004753 struct device *dev = hda_codec_dev(codec);
4754
4755 if (codec_in_pm(codec))
4756 return;
4757 pm_runtime_mark_last_busy(dev);
4758 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004760EXPORT_SYMBOL_GPL(snd_hda_power_down);
4761
Takashi Iwaibb573922015-02-20 09:26:04 +01004762static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004763{
4764 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004765
Takashi Iwaicc72da72015-02-19 16:00:22 +01004766 if (delay > 0) {
4767 pm_runtime_set_autosuspend_delay(dev, delay);
4768 pm_runtime_use_autosuspend(dev);
4769 pm_runtime_allow(dev);
4770 if (!pm_runtime_suspended(dev))
4771 pm_runtime_mark_last_busy(dev);
4772 } else {
4773 pm_runtime_dont_use_autosuspend(dev);
4774 pm_runtime_forbid(dev);
4775 }
4776}
Takashi Iwaibb573922015-02-20 09:26:04 +01004777
4778/**
4779 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4780 * @bus: HD-audio bus
4781 * @delay: autosuspend delay in msec, 0 = off
4782 *
4783 * Synchronize the runtime PM autosuspend state from the power_save option.
4784 */
4785void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4786{
4787 struct hda_codec *c;
4788
4789 list_for_each_entry(c, &bus->codec_list, list)
4790 codec_set_power_save(c, delay);
4791}
4792EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793
Takashi Iwaid5191e52009-11-16 14:58:17 +01004794/**
4795 * snd_hda_check_amp_list_power - Check the amp list and update the power
4796 * @codec: HD-audio codec
4797 * @check: the object containing an AMP list and the status
4798 * @nid: NID to check / update
4799 *
4800 * Check whether the given NID is in the amp list. If it's in the list,
4801 * check the current AMP status, and update the the power-status according
4802 * to the mute status.
4803 *
4804 * This function is supposed to be set or called from the check_power_status
4805 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004806 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004807int snd_hda_check_amp_list_power(struct hda_codec *codec,
4808 struct hda_loopback_check *check,
4809 hda_nid_t nid)
4810{
Takashi Iwai031024e2011-05-02 11:29:30 +02004811 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004812 int ch, v;
4813
4814 if (!check->amplist)
4815 return 0;
4816 for (p = check->amplist; p->nid; p++) {
4817 if (p->nid == nid)
4818 break;
4819 }
4820 if (!p->nid)
4821 return 0; /* nothing changed */
4822
4823 for (p = check->amplist; p->nid; p++) {
4824 for (ch = 0; ch < 2; ch++) {
4825 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4826 p->idx);
4827 if (!(v & HDA_AMP_MUTE) && v > 0) {
4828 if (!check->power_on) {
4829 check->power_on = 1;
4830 snd_hda_power_up(codec);
4831 }
4832 return 1;
4833 }
4834 }
4835 }
4836 if (check->power_on) {
4837 check->power_on = 0;
4838 snd_hda_power_down(codec);
4839 }
4840 return 0;
4841}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004842EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004845/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004846 * Channel mode helper
4847 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004848
4849/**
4850 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004851 * @codec: the HDA codec
4852 * @uinfo: pointer to get/store the data
4853 * @chmode: channel mode array
4854 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01004855 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004856int snd_hda_ch_mode_info(struct hda_codec *codec,
4857 struct snd_ctl_elem_info *uinfo,
4858 const struct hda_channel_mode *chmode,
4859 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004860{
4861 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4862 uinfo->count = 1;
4863 uinfo->value.enumerated.items = num_chmodes;
4864 if (uinfo->value.enumerated.item >= num_chmodes)
4865 uinfo->value.enumerated.item = num_chmodes - 1;
4866 sprintf(uinfo->value.enumerated.name, "%dch",
4867 chmode[uinfo->value.enumerated.item].channels);
4868 return 0;
4869}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004870EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004871
Takashi Iwaid5191e52009-11-16 14:58:17 +01004872/**
4873 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004874 * @codec: the HDA codec
4875 * @ucontrol: pointer to get/store the data
4876 * @chmode: channel mode array
4877 * @num_chmodes: channel mode array size
4878 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004879 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004880int snd_hda_ch_mode_get(struct hda_codec *codec,
4881 struct snd_ctl_elem_value *ucontrol,
4882 const struct hda_channel_mode *chmode,
4883 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004884 int max_channels)
4885{
4886 int i;
4887
4888 for (i = 0; i < num_chmodes; i++) {
4889 if (max_channels == chmode[i].channels) {
4890 ucontrol->value.enumerated.item[0] = i;
4891 break;
4892 }
4893 }
4894 return 0;
4895}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004896EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004897
Takashi Iwaid5191e52009-11-16 14:58:17 +01004898/**
4899 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004900 * @codec: the HDA codec
4901 * @ucontrol: pointer to get/store the data
4902 * @chmode: channel mode array
4903 * @num_chmodes: channel mode array size
4904 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004906int snd_hda_ch_mode_put(struct hda_codec *codec,
4907 struct snd_ctl_elem_value *ucontrol,
4908 const struct hda_channel_mode *chmode,
4909 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004910 int *max_channelsp)
4911{
4912 unsigned int mode;
4913
4914 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004915 if (mode >= num_chmodes)
4916 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004917 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004918 return 0;
4919 /* change the current channel setting */
4920 *max_channelsp = chmode[mode].channels;
4921 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004922 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004923 return 1;
4924}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004925EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927/*
4928 * input MUX helper
4929 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004930
4931/**
4932 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004933 * @imux: imux helper object
4934 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004935 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004936int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4937 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
4939 unsigned int index;
4940
4941 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4942 uinfo->count = 1;
4943 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004944 if (!imux->num_items)
4945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 index = uinfo->value.enumerated.item;
4947 if (index >= imux->num_items)
4948 index = imux->num_items - 1;
4949 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4950 return 0;
4951}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004952EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
Takashi Iwaid5191e52009-11-16 14:58:17 +01004954/**
4955 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004956 * @codec: the HDA codec
4957 * @imux: imux helper object
4958 * @ucontrol: pointer to get/store the data
4959 * @nid: input mux NID
4960 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004961 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004962int snd_hda_input_mux_put(struct hda_codec *codec,
4963 const struct hda_input_mux *imux,
4964 struct snd_ctl_elem_value *ucontrol,
4965 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 unsigned int *cur_val)
4967{
4968 unsigned int idx;
4969
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004970 if (!imux->num_items)
4971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 idx = ucontrol->value.enumerated.item[0];
4973 if (idx >= imux->num_items)
4974 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004975 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004977 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4978 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 *cur_val = idx;
4980 return 1;
4981}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004982EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004985/**
4986 * snd_hda_enum_helper_info - Helper for simple enum ctls
4987 * @kcontrol: ctl element
4988 * @uinfo: pointer to get/store the data
4989 * @num_items: number of enum items
4990 * @texts: enum item string array
4991 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004992 * process kcontrol info callback of a simple string enum array
4993 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4994 */
4995int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4996 struct snd_ctl_elem_info *uinfo,
4997 int num_items, const char * const *texts)
4998{
4999 static const char * const texts_default[] = {
5000 "Disabled", "Enabled"
5001 };
5002
5003 if (!texts || !num_items) {
5004 num_items = 2;
5005 texts = texts_default;
5006 }
5007
Takashi Iwai3ff72212014-10-20 18:17:28 +02005008 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005009}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005010EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005011
5012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 * Multi-channel / digital-out PCM helper functions
5014 */
5015
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005016/* setup SPDIF output stream */
5017static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5018 unsigned int stream_tag, unsigned int format)
5019{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005020 struct hda_spdif_out *spdif;
5021 unsigned int curr_fmt;
5022 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005023
Laurence Darby3bef1c32012-11-03 17:00:06 +00005024 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5025 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5026 AC_VERB_GET_STREAM_FORMAT, 0);
5027 reset = codec->spdif_status_reset &&
5028 (spdif->ctls & AC_DIG1_ENABLE) &&
5029 curr_fmt != format;
5030
5031 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5032 updated */
5033 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005034 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005035 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005036 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005037 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005038 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005039 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005040 for (d = codec->slave_dig_outs; *d; d++)
5041 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5042 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005043 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005044 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005045 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005046 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005047 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005048}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005049
Takashi Iwai2f728532008-09-25 16:32:41 +02005050static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5051{
5052 snd_hda_codec_cleanup_stream(codec, nid);
5053 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005054 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005055 for (d = codec->slave_dig_outs; *d; d++)
5056 snd_hda_codec_cleanup_stream(codec, *d);
5057 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005058}
5059
Takashi Iwaid5191e52009-11-16 14:58:17 +01005060/**
5061 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5062 * @bus: HD-audio bus
5063 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005064void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5065{
5066 struct hda_codec *codec;
5067
5068 if (!bus)
5069 return;
5070 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005071 if (hda_codec_is_power_on(codec) &&
5072 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005073 codec->patch_ops.reboot_notify(codec);
5074 }
5075}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005076EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005077
Takashi Iwaid5191e52009-11-16 14:58:17 +01005078/**
5079 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005080 * @codec: the HDA codec
5081 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005083int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5084 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
Ingo Molnar62932df2006-01-16 16:34:20 +01005086 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005087 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5088 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005089 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005091 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 return 0;
5093}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005094EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Takashi Iwaid5191e52009-11-16 14:58:17 +01005096/**
5097 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005098 * @codec: the HDA codec
5099 * @mout: hda_multi_out object
5100 * @stream_tag: stream tag to assign
5101 * @format: format id to assign
5102 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005103 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005104int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5105 struct hda_multi_out *mout,
5106 unsigned int stream_tag,
5107 unsigned int format,
5108 struct snd_pcm_substream *substream)
5109{
5110 mutex_lock(&codec->spdif_mutex);
5111 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5112 mutex_unlock(&codec->spdif_mutex);
5113 return 0;
5114}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005115EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005116
Takashi Iwaid5191e52009-11-16 14:58:17 +01005117/**
5118 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005119 * @codec: the HDA codec
5120 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005121 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005122int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5123 struct hda_multi_out *mout)
5124{
5125 mutex_lock(&codec->spdif_mutex);
5126 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5127 mutex_unlock(&codec->spdif_mutex);
5128 return 0;
5129}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005130EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005131
Takashi Iwaid5191e52009-11-16 14:58:17 +01005132/**
5133 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005134 * @codec: the HDA codec
5135 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005137int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5138 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
Ingo Molnar62932df2006-01-16 16:34:20 +01005140 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005142 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 return 0;
5144}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005145EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Takashi Iwaid5191e52009-11-16 14:58:17 +01005147/**
5148 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005149 * @codec: the HDA codec
5150 * @mout: hda_multi_out object
5151 * @substream: PCM substream to assign
5152 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005153 *
5154 * Open analog outputs and set up the hw-constraints.
5155 * If the digital outputs can be opened as slave, open the digital
5156 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005158int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5159 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005160 struct snd_pcm_substream *substream,
5161 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Takashi Iwai9a081602008-02-12 18:37:26 +01005163 struct snd_pcm_runtime *runtime = substream->runtime;
5164 runtime->hw.channels_max = mout->max_channels;
5165 if (mout->dig_out_nid) {
5166 if (!mout->analog_rates) {
5167 mout->analog_rates = hinfo->rates;
5168 mout->analog_formats = hinfo->formats;
5169 mout->analog_maxbps = hinfo->maxbps;
5170 } else {
5171 runtime->hw.rates = mout->analog_rates;
5172 runtime->hw.formats = mout->analog_formats;
5173 hinfo->maxbps = mout->analog_maxbps;
5174 }
5175 if (!mout->spdif_rates) {
5176 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5177 &mout->spdif_rates,
5178 &mout->spdif_formats,
5179 &mout->spdif_maxbps);
5180 }
5181 mutex_lock(&codec->spdif_mutex);
5182 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005183 if ((runtime->hw.rates & mout->spdif_rates) &&
5184 (runtime->hw.formats & mout->spdif_formats)) {
5185 runtime->hw.rates &= mout->spdif_rates;
5186 runtime->hw.formats &= mout->spdif_formats;
5187 if (mout->spdif_maxbps < hinfo->maxbps)
5188 hinfo->maxbps = mout->spdif_maxbps;
5189 } else {
5190 mout->share_spdif = 0;
5191 /* FIXME: need notify? */
5192 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005193 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005194 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5197 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5198}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005199EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Takashi Iwaid5191e52009-11-16 14:58:17 +01005201/**
5202 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005203 * @codec: the HDA codec
5204 * @mout: hda_multi_out object
5205 * @stream_tag: stream tag to assign
5206 * @format: format id to assign
5207 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005208 *
5209 * Set up the i/o for analog out.
5210 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005212int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5213 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 unsigned int stream_tag,
5215 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005216 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217{
Takashi Iwaidda14412011-05-02 11:29:30 +02005218 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005220 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 int i;
5222
Ingo Molnar62932df2006-01-16 16:34:20 +01005223 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005224 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005225 if (mout->dig_out_nid && mout->share_spdif &&
5226 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005228 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5229 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005230 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005232 setup_dig_out_stream(codec, mout->dig_out_nid,
5233 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 } else {
5235 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005236 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
5238 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005239 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
5241 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005242 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5243 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005244 if (!mout->no_share_stream &&
5245 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005247 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5248 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005249 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005250 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5251 if (!mout->no_share_stream && mout->hp_out_nid[i])
5252 snd_hda_codec_setup_stream(codec,
5253 mout->hp_out_nid[i],
5254 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 /* surrounds */
5257 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005258 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005259 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5260 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005261 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005262 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5263 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005265
5266 /* extra surrounds */
5267 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5268 int ch = 0;
5269 if (!mout->extra_out_nid[i])
5270 break;
5271 if (chs >= (i + 1) * 2)
5272 ch = i * 2;
5273 else if (!mout->no_share_stream)
5274 break;
5275 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5276 stream_tag, ch, format);
5277 }
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 return 0;
5280}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005281EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
Takashi Iwaid5191e52009-11-16 14:58:17 +01005283/**
5284 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005285 * @codec: the HDA codec
5286 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005288int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5289 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Takashi Iwaidda14412011-05-02 11:29:30 +02005291 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 int i;
5293
5294 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005295 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005297 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005298 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5299 if (mout->hp_out_nid[i])
5300 snd_hda_codec_cleanup_stream(codec,
5301 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005302 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5303 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005304 snd_hda_codec_cleanup_stream(codec,
5305 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005306 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005308 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 mout->dig_out_used = 0;
5310 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005311 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return 0;
5313}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005314EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
Takashi Iwai47408602012-04-20 13:06:53 +02005316/**
5317 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005318 * @codec: the HDA codec
5319 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005320 *
5321 * Guess the suitable VREF pin bits to be set as the pin-control value.
5322 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5323 */
5324unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5325{
5326 unsigned int pincap;
5327 unsigned int oldval;
5328 oldval = snd_hda_codec_read(codec, pin, 0,
5329 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5330 pincap = snd_hda_query_pin_caps(codec, pin);
5331 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5332 /* Exception: if the default pin setup is vref50, we give it priority */
5333 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5334 return AC_PINCTL_VREF_80;
5335 else if (pincap & AC_PINCAP_VREF_50)
5336 return AC_PINCTL_VREF_50;
5337 else if (pincap & AC_PINCAP_VREF_100)
5338 return AC_PINCTL_VREF_100;
5339 else if (pincap & AC_PINCAP_VREF_GRD)
5340 return AC_PINCTL_VREF_GRD;
5341 return AC_PINCTL_VREF_HIZ;
5342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005343EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005344
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005345/**
5346 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5347 * @codec: the HDA codec
5348 * @pin: referred pin NID
5349 * @val: pin ctl value to audit
5350 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005351unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5352 hda_nid_t pin, unsigned int val)
5353{
5354 static unsigned int cap_lists[][2] = {
5355 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5356 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5357 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5358 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5359 };
5360 unsigned int cap;
5361
5362 if (!val)
5363 return 0;
5364 cap = snd_hda_query_pin_caps(codec, pin);
5365 if (!cap)
5366 return val; /* don't know what to do... */
5367
5368 if (val & AC_PINCTL_OUT_EN) {
5369 if (!(cap & AC_PINCAP_OUT))
5370 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5371 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5372 val &= ~AC_PINCTL_HP_EN;
5373 }
5374
5375 if (val & AC_PINCTL_IN_EN) {
5376 if (!(cap & AC_PINCAP_IN))
5377 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5378 else {
5379 unsigned int vcap, vref;
5380 int i;
5381 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5382 vref = val & AC_PINCTL_VREFEN;
5383 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5384 if (vref == cap_lists[i][0] &&
5385 !(vcap & cap_lists[i][1])) {
5386 if (i == ARRAY_SIZE(cap_lists) - 1)
5387 vref = AC_PINCTL_VREF_HIZ;
5388 else
5389 vref = cap_lists[i + 1][0];
5390 }
5391 }
5392 val &= ~AC_PINCTL_VREFEN;
5393 val |= vref;
5394 }
5395 }
5396
5397 return val;
5398}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005399EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005400
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005401/**
5402 * _snd_hda_pin_ctl - Helper to set pin ctl value
5403 * @codec: the HDA codec
5404 * @pin: referred pin NID
5405 * @val: pin control value to set
5406 * @cached: access over codec pinctl cache or direct write
5407 *
5408 * This function is a helper to set a pin ctl value more safely.
5409 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5410 * value in pin target array via snd_hda_codec_set_pin_target(), then
5411 * actually writes the value via either snd_hda_codec_update_cache() or
5412 * snd_hda_codec_write() depending on @cached flag.
5413 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005414int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5415 unsigned int val, bool cached)
5416{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005417 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005418 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005419 if (cached)
5420 return snd_hda_codec_update_cache(codec, pin, 0,
5421 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5422 else
5423 return snd_hda_codec_write(codec, pin, 0,
5424 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5425}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005426EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005427
Takashi Iwai990061c2010-09-09 22:08:44 +02005428/**
5429 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005430 * @codec: the HDA codec
5431 * @imux: imux helper object
5432 * @label: the name of imux item to assign
5433 * @index: index number of imux item to assign
5434 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005435 *
5436 * When the same label is used already in the existing items, the number
5437 * suffix is appended to the label. This label index number is stored
5438 * to type_idx when non-NULL pointer is given.
5439 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005440int snd_hda_add_imux_item(struct hda_codec *codec,
5441 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005442 int index, int *type_idx)
5443{
5444 int i, label_idx = 0;
5445 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005446 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005447 return -EINVAL;
5448 }
5449 for (i = 0; i < imux->num_items; i++) {
5450 if (!strncmp(label, imux->items[i].label, strlen(label)))
5451 label_idx++;
5452 }
5453 if (type_idx)
5454 *type_idx = label_idx;
5455 if (label_idx > 0)
5456 snprintf(imux->items[imux->num_items].label,
5457 sizeof(imux->items[imux->num_items].label),
5458 "%s %d", label, label_idx);
5459 else
5460 strlcpy(imux->items[imux->num_items].label, label,
5461 sizeof(imux->items[imux->num_items].label));
5462 imux->items[imux->num_items].index = index;
5463 imux->num_items++;
5464 return 0;
5465}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005466EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005469 * snd_hda_bus_reset - Reset the bus
5470 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005472void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005474 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Takashi Iwai0ba21762007-04-16 11:29:14 +02005476 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005477 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005478 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005479#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005480 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005481 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005482 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005483 }
5484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005487EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005488
5489/*
5490 * generic arrays
5491 */
5492
Takashi Iwaid5191e52009-11-16 14:58:17 +01005493/**
5494 * snd_array_new - get a new element from the given array
5495 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005496 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005497 * Get a new element from the given array. If it exceeds the
5498 * pre-allocated array size, re-allocate the array.
5499 *
5500 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005501 */
5502void *snd_array_new(struct snd_array *array)
5503{
Takashi Iwai12f17712012-10-10 08:59:14 +02005504 if (snd_BUG_ON(!array->elem_size))
5505 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005506 if (array->used >= array->alloced) {
5507 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005508 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005509 void *nlist;
5510 if (snd_BUG_ON(num >= 4096))
5511 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005512 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005513 if (!nlist)
5514 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005515 array->list = nlist;
5516 array->alloced = num;
5517 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005518 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005519}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005520EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005521
Takashi Iwaid5191e52009-11-16 14:58:17 +01005522/**
5523 * snd_array_free - free the given array elements
5524 * @array: the array object
5525 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005526void snd_array_free(struct snd_array *array)
5527{
5528 kfree(array->list);
5529 array->used = 0;
5530 array->alloced = 0;
5531 array->list = NULL;
5532}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005533EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005534
Takashi Iwaid5191e52009-11-16 14:58:17 +01005535/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005536 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5537 * @pcm: PCM caps bits
5538 * @buf: the string buffer to write
5539 * @buflen: the max buffer length
5540 *
5541 * used by hda_proc.c and hda_eld.c
5542 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005543void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5544{
5545 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5546 int i, j;
5547
5548 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5549 if (pcm & (AC_SUPPCM_BITS_8 << i))
5550 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5551
5552 buf[j] = '\0'; /* necessary when j == 0 */
5553}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005554EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005555
5556MODULE_DESCRIPTION("HDA codec core");
5557MODULE_LICENSE("GPL");