blob: 0a531f2f9255c012edb06b090ec552c9ee29ebfc [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 Iwai346ff702009-02-23 09:42:57 +01001089 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 if (pin)
1091 return pin->cfg;
1092#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001093 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1094 if (pin)
1095 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001096 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1097 if (pin)
1098 return pin->cfg;
1099 return 0;
1100}
1101EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1102
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001103/* remember the current pinctl target value */
1104int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1105 unsigned int val)
1106{
1107 struct hda_pincfg *pin;
1108
1109 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1110 if (!pin)
1111 return -EINVAL;
1112 pin->target = val;
1113 return 0;
1114}
1115EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1116
1117/* return the current pinctl target value */
1118int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1119{
1120 struct hda_pincfg *pin;
1121
1122 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1123 if (!pin)
1124 return 0;
1125 return pin->target;
1126}
1127EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1128
Takashi Iwai92ee6162009-12-27 11:18:59 +01001129/**
1130 * snd_hda_shutup_pins - Shut up all pins
1131 * @codec: the HDA codec
1132 *
1133 * Clear all pin controls to shup up before suspend for avoiding click noise.
1134 * The controls aren't cached so that they can be resumed properly.
1135 */
1136void snd_hda_shutup_pins(struct hda_codec *codec)
1137{
1138 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001139 /* don't shut up pins when unloading the driver; otherwise it breaks
1140 * the default pin setup at the next load of the driver
1141 */
1142 if (codec->bus->shutdown)
1143 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001144 for (i = 0; i < codec->init_pins.used; i++) {
1145 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1146 /* use read here for syncing after issuing each verb */
1147 snd_hda_codec_read(codec, pin->nid, 0,
1148 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1149 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001150 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001151}
1152EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1153
Takashi Iwai2a439522011-07-26 09:52:50 +02001154#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001155/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1156static void restore_shutup_pins(struct hda_codec *codec)
1157{
1158 int i;
1159 if (!codec->pins_shutup)
1160 return;
1161 if (codec->bus->shutdown)
1162 return;
1163 for (i = 0; i < codec->init_pins.used; i++) {
1164 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1165 snd_hda_codec_write(codec, pin->nid, 0,
1166 AC_VERB_SET_PIN_WIDGET_CONTROL,
1167 pin->ctrl);
1168 }
1169 codec->pins_shutup = 0;
1170}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001171#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001172
David Henningsson26a6cb62012-10-09 15:04:21 +02001173static void hda_jackpoll_work(struct work_struct *work)
1174{
1175 struct hda_codec *codec =
1176 container_of(work, struct hda_codec, jackpoll_work.work);
1177 if (!codec->jackpoll_interval)
1178 return;
1179
1180 snd_hda_jack_set_dirty_all(codec);
1181 snd_hda_jack_poll_all(codec);
1182 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1183 codec->jackpoll_interval);
1184}
1185
Takashi Iwai01751f52007-08-10 16:59:39 +02001186static void init_hda_cache(struct hda_cache_rec *cache,
1187 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001188static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001189
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001190/* release all pincfg lists */
1191static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001192{
Takashi Iwai346ff702009-02-23 09:42:57 +01001193 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001194#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001195 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001196#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001197 snd_array_free(&codec->init_pins);
1198}
1199
Takashi Iwai54d17402005-11-21 16:33:22 +01001200/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001201 * audio-converter setup caches
1202 */
1203struct hda_cvt_setup {
1204 hda_nid_t nid;
1205 u8 stream_tag;
1206 u8 channel_id;
1207 u16 format_id;
1208 unsigned char active; /* cvt is currently used */
1209 unsigned char dirty; /* setups should be cleared */
1210};
1211
1212/* get or create a cache entry for the given audio converter NID */
1213static struct hda_cvt_setup *
1214get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1215{
1216 struct hda_cvt_setup *p;
1217 int i;
1218
1219 for (i = 0; i < codec->cvt_setups.used; i++) {
1220 p = snd_array_elem(&codec->cvt_setups, i);
1221 if (p->nid == nid)
1222 return p;
1223 }
1224 p = snd_array_new(&codec->cvt_setups);
1225 if (p)
1226 p->nid = nid;
1227 return p;
1228}
1229
1230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * codec destructor
1232 */
1233static void snd_hda_codec_free(struct hda_codec *codec)
1234{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001235 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001237 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001238 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001239 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001240#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001241 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001242 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001245 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001246 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001247 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001248 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001249 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 codec->bus->caddr_tbl[codec->addr] = NULL;
1251 if (codec->patch_ops.free)
1252 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001253#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001254 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001255 hda_call_pm_notify(codec->bus, false);
1256#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001257 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001258 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001259 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001260 kfree(codec->vendor_name);
1261 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001262 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001263 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 kfree(codec);
1265}
1266
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001267static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1268 hda_nid_t fg, unsigned int power_state);
1269
Takashi Iwaid8193872012-08-31 07:54:38 -07001270static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001271 unsigned int power_state);
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/**
1274 * snd_hda_codec_new - create a HDA codec
1275 * @bus: the bus to assign
1276 * @codec_addr: the codec address
1277 * @codecp: the pointer to store the generated codec
1278 *
1279 * Returns 0 if successful, or a negative error code.
1280 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001281int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001282 unsigned int codec_addr,
1283 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001286 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001287 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int err;
1289
Takashi Iwaida3cec32008-08-08 17:12:14 +02001290 if (snd_BUG_ON(!bus))
1291 return -EINVAL;
1292 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 snd_printk(KERN_ERR "hda_codec: "
1297 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EBUSY;
1299 }
1300
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001301 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (codec == NULL) {
1303 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1304 return -ENOMEM;
1305 }
1306
1307 codec->bus = bus;
1308 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001309 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001310 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001311 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001312 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001313 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001314 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1315 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001316 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001317 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001318 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001319 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001320 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001321 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001322 INIT_LIST_HEAD(&codec->conn_list);
1323
David Henningsson26a6cb62012-10-09 15:04:21 +02001324 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Takashi Iwai83012a72012-08-24 18:38:08 +02001326#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001327 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001328 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1329 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1330 * the caller has to power down appropriatley after initialization
1331 * phase.
1332 */
1333 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001334 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001335#endif
1336
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001337 if (codec->bus->modelname) {
1338 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1339 if (!codec->modelname) {
1340 snd_hda_codec_free(codec);
1341 return -ENODEV;
1342 }
1343 }
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 list_add_tail(&codec->list, &bus->codec_list);
1346 bus->caddr_tbl[codec_addr] = codec;
1347
Takashi Iwai0ba21762007-04-16 11:29:14 +02001348 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1349 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001350 if (codec->vendor_id == -1)
1351 /* read again, hopefully the access method was corrected
1352 * in the last read...
1353 */
1354 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1355 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001356 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1357 AC_PAR_SUBSYSTEM_ID);
1358 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1359 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001361 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001362 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001363 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001364 err = -ENODEV;
1365 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Takashi Iwaid8193872012-08-31 07:54:38 -07001368 fg = codec->afg ? codec->afg : codec->mfg;
1369 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001370 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001371 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001372 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001373 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001374 err = read_pin_defaults(codec);
1375 if (err < 0)
1376 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001377
Takashi Iwai0ba21762007-04-16 11:29:14 +02001378 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001379 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001380 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001381 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001382 }
1383
Takashi Iwai83012a72012-08-24 18:38:08 +02001384#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001385 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001386 AC_PWRST_CLKSTOP);
1387 if (!codec->d3_stop_clk)
1388 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001389#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001390 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001391 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001392
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001393 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001394 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001395
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001396 snd_hda_codec_proc_new(codec);
1397
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001398 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001399
1400 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1401 codec->subsystem_id, codec->revision_id);
1402 snd_component_add(codec->bus->card, component);
1403
1404 if (codecp)
1405 *codecp = codec;
1406 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001407
1408 error:
1409 snd_hda_codec_free(codec);
1410 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001411}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001412EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001413
Takashi Iwaid5191e52009-11-16 14:58:17 +01001414/**
1415 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1416 * @codec: the HDA codec
1417 *
1418 * Start parsing of the given codec tree and (re-)initialize the whole
1419 * patch instance.
1420 *
1421 * Returns 0 if successful or a negative error code.
1422 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001423int snd_hda_codec_configure(struct hda_codec *codec)
1424{
1425 int err;
1426
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001427 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001428 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001429 err = get_codec_name(codec);
1430 if (err < 0)
1431 return err;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Takashi Iwai82467612007-07-27 19:15:54 +02001434 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001436 goto patched;
1437 }
Takashi Iwai82467612007-07-27 19:15:54 +02001438 if (codec->preset && codec->preset->patch) {
1439 err = codec->preset->patch(codec);
1440 goto patched;
1441 }
1442
1443 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001444 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001445 if (err < 0)
1446 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001447
1448 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001449 if (!err && codec->patch_ops.unsol_event)
1450 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001451 /* audio codec should override the mixer name */
1452 if (!err && (codec->afg || !*codec->bus->card->mixername))
1453 snprintf(codec->bus->card->mixername,
1454 sizeof(codec->bus->card->mixername),
1455 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001458EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Takashi Iwaied360812012-08-08 17:12:52 +02001460/* update the stream-id if changed */
1461static void update_pcm_stream_id(struct hda_codec *codec,
1462 struct hda_cvt_setup *p, hda_nid_t nid,
1463 u32 stream_tag, int channel_id)
1464{
1465 unsigned int oldval, newval;
1466
1467 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1468 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1469 newval = (stream_tag << 4) | channel_id;
1470 if (oldval != newval)
1471 snd_hda_codec_write(codec, nid, 0,
1472 AC_VERB_SET_CHANNEL_STREAMID,
1473 newval);
1474 p->stream_tag = stream_tag;
1475 p->channel_id = channel_id;
1476 }
1477}
1478
1479/* update the format-id if changed */
1480static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1481 hda_nid_t nid, int format)
1482{
1483 unsigned int oldval;
1484
1485 if (p->format_id != format) {
1486 oldval = snd_hda_codec_read(codec, nid, 0,
1487 AC_VERB_GET_STREAM_FORMAT, 0);
1488 if (oldval != format) {
1489 msleep(1);
1490 snd_hda_codec_write(codec, nid, 0,
1491 AC_VERB_SET_STREAM_FORMAT,
1492 format);
1493 }
1494 p->format_id = format;
1495 }
1496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/**
1499 * snd_hda_codec_setup_stream - set up the codec for streaming
1500 * @codec: the CODEC to set up
1501 * @nid: the NID to set up
1502 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1503 * @channel_id: channel id to pass, zero based.
1504 * @format: stream format.
1505 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001506void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1507 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int channel_id, int format)
1509{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001510 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001511 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001512 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001513 int i;
1514
Takashi Iwai0ba21762007-04-16 11:29:14 +02001515 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001516 return;
1517
Takashi Iwai0ba21762007-04-16 11:29:14 +02001518 snd_printdd("hda_codec_setup_stream: "
1519 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001522 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001523 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001524
1525 if (codec->pcm_format_first)
1526 update_pcm_format(codec, p, nid, format);
1527 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1528 if (!codec->pcm_format_first)
1529 update_pcm_format(codec, p, nid, format);
1530
Takashi Iwaieb541332010-08-06 13:48:11 +02001531 p->active = 1;
1532 p->dirty = 0;
1533
1534 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001535 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001536 list_for_each_entry(c, &codec->bus->codec_list, list) {
1537 for (i = 0; i < c->cvt_setups.used; i++) {
1538 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001539 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001540 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001541 p->dirty = 1;
1542 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001545EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Takashi Iwaif0cea792010-08-13 11:56:53 +02001547static void really_cleanup_stream(struct hda_codec *codec,
1548 struct hda_cvt_setup *q);
1549
Takashi Iwaid5191e52009-11-16 14:58:17 +01001550/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001551 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001552 * @codec: the CODEC to clean up
1553 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001554 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001555 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001556void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1557 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001558{
Takashi Iwaieb541332010-08-06 13:48:11 +02001559 struct hda_cvt_setup *p;
1560
Takashi Iwai888afa12008-03-18 09:57:50 +01001561 if (!nid)
1562 return;
1563
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001564 if (codec->no_sticky_stream)
1565 do_now = 1;
1566
Takashi Iwai888afa12008-03-18 09:57:50 +01001567 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001568 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001569 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001570 /* here we just clear the active flag when do_now isn't set;
1571 * actual clean-ups will be done later in
1572 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1573 */
1574 if (do_now)
1575 really_cleanup_stream(codec, p);
1576 else
1577 p->active = 0;
1578 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001579}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001580EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001581
Takashi Iwaieb541332010-08-06 13:48:11 +02001582static void really_cleanup_stream(struct hda_codec *codec,
1583 struct hda_cvt_setup *q)
1584{
1585 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001586 if (q->stream_tag || q->channel_id)
1587 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1588 if (q->format_id)
1589 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1590);
Takashi Iwaieb541332010-08-06 13:48:11 +02001591 memset(q, 0, sizeof(*q));
1592 q->nid = nid;
1593}
1594
1595/* clean up the all conflicting obsolete streams */
1596static void purify_inactive_streams(struct hda_codec *codec)
1597{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001598 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001599 int i;
1600
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001601 list_for_each_entry(c, &codec->bus->codec_list, list) {
1602 for (i = 0; i < c->cvt_setups.used; i++) {
1603 struct hda_cvt_setup *p;
1604 p = snd_array_elem(&c->cvt_setups, i);
1605 if (p->dirty)
1606 really_cleanup_stream(c, p);
1607 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001608 }
1609}
1610
Takashi Iwai2a439522011-07-26 09:52:50 +02001611#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001612/* clean up all streams; called from suspend */
1613static void hda_cleanup_all_streams(struct hda_codec *codec)
1614{
1615 int i;
1616
1617 for (i = 0; i < codec->cvt_setups.used; i++) {
1618 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1619 if (p->stream_tag)
1620 really_cleanup_stream(codec, p);
1621 }
1622}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001623#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625/*
1626 * amp access functions
1627 */
1628
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001629/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001630#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001631#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001632#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1633#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001635#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001638static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001639 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwai01751f52007-08-10 16:59:39 +02001641 memset(cache, 0, sizeof(*cache));
1642 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001643 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001644}
1645
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001646static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001647{
Takashi Iwai603c4012008-07-30 15:01:44 +02001648 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001652static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Takashi Iwai01751f52007-08-10 16:59:39 +02001654 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1655 u16 cur = cache->hash[idx];
1656 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001659 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (info->key == key)
1661 return info;
1662 cur = info->next;
1663 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001664 return NULL;
1665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001667/* query the hash. allocate an entry if not found. */
1668static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1669 u32 key)
1670{
1671 struct hda_cache_head *info = get_hash(cache, key);
1672 if (!info) {
1673 u16 idx, cur;
1674 /* add a new hash entry */
1675 info = snd_array_new(&cache->buf);
1676 if (!info)
1677 return NULL;
1678 cur = snd_array_index(&cache->buf, info);
1679 info->key = key;
1680 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001681 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001682 idx = key % (u16)ARRAY_SIZE(cache->hash);
1683 info->next = cache->hash[idx];
1684 cache->hash[idx] = cur;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return info;
1687}
1688
Takashi Iwai01751f52007-08-10 16:59:39 +02001689/* query and allocate an amp hash entry */
1690static inline struct hda_amp_info *
1691get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1692{
1693 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1694}
1695
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001696/* overwrite the value with the key in the caps hash */
1697static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1698{
1699 struct hda_amp_info *info;
1700
1701 mutex_lock(&codec->hash_mutex);
1702 info = get_alloc_amp_hash(codec, key);
1703 if (!info) {
1704 mutex_unlock(&codec->hash_mutex);
1705 return -EINVAL;
1706 }
1707 info->amp_caps = val;
1708 info->head.val |= INFO_AMP_CAPS;
1709 mutex_unlock(&codec->hash_mutex);
1710 return 0;
1711}
1712
1713/* query the value from the caps hash; if not found, fetch the current
1714 * value from the given function and store in the hash
1715 */
1716static unsigned int
1717query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1718 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1719{
1720 struct hda_amp_info *info;
1721 unsigned int val;
1722
1723 mutex_lock(&codec->hash_mutex);
1724 info = get_alloc_amp_hash(codec, key);
1725 if (!info) {
1726 mutex_unlock(&codec->hash_mutex);
1727 return 0;
1728 }
1729 if (!(info->head.val & INFO_AMP_CAPS)) {
1730 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1731 val = func(codec, nid, dir);
1732 write_caps_hash(codec, key, val);
1733 } else {
1734 val = info->amp_caps;
1735 mutex_unlock(&codec->hash_mutex);
1736 }
1737 return val;
1738}
1739
1740static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1741 int direction)
1742{
1743 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1744 nid = codec->afg;
1745 return snd_hda_param_read(codec, nid,
1746 direction == HDA_OUTPUT ?
1747 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1748}
1749
Takashi Iwaid5191e52009-11-16 14:58:17 +01001750/**
1751 * query_amp_caps - query AMP capabilities
1752 * @codec: the HD-auio codec
1753 * @nid: the NID to query
1754 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1755 *
1756 * Query AMP capabilities for the given widget and direction.
1757 * Returns the obtained capability bits.
1758 *
1759 * When cap bits have been already read, this doesn't read again but
1760 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001762u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001764 return query_caps_hash(codec, nid, direction,
1765 HDA_HASH_KEY(nid, direction, 0),
1766 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001768EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Takashi Iwaid5191e52009-11-16 14:58:17 +01001770/**
1771 * snd_hda_override_amp_caps - Override the AMP capabilities
1772 * @codec: the CODEC to clean up
1773 * @nid: the NID to clean up
1774 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1775 * @caps: the capability bits to set
1776 *
1777 * Override the cached AMP caps bits value by the given one.
1778 * This function is useful if the driver needs to adjust the AMP ranges,
1779 * e.g. limit to 0dB, etc.
1780 *
1781 * Returns zero if successful or a negative error code.
1782 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001783int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1784 unsigned int caps)
1785{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001786 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001788EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001789
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001790static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1791 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001792{
1793 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1794}
1795
Takashi Iwaid5191e52009-11-16 14:58:17 +01001796/**
1797 * snd_hda_query_pin_caps - Query PIN capabilities
1798 * @codec: the HD-auio codec
1799 * @nid: the NID to query
1800 *
1801 * Query PIN capabilities for the given widget.
1802 * Returns the obtained capability bits.
1803 *
1804 * When cap bits have been already read, this doesn't read again but
1805 * returns the cached value.
1806 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001807u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1808{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001809 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001810 read_pin_cap);
1811}
Takashi Iwai1327a322009-03-23 13:07:47 +01001812EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1813
Wu Fengguang864f92b2009-11-18 12:38:02 +08001814/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001815 * snd_hda_override_pin_caps - Override the pin capabilities
1816 * @codec: the CODEC
1817 * @nid: the NID to override
1818 * @caps: the capability bits to set
1819 *
1820 * Override the cached PIN capabilitiy bits value by the given one.
1821 *
1822 * Returns zero if successful or a negative error code.
1823 */
1824int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1825 unsigned int caps)
1826{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001827 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001828}
1829EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1830
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001831/* read or sync the hash value with the current value;
1832 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001834static struct hda_amp_info *
1835update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001836 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001838 struct hda_amp_info *info;
1839 unsigned int parm, val = 0;
1840 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001842 retry:
1843 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1844 if (!info)
1845 return NULL;
1846 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1847 if (!val_read) {
1848 mutex_unlock(&codec->hash_mutex);
1849 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1850 parm |= direction == HDA_OUTPUT ?
1851 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1852 parm |= index;
1853 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001854 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001855 val &= 0xff;
1856 val_read = true;
1857 mutex_lock(&codec->hash_mutex);
1858 goto retry;
1859 }
1860 info->vol[ch] = val;
1861 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001862 } else if (init_only)
1863 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001864 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001868 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001870static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001871 hda_nid_t nid, int ch, int direction, int index,
1872 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 u32 parm;
1875
1876 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1877 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1878 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001879 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1880 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001881 ; /* set the zero value as a fake mute */
1882 else
1883 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1885}
1886
Takashi Iwaid5191e52009-11-16 14:58:17 +01001887/**
1888 * snd_hda_codec_amp_read - Read AMP value
1889 * @codec: HD-audio codec
1890 * @nid: NID to read the AMP value
1891 * @ch: channel (left=0 or right=1)
1892 * @direction: #HDA_INPUT or #HDA_OUTPUT
1893 * @index: the index value (only for input direction)
1894 *
1895 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Takashi Iwai834be882006-03-01 14:16:17 +01001897int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1898 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001900 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001901 unsigned int val = 0;
1902
1903 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001904 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001905 if (info)
1906 val = info->vol[ch];
1907 mutex_unlock(&codec->hash_mutex);
1908 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001910EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Takashi Iwai280e57d2012-12-14 10:32:21 +01001912static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1913 int direction, int idx, int mask, int val,
1914 bool init_only)
1915{
1916 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001917 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001918 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001919
1920 if (snd_BUG_ON(mask & ~0xff))
1921 mask &= 0xff;
1922 val &= mask;
1923
1924 mutex_lock(&codec->hash_mutex);
1925 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1926 if (!info) {
1927 mutex_unlock(&codec->hash_mutex);
1928 return 0;
1929 }
1930 val |= info->vol[ch] & ~mask;
1931 if (info->vol[ch] == val) {
1932 mutex_unlock(&codec->hash_mutex);
1933 return 0;
1934 }
1935 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001936 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001937 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001938 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001939 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001940 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001941 return 1;
1942}
1943
Takashi Iwaid5191e52009-11-16 14:58:17 +01001944/**
1945 * snd_hda_codec_amp_update - update the AMP value
1946 * @codec: HD-audio codec
1947 * @nid: NID to read the AMP value
1948 * @ch: channel (left=0 or right=1)
1949 * @direction: #HDA_INPUT or #HDA_OUTPUT
1950 * @idx: the index value (only for input direction)
1951 * @mask: bit mask to set
1952 * @val: the bits value to set
1953 *
1954 * Update the AMP value with a bit mask.
1955 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001956 */
Takashi Iwai834be882006-03-01 14:16:17 +01001957int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1958 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001960 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001962EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Takashi Iwaid5191e52009-11-16 14:58:17 +01001964/**
1965 * snd_hda_codec_amp_stereo - update the AMP stereo values
1966 * @codec: HD-audio codec
1967 * @nid: NID to read the AMP value
1968 * @direction: #HDA_INPUT or #HDA_OUTPUT
1969 * @idx: the index value (only for input direction)
1970 * @mask: bit mask to set
1971 * @val: the bits value to set
1972 *
1973 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1974 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001975 */
1976int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1977 int direction, int idx, int mask, int val)
1978{
1979 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001980
1981 if (snd_BUG_ON(mask & ~0xff))
1982 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001983 for (ch = 0; ch < 2; ch++)
1984 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1985 idx, mask, val);
1986 return ret;
1987}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001988EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001989
Takashi Iwai280e57d2012-12-14 10:32:21 +01001990/* Works like snd_hda_codec_amp_update() but it writes the value only at
1991 * the first access. If the amp was already initialized / updated beforehand,
1992 * this does nothing.
1993 */
1994int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1995 int dir, int idx, int mask, int val)
1996{
1997 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1998}
1999EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2000
2001int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2002 int dir, int idx, int mask, int val)
2003{
2004 int ch, ret = 0;
2005
2006 if (snd_BUG_ON(mask & ~0xff))
2007 mask &= 0xff;
2008 for (ch = 0; ch < 2; ch++)
2009 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2010 idx, mask, val);
2011 return ret;
2012}
2013EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2014
Takashi Iwaid5191e52009-11-16 14:58:17 +01002015/**
2016 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2017 * @codec: HD-audio codec
2018 *
2019 * Resume the all amp commands from the cache.
2020 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002021void snd_hda_codec_resume_amp(struct hda_codec *codec)
2022{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002023 int i;
2024
Takashi Iwaic370dd62012-12-13 18:30:04 +01002025 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002026 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002027 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2028 struct hda_amp_info *buffer;
2029 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002030 hda_nid_t nid;
2031 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002032 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002033
2034 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002035 if (!buffer->head.dirty)
2036 continue;
2037 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002038 info = *buffer;
2039 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002040 if (!key)
2041 continue;
2042 nid = key & 0xff;
2043 idx = (key >> 16) & 0xff;
2044 dir = (key >> 24) & 0xff;
2045 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002046 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002047 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002048 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002049 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2050 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002051 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002052 }
2053 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002054 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002056EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002058static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2059 unsigned int ofs)
2060{
2061 u32 caps = query_amp_caps(codec, nid, dir);
2062 /* get num steps */
2063 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2064 if (ofs < caps)
2065 caps -= ofs;
2066 return caps;
2067}
2068
Takashi Iwaid5191e52009-11-16 14:58:17 +01002069/**
2070 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2071 *
2072 * The control element is supposed to have the private_value field
2073 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2074 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002075int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2076 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2079 u16 nid = get_amp_nid(kcontrol);
2080 u8 chs = get_amp_channels(kcontrol);
2081 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002082 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002084 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2085 uinfo->count = chs == 3 ? 2 : 1;
2086 uinfo->value.integer.min = 0;
2087 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2088 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002089 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002090 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2091 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return -EINVAL;
2093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002096EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002098
2099static inline unsigned int
2100read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2101 int ch, int dir, int idx, unsigned int ofs)
2102{
2103 unsigned int val;
2104 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2105 val &= HDA_AMP_VOLMASK;
2106 if (val >= ofs)
2107 val -= ofs;
2108 else
2109 val = 0;
2110 return val;
2111}
2112
2113static inline int
2114update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2115 int ch, int dir, int idx, unsigned int ofs,
2116 unsigned int val)
2117{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002118 unsigned int maxval;
2119
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002120 if (val > 0)
2121 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002122 /* ofs = 0: raw max value */
2123 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002124 if (val > maxval)
2125 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002126 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2127 HDA_AMP_VOLMASK, val);
2128}
2129
Takashi Iwaid5191e52009-11-16 14:58:17 +01002130/**
2131 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2132 *
2133 * The control element is supposed to have the private_value field
2134 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2135 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002136int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 hda_nid_t nid = get_amp_nid(kcontrol);
2141 int chs = get_amp_channels(kcontrol);
2142 int dir = get_amp_direction(kcontrol);
2143 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002144 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 long *valp = ucontrol->value.integer.value;
2146
2147 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002148 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002150 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return 0;
2152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002153EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Takashi Iwaid5191e52009-11-16 14:58:17 +01002155/**
2156 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2157 *
2158 * The control element is supposed to have the private_value field
2159 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2160 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002161int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2165 hda_nid_t nid = get_amp_nid(kcontrol);
2166 int chs = get_amp_channels(kcontrol);
2167 int dir = get_amp_direction(kcontrol);
2168 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002169 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 long *valp = ucontrol->value.integer.value;
2171 int change = 0;
2172
Takashi Iwaicb53c622007-08-10 17:21:45 +02002173 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002174 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002175 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002176 valp++;
2177 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002178 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002179 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002180 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return change;
2182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002183EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Takashi Iwaid5191e52009-11-16 14:58:17 +01002185/**
2186 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2187 *
2188 * The control element is supposed to have the private_value field
2189 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2190 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002191int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2192 unsigned int size, unsigned int __user *_tlv)
2193{
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 hda_nid_t nid = get_amp_nid(kcontrol);
2196 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002197 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002198 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002199 u32 caps, val1, val2;
2200
2201 if (size < 4 * sizeof(unsigned int))
2202 return -ENOMEM;
2203 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002204 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2205 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002206 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002207 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002208 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002209 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002210 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002211 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2212 return -EFAULT;
2213 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2214 return -EFAULT;
2215 if (put_user(val1, _tlv + 2))
2216 return -EFAULT;
2217 if (put_user(val2, _tlv + 3))
2218 return -EFAULT;
2219 return 0;
2220}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002221EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002222
Takashi Iwaid5191e52009-11-16 14:58:17 +01002223/**
2224 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2225 * @codec: HD-audio codec
2226 * @nid: NID of a reference widget
2227 * @dir: #HDA_INPUT or #HDA_OUTPUT
2228 * @tlv: TLV data to be stored, at least 4 elements
2229 *
2230 * Set (static) TLV data for a virtual master volume using the AMP caps
2231 * obtained from the reference NID.
2232 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002233 */
2234void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2235 unsigned int *tlv)
2236{
2237 u32 caps;
2238 int nums, step;
2239
2240 caps = query_amp_caps(codec, nid, dir);
2241 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2242 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2243 step = (step + 1) * 25;
2244 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2245 tlv[1] = 2 * sizeof(unsigned int);
2246 tlv[2] = -nums * step;
2247 tlv[3] = step;
2248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002249EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002250
2251/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002252static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002253find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002254{
2255 struct snd_ctl_elem_id id;
2256 memset(&id, 0, sizeof(id));
2257 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002258 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002259 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002260 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2261 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002262 strcpy(id.name, name);
2263 return snd_ctl_find_id(codec->bus->card, &id);
2264}
2265
Takashi Iwaid5191e52009-11-16 14:58:17 +01002266/**
2267 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2268 * @codec: HD-audio codec
2269 * @name: ctl id name string
2270 *
2271 * Get the control element with the given id string and IFACE_MIXER.
2272 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002273struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2274 const char *name)
2275{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002276 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002277}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002278EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002279
Takashi Iwaidcda5802012-10-12 17:24:51 +02002280static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2281 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002282{
2283 int idx;
2284 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002285 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002286 return idx;
2287 }
2288 return -EBUSY;
2289}
2290
Takashi Iwaid5191e52009-11-16 14:58:17 +01002291/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002292 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002293 * @codec: HD-audio codec
2294 * @nid: corresponding NID (optional)
2295 * @kctl: the control element to assign
2296 *
2297 * Add the given control element to an array inside the codec instance.
2298 * All control elements belonging to a codec are supposed to be added
2299 * by this function so that a proper clean-up works at the free or
2300 * reconfiguration time.
2301 *
2302 * If non-zero @nid is passed, the NID is assigned to the control element.
2303 * The assignment is shown in the codec proc file.
2304 *
2305 * snd_hda_ctl_add() checks the control subdev id field whether
2306 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002307 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2308 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002309 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002310int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2311 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312{
2313 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002314 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002315 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002316
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002317 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002318 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002319 if (nid == 0)
2320 nid = get_amp_nid_(kctl->private_value);
2321 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002322 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2323 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002324 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002325 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002326 err = snd_ctl_add(codec->bus->card, kctl);
2327 if (err < 0)
2328 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002329 item = snd_array_new(&codec->mixers);
2330 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002331 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002332 item->kctl = kctl;
2333 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002334 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002335 return 0;
2336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002337EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002338
Takashi Iwaid5191e52009-11-16 14:58:17 +01002339/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002340 * snd_hda_add_nid - Assign a NID to a control element
2341 * @codec: HD-audio codec
2342 * @nid: corresponding NID (optional)
2343 * @kctl: the control element to assign
2344 * @index: index to kctl
2345 *
2346 * Add the given control element to an array inside the codec instance.
2347 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2348 * NID:KCTL mapping - for example "Capture Source" selector.
2349 */
2350int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2351 unsigned int index, hda_nid_t nid)
2352{
2353 struct hda_nid_item *item;
2354
2355 if (nid > 0) {
2356 item = snd_array_new(&codec->nids);
2357 if (!item)
2358 return -ENOMEM;
2359 item->kctl = kctl;
2360 item->index = index;
2361 item->nid = nid;
2362 return 0;
2363 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002364 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2365 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002366 return -EINVAL;
2367}
2368EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2369
2370/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002371 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2372 * @codec: HD-audio codec
2373 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002374void snd_hda_ctls_clear(struct hda_codec *codec)
2375{
2376 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002377 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002378 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002379 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002380 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002381 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002382}
2383
Takashi Iwaia65d6292009-02-23 16:57:04 +01002384/* pseudo device locking
2385 * toggle card->shutdown to allow/disallow the device access (as a hack)
2386 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002387int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002388{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002389 struct snd_card *card = bus->card;
2390 struct hda_codec *codec;
2391
Takashi Iwaia65d6292009-02-23 16:57:04 +01002392 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002393 if (card->shutdown)
2394 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002395 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002396 if (!list_empty(&card->ctl_files))
2397 goto err_clear;
2398
2399 list_for_each_entry(codec, &bus->codec_list, list) {
2400 int pcm;
2401 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2402 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2403 if (!cpcm->pcm)
2404 continue;
2405 if (cpcm->pcm->streams[0].substream_opened ||
2406 cpcm->pcm->streams[1].substream_opened)
2407 goto err_clear;
2408 }
2409 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002410 spin_unlock(&card->files_lock);
2411 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002412
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002413 err_clear:
2414 card->shutdown = 0;
2415 err_unlock:
2416 spin_unlock(&card->files_lock);
2417 return -EINVAL;
2418}
2419EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2420
2421void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002422{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 struct snd_card *card = bus->card;
2424
2425 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002426 spin_lock(&card->files_lock);
2427 card->shutdown = 0;
2428 spin_unlock(&card->files_lock);
2429}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002430EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002431
Takashi Iwaid5191e52009-11-16 14:58:17 +01002432/**
2433 * snd_hda_codec_reset - Clear all objects assigned to the codec
2434 * @codec: HD-audio codec
2435 *
2436 * This frees the all PCM and control elements assigned to the codec, and
2437 * clears the caches and restores the pin default configurations.
2438 *
2439 * When a device is being used, it returns -EBSY. If successfully freed,
2440 * returns zero.
2441 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002442int snd_hda_codec_reset(struct hda_codec *codec)
2443{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002444 struct hda_bus *bus = codec->bus;
2445 struct snd_card *card = bus->card;
2446 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002447
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002448 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002449 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002450
2451 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002452 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002453#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002454 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002455 codec->power_on = 0;
2456 codec->power_transition = 0;
2457 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002458 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002459#endif
2460 snd_hda_ctls_clear(codec);
2461 /* relase PCMs */
2462 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002463 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002464 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002465 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002466 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002467 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002468 }
2469 if (codec->patch_ops.free)
2470 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002471 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002472 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002473 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002474 codec->spec = NULL;
2475 free_hda_cache(&codec->amp_cache);
2476 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002477 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2478 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002479 /* free only driver_pins so that init_pins + user_pins are restored */
2480 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002481 snd_array_free(&codec->cvt_setups);
2482 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002483 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002484 codec->num_pcms = 0;
2485 codec->pcm_info = NULL;
2486 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002487 codec->slave_dig_outs = NULL;
2488 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002489 module_put(codec->owner);
2490 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002491
2492 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002493 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002494 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002495}
2496
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002497typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2498
2499/* apply the function to all matching slave ctls in the mixer list */
2500static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002501 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002502{
2503 struct hda_nid_item *items;
2504 const char * const *s;
2505 int i, err;
2506
2507 items = codec->mixers.list;
2508 for (i = 0; i < codec->mixers.used; i++) {
2509 struct snd_kcontrol *sctl = items[i].kctl;
2510 if (!sctl || !sctl->id.name ||
2511 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2512 continue;
2513 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002514 char tmpname[sizeof(sctl->id.name)];
2515 const char *name = *s;
2516 if (suffix) {
2517 snprintf(tmpname, sizeof(tmpname), "%s %s",
2518 name, suffix);
2519 name = tmpname;
2520 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002521 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002522 err = func(data, sctl);
2523 if (err)
2524 return err;
2525 break;
2526 }
2527 }
2528 }
2529 return 0;
2530}
2531
2532static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2533{
2534 return 1;
2535}
2536
Takashi Iwai18478e82012-03-09 17:51:10 +01002537/* guess the value corresponding to 0dB */
2538static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2539{
2540 int _tlv[4];
2541 const int *tlv = NULL;
2542 int val = -1;
2543
2544 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2545 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2546 mm_segment_t fs = get_fs();
2547 set_fs(get_ds());
2548 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2549 tlv = _tlv;
2550 set_fs(fs);
2551 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2552 tlv = kctl->tlv.p;
2553 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2554 val = -tlv[2] / tlv[3];
2555 return val;
2556}
2557
2558/* call kctl->put with the given value(s) */
2559static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2560{
2561 struct snd_ctl_elem_value *ucontrol;
2562 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2563 if (!ucontrol)
2564 return -ENOMEM;
2565 ucontrol->value.integer.value[0] = val;
2566 ucontrol->value.integer.value[1] = val;
2567 kctl->put(kctl, ucontrol);
2568 kfree(ucontrol);
2569 return 0;
2570}
2571
2572/* initialize the slave volume with 0dB */
2573static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2574{
2575 int offset = get_kctl_0dB_offset(slave);
2576 if (offset > 0)
2577 put_kctl_with_value(slave, offset);
2578 return 0;
2579}
2580
2581/* unmute the slave */
2582static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2583{
2584 return put_kctl_with_value(slave, 1);
2585}
2586
Takashi Iwaid5191e52009-11-16 14:58:17 +01002587/**
2588 * snd_hda_add_vmaster - create a virtual master control and add slaves
2589 * @codec: HD-audio codec
2590 * @name: vmaster control name
2591 * @tlv: TLV data (optional)
2592 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002593 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002594 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002595 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002596 *
2597 * Create a virtual master control with the given name. The TLV data
2598 * must be either NULL or a valid data.
2599 *
2600 * @slaves is a NULL-terminated array of strings, each of which is a
2601 * slave control name. All controls with these names are assigned to
2602 * the new virtual master control.
2603 *
2604 * This function returns zero if successful or a negative error code.
2605 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002606int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002607 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002608 const char *suffix, bool init_slave_vol,
2609 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002610{
2611 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002612 int err;
2613
Takashi Iwai29e58532012-03-12 12:25:03 +01002614 if (ctl_ret)
2615 *ctl_ret = NULL;
2616
Takashi Iwai9322ca52012-02-03 14:28:01 +01002617 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002618 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002619 snd_printdd("No slave found for %s\n", name);
2620 return 0;
2621 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002622 kctl = snd_ctl_make_virtual_master(name, tlv);
2623 if (!kctl)
2624 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002625 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002626 if (err < 0)
2627 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002628
Takashi Iwai9322ca52012-02-03 14:28:01 +01002629 err = map_slaves(codec, slaves, suffix,
2630 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002631 if (err < 0)
2632 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002633
2634 /* init with master mute & zero volume */
2635 put_kctl_with_value(kctl, 0);
2636 if (init_slave_vol)
2637 map_slaves(codec, slaves, suffix,
2638 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2639
Takashi Iwai29e58532012-03-12 12:25:03 +01002640 if (ctl_ret)
2641 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002642 return 0;
2643}
Takashi Iwai18478e82012-03-09 17:51:10 +01002644EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002645
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002646/*
2647 * mute-LED control using vmaster
2648 */
2649static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
2651{
2652 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002653 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002654 };
2655 unsigned int index;
2656
2657 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2658 uinfo->count = 1;
2659 uinfo->value.enumerated.items = 3;
2660 index = uinfo->value.enumerated.item;
2661 if (index >= 3)
2662 index = 2;
2663 strcpy(uinfo->value.enumerated.name, texts[index]);
2664 return 0;
2665}
2666
2667static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
2669{
2670 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2671 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2672 return 0;
2673}
2674
2675static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
2677{
2678 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2679 unsigned int old_mode = hook->mute_mode;
2680
2681 hook->mute_mode = ucontrol->value.enumerated.item[0];
2682 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2683 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2684 if (old_mode == hook->mute_mode)
2685 return 0;
2686 snd_hda_sync_vmaster_hook(hook);
2687 return 1;
2688}
2689
2690static struct snd_kcontrol_new vmaster_mute_mode = {
2691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2692 .name = "Mute-LED Mode",
2693 .info = vmaster_mute_mode_info,
2694 .get = vmaster_mute_mode_get,
2695 .put = vmaster_mute_mode_put,
2696};
2697
2698/*
2699 * Add a mute-LED hook with the given vmaster switch kctl
2700 * "Mute-LED Mode" control is automatically created and associated with
2701 * the given hook.
2702 */
2703int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002704 struct hda_vmaster_mute_hook *hook,
2705 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002706{
2707 struct snd_kcontrol *kctl;
2708
2709 if (!hook->hook || !hook->sw_kctl)
2710 return 0;
2711 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2712 hook->codec = codec;
2713 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002714 if (!expose_enum_ctl)
2715 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002716 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2717 if (!kctl)
2718 return -ENOMEM;
2719 return snd_hda_ctl_add(codec, 0, kctl);
2720}
2721EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2722
2723/*
2724 * Call the hook with the current value for synchronization
2725 * Should be called in init callback
2726 */
2727void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2728{
2729 if (!hook->hook || !hook->codec)
2730 return;
2731 switch (hook->mute_mode) {
2732 case HDA_VMUTE_FOLLOW_MASTER:
2733 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2734 break;
2735 default:
2736 hook->hook(hook->codec, hook->mute_mode);
2737 break;
2738 }
2739}
2740EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2741
2742
Takashi Iwaid5191e52009-11-16 14:58:17 +01002743/**
2744 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2745 *
2746 * The control element is supposed to have the private_value field
2747 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2748 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 int chs = get_amp_channels(kcontrol);
2753
2754 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2755 uinfo->count = chs == 3 ? 2 : 1;
2756 uinfo->value.integer.min = 0;
2757 uinfo->value.integer.max = 1;
2758 return 0;
2759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002760EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Takashi Iwaid5191e52009-11-16 14:58:17 +01002762/**
2763 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2764 *
2765 * The control element is supposed to have the private_value field
2766 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2767 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002768int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 hda_nid_t nid = get_amp_nid(kcontrol);
2773 int chs = get_amp_channels(kcontrol);
2774 int dir = get_amp_direction(kcontrol);
2775 int idx = get_amp_index(kcontrol);
2776 long *valp = ucontrol->value.integer.value;
2777
2778 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002779 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002780 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002782 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002783 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002786EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Takashi Iwaid5191e52009-11-16 14:58:17 +01002788/**
2789 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2790 *
2791 * The control element is supposed to have the private_value field
2792 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2793 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002794int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
2797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2798 hda_nid_t nid = get_amp_nid(kcontrol);
2799 int chs = get_amp_channels(kcontrol);
2800 int dir = get_amp_direction(kcontrol);
2801 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 long *valp = ucontrol->value.integer.value;
2803 int change = 0;
2804
Takashi Iwaicb53c622007-08-10 17:21:45 +02002805 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002806 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002807 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002808 HDA_AMP_MUTE,
2809 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002810 valp++;
2811 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002812 if (chs & 2)
2813 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002814 HDA_AMP_MUTE,
2815 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002816 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002817 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return change;
2819}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002820EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822/*
Takashi Iwai985be542005-11-02 18:26:49 +01002823 * bound volume controls
2824 *
2825 * bind multiple volumes (# indices, from 0)
2826 */
2827
2828#define AMP_VAL_IDX_SHIFT 19
2829#define AMP_VAL_IDX_MASK (0x0f<<19)
2830
Takashi Iwaid5191e52009-11-16 14:58:17 +01002831/**
2832 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2833 *
2834 * The control element is supposed to have the private_value field
2835 * set up via HDA_BIND_MUTE*() macros.
2836 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002837int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002839{
2840 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2841 unsigned long pval;
2842 int err;
2843
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002844 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002845 pval = kcontrol->private_value;
2846 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2847 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2848 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002849 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002850 return err;
2851}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002852EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002853
Takashi Iwaid5191e52009-11-16 14:58:17 +01002854/**
2855 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2856 *
2857 * The control element is supposed to have the private_value field
2858 * set up via HDA_BIND_MUTE*() macros.
2859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002860int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2861 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002862{
2863 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2864 unsigned long pval;
2865 int i, indices, err = 0, change = 0;
2866
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002867 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002868 pval = kcontrol->private_value;
2869 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2870 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002871 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2872 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002873 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2874 if (err < 0)
2875 break;
2876 change |= err;
2877 }
2878 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002879 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002880 return err < 0 ? err : change;
2881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002882EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002883
Takashi Iwaid5191e52009-11-16 14:58:17 +01002884/**
2885 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2886 *
2887 * The control element is supposed to have the private_value field
2888 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002889 */
2890int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_info *uinfo)
2892{
2893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2894 struct hda_bind_ctls *c;
2895 int err;
2896
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002897 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002898 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002899 kcontrol->private_value = *c->values;
2900 err = c->ops->info(kcontrol, uinfo);
2901 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002902 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002903 return err;
2904}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002905EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002906
Takashi Iwaid5191e52009-11-16 14:58:17 +01002907/**
2908 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2909 *
2910 * The control element is supposed to have the private_value field
2911 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2912 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002913int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2915{
2916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2917 struct hda_bind_ctls *c;
2918 int err;
2919
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002920 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002921 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002922 kcontrol->private_value = *c->values;
2923 err = c->ops->get(kcontrol, ucontrol);
2924 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002925 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002926 return err;
2927}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002928EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002929
Takashi Iwaid5191e52009-11-16 14:58:17 +01002930/**
2931 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2932 *
2933 * The control element is supposed to have the private_value field
2934 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2935 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002936int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
2938{
2939 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2940 struct hda_bind_ctls *c;
2941 unsigned long *vals;
2942 int err = 0, change = 0;
2943
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002944 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002945 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002946 for (vals = c->values; *vals; vals++) {
2947 kcontrol->private_value = *vals;
2948 err = c->ops->put(kcontrol, ucontrol);
2949 if (err < 0)
2950 break;
2951 change |= err;
2952 }
2953 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002954 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002955 return err < 0 ? err : change;
2956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002957EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002958
Takashi Iwaid5191e52009-11-16 14:58:17 +01002959/**
2960 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2961 *
2962 * The control element is supposed to have the private_value field
2963 * set up via HDA_BIND_VOL() macro.
2964 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002965int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2966 unsigned int size, unsigned int __user *tlv)
2967{
2968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2969 struct hda_bind_ctls *c;
2970 int err;
2971
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002972 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002973 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002974 kcontrol->private_value = *c->values;
2975 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2976 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002977 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002978 return err;
2979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002980EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002981
2982struct hda_ctl_ops snd_hda_bind_vol = {
2983 .info = snd_hda_mixer_amp_volume_info,
2984 .get = snd_hda_mixer_amp_volume_get,
2985 .put = snd_hda_mixer_amp_volume_put,
2986 .tlv = snd_hda_mixer_amp_tlv
2987};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002988EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002989
2990struct hda_ctl_ops snd_hda_bind_sw = {
2991 .info = snd_hda_mixer_amp_switch_info,
2992 .get = snd_hda_mixer_amp_switch_get,
2993 .put = snd_hda_mixer_amp_switch_put,
2994 .tlv = snd_hda_mixer_amp_tlv
2995};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002996EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002997
2998/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 * SPDIF out controls
3000 */
3001
Takashi Iwai0ba21762007-04-16 11:29:14 +02003002static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3006 uinfo->count = 1;
3007 return 0;
3008}
3009
Takashi Iwai0ba21762007-04-16 11:29:14 +02003010static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3014 IEC958_AES0_NONAUDIO |
3015 IEC958_AES0_CON_EMPHASIS_5015 |
3016 IEC958_AES0_CON_NOT_COPYRIGHT;
3017 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3018 IEC958_AES1_CON_ORIGINAL;
3019 return 0;
3020}
3021
Takashi Iwai0ba21762007-04-16 11:29:14 +02003022static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
3025 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3026 IEC958_AES0_NONAUDIO |
3027 IEC958_AES0_PRO_EMPHASIS_5015;
3028 return 0;
3029}
3030
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003035 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003036 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003038 mutex_lock(&codec->spdif_mutex);
3039 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003040 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3041 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3042 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3043 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003044 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 return 0;
3047}
3048
3049/* convert from SPDIF status bits to HDA SPDIF bits
3050 * bit 0 (DigEn) is always set zero (to be filled later)
3051 */
3052static unsigned short convert_from_spdif_status(unsigned int sbits)
3053{
3054 unsigned short val = 0;
3055
3056 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003059 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003061 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3062 IEC958_AES0_PRO_EMPHASIS_5015)
3063 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003065 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3066 IEC958_AES0_CON_EMPHASIS_5015)
3067 val |= AC_DIG1_EMPHASIS;
3068 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3069 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003071 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3073 }
3074 return val;
3075}
3076
3077/* convert to SPDIF status bits from HDA SPDIF bits
3078 */
3079static unsigned int convert_to_spdif_status(unsigned short val)
3080{
3081 unsigned int sbits = 0;
3082
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003085 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 sbits |= IEC958_AES0_PROFESSIONAL;
3087 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003088 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3090 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003091 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003093 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3097 sbits |= val & (0x7f << 8);
3098 }
3099 return sbits;
3100}
3101
Takashi Iwai2f728532008-09-25 16:32:41 +02003102/* set digital convert verbs both for the given NID and its slaves */
3103static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3104 int verb, int val)
3105{
Takashi Iwaidda14412011-05-02 11:29:30 +02003106 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003107
Takashi Iwai9e976972008-11-25 08:17:20 +01003108 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003109 d = codec->slave_dig_outs;
3110 if (!d)
3111 return;
3112 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003113 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003114}
3115
3116static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3117 int dig1, int dig2)
3118{
3119 if (dig1 != -1)
3120 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3121 if (dig2 != -1)
3122 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3123}
3124
Takashi Iwai0ba21762007-04-16 11:29:14 +02003125static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003129 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003130 struct hda_spdif_out *spdif;
3131 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 unsigned short val;
3133 int change;
3134
Ingo Molnar62932df2006-01-16 16:34:20 +01003135 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003136 spdif = snd_array_elem(&codec->spdif_out, idx);
3137 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003138 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3140 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3141 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003142 val = convert_from_spdif_status(spdif->status);
3143 val |= spdif->ctls & 1;
3144 change = spdif->ctls != val;
3145 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003146 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003147 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003148 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return change;
3150}
3151
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003152#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Takashi Iwai0ba21762007-04-16 11:29:14 +02003154static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3155 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003158 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003159 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003161 mutex_lock(&codec->spdif_mutex);
3162 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003163 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003164 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return 0;
3166}
3167
Stephen Warren74b654c2011-06-01 11:14:18 -06003168static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3169 int dig1, int dig2)
3170{
3171 set_dig_out_convert(codec, nid, dig1, dig2);
3172 /* unmute amp switch (if any) */
3173 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3174 (dig1 & AC_DIG1_ENABLE))
3175 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3176 HDA_AMP_MUTE, 0);
3177}
3178
Takashi Iwai0ba21762007-04-16 11:29:14 +02003179static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003183 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003184 struct hda_spdif_out *spdif;
3185 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 unsigned short val;
3187 int change;
3188
Ingo Molnar62932df2006-01-16 16:34:20 +01003189 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003190 spdif = snd_array_elem(&codec->spdif_out, idx);
3191 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003192 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003194 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003195 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003196 spdif->ctls = val;
3197 if (change && nid != (u16)-1)
3198 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003199 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return change;
3201}
3202
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003203static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 {
3205 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3206 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003207 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 .info = snd_hda_spdif_mask_info,
3209 .get = snd_hda_spdif_cmask_get,
3210 },
3211 {
3212 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003214 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 .info = snd_hda_spdif_mask_info,
3216 .get = snd_hda_spdif_pmask_get,
3217 },
3218 {
3219 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003220 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 .info = snd_hda_spdif_mask_info,
3222 .get = snd_hda_spdif_default_get,
3223 .put = snd_hda_spdif_default_put,
3224 },
3225 {
3226 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003227 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 .info = snd_hda_spdif_out_switch_info,
3229 .get = snd_hda_spdif_out_switch_get,
3230 .put = snd_hda_spdif_out_switch_put,
3231 },
3232 { } /* end */
3233};
3234
3235/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003236 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003238 * @associated_nid: NID that new ctls associated with
3239 * @cvt_nid: converter NID
3240 * @type: HDA_PCM_TYPE_*
3241 * Creates controls related with the digital output.
3242 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 *
3244 * Returns 0 if successful, or a negative error code.
3245 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003246int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3247 hda_nid_t associated_nid,
3248 hda_nid_t cvt_nid,
3249 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003252 struct snd_kcontrol *kctl;
3253 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003254 int idx, dev = 0;
3255 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003256 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Takashi Iwaidcda5802012-10-12 17:24:51 +02003258 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3259 type == HDA_PCM_TYPE_SPDIF) {
3260 dev = spdif_pcm_dev;
3261 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3262 type == HDA_PCM_TYPE_HDMI) {
3263 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3264 spdif = snd_array_elem(&codec->spdif_out, idx);
3265 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3266 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3267 if (!kctl)
3268 break;
3269 kctl->id.device = spdif_pcm_dev;
3270 }
3271 }
3272 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3273 }
3274 if (!codec->primary_dig_out_type)
3275 codec->primary_dig_out_type = type;
3276
3277 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003278 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003279 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3280 return -EBUSY;
3281 }
Stephen Warren7c935972011-06-01 11:14:17 -06003282 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3284 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003285 if (!kctl)
3286 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003287 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003288 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003289 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003290 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003291 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return err;
3293 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003294 spdif->nid = cvt_nid;
3295 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003296 AC_VERB_GET_DIGI_CONVERT_1, 0);
3297 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return 0;
3299}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003300EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003302/* get the hda_spdif_out entry from the given NID
3303 * call within spdif_mutex lock
3304 */
Stephen Warren7c935972011-06-01 11:14:17 -06003305struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3306 hda_nid_t nid)
3307{
3308 int i;
3309 for (i = 0; i < codec->spdif_out.used; i++) {
3310 struct hda_spdif_out *spdif =
3311 snd_array_elem(&codec->spdif_out, i);
3312 if (spdif->nid == nid)
3313 return spdif;
3314 }
3315 return NULL;
3316}
3317EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3318
Stephen Warren74b654c2011-06-01 11:14:18 -06003319void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3320{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003321 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003322
3323 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003324 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003325 spdif->nid = (u16)-1;
3326 mutex_unlock(&codec->spdif_mutex);
3327}
3328EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3329
3330void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3331{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003332 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003333 unsigned short val;
3334
3335 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003336 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003337 if (spdif->nid != nid) {
3338 spdif->nid = nid;
3339 val = spdif->ctls;
3340 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3341 }
3342 mutex_unlock(&codec->spdif_mutex);
3343}
3344EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003347 * SPDIF sharing with analog output
3348 */
3349static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3350 struct snd_ctl_elem_value *ucontrol)
3351{
3352 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3353 ucontrol->value.integer.value[0] = mout->share_spdif;
3354 return 0;
3355}
3356
3357static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3358 struct snd_ctl_elem_value *ucontrol)
3359{
3360 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3361 mout->share_spdif = !!ucontrol->value.integer.value[0];
3362 return 0;
3363}
3364
3365static struct snd_kcontrol_new spdif_share_sw = {
3366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3367 .name = "IEC958 Default PCM Playback Switch",
3368 .info = snd_ctl_boolean_mono_info,
3369 .get = spdif_share_sw_get,
3370 .put = spdif_share_sw_put,
3371};
3372
Takashi Iwaid5191e52009-11-16 14:58:17 +01003373/**
3374 * snd_hda_create_spdif_share_sw - create Default PCM switch
3375 * @codec: the HDA codec
3376 * @mout: multi-out instance
3377 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003378int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3379 struct hda_multi_out *mout)
3380{
3381 if (!mout->dig_out_nid)
3382 return 0;
3383 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003384 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3385 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003386}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003387EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003388
3389/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 * SPDIF input
3391 */
3392
3393#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3394
Takashi Iwai0ba21762007-04-16 11:29:14 +02003395static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3399
3400 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3401 return 0;
3402}
3403
Takashi Iwai0ba21762007-04-16 11:29:14 +02003404static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3408 hda_nid_t nid = kcontrol->private_value;
3409 unsigned int val = !!ucontrol->value.integer.value[0];
3410 int change;
3411
Ingo Molnar62932df2006-01-16 16:34:20 +01003412 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003414 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003416 snd_hda_codec_write_cache(codec, nid, 0,
3417 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003419 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return change;
3421}
3422
Takashi Iwai0ba21762007-04-16 11:29:14 +02003423static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3424 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
3426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3427 hda_nid_t nid = kcontrol->private_value;
3428 unsigned short val;
3429 unsigned int sbits;
3430
Andrew Paprocki3982d172007-12-19 12:13:44 +01003431 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 sbits = convert_to_spdif_status(val);
3433 ucontrol->value.iec958.status[0] = sbits;
3434 ucontrol->value.iec958.status[1] = sbits >> 8;
3435 ucontrol->value.iec958.status[2] = sbits >> 16;
3436 ucontrol->value.iec958.status[3] = sbits >> 24;
3437 return 0;
3438}
3439
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003440static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 {
3442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003443 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 .info = snd_hda_spdif_in_switch_info,
3445 .get = snd_hda_spdif_in_switch_get,
3446 .put = snd_hda_spdif_in_switch_put,
3447 },
3448 {
3449 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3450 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003451 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 .info = snd_hda_spdif_mask_info,
3453 .get = snd_hda_spdif_in_status_get,
3454 },
3455 { } /* end */
3456};
3457
3458/**
3459 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3460 * @codec: the HDA codec
3461 * @nid: audio in widget NID
3462 *
3463 * Creates controls related with the SPDIF input.
3464 * Called from each patch supporting the SPDIF in.
3465 *
3466 * Returns 0 if successful, or a negative error code.
3467 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003468int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003471 struct snd_kcontrol *kctl;
3472 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003473 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Takashi Iwaidcda5802012-10-12 17:24:51 +02003475 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003476 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003477 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3478 return -EBUSY;
3479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3481 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003482 if (!kctl)
3483 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003485 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003486 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return err;
3488 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003490 snd_hda_codec_read(codec, nid, 0,
3491 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003492 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return 0;
3494}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003495EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003497/*
3498 * command cache
3499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Takashi Iwaic370dd62012-12-13 18:30:04 +01003501/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003502#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3503#define get_cmd_cache_nid(key) ((key) & 0xff)
3504#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3505
3506/**
3507 * snd_hda_codec_write_cache - send a single command with caching
3508 * @codec: the HDA codec
3509 * @nid: NID to send the command
3510 * @direct: direct flag
3511 * @verb: the verb to send
3512 * @parm: the parameter for the verb
3513 *
3514 * Send a single command without waiting for response.
3515 *
3516 * Returns 0 if successful, or a negative error code.
3517 */
3518int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3519 int direct, unsigned int verb, unsigned int parm)
3520{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003521 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003522 struct hda_cache_head *c;
3523 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003524 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003525
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003526 cache_only = codec->cached_write;
3527 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003528 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3529 if (err < 0)
3530 return err;
3531 }
3532
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003533 /* parm may contain the verb stuff for get/set amp */
3534 verb = verb | (parm >> 8);
3535 parm &= 0xff;
3536 key = build_cmd_cache_key(nid, verb);
3537 mutex_lock(&codec->bus->cmd_mutex);
3538 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003539 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003540 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003541 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003542 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003543 mutex_unlock(&codec->bus->cmd_mutex);
3544 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003545}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003546EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003547
Takashi Iwaid5191e52009-11-16 14:58:17 +01003548/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003549 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3550 * @codec: the HDA codec
3551 * @nid: NID to send the command
3552 * @direct: direct flag
3553 * @verb: the verb to send
3554 * @parm: the parameter for the verb
3555 *
3556 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3557 * command if the parameter is already identical with the cached value.
3558 * If not, it sends the command and refreshes the cache.
3559 *
3560 * Returns 0 if successful, or a negative error code.
3561 */
3562int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3563 int direct, unsigned int verb, unsigned int parm)
3564{
3565 struct hda_cache_head *c;
3566 u32 key;
3567
3568 /* parm may contain the verb stuff for get/set amp */
3569 verb = verb | (parm >> 8);
3570 parm &= 0xff;
3571 key = build_cmd_cache_key(nid, verb);
3572 mutex_lock(&codec->bus->cmd_mutex);
3573 c = get_hash(&codec->cmd_cache, key);
3574 if (c && c->val == parm) {
3575 mutex_unlock(&codec->bus->cmd_mutex);
3576 return 0;
3577 }
3578 mutex_unlock(&codec->bus->cmd_mutex);
3579 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3580}
3581EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3582
3583/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003584 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3585 * @codec: HD-audio codec
3586 *
3587 * Execute all verbs recorded in the command caches to resume.
3588 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003589void snd_hda_codec_resume_cache(struct hda_codec *codec)
3590{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003591 int i;
3592
Takashi Iwaic370dd62012-12-13 18:30:04 +01003593 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003594 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003595 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3596 struct hda_cache_head *buffer;
3597 u32 key;
3598
3599 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3600 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003601 if (!key)
3602 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 if (!buffer->dirty)
3604 continue;
3605 buffer->dirty = 0;
3606 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003607 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3608 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003609 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003610 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003611 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003613EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003614
3615/**
3616 * snd_hda_sequence_write_cache - sequence writes with caching
3617 * @codec: the HDA codec
3618 * @seq: VERB array to send
3619 *
3620 * Send the commands sequentially from the given array.
3621 * Thte commands are recorded on cache for power-save and resume.
3622 * The array must be terminated with NID=0.
3623 */
3624void snd_hda_sequence_write_cache(struct hda_codec *codec,
3625 const struct hda_verb *seq)
3626{
3627 for (; seq->nid; seq++)
3628 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3629 seq->param);
3630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003631EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003632
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003633void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3634 unsigned int power_state,
3635 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003636{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003637 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003638 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003639
Takashi Iwaicb53c622007-08-10 17:21:45 +02003640 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003641 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003642 if (!(wcaps & AC_WCAP_POWER))
3643 continue;
3644 /* don't power down the widget if it controls eapd and
3645 * EAPD_BTLENABLE is set.
3646 */
3647 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3648 get_wcaps_type(wcaps) == AC_WID_PIN &&
3649 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3650 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003651 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003652 if (eapd & 0x02)
3653 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003654 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3656 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003657 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003658}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003659EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3660
3661/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003662 * supported power states check
3663 */
3664static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3665 unsigned int power_state)
3666{
3667 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3668
Mengdong Line037cb42012-08-10 14:11:58 +02003669 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003670 return false;
3671 if (sup & power_state)
3672 return true;
3673 else
3674 return false;
3675}
3676
3677/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003678 * wait until the state is reached, returns the current state
3679 */
3680static unsigned int hda_sync_power_state(struct hda_codec *codec,
3681 hda_nid_t fg,
3682 unsigned int power_state)
3683{
3684 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3685 unsigned int state, actual_state;
3686
3687 for (;;) {
3688 state = snd_hda_codec_read(codec, fg, 0,
3689 AC_VERB_GET_POWER_STATE, 0);
3690 if (state & AC_PWRST_ERROR)
3691 break;
3692 actual_state = (state >> 4) & 0x0f;
3693 if (actual_state == power_state)
3694 break;
3695 if (time_after_eq(jiffies, end_time))
3696 break;
3697 /* wait until the codec reachs to the target state */
3698 msleep(1);
3699 }
3700 return state;
3701}
3702
3703/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003704 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003705 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003706static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003707 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003708{
Takashi Iwaid8193872012-08-31 07:54:38 -07003709 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003710 int count;
3711 unsigned int state;
3712
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003713 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003714 if (power_state == AC_PWRST_D3) {
3715 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003716 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003717 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003718
3719 /* repeat power states setting at most 10 times*/
3720 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003721 if (codec->patch_ops.set_power_state)
3722 codec->patch_ops.set_power_state(codec, fg,
3723 power_state);
3724 else {
3725 snd_hda_codec_read(codec, fg, 0,
3726 AC_VERB_SET_POWER_STATE,
3727 power_state);
3728 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3729 true);
3730 }
3731 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003732 if (!(state & AC_PWRST_ERROR))
3733 break;
3734 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003735
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003736 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003737}
Takashi Iwai54d17402005-11-21 16:33:22 +01003738
Takashi Iwai11aeff02008-07-30 15:01:46 +02003739#ifdef CONFIG_SND_HDA_HWDEP
3740/* execute additional init verbs */
3741static void hda_exec_init_verbs(struct hda_codec *codec)
3742{
3743 if (codec->init_verbs.list)
3744 snd_hda_sequence_write(codec, codec->init_verbs.list);
3745}
3746#else
3747static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3748#endif
3749
Takashi Iwai2a439522011-07-26 09:52:50 +02003750#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003751/*
3752 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003753 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003754 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003755static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003756{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003757 unsigned int state;
3758
Takashi Iwai989c3182012-11-19 14:14:58 +01003759 codec->in_pm = 1;
3760
Takashi Iwaicb53c622007-08-10 17:21:45 +02003761 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003762 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003763 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003764 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003765 /* Cancel delayed work if we aren't currently running from it. */
3766 if (!in_wq)
3767 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003768 spin_lock(&codec->power_lock);
3769 snd_hda_update_power_acct(codec);
3770 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003771 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003772 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003773 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003774 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003775 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003776 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003777}
3778
Takashi Iwaic370dd62012-12-13 18:30:04 +01003779/* mark all entries of cmd and amp caches dirty */
3780static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3781{
3782 int i;
3783 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3784 struct hda_cache_head *cmd;
3785 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3786 cmd->dirty = 1;
3787 }
3788 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3789 struct hda_amp_info *amp;
3790 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3791 amp->head.dirty = 1;
3792 }
3793}
3794
Takashi Iwaicb53c622007-08-10 17:21:45 +02003795/*
3796 * kick up codec; used both from PM and power-save
3797 */
3798static void hda_call_codec_resume(struct hda_codec *codec)
3799{
Takashi Iwai989c3182012-11-19 14:14:58 +01003800 codec->in_pm = 1;
3801
Takashi Iwaic370dd62012-12-13 18:30:04 +01003802 hda_mark_cmd_cache_dirty(codec);
3803
Takashi Iwai7f308302012-05-08 16:52:23 +02003804 /* set as if powered on for avoiding re-entering the resume
3805 * in the resume / power-save sequence
3806 */
3807 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003808 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003809 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003810 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003811 if (codec->patch_ops.resume)
3812 codec->patch_ops.resume(codec);
3813 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003814 if (codec->patch_ops.init)
3815 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003816 snd_hda_codec_resume_amp(codec);
3817 snd_hda_codec_resume_cache(codec);
3818 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003819
3820 if (codec->jackpoll_interval)
3821 hda_jackpoll_work(&codec->jackpoll_work.work);
3822 else {
3823 snd_hda_jack_set_dirty_all(codec);
3824 snd_hda_jack_report_sync(codec);
3825 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003826
3827 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003828 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003829}
Takashi Iwai2a439522011-07-26 09:52:50 +02003830#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003831
Takashi Iwai54d17402005-11-21 16:33:22 +01003832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833/**
3834 * snd_hda_build_controls - build mixer controls
3835 * @bus: the BUS
3836 *
3837 * Creates mixer controls for each codec included in the bus.
3838 *
3839 * Returns 0 if successful, otherwise a negative error code.
3840 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003841int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003843 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Takashi Iwai0ba21762007-04-16 11:29:14 +02003845 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003846 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003847 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003848 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003849 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003850 err = snd_hda_codec_reset(codec);
3851 if (err < 0) {
3852 printk(KERN_ERR
3853 "hda_codec: cannot revert codec\n");
3854 return err;
3855 }
3856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003858 return 0;
3859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003860EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003862/*
3863 * add standard channel maps if not specified
3864 */
3865static int add_std_chmaps(struct hda_codec *codec)
3866{
3867 int i, str, err;
3868
3869 for (i = 0; i < codec->num_pcms; i++) {
3870 for (str = 0; str < 2; str++) {
3871 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3872 struct hda_pcm_stream *hinfo =
3873 &codec->pcm_info[i].stream[str];
3874 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003875 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003876
3877 if (codec->pcm_info[i].own_chmap)
3878 continue;
3879 if (!pcm || !hinfo->substreams)
3880 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003881 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3882 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003883 hinfo->channels_max,
3884 0, &chmap);
3885 if (err < 0)
3886 return err;
3887 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3888 }
3889 }
3890 return 0;
3891}
3892
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003893/* default channel maps for 2.1 speakers;
3894 * since HD-audio supports only stereo, odd number channels are omitted
3895 */
3896const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3897 { .channels = 2,
3898 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3899 { .channels = 4,
3900 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3901 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3902 { }
3903};
3904EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3905
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003906int snd_hda_codec_build_controls(struct hda_codec *codec)
3907{
3908 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003909 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003910 /* continue to initialize... */
3911 if (codec->patch_ops.init)
3912 err = codec->patch_ops.init(codec);
3913 if (!err && codec->patch_ops.build_controls)
3914 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003915 if (err < 0)
3916 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003917
3918 /* we create chmaps here instead of build_pcms */
3919 err = add_std_chmaps(codec);
3920 if (err < 0)
3921 return err;
3922
David Henningsson26a6cb62012-10-09 15:04:21 +02003923 if (codec->jackpoll_interval)
3924 hda_jackpoll_work(&codec->jackpoll_work.work);
3925 else
3926 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return 0;
3928}
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930/*
3931 * stream formats
3932 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003933struct hda_rate_tbl {
3934 unsigned int hz;
3935 unsigned int alsa_bits;
3936 unsigned int hda_fmt;
3937};
3938
Takashi Iwai92f10b32010-08-03 14:21:00 +02003939/* rate = base * mult / div */
3940#define HDA_RATE(base, mult, div) \
3941 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3942 (((div) - 1) << AC_FMT_DIV_SHIFT))
3943
Takashi Iwaibefdf312005-08-22 13:57:55 +02003944static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003946
3947 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003948 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3949 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3950 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3951 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3952 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3953 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3954 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3955 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3956 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3957 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3958 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003959#define AC_PAR_PCM_RATE_BITS 11
3960 /* up to bits 10, 384kHZ isn't supported properly */
3961
3962 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003963 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003964
Takashi Iwaibefdf312005-08-22 13:57:55 +02003965 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966};
3967
3968/**
3969 * snd_hda_calc_stream_format - calculate format bitset
3970 * @rate: the sample rate
3971 * @channels: the number of channels
3972 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3973 * @maxbps: the max. bps
3974 *
3975 * Calculate the format bitset from the given rate, channels and th PCM format.
3976 *
3977 * Return zero if invalid.
3978 */
3979unsigned int snd_hda_calc_stream_format(unsigned int rate,
3980 unsigned int channels,
3981 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003982 unsigned int maxbps,
3983 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 int i;
3986 unsigned int val = 0;
3987
Takashi Iwaibefdf312005-08-22 13:57:55 +02003988 for (i = 0; rate_bits[i].hz; i++)
3989 if (rate_bits[i].hz == rate) {
3990 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 break;
3992 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003993 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 snd_printdd("invalid rate %d\n", rate);
3995 return 0;
3996 }
3997
3998 if (channels == 0 || channels > 8) {
3999 snd_printdd("invalid channels %d\n", channels);
4000 return 0;
4001 }
4002 val |= channels - 1;
4003
4004 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004005 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004006 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004007 break;
4008 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004009 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 case 20:
4012 case 24:
4013 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004014 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004015 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004017 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004019 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 break;
4021 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004022 snd_printdd("invalid format width %d\n",
4023 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return 0;
4025 }
4026
Anssi Hannula32c168c2010-08-03 13:28:57 +03004027 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004028 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return val;
4031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004032EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004034static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4035 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004036{
4037 unsigned int val = 0;
4038 if (nid != codec->afg &&
4039 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4040 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4041 if (!val || val == -1)
4042 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4043 if (!val || val == -1)
4044 return 0;
4045 return val;
4046}
4047
4048static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4049{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004050 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004051 get_pcm_param);
4052}
4053
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004054static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4055 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004056{
4057 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4058 if (!streams || streams == -1)
4059 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4060 if (!streams || streams == -1)
4061 return 0;
4062 return streams;
4063}
4064
4065static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4066{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004067 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004068 get_stream_param);
4069}
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071/**
4072 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4073 * @codec: the HDA codec
4074 * @nid: NID to query
4075 * @ratesp: the pointer to store the detected rate bitflags
4076 * @formatsp: the pointer to store the detected formats
4077 * @bpsp: the pointer to store the detected format widths
4078 *
4079 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4080 * or @bsps argument is ignored.
4081 *
4082 * Returns 0 if successful, otherwise a negative error code.
4083 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004084int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4086{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004087 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004089 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004090 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092 if (ratesp) {
4093 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004094 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004096 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004098 if (rates == 0) {
4099 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4100 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4101 nid, val,
4102 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4103 return -EIO;
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 *ratesp = rates;
4106 }
4107
4108 if (formatsp || bpsp) {
4109 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004110 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004112 streams = query_stream_param(codec, nid);
4113 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116 bps = 0;
4117 if (streams & AC_SUPFMT_PCM) {
4118 if (val & AC_SUPPCM_BITS_8) {
4119 formats |= SNDRV_PCM_FMTBIT_U8;
4120 bps = 8;
4121 }
4122 if (val & AC_SUPPCM_BITS_16) {
4123 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4124 bps = 16;
4125 }
4126 if (wcaps & AC_WCAP_DIGITAL) {
4127 if (val & AC_SUPPCM_BITS_32)
4128 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4129 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4130 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4131 if (val & AC_SUPPCM_BITS_24)
4132 bps = 24;
4133 else if (val & AC_SUPPCM_BITS_20)
4134 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004135 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4136 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4138 if (val & AC_SUPPCM_BITS_32)
4139 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 else if (val & AC_SUPPCM_BITS_24)
4141 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004142 else if (val & AC_SUPPCM_BITS_20)
4143 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
4145 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004146#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004147 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004149 if (!bps)
4150 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004151 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004152#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004153 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004154 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 /* temporary hack: we have still no proper support
4156 * for the direct AC3 stream...
4157 */
4158 formats |= SNDRV_PCM_FMTBIT_U8;
4159 bps = 8;
4160 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004161 if (formats == 0) {
4162 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4163 "(nid=0x%x, val=0x%x, ovrd=%i, "
4164 "streams=0x%x)\n",
4165 nid, val,
4166 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4167 streams);
4168 return -EIO;
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 if (formatsp)
4171 *formatsp = formats;
4172 if (bpsp)
4173 *bpsp = bps;
4174 }
4175
4176 return 0;
4177}
Stephen Warren384a48d2011-06-01 11:14:21 -06004178EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004181 * snd_hda_is_supported_format - Check the validity of the format
4182 * @codec: HD-audio codec
4183 * @nid: NID to check
4184 * @format: the HD-audio format value to check
4185 *
4186 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 *
4188 * Returns 1 if supported, 0 if not.
4189 */
4190int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4191 unsigned int format)
4192{
4193 int i;
4194 unsigned int val = 0, rate, stream;
4195
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004196 val = query_pcm_param(codec, nid);
4197 if (!val)
4198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004201 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004202 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (val & (1 << i))
4204 break;
4205 return 0;
4206 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004207 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return 0;
4209
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004210 stream = query_stream_param(codec, nid);
4211 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return 0;
4213
4214 if (stream & AC_SUPFMT_PCM) {
4215 switch (format & 0xf0) {
4216 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004217 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return 0;
4219 break;
4220 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004221 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return 0;
4223 break;
4224 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004225 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return 0;
4227 break;
4228 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004229 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return 0;
4231 break;
4232 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004233 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 return 0;
4235 break;
4236 default:
4237 return 0;
4238 }
4239 } else {
4240 /* FIXME: check for float32 and AC3? */
4241 }
4242
4243 return 1;
4244}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004245EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247/*
4248 * PCM stuff
4249 */
4250static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4251 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004252 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
4254 return 0;
4255}
4256
4257static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4258 struct hda_codec *codec,
4259 unsigned int stream_tag,
4260 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004261 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
4263 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4264 return 0;
4265}
4266
4267static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4268 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004269 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
Takashi Iwai888afa12008-03-18 09:57:50 +01004271 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 return 0;
4273}
4274
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004275static int set_pcm_default_values(struct hda_codec *codec,
4276 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004278 int err;
4279
Takashi Iwai0ba21762007-04-16 11:29:14 +02004280 /* query support PCM information from the given NID */
4281 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004282 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004283 info->rates ? NULL : &info->rates,
4284 info->formats ? NULL : &info->formats,
4285 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004286 if (err < 0)
4287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289 if (info->ops.open == NULL)
4290 info->ops.open = hda_pcm_default_open_close;
4291 if (info->ops.close == NULL)
4292 info->ops.close = hda_pcm_default_open_close;
4293 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004294 if (snd_BUG_ON(!info->nid))
4295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 info->ops.prepare = hda_pcm_default_prepare;
4297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004299 if (snd_BUG_ON(!info->nid))
4300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 info->ops.cleanup = hda_pcm_default_cleanup;
4302 }
4303 return 0;
4304}
4305
Takashi Iwaieb541332010-08-06 13:48:11 +02004306/*
4307 * codec prepare/cleanup entries
4308 */
4309int snd_hda_codec_prepare(struct hda_codec *codec,
4310 struct hda_pcm_stream *hinfo,
4311 unsigned int stream,
4312 unsigned int format,
4313 struct snd_pcm_substream *substream)
4314{
4315 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004316 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004317 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4318 if (ret >= 0)
4319 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004320 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004321 return ret;
4322}
4323EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4324
4325void snd_hda_codec_cleanup(struct hda_codec *codec,
4326 struct hda_pcm_stream *hinfo,
4327 struct snd_pcm_substream *substream)
4328{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004329 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004330 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004331 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004332}
4333EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4334
Takashi Iwaid5191e52009-11-16 14:58:17 +01004335/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004336const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4337 "Audio", "SPDIF", "HDMI", "Modem"
4338};
4339
Takashi Iwai176d5332008-07-30 15:01:44 +02004340/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004341 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004342 *
4343 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004344 */
4345static int get_empty_pcm_device(struct hda_bus *bus, int type)
4346{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004347 /* audio device indices; not linear to keep compatibility */
4348 static int audio_idx[HDA_PCM_NTYPES][5] = {
4349 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4350 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004351 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004352 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004353 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004354 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004355
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004356 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004357 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4358 return -EINVAL;
4359 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004360
4361 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4362 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4363 return audio_idx[type][i];
4364
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004365 /* non-fixed slots starting from 10 */
4366 for (i = 10; i < 32; i++) {
4367 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4368 return i;
4369 }
4370
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004371 snd_printk(KERN_WARNING "Too many %s devices\n",
4372 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004373 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004374}
4375
4376/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004377 * attach a new PCM stream
4378 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004379static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004380{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004381 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004382 struct hda_pcm_stream *info;
4383 int stream, err;
4384
Takashi Iwaib91f0802008-11-04 08:43:08 +01004385 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004386 return -EINVAL;
4387 for (stream = 0; stream < 2; stream++) {
4388 info = &pcm->stream[stream];
4389 if (info->substreams) {
4390 err = set_pcm_default_values(codec, info);
4391 if (err < 0)
4392 return err;
4393 }
4394 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004395 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004396}
4397
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004398/* assign all PCMs of the given codec */
4399int snd_hda_codec_build_pcms(struct hda_codec *codec)
4400{
4401 unsigned int pcm;
4402 int err;
4403
4404 if (!codec->num_pcms) {
4405 if (!codec->patch_ops.build_pcms)
4406 return 0;
4407 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004408 if (err < 0) {
4409 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004410 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004411 err = snd_hda_codec_reset(codec);
4412 if (err < 0) {
4413 printk(KERN_ERR
4414 "hda_codec: cannot revert codec\n");
4415 return err;
4416 }
4417 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004418 }
4419 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4420 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4421 int dev;
4422
4423 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004424 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004425
4426 if (!cpcm->pcm) {
4427 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4428 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004429 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004430 cpcm->device = dev;
4431 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004432 if (err < 0) {
4433 printk(KERN_ERR "hda_codec: cannot attach "
4434 "PCM stream %d for codec #%d\n",
4435 dev, codec->addr);
4436 continue; /* no fatal error */
4437 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004438 }
4439 }
4440 return 0;
4441}
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443/**
4444 * snd_hda_build_pcms - build PCM information
4445 * @bus: the BUS
4446 *
4447 * Create PCM information for each codec included in the bus.
4448 *
4449 * The build_pcms codec patch is requested to set up codec->num_pcms and
4450 * codec->pcm_info properly. The array is referred by the top-level driver
4451 * to create its PCM instances.
4452 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4453 * callback.
4454 *
4455 * At least, substreams, channels_min and channels_max must be filled for
4456 * each stream. substreams = 0 indicates that the stream doesn't exist.
4457 * When rates and/or formats are zero, the supported values are queried
4458 * from the given nid. The nid is used also by the default ops.prepare
4459 * and ops.cleanup callbacks.
4460 *
4461 * The driver needs to call ops.open in its open callback. Similarly,
4462 * ops.close is supposed to be called in the close callback.
4463 * ops.prepare should be called in the prepare or hw_params callback
4464 * with the proper parameters for set up.
4465 * ops.cleanup should be called in hw_free for clean up of streams.
4466 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004467 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004469int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004471 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Takashi Iwai0ba21762007-04-16 11:29:14 +02004473 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004474 int err = snd_hda_codec_build_pcms(codec);
4475 if (err < 0)
4476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
4478 return 0;
4479}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004480EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482/**
4483 * snd_hda_check_board_config - compare the current codec with the config table
4484 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004485 * @num_configs: number of config enums
4486 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 * @tbl: configuration table, terminated by null entries
4488 *
4489 * Compares the modelname or PCI subsystem id of the current codec with the
4490 * given configuration table. If a matching entry is found, returns its
4491 * config value (supposed to be 0 or positive).
4492 *
4493 * If no entries are matching, the function returns a negative value.
4494 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004495int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004496 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004497 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004499 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004500 int i;
4501 for (i = 0; i < num_configs; i++) {
4502 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004503 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004504 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4505 "selected\n", models[i]);
4506 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508 }
4509 }
4510
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004511 if (!codec->bus->pci || !tbl)
4512 return -1;
4513
4514 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4515 if (!tbl)
4516 return -1;
4517 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004518#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004519 char tmp[10];
4520 const char *model = NULL;
4521 if (models)
4522 model = models[tbl->value];
4523 if (!model) {
4524 sprintf(tmp, "#%d", tbl->value);
4525 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004527 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4528 "for config %x:%x (%s)\n",
4529 model, tbl->subvendor, tbl->subdevice,
4530 (tbl->name ? tbl->name : "Unknown device"));
4531#endif
4532 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534 return -1;
4535}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004536EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004539 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004540 subsystem ID with the
4541 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004542
4543 This is important for Gateway notebooks with SB450 HDA Audio
4544 where the vendor ID of the PCI device is:
4545 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4546 and the vendor/subvendor are found only at the codec.
4547
4548 * @codec: the HDA codec
4549 * @num_configs: number of config enums
4550 * @models: array of model name strings
4551 * @tbl: configuration table, terminated by null entries
4552 *
4553 * Compares the modelname or PCI subsystem id of the current codec with the
4554 * given configuration table. If a matching entry is found, returns its
4555 * config value (supposed to be 0 or positive).
4556 *
4557 * If no entries are matching, the function returns a negative value.
4558 */
4559int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004560 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004561 const struct snd_pci_quirk *tbl)
4562{
4563 const struct snd_pci_quirk *q;
4564
4565 /* Search for codec ID */
4566 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004567 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4568 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4569 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004570 break;
4571 }
4572
4573 if (!q->subvendor)
4574 return -1;
4575
4576 tbl = q;
4577
4578 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004579#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004580 char tmp[10];
4581 const char *model = NULL;
4582 if (models)
4583 model = models[tbl->value];
4584 if (!model) {
4585 sprintf(tmp, "#%d", tbl->value);
4586 model = tmp;
4587 }
4588 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4589 "for config %x:%x (%s)\n",
4590 model, tbl->subvendor, tbl->subdevice,
4591 (tbl->name ? tbl->name : "Unknown device"));
4592#endif
4593 return tbl->value;
4594 }
4595 return -1;
4596}
4597EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4598
4599/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 * snd_hda_add_new_ctls - create controls from the array
4601 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004602 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 *
4604 * This helper function creates and add new controls in the given array.
4605 * The array must be terminated with an empty entry as terminator.
4606 *
4607 * Returns 0 if successful, or a negative error code.
4608 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004609int snd_hda_add_new_ctls(struct hda_codec *codec,
4610 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004615 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004616 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004617 if (knew->iface == -1) /* skip this codec private value */
4618 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004619 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004620 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004621 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004622 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004623 if (addr > 0)
4624 kctl->id.device = addr;
4625 if (idx > 0)
4626 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004627 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004628 if (!err)
4629 break;
4630 /* try first with another device index corresponding to
4631 * the codec addr; if it still fails (or it's the
4632 * primary codec), then try another control index
4633 */
4634 if (!addr && codec->addr)
4635 addr = codec->addr;
4636 else if (!idx && !knew->index) {
4637 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004638 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004639 if (idx <= 0)
4640 return err;
4641 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004642 return err;
4643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645 return 0;
4646}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004647EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Takashi Iwai83012a72012-08-24 18:38:08 +02004649#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004650static void hda_power_work(struct work_struct *work)
4651{
4652 struct hda_codec *codec =
4653 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004654 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004655 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004656
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004657 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004658 if (codec->power_transition > 0) { /* during power-up sequence? */
4659 spin_unlock(&codec->power_lock);
4660 return;
4661 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004662 if (!codec->power_on || codec->power_count) {
4663 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004664 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004665 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004666 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004667 spin_unlock(&codec->power_lock);
4668
Dylan Reidd17344b2012-09-28 15:57:01 -07004669 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004670 codec->pm_down_notified = 0;
4671 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4672 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004673 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004674 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004675}
4676
4677static void hda_keep_power_on(struct hda_codec *codec)
4678{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004679 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004680 codec->power_count++;
4681 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004682 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004683 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004684}
4685
Takashi Iwaid5191e52009-11-16 14:58:17 +01004686/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004687void snd_hda_update_power_acct(struct hda_codec *codec)
4688{
4689 unsigned long delta = jiffies - codec->power_jiffies;
4690 if (codec->power_on)
4691 codec->power_on_acct += delta;
4692 else
4693 codec->power_off_acct += delta;
4694 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004695}
4696
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004697/* Transition to powered up, if wait_power_down then wait for a pending
4698 * transition to D3 to complete. A pending D3 transition is indicated
4699 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004700/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004701static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004702{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004703 struct hda_bus *bus = codec->bus;
4704
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004705 /* Return if power_on or transitioning to power_on, unless currently
4706 * powering down. */
4707 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004708 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004709 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004710 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004711
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004712 cancel_delayed_work_sync(&codec->power_work);
4713
4714 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004715 /* If the power down delayed work was cancelled above before starting,
4716 * then there is no need to go through power up here.
4717 */
4718 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004719 if (codec->power_transition < 0)
4720 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004721 return;
4722 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004723
Takashi Iwaid66fee52011-08-02 15:39:31 +02004724 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004725 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004726 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004727 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004728 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004729 spin_unlock(&codec->power_lock);
4730
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004731 if (codec->pm_down_notified) {
4732 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004733 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004734 }
4735
Takashi Iwaicb53c622007-08-10 17:21:45 +02004736 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004737
4738 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004739 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004740}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004741
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004742#define power_save(codec) \
4743 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004744
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004745/* Transition to powered down */
4746static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004747{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004748 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004749 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004750
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004751 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004752 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004753 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004754 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004755 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004756}
4757
4758/**
4759 * snd_hda_power_save - Power-up/down/sync the codec
4760 * @codec: HD-audio codec
4761 * @delta: the counter delta to change
4762 *
4763 * Change the power-up counter via @delta, and power up or down the hardware
4764 * appropriately. For the power-down, queue to the delayed action.
4765 * Passing zero to @delta means to synchronize the power state.
4766 */
4767void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4768{
4769 spin_lock(&codec->power_lock);
4770 codec->power_count += delta;
4771 trace_hda_power_count(codec);
4772 if (delta > 0)
4773 __snd_hda_power_up(codec, d3wait);
4774 else
4775 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004776 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004777}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004778EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004779
Takashi Iwaid5191e52009-11-16 14:58:17 +01004780/**
4781 * snd_hda_check_amp_list_power - Check the amp list and update the power
4782 * @codec: HD-audio codec
4783 * @check: the object containing an AMP list and the status
4784 * @nid: NID to check / update
4785 *
4786 * Check whether the given NID is in the amp list. If it's in the list,
4787 * check the current AMP status, and update the the power-status according
4788 * to the mute status.
4789 *
4790 * This function is supposed to be set or called from the check_power_status
4791 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004792 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793int snd_hda_check_amp_list_power(struct hda_codec *codec,
4794 struct hda_loopback_check *check,
4795 hda_nid_t nid)
4796{
Takashi Iwai031024e2011-05-02 11:29:30 +02004797 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004798 int ch, v;
4799
4800 if (!check->amplist)
4801 return 0;
4802 for (p = check->amplist; p->nid; p++) {
4803 if (p->nid == nid)
4804 break;
4805 }
4806 if (!p->nid)
4807 return 0; /* nothing changed */
4808
4809 for (p = check->amplist; p->nid; p++) {
4810 for (ch = 0; ch < 2; ch++) {
4811 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4812 p->idx);
4813 if (!(v & HDA_AMP_MUTE) && v > 0) {
4814 if (!check->power_on) {
4815 check->power_on = 1;
4816 snd_hda_power_up(codec);
4817 }
4818 return 1;
4819 }
4820 }
4821 }
4822 if (check->power_on) {
4823 check->power_on = 0;
4824 snd_hda_power_down(codec);
4825 }
4826 return 0;
4827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004828EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004831/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004832 * Channel mode helper
4833 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004834
4835/**
4836 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4837 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004838int snd_hda_ch_mode_info(struct hda_codec *codec,
4839 struct snd_ctl_elem_info *uinfo,
4840 const struct hda_channel_mode *chmode,
4841 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004842{
4843 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4844 uinfo->count = 1;
4845 uinfo->value.enumerated.items = num_chmodes;
4846 if (uinfo->value.enumerated.item >= num_chmodes)
4847 uinfo->value.enumerated.item = num_chmodes - 1;
4848 sprintf(uinfo->value.enumerated.name, "%dch",
4849 chmode[uinfo->value.enumerated.item].channels);
4850 return 0;
4851}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004852EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004853
Takashi Iwaid5191e52009-11-16 14:58:17 +01004854/**
4855 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4856 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004857int snd_hda_ch_mode_get(struct hda_codec *codec,
4858 struct snd_ctl_elem_value *ucontrol,
4859 const struct hda_channel_mode *chmode,
4860 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004861 int max_channels)
4862{
4863 int i;
4864
4865 for (i = 0; i < num_chmodes; i++) {
4866 if (max_channels == chmode[i].channels) {
4867 ucontrol->value.enumerated.item[0] = i;
4868 break;
4869 }
4870 }
4871 return 0;
4872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004873EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004874
Takashi Iwaid5191e52009-11-16 14:58:17 +01004875/**
4876 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4877 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004878int snd_hda_ch_mode_put(struct hda_codec *codec,
4879 struct snd_ctl_elem_value *ucontrol,
4880 const struct hda_channel_mode *chmode,
4881 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004882 int *max_channelsp)
4883{
4884 unsigned int mode;
4885
4886 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004887 if (mode >= num_chmodes)
4888 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004889 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004890 return 0;
4891 /* change the current channel setting */
4892 *max_channelsp = chmode[mode].channels;
4893 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004894 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004895 return 1;
4896}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004897EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899/*
4900 * input MUX helper
4901 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004902
4903/**
4904 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004906int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4907 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
4909 unsigned int index;
4910
4911 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4912 uinfo->count = 1;
4913 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004914 if (!imux->num_items)
4915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 index = uinfo->value.enumerated.item;
4917 if (index >= imux->num_items)
4918 index = imux->num_items - 1;
4919 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4920 return 0;
4921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004922EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Takashi Iwaid5191e52009-11-16 14:58:17 +01004924/**
4925 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4926 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004927int snd_hda_input_mux_put(struct hda_codec *codec,
4928 const struct hda_input_mux *imux,
4929 struct snd_ctl_elem_value *ucontrol,
4930 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 unsigned int *cur_val)
4932{
4933 unsigned int idx;
4934
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004935 if (!imux->num_items)
4936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 idx = ucontrol->value.enumerated.item[0];
4938 if (idx >= imux->num_items)
4939 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004940 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004942 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4943 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 *cur_val = idx;
4945 return 1;
4946}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004947EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949
4950/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004951 * process kcontrol info callback of a simple string enum array
4952 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4953 */
4954int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4955 struct snd_ctl_elem_info *uinfo,
4956 int num_items, const char * const *texts)
4957{
4958 static const char * const texts_default[] = {
4959 "Disabled", "Enabled"
4960 };
4961
4962 if (!texts || !num_items) {
4963 num_items = 2;
4964 texts = texts_default;
4965 }
4966
4967 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4968 uinfo->count = 1;
4969 uinfo->value.enumerated.items = num_items;
4970 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4971 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4972 strcpy(uinfo->value.enumerated.name,
4973 texts[uinfo->value.enumerated.item]);
4974 return 0;
4975}
4976EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4977
4978/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 * Multi-channel / digital-out PCM helper functions
4980 */
4981
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004982/* setup SPDIF output stream */
4983static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4984 unsigned int stream_tag, unsigned int format)
4985{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004986 struct hda_spdif_out *spdif;
4987 unsigned int curr_fmt;
4988 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004989
Laurence Darby3bef1c32012-11-03 17:00:06 +00004990 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4991 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4992 AC_VERB_GET_STREAM_FORMAT, 0);
4993 reset = codec->spdif_status_reset &&
4994 (spdif->ctls & AC_DIG1_ENABLE) &&
4995 curr_fmt != format;
4996
4997 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4998 updated */
4999 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005000 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005001 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005002 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005003 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005004 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005005 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005006 for (d = codec->slave_dig_outs; *d; d++)
5007 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5008 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005009 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005010 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005011 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005012 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005013 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005014}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005015
Takashi Iwai2f728532008-09-25 16:32:41 +02005016static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5017{
5018 snd_hda_codec_cleanup_stream(codec, nid);
5019 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005020 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005021 for (d = codec->slave_dig_outs; *d; d++)
5022 snd_hda_codec_cleanup_stream(codec, *d);
5023 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005024}
5025
Takashi Iwaid5191e52009-11-16 14:58:17 +01005026/**
5027 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5028 * @bus: HD-audio bus
5029 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005030void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5031{
5032 struct hda_codec *codec;
5033
5034 if (!bus)
5035 return;
5036 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005037 if (hda_codec_is_power_on(codec) &&
5038 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005039 codec->patch_ops.reboot_notify(codec);
5040 }
5041}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005042EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005043
Takashi Iwaid5191e52009-11-16 14:58:17 +01005044/**
5045 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005047int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5048 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
Ingo Molnar62932df2006-01-16 16:34:20 +01005050 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005051 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5052 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005053 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005055 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 return 0;
5057}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005058EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Takashi Iwaid5191e52009-11-16 14:58:17 +01005060/**
5061 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5062 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005063int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5064 struct hda_multi_out *mout,
5065 unsigned int stream_tag,
5066 unsigned int format,
5067 struct snd_pcm_substream *substream)
5068{
5069 mutex_lock(&codec->spdif_mutex);
5070 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5071 mutex_unlock(&codec->spdif_mutex);
5072 return 0;
5073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005074EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005075
Takashi Iwaid5191e52009-11-16 14:58:17 +01005076/**
5077 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5078 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005079int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5080 struct hda_multi_out *mout)
5081{
5082 mutex_lock(&codec->spdif_mutex);
5083 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5084 mutex_unlock(&codec->spdif_mutex);
5085 return 0;
5086}
5087EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5088
Takashi Iwaid5191e52009-11-16 14:58:17 +01005089/**
5090 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005092int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5093 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
Ingo Molnar62932df2006-01-16 16:34:20 +01005095 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005097 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 return 0;
5099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005100EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Takashi Iwaid5191e52009-11-16 14:58:17 +01005102/**
5103 * snd_hda_multi_out_analog_open - open analog outputs
5104 *
5105 * Open analog outputs and set up the hw-constraints.
5106 * If the digital outputs can be opened as slave, open the digital
5107 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005109int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5110 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005111 struct snd_pcm_substream *substream,
5112 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
Takashi Iwai9a081602008-02-12 18:37:26 +01005114 struct snd_pcm_runtime *runtime = substream->runtime;
5115 runtime->hw.channels_max = mout->max_channels;
5116 if (mout->dig_out_nid) {
5117 if (!mout->analog_rates) {
5118 mout->analog_rates = hinfo->rates;
5119 mout->analog_formats = hinfo->formats;
5120 mout->analog_maxbps = hinfo->maxbps;
5121 } else {
5122 runtime->hw.rates = mout->analog_rates;
5123 runtime->hw.formats = mout->analog_formats;
5124 hinfo->maxbps = mout->analog_maxbps;
5125 }
5126 if (!mout->spdif_rates) {
5127 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5128 &mout->spdif_rates,
5129 &mout->spdif_formats,
5130 &mout->spdif_maxbps);
5131 }
5132 mutex_lock(&codec->spdif_mutex);
5133 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005134 if ((runtime->hw.rates & mout->spdif_rates) &&
5135 (runtime->hw.formats & mout->spdif_formats)) {
5136 runtime->hw.rates &= mout->spdif_rates;
5137 runtime->hw.formats &= mout->spdif_formats;
5138 if (mout->spdif_maxbps < hinfo->maxbps)
5139 hinfo->maxbps = mout->spdif_maxbps;
5140 } else {
5141 mout->share_spdif = 0;
5142 /* FIXME: need notify? */
5143 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005144 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005145 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5148 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005150EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Takashi Iwaid5191e52009-11-16 14:58:17 +01005152/**
5153 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5154 *
5155 * Set up the i/o for analog out.
5156 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005158int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5159 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 unsigned int stream_tag,
5161 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005162 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
Takashi Iwaidda14412011-05-02 11:29:30 +02005164 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005166 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 int i;
5168
Ingo Molnar62932df2006-01-16 16:34:20 +01005169 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005170 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005171 if (mout->dig_out_nid && mout->share_spdif &&
5172 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005174 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5175 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005176 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005178 setup_dig_out_stream(codec, mout->dig_out_nid,
5179 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 } else {
5181 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005182 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 }
5184 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005185 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005188 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5189 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005190 if (!mout->no_share_stream &&
5191 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005193 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5194 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005195 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005196 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5197 if (!mout->no_share_stream && mout->hp_out_nid[i])
5198 snd_hda_codec_setup_stream(codec,
5199 mout->hp_out_nid[i],
5200 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005201 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005202 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005203 snd_hda_codec_setup_stream(codec,
5204 mout->extra_out_nid[i],
5205 stream_tag, 0, format);
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 /* surrounds */
5208 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005209 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005210 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5211 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005212 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005213 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5214 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 }
5216 return 0;
5217}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005218EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
Takashi Iwaid5191e52009-11-16 14:58:17 +01005220/**
5221 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005223int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5224 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
Takashi Iwaidda14412011-05-02 11:29:30 +02005226 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 int i;
5228
5229 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005230 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005232 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005233 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5234 if (mout->hp_out_nid[i])
5235 snd_hda_codec_cleanup_stream(codec,
5236 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005237 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5238 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005239 snd_hda_codec_cleanup_stream(codec,
5240 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005241 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005243 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 mout->dig_out_used = 0;
5245 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005246 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 return 0;
5248}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005249EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Takashi Iwai47408602012-04-20 13:06:53 +02005251/**
5252 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5253 *
5254 * Guess the suitable VREF pin bits to be set as the pin-control value.
5255 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5256 */
5257unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5258{
5259 unsigned int pincap;
5260 unsigned int oldval;
5261 oldval = snd_hda_codec_read(codec, pin, 0,
5262 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5263 pincap = snd_hda_query_pin_caps(codec, pin);
5264 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5265 /* Exception: if the default pin setup is vref50, we give it priority */
5266 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5267 return AC_PINCTL_VREF_80;
5268 else if (pincap & AC_PINCAP_VREF_50)
5269 return AC_PINCTL_VREF_50;
5270 else if (pincap & AC_PINCAP_VREF_100)
5271 return AC_PINCTL_VREF_100;
5272 else if (pincap & AC_PINCAP_VREF_GRD)
5273 return AC_PINCTL_VREF_GRD;
5274 return AC_PINCTL_VREF_HIZ;
5275}
5276EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5277
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005278/* correct the pin ctl value for matching with the pin cap */
5279unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5280 hda_nid_t pin, unsigned int val)
5281{
5282 static unsigned int cap_lists[][2] = {
5283 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5284 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5285 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5286 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5287 };
5288 unsigned int cap;
5289
5290 if (!val)
5291 return 0;
5292 cap = snd_hda_query_pin_caps(codec, pin);
5293 if (!cap)
5294 return val; /* don't know what to do... */
5295
5296 if (val & AC_PINCTL_OUT_EN) {
5297 if (!(cap & AC_PINCAP_OUT))
5298 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5299 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5300 val &= ~AC_PINCTL_HP_EN;
5301 }
5302
5303 if (val & AC_PINCTL_IN_EN) {
5304 if (!(cap & AC_PINCAP_IN))
5305 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5306 else {
5307 unsigned int vcap, vref;
5308 int i;
5309 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5310 vref = val & AC_PINCTL_VREFEN;
5311 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5312 if (vref == cap_lists[i][0] &&
5313 !(vcap & cap_lists[i][1])) {
5314 if (i == ARRAY_SIZE(cap_lists) - 1)
5315 vref = AC_PINCTL_VREF_HIZ;
5316 else
5317 vref = cap_lists[i + 1][0];
5318 }
5319 }
5320 val &= ~AC_PINCTL_VREFEN;
5321 val |= vref;
5322 }
5323 }
5324
5325 return val;
5326}
5327EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5328
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005329int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5330 unsigned int val, bool cached)
5331{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005332 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005333 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005334 if (cached)
5335 return snd_hda_codec_update_cache(codec, pin, 0,
5336 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5337 else
5338 return snd_hda_codec_write(codec, pin, 0,
5339 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5340}
5341EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5342
Takashi Iwai990061c2010-09-09 22:08:44 +02005343/**
5344 * snd_hda_add_imux_item - Add an item to input_mux
5345 *
5346 * When the same label is used already in the existing items, the number
5347 * suffix is appended to the label. This label index number is stored
5348 * to type_idx when non-NULL pointer is given.
5349 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005350int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5351 int index, int *type_idx)
5352{
5353 int i, label_idx = 0;
5354 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5355 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5356 return -EINVAL;
5357 }
5358 for (i = 0; i < imux->num_items; i++) {
5359 if (!strncmp(label, imux->items[i].label, strlen(label)))
5360 label_idx++;
5361 }
5362 if (type_idx)
5363 *type_idx = label_idx;
5364 if (label_idx > 0)
5365 snprintf(imux->items[imux->num_items].label,
5366 sizeof(imux->items[imux->num_items].label),
5367 "%s %d", label, label_idx);
5368 else
5369 strlcpy(imux->items[imux->num_items].label, label,
5370 sizeof(imux->items[imux->num_items].label));
5371 imux->items[imux->num_items].index = index;
5372 imux->num_items++;
5373 return 0;
5374}
5375EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005376
Takashi Iwai4a471b72005-12-07 13:56:29 +01005377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378#ifdef CONFIG_PM
5379/*
5380 * power management
5381 */
5382
5383/**
5384 * snd_hda_suspend - suspend the codecs
5385 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 *
5387 * Returns 0 if successful.
5388 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005389int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005391 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Takashi Iwai0ba21762007-04-16 11:29:14 +02005393 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005394 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005395 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005396 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 }
5398 return 0;
5399}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005400EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402/**
5403 * snd_hda_resume - resume the codecs
5404 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 *
5406 * Returns 0 if successful.
5407 */
5408int snd_hda_resume(struct hda_bus *bus)
5409{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005410 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Takashi Iwai0ba21762007-04-16 11:29:14 +02005412 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005413 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return 0;
5416}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005417EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005418#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005419
5420/*
5421 * generic arrays
5422 */
5423
Takashi Iwaid5191e52009-11-16 14:58:17 +01005424/**
5425 * snd_array_new - get a new element from the given array
5426 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005427 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005428 * Get a new element from the given array. If it exceeds the
5429 * pre-allocated array size, re-allocate the array.
5430 *
5431 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005432 */
5433void *snd_array_new(struct snd_array *array)
5434{
Takashi Iwai12f17712012-10-10 08:59:14 +02005435 if (snd_BUG_ON(!array->elem_size))
5436 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005437 if (array->used >= array->alloced) {
5438 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005439 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005440 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005441 void *nlist;
5442 if (snd_BUG_ON(num >= 4096))
5443 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005444 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005445 if (!nlist)
5446 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005447 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005448 array->list = nlist;
5449 array->alloced = num;
5450 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005451 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005453EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005454
Takashi Iwaid5191e52009-11-16 14:58:17 +01005455/**
5456 * snd_array_free - free the given array elements
5457 * @array: the array object
5458 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005459void snd_array_free(struct snd_array *array)
5460{
5461 kfree(array->list);
5462 array->used = 0;
5463 array->alloced = 0;
5464 array->list = NULL;
5465}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005466EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005467
Takashi Iwaid5191e52009-11-16 14:58:17 +01005468/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005469 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5470 * @pcm: PCM caps bits
5471 * @buf: the string buffer to write
5472 * @buflen: the max buffer length
5473 *
5474 * used by hda_proc.c and hda_eld.c
5475 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005476void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5477{
5478 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5479 int i, j;
5480
5481 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5482 if (pcm & (AC_SUPPCM_BITS_8 << i))
5483 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5484
5485 buf[j] = '\0'; /* necessary when j == 0 */
5486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005487EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005488
5489MODULE_DESCRIPTION("HDA codec core");
5490MODULE_LICENSE("GPL");