blob: 262c41a8f96e991eb63ab990b41f6108a6c8fcb7 [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);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100779 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100783 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200784 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100786 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100788 snprintf(bus->workq_name, sizeof(bus->workq_name),
789 "hd-audio%d", card->number);
790 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100791 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100792 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100793 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100794 kfree(bus);
795 return -ENOMEM;
796 }
797
Takashi Iwai0ba21762007-04-16 11:29:14 +0200798 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
799 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 snd_hda_bus_free(bus);
801 return err;
802 }
803 if (busp)
804 *busp = bus;
805 return 0;
806}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100807EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200810 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100812static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200814 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 hda_nid_t nid;
816
817 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
818 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200819 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200820 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200821 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200822 case AC_GRP_AUDIO_FUNCTION:
823 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200824 codec->afg_function_id = function_id & 0xff;
825 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200826 break;
827 case AC_GRP_MODEM_FUNCTION:
828 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200829 codec->mfg_function_id = function_id & 0xff;
830 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200831 break;
832 default:
833 break;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100839 * read widget caps for each widget and store in cache
840 */
841static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
842{
843 int i;
844 hda_nid_t nid;
845
846 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
847 &codec->start_nid);
848 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200849 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100850 return -ENOMEM;
851 nid = codec->start_nid;
852 for (i = 0; i < codec->num_nodes; i++, nid++)
853 codec->wcaps[i] = snd_hda_param_read(codec, nid,
854 AC_PAR_AUDIO_WIDGET_CAP);
855 return 0;
856}
857
Takashi Iwai3be14142009-02-20 14:11:16 +0100858/* read all pin default configurations and save codec->init_pins */
859static int read_pin_defaults(struct hda_codec *codec)
860{
861 int i;
862 hda_nid_t nid = codec->start_nid;
863
864 for (i = 0; i < codec->num_nodes; i++, nid++) {
865 struct hda_pincfg *pin;
866 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200867 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100868 if (wid_type != AC_WID_PIN)
869 continue;
870 pin = snd_array_new(&codec->init_pins);
871 if (!pin)
872 return -ENOMEM;
873 pin->nid = nid;
874 pin->cfg = snd_hda_codec_read(codec, nid, 0,
875 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200876 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
877 AC_VERB_GET_PIN_WIDGET_CONTROL,
878 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100879 }
880 return 0;
881}
882
883/* look up the given pin config list and return the item matching with NID */
884static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
885 struct snd_array *array,
886 hda_nid_t nid)
887{
888 int i;
889 for (i = 0; i < array->used; i++) {
890 struct hda_pincfg *pin = snd_array_elem(array, i);
891 if (pin->nid == nid)
892 return pin;
893 }
894 return NULL;
895}
896
Takashi Iwai3be14142009-02-20 14:11:16 +0100897/* set the current pin config value for the given NID.
898 * the value is cached, and read via snd_hda_codec_get_pincfg()
899 */
900int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
901 hda_nid_t nid, unsigned int cfg)
902{
903 struct hda_pincfg *pin;
904
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200905 /* the check below may be invalid when pins are added by a fixup
906 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
907 * for now
908 */
909 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100910 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
911 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200912 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100913
Takashi Iwai3be14142009-02-20 14:11:16 +0100914 pin = look_up_pincfg(codec, list, nid);
915 if (!pin) {
916 pin = snd_array_new(list);
917 if (!pin)
918 return -ENOMEM;
919 pin->nid = nid;
920 }
921 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100922 return 0;
923}
924
Takashi Iwaid5191e52009-11-16 14:58:17 +0100925/**
926 * snd_hda_codec_set_pincfg - Override a pin default configuration
927 * @codec: the HDA codec
928 * @nid: NID to set the pin config
929 * @cfg: the pin default config value
930 *
931 * Override a pin default configuration value in the cache.
932 * This value can be read by snd_hda_codec_get_pincfg() in a higher
933 * priority than the real hardware value.
934 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100935int snd_hda_codec_set_pincfg(struct hda_codec *codec,
936 hda_nid_t nid, unsigned int cfg)
937{
Takashi Iwai346ff702009-02-23 09:42:57 +0100938 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100939}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100940EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100941
Takashi Iwaid5191e52009-11-16 14:58:17 +0100942/**
943 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
944 * @codec: the HDA codec
945 * @nid: NID to get the pin config
946 *
947 * Get the current pin config value of the given pin NID.
948 * If the pincfg value is cached or overridden via sysfs or driver,
949 * returns the cached value.
950 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100951unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
952{
953 struct hda_pincfg *pin;
954
Takashi Iwai648a8d22014-02-25 10:38:13 +0100955#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100956 {
957 unsigned int cfg = 0;
958 mutex_lock(&codec->user_mutex);
959 pin = look_up_pincfg(codec, &codec->user_pins, nid);
960 if (pin)
961 cfg = pin->cfg;
962 mutex_unlock(&codec->user_mutex);
963 if (cfg)
964 return cfg;
965 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100966#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100967 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
968 if (pin)
969 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100970 pin = look_up_pincfg(codec, &codec->init_pins, nid);
971 if (pin)
972 return pin->cfg;
973 return 0;
974}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100975EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976
Takashi Iwai95a962c2014-10-29 16:03:58 +0100977/**
978 * snd_hda_codec_set_pin_target - remember the current pinctl target value
979 * @codec: the HDA codec
980 * @nid: pin NID
981 * @val: assigned pinctl value
982 *
983 * This function stores the given value to a pinctl target value in the
984 * pincfg table. This isn't always as same as the actually written value
985 * but can be referred at any time via snd_hda_codec_get_pin_target().
986 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100987int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
988 unsigned int val)
989{
990 struct hda_pincfg *pin;
991
992 pin = look_up_pincfg(codec, &codec->init_pins, nid);
993 if (!pin)
994 return -EINVAL;
995 pin->target = val;
996 return 0;
997}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100998EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100999
Takashi Iwai95a962c2014-10-29 16:03:58 +01001000/**
1001 * snd_hda_codec_get_pin_target - return the current pinctl target value
1002 * @codec: the HDA codec
1003 * @nid: pin NID
1004 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001005int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1006{
1007 struct hda_pincfg *pin;
1008
1009 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1010 if (!pin)
1011 return 0;
1012 return pin->target;
1013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001014EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001015
Takashi Iwai92ee6162009-12-27 11:18:59 +01001016/**
1017 * snd_hda_shutup_pins - Shut up all pins
1018 * @codec: the HDA codec
1019 *
1020 * Clear all pin controls to shup up before suspend for avoiding click noise.
1021 * The controls aren't cached so that they can be resumed properly.
1022 */
1023void snd_hda_shutup_pins(struct hda_codec *codec)
1024{
1025 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001026 /* don't shut up pins when unloading the driver; otherwise it breaks
1027 * the default pin setup at the next load of the driver
1028 */
1029 if (codec->bus->shutdown)
1030 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001031 for (i = 0; i < codec->init_pins.used; i++) {
1032 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1033 /* use read here for syncing after issuing each verb */
1034 snd_hda_codec_read(codec, pin->nid, 0,
1035 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1036 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001037 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001038}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001039EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001040
Takashi Iwai2a439522011-07-26 09:52:50 +02001041#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001042/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1043static void restore_shutup_pins(struct hda_codec *codec)
1044{
1045 int i;
1046 if (!codec->pins_shutup)
1047 return;
1048 if (codec->bus->shutdown)
1049 return;
1050 for (i = 0; i < codec->init_pins.used; i++) {
1051 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1052 snd_hda_codec_write(codec, pin->nid, 0,
1053 AC_VERB_SET_PIN_WIDGET_CONTROL,
1054 pin->ctrl);
1055 }
1056 codec->pins_shutup = 0;
1057}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001058#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001059
David Henningsson26a6cb62012-10-09 15:04:21 +02001060static void hda_jackpoll_work(struct work_struct *work)
1061{
1062 struct hda_codec *codec =
1063 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001064
1065 snd_hda_jack_set_dirty_all(codec);
1066 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001067
1068 if (!codec->jackpoll_interval)
1069 return;
1070
David Henningsson26a6cb62012-10-09 15:04:21 +02001071 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1072 codec->jackpoll_interval);
1073}
1074
Takashi Iwai01751f52007-08-10 16:59:39 +02001075static void init_hda_cache(struct hda_cache_rec *cache,
1076 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001077static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001078
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001079/* release all pincfg lists */
1080static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001081{
Takashi Iwai346ff702009-02-23 09:42:57 +01001082 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001083#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001084 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001085#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001086 snd_array_free(&codec->init_pins);
1087}
1088
Takashi Iwai54d17402005-11-21 16:33:22 +01001089/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001090 * audio-converter setup caches
1091 */
1092struct hda_cvt_setup {
1093 hda_nid_t nid;
1094 u8 stream_tag;
1095 u8 channel_id;
1096 u16 format_id;
1097 unsigned char active; /* cvt is currently used */
1098 unsigned char dirty; /* setups should be cleared */
1099};
1100
1101/* get or create a cache entry for the given audio converter NID */
1102static struct hda_cvt_setup *
1103get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1104{
1105 struct hda_cvt_setup *p;
1106 int i;
1107
1108 for (i = 0; i < codec->cvt_setups.used; i++) {
1109 p = snd_array_elem(&codec->cvt_setups, i);
1110 if (p->nid == nid)
1111 return p;
1112 }
1113 p = snd_array_new(&codec->cvt_setups);
1114 if (p)
1115 p->nid = nid;
1116 return p;
1117}
1118
1119/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * codec destructor
1121 */
1122static void snd_hda_codec_free(struct hda_codec *codec)
1123{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001124 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001126 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001127 if (device_is_registered(hda_codec_dev(codec)))
1128 device_del(hda_codec_dev(codec));
Takashi Iwai59cad162012-06-26 15:00:20 +02001129 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001130 free_init_pincfgs(codec);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001131 flush_workqueue(codec->bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001133 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001134 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001135 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001136 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001137 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 codec->bus->caddr_tbl[codec->addr] = NULL;
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001139 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001140 snd_hda_sysfs_clear(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +02001141 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001142 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001143 kfree(codec->vendor_name);
1144 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001145 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001146 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001147 codec->bus->num_codecs--;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001148 put_device(hda_codec_dev(codec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001151static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1152 hda_nid_t fg, unsigned int power_state);
1153
Takashi Iwaid8193872012-08-31 07:54:38 -07001154static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001155 unsigned int power_state);
1156
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001157static int snd_hda_codec_dev_register(struct snd_device *device)
1158{
1159 struct hda_codec *codec = device->device_data;
1160
Takashi Iwaid604b392014-02-28 13:42:09 +01001161 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001162 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001163 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwai709949f2015-02-20 09:58:14 +01001164 /* it was powered up in snd_hda_codec_new(), now all done */
1165 snd_hda_power_down(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 Iwai6efdd852015-02-27 16:09:22 +01001197int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1198 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001201 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001202 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001203 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001205 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001206 .dev_register = snd_hda_codec_dev_register,
1207 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001208 .dev_free = snd_hda_codec_dev_free,
1209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Takashi Iwaida3cec32008-08-08 17:12:14 +02001211 if (snd_BUG_ON(!bus))
1212 return -EINVAL;
1213 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001217 dev_err(card->dev,
Takashi Iwai4e76a882014-02-25 12:21:03 +01001218 "address 0x%x is already occupied\n",
1219 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EBUSY;
1221 }
1222
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001223 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001224 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001227 dev = hda_codec_dev(codec);
1228 device_initialize(dev);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001229 dev->parent = card->dev;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001230 dev->bus = &snd_hda_bus_type;
1231 dev->release = snd_hda_codec_dev_release;
1232 dev->groups = snd_hda_dev_attr_groups;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001233 dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001234 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001235 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001238 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001240 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001241 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001242 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001243 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001244 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001245 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1246 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001247 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001248 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001249 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001250 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001251 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001252 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001253 INIT_LIST_HEAD(&codec->conn_list);
1254
David Henningsson26a6cb62012-10-09 15:04:21 +02001255 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001256 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001257 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Takashi Iwai83012a72012-08-24 18:38:08 +02001259#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001260 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
Takashi Iwai709949f2015-02-20 09:58:14 +01001261 * it's powered down later in snd_hda_codec_dev_register().
Takashi Iwaicb53c622007-08-10 17:21:45 +02001262 */
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001263 set_bit(codec->addr, &bus->codec_powered);
Takashi Iwaicc72da72015-02-19 16:00:22 +01001264 pm_runtime_set_active(hda_codec_dev(codec));
1265 pm_runtime_get_noresume(hda_codec_dev(codec));
1266 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001267#endif
1268
Takashi Iwai648a8d22014-02-25 10:38:13 +01001269 snd_hda_sysfs_init(codec);
1270
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001271 if (codec->bus->modelname) {
1272 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1273 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001274 err = -ENODEV;
1275 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001276 }
1277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001280 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 bus->caddr_tbl[codec_addr] = codec;
1283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001286 if (codec->vendor_id == -1)
1287 /* read again, hopefully the access method was corrected
1288 * in the last read...
1289 */
1290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_SUBSYSTEM_ID);
1294 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 if (!codec->afg && !codec->mfg) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001299 dev_err(card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = -ENODEV;
1301 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Takashi Iwaid8193872012-08-31 07:54:38 -07001304 fg = codec->afg ? codec->afg : codec->mfg;
1305 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001306 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = read_pin_defaults(codec);
1309 if (err < 0)
1310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 }
1317
Takashi Iwai83012a72012-08-24 18:38:08 +02001318#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001320 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001321#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001322 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001323 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001326 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_codec_proc_new(codec);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331
1332 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1333 codec->subsystem_id, codec->revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001334 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001335
Takashi Iwai6efdd852015-02-27 16:09:22 +01001336 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001337 if (err < 0)
1338 goto error;
1339
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340 if (codecp)
1341 *codecp = codec;
1342 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001343
1344 error:
1345 snd_hda_codec_free(codec);
1346 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001348EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001349
Takashi Iwai95a962c2014-10-29 16:03:58 +01001350/**
1351 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1352 * @codec: the HDA codec
1353 *
1354 * Forcibly refresh the all widget caps and the init pin configurations of
1355 * the given codec.
1356 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001357int snd_hda_codec_update_widgets(struct hda_codec *codec)
1358{
1359 hda_nid_t fg;
1360 int err;
1361
1362 /* Assume the function group node does not change,
1363 * only the widget nodes may change.
1364 */
1365 kfree(codec->wcaps);
1366 fg = codec->afg ? codec->afg : codec->mfg;
1367 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +01001368 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -05001369 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -05001370
1371 snd_array_free(&codec->init_pins);
1372 err = read_pin_defaults(codec);
1373
1374 return err;
1375}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001376EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001377
Takashi Iwaied360812012-08-08 17:12:52 +02001378/* update the stream-id if changed */
1379static void update_pcm_stream_id(struct hda_codec *codec,
1380 struct hda_cvt_setup *p, hda_nid_t nid,
1381 u32 stream_tag, int channel_id)
1382{
1383 unsigned int oldval, newval;
1384
1385 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1386 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1387 newval = (stream_tag << 4) | channel_id;
1388 if (oldval != newval)
1389 snd_hda_codec_write(codec, nid, 0,
1390 AC_VERB_SET_CHANNEL_STREAMID,
1391 newval);
1392 p->stream_tag = stream_tag;
1393 p->channel_id = channel_id;
1394 }
1395}
1396
1397/* update the format-id if changed */
1398static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1399 hda_nid_t nid, int format)
1400{
1401 unsigned int oldval;
1402
1403 if (p->format_id != format) {
1404 oldval = snd_hda_codec_read(codec, nid, 0,
1405 AC_VERB_GET_STREAM_FORMAT, 0);
1406 if (oldval != format) {
1407 msleep(1);
1408 snd_hda_codec_write(codec, nid, 0,
1409 AC_VERB_SET_STREAM_FORMAT,
1410 format);
1411 }
1412 p->format_id = format;
1413 }
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/**
1417 * snd_hda_codec_setup_stream - set up the codec for streaming
1418 * @codec: the CODEC to set up
1419 * @nid: the NID to set up
1420 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1421 * @channel_id: channel id to pass, zero based.
1422 * @format: stream format.
1423 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1425 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int channel_id, int format)
1427{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001428 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001429 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001430 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001431 int i;
1432
Takashi Iwai0ba21762007-04-16 11:29:14 +02001433 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001434 return;
1435
Takashi Iwai4e76a882014-02-25 12:21:03 +01001436 codec_dbg(codec,
1437 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1438 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001439 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001440 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001441 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001442
1443 if (codec->pcm_format_first)
1444 update_pcm_format(codec, p, nid, format);
1445 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1446 if (!codec->pcm_format_first)
1447 update_pcm_format(codec, p, nid, format);
1448
Takashi Iwaieb541332010-08-06 13:48:11 +02001449 p->active = 1;
1450 p->dirty = 0;
1451
1452 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001453 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001454 list_for_each_entry(c, &codec->bus->codec_list, list) {
1455 for (i = 0; i < c->cvt_setups.used; i++) {
1456 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001457 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001458 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001459 p->dirty = 1;
1460 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001463EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Takashi Iwaif0cea792010-08-13 11:56:53 +02001465static void really_cleanup_stream(struct hda_codec *codec,
1466 struct hda_cvt_setup *q);
1467
Takashi Iwaid5191e52009-11-16 14:58:17 +01001468/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001469 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001470 * @codec: the CODEC to clean up
1471 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001472 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001473 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001474void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1475 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001476{
Takashi Iwaieb541332010-08-06 13:48:11 +02001477 struct hda_cvt_setup *p;
1478
Takashi Iwai888afa12008-03-18 09:57:50 +01001479 if (!nid)
1480 return;
1481
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001482 if (codec->no_sticky_stream)
1483 do_now = 1;
1484
Takashi Iwai4e76a882014-02-25 12:21:03 +01001485 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001486 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001487 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488 /* here we just clear the active flag when do_now isn't set;
1489 * actual clean-ups will be done later in
1490 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1491 */
1492 if (do_now)
1493 really_cleanup_stream(codec, p);
1494 else
1495 p->active = 0;
1496 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001497}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001498EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001499
Takashi Iwaieb541332010-08-06 13:48:11 +02001500static void really_cleanup_stream(struct hda_codec *codec,
1501 struct hda_cvt_setup *q)
1502{
1503 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001504 if (q->stream_tag || q->channel_id)
1505 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1506 if (q->format_id)
1507 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1508);
Takashi Iwaieb541332010-08-06 13:48:11 +02001509 memset(q, 0, sizeof(*q));
1510 q->nid = nid;
1511}
1512
1513/* clean up the all conflicting obsolete streams */
1514static void purify_inactive_streams(struct hda_codec *codec)
1515{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001516 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001517 int i;
1518
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001519 list_for_each_entry(c, &codec->bus->codec_list, list) {
1520 for (i = 0; i < c->cvt_setups.used; i++) {
1521 struct hda_cvt_setup *p;
1522 p = snd_array_elem(&c->cvt_setups, i);
1523 if (p->dirty)
1524 really_cleanup_stream(c, p);
1525 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001526 }
1527}
1528
Takashi Iwai2a439522011-07-26 09:52:50 +02001529#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001530/* clean up all streams; called from suspend */
1531static void hda_cleanup_all_streams(struct hda_codec *codec)
1532{
1533 int i;
1534
1535 for (i = 0; i < codec->cvt_setups.used; i++) {
1536 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1537 if (p->stream_tag)
1538 really_cleanup_stream(codec, p);
1539 }
1540}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001541#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/*
1544 * amp access functions
1545 */
1546
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001547/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001548#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001549#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001550#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1551#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001553#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001556static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001557 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwai01751f52007-08-10 16:59:39 +02001559 memset(cache, 0, sizeof(*cache));
1560 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001561 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001562}
1563
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001564static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001565{
Takashi Iwai603c4012008-07-30 15:01:44 +02001566 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001570static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Takashi Iwai01751f52007-08-10 16:59:39 +02001572 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1573 u16 cur = cache->hash[idx];
1574 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001577 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (info->key == key)
1579 return info;
1580 cur = info->next;
1581 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001582 return NULL;
1583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001585/* query the hash. allocate an entry if not found. */
1586static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1587 u32 key)
1588{
1589 struct hda_cache_head *info = get_hash(cache, key);
1590 if (!info) {
1591 u16 idx, cur;
1592 /* add a new hash entry */
1593 info = snd_array_new(&cache->buf);
1594 if (!info)
1595 return NULL;
1596 cur = snd_array_index(&cache->buf, info);
1597 info->key = key;
1598 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001599 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001600 idx = key % (u16)ARRAY_SIZE(cache->hash);
1601 info->next = cache->hash[idx];
1602 cache->hash[idx] = cur;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return info;
1605}
1606
Takashi Iwai01751f52007-08-10 16:59:39 +02001607/* query and allocate an amp hash entry */
1608static inline struct hda_amp_info *
1609get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1610{
1611 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1612}
1613
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001614/* overwrite the value with the key in the caps hash */
1615static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1616{
1617 struct hda_amp_info *info;
1618
1619 mutex_lock(&codec->hash_mutex);
1620 info = get_alloc_amp_hash(codec, key);
1621 if (!info) {
1622 mutex_unlock(&codec->hash_mutex);
1623 return -EINVAL;
1624 }
1625 info->amp_caps = val;
1626 info->head.val |= INFO_AMP_CAPS;
1627 mutex_unlock(&codec->hash_mutex);
1628 return 0;
1629}
1630
1631/* query the value from the caps hash; if not found, fetch the current
1632 * value from the given function and store in the hash
1633 */
1634static unsigned int
1635query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1636 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1637{
1638 struct hda_amp_info *info;
1639 unsigned int val;
1640
1641 mutex_lock(&codec->hash_mutex);
1642 info = get_alloc_amp_hash(codec, key);
1643 if (!info) {
1644 mutex_unlock(&codec->hash_mutex);
1645 return 0;
1646 }
1647 if (!(info->head.val & INFO_AMP_CAPS)) {
1648 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1649 val = func(codec, nid, dir);
1650 write_caps_hash(codec, key, val);
1651 } else {
1652 val = info->amp_caps;
1653 mutex_unlock(&codec->hash_mutex);
1654 }
1655 return val;
1656}
1657
1658static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1659 int direction)
1660{
1661 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1662 nid = codec->afg;
1663 return snd_hda_param_read(codec, nid,
1664 direction == HDA_OUTPUT ?
1665 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1666}
1667
Takashi Iwaid5191e52009-11-16 14:58:17 +01001668/**
1669 * query_amp_caps - query AMP capabilities
1670 * @codec: the HD-auio codec
1671 * @nid: the NID to query
1672 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1673 *
1674 * Query AMP capabilities for the given widget and direction.
1675 * Returns the obtained capability bits.
1676 *
1677 * When cap bits have been already read, this doesn't read again but
1678 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001680u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001682 return query_caps_hash(codec, nid, direction,
1683 HDA_HASH_KEY(nid, direction, 0),
1684 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001686EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Takashi Iwaid5191e52009-11-16 14:58:17 +01001688/**
David Henningsson861a04e2014-09-23 10:38:17 +02001689 * snd_hda_check_amp_caps - query AMP capabilities
1690 * @codec: the HD-audio codec
1691 * @nid: the NID to query
1692 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001693 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001694 *
1695 * Check whether the widget has the given amp capability for the direction.
1696 */
1697bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1698 int dir, unsigned int bits)
1699{
1700 if (!nid)
1701 return false;
1702 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1703 if (query_amp_caps(codec, nid, dir) & bits)
1704 return true;
1705 return false;
1706}
1707EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1708
1709/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001710 * snd_hda_override_amp_caps - Override the AMP capabilities
1711 * @codec: the CODEC to clean up
1712 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001713 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001714 * @caps: the capability bits to set
1715 *
1716 * Override the cached AMP caps bits value by the given one.
1717 * This function is useful if the driver needs to adjust the AMP ranges,
1718 * e.g. limit to 0dB, etc.
1719 *
1720 * Returns zero if successful or a negative error code.
1721 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001722int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1723 unsigned int caps)
1724{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001725 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001726}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001727EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001728
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001729static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1730 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001731{
1732 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1733}
1734
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735/**
1736 * snd_hda_query_pin_caps - Query PIN capabilities
1737 * @codec: the HD-auio codec
1738 * @nid: the NID to query
1739 *
1740 * Query PIN capabilities for the given widget.
1741 * Returns the obtained capability bits.
1742 *
1743 * When cap bits have been already read, this doesn't read again but
1744 * returns the cached value.
1745 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001746u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1747{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001748 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001749 read_pin_cap);
1750}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001751EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001752
Wu Fengguang864f92b2009-11-18 12:38:02 +08001753/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001754 * snd_hda_override_pin_caps - Override the pin capabilities
1755 * @codec: the CODEC
1756 * @nid: the NID to override
1757 * @caps: the capability bits to set
1758 *
1759 * Override the cached PIN capabilitiy bits value by the given one.
1760 *
1761 * Returns zero if successful or a negative error code.
1762 */
1763int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1764 unsigned int caps)
1765{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001766 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001767}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001768EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001769
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001770/* read or sync the hash value with the current value;
1771 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001773static struct hda_amp_info *
1774update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001775 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001777 struct hda_amp_info *info;
1778 unsigned int parm, val = 0;
1779 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001781 retry:
1782 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1783 if (!info)
1784 return NULL;
1785 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1786 if (!val_read) {
1787 mutex_unlock(&codec->hash_mutex);
1788 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1789 parm |= direction == HDA_OUTPUT ?
1790 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1791 parm |= index;
1792 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001793 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001794 val &= 0xff;
1795 val_read = true;
1796 mutex_lock(&codec->hash_mutex);
1797 goto retry;
1798 }
1799 info->vol[ch] = val;
1800 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001801 } else if (init_only)
1802 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001803 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001807 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001809static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001810 hda_nid_t nid, int ch, int direction, int index,
1811 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 u32 parm;
1814
1815 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1816 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1817 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001818 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1819 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001820 ; /* set the zero value as a fake mute */
1821 else
1822 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1824}
1825
Takashi Iwaid5191e52009-11-16 14:58:17 +01001826/**
1827 * snd_hda_codec_amp_read - Read AMP value
1828 * @codec: HD-audio codec
1829 * @nid: NID to read the AMP value
1830 * @ch: channel (left=0 or right=1)
1831 * @direction: #HDA_INPUT or #HDA_OUTPUT
1832 * @index: the index value (only for input direction)
1833 *
1834 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 */
Takashi Iwai834be882006-03-01 14:16:17 +01001836int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1837 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001839 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001840 unsigned int val = 0;
1841
1842 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001843 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001844 if (info)
1845 val = info->vol[ch];
1846 mutex_unlock(&codec->hash_mutex);
1847 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001849EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Takashi Iwai280e57d2012-12-14 10:32:21 +01001851static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1852 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001853 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001854{
1855 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001856 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001857
1858 if (snd_BUG_ON(mask & ~0xff))
1859 mask &= 0xff;
1860 val &= mask;
1861
1862 mutex_lock(&codec->hash_mutex);
1863 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1864 if (!info) {
1865 mutex_unlock(&codec->hash_mutex);
1866 return 0;
1867 }
1868 val |= info->vol[ch] & ~mask;
1869 if (info->vol[ch] == val) {
1870 mutex_unlock(&codec->hash_mutex);
1871 return 0;
1872 }
1873 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001874 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001875 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001876 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001877 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001878 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001879 return 1;
1880}
1881
Takashi Iwaid5191e52009-11-16 14:58:17 +01001882/**
1883 * snd_hda_codec_amp_update - update the AMP value
1884 * @codec: HD-audio codec
1885 * @nid: NID to read the AMP value
1886 * @ch: channel (left=0 or right=1)
1887 * @direction: #HDA_INPUT or #HDA_OUTPUT
1888 * @idx: the index value (only for input direction)
1889 * @mask: bit mask to set
1890 * @val: the bits value to set
1891 *
1892 * Update the AMP value with a bit mask.
1893 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001894 */
Takashi Iwai834be882006-03-01 14:16:17 +01001895int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1896 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001898 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1899 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001901EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Takashi Iwaid5191e52009-11-16 14:58:17 +01001903/**
1904 * snd_hda_codec_amp_stereo - update the AMP stereo values
1905 * @codec: HD-audio codec
1906 * @nid: NID to read the AMP value
1907 * @direction: #HDA_INPUT or #HDA_OUTPUT
1908 * @idx: the index value (only for input direction)
1909 * @mask: bit mask to set
1910 * @val: the bits value to set
1911 *
1912 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1913 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001914 */
1915int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1916 int direction, int idx, int mask, int val)
1917{
1918 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001919
1920 if (snd_BUG_ON(mask & ~0xff))
1921 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001922 for (ch = 0; ch < 2; ch++)
1923 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1924 idx, mask, val);
1925 return ret;
1926}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001927EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001928
Takashi Iwai95a962c2014-10-29 16:03:58 +01001929/**
1930 * snd_hda_codec_amp_init - initialize the AMP value
1931 * @codec: the HDA codec
1932 * @nid: NID to read the AMP value
1933 * @ch: channel (left=0 or right=1)
1934 * @dir: #HDA_INPUT or #HDA_OUTPUT
1935 * @idx: the index value (only for input direction)
1936 * @mask: bit mask to set
1937 * @val: the bits value to set
1938 *
1939 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001940 * the first access. If the amp was already initialized / updated beforehand,
1941 * this does nothing.
1942 */
1943int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1944 int dir, int idx, int mask, int val)
1945{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001946 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1947 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001948}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001949EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001950
Takashi Iwai95a962c2014-10-29 16:03:58 +01001951/**
1952 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1953 * @codec: the HDA codec
1954 * @nid: NID to read the AMP value
1955 * @dir: #HDA_INPUT or #HDA_OUTPUT
1956 * @idx: the index value (only for input direction)
1957 * @mask: bit mask to set
1958 * @val: the bits value to set
1959 *
1960 * Call snd_hda_codec_amp_init() for both stereo channels.
1961 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001962int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1963 int dir, int idx, int mask, int val)
1964{
1965 int ch, ret = 0;
1966
1967 if (snd_BUG_ON(mask & ~0xff))
1968 mask &= 0xff;
1969 for (ch = 0; ch < 2; ch++)
1970 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1971 idx, mask, val);
1972 return ret;
1973}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001974EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001975
Takashi Iwaid5191e52009-11-16 14:58:17 +01001976/**
1977 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1978 * @codec: HD-audio codec
1979 *
1980 * Resume the all amp commands from the cache.
1981 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001982void snd_hda_codec_resume_amp(struct hda_codec *codec)
1983{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001984 int i;
1985
Takashi Iwaic370dd62012-12-13 18:30:04 +01001986 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001987 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001988 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1989 struct hda_amp_info *buffer;
1990 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001991 hda_nid_t nid;
1992 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001993 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001994
1995 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01001996 if (!buffer->head.dirty)
1997 continue;
1998 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001999 info = *buffer;
2000 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002001 if (!key)
2002 continue;
2003 nid = key & 0xff;
2004 idx = (key >> 16) & 0xff;
2005 dir = (key >> 24) & 0xff;
2006 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002007 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002008 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002009 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002010 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2011 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002012 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002013 }
2014 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002015 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002016}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002017EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002019static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2020 unsigned int ofs)
2021{
2022 u32 caps = query_amp_caps(codec, nid, dir);
2023 /* get num steps */
2024 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2025 if (ofs < caps)
2026 caps -= ofs;
2027 return caps;
2028}
2029
Takashi Iwaid5191e52009-11-16 14:58:17 +01002030/**
2031 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002032 * @kcontrol: referred ctl element
2033 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034 *
2035 * The control element is supposed to have the private_value field
2036 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2037 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002038int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2042 u16 nid = get_amp_nid(kcontrol);
2043 u8 chs = get_amp_channels(kcontrol);
2044 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002045 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002047 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2048 uinfo->count = chs == 3 ? 2 : 1;
2049 uinfo->value.integer.min = 0;
2050 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2051 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002052 codec_warn(codec,
2053 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2054 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return -EINVAL;
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
2058}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002059EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002061
2062static inline unsigned int
2063read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2064 int ch, int dir, int idx, unsigned int ofs)
2065{
2066 unsigned int val;
2067 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2068 val &= HDA_AMP_VOLMASK;
2069 if (val >= ofs)
2070 val -= ofs;
2071 else
2072 val = 0;
2073 return val;
2074}
2075
2076static inline int
2077update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2078 int ch, int dir, int idx, unsigned int ofs,
2079 unsigned int val)
2080{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002081 unsigned int maxval;
2082
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002083 if (val > 0)
2084 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002085 /* ofs = 0: raw max value */
2086 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002087 if (val > maxval)
2088 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002089 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2090 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002091}
2092
Takashi Iwaid5191e52009-11-16 14:58:17 +01002093/**
2094 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002095 * @kcontrol: ctl element
2096 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002097 *
2098 * The control element is supposed to have the private_value field
2099 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2100 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002101int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2102 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2105 hda_nid_t nid = get_amp_nid(kcontrol);
2106 int chs = get_amp_channels(kcontrol);
2107 int dir = get_amp_direction(kcontrol);
2108 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002109 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 long *valp = ucontrol->value.integer.value;
2111
2112 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002113 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002115 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return 0;
2117}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002118EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002122 * @kcontrol: ctl element
2123 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124 *
2125 * The control element is supposed to have the private_value field
2126 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2127 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002128int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2129 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2132 hda_nid_t nid = get_amp_nid(kcontrol);
2133 int chs = get_amp_channels(kcontrol);
2134 int dir = get_amp_direction(kcontrol);
2135 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002136 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 long *valp = ucontrol->value.integer.value;
2138 int change = 0;
2139
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002140 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002141 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002142 valp++;
2143 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002144 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002145 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return change;
2147}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002148EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150/**
2151 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002152 * @kcontrol: ctl element
2153 * @op_flag: operation flag
2154 * @size: byte size of input TLV
2155 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002156 *
2157 * The control element is supposed to have the private_value field
2158 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2159 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002160int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2161 unsigned int size, unsigned int __user *_tlv)
2162{
2163 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2164 hda_nid_t nid = get_amp_nid(kcontrol);
2165 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002166 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002167 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002168 u32 caps, val1, val2;
2169
2170 if (size < 4 * sizeof(unsigned int))
2171 return -ENOMEM;
2172 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002173 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2174 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002175 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002176 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002177 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002178 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002179 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002180 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2181 return -EFAULT;
2182 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2183 return -EFAULT;
2184 if (put_user(val1, _tlv + 2))
2185 return -EFAULT;
2186 if (put_user(val2, _tlv + 3))
2187 return -EFAULT;
2188 return 0;
2189}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002190EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002191
Takashi Iwaid5191e52009-11-16 14:58:17 +01002192/**
2193 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2194 * @codec: HD-audio codec
2195 * @nid: NID of a reference widget
2196 * @dir: #HDA_INPUT or #HDA_OUTPUT
2197 * @tlv: TLV data to be stored, at least 4 elements
2198 *
2199 * Set (static) TLV data for a virtual master volume using the AMP caps
2200 * obtained from the reference NID.
2201 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002202 */
2203void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2204 unsigned int *tlv)
2205{
2206 u32 caps;
2207 int nums, step;
2208
2209 caps = query_amp_caps(codec, nid, dir);
2210 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2211 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2212 step = (step + 1) * 25;
2213 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2214 tlv[1] = 2 * sizeof(unsigned int);
2215 tlv[2] = -nums * step;
2216 tlv[3] = step;
2217}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002218EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002219
2220/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002221static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002222find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002223{
2224 struct snd_ctl_elem_id id;
2225 memset(&id, 0, sizeof(id));
2226 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002227 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002228 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002229 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2230 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002231 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002232 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002233}
2234
Takashi Iwaid5191e52009-11-16 14:58:17 +01002235/**
2236 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2237 * @codec: HD-audio codec
2238 * @name: ctl id name string
2239 *
2240 * Get the control element with the given id string and IFACE_MIXER.
2241 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002242struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2243 const char *name)
2244{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002245 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002246}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002247EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002248
Takashi Iwaidcda5802012-10-12 17:24:51 +02002249static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002250 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002251{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002252 int i, idx;
2253 /* 16 ctlrs should be large enough */
2254 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2255 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002256 return idx;
2257 }
2258 return -EBUSY;
2259}
2260
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002262 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002263 * @codec: HD-audio codec
2264 * @nid: corresponding NID (optional)
2265 * @kctl: the control element to assign
2266 *
2267 * Add the given control element to an array inside the codec instance.
2268 * All control elements belonging to a codec are supposed to be added
2269 * by this function so that a proper clean-up works at the free or
2270 * reconfiguration time.
2271 *
2272 * If non-zero @nid is passed, the NID is assigned to the control element.
2273 * The assignment is shown in the codec proc file.
2274 *
2275 * snd_hda_ctl_add() checks the control subdev id field whether
2276 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002277 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2278 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002279 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002280int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2281 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002282{
2283 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002284 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002285 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002286
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002287 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002288 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002289 if (nid == 0)
2290 nid = get_amp_nid_(kctl->private_value);
2291 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002292 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2293 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002294 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002295 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002296 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002297 if (err < 0)
2298 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002299 item = snd_array_new(&codec->mixers);
2300 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002301 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002302 item->kctl = kctl;
2303 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002304 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002305 return 0;
2306}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002307EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002308
Takashi Iwaid5191e52009-11-16 14:58:17 +01002309/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002310 * snd_hda_add_nid - Assign a NID to a control element
2311 * @codec: HD-audio codec
2312 * @nid: corresponding NID (optional)
2313 * @kctl: the control element to assign
2314 * @index: index to kctl
2315 *
2316 * Add the given control element to an array inside the codec instance.
2317 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2318 * NID:KCTL mapping - for example "Capture Source" selector.
2319 */
2320int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2321 unsigned int index, hda_nid_t nid)
2322{
2323 struct hda_nid_item *item;
2324
2325 if (nid > 0) {
2326 item = snd_array_new(&codec->nids);
2327 if (!item)
2328 return -ENOMEM;
2329 item->kctl = kctl;
2330 item->index = index;
2331 item->nid = nid;
2332 return 0;
2333 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002334 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2335 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002336 return -EINVAL;
2337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002338EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002339
2340/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002341 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2342 * @codec: HD-audio codec
2343 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002344void snd_hda_ctls_clear(struct hda_codec *codec)
2345{
2346 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002347 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002349 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002350 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002351 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002352}
2353
Takashi Iwai95a962c2014-10-29 16:03:58 +01002354/**
2355 * snd_hda_lock_devices - pseudo device locking
2356 * @bus: the BUS
2357 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358 * toggle card->shutdown to allow/disallow the device access (as a hack)
2359 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002360int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002361{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002362 struct snd_card *card = bus->card;
2363 struct hda_codec *codec;
2364
Takashi Iwaia65d6292009-02-23 16:57:04 +01002365 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002366 if (card->shutdown)
2367 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002368 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002369 if (!list_empty(&card->ctl_files))
2370 goto err_clear;
2371
2372 list_for_each_entry(codec, &bus->codec_list, list) {
2373 int pcm;
2374 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2375 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2376 if (!cpcm->pcm)
2377 continue;
2378 if (cpcm->pcm->streams[0].substream_opened ||
2379 cpcm->pcm->streams[1].substream_opened)
2380 goto err_clear;
2381 }
2382 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002383 spin_unlock(&card->files_lock);
2384 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002385
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002386 err_clear:
2387 card->shutdown = 0;
2388 err_unlock:
2389 spin_unlock(&card->files_lock);
2390 return -EINVAL;
2391}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002392EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002393
Takashi Iwai95a962c2014-10-29 16:03:58 +01002394/**
2395 * snd_hda_unlock_devices - pseudo device unlocking
2396 * @bus: the BUS
2397 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002398void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002399{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002400 struct snd_card *card = bus->card;
2401
2402 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002403 spin_lock(&card->files_lock);
2404 card->shutdown = 0;
2405 spin_unlock(&card->files_lock);
2406}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002407EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002408
Takashi Iwaid5191e52009-11-16 14:58:17 +01002409/**
2410 * snd_hda_codec_reset - Clear all objects assigned to the codec
2411 * @codec: HD-audio codec
2412 *
2413 * This frees the all PCM and control elements assigned to the codec, and
2414 * clears the caches and restores the pin default configurations.
2415 *
2416 * When a device is being used, it returns -EBSY. If successfully freed,
2417 * returns zero.
2418 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002419int snd_hda_codec_reset(struct hda_codec *codec)
2420{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002421 struct hda_bus *bus = codec->bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002422 struct snd_card *card = codec->card;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002425 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002426 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002427
2428 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002429 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002430 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002431 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002432 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002433 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002434 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002435 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002436 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002437 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002438 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002439 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002440 snd_hda_detach_beep_device(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002441 if (device_is_registered(hda_codec_dev(codec)))
2442 device_del(hda_codec_dev(codec));
2443
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002444 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002445 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002446 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002447 codec->spec = NULL;
2448 free_hda_cache(&codec->amp_cache);
2449 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002450 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2451 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002452 /* free only driver_pins so that init_pins + user_pins are restored */
2453 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002454 snd_array_free(&codec->cvt_setups);
2455 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002456 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002457 codec->num_pcms = 0;
2458 codec->pcm_info = NULL;
2459 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002460 codec->slave_dig_outs = NULL;
2461 codec->spdif_status_reset = 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002462
2463 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002464 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002465 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002466}
2467
Takashi Iwai6194b992014-06-06 18:12:16 +02002468typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002469
2470/* apply the function to all matching slave ctls in the mixer list */
2471static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002472 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002473{
2474 struct hda_nid_item *items;
2475 const char * const *s;
2476 int i, err;
2477
2478 items = codec->mixers.list;
2479 for (i = 0; i < codec->mixers.used; i++) {
2480 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002481 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002482 continue;
2483 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002484 char tmpname[sizeof(sctl->id.name)];
2485 const char *name = *s;
2486 if (suffix) {
2487 snprintf(tmpname, sizeof(tmpname), "%s %s",
2488 name, suffix);
2489 name = tmpname;
2490 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002491 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002492 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002493 if (err)
2494 return err;
2495 break;
2496 }
2497 }
2498 }
2499 return 0;
2500}
2501
Takashi Iwai6194b992014-06-06 18:12:16 +02002502static int check_slave_present(struct hda_codec *codec,
2503 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002504{
2505 return 1;
2506}
2507
Takashi Iwai18478e82012-03-09 17:51:10 +01002508/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002509static int get_kctl_0dB_offset(struct hda_codec *codec,
2510 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002511{
2512 int _tlv[4];
2513 const int *tlv = NULL;
2514 int val = -1;
2515
2516 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2517 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2518 mm_segment_t fs = get_fs();
2519 set_fs(get_ds());
2520 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2521 tlv = _tlv;
2522 set_fs(fs);
2523 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2524 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002525 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2526 int step = tlv[3];
2527 step &= ~TLV_DB_SCALE_MUTE;
2528 if (!step)
2529 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002530 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002531 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002532- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002533 return -1;
2534 }
2535 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002536 val = -tlv[2] / step;
2537 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002538 return val;
2539}
2540
2541/* call kctl->put with the given value(s) */
2542static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2543{
2544 struct snd_ctl_elem_value *ucontrol;
2545 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2546 if (!ucontrol)
2547 return -ENOMEM;
2548 ucontrol->value.integer.value[0] = val;
2549 ucontrol->value.integer.value[1] = val;
2550 kctl->put(kctl, ucontrol);
2551 kfree(ucontrol);
2552 return 0;
2553}
2554
2555/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002556static int init_slave_0dB(struct hda_codec *codec,
2557 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002558{
Takashi Iwai6194b992014-06-06 18:12:16 +02002559 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002560 if (offset > 0)
2561 put_kctl_with_value(slave, offset);
2562 return 0;
2563}
2564
2565/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002566static int init_slave_unmute(struct hda_codec *codec,
2567 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002568{
2569 return put_kctl_with_value(slave, 1);
2570}
2571
Takashi Iwaie8750942014-06-30 14:02:39 +02002572static int add_slave(struct hda_codec *codec,
2573 void *data, struct snd_kcontrol *slave)
2574{
2575 return snd_ctl_add_slave(data, slave);
2576}
2577
Takashi Iwaid5191e52009-11-16 14:58:17 +01002578/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002579 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002580 * @codec: HD-audio codec
2581 * @name: vmaster control name
2582 * @tlv: TLV data (optional)
2583 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002584 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002585 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002586 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002587 *
2588 * Create a virtual master control with the given name. The TLV data
2589 * must be either NULL or a valid data.
2590 *
2591 * @slaves is a NULL-terminated array of strings, each of which is a
2592 * slave control name. All controls with these names are assigned to
2593 * the new virtual master control.
2594 *
2595 * This function returns zero if successful or a negative error code.
2596 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002597int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002598 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002599 const char *suffix, bool init_slave_vol,
2600 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002601{
2602 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002603 int err;
2604
Takashi Iwai29e58532012-03-12 12:25:03 +01002605 if (ctl_ret)
2606 *ctl_ret = NULL;
2607
Takashi Iwai9322ca52012-02-03 14:28:01 +01002608 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002609 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002610 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002611 return 0;
2612 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002613 kctl = snd_ctl_make_virtual_master(name, tlv);
2614 if (!kctl)
2615 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002616 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002617 if (err < 0)
2618 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002619
Takashi Iwaie8750942014-06-30 14:02:39 +02002620 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002621 if (err < 0)
2622 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002623
2624 /* init with master mute & zero volume */
2625 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002626 if (init_slave_vol) {
2627 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002628 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002629 tlv ? init_slave_0dB : init_slave_unmute, &step);
2630 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002631
Takashi Iwai29e58532012-03-12 12:25:03 +01002632 if (ctl_ret)
2633 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002634 return 0;
2635}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002636EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002637
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002638/*
2639 * mute-LED control using vmaster
2640 */
2641static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_info *uinfo)
2643{
2644 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002645 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002646 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002647
Takashi Iwai3ff72212014-10-20 18:17:28 +02002648 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002649}
2650
2651static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_value *ucontrol)
2653{
2654 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2655 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2656 return 0;
2657}
2658
2659static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2661{
2662 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2663 unsigned int old_mode = hook->mute_mode;
2664
2665 hook->mute_mode = ucontrol->value.enumerated.item[0];
2666 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2667 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2668 if (old_mode == hook->mute_mode)
2669 return 0;
2670 snd_hda_sync_vmaster_hook(hook);
2671 return 1;
2672}
2673
2674static struct snd_kcontrol_new vmaster_mute_mode = {
2675 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2676 .name = "Mute-LED Mode",
2677 .info = vmaster_mute_mode_info,
2678 .get = vmaster_mute_mode_get,
2679 .put = vmaster_mute_mode_put,
2680};
2681
Takashi Iwai95a962c2014-10-29 16:03:58 +01002682/**
2683 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2684 * @codec: the HDA codec
2685 * @hook: the vmaster hook object
2686 * @expose_enum_ctl: flag to create an enum ctl
2687 *
2688 * Add a mute-LED hook with the given vmaster switch kctl.
2689 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2690 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002691 */
2692int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002693 struct hda_vmaster_mute_hook *hook,
2694 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002695{
2696 struct snd_kcontrol *kctl;
2697
2698 if (!hook->hook || !hook->sw_kctl)
2699 return 0;
2700 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2701 hook->codec = codec;
2702 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002703 if (!expose_enum_ctl)
2704 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002705 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2706 if (!kctl)
2707 return -ENOMEM;
2708 return snd_hda_ctl_add(codec, 0, kctl);
2709}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002710EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002711
Takashi Iwai95a962c2014-10-29 16:03:58 +01002712/**
2713 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2714 * @hook: the vmaster hook
2715 *
2716 * Call the hook with the current value for synchronization.
2717 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002718 */
2719void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2720{
2721 if (!hook->hook || !hook->codec)
2722 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002723 /* don't call vmaster hook in the destructor since it might have
2724 * been already destroyed
2725 */
2726 if (hook->codec->bus->shutdown)
2727 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002728 switch (hook->mute_mode) {
2729 case HDA_VMUTE_FOLLOW_MASTER:
2730 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2731 break;
2732 default:
2733 hook->hook(hook->codec, hook->mute_mode);
2734 break;
2735 }
2736}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002737EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002738
2739
Takashi Iwaid5191e52009-11-16 14:58:17 +01002740/**
2741 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002742 * @kcontrol: referred ctl element
2743 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002744 *
2745 * The control element is supposed to have the private_value field
2746 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2747 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002748int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 int chs = get_amp_channels(kcontrol);
2752
2753 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2754 uinfo->count = chs == 3 ? 2 : 1;
2755 uinfo->value.integer.min = 0;
2756 uinfo->value.integer.max = 1;
2757 return 0;
2758}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002759EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761/**
2762 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002763 * @kcontrol: ctl element
2764 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002765 *
2766 * The control element is supposed to have the private_value field
2767 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2768 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002769int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2773 hda_nid_t nid = get_amp_nid(kcontrol);
2774 int chs = get_amp_channels(kcontrol);
2775 int dir = get_amp_direction(kcontrol);
2776 int idx = get_amp_index(kcontrol);
2777 long *valp = ucontrol->value.integer.value;
2778
2779 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002780 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002781 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002783 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002784 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002787EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Takashi Iwaid5191e52009-11-16 14:58:17 +01002789/**
2790 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002791 * @kcontrol: ctl element
2792 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002793 *
2794 * The control element is supposed to have the private_value field
2795 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2801 hda_nid_t nid = get_amp_nid(kcontrol);
2802 int chs = get_amp_channels(kcontrol);
2803 int dir = get_amp_direction(kcontrol);
2804 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 long *valp = ucontrol->value.integer.value;
2806 int change = 0;
2807
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002808 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002809 change = codec_amp_update(codec, nid, 0, dir, idx,
2810 HDA_AMP_MUTE,
2811 *valp ? 0 : HDA_AMP_MUTE, false,
2812 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002813 valp++;
2814 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002815 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002816 change |= codec_amp_update(codec, nid, 1, dir, idx,
2817 HDA_AMP_MUTE,
2818 *valp ? 0 : HDA_AMP_MUTE, false,
2819 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002820 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return change;
2822}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002823EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825/*
Takashi Iwai985be542005-11-02 18:26:49 +01002826 * bound volume controls
2827 *
2828 * bind multiple volumes (# indices, from 0)
2829 */
2830
2831#define AMP_VAL_IDX_SHIFT 19
2832#define AMP_VAL_IDX_MASK (0x0f<<19)
2833
Takashi Iwaid5191e52009-11-16 14:58:17 +01002834/**
2835 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002836 * @kcontrol: ctl element
2837 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002838 *
2839 * The control element is supposed to have the private_value field
2840 * set up via HDA_BIND_MUTE*() macros.
2841 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002842int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2843 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002844{
2845 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2846 unsigned long pval;
2847 int err;
2848
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002849 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002850 pval = kcontrol->private_value;
2851 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2852 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2853 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002854 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002855 return err;
2856}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002857EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002858
Takashi Iwaid5191e52009-11-16 14:58:17 +01002859/**
2860 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002861 * @kcontrol: ctl element
2862 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002863 *
2864 * The control element is supposed to have the private_value field
2865 * set up via HDA_BIND_MUTE*() macros.
2866 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002867int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002869{
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2871 unsigned long pval;
2872 int i, indices, err = 0, change = 0;
2873
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002874 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002875 pval = kcontrol->private_value;
2876 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2877 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2879 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002880 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2881 if (err < 0)
2882 break;
2883 change |= err;
2884 }
2885 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002886 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002887 return err < 0 ? err : change;
2888}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002889EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002890
Takashi Iwaid5191e52009-11-16 14:58:17 +01002891/**
2892 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002893 * @kcontrol: referred ctl element
2894 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002895 *
2896 * The control element is supposed to have the private_value field
2897 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002898 */
2899int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_info *uinfo)
2901{
2902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2903 struct hda_bind_ctls *c;
2904 int err;
2905
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002906 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002907 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002908 kcontrol->private_value = *c->values;
2909 err = c->ops->info(kcontrol, uinfo);
2910 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002911 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002912 return err;
2913}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002914EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002915
Takashi Iwaid5191e52009-11-16 14:58:17 +01002916/**
2917 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002918 * @kcontrol: ctl element
2919 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002920 *
2921 * The control element is supposed to have the private_value field
2922 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2923 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002924int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_value *ucontrol)
2926{
2927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2928 struct hda_bind_ctls *c;
2929 int err;
2930
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002931 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002932 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002933 kcontrol->private_value = *c->values;
2934 err = c->ops->get(kcontrol, ucontrol);
2935 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002936 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002937 return err;
2938}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002939EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002940
Takashi Iwaid5191e52009-11-16 14:58:17 +01002941/**
2942 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002943 * @kcontrol: ctl element
2944 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002945 *
2946 * The control element is supposed to have the private_value field
2947 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2948 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002949int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_value *ucontrol)
2951{
2952 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2953 struct hda_bind_ctls *c;
2954 unsigned long *vals;
2955 int err = 0, change = 0;
2956
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002957 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002958 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002959 for (vals = c->values; *vals; vals++) {
2960 kcontrol->private_value = *vals;
2961 err = c->ops->put(kcontrol, ucontrol);
2962 if (err < 0)
2963 break;
2964 change |= err;
2965 }
2966 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002967 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002968 return err < 0 ? err : change;
2969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002970EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002971
Takashi Iwaid5191e52009-11-16 14:58:17 +01002972/**
2973 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002974 * @kcontrol: ctl element
2975 * @op_flag: operation flag
2976 * @size: byte size of input TLV
2977 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002978 *
2979 * The control element is supposed to have the private_value field
2980 * set up via HDA_BIND_VOL() macro.
2981 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002982int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2983 unsigned int size, unsigned int __user *tlv)
2984{
2985 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2986 struct hda_bind_ctls *c;
2987 int err;
2988
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002989 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002990 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002991 kcontrol->private_value = *c->values;
2992 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2993 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002994 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002995 return err;
2996}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002997EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002998
2999struct hda_ctl_ops snd_hda_bind_vol = {
3000 .info = snd_hda_mixer_amp_volume_info,
3001 .get = snd_hda_mixer_amp_volume_get,
3002 .put = snd_hda_mixer_amp_volume_put,
3003 .tlv = snd_hda_mixer_amp_tlv
3004};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003005EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003006
3007struct hda_ctl_ops snd_hda_bind_sw = {
3008 .info = snd_hda_mixer_amp_switch_info,
3009 .get = snd_hda_mixer_amp_switch_get,
3010 .put = snd_hda_mixer_amp_switch_put,
3011 .tlv = snd_hda_mixer_amp_tlv
3012};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003013EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003014
3015/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 * SPDIF out controls
3017 */
3018
Takashi Iwai0ba21762007-04-16 11:29:14 +02003019static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3020 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3023 uinfo->count = 1;
3024 return 0;
3025}
3026
Takashi Iwai0ba21762007-04-16 11:29:14 +02003027static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3031 IEC958_AES0_NONAUDIO |
3032 IEC958_AES0_CON_EMPHASIS_5015 |
3033 IEC958_AES0_CON_NOT_COPYRIGHT;
3034 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3035 IEC958_AES1_CON_ORIGINAL;
3036 return 0;
3037}
3038
Takashi Iwai0ba21762007-04-16 11:29:14 +02003039static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3040 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3043 IEC958_AES0_NONAUDIO |
3044 IEC958_AES0_PRO_EMPHASIS_5015;
3045 return 0;
3046}
3047
Takashi Iwai0ba21762007-04-16 11:29:14 +02003048static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3049 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003052 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003053 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003055 mutex_lock(&codec->spdif_mutex);
3056 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003057 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3058 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3059 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3060 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003061 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 return 0;
3064}
3065
3066/* convert from SPDIF status bits to HDA SPDIF bits
3067 * bit 0 (DigEn) is always set zero (to be filled later)
3068 */
3069static unsigned short convert_from_spdif_status(unsigned int sbits)
3070{
3071 unsigned short val = 0;
3072
3073 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003076 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003078 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3079 IEC958_AES0_PRO_EMPHASIS_5015)
3080 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3083 IEC958_AES0_CON_EMPHASIS_5015)
3084 val |= AC_DIG1_EMPHASIS;
3085 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3086 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003088 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3090 }
3091 return val;
3092}
3093
3094/* convert to SPDIF status bits from HDA SPDIF bits
3095 */
3096static unsigned int convert_to_spdif_status(unsigned short val)
3097{
3098 unsigned int sbits = 0;
3099
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 sbits |= IEC958_AES0_PROFESSIONAL;
3104 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003105 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3107 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003112 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3114 sbits |= val & (0x7f << 8);
3115 }
3116 return sbits;
3117}
3118
Takashi Iwai2f728532008-09-25 16:32:41 +02003119/* set digital convert verbs both for the given NID and its slaves */
3120static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3121 int verb, int val)
3122{
Takashi Iwaidda14412011-05-02 11:29:30 +02003123 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003124
Takashi Iwai9e976972008-11-25 08:17:20 +01003125 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003126 d = codec->slave_dig_outs;
3127 if (!d)
3128 return;
3129 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003130 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003131}
3132
3133static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3134 int dig1, int dig2)
3135{
3136 if (dig1 != -1)
3137 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3138 if (dig2 != -1)
3139 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3140}
3141
Takashi Iwai0ba21762007-04-16 11:29:14 +02003142static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
3145 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003146 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003147 struct hda_spdif_out *spdif;
3148 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 unsigned short val;
3150 int change;
3151
Ingo Molnar62932df2006-01-16 16:34:20 +01003152 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003153 spdif = snd_array_elem(&codec->spdif_out, idx);
3154 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003155 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3157 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3158 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003159 val = convert_from_spdif_status(spdif->status);
3160 val |= spdif->ctls & 1;
3161 change = spdif->ctls != val;
3162 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003163 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003164 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003165 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return change;
3167}
3168
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003169#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Takashi Iwai0ba21762007-04-16 11:29:14 +02003171static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3172 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
3174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003175 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003176 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003178 mutex_lock(&codec->spdif_mutex);
3179 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003180 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003181 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return 0;
3183}
3184
Stephen Warren74b654c2011-06-01 11:14:18 -06003185static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3186 int dig1, int dig2)
3187{
3188 set_dig_out_convert(codec, nid, dig1, dig2);
3189 /* unmute amp switch (if any) */
3190 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3191 (dig1 & AC_DIG1_ENABLE))
3192 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3193 HDA_AMP_MUTE, 0);
3194}
3195
Takashi Iwai0ba21762007-04-16 11:29:14 +02003196static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
3199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003200 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003201 struct hda_spdif_out *spdif;
3202 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 unsigned short val;
3204 int change;
3205
Ingo Molnar62932df2006-01-16 16:34:20 +01003206 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003207 spdif = snd_array_elem(&codec->spdif_out, idx);
3208 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003209 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003211 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003212 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003213 spdif->ctls = val;
3214 if (change && nid != (u16)-1)
3215 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003216 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return change;
3218}
3219
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003220static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 {
3222 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003224 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 .info = snd_hda_spdif_mask_info,
3226 .get = snd_hda_spdif_cmask_get,
3227 },
3228 {
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, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 .info = snd_hda_spdif_mask_info,
3233 .get = snd_hda_spdif_pmask_get,
3234 },
3235 {
3236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003237 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 .info = snd_hda_spdif_mask_info,
3239 .get = snd_hda_spdif_default_get,
3240 .put = snd_hda_spdif_default_put,
3241 },
3242 {
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003244 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .info = snd_hda_spdif_out_switch_info,
3246 .get = snd_hda_spdif_out_switch_get,
3247 .put = snd_hda_spdif_out_switch_put,
3248 },
3249 { } /* end */
3250};
3251
3252/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003253 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003255 * @associated_nid: NID that new ctls associated with
3256 * @cvt_nid: converter NID
3257 * @type: HDA_PCM_TYPE_*
3258 * Creates controls related with the digital output.
3259 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 *
3261 * Returns 0 if successful, or a negative error code.
3262 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003263int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3264 hda_nid_t associated_nid,
3265 hda_nid_t cvt_nid,
3266 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003269 struct snd_kcontrol *kctl;
3270 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003271 int idx = 0;
3272 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003273 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003274 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003276 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003277 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003278 idx = spdif_index;
3279 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003280 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003281 /* suppose a single SPDIF device */
3282 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3283 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3284 if (!kctl)
3285 break;
3286 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003287 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003288 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003289 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003290 if (!bus->primary_dig_out_type)
3291 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003292
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003293 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003294 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003295 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003296 return -EBUSY;
3297 }
Stephen Warren7c935972011-06-01 11:14:17 -06003298 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003299 if (!spdif)
3300 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3302 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003303 if (!kctl)
3304 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003305 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003306 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003307 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003308 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return err;
3310 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003311 spdif->nid = cvt_nid;
3312 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003313 AC_VERB_GET_DIGI_CONVERT_1, 0);
3314 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003317EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Takashi Iwai95a962c2014-10-29 16:03:58 +01003319/**
3320 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3321 * @codec: the HDA codec
3322 * @nid: widget NID
3323 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003324 * call within spdif_mutex lock
3325 */
Stephen Warren7c935972011-06-01 11:14:17 -06003326struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3327 hda_nid_t nid)
3328{
3329 int i;
3330 for (i = 0; i < codec->spdif_out.used; i++) {
3331 struct hda_spdif_out *spdif =
3332 snd_array_elem(&codec->spdif_out, i);
3333 if (spdif->nid == nid)
3334 return spdif;
3335 }
3336 return NULL;
3337}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003338EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003339
Takashi Iwai95a962c2014-10-29 16:03:58 +01003340/**
3341 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3342 * @codec: the HDA codec
3343 * @idx: the SPDIF ctl index
3344 *
3345 * Unassign the widget from the given SPDIF control.
3346 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003347void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3348{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003349 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003350
3351 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003352 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003353 spdif->nid = (u16)-1;
3354 mutex_unlock(&codec->spdif_mutex);
3355}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003356EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003357
Takashi Iwai95a962c2014-10-29 16:03:58 +01003358/**
3359 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3360 * @codec: the HDA codec
3361 * @idx: the SPDIF ctl idx
3362 * @nid: widget NID
3363 *
3364 * Assign the widget to the SPDIF control with the given index.
3365 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003366void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3367{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003368 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003369 unsigned short val;
3370
3371 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003372 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003373 if (spdif->nid != nid) {
3374 spdif->nid = nid;
3375 val = spdif->ctls;
3376 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3377 }
3378 mutex_unlock(&codec->spdif_mutex);
3379}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003380EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003383 * SPDIF sharing with analog output
3384 */
3385static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_value *ucontrol)
3387{
3388 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3389 ucontrol->value.integer.value[0] = mout->share_spdif;
3390 return 0;
3391}
3392
3393static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3395{
3396 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3397 mout->share_spdif = !!ucontrol->value.integer.value[0];
3398 return 0;
3399}
3400
3401static struct snd_kcontrol_new spdif_share_sw = {
3402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3403 .name = "IEC958 Default PCM Playback Switch",
3404 .info = snd_ctl_boolean_mono_info,
3405 .get = spdif_share_sw_get,
3406 .put = spdif_share_sw_put,
3407};
3408
Takashi Iwaid5191e52009-11-16 14:58:17 +01003409/**
3410 * snd_hda_create_spdif_share_sw - create Default PCM switch
3411 * @codec: the HDA codec
3412 * @mout: multi-out instance
3413 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003414int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3415 struct hda_multi_out *mout)
3416{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003417 struct snd_kcontrol *kctl;
3418
Takashi Iwai9a081602008-02-12 18:37:26 +01003419 if (!mout->dig_out_nid)
3420 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003421
3422 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3423 if (!kctl)
3424 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003425 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003426 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003427}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003428EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003429
3430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 * SPDIF input
3432 */
3433
3434#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3435
Takashi Iwai0ba21762007-04-16 11:29:14 +02003436static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3440
3441 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3442 return 0;
3443}
3444
Takashi Iwai0ba21762007-04-16 11:29:14 +02003445static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3446 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3449 hda_nid_t nid = kcontrol->private_value;
3450 unsigned int val = !!ucontrol->value.integer.value[0];
3451 int change;
3452
Ingo Molnar62932df2006-01-16 16:34:20 +01003453 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003455 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003457 snd_hda_codec_write_cache(codec, nid, 0,
3458 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003460 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return change;
3462}
3463
Takashi Iwai0ba21762007-04-16 11:29:14 +02003464static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
3467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3468 hda_nid_t nid = kcontrol->private_value;
3469 unsigned short val;
3470 unsigned int sbits;
3471
Andrew Paprocki3982d172007-12-19 12:13:44 +01003472 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 sbits = convert_to_spdif_status(val);
3474 ucontrol->value.iec958.status[0] = sbits;
3475 ucontrol->value.iec958.status[1] = sbits >> 8;
3476 ucontrol->value.iec958.status[2] = sbits >> 16;
3477 ucontrol->value.iec958.status[3] = sbits >> 24;
3478 return 0;
3479}
3480
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003481static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 {
3483 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003484 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .info = snd_hda_spdif_in_switch_info,
3486 .get = snd_hda_spdif_in_switch_get,
3487 .put = snd_hda_spdif_in_switch_put,
3488 },
3489 {
3490 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003492 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 .info = snd_hda_spdif_mask_info,
3494 .get = snd_hda_spdif_in_status_get,
3495 },
3496 { } /* end */
3497};
3498
3499/**
3500 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3501 * @codec: the HDA codec
3502 * @nid: audio in widget NID
3503 *
3504 * Creates controls related with the SPDIF input.
3505 * Called from each patch supporting the SPDIF in.
3506 *
3507 * Returns 0 if successful, or a negative error code.
3508 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003509int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
3511 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003512 struct snd_kcontrol *kctl;
3513 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003514 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Takashi Iwaidcda5802012-10-12 17:24:51 +02003516 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003517 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003518 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003519 return -EBUSY;
3520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3522 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003523 if (!kctl)
3524 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003526 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003527 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return err;
3529 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003530 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003531 snd_hda_codec_read(codec, nid, 0,
3532 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003533 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return 0;
3535}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003536EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003538/*
3539 * command cache
3540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Takashi Iwaic370dd62012-12-13 18:30:04 +01003542/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003543#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3544#define get_cmd_cache_nid(key) ((key) & 0xff)
3545#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3546
3547/**
3548 * snd_hda_codec_write_cache - send a single command with caching
3549 * @codec: the HDA codec
3550 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003551 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003552 * @verb: the verb to send
3553 * @parm: the parameter for the verb
3554 *
3555 * Send a single command without waiting for response.
3556 *
3557 * Returns 0 if successful, or a negative error code.
3558 */
3559int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003560 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003561{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003562 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003563 struct hda_cache_head *c;
3564 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003565 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003566
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003567 cache_only = codec->cached_write;
3568 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003569 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003570 if (err < 0)
3571 return err;
3572 }
3573
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003574 /* parm may contain the verb stuff for get/set amp */
3575 verb = verb | (parm >> 8);
3576 parm &= 0xff;
3577 key = build_cmd_cache_key(nid, verb);
3578 mutex_lock(&codec->bus->cmd_mutex);
3579 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003580 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003581 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003582 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003583 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003584 mutex_unlock(&codec->bus->cmd_mutex);
3585 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003586}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003587EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003588
Takashi Iwaid5191e52009-11-16 14:58:17 +01003589/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003590 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3591 * @codec: the HDA codec
3592 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003593 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003594 * @verb: the verb to send
3595 * @parm: the parameter for the verb
3596 *
3597 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3598 * command if the parameter is already identical with the cached value.
3599 * If not, it sends the command and refreshes the cache.
3600 *
3601 * Returns 0 if successful, or a negative error code.
3602 */
3603int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003604 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003605{
3606 struct hda_cache_head *c;
3607 u32 key;
3608
3609 /* parm may contain the verb stuff for get/set amp */
3610 verb = verb | (parm >> 8);
3611 parm &= 0xff;
3612 key = build_cmd_cache_key(nid, verb);
3613 mutex_lock(&codec->bus->cmd_mutex);
3614 c = get_hash(&codec->cmd_cache, key);
3615 if (c && c->val == parm) {
3616 mutex_unlock(&codec->bus->cmd_mutex);
3617 return 0;
3618 }
3619 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003620 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003621}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003622EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003623
3624/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003625 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3626 * @codec: HD-audio codec
3627 *
3628 * Execute all verbs recorded in the command caches to resume.
3629 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003630void snd_hda_codec_resume_cache(struct hda_codec *codec)
3631{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003632 int i;
3633
Takashi Iwaic370dd62012-12-13 18:30:04 +01003634 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003635 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003636 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3637 struct hda_cache_head *buffer;
3638 u32 key;
3639
3640 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3641 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003642 if (!key)
3643 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003644 if (!buffer->dirty)
3645 continue;
3646 buffer->dirty = 0;
3647 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003648 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3649 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003650 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003651 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003652 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003653}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003654EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003655
3656/**
3657 * snd_hda_sequence_write_cache - sequence writes with caching
3658 * @codec: the HDA codec
3659 * @seq: VERB array to send
3660 *
3661 * Send the commands sequentially from the given array.
3662 * Thte commands are recorded on cache for power-save and resume.
3663 * The array must be terminated with NID=0.
3664 */
3665void snd_hda_sequence_write_cache(struct hda_codec *codec,
3666 const struct hda_verb *seq)
3667{
3668 for (; seq->nid; seq++)
3669 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3670 seq->param);
3671}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003672EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003673
Takashi Iwaidc870f32013-01-22 15:24:30 +01003674/**
3675 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3676 * @codec: HD-audio codec
3677 */
3678void snd_hda_codec_flush_cache(struct hda_codec *codec)
3679{
3680 snd_hda_codec_resume_amp(codec);
3681 snd_hda_codec_resume_cache(codec);
3682}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003683EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003684
Takashi Iwai95a962c2014-10-29 16:03:58 +01003685/**
3686 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3687 * @codec: the HDA codec
3688 * @fg: function group (not used now)
3689 * @power_state: the power state to set (AC_PWRST_*)
3690 *
3691 * Set the given power state to all widgets that have the power control.
3692 * If the codec has power_filter set, it evaluates the power state and
3693 * filter out if it's unchanged as D3.
3694 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003695void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003696 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003697{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003698 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003699 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003700
Takashi Iwaicb53c622007-08-10 17:21:45 +02003701 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003702 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003703 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003704 if (!(wcaps & AC_WCAP_POWER))
3705 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003706 if (codec->power_filter) {
3707 state = codec->power_filter(codec, nid, power_state);
3708 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003709 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003710 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003711 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003712 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003713 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003714}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003715EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003716
3717/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003718 * supported power states check
3719 */
3720static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3721 unsigned int power_state)
3722{
3723 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3724
Mengdong Line037cb42012-08-10 14:11:58 +02003725 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003726 return false;
3727 if (sup & power_state)
3728 return true;
3729 else
3730 return false;
3731}
3732
3733/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003734 * wait until the state is reached, returns the current state
3735 */
3736static unsigned int hda_sync_power_state(struct hda_codec *codec,
3737 hda_nid_t fg,
3738 unsigned int power_state)
3739{
3740 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3741 unsigned int state, actual_state;
3742
3743 for (;;) {
3744 state = snd_hda_codec_read(codec, fg, 0,
3745 AC_VERB_GET_POWER_STATE, 0);
3746 if (state & AC_PWRST_ERROR)
3747 break;
3748 actual_state = (state >> 4) & 0x0f;
3749 if (actual_state == power_state)
3750 break;
3751 if (time_after_eq(jiffies, end_time))
3752 break;
3753 /* wait until the codec reachs to the target state */
3754 msleep(1);
3755 }
3756 return state;
3757}
3758
Takashi Iwai95a962c2014-10-29 16:03:58 +01003759/**
3760 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3761 * @codec: the HDA codec
3762 * @nid: widget NID
3763 * @power_state: power state to evalue
3764 *
3765 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3766 * This can be used a codec power_filter callback.
3767 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003768unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3769 hda_nid_t nid,
3770 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003771{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003772 if (nid == codec->afg || nid == codec->mfg)
3773 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003774 if (power_state == AC_PWRST_D3 &&
3775 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3776 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3777 int eapd = snd_hda_codec_read(codec, nid, 0,
3778 AC_VERB_GET_EAPD_BTLENABLE, 0);
3779 if (eapd & 0x02)
3780 return AC_PWRST_D0;
3781 }
3782 return power_state;
3783}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003784EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003785
Takashi Iwai432c6412012-08-28 09:59:20 -07003786/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003787 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003788 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003789static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003790 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003791{
Takashi Iwaid8193872012-08-31 07:54:38 -07003792 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003793 int count;
3794 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003795 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003796
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003797 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003798 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003799 if (codec->depop_delay < 0)
3800 msleep(codec->epss ? 10 : 100);
3801 else if (codec->depop_delay > 0)
3802 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003803 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003804 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003805
3806 /* repeat power states setting at most 10 times*/
3807 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003808 if (codec->patch_ops.set_power_state)
3809 codec->patch_ops.set_power_state(codec, fg,
3810 power_state);
3811 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003812 state = power_state;
3813 if (codec->power_filter)
3814 state = codec->power_filter(codec, fg, state);
3815 if (state == power_state || power_state != AC_PWRST_D3)
3816 snd_hda_codec_read(codec, fg, flags,
3817 AC_VERB_SET_POWER_STATE,
3818 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003819 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003820 }
3821 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003822 if (!(state & AC_PWRST_ERROR))
3823 break;
3824 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003825
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003826 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003827}
Takashi Iwai54d17402005-11-21 16:33:22 +01003828
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003829/* sync power states of all widgets;
3830 * this is called at the end of codec parsing
3831 */
3832static void sync_power_up_states(struct hda_codec *codec)
3833{
3834 hda_nid_t nid = codec->start_nid;
3835 int i;
3836
Takashi Iwaiba615b82013-03-13 14:47:21 +01003837 /* don't care if no filter is used */
3838 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003839 return;
3840
3841 for (i = 0; i < codec->num_nodes; i++, nid++) {
3842 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003843 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003844 if (!(wcaps & AC_WCAP_POWER))
3845 continue;
3846 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3847 if (target == AC_PWRST_D0)
3848 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003849 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003850 snd_hda_codec_write(codec, nid, 0,
3851 AC_VERB_SET_POWER_STATE, target);
3852 }
3853}
3854
Takashi Iwai648a8d22014-02-25 10:38:13 +01003855#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003856/* execute additional init verbs */
3857static void hda_exec_init_verbs(struct hda_codec *codec)
3858{
3859 if (codec->init_verbs.list)
3860 snd_hda_sequence_write(codec, codec->init_verbs.list);
3861}
3862#else
3863static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3864#endif
3865
Takashi Iwai2a439522011-07-26 09:52:50 +02003866#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003867/* update the power on/off account with the current jiffies */
3868static void update_power_acct(struct hda_codec *codec, bool on)
3869{
3870 unsigned long delta = jiffies - codec->power_jiffies;
3871
3872 if (on)
3873 codec->power_on_acct += delta;
3874 else
3875 codec->power_off_acct += delta;
3876 codec->power_jiffies += delta;
3877}
3878
3879void snd_hda_update_power_acct(struct hda_codec *codec)
3880{
3881 update_power_acct(codec, hda_codec_is_power_on(codec));
3882}
3883
Takashi Iwaicb53c622007-08-10 17:21:45 +02003884/*
3885 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003886 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003887 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003888static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003889{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003890 unsigned int state;
3891
Takashi Iwaicc72da72015-02-19 16:00:22 +01003892 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003893
Takashi Iwaicb53c622007-08-10 17:21:45 +02003894 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003895 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003896 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003897 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003898 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003899 update_power_acct(codec, true);
3900 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003901 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003902}
3903
Takashi Iwaic370dd62012-12-13 18:30:04 +01003904/* mark all entries of cmd and amp caches dirty */
3905static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3906{
3907 int i;
3908 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3909 struct hda_cache_head *cmd;
3910 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3911 cmd->dirty = 1;
3912 }
3913 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3914 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003915 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003916 amp->head.dirty = 1;
3917 }
3918}
3919
Takashi Iwaicb53c622007-08-10 17:21:45 +02003920/*
3921 * kick up codec; used both from PM and power-save
3922 */
3923static void hda_call_codec_resume(struct hda_codec *codec)
3924{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003925 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003926
Takashi Iwaicc72da72015-02-19 16:00:22 +01003927 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003928 hda_mark_cmd_cache_dirty(codec);
3929
Takashi Iwaicc72da72015-02-19 16:00:22 +01003930 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003931
Takashi Iwaid8193872012-08-31 07:54:38 -07003932 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003933 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003934 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003935 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003936 if (codec->patch_ops.resume)
3937 codec->patch_ops.resume(codec);
3938 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003939 if (codec->patch_ops.init)
3940 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003941 snd_hda_codec_resume_amp(codec);
3942 snd_hda_codec_resume_cache(codec);
3943 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003944
3945 if (codec->jackpoll_interval)
3946 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003947 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003948 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003949 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003950}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003951
Takashi Iwaicc72da72015-02-19 16:00:22 +01003952static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003953{
3954 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003955 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003956 int i;
3957
3958 cancel_delayed_work_sync(&codec->jackpoll_work);
3959 for (i = 0; i < codec->num_pcms; i++)
3960 snd_pcm_suspend_all(codec->pcm_info[i].pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003961 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003962 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
3963 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003964 return 0;
3965}
3966
Takashi Iwaicc72da72015-02-19 16:00:22 +01003967static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003968{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003969 struct hda_codec *codec = dev_to_hda_codec(dev);
3970
3971 set_bit(codec->addr, &codec->bus->codec_powered);
3972 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003973 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003974 return 0;
3975}
Takashi Iwai2a439522011-07-26 09:52:50 +02003976#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003977
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003978/* referred in hda_bind.c */
3979const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003980 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3981 pm_runtime_force_resume)
3982 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3983 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003984};
Takashi Iwai54d17402005-11-21 16:33:22 +01003985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986/**
3987 * snd_hda_build_controls - build mixer controls
3988 * @bus: the BUS
3989 *
3990 * Creates mixer controls for each codec included in the bus.
3991 *
3992 * Returns 0 if successful, otherwise a negative error code.
3993 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003994int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003996 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Takashi Iwai0ba21762007-04-16 11:29:14 +02003998 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003999 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004000 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004001 codec_err(codec,
4002 "cannot build controls for #%d (error %d)\n",
4003 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004004 err = snd_hda_codec_reset(codec);
4005 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004006 codec_err(codec,
4007 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004008 return err;
4009 }
4010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004012 return 0;
4013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004014EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004016/*
4017 * add standard channel maps if not specified
4018 */
4019static int add_std_chmaps(struct hda_codec *codec)
4020{
4021 int i, str, err;
4022
4023 for (i = 0; i < codec->num_pcms; i++) {
4024 for (str = 0; str < 2; str++) {
4025 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4026 struct hda_pcm_stream *hinfo =
4027 &codec->pcm_info[i].stream[str];
4028 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004029 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004030
4031 if (codec->pcm_info[i].own_chmap)
4032 continue;
4033 if (!pcm || !hinfo->substreams)
4034 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004035 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4036 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004037 hinfo->channels_max,
4038 0, &chmap);
4039 if (err < 0)
4040 return err;
4041 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4042 }
4043 }
4044 return 0;
4045}
4046
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004047/* default channel maps for 2.1 speakers;
4048 * since HD-audio supports only stereo, odd number channels are omitted
4049 */
4050const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4051 { .channels = 2,
4052 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4053 { .channels = 4,
4054 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4055 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4056 { }
4057};
4058EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4059
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004060int snd_hda_codec_build_controls(struct hda_codec *codec)
4061{
4062 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004063 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004064 /* continue to initialize... */
4065 if (codec->patch_ops.init)
4066 err = codec->patch_ops.init(codec);
4067 if (!err && codec->patch_ops.build_controls)
4068 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004069 if (err < 0)
4070 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004071
4072 /* we create chmaps here instead of build_pcms */
4073 err = add_std_chmaps(codec);
4074 if (err < 0)
4075 return err;
4076
David Henningsson26a6cb62012-10-09 15:04:21 +02004077 if (codec->jackpoll_interval)
4078 hda_jackpoll_work(&codec->jackpoll_work.work);
4079 else
4080 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004081 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return 0;
4083}
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085/*
4086 * stream formats
4087 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004088struct hda_rate_tbl {
4089 unsigned int hz;
4090 unsigned int alsa_bits;
4091 unsigned int hda_fmt;
4092};
4093
Takashi Iwai92f10b32010-08-03 14:21:00 +02004094/* rate = base * mult / div */
4095#define HDA_RATE(base, mult, div) \
4096 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4097 (((div) - 1) << AC_FMT_DIV_SHIFT))
4098
Takashi Iwaibefdf312005-08-22 13:57:55 +02004099static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004101
4102 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004103 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4104 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4105 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4106 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4107 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4108 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4109 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4110 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4111 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4112 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4113 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004114#define AC_PAR_PCM_RATE_BITS 11
4115 /* up to bits 10, 384kHZ isn't supported properly */
4116
4117 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004118 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004119
Takashi Iwaibefdf312005-08-22 13:57:55 +02004120 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121};
4122
4123/**
4124 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004125 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 * @rate: the sample rate
4127 * @channels: the number of channels
4128 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4129 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004130 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 *
4132 * Calculate the format bitset from the given rate, channels and th PCM format.
4133 *
4134 * Return zero if invalid.
4135 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004136unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4137 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 unsigned int channels,
4139 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004140 unsigned int maxbps,
4141 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 int i;
4144 unsigned int val = 0;
4145
Takashi Iwaibefdf312005-08-22 13:57:55 +02004146 for (i = 0; rate_bits[i].hz; i++)
4147 if (rate_bits[i].hz == rate) {
4148 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 break;
4150 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004151 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004152 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 return 0;
4154 }
4155
4156 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004157 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 }
4160 val |= channels - 1;
4161
4162 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004163 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004164 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004165 break;
4166 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004167 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 case 20:
4170 case 24:
4171 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004172 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004173 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004175 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004177 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004180 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004181 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return 0;
4183 }
4184
Anssi Hannula32c168c2010-08-03 13:28:57 +03004185 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004186 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return val;
4189}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004190EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004192static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4193 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004194{
4195 unsigned int val = 0;
4196 if (nid != codec->afg &&
4197 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4198 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4199 if (!val || val == -1)
4200 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4201 if (!val || val == -1)
4202 return 0;
4203 return val;
4204}
4205
4206static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4207{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004208 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004209 get_pcm_param);
4210}
4211
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004212static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4213 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004214{
4215 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4216 if (!streams || streams == -1)
4217 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4218 if (!streams || streams == -1)
4219 return 0;
4220 return streams;
4221}
4222
4223static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4224{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004225 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004226 get_stream_param);
4227}
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229/**
4230 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4231 * @codec: the HDA codec
4232 * @nid: NID to query
4233 * @ratesp: the pointer to store the detected rate bitflags
4234 * @formatsp: the pointer to store the detected formats
4235 * @bpsp: the pointer to store the detected format widths
4236 *
4237 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4238 * or @bsps argument is ignored.
4239 *
4240 * Returns 0 if successful, otherwise a negative error code.
4241 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004242int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4244{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004245 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004247 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004248 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 if (ratesp) {
4251 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004252 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004254 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004256 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004257 codec_err(codec,
4258 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4259 nid, val,
4260 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004261 return -EIO;
4262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 *ratesp = rates;
4264 }
4265
4266 if (formatsp || bpsp) {
4267 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004268 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004270 streams = query_stream_param(codec, nid);
4271 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 bps = 0;
4275 if (streams & AC_SUPFMT_PCM) {
4276 if (val & AC_SUPPCM_BITS_8) {
4277 formats |= SNDRV_PCM_FMTBIT_U8;
4278 bps = 8;
4279 }
4280 if (val & AC_SUPPCM_BITS_16) {
4281 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4282 bps = 16;
4283 }
4284 if (wcaps & AC_WCAP_DIGITAL) {
4285 if (val & AC_SUPPCM_BITS_32)
4286 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4287 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4288 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4289 if (val & AC_SUPPCM_BITS_24)
4290 bps = 24;
4291 else if (val & AC_SUPPCM_BITS_20)
4292 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004293 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4294 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4296 if (val & AC_SUPPCM_BITS_32)
4297 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 else if (val & AC_SUPPCM_BITS_24)
4299 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004300 else if (val & AC_SUPPCM_BITS_20)
4301 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
4303 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004304#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004305 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004307 if (!bps)
4308 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004309 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004310#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004311 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004312 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 /* temporary hack: we have still no proper support
4314 * for the direct AC3 stream...
4315 */
4316 formats |= SNDRV_PCM_FMTBIT_U8;
4317 bps = 8;
4318 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004319 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004320 codec_err(codec,
4321 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4322 nid, val,
4323 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4324 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004325 return -EIO;
4326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (formatsp)
4328 *formatsp = formats;
4329 if (bpsp)
4330 *bpsp = bps;
4331 }
4332
4333 return 0;
4334}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004335EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004338 * snd_hda_is_supported_format - Check the validity of the format
4339 * @codec: HD-audio codec
4340 * @nid: NID to check
4341 * @format: the HD-audio format value to check
4342 *
4343 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 *
4345 * Returns 1 if supported, 0 if not.
4346 */
4347int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4348 unsigned int format)
4349{
4350 int i;
4351 unsigned int val = 0, rate, stream;
4352
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004353 val = query_pcm_param(codec, nid);
4354 if (!val)
4355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
4357 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004358 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004359 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (val & (1 << i))
4361 break;
4362 return 0;
4363 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004364 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004367 stream = query_stream_param(codec, nid);
4368 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return 0;
4370
4371 if (stream & AC_SUPFMT_PCM) {
4372 switch (format & 0xf0) {
4373 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004374 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return 0;
4376 break;
4377 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004378 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return 0;
4380 break;
4381 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004382 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return 0;
4384 break;
4385 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004386 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388 break;
4389 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004390 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392 break;
4393 default:
4394 return 0;
4395 }
4396 } else {
4397 /* FIXME: check for float32 and AC3? */
4398 }
4399
4400 return 1;
4401}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004402EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404/*
4405 * PCM stuff
4406 */
4407static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4408 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004409 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411 return 0;
4412}
4413
4414static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
4416 unsigned int stream_tag,
4417 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004418 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
4420 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4421 return 0;
4422}
4423
4424static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4425 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004426 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Takashi Iwai888afa12008-03-18 09:57:50 +01004428 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return 0;
4430}
4431
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004432static int set_pcm_default_values(struct hda_codec *codec,
4433 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004435 int err;
4436
Takashi Iwai0ba21762007-04-16 11:29:14 +02004437 /* query support PCM information from the given NID */
4438 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004439 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004440 info->rates ? NULL : &info->rates,
4441 info->formats ? NULL : &info->formats,
4442 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004443 if (err < 0)
4444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446 if (info->ops.open == NULL)
4447 info->ops.open = hda_pcm_default_open_close;
4448 if (info->ops.close == NULL)
4449 info->ops.close = hda_pcm_default_open_close;
4450 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004451 if (snd_BUG_ON(!info->nid))
4452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 info->ops.prepare = hda_pcm_default_prepare;
4454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004456 if (snd_BUG_ON(!info->nid))
4457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 info->ops.cleanup = hda_pcm_default_cleanup;
4459 }
4460 return 0;
4461}
4462
Takashi Iwaieb541332010-08-06 13:48:11 +02004463/*
4464 * codec prepare/cleanup entries
4465 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004466/**
4467 * snd_hda_codec_prepare - Prepare a stream
4468 * @codec: the HDA codec
4469 * @hinfo: PCM information
4470 * @stream: stream tag to assign
4471 * @format: format id to assign
4472 * @substream: PCM substream to assign
4473 *
4474 * Calls the prepare callback set by the codec with the given arguments.
4475 * Clean up the inactive streams when successful.
4476 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004477int snd_hda_codec_prepare(struct hda_codec *codec,
4478 struct hda_pcm_stream *hinfo,
4479 unsigned int stream,
4480 unsigned int format,
4481 struct snd_pcm_substream *substream)
4482{
4483 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004484 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004485 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4486 if (ret >= 0)
4487 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004488 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004489 return ret;
4490}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004491EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004492
Takashi Iwai95a962c2014-10-29 16:03:58 +01004493/**
4494 * snd_hda_codec_cleanup - Prepare a stream
4495 * @codec: the HDA codec
4496 * @hinfo: PCM information
4497 * @substream: PCM substream
4498 *
4499 * Calls the cleanup callback set by the codec with the given arguments.
4500 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004501void snd_hda_codec_cleanup(struct hda_codec *codec,
4502 struct hda_pcm_stream *hinfo,
4503 struct snd_pcm_substream *substream)
4504{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004505 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004506 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004507 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004508}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004509EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004510
Takashi Iwaid5191e52009-11-16 14:58:17 +01004511/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004512const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4513 "Audio", "SPDIF", "HDMI", "Modem"
4514};
4515
Takashi Iwai176d5332008-07-30 15:01:44 +02004516/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004517 * get the empty PCM device number to assign
4518 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004519static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004520{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004521 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004522 /* assigned to static slots up to dev#10; if more needed, assign
4523 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4524 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004525 static int audio_idx[HDA_PCM_NTYPES][5] = {
4526 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4527 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004528 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004529 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004530 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004531 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004532
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004533 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004534 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004535 return -EINVAL;
4536 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004537
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004538 for (i = 0; audio_idx[type][i] >= 0; i++) {
4539#ifndef CONFIG_SND_DYNAMIC_MINORS
4540 if (audio_idx[type][i] >= 8)
4541 break;
4542#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004543 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4544 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004545 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004546
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004547#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004548 /* non-fixed slots starting from 10 */
4549 for (i = 10; i < 32; i++) {
4550 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4551 return i;
4552 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004553#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004554
Takashi Iwai4e76a882014-02-25 12:21:03 +01004555 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004556 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004557#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004558 dev_warn(bus->card->dev,
4559 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004560#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004561 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004562}
4563
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004564/* call build_pcms ops of the given codec and set up the default parameters */
4565int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004566{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004567 unsigned int pcm;
4568 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004569
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004570 if (codec->num_pcms)
4571 return 0; /* already parsed */
4572
4573 if (!codec->patch_ops.build_pcms)
4574 return 0;
4575
4576 err = codec->patch_ops.build_pcms(codec);
4577 if (err < 0) {
4578 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4579 codec->addr, err);
4580 return err;
4581 }
4582
4583 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4584 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4585 int stream;
4586
4587 for (stream = 0; stream < 2; stream++) {
4588 struct hda_pcm_stream *info = &cpcm->stream[stream];
4589
4590 if (!info->substreams)
4591 continue;
4592 if (snd_BUG_ON(!cpcm->name))
4593 return -EINVAL;
Takashi Iwai176d5332008-07-30 15:01:44 +02004594 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004595 if (err < 0) {
4596 codec_warn(codec,
4597 "fail to setup default for PCM %s\n",
4598 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004599 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004600 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004601 }
4602 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004603
4604 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004605}
4606
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004607/* assign all PCMs of the given codec */
4608int snd_hda_codec_build_pcms(struct hda_codec *codec)
4609{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004610 struct hda_bus *bus = codec->bus;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004611 unsigned int pcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004612 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004613
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004614 if (snd_BUG_ON(!bus->ops.attach_pcm))
4615 return -EINVAL;
4616
4617 err = snd_hda_codec_parse_pcms(codec);
4618 if (err < 0) {
4619 snd_hda_codec_reset(codec);
4620 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004621 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004622
4623 /* attach a new PCM streams */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004624 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4625 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004626
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004627 if (cpcm->pcm)
4628 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004629 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004630 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004631
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004632 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4633 if (dev < 0)
4634 continue; /* no fatal error */
4635 cpcm->device = dev;
4636 err = bus->ops.attach_pcm(bus, codec, cpcm);
4637 if (err < 0) {
4638 codec_err(codec,
4639 "cannot attach PCM stream %d for codec #%d\n",
4640 dev, codec->addr);
4641 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004642 }
4643 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004644
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004645 return 0;
4646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/**
4649 * snd_hda_build_pcms - build PCM information
4650 * @bus: the BUS
4651 *
4652 * Create PCM information for each codec included in the bus.
4653 *
4654 * The build_pcms codec patch is requested to set up codec->num_pcms and
4655 * codec->pcm_info properly. The array is referred by the top-level driver
4656 * to create its PCM instances.
4657 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4658 * callback.
4659 *
4660 * At least, substreams, channels_min and channels_max must be filled for
4661 * each stream. substreams = 0 indicates that the stream doesn't exist.
4662 * When rates and/or formats are zero, the supported values are queried
4663 * from the given nid. The nid is used also by the default ops.prepare
4664 * and ops.cleanup callbacks.
4665 *
4666 * The driver needs to call ops.open in its open callback. Similarly,
4667 * ops.close is supposed to be called in the close callback.
4668 * ops.prepare should be called in the prepare or hw_params callback
4669 * with the proper parameters for set up.
4670 * ops.cleanup should be called in hw_free for clean up of streams.
4671 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004672 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004674int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004676 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Takashi Iwai0ba21762007-04-16 11:29:14 +02004678 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004679 int err = snd_hda_codec_build_pcms(codec);
4680 if (err < 0)
4681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683 return 0;
4684}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004685EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 * snd_hda_add_new_ctls - create controls from the array
4689 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004690 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 *
4692 * This helper function creates and add new controls in the given array.
4693 * The array must be terminated with an empty entry as terminator.
4694 *
4695 * Returns 0 if successful, or a negative error code.
4696 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004697int snd_hda_add_new_ctls(struct hda_codec *codec,
4698 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004700 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004703 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004704 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004705 if (knew->iface == -1) /* skip this codec private value */
4706 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004707 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004708 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004709 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004710 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004711 if (addr > 0)
4712 kctl->id.device = addr;
4713 if (idx > 0)
4714 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004715 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004716 if (!err)
4717 break;
4718 /* try first with another device index corresponding to
4719 * the codec addr; if it still fails (or it's the
4720 * primary codec), then try another control index
4721 */
4722 if (!addr && codec->addr)
4723 addr = codec->addr;
4724 else if (!idx && !knew->index) {
4725 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004726 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004727 if (idx <= 0)
4728 return err;
4729 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004730 return err;
4731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
4733 return 0;
4734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004735EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Takashi Iwai83012a72012-08-24 18:38:08 +02004737#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004738/**
4739 * snd_hda_power_up - Power-up the codec
4740 * @codec: HD-audio codec
4741 *
4742 * Increment the usage counter and resume the device if not done yet.
4743 */
4744void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004745{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004746 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004747
Takashi Iwaicc72da72015-02-19 16:00:22 +01004748 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004749 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004750 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004751}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004752EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004753
4754/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004755 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004756 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004757 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004758 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004759 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004760void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004761{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004762 struct device *dev = hda_codec_dev(codec);
4763
4764 if (codec_in_pm(codec))
4765 return;
4766 pm_runtime_mark_last_busy(dev);
4767 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004768}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004769EXPORT_SYMBOL_GPL(snd_hda_power_down);
4770
Takashi Iwaibb573922015-02-20 09:26:04 +01004771static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004772{
4773 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004774
Takashi Iwaicc72da72015-02-19 16:00:22 +01004775 if (delay > 0) {
4776 pm_runtime_set_autosuspend_delay(dev, delay);
4777 pm_runtime_use_autosuspend(dev);
4778 pm_runtime_allow(dev);
4779 if (!pm_runtime_suspended(dev))
4780 pm_runtime_mark_last_busy(dev);
4781 } else {
4782 pm_runtime_dont_use_autosuspend(dev);
4783 pm_runtime_forbid(dev);
4784 }
4785}
Takashi Iwaibb573922015-02-20 09:26:04 +01004786
4787/**
4788 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4789 * @bus: HD-audio bus
4790 * @delay: autosuspend delay in msec, 0 = off
4791 *
4792 * Synchronize the runtime PM autosuspend state from the power_save option.
4793 */
4794void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4795{
4796 struct hda_codec *c;
4797
4798 list_for_each_entry(c, &bus->codec_list, list)
4799 codec_set_power_save(c, delay);
4800}
4801EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004802
Takashi Iwaid5191e52009-11-16 14:58:17 +01004803/**
4804 * snd_hda_check_amp_list_power - Check the amp list and update the power
4805 * @codec: HD-audio codec
4806 * @check: the object containing an AMP list and the status
4807 * @nid: NID to check / update
4808 *
4809 * Check whether the given NID is in the amp list. If it's in the list,
4810 * check the current AMP status, and update the the power-status according
4811 * to the mute status.
4812 *
4813 * This function is supposed to be set or called from the check_power_status
4814 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004815 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004816int snd_hda_check_amp_list_power(struct hda_codec *codec,
4817 struct hda_loopback_check *check,
4818 hda_nid_t nid)
4819{
Takashi Iwai031024e2011-05-02 11:29:30 +02004820 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004821 int ch, v;
4822
4823 if (!check->amplist)
4824 return 0;
4825 for (p = check->amplist; p->nid; p++) {
4826 if (p->nid == nid)
4827 break;
4828 }
4829 if (!p->nid)
4830 return 0; /* nothing changed */
4831
4832 for (p = check->amplist; p->nid; p++) {
4833 for (ch = 0; ch < 2; ch++) {
4834 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4835 p->idx);
4836 if (!(v & HDA_AMP_MUTE) && v > 0) {
4837 if (!check->power_on) {
4838 check->power_on = 1;
4839 snd_hda_power_up(codec);
4840 }
4841 return 1;
4842 }
4843 }
4844 }
4845 if (check->power_on) {
4846 check->power_on = 0;
4847 snd_hda_power_down(codec);
4848 }
4849 return 0;
4850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004851EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004852#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004854/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 * input MUX helper
4856 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004857
4858/**
4859 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004860 * @imux: imux helper object
4861 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004862 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004863int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4864 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
4866 unsigned int index;
4867
4868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4869 uinfo->count = 1;
4870 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004871 if (!imux->num_items)
4872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 index = uinfo->value.enumerated.item;
4874 if (index >= imux->num_items)
4875 index = imux->num_items - 1;
4876 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4877 return 0;
4878}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004879EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Takashi Iwaid5191e52009-11-16 14:58:17 +01004881/**
4882 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004883 * @codec: the HDA codec
4884 * @imux: imux helper object
4885 * @ucontrol: pointer to get/store the data
4886 * @nid: input mux NID
4887 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004888 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004889int snd_hda_input_mux_put(struct hda_codec *codec,
4890 const struct hda_input_mux *imux,
4891 struct snd_ctl_elem_value *ucontrol,
4892 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 unsigned int *cur_val)
4894{
4895 unsigned int idx;
4896
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004897 if (!imux->num_items)
4898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 idx = ucontrol->value.enumerated.item[0];
4900 if (idx >= imux->num_items)
4901 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004902 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004904 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4905 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 *cur_val = idx;
4907 return 1;
4908}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004909EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
4911
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004912/**
4913 * snd_hda_enum_helper_info - Helper for simple enum ctls
4914 * @kcontrol: ctl element
4915 * @uinfo: pointer to get/store the data
4916 * @num_items: number of enum items
4917 * @texts: enum item string array
4918 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004919 * process kcontrol info callback of a simple string enum array
4920 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4921 */
4922int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4923 struct snd_ctl_elem_info *uinfo,
4924 int num_items, const char * const *texts)
4925{
4926 static const char * const texts_default[] = {
4927 "Disabled", "Enabled"
4928 };
4929
4930 if (!texts || !num_items) {
4931 num_items = 2;
4932 texts = texts_default;
4933 }
4934
Takashi Iwai3ff72212014-10-20 18:17:28 +02004935 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004936}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004937EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004938
4939/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 * Multi-channel / digital-out PCM helper functions
4941 */
4942
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004943/* setup SPDIF output stream */
4944static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4945 unsigned int stream_tag, unsigned int format)
4946{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004947 struct hda_spdif_out *spdif;
4948 unsigned int curr_fmt;
4949 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004950
Laurence Darby3bef1c32012-11-03 17:00:06 +00004951 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4952 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4953 AC_VERB_GET_STREAM_FORMAT, 0);
4954 reset = codec->spdif_status_reset &&
4955 (spdif->ctls & AC_DIG1_ENABLE) &&
4956 curr_fmt != format;
4957
4958 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4959 updated */
4960 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004961 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004962 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004963 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004964 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004965 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004966 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004967 for (d = codec->slave_dig_outs; *d; d++)
4968 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4969 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004970 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004971 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004972 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004973 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004974 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004975}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004976
Takashi Iwai2f728532008-09-25 16:32:41 +02004977static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4978{
4979 snd_hda_codec_cleanup_stream(codec, nid);
4980 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004981 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004982 for (d = codec->slave_dig_outs; *d; d++)
4983 snd_hda_codec_cleanup_stream(codec, *d);
4984 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004985}
4986
Takashi Iwaid5191e52009-11-16 14:58:17 +01004987/**
4988 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4989 * @bus: HD-audio bus
4990 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004991void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4992{
4993 struct hda_codec *codec;
4994
4995 if (!bus)
4996 return;
4997 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004998 if (hda_codec_is_power_on(codec) &&
4999 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005000 codec->patch_ops.reboot_notify(codec);
5001 }
5002}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005003EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005004
Takashi Iwaid5191e52009-11-16 14:58:17 +01005005/**
5006 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005007 * @codec: the HDA codec
5008 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005010int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5011 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Ingo Molnar62932df2006-01-16 16:34:20 +01005013 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005014 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5015 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005016 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005018 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return 0;
5020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005021EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Takashi Iwaid5191e52009-11-16 14:58:17 +01005023/**
5024 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005025 * @codec: the HDA codec
5026 * @mout: hda_multi_out object
5027 * @stream_tag: stream tag to assign
5028 * @format: format id to assign
5029 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005030 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005031int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5032 struct hda_multi_out *mout,
5033 unsigned int stream_tag,
5034 unsigned int format,
5035 struct snd_pcm_substream *substream)
5036{
5037 mutex_lock(&codec->spdif_mutex);
5038 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5039 mutex_unlock(&codec->spdif_mutex);
5040 return 0;
5041}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005042EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005043
Takashi Iwaid5191e52009-11-16 14:58:17 +01005044/**
5045 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005046 * @codec: the HDA codec
5047 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005048 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005049int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5050 struct hda_multi_out *mout)
5051{
5052 mutex_lock(&codec->spdif_mutex);
5053 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5054 mutex_unlock(&codec->spdif_mutex);
5055 return 0;
5056}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005057EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005058
Takashi Iwaid5191e52009-11-16 14:58:17 +01005059/**
5060 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005061 * @codec: the HDA codec
5062 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005064int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5065 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Ingo Molnar62932df2006-01-16 16:34:20 +01005067 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005069 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 return 0;
5071}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005072EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Takashi Iwaid5191e52009-11-16 14:58:17 +01005074/**
5075 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005076 * @codec: the HDA codec
5077 * @mout: hda_multi_out object
5078 * @substream: PCM substream to assign
5079 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005080 *
5081 * Open analog outputs and set up the hw-constraints.
5082 * If the digital outputs can be opened as slave, open the digital
5083 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005085int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5086 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005087 struct snd_pcm_substream *substream,
5088 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
Takashi Iwai9a081602008-02-12 18:37:26 +01005090 struct snd_pcm_runtime *runtime = substream->runtime;
5091 runtime->hw.channels_max = mout->max_channels;
5092 if (mout->dig_out_nid) {
5093 if (!mout->analog_rates) {
5094 mout->analog_rates = hinfo->rates;
5095 mout->analog_formats = hinfo->formats;
5096 mout->analog_maxbps = hinfo->maxbps;
5097 } else {
5098 runtime->hw.rates = mout->analog_rates;
5099 runtime->hw.formats = mout->analog_formats;
5100 hinfo->maxbps = mout->analog_maxbps;
5101 }
5102 if (!mout->spdif_rates) {
5103 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5104 &mout->spdif_rates,
5105 &mout->spdif_formats,
5106 &mout->spdif_maxbps);
5107 }
5108 mutex_lock(&codec->spdif_mutex);
5109 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005110 if ((runtime->hw.rates & mout->spdif_rates) &&
5111 (runtime->hw.formats & mout->spdif_formats)) {
5112 runtime->hw.rates &= mout->spdif_rates;
5113 runtime->hw.formats &= mout->spdif_formats;
5114 if (mout->spdif_maxbps < hinfo->maxbps)
5115 hinfo->maxbps = mout->spdif_maxbps;
5116 } else {
5117 mout->share_spdif = 0;
5118 /* FIXME: need notify? */
5119 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005120 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005121 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5124 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5125}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005126EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Takashi Iwaid5191e52009-11-16 14:58:17 +01005128/**
5129 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005130 * @codec: the HDA codec
5131 * @mout: hda_multi_out object
5132 * @stream_tag: stream tag to assign
5133 * @format: format id to assign
5134 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005135 *
5136 * Set up the i/o for analog out.
5137 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005139int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5140 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 unsigned int stream_tag,
5142 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005143 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
Takashi Iwaidda14412011-05-02 11:29:30 +02005145 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005147 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 int i;
5149
Ingo Molnar62932df2006-01-16 16:34:20 +01005150 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005151 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005152 if (mout->dig_out_nid && mout->share_spdif &&
5153 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005155 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5156 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005157 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005159 setup_dig_out_stream(codec, mout->dig_out_nid,
5160 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 } else {
5162 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005163 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 }
5165 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005166 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005169 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5170 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005171 if (!mout->no_share_stream &&
5172 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005174 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5175 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005176 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005177 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5178 if (!mout->no_share_stream && mout->hp_out_nid[i])
5179 snd_hda_codec_setup_stream(codec,
5180 mout->hp_out_nid[i],
5181 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /* surrounds */
5184 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005185 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005186 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5187 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005188 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005189 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5190 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005192
5193 /* extra surrounds */
5194 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5195 int ch = 0;
5196 if (!mout->extra_out_nid[i])
5197 break;
5198 if (chs >= (i + 1) * 2)
5199 ch = i * 2;
5200 else if (!mout->no_share_stream)
5201 break;
5202 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5203 stream_tag, ch, format);
5204 }
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 return 0;
5207}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005208EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Takashi Iwaid5191e52009-11-16 14:58:17 +01005210/**
5211 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005212 * @codec: the HDA codec
5213 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005215int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5216 struct hda_multi_out *mout)
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 i;
5220
5221 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005222 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005224 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005225 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5226 if (mout->hp_out_nid[i])
5227 snd_hda_codec_cleanup_stream(codec,
5228 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005229 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5230 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005231 snd_hda_codec_cleanup_stream(codec,
5232 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005233 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005235 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 mout->dig_out_used = 0;
5237 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005238 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 return 0;
5240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005241EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Takashi Iwai47408602012-04-20 13:06:53 +02005243/**
5244 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005245 * @codec: the HDA codec
5246 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005247 *
5248 * Guess the suitable VREF pin bits to be set as the pin-control value.
5249 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5250 */
5251unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5252{
5253 unsigned int pincap;
5254 unsigned int oldval;
5255 oldval = snd_hda_codec_read(codec, pin, 0,
5256 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5257 pincap = snd_hda_query_pin_caps(codec, pin);
5258 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5259 /* Exception: if the default pin setup is vref50, we give it priority */
5260 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5261 return AC_PINCTL_VREF_80;
5262 else if (pincap & AC_PINCAP_VREF_50)
5263 return AC_PINCTL_VREF_50;
5264 else if (pincap & AC_PINCAP_VREF_100)
5265 return AC_PINCTL_VREF_100;
5266 else if (pincap & AC_PINCAP_VREF_GRD)
5267 return AC_PINCTL_VREF_GRD;
5268 return AC_PINCTL_VREF_HIZ;
5269}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005270EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005271
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005272/**
5273 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5274 * @codec: the HDA codec
5275 * @pin: referred pin NID
5276 * @val: pin ctl value to audit
5277 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005278unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5279 hda_nid_t pin, unsigned int val)
5280{
5281 static unsigned int cap_lists[][2] = {
5282 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5283 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5284 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5285 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5286 };
5287 unsigned int cap;
5288
5289 if (!val)
5290 return 0;
5291 cap = snd_hda_query_pin_caps(codec, pin);
5292 if (!cap)
5293 return val; /* don't know what to do... */
5294
5295 if (val & AC_PINCTL_OUT_EN) {
5296 if (!(cap & AC_PINCAP_OUT))
5297 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5298 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5299 val &= ~AC_PINCTL_HP_EN;
5300 }
5301
5302 if (val & AC_PINCTL_IN_EN) {
5303 if (!(cap & AC_PINCAP_IN))
5304 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5305 else {
5306 unsigned int vcap, vref;
5307 int i;
5308 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5309 vref = val & AC_PINCTL_VREFEN;
5310 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5311 if (vref == cap_lists[i][0] &&
5312 !(vcap & cap_lists[i][1])) {
5313 if (i == ARRAY_SIZE(cap_lists) - 1)
5314 vref = AC_PINCTL_VREF_HIZ;
5315 else
5316 vref = cap_lists[i + 1][0];
5317 }
5318 }
5319 val &= ~AC_PINCTL_VREFEN;
5320 val |= vref;
5321 }
5322 }
5323
5324 return val;
5325}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005326EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005327
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005328/**
5329 * _snd_hda_pin_ctl - Helper to set pin ctl value
5330 * @codec: the HDA codec
5331 * @pin: referred pin NID
5332 * @val: pin control value to set
5333 * @cached: access over codec pinctl cache or direct write
5334 *
5335 * This function is a helper to set a pin ctl value more safely.
5336 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5337 * value in pin target array via snd_hda_codec_set_pin_target(), then
5338 * actually writes the value via either snd_hda_codec_update_cache() or
5339 * snd_hda_codec_write() depending on @cached flag.
5340 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005341int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5342 unsigned int val, bool cached)
5343{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005344 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005345 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005346 if (cached)
5347 return snd_hda_codec_update_cache(codec, pin, 0,
5348 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5349 else
5350 return snd_hda_codec_write(codec, pin, 0,
5351 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5352}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005353EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005354
Takashi Iwai990061c2010-09-09 22:08:44 +02005355/**
5356 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005357 * @codec: the HDA codec
5358 * @imux: imux helper object
5359 * @label: the name of imux item to assign
5360 * @index: index number of imux item to assign
5361 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005362 *
5363 * When the same label is used already in the existing items, the number
5364 * suffix is appended to the label. This label index number is stored
5365 * to type_idx when non-NULL pointer is given.
5366 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005367int snd_hda_add_imux_item(struct hda_codec *codec,
5368 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005369 int index, int *type_idx)
5370{
5371 int i, label_idx = 0;
5372 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005373 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005374 return -EINVAL;
5375 }
5376 for (i = 0; i < imux->num_items; i++) {
5377 if (!strncmp(label, imux->items[i].label, strlen(label)))
5378 label_idx++;
5379 }
5380 if (type_idx)
5381 *type_idx = label_idx;
5382 if (label_idx > 0)
5383 snprintf(imux->items[imux->num_items].label,
5384 sizeof(imux->items[imux->num_items].label),
5385 "%s %d", label, label_idx);
5386 else
5387 strlcpy(imux->items[imux->num_items].label, label,
5388 sizeof(imux->items[imux->num_items].label));
5389 imux->items[imux->num_items].index = index;
5390 imux->num_items++;
5391 return 0;
5392}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005393EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005396 * snd_hda_bus_reset - Reset the bus
5397 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005399void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005401 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Takashi Iwai0ba21762007-04-16 11:29:14 +02005403 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005404 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005405 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005406#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005407 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005408 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005409 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005410 }
5411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005414EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005415
5416/*
5417 * generic arrays
5418 */
5419
Takashi Iwaid5191e52009-11-16 14:58:17 +01005420/**
5421 * snd_array_new - get a new element from the given array
5422 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005423 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005424 * Get a new element from the given array. If it exceeds the
5425 * pre-allocated array size, re-allocate the array.
5426 *
5427 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005428 */
5429void *snd_array_new(struct snd_array *array)
5430{
Takashi Iwai12f17712012-10-10 08:59:14 +02005431 if (snd_BUG_ON(!array->elem_size))
5432 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005433 if (array->used >= array->alloced) {
5434 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005435 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005436 void *nlist;
5437 if (snd_BUG_ON(num >= 4096))
5438 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005439 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005440 if (!nlist)
5441 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005442 array->list = nlist;
5443 array->alloced = num;
5444 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005445 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005446}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005447EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005448
Takashi Iwaid5191e52009-11-16 14:58:17 +01005449/**
5450 * snd_array_free - free the given array elements
5451 * @array: the array object
5452 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005453void snd_array_free(struct snd_array *array)
5454{
5455 kfree(array->list);
5456 array->used = 0;
5457 array->alloced = 0;
5458 array->list = NULL;
5459}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005460EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005461
Takashi Iwaid5191e52009-11-16 14:58:17 +01005462/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005463 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5464 * @pcm: PCM caps bits
5465 * @buf: the string buffer to write
5466 * @buflen: the max buffer length
5467 *
5468 * used by hda_proc.c and hda_eld.c
5469 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005470void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5471{
5472 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5473 int i, j;
5474
5475 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5476 if (pcm & (AC_SUPPCM_BITS_8 << i))
5477 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5478
5479 buf[j] = '\0'; /* necessary when j == 0 */
5480}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005481EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005482
5483MODULE_DESCRIPTION("HDA codec core");
5484MODULE_LICENSE("GPL");