blob: 0533c862b539461256014fc0eea8dc789250ff18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwaid66fee52011-08-02 15:39:31 +020042#define CREATE_TRACE_POINTS
43#include "hda_trace.h"
44
Takashi Iwai83012a72012-08-24 18:38:08 +020045#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +010046#define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
47#define hda_codec_is_power_on(codec) \
48 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#else
Takashi Iwaid846b172012-11-24 11:58:24 +010050#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020051#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020052#endif
53
Takashi Iwaid5191e52009-11-16 14:58:17 +010054/**
55 * snd_hda_get_jack_location - Give a location string of the jack
56 * @cfg: pin default config value
57 *
58 * Parse the pin default config value and returns the string of the
59 * jack location, e.g. "Rear", "Front", etc.
60 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040061const char *snd_hda_get_jack_location(u32 cfg)
62{
63 static char *bases[7] = {
64 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
65 };
66 static unsigned char specials_idx[] = {
67 0x07, 0x08,
68 0x17, 0x18, 0x19,
69 0x37, 0x38
70 };
71 static char *specials[] = {
72 "Rear Panel", "Drive Bar",
73 "Riser", "HDMI", "ATAPI",
74 "Mobile-In", "Mobile-Out"
75 };
76 int i;
77 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
78 if ((cfg & 0x0f) < 7)
79 return bases[cfg & 0x0f];
80 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
81 if (cfg == specials_idx[i])
82 return specials[i];
83 }
84 return "UNKNOWN";
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -040087
Takashi Iwaid5191e52009-11-16 14:58:17 +010088/**
89 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
90 * @cfg: pin default config value
91 *
92 * Parse the pin default config value and returns the string of the
93 * jack connectivity, i.e. external or internal connection.
94 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040095const char *snd_hda_get_jack_connectivity(u32 cfg)
96{
97 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
98
99 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
100}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100101EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400102
Takashi Iwaid5191e52009-11-16 14:58:17 +0100103/**
104 * snd_hda_get_jack_type - Give a type string of the jack
105 * @cfg: pin default config value
106 *
107 * Parse the pin default config value and returns the string of the
108 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
109 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400110const char *snd_hda_get_jack_type(u32 cfg)
111{
112 static char *jack_types[16] = {
113 "Line Out", "Speaker", "HP Out", "CD",
114 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
115 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200116 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400117 };
118
119 return jack_types[(cfg & AC_DEFCFG_DEVICE)
120 >> AC_DEFCFG_DEVICE_SHIFT];
121}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100122EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400123
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100124/*
125 * Compose a 32bit command word to be sent to the HD-audio controller
126 */
127static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200128make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100129 unsigned int verb, unsigned int parm)
130{
131 u32 val;
132
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200133 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200134 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100135 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200136 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800137 return ~0;
138 }
139
140 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100141 val |= (u32)nid << 20;
142 val |= verb << 8;
143 val |= parm;
144 return val;
145}
146
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200147/*
148 * Send and receive a verb
149 */
150static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200151 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200152{
153 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200154 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200155
Wu Fengguang6430aee2009-07-17 16:49:19 +0800156 if (cmd == ~0)
157 return -1;
158
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200159 if (res)
160 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200161 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200162 snd_hda_power_up(codec);
163 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200164 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
165 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100166 for (;;) {
167 trace_hda_send_cmd(codec, cmd);
168 err = bus->ops.command(bus, cmd);
169 if (err != -EAGAIN)
170 break;
171 /* process pending verbs */
172 bus->ops.get_response(bus, codec->addr);
173 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200174 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800175 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200176 trace_hda_get_response(codec, *res);
177 }
Takashi Iwai63e51fd72013-06-06 14:20:19 +0200178 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200179 mutex_unlock(&bus->cmd_mutex);
180 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100181 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200182 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100183 codec_dbg(codec,
184 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200185 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200186 bus->ops.bus_reset(bus);
187 }
188 goto again;
189 }
190 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100191 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200192 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200193 return err;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
197 * snd_hda_codec_read - send a command and get the response
198 * @codec: the HDA codec
199 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200200 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * @verb: the verb to send
202 * @parm: the parameter for the verb
203 *
204 * Send a single command and read the corresponding response.
205 *
206 * Returns the obtained response value, or -1 for an error.
207 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200208unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200209 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int verb, unsigned int parm)
211{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200212 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200213 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200214 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700215 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return res;
217}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100218EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/**
221 * snd_hda_codec_write - send a single command without waiting for response
222 * @codec: the HDA codec
223 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200224 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * @verb: the verb to send
226 * @parm: the parameter for the verb
227 *
228 * Send a single command without waiting for response.
229 *
230 * Returns 0 if successful, or a negative error code.
231 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200232int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
233 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200235 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100236 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200237 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200238 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100240EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/**
243 * snd_hda_sequence_write - sequence writes
244 * @codec: the HDA codec
245 * @seq: VERB array to send
246 *
247 * Send the commands sequentially from the given array.
248 * The array must be terminated with NID=0.
249 */
250void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
251{
252 for (; seq->nid; seq++)
253 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
254}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100255EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/**
258 * snd_hda_get_sub_nodes - get the range of sub nodes
259 * @codec: the HDA codec
260 * @nid: NID to parse
261 * @start_id: the pointer to store the start NID
262 *
263 * Parse the NID and store the start NID of its sub-nodes.
264 * Returns the number of sub-nodes.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
267 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 unsigned int parm;
270
271 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300272 if (parm == -1) {
273 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200274 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *start_id = (parm >> 16) & 0x7fff;
277 return (int)(parm & 0x7fff);
278}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100279EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100281/* connection list element */
282struct hda_conn_list {
283 struct list_head list;
284 int len;
285 hda_nid_t nid;
286 hda_nid_t conns[0];
287};
288
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200289/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290static struct hda_conn_list *
291lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200292{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100293 struct hda_conn_list *p;
294 list_for_each_entry(p, &codec->conn_list, list) {
295 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200296 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200297 }
298 return NULL;
299}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200300
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100301static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
302 const hda_nid_t *list)
303{
304 struct hda_conn_list *p;
305
306 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
307 if (!p)
308 return -ENOMEM;
309 p->len = len;
310 p->nid = nid;
311 memcpy(p->conns, list, len * sizeof(hda_nid_t));
312 list_add(&p->list, &codec->conn_list);
313 return 0;
314}
315
316static void remove_conn_list(struct hda_codec *codec)
317{
318 while (!list_empty(&codec->conn_list)) {
319 struct hda_conn_list *p;
320 p = list_first_entry(&codec->conn_list, typeof(*p), list);
321 list_del(&p->list);
322 kfree(p);
323 }
324}
325
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200326/* read the connection and add to the cache */
327static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200328{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100329 hda_nid_t list[32];
330 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200331 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200332
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200333 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100334 if (len == -ENOSPC) {
335 len = snd_hda_get_num_raw_conns(codec, nid);
336 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
337 if (!result)
338 return -ENOMEM;
339 len = snd_hda_get_raw_connections(codec, nid, result, len);
340 }
341 if (len >= 0)
342 len = snd_hda_override_conn_list(codec, nid, len, result);
343 if (result != list)
344 kfree(result);
345 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346}
Takashi Iwaidce20792011-06-24 14:10:28 +0200347
348/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 * snd_hda_get_conn_list - get connection list
350 * @codec: the HDA codec
351 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100352 * @listp: the pointer to store NID list
353 *
354 * Parses the connection list of the given widget and stores the pointer
355 * to the list of NIDs.
356 *
357 * Returns the number of connections, or a negative error code.
358 *
359 * Note that the returned pointer isn't protected against the list
360 * modification. If snd_hda_override_conn_list() might be called
361 * concurrently, protect with a mutex appropriately.
362 */
363int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
364 const hda_nid_t **listp)
365{
366 bool added = false;
367
368 for (;;) {
369 int err;
370 const struct hda_conn_list *p;
371
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(codec, nid);
374 if (p) {
375 if (listp)
376 *listp = p->conns;
377 return p->len;
378 }
379 if (snd_BUG_ON(added))
380 return -EINVAL;
381
382 err = read_and_add_raw_conns(codec, nid);
383 if (err < 0)
384 return err;
385 added = true;
386 }
387}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100388EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100389
390/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * @codec: the HDA codec
393 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @max_conns: max. number of connections to store
396 *
397 * Parses the connection list of the given widget and stores the list
398 * of NIDs.
399 *
400 * Returns the number of connections, or a negative error code.
401 */
402int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200403 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 const hda_nid_t *list;
406 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200407
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100408 if (len > 0 && conn_list) {
409 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100410 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200411 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200412 return -EINVAL;
413 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100414 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200415 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200416
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100417 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200418}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100419EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200420
Takashi Iwai4eea3092013-02-07 18:18:19 +0100421/* return CONNLIST_LEN parameter of the given widget */
422static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
423{
424 unsigned int wcaps = get_wcaps(codec, nid);
425 unsigned int parm;
426
427 if (!(wcaps & AC_WCAP_CONN_LIST) &&
428 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
429 return 0;
430
431 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
432 if (parm == -1)
433 parm = 0;
434 return parm;
435}
436
437int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
438{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100439 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100440}
441
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200442/**
443 * snd_hda_get_raw_connections - copy connection list without cache
444 * @codec: the HDA codec
445 * @nid: NID to parse
446 * @conn_list: connection list array
447 * @max_conns: max. number of connections to store
448 *
449 * Like snd_hda_get_connections(), copy the connection list but without
450 * checking through the connection-list cache.
451 * Currently called only from hda_proc.c, so not exported.
452 */
453int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
454 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100457 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100459 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100460 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Takashi Iwai4eea3092013-02-07 18:18:19 +0100462 parm = get_num_conns(codec, nid);
463 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200464 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (parm & AC_CLIST_LONG) {
467 /* long form */
468 shift = 16;
469 num_elems = 2;
470 } else {
471 /* short form */
472 shift = 8;
473 num_elems = 4;
474 }
475 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 mask = (1 << (shift-1)) - 1;
477
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0; /* no connection */
480
481 if (conn_len == 1) {
482 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200483 parm = snd_hda_codec_read(codec, nid, 0,
484 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200485 if (parm == -1 && codec->bus->rirb_error)
486 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100487 if (conn_list)
488 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 1;
490 }
491
492 /* multi connection */
493 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100494 prev_nid = 0;
495 for (i = 0; i < conn_len; i++) {
496 int range_val;
497 hda_nid_t val, n;
498
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200499 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 parm = snd_hda_codec_read(codec, nid, 0,
501 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200502 if (parm == -1 && codec->bus->rirb_error)
503 return -EIO;
504 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200505 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100507 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100508 codec_dbg(codec,
509 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200510 nid, i, parm);
511 return 0;
512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 parm >>= shift;
514 if (range_val) {
515 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100517 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 "invalid dep_range_val %x:%x\n",
519 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100520 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100522 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100523 if (conn_list) {
524 if (conns >= max_conns)
525 return -ENOSPC;
526 conn_list[conns] = n;
527 }
528 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100529 }
530 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100531 if (conn_list) {
532 if (conns >= max_conns)
533 return -ENOSPC;
534 conn_list[conns] = val;
535 }
536 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100538 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 return conns;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200544 * snd_hda_override_conn_list - add/modify the connection-list to cache
545 * @codec: the HDA codec
546 * @nid: NID to parse
547 * @len: number of connection list entries
548 * @list: the list of connection entries
549 *
550 * Add or modify the given connection-list to the cache. If the corresponding
551 * cache already exists, invalidate it and append a new one.
552 *
553 * Returns zero or a negative error code.
554 */
555int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
556 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100558 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200559
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100560 p = lookup_conn_list(codec, nid);
561 if (p) {
562 list_del(&p->list);
563 kfree(p);
564 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200565
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100566 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100568EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200569
570/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200571 * snd_hda_get_conn_index - get the connection index of the given NID
572 * @codec: the HDA codec
573 * @mux: NID containing the list
574 * @nid: NID to select
575 * @recursive: 1 when searching NID recursively, otherwise 0
576 *
577 * Parses the connection list of the widget @mux and checks whether the
578 * widget @nid is present. If it is, return the connection index.
579 * Otherwise it returns -1.
580 */
581int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
582 hda_nid_t nid, int recursive)
583{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100584 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 int i, nums;
586
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100587 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 for (i = 0; i < nums; i++)
589 if (conn[i] == nid)
590 return i;
591 if (!recursive)
592 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100593 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200595 return -1;
596 }
597 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 for (i = 0; i < nums; i++) {
599 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
600 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
601 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200602 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
603 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200604 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200605 return -1;
606}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100607EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Mengdong Linf1aa0682013-08-26 21:35:21 -0400609
610/* return DEVLIST_LEN parameter of the given widget */
611static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
612{
613 unsigned int wcaps = get_wcaps(codec, nid);
614 unsigned int parm;
615
616 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
617 get_wcaps_type(wcaps) != AC_WID_PIN)
618 return 0;
619
620 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
621 if (parm == -1 && codec->bus->rirb_error)
622 parm = 0;
623 return parm & AC_DEV_LIST_LEN_MASK;
624}
625
626/**
627 * snd_hda_get_devices - copy device list without cache
628 * @codec: the HDA codec
629 * @nid: NID of the pin to parse
630 * @dev_list: device list array
631 * @max_devices: max. number of devices to store
632 *
633 * Copy the device list. This info is dynamic and so not cached.
634 * Currently called only from hda_proc.c, so not exported.
635 */
636int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
637 u8 *dev_list, int max_devices)
638{
639 unsigned int parm;
640 int i, dev_len, devices;
641
642 parm = get_num_devices(codec, nid);
643 if (!parm) /* not multi-stream capable */
644 return 0;
645
646 dev_len = parm + 1;
647 dev_len = dev_len < max_devices ? dev_len : max_devices;
648
649 devices = 0;
650 while (devices < dev_len) {
651 parm = snd_hda_codec_read(codec, nid, 0,
652 AC_VERB_GET_DEVICE_LIST, devices);
653 if (parm == -1 && codec->bus->rirb_error)
654 break;
655
656 for (i = 0; i < 8; i++) {
657 dev_list[devices] = (u8)parm;
658 parm >>= 4;
659 devices++;
660 if (devices >= dev_len)
661 break;
662 }
663 }
664 return devices;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Wang YanQing2195b062013-05-07 11:27:33 +0800684 if (!bus || !bus->workq)
685 return 0;
686
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200687 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100688 unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
690 unsol->wp = wp;
691
692 wp <<= 1;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
695
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100700EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800703 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Howellsc4028952006-11-22 14:57:56 +0000705static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai922c88a2015-02-17 14:46:40 +0100707 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
708 struct hda_bus_unsolicited *unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct hda_codec *codec;
710 unsigned int rp, caddr, res;
711
712 while (unsol->rp != unsol->wp) {
713 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
714 unsol->rp = rp;
715 rp <<= 1;
716 res = unsol->queue[rp];
717 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200718 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
720 codec = bus->caddr_tbl[caddr & 0x0f];
721 if (codec && codec->patch_ops.unsol_event)
722 codec->patch_ops.unsol_event(codec, res);
723 }
724}
725
726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * destructor
728 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100729static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200731 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100732 return;
733
734 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100735 if (bus->workq)
736 flush_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (bus->ops.private_free)
738 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100739 if (bus->workq)
740 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwai2565c892014-02-19 11:41:09 +0100747 snd_hda_bus_free(device->device_data);
748 return 0;
749}
750
751static int snd_hda_bus_dev_disconnect(struct snd_device *device)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100754 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
759 * snd_hda_bus_new - create a HDA bus
760 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * @busp: the pointer to store the created bus instance
762 *
763 * Returns 0 if successful, or a negative error code.
764 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100765int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100766 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct hda_bus *bus;
769 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100770 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100771 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .dev_free = snd_hda_bus_dev_free,
773 };
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (busp)
776 *busp = NULL;
777
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200778 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100780 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENOMEM;
782 }
783
784 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100785 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200786 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100788 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100790 snprintf(bus->workq_name, sizeof(bus->workq_name),
791 "hd-audio%d", card->number);
792 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100793 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100794 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100795 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100796 kfree(bus);
797 return -ENOMEM;
798 }
799
Takashi Iwai0ba21762007-04-16 11:29:14 +0200800 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
801 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 snd_hda_bus_free(bus);
803 return err;
804 }
805 if (busp)
806 *busp = bus;
807 return 0;
808}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100809EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200812 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100814static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200816 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hda_nid_t nid;
818
819 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
820 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200821 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200822 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200823 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200824 case AC_GRP_AUDIO_FUNCTION:
825 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200826 codec->afg_function_id = function_id & 0xff;
827 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200828 break;
829 case AC_GRP_MODEM_FUNCTION:
830 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200831 codec->mfg_function_id = function_id & 0xff;
832 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200833 break;
834 default:
835 break;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100841 * read widget caps for each widget and store in cache
842 */
843static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
844{
845 int i;
846 hda_nid_t nid;
847
848 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
849 &codec->start_nid);
850 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200851 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100852 return -ENOMEM;
853 nid = codec->start_nid;
854 for (i = 0; i < codec->num_nodes; i++, nid++)
855 codec->wcaps[i] = snd_hda_param_read(codec, nid,
856 AC_PAR_AUDIO_WIDGET_CAP);
857 return 0;
858}
859
Takashi Iwai3be14142009-02-20 14:11:16 +0100860/* read all pin default configurations and save codec->init_pins */
861static int read_pin_defaults(struct hda_codec *codec)
862{
863 int i;
864 hda_nid_t nid = codec->start_nid;
865
866 for (i = 0; i < codec->num_nodes; i++, nid++) {
867 struct hda_pincfg *pin;
868 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200869 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100870 if (wid_type != AC_WID_PIN)
871 continue;
872 pin = snd_array_new(&codec->init_pins);
873 if (!pin)
874 return -ENOMEM;
875 pin->nid = nid;
876 pin->cfg = snd_hda_codec_read(codec, nid, 0,
877 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200878 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
879 AC_VERB_GET_PIN_WIDGET_CONTROL,
880 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100881 }
882 return 0;
883}
884
885/* look up the given pin config list and return the item matching with NID */
886static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
887 struct snd_array *array,
888 hda_nid_t nid)
889{
890 int i;
891 for (i = 0; i < array->used; i++) {
892 struct hda_pincfg *pin = snd_array_elem(array, i);
893 if (pin->nid == nid)
894 return pin;
895 }
896 return NULL;
897}
898
Takashi Iwai3be14142009-02-20 14:11:16 +0100899/* set the current pin config value for the given NID.
900 * the value is cached, and read via snd_hda_codec_get_pincfg()
901 */
902int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
903 hda_nid_t nid, unsigned int cfg)
904{
905 struct hda_pincfg *pin;
906
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200907 /* the check below may be invalid when pins are added by a fixup
908 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
909 * for now
910 */
911 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100912 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
913 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200914 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100915
Takashi Iwai3be14142009-02-20 14:11:16 +0100916 pin = look_up_pincfg(codec, list, nid);
917 if (!pin) {
918 pin = snd_array_new(list);
919 if (!pin)
920 return -ENOMEM;
921 pin->nid = nid;
922 }
923 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100924 return 0;
925}
926
Takashi Iwaid5191e52009-11-16 14:58:17 +0100927/**
928 * snd_hda_codec_set_pincfg - Override a pin default configuration
929 * @codec: the HDA codec
930 * @nid: NID to set the pin config
931 * @cfg: the pin default config value
932 *
933 * Override a pin default configuration value in the cache.
934 * This value can be read by snd_hda_codec_get_pincfg() in a higher
935 * priority than the real hardware value.
936 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100937int snd_hda_codec_set_pincfg(struct hda_codec *codec,
938 hda_nid_t nid, unsigned int cfg)
939{
Takashi Iwai346ff702009-02-23 09:42:57 +0100940 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100941}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100942EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100943
Takashi Iwaid5191e52009-11-16 14:58:17 +0100944/**
945 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
946 * @codec: the HDA codec
947 * @nid: NID to get the pin config
948 *
949 * Get the current pin config value of the given pin NID.
950 * If the pincfg value is cached or overridden via sysfs or driver,
951 * returns the cached value.
952 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100953unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
954{
955 struct hda_pincfg *pin;
956
Takashi Iwai648a8d22014-02-25 10:38:13 +0100957#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100958 {
959 unsigned int cfg = 0;
960 mutex_lock(&codec->user_mutex);
961 pin = look_up_pincfg(codec, &codec->user_pins, nid);
962 if (pin)
963 cfg = pin->cfg;
964 mutex_unlock(&codec->user_mutex);
965 if (cfg)
966 return cfg;
967 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100968#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100969 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
970 if (pin)
971 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100972 pin = look_up_pincfg(codec, &codec->init_pins, nid);
973 if (pin)
974 return pin->cfg;
975 return 0;
976}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100977EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978
Takashi Iwai95a962c2014-10-29 16:03:58 +0100979/**
980 * snd_hda_codec_set_pin_target - remember the current pinctl target value
981 * @codec: the HDA codec
982 * @nid: pin NID
983 * @val: assigned pinctl value
984 *
985 * This function stores the given value to a pinctl target value in the
986 * pincfg table. This isn't always as same as the actually written value
987 * but can be referred at any time via snd_hda_codec_get_pin_target().
988 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100989int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
990 unsigned int val)
991{
992 struct hda_pincfg *pin;
993
994 pin = look_up_pincfg(codec, &codec->init_pins, nid);
995 if (!pin)
996 return -EINVAL;
997 pin->target = val;
998 return 0;
999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001000EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001001
Takashi Iwai95a962c2014-10-29 16:03:58 +01001002/**
1003 * snd_hda_codec_get_pin_target - return the current pinctl target value
1004 * @codec: the HDA codec
1005 * @nid: pin NID
1006 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001007int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1008{
1009 struct hda_pincfg *pin;
1010
1011 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1012 if (!pin)
1013 return 0;
1014 return pin->target;
1015}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001016EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001017
Takashi Iwai92ee6162009-12-27 11:18:59 +01001018/**
1019 * snd_hda_shutup_pins - Shut up all pins
1020 * @codec: the HDA codec
1021 *
1022 * Clear all pin controls to shup up before suspend for avoiding click noise.
1023 * The controls aren't cached so that they can be resumed properly.
1024 */
1025void snd_hda_shutup_pins(struct hda_codec *codec)
1026{
1027 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001028 /* don't shut up pins when unloading the driver; otherwise it breaks
1029 * the default pin setup at the next load of the driver
1030 */
1031 if (codec->bus->shutdown)
1032 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001033 for (i = 0; i < codec->init_pins.used; i++) {
1034 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1035 /* use read here for syncing after issuing each verb */
1036 snd_hda_codec_read(codec, pin->nid, 0,
1037 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1038 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001039 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001040}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001041EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001042
Takashi Iwai2a439522011-07-26 09:52:50 +02001043#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001044/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1045static void restore_shutup_pins(struct hda_codec *codec)
1046{
1047 int i;
1048 if (!codec->pins_shutup)
1049 return;
1050 if (codec->bus->shutdown)
1051 return;
1052 for (i = 0; i < codec->init_pins.used; i++) {
1053 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1054 snd_hda_codec_write(codec, pin->nid, 0,
1055 AC_VERB_SET_PIN_WIDGET_CONTROL,
1056 pin->ctrl);
1057 }
1058 codec->pins_shutup = 0;
1059}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001060#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001061
David Henningsson26a6cb62012-10-09 15:04:21 +02001062static void hda_jackpoll_work(struct work_struct *work)
1063{
1064 struct hda_codec *codec =
1065 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001066
1067 snd_hda_jack_set_dirty_all(codec);
1068 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001069
1070 if (!codec->jackpoll_interval)
1071 return;
1072
David Henningsson26a6cb62012-10-09 15:04:21 +02001073 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1074 codec->jackpoll_interval);
1075}
1076
Takashi Iwai01751f52007-08-10 16:59:39 +02001077static void init_hda_cache(struct hda_cache_rec *cache,
1078 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001079static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001080
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001081/* release all pincfg lists */
1082static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001083{
Takashi Iwai346ff702009-02-23 09:42:57 +01001084 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001085#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001086 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001087#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 snd_array_free(&codec->init_pins);
1089}
1090
Takashi Iwai54d17402005-11-21 16:33:22 +01001091/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001092 * audio-converter setup caches
1093 */
1094struct hda_cvt_setup {
1095 hda_nid_t nid;
1096 u8 stream_tag;
1097 u8 channel_id;
1098 u16 format_id;
1099 unsigned char active; /* cvt is currently used */
1100 unsigned char dirty; /* setups should be cleared */
1101};
1102
1103/* get or create a cache entry for the given audio converter NID */
1104static struct hda_cvt_setup *
1105get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1106{
1107 struct hda_cvt_setup *p;
1108 int i;
1109
1110 for (i = 0; i < codec->cvt_setups.used; i++) {
1111 p = snd_array_elem(&codec->cvt_setups, i);
1112 if (p->nid == nid)
1113 return p;
1114 }
1115 p = snd_array_new(&codec->cvt_setups);
1116 if (p)
1117 p->nid = nid;
1118 return p;
1119}
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * codec destructor
1123 */
1124static void snd_hda_codec_free(struct hda_codec *codec)
1125{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001126 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001128 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001129 if (device_is_registered(hda_codec_dev(codec)))
1130 device_del(hda_codec_dev(codec));
Takashi Iwai59cad162012-06-26 15:00:20 +02001131 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001132 free_init_pincfgs(codec);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001133 flush_workqueue(codec->bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001135 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001136 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001137 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001138 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001139 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 codec->bus->caddr_tbl[codec->addr] = NULL;
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001141 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001142 snd_hda_sysfs_clear(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001144 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001145 kfree(codec->vendor_name);
1146 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001147 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001148 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001149 codec->bus->num_codecs--;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001150 put_device(hda_codec_dev(codec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001153static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1154 hda_nid_t fg, unsigned int power_state);
1155
Takashi Iwaid8193872012-08-31 07:54:38 -07001156static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001157 unsigned int power_state);
1158
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001159static int snd_hda_codec_dev_register(struct snd_device *device)
1160{
1161 struct hda_codec *codec = device->device_data;
1162
Takashi Iwaid604b392014-02-28 13:42:09 +01001163 snd_hda_register_beep_device(codec);
Takashi Iwaibb573922015-02-20 09:26:04 +01001164 if (device_is_registered(hda_codec_dev(codec)))
Takashi Iwaicc72da72015-02-19 16:00:22 +01001165 pm_runtime_enable(hda_codec_dev(codec));
Takashi Iwai709949f2015-02-20 09:58:14 +01001166 /* it was powered up in snd_hda_codec_new(), now all done */
1167 snd_hda_power_down(codec);
Takashi Iwaid604b392014-02-28 13:42:09 +01001168 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001169}
1170
1171static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1172{
1173 struct hda_codec *codec = device->device_data;
1174
Takashi Iwaid604b392014-02-28 13:42:09 +01001175 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001176 return 0;
1177}
1178
Takashi Iwai2565c892014-02-19 11:41:09 +01001179static int snd_hda_codec_dev_free(struct snd_device *device)
1180{
1181 snd_hda_codec_free(device->device_data);
1182 return 0;
1183}
1184
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001185/* just free the container */
1186static void snd_hda_codec_dev_release(struct device *dev)
1187{
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001188 kfree(dev_to_hda_codec(dev));
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/**
1192 * snd_hda_codec_new - create a HDA codec
1193 * @bus: the bus to assign
1194 * @codec_addr: the codec address
1195 * @codecp: the pointer to store the generated codec
1196 *
1197 * Returns 0 if successful, or a negative error code.
1198 */
Takashi Iwai6efdd852015-02-27 16:09:22 +01001199int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1200 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001203 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001204 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001205 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001207 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001208 .dev_register = snd_hda_codec_dev_register,
1209 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001210 .dev_free = snd_hda_codec_dev_free,
1211 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Takashi Iwaida3cec32008-08-08 17:12:14 +02001213 if (snd_BUG_ON(!bus))
1214 return -EINVAL;
1215 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1216 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001219 dev_err(card->dev,
Takashi Iwai4e76a882014-02-25 12:21:03 +01001220 "address 0x%x is already occupied\n",
1221 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EBUSY;
1223 }
1224
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001225 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (codec == NULL) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001227 dev_err(card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -ENOMEM;
1229 }
1230
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001231 dev = hda_codec_dev(codec);
1232 device_initialize(dev);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001233 dev->parent = card->dev;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001234 dev->bus = &snd_hda_bus_type;
1235 dev->release = snd_hda_codec_dev_release;
1236 dev->groups = snd_hda_dev_attr_groups;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001237 dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001238 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01001239 device_enable_async_suspend(dev);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001242 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001244 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001245 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001246 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001247 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001248 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001249 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1250 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001251 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001252 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001253 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001254 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001255 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001256 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001257 INIT_LIST_HEAD(&codec->conn_list);
1258
David Henningsson26a6cb62012-10-09 15:04:21 +02001259 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001260 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001261 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Takashi Iwai83012a72012-08-24 18:38:08 +02001263#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001264 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
Takashi Iwai709949f2015-02-20 09:58:14 +01001265 * it's powered down later in snd_hda_codec_dev_register().
Takashi Iwaicb53c622007-08-10 17:21:45 +02001266 */
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01001267 set_bit(codec->addr, &bus->codec_powered);
Takashi Iwaicc72da72015-02-19 16:00:22 +01001268 pm_runtime_set_active(hda_codec_dev(codec));
1269 pm_runtime_get_noresume(hda_codec_dev(codec));
1270 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001271#endif
1272
Takashi Iwai648a8d22014-02-25 10:38:13 +01001273 snd_hda_sysfs_init(codec);
1274
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001275 if (codec->bus->modelname) {
1276 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1277 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001278 err = -ENODEV;
1279 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001280 }
1281 }
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001284 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 bus->caddr_tbl[codec_addr] = codec;
1287
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001290 if (codec->vendor_id == -1)
1291 /* read again, hopefully the access method was corrected
1292 * in the last read...
1293 */
1294 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_SUBSYSTEM_ID);
1298 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001301 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001302 if (!codec->afg && !codec->mfg) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001303 dev_err(card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = -ENODEV;
1305 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Takashi Iwaid8193872012-08-31 07:54:38 -07001308 fg = codec->afg ? codec->afg : codec->mfg;
1309 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 if (err < 0) {
Takashi Iwai6efdd852015-02-27 16:09:22 +01001311 dev_err(card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = read_pin_defaults(codec);
1315 if (err < 0)
1316 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001320 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001321 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001322 }
1323
Takashi Iwai83012a72012-08-24 18:38:08 +02001324#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001325 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001326 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001327#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001328 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001329 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001330
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001331 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001332 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001333
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001334 snd_hda_codec_proc_new(codec);
1335
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001337
1338 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1339 codec->subsystem_id, codec->revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001340 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001341
Takashi Iwai6efdd852015-02-27 16:09:22 +01001342 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +01001343 if (err < 0)
1344 goto error;
1345
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001346 if (codecp)
1347 *codecp = codec;
1348 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001349
1350 error:
1351 snd_hda_codec_free(codec);
1352 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001354EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001355
Takashi Iwai95a962c2014-10-29 16:03:58 +01001356/**
1357 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1358 * @codec: the HDA codec
1359 *
1360 * Forcibly refresh the all widget caps and the init pin configurations of
1361 * the given codec.
1362 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001363int snd_hda_codec_update_widgets(struct hda_codec *codec)
1364{
1365 hda_nid_t fg;
1366 int err;
1367
1368 /* Assume the function group node does not change,
1369 * only the widget nodes may change.
1370 */
1371 kfree(codec->wcaps);
1372 fg = codec->afg ? codec->afg : codec->mfg;
1373 err = read_widget_caps(codec, fg);
1374 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001375 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001376 return err;
1377 }
1378
1379 snd_array_free(&codec->init_pins);
1380 err = read_pin_defaults(codec);
1381
1382 return err;
1383}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001384EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001385
Takashi Iwaied360812012-08-08 17:12:52 +02001386/* update the stream-id if changed */
1387static void update_pcm_stream_id(struct hda_codec *codec,
1388 struct hda_cvt_setup *p, hda_nid_t nid,
1389 u32 stream_tag, int channel_id)
1390{
1391 unsigned int oldval, newval;
1392
1393 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1394 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1395 newval = (stream_tag << 4) | channel_id;
1396 if (oldval != newval)
1397 snd_hda_codec_write(codec, nid, 0,
1398 AC_VERB_SET_CHANNEL_STREAMID,
1399 newval);
1400 p->stream_tag = stream_tag;
1401 p->channel_id = channel_id;
1402 }
1403}
1404
1405/* update the format-id if changed */
1406static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1407 hda_nid_t nid, int format)
1408{
1409 unsigned int oldval;
1410
1411 if (p->format_id != format) {
1412 oldval = snd_hda_codec_read(codec, nid, 0,
1413 AC_VERB_GET_STREAM_FORMAT, 0);
1414 if (oldval != format) {
1415 msleep(1);
1416 snd_hda_codec_write(codec, nid, 0,
1417 AC_VERB_SET_STREAM_FORMAT,
1418 format);
1419 }
1420 p->format_id = format;
1421 }
1422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/**
1425 * snd_hda_codec_setup_stream - set up the codec for streaming
1426 * @codec: the CODEC to set up
1427 * @nid: the NID to set up
1428 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1429 * @channel_id: channel id to pass, zero based.
1430 * @format: stream format.
1431 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001432void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1433 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int channel_id, int format)
1435{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001436 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001437 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001438 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001439 int i;
1440
Takashi Iwai0ba21762007-04-16 11:29:14 +02001441 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001442 return;
1443
Takashi Iwai4e76a882014-02-25 12:21:03 +01001444 codec_dbg(codec,
1445 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1446 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001447 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001448 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001449 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001450
1451 if (codec->pcm_format_first)
1452 update_pcm_format(codec, p, nid, format);
1453 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1454 if (!codec->pcm_format_first)
1455 update_pcm_format(codec, p, nid, format);
1456
Takashi Iwaieb541332010-08-06 13:48:11 +02001457 p->active = 1;
1458 p->dirty = 0;
1459
1460 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001461 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001462 list_for_each_entry(c, &codec->bus->codec_list, list) {
1463 for (i = 0; i < c->cvt_setups.used; i++) {
1464 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001465 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001466 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001467 p->dirty = 1;
1468 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001471EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Takashi Iwaif0cea792010-08-13 11:56:53 +02001473static void really_cleanup_stream(struct hda_codec *codec,
1474 struct hda_cvt_setup *q);
1475
Takashi Iwaid5191e52009-11-16 14:58:17 +01001476/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001477 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001478 * @codec: the CODEC to clean up
1479 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001480 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001481 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001482void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1483 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001484{
Takashi Iwaieb541332010-08-06 13:48:11 +02001485 struct hda_cvt_setup *p;
1486
Takashi Iwai888afa12008-03-18 09:57:50 +01001487 if (!nid)
1488 return;
1489
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001490 if (codec->no_sticky_stream)
1491 do_now = 1;
1492
Takashi Iwai4e76a882014-02-25 12:21:03 +01001493 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001494 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001495 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001496 /* here we just clear the active flag when do_now isn't set;
1497 * actual clean-ups will be done later in
1498 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1499 */
1500 if (do_now)
1501 really_cleanup_stream(codec, p);
1502 else
1503 p->active = 0;
1504 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001505}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001506EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001507
Takashi Iwaieb541332010-08-06 13:48:11 +02001508static void really_cleanup_stream(struct hda_codec *codec,
1509 struct hda_cvt_setup *q)
1510{
1511 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001512 if (q->stream_tag || q->channel_id)
1513 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1514 if (q->format_id)
1515 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1516);
Takashi Iwaieb541332010-08-06 13:48:11 +02001517 memset(q, 0, sizeof(*q));
1518 q->nid = nid;
1519}
1520
1521/* clean up the all conflicting obsolete streams */
1522static void purify_inactive_streams(struct hda_codec *codec)
1523{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001524 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001525 int i;
1526
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001527 list_for_each_entry(c, &codec->bus->codec_list, list) {
1528 for (i = 0; i < c->cvt_setups.used; i++) {
1529 struct hda_cvt_setup *p;
1530 p = snd_array_elem(&c->cvt_setups, i);
1531 if (p->dirty)
1532 really_cleanup_stream(c, p);
1533 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001534 }
1535}
1536
Takashi Iwai2a439522011-07-26 09:52:50 +02001537#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001538/* clean up all streams; called from suspend */
1539static void hda_cleanup_all_streams(struct hda_codec *codec)
1540{
1541 int i;
1542
1543 for (i = 0; i < codec->cvt_setups.used; i++) {
1544 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1545 if (p->stream_tag)
1546 really_cleanup_stream(codec, p);
1547 }
1548}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001549#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/*
1552 * amp access functions
1553 */
1554
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001555/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001556#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001557#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001558#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1559#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001561#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001564static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001565 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai01751f52007-08-10 16:59:39 +02001567 memset(cache, 0, sizeof(*cache));
1568 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001569 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001570}
1571
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001572static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001573{
Takashi Iwai603c4012008-07-30 15:01:44 +02001574 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001578static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Takashi Iwai01751f52007-08-10 16:59:39 +02001580 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1581 u16 cur = cache->hash[idx];
1582 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001585 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (info->key == key)
1587 return info;
1588 cur = info->next;
1589 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001590 return NULL;
1591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001593/* query the hash. allocate an entry if not found. */
1594static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1595 u32 key)
1596{
1597 struct hda_cache_head *info = get_hash(cache, key);
1598 if (!info) {
1599 u16 idx, cur;
1600 /* add a new hash entry */
1601 info = snd_array_new(&cache->buf);
1602 if (!info)
1603 return NULL;
1604 cur = snd_array_index(&cache->buf, info);
1605 info->key = key;
1606 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001607 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001608 idx = key % (u16)ARRAY_SIZE(cache->hash);
1609 info->next = cache->hash[idx];
1610 cache->hash[idx] = cur;
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return info;
1613}
1614
Takashi Iwai01751f52007-08-10 16:59:39 +02001615/* query and allocate an amp hash entry */
1616static inline struct hda_amp_info *
1617get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1618{
1619 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1620}
1621
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001622/* overwrite the value with the key in the caps hash */
1623static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1624{
1625 struct hda_amp_info *info;
1626
1627 mutex_lock(&codec->hash_mutex);
1628 info = get_alloc_amp_hash(codec, key);
1629 if (!info) {
1630 mutex_unlock(&codec->hash_mutex);
1631 return -EINVAL;
1632 }
1633 info->amp_caps = val;
1634 info->head.val |= INFO_AMP_CAPS;
1635 mutex_unlock(&codec->hash_mutex);
1636 return 0;
1637}
1638
1639/* query the value from the caps hash; if not found, fetch the current
1640 * value from the given function and store in the hash
1641 */
1642static unsigned int
1643query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1644 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1645{
1646 struct hda_amp_info *info;
1647 unsigned int val;
1648
1649 mutex_lock(&codec->hash_mutex);
1650 info = get_alloc_amp_hash(codec, key);
1651 if (!info) {
1652 mutex_unlock(&codec->hash_mutex);
1653 return 0;
1654 }
1655 if (!(info->head.val & INFO_AMP_CAPS)) {
1656 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1657 val = func(codec, nid, dir);
1658 write_caps_hash(codec, key, val);
1659 } else {
1660 val = info->amp_caps;
1661 mutex_unlock(&codec->hash_mutex);
1662 }
1663 return val;
1664}
1665
1666static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1667 int direction)
1668{
1669 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1670 nid = codec->afg;
1671 return snd_hda_param_read(codec, nid,
1672 direction == HDA_OUTPUT ?
1673 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1674}
1675
Takashi Iwaid5191e52009-11-16 14:58:17 +01001676/**
1677 * query_amp_caps - query AMP capabilities
1678 * @codec: the HD-auio codec
1679 * @nid: the NID to query
1680 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1681 *
1682 * Query AMP capabilities for the given widget and direction.
1683 * Returns the obtained capability bits.
1684 *
1685 * When cap bits have been already read, this doesn't read again but
1686 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001688u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001690 return query_caps_hash(codec, nid, direction,
1691 HDA_HASH_KEY(nid, direction, 0),
1692 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001694EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Takashi Iwaid5191e52009-11-16 14:58:17 +01001696/**
David Henningsson861a04e2014-09-23 10:38:17 +02001697 * snd_hda_check_amp_caps - query AMP capabilities
1698 * @codec: the HD-audio codec
1699 * @nid: the NID to query
1700 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001701 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001702 *
1703 * Check whether the widget has the given amp capability for the direction.
1704 */
1705bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1706 int dir, unsigned int bits)
1707{
1708 if (!nid)
1709 return false;
1710 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1711 if (query_amp_caps(codec, nid, dir) & bits)
1712 return true;
1713 return false;
1714}
1715EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1716
1717/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001718 * snd_hda_override_amp_caps - Override the AMP capabilities
1719 * @codec: the CODEC to clean up
1720 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001721 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001722 * @caps: the capability bits to set
1723 *
1724 * Override the cached AMP caps bits value by the given one.
1725 * This function is useful if the driver needs to adjust the AMP ranges,
1726 * e.g. limit to 0dB, etc.
1727 *
1728 * Returns zero if successful or a negative error code.
1729 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001730int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1731 unsigned int caps)
1732{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001733 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001735EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001736
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001737static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1738 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001739{
1740 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1741}
1742
Takashi Iwaid5191e52009-11-16 14:58:17 +01001743/**
1744 * snd_hda_query_pin_caps - Query PIN capabilities
1745 * @codec: the HD-auio codec
1746 * @nid: the NID to query
1747 *
1748 * Query PIN capabilities for the given widget.
1749 * Returns the obtained capability bits.
1750 *
1751 * When cap bits have been already read, this doesn't read again but
1752 * returns the cached value.
1753 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001754u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1755{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001756 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001757 read_pin_cap);
1758}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001759EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001760
Wu Fengguang864f92b2009-11-18 12:38:02 +08001761/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001762 * snd_hda_override_pin_caps - Override the pin capabilities
1763 * @codec: the CODEC
1764 * @nid: the NID to override
1765 * @caps: the capability bits to set
1766 *
1767 * Override the cached PIN capabilitiy bits value by the given one.
1768 *
1769 * Returns zero if successful or a negative error code.
1770 */
1771int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1772 unsigned int caps)
1773{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001776EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001777
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001778/* read or sync the hash value with the current value;
1779 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001781static struct hda_amp_info *
1782update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001783 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001785 struct hda_amp_info *info;
1786 unsigned int parm, val = 0;
1787 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001789 retry:
1790 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1791 if (!info)
1792 return NULL;
1793 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1794 if (!val_read) {
1795 mutex_unlock(&codec->hash_mutex);
1796 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1797 parm |= direction == HDA_OUTPUT ?
1798 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1799 parm |= index;
1800 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001801 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001802 val &= 0xff;
1803 val_read = true;
1804 mutex_lock(&codec->hash_mutex);
1805 goto retry;
1806 }
1807 info->vol[ch] = val;
1808 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001809 } else if (init_only)
1810 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001811 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001815 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001817static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001818 hda_nid_t nid, int ch, int direction, int index,
1819 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 u32 parm;
1822
1823 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1824 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1825 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001826 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1827 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001828 ; /* set the zero value as a fake mute */
1829 else
1830 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1832}
1833
Takashi Iwaid5191e52009-11-16 14:58:17 +01001834/**
1835 * snd_hda_codec_amp_read - Read AMP value
1836 * @codec: HD-audio codec
1837 * @nid: NID to read the AMP value
1838 * @ch: channel (left=0 or right=1)
1839 * @direction: #HDA_INPUT or #HDA_OUTPUT
1840 * @index: the index value (only for input direction)
1841 *
1842 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Takashi Iwai834be882006-03-01 14:16:17 +01001844int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1845 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001847 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001848 unsigned int val = 0;
1849
1850 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001851 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001852 if (info)
1853 val = info->vol[ch];
1854 mutex_unlock(&codec->hash_mutex);
1855 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001857EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Takashi Iwai280e57d2012-12-14 10:32:21 +01001859static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1860 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001861 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001862{
1863 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001864 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001865
1866 if (snd_BUG_ON(mask & ~0xff))
1867 mask &= 0xff;
1868 val &= mask;
1869
1870 mutex_lock(&codec->hash_mutex);
1871 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1872 if (!info) {
1873 mutex_unlock(&codec->hash_mutex);
1874 return 0;
1875 }
1876 val |= info->vol[ch] & ~mask;
1877 if (info->vol[ch] == val) {
1878 mutex_unlock(&codec->hash_mutex);
1879 return 0;
1880 }
1881 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001882 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001883 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001884 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001885 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001886 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001887 return 1;
1888}
1889
Takashi Iwaid5191e52009-11-16 14:58:17 +01001890/**
1891 * snd_hda_codec_amp_update - update the AMP value
1892 * @codec: HD-audio codec
1893 * @nid: NID to read the AMP value
1894 * @ch: channel (left=0 or right=1)
1895 * @direction: #HDA_INPUT or #HDA_OUTPUT
1896 * @idx: the index value (only for input direction)
1897 * @mask: bit mask to set
1898 * @val: the bits value to set
1899 *
1900 * Update the AMP value with a bit mask.
1901 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001902 */
Takashi Iwai834be882006-03-01 14:16:17 +01001903int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1904 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001906 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1907 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001909EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Takashi Iwaid5191e52009-11-16 14:58:17 +01001911/**
1912 * snd_hda_codec_amp_stereo - update the AMP stereo values
1913 * @codec: HD-audio codec
1914 * @nid: NID to read the AMP value
1915 * @direction: #HDA_INPUT or #HDA_OUTPUT
1916 * @idx: the index value (only for input direction)
1917 * @mask: bit mask to set
1918 * @val: the bits value to set
1919 *
1920 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1921 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001922 */
1923int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1924 int direction, int idx, int mask, int val)
1925{
1926 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001927
1928 if (snd_BUG_ON(mask & ~0xff))
1929 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001930 for (ch = 0; ch < 2; ch++)
1931 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1932 idx, mask, val);
1933 return ret;
1934}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001935EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001936
Takashi Iwai95a962c2014-10-29 16:03:58 +01001937/**
1938 * snd_hda_codec_amp_init - initialize the AMP value
1939 * @codec: the HDA codec
1940 * @nid: NID to read the AMP value
1941 * @ch: channel (left=0 or right=1)
1942 * @dir: #HDA_INPUT or #HDA_OUTPUT
1943 * @idx: the index value (only for input direction)
1944 * @mask: bit mask to set
1945 * @val: the bits value to set
1946 *
1947 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001948 * the first access. If the amp was already initialized / updated beforehand,
1949 * this does nothing.
1950 */
1951int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1952 int dir, int idx, int mask, int val)
1953{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001954 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1955 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001956}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001957EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001958
Takashi Iwai95a962c2014-10-29 16:03:58 +01001959/**
1960 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1961 * @codec: the HDA codec
1962 * @nid: NID to read the AMP value
1963 * @dir: #HDA_INPUT or #HDA_OUTPUT
1964 * @idx: the index value (only for input direction)
1965 * @mask: bit mask to set
1966 * @val: the bits value to set
1967 *
1968 * Call snd_hda_codec_amp_init() for both stereo channels.
1969 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1971 int dir, int idx, int mask, int val)
1972{
1973 int ch, ret = 0;
1974
1975 if (snd_BUG_ON(mask & ~0xff))
1976 mask &= 0xff;
1977 for (ch = 0; ch < 2; ch++)
1978 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1979 idx, mask, val);
1980 return ret;
1981}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001982EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001983
Takashi Iwaid5191e52009-11-16 14:58:17 +01001984/**
1985 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1986 * @codec: HD-audio codec
1987 *
1988 * Resume the all amp commands from the cache.
1989 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001990void snd_hda_codec_resume_amp(struct hda_codec *codec)
1991{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001992 int i;
1993
Takashi Iwaic370dd62012-12-13 18:30:04 +01001994 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001995 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001996 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1997 struct hda_amp_info *buffer;
1998 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001999 hda_nid_t nid;
2000 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002001 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002002
2003 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002004 if (!buffer->head.dirty)
2005 continue;
2006 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002007 info = *buffer;
2008 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002009 if (!key)
2010 continue;
2011 nid = key & 0xff;
2012 idx = (key >> 16) & 0xff;
2013 dir = (key >> 24) & 0xff;
2014 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002015 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002016 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002017 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002018 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2019 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002020 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002021 }
2022 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002023 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002025EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002027static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2028 unsigned int ofs)
2029{
2030 u32 caps = query_amp_caps(codec, nid, dir);
2031 /* get num steps */
2032 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2033 if (ofs < caps)
2034 caps -= ofs;
2035 return caps;
2036}
2037
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038/**
2039 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002040 * @kcontrol: referred ctl element
2041 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002042 *
2043 * The control element is supposed to have the private_value field
2044 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002046int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2050 u16 nid = get_amp_nid(kcontrol);
2051 u8 chs = get_amp_channels(kcontrol);
2052 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002053 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002055 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2056 uinfo->count = chs == 3 ? 2 : 1;
2057 uinfo->value.integer.min = 0;
2058 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2059 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002060 codec_warn(codec,
2061 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2062 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
2066}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002067EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002069
2070static inline unsigned int
2071read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2072 int ch, int dir, int idx, unsigned int ofs)
2073{
2074 unsigned int val;
2075 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2076 val &= HDA_AMP_VOLMASK;
2077 if (val >= ofs)
2078 val -= ofs;
2079 else
2080 val = 0;
2081 return val;
2082}
2083
2084static inline int
2085update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2086 int ch, int dir, int idx, unsigned int ofs,
2087 unsigned int val)
2088{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002089 unsigned int maxval;
2090
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002091 if (val > 0)
2092 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002093 /* ofs = 0: raw max value */
2094 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002095 if (val > maxval)
2096 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002097 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2098 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002099}
2100
Takashi Iwaid5191e52009-11-16 14:58:17 +01002101/**
2102 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002103 * @kcontrol: ctl element
2104 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002105 *
2106 * The control element is supposed to have the private_value field
2107 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2108 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002109int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2113 hda_nid_t nid = get_amp_nid(kcontrol);
2114 int chs = get_amp_channels(kcontrol);
2115 int dir = get_amp_direction(kcontrol);
2116 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002117 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 long *valp = ucontrol->value.integer.value;
2119
2120 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002121 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002123 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002126EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Takashi Iwaid5191e52009-11-16 14:58:17 +01002128/**
2129 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002130 * @kcontrol: ctl element
2131 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002132 *
2133 * The control element is supposed to have the private_value field
2134 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2135 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002136int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 hda_nid_t nid = get_amp_nid(kcontrol);
2141 int chs = get_amp_channels(kcontrol);
2142 int dir = get_amp_direction(kcontrol);
2143 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002144 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 long *valp = ucontrol->value.integer.value;
2146 int change = 0;
2147
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002148 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002149 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002150 valp++;
2151 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002152 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002153 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return change;
2155}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002156EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Takashi Iwaid5191e52009-11-16 14:58:17 +01002158/**
2159 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002160 * @kcontrol: ctl element
2161 * @op_flag: operation flag
2162 * @size: byte size of input TLV
2163 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002164 *
2165 * The control element is supposed to have the private_value field
2166 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2167 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002168int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2169 unsigned int size, unsigned int __user *_tlv)
2170{
2171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2172 hda_nid_t nid = get_amp_nid(kcontrol);
2173 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002174 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002175 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002176 u32 caps, val1, val2;
2177
2178 if (size < 4 * sizeof(unsigned int))
2179 return -ENOMEM;
2180 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002181 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2182 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002183 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002184 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002185 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002186 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002187 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002188 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2189 return -EFAULT;
2190 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2191 return -EFAULT;
2192 if (put_user(val1, _tlv + 2))
2193 return -EFAULT;
2194 if (put_user(val2, _tlv + 3))
2195 return -EFAULT;
2196 return 0;
2197}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002198EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002199
Takashi Iwaid5191e52009-11-16 14:58:17 +01002200/**
2201 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2202 * @codec: HD-audio codec
2203 * @nid: NID of a reference widget
2204 * @dir: #HDA_INPUT or #HDA_OUTPUT
2205 * @tlv: TLV data to be stored, at least 4 elements
2206 *
2207 * Set (static) TLV data for a virtual master volume using the AMP caps
2208 * obtained from the reference NID.
2209 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002210 */
2211void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2212 unsigned int *tlv)
2213{
2214 u32 caps;
2215 int nums, step;
2216
2217 caps = query_amp_caps(codec, nid, dir);
2218 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2219 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2220 step = (step + 1) * 25;
2221 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2222 tlv[1] = 2 * sizeof(unsigned int);
2223 tlv[2] = -nums * step;
2224 tlv[3] = step;
2225}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002226EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002227
2228/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002229static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002230find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002231{
2232 struct snd_ctl_elem_id id;
2233 memset(&id, 0, sizeof(id));
2234 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002235 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002236 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002237 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2238 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002239 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01002240 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002241}
2242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2245 * @codec: HD-audio codec
2246 * @name: ctl id name string
2247 *
2248 * Get the control element with the given id string and IFACE_MIXER.
2249 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002250struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2251 const char *name)
2252{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002253 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002254}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002255EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002256
Takashi Iwaidcda5802012-10-12 17:24:51 +02002257static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002258 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002259{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002260 int i, idx;
2261 /* 16 ctlrs should be large enough */
2262 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2263 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002264 return idx;
2265 }
2266 return -EBUSY;
2267}
2268
Takashi Iwaid5191e52009-11-16 14:58:17 +01002269/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002270 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002271 * @codec: HD-audio codec
2272 * @nid: corresponding NID (optional)
2273 * @kctl: the control element to assign
2274 *
2275 * Add the given control element to an array inside the codec instance.
2276 * All control elements belonging to a codec are supposed to be added
2277 * by this function so that a proper clean-up works at the free or
2278 * reconfiguration time.
2279 *
2280 * If non-zero @nid is passed, the NID is assigned to the control element.
2281 * The assignment is shown in the codec proc file.
2282 *
2283 * snd_hda_ctl_add() checks the control subdev id field whether
2284 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002285 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2286 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002287 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002288int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2289 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002290{
2291 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002292 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002293 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002294
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002295 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002296 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002297 if (nid == 0)
2298 nid = get_amp_nid_(kctl->private_value);
2299 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002300 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2301 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002302 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002303 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002304 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002305 if (err < 0)
2306 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002307 item = snd_array_new(&codec->mixers);
2308 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002309 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002310 item->kctl = kctl;
2311 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002312 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002313 return 0;
2314}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002315EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002316
Takashi Iwaid5191e52009-11-16 14:58:17 +01002317/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002318 * snd_hda_add_nid - Assign a NID to a control element
2319 * @codec: HD-audio codec
2320 * @nid: corresponding NID (optional)
2321 * @kctl: the control element to assign
2322 * @index: index to kctl
2323 *
2324 * Add the given control element to an array inside the codec instance.
2325 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2326 * NID:KCTL mapping - for example "Capture Source" selector.
2327 */
2328int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2329 unsigned int index, hda_nid_t nid)
2330{
2331 struct hda_nid_item *item;
2332
2333 if (nid > 0) {
2334 item = snd_array_new(&codec->nids);
2335 if (!item)
2336 return -ENOMEM;
2337 item->kctl = kctl;
2338 item->index = index;
2339 item->nid = nid;
2340 return 0;
2341 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002342 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2343 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002344 return -EINVAL;
2345}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002346EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002347
2348/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2350 * @codec: HD-audio codec
2351 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002352void snd_hda_ctls_clear(struct hda_codec *codec)
2353{
2354 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002355 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002356 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01002357 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002358 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002359 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002360}
2361
Takashi Iwai95a962c2014-10-29 16:03:58 +01002362/**
2363 * snd_hda_lock_devices - pseudo device locking
2364 * @bus: the BUS
2365 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002366 * toggle card->shutdown to allow/disallow the device access (as a hack)
2367 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002368int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002369{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002370 struct snd_card *card = bus->card;
2371 struct hda_codec *codec;
2372
Takashi Iwaia65d6292009-02-23 16:57:04 +01002373 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002374 if (card->shutdown)
2375 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002376 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002377 if (!list_empty(&card->ctl_files))
2378 goto err_clear;
2379
2380 list_for_each_entry(codec, &bus->codec_list, list) {
2381 int pcm;
2382 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2383 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2384 if (!cpcm->pcm)
2385 continue;
2386 if (cpcm->pcm->streams[0].substream_opened ||
2387 cpcm->pcm->streams[1].substream_opened)
2388 goto err_clear;
2389 }
2390 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002391 spin_unlock(&card->files_lock);
2392 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002393
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002394 err_clear:
2395 card->shutdown = 0;
2396 err_unlock:
2397 spin_unlock(&card->files_lock);
2398 return -EINVAL;
2399}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002400EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002401
Takashi Iwai95a962c2014-10-29 16:03:58 +01002402/**
2403 * snd_hda_unlock_devices - pseudo device unlocking
2404 * @bus: the BUS
2405 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002406void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002407{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002408 struct snd_card *card = bus->card;
2409
2410 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002411 spin_lock(&card->files_lock);
2412 card->shutdown = 0;
2413 spin_unlock(&card->files_lock);
2414}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002415EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002416
Takashi Iwaid5191e52009-11-16 14:58:17 +01002417/**
2418 * snd_hda_codec_reset - Clear all objects assigned to the codec
2419 * @codec: HD-audio codec
2420 *
2421 * This frees the all PCM and control elements assigned to the codec, and
2422 * clears the caches and restores the pin default configurations.
2423 *
2424 * When a device is being used, it returns -EBSY. If successfully freed,
2425 * returns zero.
2426 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002427int snd_hda_codec_reset(struct hda_codec *codec)
2428{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002429 struct hda_bus *bus = codec->bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +01002430 struct snd_card *card = codec->card;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002431 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002432
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002433 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002435
2436 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002437 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002438 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002439 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002440 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002441 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002442 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002443 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002444 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002445 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002446 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002447 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002448 snd_hda_detach_beep_device(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002449 if (device_is_registered(hda_codec_dev(codec)))
2450 device_del(hda_codec_dev(codec));
2451
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002452 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002453 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002454 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002455 codec->spec = NULL;
2456 free_hda_cache(&codec->amp_cache);
2457 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002458 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2459 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002460 /* free only driver_pins so that init_pins + user_pins are restored */
2461 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002462 snd_array_free(&codec->cvt_setups);
2463 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002464 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002465 codec->num_pcms = 0;
2466 codec->pcm_info = NULL;
2467 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002468 codec->slave_dig_outs = NULL;
2469 codec->spdif_status_reset = 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002470
2471 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002472 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002473 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002474}
2475
Takashi Iwai6194b992014-06-06 18:12:16 +02002476typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002477
2478/* apply the function to all matching slave ctls in the mixer list */
2479static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002480 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002481{
2482 struct hda_nid_item *items;
2483 const char * const *s;
2484 int i, err;
2485
2486 items = codec->mixers.list;
2487 for (i = 0; i < codec->mixers.used; i++) {
2488 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002489 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002490 continue;
2491 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002492 char tmpname[sizeof(sctl->id.name)];
2493 const char *name = *s;
2494 if (suffix) {
2495 snprintf(tmpname, sizeof(tmpname), "%s %s",
2496 name, suffix);
2497 name = tmpname;
2498 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002499 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002500 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002501 if (err)
2502 return err;
2503 break;
2504 }
2505 }
2506 }
2507 return 0;
2508}
2509
Takashi Iwai6194b992014-06-06 18:12:16 +02002510static int check_slave_present(struct hda_codec *codec,
2511 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002512{
2513 return 1;
2514}
2515
Takashi Iwai18478e82012-03-09 17:51:10 +01002516/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002517static int get_kctl_0dB_offset(struct hda_codec *codec,
2518 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002519{
2520 int _tlv[4];
2521 const int *tlv = NULL;
2522 int val = -1;
2523
2524 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2525 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2526 mm_segment_t fs = get_fs();
2527 set_fs(get_ds());
2528 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2529 tlv = _tlv;
2530 set_fs(fs);
2531 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2532 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002533 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2534 int step = tlv[3];
2535 step &= ~TLV_DB_SCALE_MUTE;
2536 if (!step)
2537 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002538 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002539 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002540- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002541 return -1;
2542 }
2543 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002544 val = -tlv[2] / step;
2545 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002546 return val;
2547}
2548
2549/* call kctl->put with the given value(s) */
2550static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2551{
2552 struct snd_ctl_elem_value *ucontrol;
2553 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2554 if (!ucontrol)
2555 return -ENOMEM;
2556 ucontrol->value.integer.value[0] = val;
2557 ucontrol->value.integer.value[1] = val;
2558 kctl->put(kctl, ucontrol);
2559 kfree(ucontrol);
2560 return 0;
2561}
2562
2563/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002564static int init_slave_0dB(struct hda_codec *codec,
2565 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002566{
Takashi Iwai6194b992014-06-06 18:12:16 +02002567 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002568 if (offset > 0)
2569 put_kctl_with_value(slave, offset);
2570 return 0;
2571}
2572
2573/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002574static int init_slave_unmute(struct hda_codec *codec,
2575 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002576{
2577 return put_kctl_with_value(slave, 1);
2578}
2579
Takashi Iwaie8750942014-06-30 14:02:39 +02002580static int add_slave(struct hda_codec *codec,
2581 void *data, struct snd_kcontrol *slave)
2582{
2583 return snd_ctl_add_slave(data, slave);
2584}
2585
Takashi Iwaid5191e52009-11-16 14:58:17 +01002586/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002587 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002588 * @codec: HD-audio codec
2589 * @name: vmaster control name
2590 * @tlv: TLV data (optional)
2591 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002592 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002593 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002594 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002595 *
2596 * Create a virtual master control with the given name. The TLV data
2597 * must be either NULL or a valid data.
2598 *
2599 * @slaves is a NULL-terminated array of strings, each of which is a
2600 * slave control name. All controls with these names are assigned to
2601 * the new virtual master control.
2602 *
2603 * This function returns zero if successful or a negative error code.
2604 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002605int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002606 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002607 const char *suffix, bool init_slave_vol,
2608 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002609{
2610 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002611 int err;
2612
Takashi Iwai29e58532012-03-12 12:25:03 +01002613 if (ctl_ret)
2614 *ctl_ret = NULL;
2615
Takashi Iwai9322ca52012-02-03 14:28:01 +01002616 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002617 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002618 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002619 return 0;
2620 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002621 kctl = snd_ctl_make_virtual_master(name, tlv);
2622 if (!kctl)
2623 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002624 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002625 if (err < 0)
2626 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002627
Takashi Iwaie8750942014-06-30 14:02:39 +02002628 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002629 if (err < 0)
2630 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002631
2632 /* init with master mute & zero volume */
2633 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002634 if (init_slave_vol) {
2635 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002636 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01002637 tlv ? init_slave_0dB : init_slave_unmute, &step);
2638 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002639
Takashi Iwai29e58532012-03-12 12:25:03 +01002640 if (ctl_ret)
2641 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002642 return 0;
2643}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002644EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002645
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002646/*
2647 * mute-LED control using vmaster
2648 */
2649static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
2651{
2652 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002653 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002654 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002655
Takashi Iwai3ff72212014-10-20 18:17:28 +02002656 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002657}
2658
2659static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2661{
2662 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2663 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2664 return 0;
2665}
2666
2667static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
2669{
2670 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2671 unsigned int old_mode = hook->mute_mode;
2672
2673 hook->mute_mode = ucontrol->value.enumerated.item[0];
2674 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2675 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2676 if (old_mode == hook->mute_mode)
2677 return 0;
2678 snd_hda_sync_vmaster_hook(hook);
2679 return 1;
2680}
2681
2682static struct snd_kcontrol_new vmaster_mute_mode = {
2683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2684 .name = "Mute-LED Mode",
2685 .info = vmaster_mute_mode_info,
2686 .get = vmaster_mute_mode_get,
2687 .put = vmaster_mute_mode_put,
2688};
2689
Takashi Iwai95a962c2014-10-29 16:03:58 +01002690/**
2691 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2692 * @codec: the HDA codec
2693 * @hook: the vmaster hook object
2694 * @expose_enum_ctl: flag to create an enum ctl
2695 *
2696 * Add a mute-LED hook with the given vmaster switch kctl.
2697 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2698 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002699 */
2700int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002701 struct hda_vmaster_mute_hook *hook,
2702 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002703{
2704 struct snd_kcontrol *kctl;
2705
2706 if (!hook->hook || !hook->sw_kctl)
2707 return 0;
2708 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2709 hook->codec = codec;
2710 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002711 if (!expose_enum_ctl)
2712 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002713 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2714 if (!kctl)
2715 return -ENOMEM;
2716 return snd_hda_ctl_add(codec, 0, kctl);
2717}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002718EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002719
Takashi Iwai95a962c2014-10-29 16:03:58 +01002720/**
2721 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2722 * @hook: the vmaster hook
2723 *
2724 * Call the hook with the current value for synchronization.
2725 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002726 */
2727void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2728{
2729 if (!hook->hook || !hook->codec)
2730 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002731 /* don't call vmaster hook in the destructor since it might have
2732 * been already destroyed
2733 */
2734 if (hook->codec->bus->shutdown)
2735 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002736 switch (hook->mute_mode) {
2737 case HDA_VMUTE_FOLLOW_MASTER:
2738 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2739 break;
2740 default:
2741 hook->hook(hook->codec, hook->mute_mode);
2742 break;
2743 }
2744}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002745EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002746
2747
Takashi Iwaid5191e52009-11-16 14:58:17 +01002748/**
2749 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002750 * @kcontrol: referred ctl element
2751 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002752 *
2753 * The control element is supposed to have the private_value field
2754 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2755 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002756int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 int chs = get_amp_channels(kcontrol);
2760
2761 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2762 uinfo->count = chs == 3 ? 2 : 1;
2763 uinfo->value.integer.min = 0;
2764 uinfo->value.integer.max = 1;
2765 return 0;
2766}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002767EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Takashi Iwaid5191e52009-11-16 14:58:17 +01002769/**
2770 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002771 * @kcontrol: ctl element
2772 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002773 *
2774 * The control element is supposed to have the private_value field
2775 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2776 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2781 hda_nid_t nid = get_amp_nid(kcontrol);
2782 int chs = get_amp_channels(kcontrol);
2783 int dir = get_amp_direction(kcontrol);
2784 int idx = get_amp_index(kcontrol);
2785 long *valp = ucontrol->value.integer.value;
2786
2787 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002788 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002789 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002791 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002792 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return 0;
2794}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002795EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Takashi Iwaid5191e52009-11-16 14:58:17 +01002797/**
2798 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002799 * @kcontrol: ctl element
2800 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002801 *
2802 * The control element is supposed to have the private_value field
2803 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2804 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002805int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2809 hda_nid_t nid = get_amp_nid(kcontrol);
2810 int chs = get_amp_channels(kcontrol);
2811 int dir = get_amp_direction(kcontrol);
2812 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 long *valp = ucontrol->value.integer.value;
2814 int change = 0;
2815
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002816 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002817 change = codec_amp_update(codec, nid, 0, dir, idx,
2818 HDA_AMP_MUTE,
2819 *valp ? 0 : HDA_AMP_MUTE, false,
2820 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002821 valp++;
2822 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002823 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002824 change |= codec_amp_update(codec, nid, 1, dir, idx,
2825 HDA_AMP_MUTE,
2826 *valp ? 0 : HDA_AMP_MUTE, false,
2827 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002828 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return change;
2830}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002831EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833/*
Takashi Iwai985be542005-11-02 18:26:49 +01002834 * bound volume controls
2835 *
2836 * bind multiple volumes (# indices, from 0)
2837 */
2838
2839#define AMP_VAL_IDX_SHIFT 19
2840#define AMP_VAL_IDX_MASK (0x0f<<19)
2841
Takashi Iwaid5191e52009-11-16 14:58:17 +01002842/**
2843 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002844 * @kcontrol: ctl element
2845 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002846 *
2847 * The control element is supposed to have the private_value field
2848 * set up via HDA_BIND_MUTE*() macros.
2849 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002850int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002852{
2853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2854 unsigned long pval;
2855 int err;
2856
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002857 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002858 pval = kcontrol->private_value;
2859 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2860 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2861 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002862 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002863 return err;
2864}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002865EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002866
Takashi Iwaid5191e52009-11-16 14:58:17 +01002867/**
2868 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002869 * @kcontrol: ctl element
2870 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002871 *
2872 * The control element is supposed to have the private_value field
2873 * set up via HDA_BIND_MUTE*() macros.
2874 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002875int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002877{
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 unsigned long pval;
2880 int i, indices, err = 0, change = 0;
2881
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002882 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002883 pval = kcontrol->private_value;
2884 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2885 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2887 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002888 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2889 if (err < 0)
2890 break;
2891 change |= err;
2892 }
2893 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002894 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002895 return err < 0 ? err : change;
2896}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002897EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002898
Takashi Iwaid5191e52009-11-16 14:58:17 +01002899/**
2900 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002901 * @kcontrol: referred ctl element
2902 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002903 *
2904 * The control element is supposed to have the private_value field
2905 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002906 */
2907int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_info *uinfo)
2909{
2910 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2911 struct hda_bind_ctls *c;
2912 int err;
2913
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002914 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002915 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002916 kcontrol->private_value = *c->values;
2917 err = c->ops->info(kcontrol, uinfo);
2918 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002919 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002920 return err;
2921}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002922EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002923
Takashi Iwaid5191e52009-11-16 14:58:17 +01002924/**
2925 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002926 * @kcontrol: ctl element
2927 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002928 *
2929 * The control element is supposed to have the private_value field
2930 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2931 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002932int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2934{
2935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2936 struct hda_bind_ctls *c;
2937 int err;
2938
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002939 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002940 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002941 kcontrol->private_value = *c->values;
2942 err = c->ops->get(kcontrol, ucontrol);
2943 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002944 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002945 return err;
2946}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002947EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002948
Takashi Iwaid5191e52009-11-16 14:58:17 +01002949/**
2950 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002951 * @kcontrol: ctl element
2952 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002953 *
2954 * The control element is supposed to have the private_value field
2955 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2956 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002957int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_value *ucontrol)
2959{
2960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2961 struct hda_bind_ctls *c;
2962 unsigned long *vals;
2963 int err = 0, change = 0;
2964
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002965 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002966 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002967 for (vals = c->values; *vals; vals++) {
2968 kcontrol->private_value = *vals;
2969 err = c->ops->put(kcontrol, ucontrol);
2970 if (err < 0)
2971 break;
2972 change |= err;
2973 }
2974 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002975 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002976 return err < 0 ? err : change;
2977}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002978EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002979
Takashi Iwaid5191e52009-11-16 14:58:17 +01002980/**
2981 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002982 * @kcontrol: ctl element
2983 * @op_flag: operation flag
2984 * @size: byte size of input TLV
2985 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002986 *
2987 * The control element is supposed to have the private_value field
2988 * set up via HDA_BIND_VOL() macro.
2989 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002990int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2991 unsigned int size, unsigned int __user *tlv)
2992{
2993 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2994 struct hda_bind_ctls *c;
2995 int err;
2996
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002997 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002998 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002999 kcontrol->private_value = *c->values;
3000 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3001 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003002 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003003 return err;
3004}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003005EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003006
3007struct hda_ctl_ops snd_hda_bind_vol = {
3008 .info = snd_hda_mixer_amp_volume_info,
3009 .get = snd_hda_mixer_amp_volume_get,
3010 .put = snd_hda_mixer_amp_volume_put,
3011 .tlv = snd_hda_mixer_amp_tlv
3012};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003013EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003014
3015struct hda_ctl_ops snd_hda_bind_sw = {
3016 .info = snd_hda_mixer_amp_switch_info,
3017 .get = snd_hda_mixer_amp_switch_get,
3018 .put = snd_hda_mixer_amp_switch_put,
3019 .tlv = snd_hda_mixer_amp_tlv
3020};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003021EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003022
3023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 * SPDIF out controls
3025 */
3026
Takashi Iwai0ba21762007-04-16 11:29:14 +02003027static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3031 uinfo->count = 1;
3032 return 0;
3033}
3034
Takashi Iwai0ba21762007-04-16 11:29:14 +02003035static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3036 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3039 IEC958_AES0_NONAUDIO |
3040 IEC958_AES0_CON_EMPHASIS_5015 |
3041 IEC958_AES0_CON_NOT_COPYRIGHT;
3042 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3043 IEC958_AES1_CON_ORIGINAL;
3044 return 0;
3045}
3046
Takashi Iwai0ba21762007-04-16 11:29:14 +02003047static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3051 IEC958_AES0_NONAUDIO |
3052 IEC958_AES0_PRO_EMPHASIS_5015;
3053 return 0;
3054}
3055
Takashi Iwai0ba21762007-04-16 11:29:14 +02003056static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3057 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
3059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003060 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003061 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003063 mutex_lock(&codec->spdif_mutex);
3064 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003065 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3066 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3067 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3068 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003069 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 return 0;
3072}
3073
3074/* convert from SPDIF status bits to HDA SPDIF bits
3075 * bit 0 (DigEn) is always set zero (to be filled later)
3076 */
3077static unsigned short convert_from_spdif_status(unsigned int sbits)
3078{
3079 unsigned short val = 0;
3080
3081 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003084 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003086 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3087 IEC958_AES0_PRO_EMPHASIS_5015)
3088 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3091 IEC958_AES0_CON_EMPHASIS_5015)
3092 val |= AC_DIG1_EMPHASIS;
3093 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3094 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003096 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3098 }
3099 return val;
3100}
3101
3102/* convert to SPDIF status bits from HDA SPDIF bits
3103 */
3104static unsigned int convert_to_spdif_status(unsigned short val)
3105{
3106 unsigned int sbits = 0;
3107
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 sbits |= IEC958_AES0_PROFESSIONAL;
3112 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003113 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3115 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003116 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3122 sbits |= val & (0x7f << 8);
3123 }
3124 return sbits;
3125}
3126
Takashi Iwai2f728532008-09-25 16:32:41 +02003127/* set digital convert verbs both for the given NID and its slaves */
3128static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3129 int verb, int val)
3130{
Takashi Iwaidda14412011-05-02 11:29:30 +02003131 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003132
Takashi Iwai9e976972008-11-25 08:17:20 +01003133 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003134 d = codec->slave_dig_outs;
3135 if (!d)
3136 return;
3137 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003138 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003139}
3140
3141static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3142 int dig1, int dig2)
3143{
3144 if (dig1 != -1)
3145 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3146 if (dig2 != -1)
3147 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3148}
3149
Takashi Iwai0ba21762007-04-16 11:29:14 +02003150static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003154 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003155 struct hda_spdif_out *spdif;
3156 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 unsigned short val;
3158 int change;
3159
Ingo Molnar62932df2006-01-16 16:34:20 +01003160 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003161 spdif = snd_array_elem(&codec->spdif_out, idx);
3162 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003163 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3165 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3166 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003167 val = convert_from_spdif_status(spdif->status);
3168 val |= spdif->ctls & 1;
3169 change = spdif->ctls != val;
3170 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003171 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003172 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003173 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return change;
3175}
3176
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003177#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Takashi Iwai0ba21762007-04-16 11:29:14 +02003179static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003183 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003184 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003186 mutex_lock(&codec->spdif_mutex);
3187 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003188 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003189 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return 0;
3191}
3192
Stephen Warren74b654c2011-06-01 11:14:18 -06003193static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3194 int dig1, int dig2)
3195{
3196 set_dig_out_convert(codec, nid, dig1, dig2);
3197 /* unmute amp switch (if any) */
3198 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3199 (dig1 & AC_DIG1_ENABLE))
3200 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3201 HDA_AMP_MUTE, 0);
3202}
3203
Takashi Iwai0ba21762007-04-16 11:29:14 +02003204static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003208 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003209 struct hda_spdif_out *spdif;
3210 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 unsigned short val;
3212 int change;
3213
Ingo Molnar62932df2006-01-16 16:34:20 +01003214 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003215 spdif = snd_array_elem(&codec->spdif_out, idx);
3216 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003217 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003219 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003220 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003221 spdif->ctls = val;
3222 if (change && nid != (u16)-1)
3223 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003224 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return change;
3226}
3227
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003228static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 {
3230 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003232 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 .info = snd_hda_spdif_mask_info,
3234 .get = snd_hda_spdif_cmask_get,
3235 },
3236 {
3237 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003239 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 .info = snd_hda_spdif_mask_info,
3241 .get = snd_hda_spdif_pmask_get,
3242 },
3243 {
3244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003245 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 .info = snd_hda_spdif_mask_info,
3247 .get = snd_hda_spdif_default_get,
3248 .put = snd_hda_spdif_default_put,
3249 },
3250 {
3251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003252 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 .info = snd_hda_spdif_out_switch_info,
3254 .get = snd_hda_spdif_out_switch_get,
3255 .put = snd_hda_spdif_out_switch_put,
3256 },
3257 { } /* end */
3258};
3259
3260/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003261 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003263 * @associated_nid: NID that new ctls associated with
3264 * @cvt_nid: converter NID
3265 * @type: HDA_PCM_TYPE_*
3266 * Creates controls related with the digital output.
3267 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 *
3269 * Returns 0 if successful, or a negative error code.
3270 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003271int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3272 hda_nid_t associated_nid,
3273 hda_nid_t cvt_nid,
3274 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003277 struct snd_kcontrol *kctl;
3278 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003279 int idx = 0;
3280 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003281 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003282 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003284 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003285 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003286 idx = spdif_index;
3287 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003288 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003289 /* suppose a single SPDIF device */
3290 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3291 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3292 if (!kctl)
3293 break;
3294 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003295 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003296 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003297 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003298 if (!bus->primary_dig_out_type)
3299 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003300
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003301 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003302 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003303 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003304 return -EBUSY;
3305 }
Stephen Warren7c935972011-06-01 11:14:17 -06003306 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003307 if (!spdif)
3308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3310 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003311 if (!kctl)
3312 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003313 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003314 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003315 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003316 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return err;
3318 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003319 spdif->nid = cvt_nid;
3320 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003321 AC_VERB_GET_DIGI_CONVERT_1, 0);
3322 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return 0;
3324}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003325EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Takashi Iwai95a962c2014-10-29 16:03:58 +01003327/**
3328 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3329 * @codec: the HDA codec
3330 * @nid: widget NID
3331 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003332 * call within spdif_mutex lock
3333 */
Stephen Warren7c935972011-06-01 11:14:17 -06003334struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3335 hda_nid_t nid)
3336{
3337 int i;
3338 for (i = 0; i < codec->spdif_out.used; i++) {
3339 struct hda_spdif_out *spdif =
3340 snd_array_elem(&codec->spdif_out, i);
3341 if (spdif->nid == nid)
3342 return spdif;
3343 }
3344 return NULL;
3345}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003346EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06003347
Takashi Iwai95a962c2014-10-29 16:03:58 +01003348/**
3349 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3350 * @codec: the HDA codec
3351 * @idx: the SPDIF ctl index
3352 *
3353 * Unassign the widget from the given SPDIF control.
3354 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003355void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3356{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003357 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003358
3359 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003360 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003361 spdif->nid = (u16)-1;
3362 mutex_unlock(&codec->spdif_mutex);
3363}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003364EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003365
Takashi Iwai95a962c2014-10-29 16:03:58 +01003366/**
3367 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3368 * @codec: the HDA codec
3369 * @idx: the SPDIF ctl idx
3370 * @nid: widget NID
3371 *
3372 * Assign the widget to the SPDIF control with the given index.
3373 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003374void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3375{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003376 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003377 unsigned short val;
3378
3379 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003380 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003381 if (spdif->nid != nid) {
3382 spdif->nid = nid;
3383 val = spdif->ctls;
3384 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3385 }
3386 mutex_unlock(&codec->spdif_mutex);
3387}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003388EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003391 * SPDIF sharing with analog output
3392 */
3393static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3395{
3396 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3397 ucontrol->value.integer.value[0] = mout->share_spdif;
3398 return 0;
3399}
3400
3401static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3403{
3404 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3405 mout->share_spdif = !!ucontrol->value.integer.value[0];
3406 return 0;
3407}
3408
3409static struct snd_kcontrol_new spdif_share_sw = {
3410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3411 .name = "IEC958 Default PCM Playback Switch",
3412 .info = snd_ctl_boolean_mono_info,
3413 .get = spdif_share_sw_get,
3414 .put = spdif_share_sw_put,
3415};
3416
Takashi Iwaid5191e52009-11-16 14:58:17 +01003417/**
3418 * snd_hda_create_spdif_share_sw - create Default PCM switch
3419 * @codec: the HDA codec
3420 * @mout: multi-out instance
3421 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003422int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3423 struct hda_multi_out *mout)
3424{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003425 struct snd_kcontrol *kctl;
3426
Takashi Iwai9a081602008-02-12 18:37:26 +01003427 if (!mout->dig_out_nid)
3428 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003429
3430 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3431 if (!kctl)
3432 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003433 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003434 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003435}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003436EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003437
3438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 * SPDIF input
3440 */
3441
3442#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3443
Takashi Iwai0ba21762007-04-16 11:29:14 +02003444static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3445 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
3447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3448
3449 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3450 return 0;
3451}
3452
Takashi Iwai0ba21762007-04-16 11:29:14 +02003453static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3457 hda_nid_t nid = kcontrol->private_value;
3458 unsigned int val = !!ucontrol->value.integer.value[0];
3459 int change;
3460
Ingo Molnar62932df2006-01-16 16:34:20 +01003461 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003463 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003465 snd_hda_codec_write_cache(codec, nid, 0,
3466 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003468 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return change;
3470}
3471
Takashi Iwai0ba21762007-04-16 11:29:14 +02003472static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3476 hda_nid_t nid = kcontrol->private_value;
3477 unsigned short val;
3478 unsigned int sbits;
3479
Andrew Paprocki3982d172007-12-19 12:13:44 +01003480 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 sbits = convert_to_spdif_status(val);
3482 ucontrol->value.iec958.status[0] = sbits;
3483 ucontrol->value.iec958.status[1] = sbits >> 8;
3484 ucontrol->value.iec958.status[2] = sbits >> 16;
3485 ucontrol->value.iec958.status[3] = sbits >> 24;
3486 return 0;
3487}
3488
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003489static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 {
3491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003492 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 .info = snd_hda_spdif_in_switch_info,
3494 .get = snd_hda_spdif_in_switch_get,
3495 .put = snd_hda_spdif_in_switch_put,
3496 },
3497 {
3498 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003500 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 .info = snd_hda_spdif_mask_info,
3502 .get = snd_hda_spdif_in_status_get,
3503 },
3504 { } /* end */
3505};
3506
3507/**
3508 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3509 * @codec: the HDA codec
3510 * @nid: audio in widget NID
3511 *
3512 * Creates controls related with the SPDIF input.
3513 * Called from each patch supporting the SPDIF in.
3514 *
3515 * Returns 0 if successful, or a negative error code.
3516 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003517int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003520 struct snd_kcontrol *kctl;
3521 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003522 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Takashi Iwaidcda5802012-10-12 17:24:51 +02003524 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003525 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003526 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003527 return -EBUSY;
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3530 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003531 if (!kctl)
3532 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003534 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003535 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return err;
3537 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003538 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003539 snd_hda_codec_read(codec, nid, 0,
3540 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003541 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return 0;
3543}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003544EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003546/*
3547 * command cache
3548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Takashi Iwaic370dd62012-12-13 18:30:04 +01003550/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003551#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3552#define get_cmd_cache_nid(key) ((key) & 0xff)
3553#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3554
3555/**
3556 * snd_hda_codec_write_cache - send a single command with caching
3557 * @codec: the HDA codec
3558 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003559 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003560 * @verb: the verb to send
3561 * @parm: the parameter for the verb
3562 *
3563 * Send a single command without waiting for response.
3564 *
3565 * Returns 0 if successful, or a negative error code.
3566 */
3567int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003568 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003569{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003570 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003571 struct hda_cache_head *c;
3572 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003573 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003574
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003575 cache_only = codec->cached_write;
3576 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003577 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003578 if (err < 0)
3579 return err;
3580 }
3581
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003582 /* parm may contain the verb stuff for get/set amp */
3583 verb = verb | (parm >> 8);
3584 parm &= 0xff;
3585 key = build_cmd_cache_key(nid, verb);
3586 mutex_lock(&codec->bus->cmd_mutex);
3587 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003588 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003589 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003590 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003591 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003592 mutex_unlock(&codec->bus->cmd_mutex);
3593 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003594}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003595EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003596
Takashi Iwaid5191e52009-11-16 14:58:17 +01003597/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003598 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3599 * @codec: the HDA codec
3600 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003601 * @flags: optional bit flags
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003602 * @verb: the verb to send
3603 * @parm: the parameter for the verb
3604 *
3605 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3606 * command if the parameter is already identical with the cached value.
3607 * If not, it sends the command and refreshes the cache.
3608 *
3609 * Returns 0 if successful, or a negative error code.
3610 */
3611int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003612 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003613{
3614 struct hda_cache_head *c;
3615 u32 key;
3616
3617 /* parm may contain the verb stuff for get/set amp */
3618 verb = verb | (parm >> 8);
3619 parm &= 0xff;
3620 key = build_cmd_cache_key(nid, verb);
3621 mutex_lock(&codec->bus->cmd_mutex);
3622 c = get_hash(&codec->cmd_cache, key);
3623 if (c && c->val == parm) {
3624 mutex_unlock(&codec->bus->cmd_mutex);
3625 return 0;
3626 }
3627 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003628 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003629}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003630EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003631
3632/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003633 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3634 * @codec: HD-audio codec
3635 *
3636 * Execute all verbs recorded in the command caches to resume.
3637 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003638void snd_hda_codec_resume_cache(struct hda_codec *codec)
3639{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003640 int i;
3641
Takashi Iwaic370dd62012-12-13 18:30:04 +01003642 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003643 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003644 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3645 struct hda_cache_head *buffer;
3646 u32 key;
3647
3648 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3649 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003650 if (!key)
3651 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003652 if (!buffer->dirty)
3653 continue;
3654 buffer->dirty = 0;
3655 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003656 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3657 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003658 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003659 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003660 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003661}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003662EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003663
3664/**
3665 * snd_hda_sequence_write_cache - sequence writes with caching
3666 * @codec: the HDA codec
3667 * @seq: VERB array to send
3668 *
3669 * Send the commands sequentially from the given array.
3670 * Thte commands are recorded on cache for power-save and resume.
3671 * The array must be terminated with NID=0.
3672 */
3673void snd_hda_sequence_write_cache(struct hda_codec *codec,
3674 const struct hda_verb *seq)
3675{
3676 for (; seq->nid; seq++)
3677 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3678 seq->param);
3679}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003680EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003681
Takashi Iwaidc870f32013-01-22 15:24:30 +01003682/**
3683 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3684 * @codec: HD-audio codec
3685 */
3686void snd_hda_codec_flush_cache(struct hda_codec *codec)
3687{
3688 snd_hda_codec_resume_amp(codec);
3689 snd_hda_codec_resume_cache(codec);
3690}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003691EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003692
Takashi Iwai95a962c2014-10-29 16:03:58 +01003693/**
3694 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3695 * @codec: the HDA codec
3696 * @fg: function group (not used now)
3697 * @power_state: the power state to set (AC_PWRST_*)
3698 *
3699 * Set the given power state to all widgets that have the power control.
3700 * If the codec has power_filter set, it evaluates the power state and
3701 * filter out if it's unchanged as D3.
3702 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003703void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003704 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003705{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003706 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003707 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003708
Takashi Iwaicb53c622007-08-10 17:21:45 +02003709 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003710 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003711 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003712 if (!(wcaps & AC_WCAP_POWER))
3713 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003714 if (codec->power_filter) {
3715 state = codec->power_filter(codec, nid, power_state);
3716 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003717 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003718 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003719 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003720 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003721 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003722}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003723EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724
3725/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003726 * supported power states check
3727 */
3728static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3729 unsigned int power_state)
3730{
3731 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3732
Mengdong Line037cb42012-08-10 14:11:58 +02003733 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003734 return false;
3735 if (sup & power_state)
3736 return true;
3737 else
3738 return false;
3739}
3740
3741/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003742 * wait until the state is reached, returns the current state
3743 */
3744static unsigned int hda_sync_power_state(struct hda_codec *codec,
3745 hda_nid_t fg,
3746 unsigned int power_state)
3747{
3748 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3749 unsigned int state, actual_state;
3750
3751 for (;;) {
3752 state = snd_hda_codec_read(codec, fg, 0,
3753 AC_VERB_GET_POWER_STATE, 0);
3754 if (state & AC_PWRST_ERROR)
3755 break;
3756 actual_state = (state >> 4) & 0x0f;
3757 if (actual_state == power_state)
3758 break;
3759 if (time_after_eq(jiffies, end_time))
3760 break;
3761 /* wait until the codec reachs to the target state */
3762 msleep(1);
3763 }
3764 return state;
3765}
3766
Takashi Iwai95a962c2014-10-29 16:03:58 +01003767/**
3768 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3769 * @codec: the HDA codec
3770 * @nid: widget NID
3771 * @power_state: power state to evalue
3772 *
3773 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3774 * This can be used a codec power_filter callback.
3775 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003776unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3777 hda_nid_t nid,
3778 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003779{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003780 if (nid == codec->afg || nid == codec->mfg)
3781 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003782 if (power_state == AC_PWRST_D3 &&
3783 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3784 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3785 int eapd = snd_hda_codec_read(codec, nid, 0,
3786 AC_VERB_GET_EAPD_BTLENABLE, 0);
3787 if (eapd & 0x02)
3788 return AC_PWRST_D0;
3789 }
3790 return power_state;
3791}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003792EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003793
Takashi Iwai432c6412012-08-28 09:59:20 -07003794/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003795 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003796 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003797static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003798 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003799{
Takashi Iwaid8193872012-08-31 07:54:38 -07003800 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003801 int count;
3802 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003803 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003804
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003805 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003806 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003807 if (codec->depop_delay < 0)
3808 msleep(codec->epss ? 10 : 100);
3809 else if (codec->depop_delay > 0)
3810 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02003811 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003812 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003813
3814 /* repeat power states setting at most 10 times*/
3815 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003816 if (codec->patch_ops.set_power_state)
3817 codec->patch_ops.set_power_state(codec, fg,
3818 power_state);
3819 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003820 state = power_state;
3821 if (codec->power_filter)
3822 state = codec->power_filter(codec, fg, state);
3823 if (state == power_state || power_state != AC_PWRST_D3)
3824 snd_hda_codec_read(codec, fg, flags,
3825 AC_VERB_SET_POWER_STATE,
3826 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003827 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003828 }
3829 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003830 if (!(state & AC_PWRST_ERROR))
3831 break;
3832 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003833
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003834 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003835}
Takashi Iwai54d17402005-11-21 16:33:22 +01003836
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003837/* sync power states of all widgets;
3838 * this is called at the end of codec parsing
3839 */
3840static void sync_power_up_states(struct hda_codec *codec)
3841{
3842 hda_nid_t nid = codec->start_nid;
3843 int i;
3844
Takashi Iwaiba615b82013-03-13 14:47:21 +01003845 /* don't care if no filter is used */
3846 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003847 return;
3848
3849 for (i = 0; i < codec->num_nodes; i++, nid++) {
3850 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003851 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003852 if (!(wcaps & AC_WCAP_POWER))
3853 continue;
3854 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3855 if (target == AC_PWRST_D0)
3856 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003857 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003858 snd_hda_codec_write(codec, nid, 0,
3859 AC_VERB_SET_POWER_STATE, target);
3860 }
3861}
3862
Takashi Iwai648a8d22014-02-25 10:38:13 +01003863#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003864/* execute additional init verbs */
3865static void hda_exec_init_verbs(struct hda_codec *codec)
3866{
3867 if (codec->init_verbs.list)
3868 snd_hda_sequence_write(codec, codec->init_verbs.list);
3869}
3870#else
3871static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3872#endif
3873
Takashi Iwai2a439522011-07-26 09:52:50 +02003874#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003875/* update the power on/off account with the current jiffies */
3876static void update_power_acct(struct hda_codec *codec, bool on)
3877{
3878 unsigned long delta = jiffies - codec->power_jiffies;
3879
3880 if (on)
3881 codec->power_on_acct += delta;
3882 else
3883 codec->power_off_acct += delta;
3884 codec->power_jiffies += delta;
3885}
3886
3887void snd_hda_update_power_acct(struct hda_codec *codec)
3888{
3889 update_power_acct(codec, hda_codec_is_power_on(codec));
3890}
3891
Takashi Iwaicb53c622007-08-10 17:21:45 +02003892/*
3893 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003894 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003895 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003896static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003897{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003898 unsigned int state;
3899
Takashi Iwaicc72da72015-02-19 16:00:22 +01003900 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003901
Takashi Iwaicb53c622007-08-10 17:21:45 +02003902 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003903 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003904 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003905 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003906 trace_hda_power_down(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003907 update_power_acct(codec, true);
3908 atomic_dec(&codec->in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003909 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003910}
3911
Takashi Iwaic370dd62012-12-13 18:30:04 +01003912/* mark all entries of cmd and amp caches dirty */
3913static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3914{
3915 int i;
3916 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3917 struct hda_cache_head *cmd;
3918 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3919 cmd->dirty = 1;
3920 }
3921 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3922 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003923 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003924 amp->head.dirty = 1;
3925 }
3926}
3927
Takashi Iwaicb53c622007-08-10 17:21:45 +02003928/*
3929 * kick up codec; used both from PM and power-save
3930 */
3931static void hda_call_codec_resume(struct hda_codec *codec)
3932{
Takashi Iwaicc72da72015-02-19 16:00:22 +01003933 atomic_inc(&codec->in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003934
Takashi Iwaicc72da72015-02-19 16:00:22 +01003935 trace_hda_power_up(codec);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003936 hda_mark_cmd_cache_dirty(codec);
3937
Takashi Iwaicc72da72015-02-19 16:00:22 +01003938 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003939
Takashi Iwaid8193872012-08-31 07:54:38 -07003940 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003941 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003942 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003943 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003944 if (codec->patch_ops.resume)
3945 codec->patch_ops.resume(codec);
3946 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003947 if (codec->patch_ops.init)
3948 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003949 snd_hda_codec_resume_amp(codec);
3950 snd_hda_codec_resume_cache(codec);
3951 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003952
3953 if (codec->jackpoll_interval)
3954 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003955 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003956 snd_hda_jack_report_sync(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003957 atomic_dec(&codec->in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003958}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003959
Takashi Iwaicc72da72015-02-19 16:00:22 +01003960static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003961{
3962 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003963 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003964 int i;
3965
3966 cancel_delayed_work_sync(&codec->jackpoll_work);
3967 for (i = 0; i < codec->num_pcms; i++)
3968 snd_pcm_suspend_all(codec->pcm_info[i].pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003969 state = hda_call_codec_suspend(codec);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003970 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
3971 clear_bit(codec->addr, &codec->bus->codec_powered);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003972 return 0;
3973}
3974
Takashi Iwaicc72da72015-02-19 16:00:22 +01003975static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003976{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003977 struct hda_codec *codec = dev_to_hda_codec(dev);
3978
3979 set_bit(codec->addr, &codec->bus->codec_powered);
3980 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003981 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003982 return 0;
3983}
Takashi Iwai2a439522011-07-26 09:52:50 +02003984#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003985
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003986/* referred in hda_bind.c */
3987const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003988 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3989 pm_runtime_force_resume)
3990 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3991 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003992};
Takashi Iwai54d17402005-11-21 16:33:22 +01003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994/**
3995 * snd_hda_build_controls - build mixer controls
3996 * @bus: the BUS
3997 *
3998 * Creates mixer controls for each codec included in the bus.
3999 *
4000 * Returns 0 if successful, otherwise a negative error code.
4001 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01004002int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004004 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Takashi Iwai0ba21762007-04-16 11:29:14 +02004006 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004007 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004008 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004009 codec_err(codec,
4010 "cannot build controls for #%d (error %d)\n",
4011 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01004012 err = snd_hda_codec_reset(codec);
4013 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004014 codec_err(codec,
4015 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01004016 return err;
4017 }
4018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004020 return 0;
4021}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004022EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004024/*
4025 * add standard channel maps if not specified
4026 */
4027static int add_std_chmaps(struct hda_codec *codec)
4028{
4029 int i, str, err;
4030
4031 for (i = 0; i < codec->num_pcms; i++) {
4032 for (str = 0; str < 2; str++) {
4033 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4034 struct hda_pcm_stream *hinfo =
4035 &codec->pcm_info[i].stream[str];
4036 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004037 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004038
4039 if (codec->pcm_info[i].own_chmap)
4040 continue;
4041 if (!pcm || !hinfo->substreams)
4042 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004043 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4044 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004045 hinfo->channels_max,
4046 0, &chmap);
4047 if (err < 0)
4048 return err;
4049 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4050 }
4051 }
4052 return 0;
4053}
4054
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004055/* default channel maps for 2.1 speakers;
4056 * since HD-audio supports only stereo, odd number channels are omitted
4057 */
4058const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4059 { .channels = 2,
4060 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4061 { .channels = 4,
4062 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4063 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4064 { }
4065};
4066EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4067
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004068int snd_hda_codec_build_controls(struct hda_codec *codec)
4069{
4070 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004071 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004072 /* continue to initialize... */
4073 if (codec->patch_ops.init)
4074 err = codec->patch_ops.init(codec);
4075 if (!err && codec->patch_ops.build_controls)
4076 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004077 if (err < 0)
4078 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004079
4080 /* we create chmaps here instead of build_pcms */
4081 err = add_std_chmaps(codec);
4082 if (err < 0)
4083 return err;
4084
David Henningsson26a6cb62012-10-09 15:04:21 +02004085 if (codec->jackpoll_interval)
4086 hda_jackpoll_work(&codec->jackpoll_work.work);
4087 else
4088 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004089 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return 0;
4091}
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093/*
4094 * stream formats
4095 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004096struct hda_rate_tbl {
4097 unsigned int hz;
4098 unsigned int alsa_bits;
4099 unsigned int hda_fmt;
4100};
4101
Takashi Iwai92f10b32010-08-03 14:21:00 +02004102/* rate = base * mult / div */
4103#define HDA_RATE(base, mult, div) \
4104 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4105 (((div) - 1) << AC_FMT_DIV_SHIFT))
4106
Takashi Iwaibefdf312005-08-22 13:57:55 +02004107static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004109
4110 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004111 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4112 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4113 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4114 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4115 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4116 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4117 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4118 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4119 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4120 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4121 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004122#define AC_PAR_PCM_RATE_BITS 11
4123 /* up to bits 10, 384kHZ isn't supported properly */
4124
4125 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004126 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004127
Takashi Iwaibefdf312005-08-22 13:57:55 +02004128 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129};
4130
4131/**
4132 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004133 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 * @rate: the sample rate
4135 * @channels: the number of channels
4136 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4137 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004138 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 *
4140 * Calculate the format bitset from the given rate, channels and th PCM format.
4141 *
4142 * Return zero if invalid.
4143 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004144unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4145 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 unsigned int channels,
4147 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004148 unsigned int maxbps,
4149 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 int i;
4152 unsigned int val = 0;
4153
Takashi Iwaibefdf312005-08-22 13:57:55 +02004154 for (i = 0; rate_bits[i].hz; i++)
4155 if (rate_bits[i].hz == rate) {
4156 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 break;
4158 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004159 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004160 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return 0;
4162 }
4163
4164 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004165 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return 0;
4167 }
4168 val |= channels - 1;
4169
4170 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004171 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004172 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004173 break;
4174 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004175 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 case 20:
4178 case 24:
4179 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004180 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004181 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004183 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004185 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 break;
4187 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004188 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004189 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return 0;
4191 }
4192
Anssi Hannula32c168c2010-08-03 13:28:57 +03004193 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004194 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return val;
4197}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004198EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004200static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4201 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004202{
4203 unsigned int val = 0;
4204 if (nid != codec->afg &&
4205 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4206 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4207 if (!val || val == -1)
4208 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4209 if (!val || val == -1)
4210 return 0;
4211 return val;
4212}
4213
4214static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4215{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004216 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004217 get_pcm_param);
4218}
4219
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004220static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4221 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004222{
4223 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4224 if (!streams || streams == -1)
4225 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4226 if (!streams || streams == -1)
4227 return 0;
4228 return streams;
4229}
4230
4231static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4232{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004233 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004234 get_stream_param);
4235}
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237/**
4238 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4239 * @codec: the HDA codec
4240 * @nid: NID to query
4241 * @ratesp: the pointer to store the detected rate bitflags
4242 * @formatsp: the pointer to store the detected formats
4243 * @bpsp: the pointer to store the detected format widths
4244 *
4245 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4246 * or @bsps argument is ignored.
4247 *
4248 * Returns 0 if successful, otherwise a negative error code.
4249 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004250int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4252{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004253 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004255 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004256 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 if (ratesp) {
4259 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004260 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004262 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004264 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004265 codec_err(codec,
4266 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4267 nid, val,
4268 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004269 return -EIO;
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 *ratesp = rates;
4272 }
4273
4274 if (formatsp || bpsp) {
4275 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004276 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004278 streams = query_stream_param(codec, nid);
4279 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 bps = 0;
4283 if (streams & AC_SUPFMT_PCM) {
4284 if (val & AC_SUPPCM_BITS_8) {
4285 formats |= SNDRV_PCM_FMTBIT_U8;
4286 bps = 8;
4287 }
4288 if (val & AC_SUPPCM_BITS_16) {
4289 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4290 bps = 16;
4291 }
4292 if (wcaps & AC_WCAP_DIGITAL) {
4293 if (val & AC_SUPPCM_BITS_32)
4294 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4295 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4296 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4297 if (val & AC_SUPPCM_BITS_24)
4298 bps = 24;
4299 else if (val & AC_SUPPCM_BITS_20)
4300 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004301 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4302 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4304 if (val & AC_SUPPCM_BITS_32)
4305 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 else if (val & AC_SUPPCM_BITS_24)
4307 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004308 else if (val & AC_SUPPCM_BITS_20)
4309 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
4311 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004312#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004313 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004315 if (!bps)
4316 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004317 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004318#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004319 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004320 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /* temporary hack: we have still no proper support
4322 * for the direct AC3 stream...
4323 */
4324 formats |= SNDRV_PCM_FMTBIT_U8;
4325 bps = 8;
4326 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004327 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004328 codec_err(codec,
4329 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4330 nid, val,
4331 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4332 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004333 return -EIO;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (formatsp)
4336 *formatsp = formats;
4337 if (bpsp)
4338 *bpsp = bps;
4339 }
4340
4341 return 0;
4342}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004343EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004346 * snd_hda_is_supported_format - Check the validity of the format
4347 * @codec: HD-audio codec
4348 * @nid: NID to check
4349 * @format: the HD-audio format value to check
4350 *
4351 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 *
4353 * Returns 1 if supported, 0 if not.
4354 */
4355int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4356 unsigned int format)
4357{
4358 int i;
4359 unsigned int val = 0, rate, stream;
4360
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004361 val = query_pcm_param(codec, nid);
4362 if (!val)
4363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004366 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004367 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (val & (1 << i))
4369 break;
4370 return 0;
4371 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004372 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return 0;
4374
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004375 stream = query_stream_param(codec, nid);
4376 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return 0;
4378
4379 if (stream & AC_SUPFMT_PCM) {
4380 switch (format & 0xf0) {
4381 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004382 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return 0;
4384 break;
4385 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004386 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return 0;
4388 break;
4389 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004390 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392 break;
4393 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004394 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return 0;
4396 break;
4397 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004398 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return 0;
4400 break;
4401 default:
4402 return 0;
4403 }
4404 } else {
4405 /* FIXME: check for float32 and AC3? */
4406 }
4407
4408 return 1;
4409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004410EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412/*
4413 * PCM stuff
4414 */
4415static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4416 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004417 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
4419 return 0;
4420}
4421
4422static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4423 struct hda_codec *codec,
4424 unsigned int stream_tag,
4425 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004426 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4429 return 0;
4430}
4431
4432static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4433 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004434 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
Takashi Iwai888afa12008-03-18 09:57:50 +01004436 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 return 0;
4438}
4439
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004440static int set_pcm_default_values(struct hda_codec *codec,
4441 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004443 int err;
4444
Takashi Iwai0ba21762007-04-16 11:29:14 +02004445 /* query support PCM information from the given NID */
4446 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004447 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004448 info->rates ? NULL : &info->rates,
4449 info->formats ? NULL : &info->formats,
4450 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004451 if (err < 0)
4452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454 if (info->ops.open == NULL)
4455 info->ops.open = hda_pcm_default_open_close;
4456 if (info->ops.close == NULL)
4457 info->ops.close = hda_pcm_default_open_close;
4458 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004459 if (snd_BUG_ON(!info->nid))
4460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 info->ops.prepare = hda_pcm_default_prepare;
4462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004464 if (snd_BUG_ON(!info->nid))
4465 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 info->ops.cleanup = hda_pcm_default_cleanup;
4467 }
4468 return 0;
4469}
4470
Takashi Iwaieb541332010-08-06 13:48:11 +02004471/*
4472 * codec prepare/cleanup entries
4473 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004474/**
4475 * snd_hda_codec_prepare - Prepare a stream
4476 * @codec: the HDA codec
4477 * @hinfo: PCM information
4478 * @stream: stream tag to assign
4479 * @format: format id to assign
4480 * @substream: PCM substream to assign
4481 *
4482 * Calls the prepare callback set by the codec with the given arguments.
4483 * Clean up the inactive streams when successful.
4484 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004485int snd_hda_codec_prepare(struct hda_codec *codec,
4486 struct hda_pcm_stream *hinfo,
4487 unsigned int stream,
4488 unsigned int format,
4489 struct snd_pcm_substream *substream)
4490{
4491 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004492 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004493 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4494 if (ret >= 0)
4495 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004496 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004497 return ret;
4498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004499EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004500
Takashi Iwai95a962c2014-10-29 16:03:58 +01004501/**
4502 * snd_hda_codec_cleanup - Prepare a stream
4503 * @codec: the HDA codec
4504 * @hinfo: PCM information
4505 * @substream: PCM substream
4506 *
4507 * Calls the cleanup callback set by the codec with the given arguments.
4508 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004509void snd_hda_codec_cleanup(struct hda_codec *codec,
4510 struct hda_pcm_stream *hinfo,
4511 struct snd_pcm_substream *substream)
4512{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004513 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004514 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004515 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004516}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004517EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004518
Takashi Iwaid5191e52009-11-16 14:58:17 +01004519/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004520const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4521 "Audio", "SPDIF", "HDMI", "Modem"
4522};
4523
Takashi Iwai176d5332008-07-30 15:01:44 +02004524/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004525 * get the empty PCM device number to assign
4526 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004527static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004528{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004529 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004530 /* assigned to static slots up to dev#10; if more needed, assign
4531 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4532 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004533 static int audio_idx[HDA_PCM_NTYPES][5] = {
4534 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4535 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004536 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004537 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004538 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004539 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004540
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004541 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004542 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004543 return -EINVAL;
4544 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004545
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004546 for (i = 0; audio_idx[type][i] >= 0; i++) {
4547#ifndef CONFIG_SND_DYNAMIC_MINORS
4548 if (audio_idx[type][i] >= 8)
4549 break;
4550#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004551 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4552 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004553 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004554
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004555#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004556 /* non-fixed slots starting from 10 */
4557 for (i = 10; i < 32; i++) {
4558 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4559 return i;
4560 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004561#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004562
Takashi Iwai4e76a882014-02-25 12:21:03 +01004563 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004564 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004565#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004566 dev_warn(bus->card->dev,
4567 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004568#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004569 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004570}
4571
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004572/* call build_pcms ops of the given codec and set up the default parameters */
4573int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02004574{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004575 unsigned int pcm;
4576 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02004577
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004578 if (codec->num_pcms)
4579 return 0; /* already parsed */
4580
4581 if (!codec->patch_ops.build_pcms)
4582 return 0;
4583
4584 err = codec->patch_ops.build_pcms(codec);
4585 if (err < 0) {
4586 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4587 codec->addr, err);
4588 return err;
4589 }
4590
4591 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4592 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4593 int stream;
4594
4595 for (stream = 0; stream < 2; stream++) {
4596 struct hda_pcm_stream *info = &cpcm->stream[stream];
4597
4598 if (!info->substreams)
4599 continue;
4600 if (snd_BUG_ON(!cpcm->name))
4601 return -EINVAL;
Takashi Iwai176d5332008-07-30 15:01:44 +02004602 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004603 if (err < 0) {
4604 codec_warn(codec,
4605 "fail to setup default for PCM %s\n",
4606 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02004607 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004608 }
Takashi Iwai176d5332008-07-30 15:01:44 +02004609 }
4610 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004611
4612 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02004613}
4614
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004615/* assign all PCMs of the given codec */
4616int snd_hda_codec_build_pcms(struct hda_codec *codec)
4617{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004618 struct hda_bus *bus = codec->bus;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004619 unsigned int pcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004620 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004621
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004622 if (snd_BUG_ON(!bus->ops.attach_pcm))
4623 return -EINVAL;
4624
4625 err = snd_hda_codec_parse_pcms(codec);
4626 if (err < 0) {
4627 snd_hda_codec_reset(codec);
4628 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004629 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004630
4631 /* attach a new PCM streams */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004632 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4633 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004634
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004635 if (cpcm->pcm)
4636 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004637 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004638 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004639
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004640 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4641 if (dev < 0)
4642 continue; /* no fatal error */
4643 cpcm->device = dev;
4644 err = bus->ops.attach_pcm(bus, codec, cpcm);
4645 if (err < 0) {
4646 codec_err(codec,
4647 "cannot attach PCM stream %d for codec #%d\n",
4648 dev, codec->addr);
4649 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004650 }
4651 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01004652
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004653 return 0;
4654}
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656/**
4657 * snd_hda_build_pcms - build PCM information
4658 * @bus: the BUS
4659 *
4660 * Create PCM information for each codec included in the bus.
4661 *
4662 * The build_pcms codec patch is requested to set up codec->num_pcms and
4663 * codec->pcm_info properly. The array is referred by the top-level driver
4664 * to create its PCM instances.
4665 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4666 * callback.
4667 *
4668 * At least, substreams, channels_min and channels_max must be filled for
4669 * each stream. substreams = 0 indicates that the stream doesn't exist.
4670 * When rates and/or formats are zero, the supported values are queried
4671 * from the given nid. The nid is used also by the default ops.prepare
4672 * and ops.cleanup callbacks.
4673 *
4674 * The driver needs to call ops.open in its open callback. Similarly,
4675 * ops.close is supposed to be called in the close callback.
4676 * ops.prepare should be called in the prepare or hw_params callback
4677 * with the proper parameters for set up.
4678 * ops.cleanup should be called in hw_free for clean up of streams.
4679 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004680 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004682int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004684 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Takashi Iwai0ba21762007-04-16 11:29:14 +02004686 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004687 int err = snd_hda_codec_build_pcms(codec);
4688 if (err < 0)
4689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
4691 return 0;
4692}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004693EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 * snd_hda_add_new_ctls - create controls from the array
4697 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004698 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 *
4700 * This helper function creates and add new controls in the given array.
4701 * The array must be terminated with an empty entry as terminator.
4702 *
4703 * Returns 0 if successful, or a negative error code.
4704 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004705int snd_hda_add_new_ctls(struct hda_codec *codec,
4706 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004711 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004712 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004713 if (knew->iface == -1) /* skip this codec private value */
4714 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004715 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004716 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004717 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004718 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004719 if (addr > 0)
4720 kctl->id.device = addr;
4721 if (idx > 0)
4722 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004723 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004724 if (!err)
4725 break;
4726 /* try first with another device index corresponding to
4727 * the codec addr; if it still fails (or it's the
4728 * primary codec), then try another control index
4729 */
4730 if (!addr && codec->addr)
4731 addr = codec->addr;
4732 else if (!idx && !knew->index) {
4733 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004734 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004735 if (idx <= 0)
4736 return err;
4737 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004738 return err;
4739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
4741 return 0;
4742}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004743EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Takashi Iwai83012a72012-08-24 18:38:08 +02004745#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01004746/**
4747 * snd_hda_power_up - Power-up the codec
4748 * @codec: HD-audio codec
4749 *
4750 * Increment the usage counter and resume the device if not done yet.
4751 */
4752void snd_hda_power_up(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004753{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004754 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004755
Takashi Iwaicc72da72015-02-19 16:00:22 +01004756 if (codec_in_pm(codec))
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004757 return;
Takashi Iwaicc72da72015-02-19 16:00:22 +01004758 pm_runtime_get_sync(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004760EXPORT_SYMBOL_GPL(snd_hda_power_up);
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004761
4762/**
Takashi Iwaicc72da72015-02-19 16:00:22 +01004763 * snd_hda_power_down - Power-down the codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004764 * @codec: HD-audio codec
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004765 *
Takashi Iwaicc72da72015-02-19 16:00:22 +01004766 * Decrement the usage counter and schedules the autosuspend if none used.
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004767 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01004768void snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004769{
Takashi Iwaicc72da72015-02-19 16:00:22 +01004770 struct device *dev = hda_codec_dev(codec);
4771
4772 if (codec_in_pm(codec))
4773 return;
4774 pm_runtime_mark_last_busy(dev);
4775 pm_runtime_put_autosuspend(dev);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004776}
Takashi Iwaicc72da72015-02-19 16:00:22 +01004777EXPORT_SYMBOL_GPL(snd_hda_power_down);
4778
Takashi Iwaibb573922015-02-20 09:26:04 +01004779static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01004780{
4781 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01004782
Takashi Iwaicc72da72015-02-19 16:00:22 +01004783 if (delay > 0) {
4784 pm_runtime_set_autosuspend_delay(dev, delay);
4785 pm_runtime_use_autosuspend(dev);
4786 pm_runtime_allow(dev);
4787 if (!pm_runtime_suspended(dev))
4788 pm_runtime_mark_last_busy(dev);
4789 } else {
4790 pm_runtime_dont_use_autosuspend(dev);
4791 pm_runtime_forbid(dev);
4792 }
4793}
Takashi Iwaibb573922015-02-20 09:26:04 +01004794
4795/**
4796 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4797 * @bus: HD-audio bus
4798 * @delay: autosuspend delay in msec, 0 = off
4799 *
4800 * Synchronize the runtime PM autosuspend state from the power_save option.
4801 */
4802void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4803{
4804 struct hda_codec *c;
4805
4806 list_for_each_entry(c, &bus->codec_list, list)
4807 codec_set_power_save(c, delay);
4808}
4809EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004810
Takashi Iwaid5191e52009-11-16 14:58:17 +01004811/**
4812 * snd_hda_check_amp_list_power - Check the amp list and update the power
4813 * @codec: HD-audio codec
4814 * @check: the object containing an AMP list and the status
4815 * @nid: NID to check / update
4816 *
4817 * Check whether the given NID is in the amp list. If it's in the list,
4818 * check the current AMP status, and update the the power-status according
4819 * to the mute status.
4820 *
4821 * This function is supposed to be set or called from the check_power_status
4822 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004823 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824int snd_hda_check_amp_list_power(struct hda_codec *codec,
4825 struct hda_loopback_check *check,
4826 hda_nid_t nid)
4827{
Takashi Iwai031024e2011-05-02 11:29:30 +02004828 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004829 int ch, v;
4830
4831 if (!check->amplist)
4832 return 0;
4833 for (p = check->amplist; p->nid; p++) {
4834 if (p->nid == nid)
4835 break;
4836 }
4837 if (!p->nid)
4838 return 0; /* nothing changed */
4839
4840 for (p = check->amplist; p->nid; p++) {
4841 for (ch = 0; ch < 2; ch++) {
4842 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4843 p->idx);
4844 if (!(v & HDA_AMP_MUTE) && v > 0) {
4845 if (!check->power_on) {
4846 check->power_on = 1;
4847 snd_hda_power_up(codec);
4848 }
4849 return 1;
4850 }
4851 }
4852 }
4853 if (check->power_on) {
4854 check->power_on = 0;
4855 snd_hda_power_down(codec);
4856 }
4857 return 0;
4858}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004859EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004862/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 * input MUX helper
4864 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004865
4866/**
4867 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004868 * @imux: imux helper object
4869 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004870 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004871int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4872 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
4874 unsigned int index;
4875
4876 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4877 uinfo->count = 1;
4878 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004879 if (!imux->num_items)
4880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 index = uinfo->value.enumerated.item;
4882 if (index >= imux->num_items)
4883 index = imux->num_items - 1;
4884 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4885 return 0;
4886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004887EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Takashi Iwaid5191e52009-11-16 14:58:17 +01004889/**
4890 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004891 * @codec: the HDA codec
4892 * @imux: imux helper object
4893 * @ucontrol: pointer to get/store the data
4894 * @nid: input mux NID
4895 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01004896 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004897int snd_hda_input_mux_put(struct hda_codec *codec,
4898 const struct hda_input_mux *imux,
4899 struct snd_ctl_elem_value *ucontrol,
4900 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 unsigned int *cur_val)
4902{
4903 unsigned int idx;
4904
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004905 if (!imux->num_items)
4906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 idx = ucontrol->value.enumerated.item[0];
4908 if (idx >= imux->num_items)
4909 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004910 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004912 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4913 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 *cur_val = idx;
4915 return 1;
4916}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004917EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004920/**
4921 * snd_hda_enum_helper_info - Helper for simple enum ctls
4922 * @kcontrol: ctl element
4923 * @uinfo: pointer to get/store the data
4924 * @num_items: number of enum items
4925 * @texts: enum item string array
4926 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01004927 * process kcontrol info callback of a simple string enum array
4928 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4929 */
4930int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4931 struct snd_ctl_elem_info *uinfo,
4932 int num_items, const char * const *texts)
4933{
4934 static const char * const texts_default[] = {
4935 "Disabled", "Enabled"
4936 };
4937
4938 if (!texts || !num_items) {
4939 num_items = 2;
4940 texts = texts_default;
4941 }
4942
Takashi Iwai3ff72212014-10-20 18:17:28 +02004943 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004944}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004945EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01004946
4947/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 * Multi-channel / digital-out PCM helper functions
4949 */
4950
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004951/* setup SPDIF output stream */
4952static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4953 unsigned int stream_tag, unsigned int format)
4954{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004955 struct hda_spdif_out *spdif;
4956 unsigned int curr_fmt;
4957 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004958
Laurence Darby3bef1c32012-11-03 17:00:06 +00004959 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4960 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4961 AC_VERB_GET_STREAM_FORMAT, 0);
4962 reset = codec->spdif_status_reset &&
4963 (spdif->ctls & AC_DIG1_ENABLE) &&
4964 curr_fmt != format;
4965
4966 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4967 updated */
4968 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004969 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004970 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004971 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004972 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004973 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004974 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004975 for (d = codec->slave_dig_outs; *d; d++)
4976 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4977 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004978 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004979 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004980 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004981 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004982 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004983}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004984
Takashi Iwai2f728532008-09-25 16:32:41 +02004985static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4986{
4987 snd_hda_codec_cleanup_stream(codec, nid);
4988 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004989 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004990 for (d = codec->slave_dig_outs; *d; d++)
4991 snd_hda_codec_cleanup_stream(codec, *d);
4992 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004993}
4994
Takashi Iwaid5191e52009-11-16 14:58:17 +01004995/**
4996 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4997 * @bus: HD-audio bus
4998 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004999void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5000{
5001 struct hda_codec *codec;
5002
5003 if (!bus)
5004 return;
5005 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005006 if (hda_codec_is_power_on(codec) &&
5007 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005008 codec->patch_ops.reboot_notify(codec);
5009 }
5010}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005011EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005012
Takashi Iwaid5191e52009-11-16 14:58:17 +01005013/**
5014 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005015 * @codec: the HDA codec
5016 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005018int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5019 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Ingo Molnar62932df2006-01-16 16:34:20 +01005021 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005022 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5023 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005024 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005026 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 return 0;
5028}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005029EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Takashi Iwaid5191e52009-11-16 14:58:17 +01005031/**
5032 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005033 * @codec: the HDA codec
5034 * @mout: hda_multi_out object
5035 * @stream_tag: stream tag to assign
5036 * @format: format id to assign
5037 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005038 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005039int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5040 struct hda_multi_out *mout,
5041 unsigned int stream_tag,
5042 unsigned int format,
5043 struct snd_pcm_substream *substream)
5044{
5045 mutex_lock(&codec->spdif_mutex);
5046 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5047 mutex_unlock(&codec->spdif_mutex);
5048 return 0;
5049}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005050EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005051
Takashi Iwaid5191e52009-11-16 14:58:17 +01005052/**
5053 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005054 * @codec: the HDA codec
5055 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005056 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005057int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5058 struct hda_multi_out *mout)
5059{
5060 mutex_lock(&codec->spdif_mutex);
5061 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5062 mutex_unlock(&codec->spdif_mutex);
5063 return 0;
5064}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005065EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005066
Takashi Iwaid5191e52009-11-16 14:58:17 +01005067/**
5068 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005069 * @codec: the HDA codec
5070 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005072int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5073 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
Ingo Molnar62932df2006-01-16 16:34:20 +01005075 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005077 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return 0;
5079}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005080EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Takashi Iwaid5191e52009-11-16 14:58:17 +01005082/**
5083 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005084 * @codec: the HDA codec
5085 * @mout: hda_multi_out object
5086 * @substream: PCM substream to assign
5087 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005088 *
5089 * Open analog outputs and set up the hw-constraints.
5090 * If the digital outputs can be opened as slave, open the digital
5091 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005093int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5094 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005095 struct snd_pcm_substream *substream,
5096 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097{
Takashi Iwai9a081602008-02-12 18:37:26 +01005098 struct snd_pcm_runtime *runtime = substream->runtime;
5099 runtime->hw.channels_max = mout->max_channels;
5100 if (mout->dig_out_nid) {
5101 if (!mout->analog_rates) {
5102 mout->analog_rates = hinfo->rates;
5103 mout->analog_formats = hinfo->formats;
5104 mout->analog_maxbps = hinfo->maxbps;
5105 } else {
5106 runtime->hw.rates = mout->analog_rates;
5107 runtime->hw.formats = mout->analog_formats;
5108 hinfo->maxbps = mout->analog_maxbps;
5109 }
5110 if (!mout->spdif_rates) {
5111 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5112 &mout->spdif_rates,
5113 &mout->spdif_formats,
5114 &mout->spdif_maxbps);
5115 }
5116 mutex_lock(&codec->spdif_mutex);
5117 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005118 if ((runtime->hw.rates & mout->spdif_rates) &&
5119 (runtime->hw.formats & mout->spdif_formats)) {
5120 runtime->hw.rates &= mout->spdif_rates;
5121 runtime->hw.formats &= mout->spdif_formats;
5122 if (mout->spdif_maxbps < hinfo->maxbps)
5123 hinfo->maxbps = mout->spdif_maxbps;
5124 } else {
5125 mout->share_spdif = 0;
5126 /* FIXME: need notify? */
5127 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005128 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005129 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5132 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5133}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005134EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Takashi Iwaid5191e52009-11-16 14:58:17 +01005136/**
5137 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005138 * @codec: the HDA codec
5139 * @mout: hda_multi_out object
5140 * @stream_tag: stream tag to assign
5141 * @format: format id to assign
5142 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005143 *
5144 * Set up the i/o for analog out.
5145 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005147int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5148 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 unsigned int stream_tag,
5150 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005151 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
Takashi Iwaidda14412011-05-02 11:29:30 +02005153 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005155 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 int i;
5157
Ingo Molnar62932df2006-01-16 16:34:20 +01005158 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005159 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005160 if (mout->dig_out_nid && mout->share_spdif &&
5161 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005163 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5164 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005165 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005167 setup_dig_out_stream(codec, mout->dig_out_nid,
5168 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 } else {
5170 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005171 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 }
5173 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005174 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
5176 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005177 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5178 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005179 if (!mout->no_share_stream &&
5180 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005182 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5183 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005184 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005185 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5186 if (!mout->no_share_stream && mout->hp_out_nid[i])
5187 snd_hda_codec_setup_stream(codec,
5188 mout->hp_out_nid[i],
5189 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 /* surrounds */
5192 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005193 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005194 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5195 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005196 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005197 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5198 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005200
5201 /* extra surrounds */
5202 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5203 int ch = 0;
5204 if (!mout->extra_out_nid[i])
5205 break;
5206 if (chs >= (i + 1) * 2)
5207 ch = i * 2;
5208 else if (!mout->no_share_stream)
5209 break;
5210 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5211 stream_tag, ch, format);
5212 }
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 return 0;
5215}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005216EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Takashi Iwaid5191e52009-11-16 14:58:17 +01005218/**
5219 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005220 * @codec: the HDA codec
5221 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005223int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5224 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
Takashi Iwaidda14412011-05-02 11:29:30 +02005226 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 int i;
5228
5229 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005230 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005232 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005233 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5234 if (mout->hp_out_nid[i])
5235 snd_hda_codec_cleanup_stream(codec,
5236 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005237 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5238 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005239 snd_hda_codec_cleanup_stream(codec,
5240 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005241 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005243 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 mout->dig_out_used = 0;
5245 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005246 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 return 0;
5248}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005249EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Takashi Iwai47408602012-04-20 13:06:53 +02005251/**
5252 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005253 * @codec: the HDA codec
5254 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005255 *
5256 * Guess the suitable VREF pin bits to be set as the pin-control value.
5257 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5258 */
5259unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5260{
5261 unsigned int pincap;
5262 unsigned int oldval;
5263 oldval = snd_hda_codec_read(codec, pin, 0,
5264 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5265 pincap = snd_hda_query_pin_caps(codec, pin);
5266 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5267 /* Exception: if the default pin setup is vref50, we give it priority */
5268 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5269 return AC_PINCTL_VREF_80;
5270 else if (pincap & AC_PINCAP_VREF_50)
5271 return AC_PINCTL_VREF_50;
5272 else if (pincap & AC_PINCAP_VREF_100)
5273 return AC_PINCTL_VREF_100;
5274 else if (pincap & AC_PINCAP_VREF_GRD)
5275 return AC_PINCTL_VREF_GRD;
5276 return AC_PINCTL_VREF_HIZ;
5277}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005278EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005279
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005280/**
5281 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5282 * @codec: the HDA codec
5283 * @pin: referred pin NID
5284 * @val: pin ctl value to audit
5285 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005286unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5287 hda_nid_t pin, unsigned int val)
5288{
5289 static unsigned int cap_lists[][2] = {
5290 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5291 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5292 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5293 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5294 };
5295 unsigned int cap;
5296
5297 if (!val)
5298 return 0;
5299 cap = snd_hda_query_pin_caps(codec, pin);
5300 if (!cap)
5301 return val; /* don't know what to do... */
5302
5303 if (val & AC_PINCTL_OUT_EN) {
5304 if (!(cap & AC_PINCAP_OUT))
5305 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5306 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5307 val &= ~AC_PINCTL_HP_EN;
5308 }
5309
5310 if (val & AC_PINCTL_IN_EN) {
5311 if (!(cap & AC_PINCAP_IN))
5312 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5313 else {
5314 unsigned int vcap, vref;
5315 int i;
5316 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5317 vref = val & AC_PINCTL_VREFEN;
5318 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5319 if (vref == cap_lists[i][0] &&
5320 !(vcap & cap_lists[i][1])) {
5321 if (i == ARRAY_SIZE(cap_lists) - 1)
5322 vref = AC_PINCTL_VREF_HIZ;
5323 else
5324 vref = cap_lists[i + 1][0];
5325 }
5326 }
5327 val &= ~AC_PINCTL_VREFEN;
5328 val |= vref;
5329 }
5330 }
5331
5332 return val;
5333}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005334EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005335
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005336/**
5337 * _snd_hda_pin_ctl - Helper to set pin ctl value
5338 * @codec: the HDA codec
5339 * @pin: referred pin NID
5340 * @val: pin control value to set
5341 * @cached: access over codec pinctl cache or direct write
5342 *
5343 * This function is a helper to set a pin ctl value more safely.
5344 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5345 * value in pin target array via snd_hda_codec_set_pin_target(), then
5346 * actually writes the value via either snd_hda_codec_update_cache() or
5347 * snd_hda_codec_write() depending on @cached flag.
5348 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005349int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5350 unsigned int val, bool cached)
5351{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005352 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005353 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005354 if (cached)
5355 return snd_hda_codec_update_cache(codec, pin, 0,
5356 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5357 else
5358 return snd_hda_codec_write(codec, pin, 0,
5359 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5360}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005361EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005362
Takashi Iwai990061c2010-09-09 22:08:44 +02005363/**
5364 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005365 * @codec: the HDA codec
5366 * @imux: imux helper object
5367 * @label: the name of imux item to assign
5368 * @index: index number of imux item to assign
5369 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005370 *
5371 * When the same label is used already in the existing items, the number
5372 * suffix is appended to the label. This label index number is stored
5373 * to type_idx when non-NULL pointer is given.
5374 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005375int snd_hda_add_imux_item(struct hda_codec *codec,
5376 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005377 int index, int *type_idx)
5378{
5379 int i, label_idx = 0;
5380 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005381 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005382 return -EINVAL;
5383 }
5384 for (i = 0; i < imux->num_items; i++) {
5385 if (!strncmp(label, imux->items[i].label, strlen(label)))
5386 label_idx++;
5387 }
5388 if (type_idx)
5389 *type_idx = label_idx;
5390 if (label_idx > 0)
5391 snprintf(imux->items[imux->num_items].label,
5392 sizeof(imux->items[imux->num_items].label),
5393 "%s %d", label, label_idx);
5394 else
5395 strlcpy(imux->items[imux->num_items].label, label,
5396 sizeof(imux->items[imux->num_items].label));
5397 imux->items[imux->num_items].index = index;
5398 imux->num_items++;
5399 return 0;
5400}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005401EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403/**
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005404 * snd_hda_bus_reset - Reset the bus
5405 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 */
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005407void snd_hda_bus_reset(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005409 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Takashi Iwai0ba21762007-04-16 11:29:14 +02005411 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005412 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02005413 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005414#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005415 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01005416 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05005417 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005418 }
5419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01005422EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005423
5424/*
5425 * generic arrays
5426 */
5427
Takashi Iwaid5191e52009-11-16 14:58:17 +01005428/**
5429 * snd_array_new - get a new element from the given array
5430 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005431 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005432 * Get a new element from the given array. If it exceeds the
5433 * pre-allocated array size, re-allocate the array.
5434 *
5435 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005436 */
5437void *snd_array_new(struct snd_array *array)
5438{
Takashi Iwai12f17712012-10-10 08:59:14 +02005439 if (snd_BUG_ON(!array->elem_size))
5440 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005441 if (array->used >= array->alloced) {
5442 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005443 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005444 void *nlist;
5445 if (snd_BUG_ON(num >= 4096))
5446 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005447 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005448 if (!nlist)
5449 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005450 array->list = nlist;
5451 array->alloced = num;
5452 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005453 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005454}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005455EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005456
Takashi Iwaid5191e52009-11-16 14:58:17 +01005457/**
5458 * snd_array_free - free the given array elements
5459 * @array: the array object
5460 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005461void snd_array_free(struct snd_array *array)
5462{
5463 kfree(array->list);
5464 array->used = 0;
5465 array->alloced = 0;
5466 array->list = NULL;
5467}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005468EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005469
Takashi Iwaid5191e52009-11-16 14:58:17 +01005470/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005471 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5472 * @pcm: PCM caps bits
5473 * @buf: the string buffer to write
5474 * @buflen: the max buffer length
5475 *
5476 * used by hda_proc.c and hda_eld.c
5477 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005478void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5479{
5480 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5481 int i, j;
5482
5483 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5484 if (pcm & (AC_SUPPCM_BITS_8 << i))
5485 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5486
5487 buf[j] = '\0'; /* necessary when j == 0 */
5488}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005489EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005490
5491MODULE_DESCRIPTION("HDA codec core");
5492MODULE_LICENSE("GPL");