blob: 19ff923b243167912744133ee407d6f1e3cb6d73 [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 Iwaia12d3e12011-04-07 15:55:15 +0200385 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200388 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200389 if (len < 0)
390 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200391 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200392}
Takashi Iwaidce20792011-06-24 14:10:28 +0200393
394/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100395 * snd_hda_get_conn_list - get connection list
396 * @codec: the HDA codec
397 * @nid: NID to parse
398 * @len: number of connection list entries
399 * @listp: the pointer to store NID list
400 *
401 * Parses the connection list of the given widget and stores the pointer
402 * to the list of NIDs.
403 *
404 * Returns the number of connections, or a negative error code.
405 *
406 * Note that the returned pointer isn't protected against the list
407 * modification. If snd_hda_override_conn_list() might be called
408 * concurrently, protect with a mutex appropriately.
409 */
410int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
411 const hda_nid_t **listp)
412{
413 bool added = false;
414
415 for (;;) {
416 int err;
417 const struct hda_conn_list *p;
418
419 /* if the connection-list is already cached, read it */
420 p = lookup_conn_list(codec, nid);
421 if (p) {
422 if (listp)
423 *listp = p->conns;
424 return p->len;
425 }
426 if (snd_BUG_ON(added))
427 return -EINVAL;
428
429 err = read_and_add_raw_conns(codec, nid);
430 if (err < 0)
431 return err;
432 added = true;
433 }
434}
435EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
436
437/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200438 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * @codec: the HDA codec
440 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200441 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * @max_conns: max. number of connections to store
443 *
444 * Parses the connection list of the given widget and stores the list
445 * of NIDs.
446 *
447 * Returns the number of connections, or a negative error code.
448 */
449int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200450 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200451{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100452 const hda_nid_t *list;
453 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200454
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100455 if (len > 0 && conn_list) {
456 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200457 snd_printk(KERN_ERR "hda_codec: "
458 "Too many connections %d for NID 0x%x\n",
459 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 return -EINVAL;
461 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200463 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200466}
467EXPORT_SYMBOL_HDA(snd_hda_get_connections);
468
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200469/**
470 * snd_hda_get_raw_connections - copy connection list without cache
471 * @codec: the HDA codec
472 * @nid: NID to parse
473 * @conn_list: connection list array
474 * @max_conns: max. number of connections to store
475 *
476 * Like snd_hda_get_connections(), copy the connection list but without
477 * checking through the connection-list cache.
478 * Currently called only from hda_proc.c, so not exported.
479 */
480int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
481 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200486 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100487 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100488 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Takashi Iwaida3cec32008-08-08 17:12:14 +0200490 if (snd_BUG_ON(!conn_list || max_conns <= 0))
491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200493 wcaps = get_wcaps(codec, nid);
494 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200495 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
496 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
499 if (parm & AC_CLIST_LONG) {
500 /* long form */
501 shift = 16;
502 num_elems = 2;
503 } else {
504 /* short form */
505 shift = 8;
506 num_elems = 4;
507 }
508 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 mask = (1 << (shift-1)) - 1;
510
Takashi Iwai0ba21762007-04-16 11:29:14 +0200511 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0; /* no connection */
513
514 if (conn_len == 1) {
515 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 parm = snd_hda_codec_read(codec, nid, 0,
517 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200518 if (parm == -1 && codec->bus->rirb_error)
519 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 conn_list[0] = parm & mask;
521 return 1;
522 }
523
524 /* multi connection */
525 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100526 prev_nid = 0;
527 for (i = 0; i < conn_len; i++) {
528 int range_val;
529 hda_nid_t val, n;
530
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200531 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100532 parm = snd_hda_codec_read(codec, nid, 0,
533 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200534 if (parm == -1 && codec->bus->rirb_error)
535 return -EIO;
536 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100538 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100539 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200540 snd_printk(KERN_WARNING "hda_codec: "
541 "invalid CONNECT_LIST verb %x[%i]:%x\n",
542 nid, i, parm);
543 return 0;
544 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100545 parm >>= shift;
546 if (range_val) {
547 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200548 if (!prev_nid || prev_nid >= val) {
549 snd_printk(KERN_WARNING "hda_codec: "
550 "invalid dep_range_val %x:%x\n",
551 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 for (n = prev_nid + 1; n <= val; n++) {
555 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200556 snd_printk(KERN_ERR "hda_codec: "
557 "Too many connections %d for NID 0x%x\n",
558 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100559 return -EINVAL;
560 }
561 conn_list[conns++] = n;
562 }
563 } else {
564 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200565 snd_printk(KERN_ERR "hda_codec: "
566 "Too many connections %d for NID 0x%x\n",
567 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100568 return -EINVAL;
569 }
570 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100572 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 return conns;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200578 * snd_hda_override_conn_list - add/modify the connection-list to cache
579 * @codec: the HDA codec
580 * @nid: NID to parse
581 * @len: number of connection list entries
582 * @list: the list of connection entries
583 *
584 * Add or modify the given connection-list to the cache. If the corresponding
585 * cache already exists, invalidate it and append a new one.
586 *
587 * Returns zero or a negative error code.
588 */
589int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
590 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100592 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200593
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100594 p = lookup_conn_list(codec, nid);
595 if (p) {
596 list_del(&p->list);
597 kfree(p);
598 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200599
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100600 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
603
604/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200605 * snd_hda_get_conn_index - get the connection index of the given NID
606 * @codec: the HDA codec
607 * @mux: NID containing the list
608 * @nid: NID to select
609 * @recursive: 1 when searching NID recursively, otherwise 0
610 *
611 * Parses the connection list of the widget @mux and checks whether the
612 * widget @nid is present. If it is, return the connection index.
613 * Otherwise it returns -1.
614 */
615int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
616 hda_nid_t nid, int recursive)
617{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200619 int i, nums;
620
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100621 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200622 for (i = 0; i < nums; i++)
623 if (conn[i] == nid)
624 return i;
625 if (!recursive)
626 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100627 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200628 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
629 return -1;
630 }
631 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200632 for (i = 0; i < nums; i++) {
633 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
634 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
635 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200636 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
637 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200638 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 return -1;
640}
641EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643/**
644 * snd_hda_queue_unsol_event - add an unsolicited event to queue
645 * @bus: the BUS
646 * @res: unsolicited event (lower 32bit of RIRB entry)
647 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
648 *
649 * Adds the given event to the queue. The events are processed in
650 * the workqueue asynchronously. Call this function in the interrupt
651 * hanlder when RIRB receives an unsolicited event.
652 *
653 * Returns 0 if successful, or a negative error code.
654 */
655int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
656{
657 struct hda_bus_unsolicited *unsol;
658 unsigned int wp;
659
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200660 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200661 unsol = bus->unsol;
662 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664
665 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
666 unsol->wp = wp;
667
668 wp <<= 1;
669 unsol->queue[wp] = res;
670 unsol->queue[wp + 1] = res_ex;
671
Takashi Iwai6acaed32009-01-12 10:09:24 +0100672 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return 0;
675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100676EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800679 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
David Howellsc4028952006-11-22 14:57:56 +0000681static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
David Howellsc4028952006-11-22 14:57:56 +0000683 struct hda_bus_unsolicited *unsol =
684 container_of(work, struct hda_bus_unsolicited, work);
685 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct hda_codec *codec;
687 unsigned int rp, caddr, res;
688
689 while (unsol->rp != unsol->wp) {
690 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
691 unsol->rp = rp;
692 rp <<= 1;
693 res = unsol->queue[rp];
694 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200695 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 continue;
697 codec = bus->caddr_tbl[caddr & 0x0f];
698 if (codec && codec->patch_ops.unsol_event)
699 codec->patch_ops.unsol_event(codec, res);
700 }
701}
702
703/*
704 * initialize unsolicited queue
705 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200706static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct hda_bus_unsolicited *unsol;
709
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100710 if (bus->unsol) /* already initialized */
711 return 0;
712
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200713 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200714 if (!unsol) {
715 snd_printk(KERN_ERR "hda_codec: "
716 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -ENOMEM;
718 }
David Howellsc4028952006-11-22 14:57:56 +0000719 INIT_WORK(&unsol->work, process_unsol_events);
720 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 bus->unsol = unsol;
722 return 0;
723}
724
725/*
726 * destructor
727 */
728static void snd_hda_codec_free(struct hda_codec *codec);
729
730static int snd_hda_bus_free(struct hda_bus *bus)
731{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200732 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Takashi Iwai0ba21762007-04-16 11:29:14 +0200734 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100736 if (bus->workq)
737 flush_workqueue(bus->workq);
738 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200740 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 snd_hda_codec_free(codec);
742 }
743 if (bus->ops.private_free)
744 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100745 if (bus->workq)
746 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 kfree(bus);
748 return 0;
749}
750
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100751static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100754 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return snd_hda_bus_free(bus);
756}
757
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200758#ifdef CONFIG_SND_HDA_HWDEP
759static int snd_hda_bus_dev_register(struct snd_device *device)
760{
761 struct hda_bus *bus = device->device_data;
762 struct hda_codec *codec;
763 list_for_each_entry(codec, &bus->codec_list, list) {
764 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100765 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200766 }
767 return 0;
768}
769#else
770#define snd_hda_bus_dev_register NULL
771#endif
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/**
774 * snd_hda_bus_new - create a HDA bus
775 * @card: the card entry
776 * @temp: the template for hda_bus information
777 * @busp: the pointer to store the created bus instance
778 *
779 * Returns 0 if successful, or a negative error code.
780 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100781int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200782 const struct hda_bus_template *temp,
783 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct hda_bus *bus;
786 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100787 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200788 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .dev_free = snd_hda_bus_dev_free,
790 };
791
Takashi Iwaida3cec32008-08-08 17:12:14 +0200792 if (snd_BUG_ON(!temp))
793 return -EINVAL;
794 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
795 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (busp)
798 *busp = NULL;
799
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200800 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (bus == NULL) {
802 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
803 return -ENOMEM;
804 }
805
806 bus->card = card;
807 bus->private_data = temp->private_data;
808 bus->pci = temp->pci;
809 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100810 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bus->ops = temp->ops;
812
Ingo Molnar62932df2006-01-16 16:34:20 +0100813 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200814 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 INIT_LIST_HEAD(&bus->codec_list);
816
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100817 snprintf(bus->workq_name, sizeof(bus->workq_name),
818 "hd-audio%d", card->number);
819 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100820 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100821 snd_printk(KERN_ERR "cannot create workqueue %s\n",
822 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100823 kfree(bus);
824 return -ENOMEM;
825 }
826
Takashi Iwai0ba21762007-04-16 11:29:14 +0200827 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
828 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 snd_hda_bus_free(bus);
830 return err;
831 }
832 if (busp)
833 *busp = bus;
834 return 0;
835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100836EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Takashi Iwai82467612007-07-27 19:15:54 +0200838#ifdef CONFIG_SND_HDA_GENERIC
839#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200840 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200841#else
842#define is_generic_config(codec) 0
843#endif
844
Takashi Iwai645f10c2008-11-28 15:07:37 +0100845#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100846#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
847#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100848#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100849#endif
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
852 * find a matching codec preset
853 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200854static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200855find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100857 struct hda_codec_preset_list *tbl;
858 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200859 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Takashi Iwai82467612007-07-27 19:15:54 +0200861 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100862 return NULL; /* use the generic parser */
863
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100864 again:
865 mutex_lock(&preset_mutex);
866 list_for_each_entry(tbl, &hda_preset_tables, list) {
867 if (!try_module_get(tbl->owner)) {
868 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
869 continue;
870 }
871 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100873 if (preset->afg && preset->afg != codec->afg)
874 continue;
875 if (preset->mfg && preset->mfg != codec->mfg)
876 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200879 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200880 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100881 preset->rev == codec->revision_id)) {
882 mutex_unlock(&preset_mutex);
883 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100887 module_put(tbl->owner);
888 }
889 mutex_unlock(&preset_mutex);
890
891 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
892 char name[32];
893 if (!mod_requested)
894 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
895 codec->vendor_id);
896 else
897 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
898 (codec->vendor_id >> 16) & 0xffff);
899 request_module(name);
900 mod_requested++;
901 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 return NULL;
904}
905
906/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200907 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200909static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 const struct hda_vendor_id *c;
912 const char *vendor = NULL;
913 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200914 char tmp[16];
915
916 if (codec->vendor_name)
917 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 for (c = hda_vendor_ids; c->id; c++) {
920 if (c->id == vendor_id) {
921 vendor = c->name;
922 break;
923 }
924 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200925 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sprintf(tmp, "Generic %04x", vendor_id);
927 vendor = tmp;
928 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200929 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
930 if (!codec->vendor_name)
931 return -ENOMEM;
932
933 get_chip_name:
934 if (codec->chip_name)
935 return 0;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200938 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
939 else {
940 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
941 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
942 }
943 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200944 return -ENOMEM;
945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200949 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100951static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200953 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 hda_nid_t nid;
955
956 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
957 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200958 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200959 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200960 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200961 case AC_GRP_AUDIO_FUNCTION:
962 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200963 codec->afg_function_id = function_id & 0xff;
964 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200965 break;
966 case AC_GRP_MODEM_FUNCTION:
967 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200968 codec->mfg_function_id = function_id & 0xff;
969 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200970 break;
971 default:
972 break;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100978 * read widget caps for each widget and store in cache
979 */
980static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
981{
982 int i;
983 hda_nid_t nid;
984
985 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
986 &codec->start_nid);
987 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200988 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100989 return -ENOMEM;
990 nid = codec->start_nid;
991 for (i = 0; i < codec->num_nodes; i++, nid++)
992 codec->wcaps[i] = snd_hda_param_read(codec, nid,
993 AC_PAR_AUDIO_WIDGET_CAP);
994 return 0;
995}
996
Takashi Iwai3be14142009-02-20 14:11:16 +0100997/* read all pin default configurations and save codec->init_pins */
998static int read_pin_defaults(struct hda_codec *codec)
999{
1000 int i;
1001 hda_nid_t nid = codec->start_nid;
1002
1003 for (i = 0; i < codec->num_nodes; i++, nid++) {
1004 struct hda_pincfg *pin;
1005 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001006 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001007 if (wid_type != AC_WID_PIN)
1008 continue;
1009 pin = snd_array_new(&codec->init_pins);
1010 if (!pin)
1011 return -ENOMEM;
1012 pin->nid = nid;
1013 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1014 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001015 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1016 AC_VERB_GET_PIN_WIDGET_CONTROL,
1017 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001018 }
1019 return 0;
1020}
1021
1022/* look up the given pin config list and return the item matching with NID */
1023static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1024 struct snd_array *array,
1025 hda_nid_t nid)
1026{
1027 int i;
1028 for (i = 0; i < array->used; i++) {
1029 struct hda_pincfg *pin = snd_array_elem(array, i);
1030 if (pin->nid == nid)
1031 return pin;
1032 }
1033 return NULL;
1034}
1035
Takashi Iwai3be14142009-02-20 14:11:16 +01001036/* set the current pin config value for the given NID.
1037 * the value is cached, and read via snd_hda_codec_get_pincfg()
1038 */
1039int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1040 hda_nid_t nid, unsigned int cfg)
1041{
1042 struct hda_pincfg *pin;
1043
Takashi Iwaib82855a2009-12-27 11:24:56 +01001044 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1045 return -EINVAL;
1046
Takashi Iwai3be14142009-02-20 14:11:16 +01001047 pin = look_up_pincfg(codec, list, nid);
1048 if (!pin) {
1049 pin = snd_array_new(list);
1050 if (!pin)
1051 return -ENOMEM;
1052 pin->nid = nid;
1053 }
1054 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001055 return 0;
1056}
1057
Takashi Iwaid5191e52009-11-16 14:58:17 +01001058/**
1059 * snd_hda_codec_set_pincfg - Override a pin default configuration
1060 * @codec: the HDA codec
1061 * @nid: NID to set the pin config
1062 * @cfg: the pin default config value
1063 *
1064 * Override a pin default configuration value in the cache.
1065 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1066 * priority than the real hardware value.
1067 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001068int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1069 hda_nid_t nid, unsigned int cfg)
1070{
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072}
1073EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1074
Takashi Iwaid5191e52009-11-16 14:58:17 +01001075/**
1076 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1077 * @codec: the HDA codec
1078 * @nid: NID to get the pin config
1079 *
1080 * Get the current pin config value of the given pin NID.
1081 * If the pincfg value is cached or overridden via sysfs or driver,
1082 * returns the cached value.
1083 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001084unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1085{
1086 struct hda_pincfg *pin;
1087
Takashi Iwai3be14142009-02-20 14:11:16 +01001088#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001089 {
1090 unsigned int cfg = 0;
1091 mutex_lock(&codec->user_mutex);
1092 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1093 if (pin)
1094 cfg = pin->cfg;
1095 mutex_unlock(&codec->user_mutex);
1096 if (cfg)
1097 return cfg;
1098 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001099#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001100 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1101 if (pin)
1102 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001103 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1104 if (pin)
1105 return pin->cfg;
1106 return 0;
1107}
1108EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1109
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001110/* remember the current pinctl target value */
1111int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1112 unsigned int val)
1113{
1114 struct hda_pincfg *pin;
1115
1116 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1117 if (!pin)
1118 return -EINVAL;
1119 pin->target = val;
1120 return 0;
1121}
1122EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1123
1124/* return the current pinctl target value */
1125int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1126{
1127 struct hda_pincfg *pin;
1128
1129 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1130 if (!pin)
1131 return 0;
1132 return pin->target;
1133}
1134EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1135
Takashi Iwai92ee6162009-12-27 11:18:59 +01001136/**
1137 * snd_hda_shutup_pins - Shut up all pins
1138 * @codec: the HDA codec
1139 *
1140 * Clear all pin controls to shup up before suspend for avoiding click noise.
1141 * The controls aren't cached so that they can be resumed properly.
1142 */
1143void snd_hda_shutup_pins(struct hda_codec *codec)
1144{
1145 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001146 /* don't shut up pins when unloading the driver; otherwise it breaks
1147 * the default pin setup at the next load of the driver
1148 */
1149 if (codec->bus->shutdown)
1150 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001151 for (i = 0; i < codec->init_pins.used; i++) {
1152 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1153 /* use read here for syncing after issuing each verb */
1154 snd_hda_codec_read(codec, pin->nid, 0,
1155 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1156 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001157 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001158}
1159EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1160
Takashi Iwai2a439522011-07-26 09:52:50 +02001161#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001162/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1163static void restore_shutup_pins(struct hda_codec *codec)
1164{
1165 int i;
1166 if (!codec->pins_shutup)
1167 return;
1168 if (codec->bus->shutdown)
1169 return;
1170 for (i = 0; i < codec->init_pins.used; i++) {
1171 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1172 snd_hda_codec_write(codec, pin->nid, 0,
1173 AC_VERB_SET_PIN_WIDGET_CONTROL,
1174 pin->ctrl);
1175 }
1176 codec->pins_shutup = 0;
1177}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001178#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001179
David Henningsson26a6cb62012-10-09 15:04:21 +02001180static void hda_jackpoll_work(struct work_struct *work)
1181{
1182 struct hda_codec *codec =
1183 container_of(work, struct hda_codec, jackpoll_work.work);
1184 if (!codec->jackpoll_interval)
1185 return;
1186
1187 snd_hda_jack_set_dirty_all(codec);
1188 snd_hda_jack_poll_all(codec);
1189 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1190 codec->jackpoll_interval);
1191}
1192
Takashi Iwai01751f52007-08-10 16:59:39 +02001193static void init_hda_cache(struct hda_cache_rec *cache,
1194 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001195static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001196
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001197/* release all pincfg lists */
1198static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001199{
Takashi Iwai346ff702009-02-23 09:42:57 +01001200 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001201#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001202 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001203#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001204 snd_array_free(&codec->init_pins);
1205}
1206
Takashi Iwai54d17402005-11-21 16:33:22 +01001207/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001208 * audio-converter setup caches
1209 */
1210struct hda_cvt_setup {
1211 hda_nid_t nid;
1212 u8 stream_tag;
1213 u8 channel_id;
1214 u16 format_id;
1215 unsigned char active; /* cvt is currently used */
1216 unsigned char dirty; /* setups should be cleared */
1217};
1218
1219/* get or create a cache entry for the given audio converter NID */
1220static struct hda_cvt_setup *
1221get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1222{
1223 struct hda_cvt_setup *p;
1224 int i;
1225
1226 for (i = 0; i < codec->cvt_setups.used; i++) {
1227 p = snd_array_elem(&codec->cvt_setups, i);
1228 if (p->nid == nid)
1229 return p;
1230 }
1231 p = snd_array_new(&codec->cvt_setups);
1232 if (p)
1233 p->nid = nid;
1234 return p;
1235}
1236
1237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * codec destructor
1239 */
1240static void snd_hda_codec_free(struct hda_codec *codec)
1241{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001242 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001244 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001245 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001246 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001247#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001248 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001249 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001252 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001253 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001254 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001255 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001256 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 codec->bus->caddr_tbl[codec->addr] = NULL;
1258 if (codec->patch_ops.free)
1259 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001260#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001261 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001262 hda_call_pm_notify(codec->bus, false);
1263#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001264 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001265 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001266 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001267 kfree(codec->vendor_name);
1268 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001269 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001270 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 kfree(codec);
1272}
1273
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001274static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1275 hda_nid_t fg, unsigned int power_state);
1276
Takashi Iwaid8193872012-08-31 07:54:38 -07001277static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001278 unsigned int power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001279static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
1280 unsigned int power_state);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/**
1283 * snd_hda_codec_new - create a HDA codec
1284 * @bus: the bus to assign
1285 * @codec_addr: the codec address
1286 * @codecp: the pointer to store the generated codec
1287 *
1288 * Returns 0 if successful, or a negative error code.
1289 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001290int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001291 unsigned int codec_addr,
1292 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001295 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001296 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int err;
1298
Takashi Iwaida3cec32008-08-08 17:12:14 +02001299 if (snd_BUG_ON(!bus))
1300 return -EINVAL;
1301 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001305 snd_printk(KERN_ERR "hda_codec: "
1306 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EBUSY;
1308 }
1309
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001310 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (codec == NULL) {
1312 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1313 return -ENOMEM;
1314 }
1315
1316 codec->bus = bus;
1317 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001318 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001319 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001320 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001321 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001322 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001323 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1324 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001325 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001326 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001327 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001328 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001329 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001330 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001331 INIT_LIST_HEAD(&codec->conn_list);
1332
David Henningsson26a6cb62012-10-09 15:04:21 +02001333 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Takashi Iwai83012a72012-08-24 18:38:08 +02001335#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001336 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001337 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1338 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1339 * the caller has to power down appropriatley after initialization
1340 * phase.
1341 */
1342 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001343 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001344#endif
1345
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001346 if (codec->bus->modelname) {
1347 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1348 if (!codec->modelname) {
1349 snd_hda_codec_free(codec);
1350 return -ENODEV;
1351 }
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 list_add_tail(&codec->list, &bus->codec_list);
1355 bus->caddr_tbl[codec_addr] = codec;
1356
Takashi Iwai0ba21762007-04-16 11:29:14 +02001357 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1358 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001359 if (codec->vendor_id == -1)
1360 /* read again, hopefully the access method was corrected
1361 * in the last read...
1362 */
1363 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1364 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001365 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1366 AC_PAR_SUBSYSTEM_ID);
1367 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1368 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001370 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001371 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001372 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001373 err = -ENODEV;
1374 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
Takashi Iwaid8193872012-08-31 07:54:38 -07001377 fg = codec->afg ? codec->afg : codec->mfg;
1378 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001379 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001380 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001381 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001382 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001383 err = read_pin_defaults(codec);
1384 if (err < 0)
1385 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001386
Takashi Iwai0ba21762007-04-16 11:29:14 +02001387 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001389 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001390 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001391 }
1392
Takashi Iwai83012a72012-08-24 18:38:08 +02001393#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001394 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001395 AC_PWRST_CLKSTOP);
1396 if (!codec->d3_stop_clk)
1397 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001398#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001399 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001400 AC_PWRST_EPSS);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001401 codec->power_filter = default_power_filter;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001402
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001403 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001404 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001405
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001406 snd_hda_codec_proc_new(codec);
1407
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001408 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001409
1410 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1411 codec->subsystem_id, codec->revision_id);
1412 snd_component_add(codec->bus->card, component);
1413
1414 if (codecp)
1415 *codecp = codec;
1416 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001417
1418 error:
1419 snd_hda_codec_free(codec);
1420 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001421}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001422EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001423
Takashi Iwaid5191e52009-11-16 14:58:17 +01001424/**
1425 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1426 * @codec: the HDA codec
1427 *
1428 * Start parsing of the given codec tree and (re-)initialize the whole
1429 * patch instance.
1430 *
1431 * Returns 0 if successful or a negative error code.
1432 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001433int snd_hda_codec_configure(struct hda_codec *codec)
1434{
1435 int err;
1436
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001437 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001438 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001439 err = get_codec_name(codec);
1440 if (err < 0)
1441 return err;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Takashi Iwai82467612007-07-27 19:15:54 +02001444 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001446 goto patched;
1447 }
Takashi Iwai82467612007-07-27 19:15:54 +02001448 if (codec->preset && codec->preset->patch) {
1449 err = codec->preset->patch(codec);
1450 goto patched;
1451 }
1452
1453 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001454 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001455 if (err < 0)
1456 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001457
1458 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001459 if (!err && codec->patch_ops.unsol_event)
1460 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001461 /* audio codec should override the mixer name */
1462 if (!err && (codec->afg || !*codec->bus->card->mixername))
1463 snprintf(codec->bus->card->mixername,
1464 sizeof(codec->bus->card->mixername),
1465 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001468EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Takashi Iwaied360812012-08-08 17:12:52 +02001470/* update the stream-id if changed */
1471static void update_pcm_stream_id(struct hda_codec *codec,
1472 struct hda_cvt_setup *p, hda_nid_t nid,
1473 u32 stream_tag, int channel_id)
1474{
1475 unsigned int oldval, newval;
1476
1477 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1478 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1479 newval = (stream_tag << 4) | channel_id;
1480 if (oldval != newval)
1481 snd_hda_codec_write(codec, nid, 0,
1482 AC_VERB_SET_CHANNEL_STREAMID,
1483 newval);
1484 p->stream_tag = stream_tag;
1485 p->channel_id = channel_id;
1486 }
1487}
1488
1489/* update the format-id if changed */
1490static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1491 hda_nid_t nid, int format)
1492{
1493 unsigned int oldval;
1494
1495 if (p->format_id != format) {
1496 oldval = snd_hda_codec_read(codec, nid, 0,
1497 AC_VERB_GET_STREAM_FORMAT, 0);
1498 if (oldval != format) {
1499 msleep(1);
1500 snd_hda_codec_write(codec, nid, 0,
1501 AC_VERB_SET_STREAM_FORMAT,
1502 format);
1503 }
1504 p->format_id = format;
1505 }
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/**
1509 * snd_hda_codec_setup_stream - set up the codec for streaming
1510 * @codec: the CODEC to set up
1511 * @nid: the NID to set up
1512 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1513 * @channel_id: channel id to pass, zero based.
1514 * @format: stream format.
1515 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001516void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1517 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int channel_id, int format)
1519{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001520 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001522 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001523 int i;
1524
Takashi Iwai0ba21762007-04-16 11:29:14 +02001525 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001526 return;
1527
Takashi Iwai0ba21762007-04-16 11:29:14 +02001528 snd_printdd("hda_codec_setup_stream: "
1529 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001531 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001532 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001533 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001534
1535 if (codec->pcm_format_first)
1536 update_pcm_format(codec, p, nid, format);
1537 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1538 if (!codec->pcm_format_first)
1539 update_pcm_format(codec, p, nid, format);
1540
Takashi Iwaieb541332010-08-06 13:48:11 +02001541 p->active = 1;
1542 p->dirty = 0;
1543
1544 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001545 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001546 list_for_each_entry(c, &codec->bus->codec_list, list) {
1547 for (i = 0; i < c->cvt_setups.used; i++) {
1548 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001549 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001550 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001551 p->dirty = 1;
1552 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001555EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Takashi Iwaif0cea792010-08-13 11:56:53 +02001557static void really_cleanup_stream(struct hda_codec *codec,
1558 struct hda_cvt_setup *q);
1559
Takashi Iwaid5191e52009-11-16 14:58:17 +01001560/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001561 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001562 * @codec: the CODEC to clean up
1563 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001564 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001565 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001566void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1567 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001568{
Takashi Iwaieb541332010-08-06 13:48:11 +02001569 struct hda_cvt_setup *p;
1570
Takashi Iwai888afa12008-03-18 09:57:50 +01001571 if (!nid)
1572 return;
1573
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001574 if (codec->no_sticky_stream)
1575 do_now = 1;
1576
Takashi Iwai888afa12008-03-18 09:57:50 +01001577 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001578 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001579 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001580 /* here we just clear the active flag when do_now isn't set;
1581 * actual clean-ups will be done later in
1582 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1583 */
1584 if (do_now)
1585 really_cleanup_stream(codec, p);
1586 else
1587 p->active = 0;
1588 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001589}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001590EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001591
Takashi Iwaieb541332010-08-06 13:48:11 +02001592static void really_cleanup_stream(struct hda_codec *codec,
1593 struct hda_cvt_setup *q)
1594{
1595 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001596 if (q->stream_tag || q->channel_id)
1597 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1598 if (q->format_id)
1599 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1600);
Takashi Iwaieb541332010-08-06 13:48:11 +02001601 memset(q, 0, sizeof(*q));
1602 q->nid = nid;
1603}
1604
1605/* clean up the all conflicting obsolete streams */
1606static void purify_inactive_streams(struct hda_codec *codec)
1607{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001608 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001609 int i;
1610
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001611 list_for_each_entry(c, &codec->bus->codec_list, list) {
1612 for (i = 0; i < c->cvt_setups.used; i++) {
1613 struct hda_cvt_setup *p;
1614 p = snd_array_elem(&c->cvt_setups, i);
1615 if (p->dirty)
1616 really_cleanup_stream(c, p);
1617 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001618 }
1619}
1620
Takashi Iwai2a439522011-07-26 09:52:50 +02001621#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001622/* clean up all streams; called from suspend */
1623static void hda_cleanup_all_streams(struct hda_codec *codec)
1624{
1625 int i;
1626
1627 for (i = 0; i < codec->cvt_setups.used; i++) {
1628 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1629 if (p->stream_tag)
1630 really_cleanup_stream(codec, p);
1631 }
1632}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001633#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635/*
1636 * amp access functions
1637 */
1638
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001639/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001640#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001641#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001642#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1643#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001645#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001648static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001649 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Takashi Iwai01751f52007-08-10 16:59:39 +02001651 memset(cache, 0, sizeof(*cache));
1652 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001653 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001654}
1655
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001656static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001657{
Takashi Iwai603c4012008-07-30 15:01:44 +02001658 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001662static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwai01751f52007-08-10 16:59:39 +02001664 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1665 u16 cur = cache->hash[idx];
1666 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001669 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (info->key == key)
1671 return info;
1672 cur = info->next;
1673 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001674 return NULL;
1675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001677/* query the hash. allocate an entry if not found. */
1678static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1679 u32 key)
1680{
1681 struct hda_cache_head *info = get_hash(cache, key);
1682 if (!info) {
1683 u16 idx, cur;
1684 /* add a new hash entry */
1685 info = snd_array_new(&cache->buf);
1686 if (!info)
1687 return NULL;
1688 cur = snd_array_index(&cache->buf, info);
1689 info->key = key;
1690 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001691 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001692 idx = key % (u16)ARRAY_SIZE(cache->hash);
1693 info->next = cache->hash[idx];
1694 cache->hash[idx] = cur;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return info;
1697}
1698
Takashi Iwai01751f52007-08-10 16:59:39 +02001699/* query and allocate an amp hash entry */
1700static inline struct hda_amp_info *
1701get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1702{
1703 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1704}
1705
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001706/* overwrite the value with the key in the caps hash */
1707static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1708{
1709 struct hda_amp_info *info;
1710
1711 mutex_lock(&codec->hash_mutex);
1712 info = get_alloc_amp_hash(codec, key);
1713 if (!info) {
1714 mutex_unlock(&codec->hash_mutex);
1715 return -EINVAL;
1716 }
1717 info->amp_caps = val;
1718 info->head.val |= INFO_AMP_CAPS;
1719 mutex_unlock(&codec->hash_mutex);
1720 return 0;
1721}
1722
1723/* query the value from the caps hash; if not found, fetch the current
1724 * value from the given function and store in the hash
1725 */
1726static unsigned int
1727query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1728 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1729{
1730 struct hda_amp_info *info;
1731 unsigned int val;
1732
1733 mutex_lock(&codec->hash_mutex);
1734 info = get_alloc_amp_hash(codec, key);
1735 if (!info) {
1736 mutex_unlock(&codec->hash_mutex);
1737 return 0;
1738 }
1739 if (!(info->head.val & INFO_AMP_CAPS)) {
1740 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1741 val = func(codec, nid, dir);
1742 write_caps_hash(codec, key, val);
1743 } else {
1744 val = info->amp_caps;
1745 mutex_unlock(&codec->hash_mutex);
1746 }
1747 return val;
1748}
1749
1750static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1751 int direction)
1752{
1753 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1754 nid = codec->afg;
1755 return snd_hda_param_read(codec, nid,
1756 direction == HDA_OUTPUT ?
1757 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1758}
1759
Takashi Iwaid5191e52009-11-16 14:58:17 +01001760/**
1761 * query_amp_caps - query AMP capabilities
1762 * @codec: the HD-auio codec
1763 * @nid: the NID to query
1764 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1765 *
1766 * Query AMP capabilities for the given widget and direction.
1767 * Returns the obtained capability bits.
1768 *
1769 * When cap bits have been already read, this doesn't read again but
1770 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001772u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 return query_caps_hash(codec, nid, direction,
1775 HDA_HASH_KEY(nid, direction, 0),
1776 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001778EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Takashi Iwaid5191e52009-11-16 14:58:17 +01001780/**
1781 * snd_hda_override_amp_caps - Override the AMP capabilities
1782 * @codec: the CODEC to clean up
1783 * @nid: the NID to clean up
1784 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1785 * @caps: the capability bits to set
1786 *
1787 * Override the cached AMP caps bits value by the given one.
1788 * This function is useful if the driver needs to adjust the AMP ranges,
1789 * e.g. limit to 0dB, etc.
1790 *
1791 * Returns zero if successful or a negative error code.
1792 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001793int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1794 unsigned int caps)
1795{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001796 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001798EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001799
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001800static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1801 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001802{
1803 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1804}
1805
Takashi Iwaid5191e52009-11-16 14:58:17 +01001806/**
1807 * snd_hda_query_pin_caps - Query PIN capabilities
1808 * @codec: the HD-auio codec
1809 * @nid: the NID to query
1810 *
1811 * Query PIN capabilities for the given widget.
1812 * Returns the obtained capability bits.
1813 *
1814 * When cap bits have been already read, this doesn't read again but
1815 * returns the cached value.
1816 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001817u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1818{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001819 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001820 read_pin_cap);
1821}
Takashi Iwai1327a322009-03-23 13:07:47 +01001822EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1823
Wu Fengguang864f92b2009-11-18 12:38:02 +08001824/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001825 * snd_hda_override_pin_caps - Override the pin capabilities
1826 * @codec: the CODEC
1827 * @nid: the NID to override
1828 * @caps: the capability bits to set
1829 *
1830 * Override the cached PIN capabilitiy bits value by the given one.
1831 *
1832 * Returns zero if successful or a negative error code.
1833 */
1834int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1835 unsigned int caps)
1836{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001837 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001838}
1839EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1840
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001841/* read or sync the hash value with the current value;
1842 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001844static struct hda_amp_info *
1845update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001846 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001848 struct hda_amp_info *info;
1849 unsigned int parm, val = 0;
1850 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001852 retry:
1853 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1854 if (!info)
1855 return NULL;
1856 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1857 if (!val_read) {
1858 mutex_unlock(&codec->hash_mutex);
1859 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1860 parm |= direction == HDA_OUTPUT ?
1861 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1862 parm |= index;
1863 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001864 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001865 val &= 0xff;
1866 val_read = true;
1867 mutex_lock(&codec->hash_mutex);
1868 goto retry;
1869 }
1870 info->vol[ch] = val;
1871 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001872 } else if (init_only)
1873 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001874 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001878 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001880static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001881 hda_nid_t nid, int ch, int direction, int index,
1882 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 u32 parm;
1885
1886 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1887 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1888 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001889 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1890 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001891 ; /* set the zero value as a fake mute */
1892 else
1893 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1895}
1896
Takashi Iwaid5191e52009-11-16 14:58:17 +01001897/**
1898 * snd_hda_codec_amp_read - Read AMP value
1899 * @codec: HD-audio codec
1900 * @nid: NID to read the AMP value
1901 * @ch: channel (left=0 or right=1)
1902 * @direction: #HDA_INPUT or #HDA_OUTPUT
1903 * @index: the index value (only for input direction)
1904 *
1905 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
Takashi Iwai834be882006-03-01 14:16:17 +01001907int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1908 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001910 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001911 unsigned int val = 0;
1912
1913 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001914 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001915 if (info)
1916 val = info->vol[ch];
1917 mutex_unlock(&codec->hash_mutex);
1918 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001920EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Takashi Iwai280e57d2012-12-14 10:32:21 +01001922static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1923 int direction, int idx, int mask, int val,
1924 bool init_only)
1925{
1926 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001927 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001928 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001929
1930 if (snd_BUG_ON(mask & ~0xff))
1931 mask &= 0xff;
1932 val &= mask;
1933
1934 mutex_lock(&codec->hash_mutex);
1935 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1936 if (!info) {
1937 mutex_unlock(&codec->hash_mutex);
1938 return 0;
1939 }
1940 val |= info->vol[ch] & ~mask;
1941 if (info->vol[ch] == val) {
1942 mutex_unlock(&codec->hash_mutex);
1943 return 0;
1944 }
1945 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001946 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001947 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001948 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001949 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001950 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001951 return 1;
1952}
1953
Takashi Iwaid5191e52009-11-16 14:58:17 +01001954/**
1955 * snd_hda_codec_amp_update - update the AMP value
1956 * @codec: HD-audio codec
1957 * @nid: NID to read the AMP value
1958 * @ch: channel (left=0 or right=1)
1959 * @direction: #HDA_INPUT or #HDA_OUTPUT
1960 * @idx: the index value (only for input direction)
1961 * @mask: bit mask to set
1962 * @val: the bits value to set
1963 *
1964 * Update the AMP value with a bit mask.
1965 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001966 */
Takashi Iwai834be882006-03-01 14:16:17 +01001967int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1968 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001972EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Takashi Iwaid5191e52009-11-16 14:58:17 +01001974/**
1975 * snd_hda_codec_amp_stereo - update the AMP stereo values
1976 * @codec: HD-audio codec
1977 * @nid: NID to read the AMP value
1978 * @direction: #HDA_INPUT or #HDA_OUTPUT
1979 * @idx: the index value (only for input direction)
1980 * @mask: bit mask to set
1981 * @val: the bits value to set
1982 *
1983 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1984 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001985 */
1986int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1987 int direction, int idx, int mask, int val)
1988{
1989 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001990
1991 if (snd_BUG_ON(mask & ~0xff))
1992 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001993 for (ch = 0; ch < 2; ch++)
1994 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1995 idx, mask, val);
1996 return ret;
1997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001998EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001999
Takashi Iwai280e57d2012-12-14 10:32:21 +01002000/* Works like snd_hda_codec_amp_update() but it writes the value only at
2001 * the first access. If the amp was already initialized / updated beforehand,
2002 * this does nothing.
2003 */
2004int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2005 int dir, int idx, int mask, int val)
2006{
2007 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2008}
2009EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2010
2011int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2012 int dir, int idx, int mask, int val)
2013{
2014 int ch, ret = 0;
2015
2016 if (snd_BUG_ON(mask & ~0xff))
2017 mask &= 0xff;
2018 for (ch = 0; ch < 2; ch++)
2019 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2020 idx, mask, val);
2021 return ret;
2022}
2023EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2024
Takashi Iwaid5191e52009-11-16 14:58:17 +01002025/**
2026 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2027 * @codec: HD-audio codec
2028 *
2029 * Resume the all amp commands from the cache.
2030 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002031void snd_hda_codec_resume_amp(struct hda_codec *codec)
2032{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002033 int i;
2034
Takashi Iwaic370dd62012-12-13 18:30:04 +01002035 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002036 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002037 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2038 struct hda_amp_info *buffer;
2039 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002040 hda_nid_t nid;
2041 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002042 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002043
2044 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002045 if (!buffer->head.dirty)
2046 continue;
2047 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002048 info = *buffer;
2049 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002050 if (!key)
2051 continue;
2052 nid = key & 0xff;
2053 idx = (key >> 16) & 0xff;
2054 dir = (key >> 24) & 0xff;
2055 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002056 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002057 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002058 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002059 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2060 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002061 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002062 }
2063 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002064 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002066EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002068static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2069 unsigned int ofs)
2070{
2071 u32 caps = query_amp_caps(codec, nid, dir);
2072 /* get num steps */
2073 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2074 if (ofs < caps)
2075 caps -= ofs;
2076 return caps;
2077}
2078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2081 *
2082 * The control element is supposed to have the private_value field
2083 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2084 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002085int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2089 u16 nid = get_amp_nid(kcontrol);
2090 u8 chs = get_amp_channels(kcontrol);
2091 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002092 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002094 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2095 uinfo->count = chs == 3 ? 2 : 1;
2096 uinfo->value.integer.min = 0;
2097 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2098 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002099 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002100 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2101 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return -EINVAL;
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
2105}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002106EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002108
2109static inline unsigned int
2110read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2111 int ch, int dir, int idx, unsigned int ofs)
2112{
2113 unsigned int val;
2114 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2115 val &= HDA_AMP_VOLMASK;
2116 if (val >= ofs)
2117 val -= ofs;
2118 else
2119 val = 0;
2120 return val;
2121}
2122
2123static inline int
2124update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2125 int ch, int dir, int idx, unsigned int ofs,
2126 unsigned int val)
2127{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002128 unsigned int maxval;
2129
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002130 if (val > 0)
2131 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002132 /* ofs = 0: raw max value */
2133 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002134 if (val > maxval)
2135 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002136 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2137 HDA_AMP_VOLMASK, val);
2138}
2139
Takashi Iwaid5191e52009-11-16 14:58:17 +01002140/**
2141 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2142 *
2143 * The control element is supposed to have the private_value field
2144 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2145 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002146int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2147 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2150 hda_nid_t nid = get_amp_nid(kcontrol);
2151 int chs = get_amp_channels(kcontrol);
2152 int dir = get_amp_direction(kcontrol);
2153 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002154 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 long *valp = ucontrol->value.integer.value;
2156
2157 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002158 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002160 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return 0;
2162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002163EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165/**
2166 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2167 *
2168 * The control element is supposed to have the private_value field
2169 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002171int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 hda_nid_t nid = get_amp_nid(kcontrol);
2176 int chs = get_amp_channels(kcontrol);
2177 int dir = get_amp_direction(kcontrol);
2178 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002179 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 long *valp = ucontrol->value.integer.value;
2181 int change = 0;
2182
Takashi Iwaicb53c622007-08-10 17:21:45 +02002183 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002184 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002185 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002186 valp++;
2187 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002188 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002189 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002190 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return change;
2192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002193EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195/**
2196 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2197 *
2198 * The control element is supposed to have the private_value field
2199 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2200 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002201int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2202 unsigned int size, unsigned int __user *_tlv)
2203{
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 hda_nid_t nid = get_amp_nid(kcontrol);
2206 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002207 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002208 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002209 u32 caps, val1, val2;
2210
2211 if (size < 4 * sizeof(unsigned int))
2212 return -ENOMEM;
2213 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002214 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2215 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002216 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002217 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002218 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002219 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002220 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002221 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2222 return -EFAULT;
2223 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2224 return -EFAULT;
2225 if (put_user(val1, _tlv + 2))
2226 return -EFAULT;
2227 if (put_user(val2, _tlv + 3))
2228 return -EFAULT;
2229 return 0;
2230}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002231EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002232
Takashi Iwaid5191e52009-11-16 14:58:17 +01002233/**
2234 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2235 * @codec: HD-audio codec
2236 * @nid: NID of a reference widget
2237 * @dir: #HDA_INPUT or #HDA_OUTPUT
2238 * @tlv: TLV data to be stored, at least 4 elements
2239 *
2240 * Set (static) TLV data for a virtual master volume using the AMP caps
2241 * obtained from the reference NID.
2242 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002243 */
2244void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2245 unsigned int *tlv)
2246{
2247 u32 caps;
2248 int nums, step;
2249
2250 caps = query_amp_caps(codec, nid, dir);
2251 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2252 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2253 step = (step + 1) * 25;
2254 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2255 tlv[1] = 2 * sizeof(unsigned int);
2256 tlv[2] = -nums * step;
2257 tlv[3] = step;
2258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002259EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002260
2261/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002262static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002263find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002264{
2265 struct snd_ctl_elem_id id;
2266 memset(&id, 0, sizeof(id));
2267 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002268 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002269 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002270 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2271 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002272 strcpy(id.name, name);
2273 return snd_ctl_find_id(codec->bus->card, &id);
2274}
2275
Takashi Iwaid5191e52009-11-16 14:58:17 +01002276/**
2277 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2278 * @codec: HD-audio codec
2279 * @name: ctl id name string
2280 *
2281 * Get the control element with the given id string and IFACE_MIXER.
2282 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002283struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2284 const char *name)
2285{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002286 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002288EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002289
Takashi Iwaidcda5802012-10-12 17:24:51 +02002290static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2291 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002292{
2293 int idx;
2294 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002295 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002296 return idx;
2297 }
2298 return -EBUSY;
2299}
2300
Takashi Iwaid5191e52009-11-16 14:58:17 +01002301/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002302 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002303 * @codec: HD-audio codec
2304 * @nid: corresponding NID (optional)
2305 * @kctl: the control element to assign
2306 *
2307 * Add the given control element to an array inside the codec instance.
2308 * All control elements belonging to a codec are supposed to be added
2309 * by this function so that a proper clean-up works at the free or
2310 * reconfiguration time.
2311 *
2312 * If non-zero @nid is passed, the NID is assigned to the control element.
2313 * The assignment is shown in the codec proc file.
2314 *
2315 * snd_hda_ctl_add() checks the control subdev id field whether
2316 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002317 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2318 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002319 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002320int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2321 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002322{
2323 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002324 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002325 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002326
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002327 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002328 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002329 if (nid == 0)
2330 nid = get_amp_nid_(kctl->private_value);
2331 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002332 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2333 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002334 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002335 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002336 err = snd_ctl_add(codec->bus->card, kctl);
2337 if (err < 0)
2338 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002339 item = snd_array_new(&codec->mixers);
2340 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002341 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002342 item->kctl = kctl;
2343 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002344 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002345 return 0;
2346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002347EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002350 * snd_hda_add_nid - Assign a NID to a control element
2351 * @codec: HD-audio codec
2352 * @nid: corresponding NID (optional)
2353 * @kctl: the control element to assign
2354 * @index: index to kctl
2355 *
2356 * Add the given control element to an array inside the codec instance.
2357 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2358 * NID:KCTL mapping - for example "Capture Source" selector.
2359 */
2360int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2361 unsigned int index, hda_nid_t nid)
2362{
2363 struct hda_nid_item *item;
2364
2365 if (nid > 0) {
2366 item = snd_array_new(&codec->nids);
2367 if (!item)
2368 return -ENOMEM;
2369 item->kctl = kctl;
2370 item->index = index;
2371 item->nid = nid;
2372 return 0;
2373 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002374 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2375 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002376 return -EINVAL;
2377}
2378EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2379
2380/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002381 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2382 * @codec: HD-audio codec
2383 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002384void snd_hda_ctls_clear(struct hda_codec *codec)
2385{
2386 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002387 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002388 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002389 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002390 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002391 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002392}
2393
Takashi Iwaia65d6292009-02-23 16:57:04 +01002394/* pseudo device locking
2395 * toggle card->shutdown to allow/disallow the device access (as a hack)
2396 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002397int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002398{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002399 struct snd_card *card = bus->card;
2400 struct hda_codec *codec;
2401
Takashi Iwaia65d6292009-02-23 16:57:04 +01002402 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002403 if (card->shutdown)
2404 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002405 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002406 if (!list_empty(&card->ctl_files))
2407 goto err_clear;
2408
2409 list_for_each_entry(codec, &bus->codec_list, list) {
2410 int pcm;
2411 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2412 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2413 if (!cpcm->pcm)
2414 continue;
2415 if (cpcm->pcm->streams[0].substream_opened ||
2416 cpcm->pcm->streams[1].substream_opened)
2417 goto err_clear;
2418 }
2419 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002420 spin_unlock(&card->files_lock);
2421 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002422
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 err_clear:
2424 card->shutdown = 0;
2425 err_unlock:
2426 spin_unlock(&card->files_lock);
2427 return -EINVAL;
2428}
2429EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2430
2431void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002432{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002433 struct snd_card *card = bus->card;
2434
2435 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002436 spin_lock(&card->files_lock);
2437 card->shutdown = 0;
2438 spin_unlock(&card->files_lock);
2439}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002440EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002441
Takashi Iwaid5191e52009-11-16 14:58:17 +01002442/**
2443 * snd_hda_codec_reset - Clear all objects assigned to the codec
2444 * @codec: HD-audio codec
2445 *
2446 * This frees the all PCM and control elements assigned to the codec, and
2447 * clears the caches and restores the pin default configurations.
2448 *
2449 * When a device is being used, it returns -EBSY. If successfully freed,
2450 * returns zero.
2451 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002452int snd_hda_codec_reset(struct hda_codec *codec)
2453{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002454 struct hda_bus *bus = codec->bus;
2455 struct snd_card *card = bus->card;
2456 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002457
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002458 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002459 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002460
2461 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002462 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002463#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002464 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002465 codec->power_on = 0;
2466 codec->power_transition = 0;
2467 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002468 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002469#endif
2470 snd_hda_ctls_clear(codec);
2471 /* relase PCMs */
2472 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002473 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002474 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002475 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002476 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002477 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002478 }
2479 if (codec->patch_ops.free)
2480 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002481 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002482 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002483 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002484 codec->spec = NULL;
2485 free_hda_cache(&codec->amp_cache);
2486 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002487 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2488 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002489 /* free only driver_pins so that init_pins + user_pins are restored */
2490 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002491 snd_array_free(&codec->cvt_setups);
2492 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002493 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002494 codec->num_pcms = 0;
2495 codec->pcm_info = NULL;
2496 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002497 codec->slave_dig_outs = NULL;
2498 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002499 module_put(codec->owner);
2500 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002501
2502 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002503 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002504 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002505}
2506
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002507typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2508
2509/* apply the function to all matching slave ctls in the mixer list */
2510static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002511 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002512{
2513 struct hda_nid_item *items;
2514 const char * const *s;
2515 int i, err;
2516
2517 items = codec->mixers.list;
2518 for (i = 0; i < codec->mixers.used; i++) {
2519 struct snd_kcontrol *sctl = items[i].kctl;
2520 if (!sctl || !sctl->id.name ||
2521 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2522 continue;
2523 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002524 char tmpname[sizeof(sctl->id.name)];
2525 const char *name = *s;
2526 if (suffix) {
2527 snprintf(tmpname, sizeof(tmpname), "%s %s",
2528 name, suffix);
2529 name = tmpname;
2530 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002531 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002532 err = func(data, sctl);
2533 if (err)
2534 return err;
2535 break;
2536 }
2537 }
2538 }
2539 return 0;
2540}
2541
2542static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2543{
2544 return 1;
2545}
2546
Takashi Iwai18478e82012-03-09 17:51:10 +01002547/* guess the value corresponding to 0dB */
2548static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2549{
2550 int _tlv[4];
2551 const int *tlv = NULL;
2552 int val = -1;
2553
2554 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2555 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2556 mm_segment_t fs = get_fs();
2557 set_fs(get_ds());
2558 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2559 tlv = _tlv;
2560 set_fs(fs);
2561 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2562 tlv = kctl->tlv.p;
2563 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2564 val = -tlv[2] / tlv[3];
2565 return val;
2566}
2567
2568/* call kctl->put with the given value(s) */
2569static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2570{
2571 struct snd_ctl_elem_value *ucontrol;
2572 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2573 if (!ucontrol)
2574 return -ENOMEM;
2575 ucontrol->value.integer.value[0] = val;
2576 ucontrol->value.integer.value[1] = val;
2577 kctl->put(kctl, ucontrol);
2578 kfree(ucontrol);
2579 return 0;
2580}
2581
2582/* initialize the slave volume with 0dB */
2583static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2584{
2585 int offset = get_kctl_0dB_offset(slave);
2586 if (offset > 0)
2587 put_kctl_with_value(slave, offset);
2588 return 0;
2589}
2590
2591/* unmute the slave */
2592static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2593{
2594 return put_kctl_with_value(slave, 1);
2595}
2596
Takashi Iwaid5191e52009-11-16 14:58:17 +01002597/**
2598 * snd_hda_add_vmaster - create a virtual master control and add slaves
2599 * @codec: HD-audio codec
2600 * @name: vmaster control name
2601 * @tlv: TLV data (optional)
2602 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002603 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002604 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002605 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002606 *
2607 * Create a virtual master control with the given name. The TLV data
2608 * must be either NULL or a valid data.
2609 *
2610 * @slaves is a NULL-terminated array of strings, each of which is a
2611 * slave control name. All controls with these names are assigned to
2612 * the new virtual master control.
2613 *
2614 * This function returns zero if successful or a negative error code.
2615 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002616int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002617 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002618 const char *suffix, bool init_slave_vol,
2619 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002620{
2621 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002622 int err;
2623
Takashi Iwai29e58532012-03-12 12:25:03 +01002624 if (ctl_ret)
2625 *ctl_ret = NULL;
2626
Takashi Iwai9322ca52012-02-03 14:28:01 +01002627 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002628 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002629 snd_printdd("No slave found for %s\n", name);
2630 return 0;
2631 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002632 kctl = snd_ctl_make_virtual_master(name, tlv);
2633 if (!kctl)
2634 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002635 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002636 if (err < 0)
2637 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002638
Takashi Iwai9322ca52012-02-03 14:28:01 +01002639 err = map_slaves(codec, slaves, suffix,
2640 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002641 if (err < 0)
2642 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002643
2644 /* init with master mute & zero volume */
2645 put_kctl_with_value(kctl, 0);
2646 if (init_slave_vol)
2647 map_slaves(codec, slaves, suffix,
2648 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2649
Takashi Iwai29e58532012-03-12 12:25:03 +01002650 if (ctl_ret)
2651 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002652 return 0;
2653}
Takashi Iwai18478e82012-03-09 17:51:10 +01002654EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002655
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002656/*
2657 * mute-LED control using vmaster
2658 */
2659static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
2662 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002663 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002664 };
2665 unsigned int index;
2666
2667 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2668 uinfo->count = 1;
2669 uinfo->value.enumerated.items = 3;
2670 index = uinfo->value.enumerated.item;
2671 if (index >= 3)
2672 index = 2;
2673 strcpy(uinfo->value.enumerated.name, texts[index]);
2674 return 0;
2675}
2676
2677static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2681 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2682 return 0;
2683}
2684
2685static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2687{
2688 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2689 unsigned int old_mode = hook->mute_mode;
2690
2691 hook->mute_mode = ucontrol->value.enumerated.item[0];
2692 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2693 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2694 if (old_mode == hook->mute_mode)
2695 return 0;
2696 snd_hda_sync_vmaster_hook(hook);
2697 return 1;
2698}
2699
2700static struct snd_kcontrol_new vmaster_mute_mode = {
2701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2702 .name = "Mute-LED Mode",
2703 .info = vmaster_mute_mode_info,
2704 .get = vmaster_mute_mode_get,
2705 .put = vmaster_mute_mode_put,
2706};
2707
2708/*
2709 * Add a mute-LED hook with the given vmaster switch kctl
2710 * "Mute-LED Mode" control is automatically created and associated with
2711 * the given hook.
2712 */
2713int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002714 struct hda_vmaster_mute_hook *hook,
2715 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002716{
2717 struct snd_kcontrol *kctl;
2718
2719 if (!hook->hook || !hook->sw_kctl)
2720 return 0;
2721 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2722 hook->codec = codec;
2723 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002724 if (!expose_enum_ctl)
2725 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002726 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2727 if (!kctl)
2728 return -ENOMEM;
2729 return snd_hda_ctl_add(codec, 0, kctl);
2730}
2731EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2732
2733/*
2734 * Call the hook with the current value for synchronization
2735 * Should be called in init callback
2736 */
2737void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2738{
2739 if (!hook->hook || !hook->codec)
2740 return;
2741 switch (hook->mute_mode) {
2742 case HDA_VMUTE_FOLLOW_MASTER:
2743 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2744 break;
2745 default:
2746 hook->hook(hook->codec, hook->mute_mode);
2747 break;
2748 }
2749}
2750EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2751
2752
Takashi Iwaid5191e52009-11-16 14:58:17 +01002753/**
2754 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2755 *
2756 * The control element is supposed to have the private_value field
2757 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2758 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002759int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 int chs = get_amp_channels(kcontrol);
2763
2764 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2765 uinfo->count = chs == 3 ? 2 : 1;
2766 uinfo->value.integer.min = 0;
2767 uinfo->value.integer.max = 1;
2768 return 0;
2769}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002770EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Takashi Iwaid5191e52009-11-16 14:58:17 +01002772/**
2773 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2774 *
2775 * The control element is supposed to have the private_value field
2776 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2777 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002778int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2782 hda_nid_t nid = get_amp_nid(kcontrol);
2783 int chs = get_amp_channels(kcontrol);
2784 int dir = get_amp_direction(kcontrol);
2785 int idx = get_amp_index(kcontrol);
2786 long *valp = ucontrol->value.integer.value;
2787
2788 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002789 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002790 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002792 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002793 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return 0;
2795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002796EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Takashi Iwaid5191e52009-11-16 14:58:17 +01002798/**
2799 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2800 *
2801 * The control element is supposed to have the private_value field
2802 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2803 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002804int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2808 hda_nid_t nid = get_amp_nid(kcontrol);
2809 int chs = get_amp_channels(kcontrol);
2810 int dir = get_amp_direction(kcontrol);
2811 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 long *valp = ucontrol->value.integer.value;
2813 int change = 0;
2814
Takashi Iwaicb53c622007-08-10 17:21:45 +02002815 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002816 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002817 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002818 HDA_AMP_MUTE,
2819 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002820 valp++;
2821 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002822 if (chs & 2)
2823 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002824 HDA_AMP_MUTE,
2825 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002826 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002827 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return change;
2829}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002830EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832/*
Takashi Iwai985be542005-11-02 18:26:49 +01002833 * bound volume controls
2834 *
2835 * bind multiple volumes (# indices, from 0)
2836 */
2837
2838#define AMP_VAL_IDX_SHIFT 19
2839#define AMP_VAL_IDX_MASK (0x0f<<19)
2840
Takashi Iwaid5191e52009-11-16 14:58:17 +01002841/**
2842 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2843 *
2844 * The control element is supposed to have the private_value field
2845 * set up via HDA_BIND_MUTE*() macros.
2846 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002849{
2850 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2851 unsigned long pval;
2852 int err;
2853
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002854 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002855 pval = kcontrol->private_value;
2856 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2857 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2858 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002859 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002860 return err;
2861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002862EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002863
Takashi Iwaid5191e52009-11-16 14:58:17 +01002864/**
2865 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2866 *
2867 * The control element is supposed to have the private_value field
2868 * set up via HDA_BIND_MUTE*() macros.
2869 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002870int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002872{
2873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2874 unsigned long pval;
2875 int i, indices, err = 0, change = 0;
2876
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002877 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002878 pval = kcontrol->private_value;
2879 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2880 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002881 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2882 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002883 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2884 if (err < 0)
2885 break;
2886 change |= err;
2887 }
2888 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002889 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002890 return err < 0 ? err : change;
2891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002892EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002893
Takashi Iwaid5191e52009-11-16 14:58:17 +01002894/**
2895 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2896 *
2897 * The control element is supposed to have the private_value field
2898 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002899 */
2900int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_info *uinfo)
2902{
2903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2904 struct hda_bind_ctls *c;
2905 int err;
2906
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002907 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002908 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002909 kcontrol->private_value = *c->values;
2910 err = c->ops->info(kcontrol, uinfo);
2911 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002912 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002913 return err;
2914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002915EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002916
Takashi Iwaid5191e52009-11-16 14:58:17 +01002917/**
2918 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2919 *
2920 * The control element is supposed to have the private_value field
2921 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2922 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002923int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2925{
2926 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2927 struct hda_bind_ctls *c;
2928 int err;
2929
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002930 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002931 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002932 kcontrol->private_value = *c->values;
2933 err = c->ops->get(kcontrol, ucontrol);
2934 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002935 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002936 return err;
2937}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002938EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002939
Takashi Iwaid5191e52009-11-16 14:58:17 +01002940/**
2941 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2942 *
2943 * The control element is supposed to have the private_value field
2944 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2945 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002946int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
2949 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2950 struct hda_bind_ctls *c;
2951 unsigned long *vals;
2952 int err = 0, change = 0;
2953
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002954 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002955 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002956 for (vals = c->values; *vals; vals++) {
2957 kcontrol->private_value = *vals;
2958 err = c->ops->put(kcontrol, ucontrol);
2959 if (err < 0)
2960 break;
2961 change |= err;
2962 }
2963 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002964 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002965 return err < 0 ? err : change;
2966}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002967EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002968
Takashi Iwaid5191e52009-11-16 14:58:17 +01002969/**
2970 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2971 *
2972 * The control element is supposed to have the private_value field
2973 * set up via HDA_BIND_VOL() macro.
2974 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002975int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2976 unsigned int size, unsigned int __user *tlv)
2977{
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979 struct hda_bind_ctls *c;
2980 int err;
2981
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002982 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002983 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002984 kcontrol->private_value = *c->values;
2985 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2986 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002987 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002988 return err;
2989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002990EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002991
2992struct hda_ctl_ops snd_hda_bind_vol = {
2993 .info = snd_hda_mixer_amp_volume_info,
2994 .get = snd_hda_mixer_amp_volume_get,
2995 .put = snd_hda_mixer_amp_volume_put,
2996 .tlv = snd_hda_mixer_amp_tlv
2997};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002998EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002999
3000struct hda_ctl_ops snd_hda_bind_sw = {
3001 .info = snd_hda_mixer_amp_switch_info,
3002 .get = snd_hda_mixer_amp_switch_get,
3003 .put = snd_hda_mixer_amp_switch_put,
3004 .tlv = snd_hda_mixer_amp_tlv
3005};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003006EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003007
3008/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 * SPDIF out controls
3010 */
3011
Takashi Iwai0ba21762007-04-16 11:29:14 +02003012static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3016 uinfo->count = 1;
3017 return 0;
3018}
3019
Takashi Iwai0ba21762007-04-16 11:29:14 +02003020static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3024 IEC958_AES0_NONAUDIO |
3025 IEC958_AES0_CON_EMPHASIS_5015 |
3026 IEC958_AES0_CON_NOT_COPYRIGHT;
3027 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3028 IEC958_AES1_CON_ORIGINAL;
3029 return 0;
3030}
3031
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3036 IEC958_AES0_NONAUDIO |
3037 IEC958_AES0_PRO_EMPHASIS_5015;
3038 return 0;
3039}
3040
Takashi Iwai0ba21762007-04-16 11:29:14 +02003041static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3042 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003045 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003046 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003048 mutex_lock(&codec->spdif_mutex);
3049 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003050 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3051 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3052 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3053 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003054 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 return 0;
3057}
3058
3059/* convert from SPDIF status bits to HDA SPDIF bits
3060 * bit 0 (DigEn) is always set zero (to be filled later)
3061 */
3062static unsigned short convert_from_spdif_status(unsigned int sbits)
3063{
3064 unsigned short val = 0;
3065
3066 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003067 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003071 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3072 IEC958_AES0_PRO_EMPHASIS_5015)
3073 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003075 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3076 IEC958_AES0_CON_EMPHASIS_5015)
3077 val |= AC_DIG1_EMPHASIS;
3078 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3079 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3083 }
3084 return val;
3085}
3086
3087/* convert to SPDIF status bits from HDA SPDIF bits
3088 */
3089static unsigned int convert_to_spdif_status(unsigned short val)
3090{
3091 unsigned int sbits = 0;
3092
Takashi Iwai0ba21762007-04-16 11:29:14 +02003093 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 sbits |= IEC958_AES0_PROFESSIONAL;
3097 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003098 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3100 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3107 sbits |= val & (0x7f << 8);
3108 }
3109 return sbits;
3110}
3111
Takashi Iwai2f728532008-09-25 16:32:41 +02003112/* set digital convert verbs both for the given NID and its slaves */
3113static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3114 int verb, int val)
3115{
Takashi Iwaidda14412011-05-02 11:29:30 +02003116 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003117
Takashi Iwai9e976972008-11-25 08:17:20 +01003118 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003119 d = codec->slave_dig_outs;
3120 if (!d)
3121 return;
3122 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003123 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003124}
3125
3126static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3127 int dig1, int dig2)
3128{
3129 if (dig1 != -1)
3130 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3131 if (dig2 != -1)
3132 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3133}
3134
Takashi Iwai0ba21762007-04-16 11:29:14 +02003135static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
3138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003139 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003140 struct hda_spdif_out *spdif;
3141 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 unsigned short val;
3143 int change;
3144
Ingo Molnar62932df2006-01-16 16:34:20 +01003145 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003146 spdif = snd_array_elem(&codec->spdif_out, idx);
3147 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003148 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3150 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3151 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003152 val = convert_from_spdif_status(spdif->status);
3153 val |= spdif->ctls & 1;
3154 change = spdif->ctls != val;
3155 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003156 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003157 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003158 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return change;
3160}
3161
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003162#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Takashi Iwai0ba21762007-04-16 11:29:14 +02003164static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3165 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003168 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003169 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003171 mutex_lock(&codec->spdif_mutex);
3172 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003173 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003174 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
3176}
3177
Stephen Warren74b654c2011-06-01 11:14:18 -06003178static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3179 int dig1, int dig2)
3180{
3181 set_dig_out_convert(codec, nid, dig1, dig2);
3182 /* unmute amp switch (if any) */
3183 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3184 (dig1 & AC_DIG1_ENABLE))
3185 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3186 HDA_AMP_MUTE, 0);
3187}
3188
Takashi Iwai0ba21762007-04-16 11:29:14 +02003189static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3190 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003193 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003194 struct hda_spdif_out *spdif;
3195 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 unsigned short val;
3197 int change;
3198
Ingo Molnar62932df2006-01-16 16:34:20 +01003199 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003200 spdif = snd_array_elem(&codec->spdif_out, idx);
3201 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003202 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003204 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003205 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003206 spdif->ctls = val;
3207 if (change && nid != (u16)-1)
3208 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003209 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return change;
3211}
3212
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003213static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 {
3215 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003217 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 .info = snd_hda_spdif_mask_info,
3219 .get = snd_hda_spdif_cmask_get,
3220 },
3221 {
3222 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003224 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 .info = snd_hda_spdif_mask_info,
3226 .get = snd_hda_spdif_pmask_get,
3227 },
3228 {
3229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003230 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 .info = snd_hda_spdif_mask_info,
3232 .get = snd_hda_spdif_default_get,
3233 .put = snd_hda_spdif_default_put,
3234 },
3235 {
3236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003237 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 .info = snd_hda_spdif_out_switch_info,
3239 .get = snd_hda_spdif_out_switch_get,
3240 .put = snd_hda_spdif_out_switch_put,
3241 },
3242 { } /* end */
3243};
3244
3245/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003246 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003248 * @associated_nid: NID that new ctls associated with
3249 * @cvt_nid: converter NID
3250 * @type: HDA_PCM_TYPE_*
3251 * Creates controls related with the digital output.
3252 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 *
3254 * Returns 0 if successful, or a negative error code.
3255 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003256int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3257 hda_nid_t associated_nid,
3258 hda_nid_t cvt_nid,
3259 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003262 struct snd_kcontrol *kctl;
3263 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003264 int idx, dev = 0;
3265 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003266 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Takashi Iwaidcda5802012-10-12 17:24:51 +02003268 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3269 type == HDA_PCM_TYPE_SPDIF) {
3270 dev = spdif_pcm_dev;
3271 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3272 type == HDA_PCM_TYPE_HDMI) {
3273 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3274 spdif = snd_array_elem(&codec->spdif_out, idx);
3275 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3276 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3277 if (!kctl)
3278 break;
3279 kctl->id.device = spdif_pcm_dev;
3280 }
3281 }
3282 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3283 }
3284 if (!codec->primary_dig_out_type)
3285 codec->primary_dig_out_type = type;
3286
3287 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003288 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003289 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3290 return -EBUSY;
3291 }
Stephen Warren7c935972011-06-01 11:14:17 -06003292 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3294 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003295 if (!kctl)
3296 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003297 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003298 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003299 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003300 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003301 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return err;
3303 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003304 spdif->nid = cvt_nid;
3305 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003306 AC_VERB_GET_DIGI_CONVERT_1, 0);
3307 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
3309}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003310EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003312/* get the hda_spdif_out entry from the given NID
3313 * call within spdif_mutex lock
3314 */
Stephen Warren7c935972011-06-01 11:14:17 -06003315struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3316 hda_nid_t nid)
3317{
3318 int i;
3319 for (i = 0; i < codec->spdif_out.used; i++) {
3320 struct hda_spdif_out *spdif =
3321 snd_array_elem(&codec->spdif_out, i);
3322 if (spdif->nid == nid)
3323 return spdif;
3324 }
3325 return NULL;
3326}
3327EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3328
Stephen Warren74b654c2011-06-01 11:14:18 -06003329void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3330{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003331 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003332
3333 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003334 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003335 spdif->nid = (u16)-1;
3336 mutex_unlock(&codec->spdif_mutex);
3337}
3338EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3339
3340void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3341{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003342 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003343 unsigned short val;
3344
3345 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003346 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003347 if (spdif->nid != nid) {
3348 spdif->nid = nid;
3349 val = spdif->ctls;
3350 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3351 }
3352 mutex_unlock(&codec->spdif_mutex);
3353}
3354EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003357 * SPDIF sharing with analog output
3358 */
3359static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3360 struct snd_ctl_elem_value *ucontrol)
3361{
3362 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3363 ucontrol->value.integer.value[0] = mout->share_spdif;
3364 return 0;
3365}
3366
3367static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_value *ucontrol)
3369{
3370 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3371 mout->share_spdif = !!ucontrol->value.integer.value[0];
3372 return 0;
3373}
3374
3375static struct snd_kcontrol_new spdif_share_sw = {
3376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3377 .name = "IEC958 Default PCM Playback Switch",
3378 .info = snd_ctl_boolean_mono_info,
3379 .get = spdif_share_sw_get,
3380 .put = spdif_share_sw_put,
3381};
3382
Takashi Iwaid5191e52009-11-16 14:58:17 +01003383/**
3384 * snd_hda_create_spdif_share_sw - create Default PCM switch
3385 * @codec: the HDA codec
3386 * @mout: multi-out instance
3387 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003388int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3389 struct hda_multi_out *mout)
3390{
3391 if (!mout->dig_out_nid)
3392 return 0;
3393 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003394 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3395 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003396}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003397EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003398
3399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 * SPDIF input
3401 */
3402
3403#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3404
Takashi Iwai0ba21762007-04-16 11:29:14 +02003405static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3409
3410 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3411 return 0;
3412}
3413
Takashi Iwai0ba21762007-04-16 11:29:14 +02003414static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3415 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3418 hda_nid_t nid = kcontrol->private_value;
3419 unsigned int val = !!ucontrol->value.integer.value[0];
3420 int change;
3421
Ingo Molnar62932df2006-01-16 16:34:20 +01003422 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003424 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003426 snd_hda_codec_write_cache(codec, nid, 0,
3427 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003429 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return change;
3431}
3432
Takashi Iwai0ba21762007-04-16 11:29:14 +02003433static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
3436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3437 hda_nid_t nid = kcontrol->private_value;
3438 unsigned short val;
3439 unsigned int sbits;
3440
Andrew Paprocki3982d172007-12-19 12:13:44 +01003441 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 sbits = convert_to_spdif_status(val);
3443 ucontrol->value.iec958.status[0] = sbits;
3444 ucontrol->value.iec958.status[1] = sbits >> 8;
3445 ucontrol->value.iec958.status[2] = sbits >> 16;
3446 ucontrol->value.iec958.status[3] = sbits >> 24;
3447 return 0;
3448}
3449
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003450static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 {
3452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003453 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 .info = snd_hda_spdif_in_switch_info,
3455 .get = snd_hda_spdif_in_switch_get,
3456 .put = snd_hda_spdif_in_switch_put,
3457 },
3458 {
3459 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003461 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 .info = snd_hda_spdif_mask_info,
3463 .get = snd_hda_spdif_in_status_get,
3464 },
3465 { } /* end */
3466};
3467
3468/**
3469 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3470 * @codec: the HDA codec
3471 * @nid: audio in widget NID
3472 *
3473 * Creates controls related with the SPDIF input.
3474 * Called from each patch supporting the SPDIF in.
3475 *
3476 * Returns 0 if successful, or a negative error code.
3477 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003478int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003481 struct snd_kcontrol *kctl;
3482 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003483 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Takashi Iwaidcda5802012-10-12 17:24:51 +02003485 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003486 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003487 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3488 return -EBUSY;
3489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3491 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003492 if (!kctl)
3493 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003495 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return err;
3498 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003499 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003500 snd_hda_codec_read(codec, nid, 0,
3501 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003502 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return 0;
3504}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003505EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003507/*
3508 * command cache
3509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Takashi Iwaic370dd62012-12-13 18:30:04 +01003511/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003512#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3513#define get_cmd_cache_nid(key) ((key) & 0xff)
3514#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3515
3516/**
3517 * snd_hda_codec_write_cache - send a single command with caching
3518 * @codec: the HDA codec
3519 * @nid: NID to send the command
3520 * @direct: direct flag
3521 * @verb: the verb to send
3522 * @parm: the parameter for the verb
3523 *
3524 * Send a single command without waiting for response.
3525 *
3526 * Returns 0 if successful, or a negative error code.
3527 */
3528int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3529 int direct, unsigned int verb, unsigned int parm)
3530{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003531 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003532 struct hda_cache_head *c;
3533 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003534 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003535
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003536 cache_only = codec->cached_write;
3537 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003538 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3539 if (err < 0)
3540 return err;
3541 }
3542
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003543 /* parm may contain the verb stuff for get/set amp */
3544 verb = verb | (parm >> 8);
3545 parm &= 0xff;
3546 key = build_cmd_cache_key(nid, verb);
3547 mutex_lock(&codec->bus->cmd_mutex);
3548 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003549 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003550 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003551 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003552 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003553 mutex_unlock(&codec->bus->cmd_mutex);
3554 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003555}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003556EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003557
Takashi Iwaid5191e52009-11-16 14:58:17 +01003558/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003559 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3560 * @codec: the HDA codec
3561 * @nid: NID to send the command
3562 * @direct: direct flag
3563 * @verb: the verb to send
3564 * @parm: the parameter for the verb
3565 *
3566 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3567 * command if the parameter is already identical with the cached value.
3568 * If not, it sends the command and refreshes the cache.
3569 *
3570 * Returns 0 if successful, or a negative error code.
3571 */
3572int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3573 int direct, unsigned int verb, unsigned int parm)
3574{
3575 struct hda_cache_head *c;
3576 u32 key;
3577
3578 /* parm may contain the verb stuff for get/set amp */
3579 verb = verb | (parm >> 8);
3580 parm &= 0xff;
3581 key = build_cmd_cache_key(nid, verb);
3582 mutex_lock(&codec->bus->cmd_mutex);
3583 c = get_hash(&codec->cmd_cache, key);
3584 if (c && c->val == parm) {
3585 mutex_unlock(&codec->bus->cmd_mutex);
3586 return 0;
3587 }
3588 mutex_unlock(&codec->bus->cmd_mutex);
3589 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3590}
3591EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3592
3593/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003594 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3595 * @codec: HD-audio codec
3596 *
3597 * Execute all verbs recorded in the command caches to resume.
3598 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003599void snd_hda_codec_resume_cache(struct hda_codec *codec)
3600{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003601 int i;
3602
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003604 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003605 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3606 struct hda_cache_head *buffer;
3607 u32 key;
3608
3609 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3610 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003611 if (!key)
3612 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003613 if (!buffer->dirty)
3614 continue;
3615 buffer->dirty = 0;
3616 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003617 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3618 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003619 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003620 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003621 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003622}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003623EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003624
3625/**
3626 * snd_hda_sequence_write_cache - sequence writes with caching
3627 * @codec: the HDA codec
3628 * @seq: VERB array to send
3629 *
3630 * Send the commands sequentially from the given array.
3631 * Thte commands are recorded on cache for power-save and resume.
3632 * The array must be terminated with NID=0.
3633 */
3634void snd_hda_sequence_write_cache(struct hda_codec *codec,
3635 const struct hda_verb *seq)
3636{
3637 for (; seq->nid; seq++)
3638 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3639 seq->param);
3640}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003641EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003642
Takashi Iwaidc870f32013-01-22 15:24:30 +01003643/**
3644 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3645 * @codec: HD-audio codec
3646 */
3647void snd_hda_codec_flush_cache(struct hda_codec *codec)
3648{
3649 snd_hda_codec_resume_amp(codec);
3650 snd_hda_codec_resume_cache(codec);
3651}
3652EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3653
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003654void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003655 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003656{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003657 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003658 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003659
Takashi Iwaicb53c622007-08-10 17:21:45 +02003660 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003661 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003662 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003663 if (!(wcaps & AC_WCAP_POWER))
3664 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003665 if (codec->power_filter) {
3666 state = codec->power_filter(codec, nid, power_state);
3667 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003668 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003669 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003670 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003671 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003672 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003673}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003674EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3675
3676/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003677 * supported power states check
3678 */
3679static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3680 unsigned int power_state)
3681{
3682 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3683
Mengdong Line037cb42012-08-10 14:11:58 +02003684 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003685 return false;
3686 if (sup & power_state)
3687 return true;
3688 else
3689 return false;
3690}
3691
3692/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003693 * wait until the state is reached, returns the current state
3694 */
3695static unsigned int hda_sync_power_state(struct hda_codec *codec,
3696 hda_nid_t fg,
3697 unsigned int power_state)
3698{
3699 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3700 unsigned int state, actual_state;
3701
3702 for (;;) {
3703 state = snd_hda_codec_read(codec, fg, 0,
3704 AC_VERB_GET_POWER_STATE, 0);
3705 if (state & AC_PWRST_ERROR)
3706 break;
3707 actual_state = (state >> 4) & 0x0f;
3708 if (actual_state == power_state)
3709 break;
3710 if (time_after_eq(jiffies, end_time))
3711 break;
3712 /* wait until the codec reachs to the target state */
3713 msleep(1);
3714 }
3715 return state;
3716}
3717
Takashi Iwai9419ab62013-01-24 17:23:35 +01003718/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3719static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
3720 unsigned int power_state)
3721{
3722 if (power_state == AC_PWRST_D3 &&
3723 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3724 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3725 int eapd = snd_hda_codec_read(codec, nid, 0,
3726 AC_VERB_GET_EAPD_BTLENABLE, 0);
3727 if (eapd & 0x02)
3728 return AC_PWRST_D0;
3729 }
3730 return power_state;
3731}
3732
Takashi Iwai432c6412012-08-28 09:59:20 -07003733/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003734 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003736static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003737 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003738{
Takashi Iwaid8193872012-08-31 07:54:38 -07003739 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003740 int count;
3741 unsigned int state;
3742
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003743 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003744 if (power_state == AC_PWRST_D3) {
3745 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003746 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003747 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003748
3749 /* repeat power states setting at most 10 times*/
3750 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003751 if (codec->patch_ops.set_power_state)
3752 codec->patch_ops.set_power_state(codec, fg,
3753 power_state);
3754 else {
3755 snd_hda_codec_read(codec, fg, 0,
3756 AC_VERB_SET_POWER_STATE,
3757 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003758 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003759 }
3760 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003761 if (!(state & AC_PWRST_ERROR))
3762 break;
3763 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003764
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003765 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003766}
Takashi Iwai54d17402005-11-21 16:33:22 +01003767
Takashi Iwai11aeff02008-07-30 15:01:46 +02003768#ifdef CONFIG_SND_HDA_HWDEP
3769/* execute additional init verbs */
3770static void hda_exec_init_verbs(struct hda_codec *codec)
3771{
3772 if (codec->init_verbs.list)
3773 snd_hda_sequence_write(codec, codec->init_verbs.list);
3774}
3775#else
3776static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3777#endif
3778
Takashi Iwai2a439522011-07-26 09:52:50 +02003779#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003780/*
3781 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003782 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003783 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003784static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003785{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003786 unsigned int state;
3787
Takashi Iwai989c3182012-11-19 14:14:58 +01003788 codec->in_pm = 1;
3789
Takashi Iwaicb53c622007-08-10 17:21:45 +02003790 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003791 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003792 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003793 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003794 /* Cancel delayed work if we aren't currently running from it. */
3795 if (!in_wq)
3796 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003797 spin_lock(&codec->power_lock);
3798 snd_hda_update_power_acct(codec);
3799 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003800 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003801 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003802 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003803 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003804 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003805 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003806}
3807
Takashi Iwaic370dd62012-12-13 18:30:04 +01003808/* mark all entries of cmd and amp caches dirty */
3809static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3810{
3811 int i;
3812 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3813 struct hda_cache_head *cmd;
3814 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3815 cmd->dirty = 1;
3816 }
3817 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3818 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003819 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003820 amp->head.dirty = 1;
3821 }
3822}
3823
Takashi Iwaicb53c622007-08-10 17:21:45 +02003824/*
3825 * kick up codec; used both from PM and power-save
3826 */
3827static void hda_call_codec_resume(struct hda_codec *codec)
3828{
Takashi Iwai989c3182012-11-19 14:14:58 +01003829 codec->in_pm = 1;
3830
Takashi Iwaic370dd62012-12-13 18:30:04 +01003831 hda_mark_cmd_cache_dirty(codec);
3832
Takashi Iwai7f308302012-05-08 16:52:23 +02003833 /* set as if powered on for avoiding re-entering the resume
3834 * in the resume / power-save sequence
3835 */
3836 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003837 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003838 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003839 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003840 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003841 if (codec->patch_ops.resume)
3842 codec->patch_ops.resume(codec);
3843 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003844 if (codec->patch_ops.init)
3845 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003846 snd_hda_codec_resume_amp(codec);
3847 snd_hda_codec_resume_cache(codec);
3848 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003849
3850 if (codec->jackpoll_interval)
3851 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003852 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003853 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003854
3855 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003856 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003857}
Takashi Iwai2a439522011-07-26 09:52:50 +02003858#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003859
Takashi Iwai54d17402005-11-21 16:33:22 +01003860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861/**
3862 * snd_hda_build_controls - build mixer controls
3863 * @bus: the BUS
3864 *
3865 * Creates mixer controls for each codec included in the bus.
3866 *
3867 * Returns 0 if successful, otherwise a negative error code.
3868 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003869int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003871 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Takashi Iwai0ba21762007-04-16 11:29:14 +02003873 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003874 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003875 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003876 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003877 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003878 err = snd_hda_codec_reset(codec);
3879 if (err < 0) {
3880 printk(KERN_ERR
3881 "hda_codec: cannot revert codec\n");
3882 return err;
3883 }
3884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003886 return 0;
3887}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003888EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003890/*
3891 * add standard channel maps if not specified
3892 */
3893static int add_std_chmaps(struct hda_codec *codec)
3894{
3895 int i, str, err;
3896
3897 for (i = 0; i < codec->num_pcms; i++) {
3898 for (str = 0; str < 2; str++) {
3899 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3900 struct hda_pcm_stream *hinfo =
3901 &codec->pcm_info[i].stream[str];
3902 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003903 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003904
3905 if (codec->pcm_info[i].own_chmap)
3906 continue;
3907 if (!pcm || !hinfo->substreams)
3908 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003909 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3910 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003911 hinfo->channels_max,
3912 0, &chmap);
3913 if (err < 0)
3914 return err;
3915 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3916 }
3917 }
3918 return 0;
3919}
3920
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003921/* default channel maps for 2.1 speakers;
3922 * since HD-audio supports only stereo, odd number channels are omitted
3923 */
3924const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3925 { .channels = 2,
3926 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3927 { .channels = 4,
3928 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3929 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3930 { }
3931};
3932EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3933
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003934int snd_hda_codec_build_controls(struct hda_codec *codec)
3935{
3936 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003937 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003938 /* continue to initialize... */
3939 if (codec->patch_ops.init)
3940 err = codec->patch_ops.init(codec);
3941 if (!err && codec->patch_ops.build_controls)
3942 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003943 if (err < 0)
3944 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003945
3946 /* we create chmaps here instead of build_pcms */
3947 err = add_std_chmaps(codec);
3948 if (err < 0)
3949 return err;
3950
David Henningsson26a6cb62012-10-09 15:04:21 +02003951 if (codec->jackpoll_interval)
3952 hda_jackpoll_work(&codec->jackpoll_work.work);
3953 else
3954 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return 0;
3956}
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958/*
3959 * stream formats
3960 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003961struct hda_rate_tbl {
3962 unsigned int hz;
3963 unsigned int alsa_bits;
3964 unsigned int hda_fmt;
3965};
3966
Takashi Iwai92f10b32010-08-03 14:21:00 +02003967/* rate = base * mult / div */
3968#define HDA_RATE(base, mult, div) \
3969 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3970 (((div) - 1) << AC_FMT_DIV_SHIFT))
3971
Takashi Iwaibefdf312005-08-22 13:57:55 +02003972static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003974
3975 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003976 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3977 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3978 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3979 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3980 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3981 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3982 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3983 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3984 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3985 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3986 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003987#define AC_PAR_PCM_RATE_BITS 11
3988 /* up to bits 10, 384kHZ isn't supported properly */
3989
3990 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003991 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003992
Takashi Iwaibefdf312005-08-22 13:57:55 +02003993 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994};
3995
3996/**
3997 * snd_hda_calc_stream_format - calculate format bitset
3998 * @rate: the sample rate
3999 * @channels: the number of channels
4000 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4001 * @maxbps: the max. bps
4002 *
4003 * Calculate the format bitset from the given rate, channels and th PCM format.
4004 *
4005 * Return zero if invalid.
4006 */
4007unsigned int snd_hda_calc_stream_format(unsigned int rate,
4008 unsigned int channels,
4009 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004010 unsigned int maxbps,
4011 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 int i;
4014 unsigned int val = 0;
4015
Takashi Iwaibefdf312005-08-22 13:57:55 +02004016 for (i = 0; rate_bits[i].hz; i++)
4017 if (rate_bits[i].hz == rate) {
4018 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 break;
4020 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004021 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 snd_printdd("invalid rate %d\n", rate);
4023 return 0;
4024 }
4025
4026 if (channels == 0 || channels > 8) {
4027 snd_printdd("invalid channels %d\n", channels);
4028 return 0;
4029 }
4030 val |= channels - 1;
4031
4032 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004033 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004034 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004035 break;
4036 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004037 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 case 20:
4040 case 24:
4041 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004042 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004043 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004045 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004047 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 break;
4049 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004050 snd_printdd("invalid format width %d\n",
4051 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 return 0;
4053 }
4054
Anssi Hannula32c168c2010-08-03 13:28:57 +03004055 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004056 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return val;
4059}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004060EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004062static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4063 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004064{
4065 unsigned int val = 0;
4066 if (nid != codec->afg &&
4067 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4068 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4069 if (!val || val == -1)
4070 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4071 if (!val || val == -1)
4072 return 0;
4073 return val;
4074}
4075
4076static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4077{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004078 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004079 get_pcm_param);
4080}
4081
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004082static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4083 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004084{
4085 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4086 if (!streams || streams == -1)
4087 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4088 if (!streams || streams == -1)
4089 return 0;
4090 return streams;
4091}
4092
4093static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4094{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004095 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004096 get_stream_param);
4097}
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099/**
4100 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4101 * @codec: the HDA codec
4102 * @nid: NID to query
4103 * @ratesp: the pointer to store the detected rate bitflags
4104 * @formatsp: the pointer to store the detected formats
4105 * @bpsp: the pointer to store the detected format widths
4106 *
4107 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4108 * or @bsps argument is ignored.
4109 *
4110 * Returns 0 if successful, otherwise a negative error code.
4111 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004112int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4114{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004115 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004117 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004118 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 if (ratesp) {
4121 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004122 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004124 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004126 if (rates == 0) {
4127 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4128 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4129 nid, val,
4130 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4131 return -EIO;
4132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 *ratesp = rates;
4134 }
4135
4136 if (formatsp || bpsp) {
4137 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004138 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004140 streams = query_stream_param(codec, nid);
4141 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
4144 bps = 0;
4145 if (streams & AC_SUPFMT_PCM) {
4146 if (val & AC_SUPPCM_BITS_8) {
4147 formats |= SNDRV_PCM_FMTBIT_U8;
4148 bps = 8;
4149 }
4150 if (val & AC_SUPPCM_BITS_16) {
4151 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4152 bps = 16;
4153 }
4154 if (wcaps & AC_WCAP_DIGITAL) {
4155 if (val & AC_SUPPCM_BITS_32)
4156 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4157 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4158 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4159 if (val & AC_SUPPCM_BITS_24)
4160 bps = 24;
4161 else if (val & AC_SUPPCM_BITS_20)
4162 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004163 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4164 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4166 if (val & AC_SUPPCM_BITS_32)
4167 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 else if (val & AC_SUPPCM_BITS_24)
4169 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004170 else if (val & AC_SUPPCM_BITS_20)
4171 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004174#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004175 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004177 if (!bps)
4178 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004179 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004180#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004181 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004182 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 /* temporary hack: we have still no proper support
4184 * for the direct AC3 stream...
4185 */
4186 formats |= SNDRV_PCM_FMTBIT_U8;
4187 bps = 8;
4188 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004189 if (formats == 0) {
4190 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4191 "(nid=0x%x, val=0x%x, ovrd=%i, "
4192 "streams=0x%x)\n",
4193 nid, val,
4194 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4195 streams);
4196 return -EIO;
4197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 if (formatsp)
4199 *formatsp = formats;
4200 if (bpsp)
4201 *bpsp = bps;
4202 }
4203
4204 return 0;
4205}
Stephen Warren384a48d2011-06-01 11:14:21 -06004206EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004209 * snd_hda_is_supported_format - Check the validity of the format
4210 * @codec: HD-audio codec
4211 * @nid: NID to check
4212 * @format: the HD-audio format value to check
4213 *
4214 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 *
4216 * Returns 1 if supported, 0 if not.
4217 */
4218int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4219 unsigned int format)
4220{
4221 int i;
4222 unsigned int val = 0, rate, stream;
4223
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004224 val = query_pcm_param(codec, nid);
4225 if (!val)
4226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004229 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004230 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 if (val & (1 << i))
4232 break;
4233 return 0;
4234 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004235 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return 0;
4237
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004238 stream = query_stream_param(codec, nid);
4239 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return 0;
4241
4242 if (stream & AC_SUPFMT_PCM) {
4243 switch (format & 0xf0) {
4244 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004245 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return 0;
4247 break;
4248 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004249 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return 0;
4251 break;
4252 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004253 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return 0;
4255 break;
4256 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004257 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return 0;
4259 break;
4260 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004261 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return 0;
4263 break;
4264 default:
4265 return 0;
4266 }
4267 } else {
4268 /* FIXME: check for float32 and AC3? */
4269 }
4270
4271 return 1;
4272}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004273EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275/*
4276 * PCM stuff
4277 */
4278static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4279 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004280 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 return 0;
4283}
4284
4285static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4286 struct hda_codec *codec,
4287 unsigned int stream_tag,
4288 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004289 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
4291 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4292 return 0;
4293}
4294
4295static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4296 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004297 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
Takashi Iwai888afa12008-03-18 09:57:50 +01004299 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return 0;
4301}
4302
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004303static int set_pcm_default_values(struct hda_codec *codec,
4304 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004306 int err;
4307
Takashi Iwai0ba21762007-04-16 11:29:14 +02004308 /* query support PCM information from the given NID */
4309 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004310 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004311 info->rates ? NULL : &info->rates,
4312 info->formats ? NULL : &info->formats,
4313 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004314 if (err < 0)
4315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317 if (info->ops.open == NULL)
4318 info->ops.open = hda_pcm_default_open_close;
4319 if (info->ops.close == NULL)
4320 info->ops.close = hda_pcm_default_open_close;
4321 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004322 if (snd_BUG_ON(!info->nid))
4323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 info->ops.prepare = hda_pcm_default_prepare;
4325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004327 if (snd_BUG_ON(!info->nid))
4328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 info->ops.cleanup = hda_pcm_default_cleanup;
4330 }
4331 return 0;
4332}
4333
Takashi Iwaieb541332010-08-06 13:48:11 +02004334/*
4335 * codec prepare/cleanup entries
4336 */
4337int snd_hda_codec_prepare(struct hda_codec *codec,
4338 struct hda_pcm_stream *hinfo,
4339 unsigned int stream,
4340 unsigned int format,
4341 struct snd_pcm_substream *substream)
4342{
4343 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004344 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004345 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4346 if (ret >= 0)
4347 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004348 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004349 return ret;
4350}
4351EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4352
4353void snd_hda_codec_cleanup(struct hda_codec *codec,
4354 struct hda_pcm_stream *hinfo,
4355 struct snd_pcm_substream *substream)
4356{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004357 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004358 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004359 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004360}
4361EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4362
Takashi Iwaid5191e52009-11-16 14:58:17 +01004363/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004364const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4365 "Audio", "SPDIF", "HDMI", "Modem"
4366};
4367
Takashi Iwai176d5332008-07-30 15:01:44 +02004368/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004369 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004370 *
4371 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004372 */
4373static int get_empty_pcm_device(struct hda_bus *bus, int type)
4374{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004375 /* audio device indices; not linear to keep compatibility */
4376 static int audio_idx[HDA_PCM_NTYPES][5] = {
4377 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4378 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004379 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004380 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004381 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004382 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004383
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004384 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004385 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4386 return -EINVAL;
4387 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004388
4389 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4390 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4391 return audio_idx[type][i];
4392
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004393 /* non-fixed slots starting from 10 */
4394 for (i = 10; i < 32; i++) {
4395 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4396 return i;
4397 }
4398
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004399 snd_printk(KERN_WARNING "Too many %s devices\n",
4400 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004401 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004402}
4403
4404/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004405 * attach a new PCM stream
4406 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004407static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004408{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004409 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004410 struct hda_pcm_stream *info;
4411 int stream, err;
4412
Takashi Iwaib91f0802008-11-04 08:43:08 +01004413 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004414 return -EINVAL;
4415 for (stream = 0; stream < 2; stream++) {
4416 info = &pcm->stream[stream];
4417 if (info->substreams) {
4418 err = set_pcm_default_values(codec, info);
4419 if (err < 0)
4420 return err;
4421 }
4422 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004423 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004424}
4425
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004426/* assign all PCMs of the given codec */
4427int snd_hda_codec_build_pcms(struct hda_codec *codec)
4428{
4429 unsigned int pcm;
4430 int err;
4431
4432 if (!codec->num_pcms) {
4433 if (!codec->patch_ops.build_pcms)
4434 return 0;
4435 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004436 if (err < 0) {
4437 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004438 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004439 err = snd_hda_codec_reset(codec);
4440 if (err < 0) {
4441 printk(KERN_ERR
4442 "hda_codec: cannot revert codec\n");
4443 return err;
4444 }
4445 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004446 }
4447 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4448 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4449 int dev;
4450
4451 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004452 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004453
4454 if (!cpcm->pcm) {
4455 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4456 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004457 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004458 cpcm->device = dev;
4459 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004460 if (err < 0) {
4461 printk(KERN_ERR "hda_codec: cannot attach "
4462 "PCM stream %d for codec #%d\n",
4463 dev, codec->addr);
4464 continue; /* no fatal error */
4465 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004466 }
4467 }
4468 return 0;
4469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471/**
4472 * snd_hda_build_pcms - build PCM information
4473 * @bus: the BUS
4474 *
4475 * Create PCM information for each codec included in the bus.
4476 *
4477 * The build_pcms codec patch is requested to set up codec->num_pcms and
4478 * codec->pcm_info properly. The array is referred by the top-level driver
4479 * to create its PCM instances.
4480 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4481 * callback.
4482 *
4483 * At least, substreams, channels_min and channels_max must be filled for
4484 * each stream. substreams = 0 indicates that the stream doesn't exist.
4485 * When rates and/or formats are zero, the supported values are queried
4486 * from the given nid. The nid is used also by the default ops.prepare
4487 * and ops.cleanup callbacks.
4488 *
4489 * The driver needs to call ops.open in its open callback. Similarly,
4490 * ops.close is supposed to be called in the close callback.
4491 * ops.prepare should be called in the prepare or hw_params callback
4492 * with the proper parameters for set up.
4493 * ops.cleanup should be called in hw_free for clean up of streams.
4494 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004495 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004497int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004499 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Takashi Iwai0ba21762007-04-16 11:29:14 +02004501 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004502 int err = snd_hda_codec_build_pcms(codec);
4503 if (err < 0)
4504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
4506 return 0;
4507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004508EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510/**
4511 * snd_hda_check_board_config - compare the current codec with the config table
4512 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004513 * @num_configs: number of config enums
4514 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 * @tbl: configuration table, terminated by null entries
4516 *
4517 * Compares the modelname or PCI subsystem id of the current codec with the
4518 * given configuration table. If a matching entry is found, returns its
4519 * config value (supposed to be 0 or positive).
4520 *
4521 * If no entries are matching, the function returns a negative value.
4522 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004523int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004524 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004525 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004527 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004528 int i;
4529 for (i = 0; i < num_configs; i++) {
4530 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004531 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004532 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4533 "selected\n", models[i]);
4534 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
4536 }
4537 }
4538
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004539 if (!codec->bus->pci || !tbl)
4540 return -1;
4541
4542 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4543 if (!tbl)
4544 return -1;
4545 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004546#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004547 char tmp[10];
4548 const char *model = NULL;
4549 if (models)
4550 model = models[tbl->value];
4551 if (!model) {
4552 sprintf(tmp, "#%d", tbl->value);
4553 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004555 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4556 "for config %x:%x (%s)\n",
4557 model, tbl->subvendor, tbl->subdevice,
4558 (tbl->name ? tbl->name : "Unknown device"));
4559#endif
4560 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 }
4562 return -1;
4563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004564EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004567 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004568 subsystem ID with the
4569 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004570
4571 This is important for Gateway notebooks with SB450 HDA Audio
4572 where the vendor ID of the PCI device is:
4573 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4574 and the vendor/subvendor are found only at the codec.
4575
4576 * @codec: the HDA codec
4577 * @num_configs: number of config enums
4578 * @models: array of model name strings
4579 * @tbl: configuration table, terminated by null entries
4580 *
4581 * Compares the modelname or PCI subsystem id of the current codec with the
4582 * given configuration table. If a matching entry is found, returns its
4583 * config value (supposed to be 0 or positive).
4584 *
4585 * If no entries are matching, the function returns a negative value.
4586 */
4587int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004588 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004589 const struct snd_pci_quirk *tbl)
4590{
4591 const struct snd_pci_quirk *q;
4592
4593 /* Search for codec ID */
4594 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004595 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4596 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4597 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004598 break;
4599 }
4600
4601 if (!q->subvendor)
4602 return -1;
4603
4604 tbl = q;
4605
4606 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004607#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004608 char tmp[10];
4609 const char *model = NULL;
4610 if (models)
4611 model = models[tbl->value];
4612 if (!model) {
4613 sprintf(tmp, "#%d", tbl->value);
4614 model = tmp;
4615 }
4616 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4617 "for config %x:%x (%s)\n",
4618 model, tbl->subvendor, tbl->subdevice,
4619 (tbl->name ? tbl->name : "Unknown device"));
4620#endif
4621 return tbl->value;
4622 }
4623 return -1;
4624}
4625EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4626
4627/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 * snd_hda_add_new_ctls - create controls from the array
4629 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004630 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 *
4632 * This helper function creates and add new controls in the given array.
4633 * The array must be terminated with an empty entry as terminator.
4634 *
4635 * Returns 0 if successful, or a negative error code.
4636 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004637int snd_hda_add_new_ctls(struct hda_codec *codec,
4638 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004640 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004643 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004644 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004645 if (knew->iface == -1) /* skip this codec private value */
4646 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004647 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004648 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004649 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004650 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004651 if (addr > 0)
4652 kctl->id.device = addr;
4653 if (idx > 0)
4654 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004655 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004656 if (!err)
4657 break;
4658 /* try first with another device index corresponding to
4659 * the codec addr; if it still fails (or it's the
4660 * primary codec), then try another control index
4661 */
4662 if (!addr && codec->addr)
4663 addr = codec->addr;
4664 else if (!idx && !knew->index) {
4665 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004666 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004667 if (idx <= 0)
4668 return err;
4669 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004670 return err;
4671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 }
4673 return 0;
4674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004675EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Takashi Iwai83012a72012-08-24 18:38:08 +02004677#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004678static void hda_power_work(struct work_struct *work)
4679{
4680 struct hda_codec *codec =
4681 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004682 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004683 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004684
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004685 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004686 if (codec->power_transition > 0) { /* during power-up sequence? */
4687 spin_unlock(&codec->power_lock);
4688 return;
4689 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004690 if (!codec->power_on || codec->power_count) {
4691 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004692 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004693 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004694 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004695 spin_unlock(&codec->power_lock);
4696
Dylan Reidd17344b2012-09-28 15:57:01 -07004697 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004698 codec->pm_down_notified = 0;
4699 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4700 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004701 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004702 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004703}
4704
4705static void hda_keep_power_on(struct hda_codec *codec)
4706{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004707 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004708 codec->power_count++;
4709 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004710 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004711 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004712}
4713
Takashi Iwaid5191e52009-11-16 14:58:17 +01004714/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004715void snd_hda_update_power_acct(struct hda_codec *codec)
4716{
4717 unsigned long delta = jiffies - codec->power_jiffies;
4718 if (codec->power_on)
4719 codec->power_on_acct += delta;
4720 else
4721 codec->power_off_acct += delta;
4722 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004723}
4724
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004725/* Transition to powered up, if wait_power_down then wait for a pending
4726 * transition to D3 to complete. A pending D3 transition is indicated
4727 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004728/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004729static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004730{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004731 struct hda_bus *bus = codec->bus;
4732
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004733 /* Return if power_on or transitioning to power_on, unless currently
4734 * powering down. */
4735 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004736 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004737 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004738 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004739
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004740 cancel_delayed_work_sync(&codec->power_work);
4741
4742 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004743 /* If the power down delayed work was cancelled above before starting,
4744 * then there is no need to go through power up here.
4745 */
4746 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004747 if (codec->power_transition < 0)
4748 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004749 return;
4750 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004751
Takashi Iwaid66fee52011-08-02 15:39:31 +02004752 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004753 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004754 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004755 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004756 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004757 spin_unlock(&codec->power_lock);
4758
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004759 if (codec->pm_down_notified) {
4760 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004761 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004762 }
4763
Takashi Iwaicb53c622007-08-10 17:21:45 +02004764 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004765
4766 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004767 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004768}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004769
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004770#define power_save(codec) \
4771 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004772
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004773/* Transition to powered down */
4774static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004775{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004776 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004777 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004778
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004779 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004780 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004781 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004782 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004783 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004784}
4785
4786/**
4787 * snd_hda_power_save - Power-up/down/sync the codec
4788 * @codec: HD-audio codec
4789 * @delta: the counter delta to change
4790 *
4791 * Change the power-up counter via @delta, and power up or down the hardware
4792 * appropriately. For the power-down, queue to the delayed action.
4793 * Passing zero to @delta means to synchronize the power state.
4794 */
4795void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4796{
4797 spin_lock(&codec->power_lock);
4798 codec->power_count += delta;
4799 trace_hda_power_count(codec);
4800 if (delta > 0)
4801 __snd_hda_power_up(codec, d3wait);
4802 else
4803 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004804 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004805}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004806EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004807
Takashi Iwaid5191e52009-11-16 14:58:17 +01004808/**
4809 * snd_hda_check_amp_list_power - Check the amp list and update the power
4810 * @codec: HD-audio codec
4811 * @check: the object containing an AMP list and the status
4812 * @nid: NID to check / update
4813 *
4814 * Check whether the given NID is in the amp list. If it's in the list,
4815 * check the current AMP status, and update the the power-status according
4816 * to the mute status.
4817 *
4818 * This function is supposed to be set or called from the check_power_status
4819 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004820 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004821int snd_hda_check_amp_list_power(struct hda_codec *codec,
4822 struct hda_loopback_check *check,
4823 hda_nid_t nid)
4824{
Takashi Iwai031024e2011-05-02 11:29:30 +02004825 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004826 int ch, v;
4827
4828 if (!check->amplist)
4829 return 0;
4830 for (p = check->amplist; p->nid; p++) {
4831 if (p->nid == nid)
4832 break;
4833 }
4834 if (!p->nid)
4835 return 0; /* nothing changed */
4836
4837 for (p = check->amplist; p->nid; p++) {
4838 for (ch = 0; ch < 2; ch++) {
4839 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4840 p->idx);
4841 if (!(v & HDA_AMP_MUTE) && v > 0) {
4842 if (!check->power_on) {
4843 check->power_on = 1;
4844 snd_hda_power_up(codec);
4845 }
4846 return 1;
4847 }
4848 }
4849 }
4850 if (check->power_on) {
4851 check->power_on = 0;
4852 snd_hda_power_down(codec);
4853 }
4854 return 0;
4855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004856EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004857#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004859/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004860 * Channel mode helper
4861 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004862
4863/**
4864 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4865 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004866int snd_hda_ch_mode_info(struct hda_codec *codec,
4867 struct snd_ctl_elem_info *uinfo,
4868 const struct hda_channel_mode *chmode,
4869 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004870{
4871 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4872 uinfo->count = 1;
4873 uinfo->value.enumerated.items = num_chmodes;
4874 if (uinfo->value.enumerated.item >= num_chmodes)
4875 uinfo->value.enumerated.item = num_chmodes - 1;
4876 sprintf(uinfo->value.enumerated.name, "%dch",
4877 chmode[uinfo->value.enumerated.item].channels);
4878 return 0;
4879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004880EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004881
Takashi Iwaid5191e52009-11-16 14:58:17 +01004882/**
4883 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4884 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004885int snd_hda_ch_mode_get(struct hda_codec *codec,
4886 struct snd_ctl_elem_value *ucontrol,
4887 const struct hda_channel_mode *chmode,
4888 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004889 int max_channels)
4890{
4891 int i;
4892
4893 for (i = 0; i < num_chmodes; i++) {
4894 if (max_channels == chmode[i].channels) {
4895 ucontrol->value.enumerated.item[0] = i;
4896 break;
4897 }
4898 }
4899 return 0;
4900}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004901EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004902
Takashi Iwaid5191e52009-11-16 14:58:17 +01004903/**
4904 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004906int snd_hda_ch_mode_put(struct hda_codec *codec,
4907 struct snd_ctl_elem_value *ucontrol,
4908 const struct hda_channel_mode *chmode,
4909 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004910 int *max_channelsp)
4911{
4912 unsigned int mode;
4913
4914 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004915 if (mode >= num_chmodes)
4916 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004917 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004918 return 0;
4919 /* change the current channel setting */
4920 *max_channelsp = chmode[mode].channels;
4921 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004922 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004923 return 1;
4924}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004925EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927/*
4928 * input MUX helper
4929 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004930
4931/**
4932 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4933 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4935 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 unsigned int index;
4938
4939 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4940 uinfo->count = 1;
4941 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004942 if (!imux->num_items)
4943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 index = uinfo->value.enumerated.item;
4945 if (index >= imux->num_items)
4946 index = imux->num_items - 1;
4947 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4948 return 0;
4949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004950EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Takashi Iwaid5191e52009-11-16 14:58:17 +01004952/**
4953 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4954 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004955int snd_hda_input_mux_put(struct hda_codec *codec,
4956 const struct hda_input_mux *imux,
4957 struct snd_ctl_elem_value *ucontrol,
4958 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 unsigned int *cur_val)
4960{
4961 unsigned int idx;
4962
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004963 if (!imux->num_items)
4964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 idx = ucontrol->value.enumerated.item[0];
4966 if (idx >= imux->num_items)
4967 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004968 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004970 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4971 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 *cur_val = idx;
4973 return 1;
4974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004975EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977
4978/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004979 * process kcontrol info callback of a simple string enum array
4980 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4981 */
4982int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4983 struct snd_ctl_elem_info *uinfo,
4984 int num_items, const char * const *texts)
4985{
4986 static const char * const texts_default[] = {
4987 "Disabled", "Enabled"
4988 };
4989
4990 if (!texts || !num_items) {
4991 num_items = 2;
4992 texts = texts_default;
4993 }
4994
4995 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4996 uinfo->count = 1;
4997 uinfo->value.enumerated.items = num_items;
4998 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4999 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5000 strcpy(uinfo->value.enumerated.name,
5001 texts[uinfo->value.enumerated.item]);
5002 return 0;
5003}
5004EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5005
5006/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 * Multi-channel / digital-out PCM helper functions
5008 */
5009
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005010/* setup SPDIF output stream */
5011static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5012 unsigned int stream_tag, unsigned int format)
5013{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005014 struct hda_spdif_out *spdif;
5015 unsigned int curr_fmt;
5016 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005017
Laurence Darby3bef1c32012-11-03 17:00:06 +00005018 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5019 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5020 AC_VERB_GET_STREAM_FORMAT, 0);
5021 reset = codec->spdif_status_reset &&
5022 (spdif->ctls & AC_DIG1_ENABLE) &&
5023 curr_fmt != format;
5024
5025 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5026 updated */
5027 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005028 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005029 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005030 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005031 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005032 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005033 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005034 for (d = codec->slave_dig_outs; *d; d++)
5035 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5036 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005037 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005038 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005039 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005040 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005041 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005042}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005043
Takashi Iwai2f728532008-09-25 16:32:41 +02005044static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5045{
5046 snd_hda_codec_cleanup_stream(codec, nid);
5047 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005048 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005049 for (d = codec->slave_dig_outs; *d; d++)
5050 snd_hda_codec_cleanup_stream(codec, *d);
5051 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005052}
5053
Takashi Iwaid5191e52009-11-16 14:58:17 +01005054/**
5055 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5056 * @bus: HD-audio bus
5057 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005058void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5059{
5060 struct hda_codec *codec;
5061
5062 if (!bus)
5063 return;
5064 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005065 if (hda_codec_is_power_on(codec) &&
5066 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005067 codec->patch_ops.reboot_notify(codec);
5068 }
5069}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005070EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005071
Takashi Iwaid5191e52009-11-16 14:58:17 +01005072/**
5073 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005075int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5076 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
Ingo Molnar62932df2006-01-16 16:34:20 +01005078 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005079 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5080 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005081 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005083 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 return 0;
5085}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005086EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Takashi Iwaid5191e52009-11-16 14:58:17 +01005088/**
5089 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5090 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005091int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5092 struct hda_multi_out *mout,
5093 unsigned int stream_tag,
5094 unsigned int format,
5095 struct snd_pcm_substream *substream)
5096{
5097 mutex_lock(&codec->spdif_mutex);
5098 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5099 mutex_unlock(&codec->spdif_mutex);
5100 return 0;
5101}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005102EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005103
Takashi Iwaid5191e52009-11-16 14:58:17 +01005104/**
5105 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5106 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005107int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5108 struct hda_multi_out *mout)
5109{
5110 mutex_lock(&codec->spdif_mutex);
5111 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5112 mutex_unlock(&codec->spdif_mutex);
5113 return 0;
5114}
5115EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5116
Takashi Iwaid5191e52009-11-16 14:58:17 +01005117/**
5118 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005120int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5121 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Ingo Molnar62932df2006-01-16 16:34:20 +01005123 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005125 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return 0;
5127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005128EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Takashi Iwaid5191e52009-11-16 14:58:17 +01005130/**
5131 * snd_hda_multi_out_analog_open - open analog outputs
5132 *
5133 * Open analog outputs and set up the hw-constraints.
5134 * If the digital outputs can be opened as slave, open the digital
5135 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005137int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5138 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005139 struct snd_pcm_substream *substream,
5140 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
Takashi Iwai9a081602008-02-12 18:37:26 +01005142 struct snd_pcm_runtime *runtime = substream->runtime;
5143 runtime->hw.channels_max = mout->max_channels;
5144 if (mout->dig_out_nid) {
5145 if (!mout->analog_rates) {
5146 mout->analog_rates = hinfo->rates;
5147 mout->analog_formats = hinfo->formats;
5148 mout->analog_maxbps = hinfo->maxbps;
5149 } else {
5150 runtime->hw.rates = mout->analog_rates;
5151 runtime->hw.formats = mout->analog_formats;
5152 hinfo->maxbps = mout->analog_maxbps;
5153 }
5154 if (!mout->spdif_rates) {
5155 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5156 &mout->spdif_rates,
5157 &mout->spdif_formats,
5158 &mout->spdif_maxbps);
5159 }
5160 mutex_lock(&codec->spdif_mutex);
5161 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005162 if ((runtime->hw.rates & mout->spdif_rates) &&
5163 (runtime->hw.formats & mout->spdif_formats)) {
5164 runtime->hw.rates &= mout->spdif_rates;
5165 runtime->hw.formats &= mout->spdif_formats;
5166 if (mout->spdif_maxbps < hinfo->maxbps)
5167 hinfo->maxbps = mout->spdif_maxbps;
5168 } else {
5169 mout->share_spdif = 0;
5170 /* FIXME: need notify? */
5171 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005172 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005173 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5176 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5177}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005178EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Takashi Iwaid5191e52009-11-16 14:58:17 +01005180/**
5181 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5182 *
5183 * Set up the i/o for analog out.
5184 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005186int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5187 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 unsigned int stream_tag,
5189 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005190 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
Takashi Iwaidda14412011-05-02 11:29:30 +02005192 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005194 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 int i;
5196
Ingo Molnar62932df2006-01-16 16:34:20 +01005197 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005198 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005199 if (mout->dig_out_nid && mout->share_spdif &&
5200 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005202 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5203 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005204 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005206 setup_dig_out_stream(codec, mout->dig_out_nid,
5207 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 } else {
5209 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005210 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 }
5212 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005213 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
5215 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005216 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5217 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005218 if (!mout->no_share_stream &&
5219 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005221 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5222 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005223 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005224 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5225 if (!mout->no_share_stream && mout->hp_out_nid[i])
5226 snd_hda_codec_setup_stream(codec,
5227 mout->hp_out_nid[i],
5228 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005229 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005230 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005231 snd_hda_codec_setup_stream(codec,
5232 mout->extra_out_nid[i],
5233 stream_tag, 0, format);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 /* surrounds */
5236 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005237 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005238 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5239 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005240 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005241 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5242 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244 return 0;
5245}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005246EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Takashi Iwaid5191e52009-11-16 14:58:17 +01005248/**
5249 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005251int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5252 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
Takashi Iwaidda14412011-05-02 11:29:30 +02005254 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 int i;
5256
5257 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005258 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005260 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005261 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5262 if (mout->hp_out_nid[i])
5263 snd_hda_codec_cleanup_stream(codec,
5264 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005265 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5266 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005267 snd_hda_codec_cleanup_stream(codec,
5268 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005269 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005271 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 mout->dig_out_used = 0;
5273 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005274 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 return 0;
5276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005277EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Takashi Iwai47408602012-04-20 13:06:53 +02005279/**
5280 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5281 *
5282 * Guess the suitable VREF pin bits to be set as the pin-control value.
5283 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5284 */
5285unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5286{
5287 unsigned int pincap;
5288 unsigned int oldval;
5289 oldval = snd_hda_codec_read(codec, pin, 0,
5290 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5291 pincap = snd_hda_query_pin_caps(codec, pin);
5292 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5293 /* Exception: if the default pin setup is vref50, we give it priority */
5294 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5295 return AC_PINCTL_VREF_80;
5296 else if (pincap & AC_PINCAP_VREF_50)
5297 return AC_PINCTL_VREF_50;
5298 else if (pincap & AC_PINCAP_VREF_100)
5299 return AC_PINCTL_VREF_100;
5300 else if (pincap & AC_PINCAP_VREF_GRD)
5301 return AC_PINCTL_VREF_GRD;
5302 return AC_PINCTL_VREF_HIZ;
5303}
5304EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5305
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005306/* correct the pin ctl value for matching with the pin cap */
5307unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5308 hda_nid_t pin, unsigned int val)
5309{
5310 static unsigned int cap_lists[][2] = {
5311 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5312 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5313 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5314 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5315 };
5316 unsigned int cap;
5317
5318 if (!val)
5319 return 0;
5320 cap = snd_hda_query_pin_caps(codec, pin);
5321 if (!cap)
5322 return val; /* don't know what to do... */
5323
5324 if (val & AC_PINCTL_OUT_EN) {
5325 if (!(cap & AC_PINCAP_OUT))
5326 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5327 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5328 val &= ~AC_PINCTL_HP_EN;
5329 }
5330
5331 if (val & AC_PINCTL_IN_EN) {
5332 if (!(cap & AC_PINCAP_IN))
5333 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5334 else {
5335 unsigned int vcap, vref;
5336 int i;
5337 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5338 vref = val & AC_PINCTL_VREFEN;
5339 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5340 if (vref == cap_lists[i][0] &&
5341 !(vcap & cap_lists[i][1])) {
5342 if (i == ARRAY_SIZE(cap_lists) - 1)
5343 vref = AC_PINCTL_VREF_HIZ;
5344 else
5345 vref = cap_lists[i + 1][0];
5346 }
5347 }
5348 val &= ~AC_PINCTL_VREFEN;
5349 val |= vref;
5350 }
5351 }
5352
5353 return val;
5354}
5355EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5356
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005357int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5358 unsigned int val, bool cached)
5359{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005360 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005361 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005362 if (cached)
5363 return snd_hda_codec_update_cache(codec, pin, 0,
5364 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5365 else
5366 return snd_hda_codec_write(codec, pin, 0,
5367 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5368}
5369EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5370
Takashi Iwai990061c2010-09-09 22:08:44 +02005371/**
5372 * snd_hda_add_imux_item - Add an item to input_mux
5373 *
5374 * When the same label is used already in the existing items, the number
5375 * suffix is appended to the label. This label index number is stored
5376 * to type_idx when non-NULL pointer is given.
5377 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005378int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5379 int index, int *type_idx)
5380{
5381 int i, label_idx = 0;
5382 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5383 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5384 return -EINVAL;
5385 }
5386 for (i = 0; i < imux->num_items; i++) {
5387 if (!strncmp(label, imux->items[i].label, strlen(label)))
5388 label_idx++;
5389 }
5390 if (type_idx)
5391 *type_idx = label_idx;
5392 if (label_idx > 0)
5393 snprintf(imux->items[imux->num_items].label,
5394 sizeof(imux->items[imux->num_items].label),
5395 "%s %d", label, label_idx);
5396 else
5397 strlcpy(imux->items[imux->num_items].label, label,
5398 sizeof(imux->items[imux->num_items].label));
5399 imux->items[imux->num_items].index = index;
5400 imux->num_items++;
5401 return 0;
5402}
5403EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005404
Takashi Iwai4a471b72005-12-07 13:56:29 +01005405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406#ifdef CONFIG_PM
5407/*
5408 * power management
5409 */
5410
5411/**
5412 * snd_hda_suspend - suspend the codecs
5413 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 *
5415 * Returns 0 if successful.
5416 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005417int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005419 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Takashi Iwai0ba21762007-04-16 11:29:14 +02005421 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005422 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005423 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005424 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 }
5426 return 0;
5427}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005428EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430/**
5431 * snd_hda_resume - resume the codecs
5432 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 *
5434 * Returns 0 if successful.
5435 */
5436int snd_hda_resume(struct hda_bus *bus)
5437{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005438 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Takashi Iwai0ba21762007-04-16 11:29:14 +02005440 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005441 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 return 0;
5444}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005445EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005446#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005447
5448/*
5449 * generic arrays
5450 */
5451
Takashi Iwaid5191e52009-11-16 14:58:17 +01005452/**
5453 * snd_array_new - get a new element from the given array
5454 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005455 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005456 * Get a new element from the given array. If it exceeds the
5457 * pre-allocated array size, re-allocate the array.
5458 *
5459 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005460 */
5461void *snd_array_new(struct snd_array *array)
5462{
Takashi Iwai12f17712012-10-10 08:59:14 +02005463 if (snd_BUG_ON(!array->elem_size))
5464 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005465 if (array->used >= array->alloced) {
5466 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005467 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005468 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005469 void *nlist;
5470 if (snd_BUG_ON(num >= 4096))
5471 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005472 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005473 if (!nlist)
5474 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005475 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005476 array->list = nlist;
5477 array->alloced = num;
5478 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005479 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005480}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005481EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005482
Takashi Iwaid5191e52009-11-16 14:58:17 +01005483/**
5484 * snd_array_free - free the given array elements
5485 * @array: the array object
5486 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005487void snd_array_free(struct snd_array *array)
5488{
5489 kfree(array->list);
5490 array->used = 0;
5491 array->alloced = 0;
5492 array->list = NULL;
5493}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005494EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005495
Takashi Iwaid5191e52009-11-16 14:58:17 +01005496/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005497 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5498 * @pcm: PCM caps bits
5499 * @buf: the string buffer to write
5500 * @buflen: the max buffer length
5501 *
5502 * used by hda_proc.c and hda_eld.c
5503 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005504void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5505{
5506 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5507 int i, j;
5508
5509 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5510 if (pcm & (AC_SUPPCM_BITS_8 << i))
5511 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5512
5513 buf[j] = '\0'; /* necessary when j == 0 */
5514}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005515EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005516
5517MODULE_DESCRIPTION("HDA codec core");
5518MODULE_LICENSE("GPL");