blob: 61c8174e8013c4d95433d0f78da7997eb49e41f2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Takashi Iwai83012a72012-08-24 18:38:08 +020043#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010044#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020045static void hda_power_work(struct work_struct *work);
46static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020047#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaia40e0a82013-11-20 12:41:20 +010048
49static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
Takashi Iwai68467f52012-08-28 09:14:29 -070050{
Takashi Iwaia40e0a82013-11-20 12:41:20 +010051 struct hda_bus *bus = codec->bus;
52
53 if ((power_up && codec->pm_up_notified) ||
54 (!power_up && !codec->pm_up_notified))
55 return;
Takashi Iwai68467f52012-08-28 09:14:29 -070056 if (bus->ops.pm_notify)
57 bus->ops.pm_notify(bus, power_up);
Takashi Iwaia40e0a82013-11-20 12:41:20 +010058 codec->pm_up_notified = power_up;
Takashi Iwai68467f52012-08-28 09:14:29 -070059}
Takashi Iwaia40e0a82013-11-20 12:41:20 +010060
Takashi Iwaicb53c622007-08-10 17:21:45 +020061#else
Takashi Iwaid846b172012-11-24 11:58:24 +010062#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +020063static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +020064#define hda_codec_is_power_on(codec) 1
Takashi Iwaia40e0a82013-11-20 12:41:20 +010065#define hda_call_pm_notify(codec, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +020066#endif
67
Takashi Iwaid5191e52009-11-16 14:58:17 +010068/**
69 * snd_hda_get_jack_location - Give a location string of the jack
70 * @cfg: pin default config value
71 *
72 * Parse the pin default config value and returns the string of the
73 * jack location, e.g. "Rear", "Front", etc.
74 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -040075const char *snd_hda_get_jack_location(u32 cfg)
76{
77 static char *bases[7] = {
78 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
79 };
80 static unsigned char specials_idx[] = {
81 0x07, 0x08,
82 0x17, 0x18, 0x19,
83 0x37, 0x38
84 };
85 static char *specials[] = {
86 "Rear Panel", "Drive Bar",
87 "Riser", "HDMI", "ATAPI",
88 "Mobile-In", "Mobile-Out"
89 };
90 int i;
91 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
92 if ((cfg & 0x0f) < 7)
93 return bases[cfg & 0x0f];
94 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
95 if (cfg == specials_idx[i])
96 return specials[i];
97 }
98 return "UNKNOWN";
99}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100100EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400101
Takashi Iwaid5191e52009-11-16 14:58:17 +0100102/**
103 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
104 * @cfg: pin default config value
105 *
106 * Parse the pin default config value and returns the string of the
107 * jack connectivity, i.e. external or internal connection.
108 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400109const char *snd_hda_get_jack_connectivity(u32 cfg)
110{
111 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
112
113 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
114}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100115EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400116
Takashi Iwaid5191e52009-11-16 14:58:17 +0100117/**
118 * snd_hda_get_jack_type - Give a type string of the jack
119 * @cfg: pin default config value
120 *
121 * Parse the pin default config value and returns the string of the
122 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
123 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400124const char *snd_hda_get_jack_type(u32 cfg)
125{
126 static char *jack_types[16] = {
127 "Line Out", "Speaker", "HP Out", "CD",
128 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
129 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200130 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400131 };
132
133 return jack_types[(cfg & AC_DEFCFG_DEVICE)
134 >> AC_DEFCFG_DEVICE_SHIFT];
135}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100136EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400137
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100138/*
139 * Compose a 32bit command word to be sent to the HD-audio controller
140 */
141static inline unsigned int
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200142make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100143 unsigned int verb, unsigned int parm)
144{
145 u32 val;
146
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200147 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
Takashi Iwai82e1b802009-07-17 12:47:34 +0200148 (verb & ~0xfff) || (parm & ~0xffff)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100149 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200150 codec->addr, nid, verb, parm);
Wu Fengguang6430aee2009-07-17 16:49:19 +0800151 return ~0;
152 }
153
154 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100155 val |= (u32)nid << 20;
156 val |= verb << 8;
157 val |= parm;
158 return val;
159}
160
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200161/*
162 * Send and receive a verb
163 */
164static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200165 int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200166{
167 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200168 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200169
Wu Fengguang6430aee2009-07-17 16:49:19 +0800170 if (cmd == ~0)
171 return -1;
172
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200173 if (res)
174 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200175 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200176 snd_hda_power_up(codec);
177 mutex_lock(&bus->cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200178 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
179 bus->no_response_fallback = 1;
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100180 for (;;) {
181 trace_hda_send_cmd(codec, cmd);
182 err = bus->ops.command(bus, cmd);
183 if (err != -EAGAIN)
184 break;
185 /* process pending verbs */
186 bus->ops.get_response(bus, codec->addr);
187 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200188 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800189 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200190 trace_hda_get_response(codec, *res);
191 }
Takashi Iwai63e51fd2013-06-06 14:20:19 +0200192 bus->no_response_fallback = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200193 mutex_unlock(&bus->cmd_mutex);
194 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100195 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200196 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100197 codec_dbg(codec,
198 "resetting BUS due to fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200199 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200200 bus->ops.bus_reset(bus);
201 }
202 goto again;
203 }
204 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100205 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200206 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200207 return err;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
211 * snd_hda_codec_read - send a command and get the response
212 * @codec: the HDA codec
213 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200214 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @verb: the verb to send
216 * @parm: the parameter for the verb
217 *
218 * Send a single command and read the corresponding response.
219 *
220 * Returns the obtained response value, or -1 for an error.
221 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200222unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200223 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned int verb, unsigned int parm)
225{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200226 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200227 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200228 if (codec_exec_verb(codec, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return res;
231}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100232EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/**
235 * snd_hda_codec_write - send a single command without waiting for response
236 * @codec: the HDA codec
237 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200238 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @verb: the verb to send
240 * @parm: the parameter for the verb
241 *
242 * Send a single command without waiting for response.
243 *
244 * Returns 0 if successful, or a negative error code.
245 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200246int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
247 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200249 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100250 unsigned int res;
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200251 return codec_exec_verb(codec, cmd, flags,
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200252 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100254EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256/**
257 * snd_hda_sequence_write - sequence writes
258 * @codec: the HDA codec
259 * @seq: VERB array to send
260 *
261 * Send the commands sequentially from the given array.
262 * The array must be terminated with NID=0.
263 */
264void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
265{
266 for (; seq->nid; seq++)
267 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
268}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100269EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/**
272 * snd_hda_get_sub_nodes - get the range of sub nodes
273 * @codec: the HDA codec
274 * @nid: NID to parse
275 * @start_id: the pointer to store the start NID
276 *
277 * Parse the NID and store the start NID of its sub-nodes.
278 * Returns the number of sub-nodes.
279 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200280int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
281 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned int parm;
284
285 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Dan Carpenter69eba102014-11-27 01:34:43 +0300286 if (parm == -1) {
287 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200288 return 0;
Dan Carpenter69eba102014-11-27 01:34:43 +0300289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *start_id = (parm >> 16) & 0x7fff;
291 return (int)(parm & 0x7fff);
292}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100293EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100295/* connection list element */
296struct hda_conn_list {
297 struct list_head list;
298 int len;
299 hda_nid_t nid;
300 hda_nid_t conns[0];
301};
302
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200303/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100304static struct hda_conn_list *
305lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200306{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100307 struct hda_conn_list *p;
308 list_for_each_entry(p, &codec->conn_list, list) {
309 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200310 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200311 }
312 return NULL;
313}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200314
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100315static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
316 const hda_nid_t *list)
317{
318 struct hda_conn_list *p;
319
320 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
321 if (!p)
322 return -ENOMEM;
323 p->len = len;
324 p->nid = nid;
325 memcpy(p->conns, list, len * sizeof(hda_nid_t));
326 list_add(&p->list, &codec->conn_list);
327 return 0;
328}
329
330static void remove_conn_list(struct hda_codec *codec)
331{
332 while (!list_empty(&codec->conn_list)) {
333 struct hda_conn_list *p;
334 p = list_first_entry(&codec->conn_list, typeof(*p), list);
335 list_del(&p->list);
336 kfree(p);
337 }
338}
339
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200340/* read the connection and add to the cache */
341static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100343 hda_nid_t list[32];
344 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200345 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200347 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100348 if (len == -ENOSPC) {
349 len = snd_hda_get_num_raw_conns(codec, nid);
350 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
351 if (!result)
352 return -ENOMEM;
353 len = snd_hda_get_raw_connections(codec, nid, result, len);
354 }
355 if (len >= 0)
356 len = snd_hda_override_conn_list(codec, nid, len, result);
357 if (result != list)
358 kfree(result);
359 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200360}
Takashi Iwaidce20792011-06-24 14:10:28 +0200361
362/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100363 * snd_hda_get_conn_list - get connection list
364 * @codec: the HDA codec
365 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100366 * @listp: the pointer to store NID list
367 *
368 * Parses the connection list of the given widget and stores the pointer
369 * to the list of NIDs.
370 *
371 * Returns the number of connections, or a negative error code.
372 *
373 * Note that the returned pointer isn't protected against the list
374 * modification. If snd_hda_override_conn_list() might be called
375 * concurrently, protect with a mutex appropriately.
376 */
377int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
378 const hda_nid_t **listp)
379{
380 bool added = false;
381
382 for (;;) {
383 int err;
384 const struct hda_conn_list *p;
385
386 /* if the connection-list is already cached, read it */
387 p = lookup_conn_list(codec, nid);
388 if (p) {
389 if (listp)
390 *listp = p->conns;
391 return p->len;
392 }
393 if (snd_BUG_ON(added))
394 return -EINVAL;
395
396 err = read_and_add_raw_conns(codec, nid);
397 if (err < 0)
398 return err;
399 added = true;
400 }
401}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100402EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100403
404/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200405 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * @codec: the HDA codec
407 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200408 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * @max_conns: max. number of connections to store
410 *
411 * Parses the connection list of the given widget and stores the list
412 * of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 */
416int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200417 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200418{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100419 const hda_nid_t *list;
420 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200421
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100422 if (len > 0 && conn_list) {
423 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100424 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200425 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200426 return -EINVAL;
427 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100428 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200429 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200430
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100431 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200432}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100433EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200434
Takashi Iwai4eea3092013-02-07 18:18:19 +0100435/* return CONNLIST_LEN parameter of the given widget */
436static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
437{
438 unsigned int wcaps = get_wcaps(codec, nid);
439 unsigned int parm;
440
441 if (!(wcaps & AC_WCAP_CONN_LIST) &&
442 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
443 return 0;
444
445 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
446 if (parm == -1)
447 parm = 0;
448 return parm;
449}
450
451int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
452{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100453 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100454}
455
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200456/**
457 * snd_hda_get_raw_connections - copy connection list without cache
458 * @codec: the HDA codec
459 * @nid: NID to parse
460 * @conn_list: connection list array
461 * @max_conns: max. number of connections to store
462 *
463 * Like snd_hda_get_connections(), copy the connection list but without
464 * checking through the connection-list cache.
465 * Currently called only from hda_proc.c, so not exported.
466 */
467int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
468 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100474 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Takashi Iwai4eea3092013-02-07 18:18:19 +0100476 parm = get_num_conns(codec, nid);
477 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200478 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (parm & AC_CLIST_LONG) {
481 /* long form */
482 shift = 16;
483 num_elems = 2;
484 } else {
485 /* short form */
486 shift = 8;
487 num_elems = 4;
488 }
489 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 mask = (1 << (shift-1)) - 1;
491
Takashi Iwai0ba21762007-04-16 11:29:14 +0200492 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0; /* no connection */
494
495 if (conn_len == 1) {
496 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200497 parm = snd_hda_codec_read(codec, nid, 0,
498 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200499 if (parm == -1 && codec->bus->rirb_error)
500 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100501 if (conn_list)
502 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 1;
504 }
505
506 /* multi connection */
507 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 prev_nid = 0;
509 for (i = 0; i < conn_len; i++) {
510 int range_val;
511 hda_nid_t val, n;
512
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200513 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100514 parm = snd_hda_codec_read(codec, nid, 0,
515 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200516 if (parm == -1 && codec->bus->rirb_error)
517 return -EIO;
518 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200519 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100520 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100521 if (val == 0 && null_count++) { /* no second chance */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100522 codec_dbg(codec,
523 "invalid CONNECT_LIST verb %x[%i]:%x\n",
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200524 nid, i, parm);
525 return 0;
526 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100527 parm >>= shift;
528 if (range_val) {
529 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200530 if (!prev_nid || prev_nid >= val) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100531 codec_warn(codec,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200532 "invalid dep_range_val %x:%x\n",
533 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100536 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100537 if (conn_list) {
538 if (conns >= max_conns)
539 return -ENOSPC;
540 conn_list[conns] = n;
541 }
542 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100543 }
544 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list) {
546 if (conns >= max_conns)
547 return -ENOSPC;
548 conn_list[conns] = val;
549 }
550 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 return conns;
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200558 * snd_hda_override_conn_list - add/modify the connection-list to cache
559 * @codec: the HDA codec
560 * @nid: NID to parse
561 * @len: number of connection list entries
562 * @list: the list of connection entries
563 *
564 * Add or modify the given connection-list to the cache. If the corresponding
565 * cache already exists, invalidate it and append a new one.
566 *
567 * Returns zero or a negative error code.
568 */
569int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
570 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100572 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200573
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100574 p = lookup_conn_list(codec, nid);
575 if (p) {
576 list_del(&p->list);
577 kfree(p);
578 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200579
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100580 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100582EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200583
584/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200585 * snd_hda_get_conn_index - get the connection index of the given NID
586 * @codec: the HDA codec
587 * @mux: NID containing the list
588 * @nid: NID to select
589 * @recursive: 1 when searching NID recursively, otherwise 0
590 *
591 * Parses the connection list of the widget @mux and checks whether the
592 * widget @nid is present. If it is, return the connection index.
593 * Otherwise it returns -1.
594 */
595int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
596 hda_nid_t nid, int recursive)
597{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100598 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 int i, nums;
600
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100601 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200602 for (i = 0; i < nums; i++)
603 if (conn[i] == nid)
604 return i;
605 if (!recursive)
606 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100607 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100608 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200609 return -1;
610 }
611 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200612 for (i = 0; i < nums; i++) {
613 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
614 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
615 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200616 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
617 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200618 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200619 return -1;
620}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100621EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mengdong Linf1aa0682013-08-26 21:35:21 -0400623
624/* return DEVLIST_LEN parameter of the given widget */
625static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
626{
627 unsigned int wcaps = get_wcaps(codec, nid);
628 unsigned int parm;
629
630 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
631 get_wcaps_type(wcaps) != AC_WID_PIN)
632 return 0;
633
634 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
635 if (parm == -1 && codec->bus->rirb_error)
636 parm = 0;
637 return parm & AC_DEV_LIST_LEN_MASK;
638}
639
640/**
641 * snd_hda_get_devices - copy device list without cache
642 * @codec: the HDA codec
643 * @nid: NID of the pin to parse
644 * @dev_list: device list array
645 * @max_devices: max. number of devices to store
646 *
647 * Copy the device list. This info is dynamic and so not cached.
648 * Currently called only from hda_proc.c, so not exported.
649 */
650int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
651 u8 *dev_list, int max_devices)
652{
653 unsigned int parm;
654 int i, dev_len, devices;
655
656 parm = get_num_devices(codec, nid);
657 if (!parm) /* not multi-stream capable */
658 return 0;
659
660 dev_len = parm + 1;
661 dev_len = dev_len < max_devices ? dev_len : max_devices;
662
663 devices = 0;
664 while (devices < dev_len) {
665 parm = snd_hda_codec_read(codec, nid, 0,
666 AC_VERB_GET_DEVICE_LIST, devices);
667 if (parm == -1 && codec->bus->rirb_error)
668 break;
669
670 for (i = 0; i < 8; i++) {
671 dev_list[devices] = (u8)parm;
672 parm >>= 4;
673 devices++;
674 if (devices >= dev_len)
675 break;
676 }
677 }
678 return devices;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * snd_hda_queue_unsol_event - add an unsolicited event to queue
683 * @bus: the BUS
684 * @res: unsolicited event (lower 32bit of RIRB entry)
685 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
686 *
687 * Adds the given event to the queue. The events are processed in
688 * the workqueue asynchronously. Call this function in the interrupt
689 * hanlder when RIRB receives an unsolicited event.
690 *
691 * Returns 0 if successful, or a negative error code.
692 */
693int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
694{
695 struct hda_bus_unsolicited *unsol;
696 unsigned int wp;
697
Wang YanQing2195b062013-05-07 11:27:33 +0800698 if (!bus || !bus->workq)
699 return 0;
700
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200701 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100702 unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
704 unsol->wp = wp;
705
706 wp <<= 1;
707 unsol->queue[wp] = res;
708 unsol->queue[wp + 1] = res_ex;
709
Takashi Iwai6acaed32009-01-12 10:09:24 +0100710 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return 0;
713}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100714EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800717 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
David Howellsc4028952006-11-22 14:57:56 +0000719static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Takashi Iwai922c88a2015-02-17 14:46:40 +0100721 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
722 struct hda_bus_unsolicited *unsol = &bus->unsol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct hda_codec *codec;
724 unsigned int rp, caddr, res;
725
726 while (unsol->rp != unsol->wp) {
727 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
728 unsol->rp = rp;
729 rp <<= 1;
730 res = unsol->queue[rp];
731 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200732 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 continue;
734 codec = bus->caddr_tbl[caddr & 0x0f];
735 if (codec && codec->patch_ops.unsol_event)
736 codec->patch_ops.unsol_event(codec, res);
737 }
738}
739
740/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * destructor
742 */
Takashi Iwai2565c892014-02-19 11:41:09 +0100743static void snd_hda_bus_free(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200745 if (!bus)
Takashi Iwai2565c892014-02-19 11:41:09 +0100746 return;
747
748 WARN_ON(!list_empty(&bus->codec_list));
Takashi Iwai6acaed32009-01-12 10:09:24 +0100749 if (bus->workq)
750 flush_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (bus->ops.private_free)
752 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100753 if (bus->workq)
754 destroy_workqueue(bus->workq);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -0500755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 kfree(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100759static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai2565c892014-02-19 11:41:09 +0100761 snd_hda_bus_free(device->device_data);
762 return 0;
763}
764
765static int snd_hda_bus_dev_disconnect(struct snd_device *device)
766{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100768 bus->shutdown = 1;
Takashi Iwai2565c892014-02-19 11:41:09 +0100769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772/**
773 * snd_hda_bus_new - create a HDA bus
774 * @card: the card entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * @busp: the pointer to store the created bus instance
776 *
777 * Returns 0 if successful, or a negative error code.
778 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100779int snd_hda_bus_new(struct snd_card *card,
Takashi Iwaief744972015-02-17 13:56:29 +0100780 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct hda_bus *bus;
783 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100784 static struct snd_device_ops dev_ops = {
Takashi Iwai2565c892014-02-19 11:41:09 +0100785 .dev_disconnect = snd_hda_bus_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .dev_free = snd_hda_bus_dev_free,
787 };
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (busp)
790 *busp = NULL;
791
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200792 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (bus == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100794 dev_err(card->dev, "can't allocate struct hda_bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
797
798 bus->card = card;
Ingo Molnar62932df2006-01-16 16:34:20 +0100799 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200800 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 INIT_LIST_HEAD(&bus->codec_list);
Takashi Iwai922c88a2015-02-17 14:46:40 +0100802 INIT_WORK(&bus->unsol.work, process_unsol_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100804 snprintf(bus->workq_name, sizeof(bus->workq_name),
805 "hd-audio%d", card->number);
806 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100807 if (!bus->workq) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100808 dev_err(card->dev, "cannot create workqueue %s\n",
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100809 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100810 kfree(bus);
811 return -ENOMEM;
812 }
813
Takashi Iwai0ba21762007-04-16 11:29:14 +0200814 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
815 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 snd_hda_bus_free(bus);
817 return err;
818 }
819 if (busp)
820 *busp = bus;
821 return 0;
822}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100823EXPORT_SYMBOL_GPL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200826 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100828static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200830 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 hda_nid_t nid;
832
833 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
834 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200835 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200836 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200837 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200838 case AC_GRP_AUDIO_FUNCTION:
839 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200840 codec->afg_function_id = function_id & 0xff;
841 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200842 break;
843 case AC_GRP_MODEM_FUNCTION:
844 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200845 codec->mfg_function_id = function_id & 0xff;
846 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200847 break;
848 default:
849 break;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100855 * read widget caps for each widget and store in cache
856 */
857static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
858{
859 int i;
860 hda_nid_t nid;
861
862 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
863 &codec->start_nid);
864 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200865 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100866 return -ENOMEM;
867 nid = codec->start_nid;
868 for (i = 0; i < codec->num_nodes; i++, nid++)
869 codec->wcaps[i] = snd_hda_param_read(codec, nid,
870 AC_PAR_AUDIO_WIDGET_CAP);
871 return 0;
872}
873
Takashi Iwai3be14142009-02-20 14:11:16 +0100874/* read all pin default configurations and save codec->init_pins */
875static int read_pin_defaults(struct hda_codec *codec)
876{
877 int i;
878 hda_nid_t nid = codec->start_nid;
879
880 for (i = 0; i < codec->num_nodes; i++, nid++) {
881 struct hda_pincfg *pin;
882 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200883 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100884 if (wid_type != AC_WID_PIN)
885 continue;
886 pin = snd_array_new(&codec->init_pins);
887 if (!pin)
888 return -ENOMEM;
889 pin->nid = nid;
890 pin->cfg = snd_hda_codec_read(codec, nid, 0,
891 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200892 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
893 AC_VERB_GET_PIN_WIDGET_CONTROL,
894 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100895 }
896 return 0;
897}
898
899/* look up the given pin config list and return the item matching with NID */
900static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
901 struct snd_array *array,
902 hda_nid_t nid)
903{
904 int i;
905 for (i = 0; i < array->used; i++) {
906 struct hda_pincfg *pin = snd_array_elem(array, i);
907 if (pin->nid == nid)
908 return pin;
909 }
910 return NULL;
911}
912
Takashi Iwai3be14142009-02-20 14:11:16 +0100913/* set the current pin config value for the given NID.
914 * the value is cached, and read via snd_hda_codec_get_pincfg()
915 */
916int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
917 hda_nid_t nid, unsigned int cfg)
918{
919 struct hda_pincfg *pin;
920
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200921 /* the check below may be invalid when pins are added by a fixup
922 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
923 * for now
924 */
925 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100926 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
927 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200928 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100929
Takashi Iwai3be14142009-02-20 14:11:16 +0100930 pin = look_up_pincfg(codec, list, nid);
931 if (!pin) {
932 pin = snd_array_new(list);
933 if (!pin)
934 return -ENOMEM;
935 pin->nid = nid;
936 }
937 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100938 return 0;
939}
940
Takashi Iwaid5191e52009-11-16 14:58:17 +0100941/**
942 * snd_hda_codec_set_pincfg - Override a pin default configuration
943 * @codec: the HDA codec
944 * @nid: NID to set the pin config
945 * @cfg: the pin default config value
946 *
947 * Override a pin default configuration value in the cache.
948 * This value can be read by snd_hda_codec_get_pincfg() in a higher
949 * priority than the real hardware value.
950 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100951int snd_hda_codec_set_pincfg(struct hda_codec *codec,
952 hda_nid_t nid, unsigned int cfg)
953{
Takashi Iwai346ff702009-02-23 09:42:57 +0100954 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100955}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100956EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100957
Takashi Iwaid5191e52009-11-16 14:58:17 +0100958/**
959 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
960 * @codec: the HDA codec
961 * @nid: NID to get the pin config
962 *
963 * Get the current pin config value of the given pin NID.
964 * If the pincfg value is cached or overridden via sysfs or driver,
965 * returns the cached value.
966 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100967unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
968{
969 struct hda_pincfg *pin;
970
Takashi Iwai648a8d22014-02-25 10:38:13 +0100971#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100972 {
973 unsigned int cfg = 0;
974 mutex_lock(&codec->user_mutex);
975 pin = look_up_pincfg(codec, &codec->user_pins, nid);
976 if (pin)
977 cfg = pin->cfg;
978 mutex_unlock(&codec->user_mutex);
979 if (cfg)
980 return cfg;
981 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100982#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100983 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
984 if (pin)
985 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100986 pin = look_up_pincfg(codec, &codec->init_pins, nid);
987 if (pin)
988 return pin->cfg;
989 return 0;
990}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100991EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100992
Takashi Iwai95a962c2014-10-29 16:03:58 +0100993/**
994 * snd_hda_codec_set_pin_target - remember the current pinctl target value
995 * @codec: the HDA codec
996 * @nid: pin NID
997 * @val: assigned pinctl value
998 *
999 * This function stores the given value to a pinctl target value in the
1000 * pincfg table. This isn't always as same as the actually written value
1001 * but can be referred at any time via snd_hda_codec_get_pin_target().
1002 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001003int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1004 unsigned int val)
1005{
1006 struct hda_pincfg *pin;
1007
1008 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1009 if (!pin)
1010 return -EINVAL;
1011 pin->target = val;
1012 return 0;
1013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001014EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001015
Takashi Iwai95a962c2014-10-29 16:03:58 +01001016/**
1017 * snd_hda_codec_get_pin_target - return the current pinctl target value
1018 * @codec: the HDA codec
1019 * @nid: pin NID
1020 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001021int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1022{
1023 struct hda_pincfg *pin;
1024
1025 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1026 if (!pin)
1027 return 0;
1028 return pin->target;
1029}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001030EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001031
Takashi Iwai92ee6162009-12-27 11:18:59 +01001032/**
1033 * snd_hda_shutup_pins - Shut up all pins
1034 * @codec: the HDA codec
1035 *
1036 * Clear all pin controls to shup up before suspend for avoiding click noise.
1037 * The controls aren't cached so that they can be resumed properly.
1038 */
1039void snd_hda_shutup_pins(struct hda_codec *codec)
1040{
1041 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001042 /* don't shut up pins when unloading the driver; otherwise it breaks
1043 * the default pin setup at the next load of the driver
1044 */
1045 if (codec->bus->shutdown)
1046 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001047 for (i = 0; i < codec->init_pins.used; i++) {
1048 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1049 /* use read here for syncing after issuing each verb */
1050 snd_hda_codec_read(codec, pin->nid, 0,
1051 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1052 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001053 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001054}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001055EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +01001056
Takashi Iwai2a439522011-07-26 09:52:50 +02001057#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001058/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1059static void restore_shutup_pins(struct hda_codec *codec)
1060{
1061 int i;
1062 if (!codec->pins_shutup)
1063 return;
1064 if (codec->bus->shutdown)
1065 return;
1066 for (i = 0; i < codec->init_pins.used; i++) {
1067 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1068 snd_hda_codec_write(codec, pin->nid, 0,
1069 AC_VERB_SET_PIN_WIDGET_CONTROL,
1070 pin->ctrl);
1071 }
1072 codec->pins_shutup = 0;
1073}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001074#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001075
David Henningsson26a6cb62012-10-09 15:04:21 +02001076static void hda_jackpoll_work(struct work_struct *work)
1077{
1078 struct hda_codec *codec =
1079 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +02001080
1081 snd_hda_jack_set_dirty_all(codec);
1082 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -04001083
1084 if (!codec->jackpoll_interval)
1085 return;
1086
David Henningsson26a6cb62012-10-09 15:04:21 +02001087 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1088 codec->jackpoll_interval);
1089}
1090
Takashi Iwai01751f52007-08-10 16:59:39 +02001091static void init_hda_cache(struct hda_cache_rec *cache,
1092 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001093static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001094
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001095/* release all pincfg lists */
1096static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001097{
Takashi Iwai346ff702009-02-23 09:42:57 +01001098 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +01001099#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +01001100 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001101#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001102 snd_array_free(&codec->init_pins);
1103}
1104
Takashi Iwai54d17402005-11-21 16:33:22 +01001105/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001106 * audio-converter setup caches
1107 */
1108struct hda_cvt_setup {
1109 hda_nid_t nid;
1110 u8 stream_tag;
1111 u8 channel_id;
1112 u16 format_id;
1113 unsigned char active; /* cvt is currently used */
1114 unsigned char dirty; /* setups should be cleared */
1115};
1116
1117/* get or create a cache entry for the given audio converter NID */
1118static struct hda_cvt_setup *
1119get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1120{
1121 struct hda_cvt_setup *p;
1122 int i;
1123
1124 for (i = 0; i < codec->cvt_setups.used; i++) {
1125 p = snd_array_elem(&codec->cvt_setups, i);
1126 if (p->nid == nid)
1127 return p;
1128 }
1129 p = snd_array_new(&codec->cvt_setups);
1130 if (p)
1131 p->nid = nid;
1132 return p;
1133}
1134
1135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * codec destructor
1137 */
1138static void snd_hda_codec_free(struct hda_codec *codec)
1139{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001140 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001142 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001143 if (device_is_registered(hda_codec_dev(codec)))
1144 device_del(hda_codec_dev(codec));
Takashi Iwai59cad162012-06-26 15:00:20 +02001145 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001146 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001147#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001148 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001149 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001152 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001153 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001154 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001155 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001156 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 codec->bus->caddr_tbl[codec->addr] = NULL;
Takashi Iwaia40e0a82013-11-20 12:41:20 +01001158 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
Takashi Iwai648a8d22014-02-25 10:38:13 +01001159 snd_hda_sysfs_clear(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +02001160 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001161 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001162 kfree(codec->vendor_name);
1163 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001164 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001165 kfree(codec->wcaps);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001166 codec->bus->num_codecs--;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001167 put_device(hda_codec_dev(codec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001170static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1171 hda_nid_t fg, unsigned int power_state);
1172
Takashi Iwaid8193872012-08-31 07:54:38 -07001173static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001174 unsigned int power_state);
1175
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001176static int snd_hda_codec_dev_register(struct snd_device *device)
1177{
1178 struct hda_codec *codec = device->device_data;
1179
Takashi Iwaid604b392014-02-28 13:42:09 +01001180 snd_hda_register_beep_device(codec);
1181 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001182}
1183
1184static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1185{
1186 struct hda_codec *codec = device->device_data;
1187
Takashi Iwaid604b392014-02-28 13:42:09 +01001188 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001189 return 0;
1190}
1191
Takashi Iwai2565c892014-02-19 11:41:09 +01001192static int snd_hda_codec_dev_free(struct snd_device *device)
1193{
1194 snd_hda_codec_free(device->device_data);
1195 return 0;
1196}
1197
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001198/* just free the container */
1199static void snd_hda_codec_dev_release(struct device *dev)
1200{
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001201 kfree(dev_to_hda_codec(dev));
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
1205 * snd_hda_codec_new - create a HDA codec
1206 * @bus: the bus to assign
1207 * @codec_addr: the codec address
1208 * @codecp: the pointer to store the generated codec
1209 *
1210 * Returns 0 if successful, or a negative error code.
1211 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001212int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001213 unsigned int codec_addr,
1214 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct hda_codec *codec;
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001217 struct device *dev;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001218 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001219 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +01001221 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001222 .dev_register = snd_hda_codec_dev_register,
1223 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +01001224 .dev_free = snd_hda_codec_dev_free,
1225 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwaida3cec32008-08-08 17:12:14 +02001227 if (snd_BUG_ON(!bus))
1228 return -EINVAL;
1229 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001233 dev_err(bus->card->dev,
1234 "address 0x%x is already occupied\n",
1235 codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EBUSY;
1237 }
1238
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001239 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (codec == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001241 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -ENOMEM;
1243 }
1244
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001245 dev = hda_codec_dev(codec);
1246 device_initialize(dev);
1247 dev->parent = bus->card->dev;
1248 dev->bus = &snd_hda_bus_type;
1249 dev->release = snd_hda_codec_dev_release;
1250 dev->groups = snd_hda_dev_attr_groups;
1251 dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr);
1252 dev_set_drvdata(dev, codec); /* for sysfs */
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001258 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001259 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001260 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001261 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1262 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001263 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001264 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001265 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001267 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001268 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001269 INIT_LIST_HEAD(&codec->conn_list);
1270
David Henningsson26a6cb62012-10-09 15:04:21 +02001271 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -05001272 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +02001273 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Takashi Iwai83012a72012-08-24 18:38:08 +02001275#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001276 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001277 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1278 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1279 * the caller has to power down appropriatley after initialization
1280 * phase.
1281 */
1282 hda_keep_power_on(codec);
1283#endif
1284
Takashi Iwai648a8d22014-02-25 10:38:13 +01001285 snd_hda_sysfs_init(codec);
1286
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001287 if (codec->bus->modelname) {
1288 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1289 if (!codec->modelname) {
Takashi Iwai13aeaf62014-02-25 07:53:47 +01001290 err = -ENODEV;
1291 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001292 }
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 list_add_tail(&codec->list, &bus->codec_list);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001296 bus->num_codecs++;
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 bus->caddr_tbl[codec_addr] = codec;
1299
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1301 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001302 if (codec->vendor_id == -1)
1303 /* read again, hopefully the access method was corrected
1304 * in the last read...
1305 */
1306 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1307 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001308 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1309 AC_PAR_SUBSYSTEM_ID);
1310 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1311 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001313 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 if (!codec->afg && !codec->mfg) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001315 dev_err(bus->card->dev, "no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001316 err = -ENODEV;
1317 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Takashi Iwaid8193872012-08-31 07:54:38 -07001320 fg = codec->afg ? codec->afg : codec->mfg;
1321 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001322 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001323 dev_err(bus->card->dev, "cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001324 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001325 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001326 err = read_pin_defaults(codec);
1327 if (err < 0)
1328 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001329
Takashi Iwai0ba21762007-04-16 11:29:14 +02001330 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001331 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001332 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001333 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001334 }
1335
Takashi Iwai83012a72012-08-24 18:38:08 +02001336#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001337 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001338 AC_PWRST_CLKSTOP);
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001339#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001340 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001341 AC_PWRST_EPSS);
Takashi Iwai3e9bc582013-11-26 09:58:46 +01001342#ifdef CONFIG_PM
1343 if (!codec->d3_stop_clk || !codec->epss)
1344 bus->power_keep_link_on = 1;
1345#endif
1346
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001347
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001348 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001349 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001350
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001351 snd_hda_codec_proc_new(codec);
1352
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001354
1355 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1356 codec->subsystem_id, codec->revision_id);
1357 snd_component_add(codec->bus->card, component);
1358
Takashi Iwai2565c892014-02-19 11:41:09 +01001359 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1360 if (err < 0)
1361 goto error;
1362
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001363 if (codecp)
1364 *codecp = codec;
1365 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001366
1367 error:
1368 snd_hda_codec_free(codec);
1369 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001370}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001371EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001372
Takashi Iwai95a962c2014-10-29 16:03:58 +01001373/**
1374 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1375 * @codec: the HDA codec
1376 *
1377 * Forcibly refresh the all widget caps and the init pin configurations of
1378 * the given codec.
1379 */
Mengdong Lina15d05d2013-02-08 17:09:31 -05001380int snd_hda_codec_update_widgets(struct hda_codec *codec)
1381{
1382 hda_nid_t fg;
1383 int err;
1384
1385 /* Assume the function group node does not change,
1386 * only the widget nodes may change.
1387 */
1388 kfree(codec->wcaps);
1389 fg = codec->afg ? codec->afg : codec->mfg;
1390 err = read_widget_caps(codec, fg);
1391 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001392 codec_err(codec, "cannot malloc\n");
Mengdong Lina15d05d2013-02-08 17:09:31 -05001393 return err;
1394 }
1395
1396 snd_array_free(&codec->init_pins);
1397 err = read_pin_defaults(codec);
1398
1399 return err;
1400}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001401EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001402
Takashi Iwaied360812012-08-08 17:12:52 +02001403/* update the stream-id if changed */
1404static void update_pcm_stream_id(struct hda_codec *codec,
1405 struct hda_cvt_setup *p, hda_nid_t nid,
1406 u32 stream_tag, int channel_id)
1407{
1408 unsigned int oldval, newval;
1409
1410 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1411 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1412 newval = (stream_tag << 4) | channel_id;
1413 if (oldval != newval)
1414 snd_hda_codec_write(codec, nid, 0,
1415 AC_VERB_SET_CHANNEL_STREAMID,
1416 newval);
1417 p->stream_tag = stream_tag;
1418 p->channel_id = channel_id;
1419 }
1420}
1421
1422/* update the format-id if changed */
1423static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1424 hda_nid_t nid, int format)
1425{
1426 unsigned int oldval;
1427
1428 if (p->format_id != format) {
1429 oldval = snd_hda_codec_read(codec, nid, 0,
1430 AC_VERB_GET_STREAM_FORMAT, 0);
1431 if (oldval != format) {
1432 msleep(1);
1433 snd_hda_codec_write(codec, nid, 0,
1434 AC_VERB_SET_STREAM_FORMAT,
1435 format);
1436 }
1437 p->format_id = format;
1438 }
1439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/**
1442 * snd_hda_codec_setup_stream - set up the codec for streaming
1443 * @codec: the CODEC to set up
1444 * @nid: the NID to set up
1445 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1446 * @channel_id: channel id to pass, zero based.
1447 * @format: stream format.
1448 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001449void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1450 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int channel_id, int format)
1452{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001453 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001454 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001455 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001456 int i;
1457
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001459 return;
1460
Takashi Iwai4e76a882014-02-25 12:21:03 +01001461 codec_dbg(codec,
1462 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1463 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001464 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001465 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001466 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001467
1468 if (codec->pcm_format_first)
1469 update_pcm_format(codec, p, nid, format);
1470 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1471 if (!codec->pcm_format_first)
1472 update_pcm_format(codec, p, nid, format);
1473
Takashi Iwaieb541332010-08-06 13:48:11 +02001474 p->active = 1;
1475 p->dirty = 0;
1476
1477 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001478 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001479 list_for_each_entry(c, &codec->bus->codec_list, list) {
1480 for (i = 0; i < c->cvt_setups.used; i++) {
1481 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001482 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001483 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001484 p->dirty = 1;
1485 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001488EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Takashi Iwaif0cea792010-08-13 11:56:53 +02001490static void really_cleanup_stream(struct hda_codec *codec,
1491 struct hda_cvt_setup *q);
1492
Takashi Iwaid5191e52009-11-16 14:58:17 +01001493/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001494 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001495 * @codec: the CODEC to clean up
1496 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001497 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001498 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001499void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1500 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001501{
Takashi Iwaieb541332010-08-06 13:48:11 +02001502 struct hda_cvt_setup *p;
1503
Takashi Iwai888afa12008-03-18 09:57:50 +01001504 if (!nid)
1505 return;
1506
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001507 if (codec->no_sticky_stream)
1508 do_now = 1;
1509
Takashi Iwai4e76a882014-02-25 12:21:03 +01001510 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001511 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001512 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001513 /* here we just clear the active flag when do_now isn't set;
1514 * actual clean-ups will be done later in
1515 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1516 */
1517 if (do_now)
1518 really_cleanup_stream(codec, p);
1519 else
1520 p->active = 0;
1521 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001522}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001523EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001524
Takashi Iwaieb541332010-08-06 13:48:11 +02001525static void really_cleanup_stream(struct hda_codec *codec,
1526 struct hda_cvt_setup *q)
1527{
1528 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001529 if (q->stream_tag || q->channel_id)
1530 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1531 if (q->format_id)
1532 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1533);
Takashi Iwaieb541332010-08-06 13:48:11 +02001534 memset(q, 0, sizeof(*q));
1535 q->nid = nid;
1536}
1537
1538/* clean up the all conflicting obsolete streams */
1539static void purify_inactive_streams(struct hda_codec *codec)
1540{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001541 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001542 int i;
1543
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001544 list_for_each_entry(c, &codec->bus->codec_list, list) {
1545 for (i = 0; i < c->cvt_setups.used; i++) {
1546 struct hda_cvt_setup *p;
1547 p = snd_array_elem(&c->cvt_setups, i);
1548 if (p->dirty)
1549 really_cleanup_stream(c, p);
1550 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001551 }
1552}
1553
Takashi Iwai2a439522011-07-26 09:52:50 +02001554#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001555/* clean up all streams; called from suspend */
1556static void hda_cleanup_all_streams(struct hda_codec *codec)
1557{
1558 int i;
1559
1560 for (i = 0; i < codec->cvt_setups.used; i++) {
1561 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1562 if (p->stream_tag)
1563 really_cleanup_stream(codec, p);
1564 }
1565}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001566#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/*
1569 * amp access functions
1570 */
1571
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001572/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001573#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001574#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001575#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1576#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001578#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001581static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001582 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwai01751f52007-08-10 16:59:39 +02001584 memset(cache, 0, sizeof(*cache));
1585 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001586 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001587}
1588
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001589static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001590{
Takashi Iwai603c4012008-07-30 15:01:44 +02001591 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001595static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwai01751f52007-08-10 16:59:39 +02001597 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1598 u16 cur = cache->hash[idx];
1599 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001602 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (info->key == key)
1604 return info;
1605 cur = info->next;
1606 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001607 return NULL;
1608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001610/* query the hash. allocate an entry if not found. */
1611static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1612 u32 key)
1613{
1614 struct hda_cache_head *info = get_hash(cache, key);
1615 if (!info) {
1616 u16 idx, cur;
1617 /* add a new hash entry */
1618 info = snd_array_new(&cache->buf);
1619 if (!info)
1620 return NULL;
1621 cur = snd_array_index(&cache->buf, info);
1622 info->key = key;
1623 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001624 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001625 idx = key % (u16)ARRAY_SIZE(cache->hash);
1626 info->next = cache->hash[idx];
1627 cache->hash[idx] = cur;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return info;
1630}
1631
Takashi Iwai01751f52007-08-10 16:59:39 +02001632/* query and allocate an amp hash entry */
1633static inline struct hda_amp_info *
1634get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1635{
1636 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1637}
1638
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001639/* overwrite the value with the key in the caps hash */
1640static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1641{
1642 struct hda_amp_info *info;
1643
1644 mutex_lock(&codec->hash_mutex);
1645 info = get_alloc_amp_hash(codec, key);
1646 if (!info) {
1647 mutex_unlock(&codec->hash_mutex);
1648 return -EINVAL;
1649 }
1650 info->amp_caps = val;
1651 info->head.val |= INFO_AMP_CAPS;
1652 mutex_unlock(&codec->hash_mutex);
1653 return 0;
1654}
1655
1656/* query the value from the caps hash; if not found, fetch the current
1657 * value from the given function and store in the hash
1658 */
1659static unsigned int
1660query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1661 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1662{
1663 struct hda_amp_info *info;
1664 unsigned int val;
1665
1666 mutex_lock(&codec->hash_mutex);
1667 info = get_alloc_amp_hash(codec, key);
1668 if (!info) {
1669 mutex_unlock(&codec->hash_mutex);
1670 return 0;
1671 }
1672 if (!(info->head.val & INFO_AMP_CAPS)) {
1673 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1674 val = func(codec, nid, dir);
1675 write_caps_hash(codec, key, val);
1676 } else {
1677 val = info->amp_caps;
1678 mutex_unlock(&codec->hash_mutex);
1679 }
1680 return val;
1681}
1682
1683static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1684 int direction)
1685{
1686 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1687 nid = codec->afg;
1688 return snd_hda_param_read(codec, nid,
1689 direction == HDA_OUTPUT ?
1690 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1691}
1692
Takashi Iwaid5191e52009-11-16 14:58:17 +01001693/**
1694 * query_amp_caps - query AMP capabilities
1695 * @codec: the HD-auio codec
1696 * @nid: the NID to query
1697 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1698 *
1699 * Query AMP capabilities for the given widget and direction.
1700 * Returns the obtained capability bits.
1701 *
1702 * When cap bits have been already read, this doesn't read again but
1703 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001705u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001707 return query_caps_hash(codec, nid, direction,
1708 HDA_HASH_KEY(nid, direction, 0),
1709 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001711EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Takashi Iwaid5191e52009-11-16 14:58:17 +01001713/**
David Henningsson861a04e2014-09-23 10:38:17 +02001714 * snd_hda_check_amp_caps - query AMP capabilities
1715 * @codec: the HD-audio codec
1716 * @nid: the NID to query
1717 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001718 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001719 *
1720 * Check whether the widget has the given amp capability for the direction.
1721 */
1722bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1723 int dir, unsigned int bits)
1724{
1725 if (!nid)
1726 return false;
1727 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1728 if (query_amp_caps(codec, nid, dir) & bits)
1729 return true;
1730 return false;
1731}
1732EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1733
1734/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735 * snd_hda_override_amp_caps - Override the AMP capabilities
1736 * @codec: the CODEC to clean up
1737 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001738 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001739 * @caps: the capability bits to set
1740 *
1741 * Override the cached AMP caps bits value by the given one.
1742 * This function is useful if the driver needs to adjust the AMP ranges,
1743 * e.g. limit to 0dB, etc.
1744 *
1745 * Returns zero if successful or a negative error code.
1746 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001747int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1748 unsigned int caps)
1749{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001750 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001751}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001752EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001753
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001754static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1755 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001756{
1757 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1758}
1759
Takashi Iwaid5191e52009-11-16 14:58:17 +01001760/**
1761 * snd_hda_query_pin_caps - Query PIN capabilities
1762 * @codec: the HD-auio codec
1763 * @nid: the NID to query
1764 *
1765 * Query PIN capabilities for the given widget.
1766 * Returns the obtained capability bits.
1767 *
1768 * When cap bits have been already read, this doesn't read again but
1769 * returns the cached value.
1770 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001771u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1772{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001773 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001774 read_pin_cap);
1775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001776EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
Takashi Iwai1327a322009-03-23 13:07:47 +01001777
Wu Fengguang864f92b2009-11-18 12:38:02 +08001778/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001779 * snd_hda_override_pin_caps - Override the pin capabilities
1780 * @codec: the CODEC
1781 * @nid: the NID to override
1782 * @caps: the capability bits to set
1783 *
1784 * Override the cached PIN capabilitiy bits value by the given one.
1785 *
1786 * Returns zero if successful or a negative error code.
1787 */
1788int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1789 unsigned int caps)
1790{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001791 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001792}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001793EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001794
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795/* read or sync the hash value with the current value;
1796 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001798static struct hda_amp_info *
1799update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001800 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001802 struct hda_amp_info *info;
1803 unsigned int parm, val = 0;
1804 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001806 retry:
1807 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1808 if (!info)
1809 return NULL;
1810 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1811 if (!val_read) {
1812 mutex_unlock(&codec->hash_mutex);
1813 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1814 parm |= direction == HDA_OUTPUT ?
1815 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1816 parm |= index;
1817 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001818 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001819 val &= 0xff;
1820 val_read = true;
1821 mutex_lock(&codec->hash_mutex);
1822 goto retry;
1823 }
1824 info->vol[ch] = val;
1825 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001826 } else if (init_only)
1827 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001828 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001832 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001834static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001835 hda_nid_t nid, int ch, int direction, int index,
1836 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 u32 parm;
1839
1840 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1841 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1842 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001843 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1844 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001845 ; /* set the zero value as a fake mute */
1846 else
1847 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1849}
1850
Takashi Iwaid5191e52009-11-16 14:58:17 +01001851/**
1852 * snd_hda_codec_amp_read - Read AMP value
1853 * @codec: HD-audio codec
1854 * @nid: NID to read the AMP value
1855 * @ch: channel (left=0 or right=1)
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @index: the index value (only for input direction)
1858 *
1859 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
Takashi Iwai834be882006-03-01 14:16:17 +01001861int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1862 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001864 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001865 unsigned int val = 0;
1866
1867 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001868 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001869 if (info)
1870 val = info->vol[ch];
1871 mutex_unlock(&codec->hash_mutex);
1872 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001874EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Takashi Iwai280e57d2012-12-14 10:32:21 +01001876static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1877 int direction, int idx, int mask, int val,
Takashi Iwai781c7b92015-02-20 10:26:25 +01001878 bool init_only, bool cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001879{
1880 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001881 unsigned int caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001882
1883 if (snd_BUG_ON(mask & ~0xff))
1884 mask &= 0xff;
1885 val &= mask;
1886
1887 mutex_lock(&codec->hash_mutex);
1888 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1889 if (!info) {
1890 mutex_unlock(&codec->hash_mutex);
1891 return 0;
1892 }
1893 val |= info->vol[ch] & ~mask;
1894 if (info->vol[ch] == val) {
1895 mutex_unlock(&codec->hash_mutex);
1896 return 0;
1897 }
1898 info->vol[ch] = val;
Takashi Iwai781c7b92015-02-20 10:26:25 +01001899 info->head.dirty |= cache_only;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001900 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001901 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001902 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001903 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001904 return 1;
1905}
1906
Takashi Iwaid5191e52009-11-16 14:58:17 +01001907/**
1908 * snd_hda_codec_amp_update - update the AMP value
1909 * @codec: HD-audio codec
1910 * @nid: NID to read the AMP value
1911 * @ch: channel (left=0 or right=1)
1912 * @direction: #HDA_INPUT or #HDA_OUTPUT
1913 * @idx: the index value (only for input direction)
1914 * @mask: bit mask to set
1915 * @val: the bits value to set
1916 *
1917 * Update the AMP value with a bit mask.
1918 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001919 */
Takashi Iwai834be882006-03-01 14:16:17 +01001920int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1921 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001923 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1924 false, codec->cached_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001926EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Takashi Iwaid5191e52009-11-16 14:58:17 +01001928/**
1929 * snd_hda_codec_amp_stereo - update the AMP stereo values
1930 * @codec: HD-audio codec
1931 * @nid: NID to read the AMP value
1932 * @direction: #HDA_INPUT or #HDA_OUTPUT
1933 * @idx: the index value (only for input direction)
1934 * @mask: bit mask to set
1935 * @val: the bits value to set
1936 *
1937 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1938 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001939 */
1940int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1941 int direction, int idx, int mask, int val)
1942{
1943 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001944
1945 if (snd_BUG_ON(mask & ~0xff))
1946 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001947 for (ch = 0; ch < 2; ch++)
1948 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1949 idx, mask, val);
1950 return ret;
1951}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001952EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001953
Takashi Iwai95a962c2014-10-29 16:03:58 +01001954/**
1955 * snd_hda_codec_amp_init - initialize the AMP value
1956 * @codec: the HDA codec
1957 * @nid: NID to read the AMP value
1958 * @ch: channel (left=0 or right=1)
1959 * @dir: #HDA_INPUT or #HDA_OUTPUT
1960 * @idx: the index value (only for input direction)
1961 * @mask: bit mask to set
1962 * @val: the bits value to set
1963 *
1964 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001965 * the first access. If the amp was already initialized / updated beforehand,
1966 * this does nothing.
1967 */
1968int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1969 int dir, int idx, int mask, int val)
1970{
Takashi Iwai781c7b92015-02-20 10:26:25 +01001971 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1972 codec->cached_write);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001973}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001974EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001975
Takashi Iwai95a962c2014-10-29 16:03:58 +01001976/**
1977 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1978 * @codec: the HDA codec
1979 * @nid: NID to read the AMP value
1980 * @dir: #HDA_INPUT or #HDA_OUTPUT
1981 * @idx: the index value (only for input direction)
1982 * @mask: bit mask to set
1983 * @val: the bits value to set
1984 *
1985 * Call snd_hda_codec_amp_init() for both stereo channels.
1986 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001987int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1988 int dir, int idx, int mask, int val)
1989{
1990 int ch, ret = 0;
1991
1992 if (snd_BUG_ON(mask & ~0xff))
1993 mask &= 0xff;
1994 for (ch = 0; ch < 2; ch++)
1995 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1996 idx, mask, val);
1997 return ret;
1998}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001999EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002000
Takashi Iwaid5191e52009-11-16 14:58:17 +01002001/**
2002 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2003 * @codec: HD-audio codec
2004 *
2005 * Resume the all amp commands from the cache.
2006 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002007void snd_hda_codec_resume_amp(struct hda_codec *codec)
2008{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002009 int i;
2010
Takashi Iwaic370dd62012-12-13 18:30:04 +01002011 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002012 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002013 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2014 struct hda_amp_info *buffer;
2015 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002016 hda_nid_t nid;
2017 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002018 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002019
2020 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002021 if (!buffer->head.dirty)
2022 continue;
2023 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002024 info = *buffer;
2025 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002026 if (!key)
2027 continue;
2028 nid = key & 0xff;
2029 idx = (key >> 16) & 0xff;
2030 dir = (key >> 24) & 0xff;
2031 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002032 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002033 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002034 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002035 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2036 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002037 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002038 }
2039 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002040 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002041}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002042EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002044static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2045 unsigned int ofs)
2046{
2047 u32 caps = query_amp_caps(codec, nid, dir);
2048 /* get num steps */
2049 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2050 if (ofs < caps)
2051 caps -= ofs;
2052 return caps;
2053}
2054
Takashi Iwaid5191e52009-11-16 14:58:17 +01002055/**
2056 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002057 * @kcontrol: referred ctl element
2058 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002059 *
2060 * The control element is supposed to have the private_value field
2061 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2062 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002063int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2067 u16 nid = get_amp_nid(kcontrol);
2068 u8 chs = get_amp_channels(kcontrol);
2069 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002070 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002072 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2073 uinfo->count = chs == 3 ? 2 : 1;
2074 uinfo->value.integer.min = 0;
2075 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2076 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002077 codec_warn(codec,
2078 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2079 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return -EINVAL;
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002084EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002086
2087static inline unsigned int
2088read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2089 int ch, int dir, int idx, unsigned int ofs)
2090{
2091 unsigned int val;
2092 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2093 val &= HDA_AMP_VOLMASK;
2094 if (val >= ofs)
2095 val -= ofs;
2096 else
2097 val = 0;
2098 return val;
2099}
2100
2101static inline int
2102update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2103 int ch, int dir, int idx, unsigned int ofs,
2104 unsigned int val)
2105{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002106 unsigned int maxval;
2107
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002108 if (val > 0)
2109 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002110 /* ofs = 0: raw max value */
2111 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002112 if (val > maxval)
2113 val = maxval;
Takashi Iwai781c7b92015-02-20 10:26:25 +01002114 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2115 false, !hda_codec_is_power_on(codec));
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002116}
2117
Takashi Iwaid5191e52009-11-16 14:58:17 +01002118/**
2119 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002120 * @kcontrol: ctl element
2121 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002122 *
2123 * The control element is supposed to have the private_value field
2124 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2125 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002126int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 hda_nid_t nid = get_amp_nid(kcontrol);
2131 int chs = get_amp_channels(kcontrol);
2132 int dir = get_amp_direction(kcontrol);
2133 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002134 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 long *valp = ucontrol->value.integer.value;
2136
2137 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002138 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002140 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002143EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
2146 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002147 * @kcontrol: ctl element
2148 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002149 *
2150 * The control element is supposed to have the private_value field
2151 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2152 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002153int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2154 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2157 hda_nid_t nid = get_amp_nid(kcontrol);
2158 int chs = get_amp_channels(kcontrol);
2159 int dir = get_amp_direction(kcontrol);
2160 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002161 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 long *valp = ucontrol->value.integer.value;
2163 int change = 0;
2164
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002165 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002166 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002167 valp++;
2168 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002169 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002170 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return change;
2172}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002173EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Takashi Iwaid5191e52009-11-16 14:58:17 +01002175/**
2176 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002177 * @kcontrol: ctl element
2178 * @op_flag: operation flag
2179 * @size: byte size of input TLV
2180 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002181 *
2182 * The control element is supposed to have the private_value field
2183 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2184 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002185int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2186 unsigned int size, unsigned int __user *_tlv)
2187{
2188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2189 hda_nid_t nid = get_amp_nid(kcontrol);
2190 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002191 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002192 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002193 u32 caps, val1, val2;
2194
2195 if (size < 4 * sizeof(unsigned int))
2196 return -ENOMEM;
2197 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002198 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2199 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002200 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002201 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002202 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002203 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002204 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002205 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2206 return -EFAULT;
2207 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2208 return -EFAULT;
2209 if (put_user(val1, _tlv + 2))
2210 return -EFAULT;
2211 if (put_user(val2, _tlv + 3))
2212 return -EFAULT;
2213 return 0;
2214}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002215EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002216
Takashi Iwaid5191e52009-11-16 14:58:17 +01002217/**
2218 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2219 * @codec: HD-audio codec
2220 * @nid: NID of a reference widget
2221 * @dir: #HDA_INPUT or #HDA_OUTPUT
2222 * @tlv: TLV data to be stored, at least 4 elements
2223 *
2224 * Set (static) TLV data for a virtual master volume using the AMP caps
2225 * obtained from the reference NID.
2226 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002227 */
2228void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2229 unsigned int *tlv)
2230{
2231 u32 caps;
2232 int nums, step;
2233
2234 caps = query_amp_caps(codec, nid, dir);
2235 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2236 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2237 step = (step + 1) * 25;
2238 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2239 tlv[1] = 2 * sizeof(unsigned int);
2240 tlv[2] = -nums * step;
2241 tlv[3] = step;
2242}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002243EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002244
2245/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002246static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002247find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002248{
2249 struct snd_ctl_elem_id id;
2250 memset(&id, 0, sizeof(id));
2251 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002252 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002253 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002254 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2255 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002256 strcpy(id.name, name);
2257 return snd_ctl_find_id(codec->bus->card, &id);
2258}
2259
Takashi Iwaid5191e52009-11-16 14:58:17 +01002260/**
2261 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2262 * @codec: HD-audio codec
2263 * @name: ctl id name string
2264 *
2265 * Get the control element with the given id string and IFACE_MIXER.
2266 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002267struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2268 const char *name)
2269{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002270 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002272EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002273
Takashi Iwaidcda5802012-10-12 17:24:51 +02002274static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002275 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002276{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002277 int i, idx;
2278 /* 16 ctlrs should be large enough */
2279 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2280 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002281 return idx;
2282 }
2283 return -EBUSY;
2284}
2285
Takashi Iwaid5191e52009-11-16 14:58:17 +01002286/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002287 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002288 * @codec: HD-audio codec
2289 * @nid: corresponding NID (optional)
2290 * @kctl: the control element to assign
2291 *
2292 * Add the given control element to an array inside the codec instance.
2293 * All control elements belonging to a codec are supposed to be added
2294 * by this function so that a proper clean-up works at the free or
2295 * reconfiguration time.
2296 *
2297 * If non-zero @nid is passed, the NID is assigned to the control element.
2298 * The assignment is shown in the codec proc file.
2299 *
2300 * snd_hda_ctl_add() checks the control subdev id field whether
2301 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002302 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2303 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002304 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002305int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2306 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002307{
2308 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002309 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002310 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002311
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002312 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002313 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002314 if (nid == 0)
2315 nid = get_amp_nid_(kctl->private_value);
2316 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002317 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2318 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002319 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002320 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002321 err = snd_ctl_add(codec->bus->card, kctl);
2322 if (err < 0)
2323 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002324 item = snd_array_new(&codec->mixers);
2325 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002326 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002327 item->kctl = kctl;
2328 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002329 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002330 return 0;
2331}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002332EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002333
Takashi Iwaid5191e52009-11-16 14:58:17 +01002334/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002335 * snd_hda_add_nid - Assign a NID to a control element
2336 * @codec: HD-audio codec
2337 * @nid: corresponding NID (optional)
2338 * @kctl: the control element to assign
2339 * @index: index to kctl
2340 *
2341 * Add the given control element to an array inside the codec instance.
2342 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2343 * NID:KCTL mapping - for example "Capture Source" selector.
2344 */
2345int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2346 unsigned int index, hda_nid_t nid)
2347{
2348 struct hda_nid_item *item;
2349
2350 if (nid > 0) {
2351 item = snd_array_new(&codec->nids);
2352 if (!item)
2353 return -ENOMEM;
2354 item->kctl = kctl;
2355 item->index = index;
2356 item->nid = nid;
2357 return 0;
2358 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002359 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2360 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002361 return -EINVAL;
2362}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002363EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002364
2365/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002366 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2367 * @codec: HD-audio codec
2368 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002369void snd_hda_ctls_clear(struct hda_codec *codec)
2370{
2371 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002372 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002373 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002374 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002375 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002376 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002377}
2378
Takashi Iwai95a962c2014-10-29 16:03:58 +01002379/**
2380 * snd_hda_lock_devices - pseudo device locking
2381 * @bus: the BUS
2382 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01002383 * toggle card->shutdown to allow/disallow the device access (as a hack)
2384 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002385int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002386{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002387 struct snd_card *card = bus->card;
2388 struct hda_codec *codec;
2389
Takashi Iwaia65d6292009-02-23 16:57:04 +01002390 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002391 if (card->shutdown)
2392 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002393 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002394 if (!list_empty(&card->ctl_files))
2395 goto err_clear;
2396
2397 list_for_each_entry(codec, &bus->codec_list, list) {
2398 int pcm;
2399 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2400 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2401 if (!cpcm->pcm)
2402 continue;
2403 if (cpcm->pcm->streams[0].substream_opened ||
2404 cpcm->pcm->streams[1].substream_opened)
2405 goto err_clear;
2406 }
2407 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002408 spin_unlock(&card->files_lock);
2409 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002410
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002411 err_clear:
2412 card->shutdown = 0;
2413 err_unlock:
2414 spin_unlock(&card->files_lock);
2415 return -EINVAL;
2416}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002417EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002418
Takashi Iwai95a962c2014-10-29 16:03:58 +01002419/**
2420 * snd_hda_unlock_devices - pseudo device unlocking
2421 * @bus: the BUS
2422 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002425 struct snd_card *card = bus->card;
2426
2427 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002428 spin_lock(&card->files_lock);
2429 card->shutdown = 0;
2430 spin_unlock(&card->files_lock);
2431}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002432EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002433
Takashi Iwaid5191e52009-11-16 14:58:17 +01002434/**
2435 * snd_hda_codec_reset - Clear all objects assigned to the codec
2436 * @codec: HD-audio codec
2437 *
2438 * This frees the all PCM and control elements assigned to the codec, and
2439 * clears the caches and restores the pin default configurations.
2440 *
2441 * When a device is being used, it returns -EBSY. If successfully freed,
2442 * returns zero.
2443 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002444int snd_hda_codec_reset(struct hda_codec *codec)
2445{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002446 struct hda_bus *bus = codec->bus;
2447 struct snd_card *card = bus->card;
2448 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002449
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002450 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002451 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002452
2453 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002454 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002455#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002456 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002457 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002458#endif
2459 snd_hda_ctls_clear(codec);
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002460 /* release PCMs */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002461 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002462 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002463 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002464 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002465 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002466 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002467 }
Takashi Iwaid604b392014-02-28 13:42:09 +01002468 snd_hda_detach_beep_device(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01002469 if (device_is_registered(hda_codec_dev(codec)))
2470 device_del(hda_codec_dev(codec));
2471
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002472 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002473 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002474 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002475 codec->spec = NULL;
2476 free_hda_cache(&codec->amp_cache);
2477 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002478 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2479 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002480 /* free only driver_pins so that init_pins + user_pins are restored */
2481 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002482 snd_array_free(&codec->cvt_setups);
2483 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002484 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002485 codec->num_pcms = 0;
2486 codec->pcm_info = NULL;
2487 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002488 codec->slave_dig_outs = NULL;
2489 codec->spdif_status_reset = 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002490
2491 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002492 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002493 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002494}
2495
Takashi Iwai6194b992014-06-06 18:12:16 +02002496typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002497
2498/* apply the function to all matching slave ctls in the mixer list */
2499static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002500 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002501{
2502 struct hda_nid_item *items;
2503 const char * const *s;
2504 int i, err;
2505
2506 items = codec->mixers.list;
2507 for (i = 0; i < codec->mixers.used; i++) {
2508 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01002509 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002510 continue;
2511 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002512 char tmpname[sizeof(sctl->id.name)];
2513 const char *name = *s;
2514 if (suffix) {
2515 snprintf(tmpname, sizeof(tmpname), "%s %s",
2516 name, suffix);
2517 name = tmpname;
2518 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002519 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002520 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002521 if (err)
2522 return err;
2523 break;
2524 }
2525 }
2526 }
2527 return 0;
2528}
2529
Takashi Iwai6194b992014-06-06 18:12:16 +02002530static int check_slave_present(struct hda_codec *codec,
2531 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002532{
2533 return 1;
2534}
2535
Takashi Iwai18478e82012-03-09 17:51:10 +01002536/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002537static int get_kctl_0dB_offset(struct hda_codec *codec,
2538 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01002539{
2540 int _tlv[4];
2541 const int *tlv = NULL;
2542 int val = -1;
2543
2544 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2545 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2546 mm_segment_t fs = get_fs();
2547 set_fs(get_ds());
2548 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2549 tlv = _tlv;
2550 set_fs(fs);
2551 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2552 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002553 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2554 int step = tlv[3];
2555 step &= ~TLV_DB_SCALE_MUTE;
2556 if (!step)
2557 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01002558 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02002559 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01002560- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002561 return -1;
2562 }
2563 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01002564 val = -tlv[2] / step;
2565 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002566 return val;
2567}
2568
2569/* call kctl->put with the given value(s) */
2570static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2571{
2572 struct snd_ctl_elem_value *ucontrol;
2573 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2574 if (!ucontrol)
2575 return -ENOMEM;
2576 ucontrol->value.integer.value[0] = val;
2577 ucontrol->value.integer.value[1] = val;
2578 kctl->put(kctl, ucontrol);
2579 kfree(ucontrol);
2580 return 0;
2581}
2582
2583/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02002584static int init_slave_0dB(struct hda_codec *codec,
2585 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002586{
Takashi Iwai6194b992014-06-06 18:12:16 +02002587 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01002588 if (offset > 0)
2589 put_kctl_with_value(slave, offset);
2590 return 0;
2591}
2592
2593/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02002594static int init_slave_unmute(struct hda_codec *codec,
2595 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01002596{
2597 return put_kctl_with_value(slave, 1);
2598}
2599
Takashi Iwaie8750942014-06-30 14:02:39 +02002600static int add_slave(struct hda_codec *codec,
2601 void *data, struct snd_kcontrol *slave)
2602{
2603 return snd_ctl_add_slave(data, slave);
2604}
2605
Takashi Iwaid5191e52009-11-16 14:58:17 +01002606/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01002607 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01002608 * @codec: HD-audio codec
2609 * @name: vmaster control name
2610 * @tlv: TLV data (optional)
2611 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002612 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002613 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002614 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002615 *
2616 * Create a virtual master control with the given name. The TLV data
2617 * must be either NULL or a valid data.
2618 *
2619 * @slaves is a NULL-terminated array of strings, each of which is a
2620 * slave control name. All controls with these names are assigned to
2621 * the new virtual master control.
2622 *
2623 * This function returns zero if successful or a negative error code.
2624 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002625int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002626 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002627 const char *suffix, bool init_slave_vol,
2628 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002629{
2630 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002631 int err;
2632
Takashi Iwai29e58532012-03-12 12:25:03 +01002633 if (ctl_ret)
2634 *ctl_ret = NULL;
2635
Takashi Iwai9322ca52012-02-03 14:28:01 +01002636 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002637 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002638 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01002639 return 0;
2640 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002641 kctl = snd_ctl_make_virtual_master(name, tlv);
2642 if (!kctl)
2643 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002644 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002645 if (err < 0)
2646 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002647
Takashi Iwaie8750942014-06-30 14:02:39 +02002648 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002649 if (err < 0)
2650 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002651
2652 /* init with master mute & zero volume */
2653 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01002654 if (init_slave_vol) {
2655 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01002656 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01002657 tlv ? init_slave_0dB : init_slave_unmute, &step);
2658 }
Takashi Iwai18478e82012-03-09 17:51:10 +01002659
Takashi Iwai29e58532012-03-12 12:25:03 +01002660 if (ctl_ret)
2661 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002662 return 0;
2663}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002664EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002665
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002666/*
2667 * mute-LED control using vmaster
2668 */
2669static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_info *uinfo)
2671{
2672 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002673 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002674 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002675
Takashi Iwai3ff72212014-10-20 18:17:28 +02002676 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002677}
2678
2679static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
2682 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2683 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2684 return 0;
2685}
2686
2687static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2689{
2690 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2691 unsigned int old_mode = hook->mute_mode;
2692
2693 hook->mute_mode = ucontrol->value.enumerated.item[0];
2694 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2695 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2696 if (old_mode == hook->mute_mode)
2697 return 0;
2698 snd_hda_sync_vmaster_hook(hook);
2699 return 1;
2700}
2701
2702static struct snd_kcontrol_new vmaster_mute_mode = {
2703 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2704 .name = "Mute-LED Mode",
2705 .info = vmaster_mute_mode_info,
2706 .get = vmaster_mute_mode_get,
2707 .put = vmaster_mute_mode_put,
2708};
2709
Takashi Iwai95a962c2014-10-29 16:03:58 +01002710/**
2711 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2712 * @codec: the HDA codec
2713 * @hook: the vmaster hook object
2714 * @expose_enum_ctl: flag to create an enum ctl
2715 *
2716 * Add a mute-LED hook with the given vmaster switch kctl.
2717 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2718 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002719 */
2720int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002721 struct hda_vmaster_mute_hook *hook,
2722 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002723{
2724 struct snd_kcontrol *kctl;
2725
2726 if (!hook->hook || !hook->sw_kctl)
2727 return 0;
2728 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2729 hook->codec = codec;
2730 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002731 if (!expose_enum_ctl)
2732 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002733 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2734 if (!kctl)
2735 return -ENOMEM;
2736 return snd_hda_ctl_add(codec, 0, kctl);
2737}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002738EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002739
Takashi Iwai95a962c2014-10-29 16:03:58 +01002740/**
2741 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2742 * @hook: the vmaster hook
2743 *
2744 * Call the hook with the current value for synchronization.
2745 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002746 */
2747void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2748{
2749 if (!hook->hook || !hook->codec)
2750 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002751 /* don't call vmaster hook in the destructor since it might have
2752 * been already destroyed
2753 */
2754 if (hook->codec->bus->shutdown)
2755 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002756 switch (hook->mute_mode) {
2757 case HDA_VMUTE_FOLLOW_MASTER:
2758 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2759 break;
2760 default:
2761 hook->hook(hook->codec, hook->mute_mode);
2762 break;
2763 }
2764}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002765EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002766
2767
Takashi Iwaid5191e52009-11-16 14:58:17 +01002768/**
2769 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002770 * @kcontrol: referred ctl element
2771 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002772 *
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2775 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002776int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 int chs = get_amp_channels(kcontrol);
2780
2781 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2782 uinfo->count = chs == 3 ? 2 : 1;
2783 uinfo->value.integer.min = 0;
2784 uinfo->value.integer.max = 1;
2785 return 0;
2786}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002787EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Takashi Iwaid5191e52009-11-16 14:58:17 +01002789/**
2790 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002791 * @kcontrol: ctl element
2792 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002793 *
2794 * The control element is supposed to have the private_value field
2795 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2801 hda_nid_t nid = get_amp_nid(kcontrol);
2802 int chs = get_amp_channels(kcontrol);
2803 int dir = get_amp_direction(kcontrol);
2804 int idx = get_amp_index(kcontrol);
2805 long *valp = ucontrol->value.integer.value;
2806
2807 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002808 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002809 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002812 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return 0;
2814}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002815EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Takashi Iwaid5191e52009-11-16 14:58:17 +01002817/**
2818 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002819 * @kcontrol: ctl element
2820 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002821 *
2822 * The control element is supposed to have the private_value field
2823 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2824 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002825int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2829 hda_nid_t nid = get_amp_nid(kcontrol);
2830 int chs = get_amp_channels(kcontrol);
2831 int dir = get_amp_direction(kcontrol);
2832 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 long *valp = ucontrol->value.integer.value;
2834 int change = 0;
2835
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002836 if (chs & 1) {
Takashi Iwai781c7b92015-02-20 10:26:25 +01002837 change = codec_amp_update(codec, nid, 0, dir, idx,
2838 HDA_AMP_MUTE,
2839 *valp ? 0 : HDA_AMP_MUTE, false,
2840 !hda_codec_is_power_on(codec));
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002841 valp++;
2842 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002843 if (chs & 2)
Takashi Iwai781c7b92015-02-20 10:26:25 +01002844 change |= codec_amp_update(codec, nid, 1, dir, idx,
2845 HDA_AMP_MUTE,
2846 *valp ? 0 : HDA_AMP_MUTE, false,
2847 !hda_codec_is_power_on(codec));
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002848 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return change;
2850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002851EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853/*
Takashi Iwai985be542005-11-02 18:26:49 +01002854 * bound volume controls
2855 *
2856 * bind multiple volumes (# indices, from 0)
2857 */
2858
2859#define AMP_VAL_IDX_SHIFT 19
2860#define AMP_VAL_IDX_MASK (0x0f<<19)
2861
Takashi Iwaid5191e52009-11-16 14:58:17 +01002862/**
2863 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002864 * @kcontrol: ctl element
2865 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002866 *
2867 * The control element is supposed to have the private_value field
2868 * set up via HDA_BIND_MUTE*() macros.
2869 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002870int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002872{
2873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2874 unsigned long pval;
2875 int err;
2876
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002877 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002878 pval = kcontrol->private_value;
2879 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2880 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2881 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002882 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002883 return err;
2884}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002885EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002886
Takashi Iwaid5191e52009-11-16 14:58:17 +01002887/**
2888 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002889 * @kcontrol: ctl element
2890 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002891 *
2892 * The control element is supposed to have the private_value field
2893 * set up via HDA_BIND_MUTE*() macros.
2894 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002895int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002897{
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2899 unsigned long pval;
2900 int i, indices, err = 0, change = 0;
2901
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002902 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002903 pval = kcontrol->private_value;
2904 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2905 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002906 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2907 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002908 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2909 if (err < 0)
2910 break;
2911 change |= err;
2912 }
2913 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002914 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002915 return err < 0 ? err : change;
2916}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002917EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002918
Takashi Iwaid5191e52009-11-16 14:58:17 +01002919/**
2920 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002921 * @kcontrol: referred ctl element
2922 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002923 *
2924 * The control element is supposed to have the private_value field
2925 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002926 */
2927int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_info *uinfo)
2929{
2930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2931 struct hda_bind_ctls *c;
2932 int err;
2933
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002934 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002935 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002936 kcontrol->private_value = *c->values;
2937 err = c->ops->info(kcontrol, uinfo);
2938 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002939 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002940 return err;
2941}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002942EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002943
Takashi Iwaid5191e52009-11-16 14:58:17 +01002944/**
2945 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002946 * @kcontrol: ctl element
2947 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002948 *
2949 * The control element is supposed to have the private_value field
2950 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2951 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002952int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2954{
2955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2956 struct hda_bind_ctls *c;
2957 int err;
2958
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002959 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002960 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002961 kcontrol->private_value = *c->values;
2962 err = c->ops->get(kcontrol, ucontrol);
2963 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002964 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002965 return err;
2966}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002967EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002968
Takashi Iwaid5191e52009-11-16 14:58:17 +01002969/**
2970 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002971 * @kcontrol: ctl element
2972 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002973 *
2974 * The control element is supposed to have the private_value field
2975 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2976 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002977int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
2979{
2980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2981 struct hda_bind_ctls *c;
2982 unsigned long *vals;
2983 int err = 0, change = 0;
2984
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002985 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002986 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002987 for (vals = c->values; *vals; vals++) {
2988 kcontrol->private_value = *vals;
2989 err = c->ops->put(kcontrol, ucontrol);
2990 if (err < 0)
2991 break;
2992 change |= err;
2993 }
2994 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002995 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002996 return err < 0 ? err : change;
2997}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002998EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002999
Takashi Iwaid5191e52009-11-16 14:58:17 +01003000/**
3001 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003002 * @kcontrol: ctl element
3003 * @op_flag: operation flag
3004 * @size: byte size of input TLV
3005 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003006 *
3007 * The control element is supposed to have the private_value field
3008 * set up via HDA_BIND_VOL() macro.
3009 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003010int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3011 unsigned int size, unsigned int __user *tlv)
3012{
3013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3014 struct hda_bind_ctls *c;
3015 int err;
3016
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003017 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003018 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003019 kcontrol->private_value = *c->values;
3020 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3021 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003022 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003023 return err;
3024}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003025EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003026
3027struct hda_ctl_ops snd_hda_bind_vol = {
3028 .info = snd_hda_mixer_amp_volume_info,
3029 .get = snd_hda_mixer_amp_volume_get,
3030 .put = snd_hda_mixer_amp_volume_put,
3031 .tlv = snd_hda_mixer_amp_tlv
3032};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003033EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003034
3035struct hda_ctl_ops snd_hda_bind_sw = {
3036 .info = snd_hda_mixer_amp_switch_info,
3037 .get = snd_hda_mixer_amp_switch_get,
3038 .put = snd_hda_mixer_amp_switch_put,
3039 .tlv = snd_hda_mixer_amp_tlv
3040};
Takashi Iwai2698ea92013-12-18 07:45:52 +01003041EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003042
3043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 * SPDIF out controls
3045 */
3046
Takashi Iwai0ba21762007-04-16 11:29:14 +02003047static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3051 uinfo->count = 1;
3052 return 0;
3053}
3054
Takashi Iwai0ba21762007-04-16 11:29:14 +02003055static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3059 IEC958_AES0_NONAUDIO |
3060 IEC958_AES0_CON_EMPHASIS_5015 |
3061 IEC958_AES0_CON_NOT_COPYRIGHT;
3062 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3063 IEC958_AES1_CON_ORIGINAL;
3064 return 0;
3065}
3066
Takashi Iwai0ba21762007-04-16 11:29:14 +02003067static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3071 IEC958_AES0_NONAUDIO |
3072 IEC958_AES0_PRO_EMPHASIS_5015;
3073 return 0;
3074}
3075
Takashi Iwai0ba21762007-04-16 11:29:14 +02003076static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3077 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003080 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003081 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003083 mutex_lock(&codec->spdif_mutex);
3084 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003085 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3086 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3087 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3088 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003089 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 return 0;
3092}
3093
3094/* convert from SPDIF status bits to HDA SPDIF bits
3095 * bit 0 (DigEn) is always set zero (to be filled later)
3096 */
3097static unsigned short convert_from_spdif_status(unsigned int sbits)
3098{
3099 unsigned short val = 0;
3100
3101 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003104 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003106 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3107 IEC958_AES0_PRO_EMPHASIS_5015)
3108 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3111 IEC958_AES0_CON_EMPHASIS_5015)
3112 val |= AC_DIG1_EMPHASIS;
3113 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3114 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003116 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3118 }
3119 return val;
3120}
3121
3122/* convert to SPDIF status bits from HDA SPDIF bits
3123 */
3124static unsigned int convert_to_spdif_status(unsigned short val)
3125{
3126 unsigned int sbits = 0;
3127
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 sbits |= IEC958_AES0_PROFESSIONAL;
3132 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003133 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3135 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003136 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003138 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003140 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3142 sbits |= val & (0x7f << 8);
3143 }
3144 return sbits;
3145}
3146
Takashi Iwai2f728532008-09-25 16:32:41 +02003147/* set digital convert verbs both for the given NID and its slaves */
3148static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3149 int verb, int val)
3150{
Takashi Iwaidda14412011-05-02 11:29:30 +02003151 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003152
Takashi Iwai9e976972008-11-25 08:17:20 +01003153 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003154 d = codec->slave_dig_outs;
3155 if (!d)
3156 return;
3157 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003158 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003159}
3160
3161static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3162 int dig1, int dig2)
3163{
3164 if (dig1 != -1)
3165 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3166 if (dig2 != -1)
3167 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3168}
3169
Takashi Iwai0ba21762007-04-16 11:29:14 +02003170static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003174 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003175 struct hda_spdif_out *spdif;
3176 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 unsigned short val;
3178 int change;
3179
Ingo Molnar62932df2006-01-16 16:34:20 +01003180 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003181 spdif = snd_array_elem(&codec->spdif_out, idx);
3182 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003183 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3185 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3186 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003187 val = convert_from_spdif_status(spdif->status);
3188 val |= spdif->ctls & 1;
3189 change = spdif->ctls != val;
3190 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003191 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003192 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003193 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return change;
3195}
3196
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003197#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Takashi Iwai0ba21762007-04-16 11:29:14 +02003199static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3200 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003203 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003204 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003206 mutex_lock(&codec->spdif_mutex);
3207 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003208 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003209 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
3211}
3212
Stephen Warren74b654c2011-06-01 11:14:18 -06003213static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3214 int dig1, int dig2)
3215{
3216 set_dig_out_convert(codec, nid, dig1, dig2);
3217 /* unmute amp switch (if any) */
3218 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3219 (dig1 & AC_DIG1_ENABLE))
3220 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3221 HDA_AMP_MUTE, 0);
3222}
3223
Takashi Iwai0ba21762007-04-16 11:29:14 +02003224static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003228 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003229 struct hda_spdif_out *spdif;
3230 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 unsigned short val;
3232 int change;
3233
Ingo Molnar62932df2006-01-16 16:34:20 +01003234 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003235 spdif = snd_array_elem(&codec->spdif_out, idx);
3236 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003237 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003239 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003240 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003241 spdif->ctls = val;
3242 if (change && nid != (u16)-1)
3243 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003244 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return change;
3246}
3247
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003248static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 {
3250 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003252 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 .info = snd_hda_spdif_mask_info,
3254 .get = snd_hda_spdif_cmask_get,
3255 },
3256 {
3257 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003259 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 .info = snd_hda_spdif_mask_info,
3261 .get = snd_hda_spdif_pmask_get,
3262 },
3263 {
3264 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003265 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 .info = snd_hda_spdif_mask_info,
3267 .get = snd_hda_spdif_default_get,
3268 .put = snd_hda_spdif_default_put,
3269 },
3270 {
3271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003272 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 .info = snd_hda_spdif_out_switch_info,
3274 .get = snd_hda_spdif_out_switch_get,
3275 .put = snd_hda_spdif_out_switch_put,
3276 },
3277 { } /* end */
3278};
3279
3280/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003281 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003283 * @associated_nid: NID that new ctls associated with
3284 * @cvt_nid: converter NID
3285 * @type: HDA_PCM_TYPE_*
3286 * Creates controls related with the digital output.
3287 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 *
3289 * Returns 0 if successful, or a negative error code.
3290 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003291int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3292 hda_nid_t associated_nid,
3293 hda_nid_t cvt_nid,
3294 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
3296 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003297 struct snd_kcontrol *kctl;
3298 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003299 int idx = 0;
3300 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003301 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003302 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003304 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003305 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003306 idx = spdif_index;
3307 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003308 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003309 /* suppose a single SPDIF device */
3310 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3311 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3312 if (!kctl)
3313 break;
3314 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003315 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003316 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003317 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003318 if (!bus->primary_dig_out_type)
3319 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003320
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003321 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003322 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003323 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003324 return -EBUSY;
3325 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003326 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003327 if (!spdif)
3328 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3330 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003331 if (!kctl)
3332 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003333 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003334 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003335 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003336 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return err;
3338 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003339 spdif->nid = cvt_nid;
3340 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003341 AC_VERB_GET_DIGI_CONVERT_1, 0);
3342 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return 0;
3344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003345EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Takashi Iwai95a962c2014-10-29 16:03:58 +01003347/**
3348 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3349 * @codec: the HDA codec
3350 * @nid: widget NID
3351 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003352 * call within spdif_mutex lock
3353 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003354struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3355 hda_nid_t nid)
3356{
3357 int i;
3358 for (i = 0; i < codec->spdif_out.used; i++) {
3359 struct hda_spdif_out *spdif =
3360 snd_array_elem(&codec->spdif_out, i);
3361 if (spdif->nid == nid)
3362 return spdif;
3363 }
3364 return NULL;
3365}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003366EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06003367
Takashi Iwai95a962c2014-10-29 16:03:58 +01003368/**
3369 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3370 * @codec: the HDA codec
3371 * @idx: the SPDIF ctl index
3372 *
3373 * Unassign the widget from the given SPDIF control.
3374 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003375void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3376{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003377 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003378
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 spdif->nid = (u16)-1;
3382 mutex_unlock(&codec->spdif_mutex);
3383}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003384EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003385
Takashi Iwai95a962c2014-10-29 16:03:58 +01003386/**
3387 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3388 * @codec: the HDA codec
3389 * @idx: the SPDIF ctl idx
3390 * @nid: widget NID
3391 *
3392 * Assign the widget to the SPDIF control with the given index.
3393 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003394void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3395{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003396 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003397 unsigned short val;
3398
3399 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003400 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003401 if (spdif->nid != nid) {
3402 spdif->nid = nid;
3403 val = spdif->ctls;
3404 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3405 }
3406 mutex_unlock(&codec->spdif_mutex);
3407}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003408EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003411 * SPDIF sharing with analog output
3412 */
3413static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_value *ucontrol)
3415{
3416 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3417 ucontrol->value.integer.value[0] = mout->share_spdif;
3418 return 0;
3419}
3420
3421static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3423{
3424 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3425 mout->share_spdif = !!ucontrol->value.integer.value[0];
3426 return 0;
3427}
3428
3429static struct snd_kcontrol_new spdif_share_sw = {
3430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3431 .name = "IEC958 Default PCM Playback Switch",
3432 .info = snd_ctl_boolean_mono_info,
3433 .get = spdif_share_sw_get,
3434 .put = spdif_share_sw_put,
3435};
3436
Takashi Iwaid5191e52009-11-16 14:58:17 +01003437/**
3438 * snd_hda_create_spdif_share_sw - create Default PCM switch
3439 * @codec: the HDA codec
3440 * @mout: multi-out instance
3441 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003442int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3443 struct hda_multi_out *mout)
3444{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003445 struct snd_kcontrol *kctl;
3446
Takashi Iwai9a081602008-02-12 18:37:26 +01003447 if (!mout->dig_out_nid)
3448 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003449
3450 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3451 if (!kctl)
3452 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003453 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003454 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003455}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003456EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003457
3458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 * SPDIF input
3460 */
3461
3462#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3463
Takashi Iwai0ba21762007-04-16 11:29:14 +02003464static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
3467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3468
3469 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3470 return 0;
3471}
3472
Takashi Iwai0ba21762007-04-16 11:29:14 +02003473static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3474 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
3476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3477 hda_nid_t nid = kcontrol->private_value;
3478 unsigned int val = !!ucontrol->value.integer.value[0];
3479 int change;
3480
Ingo Molnar62932df2006-01-16 16:34:20 +01003481 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003483 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003485 snd_hda_codec_write_cache(codec, nid, 0,
3486 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003488 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return change;
3490}
3491
Takashi Iwai0ba21762007-04-16 11:29:14 +02003492static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3493 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3496 hda_nid_t nid = kcontrol->private_value;
3497 unsigned short val;
3498 unsigned int sbits;
3499
Andrew Paprocki3982d172007-12-19 12:13:44 +01003500 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 sbits = convert_to_spdif_status(val);
3502 ucontrol->value.iec958.status[0] = sbits;
3503 ucontrol->value.iec958.status[1] = sbits >> 8;
3504 ucontrol->value.iec958.status[2] = sbits >> 16;
3505 ucontrol->value.iec958.status[3] = sbits >> 24;
3506 return 0;
3507}
3508
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003509static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 {
3511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003512 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 .info = snd_hda_spdif_in_switch_info,
3514 .get = snd_hda_spdif_in_switch_get,
3515 .put = snd_hda_spdif_in_switch_put,
3516 },
3517 {
3518 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003520 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 .info = snd_hda_spdif_mask_info,
3522 .get = snd_hda_spdif_in_status_get,
3523 },
3524 { } /* end */
3525};
3526
3527/**
3528 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3529 * @codec: the HDA codec
3530 * @nid: audio in widget NID
3531 *
3532 * Creates controls related with the SPDIF input.
3533 * Called from each patch supporting the SPDIF in.
3534 *
3535 * Returns 0 if successful, or a negative error code.
3536 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003537int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003540 struct snd_kcontrol *kctl;
3541 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003542 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Takashi Iwaidcda5802012-10-12 17:24:51 +02003544 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003545 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003546 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01003547 return -EBUSY;
3548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3550 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003551 if (!kctl)
3552 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003554 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003555 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return err;
3557 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003559 snd_hda_codec_read(codec, nid, 0,
3560 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003561 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return 0;
3563}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003564EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003566/*
3567 * command cache
3568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Takashi Iwaic370dd62012-12-13 18:30:04 +01003570/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003571#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3572#define get_cmd_cache_nid(key) ((key) & 0xff)
3573#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3574
3575/**
3576 * snd_hda_codec_write_cache - send a single command with caching
3577 * @codec: the HDA codec
3578 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003579 * @flags: optional bit flags
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003580 * @verb: the verb to send
3581 * @parm: the parameter for the verb
3582 *
3583 * Send a single command without waiting for response.
3584 *
3585 * Returns 0 if successful, or a negative error code.
3586 */
3587int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003588 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003589{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003590 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003591 struct hda_cache_head *c;
3592 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003593 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003594
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003595 cache_only = codec->cached_write;
3596 if (!cache_only) {
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003597 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003598 if (err < 0)
3599 return err;
3600 }
3601
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003602 /* parm may contain the verb stuff for get/set amp */
3603 verb = verb | (parm >> 8);
3604 parm &= 0xff;
3605 key = build_cmd_cache_key(nid, verb);
3606 mutex_lock(&codec->bus->cmd_mutex);
3607 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003608 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003609 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003610 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003611 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003612 mutex_unlock(&codec->bus->cmd_mutex);
3613 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003614}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003615EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003616
Takashi Iwaid5191e52009-11-16 14:58:17 +01003617/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003618 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3619 * @codec: the HDA codec
3620 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003621 * @flags: optional bit flags
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003622 * @verb: the verb to send
3623 * @parm: the parameter for the verb
3624 *
3625 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3626 * command if the parameter is already identical with the cached value.
3627 * If not, it sends the command and refreshes the cache.
3628 *
3629 * Returns 0 if successful, or a negative error code.
3630 */
3631int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003632 int flags, unsigned int verb, unsigned int parm)
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003633{
3634 struct hda_cache_head *c;
3635 u32 key;
3636
3637 /* parm may contain the verb stuff for get/set amp */
3638 verb = verb | (parm >> 8);
3639 parm &= 0xff;
3640 key = build_cmd_cache_key(nid, verb);
3641 mutex_lock(&codec->bus->cmd_mutex);
3642 c = get_hash(&codec->cmd_cache, key);
3643 if (c && c->val == parm) {
3644 mutex_unlock(&codec->bus->cmd_mutex);
3645 return 0;
3646 }
3647 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaie7ecc272013-06-06 14:00:23 +02003648 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003649}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003650EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003651
3652/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003653 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3654 * @codec: HD-audio codec
3655 *
3656 * Execute all verbs recorded in the command caches to resume.
3657 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003658void snd_hda_codec_resume_cache(struct hda_codec *codec)
3659{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003660 int i;
3661
Takashi Iwaic370dd62012-12-13 18:30:04 +01003662 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003663 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003664 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3665 struct hda_cache_head *buffer;
3666 u32 key;
3667
3668 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3669 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003670 if (!key)
3671 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003672 if (!buffer->dirty)
3673 continue;
3674 buffer->dirty = 0;
3675 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003676 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3677 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003678 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003679 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003680 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003681}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003682EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003683
3684/**
3685 * snd_hda_sequence_write_cache - sequence writes with caching
3686 * @codec: the HDA codec
3687 * @seq: VERB array to send
3688 *
3689 * Send the commands sequentially from the given array.
3690 * Thte commands are recorded on cache for power-save and resume.
3691 * The array must be terminated with NID=0.
3692 */
3693void snd_hda_sequence_write_cache(struct hda_codec *codec,
3694 const struct hda_verb *seq)
3695{
3696 for (; seq->nid; seq++)
3697 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3698 seq->param);
3699}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003700EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003701
Takashi Iwaidc870f32013-01-22 15:24:30 +01003702/**
3703 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3704 * @codec: HD-audio codec
3705 */
3706void snd_hda_codec_flush_cache(struct hda_codec *codec)
3707{
3708 snd_hda_codec_resume_amp(codec);
3709 snd_hda_codec_resume_cache(codec);
3710}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003711EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003712
Takashi Iwai95a962c2014-10-29 16:03:58 +01003713/**
3714 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3715 * @codec: the HDA codec
3716 * @fg: function group (not used now)
3717 * @power_state: the power state to set (AC_PWRST_*)
3718 *
3719 * Set the given power state to all widgets that have the power control.
3720 * If the codec has power_filter set, it evaluates the power state and
3721 * filter out if it's unchanged as D3.
3722 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003723void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003724 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003725{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003726 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003727 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003728
Takashi Iwaicb53c622007-08-10 17:21:45 +02003729 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003730 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003731 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003732 if (!(wcaps & AC_WCAP_POWER))
3733 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003734 if (codec->power_filter) {
3735 state = codec->power_filter(codec, nid, power_state);
3736 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003737 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003738 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003739 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003740 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003741 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003742}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003743EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003744
3745/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003746 * supported power states check
3747 */
3748static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3749 unsigned int power_state)
3750{
3751 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3752
Mengdong Line037cb42012-08-10 14:11:58 +02003753 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003754 return false;
3755 if (sup & power_state)
3756 return true;
3757 else
3758 return false;
3759}
3760
3761/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003762 * wait until the state is reached, returns the current state
3763 */
3764static unsigned int hda_sync_power_state(struct hda_codec *codec,
3765 hda_nid_t fg,
3766 unsigned int power_state)
3767{
3768 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3769 unsigned int state, actual_state;
3770
3771 for (;;) {
3772 state = snd_hda_codec_read(codec, fg, 0,
3773 AC_VERB_GET_POWER_STATE, 0);
3774 if (state & AC_PWRST_ERROR)
3775 break;
3776 actual_state = (state >> 4) & 0x0f;
3777 if (actual_state == power_state)
3778 break;
3779 if (time_after_eq(jiffies, end_time))
3780 break;
3781 /* wait until the codec reachs to the target state */
3782 msleep(1);
3783 }
3784 return state;
3785}
3786
Takashi Iwai95a962c2014-10-29 16:03:58 +01003787/**
3788 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3789 * @codec: the HDA codec
3790 * @nid: widget NID
3791 * @power_state: power state to evalue
3792 *
3793 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3794 * This can be used a codec power_filter callback.
3795 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003796unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3797 hda_nid_t nid,
3798 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003799{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003800 if (nid == codec->afg || nid == codec->mfg)
3801 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003802 if (power_state == AC_PWRST_D3 &&
3803 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3804 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3805 int eapd = snd_hda_codec_read(codec, nid, 0,
3806 AC_VERB_GET_EAPD_BTLENABLE, 0);
3807 if (eapd & 0x02)
3808 return AC_PWRST_D0;
3809 }
3810 return power_state;
3811}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003812EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003813
Takashi Iwai432c6412012-08-28 09:59:20 -07003814/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003815 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003816 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003817static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003818 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003819{
Takashi Iwaid8193872012-08-31 07:54:38 -07003820 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003821 int count;
3822 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003823 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003824
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003825 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003826 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05003827 if (codec->depop_delay < 0)
3828 msleep(codec->epss ? 10 : 100);
3829 else if (codec->depop_delay > 0)
3830 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02003831 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003832 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003833
3834 /* repeat power states setting at most 10 times*/
3835 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003836 if (codec->patch_ops.set_power_state)
3837 codec->patch_ops.set_power_state(codec, fg,
3838 power_state);
3839 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01003840 state = power_state;
3841 if (codec->power_filter)
3842 state = codec->power_filter(codec, fg, state);
3843 if (state == power_state || power_state != AC_PWRST_D3)
3844 snd_hda_codec_read(codec, fg, flags,
3845 AC_VERB_SET_POWER_STATE,
3846 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003847 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003848 }
3849 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003850 if (!(state & AC_PWRST_ERROR))
3851 break;
3852 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003853
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003854 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003855}
Takashi Iwai54d17402005-11-21 16:33:22 +01003856
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003857/* sync power states of all widgets;
3858 * this is called at the end of codec parsing
3859 */
3860static void sync_power_up_states(struct hda_codec *codec)
3861{
3862 hda_nid_t nid = codec->start_nid;
3863 int i;
3864
Takashi Iwaiba615b82013-03-13 14:47:21 +01003865 /* don't care if no filter is used */
3866 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003867 return;
3868
3869 for (i = 0; i < codec->num_nodes; i++, nid++) {
3870 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003871 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003872 if (!(wcaps & AC_WCAP_POWER))
3873 continue;
3874 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3875 if (target == AC_PWRST_D0)
3876 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003877 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003878 snd_hda_codec_write(codec, nid, 0,
3879 AC_VERB_SET_POWER_STATE, target);
3880 }
3881}
3882
Takashi Iwai648a8d22014-02-25 10:38:13 +01003883#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02003884/* execute additional init verbs */
3885static void hda_exec_init_verbs(struct hda_codec *codec)
3886{
3887 if (codec->init_verbs.list)
3888 snd_hda_sequence_write(codec, codec->init_verbs.list);
3889}
3890#else
3891static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3892#endif
3893
Takashi Iwai2a439522011-07-26 09:52:50 +02003894#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003895/*
3896 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003897 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003898 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003899static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003900{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003901 unsigned int state;
3902
Takashi Iwai989c3182012-11-19 14:14:58 +01003903 codec->in_pm = 1;
3904
Takashi Iwaicb53c622007-08-10 17:21:45 +02003905 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003906 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003907 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003908 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003909 /* Cancel delayed work if we aren't currently running from it. */
3910 if (!in_wq)
3911 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003912 spin_lock(&codec->power_lock);
3913 snd_hda_update_power_acct(codec);
3914 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003915 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003916 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003917 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003918 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003919 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003920 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003921}
3922
Takashi Iwaic370dd62012-12-13 18:30:04 +01003923/* mark all entries of cmd and amp caches dirty */
3924static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3925{
3926 int i;
3927 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3928 struct hda_cache_head *cmd;
3929 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3930 cmd->dirty = 1;
3931 }
3932 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3933 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003934 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003935 amp->head.dirty = 1;
3936 }
3937}
3938
Takashi Iwaicb53c622007-08-10 17:21:45 +02003939/*
3940 * kick up codec; used both from PM and power-save
3941 */
3942static void hda_call_codec_resume(struct hda_codec *codec)
3943{
Takashi Iwai989c3182012-11-19 14:14:58 +01003944 codec->in_pm = 1;
3945
Takashi Iwaic370dd62012-12-13 18:30:04 +01003946 hda_mark_cmd_cache_dirty(codec);
3947
Takashi Iwai7f308302012-05-08 16:52:23 +02003948 /* set as if powered on for avoiding re-entering the resume
3949 * in the resume / power-save sequence
3950 */
3951 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003952 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003953 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003954 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003955 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003956 if (codec->patch_ops.resume)
3957 codec->patch_ops.resume(codec);
3958 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003959 if (codec->patch_ops.init)
3960 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003961 snd_hda_codec_resume_amp(codec);
3962 snd_hda_codec_resume_cache(codec);
3963 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003964
3965 if (codec->jackpoll_interval)
3966 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003967 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003968 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003969
3970 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003971 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003972}
Takashi Iwai2a439522011-07-26 09:52:50 +02003973#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003974
Takashi Iwai54d17402005-11-21 16:33:22 +01003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976/**
3977 * snd_hda_build_controls - build mixer controls
3978 * @bus: the BUS
3979 *
3980 * Creates mixer controls for each codec included in the bus.
3981 *
3982 * Returns 0 if successful, otherwise a negative error code.
3983 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003984int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003986 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Takashi Iwai0ba21762007-04-16 11:29:14 +02003988 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003989 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003990 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003991 codec_err(codec,
3992 "cannot build controls for #%d (error %d)\n",
3993 codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003994 err = snd_hda_codec_reset(codec);
3995 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003996 codec_err(codec,
3997 "cannot revert codec\n");
Takashi Iwaif93d4612009-03-02 10:44:15 +01003998 return err;
3999 }
4000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004002 return 0;
4003}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004004EXPORT_SYMBOL_GPL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004006/*
4007 * add standard channel maps if not specified
4008 */
4009static int add_std_chmaps(struct hda_codec *codec)
4010{
4011 int i, str, err;
4012
4013 for (i = 0; i < codec->num_pcms; i++) {
4014 for (str = 0; str < 2; str++) {
4015 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4016 struct hda_pcm_stream *hinfo =
4017 &codec->pcm_info[i].stream[str];
4018 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004019 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004020
4021 if (codec->pcm_info[i].own_chmap)
4022 continue;
4023 if (!pcm || !hinfo->substreams)
4024 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004025 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4026 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004027 hinfo->channels_max,
4028 0, &chmap);
4029 if (err < 0)
4030 return err;
4031 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4032 }
4033 }
4034 return 0;
4035}
4036
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004037/* default channel maps for 2.1 speakers;
4038 * since HD-audio supports only stereo, odd number channels are omitted
4039 */
4040const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4041 { .channels = 2,
4042 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4043 { .channels = 4,
4044 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4045 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4046 { }
4047};
4048EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4049
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004050int snd_hda_codec_build_controls(struct hda_codec *codec)
4051{
4052 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004053 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004054 /* continue to initialize... */
4055 if (codec->patch_ops.init)
4056 err = codec->patch_ops.init(codec);
4057 if (!err && codec->patch_ops.build_controls)
4058 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004059 if (err < 0)
4060 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004061
4062 /* we create chmaps here instead of build_pcms */
4063 err = add_std_chmaps(codec);
4064 if (err < 0)
4065 return err;
4066
David Henningsson26a6cb62012-10-09 15:04:21 +02004067 if (codec->jackpoll_interval)
4068 hda_jackpoll_work(&codec->jackpoll_work.work);
4069 else
4070 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004071 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return 0;
4073}
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075/*
4076 * stream formats
4077 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004078struct hda_rate_tbl {
4079 unsigned int hz;
4080 unsigned int alsa_bits;
4081 unsigned int hda_fmt;
4082};
4083
Takashi Iwai92f10b32010-08-03 14:21:00 +02004084/* rate = base * mult / div */
4085#define HDA_RATE(base, mult, div) \
4086 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4087 (((div) - 1) << AC_FMT_DIV_SHIFT))
4088
Takashi Iwaibefdf312005-08-22 13:57:55 +02004089static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004091
4092 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004093 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4094 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4095 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4096 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4097 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4098 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4099 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4100 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4101 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4102 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4103 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004104#define AC_PAR_PCM_RATE_BITS 11
4105 /* up to bits 10, 384kHZ isn't supported properly */
4106
4107 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004108 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004109
Takashi Iwaibefdf312005-08-22 13:57:55 +02004110 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111};
4112
4113/**
4114 * snd_hda_calc_stream_format - calculate format bitset
Takashi Iwai6194b992014-06-06 18:12:16 +02004115 * @codec: HD-audio codec
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 * @rate: the sample rate
4117 * @channels: the number of channels
4118 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4119 * @maxbps: the max. bps
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004120 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 *
4122 * Calculate the format bitset from the given rate, channels and th PCM format.
4123 *
4124 * Return zero if invalid.
4125 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004126unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4127 unsigned int rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 unsigned int channels,
4129 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004130 unsigned int maxbps,
4131 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 int i;
4134 unsigned int val = 0;
4135
Takashi Iwaibefdf312005-08-22 13:57:55 +02004136 for (i = 0; rate_bits[i].hz; i++)
4137 if (rate_bits[i].hz == rate) {
4138 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 break;
4140 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004141 if (!rate_bits[i].hz) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004142 codec_dbg(codec, "invalid rate %d\n", rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return 0;
4144 }
4145
4146 if (channels == 0 || channels > 8) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004147 codec_dbg(codec, "invalid channels %d\n", channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149 }
4150 val |= channels - 1;
4151
4152 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004153 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004154 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004155 break;
4156 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004157 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case 20:
4160 case 24:
4161 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004162 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004163 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004165 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004167 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 break;
4169 default:
Takashi Iwai6194b992014-06-06 18:12:16 +02004170 codec_dbg(codec, "invalid format width %d\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01004171 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return 0;
4173 }
4174
Anssi Hannula32c168c2010-08-03 13:28:57 +03004175 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004176 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return val;
4179}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004180EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004182static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4183 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004184{
4185 unsigned int val = 0;
4186 if (nid != codec->afg &&
4187 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4188 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4189 if (!val || val == -1)
4190 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4191 if (!val || val == -1)
4192 return 0;
4193 return val;
4194}
4195
4196static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4197{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004198 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004199 get_pcm_param);
4200}
4201
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004202static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4203 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004204{
4205 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4206 if (!streams || streams == -1)
4207 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4208 if (!streams || streams == -1)
4209 return 0;
4210 return streams;
4211}
4212
4213static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4214{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004215 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004216 get_stream_param);
4217}
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219/**
4220 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4221 * @codec: the HDA codec
4222 * @nid: NID to query
4223 * @ratesp: the pointer to store the detected rate bitflags
4224 * @formatsp: the pointer to store the detected formats
4225 * @bpsp: the pointer to store the detected format widths
4226 *
4227 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4228 * or @bsps argument is ignored.
4229 *
4230 * Returns 0 if successful, otherwise a negative error code.
4231 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004232int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4234{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004235 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004237 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004238 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240 if (ratesp) {
4241 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004242 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004244 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004246 if (rates == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004247 codec_err(codec,
4248 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4249 nid, val,
4250 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004251 return -EIO;
4252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 *ratesp = rates;
4254 }
4255
4256 if (formatsp || bpsp) {
4257 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004258 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004260 streams = query_stream_param(codec, nid);
4261 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264 bps = 0;
4265 if (streams & AC_SUPFMT_PCM) {
4266 if (val & AC_SUPPCM_BITS_8) {
4267 formats |= SNDRV_PCM_FMTBIT_U8;
4268 bps = 8;
4269 }
4270 if (val & AC_SUPPCM_BITS_16) {
4271 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4272 bps = 16;
4273 }
4274 if (wcaps & AC_WCAP_DIGITAL) {
4275 if (val & AC_SUPPCM_BITS_32)
4276 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4277 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4278 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4279 if (val & AC_SUPPCM_BITS_24)
4280 bps = 24;
4281 else if (val & AC_SUPPCM_BITS_20)
4282 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004283 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4284 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4286 if (val & AC_SUPPCM_BITS_32)
4287 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 else if (val & AC_SUPPCM_BITS_24)
4289 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004290 else if (val & AC_SUPPCM_BITS_20)
4291 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
4293 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004294#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004295 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004297 if (!bps)
4298 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004299 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004300#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004301 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004302 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 /* temporary hack: we have still no proper support
4304 * for the direct AC3 stream...
4305 */
4306 formats |= SNDRV_PCM_FMTBIT_U8;
4307 bps = 8;
4308 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004309 if (formats == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004310 codec_err(codec,
4311 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4312 nid, val,
4313 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4314 streams);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004315 return -EIO;
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (formatsp)
4318 *formatsp = formats;
4319 if (bpsp)
4320 *bpsp = bps;
4321 }
4322
4323 return 0;
4324}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004325EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004328 * snd_hda_is_supported_format - Check the validity of the format
4329 * @codec: HD-audio codec
4330 * @nid: NID to check
4331 * @format: the HD-audio format value to check
4332 *
4333 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 *
4335 * Returns 1 if supported, 0 if not.
4336 */
4337int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4338 unsigned int format)
4339{
4340 int i;
4341 unsigned int val = 0, rate, stream;
4342
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004343 val = query_pcm_param(codec, nid);
4344 if (!val)
4345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004348 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004349 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (val & (1 << i))
4351 break;
4352 return 0;
4353 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004354 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return 0;
4356
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004357 stream = query_stream_param(codec, nid);
4358 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return 0;
4360
4361 if (stream & AC_SUPFMT_PCM) {
4362 switch (format & 0xf0) {
4363 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004364 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366 break;
4367 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004368 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return 0;
4370 break;
4371 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004372 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return 0;
4374 break;
4375 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004376 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return 0;
4378 break;
4379 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004380 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return 0;
4382 break;
4383 default:
4384 return 0;
4385 }
4386 } else {
4387 /* FIXME: check for float32 and AC3? */
4388 }
4389
4390 return 1;
4391}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004392EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394/*
4395 * PCM stuff
4396 */
4397static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4398 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004399 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
4401 return 0;
4402}
4403
4404static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4405 struct hda_codec *codec,
4406 unsigned int stream_tag,
4407 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004408 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
4410 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4411 return 0;
4412}
4413
4414static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004416 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Takashi Iwai888afa12008-03-18 09:57:50 +01004418 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return 0;
4420}
4421
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004422static int set_pcm_default_values(struct hda_codec *codec,
4423 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004425 int err;
4426
Takashi Iwai0ba21762007-04-16 11:29:14 +02004427 /* query support PCM information from the given NID */
4428 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004429 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004430 info->rates ? NULL : &info->rates,
4431 info->formats ? NULL : &info->formats,
4432 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004433 if (err < 0)
4434 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436 if (info->ops.open == NULL)
4437 info->ops.open = hda_pcm_default_open_close;
4438 if (info->ops.close == NULL)
4439 info->ops.close = hda_pcm_default_open_close;
4440 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004441 if (snd_BUG_ON(!info->nid))
4442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 info->ops.prepare = hda_pcm_default_prepare;
4444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004446 if (snd_BUG_ON(!info->nid))
4447 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 info->ops.cleanup = hda_pcm_default_cleanup;
4449 }
4450 return 0;
4451}
4452
Takashi Iwaieb541332010-08-06 13:48:11 +02004453/*
4454 * codec prepare/cleanup entries
4455 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01004456/**
4457 * snd_hda_codec_prepare - Prepare a stream
4458 * @codec: the HDA codec
4459 * @hinfo: PCM information
4460 * @stream: stream tag to assign
4461 * @format: format id to assign
4462 * @substream: PCM substream to assign
4463 *
4464 * Calls the prepare callback set by the codec with the given arguments.
4465 * Clean up the inactive streams when successful.
4466 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004467int snd_hda_codec_prepare(struct hda_codec *codec,
4468 struct hda_pcm_stream *hinfo,
4469 unsigned int stream,
4470 unsigned int format,
4471 struct snd_pcm_substream *substream)
4472{
4473 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004474 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004475 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4476 if (ret >= 0)
4477 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004478 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004479 return ret;
4480}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004481EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02004482
Takashi Iwai95a962c2014-10-29 16:03:58 +01004483/**
4484 * snd_hda_codec_cleanup - Prepare a stream
4485 * @codec: the HDA codec
4486 * @hinfo: PCM information
4487 * @substream: PCM substream
4488 *
4489 * Calls the cleanup callback set by the codec with the given arguments.
4490 */
Takashi Iwaieb541332010-08-06 13:48:11 +02004491void snd_hda_codec_cleanup(struct hda_codec *codec,
4492 struct hda_pcm_stream *hinfo,
4493 struct snd_pcm_substream *substream)
4494{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004495 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004496 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004497 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004498}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004499EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02004500
Takashi Iwaid5191e52009-11-16 14:58:17 +01004501/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004502const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4503 "Audio", "SPDIF", "HDMI", "Modem"
4504};
4505
Takashi Iwai176d5332008-07-30 15:01:44 +02004506/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004507 * get the empty PCM device number to assign
4508 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004509static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004510{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004511 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004512 /* assigned to static slots up to dev#10; if more needed, assign
4513 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4514 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004515 static int audio_idx[HDA_PCM_NTYPES][5] = {
4516 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4517 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004518 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004519 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004520 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004521 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004522
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004523 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004524 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004525 return -EINVAL;
4526 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004527
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004528 for (i = 0; audio_idx[type][i] >= 0; i++) {
4529#ifndef CONFIG_SND_DYNAMIC_MINORS
4530 if (audio_idx[type][i] >= 8)
4531 break;
4532#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004533 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4534 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004535 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004536
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004537#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004538 /* non-fixed slots starting from 10 */
4539 for (i = 10; i < 32; i++) {
4540 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4541 return i;
4542 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004543#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004544
Takashi Iwai4e76a882014-02-25 12:21:03 +01004545 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004546 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004547#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01004548 dev_warn(bus->card->dev,
4549 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02004550#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004551 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004552}
4553
4554/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004555 * attach a new PCM stream
4556 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004557static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004558{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004559 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004560 struct hda_pcm_stream *info;
4561 int stream, err;
4562
Takashi Iwaib91f0802008-11-04 08:43:08 +01004563 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004564 return -EINVAL;
4565 for (stream = 0; stream < 2; stream++) {
4566 info = &pcm->stream[stream];
4567 if (info->substreams) {
4568 err = set_pcm_default_values(codec, info);
4569 if (err < 0)
4570 return err;
4571 }
4572 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004573 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004574}
4575
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004576/* assign all PCMs of the given codec */
4577int snd_hda_codec_build_pcms(struct hda_codec *codec)
4578{
4579 unsigned int pcm;
4580 int err;
4581
4582 if (!codec->num_pcms) {
4583 if (!codec->patch_ops.build_pcms)
4584 return 0;
4585 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004586 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004587 codec_err(codec,
4588 "cannot build PCMs for #%d (error %d)\n",
4589 codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004590 err = snd_hda_codec_reset(codec);
4591 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004592 codec_err(codec,
4593 "cannot revert codec\n");
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004594 return err;
4595 }
4596 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004597 }
4598 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4599 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4600 int dev;
4601
4602 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004603 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004604
4605 if (!cpcm->pcm) {
4606 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4607 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004608 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004609 cpcm->device = dev;
4610 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004611 if (err < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004612 codec_err(codec,
4613 "cannot attach PCM stream %d for codec #%d\n",
4614 dev, codec->addr);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004615 continue; /* no fatal error */
4616 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004617 }
4618 }
4619 return 0;
4620}
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622/**
4623 * snd_hda_build_pcms - build PCM information
4624 * @bus: the BUS
4625 *
4626 * Create PCM information for each codec included in the bus.
4627 *
4628 * The build_pcms codec patch is requested to set up codec->num_pcms and
4629 * codec->pcm_info properly. The array is referred by the top-level driver
4630 * to create its PCM instances.
4631 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4632 * callback.
4633 *
4634 * At least, substreams, channels_min and channels_max must be filled for
4635 * each stream. substreams = 0 indicates that the stream doesn't exist.
4636 * When rates and/or formats are zero, the supported values are queried
4637 * from the given nid. The nid is used also by the default ops.prepare
4638 * and ops.cleanup callbacks.
4639 *
4640 * The driver needs to call ops.open in its open callback. Similarly,
4641 * ops.close is supposed to be called in the close callback.
4642 * ops.prepare should be called in the prepare or hw_params callback
4643 * with the proper parameters for set up.
4644 * ops.cleanup should be called in hw_free for clean up of streams.
4645 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004646 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004648int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004650 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Takashi Iwai0ba21762007-04-16 11:29:14 +02004652 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004653 int err = snd_hda_codec_build_pcms(codec);
4654 if (err < 0)
4655 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657 return 0;
4658}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004659EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 * snd_hda_add_new_ctls - create controls from the array
4663 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004664 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 *
4666 * This helper function creates and add new controls in the given array.
4667 * The array must be terminated with an empty entry as terminator.
4668 *
4669 * Returns 0 if successful, or a negative error code.
4670 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004671int snd_hda_add_new_ctls(struct hda_codec *codec,
4672 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004674 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004677 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004678 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004679 if (knew->iface == -1) /* skip this codec private value */
4680 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004681 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004682 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004683 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004684 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004685 if (addr > 0)
4686 kctl->id.device = addr;
4687 if (idx > 0)
4688 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004689 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004690 if (!err)
4691 break;
4692 /* try first with another device index corresponding to
4693 * the codec addr; if it still fails (or it's the
4694 * primary codec), then try another control index
4695 */
4696 if (!addr && codec->addr)
4697 addr = codec->addr;
4698 else if (!idx && !knew->index) {
4699 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004700 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004701 if (idx <= 0)
4702 return err;
4703 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004704 return err;
4705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707 return 0;
4708}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004709EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Takashi Iwai83012a72012-08-24 18:38:08 +02004711#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004712static void hda_power_work(struct work_struct *work)
4713{
4714 struct hda_codec *codec =
4715 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004716 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004717 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004718
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004719 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004720 if (codec->power_transition > 0) { /* during power-up sequence? */
4721 spin_unlock(&codec->power_lock);
4722 return;
4723 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004724 if (!codec->power_on || codec->power_count) {
4725 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004726 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004727 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004728 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004729 spin_unlock(&codec->power_lock);
4730
Dylan Reidd17344b2012-09-28 15:57:01 -07004731 state = hda_call_codec_suspend(codec, true);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004732 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4733 hda_call_pm_notify(codec, false);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004734}
4735
4736static void hda_keep_power_on(struct hda_codec *codec)
4737{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004738 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004739 codec->power_count++;
4740 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004741 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004742 spin_unlock(&codec->power_lock);
Takashi Iwaia40e0a82013-11-20 12:41:20 +01004743 hda_call_pm_notify(codec, true);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004744}
4745
Takashi Iwaid5191e52009-11-16 14:58:17 +01004746/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004747void snd_hda_update_power_acct(struct hda_codec *codec)
4748{
4749 unsigned long delta = jiffies - codec->power_jiffies;
4750 if (codec->power_on)
4751 codec->power_on_acct += delta;
4752 else
4753 codec->power_off_acct += delta;
4754 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004755}
4756
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004757/* Transition to powered up, if wait_power_down then wait for a pending
4758 * transition to D3 to complete. A pending D3 transition is indicated
4759 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004760/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004761static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004762{
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004763 /* Return if power_on or transitioning to power_on, unless currently
4764 * powering down. */
4765 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004766 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004767 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004768 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004769
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004770 cancel_delayed_work_sync(&codec->power_work);
4771
4772 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004773 /* If the power down delayed work was cancelled above before starting,
4774 * then there is no need to go through power up here.
4775 */
4776 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004777 if (codec->power_transition < 0)
4778 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004779 return;
4780 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004781
Takashi Iwaid66fee52011-08-02 15:39:31 +02004782 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004783 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004785 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004786 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004787 spin_unlock(&codec->power_lock);
4788
Takashi Iwaicb53c622007-08-10 17:21:45 +02004789 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004790
4791 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004792 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004794
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004795#define power_save(codec) \
4796 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004797
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004798/* Transition to powered down */
4799static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004800{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004801 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004802 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004803
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004804 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004805 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004806 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004807 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004808 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004809}
4810
4811/**
4812 * snd_hda_power_save - Power-up/down/sync the codec
4813 * @codec: HD-audio codec
4814 * @delta: the counter delta to change
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004815 * @d3wait: sync for D3 transition complete
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004816 *
4817 * Change the power-up counter via @delta, and power up or down the hardware
4818 * appropriately. For the power-down, queue to the delayed action.
4819 * Passing zero to @delta means to synchronize the power state.
4820 */
4821void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4822{
4823 spin_lock(&codec->power_lock);
4824 codec->power_count += delta;
4825 trace_hda_power_count(codec);
4826 if (delta > 0)
4827 __snd_hda_power_up(codec, d3wait);
4828 else
4829 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004830 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004831}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004832EXPORT_SYMBOL_GPL(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004833
Takashi Iwaid5191e52009-11-16 14:58:17 +01004834/**
4835 * snd_hda_check_amp_list_power - Check the amp list and update the power
4836 * @codec: HD-audio codec
4837 * @check: the object containing an AMP list and the status
4838 * @nid: NID to check / update
4839 *
4840 * Check whether the given NID is in the amp list. If it's in the list,
4841 * check the current AMP status, and update the the power-status according
4842 * to the mute status.
4843 *
4844 * This function is supposed to be set or called from the check_power_status
4845 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004846 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004847int snd_hda_check_amp_list_power(struct hda_codec *codec,
4848 struct hda_loopback_check *check,
4849 hda_nid_t nid)
4850{
Takashi Iwai031024e2011-05-02 11:29:30 +02004851 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004852 int ch, v;
4853
4854 if (!check->amplist)
4855 return 0;
4856 for (p = check->amplist; p->nid; p++) {
4857 if (p->nid == nid)
4858 break;
4859 }
4860 if (!p->nid)
4861 return 0; /* nothing changed */
4862
4863 for (p = check->amplist; p->nid; p++) {
4864 for (ch = 0; ch < 2; ch++) {
4865 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4866 p->idx);
4867 if (!(v & HDA_AMP_MUTE) && v > 0) {
4868 if (!check->power_on) {
4869 check->power_on = 1;
4870 snd_hda_power_up(codec);
4871 }
4872 return 1;
4873 }
4874 }
4875 }
4876 if (check->power_on) {
4877 check->power_on = 0;
4878 snd_hda_power_down(codec);
4879 }
4880 return 0;
4881}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004882EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004885/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004886 * Channel mode helper
4887 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004888
4889/**
4890 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004891 * @codec: the HDA codec
4892 * @uinfo: pointer to get/store the data
4893 * @chmode: channel mode array
4894 * @num_chmodes: channel mode array size
Takashi Iwaid5191e52009-11-16 14:58:17 +01004895 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004896int snd_hda_ch_mode_info(struct hda_codec *codec,
4897 struct snd_ctl_elem_info *uinfo,
4898 const struct hda_channel_mode *chmode,
4899 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004900{
4901 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4902 uinfo->count = 1;
4903 uinfo->value.enumerated.items = num_chmodes;
4904 if (uinfo->value.enumerated.item >= num_chmodes)
4905 uinfo->value.enumerated.item = num_chmodes - 1;
4906 sprintf(uinfo->value.enumerated.name, "%dch",
4907 chmode[uinfo->value.enumerated.item].channels);
4908 return 0;
4909}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004910EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004911
Takashi Iwaid5191e52009-11-16 14:58:17 +01004912/**
4913 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004914 * @codec: the HDA codec
4915 * @ucontrol: pointer to get/store the data
4916 * @chmode: channel mode array
4917 * @num_chmodes: channel mode array size
4918 * @max_channels: max number of channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004919 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004920int snd_hda_ch_mode_get(struct hda_codec *codec,
4921 struct snd_ctl_elem_value *ucontrol,
4922 const struct hda_channel_mode *chmode,
4923 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004924 int max_channels)
4925{
4926 int i;
4927
4928 for (i = 0; i < num_chmodes; i++) {
4929 if (max_channels == chmode[i].channels) {
4930 ucontrol->value.enumerated.item[0] = i;
4931 break;
4932 }
4933 }
4934 return 0;
4935}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004936EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004937
Takashi Iwaid5191e52009-11-16 14:58:17 +01004938/**
4939 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004940 * @codec: the HDA codec
4941 * @ucontrol: pointer to get/store the data
4942 * @chmode: channel mode array
4943 * @num_chmodes: channel mode array size
4944 * @max_channelsp: pointer to store the max channels
Takashi Iwaid5191e52009-11-16 14:58:17 +01004945 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004946int snd_hda_ch_mode_put(struct hda_codec *codec,
4947 struct snd_ctl_elem_value *ucontrol,
4948 const struct hda_channel_mode *chmode,
4949 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004950 int *max_channelsp)
4951{
4952 unsigned int mode;
4953
4954 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004955 if (mode >= num_chmodes)
4956 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004957 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004958 return 0;
4959 /* change the current channel setting */
4960 *max_channelsp = chmode[mode].channels;
4961 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004962 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004963 return 1;
4964}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004965EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967/*
4968 * input MUX helper
4969 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004970
4971/**
4972 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004973 * @imux: imux helper object
4974 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01004975 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004976int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4977 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
4979 unsigned int index;
4980
4981 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4982 uinfo->count = 1;
4983 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004984 if (!imux->num_items)
4985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 index = uinfo->value.enumerated.item;
4987 if (index >= imux->num_items)
4988 index = imux->num_items - 1;
4989 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4990 return 0;
4991}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004992EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Takashi Iwaid5191e52009-11-16 14:58:17 +01004994/**
4995 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004996 * @codec: the HDA codec
4997 * @imux: imux helper object
4998 * @ucontrol: pointer to get/store the data
4999 * @nid: input mux NID
5000 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01005001 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005002int snd_hda_input_mux_put(struct hda_codec *codec,
5003 const struct hda_input_mux *imux,
5004 struct snd_ctl_elem_value *ucontrol,
5005 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 unsigned int *cur_val)
5007{
5008 unsigned int idx;
5009
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005010 if (!imux->num_items)
5011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 idx = ucontrol->value.enumerated.item[0];
5013 if (idx >= imux->num_items)
5014 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005015 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005017 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5018 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 *cur_val = idx;
5020 return 1;
5021}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005022EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
5024
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005025/**
5026 * snd_hda_enum_helper_info - Helper for simple enum ctls
5027 * @kcontrol: ctl element
5028 * @uinfo: pointer to get/store the data
5029 * @num_items: number of enum items
5030 * @texts: enum item string array
5031 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01005032 * process kcontrol info callback of a simple string enum array
5033 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5034 */
5035int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5036 struct snd_ctl_elem_info *uinfo,
5037 int num_items, const char * const *texts)
5038{
5039 static const char * const texts_default[] = {
5040 "Disabled", "Enabled"
5041 };
5042
5043 if (!texts || !num_items) {
5044 num_items = 2;
5045 texts = texts_default;
5046 }
5047
Takashi Iwai3ff72212014-10-20 18:17:28 +02005048 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005049}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005050EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01005051
5052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 * Multi-channel / digital-out PCM helper functions
5054 */
5055
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005056/* setup SPDIF output stream */
5057static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5058 unsigned int stream_tag, unsigned int format)
5059{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005060 struct hda_spdif_out *spdif;
5061 unsigned int curr_fmt;
5062 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005063
Laurence Darby3bef1c32012-11-03 17:00:06 +00005064 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5065 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5066 AC_VERB_GET_STREAM_FORMAT, 0);
5067 reset = codec->spdif_status_reset &&
5068 (spdif->ctls & AC_DIG1_ENABLE) &&
5069 curr_fmt != format;
5070
5071 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5072 updated */
5073 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005074 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005075 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005076 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005077 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005078 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005079 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005080 for (d = codec->slave_dig_outs; *d; d++)
5081 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5082 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005083 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005084 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005085 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005086 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005087 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005088}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005089
Takashi Iwai2f728532008-09-25 16:32:41 +02005090static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5091{
5092 snd_hda_codec_cleanup_stream(codec, nid);
5093 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005094 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005095 for (d = codec->slave_dig_outs; *d; d++)
5096 snd_hda_codec_cleanup_stream(codec, *d);
5097 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005098}
5099
Takashi Iwaid5191e52009-11-16 14:58:17 +01005100/**
5101 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5102 * @bus: HD-audio bus
5103 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005104void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5105{
5106 struct hda_codec *codec;
5107
5108 if (!bus)
5109 return;
5110 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005111 if (hda_codec_is_power_on(codec) &&
5112 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005113 codec->patch_ops.reboot_notify(codec);
5114 }
5115}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005116EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005117
Takashi Iwaid5191e52009-11-16 14:58:17 +01005118/**
5119 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005120 * @codec: the HDA codec
5121 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005123int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5124 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
Ingo Molnar62932df2006-01-16 16:34:20 +01005126 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005127 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5128 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005129 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005131 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 return 0;
5133}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005134EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Takashi Iwaid5191e52009-11-16 14:58:17 +01005136/**
5137 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
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 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005144int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5145 struct hda_multi_out *mout,
5146 unsigned int stream_tag,
5147 unsigned int format,
5148 struct snd_pcm_substream *substream)
5149{
5150 mutex_lock(&codec->spdif_mutex);
5151 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5152 mutex_unlock(&codec->spdif_mutex);
5153 return 0;
5154}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005155EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005156
Takashi Iwaid5191e52009-11-16 14:58:17 +01005157/**
5158 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005159 * @codec: the HDA codec
5160 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01005161 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005162int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5163 struct hda_multi_out *mout)
5164{
5165 mutex_lock(&codec->spdif_mutex);
5166 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5167 mutex_unlock(&codec->spdif_mutex);
5168 return 0;
5169}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005170EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01005171
Takashi Iwaid5191e52009-11-16 14:58:17 +01005172/**
5173 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005174 * @codec: the HDA codec
5175 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005177int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5178 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
Ingo Molnar62932df2006-01-16 16:34:20 +01005180 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005182 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 return 0;
5184}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005185EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Takashi Iwaid5191e52009-11-16 14:58:17 +01005187/**
5188 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005189 * @codec: the HDA codec
5190 * @mout: hda_multi_out object
5191 * @substream: PCM substream to assign
5192 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005193 *
5194 * Open analog outputs and set up the hw-constraints.
5195 * If the digital outputs can be opened as slave, open the digital
5196 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005198int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5199 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005200 struct snd_pcm_substream *substream,
5201 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
Takashi Iwai9a081602008-02-12 18:37:26 +01005203 struct snd_pcm_runtime *runtime = substream->runtime;
5204 runtime->hw.channels_max = mout->max_channels;
5205 if (mout->dig_out_nid) {
5206 if (!mout->analog_rates) {
5207 mout->analog_rates = hinfo->rates;
5208 mout->analog_formats = hinfo->formats;
5209 mout->analog_maxbps = hinfo->maxbps;
5210 } else {
5211 runtime->hw.rates = mout->analog_rates;
5212 runtime->hw.formats = mout->analog_formats;
5213 hinfo->maxbps = mout->analog_maxbps;
5214 }
5215 if (!mout->spdif_rates) {
5216 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5217 &mout->spdif_rates,
5218 &mout->spdif_formats,
5219 &mout->spdif_maxbps);
5220 }
5221 mutex_lock(&codec->spdif_mutex);
5222 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005223 if ((runtime->hw.rates & mout->spdif_rates) &&
5224 (runtime->hw.formats & mout->spdif_formats)) {
5225 runtime->hw.rates &= mout->spdif_rates;
5226 runtime->hw.formats &= mout->spdif_formats;
5227 if (mout->spdif_maxbps < hinfo->maxbps)
5228 hinfo->maxbps = mout->spdif_maxbps;
5229 } else {
5230 mout->share_spdif = 0;
5231 /* FIXME: need notify? */
5232 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005233 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005234 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5237 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5238}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005239EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
Takashi Iwaid5191e52009-11-16 14:58:17 +01005241/**
5242 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005243 * @codec: the HDA codec
5244 * @mout: hda_multi_out object
5245 * @stream_tag: stream tag to assign
5246 * @format: format id to assign
5247 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01005248 *
5249 * Set up the i/o for analog out.
5250 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005252int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5253 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 unsigned int stream_tag,
5255 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005256 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Takashi Iwaidda14412011-05-02 11:29:30 +02005258 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005260 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 int i;
5262
Ingo Molnar62932df2006-01-16 16:34:20 +01005263 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005264 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005265 if (mout->dig_out_nid && mout->share_spdif &&
5266 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005268 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5269 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005270 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005272 setup_dig_out_stream(codec, mout->dig_out_nid,
5273 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 } else {
5275 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005276 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
5278 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005279 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
5281 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005282 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5283 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005284 if (!mout->no_share_stream &&
5285 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005287 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5288 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005289 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005290 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5291 if (!mout->no_share_stream && mout->hp_out_nid[i])
5292 snd_hda_codec_setup_stream(codec,
5293 mout->hp_out_nid[i],
5294 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 /* surrounds */
5297 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005298 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005299 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5300 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005301 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005302 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5303 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
David Henningssoncd4035e2013-10-10 09:01:25 +02005305
5306 /* extra surrounds */
5307 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5308 int ch = 0;
5309 if (!mout->extra_out_nid[i])
5310 break;
5311 if (chs >= (i + 1) * 2)
5312 ch = i * 2;
5313 else if (!mout->no_share_stream)
5314 break;
5315 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5316 stream_tag, ch, format);
5317 }
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 return 0;
5320}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005321EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
Takashi Iwaid5191e52009-11-16 14:58:17 +01005323/**
5324 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005325 * @codec: the HDA codec
5326 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005328int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5329 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
Takashi Iwaidda14412011-05-02 11:29:30 +02005331 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 int i;
5333
5334 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005335 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005337 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005338 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5339 if (mout->hp_out_nid[i])
5340 snd_hda_codec_cleanup_stream(codec,
5341 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005342 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5343 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005344 snd_hda_codec_cleanup_stream(codec,
5345 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005346 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005348 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 mout->dig_out_used = 0;
5350 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005351 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 return 0;
5353}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005354EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Takashi Iwai47408602012-04-20 13:06:53 +02005356/**
5357 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005358 * @codec: the HDA codec
5359 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02005360 *
5361 * Guess the suitable VREF pin bits to be set as the pin-control value.
5362 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5363 */
5364unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5365{
5366 unsigned int pincap;
5367 unsigned int oldval;
5368 oldval = snd_hda_codec_read(codec, pin, 0,
5369 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5370 pincap = snd_hda_query_pin_caps(codec, pin);
5371 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5372 /* Exception: if the default pin setup is vref50, we give it priority */
5373 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5374 return AC_PINCTL_VREF_80;
5375 else if (pincap & AC_PINCAP_VREF_50)
5376 return AC_PINCTL_VREF_50;
5377 else if (pincap & AC_PINCAP_VREF_100)
5378 return AC_PINCTL_VREF_100;
5379 else if (pincap & AC_PINCAP_VREF_GRD)
5380 return AC_PINCTL_VREF_GRD;
5381 return AC_PINCTL_VREF_HIZ;
5382}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005383EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02005384
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005385/**
5386 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5387 * @codec: the HDA codec
5388 * @pin: referred pin NID
5389 * @val: pin ctl value to audit
5390 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005391unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5392 hda_nid_t pin, unsigned int val)
5393{
5394 static unsigned int cap_lists[][2] = {
5395 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5396 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5397 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5398 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5399 };
5400 unsigned int cap;
5401
5402 if (!val)
5403 return 0;
5404 cap = snd_hda_query_pin_caps(codec, pin);
5405 if (!cap)
5406 return val; /* don't know what to do... */
5407
5408 if (val & AC_PINCTL_OUT_EN) {
5409 if (!(cap & AC_PINCAP_OUT))
5410 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5411 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5412 val &= ~AC_PINCTL_HP_EN;
5413 }
5414
5415 if (val & AC_PINCTL_IN_EN) {
5416 if (!(cap & AC_PINCAP_IN))
5417 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5418 else {
5419 unsigned int vcap, vref;
5420 int i;
5421 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5422 vref = val & AC_PINCTL_VREFEN;
5423 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5424 if (vref == cap_lists[i][0] &&
5425 !(vcap & cap_lists[i][1])) {
5426 if (i == ARRAY_SIZE(cap_lists) - 1)
5427 vref = AC_PINCTL_VREF_HIZ;
5428 else
5429 vref = cap_lists[i + 1][0];
5430 }
5431 }
5432 val &= ~AC_PINCTL_VREFEN;
5433 val |= vref;
5434 }
5435 }
5436
5437 return val;
5438}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005439EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005440
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005441/**
5442 * _snd_hda_pin_ctl - Helper to set pin ctl value
5443 * @codec: the HDA codec
5444 * @pin: referred pin NID
5445 * @val: pin control value to set
5446 * @cached: access over codec pinctl cache or direct write
5447 *
5448 * This function is a helper to set a pin ctl value more safely.
5449 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5450 * value in pin target array via snd_hda_codec_set_pin_target(), then
5451 * actually writes the value via either snd_hda_codec_update_cache() or
5452 * snd_hda_codec_write() depending on @cached flag.
5453 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005454int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5455 unsigned int val, bool cached)
5456{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005457 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005458 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005459 if (cached)
5460 return snd_hda_codec_update_cache(codec, pin, 0,
5461 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5462 else
5463 return snd_hda_codec_write(codec, pin, 0,
5464 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5465}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005466EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005467
Takashi Iwai990061c2010-09-09 22:08:44 +02005468/**
5469 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01005470 * @codec: the HDA codec
5471 * @imux: imux helper object
5472 * @label: the name of imux item to assign
5473 * @index: index number of imux item to assign
5474 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02005475 *
5476 * When the same label is used already in the existing items, the number
5477 * suffix is appended to the label. This label index number is stored
5478 * to type_idx when non-NULL pointer is given.
5479 */
Takashi Iwai6194b992014-06-06 18:12:16 +02005480int snd_hda_add_imux_item(struct hda_codec *codec,
5481 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02005482 int index, int *type_idx)
5483{
5484 int i, label_idx = 0;
5485 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02005486 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02005487 return -EINVAL;
5488 }
5489 for (i = 0; i < imux->num_items; i++) {
5490 if (!strncmp(label, imux->items[i].label, strlen(label)))
5491 label_idx++;
5492 }
5493 if (type_idx)
5494 *type_idx = label_idx;
5495 if (label_idx > 0)
5496 snprintf(imux->items[imux->num_items].label,
5497 sizeof(imux->items[imux->num_items].label),
5498 "%s %d", label, label_idx);
5499 else
5500 strlcpy(imux->items[imux->num_items].label, label,
5501 sizeof(imux->items[imux->num_items].label));
5502 imux->items[imux->num_items].index = index;
5503 imux->num_items++;
5504 return 0;
5505}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005506EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005507
Takashi Iwai4a471b72005-12-07 13:56:29 +01005508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509#ifdef CONFIG_PM
5510/*
5511 * power management
5512 */
5513
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005514
5515static void hda_async_suspend(void *data, async_cookie_t cookie)
5516{
5517 hda_call_codec_suspend(data, false);
5518}
5519
5520static void hda_async_resume(void *data, async_cookie_t cookie)
5521{
5522 hda_call_codec_resume(data);
5523}
5524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525/**
5526 * snd_hda_suspend - suspend the codecs
5527 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 *
5529 * Returns 0 if successful.
5530 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005531int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005533 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005534 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Takashi Iwai0ba21762007-04-16 11:29:14 +02005536 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005537 cancel_delayed_work_sync(&codec->jackpoll_work);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005538 if (hda_codec_is_power_on(codec)) {
5539 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005540 async_schedule_domain(hda_async_suspend, codec,
5541 &domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005542 else
5543 hda_call_codec_suspend(codec, false);
5544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005546
5547 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005548 async_synchronize_full_domain(&domain);
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05005549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 return 0;
5551}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005552EXPORT_SYMBOL_GPL(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554/**
5555 * snd_hda_resume - resume the codecs
5556 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 *
5558 * Returns 0 if successful.
5559 */
5560int snd_hda_resume(struct hda_bus *bus)
5561{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005562 struct hda_codec *codec;
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005563 ASYNC_DOMAIN_EXCLUSIVE(domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Takashi Iwai0ba21762007-04-16 11:29:14 +02005565 list_for_each_entry(codec, &bus->codec_list, list) {
Mengdong Lin12edb892013-11-26 23:32:23 -05005566 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005567 async_schedule_domain(hda_async_resume, codec, &domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005568 else
5569 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 }
Mengdong Lin12edb892013-11-26 23:32:23 -05005571
5572 if (bus->num_codecs > 1)
Takashi Iwaif4d6a552013-12-05 11:55:05 +01005573 async_synchronize_full_domain(&domain);
Mengdong Lin12edb892013-11-26 23:32:23 -05005574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return 0;
5576}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005577EXPORT_SYMBOL_GPL(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005578#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005579
5580/*
5581 * generic arrays
5582 */
5583
Takashi Iwaid5191e52009-11-16 14:58:17 +01005584/**
5585 * snd_array_new - get a new element from the given array
5586 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005587 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005588 * Get a new element from the given array. If it exceeds the
5589 * pre-allocated array size, re-allocate the array.
5590 *
5591 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005592 */
5593void *snd_array_new(struct snd_array *array)
5594{
Takashi Iwai12f17712012-10-10 08:59:14 +02005595 if (snd_BUG_ON(!array->elem_size))
5596 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005597 if (array->used >= array->alloced) {
5598 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005599 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005600 void *nlist;
5601 if (snd_BUG_ON(num >= 4096))
5602 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005603 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005604 if (!nlist)
5605 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005606 array->list = nlist;
5607 array->alloced = num;
5608 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005609 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005610}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005611EXPORT_SYMBOL_GPL(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005612
Takashi Iwaid5191e52009-11-16 14:58:17 +01005613/**
5614 * snd_array_free - free the given array elements
5615 * @array: the array object
5616 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005617void snd_array_free(struct snd_array *array)
5618{
5619 kfree(array->list);
5620 array->used = 0;
5621 array->alloced = 0;
5622 array->list = NULL;
5623}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005624EXPORT_SYMBOL_GPL(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005625
Takashi Iwaid5191e52009-11-16 14:58:17 +01005626/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005627 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5628 * @pcm: PCM caps bits
5629 * @buf: the string buffer to write
5630 * @buflen: the max buffer length
5631 *
5632 * used by hda_proc.c and hda_eld.c
5633 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005634void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5635{
5636 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5637 int i, j;
5638
5639 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5640 if (pcm & (AC_SUPPCM_BITS_8 << i))
5641 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5642
5643 buf[j] = '\0'; /* necessary when j == 0 */
5644}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005645EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005646
5647MODULE_DESCRIPTION("HDA codec core");
5648MODULE_LICENSE("GPL");