blob: 3c925145c119570842e71189f0fd03d10bfd56b6 [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",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
177 };
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{
497 return get_num_conns(codec, nid) & AC_CLIST_LENGTH;
498}
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 Iwaida3cec32008-08-08 17:12:14 +0200520 if (snd_BUG_ON(!conn_list || max_conns <= 0))
521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Takashi Iwai4eea3092013-02-07 18:18:19 +0100523 parm = get_num_conns(codec, nid);
524 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200525 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (parm & AC_CLIST_LONG) {
528 /* long form */
529 shift = 16;
530 num_elems = 2;
531 } else {
532 /* short form */
533 shift = 8;
534 num_elems = 4;
535 }
536 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 mask = (1 << (shift-1)) - 1;
538
Takashi Iwai0ba21762007-04-16 11:29:14 +0200539 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0; /* no connection */
541
542 if (conn_len == 1) {
543 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200544 parm = snd_hda_codec_read(codec, nid, 0,
545 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200546 if (parm == -1 && codec->bus->rirb_error)
547 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 conn_list[0] = parm & mask;
549 return 1;
550 }
551
552 /* multi connection */
553 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
558
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100567 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200568 snd_printk(KERN_WARNING "hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
570 nid, i, parm);
571 return 0;
572 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100573 parm >>= shift;
574 if (range_val) {
575 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
579 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100582 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwai4eea3092013-02-07 18:18:19 +0100583 if (conns >= max_conns)
584 return -ENOSPC;
Takashi Iwai54d17402005-11-21 16:33:22 +0100585 conn_list[conns++] = n;
586 }
587 } else {
Takashi Iwai4eea3092013-02-07 18:18:19 +0100588 if (conns >= max_conns)
589 return -ENOSPC;
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100592 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 return conns;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200598 * snd_hda_override_conn_list - add/modify the connection-list to cache
599 * @codec: the HDA codec
600 * @nid: NID to parse
601 * @len: number of connection list entries
602 * @list: the list of connection entries
603 *
604 * Add or modify the given connection-list to the cache. If the corresponding
605 * cache already exists, invalidate it and append a new one.
606 *
607 * Returns zero or a negative error code.
608 */
609int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
610 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100612 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200613
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100614 p = lookup_conn_list(codec, nid);
615 if (p) {
616 list_del(&p->list);
617 kfree(p);
618 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100620 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200622EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
623
624/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200625 * snd_hda_get_conn_index - get the connection index of the given NID
626 * @codec: the HDA codec
627 * @mux: NID containing the list
628 * @nid: NID to select
629 * @recursive: 1 when searching NID recursively, otherwise 0
630 *
631 * Parses the connection list of the widget @mux and checks whether the
632 * widget @nid is present. If it is, return the connection index.
633 * Otherwise it returns -1.
634 */
635int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
636 hda_nid_t nid, int recursive)
637{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100638 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 int i, nums;
640
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200642 for (i = 0; i < nums; i++)
643 if (conn[i] == nid)
644 return i;
645 if (!recursive)
646 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100647 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
649 return -1;
650 }
651 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200652 for (i = 0; i < nums; i++) {
653 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
654 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
655 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200656 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
657 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200658 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200659 return -1;
660}
661EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/**
664 * snd_hda_queue_unsol_event - add an unsolicited event to queue
665 * @bus: the BUS
666 * @res: unsolicited event (lower 32bit of RIRB entry)
667 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
668 *
669 * Adds the given event to the queue. The events are processed in
670 * the workqueue asynchronously. Call this function in the interrupt
671 * hanlder when RIRB receives an unsolicited event.
672 *
673 * Returns 0 if successful, or a negative error code.
674 */
675int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
676{
677 struct hda_bus_unsolicited *unsol;
678 unsigned int wp;
679
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200680 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200681 unsol = bus->unsol;
682 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684
685 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
686 unsol->wp = wp;
687
688 wp <<= 1;
689 unsol->queue[wp] = res;
690 unsol->queue[wp + 1] = res_ex;
691
Takashi Iwai6acaed32009-01-12 10:09:24 +0100692 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return 0;
695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100696EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800699 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
David Howellsc4028952006-11-22 14:57:56 +0000701static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
David Howellsc4028952006-11-22 14:57:56 +0000703 struct hda_bus_unsolicited *unsol =
704 container_of(work, struct hda_bus_unsolicited, work);
705 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct hda_codec *codec;
707 unsigned int rp, caddr, res;
708
709 while (unsol->rp != unsol->wp) {
710 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
711 unsol->rp = rp;
712 rp <<= 1;
713 res = unsol->queue[rp];
714 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200715 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 continue;
717 codec = bus->caddr_tbl[caddr & 0x0f];
718 if (codec && codec->patch_ops.unsol_event)
719 codec->patch_ops.unsol_event(codec, res);
720 }
721}
722
723/*
724 * initialize unsolicited queue
725 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200726static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct hda_bus_unsolicited *unsol;
729
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100730 if (bus->unsol) /* already initialized */
731 return 0;
732
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200733 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200734 if (!unsol) {
735 snd_printk(KERN_ERR "hda_codec: "
736 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ENOMEM;
738 }
David Howellsc4028952006-11-22 14:57:56 +0000739 INIT_WORK(&unsol->work, process_unsol_events);
740 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 bus->unsol = unsol;
742 return 0;
743}
744
745/*
746 * destructor
747 */
748static void snd_hda_codec_free(struct hda_codec *codec);
749
750static int snd_hda_bus_free(struct hda_bus *bus)
751{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200752 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Takashi Iwai0ba21762007-04-16 11:29:14 +0200754 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100756 if (bus->workq)
757 flush_workqueue(bus->workq);
758 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 snd_hda_codec_free(codec);
762 }
763 if (bus->ops.private_free)
764 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100765 if (bus->workq)
766 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kfree(bus);
768 return 0;
769}
770
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100771static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d3532008-11-21 09:08:06 +0100774 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return snd_hda_bus_free(bus);
776}
777
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200778#ifdef CONFIG_SND_HDA_HWDEP
779static int snd_hda_bus_dev_register(struct snd_device *device)
780{
781 struct hda_bus *bus = device->device_data;
782 struct hda_codec *codec;
783 list_for_each_entry(codec, &bus->codec_list, list) {
784 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100785 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200786 }
787 return 0;
788}
789#else
790#define snd_hda_bus_dev_register NULL
791#endif
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/**
794 * snd_hda_bus_new - create a HDA bus
795 * @card: the card entry
796 * @temp: the template for hda_bus information
797 * @busp: the pointer to store the created bus instance
798 *
799 * Returns 0 if successful, or a negative error code.
800 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100801int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200802 const struct hda_bus_template *temp,
803 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct hda_bus *bus;
806 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100807 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200808 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .dev_free = snd_hda_bus_dev_free,
810 };
811
Takashi Iwaida3cec32008-08-08 17:12:14 +0200812 if (snd_BUG_ON(!temp))
813 return -EINVAL;
814 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (busp)
818 *busp = NULL;
819
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200820 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (bus == NULL) {
822 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
823 return -ENOMEM;
824 }
825
826 bus->card = card;
827 bus->private_data = temp->private_data;
828 bus->pci = temp->pci;
829 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100830 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 bus->ops = temp->ops;
832
Ingo Molnar62932df2006-01-16 16:34:20 +0100833 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200834 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 INIT_LIST_HEAD(&bus->codec_list);
836
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100837 snprintf(bus->workq_name, sizeof(bus->workq_name),
838 "hd-audio%d", card->number);
839 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100840 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100841 snd_printk(KERN_ERR "cannot create workqueue %s\n",
842 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100843 kfree(bus);
844 return -ENOMEM;
845 }
846
Takashi Iwai0ba21762007-04-16 11:29:14 +0200847 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
848 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 snd_hda_bus_free(bus);
850 return err;
851 }
852 if (busp)
853 *busp = bus;
854 return 0;
855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100856EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Takashi Iwai82467612007-07-27 19:15:54 +0200858#ifdef CONFIG_SND_HDA_GENERIC
859#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200860 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200861#else
862#define is_generic_config(codec) 0
863#endif
864
Takashi Iwai645f10c2008-11-28 15:07:37 +0100865#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100866#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
867#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100868#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100869#endif
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * find a matching codec preset
873 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200874static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200875find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100877 struct hda_codec_preset_list *tbl;
878 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200879 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Takashi Iwai82467612007-07-27 19:15:54 +0200881 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100882 return NULL; /* use the generic parser */
883
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100884 again:
885 mutex_lock(&preset_mutex);
886 list_for_each_entry(tbl, &hda_preset_tables, list) {
887 if (!try_module_get(tbl->owner)) {
888 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
889 continue;
890 }
891 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100893 if (preset->afg && preset->afg != codec->afg)
894 continue;
895 if (preset->mfg && preset->mfg != codec->mfg)
896 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200897 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200899 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200900 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100901 preset->rev == codec->revision_id)) {
902 mutex_unlock(&preset_mutex);
903 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100907 module_put(tbl->owner);
908 }
909 mutex_unlock(&preset_mutex);
910
911 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
912 char name[32];
913 if (!mod_requested)
914 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
915 codec->vendor_id);
916 else
917 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
918 (codec->vendor_id >> 16) & 0xffff);
919 request_module(name);
920 mod_requested++;
921 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 return NULL;
924}
925
926/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200927 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200929static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 const struct hda_vendor_id *c;
932 const char *vendor = NULL;
933 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200934 char tmp[16];
935
936 if (codec->vendor_name)
937 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 for (c = hda_vendor_ids; c->id; c++) {
940 if (c->id == vendor_id) {
941 vendor = c->name;
942 break;
943 }
944 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200945 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sprintf(tmp, "Generic %04x", vendor_id);
947 vendor = tmp;
948 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200949 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
950 if (!codec->vendor_name)
951 return -ENOMEM;
952
953 get_chip_name:
954 if (codec->chip_name)
955 return 0;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200958 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
959 else {
960 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
961 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
962 }
963 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200964 return -ENOMEM;
965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200969 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100971static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200973 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 hda_nid_t nid;
975
976 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
977 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200978 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200979 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200980 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200981 case AC_GRP_AUDIO_FUNCTION:
982 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200983 codec->afg_function_id = function_id & 0xff;
984 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 break;
986 case AC_GRP_MODEM_FUNCTION:
987 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200988 codec->mfg_function_id = function_id & 0xff;
989 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200990 break;
991 default:
992 break;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100998 * read widget caps for each widget and store in cache
999 */
1000static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1001{
1002 int i;
1003 hda_nid_t nid;
1004
1005 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1006 &codec->start_nid);
1007 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001008 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001009 return -ENOMEM;
1010 nid = codec->start_nid;
1011 for (i = 0; i < codec->num_nodes; i++, nid++)
1012 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1013 AC_PAR_AUDIO_WIDGET_CAP);
1014 return 0;
1015}
1016
Takashi Iwai3be14142009-02-20 14:11:16 +01001017/* read all pin default configurations and save codec->init_pins */
1018static int read_pin_defaults(struct hda_codec *codec)
1019{
1020 int i;
1021 hda_nid_t nid = codec->start_nid;
1022
1023 for (i = 0; i < codec->num_nodes; i++, nid++) {
1024 struct hda_pincfg *pin;
1025 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001026 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001027 if (wid_type != AC_WID_PIN)
1028 continue;
1029 pin = snd_array_new(&codec->init_pins);
1030 if (!pin)
1031 return -ENOMEM;
1032 pin->nid = nid;
1033 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1034 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001035 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1036 AC_VERB_GET_PIN_WIDGET_CONTROL,
1037 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001038 }
1039 return 0;
1040}
1041
1042/* look up the given pin config list and return the item matching with NID */
1043static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1044 struct snd_array *array,
1045 hda_nid_t nid)
1046{
1047 int i;
1048 for (i = 0; i < array->used; i++) {
1049 struct hda_pincfg *pin = snd_array_elem(array, i);
1050 if (pin->nid == nid)
1051 return pin;
1052 }
1053 return NULL;
1054}
1055
Takashi Iwai3be14142009-02-20 14:11:16 +01001056/* set the current pin config value for the given NID.
1057 * the value is cached, and read via snd_hda_codec_get_pincfg()
1058 */
1059int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1060 hda_nid_t nid, unsigned int cfg)
1061{
1062 struct hda_pincfg *pin;
1063
Takashi Iwaib82855a2009-12-27 11:24:56 +01001064 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1065 return -EINVAL;
1066
Takashi Iwai3be14142009-02-20 14:11:16 +01001067 pin = look_up_pincfg(codec, list, nid);
1068 if (!pin) {
1069 pin = snd_array_new(list);
1070 if (!pin)
1071 return -ENOMEM;
1072 pin->nid = nid;
1073 }
1074 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 return 0;
1076}
1077
Takashi Iwaid5191e52009-11-16 14:58:17 +01001078/**
1079 * snd_hda_codec_set_pincfg - Override a pin default configuration
1080 * @codec: the HDA codec
1081 * @nid: NID to set the pin config
1082 * @cfg: the pin default config value
1083 *
1084 * Override a pin default configuration value in the cache.
1085 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1086 * priority than the real hardware value.
1087 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001088int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1089 hda_nid_t nid, unsigned int cfg)
1090{
Takashi Iwai346ff702009-02-23 09:42:57 +01001091 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001092}
1093EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1094
Takashi Iwaid5191e52009-11-16 14:58:17 +01001095/**
1096 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1097 * @codec: the HDA codec
1098 * @nid: NID to get the pin config
1099 *
1100 * Get the current pin config value of the given pin NID.
1101 * If the pincfg value is cached or overridden via sysfs or driver,
1102 * returns the cached value.
1103 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001104unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1105{
1106 struct hda_pincfg *pin;
1107
Takashi Iwai3be14142009-02-20 14:11:16 +01001108#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001109 {
1110 unsigned int cfg = 0;
1111 mutex_lock(&codec->user_mutex);
1112 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1113 if (pin)
1114 cfg = pin->cfg;
1115 mutex_unlock(&codec->user_mutex);
1116 if (cfg)
1117 return cfg;
1118 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001119#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001120 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1121 if (pin)
1122 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001123 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1124 if (pin)
1125 return pin->cfg;
1126 return 0;
1127}
1128EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1129
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001130/* remember the current pinctl target value */
1131int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1132 unsigned int val)
1133{
1134 struct hda_pincfg *pin;
1135
1136 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1137 if (!pin)
1138 return -EINVAL;
1139 pin->target = val;
1140 return 0;
1141}
1142EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1143
1144/* return the current pinctl target value */
1145int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1146{
1147 struct hda_pincfg *pin;
1148
1149 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1150 if (!pin)
1151 return 0;
1152 return pin->target;
1153}
1154EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1155
Takashi Iwai92ee6162009-12-27 11:18:59 +01001156/**
1157 * snd_hda_shutup_pins - Shut up all pins
1158 * @codec: the HDA codec
1159 *
1160 * Clear all pin controls to shup up before suspend for avoiding click noise.
1161 * The controls aren't cached so that they can be resumed properly.
1162 */
1163void snd_hda_shutup_pins(struct hda_codec *codec)
1164{
1165 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001166 /* don't shut up pins when unloading the driver; otherwise it breaks
1167 * the default pin setup at the next load of the driver
1168 */
1169 if (codec->bus->shutdown)
1170 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001171 for (i = 0; i < codec->init_pins.used; i++) {
1172 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1173 /* use read here for syncing after issuing each verb */
1174 snd_hda_codec_read(codec, pin->nid, 0,
1175 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1176 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001177 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001178}
1179EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1180
Takashi Iwai2a439522011-07-26 09:52:50 +02001181#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001182/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1183static void restore_shutup_pins(struct hda_codec *codec)
1184{
1185 int i;
1186 if (!codec->pins_shutup)
1187 return;
1188 if (codec->bus->shutdown)
1189 return;
1190 for (i = 0; i < codec->init_pins.used; i++) {
1191 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1192 snd_hda_codec_write(codec, pin->nid, 0,
1193 AC_VERB_SET_PIN_WIDGET_CONTROL,
1194 pin->ctrl);
1195 }
1196 codec->pins_shutup = 0;
1197}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001198#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001199
David Henningsson26a6cb62012-10-09 15:04:21 +02001200static void hda_jackpoll_work(struct work_struct *work)
1201{
1202 struct hda_codec *codec =
1203 container_of(work, struct hda_codec, jackpoll_work.work);
1204 if (!codec->jackpoll_interval)
1205 return;
1206
1207 snd_hda_jack_set_dirty_all(codec);
1208 snd_hda_jack_poll_all(codec);
1209 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1210 codec->jackpoll_interval);
1211}
1212
Takashi Iwai01751f52007-08-10 16:59:39 +02001213static void init_hda_cache(struct hda_cache_rec *cache,
1214 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001215static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001216
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001217/* release all pincfg lists */
1218static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001219{
Takashi Iwai346ff702009-02-23 09:42:57 +01001220 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001221#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001222 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001223#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001224 snd_array_free(&codec->init_pins);
1225}
1226
Takashi Iwai54d17402005-11-21 16:33:22 +01001227/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001228 * audio-converter setup caches
1229 */
1230struct hda_cvt_setup {
1231 hda_nid_t nid;
1232 u8 stream_tag;
1233 u8 channel_id;
1234 u16 format_id;
1235 unsigned char active; /* cvt is currently used */
1236 unsigned char dirty; /* setups should be cleared */
1237};
1238
1239/* get or create a cache entry for the given audio converter NID */
1240static struct hda_cvt_setup *
1241get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1242{
1243 struct hda_cvt_setup *p;
1244 int i;
1245
1246 for (i = 0; i < codec->cvt_setups.used; i++) {
1247 p = snd_array_elem(&codec->cvt_setups, i);
1248 if (p->nid == nid)
1249 return p;
1250 }
1251 p = snd_array_new(&codec->cvt_setups);
1252 if (p)
1253 p->nid = nid;
1254 return p;
1255}
1256
1257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * codec destructor
1259 */
1260static void snd_hda_codec_free(struct hda_codec *codec)
1261{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001264 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001265 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001266 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001267#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001269 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001272 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001273 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001274 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001275 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001276 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 codec->bus->caddr_tbl[codec->addr] = NULL;
1278 if (codec->patch_ops.free)
1279 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001280#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001281 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001282 hda_call_pm_notify(codec->bus, false);
1283#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001284 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001285 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001286 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001287 kfree(codec->vendor_name);
1288 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001289 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001290 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 kfree(codec);
1292}
1293
Mengdong Linb8dfc462012-08-23 17:32:30 +08001294static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1295 hda_nid_t fg, unsigned int power_state);
1296
Takashi Iwaid8193872012-08-31 07:54:38 -07001297static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001298 unsigned int power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001299static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
1300 unsigned int power_state);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/**
1303 * snd_hda_codec_new - create a HDA codec
1304 * @bus: the bus to assign
1305 * @codec_addr: the codec address
1306 * @codecp: the pointer to store the generated codec
1307 *
1308 * Returns 0 if successful, or a negative error code.
1309 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001310int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001311 unsigned int codec_addr,
1312 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001315 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001316 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int err;
1318
Takashi Iwaida3cec32008-08-08 17:12:14 +02001319 if (snd_BUG_ON(!bus))
1320 return -EINVAL;
1321 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001325 snd_printk(KERN_ERR "hda_codec: "
1326 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EBUSY;
1328 }
1329
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001330 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (codec == NULL) {
1332 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1333 return -ENOMEM;
1334 }
1335
1336 codec->bus = bus;
1337 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001338 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001339 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001340 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001341 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001342 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001343 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1344 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001345 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001346 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001347 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001348 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001349 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001350 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001351 INIT_LIST_HEAD(&codec->conn_list);
1352
David Henningsson26a6cb62012-10-09 15:04:21 +02001353 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Takashi Iwai83012a72012-08-24 18:38:08 +02001355#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001356 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001357 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1358 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1359 * the caller has to power down appropriatley after initialization
1360 * phase.
1361 */
1362 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001363 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001364#endif
1365
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001366 if (codec->bus->modelname) {
1367 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1368 if (!codec->modelname) {
1369 snd_hda_codec_free(codec);
1370 return -ENODEV;
1371 }
1372 }
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 list_add_tail(&codec->list, &bus->codec_list);
1375 bus->caddr_tbl[codec_addr] = codec;
1376
Takashi Iwai0ba21762007-04-16 11:29:14 +02001377 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1378 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001379 if (codec->vendor_id == -1)
1380 /* read again, hopefully the access method was corrected
1381 * in the last read...
1382 */
1383 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1384 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1386 AC_PAR_SUBSYSTEM_ID);
1387 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1388 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001390 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001391 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001392 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001393 err = -ENODEV;
1394 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Takashi Iwaid8193872012-08-31 07:54:38 -07001397 fg = codec->afg ? codec->afg : codec->mfg;
1398 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001399 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001400 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001401 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001402 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001403 err = read_pin_defaults(codec);
1404 if (err < 0)
1405 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001406
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001408 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001409 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001411 }
1412
Takashi Iwai83012a72012-08-24 18:38:08 +02001413#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001414 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc462012-08-23 17:32:30 +08001415 AC_PWRST_CLKSTOP);
1416 if (!codec->d3_stop_clk)
1417 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001418#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001419 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001420 AC_PWRST_EPSS);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001421 codec->power_filter = default_power_filter;
Mengdong Linb8dfc462012-08-23 17:32:30 +08001422
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001423 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001424 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001425
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001426 snd_hda_codec_proc_new(codec);
1427
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001428 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001429
1430 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1431 codec->subsystem_id, codec->revision_id);
1432 snd_component_add(codec->bus->card, component);
1433
1434 if (codecp)
1435 *codecp = codec;
1436 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001437
1438 error:
1439 snd_hda_codec_free(codec);
1440 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001441}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001442EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001443
Takashi Iwaid5191e52009-11-16 14:58:17 +01001444/**
1445 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1446 * @codec: the HDA codec
1447 *
1448 * Start parsing of the given codec tree and (re-)initialize the whole
1449 * patch instance.
1450 *
1451 * Returns 0 if successful or a negative error code.
1452 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001453int snd_hda_codec_configure(struct hda_codec *codec)
1454{
1455 int err;
1456
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001457 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001458 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001459 err = get_codec_name(codec);
1460 if (err < 0)
1461 return err;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Takashi Iwai82467612007-07-27 19:15:54 +02001464 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001466 goto patched;
1467 }
Takashi Iwai82467612007-07-27 19:15:54 +02001468 if (codec->preset && codec->preset->patch) {
1469 err = codec->preset->patch(codec);
1470 goto patched;
1471 }
1472
1473 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001474 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001475 if (err < 0)
1476 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001477
1478 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001479 if (!err && codec->patch_ops.unsol_event)
1480 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001481 /* audio codec should override the mixer name */
1482 if (!err && (codec->afg || !*codec->bus->card->mixername))
1483 snprintf(codec->bus->card->mixername,
1484 sizeof(codec->bus->card->mixername),
1485 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001488EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Takashi Iwaied360812012-08-08 17:12:52 +02001490/* update the stream-id if changed */
1491static void update_pcm_stream_id(struct hda_codec *codec,
1492 struct hda_cvt_setup *p, hda_nid_t nid,
1493 u32 stream_tag, int channel_id)
1494{
1495 unsigned int oldval, newval;
1496
1497 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1498 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1499 newval = (stream_tag << 4) | channel_id;
1500 if (oldval != newval)
1501 snd_hda_codec_write(codec, nid, 0,
1502 AC_VERB_SET_CHANNEL_STREAMID,
1503 newval);
1504 p->stream_tag = stream_tag;
1505 p->channel_id = channel_id;
1506 }
1507}
1508
1509/* update the format-id if changed */
1510static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1511 hda_nid_t nid, int format)
1512{
1513 unsigned int oldval;
1514
1515 if (p->format_id != format) {
1516 oldval = snd_hda_codec_read(codec, nid, 0,
1517 AC_VERB_GET_STREAM_FORMAT, 0);
1518 if (oldval != format) {
1519 msleep(1);
1520 snd_hda_codec_write(codec, nid, 0,
1521 AC_VERB_SET_STREAM_FORMAT,
1522 format);
1523 }
1524 p->format_id = format;
1525 }
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/**
1529 * snd_hda_codec_setup_stream - set up the codec for streaming
1530 * @codec: the CODEC to set up
1531 * @nid: the NID to set up
1532 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1533 * @channel_id: channel id to pass, zero based.
1534 * @format: stream format.
1535 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1537 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int channel_id, int format)
1539{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001540 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001541 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001542 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001543 int i;
1544
Takashi Iwai0ba21762007-04-16 11:29:14 +02001545 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001546 return;
1547
Takashi Iwai0ba21762007-04-16 11:29:14 +02001548 snd_printdd("hda_codec_setup_stream: "
1549 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001551 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001552 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001553 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001554
1555 if (codec->pcm_format_first)
1556 update_pcm_format(codec, p, nid, format);
1557 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1558 if (!codec->pcm_format_first)
1559 update_pcm_format(codec, p, nid, format);
1560
Takashi Iwaieb541332010-08-06 13:48:11 +02001561 p->active = 1;
1562 p->dirty = 0;
1563
1564 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001565 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001566 list_for_each_entry(c, &codec->bus->codec_list, list) {
1567 for (i = 0; i < c->cvt_setups.used; i++) {
1568 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001569 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001570 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001571 p->dirty = 1;
1572 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001575EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Takashi Iwaif0cea792010-08-13 11:56:53 +02001577static void really_cleanup_stream(struct hda_codec *codec,
1578 struct hda_cvt_setup *q);
1579
Takashi Iwaid5191e52009-11-16 14:58:17 +01001580/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001581 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001582 * @codec: the CODEC to clean up
1583 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001584 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001585 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001586void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1587 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001588{
Takashi Iwaieb541332010-08-06 13:48:11 +02001589 struct hda_cvt_setup *p;
1590
Takashi Iwai888afa12008-03-18 09:57:50 +01001591 if (!nid)
1592 return;
1593
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001594 if (codec->no_sticky_stream)
1595 do_now = 1;
1596
Takashi Iwai888afa12008-03-18 09:57:50 +01001597 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001598 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001599 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001600 /* here we just clear the active flag when do_now isn't set;
1601 * actual clean-ups will be done later in
1602 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1603 */
1604 if (do_now)
1605 really_cleanup_stream(codec, p);
1606 else
1607 p->active = 0;
1608 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001609}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001610EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001611
Takashi Iwaieb541332010-08-06 13:48:11 +02001612static void really_cleanup_stream(struct hda_codec *codec,
1613 struct hda_cvt_setup *q)
1614{
1615 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001616 if (q->stream_tag || q->channel_id)
1617 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1618 if (q->format_id)
1619 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1620);
Takashi Iwaieb541332010-08-06 13:48:11 +02001621 memset(q, 0, sizeof(*q));
1622 q->nid = nid;
1623}
1624
1625/* clean up the all conflicting obsolete streams */
1626static void purify_inactive_streams(struct hda_codec *codec)
1627{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001628 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001629 int i;
1630
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001631 list_for_each_entry(c, &codec->bus->codec_list, list) {
1632 for (i = 0; i < c->cvt_setups.used; i++) {
1633 struct hda_cvt_setup *p;
1634 p = snd_array_elem(&c->cvt_setups, i);
1635 if (p->dirty)
1636 really_cleanup_stream(c, p);
1637 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001638 }
1639}
1640
Takashi Iwai2a439522011-07-26 09:52:50 +02001641#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001642/* clean up all streams; called from suspend */
1643static void hda_cleanup_all_streams(struct hda_codec *codec)
1644{
1645 int i;
1646
1647 for (i = 0; i < codec->cvt_setups.used; i++) {
1648 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1649 if (p->stream_tag)
1650 really_cleanup_stream(codec, p);
1651 }
1652}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001653#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655/*
1656 * amp access functions
1657 */
1658
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001659/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001660#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001661#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001662#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1663#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001665#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001668static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001669 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai01751f52007-08-10 16:59:39 +02001671 memset(cache, 0, sizeof(*cache));
1672 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001673 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001674}
1675
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001676static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001677{
Takashi Iwai603c4012008-07-30 15:01:44 +02001678 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001682static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwai01751f52007-08-10 16:59:39 +02001684 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1685 u16 cur = cache->hash[idx];
1686 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001689 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (info->key == key)
1691 return info;
1692 cur = info->next;
1693 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001694 return NULL;
1695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001697/* query the hash. allocate an entry if not found. */
1698static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1699 u32 key)
1700{
1701 struct hda_cache_head *info = get_hash(cache, key);
1702 if (!info) {
1703 u16 idx, cur;
1704 /* add a new hash entry */
1705 info = snd_array_new(&cache->buf);
1706 if (!info)
1707 return NULL;
1708 cur = snd_array_index(&cache->buf, info);
1709 info->key = key;
1710 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001711 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001712 idx = key % (u16)ARRAY_SIZE(cache->hash);
1713 info->next = cache->hash[idx];
1714 cache->hash[idx] = cur;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return info;
1717}
1718
Takashi Iwai01751f52007-08-10 16:59:39 +02001719/* query and allocate an amp hash entry */
1720static inline struct hda_amp_info *
1721get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1722{
1723 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1724}
1725
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001726/* overwrite the value with the key in the caps hash */
1727static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1728{
1729 struct hda_amp_info *info;
1730
1731 mutex_lock(&codec->hash_mutex);
1732 info = get_alloc_amp_hash(codec, key);
1733 if (!info) {
1734 mutex_unlock(&codec->hash_mutex);
1735 return -EINVAL;
1736 }
1737 info->amp_caps = val;
1738 info->head.val |= INFO_AMP_CAPS;
1739 mutex_unlock(&codec->hash_mutex);
1740 return 0;
1741}
1742
1743/* query the value from the caps hash; if not found, fetch the current
1744 * value from the given function and store in the hash
1745 */
1746static unsigned int
1747query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1748 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1749{
1750 struct hda_amp_info *info;
1751 unsigned int val;
1752
1753 mutex_lock(&codec->hash_mutex);
1754 info = get_alloc_amp_hash(codec, key);
1755 if (!info) {
1756 mutex_unlock(&codec->hash_mutex);
1757 return 0;
1758 }
1759 if (!(info->head.val & INFO_AMP_CAPS)) {
1760 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1761 val = func(codec, nid, dir);
1762 write_caps_hash(codec, key, val);
1763 } else {
1764 val = info->amp_caps;
1765 mutex_unlock(&codec->hash_mutex);
1766 }
1767 return val;
1768}
1769
1770static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1771 int direction)
1772{
1773 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1774 nid = codec->afg;
1775 return snd_hda_param_read(codec, nid,
1776 direction == HDA_OUTPUT ?
1777 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1778}
1779
Takashi Iwaid5191e52009-11-16 14:58:17 +01001780/**
1781 * query_amp_caps - query AMP capabilities
1782 * @codec: the HD-auio codec
1783 * @nid: the NID to query
1784 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1785 *
1786 * Query AMP capabilities for the given widget and direction.
1787 * Returns the obtained capability bits.
1788 *
1789 * When cap bits have been already read, this doesn't read again but
1790 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001792u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001794 return query_caps_hash(codec, nid, direction,
1795 HDA_HASH_KEY(nid, direction, 0),
1796 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001798EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Takashi Iwaid5191e52009-11-16 14:58:17 +01001800/**
1801 * snd_hda_override_amp_caps - Override the AMP capabilities
1802 * @codec: the CODEC to clean up
1803 * @nid: the NID to clean up
1804 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1805 * @caps: the capability bits to set
1806 *
1807 * Override the cached AMP caps bits value by the given one.
1808 * This function is useful if the driver needs to adjust the AMP ranges,
1809 * e.g. limit to 0dB, etc.
1810 *
1811 * Returns zero if successful or a negative error code.
1812 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001813int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1814 unsigned int caps)
1815{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001816 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001818EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001819
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001820static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1821 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001822{
1823 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1824}
1825
Takashi Iwaid5191e52009-11-16 14:58:17 +01001826/**
1827 * snd_hda_query_pin_caps - Query PIN capabilities
1828 * @codec: the HD-auio codec
1829 * @nid: the NID to query
1830 *
1831 * Query PIN capabilities for the given widget.
1832 * Returns the obtained capability bits.
1833 *
1834 * When cap bits have been already read, this doesn't read again but
1835 * returns the cached value.
1836 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001837u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1838{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001840 read_pin_cap);
1841}
Takashi Iwai1327a322009-03-23 13:07:47 +01001842EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1843
Wu Fengguang864f92b2009-11-18 12:38:02 +08001844/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001845 * snd_hda_override_pin_caps - Override the pin capabilities
1846 * @codec: the CODEC
1847 * @nid: the NID to override
1848 * @caps: the capability bits to set
1849 *
1850 * Override the cached PIN capabilitiy bits value by the given one.
1851 *
1852 * Returns zero if successful or a negative error code.
1853 */
1854int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1855 unsigned int caps)
1856{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001857 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001858}
1859EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1860
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001861/* read or sync the hash value with the current value;
1862 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001864static struct hda_amp_info *
1865update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001866 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001868 struct hda_amp_info *info;
1869 unsigned int parm, val = 0;
1870 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001872 retry:
1873 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1874 if (!info)
1875 return NULL;
1876 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1877 if (!val_read) {
1878 mutex_unlock(&codec->hash_mutex);
1879 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1880 parm |= direction == HDA_OUTPUT ?
1881 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1882 parm |= index;
1883 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001884 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001885 val &= 0xff;
1886 val_read = true;
1887 mutex_lock(&codec->hash_mutex);
1888 goto retry;
1889 }
1890 info->vol[ch] = val;
1891 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001892 } else if (init_only)
1893 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001894 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
1897/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001898 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001900static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001901 hda_nid_t nid, int ch, int direction, int index,
1902 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 u32 parm;
1905
1906 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1907 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1908 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001909 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1910 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001911 ; /* set the zero value as a fake mute */
1912 else
1913 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1915}
1916
Takashi Iwaid5191e52009-11-16 14:58:17 +01001917/**
1918 * snd_hda_codec_amp_read - Read AMP value
1919 * @codec: HD-audio codec
1920 * @nid: NID to read the AMP value
1921 * @ch: channel (left=0 or right=1)
1922 * @direction: #HDA_INPUT or #HDA_OUTPUT
1923 * @index: the index value (only for input direction)
1924 *
1925 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Takashi Iwai834be882006-03-01 14:16:17 +01001927int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1928 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001930 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001931 unsigned int val = 0;
1932
1933 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001934 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001935 if (info)
1936 val = info->vol[ch];
1937 mutex_unlock(&codec->hash_mutex);
1938 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001940EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Takashi Iwai280e57d2012-12-14 10:32:21 +01001942static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1943 int direction, int idx, int mask, int val,
1944 bool init_only)
1945{
1946 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001947 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001948 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001949
1950 if (snd_BUG_ON(mask & ~0xff))
1951 mask &= 0xff;
1952 val &= mask;
1953
1954 mutex_lock(&codec->hash_mutex);
1955 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1956 if (!info) {
1957 mutex_unlock(&codec->hash_mutex);
1958 return 0;
1959 }
1960 val |= info->vol[ch] & ~mask;
1961 if (info->vol[ch] == val) {
1962 mutex_unlock(&codec->hash_mutex);
1963 return 0;
1964 }
1965 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001966 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001967 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001968 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001969 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001970 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001971 return 1;
1972}
1973
Takashi Iwaid5191e52009-11-16 14:58:17 +01001974/**
1975 * snd_hda_codec_amp_update - update the AMP value
1976 * @codec: HD-audio codec
1977 * @nid: NID to read the AMP value
1978 * @ch: channel (left=0 or right=1)
1979 * @direction: #HDA_INPUT or #HDA_OUTPUT
1980 * @idx: the index value (only for input direction)
1981 * @mask: bit mask to set
1982 * @val: the bits value to set
1983 *
1984 * Update the AMP value with a bit mask.
1985 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001986 */
Takashi Iwai834be882006-03-01 14:16:17 +01001987int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1988 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001990 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001992EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Takashi Iwaid5191e52009-11-16 14:58:17 +01001994/**
1995 * snd_hda_codec_amp_stereo - update the AMP stereo values
1996 * @codec: HD-audio codec
1997 * @nid: NID to read the AMP value
1998 * @direction: #HDA_INPUT or #HDA_OUTPUT
1999 * @idx: the index value (only for input direction)
2000 * @mask: bit mask to set
2001 * @val: the bits value to set
2002 *
2003 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2004 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002005 */
2006int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2007 int direction, int idx, int mask, int val)
2008{
2009 int ch, ret = 0;
Takashi Iwai46712642010-03-29 09:19:38 +02002010
2011 if (snd_BUG_ON(mask & ~0xff))
2012 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002013 for (ch = 0; ch < 2; ch++)
2014 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2015 idx, mask, val);
2016 return ret;
2017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002018EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002019
Takashi Iwai280e57d2012-12-14 10:32:21 +01002020/* Works like snd_hda_codec_amp_update() but it writes the value only at
2021 * the first access. If the amp was already initialized / updated beforehand,
2022 * this does nothing.
2023 */
2024int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2025 int dir, int idx, int mask, int val)
2026{
2027 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2028}
2029EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2030
2031int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2032 int dir, int idx, int mask, int val)
2033{
2034 int ch, ret = 0;
2035
2036 if (snd_BUG_ON(mask & ~0xff))
2037 mask &= 0xff;
2038 for (ch = 0; ch < 2; ch++)
2039 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2040 idx, mask, val);
2041 return ret;
2042}
2043EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2044
Takashi Iwaid5191e52009-11-16 14:58:17 +01002045/**
2046 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2047 * @codec: HD-audio codec
2048 *
2049 * Resume the all amp commands from the cache.
2050 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002051void snd_hda_codec_resume_amp(struct hda_codec *codec)
2052{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002053 int i;
2054
Takashi Iwaic370dd62012-12-13 18:30:04 +01002055 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002056 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002057 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2058 struct hda_amp_info *buffer;
2059 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002060 hda_nid_t nid;
2061 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002062 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002063
2064 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002065 if (!buffer->head.dirty)
2066 continue;
2067 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002068 info = *buffer;
2069 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002070 if (!key)
2071 continue;
2072 nid = key & 0xff;
2073 idx = (key >> 16) & 0xff;
2074 dir = (key >> 24) & 0xff;
2075 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002076 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002077 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002078 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002079 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2080 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002081 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002082 }
2083 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002084 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002085}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002086EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002088static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2089 unsigned int ofs)
2090{
2091 u32 caps = query_amp_caps(codec, nid, dir);
2092 /* get num steps */
2093 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2094 if (ofs < caps)
2095 caps -= ofs;
2096 return caps;
2097}
2098
Takashi Iwaid5191e52009-11-16 14:58:17 +01002099/**
2100 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2101 *
2102 * The control element is supposed to have the private_value field
2103 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2104 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002105int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 u16 nid = get_amp_nid(kcontrol);
2110 u8 chs = get_amp_channels(kcontrol);
2111 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002112 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002114 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2115 uinfo->count = chs == 3 ? 2 : 1;
2116 uinfo->value.integer.min = 0;
2117 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2118 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002119 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002120 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2121 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EINVAL;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002126EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002128
2129static inline unsigned int
2130read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2131 int ch, int dir, int idx, unsigned int ofs)
2132{
2133 unsigned int val;
2134 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2135 val &= HDA_AMP_VOLMASK;
2136 if (val >= ofs)
2137 val -= ofs;
2138 else
2139 val = 0;
2140 return val;
2141}
2142
2143static inline int
2144update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2145 int ch, int dir, int idx, unsigned int ofs,
2146 unsigned int val)
2147{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002148 unsigned int maxval;
2149
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002150 if (val > 0)
2151 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002152 /* ofs = 0: raw max value */
2153 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002154 if (val > maxval)
2155 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002156 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2157 HDA_AMP_VOLMASK, val);
2158}
2159
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160/**
2161 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2162 *
2163 * The control element is supposed to have the private_value field
2164 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2165 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002166int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 hda_nid_t nid = get_amp_nid(kcontrol);
2171 int chs = get_amp_channels(kcontrol);
2172 int dir = get_amp_direction(kcontrol);
2173 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002174 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 long *valp = ucontrol->value.integer.value;
2176
2177 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002178 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002180 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
2182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002183EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Takashi Iwaid5191e52009-11-16 14:58:17 +01002185/**
2186 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2187 *
2188 * The control element is supposed to have the private_value field
2189 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2190 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002191int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 hda_nid_t nid = get_amp_nid(kcontrol);
2196 int chs = get_amp_channels(kcontrol);
2197 int dir = get_amp_direction(kcontrol);
2198 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002199 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 long *valp = ucontrol->value.integer.value;
2201 int change = 0;
2202
Takashi Iwaicb53c622007-08-10 17:21:45 +02002203 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002204 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002205 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002206 valp++;
2207 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002208 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002209 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002210 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return change;
2212}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002213EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Takashi Iwaid5191e52009-11-16 14:58:17 +01002215/**
2216 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2217 *
2218 * The control element is supposed to have the private_value field
2219 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2220 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002221int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2222 unsigned int size, unsigned int __user *_tlv)
2223{
2224 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2225 hda_nid_t nid = get_amp_nid(kcontrol);
2226 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002227 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002228 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002229 u32 caps, val1, val2;
2230
2231 if (size < 4 * sizeof(unsigned int))
2232 return -ENOMEM;
2233 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002234 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2235 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002236 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002237 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002238 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002239 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002240 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002241 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2242 return -EFAULT;
2243 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2244 return -EFAULT;
2245 if (put_user(val1, _tlv + 2))
2246 return -EFAULT;
2247 if (put_user(val2, _tlv + 3))
2248 return -EFAULT;
2249 return 0;
2250}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002251EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002252
Takashi Iwaid5191e52009-11-16 14:58:17 +01002253/**
2254 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2255 * @codec: HD-audio codec
2256 * @nid: NID of a reference widget
2257 * @dir: #HDA_INPUT or #HDA_OUTPUT
2258 * @tlv: TLV data to be stored, at least 4 elements
2259 *
2260 * Set (static) TLV data for a virtual master volume using the AMP caps
2261 * obtained from the reference NID.
2262 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002263 */
2264void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2265 unsigned int *tlv)
2266{
2267 u32 caps;
2268 int nums, step;
2269
2270 caps = query_amp_caps(codec, nid, dir);
2271 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2272 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2273 step = (step + 1) * 25;
2274 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2275 tlv[1] = 2 * sizeof(unsigned int);
2276 tlv[2] = -nums * step;
2277 tlv[3] = step;
2278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002279EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002280
2281/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002282static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002283find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002284{
2285 struct snd_ctl_elem_id id;
2286 memset(&id, 0, sizeof(id));
2287 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002288 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002289 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002290 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2291 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002292 strcpy(id.name, name);
2293 return snd_ctl_find_id(codec->bus->card, &id);
2294}
2295
Takashi Iwaid5191e52009-11-16 14:58:17 +01002296/**
2297 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2298 * @codec: HD-audio codec
2299 * @name: ctl id name string
2300 *
2301 * Get the control element with the given id string and IFACE_MIXER.
2302 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002303struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2304 const char *name)
2305{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002306 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002308EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002309
Takashi Iwaidcda5802012-10-12 17:24:51 +02002310static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2311 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002312{
2313 int idx;
2314 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002315 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002316 return idx;
2317 }
2318 return -EBUSY;
2319}
2320
Takashi Iwaid5191e52009-11-16 14:58:17 +01002321/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002322 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002323 * @codec: HD-audio codec
2324 * @nid: corresponding NID (optional)
2325 * @kctl: the control element to assign
2326 *
2327 * Add the given control element to an array inside the codec instance.
2328 * All control elements belonging to a codec are supposed to be added
2329 * by this function so that a proper clean-up works at the free or
2330 * reconfiguration time.
2331 *
2332 * If non-zero @nid is passed, the NID is assigned to the control element.
2333 * The assignment is shown in the codec proc file.
2334 *
2335 * snd_hda_ctl_add() checks the control subdev id field whether
2336 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002337 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2338 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002339 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002340int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2341 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002342{
2343 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002344 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002345 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002346
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002347 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002348 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002349 if (nid == 0)
2350 nid = get_amp_nid_(kctl->private_value);
2351 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002352 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2353 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002354 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002355 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002356 err = snd_ctl_add(codec->bus->card, kctl);
2357 if (err < 0)
2358 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002359 item = snd_array_new(&codec->mixers);
2360 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002361 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002362 item->kctl = kctl;
2363 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002364 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002365 return 0;
2366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002367EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002368
Takashi Iwaid5191e52009-11-16 14:58:17 +01002369/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002370 * snd_hda_add_nid - Assign a NID to a control element
2371 * @codec: HD-audio codec
2372 * @nid: corresponding NID (optional)
2373 * @kctl: the control element to assign
2374 * @index: index to kctl
2375 *
2376 * Add the given control element to an array inside the codec instance.
2377 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2378 * NID:KCTL mapping - for example "Capture Source" selector.
2379 */
2380int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2381 unsigned int index, hda_nid_t nid)
2382{
2383 struct hda_nid_item *item;
2384
2385 if (nid > 0) {
2386 item = snd_array_new(&codec->nids);
2387 if (!item)
2388 return -ENOMEM;
2389 item->kctl = kctl;
2390 item->index = index;
2391 item->nid = nid;
2392 return 0;
2393 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002394 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2395 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002396 return -EINVAL;
2397}
2398EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2399
2400/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002401 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2402 * @codec: HD-audio codec
2403 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002404void snd_hda_ctls_clear(struct hda_codec *codec)
2405{
2406 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002407 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002408 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002409 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002410 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002411 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002412}
2413
Takashi Iwaia65d6292009-02-23 16:57:04 +01002414/* pseudo device locking
2415 * toggle card->shutdown to allow/disallow the device access (as a hack)
2416 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002417int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002418{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002419 struct snd_card *card = bus->card;
2420 struct hda_codec *codec;
2421
Takashi Iwaia65d6292009-02-23 16:57:04 +01002422 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 if (card->shutdown)
2424 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002425 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002426 if (!list_empty(&card->ctl_files))
2427 goto err_clear;
2428
2429 list_for_each_entry(codec, &bus->codec_list, list) {
2430 int pcm;
2431 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2432 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2433 if (!cpcm->pcm)
2434 continue;
2435 if (cpcm->pcm->streams[0].substream_opened ||
2436 cpcm->pcm->streams[1].substream_opened)
2437 goto err_clear;
2438 }
2439 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002440 spin_unlock(&card->files_lock);
2441 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002442
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002443 err_clear:
2444 card->shutdown = 0;
2445 err_unlock:
2446 spin_unlock(&card->files_lock);
2447 return -EINVAL;
2448}
2449EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2450
2451void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002452{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002453 struct snd_card *card = bus->card;
2454
2455 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002456 spin_lock(&card->files_lock);
2457 card->shutdown = 0;
2458 spin_unlock(&card->files_lock);
2459}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002460EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002461
Takashi Iwaid5191e52009-11-16 14:58:17 +01002462/**
2463 * snd_hda_codec_reset - Clear all objects assigned to the codec
2464 * @codec: HD-audio codec
2465 *
2466 * This frees the all PCM and control elements assigned to the codec, and
2467 * clears the caches and restores the pin default configurations.
2468 *
2469 * When a device is being used, it returns -EBSY. If successfully freed,
2470 * returns zero.
2471 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002472int snd_hda_codec_reset(struct hda_codec *codec)
2473{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002474 struct hda_bus *bus = codec->bus;
2475 struct snd_card *card = bus->card;
2476 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002477
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002478 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002479 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002480
2481 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002482 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002483#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002484 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002485 codec->power_on = 0;
2486 codec->power_transition = 0;
2487 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002488 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002489#endif
2490 snd_hda_ctls_clear(codec);
2491 /* relase PCMs */
2492 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002493 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002494 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002495 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002496 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002497 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002498 }
2499 if (codec->patch_ops.free)
2500 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002501 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002502 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002503 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002504 codec->spec = NULL;
2505 free_hda_cache(&codec->amp_cache);
2506 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002507 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2508 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002509 /* free only driver_pins so that init_pins + user_pins are restored */
2510 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002511 snd_array_free(&codec->cvt_setups);
2512 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002513 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002514 codec->num_pcms = 0;
2515 codec->pcm_info = NULL;
2516 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002517 codec->slave_dig_outs = NULL;
2518 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002519 module_put(codec->owner);
2520 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002521
2522 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002523 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002524 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002525}
2526
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002527typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2528
2529/* apply the function to all matching slave ctls in the mixer list */
2530static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002531 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002532{
2533 struct hda_nid_item *items;
2534 const char * const *s;
2535 int i, err;
2536
2537 items = codec->mixers.list;
2538 for (i = 0; i < codec->mixers.used; i++) {
2539 struct snd_kcontrol *sctl = items[i].kctl;
2540 if (!sctl || !sctl->id.name ||
2541 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2542 continue;
2543 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002544 char tmpname[sizeof(sctl->id.name)];
2545 const char *name = *s;
2546 if (suffix) {
2547 snprintf(tmpname, sizeof(tmpname), "%s %s",
2548 name, suffix);
2549 name = tmpname;
2550 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002551 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002552 err = func(data, sctl);
2553 if (err)
2554 return err;
2555 break;
2556 }
2557 }
2558 }
2559 return 0;
2560}
2561
2562static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2563{
2564 return 1;
2565}
2566
Takashi Iwai18478e82012-03-09 17:51:10 +01002567/* guess the value corresponding to 0dB */
2568static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2569{
2570 int _tlv[4];
2571 const int *tlv = NULL;
2572 int val = -1;
2573
2574 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2575 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2576 mm_segment_t fs = get_fs();
2577 set_fs(get_ds());
2578 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2579 tlv = _tlv;
2580 set_fs(fs);
2581 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2582 tlv = kctl->tlv.p;
2583 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2584 val = -tlv[2] / tlv[3];
2585 return val;
2586}
2587
2588/* call kctl->put with the given value(s) */
2589static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2590{
2591 struct snd_ctl_elem_value *ucontrol;
2592 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2593 if (!ucontrol)
2594 return -ENOMEM;
2595 ucontrol->value.integer.value[0] = val;
2596 ucontrol->value.integer.value[1] = val;
2597 kctl->put(kctl, ucontrol);
2598 kfree(ucontrol);
2599 return 0;
2600}
2601
2602/* initialize the slave volume with 0dB */
2603static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2604{
2605 int offset = get_kctl_0dB_offset(slave);
2606 if (offset > 0)
2607 put_kctl_with_value(slave, offset);
2608 return 0;
2609}
2610
2611/* unmute the slave */
2612static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2613{
2614 return put_kctl_with_value(slave, 1);
2615}
2616
Takashi Iwaid5191e52009-11-16 14:58:17 +01002617/**
2618 * snd_hda_add_vmaster - create a virtual master control and add slaves
2619 * @codec: HD-audio codec
2620 * @name: vmaster control name
2621 * @tlv: TLV data (optional)
2622 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002623 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002624 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002625 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002626 *
2627 * Create a virtual master control with the given name. The TLV data
2628 * must be either NULL or a valid data.
2629 *
2630 * @slaves is a NULL-terminated array of strings, each of which is a
2631 * slave control name. All controls with these names are assigned to
2632 * the new virtual master control.
2633 *
2634 * This function returns zero if successful or a negative error code.
2635 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002636int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002637 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002638 const char *suffix, bool init_slave_vol,
2639 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002640{
2641 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002642 int err;
2643
Takashi Iwai29e58532012-03-12 12:25:03 +01002644 if (ctl_ret)
2645 *ctl_ret = NULL;
2646
Takashi Iwai9322ca52012-02-03 14:28:01 +01002647 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002648 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002649 snd_printdd("No slave found for %s\n", name);
2650 return 0;
2651 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002652 kctl = snd_ctl_make_virtual_master(name, tlv);
2653 if (!kctl)
2654 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002655 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002656 if (err < 0)
2657 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002658
Takashi Iwai9322ca52012-02-03 14:28:01 +01002659 err = map_slaves(codec, slaves, suffix,
2660 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002661 if (err < 0)
2662 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002663
2664 /* init with master mute & zero volume */
2665 put_kctl_with_value(kctl, 0);
2666 if (init_slave_vol)
2667 map_slaves(codec, slaves, suffix,
2668 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2669
Takashi Iwai29e58532012-03-12 12:25:03 +01002670 if (ctl_ret)
2671 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002672 return 0;
2673}
Takashi Iwai18478e82012-03-09 17:51:10 +01002674EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002675
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002676/*
2677 * mute-LED control using vmaster
2678 */
2679static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_info *uinfo)
2681{
2682 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002683 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002684 };
2685 unsigned int index;
2686
2687 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2688 uinfo->count = 1;
2689 uinfo->value.enumerated.items = 3;
2690 index = uinfo->value.enumerated.item;
2691 if (index >= 3)
2692 index = 2;
2693 strcpy(uinfo->value.enumerated.name, texts[index]);
2694 return 0;
2695}
2696
2697static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
2699{
2700 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2701 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2702 return 0;
2703}
2704
2705static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2706 struct snd_ctl_elem_value *ucontrol)
2707{
2708 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2709 unsigned int old_mode = hook->mute_mode;
2710
2711 hook->mute_mode = ucontrol->value.enumerated.item[0];
2712 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2713 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2714 if (old_mode == hook->mute_mode)
2715 return 0;
2716 snd_hda_sync_vmaster_hook(hook);
2717 return 1;
2718}
2719
2720static struct snd_kcontrol_new vmaster_mute_mode = {
2721 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2722 .name = "Mute-LED Mode",
2723 .info = vmaster_mute_mode_info,
2724 .get = vmaster_mute_mode_get,
2725 .put = vmaster_mute_mode_put,
2726};
2727
2728/*
2729 * Add a mute-LED hook with the given vmaster switch kctl
2730 * "Mute-LED Mode" control is automatically created and associated with
2731 * the given hook.
2732 */
2733int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002734 struct hda_vmaster_mute_hook *hook,
2735 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002736{
2737 struct snd_kcontrol *kctl;
2738
2739 if (!hook->hook || !hook->sw_kctl)
2740 return 0;
2741 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2742 hook->codec = codec;
2743 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002744 if (!expose_enum_ctl)
2745 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002746 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2747 if (!kctl)
2748 return -ENOMEM;
2749 return snd_hda_ctl_add(codec, 0, kctl);
2750}
2751EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2752
2753/*
2754 * Call the hook with the current value for synchronization
2755 * Should be called in init callback
2756 */
2757void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2758{
2759 if (!hook->hook || !hook->codec)
2760 return;
2761 switch (hook->mute_mode) {
2762 case HDA_VMUTE_FOLLOW_MASTER:
2763 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2764 break;
2765 default:
2766 hook->hook(hook->codec, hook->mute_mode);
2767 break;
2768 }
2769}
2770EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2771
2772
Takashi Iwaid5191e52009-11-16 14:58:17 +01002773/**
2774 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2775 *
2776 * The control element is supposed to have the private_value field
2777 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2778 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002779int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 int chs = get_amp_channels(kcontrol);
2783
2784 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2785 uinfo->count = chs == 3 ? 2 : 1;
2786 uinfo->value.integer.min = 0;
2787 uinfo->value.integer.max = 1;
2788 return 0;
2789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002790EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Takashi Iwaid5191e52009-11-16 14:58:17 +01002792/**
2793 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2794 *
2795 * The control element is supposed to have the private_value field
2796 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2797 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002798int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2799 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
2801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2802 hda_nid_t nid = get_amp_nid(kcontrol);
2803 int chs = get_amp_channels(kcontrol);
2804 int dir = get_amp_direction(kcontrol);
2805 int idx = get_amp_index(kcontrol);
2806 long *valp = ucontrol->value.integer.value;
2807
2808 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002809 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002810 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002812 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002813 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return 0;
2815}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002816EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Takashi Iwaid5191e52009-11-16 14:58:17 +01002818/**
2819 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2820 *
2821 * The control element is supposed to have the private_value field
2822 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2823 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002824int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2828 hda_nid_t nid = get_amp_nid(kcontrol);
2829 int chs = get_amp_channels(kcontrol);
2830 int dir = get_amp_direction(kcontrol);
2831 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 long *valp = ucontrol->value.integer.value;
2833 int change = 0;
2834
Takashi Iwaicb53c622007-08-10 17:21:45 +02002835 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002836 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002837 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002838 HDA_AMP_MUTE,
2839 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002840 valp++;
2841 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002842 if (chs & 2)
2843 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002844 HDA_AMP_MUTE,
2845 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002846 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002847 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return change;
2849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002850EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852/*
Takashi Iwai985be542005-11-02 18:26:49 +01002853 * bound volume controls
2854 *
2855 * bind multiple volumes (# indices, from 0)
2856 */
2857
2858#define AMP_VAL_IDX_SHIFT 19
2859#define AMP_VAL_IDX_MASK (0x0f<<19)
2860
Takashi Iwaid5191e52009-11-16 14:58:17 +01002861/**
2862 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2863 *
2864 * The control element is supposed to have the private_value field
2865 * set up via HDA_BIND_MUTE*() macros.
2866 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002867int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002869{
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2871 unsigned long pval;
2872 int err;
2873
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002874 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002875 pval = kcontrol->private_value;
2876 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2877 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2878 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002879 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002880 return err;
2881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002882EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002883
Takashi Iwaid5191e52009-11-16 14:58:17 +01002884/**
2885 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2886 *
2887 * The control element is supposed to have the private_value field
2888 * set up via HDA_BIND_MUTE*() macros.
2889 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002890int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002892{
2893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2894 unsigned long pval;
2895 int i, indices, err = 0, change = 0;
2896
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002897 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002898 pval = kcontrol->private_value;
2899 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2900 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002901 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2902 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002903 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2904 if (err < 0)
2905 break;
2906 change |= err;
2907 }
2908 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002909 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002910 return err < 0 ? err : change;
2911}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002912EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002913
Takashi Iwaid5191e52009-11-16 14:58:17 +01002914/**
2915 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2916 *
2917 * The control element is supposed to have the private_value field
2918 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002919 */
2920int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_info *uinfo)
2922{
2923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2924 struct hda_bind_ctls *c;
2925 int err;
2926
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002927 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002928 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002929 kcontrol->private_value = *c->values;
2930 err = c->ops->info(kcontrol, uinfo);
2931 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002932 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002933 return err;
2934}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002935EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002936
Takashi Iwaid5191e52009-11-16 14:58:17 +01002937/**
2938 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2939 *
2940 * The control element is supposed to have the private_value field
2941 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2942 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002943int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2947 struct hda_bind_ctls *c;
2948 int err;
2949
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002950 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002951 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002952 kcontrol->private_value = *c->values;
2953 err = c->ops->get(kcontrol, ucontrol);
2954 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002955 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002956 return err;
2957}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002958EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002959
Takashi Iwaid5191e52009-11-16 14:58:17 +01002960/**
2961 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2962 *
2963 * The control element is supposed to have the private_value field
2964 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2965 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002966int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2968{
2969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970 struct hda_bind_ctls *c;
2971 unsigned long *vals;
2972 int err = 0, change = 0;
2973
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002974 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002975 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002976 for (vals = c->values; *vals; vals++) {
2977 kcontrol->private_value = *vals;
2978 err = c->ops->put(kcontrol, ucontrol);
2979 if (err < 0)
2980 break;
2981 change |= err;
2982 }
2983 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002984 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002985 return err < 0 ? err : change;
2986}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002987EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002988
Takashi Iwaid5191e52009-11-16 14:58:17 +01002989/**
2990 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2991 *
2992 * The control element is supposed to have the private_value field
2993 * set up via HDA_BIND_VOL() macro.
2994 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002995int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2996 unsigned int size, unsigned int __user *tlv)
2997{
2998 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2999 struct hda_bind_ctls *c;
3000 int err;
3001
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003002 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003003 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003004 kcontrol->private_value = *c->values;
3005 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3006 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003007 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003008 return err;
3009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003010EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003011
3012struct hda_ctl_ops snd_hda_bind_vol = {
3013 .info = snd_hda_mixer_amp_volume_info,
3014 .get = snd_hda_mixer_amp_volume_get,
3015 .put = snd_hda_mixer_amp_volume_put,
3016 .tlv = snd_hda_mixer_amp_tlv
3017};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003018EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003019
3020struct hda_ctl_ops snd_hda_bind_sw = {
3021 .info = snd_hda_mixer_amp_switch_info,
3022 .get = snd_hda_mixer_amp_switch_get,
3023 .put = snd_hda_mixer_amp_switch_put,
3024 .tlv = snd_hda_mixer_amp_tlv
3025};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003026EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003027
3028/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * SPDIF out controls
3030 */
3031
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3036 uinfo->count = 1;
3037 return 0;
3038}
3039
Takashi Iwai0ba21762007-04-16 11:29:14 +02003040static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
3043 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3044 IEC958_AES0_NONAUDIO |
3045 IEC958_AES0_CON_EMPHASIS_5015 |
3046 IEC958_AES0_CON_NOT_COPYRIGHT;
3047 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3048 IEC958_AES1_CON_ORIGINAL;
3049 return 0;
3050}
3051
Takashi Iwai0ba21762007-04-16 11:29:14 +02003052static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3056 IEC958_AES0_NONAUDIO |
3057 IEC958_AES0_PRO_EMPHASIS_5015;
3058 return 0;
3059}
3060
Takashi Iwai0ba21762007-04-16 11:29:14 +02003061static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003065 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003066 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003068 mutex_lock(&codec->spdif_mutex);
3069 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003070 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3071 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3072 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3073 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003074 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 return 0;
3077}
3078
3079/* convert from SPDIF status bits to HDA SPDIF bits
3080 * bit 0 (DigEn) is always set zero (to be filled later)
3081 */
3082static unsigned short convert_from_spdif_status(unsigned int sbits)
3083{
3084 unsigned short val = 0;
3085
3086 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003091 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3092 IEC958_AES0_PRO_EMPHASIS_5015)
3093 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3096 IEC958_AES0_CON_EMPHASIS_5015)
3097 val |= AC_DIG1_EMPHASIS;
3098 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3099 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3103 }
3104 return val;
3105}
3106
3107/* convert to SPDIF status bits from HDA SPDIF bits
3108 */
3109static unsigned int convert_to_spdif_status(unsigned short val)
3110{
3111 unsigned int sbits = 0;
3112
Takashi Iwai0ba21762007-04-16 11:29:14 +02003113 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 sbits |= IEC958_AES0_PROFESSIONAL;
3117 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3120 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003121 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003123 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003125 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3127 sbits |= val & (0x7f << 8);
3128 }
3129 return sbits;
3130}
3131
Takashi Iwai2f728532008-09-25 16:32:41 +02003132/* set digital convert verbs both for the given NID and its slaves */
3133static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3134 int verb, int val)
3135{
Takashi Iwaidda14412011-05-02 11:29:30 +02003136 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003137
Takashi Iwai9e976972008-11-25 08:17:20 +01003138 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003139 d = codec->slave_dig_outs;
3140 if (!d)
3141 return;
3142 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003143 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003144}
3145
3146static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3147 int dig1, int dig2)
3148{
3149 if (dig1 != -1)
3150 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3151 if (dig2 != -1)
3152 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3153}
3154
Takashi Iwai0ba21762007-04-16 11:29:14 +02003155static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3156 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003159 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003160 struct hda_spdif_out *spdif;
3161 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 unsigned short val;
3163 int change;
3164
Ingo Molnar62932df2006-01-16 16:34:20 +01003165 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003166 spdif = snd_array_elem(&codec->spdif_out, idx);
3167 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003168 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3170 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3171 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003172 val = convert_from_spdif_status(spdif->status);
3173 val |= spdif->ctls & 1;
3174 change = spdif->ctls != val;
3175 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003176 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003177 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003178 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return change;
3180}
3181
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003182#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Takashi Iwai0ba21762007-04-16 11:29:14 +02003184static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3185 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003188 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003189 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003191 mutex_lock(&codec->spdif_mutex);
3192 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003193 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003194 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return 0;
3196}
3197
Stephen Warren74b654c2011-06-01 11:14:18 -06003198static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3199 int dig1, int dig2)
3200{
3201 set_dig_out_convert(codec, nid, dig1, dig2);
3202 /* unmute amp switch (if any) */
3203 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3204 (dig1 & AC_DIG1_ENABLE))
3205 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3206 HDA_AMP_MUTE, 0);
3207}
3208
Takashi Iwai0ba21762007-04-16 11:29:14 +02003209static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
3212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003213 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003214 struct hda_spdif_out *spdif;
3215 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 unsigned short val;
3217 int change;
3218
Ingo Molnar62932df2006-01-16 16:34:20 +01003219 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003220 spdif = snd_array_elem(&codec->spdif_out, idx);
3221 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003222 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003224 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003225 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003226 spdif->ctls = val;
3227 if (change && nid != (u16)-1)
3228 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003229 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return change;
3231}
3232
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003233static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 {
3235 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003237 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 .info = snd_hda_spdif_mask_info,
3239 .get = snd_hda_spdif_cmask_get,
3240 },
3241 {
3242 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003244 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .info = snd_hda_spdif_mask_info,
3246 .get = snd_hda_spdif_pmask_get,
3247 },
3248 {
3249 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003250 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 .info = snd_hda_spdif_mask_info,
3252 .get = snd_hda_spdif_default_get,
3253 .put = snd_hda_spdif_default_put,
3254 },
3255 {
3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003257 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 .info = snd_hda_spdif_out_switch_info,
3259 .get = snd_hda_spdif_out_switch_get,
3260 .put = snd_hda_spdif_out_switch_put,
3261 },
3262 { } /* end */
3263};
3264
3265/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003266 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003268 * @associated_nid: NID that new ctls associated with
3269 * @cvt_nid: converter NID
3270 * @type: HDA_PCM_TYPE_*
3271 * Creates controls related with the digital output.
3272 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 *
3274 * Returns 0 if successful, or a negative error code.
3275 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003276int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3277 hda_nid_t associated_nid,
3278 hda_nid_t cvt_nid,
3279 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003282 struct snd_kcontrol *kctl;
3283 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003284 int idx, dev = 0;
3285 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003286 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Takashi Iwaidcda5802012-10-12 17:24:51 +02003288 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3289 type == HDA_PCM_TYPE_SPDIF) {
3290 dev = spdif_pcm_dev;
3291 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3292 type == HDA_PCM_TYPE_HDMI) {
3293 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3294 spdif = snd_array_elem(&codec->spdif_out, idx);
3295 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3296 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3297 if (!kctl)
3298 break;
3299 kctl->id.device = spdif_pcm_dev;
3300 }
3301 }
3302 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3303 }
3304 if (!codec->primary_dig_out_type)
3305 codec->primary_dig_out_type = type;
3306
3307 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003308 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003309 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3310 return -EBUSY;
3311 }
Stephen Warren7c935972011-06-01 11:14:17 -06003312 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3314 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003315 if (!kctl)
3316 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003317 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003318 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003319 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003320 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003321 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return err;
3323 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003324 spdif->nid = cvt_nid;
3325 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003326 AC_VERB_GET_DIGI_CONVERT_1, 0);
3327 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return 0;
3329}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003330EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003332/* get the hda_spdif_out entry from the given NID
3333 * call within spdif_mutex lock
3334 */
Stephen Warren7c935972011-06-01 11:14:17 -06003335struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3336 hda_nid_t nid)
3337{
3338 int i;
3339 for (i = 0; i < codec->spdif_out.used; i++) {
3340 struct hda_spdif_out *spdif =
3341 snd_array_elem(&codec->spdif_out, i);
3342 if (spdif->nid == nid)
3343 return spdif;
3344 }
3345 return NULL;
3346}
3347EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3348
Stephen Warren74b654c2011-06-01 11:14:18 -06003349void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3350{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003351 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003352
3353 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003354 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003355 spdif->nid = (u16)-1;
3356 mutex_unlock(&codec->spdif_mutex);
3357}
3358EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3359
3360void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3361{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003362 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003363 unsigned short val;
3364
3365 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003366 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003367 if (spdif->nid != nid) {
3368 spdif->nid = nid;
3369 val = spdif->ctls;
3370 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3371 }
3372 mutex_unlock(&codec->spdif_mutex);
3373}
3374EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003377 * SPDIF sharing with analog output
3378 */
3379static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3380 struct snd_ctl_elem_value *ucontrol)
3381{
3382 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3383 ucontrol->value.integer.value[0] = mout->share_spdif;
3384 return 0;
3385}
3386
3387static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_value *ucontrol)
3389{
3390 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3391 mout->share_spdif = !!ucontrol->value.integer.value[0];
3392 return 0;
3393}
3394
3395static struct snd_kcontrol_new spdif_share_sw = {
3396 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3397 .name = "IEC958 Default PCM Playback Switch",
3398 .info = snd_ctl_boolean_mono_info,
3399 .get = spdif_share_sw_get,
3400 .put = spdif_share_sw_put,
3401};
3402
Takashi Iwaid5191e52009-11-16 14:58:17 +01003403/**
3404 * snd_hda_create_spdif_share_sw - create Default PCM switch
3405 * @codec: the HDA codec
3406 * @mout: multi-out instance
3407 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003408int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3409 struct hda_multi_out *mout)
3410{
3411 if (!mout->dig_out_nid)
3412 return 0;
3413 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003414 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3415 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003416}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003417EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003418
3419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 * SPDIF input
3421 */
3422
3423#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3424
Takashi Iwai0ba21762007-04-16 11:29:14 +02003425static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3426 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3429
3430 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3431 return 0;
3432}
3433
Takashi Iwai0ba21762007-04-16 11:29:14 +02003434static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
3437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3438 hda_nid_t nid = kcontrol->private_value;
3439 unsigned int val = !!ucontrol->value.integer.value[0];
3440 int change;
3441
Ingo Molnar62932df2006-01-16 16:34:20 +01003442 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003444 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003446 snd_hda_codec_write_cache(codec, nid, 0,
3447 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003449 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return change;
3451}
3452
Takashi Iwai0ba21762007-04-16 11:29:14 +02003453static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3457 hda_nid_t nid = kcontrol->private_value;
3458 unsigned short val;
3459 unsigned int sbits;
3460
Andrew Paprocki3982d172007-12-19 12:13:44 +01003461 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 sbits = convert_to_spdif_status(val);
3463 ucontrol->value.iec958.status[0] = sbits;
3464 ucontrol->value.iec958.status[1] = sbits >> 8;
3465 ucontrol->value.iec958.status[2] = sbits >> 16;
3466 ucontrol->value.iec958.status[3] = sbits >> 24;
3467 return 0;
3468}
3469
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003470static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 {
3472 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003473 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 .info = snd_hda_spdif_in_switch_info,
3475 .get = snd_hda_spdif_in_switch_get,
3476 .put = snd_hda_spdif_in_switch_put,
3477 },
3478 {
3479 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3480 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003481 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 .info = snd_hda_spdif_mask_info,
3483 .get = snd_hda_spdif_in_status_get,
3484 },
3485 { } /* end */
3486};
3487
3488/**
3489 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3490 * @codec: the HDA codec
3491 * @nid: audio in widget NID
3492 *
3493 * Creates controls related with the SPDIF input.
3494 * Called from each patch supporting the SPDIF in.
3495 *
3496 * Returns 0 if successful, or a negative error code.
3497 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003498int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
3500 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003501 struct snd_kcontrol *kctl;
3502 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003503 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Takashi Iwaidcda5802012-10-12 17:24:51 +02003505 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003506 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003507 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3508 return -EBUSY;
3509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3511 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003512 if (!kctl)
3513 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003515 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003516 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return err;
3518 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003519 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003520 snd_hda_codec_read(codec, nid, 0,
3521 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003522 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return 0;
3524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003525EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003527/*
3528 * command cache
3529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Takashi Iwaic370dd62012-12-13 18:30:04 +01003531/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003532#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3533#define get_cmd_cache_nid(key) ((key) & 0xff)
3534#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3535
3536/**
3537 * snd_hda_codec_write_cache - send a single command with caching
3538 * @codec: the HDA codec
3539 * @nid: NID to send the command
3540 * @direct: direct flag
3541 * @verb: the verb to send
3542 * @parm: the parameter for the verb
3543 *
3544 * Send a single command without waiting for response.
3545 *
3546 * Returns 0 if successful, or a negative error code.
3547 */
3548int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3549 int direct, unsigned int verb, unsigned int parm)
3550{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003551 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003552 struct hda_cache_head *c;
3553 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003554 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003555
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003556 cache_only = codec->cached_write;
3557 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003558 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3559 if (err < 0)
3560 return err;
3561 }
3562
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003563 /* parm may contain the verb stuff for get/set amp */
3564 verb = verb | (parm >> 8);
3565 parm &= 0xff;
3566 key = build_cmd_cache_key(nid, verb);
3567 mutex_lock(&codec->bus->cmd_mutex);
3568 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003569 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003570 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003571 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003572 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003573 mutex_unlock(&codec->bus->cmd_mutex);
3574 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003575}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003576EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003577
Takashi Iwaid5191e52009-11-16 14:58:17 +01003578/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003579 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
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 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3587 * command if the parameter is already identical with the cached value.
3588 * If not, it sends the command and refreshes the cache.
3589 *
3590 * Returns 0 if successful, or a negative error code.
3591 */
3592int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3593 int direct, unsigned int verb, unsigned int parm)
3594{
3595 struct hda_cache_head *c;
3596 u32 key;
3597
3598 /* parm may contain the verb stuff for get/set amp */
3599 verb = verb | (parm >> 8);
3600 parm &= 0xff;
3601 key = build_cmd_cache_key(nid, verb);
3602 mutex_lock(&codec->bus->cmd_mutex);
3603 c = get_hash(&codec->cmd_cache, key);
3604 if (c && c->val == parm) {
3605 mutex_unlock(&codec->bus->cmd_mutex);
3606 return 0;
3607 }
3608 mutex_unlock(&codec->bus->cmd_mutex);
3609 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3610}
3611EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3612
3613/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003614 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3615 * @codec: HD-audio codec
3616 *
3617 * Execute all verbs recorded in the command caches to resume.
3618 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003619void snd_hda_codec_resume_cache(struct hda_codec *codec)
3620{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003621 int i;
3622
Takashi Iwaic370dd62012-12-13 18:30:04 +01003623 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003624 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003625 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3626 struct hda_cache_head *buffer;
3627 u32 key;
3628
3629 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3630 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003631 if (!key)
3632 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003633 if (!buffer->dirty)
3634 continue;
3635 buffer->dirty = 0;
3636 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003637 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3638 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003639 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003640 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003641 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003642}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003643EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003644
3645/**
3646 * snd_hda_sequence_write_cache - sequence writes with caching
3647 * @codec: the HDA codec
3648 * @seq: VERB array to send
3649 *
3650 * Send the commands sequentially from the given array.
3651 * Thte commands are recorded on cache for power-save and resume.
3652 * The array must be terminated with NID=0.
3653 */
3654void snd_hda_sequence_write_cache(struct hda_codec *codec,
3655 const struct hda_verb *seq)
3656{
3657 for (; seq->nid; seq++)
3658 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3659 seq->param);
3660}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003661EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003662
Takashi Iwaidc870f32013-01-22 15:24:30 +01003663/**
3664 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3665 * @codec: HD-audio codec
3666 */
3667void snd_hda_codec_flush_cache(struct hda_codec *codec)
3668{
3669 snd_hda_codec_resume_amp(codec);
3670 snd_hda_codec_resume_cache(codec);
3671}
3672EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3673
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003674void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003675 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003676{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003677 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003678 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003679
Takashi Iwaicb53c622007-08-10 17:21:45 +02003680 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003681 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003682 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003683 if (!(wcaps & AC_WCAP_POWER))
3684 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003685 if (codec->power_filter) {
3686 state = codec->power_filter(codec, nid, power_state);
3687 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003688 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003689 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003690 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003691 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003692 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003693}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003694EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3695
3696/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003697 * supported power states check
3698 */
3699static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3700 unsigned int power_state)
3701{
3702 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3703
Mengdong Line037cb42012-08-10 14:11:58 +02003704 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003705 return false;
3706 if (sup & power_state)
3707 return true;
3708 else
3709 return false;
3710}
3711
3712/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003713 * wait until the state is reached, returns the current state
3714 */
3715static unsigned int hda_sync_power_state(struct hda_codec *codec,
3716 hda_nid_t fg,
3717 unsigned int power_state)
3718{
3719 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3720 unsigned int state, actual_state;
3721
3722 for (;;) {
3723 state = snd_hda_codec_read(codec, fg, 0,
3724 AC_VERB_GET_POWER_STATE, 0);
3725 if (state & AC_PWRST_ERROR)
3726 break;
3727 actual_state = (state >> 4) & 0x0f;
3728 if (actual_state == power_state)
3729 break;
3730 if (time_after_eq(jiffies, end_time))
3731 break;
3732 /* wait until the codec reachs to the target state */
3733 msleep(1);
3734 }
3735 return state;
3736}
3737
Takashi Iwai9419ab62013-01-24 17:23:35 +01003738/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3739static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
3740 unsigned int power_state)
3741{
3742 if (power_state == AC_PWRST_D3 &&
3743 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3744 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3745 int eapd = snd_hda_codec_read(codec, nid, 0,
3746 AC_VERB_GET_EAPD_BTLENABLE, 0);
3747 if (eapd & 0x02)
3748 return AC_PWRST_D0;
3749 }
3750 return power_state;
3751}
3752
Takashi Iwai432c6412012-08-28 09:59:20 -07003753/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003754 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003755 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003756static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003757 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003758{
Takashi Iwaid8193872012-08-31 07:54:38 -07003759 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003760 int count;
3761 unsigned int state;
3762
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003763 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003764 if (power_state == AC_PWRST_D3) {
3765 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003766 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003767 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003768
3769 /* repeat power states setting at most 10 times*/
3770 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003771 if (codec->patch_ops.set_power_state)
3772 codec->patch_ops.set_power_state(codec, fg,
3773 power_state);
3774 else {
3775 snd_hda_codec_read(codec, fg, 0,
3776 AC_VERB_SET_POWER_STATE,
3777 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003778 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003779 }
3780 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003781 if (!(state & AC_PWRST_ERROR))
3782 break;
3783 }
Mengdong Linb8dfc462012-08-23 17:32:30 +08003784
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003785 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003786}
Takashi Iwai54d17402005-11-21 16:33:22 +01003787
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003788/* sync power states of all widgets;
3789 * this is called at the end of codec parsing
3790 */
3791static void sync_power_up_states(struct hda_codec *codec)
3792{
3793 hda_nid_t nid = codec->start_nid;
3794 int i;
3795
3796 /* don't care if no or standard filter is used */
3797 if (!codec->power_filter || codec->power_filter == default_power_filter)
3798 return;
3799
3800 for (i = 0; i < codec->num_nodes; i++, nid++) {
3801 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003802 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003803 if (!(wcaps & AC_WCAP_POWER))
3804 continue;
3805 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3806 if (target == AC_PWRST_D0)
3807 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003808 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003809 snd_hda_codec_write(codec, nid, 0,
3810 AC_VERB_SET_POWER_STATE, target);
3811 }
3812}
3813
Takashi Iwai11aeff02008-07-30 15:01:46 +02003814#ifdef CONFIG_SND_HDA_HWDEP
3815/* execute additional init verbs */
3816static void hda_exec_init_verbs(struct hda_codec *codec)
3817{
3818 if (codec->init_verbs.list)
3819 snd_hda_sequence_write(codec, codec->init_verbs.list);
3820}
3821#else
3822static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3823#endif
3824
Takashi Iwai2a439522011-07-26 09:52:50 +02003825#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003826/*
3827 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003828 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003829 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003830static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003831{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003832 unsigned int state;
3833
Takashi Iwai989c3182012-11-19 14:14:58 +01003834 codec->in_pm = 1;
3835
Takashi Iwaicb53c622007-08-10 17:21:45 +02003836 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003837 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003838 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003839 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003840 /* Cancel delayed work if we aren't currently running from it. */
3841 if (!in_wq)
3842 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003843 spin_lock(&codec->power_lock);
3844 snd_hda_update_power_acct(codec);
3845 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003846 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003847 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003848 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003849 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003850 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003851 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003852}
3853
Takashi Iwaic370dd62012-12-13 18:30:04 +01003854/* mark all entries of cmd and amp caches dirty */
3855static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3856{
3857 int i;
3858 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3859 struct hda_cache_head *cmd;
3860 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3861 cmd->dirty = 1;
3862 }
3863 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3864 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003865 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003866 amp->head.dirty = 1;
3867 }
3868}
3869
Takashi Iwaicb53c622007-08-10 17:21:45 +02003870/*
3871 * kick up codec; used both from PM and power-save
3872 */
3873static void hda_call_codec_resume(struct hda_codec *codec)
3874{
Takashi Iwai989c3182012-11-19 14:14:58 +01003875 codec->in_pm = 1;
3876
Takashi Iwaic370dd62012-12-13 18:30:04 +01003877 hda_mark_cmd_cache_dirty(codec);
3878
Takashi Iwai7f308302012-05-08 16:52:23 +02003879 /* set as if powered on for avoiding re-entering the resume
3880 * in the resume / power-save sequence
3881 */
3882 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003883 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003884 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003885 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003886 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003887 if (codec->patch_ops.resume)
3888 codec->patch_ops.resume(codec);
3889 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003890 if (codec->patch_ops.init)
3891 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003892 snd_hda_codec_resume_amp(codec);
3893 snd_hda_codec_resume_cache(codec);
3894 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003895
3896 if (codec->jackpoll_interval)
3897 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003898 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003899 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003900
3901 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003902 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003903}
Takashi Iwai2a439522011-07-26 09:52:50 +02003904#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003905
Takashi Iwai54d17402005-11-21 16:33:22 +01003906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907/**
3908 * snd_hda_build_controls - build mixer controls
3909 * @bus: the BUS
3910 *
3911 * Creates mixer controls for each codec included in the bus.
3912 *
3913 * Returns 0 if successful, otherwise a negative error code.
3914 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003915int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003917 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Takashi Iwai0ba21762007-04-16 11:29:14 +02003919 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003920 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003921 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003922 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003923 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003924 err = snd_hda_codec_reset(codec);
3925 if (err < 0) {
3926 printk(KERN_ERR
3927 "hda_codec: cannot revert codec\n");
3928 return err;
3929 }
3930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003932 return 0;
3933}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003934EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003936/*
3937 * add standard channel maps if not specified
3938 */
3939static int add_std_chmaps(struct hda_codec *codec)
3940{
3941 int i, str, err;
3942
3943 for (i = 0; i < codec->num_pcms; i++) {
3944 for (str = 0; str < 2; str++) {
3945 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3946 struct hda_pcm_stream *hinfo =
3947 &codec->pcm_info[i].stream[str];
3948 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003949 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003950
3951 if (codec->pcm_info[i].own_chmap)
3952 continue;
3953 if (!pcm || !hinfo->substreams)
3954 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003955 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3956 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003957 hinfo->channels_max,
3958 0, &chmap);
3959 if (err < 0)
3960 return err;
3961 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3962 }
3963 }
3964 return 0;
3965}
3966
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003967/* default channel maps for 2.1 speakers;
3968 * since HD-audio supports only stereo, odd number channels are omitted
3969 */
3970const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3971 { .channels = 2,
3972 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3973 { .channels = 4,
3974 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3975 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3976 { }
3977};
3978EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3979
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003980int snd_hda_codec_build_controls(struct hda_codec *codec)
3981{
3982 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003983 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003984 /* continue to initialize... */
3985 if (codec->patch_ops.init)
3986 err = codec->patch_ops.init(codec);
3987 if (!err && codec->patch_ops.build_controls)
3988 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003989 if (err < 0)
3990 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003991
3992 /* we create chmaps here instead of build_pcms */
3993 err = add_std_chmaps(codec);
3994 if (err < 0)
3995 return err;
3996
David Henningsson26a6cb62012-10-09 15:04:21 +02003997 if (codec->jackpoll_interval)
3998 hda_jackpoll_work(&codec->jackpoll_work.work);
3999 else
4000 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004001 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return 0;
4003}
4004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005/*
4006 * stream formats
4007 */
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004008struct hda_rate_tbl {
4009 unsigned int hz;
4010 unsigned int alsa_bits;
4011 unsigned int hda_fmt;
4012};
4013
Takashi Iwai92f10b32010-08-03 14:21:00 +02004014/* rate = base * mult / div */
4015#define HDA_RATE(base, mult, div) \
4016 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4017 (((div) - 1) << AC_FMT_DIV_SHIFT))
4018
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004019static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004021
4022 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004023 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4024 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4025 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4026 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4027 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4028 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4029 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4030 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4031 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4032 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4033 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004034#define AC_PAR_PCM_RATE_BITS 11
4035 /* up to bits 10, 384kHZ isn't supported properly */
4036
4037 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004038 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004039
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004040 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041};
4042
4043/**
4044 * snd_hda_calc_stream_format - calculate format bitset
4045 * @rate: the sample rate
4046 * @channels: the number of channels
4047 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4048 * @maxbps: the max. bps
4049 *
4050 * Calculate the format bitset from the given rate, channels and th PCM format.
4051 *
4052 * Return zero if invalid.
4053 */
4054unsigned int snd_hda_calc_stream_format(unsigned int rate,
4055 unsigned int channels,
4056 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004057 unsigned int maxbps,
4058 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 int i;
4061 unsigned int val = 0;
4062
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004063 for (i = 0; rate_bits[i].hz; i++)
4064 if (rate_bits[i].hz == rate) {
4065 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 break;
4067 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004068 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 snd_printdd("invalid rate %d\n", rate);
4070 return 0;
4071 }
4072
4073 if (channels == 0 || channels > 8) {
4074 snd_printdd("invalid channels %d\n", channels);
4075 return 0;
4076 }
4077 val |= channels - 1;
4078
4079 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004080 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004081 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004082 break;
4083 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004084 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 case 20:
4087 case 24:
4088 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004089 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004090 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004092 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004094 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 break;
4096 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004097 snd_printdd("invalid format width %d\n",
4098 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return 0;
4100 }
4101
Anssi Hannula32c168c2010-08-03 13:28:57 +03004102 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004103 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return val;
4106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004107EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004109static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4110 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004111{
4112 unsigned int val = 0;
4113 if (nid != codec->afg &&
4114 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4115 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4116 if (!val || val == -1)
4117 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4118 if (!val || val == -1)
4119 return 0;
4120 return val;
4121}
4122
4123static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4124{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004125 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004126 get_pcm_param);
4127}
4128
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004129static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4130 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004131{
4132 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4133 if (!streams || streams == -1)
4134 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4135 if (!streams || streams == -1)
4136 return 0;
4137 return streams;
4138}
4139
4140static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4141{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004142 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004143 get_stream_param);
4144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146/**
4147 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4148 * @codec: the HDA codec
4149 * @nid: NID to query
4150 * @ratesp: the pointer to store the detected rate bitflags
4151 * @formatsp: the pointer to store the detected formats
4152 * @bpsp: the pointer to store the detected format widths
4153 *
4154 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4155 * or @bsps argument is ignored.
4156 *
4157 * Returns 0 if successful, otherwise a negative error code.
4158 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004159int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4161{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004162 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004164 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004165 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 if (ratesp) {
4168 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004169 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 if (val & (1 << i))
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004171 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004173 if (rates == 0) {
4174 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4175 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4176 nid, val,
4177 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4178 return -EIO;
4179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 *ratesp = rates;
4181 }
4182
4183 if (formatsp || bpsp) {
4184 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004185 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004187 streams = query_stream_param(codec, nid);
4188 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 bps = 0;
4192 if (streams & AC_SUPFMT_PCM) {
4193 if (val & AC_SUPPCM_BITS_8) {
4194 formats |= SNDRV_PCM_FMTBIT_U8;
4195 bps = 8;
4196 }
4197 if (val & AC_SUPPCM_BITS_16) {
4198 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4199 bps = 16;
4200 }
4201 if (wcaps & AC_WCAP_DIGITAL) {
4202 if (val & AC_SUPPCM_BITS_32)
4203 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4204 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4205 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4206 if (val & AC_SUPPCM_BITS_24)
4207 bps = 24;
4208 else if (val & AC_SUPPCM_BITS_20)
4209 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004210 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4211 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4213 if (val & AC_SUPPCM_BITS_32)
4214 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 else if (val & AC_SUPPCM_BITS_24)
4216 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004217 else if (val & AC_SUPPCM_BITS_20)
4218 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004221#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004222 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004224 if (!bps)
4225 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004226 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004227#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004228 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004229 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 /* temporary hack: we have still no proper support
4231 * for the direct AC3 stream...
4232 */
4233 formats |= SNDRV_PCM_FMTBIT_U8;
4234 bps = 8;
4235 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004236 if (formats == 0) {
4237 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4238 "(nid=0x%x, val=0x%x, ovrd=%i, "
4239 "streams=0x%x)\n",
4240 nid, val,
4241 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4242 streams);
4243 return -EIO;
4244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (formatsp)
4246 *formatsp = formats;
4247 if (bpsp)
4248 *bpsp = bps;
4249 }
4250
4251 return 0;
4252}
Stephen Warren384a48d2011-06-01 11:14:21 -06004253EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
4255/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004256 * snd_hda_is_supported_format - Check the validity of the format
4257 * @codec: HD-audio codec
4258 * @nid: NID to check
4259 * @format: the HD-audio format value to check
4260 *
4261 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 *
4263 * Returns 1 if supported, 0 if not.
4264 */
4265int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4266 unsigned int format)
4267{
4268 int i;
4269 unsigned int val = 0, rate, stream;
4270
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004271 val = query_pcm_param(codec, nid);
4272 if (!val)
4273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004276 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf3162005-08-22 13:57:55 +02004277 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (val & (1 << i))
4279 break;
4280 return 0;
4281 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004282 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return 0;
4284
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004285 stream = query_stream_param(codec, nid);
4286 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return 0;
4288
4289 if (stream & AC_SUPFMT_PCM) {
4290 switch (format & 0xf0) {
4291 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004292 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return 0;
4294 break;
4295 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004296 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return 0;
4298 break;
4299 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004300 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return 0;
4302 break;
4303 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004304 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return 0;
4306 break;
4307 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004308 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return 0;
4310 break;
4311 default:
4312 return 0;
4313 }
4314 } else {
4315 /* FIXME: check for float32 and AC3? */
4316 }
4317
4318 return 1;
4319}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004320EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322/*
4323 * PCM stuff
4324 */
4325static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4326 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004327 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
4329 return 0;
4330}
4331
4332static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4333 struct hda_codec *codec,
4334 unsigned int stream_tag,
4335 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004336 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4339 return 0;
4340}
4341
4342static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4343 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004344 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Takashi Iwai888afa12008-03-18 09:57:50 +01004346 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348}
4349
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004350static int set_pcm_default_values(struct hda_codec *codec,
4351 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004353 int err;
4354
Takashi Iwai0ba21762007-04-16 11:29:14 +02004355 /* query support PCM information from the given NID */
4356 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004357 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004358 info->rates ? NULL : &info->rates,
4359 info->formats ? NULL : &info->formats,
4360 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004361 if (err < 0)
4362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
4364 if (info->ops.open == NULL)
4365 info->ops.open = hda_pcm_default_open_close;
4366 if (info->ops.close == NULL)
4367 info->ops.close = hda_pcm_default_open_close;
4368 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004369 if (snd_BUG_ON(!info->nid))
4370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 info->ops.prepare = hda_pcm_default_prepare;
4372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004374 if (snd_BUG_ON(!info->nid))
4375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 info->ops.cleanup = hda_pcm_default_cleanup;
4377 }
4378 return 0;
4379}
4380
Takashi Iwaieb541332010-08-06 13:48:11 +02004381/*
4382 * codec prepare/cleanup entries
4383 */
4384int snd_hda_codec_prepare(struct hda_codec *codec,
4385 struct hda_pcm_stream *hinfo,
4386 unsigned int stream,
4387 unsigned int format,
4388 struct snd_pcm_substream *substream)
4389{
4390 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004391 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004392 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4393 if (ret >= 0)
4394 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004395 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004396 return ret;
4397}
4398EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4399
4400void snd_hda_codec_cleanup(struct hda_codec *codec,
4401 struct hda_pcm_stream *hinfo,
4402 struct snd_pcm_substream *substream)
4403{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004404 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004405 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004406 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004407}
4408EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4409
Takashi Iwaid5191e52009-11-16 14:58:17 +01004410/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004411const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4412 "Audio", "SPDIF", "HDMI", "Modem"
4413};
4414
Takashi Iwai176d5332008-07-30 15:01:44 +02004415/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004416 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004417 *
4418 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004419 */
4420static int get_empty_pcm_device(struct hda_bus *bus, int type)
4421{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004422 /* audio device indices; not linear to keep compatibility */
4423 static int audio_idx[HDA_PCM_NTYPES][5] = {
4424 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4425 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004426 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004427 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004428 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004429 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004430
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004431 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004432 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4433 return -EINVAL;
4434 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004435
4436 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4437 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4438 return audio_idx[type][i];
4439
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004440 /* non-fixed slots starting from 10 */
4441 for (i = 10; i < 32; i++) {
4442 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4443 return i;
4444 }
4445
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004446 snd_printk(KERN_WARNING "Too many %s devices\n",
4447 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004448 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004449}
4450
4451/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004452 * attach a new PCM stream
4453 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004454static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004455{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004456 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004457 struct hda_pcm_stream *info;
4458 int stream, err;
4459
Takashi Iwaib91f0802008-11-04 08:43:08 +01004460 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004461 return -EINVAL;
4462 for (stream = 0; stream < 2; stream++) {
4463 info = &pcm->stream[stream];
4464 if (info->substreams) {
4465 err = set_pcm_default_values(codec, info);
4466 if (err < 0)
4467 return err;
4468 }
4469 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004470 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004471}
4472
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004473/* assign all PCMs of the given codec */
4474int snd_hda_codec_build_pcms(struct hda_codec *codec)
4475{
4476 unsigned int pcm;
4477 int err;
4478
4479 if (!codec->num_pcms) {
4480 if (!codec->patch_ops.build_pcms)
4481 return 0;
4482 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004483 if (err < 0) {
4484 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004485 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004486 err = snd_hda_codec_reset(codec);
4487 if (err < 0) {
4488 printk(KERN_ERR
4489 "hda_codec: cannot revert codec\n");
4490 return err;
4491 }
4492 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004493 }
4494 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4495 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4496 int dev;
4497
4498 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004499 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004500
4501 if (!cpcm->pcm) {
4502 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4503 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004504 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004505 cpcm->device = dev;
4506 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004507 if (err < 0) {
4508 printk(KERN_ERR "hda_codec: cannot attach "
4509 "PCM stream %d for codec #%d\n",
4510 dev, codec->addr);
4511 continue; /* no fatal error */
4512 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004513 }
4514 }
4515 return 0;
4516}
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518/**
4519 * snd_hda_build_pcms - build PCM information
4520 * @bus: the BUS
4521 *
4522 * Create PCM information for each codec included in the bus.
4523 *
4524 * The build_pcms codec patch is requested to set up codec->num_pcms and
4525 * codec->pcm_info properly. The array is referred by the top-level driver
4526 * to create its PCM instances.
4527 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4528 * callback.
4529 *
4530 * At least, substreams, channels_min and channels_max must be filled for
4531 * each stream. substreams = 0 indicates that the stream doesn't exist.
4532 * When rates and/or formats are zero, the supported values are queried
4533 * from the given nid. The nid is used also by the default ops.prepare
4534 * and ops.cleanup callbacks.
4535 *
4536 * The driver needs to call ops.open in its open callback. Similarly,
4537 * ops.close is supposed to be called in the close callback.
4538 * ops.prepare should be called in the prepare or hw_params callback
4539 * with the proper parameters for set up.
4540 * ops.cleanup should be called in hw_free for clean up of streams.
4541 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004542 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004544int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004546 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Takashi Iwai0ba21762007-04-16 11:29:14 +02004548 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004549 int err = snd_hda_codec_build_pcms(codec);
4550 if (err < 0)
4551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 }
4553 return 0;
4554}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004555EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557/**
4558 * snd_hda_check_board_config - compare the current codec with the config table
4559 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004560 * @num_configs: number of config enums
4561 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 * @tbl: configuration table, terminated by null entries
4563 *
4564 * Compares the modelname or PCI subsystem id of the current codec with the
4565 * given configuration table. If a matching entry is found, returns its
4566 * config value (supposed to be 0 or positive).
4567 *
4568 * If no entries are matching, the function returns a negative value.
4569 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004570int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004571 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004572 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004574 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004575 int i;
4576 for (i = 0; i < num_configs; i++) {
4577 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004578 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004579 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4580 "selected\n", models[i]);
4581 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
4583 }
4584 }
4585
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004586 if (!codec->bus->pci || !tbl)
4587 return -1;
4588
4589 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4590 if (!tbl)
4591 return -1;
4592 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004593#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004594 char tmp[10];
4595 const char *model = NULL;
4596 if (models)
4597 model = models[tbl->value];
4598 if (!model) {
4599 sprintf(tmp, "#%d", tbl->value);
4600 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004602 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4603 "for config %x:%x (%s)\n",
4604 model, tbl->subvendor, tbl->subdevice,
4605 (tbl->name ? tbl->name : "Unknown device"));
4606#endif
4607 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 }
4609 return -1;
4610}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004611EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
4613/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004614 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004615 subsystem ID with the
4616 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004617
4618 This is important for Gateway notebooks with SB450 HDA Audio
4619 where the vendor ID of the PCI device is:
4620 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4621 and the vendor/subvendor are found only at the codec.
4622
4623 * @codec: the HDA codec
4624 * @num_configs: number of config enums
4625 * @models: array of model name strings
4626 * @tbl: configuration table, terminated by null entries
4627 *
4628 * Compares the modelname or PCI subsystem id of the current codec with the
4629 * given configuration table. If a matching entry is found, returns its
4630 * config value (supposed to be 0 or positive).
4631 *
4632 * If no entries are matching, the function returns a negative value.
4633 */
4634int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004635 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004636 const struct snd_pci_quirk *tbl)
4637{
4638 const struct snd_pci_quirk *q;
4639
4640 /* Search for codec ID */
4641 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004642 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4643 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4644 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004645 break;
4646 }
4647
4648 if (!q->subvendor)
4649 return -1;
4650
4651 tbl = q;
4652
4653 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004654#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004655 char tmp[10];
4656 const char *model = NULL;
4657 if (models)
4658 model = models[tbl->value];
4659 if (!model) {
4660 sprintf(tmp, "#%d", tbl->value);
4661 model = tmp;
4662 }
4663 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4664 "for config %x:%x (%s)\n",
4665 model, tbl->subvendor, tbl->subdevice,
4666 (tbl->name ? tbl->name : "Unknown device"));
4667#endif
4668 return tbl->value;
4669 }
4670 return -1;
4671}
4672EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4673
4674/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 * snd_hda_add_new_ctls - create controls from the array
4676 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004677 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 *
4679 * This helper function creates and add new controls in the given array.
4680 * The array must be terminated with an empty entry as terminator.
4681 *
4682 * Returns 0 if successful, or a negative error code.
4683 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004684int snd_hda_add_new_ctls(struct hda_codec *codec,
4685 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004687 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
4689 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004690 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004691 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004692 if (knew->iface == -1) /* skip this codec private value */
4693 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004694 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004695 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004696 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004697 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004698 if (addr > 0)
4699 kctl->id.device = addr;
4700 if (idx > 0)
4701 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004702 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004703 if (!err)
4704 break;
4705 /* try first with another device index corresponding to
4706 * the codec addr; if it still fails (or it's the
4707 * primary codec), then try another control index
4708 */
4709 if (!addr && codec->addr)
4710 addr = codec->addr;
4711 else if (!idx && !knew->index) {
4712 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004713 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004714 if (idx <= 0)
4715 return err;
4716 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004717 return err;
4718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720 return 0;
4721}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004722EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
Takashi Iwai83012a72012-08-24 18:38:08 +02004724#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004725static void hda_power_work(struct work_struct *work)
4726{
4727 struct hda_codec *codec =
4728 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004729 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004730 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004731
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004732 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004733 if (codec->power_transition > 0) { /* during power-up sequence? */
4734 spin_unlock(&codec->power_lock);
4735 return;
4736 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004737 if (!codec->power_on || codec->power_count) {
4738 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004739 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004740 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004741 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004742 spin_unlock(&codec->power_lock);
4743
Dylan Reidd17344b2012-09-28 15:57:01 -07004744 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004745 codec->pm_down_notified = 0;
4746 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4747 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004748 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004749 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004750}
4751
4752static void hda_keep_power_on(struct hda_codec *codec)
4753{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004754 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004755 codec->power_count++;
4756 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004757 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004758 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004759}
4760
Takashi Iwaid5191e52009-11-16 14:58:17 +01004761/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004762void snd_hda_update_power_acct(struct hda_codec *codec)
4763{
4764 unsigned long delta = jiffies - codec->power_jiffies;
4765 if (codec->power_on)
4766 codec->power_on_acct += delta;
4767 else
4768 codec->power_off_acct += delta;
4769 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004770}
4771
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004772/* Transition to powered up, if wait_power_down then wait for a pending
4773 * transition to D3 to complete. A pending D3 transition is indicated
4774 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004775/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004776static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004777{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004778 struct hda_bus *bus = codec->bus;
4779
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004780 /* Return if power_on or transitioning to power_on, unless currently
4781 * powering down. */
4782 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004783 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004785 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004786
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004787 cancel_delayed_work_sync(&codec->power_work);
4788
4789 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004790 /* If the power down delayed work was cancelled above before starting,
4791 * then there is no need to go through power up here.
4792 */
4793 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004794 if (codec->power_transition < 0)
4795 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004796 return;
4797 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004798
Takashi Iwaid66fee52011-08-02 15:39:31 +02004799 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004800 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004801 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004802 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004803 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004804 spin_unlock(&codec->power_lock);
4805
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004806 if (codec->pm_down_notified) {
4807 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004808 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004809 }
4810
Takashi Iwaicb53c622007-08-10 17:21:45 +02004811 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004812
4813 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004814 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004815}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004816
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004817#define power_save(codec) \
4818 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004819
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004820/* Transition to powered down */
4821static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004822{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004823 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004825
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004826 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004827 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004828 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004829 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004830 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004831}
4832
4833/**
4834 * snd_hda_power_save - Power-up/down/sync the codec
4835 * @codec: HD-audio codec
4836 * @delta: the counter delta to change
4837 *
4838 * Change the power-up counter via @delta, and power up or down the hardware
4839 * appropriately. For the power-down, queue to the delayed action.
4840 * Passing zero to @delta means to synchronize the power state.
4841 */
4842void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4843{
4844 spin_lock(&codec->power_lock);
4845 codec->power_count += delta;
4846 trace_hda_power_count(codec);
4847 if (delta > 0)
4848 __snd_hda_power_up(codec, d3wait);
4849 else
4850 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004851 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004852}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004853EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004854
Takashi Iwaid5191e52009-11-16 14:58:17 +01004855/**
4856 * snd_hda_check_amp_list_power - Check the amp list and update the power
4857 * @codec: HD-audio codec
4858 * @check: the object containing an AMP list and the status
4859 * @nid: NID to check / update
4860 *
4861 * Check whether the given NID is in the amp list. If it's in the list,
4862 * check the current AMP status, and update the the power-status according
4863 * to the mute status.
4864 *
4865 * This function is supposed to be set or called from the check_power_status
4866 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004867 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004868int snd_hda_check_amp_list_power(struct hda_codec *codec,
4869 struct hda_loopback_check *check,
4870 hda_nid_t nid)
4871{
Takashi Iwai031024e2011-05-02 11:29:30 +02004872 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004873 int ch, v;
4874
4875 if (!check->amplist)
4876 return 0;
4877 for (p = check->amplist; p->nid; p++) {
4878 if (p->nid == nid)
4879 break;
4880 }
4881 if (!p->nid)
4882 return 0; /* nothing changed */
4883
4884 for (p = check->amplist; p->nid; p++) {
4885 for (ch = 0; ch < 2; ch++) {
4886 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4887 p->idx);
4888 if (!(v & HDA_AMP_MUTE) && v > 0) {
4889 if (!check->power_on) {
4890 check->power_on = 1;
4891 snd_hda_power_up(codec);
4892 }
4893 return 1;
4894 }
4895 }
4896 }
4897 if (check->power_on) {
4898 check->power_on = 0;
4899 snd_hda_power_down(codec);
4900 }
4901 return 0;
4902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004903EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004906/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004907 * Channel mode helper
4908 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004909
4910/**
4911 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4912 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004913int snd_hda_ch_mode_info(struct hda_codec *codec,
4914 struct snd_ctl_elem_info *uinfo,
4915 const struct hda_channel_mode *chmode,
4916 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004917{
4918 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4919 uinfo->count = 1;
4920 uinfo->value.enumerated.items = num_chmodes;
4921 if (uinfo->value.enumerated.item >= num_chmodes)
4922 uinfo->value.enumerated.item = num_chmodes - 1;
4923 sprintf(uinfo->value.enumerated.name, "%dch",
4924 chmode[uinfo->value.enumerated.item].channels);
4925 return 0;
4926}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004927EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004928
Takashi Iwaid5191e52009-11-16 14:58:17 +01004929/**
4930 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4931 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004932int snd_hda_ch_mode_get(struct hda_codec *codec,
4933 struct snd_ctl_elem_value *ucontrol,
4934 const struct hda_channel_mode *chmode,
4935 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004936 int max_channels)
4937{
4938 int i;
4939
4940 for (i = 0; i < num_chmodes; i++) {
4941 if (max_channels == chmode[i].channels) {
4942 ucontrol->value.enumerated.item[0] = i;
4943 break;
4944 }
4945 }
4946 return 0;
4947}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004948EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004949
Takashi Iwaid5191e52009-11-16 14:58:17 +01004950/**
4951 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4952 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004953int snd_hda_ch_mode_put(struct hda_codec *codec,
4954 struct snd_ctl_elem_value *ucontrol,
4955 const struct hda_channel_mode *chmode,
4956 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004957 int *max_channelsp)
4958{
4959 unsigned int mode;
4960
4961 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004962 if (mode >= num_chmodes)
4963 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004964 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004965 return 0;
4966 /* change the current channel setting */
4967 *max_channelsp = chmode[mode].channels;
4968 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004969 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004970 return 1;
4971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004972EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974/*
4975 * input MUX helper
4976 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004977
4978/**
4979 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4980 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004981int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4982 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
4984 unsigned int index;
4985
4986 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4987 uinfo->count = 1;
4988 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004989 if (!imux->num_items)
4990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 index = uinfo->value.enumerated.item;
4992 if (index >= imux->num_items)
4993 index = imux->num_items - 1;
4994 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4995 return 0;
4996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004997EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Takashi Iwaid5191e52009-11-16 14:58:17 +01004999/**
5000 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5001 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005002int snd_hda_input_mux_put(struct hda_codec *codec,
5003 const struct hda_input_mux *imux,
5004 struct snd_ctl_elem_value *ucontrol,
5005 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 unsigned int *cur_val)
5007{
5008 unsigned int idx;
5009
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005010 if (!imux->num_items)
5011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 idx = ucontrol->value.enumerated.item[0];
5013 if (idx >= imux->num_items)
5014 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005015 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005017 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5018 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 *cur_val = idx;
5020 return 1;
5021}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005022EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
5024
5025/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005026 * process kcontrol info callback of a simple string enum array
5027 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5028 */
5029int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5030 struct snd_ctl_elem_info *uinfo,
5031 int num_items, const char * const *texts)
5032{
5033 static const char * const texts_default[] = {
5034 "Disabled", "Enabled"
5035 };
5036
5037 if (!texts || !num_items) {
5038 num_items = 2;
5039 texts = texts_default;
5040 }
5041
5042 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5043 uinfo->count = 1;
5044 uinfo->value.enumerated.items = num_items;
5045 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5046 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5047 strcpy(uinfo->value.enumerated.name,
5048 texts[uinfo->value.enumerated.item]);
5049 return 0;
5050}
5051EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5052
5053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 * Multi-channel / digital-out PCM helper functions
5055 */
5056
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005057/* setup SPDIF output stream */
5058static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5059 unsigned int stream_tag, unsigned int format)
5060{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005061 struct hda_spdif_out *spdif;
5062 unsigned int curr_fmt;
5063 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005064
Laurence Darby3bef1c32012-11-03 17:00:06 +00005065 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5066 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5067 AC_VERB_GET_STREAM_FORMAT, 0);
5068 reset = codec->spdif_status_reset &&
5069 (spdif->ctls & AC_DIG1_ENABLE) &&
5070 curr_fmt != format;
5071
5072 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5073 updated */
5074 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005075 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005076 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005077 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005078 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005079 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005080 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005081 for (d = codec->slave_dig_outs; *d; d++)
5082 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5083 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005084 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005085 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005086 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005087 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005088 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005089}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005090
Takashi Iwai2f728532008-09-25 16:32:41 +02005091static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5092{
5093 snd_hda_codec_cleanup_stream(codec, nid);
5094 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005095 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005096 for (d = codec->slave_dig_outs; *d; d++)
5097 snd_hda_codec_cleanup_stream(codec, *d);
5098 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005099}
5100
Takashi Iwaid5191e52009-11-16 14:58:17 +01005101/**
5102 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5103 * @bus: HD-audio bus
5104 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005105void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5106{
5107 struct hda_codec *codec;
5108
5109 if (!bus)
5110 return;
5111 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005112 if (hda_codec_is_power_on(codec) &&
5113 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005114 codec->patch_ops.reboot_notify(codec);
5115 }
5116}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005117EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005118
Takashi Iwaid5191e52009-11-16 14:58:17 +01005119/**
5120 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005122int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5123 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
Ingo Molnar62932df2006-01-16 16:34:20 +01005125 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005126 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5127 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005128 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005130 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return 0;
5132}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005133EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Takashi Iwaid5191e52009-11-16 14:58:17 +01005135/**
5136 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5137 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005138int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5139 struct hda_multi_out *mout,
5140 unsigned int stream_tag,
5141 unsigned int format,
5142 struct snd_pcm_substream *substream)
5143{
5144 mutex_lock(&codec->spdif_mutex);
5145 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5146 mutex_unlock(&codec->spdif_mutex);
5147 return 0;
5148}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005149EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005150
Takashi Iwaid5191e52009-11-16 14:58:17 +01005151/**
5152 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5153 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005154int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5155 struct hda_multi_out *mout)
5156{
5157 mutex_lock(&codec->spdif_mutex);
5158 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5159 mutex_unlock(&codec->spdif_mutex);
5160 return 0;
5161}
5162EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5163
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164/**
5165 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005167int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5168 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
Ingo Molnar62932df2006-01-16 16:34:20 +01005170 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005172 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return 0;
5174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005175EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Takashi Iwaid5191e52009-11-16 14:58:17 +01005177/**
5178 * snd_hda_multi_out_analog_open - open analog outputs
5179 *
5180 * Open analog outputs and set up the hw-constraints.
5181 * If the digital outputs can be opened as slave, open the digital
5182 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005184int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5185 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005186 struct snd_pcm_substream *substream,
5187 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Takashi Iwai9a081602008-02-12 18:37:26 +01005189 struct snd_pcm_runtime *runtime = substream->runtime;
5190 runtime->hw.channels_max = mout->max_channels;
5191 if (mout->dig_out_nid) {
5192 if (!mout->analog_rates) {
5193 mout->analog_rates = hinfo->rates;
5194 mout->analog_formats = hinfo->formats;
5195 mout->analog_maxbps = hinfo->maxbps;
5196 } else {
5197 runtime->hw.rates = mout->analog_rates;
5198 runtime->hw.formats = mout->analog_formats;
5199 hinfo->maxbps = mout->analog_maxbps;
5200 }
5201 if (!mout->spdif_rates) {
5202 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5203 &mout->spdif_rates,
5204 &mout->spdif_formats,
5205 &mout->spdif_maxbps);
5206 }
5207 mutex_lock(&codec->spdif_mutex);
5208 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005209 if ((runtime->hw.rates & mout->spdif_rates) &&
5210 (runtime->hw.formats & mout->spdif_formats)) {
5211 runtime->hw.rates &= mout->spdif_rates;
5212 runtime->hw.formats &= mout->spdif_formats;
5213 if (mout->spdif_maxbps < hinfo->maxbps)
5214 hinfo->maxbps = mout->spdif_maxbps;
5215 } else {
5216 mout->share_spdif = 0;
5217 /* FIXME: need notify? */
5218 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005219 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005220 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5223 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5224}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005225EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Takashi Iwaid5191e52009-11-16 14:58:17 +01005227/**
5228 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5229 *
5230 * Set up the i/o for analog out.
5231 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005233int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5234 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 unsigned int stream_tag,
5236 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005237 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238{
Takashi Iwaidda14412011-05-02 11:29:30 +02005239 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005241 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 int i;
5243
Ingo Molnar62932df2006-01-16 16:34:20 +01005244 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005245 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005246 if (mout->dig_out_nid && mout->share_spdif &&
5247 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005249 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5250 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005251 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005253 setup_dig_out_stream(codec, mout->dig_out_nid,
5254 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 } else {
5256 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005257 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
5259 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005260 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
5262 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005263 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5264 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005265 if (!mout->no_share_stream &&
5266 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005268 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5269 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005270 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005271 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5272 if (!mout->no_share_stream && mout->hp_out_nid[i])
5273 snd_hda_codec_setup_stream(codec,
5274 mout->hp_out_nid[i],
5275 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005276 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005277 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005278 snd_hda_codec_setup_stream(codec,
5279 mout->extra_out_nid[i],
5280 stream_tag, 0, format);
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 /* surrounds */
5283 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005284 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005285 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5286 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005287 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005288 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5289 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 }
5291 return 0;
5292}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005293EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
Takashi Iwaid5191e52009-11-16 14:58:17 +01005295/**
5296 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005298int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5299 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Takashi Iwaidda14412011-05-02 11:29:30 +02005301 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 int i;
5303
5304 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005305 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005307 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005308 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5309 if (mout->hp_out_nid[i])
5310 snd_hda_codec_cleanup_stream(codec,
5311 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005312 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5313 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005314 snd_hda_codec_cleanup_stream(codec,
5315 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005316 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005318 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 mout->dig_out_used = 0;
5320 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005321 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 return 0;
5323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005324EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Takashi Iwai47408602012-04-20 13:06:53 +02005326/**
5327 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5328 *
5329 * Guess the suitable VREF pin bits to be set as the pin-control value.
5330 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5331 */
5332unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5333{
5334 unsigned int pincap;
5335 unsigned int oldval;
5336 oldval = snd_hda_codec_read(codec, pin, 0,
5337 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5338 pincap = snd_hda_query_pin_caps(codec, pin);
5339 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5340 /* Exception: if the default pin setup is vref50, we give it priority */
5341 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5342 return AC_PINCTL_VREF_80;
5343 else if (pincap & AC_PINCAP_VREF_50)
5344 return AC_PINCTL_VREF_50;
5345 else if (pincap & AC_PINCAP_VREF_100)
5346 return AC_PINCTL_VREF_100;
5347 else if (pincap & AC_PINCAP_VREF_GRD)
5348 return AC_PINCTL_VREF_GRD;
5349 return AC_PINCTL_VREF_HIZ;
5350}
5351EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5352
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005353/* correct the pin ctl value for matching with the pin cap */
5354unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5355 hda_nid_t pin, unsigned int val)
5356{
5357 static unsigned int cap_lists[][2] = {
5358 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5359 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5360 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5361 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5362 };
5363 unsigned int cap;
5364
5365 if (!val)
5366 return 0;
5367 cap = snd_hda_query_pin_caps(codec, pin);
5368 if (!cap)
5369 return val; /* don't know what to do... */
5370
5371 if (val & AC_PINCTL_OUT_EN) {
5372 if (!(cap & AC_PINCAP_OUT))
5373 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5374 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5375 val &= ~AC_PINCTL_HP_EN;
5376 }
5377
5378 if (val & AC_PINCTL_IN_EN) {
5379 if (!(cap & AC_PINCAP_IN))
5380 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5381 else {
5382 unsigned int vcap, vref;
5383 int i;
5384 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5385 vref = val & AC_PINCTL_VREFEN;
5386 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5387 if (vref == cap_lists[i][0] &&
5388 !(vcap & cap_lists[i][1])) {
5389 if (i == ARRAY_SIZE(cap_lists) - 1)
5390 vref = AC_PINCTL_VREF_HIZ;
5391 else
5392 vref = cap_lists[i + 1][0];
5393 }
5394 }
5395 val &= ~AC_PINCTL_VREFEN;
5396 val |= vref;
5397 }
5398 }
5399
5400 return val;
5401}
5402EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5403
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005404int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5405 unsigned int val, bool cached)
5406{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005407 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005408 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005409 if (cached)
5410 return snd_hda_codec_update_cache(codec, pin, 0,
5411 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5412 else
5413 return snd_hda_codec_write(codec, pin, 0,
5414 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5415}
5416EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5417
Takashi Iwai990061c2010-09-09 22:08:44 +02005418/**
5419 * snd_hda_add_imux_item - Add an item to input_mux
5420 *
5421 * When the same label is used already in the existing items, the number
5422 * suffix is appended to the label. This label index number is stored
5423 * to type_idx when non-NULL pointer is given.
5424 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005425int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5426 int index, int *type_idx)
5427{
5428 int i, label_idx = 0;
5429 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5430 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5431 return -EINVAL;
5432 }
5433 for (i = 0; i < imux->num_items; i++) {
5434 if (!strncmp(label, imux->items[i].label, strlen(label)))
5435 label_idx++;
5436 }
5437 if (type_idx)
5438 *type_idx = label_idx;
5439 if (label_idx > 0)
5440 snprintf(imux->items[imux->num_items].label,
5441 sizeof(imux->items[imux->num_items].label),
5442 "%s %d", label, label_idx);
5443 else
5444 strlcpy(imux->items[imux->num_items].label, label,
5445 sizeof(imux->items[imux->num_items].label));
5446 imux->items[imux->num_items].index = index;
5447 imux->num_items++;
5448 return 0;
5449}
5450EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005451
Takashi Iwai4a471b72005-12-07 13:56:29 +01005452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453#ifdef CONFIG_PM
5454/*
5455 * power management
5456 */
5457
5458/**
5459 * snd_hda_suspend - suspend the codecs
5460 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 *
5462 * Returns 0 if successful.
5463 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005464int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005466 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
Takashi Iwai0ba21762007-04-16 11:29:14 +02005468 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005469 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005470 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005471 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473 return 0;
5474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005475EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477/**
5478 * snd_hda_resume - resume the codecs
5479 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 *
5481 * Returns 0 if successful.
5482 */
5483int snd_hda_resume(struct hda_bus *bus)
5484{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005485 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Takashi Iwai0ba21762007-04-16 11:29:14 +02005487 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005488 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 return 0;
5491}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005492EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005493#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005494
5495/*
5496 * generic arrays
5497 */
5498
Takashi Iwaid5191e52009-11-16 14:58:17 +01005499/**
5500 * snd_array_new - get a new element from the given array
5501 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005502 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005503 * Get a new element from the given array. If it exceeds the
5504 * pre-allocated array size, re-allocate the array.
5505 *
5506 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005507 */
5508void *snd_array_new(struct snd_array *array)
5509{
Takashi Iwai12f17712012-10-10 08:59:14 +02005510 if (snd_BUG_ON(!array->elem_size))
5511 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005512 if (array->used >= array->alloced) {
5513 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005514 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005515 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005516 void *nlist;
5517 if (snd_BUG_ON(num >= 4096))
5518 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005519 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005520 if (!nlist)
5521 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005522 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005523 array->list = nlist;
5524 array->alloced = num;
5525 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005526 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005527}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005528EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005529
Takashi Iwaid5191e52009-11-16 14:58:17 +01005530/**
5531 * snd_array_free - free the given array elements
5532 * @array: the array object
5533 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005534void snd_array_free(struct snd_array *array)
5535{
5536 kfree(array->list);
5537 array->used = 0;
5538 array->alloced = 0;
5539 array->list = NULL;
5540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005541EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005542
Takashi Iwaid5191e52009-11-16 14:58:17 +01005543/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005544 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5545 * @pcm: PCM caps bits
5546 * @buf: the string buffer to write
5547 * @buflen: the max buffer length
5548 *
5549 * used by hda_proc.c and hda_eld.c
5550 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005551void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5552{
5553 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5554 int i, j;
5555
5556 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5557 if (pcm & (AC_SUPPCM_BITS_8 << i))
5558 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5559
5560 buf[j] = '\0'; /* necessary when j == 0 */
5561}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005562EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005563
5564MODULE_DESCRIPTION("HDA codec core");
5565MODULE_LICENSE("GPL");