blob: 6f9b64700f6e48ec859bb7153a5bc119812062b4 [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>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100225 for (;;) {
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
228 if (err != -EAGAIN)
229 break;
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
232 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200233 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800234 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_get_response(codec, *res);
236 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->ops.bus_reset(bus);
245 }
246 goto again;
247 }
248 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200251 return err;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command and read the corresponding response.
263 *
264 * Returns the obtained response value, or -1 for an error.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int verb, unsigned int parm)
269{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
271 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 if (codec_exec_verb(codec, cmd, &res))
273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100276EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
285 *
286 * Send a single command without waiting for response.
287 *
288 * Returns 0 if successful, or a negative error code.
289 */
290int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
292{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100294 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
304 *
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
307 */
308void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309{
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100313EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
318 * @nid: NID to parse
319 * @start_id: the pointer to store the start NID
320 *
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200324int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
325 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int parm;
328
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200330 if (parm == -1)
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100335EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100337/* connection list element */
338struct hda_conn_list {
339 struct list_head list;
340 int len;
341 hda_nid_t nid;
342 hda_nid_t conns[0];
343};
344
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346static struct hda_conn_list *
347lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200348{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 struct hda_conn_list *p;
350 list_for_each_entry(p, &codec->conn_list, list) {
351 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 }
354 return NULL;
355}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
358 const hda_nid_t *list)
359{
360 struct hda_conn_list *p;
361
362 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
363 if (!p)
364 return -ENOMEM;
365 p->len = len;
366 p->nid = nid;
367 memcpy(p->conns, list, len * sizeof(hda_nid_t));
368 list_add(&p->list, &codec->conn_list);
369 return 0;
370}
371
372static void remove_conn_list(struct hda_codec *codec)
373{
374 while (!list_empty(&codec->conn_list)) {
375 struct hda_conn_list *p;
376 p = list_first_entry(&codec->conn_list, typeof(*p), list);
377 list_del(&p->list);
378 kfree(p);
379 }
380}
381
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200382/* read the connection and add to the cache */
383static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200384{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100385 hda_nid_t list[32];
386 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200387 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200388
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
393 if (!result)
394 return -ENOMEM;
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
396 }
397 if (len >= 0)
398 len = snd_hda_override_conn_list(codec, nid, len, result);
399 if (result != list)
400 kfree(result);
401 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200402}
Takashi Iwaidce20792011-06-24 14:10:28 +0200403
404/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
407 * @nid: NID to parse
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
410 *
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 *
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
419 */
420int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
422{
423 bool added = false;
424
425 for (;;) {
426 int err;
427 const struct hda_conn_list *p;
428
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
431 if (p) {
432 if (listp)
433 *listp = p->conns;
434 return p->len;
435 }
436 if (snd_BUG_ON(added))
437 return -EINVAL;
438
439 err = read_and_add_raw_conns(codec, nid);
440 if (err < 0)
441 return err;
442 added = true;
443 }
444}
445EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
446
447/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200448 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @codec: the HDA codec
450 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200451 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @max_conns: max. number of connections to store
453 *
454 * Parses the connection list of the given widget and stores the list
455 * of NIDs.
456 *
457 * Returns the number of connections, or a negative error code.
458 */
459int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200461{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
469 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 return -EINVAL;
471 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200473 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476}
477EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478
Takashi Iwai4eea3092013-02-07 18:18:19 +0100479/* return CONNLIST_LEN parameter of the given widget */
480static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481{
482 unsigned int wcaps = get_wcaps(codec, nid);
483 unsigned int parm;
484
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
487 return 0;
488
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
490 if (parm == -1)
491 parm = 0;
492 return parm;
493}
494
495int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100497 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498}
499
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200500/**
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
503 * @nid: NID to parse
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
506 *
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
510 */
511int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100515 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100518 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai4eea3092013-02-07 18:18:19 +0100520 parm = get_num_conns(codec, nid);
521 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200522 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (parm & AC_CLIST_LONG) {
525 /* long form */
526 shift = 16;
527 num_elems = 2;
528 } else {
529 /* short form */
530 shift = 8;
531 num_elems = 4;
532 }
533 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 mask = (1 << (shift-1)) - 1;
535
Takashi Iwai0ba21762007-04-16 11:29:14 +0200536 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0; /* no connection */
538
539 if (conn_len == 1) {
540 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200541 parm = snd_hda_codec_read(codec, nid, 0,
542 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200543 if (parm == -1 && codec->bus->rirb_error)
544 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list)
546 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 1;
548 }
549
550 /* multi connection */
551 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = 0;
553 for (i = 0; i < conn_len; i++) {
554 int range_val;
555 hda_nid_t val, n;
556
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200557 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100564 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100565 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200566 snd_printk(KERN_WARNING "hda_codec: "
567 "invalid CONNECT_LIST verb %x[%i]:%x\n",
568 nid, i, parm);
569 return 0;
570 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 parm >>= shift;
572 if (range_val) {
573 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 if (!prev_nid || prev_nid >= val) {
575 snd_printk(KERN_WARNING "hda_codec: "
576 "invalid dep_range_val %x:%x\n",
577 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100578 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100581 if (conn_list) {
582 if (conns >= max_conns)
583 return -ENOSPC;
584 conn_list[conns] = n;
585 }
586 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100587 }
588 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100589 if (conn_list) {
590 if (conns >= max_conns)
591 return -ENOSPC;
592 conn_list[conns] = val;
593 }
594 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100596 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 return conns;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602 * snd_hda_override_conn_list - add/modify the connection-list to cache
603 * @codec: the HDA codec
604 * @nid: NID to parse
605 * @len: number of connection list entries
606 * @list: the list of connection entries
607 *
608 * Add or modify the given connection-list to the cache. If the corresponding
609 * cache already exists, invalidate it and append a new one.
610 *
611 * Returns zero or a negative error code.
612 */
613int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
614 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100616 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200617
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 p = lookup_conn_list(codec, nid);
619 if (p) {
620 list_del(&p->list);
621 kfree(p);
622 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200623
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100624 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200626EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
627
628/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200629 * snd_hda_get_conn_index - get the connection index of the given NID
630 * @codec: the HDA codec
631 * @mux: NID containing the list
632 * @nid: NID to select
633 * @recursive: 1 when searching NID recursively, otherwise 0
634 *
635 * Parses the connection list of the widget @mux and checks whether the
636 * widget @nid is present. If it is, return the connection index.
637 * Otherwise it returns -1.
638 */
639int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
640 hda_nid_t nid, int recursive)
641{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100642 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200643 int i, nums;
644
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100645 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 for (i = 0; i < nums; i++)
647 if (conn[i] == nid)
648 return i;
649 if (!recursive)
650 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100651 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200652 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
653 return -1;
654 }
655 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200656 for (i = 0; i < nums; i++) {
657 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
658 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
659 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
661 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200662 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 return -1;
664}
665EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200684 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200685 unsol = bus->unsol;
686 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688
689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
690 unsol->wp = wp;
691
692 wp <<= 1;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
695
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100700EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800703 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Howellsc4028952006-11-22 14:57:56 +0000705static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
David Howellsc4028952006-11-22 14:57:56 +0000707 struct hda_bus_unsolicited *unsol =
708 container_of(work, struct hda_bus_unsolicited, work);
709 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct hda_codec *codec;
711 unsigned int rp, caddr, res;
712
713 while (unsol->rp != unsol->wp) {
714 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
715 unsol->rp = rp;
716 rp <<= 1;
717 res = unsol->queue[rp];
718 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 continue;
721 codec = bus->caddr_tbl[caddr & 0x0f];
722 if (codec && codec->patch_ops.unsol_event)
723 codec->patch_ops.unsol_event(codec, res);
724 }
725}
726
727/*
728 * initialize unsolicited queue
729 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200730static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct hda_bus_unsolicited *unsol;
733
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100734 if (bus->unsol) /* already initialized */
735 return 0;
736
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200737 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200738 if (!unsol) {
739 snd_printk(KERN_ERR "hda_codec: "
740 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ENOMEM;
742 }
David Howellsc4028952006-11-22 14:57:56 +0000743 INIT_WORK(&unsol->work, process_unsol_events);
744 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 bus->unsol = unsol;
746 return 0;
747}
748
749/*
750 * destructor
751 */
752static void snd_hda_codec_free(struct hda_codec *codec);
753
754static int snd_hda_bus_free(struct hda_bus *bus)
755{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100760 if (bus->workq)
761 flush_workqueue(bus->workq);
762 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200764 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 snd_hda_codec_free(codec);
766 }
767 if (bus->ops.private_free)
768 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100769 if (bus->workq)
770 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 kfree(bus);
772 return 0;
773}
774
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100775static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100778 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return snd_hda_bus_free(bus);
780}
781
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200782#ifdef CONFIG_SND_HDA_HWDEP
783static int snd_hda_bus_dev_register(struct snd_device *device)
784{
785 struct hda_bus *bus = device->device_data;
786 struct hda_codec *codec;
787 list_for_each_entry(codec, &bus->codec_list, list) {
788 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100789 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200790 }
791 return 0;
792}
793#else
794#define snd_hda_bus_dev_register NULL
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/**
798 * snd_hda_bus_new - create a HDA bus
799 * @card: the card entry
800 * @temp: the template for hda_bus information
801 * @busp: the pointer to store the created bus instance
802 *
803 * Returns 0 if successful, or a negative error code.
804 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100805int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200806 const struct hda_bus_template *temp,
807 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct hda_bus *bus;
810 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100811 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200812 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .dev_free = snd_hda_bus_dev_free,
814 };
815
Takashi Iwaida3cec32008-08-08 17:12:14 +0200816 if (snd_BUG_ON(!temp))
817 return -EINVAL;
818 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (busp)
822 *busp = NULL;
823
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200824 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (bus == NULL) {
826 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
827 return -ENOMEM;
828 }
829
830 bus->card = card;
831 bus->private_data = temp->private_data;
832 bus->pci = temp->pci;
833 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100834 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 bus->ops = temp->ops;
836
Ingo Molnar62932df2006-01-16 16:34:20 +0100837 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200838 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 INIT_LIST_HEAD(&bus->codec_list);
840
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100841 snprintf(bus->workq_name, sizeof(bus->workq_name),
842 "hd-audio%d", card->number);
843 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100844 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100845 snd_printk(KERN_ERR "cannot create workqueue %s\n",
846 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100847 kfree(bus);
848 return -ENOMEM;
849 }
850
Takashi Iwai0ba21762007-04-16 11:29:14 +0200851 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
852 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 snd_hda_bus_free(bus);
854 return err;
855 }
856 if (busp)
857 *busp = bus;
858 return 0;
859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100860EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Takashi Iwai82467612007-07-27 19:15:54 +0200862#ifdef CONFIG_SND_HDA_GENERIC
863#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200864 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200865#else
866#define is_generic_config(codec) 0
867#endif
868
Takashi Iwai645f10c2008-11-28 15:07:37 +0100869#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100870#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
871#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100872#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * find a matching codec preset
877 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200878static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200879find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100881 struct hda_codec_preset_list *tbl;
882 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200883 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwai82467612007-07-27 19:15:54 +0200885 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100886 return NULL; /* use the generic parser */
887
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100888 again:
889 mutex_lock(&preset_mutex);
890 list_for_each_entry(tbl, &hda_preset_tables, list) {
891 if (!try_module_get(tbl->owner)) {
892 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
893 continue;
894 }
895 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100897 if (preset->afg && preset->afg != codec->afg)
898 continue;
899 if (preset->mfg && preset->mfg != codec->mfg)
900 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200901 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200903 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100905 preset->rev == codec->revision_id)) {
906 mutex_unlock(&preset_mutex);
907 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911 module_put(tbl->owner);
912 }
913 mutex_unlock(&preset_mutex);
914
915 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
916 char name[32];
917 if (!mod_requested)
918 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
919 codec->vendor_id);
920 else
921 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
922 (codec->vendor_id >> 16) & 0xffff);
923 request_module(name);
924 mod_requested++;
925 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 return NULL;
928}
929
930/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200931 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200933static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 const struct hda_vendor_id *c;
936 const char *vendor = NULL;
937 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200938 char tmp[16];
939
940 if (codec->vendor_name)
941 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 for (c = hda_vendor_ids; c->id; c++) {
944 if (c->id == vendor_id) {
945 vendor = c->name;
946 break;
947 }
948 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sprintf(tmp, "Generic %04x", vendor_id);
951 vendor = tmp;
952 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200953 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
954 if (!codec->vendor_name)
955 return -ENOMEM;
956
957 get_chip_name:
958 if (codec->chip_name)
959 return 0;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200962 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
963 else {
964 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
965 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
966 }
967 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200968 return -ENOMEM;
969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200973 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100975static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200977 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 hda_nid_t nid;
979
980 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
981 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200982 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200983 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200984 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 case AC_GRP_AUDIO_FUNCTION:
986 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200987 codec->afg_function_id = function_id & 0xff;
988 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200989 break;
990 case AC_GRP_MODEM_FUNCTION:
991 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200992 codec->mfg_function_id = function_id & 0xff;
993 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 break;
995 default:
996 break;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001002 * read widget caps for each widget and store in cache
1003 */
1004static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1005{
1006 int i;
1007 hda_nid_t nid;
1008
1009 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1010 &codec->start_nid);
1011 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001013 return -ENOMEM;
1014 nid = codec->start_nid;
1015 for (i = 0; i < codec->num_nodes; i++, nid++)
1016 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1017 AC_PAR_AUDIO_WIDGET_CAP);
1018 return 0;
1019}
1020
Takashi Iwai3be14142009-02-20 14:11:16 +01001021/* read all pin default configurations and save codec->init_pins */
1022static int read_pin_defaults(struct hda_codec *codec)
1023{
1024 int i;
1025 hda_nid_t nid = codec->start_nid;
1026
1027 for (i = 0; i < codec->num_nodes; i++, nid++) {
1028 struct hda_pincfg *pin;
1029 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001030 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001031 if (wid_type != AC_WID_PIN)
1032 continue;
1033 pin = snd_array_new(&codec->init_pins);
1034 if (!pin)
1035 return -ENOMEM;
1036 pin->nid = nid;
1037 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1038 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001039 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1040 AC_VERB_GET_PIN_WIDGET_CONTROL,
1041 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001042 }
1043 return 0;
1044}
1045
1046/* look up the given pin config list and return the item matching with NID */
1047static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1048 struct snd_array *array,
1049 hda_nid_t nid)
1050{
1051 int i;
1052 for (i = 0; i < array->used; i++) {
1053 struct hda_pincfg *pin = snd_array_elem(array, i);
1054 if (pin->nid == nid)
1055 return pin;
1056 }
1057 return NULL;
1058}
1059
Takashi Iwai3be14142009-02-20 14:11:16 +01001060/* set the current pin config value for the given NID.
1061 * the value is cached, and read via snd_hda_codec_get_pincfg()
1062 */
1063int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1064 hda_nid_t nid, unsigned int cfg)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001068 /* the check below may be invalid when pins are added by a fixup
1069 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1070 * for now
1071 */
1072 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001073 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1074 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001075 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001076
Takashi Iwai3be14142009-02-20 14:11:16 +01001077 pin = look_up_pincfg(codec, list, nid);
1078 if (!pin) {
1079 pin = snd_array_new(list);
1080 if (!pin)
1081 return -ENOMEM;
1082 pin->nid = nid;
1083 }
1084 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001085 return 0;
1086}
1087
Takashi Iwaid5191e52009-11-16 14:58:17 +01001088/**
1089 * snd_hda_codec_set_pincfg - Override a pin default configuration
1090 * @codec: the HDA codec
1091 * @nid: NID to set the pin config
1092 * @cfg: the pin default config value
1093 *
1094 * Override a pin default configuration value in the cache.
1095 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1096 * priority than the real hardware value.
1097 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001098int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1099 hda_nid_t nid, unsigned int cfg)
1100{
Takashi Iwai346ff702009-02-23 09:42:57 +01001101 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001102}
1103EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1104
Takashi Iwaid5191e52009-11-16 14:58:17 +01001105/**
1106 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1107 * @codec: the HDA codec
1108 * @nid: NID to get the pin config
1109 *
1110 * Get the current pin config value of the given pin NID.
1111 * If the pincfg value is cached or overridden via sysfs or driver,
1112 * returns the cached value.
1113 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001114unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1115{
1116 struct hda_pincfg *pin;
1117
Takashi Iwai3be14142009-02-20 14:11:16 +01001118#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001119 {
1120 unsigned int cfg = 0;
1121 mutex_lock(&codec->user_mutex);
1122 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1123 if (pin)
1124 cfg = pin->cfg;
1125 mutex_unlock(&codec->user_mutex);
1126 if (cfg)
1127 return cfg;
1128 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001129#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001130 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1131 if (pin)
1132 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001133 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1134 if (pin)
1135 return pin->cfg;
1136 return 0;
1137}
1138EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1139
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001140/* remember the current pinctl target value */
1141int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1142 unsigned int val)
1143{
1144 struct hda_pincfg *pin;
1145
1146 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1147 if (!pin)
1148 return -EINVAL;
1149 pin->target = val;
1150 return 0;
1151}
1152EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1153
1154/* return the current pinctl target value */
1155int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1156{
1157 struct hda_pincfg *pin;
1158
1159 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1160 if (!pin)
1161 return 0;
1162 return pin->target;
1163}
1164EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1165
Takashi Iwai92ee6162009-12-27 11:18:59 +01001166/**
1167 * snd_hda_shutup_pins - Shut up all pins
1168 * @codec: the HDA codec
1169 *
1170 * Clear all pin controls to shup up before suspend for avoiding click noise.
1171 * The controls aren't cached so that they can be resumed properly.
1172 */
1173void snd_hda_shutup_pins(struct hda_codec *codec)
1174{
1175 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001176 /* don't shut up pins when unloading the driver; otherwise it breaks
1177 * the default pin setup at the next load of the driver
1178 */
1179 if (codec->bus->shutdown)
1180 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001181 for (i = 0; i < codec->init_pins.used; i++) {
1182 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1183 /* use read here for syncing after issuing each verb */
1184 snd_hda_codec_read(codec, pin->nid, 0,
1185 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1186 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001187 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001188}
1189EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1190
Takashi Iwai2a439522011-07-26 09:52:50 +02001191#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001192/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1193static void restore_shutup_pins(struct hda_codec *codec)
1194{
1195 int i;
1196 if (!codec->pins_shutup)
1197 return;
1198 if (codec->bus->shutdown)
1199 return;
1200 for (i = 0; i < codec->init_pins.used; i++) {
1201 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1202 snd_hda_codec_write(codec, pin->nid, 0,
1203 AC_VERB_SET_PIN_WIDGET_CONTROL,
1204 pin->ctrl);
1205 }
1206 codec->pins_shutup = 0;
1207}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001208#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001209
David Henningsson26a6cb62012-10-09 15:04:21 +02001210static void hda_jackpoll_work(struct work_struct *work)
1211{
1212 struct hda_codec *codec =
1213 container_of(work, struct hda_codec, jackpoll_work.work);
1214 if (!codec->jackpoll_interval)
1215 return;
1216
1217 snd_hda_jack_set_dirty_all(codec);
1218 snd_hda_jack_poll_all(codec);
1219 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1220 codec->jackpoll_interval);
1221}
1222
Takashi Iwai01751f52007-08-10 16:59:39 +02001223static void init_hda_cache(struct hda_cache_rec *cache,
1224 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001225static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001226
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001227/* release all pincfg lists */
1228static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001229{
Takashi Iwai346ff702009-02-23 09:42:57 +01001230 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001231#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001232 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001233#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001234 snd_array_free(&codec->init_pins);
1235}
1236
Takashi Iwai54d17402005-11-21 16:33:22 +01001237/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001238 * audio-converter setup caches
1239 */
1240struct hda_cvt_setup {
1241 hda_nid_t nid;
1242 u8 stream_tag;
1243 u8 channel_id;
1244 u16 format_id;
1245 unsigned char active; /* cvt is currently used */
1246 unsigned char dirty; /* setups should be cleared */
1247};
1248
1249/* get or create a cache entry for the given audio converter NID */
1250static struct hda_cvt_setup *
1251get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1252{
1253 struct hda_cvt_setup *p;
1254 int i;
1255
1256 for (i = 0; i < codec->cvt_setups.used; i++) {
1257 p = snd_array_elem(&codec->cvt_setups, i);
1258 if (p->nid == nid)
1259 return p;
1260 }
1261 p = snd_array_new(&codec->cvt_setups);
1262 if (p)
1263 p->nid = nid;
1264 return p;
1265}
1266
1267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * codec destructor
1269 */
1270static void snd_hda_codec_free(struct hda_codec *codec)
1271{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001272 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001274 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001275 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001276 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001277#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001278 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001279 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001282 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001283 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001284 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001285 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001286 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 codec->bus->caddr_tbl[codec->addr] = NULL;
1288 if (codec->patch_ops.free)
1289 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001290#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001291 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001292 hda_call_pm_notify(codec->bus, false);
1293#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001294 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001295 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001296 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001297 kfree(codec->vendor_name);
1298 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001299 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001300 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 kfree(codec);
1302}
1303
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001304static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1305 hda_nid_t fg, unsigned int power_state);
1306
Takashi Iwaid8193872012-08-31 07:54:38 -07001307static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001308 unsigned int power_state);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/**
1311 * snd_hda_codec_new - create a HDA codec
1312 * @bus: the bus to assign
1313 * @codec_addr: the codec address
1314 * @codecp: the pointer to store the generated codec
1315 *
1316 * Returns 0 if successful, or a negative error code.
1317 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001318int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001319 unsigned int codec_addr,
1320 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001323 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001324 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int err;
1326
Takashi Iwaida3cec32008-08-08 17:12:14 +02001327 if (snd_BUG_ON(!bus))
1328 return -EINVAL;
1329 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001333 snd_printk(KERN_ERR "hda_codec: "
1334 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EBUSY;
1336 }
1337
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001338 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (codec == NULL) {
1340 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1341 return -ENOMEM;
1342 }
1343
1344 codec->bus = bus;
1345 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001346 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001347 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001348 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001349 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001350 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001351 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1352 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001353 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001354 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001355 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001356 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001357 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001358 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001359 INIT_LIST_HEAD(&codec->conn_list);
1360
David Henningsson26a6cb62012-10-09 15:04:21 +02001361 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai83012a72012-08-24 18:38:08 +02001363#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001364 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001365 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1366 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1367 * the caller has to power down appropriatley after initialization
1368 * phase.
1369 */
1370 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001371 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001372#endif
1373
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001374 if (codec->bus->modelname) {
1375 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1376 if (!codec->modelname) {
1377 snd_hda_codec_free(codec);
1378 return -ENODEV;
1379 }
1380 }
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 list_add_tail(&codec->list, &bus->codec_list);
1383 bus->caddr_tbl[codec_addr] = codec;
1384
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1386 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001387 if (codec->vendor_id == -1)
1388 /* read again, hopefully the access method was corrected
1389 * in the last read...
1390 */
1391 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1392 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1394 AC_PAR_SUBSYSTEM_ID);
1395 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1396 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001398 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001399 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001400 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001401 err = -ENODEV;
1402 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
Takashi Iwaid8193872012-08-31 07:54:38 -07001405 fg = codec->afg ? codec->afg : codec->mfg;
1406 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001407 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001408 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001409 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001410 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001411 err = read_pin_defaults(codec);
1412 if (err < 0)
1413 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001416 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001417 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001419 }
1420
Takashi Iwai83012a72012-08-24 18:38:08 +02001421#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001422 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001423 AC_PWRST_CLKSTOP);
1424 if (!codec->d3_stop_clk)
1425 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001426#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001427 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001428 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001429
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001430 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001431 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001432
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001433 snd_hda_codec_proc_new(codec);
1434
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001435 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001436
1437 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1438 codec->subsystem_id, codec->revision_id);
1439 snd_component_add(codec->bus->card, component);
1440
1441 if (codecp)
1442 *codecp = codec;
1443 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001444
1445 error:
1446 snd_hda_codec_free(codec);
1447 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001448}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001449EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001450
Mengdong Lina15d05d2013-02-08 17:09:31 -05001451int snd_hda_codec_update_widgets(struct hda_codec *codec)
1452{
1453 hda_nid_t fg;
1454 int err;
1455
1456 /* Assume the function group node does not change,
1457 * only the widget nodes may change.
1458 */
1459 kfree(codec->wcaps);
1460 fg = codec->afg ? codec->afg : codec->mfg;
1461 err = read_widget_caps(codec, fg);
1462 if (err < 0) {
1463 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1464 return err;
1465 }
1466
1467 snd_array_free(&codec->init_pins);
1468 err = read_pin_defaults(codec);
1469
1470 return err;
1471}
1472EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1473
1474
Takashi Iwaid5191e52009-11-16 14:58:17 +01001475/**
1476 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1477 * @codec: the HDA codec
1478 *
1479 * Start parsing of the given codec tree and (re-)initialize the whole
1480 * patch instance.
1481 *
1482 * Returns 0 if successful or a negative error code.
1483 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001484int snd_hda_codec_configure(struct hda_codec *codec)
1485{
1486 int err;
1487
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001488 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001489 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001490 err = get_codec_name(codec);
1491 if (err < 0)
1492 return err;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Takashi Iwai82467612007-07-27 19:15:54 +02001495 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001497 goto patched;
1498 }
Takashi Iwai82467612007-07-27 19:15:54 +02001499 if (codec->preset && codec->preset->patch) {
1500 err = codec->preset->patch(codec);
1501 goto patched;
1502 }
1503
1504 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001505 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001506 if (err < 0)
1507 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001508
1509 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001510 if (!err && codec->patch_ops.unsol_event)
1511 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001512 /* audio codec should override the mixer name */
1513 if (!err && (codec->afg || !*codec->bus->card->mixername))
1514 snprintf(codec->bus->card->mixername,
1515 sizeof(codec->bus->card->mixername),
1516 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001519EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwaied360812012-08-08 17:12:52 +02001521/* update the stream-id if changed */
1522static void update_pcm_stream_id(struct hda_codec *codec,
1523 struct hda_cvt_setup *p, hda_nid_t nid,
1524 u32 stream_tag, int channel_id)
1525{
1526 unsigned int oldval, newval;
1527
1528 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1529 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1530 newval = (stream_tag << 4) | channel_id;
1531 if (oldval != newval)
1532 snd_hda_codec_write(codec, nid, 0,
1533 AC_VERB_SET_CHANNEL_STREAMID,
1534 newval);
1535 p->stream_tag = stream_tag;
1536 p->channel_id = channel_id;
1537 }
1538}
1539
1540/* update the format-id if changed */
1541static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1542 hda_nid_t nid, int format)
1543{
1544 unsigned int oldval;
1545
1546 if (p->format_id != format) {
1547 oldval = snd_hda_codec_read(codec, nid, 0,
1548 AC_VERB_GET_STREAM_FORMAT, 0);
1549 if (oldval != format) {
1550 msleep(1);
1551 snd_hda_codec_write(codec, nid, 0,
1552 AC_VERB_SET_STREAM_FORMAT,
1553 format);
1554 }
1555 p->format_id = format;
1556 }
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/**
1560 * snd_hda_codec_setup_stream - set up the codec for streaming
1561 * @codec: the CODEC to set up
1562 * @nid: the NID to set up
1563 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1564 * @channel_id: channel id to pass, zero based.
1565 * @format: stream format.
1566 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001567void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1568 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 int channel_id, int format)
1570{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001571 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001572 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001573 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001574 int i;
1575
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001577 return;
1578
Takashi Iwai0ba21762007-04-16 11:29:14 +02001579 snd_printdd("hda_codec_setup_stream: "
1580 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001582 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001583 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001584 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001585
1586 if (codec->pcm_format_first)
1587 update_pcm_format(codec, p, nid, format);
1588 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1589 if (!codec->pcm_format_first)
1590 update_pcm_format(codec, p, nid, format);
1591
Takashi Iwaieb541332010-08-06 13:48:11 +02001592 p->active = 1;
1593 p->dirty = 0;
1594
1595 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001596 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001597 list_for_each_entry(c, &codec->bus->codec_list, list) {
1598 for (i = 0; i < c->cvt_setups.used; i++) {
1599 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001600 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001601 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001602 p->dirty = 1;
1603 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001606EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Takashi Iwaif0cea792010-08-13 11:56:53 +02001608static void really_cleanup_stream(struct hda_codec *codec,
1609 struct hda_cvt_setup *q);
1610
Takashi Iwaid5191e52009-11-16 14:58:17 +01001611/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001612 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001613 * @codec: the CODEC to clean up
1614 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001615 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001616 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001617void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1618 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001619{
Takashi Iwaieb541332010-08-06 13:48:11 +02001620 struct hda_cvt_setup *p;
1621
Takashi Iwai888afa12008-03-18 09:57:50 +01001622 if (!nid)
1623 return;
1624
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001625 if (codec->no_sticky_stream)
1626 do_now = 1;
1627
Takashi Iwai888afa12008-03-18 09:57:50 +01001628 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001629 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001630 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001631 /* here we just clear the active flag when do_now isn't set;
1632 * actual clean-ups will be done later in
1633 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1634 */
1635 if (do_now)
1636 really_cleanup_stream(codec, p);
1637 else
1638 p->active = 0;
1639 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001640}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001641EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001642
Takashi Iwaieb541332010-08-06 13:48:11 +02001643static void really_cleanup_stream(struct hda_codec *codec,
1644 struct hda_cvt_setup *q)
1645{
1646 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001647 if (q->stream_tag || q->channel_id)
1648 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1649 if (q->format_id)
1650 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1651);
Takashi Iwaieb541332010-08-06 13:48:11 +02001652 memset(q, 0, sizeof(*q));
1653 q->nid = nid;
1654}
1655
1656/* clean up the all conflicting obsolete streams */
1657static void purify_inactive_streams(struct hda_codec *codec)
1658{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001659 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001660 int i;
1661
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001662 list_for_each_entry(c, &codec->bus->codec_list, list) {
1663 for (i = 0; i < c->cvt_setups.used; i++) {
1664 struct hda_cvt_setup *p;
1665 p = snd_array_elem(&c->cvt_setups, i);
1666 if (p->dirty)
1667 really_cleanup_stream(c, p);
1668 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001669 }
1670}
1671
Takashi Iwai2a439522011-07-26 09:52:50 +02001672#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001673/* clean up all streams; called from suspend */
1674static void hda_cleanup_all_streams(struct hda_codec *codec)
1675{
1676 int i;
1677
1678 for (i = 0; i < codec->cvt_setups.used; i++) {
1679 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1680 if (p->stream_tag)
1681 really_cleanup_stream(codec, p);
1682 }
1683}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001684#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686/*
1687 * amp access functions
1688 */
1689
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001690/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001691#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001692#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001693#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1694#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001696#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001699static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001700 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai01751f52007-08-10 16:59:39 +02001702 memset(cache, 0, sizeof(*cache));
1703 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001704 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001705}
1706
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001707static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001708{
Takashi Iwai603c4012008-07-30 15:01:44 +02001709 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001713static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Takashi Iwai01751f52007-08-10 16:59:39 +02001715 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1716 u16 cur = cache->hash[idx];
1717 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001720 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (info->key == key)
1722 return info;
1723 cur = info->next;
1724 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001725 return NULL;
1726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001728/* query the hash. allocate an entry if not found. */
1729static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1730 u32 key)
1731{
1732 struct hda_cache_head *info = get_hash(cache, key);
1733 if (!info) {
1734 u16 idx, cur;
1735 /* add a new hash entry */
1736 info = snd_array_new(&cache->buf);
1737 if (!info)
1738 return NULL;
1739 cur = snd_array_index(&cache->buf, info);
1740 info->key = key;
1741 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001742 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001743 idx = key % (u16)ARRAY_SIZE(cache->hash);
1744 info->next = cache->hash[idx];
1745 cache->hash[idx] = cur;
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return info;
1748}
1749
Takashi Iwai01751f52007-08-10 16:59:39 +02001750/* query and allocate an amp hash entry */
1751static inline struct hda_amp_info *
1752get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1753{
1754 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1755}
1756
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001757/* overwrite the value with the key in the caps hash */
1758static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1759{
1760 struct hda_amp_info *info;
1761
1762 mutex_lock(&codec->hash_mutex);
1763 info = get_alloc_amp_hash(codec, key);
1764 if (!info) {
1765 mutex_unlock(&codec->hash_mutex);
1766 return -EINVAL;
1767 }
1768 info->amp_caps = val;
1769 info->head.val |= INFO_AMP_CAPS;
1770 mutex_unlock(&codec->hash_mutex);
1771 return 0;
1772}
1773
1774/* query the value from the caps hash; if not found, fetch the current
1775 * value from the given function and store in the hash
1776 */
1777static unsigned int
1778query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1779 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1780{
1781 struct hda_amp_info *info;
1782 unsigned int val;
1783
1784 mutex_lock(&codec->hash_mutex);
1785 info = get_alloc_amp_hash(codec, key);
1786 if (!info) {
1787 mutex_unlock(&codec->hash_mutex);
1788 return 0;
1789 }
1790 if (!(info->head.val & INFO_AMP_CAPS)) {
1791 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1792 val = func(codec, nid, dir);
1793 write_caps_hash(codec, key, val);
1794 } else {
1795 val = info->amp_caps;
1796 mutex_unlock(&codec->hash_mutex);
1797 }
1798 return val;
1799}
1800
1801static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1802 int direction)
1803{
1804 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1805 nid = codec->afg;
1806 return snd_hda_param_read(codec, nid,
1807 direction == HDA_OUTPUT ?
1808 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1809}
1810
Takashi Iwaid5191e52009-11-16 14:58:17 +01001811/**
1812 * query_amp_caps - query AMP capabilities
1813 * @codec: the HD-auio codec
1814 * @nid: the NID to query
1815 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1816 *
1817 * Query AMP capabilities for the given widget and direction.
1818 * Returns the obtained capability bits.
1819 *
1820 * When cap bits have been already read, this doesn't read again but
1821 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001823u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001825 return query_caps_hash(codec, nid, direction,
1826 HDA_HASH_KEY(nid, direction, 0),
1827 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001829EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Takashi Iwaid5191e52009-11-16 14:58:17 +01001831/**
1832 * snd_hda_override_amp_caps - Override the AMP capabilities
1833 * @codec: the CODEC to clean up
1834 * @nid: the NID to clean up
1835 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1836 * @caps: the capability bits to set
1837 *
1838 * Override the cached AMP caps bits value by the given one.
1839 * This function is useful if the driver needs to adjust the AMP ranges,
1840 * e.g. limit to 0dB, etc.
1841 *
1842 * Returns zero if successful or a negative error code.
1843 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001844int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1845 unsigned int caps)
1846{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001847 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001848}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001849EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001850
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001851static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1852 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001853{
1854 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1855}
1856
Takashi Iwaid5191e52009-11-16 14:58:17 +01001857/**
1858 * snd_hda_query_pin_caps - Query PIN capabilities
1859 * @codec: the HD-auio codec
1860 * @nid: the NID to query
1861 *
1862 * Query PIN capabilities for the given widget.
1863 * Returns the obtained capability bits.
1864 *
1865 * When cap bits have been already read, this doesn't read again but
1866 * returns the cached value.
1867 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001868u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1869{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001870 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001871 read_pin_cap);
1872}
Takashi Iwai1327a322009-03-23 13:07:47 +01001873EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1874
Wu Fengguang864f92b2009-11-18 12:38:02 +08001875/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001876 * snd_hda_override_pin_caps - Override the pin capabilities
1877 * @codec: the CODEC
1878 * @nid: the NID to override
1879 * @caps: the capability bits to set
1880 *
1881 * Override the cached PIN capabilitiy bits value by the given one.
1882 *
1883 * Returns zero if successful or a negative error code.
1884 */
1885int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1886 unsigned int caps)
1887{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001888 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001889}
1890EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1891
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001892/* read or sync the hash value with the current value;
1893 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001895static struct hda_amp_info *
1896update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001897 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001899 struct hda_amp_info *info;
1900 unsigned int parm, val = 0;
1901 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001903 retry:
1904 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1905 if (!info)
1906 return NULL;
1907 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1908 if (!val_read) {
1909 mutex_unlock(&codec->hash_mutex);
1910 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1911 parm |= direction == HDA_OUTPUT ?
1912 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1913 parm |= index;
1914 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001915 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001916 val &= 0xff;
1917 val_read = true;
1918 mutex_lock(&codec->hash_mutex);
1919 goto retry;
1920 }
1921 info->vol[ch] = val;
1922 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001923 } else if (init_only)
1924 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001925 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
1928/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001929 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001931static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001932 hda_nid_t nid, int ch, int direction, int index,
1933 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 u32 parm;
1936
1937 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1938 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1939 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001940 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1941 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001942 ; /* set the zero value as a fake mute */
1943 else
1944 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1946}
1947
Takashi Iwaid5191e52009-11-16 14:58:17 +01001948/**
1949 * snd_hda_codec_amp_read - Read AMP value
1950 * @codec: HD-audio codec
1951 * @nid: NID to read the AMP value
1952 * @ch: channel (left=0 or right=1)
1953 * @direction: #HDA_INPUT or #HDA_OUTPUT
1954 * @index: the index value (only for input direction)
1955 *
1956 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Takashi Iwai834be882006-03-01 14:16:17 +01001958int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1959 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001961 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001962 unsigned int val = 0;
1963
1964 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001965 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001966 if (info)
1967 val = info->vol[ch];
1968 mutex_unlock(&codec->hash_mutex);
1969 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001971EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Takashi Iwai280e57d2012-12-14 10:32:21 +01001973static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1974 int direction, int idx, int mask, int val,
1975 bool init_only)
1976{
1977 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001978 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001979 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001980
1981 if (snd_BUG_ON(mask & ~0xff))
1982 mask &= 0xff;
1983 val &= mask;
1984
1985 mutex_lock(&codec->hash_mutex);
1986 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1987 if (!info) {
1988 mutex_unlock(&codec->hash_mutex);
1989 return 0;
1990 }
1991 val |= info->vol[ch] & ~mask;
1992 if (info->vol[ch] == val) {
1993 mutex_unlock(&codec->hash_mutex);
1994 return 0;
1995 }
1996 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001997 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001998 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001999 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002000 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002001 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002002 return 1;
2003}
2004
Takashi Iwaid5191e52009-11-16 14:58:17 +01002005/**
2006 * snd_hda_codec_amp_update - update the AMP value
2007 * @codec: HD-audio codec
2008 * @nid: NID to read the AMP value
2009 * @ch: channel (left=0 or right=1)
2010 * @direction: #HDA_INPUT or #HDA_OUTPUT
2011 * @idx: the index value (only for input direction)
2012 * @mask: bit mask to set
2013 * @val: the bits value to set
2014 *
2015 * Update the AMP value with a bit mask.
2016 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002017 */
Takashi Iwai834be882006-03-01 14:16:17 +01002018int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2019 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002021 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002023EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Takashi Iwaid5191e52009-11-16 14:58:17 +01002025/**
2026 * snd_hda_codec_amp_stereo - update the AMP stereo values
2027 * @codec: HD-audio codec
2028 * @nid: NID to read the AMP value
2029 * @direction: #HDA_INPUT or #HDA_OUTPUT
2030 * @idx: the index value (only for input direction)
2031 * @mask: bit mask to set
2032 * @val: the bits value to set
2033 *
2034 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2035 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002036 */
2037int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2038 int direction, int idx, int mask, int val)
2039{
2040 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002041
2042 if (snd_BUG_ON(mask & ~0xff))
2043 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002044 for (ch = 0; ch < 2; ch++)
2045 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2046 idx, mask, val);
2047 return ret;
2048}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002049EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002050
Takashi Iwai280e57d2012-12-14 10:32:21 +01002051/* Works like snd_hda_codec_amp_update() but it writes the value only at
2052 * the first access. If the amp was already initialized / updated beforehand,
2053 * this does nothing.
2054 */
2055int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2056 int dir, int idx, int mask, int val)
2057{
2058 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2059}
2060EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2061
2062int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2063 int dir, int idx, int mask, int val)
2064{
2065 int ch, ret = 0;
2066
2067 if (snd_BUG_ON(mask & ~0xff))
2068 mask &= 0xff;
2069 for (ch = 0; ch < 2; ch++)
2070 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2071 idx, mask, val);
2072 return ret;
2073}
2074EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2075
Takashi Iwaid5191e52009-11-16 14:58:17 +01002076/**
2077 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2078 * @codec: HD-audio codec
2079 *
2080 * Resume the all amp commands from the cache.
2081 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002082void snd_hda_codec_resume_amp(struct hda_codec *codec)
2083{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002084 int i;
2085
Takashi Iwaic370dd62012-12-13 18:30:04 +01002086 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002087 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002088 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2089 struct hda_amp_info *buffer;
2090 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002091 hda_nid_t nid;
2092 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002093 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002094
2095 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002096 if (!buffer->head.dirty)
2097 continue;
2098 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002099 info = *buffer;
2100 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002101 if (!key)
2102 continue;
2103 nid = key & 0xff;
2104 idx = (key >> 16) & 0xff;
2105 dir = (key >> 24) & 0xff;
2106 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002107 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002108 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002109 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002110 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2111 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002112 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002113 }
2114 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002115 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002116}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002117EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002119static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2120 unsigned int ofs)
2121{
2122 u32 caps = query_amp_caps(codec, nid, dir);
2123 /* get num steps */
2124 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2125 if (ofs < caps)
2126 caps -= ofs;
2127 return caps;
2128}
2129
Takashi Iwaid5191e52009-11-16 14:58:17 +01002130/**
2131 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2132 *
2133 * The control element is supposed to have the private_value field
2134 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2135 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002136int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 u16 nid = get_amp_nid(kcontrol);
2141 u8 chs = get_amp_channels(kcontrol);
2142 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002143 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002145 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2146 uinfo->count = chs == 3 ? 2 : 1;
2147 uinfo->value.integer.min = 0;
2148 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2149 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002150 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002151 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2152 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return -EINVAL;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return 0;
2156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002157EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002159
2160static inline unsigned int
2161read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2162 int ch, int dir, int idx, unsigned int ofs)
2163{
2164 unsigned int val;
2165 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2166 val &= HDA_AMP_VOLMASK;
2167 if (val >= ofs)
2168 val -= ofs;
2169 else
2170 val = 0;
2171 return val;
2172}
2173
2174static inline int
2175update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2176 int ch, int dir, int idx, unsigned int ofs,
2177 unsigned int val)
2178{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002179 unsigned int maxval;
2180
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002181 if (val > 0)
2182 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002183 /* ofs = 0: raw max value */
2184 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002185 if (val > maxval)
2186 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002187 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2188 HDA_AMP_VOLMASK, val);
2189}
2190
Takashi Iwaid5191e52009-11-16 14:58:17 +01002191/**
2192 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2193 *
2194 * The control element is supposed to have the private_value field
2195 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2196 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002197int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2201 hda_nid_t nid = get_amp_nid(kcontrol);
2202 int chs = get_amp_channels(kcontrol);
2203 int dir = get_amp_direction(kcontrol);
2204 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002205 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 long *valp = ucontrol->value.integer.value;
2207
2208 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002209 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002211 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
2213}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002214EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Takashi Iwaid5191e52009-11-16 14:58:17 +01002216/**
2217 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2218 *
2219 * The control element is supposed to have the private_value field
2220 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2221 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002222int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2226 hda_nid_t nid = get_amp_nid(kcontrol);
2227 int chs = get_amp_channels(kcontrol);
2228 int dir = get_amp_direction(kcontrol);
2229 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002230 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 long *valp = ucontrol->value.integer.value;
2232 int change = 0;
2233
Takashi Iwaicb53c622007-08-10 17:21:45 +02002234 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002235 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002236 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002237 valp++;
2238 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002239 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002240 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002241 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return change;
2243}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002244EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Takashi Iwaid5191e52009-11-16 14:58:17 +01002246/**
2247 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2248 *
2249 * The control element is supposed to have the private_value field
2250 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2251 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002252int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2253 unsigned int size, unsigned int __user *_tlv)
2254{
2255 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2256 hda_nid_t nid = get_amp_nid(kcontrol);
2257 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002258 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002259 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002260 u32 caps, val1, val2;
2261
2262 if (size < 4 * sizeof(unsigned int))
2263 return -ENOMEM;
2264 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002265 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2266 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002267 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002268 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002269 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002270 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002271 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002272 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2273 return -EFAULT;
2274 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2275 return -EFAULT;
2276 if (put_user(val1, _tlv + 2))
2277 return -EFAULT;
2278 if (put_user(val2, _tlv + 3))
2279 return -EFAULT;
2280 return 0;
2281}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002282EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002283
Takashi Iwaid5191e52009-11-16 14:58:17 +01002284/**
2285 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2286 * @codec: HD-audio codec
2287 * @nid: NID of a reference widget
2288 * @dir: #HDA_INPUT or #HDA_OUTPUT
2289 * @tlv: TLV data to be stored, at least 4 elements
2290 *
2291 * Set (static) TLV data for a virtual master volume using the AMP caps
2292 * obtained from the reference NID.
2293 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002294 */
2295void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2296 unsigned int *tlv)
2297{
2298 u32 caps;
2299 int nums, step;
2300
2301 caps = query_amp_caps(codec, nid, dir);
2302 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2303 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2304 step = (step + 1) * 25;
2305 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2306 tlv[1] = 2 * sizeof(unsigned int);
2307 tlv[2] = -nums * step;
2308 tlv[3] = step;
2309}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002310EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002311
2312/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002313static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002314find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002315{
2316 struct snd_ctl_elem_id id;
2317 memset(&id, 0, sizeof(id));
2318 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002319 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002320 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002321 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2322 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002323 strcpy(id.name, name);
2324 return snd_ctl_find_id(codec->bus->card, &id);
2325}
2326
Takashi Iwaid5191e52009-11-16 14:58:17 +01002327/**
2328 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2329 * @codec: HD-audio codec
2330 * @name: ctl id name string
2331 *
2332 * Get the control element with the given id string and IFACE_MIXER.
2333 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002334struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2335 const char *name)
2336{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002337 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002339EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002340
Takashi Iwaidcda5802012-10-12 17:24:51 +02002341static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002342 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002343{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002344 int i, idx;
2345 /* 16 ctlrs should be large enough */
2346 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2347 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002348 return idx;
2349 }
2350 return -EBUSY;
2351}
2352
Takashi Iwaid5191e52009-11-16 14:58:17 +01002353/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002354 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002355 * @codec: HD-audio codec
2356 * @nid: corresponding NID (optional)
2357 * @kctl: the control element to assign
2358 *
2359 * Add the given control element to an array inside the codec instance.
2360 * All control elements belonging to a codec are supposed to be added
2361 * by this function so that a proper clean-up works at the free or
2362 * reconfiguration time.
2363 *
2364 * If non-zero @nid is passed, the NID is assigned to the control element.
2365 * The assignment is shown in the codec proc file.
2366 *
2367 * snd_hda_ctl_add() checks the control subdev id field whether
2368 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002369 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2370 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002371 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002372int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2373 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002374{
2375 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002376 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002377 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002378
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002379 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002380 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002381 if (nid == 0)
2382 nid = get_amp_nid_(kctl->private_value);
2383 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002384 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2385 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002386 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002387 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002388 err = snd_ctl_add(codec->bus->card, kctl);
2389 if (err < 0)
2390 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002391 item = snd_array_new(&codec->mixers);
2392 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002393 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002394 item->kctl = kctl;
2395 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002396 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002397 return 0;
2398}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002399EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002400
Takashi Iwaid5191e52009-11-16 14:58:17 +01002401/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002402 * snd_hda_add_nid - Assign a NID to a control element
2403 * @codec: HD-audio codec
2404 * @nid: corresponding NID (optional)
2405 * @kctl: the control element to assign
2406 * @index: index to kctl
2407 *
2408 * Add the given control element to an array inside the codec instance.
2409 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2410 * NID:KCTL mapping - for example "Capture Source" selector.
2411 */
2412int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2413 unsigned int index, hda_nid_t nid)
2414{
2415 struct hda_nid_item *item;
2416
2417 if (nid > 0) {
2418 item = snd_array_new(&codec->nids);
2419 if (!item)
2420 return -ENOMEM;
2421 item->kctl = kctl;
2422 item->index = index;
2423 item->nid = nid;
2424 return 0;
2425 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002426 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2427 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002428 return -EINVAL;
2429}
2430EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2431
2432/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002433 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2434 * @codec: HD-audio codec
2435 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002436void snd_hda_ctls_clear(struct hda_codec *codec)
2437{
2438 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002439 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002440 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002441 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002442 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002443 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002444}
2445
Takashi Iwaia65d6292009-02-23 16:57:04 +01002446/* pseudo device locking
2447 * toggle card->shutdown to allow/disallow the device access (as a hack)
2448 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002449int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002450{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002451 struct snd_card *card = bus->card;
2452 struct hda_codec *codec;
2453
Takashi Iwaia65d6292009-02-23 16:57:04 +01002454 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002455 if (card->shutdown)
2456 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002457 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002458 if (!list_empty(&card->ctl_files))
2459 goto err_clear;
2460
2461 list_for_each_entry(codec, &bus->codec_list, list) {
2462 int pcm;
2463 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2464 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2465 if (!cpcm->pcm)
2466 continue;
2467 if (cpcm->pcm->streams[0].substream_opened ||
2468 cpcm->pcm->streams[1].substream_opened)
2469 goto err_clear;
2470 }
2471 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002472 spin_unlock(&card->files_lock);
2473 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002474
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002475 err_clear:
2476 card->shutdown = 0;
2477 err_unlock:
2478 spin_unlock(&card->files_lock);
2479 return -EINVAL;
2480}
2481EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2482
2483void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002484{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002485 struct snd_card *card = bus->card;
2486
2487 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002488 spin_lock(&card->files_lock);
2489 card->shutdown = 0;
2490 spin_unlock(&card->files_lock);
2491}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002492EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002493
Takashi Iwaid5191e52009-11-16 14:58:17 +01002494/**
2495 * snd_hda_codec_reset - Clear all objects assigned to the codec
2496 * @codec: HD-audio codec
2497 *
2498 * This frees the all PCM and control elements assigned to the codec, and
2499 * clears the caches and restores the pin default configurations.
2500 *
2501 * When a device is being used, it returns -EBSY. If successfully freed,
2502 * returns zero.
2503 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002504int snd_hda_codec_reset(struct hda_codec *codec)
2505{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002506 struct hda_bus *bus = codec->bus;
2507 struct snd_card *card = bus->card;
2508 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002509
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002510 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002511 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002512
2513 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002514 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002515#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002516 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002517 codec->power_on = 0;
2518 codec->power_transition = 0;
2519 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002520 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002521#endif
2522 snd_hda_ctls_clear(codec);
2523 /* relase PCMs */
2524 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002525 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002526 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002527 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002528 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002529 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002530 }
2531 if (codec->patch_ops.free)
2532 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002533 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002534 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002535 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002536 codec->spec = NULL;
2537 free_hda_cache(&codec->amp_cache);
2538 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002539 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2540 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002541 /* free only driver_pins so that init_pins + user_pins are restored */
2542 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002543 snd_array_free(&codec->cvt_setups);
2544 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002545 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002546 codec->num_pcms = 0;
2547 codec->pcm_info = NULL;
2548 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002549 codec->slave_dig_outs = NULL;
2550 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002551 module_put(codec->owner);
2552 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002553
2554 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002555 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002556 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002557}
2558
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002559typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2560
2561/* apply the function to all matching slave ctls in the mixer list */
2562static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002563 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002564{
2565 struct hda_nid_item *items;
2566 const char * const *s;
2567 int i, err;
2568
2569 items = codec->mixers.list;
2570 for (i = 0; i < codec->mixers.used; i++) {
2571 struct snd_kcontrol *sctl = items[i].kctl;
2572 if (!sctl || !sctl->id.name ||
2573 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2574 continue;
2575 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002576 char tmpname[sizeof(sctl->id.name)];
2577 const char *name = *s;
2578 if (suffix) {
2579 snprintf(tmpname, sizeof(tmpname), "%s %s",
2580 name, suffix);
2581 name = tmpname;
2582 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002583 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002584 err = func(data, sctl);
2585 if (err)
2586 return err;
2587 break;
2588 }
2589 }
2590 }
2591 return 0;
2592}
2593
2594static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2595{
2596 return 1;
2597}
2598
Takashi Iwai18478e82012-03-09 17:51:10 +01002599/* guess the value corresponding to 0dB */
2600static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2601{
2602 int _tlv[4];
2603 const int *tlv = NULL;
2604 int val = -1;
2605
2606 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2607 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2608 mm_segment_t fs = get_fs();
2609 set_fs(get_ds());
2610 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2611 tlv = _tlv;
2612 set_fs(fs);
2613 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2614 tlv = kctl->tlv.p;
2615 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2616 val = -tlv[2] / tlv[3];
2617 return val;
2618}
2619
2620/* call kctl->put with the given value(s) */
2621static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2622{
2623 struct snd_ctl_elem_value *ucontrol;
2624 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2625 if (!ucontrol)
2626 return -ENOMEM;
2627 ucontrol->value.integer.value[0] = val;
2628 ucontrol->value.integer.value[1] = val;
2629 kctl->put(kctl, ucontrol);
2630 kfree(ucontrol);
2631 return 0;
2632}
2633
2634/* initialize the slave volume with 0dB */
2635static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2636{
2637 int offset = get_kctl_0dB_offset(slave);
2638 if (offset > 0)
2639 put_kctl_with_value(slave, offset);
2640 return 0;
2641}
2642
2643/* unmute the slave */
2644static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2645{
2646 return put_kctl_with_value(slave, 1);
2647}
2648
Takashi Iwaid5191e52009-11-16 14:58:17 +01002649/**
2650 * snd_hda_add_vmaster - create a virtual master control and add slaves
2651 * @codec: HD-audio codec
2652 * @name: vmaster control name
2653 * @tlv: TLV data (optional)
2654 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002655 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002656 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002657 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002658 *
2659 * Create a virtual master control with the given name. The TLV data
2660 * must be either NULL or a valid data.
2661 *
2662 * @slaves is a NULL-terminated array of strings, each of which is a
2663 * slave control name. All controls with these names are assigned to
2664 * the new virtual master control.
2665 *
2666 * This function returns zero if successful or a negative error code.
2667 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002668int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002669 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002670 const char *suffix, bool init_slave_vol,
2671 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002672{
2673 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002674 int err;
2675
Takashi Iwai29e58532012-03-12 12:25:03 +01002676 if (ctl_ret)
2677 *ctl_ret = NULL;
2678
Takashi Iwai9322ca52012-02-03 14:28:01 +01002679 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002680 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002681 snd_printdd("No slave found for %s\n", name);
2682 return 0;
2683 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002684 kctl = snd_ctl_make_virtual_master(name, tlv);
2685 if (!kctl)
2686 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002687 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002688 if (err < 0)
2689 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002690
Takashi Iwai9322ca52012-02-03 14:28:01 +01002691 err = map_slaves(codec, slaves, suffix,
2692 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002693 if (err < 0)
2694 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002695
2696 /* init with master mute & zero volume */
2697 put_kctl_with_value(kctl, 0);
2698 if (init_slave_vol)
2699 map_slaves(codec, slaves, suffix,
2700 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2701
Takashi Iwai29e58532012-03-12 12:25:03 +01002702 if (ctl_ret)
2703 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002704 return 0;
2705}
Takashi Iwai18478e82012-03-09 17:51:10 +01002706EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002707
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002708/*
2709 * mute-LED control using vmaster
2710 */
2711static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_info *uinfo)
2713{
2714 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002715 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002716 };
2717 unsigned int index;
2718
2719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2720 uinfo->count = 1;
2721 uinfo->value.enumerated.items = 3;
2722 index = uinfo->value.enumerated.item;
2723 if (index >= 3)
2724 index = 2;
2725 strcpy(uinfo->value.enumerated.name, texts[index]);
2726 return 0;
2727}
2728
2729static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
2731{
2732 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2733 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2734 return 0;
2735}
2736
2737static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2739{
2740 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2741 unsigned int old_mode = hook->mute_mode;
2742
2743 hook->mute_mode = ucontrol->value.enumerated.item[0];
2744 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2745 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2746 if (old_mode == hook->mute_mode)
2747 return 0;
2748 snd_hda_sync_vmaster_hook(hook);
2749 return 1;
2750}
2751
2752static struct snd_kcontrol_new vmaster_mute_mode = {
2753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2754 .name = "Mute-LED Mode",
2755 .info = vmaster_mute_mode_info,
2756 .get = vmaster_mute_mode_get,
2757 .put = vmaster_mute_mode_put,
2758};
2759
2760/*
2761 * Add a mute-LED hook with the given vmaster switch kctl
2762 * "Mute-LED Mode" control is automatically created and associated with
2763 * the given hook.
2764 */
2765int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002766 struct hda_vmaster_mute_hook *hook,
2767 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002768{
2769 struct snd_kcontrol *kctl;
2770
2771 if (!hook->hook || !hook->sw_kctl)
2772 return 0;
2773 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2774 hook->codec = codec;
2775 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002776 if (!expose_enum_ctl)
2777 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002778 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2779 if (!kctl)
2780 return -ENOMEM;
2781 return snd_hda_ctl_add(codec, 0, kctl);
2782}
2783EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2784
2785/*
2786 * Call the hook with the current value for synchronization
2787 * Should be called in init callback
2788 */
2789void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2790{
2791 if (!hook->hook || !hook->codec)
2792 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002793 /* don't call vmaster hook in the destructor since it might have
2794 * been already destroyed
2795 */
2796 if (hook->codec->bus->shutdown)
2797 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002798 switch (hook->mute_mode) {
2799 case HDA_VMUTE_FOLLOW_MASTER:
2800 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2801 break;
2802 default:
2803 hook->hook(hook->codec, hook->mute_mode);
2804 break;
2805 }
2806}
2807EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2808
2809
Takashi Iwaid5191e52009-11-16 14:58:17 +01002810/**
2811 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2812 *
2813 * The control element is supposed to have the private_value field
2814 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2815 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002816int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 int chs = get_amp_channels(kcontrol);
2820
2821 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2822 uinfo->count = chs == 3 ? 2 : 1;
2823 uinfo->value.integer.min = 0;
2824 uinfo->value.integer.max = 1;
2825 return 0;
2826}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Takashi Iwaid5191e52009-11-16 14:58:17 +01002829/**
2830 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2831 *
2832 * The control element is supposed to have the private_value field
2833 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2834 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002835int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 hda_nid_t nid = get_amp_nid(kcontrol);
2840 int chs = get_amp_channels(kcontrol);
2841 int dir = get_amp_direction(kcontrol);
2842 int idx = get_amp_index(kcontrol);
2843 long *valp = ucontrol->value.integer.value;
2844
2845 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002846 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002847 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002849 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002850 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return 0;
2852}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002853EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Takashi Iwaid5191e52009-11-16 14:58:17 +01002855/**
2856 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2857 *
2858 * The control element is supposed to have the private_value field
2859 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2860 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002861int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2865 hda_nid_t nid = get_amp_nid(kcontrol);
2866 int chs = get_amp_channels(kcontrol);
2867 int dir = get_amp_direction(kcontrol);
2868 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 long *valp = ucontrol->value.integer.value;
2870 int change = 0;
2871
Takashi Iwaicb53c622007-08-10 17:21:45 +02002872 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002873 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002874 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002875 HDA_AMP_MUTE,
2876 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002877 valp++;
2878 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002879 if (chs & 2)
2880 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002881 HDA_AMP_MUTE,
2882 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002883 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002884 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return change;
2886}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002887EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889/*
Takashi Iwai985be542005-11-02 18:26:49 +01002890 * bound volume controls
2891 *
2892 * bind multiple volumes (# indices, from 0)
2893 */
2894
2895#define AMP_VAL_IDX_SHIFT 19
2896#define AMP_VAL_IDX_MASK (0x0f<<19)
2897
Takashi Iwaid5191e52009-11-16 14:58:17 +01002898/**
2899 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2900 *
2901 * The control element is supposed to have the private_value field
2902 * set up via HDA_BIND_MUTE*() macros.
2903 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002904int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002906{
2907 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2908 unsigned long pval;
2909 int err;
2910
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002911 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002912 pval = kcontrol->private_value;
2913 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2914 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2915 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002916 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002917 return err;
2918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002919EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002920
Takashi Iwaid5191e52009-11-16 14:58:17 +01002921/**
2922 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2923 *
2924 * The control element is supposed to have the private_value field
2925 * set up via HDA_BIND_MUTE*() macros.
2926 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002927int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002929{
2930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2931 unsigned long pval;
2932 int i, indices, err = 0, change = 0;
2933
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002934 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002935 pval = kcontrol->private_value;
2936 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2937 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2939 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002940 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2941 if (err < 0)
2942 break;
2943 change |= err;
2944 }
2945 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002946 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002947 return err < 0 ? err : change;
2948}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002949EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002950
Takashi Iwaid5191e52009-11-16 14:58:17 +01002951/**
2952 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2953 *
2954 * The control element is supposed to have the private_value field
2955 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002956 */
2957int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_info *uinfo)
2959{
2960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2961 struct hda_bind_ctls *c;
2962 int err;
2963
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002964 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002965 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002966 kcontrol->private_value = *c->values;
2967 err = c->ops->info(kcontrol, uinfo);
2968 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002969 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002970 return err;
2971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002972EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002973
Takashi Iwaid5191e52009-11-16 14:58:17 +01002974/**
2975 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2976 *
2977 * The control element is supposed to have the private_value field
2978 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2979 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002980int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_value *ucontrol)
2982{
2983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2984 struct hda_bind_ctls *c;
2985 int err;
2986
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002987 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002988 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002989 kcontrol->private_value = *c->values;
2990 err = c->ops->get(kcontrol, ucontrol);
2991 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002992 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002993 return err;
2994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002995EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002996
Takashi Iwaid5191e52009-11-16 14:58:17 +01002997/**
2998 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2999 *
3000 * The control element is supposed to have the private_value field
3001 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3002 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003003int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
3005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3007 struct hda_bind_ctls *c;
3008 unsigned long *vals;
3009 int err = 0, change = 0;
3010
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003011 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003012 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003013 for (vals = c->values; *vals; vals++) {
3014 kcontrol->private_value = *vals;
3015 err = c->ops->put(kcontrol, ucontrol);
3016 if (err < 0)
3017 break;
3018 change |= err;
3019 }
3020 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003021 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003022 return err < 0 ? err : change;
3023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003024EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003025
Takashi Iwaid5191e52009-11-16 14:58:17 +01003026/**
3027 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3028 *
3029 * The control element is supposed to have the private_value field
3030 * set up via HDA_BIND_VOL() macro.
3031 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003032int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3033 unsigned int size, unsigned int __user *tlv)
3034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3036 struct hda_bind_ctls *c;
3037 int err;
3038
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003039 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003040 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003041 kcontrol->private_value = *c->values;
3042 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3043 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003044 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003045 return err;
3046}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003047EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003048
3049struct hda_ctl_ops snd_hda_bind_vol = {
3050 .info = snd_hda_mixer_amp_volume_info,
3051 .get = snd_hda_mixer_amp_volume_get,
3052 .put = snd_hda_mixer_amp_volume_put,
3053 .tlv = snd_hda_mixer_amp_tlv
3054};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003055EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003056
3057struct hda_ctl_ops snd_hda_bind_sw = {
3058 .info = snd_hda_mixer_amp_switch_info,
3059 .get = snd_hda_mixer_amp_switch_get,
3060 .put = snd_hda_mixer_amp_switch_put,
3061 .tlv = snd_hda_mixer_amp_tlv
3062};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003063EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003064
3065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 * SPDIF out controls
3067 */
3068
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3073 uinfo->count = 1;
3074 return 0;
3075}
3076
Takashi Iwai0ba21762007-04-16 11:29:14 +02003077static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3078 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3081 IEC958_AES0_NONAUDIO |
3082 IEC958_AES0_CON_EMPHASIS_5015 |
3083 IEC958_AES0_CON_NOT_COPYRIGHT;
3084 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3085 IEC958_AES1_CON_ORIGINAL;
3086 return 0;
3087}
3088
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3090 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3093 IEC958_AES0_NONAUDIO |
3094 IEC958_AES0_PRO_EMPHASIS_5015;
3095 return 0;
3096}
3097
Takashi Iwai0ba21762007-04-16 11:29:14 +02003098static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3099 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003102 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003103 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003105 mutex_lock(&codec->spdif_mutex);
3106 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003107 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3108 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3109 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3110 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003111 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 return 0;
3114}
3115
3116/* convert from SPDIF status bits to HDA SPDIF bits
3117 * bit 0 (DigEn) is always set zero (to be filled later)
3118 */
3119static unsigned short convert_from_spdif_status(unsigned int sbits)
3120{
3121 unsigned short val = 0;
3122
3123 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003124 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003126 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3129 IEC958_AES0_PRO_EMPHASIS_5015)
3130 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003132 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3133 IEC958_AES0_CON_EMPHASIS_5015)
3134 val |= AC_DIG1_EMPHASIS;
3135 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3136 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003138 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3140 }
3141 return val;
3142}
3143
3144/* convert to SPDIF status bits from HDA SPDIF bits
3145 */
3146static unsigned int convert_to_spdif_status(unsigned short val)
3147{
3148 unsigned int sbits = 0;
3149
Takashi Iwai0ba21762007-04-16 11:29:14 +02003150 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003152 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 sbits |= IEC958_AES0_PROFESSIONAL;
3154 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003155 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3157 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003158 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003160 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003162 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3164 sbits |= val & (0x7f << 8);
3165 }
3166 return sbits;
3167}
3168
Takashi Iwai2f728532008-09-25 16:32:41 +02003169/* set digital convert verbs both for the given NID and its slaves */
3170static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3171 int verb, int val)
3172{
Takashi Iwaidda14412011-05-02 11:29:30 +02003173 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003174
Takashi Iwai9e976972008-11-25 08:17:20 +01003175 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003176 d = codec->slave_dig_outs;
3177 if (!d)
3178 return;
3179 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003180 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003181}
3182
3183static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3184 int dig1, int dig2)
3185{
3186 if (dig1 != -1)
3187 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3188 if (dig2 != -1)
3189 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3190}
3191
Takashi Iwai0ba21762007-04-16 11:29:14 +02003192static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003196 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003197 struct hda_spdif_out *spdif;
3198 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 unsigned short val;
3200 int change;
3201
Ingo Molnar62932df2006-01-16 16:34:20 +01003202 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003203 spdif = snd_array_elem(&codec->spdif_out, idx);
3204 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003205 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3207 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3208 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003209 val = convert_from_spdif_status(spdif->status);
3210 val |= spdif->ctls & 1;
3211 change = spdif->ctls != val;
3212 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003213 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003214 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003215 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 return change;
3217}
3218
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003219#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Takashi Iwai0ba21762007-04-16 11:29:14 +02003221static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003225 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003226 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003228 mutex_lock(&codec->spdif_mutex);
3229 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003230 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003231 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233}
3234
Stephen Warren74b654c2011-06-01 11:14:18 -06003235static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3236 int dig1, int dig2)
3237{
3238 set_dig_out_convert(codec, nid, dig1, dig2);
3239 /* unmute amp switch (if any) */
3240 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3241 (dig1 & AC_DIG1_ENABLE))
3242 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3243 HDA_AMP_MUTE, 0);
3244}
3245
Takashi Iwai0ba21762007-04-16 11:29:14 +02003246static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
3249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003250 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003251 struct hda_spdif_out *spdif;
3252 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 unsigned short val;
3254 int change;
3255
Ingo Molnar62932df2006-01-16 16:34:20 +01003256 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003257 spdif = snd_array_elem(&codec->spdif_out, idx);
3258 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003259 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003261 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003262 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003263 spdif->ctls = val;
3264 if (change && nid != (u16)-1)
3265 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003266 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return change;
3268}
3269
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003270static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 {
3272 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003274 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 .info = snd_hda_spdif_mask_info,
3276 .get = snd_hda_spdif_cmask_get,
3277 },
3278 {
3279 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3280 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003281 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 .info = snd_hda_spdif_mask_info,
3283 .get = snd_hda_spdif_pmask_get,
3284 },
3285 {
3286 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003287 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 .info = snd_hda_spdif_mask_info,
3289 .get = snd_hda_spdif_default_get,
3290 .put = snd_hda_spdif_default_put,
3291 },
3292 {
3293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003294 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 .info = snd_hda_spdif_out_switch_info,
3296 .get = snd_hda_spdif_out_switch_get,
3297 .put = snd_hda_spdif_out_switch_put,
3298 },
3299 { } /* end */
3300};
3301
3302/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003303 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003305 * @associated_nid: NID that new ctls associated with
3306 * @cvt_nid: converter NID
3307 * @type: HDA_PCM_TYPE_*
3308 * Creates controls related with the digital output.
3309 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 *
3311 * Returns 0 if successful, or a negative error code.
3312 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003313int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3314 hda_nid_t associated_nid,
3315 hda_nid_t cvt_nid,
3316 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003319 struct snd_kcontrol *kctl;
3320 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003321 int idx = 0;
3322 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003323 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003324 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003326 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003327 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003328 idx = spdif_index;
3329 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003330 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003331 /* suppose a single SPDIF device */
3332 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3333 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3334 if (!kctl)
3335 break;
3336 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003337 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003338 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003339 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003340 if (!bus->primary_dig_out_type)
3341 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003342
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003343 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003344 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003345 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3346 return -EBUSY;
3347 }
Stephen Warren7c935972011-06-01 11:14:17 -06003348 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003349 if (!spdif)
3350 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3352 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003353 if (!kctl)
3354 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003355 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003356 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003357 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003358 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return err;
3360 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003361 spdif->nid = cvt_nid;
3362 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003363 AC_VERB_GET_DIGI_CONVERT_1, 0);
3364 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return 0;
3366}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003367EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003369/* get the hda_spdif_out entry from the given NID
3370 * call within spdif_mutex lock
3371 */
Stephen Warren7c935972011-06-01 11:14:17 -06003372struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3373 hda_nid_t nid)
3374{
3375 int i;
3376 for (i = 0; i < codec->spdif_out.used; i++) {
3377 struct hda_spdif_out *spdif =
3378 snd_array_elem(&codec->spdif_out, i);
3379 if (spdif->nid == nid)
3380 return spdif;
3381 }
3382 return NULL;
3383}
3384EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3385
Stephen Warren74b654c2011-06-01 11:14:18 -06003386void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3387{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003388 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003389
3390 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003391 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003392 spdif->nid = (u16)-1;
3393 mutex_unlock(&codec->spdif_mutex);
3394}
3395EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3396
3397void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3398{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003399 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003400 unsigned short val;
3401
3402 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003403 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003404 if (spdif->nid != nid) {
3405 spdif->nid = nid;
3406 val = spdif->ctls;
3407 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3408 }
3409 mutex_unlock(&codec->spdif_mutex);
3410}
3411EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003414 * SPDIF sharing with analog output
3415 */
3416static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3420 ucontrol->value.integer.value[0] = mout->share_spdif;
3421 return 0;
3422}
3423
3424static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_value *ucontrol)
3426{
3427 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3428 mout->share_spdif = !!ucontrol->value.integer.value[0];
3429 return 0;
3430}
3431
3432static struct snd_kcontrol_new spdif_share_sw = {
3433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3434 .name = "IEC958 Default PCM Playback Switch",
3435 .info = snd_ctl_boolean_mono_info,
3436 .get = spdif_share_sw_get,
3437 .put = spdif_share_sw_put,
3438};
3439
Takashi Iwaid5191e52009-11-16 14:58:17 +01003440/**
3441 * snd_hda_create_spdif_share_sw - create Default PCM switch
3442 * @codec: the HDA codec
3443 * @mout: multi-out instance
3444 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003445int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3446 struct hda_multi_out *mout)
3447{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003448 struct snd_kcontrol *kctl;
3449
Takashi Iwai9a081602008-02-12 18:37:26 +01003450 if (!mout->dig_out_nid)
3451 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003452
3453 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3454 if (!kctl)
3455 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003456 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003457 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003458}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003459EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003460
3461/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 * SPDIF input
3463 */
3464
3465#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3466
Takashi Iwai0ba21762007-04-16 11:29:14 +02003467static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3471
3472 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3473 return 0;
3474}
3475
Takashi Iwai0ba21762007-04-16 11:29:14 +02003476static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3477 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
3479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3480 hda_nid_t nid = kcontrol->private_value;
3481 unsigned int val = !!ucontrol->value.integer.value[0];
3482 int change;
3483
Ingo Molnar62932df2006-01-16 16:34:20 +01003484 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003486 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003488 snd_hda_codec_write_cache(codec, nid, 0,
3489 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003491 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return change;
3493}
3494
Takashi Iwai0ba21762007-04-16 11:29:14 +02003495static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3496 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3499 hda_nid_t nid = kcontrol->private_value;
3500 unsigned short val;
3501 unsigned int sbits;
3502
Andrew Paprocki3982d172007-12-19 12:13:44 +01003503 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 sbits = convert_to_spdif_status(val);
3505 ucontrol->value.iec958.status[0] = sbits;
3506 ucontrol->value.iec958.status[1] = sbits >> 8;
3507 ucontrol->value.iec958.status[2] = sbits >> 16;
3508 ucontrol->value.iec958.status[3] = sbits >> 24;
3509 return 0;
3510}
3511
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003512static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 {
3514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003515 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 .info = snd_hda_spdif_in_switch_info,
3517 .get = snd_hda_spdif_in_switch_get,
3518 .put = snd_hda_spdif_in_switch_put,
3519 },
3520 {
3521 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003523 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 .info = snd_hda_spdif_mask_info,
3525 .get = snd_hda_spdif_in_status_get,
3526 },
3527 { } /* end */
3528};
3529
3530/**
3531 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3532 * @codec: the HDA codec
3533 * @nid: audio in widget NID
3534 *
3535 * Creates controls related with the SPDIF input.
3536 * Called from each patch supporting the SPDIF in.
3537 *
3538 * Returns 0 if successful, or a negative error code.
3539 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003540int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
3542 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003543 struct snd_kcontrol *kctl;
3544 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003545 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Takashi Iwaidcda5802012-10-12 17:24:51 +02003547 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003548 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003549 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3550 return -EBUSY;
3551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3553 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003554 if (!kctl)
3555 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003557 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return err;
3560 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003561 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003562 snd_hda_codec_read(codec, nid, 0,
3563 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003564 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return 0;
3566}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003567EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003569/*
3570 * command cache
3571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Takashi Iwaic370dd62012-12-13 18:30:04 +01003573/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003574#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3575#define get_cmd_cache_nid(key) ((key) & 0xff)
3576#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3577
3578/**
3579 * snd_hda_codec_write_cache - send a single command with caching
3580 * @codec: the HDA codec
3581 * @nid: NID to send the command
3582 * @direct: direct flag
3583 * @verb: the verb to send
3584 * @parm: the parameter for the verb
3585 *
3586 * Send a single command without waiting for response.
3587 *
3588 * Returns 0 if successful, or a negative error code.
3589 */
3590int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3591 int direct, unsigned int verb, unsigned int parm)
3592{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003593 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003594 struct hda_cache_head *c;
3595 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003596 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003597
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003598 cache_only = codec->cached_write;
3599 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003600 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3601 if (err < 0)
3602 return err;
3603 }
3604
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003605 /* parm may contain the verb stuff for get/set amp */
3606 verb = verb | (parm >> 8);
3607 parm &= 0xff;
3608 key = build_cmd_cache_key(nid, verb);
3609 mutex_lock(&codec->bus->cmd_mutex);
3610 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003611 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003612 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003613 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003614 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003615 mutex_unlock(&codec->bus->cmd_mutex);
3616 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003617}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003618EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003619
Takashi Iwaid5191e52009-11-16 14:58:17 +01003620/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003621 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3622 * @codec: the HDA codec
3623 * @nid: NID to send the command
3624 * @direct: direct flag
3625 * @verb: the verb to send
3626 * @parm: the parameter for the verb
3627 *
3628 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3629 * command if the parameter is already identical with the cached value.
3630 * If not, it sends the command and refreshes the cache.
3631 *
3632 * Returns 0 if successful, or a negative error code.
3633 */
3634int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3635 int direct, unsigned int verb, unsigned int parm)
3636{
3637 struct hda_cache_head *c;
3638 u32 key;
3639
3640 /* parm may contain the verb stuff for get/set amp */
3641 verb = verb | (parm >> 8);
3642 parm &= 0xff;
3643 key = build_cmd_cache_key(nid, verb);
3644 mutex_lock(&codec->bus->cmd_mutex);
3645 c = get_hash(&codec->cmd_cache, key);
3646 if (c && c->val == parm) {
3647 mutex_unlock(&codec->bus->cmd_mutex);
3648 return 0;
3649 }
3650 mutex_unlock(&codec->bus->cmd_mutex);
3651 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3652}
3653EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3654
3655/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003656 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3657 * @codec: HD-audio codec
3658 *
3659 * Execute all verbs recorded in the command caches to resume.
3660 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003661void snd_hda_codec_resume_cache(struct hda_codec *codec)
3662{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003663 int i;
3664
Takashi Iwaic370dd62012-12-13 18:30:04 +01003665 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003666 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003667 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3668 struct hda_cache_head *buffer;
3669 u32 key;
3670
3671 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3672 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003673 if (!key)
3674 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003675 if (!buffer->dirty)
3676 continue;
3677 buffer->dirty = 0;
3678 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003679 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3680 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003681 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003682 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003683 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003684}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003685EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003686
3687/**
3688 * snd_hda_sequence_write_cache - sequence writes with caching
3689 * @codec: the HDA codec
3690 * @seq: VERB array to send
3691 *
3692 * Send the commands sequentially from the given array.
3693 * Thte commands are recorded on cache for power-save and resume.
3694 * The array must be terminated with NID=0.
3695 */
3696void snd_hda_sequence_write_cache(struct hda_codec *codec,
3697 const struct hda_verb *seq)
3698{
3699 for (; seq->nid; seq++)
3700 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3701 seq->param);
3702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003703EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003704
Takashi Iwaidc870f32013-01-22 15:24:30 +01003705/**
3706 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3707 * @codec: HD-audio codec
3708 */
3709void snd_hda_codec_flush_cache(struct hda_codec *codec)
3710{
3711 snd_hda_codec_resume_amp(codec);
3712 snd_hda_codec_resume_cache(codec);
3713}
3714EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3715
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003716void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003717 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003718{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003719 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003720 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003721
Takashi Iwaicb53c622007-08-10 17:21:45 +02003722 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003723 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003724 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003725 if (!(wcaps & AC_WCAP_POWER))
3726 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003727 if (codec->power_filter) {
3728 state = codec->power_filter(codec, nid, power_state);
3729 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003730 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003731 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003732 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003733 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003734 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003735}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003736EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3737
3738/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003739 * supported power states check
3740 */
3741static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3742 unsigned int power_state)
3743{
3744 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3745
Mengdong Line037cb42012-08-10 14:11:58 +02003746 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003747 return false;
3748 if (sup & power_state)
3749 return true;
3750 else
3751 return false;
3752}
3753
3754/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003755 * wait until the state is reached, returns the current state
3756 */
3757static unsigned int hda_sync_power_state(struct hda_codec *codec,
3758 hda_nid_t fg,
3759 unsigned int power_state)
3760{
3761 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3762 unsigned int state, actual_state;
3763
3764 for (;;) {
3765 state = snd_hda_codec_read(codec, fg, 0,
3766 AC_VERB_GET_POWER_STATE, 0);
3767 if (state & AC_PWRST_ERROR)
3768 break;
3769 actual_state = (state >> 4) & 0x0f;
3770 if (actual_state == power_state)
3771 break;
3772 if (time_after_eq(jiffies, end_time))
3773 break;
3774 /* wait until the codec reachs to the target state */
3775 msleep(1);
3776 }
3777 return state;
3778}
3779
Takashi Iwai9419ab62013-01-24 17:23:35 +01003780/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003781unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3782 hda_nid_t nid,
3783 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003784{
3785 if (power_state == AC_PWRST_D3 &&
3786 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3787 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3788 int eapd = snd_hda_codec_read(codec, nid, 0,
3789 AC_VERB_GET_EAPD_BTLENABLE, 0);
3790 if (eapd & 0x02)
3791 return AC_PWRST_D0;
3792 }
3793 return power_state;
3794}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003795EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003796
Takashi Iwai432c6412012-08-28 09:59:20 -07003797/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003798 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003799 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003800static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003801 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003802{
Takashi Iwaid8193872012-08-31 07:54:38 -07003803 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003804 int count;
3805 unsigned int state;
3806
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003807 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003808 if (power_state == AC_PWRST_D3) {
3809 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003810 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003811 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003812
3813 /* repeat power states setting at most 10 times*/
3814 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003815 if (codec->patch_ops.set_power_state)
3816 codec->patch_ops.set_power_state(codec, fg,
3817 power_state);
3818 else {
3819 snd_hda_codec_read(codec, fg, 0,
3820 AC_VERB_SET_POWER_STATE,
3821 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003822 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003823 }
3824 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003825 if (!(state & AC_PWRST_ERROR))
3826 break;
3827 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003828
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003829 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003830}
Takashi Iwai54d17402005-11-21 16:33:22 +01003831
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003832/* sync power states of all widgets;
3833 * this is called at the end of codec parsing
3834 */
3835static void sync_power_up_states(struct hda_codec *codec)
3836{
3837 hda_nid_t nid = codec->start_nid;
3838 int i;
3839
Takashi Iwaiba615b82013-03-13 14:47:21 +01003840 /* don't care if no filter is used */
3841 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003842 return;
3843
3844 for (i = 0; i < codec->num_nodes; i++, nid++) {
3845 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003846 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003847 if (!(wcaps & AC_WCAP_POWER))
3848 continue;
3849 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3850 if (target == AC_PWRST_D0)
3851 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003852 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003853 snd_hda_codec_write(codec, nid, 0,
3854 AC_VERB_SET_POWER_STATE, target);
3855 }
3856}
3857
Takashi Iwai11aeff02008-07-30 15:01:46 +02003858#ifdef CONFIG_SND_HDA_HWDEP
3859/* execute additional init verbs */
3860static void hda_exec_init_verbs(struct hda_codec *codec)
3861{
3862 if (codec->init_verbs.list)
3863 snd_hda_sequence_write(codec, codec->init_verbs.list);
3864}
3865#else
3866static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3867#endif
3868
Takashi Iwai2a439522011-07-26 09:52:50 +02003869#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003870/*
3871 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003872 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003873 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003874static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003875{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003876 unsigned int state;
3877
Takashi Iwai989c3182012-11-19 14:14:58 +01003878 codec->in_pm = 1;
3879
Takashi Iwaicb53c622007-08-10 17:21:45 +02003880 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003881 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003882 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003883 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003884 /* Cancel delayed work if we aren't currently running from it. */
3885 if (!in_wq)
3886 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003887 spin_lock(&codec->power_lock);
3888 snd_hda_update_power_acct(codec);
3889 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003890 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003891 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003892 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003893 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003894 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003895 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003896}
3897
Takashi Iwaic370dd62012-12-13 18:30:04 +01003898/* mark all entries of cmd and amp caches dirty */
3899static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3900{
3901 int i;
3902 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3903 struct hda_cache_head *cmd;
3904 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3905 cmd->dirty = 1;
3906 }
3907 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3908 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003909 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003910 amp->head.dirty = 1;
3911 }
3912}
3913
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914/*
3915 * kick up codec; used both from PM and power-save
3916 */
3917static void hda_call_codec_resume(struct hda_codec *codec)
3918{
Takashi Iwai989c3182012-11-19 14:14:58 +01003919 codec->in_pm = 1;
3920
Takashi Iwaic370dd62012-12-13 18:30:04 +01003921 hda_mark_cmd_cache_dirty(codec);
3922
Takashi Iwai7f308302012-05-08 16:52:23 +02003923 /* set as if powered on for avoiding re-entering the resume
3924 * in the resume / power-save sequence
3925 */
3926 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003927 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003928 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003929 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003930 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003931 if (codec->patch_ops.resume)
3932 codec->patch_ops.resume(codec);
3933 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003934 if (codec->patch_ops.init)
3935 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003936 snd_hda_codec_resume_amp(codec);
3937 snd_hda_codec_resume_cache(codec);
3938 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003939
3940 if (codec->jackpoll_interval)
3941 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003942 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003943 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003944
3945 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003946 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003947}
Takashi Iwai2a439522011-07-26 09:52:50 +02003948#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003949
Takashi Iwai54d17402005-11-21 16:33:22 +01003950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951/**
3952 * snd_hda_build_controls - build mixer controls
3953 * @bus: the BUS
3954 *
3955 * Creates mixer controls for each codec included in the bus.
3956 *
3957 * Returns 0 if successful, otherwise a negative error code.
3958 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003959int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003961 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Takashi Iwai0ba21762007-04-16 11:29:14 +02003963 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003964 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003965 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003966 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003967 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003968 err = snd_hda_codec_reset(codec);
3969 if (err < 0) {
3970 printk(KERN_ERR
3971 "hda_codec: cannot revert codec\n");
3972 return err;
3973 }
3974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003976 return 0;
3977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003978EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003980/*
3981 * add standard channel maps if not specified
3982 */
3983static int add_std_chmaps(struct hda_codec *codec)
3984{
3985 int i, str, err;
3986
3987 for (i = 0; i < codec->num_pcms; i++) {
3988 for (str = 0; str < 2; str++) {
3989 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3990 struct hda_pcm_stream *hinfo =
3991 &codec->pcm_info[i].stream[str];
3992 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003993 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003994
3995 if (codec->pcm_info[i].own_chmap)
3996 continue;
3997 if (!pcm || !hinfo->substreams)
3998 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003999 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4000 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004001 hinfo->channels_max,
4002 0, &chmap);
4003 if (err < 0)
4004 return err;
4005 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4006 }
4007 }
4008 return 0;
4009}
4010
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004011/* default channel maps for 2.1 speakers;
4012 * since HD-audio supports only stereo, odd number channels are omitted
4013 */
4014const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4015 { .channels = 2,
4016 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4017 { .channels = 4,
4018 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4019 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4020 { }
4021};
4022EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4023
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004024int snd_hda_codec_build_controls(struct hda_codec *codec)
4025{
4026 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004027 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004028 /* continue to initialize... */
4029 if (codec->patch_ops.init)
4030 err = codec->patch_ops.init(codec);
4031 if (!err && codec->patch_ops.build_controls)
4032 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004033 if (err < 0)
4034 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004035
4036 /* we create chmaps here instead of build_pcms */
4037 err = add_std_chmaps(codec);
4038 if (err < 0)
4039 return err;
4040
David Henningsson26a6cb62012-10-09 15:04:21 +02004041 if (codec->jackpoll_interval)
4042 hda_jackpoll_work(&codec->jackpoll_work.work);
4043 else
4044 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004045 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 return 0;
4047}
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049/*
4050 * stream formats
4051 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004052struct hda_rate_tbl {
4053 unsigned int hz;
4054 unsigned int alsa_bits;
4055 unsigned int hda_fmt;
4056};
4057
Takashi Iwai92f10b32010-08-03 14:21:00 +02004058/* rate = base * mult / div */
4059#define HDA_RATE(base, mult, div) \
4060 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4061 (((div) - 1) << AC_FMT_DIV_SHIFT))
4062
Takashi Iwaibefdf312005-08-22 13:57:55 +02004063static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004065
4066 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004067 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4068 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4069 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4070 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4071 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4072 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4073 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4074 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4075 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4076 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4077 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004078#define AC_PAR_PCM_RATE_BITS 11
4079 /* up to bits 10, 384kHZ isn't supported properly */
4080
4081 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004082 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004083
Takashi Iwaibefdf312005-08-22 13:57:55 +02004084 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085};
4086
4087/**
4088 * snd_hda_calc_stream_format - calculate format bitset
4089 * @rate: the sample rate
4090 * @channels: the number of channels
4091 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4092 * @maxbps: the max. bps
4093 *
4094 * Calculate the format bitset from the given rate, channels and th PCM format.
4095 *
4096 * Return zero if invalid.
4097 */
4098unsigned int snd_hda_calc_stream_format(unsigned int rate,
4099 unsigned int channels,
4100 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004101 unsigned int maxbps,
4102 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 int i;
4105 unsigned int val = 0;
4106
Takashi Iwaibefdf312005-08-22 13:57:55 +02004107 for (i = 0; rate_bits[i].hz; i++)
4108 if (rate_bits[i].hz == rate) {
4109 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 break;
4111 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004112 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 snd_printdd("invalid rate %d\n", rate);
4114 return 0;
4115 }
4116
4117 if (channels == 0 || channels > 8) {
4118 snd_printdd("invalid channels %d\n", channels);
4119 return 0;
4120 }
4121 val |= channels - 1;
4122
4123 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004124 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004125 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004126 break;
4127 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004128 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 case 20:
4131 case 24:
4132 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004133 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004134 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004136 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004138 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 break;
4140 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004141 snd_printdd("invalid format width %d\n",
4142 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return 0;
4144 }
4145
Anssi Hannula32c168c2010-08-03 13:28:57 +03004146 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004147 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return val;
4150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004151EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004153static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4154 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004155{
4156 unsigned int val = 0;
4157 if (nid != codec->afg &&
4158 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4159 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4160 if (!val || val == -1)
4161 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4162 if (!val || val == -1)
4163 return 0;
4164 return val;
4165}
4166
4167static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4168{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004169 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004170 get_pcm_param);
4171}
4172
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004173static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4174 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004175{
4176 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4177 if (!streams || streams == -1)
4178 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4179 if (!streams || streams == -1)
4180 return 0;
4181 return streams;
4182}
4183
4184static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4185{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004186 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004187 get_stream_param);
4188}
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190/**
4191 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4192 * @codec: the HDA codec
4193 * @nid: NID to query
4194 * @ratesp: the pointer to store the detected rate bitflags
4195 * @formatsp: the pointer to store the detected formats
4196 * @bpsp: the pointer to store the detected format widths
4197 *
4198 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4199 * or @bsps argument is ignored.
4200 *
4201 * Returns 0 if successful, otherwise a negative error code.
4202 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004203int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4205{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004206 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004208 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004209 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 if (ratesp) {
4212 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004213 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004215 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004217 if (rates == 0) {
4218 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4219 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4220 nid, val,
4221 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4222 return -EIO;
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 *ratesp = rates;
4225 }
4226
4227 if (formatsp || bpsp) {
4228 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004229 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004231 streams = query_stream_param(codec, nid);
4232 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 bps = 0;
4236 if (streams & AC_SUPFMT_PCM) {
4237 if (val & AC_SUPPCM_BITS_8) {
4238 formats |= SNDRV_PCM_FMTBIT_U8;
4239 bps = 8;
4240 }
4241 if (val & AC_SUPPCM_BITS_16) {
4242 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4243 bps = 16;
4244 }
4245 if (wcaps & AC_WCAP_DIGITAL) {
4246 if (val & AC_SUPPCM_BITS_32)
4247 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4248 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4249 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4250 if (val & AC_SUPPCM_BITS_24)
4251 bps = 24;
4252 else if (val & AC_SUPPCM_BITS_20)
4253 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004254 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4255 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4257 if (val & AC_SUPPCM_BITS_32)
4258 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 else if (val & AC_SUPPCM_BITS_24)
4260 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004261 else if (val & AC_SUPPCM_BITS_20)
4262 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004265#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004266 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004268 if (!bps)
4269 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004270 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004271#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004272 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004273 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 /* temporary hack: we have still no proper support
4275 * for the direct AC3 stream...
4276 */
4277 formats |= SNDRV_PCM_FMTBIT_U8;
4278 bps = 8;
4279 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004280 if (formats == 0) {
4281 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4282 "(nid=0x%x, val=0x%x, ovrd=%i, "
4283 "streams=0x%x)\n",
4284 nid, val,
4285 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4286 streams);
4287 return -EIO;
4288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 if (formatsp)
4290 *formatsp = formats;
4291 if (bpsp)
4292 *bpsp = bps;
4293 }
4294
4295 return 0;
4296}
Stephen Warren384a48d2011-06-01 11:14:21 -06004297EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004300 * snd_hda_is_supported_format - Check the validity of the format
4301 * @codec: HD-audio codec
4302 * @nid: NID to check
4303 * @format: the HD-audio format value to check
4304 *
4305 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 *
4307 * Returns 1 if supported, 0 if not.
4308 */
4309int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4310 unsigned int format)
4311{
4312 int i;
4313 unsigned int val = 0, rate, stream;
4314
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004315 val = query_pcm_param(codec, nid);
4316 if (!val)
4317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004320 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004321 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 if (val & (1 << i))
4323 break;
4324 return 0;
4325 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004326 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return 0;
4328
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004329 stream = query_stream_param(codec, nid);
4330 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
4332
4333 if (stream & AC_SUPFMT_PCM) {
4334 switch (format & 0xf0) {
4335 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004336 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return 0;
4338 break;
4339 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004340 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return 0;
4342 break;
4343 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004344 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return 0;
4346 break;
4347 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004348 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return 0;
4350 break;
4351 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004352 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return 0;
4354 break;
4355 default:
4356 return 0;
4357 }
4358 } else {
4359 /* FIXME: check for float32 and AC3? */
4360 }
4361
4362 return 1;
4363}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004364EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
4366/*
4367 * PCM stuff
4368 */
4369static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4370 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004371 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 return 0;
4374}
4375
4376static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4377 struct hda_codec *codec,
4378 unsigned int stream_tag,
4379 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004380 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4383 return 0;
4384}
4385
4386static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4387 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004388 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Takashi Iwai888afa12008-03-18 09:57:50 +01004390 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392}
4393
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004394static int set_pcm_default_values(struct hda_codec *codec,
4395 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004397 int err;
4398
Takashi Iwai0ba21762007-04-16 11:29:14 +02004399 /* query support PCM information from the given NID */
4400 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004401 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004402 info->rates ? NULL : &info->rates,
4403 info->formats ? NULL : &info->formats,
4404 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004405 if (err < 0)
4406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408 if (info->ops.open == NULL)
4409 info->ops.open = hda_pcm_default_open_close;
4410 if (info->ops.close == NULL)
4411 info->ops.close = hda_pcm_default_open_close;
4412 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004413 if (snd_BUG_ON(!info->nid))
4414 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 info->ops.prepare = hda_pcm_default_prepare;
4416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004418 if (snd_BUG_ON(!info->nid))
4419 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 info->ops.cleanup = hda_pcm_default_cleanup;
4421 }
4422 return 0;
4423}
4424
Takashi Iwaieb541332010-08-06 13:48:11 +02004425/*
4426 * codec prepare/cleanup entries
4427 */
4428int snd_hda_codec_prepare(struct hda_codec *codec,
4429 struct hda_pcm_stream *hinfo,
4430 unsigned int stream,
4431 unsigned int format,
4432 struct snd_pcm_substream *substream)
4433{
4434 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004435 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004436 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4437 if (ret >= 0)
4438 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004439 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004440 return ret;
4441}
4442EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4443
4444void snd_hda_codec_cleanup(struct hda_codec *codec,
4445 struct hda_pcm_stream *hinfo,
4446 struct snd_pcm_substream *substream)
4447{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004448 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004449 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004450 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004451}
4452EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4453
Takashi Iwaid5191e52009-11-16 14:58:17 +01004454/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004455const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4456 "Audio", "SPDIF", "HDMI", "Modem"
4457};
4458
Takashi Iwai176d5332008-07-30 15:01:44 +02004459/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004460 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004461 *
4462 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004463 */
4464static int get_empty_pcm_device(struct hda_bus *bus, int type)
4465{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004466 /* audio device indices; not linear to keep compatibility */
4467 static int audio_idx[HDA_PCM_NTYPES][5] = {
4468 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4469 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004470 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004471 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004472 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004473 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004474
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004475 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004476 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4477 return -EINVAL;
4478 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004479
4480 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4481 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4482 return audio_idx[type][i];
4483
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004484 /* non-fixed slots starting from 10 */
4485 for (i = 10; i < 32; i++) {
4486 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4487 return i;
4488 }
4489
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004490 snd_printk(KERN_WARNING "Too many %s devices\n",
4491 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004492 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004493}
4494
4495/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004496 * attach a new PCM stream
4497 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004498static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004499{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004500 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004501 struct hda_pcm_stream *info;
4502 int stream, err;
4503
Takashi Iwaib91f0802008-11-04 08:43:08 +01004504 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004505 return -EINVAL;
4506 for (stream = 0; stream < 2; stream++) {
4507 info = &pcm->stream[stream];
4508 if (info->substreams) {
4509 err = set_pcm_default_values(codec, info);
4510 if (err < 0)
4511 return err;
4512 }
4513 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004514 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004515}
4516
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004517/* assign all PCMs of the given codec */
4518int snd_hda_codec_build_pcms(struct hda_codec *codec)
4519{
4520 unsigned int pcm;
4521 int err;
4522
4523 if (!codec->num_pcms) {
4524 if (!codec->patch_ops.build_pcms)
4525 return 0;
4526 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004527 if (err < 0) {
4528 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004529 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004530 err = snd_hda_codec_reset(codec);
4531 if (err < 0) {
4532 printk(KERN_ERR
4533 "hda_codec: cannot revert codec\n");
4534 return err;
4535 }
4536 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004537 }
4538 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4539 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4540 int dev;
4541
4542 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004543 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004544
4545 if (!cpcm->pcm) {
4546 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4547 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004548 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004549 cpcm->device = dev;
4550 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004551 if (err < 0) {
4552 printk(KERN_ERR "hda_codec: cannot attach "
4553 "PCM stream %d for codec #%d\n",
4554 dev, codec->addr);
4555 continue; /* no fatal error */
4556 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004557 }
4558 }
4559 return 0;
4560}
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562/**
4563 * snd_hda_build_pcms - build PCM information
4564 * @bus: the BUS
4565 *
4566 * Create PCM information for each codec included in the bus.
4567 *
4568 * The build_pcms codec patch is requested to set up codec->num_pcms and
4569 * codec->pcm_info properly. The array is referred by the top-level driver
4570 * to create its PCM instances.
4571 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4572 * callback.
4573 *
4574 * At least, substreams, channels_min and channels_max must be filled for
4575 * each stream. substreams = 0 indicates that the stream doesn't exist.
4576 * When rates and/or formats are zero, the supported values are queried
4577 * from the given nid. The nid is used also by the default ops.prepare
4578 * and ops.cleanup callbacks.
4579 *
4580 * The driver needs to call ops.open in its open callback. Similarly,
4581 * ops.close is supposed to be called in the close callback.
4582 * ops.prepare should be called in the prepare or hw_params callback
4583 * with the proper parameters for set up.
4584 * ops.cleanup should be called in hw_free for clean up of streams.
4585 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004586 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004588int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Takashi Iwai0ba21762007-04-16 11:29:14 +02004592 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004593 int err = snd_hda_codec_build_pcms(codec);
4594 if (err < 0)
4595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
4597 return 0;
4598}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004599EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601/**
4602 * snd_hda_check_board_config - compare the current codec with the config table
4603 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004604 * @num_configs: number of config enums
4605 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 * @tbl: configuration table, terminated by null entries
4607 *
4608 * Compares the modelname or PCI subsystem id of the current codec with the
4609 * given configuration table. If a matching entry is found, returns its
4610 * config value (supposed to be 0 or positive).
4611 *
4612 * If no entries are matching, the function returns a negative value.
4613 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004614int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004615 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004616 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004618 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004619 int i;
4620 for (i = 0; i < num_configs; i++) {
4621 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004622 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004623 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4624 "selected\n", models[i]);
4625 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
4627 }
4628 }
4629
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004630 if (!codec->bus->pci || !tbl)
4631 return -1;
4632
4633 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4634 if (!tbl)
4635 return -1;
4636 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004637#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004638 char tmp[10];
4639 const char *model = NULL;
4640 if (models)
4641 model = models[tbl->value];
4642 if (!model) {
4643 sprintf(tmp, "#%d", tbl->value);
4644 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004646 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4647 "for config %x:%x (%s)\n",
4648 model, tbl->subvendor, tbl->subdevice,
4649 (tbl->name ? tbl->name : "Unknown device"));
4650#endif
4651 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
4653 return -1;
4654}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004655EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
4657/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004658 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004659 subsystem ID with the
4660 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004661
4662 This is important for Gateway notebooks with SB450 HDA Audio
4663 where the vendor ID of the PCI device is:
4664 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4665 and the vendor/subvendor are found only at the codec.
4666
4667 * @codec: the HDA codec
4668 * @num_configs: number of config enums
4669 * @models: array of model name strings
4670 * @tbl: configuration table, terminated by null entries
4671 *
4672 * Compares the modelname or PCI subsystem id of the current codec with the
4673 * given configuration table. If a matching entry is found, returns its
4674 * config value (supposed to be 0 or positive).
4675 *
4676 * If no entries are matching, the function returns a negative value.
4677 */
4678int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004679 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004680 const struct snd_pci_quirk *tbl)
4681{
4682 const struct snd_pci_quirk *q;
4683
4684 /* Search for codec ID */
4685 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004686 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4687 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4688 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004689 break;
4690 }
4691
4692 if (!q->subvendor)
4693 return -1;
4694
4695 tbl = q;
4696
4697 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004698#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004699 char tmp[10];
4700 const char *model = NULL;
4701 if (models)
4702 model = models[tbl->value];
4703 if (!model) {
4704 sprintf(tmp, "#%d", tbl->value);
4705 model = tmp;
4706 }
4707 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4708 "for config %x:%x (%s)\n",
4709 model, tbl->subvendor, tbl->subdevice,
4710 (tbl->name ? tbl->name : "Unknown device"));
4711#endif
4712 return tbl->value;
4713 }
4714 return -1;
4715}
4716EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4717
4718/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 * snd_hda_add_new_ctls - create controls from the array
4720 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004721 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 *
4723 * This helper function creates and add new controls in the given array.
4724 * The array must be terminated with an empty entry as terminator.
4725 *
4726 * Returns 0 if successful, or a negative error code.
4727 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004728int snd_hda_add_new_ctls(struct hda_codec *codec,
4729 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004731 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
4733 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004734 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004735 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004736 if (knew->iface == -1) /* skip this codec private value */
4737 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004738 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004739 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004740 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004741 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004742 if (addr > 0)
4743 kctl->id.device = addr;
4744 if (idx > 0)
4745 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004746 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004747 if (!err)
4748 break;
4749 /* try first with another device index corresponding to
4750 * the codec addr; if it still fails (or it's the
4751 * primary codec), then try another control index
4752 */
4753 if (!addr && codec->addr)
4754 addr = codec->addr;
4755 else if (!idx && !knew->index) {
4756 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004757 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004758 if (idx <= 0)
4759 return err;
4760 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004761 return err;
4762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
4764 return 0;
4765}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004766EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Takashi Iwai83012a72012-08-24 18:38:08 +02004768#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004769static void hda_power_work(struct work_struct *work)
4770{
4771 struct hda_codec *codec =
4772 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004773 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004774 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004775
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004776 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004777 if (codec->power_transition > 0) { /* during power-up sequence? */
4778 spin_unlock(&codec->power_lock);
4779 return;
4780 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004781 if (!codec->power_on || codec->power_count) {
4782 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004783 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004785 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004786 spin_unlock(&codec->power_lock);
4787
Dylan Reidd17344b2012-09-28 15:57:01 -07004788 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004789 codec->pm_down_notified = 0;
4790 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4791 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004792 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004793 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004794}
4795
4796static void hda_keep_power_on(struct hda_codec *codec)
4797{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004798 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004799 codec->power_count++;
4800 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004801 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004802 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004803}
4804
Takashi Iwaid5191e52009-11-16 14:58:17 +01004805/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004806void snd_hda_update_power_acct(struct hda_codec *codec)
4807{
4808 unsigned long delta = jiffies - codec->power_jiffies;
4809 if (codec->power_on)
4810 codec->power_on_acct += delta;
4811 else
4812 codec->power_off_acct += delta;
4813 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004814}
4815
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004816/* Transition to powered up, if wait_power_down then wait for a pending
4817 * transition to D3 to complete. A pending D3 transition is indicated
4818 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004819/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004820static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004821{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004822 struct hda_bus *bus = codec->bus;
4823
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004824 /* Return if power_on or transitioning to power_on, unless currently
4825 * powering down. */
4826 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004827 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004828 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004829 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004830
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004831 cancel_delayed_work_sync(&codec->power_work);
4832
4833 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004834 /* If the power down delayed work was cancelled above before starting,
4835 * then there is no need to go through power up here.
4836 */
4837 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004838 if (codec->power_transition < 0)
4839 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004840 return;
4841 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004842
Takashi Iwaid66fee52011-08-02 15:39:31 +02004843 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004844 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004845 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004846 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004847 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004848 spin_unlock(&codec->power_lock);
4849
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004850 if (codec->pm_down_notified) {
4851 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004852 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004853 }
4854
Takashi Iwaicb53c622007-08-10 17:21:45 +02004855 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004856
4857 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004858 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004859}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004860
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004861#define power_save(codec) \
4862 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004863
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004864/* Transition to powered down */
4865static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004866{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004867 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004868 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004869
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004870 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004871 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004872 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004873 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004874 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004875}
4876
4877/**
4878 * snd_hda_power_save - Power-up/down/sync the codec
4879 * @codec: HD-audio codec
4880 * @delta: the counter delta to change
4881 *
4882 * Change the power-up counter via @delta, and power up or down the hardware
4883 * appropriately. For the power-down, queue to the delayed action.
4884 * Passing zero to @delta means to synchronize the power state.
4885 */
4886void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4887{
4888 spin_lock(&codec->power_lock);
4889 codec->power_count += delta;
4890 trace_hda_power_count(codec);
4891 if (delta > 0)
4892 __snd_hda_power_up(codec, d3wait);
4893 else
4894 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004895 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004896}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004897EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004898
Takashi Iwaid5191e52009-11-16 14:58:17 +01004899/**
4900 * snd_hda_check_amp_list_power - Check the amp list and update the power
4901 * @codec: HD-audio codec
4902 * @check: the object containing an AMP list and the status
4903 * @nid: NID to check / update
4904 *
4905 * Check whether the given NID is in the amp list. If it's in the list,
4906 * check the current AMP status, and update the the power-status according
4907 * to the mute status.
4908 *
4909 * This function is supposed to be set or called from the check_power_status
4910 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004911 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004912int snd_hda_check_amp_list_power(struct hda_codec *codec,
4913 struct hda_loopback_check *check,
4914 hda_nid_t nid)
4915{
Takashi Iwai031024e2011-05-02 11:29:30 +02004916 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004917 int ch, v;
4918
4919 if (!check->amplist)
4920 return 0;
4921 for (p = check->amplist; p->nid; p++) {
4922 if (p->nid == nid)
4923 break;
4924 }
4925 if (!p->nid)
4926 return 0; /* nothing changed */
4927
4928 for (p = check->amplist; p->nid; p++) {
4929 for (ch = 0; ch < 2; ch++) {
4930 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4931 p->idx);
4932 if (!(v & HDA_AMP_MUTE) && v > 0) {
4933 if (!check->power_on) {
4934 check->power_on = 1;
4935 snd_hda_power_up(codec);
4936 }
4937 return 1;
4938 }
4939 }
4940 }
4941 if (check->power_on) {
4942 check->power_on = 0;
4943 snd_hda_power_down(codec);
4944 }
4945 return 0;
4946}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004947EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004950/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004951 * Channel mode helper
4952 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004953
4954/**
4955 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4956 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004957int snd_hda_ch_mode_info(struct hda_codec *codec,
4958 struct snd_ctl_elem_info *uinfo,
4959 const struct hda_channel_mode *chmode,
4960 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004961{
4962 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4963 uinfo->count = 1;
4964 uinfo->value.enumerated.items = num_chmodes;
4965 if (uinfo->value.enumerated.item >= num_chmodes)
4966 uinfo->value.enumerated.item = num_chmodes - 1;
4967 sprintf(uinfo->value.enumerated.name, "%dch",
4968 chmode[uinfo->value.enumerated.item].channels);
4969 return 0;
4970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004971EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004972
Takashi Iwaid5191e52009-11-16 14:58:17 +01004973/**
4974 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4975 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004976int snd_hda_ch_mode_get(struct hda_codec *codec,
4977 struct snd_ctl_elem_value *ucontrol,
4978 const struct hda_channel_mode *chmode,
4979 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004980 int max_channels)
4981{
4982 int i;
4983
4984 for (i = 0; i < num_chmodes; i++) {
4985 if (max_channels == chmode[i].channels) {
4986 ucontrol->value.enumerated.item[0] = i;
4987 break;
4988 }
4989 }
4990 return 0;
4991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004992EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004993
Takashi Iwaid5191e52009-11-16 14:58:17 +01004994/**
4995 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4996 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004997int snd_hda_ch_mode_put(struct hda_codec *codec,
4998 struct snd_ctl_elem_value *ucontrol,
4999 const struct hda_channel_mode *chmode,
5000 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005001 int *max_channelsp)
5002{
5003 unsigned int mode;
5004
5005 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005006 if (mode >= num_chmodes)
5007 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005008 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005009 return 0;
5010 /* change the current channel setting */
5011 *max_channelsp = chmode[mode].channels;
5012 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005013 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005014 return 1;
5015}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005016EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018/*
5019 * input MUX helper
5020 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005021
5022/**
5023 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5024 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005025int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5026 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
5028 unsigned int index;
5029
5030 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5031 uinfo->count = 1;
5032 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005033 if (!imux->num_items)
5034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 index = uinfo->value.enumerated.item;
5036 if (index >= imux->num_items)
5037 index = imux->num_items - 1;
5038 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5039 return 0;
5040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005041EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Takashi Iwaid5191e52009-11-16 14:58:17 +01005043/**
5044 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005046int snd_hda_input_mux_put(struct hda_codec *codec,
5047 const struct hda_input_mux *imux,
5048 struct snd_ctl_elem_value *ucontrol,
5049 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 unsigned int *cur_val)
5051{
5052 unsigned int idx;
5053
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005054 if (!imux->num_items)
5055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 idx = ucontrol->value.enumerated.item[0];
5057 if (idx >= imux->num_items)
5058 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005059 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005061 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5062 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 *cur_val = idx;
5064 return 1;
5065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005066EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068
5069/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005070 * process kcontrol info callback of a simple string enum array
5071 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5072 */
5073int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5074 struct snd_ctl_elem_info *uinfo,
5075 int num_items, const char * const *texts)
5076{
5077 static const char * const texts_default[] = {
5078 "Disabled", "Enabled"
5079 };
5080
5081 if (!texts || !num_items) {
5082 num_items = 2;
5083 texts = texts_default;
5084 }
5085
5086 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5087 uinfo->count = 1;
5088 uinfo->value.enumerated.items = num_items;
5089 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5090 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5091 strcpy(uinfo->value.enumerated.name,
5092 texts[uinfo->value.enumerated.item]);
5093 return 0;
5094}
5095EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5096
5097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 * Multi-channel / digital-out PCM helper functions
5099 */
5100
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005101/* setup SPDIF output stream */
5102static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5103 unsigned int stream_tag, unsigned int format)
5104{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005105 struct hda_spdif_out *spdif;
5106 unsigned int curr_fmt;
5107 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005108
Laurence Darby3bef1c32012-11-03 17:00:06 +00005109 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5110 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5111 AC_VERB_GET_STREAM_FORMAT, 0);
5112 reset = codec->spdif_status_reset &&
5113 (spdif->ctls & AC_DIG1_ENABLE) &&
5114 curr_fmt != format;
5115
5116 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5117 updated */
5118 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005119 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005120 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005121 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005122 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005123 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005124 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005125 for (d = codec->slave_dig_outs; *d; d++)
5126 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5127 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005128 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005129 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005130 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005131 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005132 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005133}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005134
Takashi Iwai2f728532008-09-25 16:32:41 +02005135static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5136{
5137 snd_hda_codec_cleanup_stream(codec, nid);
5138 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005139 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005140 for (d = codec->slave_dig_outs; *d; d++)
5141 snd_hda_codec_cleanup_stream(codec, *d);
5142 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005143}
5144
Takashi Iwaid5191e52009-11-16 14:58:17 +01005145/**
5146 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5147 * @bus: HD-audio bus
5148 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005149void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5150{
5151 struct hda_codec *codec;
5152
5153 if (!bus)
5154 return;
5155 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005156 if (hda_codec_is_power_on(codec) &&
5157 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005158 codec->patch_ops.reboot_notify(codec);
5159 }
5160}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005161EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005162
Takashi Iwaid5191e52009-11-16 14:58:17 +01005163/**
5164 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005166int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5167 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168{
Ingo Molnar62932df2006-01-16 16:34:20 +01005169 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005170 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5171 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005172 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005174 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return 0;
5176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005177EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Takashi Iwaid5191e52009-11-16 14:58:17 +01005179/**
5180 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5181 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005182int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5183 struct hda_multi_out *mout,
5184 unsigned int stream_tag,
5185 unsigned int format,
5186 struct snd_pcm_substream *substream)
5187{
5188 mutex_lock(&codec->spdif_mutex);
5189 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5190 mutex_unlock(&codec->spdif_mutex);
5191 return 0;
5192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005193EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005194
Takashi Iwaid5191e52009-11-16 14:58:17 +01005195/**
5196 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5197 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005198int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5199 struct hda_multi_out *mout)
5200{
5201 mutex_lock(&codec->spdif_mutex);
5202 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5203 mutex_unlock(&codec->spdif_mutex);
5204 return 0;
5205}
5206EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5207
Takashi Iwaid5191e52009-11-16 14:58:17 +01005208/**
5209 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005211int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5212 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213{
Ingo Molnar62932df2006-01-16 16:34:20 +01005214 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005216 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 return 0;
5218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005219EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Takashi Iwaid5191e52009-11-16 14:58:17 +01005221/**
5222 * snd_hda_multi_out_analog_open - open analog outputs
5223 *
5224 * Open analog outputs and set up the hw-constraints.
5225 * If the digital outputs can be opened as slave, open the digital
5226 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005228int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5229 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005230 struct snd_pcm_substream *substream,
5231 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
Takashi Iwai9a081602008-02-12 18:37:26 +01005233 struct snd_pcm_runtime *runtime = substream->runtime;
5234 runtime->hw.channels_max = mout->max_channels;
5235 if (mout->dig_out_nid) {
5236 if (!mout->analog_rates) {
5237 mout->analog_rates = hinfo->rates;
5238 mout->analog_formats = hinfo->formats;
5239 mout->analog_maxbps = hinfo->maxbps;
5240 } else {
5241 runtime->hw.rates = mout->analog_rates;
5242 runtime->hw.formats = mout->analog_formats;
5243 hinfo->maxbps = mout->analog_maxbps;
5244 }
5245 if (!mout->spdif_rates) {
5246 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5247 &mout->spdif_rates,
5248 &mout->spdif_formats,
5249 &mout->spdif_maxbps);
5250 }
5251 mutex_lock(&codec->spdif_mutex);
5252 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005253 if ((runtime->hw.rates & mout->spdif_rates) &&
5254 (runtime->hw.formats & mout->spdif_formats)) {
5255 runtime->hw.rates &= mout->spdif_rates;
5256 runtime->hw.formats &= mout->spdif_formats;
5257 if (mout->spdif_maxbps < hinfo->maxbps)
5258 hinfo->maxbps = mout->spdif_maxbps;
5259 } else {
5260 mout->share_spdif = 0;
5261 /* FIXME: need notify? */
5262 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005263 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005264 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5267 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005269EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Takashi Iwaid5191e52009-11-16 14:58:17 +01005271/**
5272 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5273 *
5274 * Set up the i/o for analog out.
5275 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005277int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5278 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 unsigned int stream_tag,
5280 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005281 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282{
Takashi Iwaidda14412011-05-02 11:29:30 +02005283 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005285 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 int i;
5287
Ingo Molnar62932df2006-01-16 16:34:20 +01005288 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005289 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005290 if (mout->dig_out_nid && mout->share_spdif &&
5291 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005293 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5294 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005295 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005297 setup_dig_out_stream(codec, mout->dig_out_nid,
5298 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 } else {
5300 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005301 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 }
5303 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005304 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005307 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5308 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005309 if (!mout->no_share_stream &&
5310 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005312 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5313 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005314 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005315 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5316 if (!mout->no_share_stream && mout->hp_out_nid[i])
5317 snd_hda_codec_setup_stream(codec,
5318 mout->hp_out_nid[i],
5319 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005320 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005321 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005322 snd_hda_codec_setup_stream(codec,
5323 mout->extra_out_nid[i],
5324 stream_tag, 0, format);
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 /* surrounds */
5327 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005328 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005329 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5330 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005331 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005332 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5333 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
5335 return 0;
5336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005337EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Takashi Iwaid5191e52009-11-16 14:58:17 +01005339/**
5340 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005342int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5343 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
Takashi Iwaidda14412011-05-02 11:29:30 +02005345 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 int i;
5347
5348 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005349 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005351 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005352 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5353 if (mout->hp_out_nid[i])
5354 snd_hda_codec_cleanup_stream(codec,
5355 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005356 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5357 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005358 snd_hda_codec_cleanup_stream(codec,
5359 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005360 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005362 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 mout->dig_out_used = 0;
5364 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005365 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return 0;
5367}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005368EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Takashi Iwai47408602012-04-20 13:06:53 +02005370/**
5371 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5372 *
5373 * Guess the suitable VREF pin bits to be set as the pin-control value.
5374 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5375 */
5376unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5377{
5378 unsigned int pincap;
5379 unsigned int oldval;
5380 oldval = snd_hda_codec_read(codec, pin, 0,
5381 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5382 pincap = snd_hda_query_pin_caps(codec, pin);
5383 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5384 /* Exception: if the default pin setup is vref50, we give it priority */
5385 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5386 return AC_PINCTL_VREF_80;
5387 else if (pincap & AC_PINCAP_VREF_50)
5388 return AC_PINCTL_VREF_50;
5389 else if (pincap & AC_PINCAP_VREF_100)
5390 return AC_PINCTL_VREF_100;
5391 else if (pincap & AC_PINCAP_VREF_GRD)
5392 return AC_PINCTL_VREF_GRD;
5393 return AC_PINCTL_VREF_HIZ;
5394}
5395EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5396
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005397/* correct the pin ctl value for matching with the pin cap */
5398unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5399 hda_nid_t pin, unsigned int val)
5400{
5401 static unsigned int cap_lists[][2] = {
5402 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5403 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5404 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5405 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5406 };
5407 unsigned int cap;
5408
5409 if (!val)
5410 return 0;
5411 cap = snd_hda_query_pin_caps(codec, pin);
5412 if (!cap)
5413 return val; /* don't know what to do... */
5414
5415 if (val & AC_PINCTL_OUT_EN) {
5416 if (!(cap & AC_PINCAP_OUT))
5417 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5418 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5419 val &= ~AC_PINCTL_HP_EN;
5420 }
5421
5422 if (val & AC_PINCTL_IN_EN) {
5423 if (!(cap & AC_PINCAP_IN))
5424 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5425 else {
5426 unsigned int vcap, vref;
5427 int i;
5428 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5429 vref = val & AC_PINCTL_VREFEN;
5430 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5431 if (vref == cap_lists[i][0] &&
5432 !(vcap & cap_lists[i][1])) {
5433 if (i == ARRAY_SIZE(cap_lists) - 1)
5434 vref = AC_PINCTL_VREF_HIZ;
5435 else
5436 vref = cap_lists[i + 1][0];
5437 }
5438 }
5439 val &= ~AC_PINCTL_VREFEN;
5440 val |= vref;
5441 }
5442 }
5443
5444 return val;
5445}
5446EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5447
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005448int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5449 unsigned int val, bool cached)
5450{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005451 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005452 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005453 if (cached)
5454 return snd_hda_codec_update_cache(codec, pin, 0,
5455 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5456 else
5457 return snd_hda_codec_write(codec, pin, 0,
5458 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5459}
5460EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5461
Takashi Iwai990061c2010-09-09 22:08:44 +02005462/**
5463 * snd_hda_add_imux_item - Add an item to input_mux
5464 *
5465 * When the same label is used already in the existing items, the number
5466 * suffix is appended to the label. This label index number is stored
5467 * to type_idx when non-NULL pointer is given.
5468 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005469int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5470 int index, int *type_idx)
5471{
5472 int i, label_idx = 0;
5473 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5474 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5475 return -EINVAL;
5476 }
5477 for (i = 0; i < imux->num_items; i++) {
5478 if (!strncmp(label, imux->items[i].label, strlen(label)))
5479 label_idx++;
5480 }
5481 if (type_idx)
5482 *type_idx = label_idx;
5483 if (label_idx > 0)
5484 snprintf(imux->items[imux->num_items].label,
5485 sizeof(imux->items[imux->num_items].label),
5486 "%s %d", label, label_idx);
5487 else
5488 strlcpy(imux->items[imux->num_items].label, label,
5489 sizeof(imux->items[imux->num_items].label));
5490 imux->items[imux->num_items].index = index;
5491 imux->num_items++;
5492 return 0;
5493}
5494EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005495
Takashi Iwai4a471b72005-12-07 13:56:29 +01005496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497#ifdef CONFIG_PM
5498/*
5499 * power management
5500 */
5501
5502/**
5503 * snd_hda_suspend - suspend the codecs
5504 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 *
5506 * Returns 0 if successful.
5507 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005508int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005510 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Takashi Iwai0ba21762007-04-16 11:29:14 +02005512 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005513 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005514 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005515 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
5517 return 0;
5518}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005519EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521/**
5522 * snd_hda_resume - resume the codecs
5523 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 *
5525 * Returns 0 if successful.
5526 */
5527int snd_hda_resume(struct hda_bus *bus)
5528{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005529 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Takashi Iwai0ba21762007-04-16 11:29:14 +02005531 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005532 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 return 0;
5535}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005536EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005537#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005538
5539/*
5540 * generic arrays
5541 */
5542
Takashi Iwaid5191e52009-11-16 14:58:17 +01005543/**
5544 * snd_array_new - get a new element from the given array
5545 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005546 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005547 * Get a new element from the given array. If it exceeds the
5548 * pre-allocated array size, re-allocate the array.
5549 *
5550 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005551 */
5552void *snd_array_new(struct snd_array *array)
5553{
Takashi Iwai12f17712012-10-10 08:59:14 +02005554 if (snd_BUG_ON(!array->elem_size))
5555 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005556 if (array->used >= array->alloced) {
5557 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005558 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005559 void *nlist;
5560 if (snd_BUG_ON(num >= 4096))
5561 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005562 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005563 if (!nlist)
5564 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005565 array->list = nlist;
5566 array->alloced = num;
5567 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005568 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005569}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005570EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005571
Takashi Iwaid5191e52009-11-16 14:58:17 +01005572/**
5573 * snd_array_free - free the given array elements
5574 * @array: the array object
5575 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005576void snd_array_free(struct snd_array *array)
5577{
5578 kfree(array->list);
5579 array->used = 0;
5580 array->alloced = 0;
5581 array->list = NULL;
5582}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005583EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005584
Takashi Iwaid5191e52009-11-16 14:58:17 +01005585/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005586 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5587 * @pcm: PCM caps bits
5588 * @buf: the string buffer to write
5589 * @buflen: the max buffer length
5590 *
5591 * used by hda_proc.c and hda_eld.c
5592 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005593void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5594{
5595 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5596 int i, j;
5597
5598 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5599 if (pcm & (AC_SUPPCM_BITS_8 << i))
5600 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5601
5602 buf[j] = '\0'; /* necessary when j == 0 */
5603}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005604EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005605
5606MODULE_DESCRIPTION("HDA codec core");
5607MODULE_LICENSE("GPL");