blob: c8f6c3bcb4f32727cb088d925e6a0adea6821e75 [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 Iwaicb53c622007-08-10 17:21:45 +020098static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700101static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
102{
103 if (bus->ops.pm_notify)
104 bus->ops.pm_notify(bus, power_up);
105}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200106#else
107static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200108#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700109#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200110#endif
111
Takashi Iwaid5191e52009-11-16 14:58:17 +0100112/**
113 * snd_hda_get_jack_location - Give a location string of the jack
114 * @cfg: pin default config value
115 *
116 * Parse the pin default config value and returns the string of the
117 * jack location, e.g. "Rear", "Front", etc.
118 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119const char *snd_hda_get_jack_location(u32 cfg)
120{
121 static char *bases[7] = {
122 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
123 };
124 static unsigned char specials_idx[] = {
125 0x07, 0x08,
126 0x17, 0x18, 0x19,
127 0x37, 0x38
128 };
129 static char *specials[] = {
130 "Rear Panel", "Drive Bar",
131 "Riser", "HDMI", "ATAPI",
132 "Mobile-In", "Mobile-Out"
133 };
134 int i;
135 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
136 if ((cfg & 0x0f) < 7)
137 return bases[cfg & 0x0f];
138 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
139 if (cfg == specials_idx[i])
140 return specials[i];
141 }
142 return "UNKNOWN";
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack connectivity, i.e. external or internal connection.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_connectivity(u32 cfg)
154{
155 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
156
157 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100159EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400160
Takashi Iwaid5191e52009-11-16 14:58:17 +0100161/**
162 * snd_hda_get_jack_type - Give a type string of the jack
163 * @cfg: pin default config value
164 *
165 * Parse the pin default config value and returns the string of the
166 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
167 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400168const char *snd_hda_get_jack_type(u32 cfg)
169{
170 static char *jack_types[16] = {
171 "Line Out", "Speaker", "HP Out", "CD",
172 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
173 "Line In", "Aux", "Mic", "Telephony",
174 "SPDIF In", "Digitial In", "Reserved", "Other"
175 };
176
177 return jack_types[(cfg & AC_DEFCFG_DEVICE)
178 >> AC_DEFCFG_DEVICE_SHIFT];
179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100180EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400181
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100182/*
183 * Compose a 32bit command word to be sent to the HD-audio controller
184 */
185static inline unsigned int
186make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
187 unsigned int verb, unsigned int parm)
188{
189 u32 val;
190
Takashi Iwai82e1b802009-07-17 12:47:34 +0200191 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
192 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800193 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
194 codec->addr, direct, nid, verb, parm);
195 return ~0;
196 }
197
198 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100199 val |= (u32)direct << 27;
200 val |= (u32)nid << 20;
201 val |= verb << 8;
202 val |= parm;
203 return val;
204}
205
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206/*
207 * Send and receive a verb
208 */
209static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
210 unsigned int *res)
211{
212 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200214
Wu Fengguang6430aee2009-07-17 16:49:19 +0800215 if (cmd == ~0)
216 return -1;
217
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 if (res)
219 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200220 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221 snd_hda_power_up(codec);
222 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200223 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800226 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 trace_hda_get_response(codec, *res);
228 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229 mutex_unlock(&bus->cmd_mutex);
230 snd_hda_power_down(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +0100231 if (!codec->in_pm && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200232 if (bus->response_reset) {
233 snd_printd("hda_codec: resetting BUS due to "
234 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200236 bus->ops.bus_reset(bus);
237 }
238 goto again;
239 }
240 /* clear reset-flag when the communication gets recovered */
Takashi Iwai989c3182012-11-19 14:14:58 +0100241 if (!err || codec->in_pm)
Takashi Iwai8dd78332009-06-02 01:16:07 +0200242 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 return err;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/**
247 * snd_hda_codec_read - send a command and get the response
248 * @codec: the HDA codec
249 * @nid: NID to send the command
250 * @direct: direct flag
251 * @verb: the verb to send
252 * @parm: the parameter for the verb
253 *
254 * Send a single command and read the corresponding response.
255 *
256 * Returns the obtained response value, or -1 for an error.
257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200258unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
259 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned int verb, unsigned int parm)
261{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200262 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
263 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700264 if (codec_exec_verb(codec, cmd, &res))
265 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return res;
267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100268EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/**
271 * snd_hda_codec_write - send a single command without waiting for response
272 * @codec: the HDA codec
273 * @nid: NID to send the command
274 * @direct: direct flag
275 * @verb: the verb to send
276 * @parm: the parameter for the verb
277 *
278 * Send a single command without waiting for response.
279 *
280 * Returns 0 if successful, or a negative error code.
281 */
282int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
283 unsigned int verb, unsigned int parm)
284{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200285 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100286 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200287 return codec_exec_verb(codec, cmd,
288 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100290EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
293 * snd_hda_sequence_write - sequence writes
294 * @codec: the HDA codec
295 * @seq: VERB array to send
296 *
297 * Send the commands sequentially from the given array.
298 * The array must be terminated with NID=0.
299 */
300void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
301{
302 for (; seq->nid; seq++)
303 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100305EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/**
308 * snd_hda_get_sub_nodes - get the range of sub nodes
309 * @codec: the HDA codec
310 * @nid: NID to parse
311 * @start_id: the pointer to store the start NID
312 *
313 * Parse the NID and store the start NID of its sub-nodes.
314 * Returns the number of sub-nodes.
315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200316int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
317 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned int parm;
320
321 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200322 if (parm == -1)
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *start_id = (parm >> 16) & 0x7fff;
325 return (int)(parm & 0x7fff);
326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100327EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200329/* look up the cached results */
330static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
331{
332 int i, len;
333 for (i = 0; i < array->used; ) {
334 hda_nid_t *p = snd_array_elem(array, i);
335 if (nid == *p)
336 return p;
337 len = p[1];
338 i += len + 2;
339 }
340 return NULL;
341}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343/* read the connection and add to the cache */
344static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200347 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350 if (len < 0)
351 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353}
Takashi Iwaidce20792011-06-24 14:10:28 +0200354
355/**
356 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * @codec: the HDA codec
358 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200359 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @max_conns: max. number of connections to store
361 *
362 * Parses the connection list of the given widget and stores the list
363 * of NIDs.
364 *
365 * Returns the number of connections, or a negative error code.
366 */
367int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200368 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200369{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 struct snd_array *array = &codec->conn_lists;
371 int len;
372 hda_nid_t *p;
373 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200374
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200375 again:
376 mutex_lock(&codec->hash_mutex);
377 len = -1;
378 /* if the connection-list is already cached, read it */
379 p = lookup_conn_list(array, nid);
380 if (p) {
381 len = p[1];
382 if (conn_list && len > max_conns) {
383 snd_printk(KERN_ERR "hda_codec: "
384 "Too many connections %d for NID 0x%x\n",
385 len, nid);
386 mutex_unlock(&codec->hash_mutex);
387 return -EINVAL;
388 }
389 if (conn_list && len)
390 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200392 mutex_unlock(&codec->hash_mutex);
393 if (len >= 0)
394 return len;
395 if (snd_BUG_ON(added))
396 return -EINVAL;
397
398 len = read_and_add_raw_conns(codec, nid);
399 if (len < 0)
400 return len;
401 added = true;
402 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403}
404EXPORT_SYMBOL_HDA(snd_hda_get_connections);
405
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200406/**
407 * snd_hda_get_raw_connections - copy connection list without cache
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @conn_list: connection list array
411 * @max_conns: max. number of connections to store
412 *
413 * Like snd_hda_get_connections(), copy the connection list but without
414 * checking through the connection-list cache.
415 * Currently called only from hda_proc.c, so not exported.
416 */
417int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
418 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100421 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200423 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100424 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Takashi Iwaida3cec32008-08-08 17:12:14 +0200426 if (snd_BUG_ON(!conn_list || max_conns <= 0))
427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200429 wcaps = get_wcaps(codec, nid);
430 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200431 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
432 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
435 if (parm & AC_CLIST_LONG) {
436 /* long form */
437 shift = 16;
438 num_elems = 2;
439 } else {
440 /* short form */
441 shift = 8;
442 num_elems = 4;
443 }
444 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 mask = (1 << (shift-1)) - 1;
446
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0; /* no connection */
449
450 if (conn_len == 1) {
451 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 parm = snd_hda_codec_read(codec, nid, 0,
453 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200454 if (parm == -1 && codec->bus->rirb_error)
455 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 conn_list[0] = parm & mask;
457 return 1;
458 }
459
460 /* multi connection */
461 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100462 prev_nid = 0;
463 for (i = 0; i < conn_len; i++) {
464 int range_val;
465 hda_nid_t val, n;
466
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200467 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100468 parm = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (parm == -1 && codec->bus->rirb_error)
471 return -EIO;
472 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200475 if (val == 0) {
476 snd_printk(KERN_WARNING "hda_codec: "
477 "invalid CONNECT_LIST verb %x[%i]:%x\n",
478 nid, i, parm);
479 return 0;
480 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 parm >>= shift;
482 if (range_val) {
483 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200484 if (!prev_nid || prev_nid >= val) {
485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid dep_range_val %x:%x\n",
487 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 for (n = prev_nid + 1; n <= val; n++) {
491 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200492 snd_printk(KERN_ERR "hda_codec: "
493 "Too many connections %d for NID 0x%x\n",
494 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 return -EINVAL;
496 }
497 conn_list[conns++] = n;
498 }
499 } else {
500 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
503 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100504 return -EINVAL;
505 }
506 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 return conns;
511}
512
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200513static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
514{
515 hda_nid_t *p = snd_array_new(array);
516 if (!p)
517 return false;
518 *p = nid;
519 return true;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200523 * snd_hda_override_conn_list - add/modify the connection-list to cache
524 * @codec: the HDA codec
525 * @nid: NID to parse
526 * @len: number of connection list entries
527 * @list: the list of connection entries
528 *
529 * Add or modify the given connection-list to the cache. If the corresponding
530 * cache already exists, invalidate it and append a new one.
531 *
532 * Returns zero or a negative error code.
533 */
534int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
535 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200537 struct snd_array *array = &codec->conn_lists;
538 hda_nid_t *p;
539 int i, old_used;
540
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200541 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 p = lookup_conn_list(array, nid);
543 if (p)
544 *p = -1; /* invalidate the old entry */
545
546 old_used = array->used;
547 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
548 goto error_add;
549 for (i = 0; i < len; i++)
550 if (!add_conn_list(array, list[i]))
551 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200552 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200553 return 0;
554
555 error_add:
556 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200557 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
561
562/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
568 *
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
572 */
573int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
574 hda_nid_t nid, int recursive)
575{
576 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
577 int i, nums;
578
579 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
580 for (i = 0; i < nums; i++)
581 if (conn[i] == nid)
582 return i;
583 if (!recursive)
584 return -1;
585 if (recursive > 5) {
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
587 return -1;
588 }
589 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 for (i = 0; i < nums; i++) {
591 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
592 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
593 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200594 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
595 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200596 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 return -1;
598}
599EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/**
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
603 * @bus: the BUS
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
606 *
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
610 *
611 * Returns 0 if successful, or a negative error code.
612 */
613int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
614{
615 struct hda_bus_unsolicited *unsol;
616 unsigned int wp;
617
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200618 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200619 unsol = bus->unsol;
620 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622
623 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
624 unsol->wp = wp;
625
626 wp <<= 1;
627 unsol->queue[wp] = res;
628 unsol->queue[wp + 1] = res_ex;
629
Takashi Iwai6acaed32009-01-12 10:09:24 +0100630 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return 0;
633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100634EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800637 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
David Howellsc4028952006-11-22 14:57:56 +0000639static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David Howellsc4028952006-11-22 14:57:56 +0000641 struct hda_bus_unsolicited *unsol =
642 container_of(work, struct hda_bus_unsolicited, work);
643 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct hda_codec *codec;
645 unsigned int rp, caddr, res;
646
647 while (unsol->rp != unsol->wp) {
648 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
649 unsol->rp = rp;
650 rp <<= 1;
651 res = unsol->queue[rp];
652 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200653 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 codec = bus->caddr_tbl[caddr & 0x0f];
656 if (codec && codec->patch_ops.unsol_event)
657 codec->patch_ops.unsol_event(codec, res);
658 }
659}
660
661/*
662 * initialize unsolicited queue
663 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200664static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct hda_bus_unsolicited *unsol;
667
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100668 if (bus->unsol) /* already initialized */
669 return 0;
670
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200671 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200672 if (!unsol) {
673 snd_printk(KERN_ERR "hda_codec: "
674 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -ENOMEM;
676 }
David Howellsc4028952006-11-22 14:57:56 +0000677 INIT_WORK(&unsol->work, process_unsol_events);
678 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 bus->unsol = unsol;
680 return 0;
681}
682
683/*
684 * destructor
685 */
686static void snd_hda_codec_free(struct hda_codec *codec);
687
688static int snd_hda_bus_free(struct hda_bus *bus)
689{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100694 if (bus->workq)
695 flush_workqueue(bus->workq);
696 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200698 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snd_hda_codec_free(codec);
700 }
701 if (bus->ops.private_free)
702 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100703 if (bus->workq)
704 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(bus);
706 return 0;
707}
708
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100709static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100712 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return snd_hda_bus_free(bus);
714}
715
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200716#ifdef CONFIG_SND_HDA_HWDEP
717static int snd_hda_bus_dev_register(struct snd_device *device)
718{
719 struct hda_bus *bus = device->device_data;
720 struct hda_codec *codec;
721 list_for_each_entry(codec, &bus->codec_list, list) {
722 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100723 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200724 }
725 return 0;
726}
727#else
728#define snd_hda_bus_dev_register NULL
729#endif
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100739int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200740 const struct hda_bus_template *temp,
741 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct hda_bus *bus;
744 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200746 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .dev_free = snd_hda_bus_dev_free,
748 };
749
Takashi Iwaida3cec32008-08-08 17:12:14 +0200750 if (snd_BUG_ON(!temp))
751 return -EINVAL;
752 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (busp)
756 *busp = NULL;
757
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200758 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (bus == NULL) {
760 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
761 return -ENOMEM;
762 }
763
764 bus->card = card;
765 bus->private_data = temp->private_data;
766 bus->pci = temp->pci;
767 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100768 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 bus->ops = temp->ops;
770
Ingo Molnar62932df2006-01-16 16:34:20 +0100771 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200772 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&bus->codec_list);
774
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100775 snprintf(bus->workq_name, sizeof(bus->workq_name),
776 "hd-audio%d", card->number);
777 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100778 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100779 snd_printk(KERN_ERR "cannot create workqueue %s\n",
780 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 kfree(bus);
782 return -ENOMEM;
783 }
784
Takashi Iwai0ba21762007-04-16 11:29:14 +0200785 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
786 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_hda_bus_free(bus);
788 return err;
789 }
790 if (busp)
791 *busp = bus;
792 return 0;
793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100794EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai82467612007-07-27 19:15:54 +0200796#ifdef CONFIG_SND_HDA_GENERIC
797#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200798 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200799#else
800#define is_generic_config(codec) 0
801#endif
802
Takashi Iwai645f10c2008-11-28 15:07:37 +0100803#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100804#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
805#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#endif
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
810 * find a matching codec preset
811 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200812static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200813find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100815 struct hda_codec_preset_list *tbl;
816 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200817 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai82467612007-07-27 19:15:54 +0200819 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100820 return NULL; /* use the generic parser */
821
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100822 again:
823 mutex_lock(&preset_mutex);
824 list_for_each_entry(tbl, &hda_preset_tables, list) {
825 if (!try_module_get(tbl->owner)) {
826 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
827 continue;
828 }
829 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100831 if (preset->afg && preset->afg != codec->afg)
832 continue;
833 if (preset->mfg && preset->mfg != codec->mfg)
834 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200837 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 preset->rev == codec->revision_id)) {
840 mutex_unlock(&preset_mutex);
841 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 module_put(tbl->owner);
846 }
847 mutex_unlock(&preset_mutex);
848
849 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
850 char name[32];
851 if (!mod_requested)
852 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
853 codec->vendor_id);
854 else
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
856 (codec->vendor_id >> 16) & 0xffff);
857 request_module(name);
858 mod_requested++;
859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 return NULL;
862}
863
864/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200865 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 const struct hda_vendor_id *c;
870 const char *vendor = NULL;
871 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200872 char tmp[16];
873
874 if (codec->vendor_name)
875 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 for (c = hda_vendor_ids; c->id; c++) {
878 if (c->id == vendor_id) {
879 vendor = c->name;
880 break;
881 }
882 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200883 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sprintf(tmp, "Generic %04x", vendor_id);
885 vendor = tmp;
886 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200887 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
888 if (!codec->vendor_name)
889 return -ENOMEM;
890
891 get_chip_name:
892 if (codec->chip_name)
893 return 0;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200896 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
897 else {
898 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
899 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
900 }
901 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200902 return -ENOMEM;
903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200907 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200911 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 hda_nid_t nid;
913
914 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
915 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200916 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200917 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200918 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200919 case AC_GRP_AUDIO_FUNCTION:
920 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200921 codec->afg_function_id = function_id & 0xff;
922 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200923 break;
924 case AC_GRP_MODEM_FUNCTION:
925 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 codec->mfg_function_id = function_id & 0xff;
927 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 break;
929 default:
930 break;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100936 * read widget caps for each widget and store in cache
937 */
938static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
939{
940 int i;
941 hda_nid_t nid;
942
943 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
944 &codec->start_nid);
945 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200946 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100947 return -ENOMEM;
948 nid = codec->start_nid;
949 for (i = 0; i < codec->num_nodes; i++, nid++)
950 codec->wcaps[i] = snd_hda_param_read(codec, nid,
951 AC_PAR_AUDIO_WIDGET_CAP);
952 return 0;
953}
954
Takashi Iwai3be14142009-02-20 14:11:16 +0100955/* read all pin default configurations and save codec->init_pins */
956static int read_pin_defaults(struct hda_codec *codec)
957{
958 int i;
959 hda_nid_t nid = codec->start_nid;
960
961 for (i = 0; i < codec->num_nodes; i++, nid++) {
962 struct hda_pincfg *pin;
963 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200964 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100965 if (wid_type != AC_WID_PIN)
966 continue;
967 pin = snd_array_new(&codec->init_pins);
968 if (!pin)
969 return -ENOMEM;
970 pin->nid = nid;
971 pin->cfg = snd_hda_codec_read(codec, nid, 0,
972 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200973 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL,
975 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976 }
977 return 0;
978}
979
980/* look up the given pin config list and return the item matching with NID */
981static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
982 struct snd_array *array,
983 hda_nid_t nid)
984{
985 int i;
986 for (i = 0; i < array->used; i++) {
987 struct hda_pincfg *pin = snd_array_elem(array, i);
988 if (pin->nid == nid)
989 return pin;
990 }
991 return NULL;
992}
993
Takashi Iwai3be14142009-02-20 14:11:16 +0100994/* set the current pin config value for the given NID.
995 * the value is cached, and read via snd_hda_codec_get_pincfg()
996 */
997int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
998 hda_nid_t nid, unsigned int cfg)
999{
1000 struct hda_pincfg *pin;
1001
Takashi Iwaib82855a2009-12-27 11:24:56 +01001002 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1003 return -EINVAL;
1004
Takashi Iwai3be14142009-02-20 14:11:16 +01001005 pin = look_up_pincfg(codec, list, nid);
1006 if (!pin) {
1007 pin = snd_array_new(list);
1008 if (!pin)
1009 return -ENOMEM;
1010 pin->nid = nid;
1011 }
1012 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001013 return 0;
1014}
1015
Takashi Iwaid5191e52009-11-16 14:58:17 +01001016/**
1017 * snd_hda_codec_set_pincfg - Override a pin default configuration
1018 * @codec: the HDA codec
1019 * @nid: NID to set the pin config
1020 * @cfg: the pin default config value
1021 *
1022 * Override a pin default configuration value in the cache.
1023 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1024 * priority than the real hardware value.
1025 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001026int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1027 hda_nid_t nid, unsigned int cfg)
1028{
Takashi Iwai346ff702009-02-23 09:42:57 +01001029 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001030}
1031EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1032
Takashi Iwaid5191e52009-11-16 14:58:17 +01001033/**
1034 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1035 * @codec: the HDA codec
1036 * @nid: NID to get the pin config
1037 *
1038 * Get the current pin config value of the given pin NID.
1039 * If the pincfg value is cached or overridden via sysfs or driver,
1040 * returns the cached value.
1041 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001042unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1043{
1044 struct hda_pincfg *pin;
1045
Takashi Iwai3be14142009-02-20 14:11:16 +01001046#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001047 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001048 if (pin)
1049 return pin->cfg;
1050#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001051 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1052 if (pin)
1053 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001054 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1055 if (pin)
1056 return pin->cfg;
1057 return 0;
1058}
1059EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1060
Takashi Iwai92ee6162009-12-27 11:18:59 +01001061/**
1062 * snd_hda_shutup_pins - Shut up all pins
1063 * @codec: the HDA codec
1064 *
1065 * Clear all pin controls to shup up before suspend for avoiding click noise.
1066 * The controls aren't cached so that they can be resumed properly.
1067 */
1068void snd_hda_shutup_pins(struct hda_codec *codec)
1069{
1070 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001071 /* don't shut up pins when unloading the driver; otherwise it breaks
1072 * the default pin setup at the next load of the driver
1073 */
1074 if (codec->bus->shutdown)
1075 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001076 for (i = 0; i < codec->init_pins.used; i++) {
1077 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1078 /* use read here for syncing after issuing each verb */
1079 snd_hda_codec_read(codec, pin->nid, 0,
1080 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1081 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001082 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001083}
1084EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1085
Takashi Iwai2a439522011-07-26 09:52:50 +02001086#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001087/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1088static void restore_shutup_pins(struct hda_codec *codec)
1089{
1090 int i;
1091 if (!codec->pins_shutup)
1092 return;
1093 if (codec->bus->shutdown)
1094 return;
1095 for (i = 0; i < codec->init_pins.used; i++) {
1096 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1097 snd_hda_codec_write(codec, pin->nid, 0,
1098 AC_VERB_SET_PIN_WIDGET_CONTROL,
1099 pin->ctrl);
1100 }
1101 codec->pins_shutup = 0;
1102}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001103#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001104
David Henningsson26a6cb62012-10-09 15:04:21 +02001105static void hda_jackpoll_work(struct work_struct *work)
1106{
1107 struct hda_codec *codec =
1108 container_of(work, struct hda_codec, jackpoll_work.work);
1109 if (!codec->jackpoll_interval)
1110 return;
1111
1112 snd_hda_jack_set_dirty_all(codec);
1113 snd_hda_jack_poll_all(codec);
1114 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1115 codec->jackpoll_interval);
1116}
1117
Takashi Iwai01751f52007-08-10 16:59:39 +02001118static void init_hda_cache(struct hda_cache_rec *cache,
1119 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001120static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001121
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001122/* release all pincfg lists */
1123static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001124{
Takashi Iwai346ff702009-02-23 09:42:57 +01001125 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001126#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001127 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001128#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001129 snd_array_free(&codec->init_pins);
1130}
1131
Takashi Iwai54d17402005-11-21 16:33:22 +01001132/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001133 * audio-converter setup caches
1134 */
1135struct hda_cvt_setup {
1136 hda_nid_t nid;
1137 u8 stream_tag;
1138 u8 channel_id;
1139 u16 format_id;
1140 unsigned char active; /* cvt is currently used */
1141 unsigned char dirty; /* setups should be cleared */
1142};
1143
1144/* get or create a cache entry for the given audio converter NID */
1145static struct hda_cvt_setup *
1146get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1147{
1148 struct hda_cvt_setup *p;
1149 int i;
1150
1151 for (i = 0; i < codec->cvt_setups.used; i++) {
1152 p = snd_array_elem(&codec->cvt_setups, i);
1153 if (p->nid == nid)
1154 return p;
1155 }
1156 p = snd_array_new(&codec->cvt_setups);
1157 if (p)
1158 p->nid = nid;
1159 return p;
1160}
1161
1162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * codec destructor
1164 */
1165static void snd_hda_codec_free(struct hda_codec *codec)
1166{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001167 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001169 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001170 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001171 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001172#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001173 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001174 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001177 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001178 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001179 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001180 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001181 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 codec->bus->caddr_tbl[codec->addr] = NULL;
1183 if (codec->patch_ops.free)
1184 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001185#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001186 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001187 hda_call_pm_notify(codec->bus, false);
1188#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001189 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001190 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001191 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001192 kfree(codec->vendor_name);
1193 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001194 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001195 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 kfree(codec);
1197}
1198
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001199static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1200 hda_nid_t fg, unsigned int power_state);
1201
Takashi Iwaid8193872012-08-31 07:54:38 -07001202static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001203 unsigned int power_state);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/**
1206 * snd_hda_codec_new - create a HDA codec
1207 * @bus: the bus to assign
1208 * @codec_addr: the codec address
1209 * @codecp: the pointer to store the generated codec
1210 *
1211 * Returns 0 if successful, or a negative error code.
1212 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001213int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1214 unsigned int codec_addr,
1215 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001218 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001219 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int err;
1221
Takashi Iwaida3cec32008-08-08 17:12:14 +02001222 if (snd_BUG_ON(!bus))
1223 return -EINVAL;
1224 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001228 snd_printk(KERN_ERR "hda_codec: "
1229 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EBUSY;
1231 }
1232
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001233 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (codec == NULL) {
1235 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1236 return -ENOMEM;
1237 }
1238
1239 codec->bus = bus;
1240 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001241 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001242 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001243 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001244 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001245 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001246 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1247 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001248 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001249 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001250 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001251 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001252 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
David Henningsson26a6cb62012-10-09 15:04:21 +02001253 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Takashi Iwai83012a72012-08-24 18:38:08 +02001255#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001256 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001257 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1258 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1259 * the caller has to power down appropriatley after initialization
1260 * phase.
1261 */
1262 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001263 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001264#endif
1265
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001266 if (codec->bus->modelname) {
1267 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1268 if (!codec->modelname) {
1269 snd_hda_codec_free(codec);
1270 return -ENODEV;
1271 }
1272 }
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 list_add_tail(&codec->list, &bus->codec_list);
1275 bus->caddr_tbl[codec_addr] = codec;
1276
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1278 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001279 if (codec->vendor_id == -1)
1280 /* read again, hopefully the access method was corrected
1281 * in the last read...
1282 */
1283 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1284 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001285 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1286 AC_PAR_SUBSYSTEM_ID);
1287 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001290 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001291 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001292 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001293 err = -ENODEV;
1294 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Takashi Iwaid8193872012-08-31 07:54:38 -07001297 fg = codec->afg ? codec->afg : codec->mfg;
1298 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001299 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001300 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001301 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001302 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 err = read_pin_defaults(codec);
1304 if (err < 0)
1305 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001306
Takashi Iwai0ba21762007-04-16 11:29:14 +02001307 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001308 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001309 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001310 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001311 }
1312
Takashi Iwai83012a72012-08-24 18:38:08 +02001313#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001315 AC_PWRST_CLKSTOP);
1316 if (!codec->d3_stop_clk)
1317 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001318#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001320 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001321
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001322 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001323 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001325 snd_hda_codec_proc_new(codec);
1326
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001327 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328
1329 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1330 codec->subsystem_id, codec->revision_id);
1331 snd_component_add(codec->bus->card, component);
1332
1333 if (codecp)
1334 *codecp = codec;
1335 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001336
1337 error:
1338 snd_hda_codec_free(codec);
1339 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001341EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001342
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343/**
1344 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1345 * @codec: the HDA codec
1346 *
1347 * Start parsing of the given codec tree and (re-)initialize the whole
1348 * patch instance.
1349 *
1350 * Returns 0 if successful or a negative error code.
1351 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352int snd_hda_codec_configure(struct hda_codec *codec)
1353{
1354 int err;
1355
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001356 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001357 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001358 err = get_codec_name(codec);
1359 if (err < 0)
1360 return err;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai82467612007-07-27 19:15:54 +02001363 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001365 goto patched;
1366 }
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (codec->preset && codec->preset->patch) {
1368 err = codec->preset->patch(codec);
1369 goto patched;
1370 }
1371
1372 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001373 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001374 if (err < 0)
1375 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001376
1377 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001378 if (!err && codec->patch_ops.unsol_event)
1379 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001380 /* audio codec should override the mixer name */
1381 if (!err && (codec->afg || !*codec->bus->card->mixername))
1382 snprintf(codec->bus->card->mixername,
1383 sizeof(codec->bus->card->mixername),
1384 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001387EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Takashi Iwaied360812012-08-08 17:12:52 +02001389/* update the stream-id if changed */
1390static void update_pcm_stream_id(struct hda_codec *codec,
1391 struct hda_cvt_setup *p, hda_nid_t nid,
1392 u32 stream_tag, int channel_id)
1393{
1394 unsigned int oldval, newval;
1395
1396 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1397 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1398 newval = (stream_tag << 4) | channel_id;
1399 if (oldval != newval)
1400 snd_hda_codec_write(codec, nid, 0,
1401 AC_VERB_SET_CHANNEL_STREAMID,
1402 newval);
1403 p->stream_tag = stream_tag;
1404 p->channel_id = channel_id;
1405 }
1406}
1407
1408/* update the format-id if changed */
1409static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1410 hda_nid_t nid, int format)
1411{
1412 unsigned int oldval;
1413
1414 if (p->format_id != format) {
1415 oldval = snd_hda_codec_read(codec, nid, 0,
1416 AC_VERB_GET_STREAM_FORMAT, 0);
1417 if (oldval != format) {
1418 msleep(1);
1419 snd_hda_codec_write(codec, nid, 0,
1420 AC_VERB_SET_STREAM_FORMAT,
1421 format);
1422 }
1423 p->format_id = format;
1424 }
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/**
1428 * snd_hda_codec_setup_stream - set up the codec for streaming
1429 * @codec: the CODEC to set up
1430 * @nid: the NID to set up
1431 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1432 * @channel_id: channel id to pass, zero based.
1433 * @format: stream format.
1434 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1436 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int channel_id, int format)
1438{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001439 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001440 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001441 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001442 int i;
1443
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001445 return;
1446
Takashi Iwai0ba21762007-04-16 11:29:14 +02001447 snd_printdd("hda_codec_setup_stream: "
1448 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001450 p = get_hda_cvt_setup(codec, nid);
1451 if (!p)
1452 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001453
1454 if (codec->pcm_format_first)
1455 update_pcm_format(codec, p, nid, format);
1456 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1457 if (!codec->pcm_format_first)
1458 update_pcm_format(codec, p, nid, format);
1459
Takashi Iwaieb541332010-08-06 13:48:11 +02001460 p->active = 1;
1461 p->dirty = 0;
1462
1463 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001464 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001465 list_for_each_entry(c, &codec->bus->codec_list, list) {
1466 for (i = 0; i < c->cvt_setups.used; i++) {
1467 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001468 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001469 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001470 p->dirty = 1;
1471 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001474EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Takashi Iwaif0cea792010-08-13 11:56:53 +02001476static void really_cleanup_stream(struct hda_codec *codec,
1477 struct hda_cvt_setup *q);
1478
Takashi Iwaid5191e52009-11-16 14:58:17 +01001479/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001480 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001481 * @codec: the CODEC to clean up
1482 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001483 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1486 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001487{
Takashi Iwaieb541332010-08-06 13:48:11 +02001488 struct hda_cvt_setup *p;
1489
Takashi Iwai888afa12008-03-18 09:57:50 +01001490 if (!nid)
1491 return;
1492
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001493 if (codec->no_sticky_stream)
1494 do_now = 1;
1495
Takashi Iwai888afa12008-03-18 09:57:50 +01001496 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001498 if (p) {
1499 /* here we just clear the active flag when do_now isn't set;
1500 * actual clean-ups will be done later in
1501 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1502 */
1503 if (do_now)
1504 really_cleanup_stream(codec, p);
1505 else
1506 p->active = 0;
1507 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001508}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001509EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001510
Takashi Iwaieb541332010-08-06 13:48:11 +02001511static void really_cleanup_stream(struct hda_codec *codec,
1512 struct hda_cvt_setup *q)
1513{
1514 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001515 if (q->stream_tag || q->channel_id)
1516 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1517 if (q->format_id)
1518 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1519);
Takashi Iwaieb541332010-08-06 13:48:11 +02001520 memset(q, 0, sizeof(*q));
1521 q->nid = nid;
1522}
1523
1524/* clean up the all conflicting obsolete streams */
1525static void purify_inactive_streams(struct hda_codec *codec)
1526{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001527 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001528 int i;
1529
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001530 list_for_each_entry(c, &codec->bus->codec_list, list) {
1531 for (i = 0; i < c->cvt_setups.used; i++) {
1532 struct hda_cvt_setup *p;
1533 p = snd_array_elem(&c->cvt_setups, i);
1534 if (p->dirty)
1535 really_cleanup_stream(c, p);
1536 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001537 }
1538}
1539
Takashi Iwai2a439522011-07-26 09:52:50 +02001540#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001541/* clean up all streams; called from suspend */
1542static void hda_cleanup_all_streams(struct hda_codec *codec)
1543{
1544 int i;
1545
1546 for (i = 0; i < codec->cvt_setups.used; i++) {
1547 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1548 if (p->stream_tag)
1549 really_cleanup_stream(codec, p);
1550 }
1551}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001552#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * amp access functions
1556 */
1557
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001558/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001559#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001560#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001561#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1562#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001564#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001567static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001568 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwai01751f52007-08-10 16:59:39 +02001570 memset(cache, 0, sizeof(*cache));
1571 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001572 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001573}
1574
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001575static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001576{
Takashi Iwai603c4012008-07-30 15:01:44 +02001577 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001581static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai01751f52007-08-10 16:59:39 +02001583 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1584 u16 cur = cache->hash[idx];
1585 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001588 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (info->key == key)
1590 return info;
1591 cur = info->next;
1592 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001593 return NULL;
1594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001596/* query the hash. allocate an entry if not found. */
1597static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1598 u32 key)
1599{
1600 struct hda_cache_head *info = get_hash(cache, key);
1601 if (!info) {
1602 u16 idx, cur;
1603 /* add a new hash entry */
1604 info = snd_array_new(&cache->buf);
1605 if (!info)
1606 return NULL;
1607 cur = snd_array_index(&cache->buf, info);
1608 info->key = key;
1609 info->val = 0;
1610 idx = key % (u16)ARRAY_SIZE(cache->hash);
1611 info->next = cache->hash[idx];
1612 cache->hash[idx] = cur;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return info;
1615}
1616
Takashi Iwai01751f52007-08-10 16:59:39 +02001617/* query and allocate an amp hash entry */
1618static inline struct hda_amp_info *
1619get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1620{
1621 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1622}
1623
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001624/* overwrite the value with the key in the caps hash */
1625static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1626{
1627 struct hda_amp_info *info;
1628
1629 mutex_lock(&codec->hash_mutex);
1630 info = get_alloc_amp_hash(codec, key);
1631 if (!info) {
1632 mutex_unlock(&codec->hash_mutex);
1633 return -EINVAL;
1634 }
1635 info->amp_caps = val;
1636 info->head.val |= INFO_AMP_CAPS;
1637 mutex_unlock(&codec->hash_mutex);
1638 return 0;
1639}
1640
1641/* query the value from the caps hash; if not found, fetch the current
1642 * value from the given function and store in the hash
1643 */
1644static unsigned int
1645query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1646 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1647{
1648 struct hda_amp_info *info;
1649 unsigned int val;
1650
1651 mutex_lock(&codec->hash_mutex);
1652 info = get_alloc_amp_hash(codec, key);
1653 if (!info) {
1654 mutex_unlock(&codec->hash_mutex);
1655 return 0;
1656 }
1657 if (!(info->head.val & INFO_AMP_CAPS)) {
1658 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1659 val = func(codec, nid, dir);
1660 write_caps_hash(codec, key, val);
1661 } else {
1662 val = info->amp_caps;
1663 mutex_unlock(&codec->hash_mutex);
1664 }
1665 return val;
1666}
1667
1668static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1669 int direction)
1670{
1671 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1672 nid = codec->afg;
1673 return snd_hda_param_read(codec, nid,
1674 direction == HDA_OUTPUT ?
1675 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1676}
1677
Takashi Iwaid5191e52009-11-16 14:58:17 +01001678/**
1679 * query_amp_caps - query AMP capabilities
1680 * @codec: the HD-auio codec
1681 * @nid: the NID to query
1682 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1683 *
1684 * Query AMP capabilities for the given widget and direction.
1685 * Returns the obtained capability bits.
1686 *
1687 * When cap bits have been already read, this doesn't read again but
1688 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001690u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001692 return query_caps_hash(codec, nid, direction,
1693 HDA_HASH_KEY(nid, direction, 0),
1694 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001696EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwaid5191e52009-11-16 14:58:17 +01001698/**
1699 * snd_hda_override_amp_caps - Override the AMP capabilities
1700 * @codec: the CODEC to clean up
1701 * @nid: the NID to clean up
1702 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1703 * @caps: the capability bits to set
1704 *
1705 * Override the cached AMP caps bits value by the given one.
1706 * This function is useful if the driver needs to adjust the AMP ranges,
1707 * e.g. limit to 0dB, etc.
1708 *
1709 * Returns zero if successful or a negative error code.
1710 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001711int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1712 unsigned int caps)
1713{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001714 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001715}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001716EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001717
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001718static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1719 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001720{
1721 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1722}
1723
Takashi Iwaid5191e52009-11-16 14:58:17 +01001724/**
1725 * snd_hda_query_pin_caps - Query PIN capabilities
1726 * @codec: the HD-auio codec
1727 * @nid: the NID to query
1728 *
1729 * Query PIN capabilities for the given widget.
1730 * Returns the obtained capability bits.
1731 *
1732 * When cap bits have been already read, this doesn't read again but
1733 * returns the cached value.
1734 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001735u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1736{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001737 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001738 read_pin_cap);
1739}
Takashi Iwai1327a322009-03-23 13:07:47 +01001740EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1741
Wu Fengguang864f92b2009-11-18 12:38:02 +08001742/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001743 * snd_hda_override_pin_caps - Override the pin capabilities
1744 * @codec: the CODEC
1745 * @nid: the NID to override
1746 * @caps: the capability bits to set
1747 *
1748 * Override the cached PIN capabilitiy bits value by the given one.
1749 *
1750 * Returns zero if successful or a negative error code.
1751 */
1752int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1753 unsigned int caps)
1754{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001756}
1757EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1758
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001759/* read or sync the hash value with the current value;
1760 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762static struct hda_amp_info *
1763update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1764 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001766 struct hda_amp_info *info;
1767 unsigned int parm, val = 0;
1768 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001770 retry:
1771 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1772 if (!info)
1773 return NULL;
1774 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1775 if (!val_read) {
1776 mutex_unlock(&codec->hash_mutex);
1777 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1778 parm |= direction == HDA_OUTPUT ?
1779 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1780 parm |= index;
1781 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001782 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001783 val &= 0xff;
1784 val_read = true;
1785 mutex_lock(&codec->hash_mutex);
1786 goto retry;
1787 }
1788 info->vol[ch] = val;
1789 info->head.val |= INFO_AMP_VOL(ch);
1790 }
1791 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001797static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001798 hda_nid_t nid, int ch, int direction, int index,
1799 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 u32 parm;
1802
1803 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1804 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1805 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001806 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1807 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1808 ; /* set the zero value as a fake mute */
1809 else
1810 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1812}
1813
Takashi Iwaid5191e52009-11-16 14:58:17 +01001814/**
1815 * snd_hda_codec_amp_read - Read AMP value
1816 * @codec: HD-audio codec
1817 * @nid: NID to read the AMP value
1818 * @ch: channel (left=0 or right=1)
1819 * @direction: #HDA_INPUT or #HDA_OUTPUT
1820 * @index: the index value (only for input direction)
1821 *
1822 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Takashi Iwai834be882006-03-01 14:16:17 +01001824int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1825 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001827 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001828 unsigned int val = 0;
1829
1830 mutex_lock(&codec->hash_mutex);
1831 info = update_amp_hash(codec, nid, ch, direction, index);
1832 if (info)
1833 val = info->vol[ch];
1834 mutex_unlock(&codec->hash_mutex);
1835 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001837EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Takashi Iwaid5191e52009-11-16 14:58:17 +01001839/**
1840 * snd_hda_codec_amp_update - update the AMP value
1841 * @codec: HD-audio codec
1842 * @nid: NID to read the AMP value
1843 * @ch: channel (left=0 or right=1)
1844 * @direction: #HDA_INPUT or #HDA_OUTPUT
1845 * @idx: the index value (only for input direction)
1846 * @mask: bit mask to set
1847 * @val: the bits value to set
1848 *
1849 * Update the AMP value with a bit mask.
1850 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001851 */
Takashi Iwai834be882006-03-01 14:16:17 +01001852int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1853 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001855 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001856
Takashi Iwai467126462010-03-29 09:19:38 +02001857 if (snd_BUG_ON(mask & ~0xff))
1858 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001859 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001860
1861 mutex_lock(&codec->hash_mutex);
1862 info = update_amp_hash(codec, nid, ch, direction, idx);
1863 if (!info) {
1864 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001866 }
1867 val |= info->vol[ch] & ~mask;
1868 if (info->vol[ch] == val) {
1869 mutex_unlock(&codec->hash_mutex);
1870 return 0;
1871 }
1872 info->vol[ch] = val;
1873 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001874 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 1;
1876}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001877EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Takashi Iwaid5191e52009-11-16 14:58:17 +01001879/**
1880 * snd_hda_codec_amp_stereo - update the AMP stereo values
1881 * @codec: HD-audio codec
1882 * @nid: NID to read the AMP value
1883 * @direction: #HDA_INPUT or #HDA_OUTPUT
1884 * @idx: the index value (only for input direction)
1885 * @mask: bit mask to set
1886 * @val: the bits value to set
1887 *
1888 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1889 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001890 */
1891int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1892 int direction, int idx, int mask, int val)
1893{
1894 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001895
1896 if (snd_BUG_ON(mask & ~0xff))
1897 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001898 for (ch = 0; ch < 2; ch++)
1899 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1900 idx, mask, val);
1901 return ret;
1902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001903EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001904
Takashi Iwai2a439522011-07-26 09:52:50 +02001905#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001906/**
1907 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1908 * @codec: HD-audio codec
1909 *
1910 * Resume the all amp commands from the cache.
1911 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001912void snd_hda_codec_resume_amp(struct hda_codec *codec)
1913{
Takashi Iwai603c4012008-07-30 15:01:44 +02001914 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001915 int i;
1916
Takashi Iwai603c4012008-07-30 15:01:44 +02001917 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001918 u32 key = buffer->head.key;
1919 hda_nid_t nid;
1920 unsigned int idx, dir, ch;
1921 if (!key)
1922 continue;
1923 nid = key & 0xff;
1924 idx = (key >> 16) & 0xff;
1925 dir = (key >> 24) & 0xff;
1926 for (ch = 0; ch < 2; ch++) {
1927 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1928 continue;
1929 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1930 buffer->vol[ch]);
1931 }
1932 }
1933}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001934EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001935#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001937static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1938 unsigned int ofs)
1939{
1940 u32 caps = query_amp_caps(codec, nid, dir);
1941 /* get num steps */
1942 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1943 if (ofs < caps)
1944 caps -= ofs;
1945 return caps;
1946}
1947
Takashi Iwaid5191e52009-11-16 14:58:17 +01001948/**
1949 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1950 *
1951 * The control element is supposed to have the private_value field
1952 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1953 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001954int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1958 u16 nid = get_amp_nid(kcontrol);
1959 u8 chs = get_amp_channels(kcontrol);
1960 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001961 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001963 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1964 uinfo->count = chs == 3 ? 2 : 1;
1965 uinfo->value.integer.min = 0;
1966 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1967 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001968 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001969 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1970 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -EINVAL;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001975EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001977
1978static inline unsigned int
1979read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1980 int ch, int dir, int idx, unsigned int ofs)
1981{
1982 unsigned int val;
1983 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1984 val &= HDA_AMP_VOLMASK;
1985 if (val >= ofs)
1986 val -= ofs;
1987 else
1988 val = 0;
1989 return val;
1990}
1991
1992static inline int
1993update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1994 int ch, int dir, int idx, unsigned int ofs,
1995 unsigned int val)
1996{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001997 unsigned int maxval;
1998
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999 if (val > 0)
2000 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002001 /* ofs = 0: raw max value */
2002 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002003 if (val > maxval)
2004 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002005 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2006 HDA_AMP_VOLMASK, val);
2007}
2008
Takashi Iwaid5191e52009-11-16 14:58:17 +01002009/**
2010 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2011 *
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002015int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 hda_nid_t nid = get_amp_nid(kcontrol);
2020 int chs = get_amp_channels(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
2022 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002023 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 long *valp = ucontrol->value.integer.value;
2025
2026 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002029 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002032EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2036 *
2037 * The control element is supposed to have the private_value field
2038 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002040int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2044 hda_nid_t nid = get_amp_nid(kcontrol);
2045 int chs = get_amp_channels(kcontrol);
2046 int dir = get_amp_direction(kcontrol);
2047 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002048 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 long *valp = ucontrol->value.integer.value;
2050 int change = 0;
2051
Takashi Iwaicb53c622007-08-10 17:21:45 +02002052 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002053 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002054 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002055 valp++;
2056 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002057 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002058 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002059 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return change;
2061}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002062EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Takashi Iwaid5191e52009-11-16 14:58:17 +01002064/**
2065 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2066 *
2067 * The control element is supposed to have the private_value field
2068 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2069 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002070int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2071 unsigned int size, unsigned int __user *_tlv)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 hda_nid_t nid = get_amp_nid(kcontrol);
2075 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002076 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002077 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002078 u32 caps, val1, val2;
2079
2080 if (size < 4 * sizeof(unsigned int))
2081 return -ENOMEM;
2082 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002083 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2084 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002085 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002086 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002087 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002088 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002089 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002090 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2091 return -EFAULT;
2092 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2093 return -EFAULT;
2094 if (put_user(val1, _tlv + 2))
2095 return -EFAULT;
2096 if (put_user(val2, _tlv + 3))
2097 return -EFAULT;
2098 return 0;
2099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002100EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002101
Takashi Iwaid5191e52009-11-16 14:58:17 +01002102/**
2103 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2104 * @codec: HD-audio codec
2105 * @nid: NID of a reference widget
2106 * @dir: #HDA_INPUT or #HDA_OUTPUT
2107 * @tlv: TLV data to be stored, at least 4 elements
2108 *
2109 * Set (static) TLV data for a virtual master volume using the AMP caps
2110 * obtained from the reference NID.
2111 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002112 */
2113void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2114 unsigned int *tlv)
2115{
2116 u32 caps;
2117 int nums, step;
2118
2119 caps = query_amp_caps(codec, nid, dir);
2120 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2121 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2122 step = (step + 1) * 25;
2123 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2124 tlv[1] = 2 * sizeof(unsigned int);
2125 tlv[2] = -nums * step;
2126 tlv[3] = step;
2127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002128EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002129
2130/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002131static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002132find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002133{
2134 struct snd_ctl_elem_id id;
2135 memset(&id, 0, sizeof(id));
2136 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002137 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002138 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002139 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2140 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002141 strcpy(id.name, name);
2142 return snd_ctl_find_id(codec->bus->card, &id);
2143}
2144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
2146 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2147 * @codec: HD-audio codec
2148 * @name: ctl id name string
2149 *
2150 * Get the control element with the given id string and IFACE_MIXER.
2151 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002152struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2153 const char *name)
2154{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002155 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002157EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002158
Takashi Iwaidcda5802012-10-12 17:24:51 +02002159static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2160 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002161{
2162 int idx;
2163 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002164 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002165 return idx;
2166 }
2167 return -EBUSY;
2168}
2169
Takashi Iwaid5191e52009-11-16 14:58:17 +01002170/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002171 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002172 * @codec: HD-audio codec
2173 * @nid: corresponding NID (optional)
2174 * @kctl: the control element to assign
2175 *
2176 * Add the given control element to an array inside the codec instance.
2177 * All control elements belonging to a codec are supposed to be added
2178 * by this function so that a proper clean-up works at the free or
2179 * reconfiguration time.
2180 *
2181 * If non-zero @nid is passed, the NID is assigned to the control element.
2182 * The assignment is shown in the codec proc file.
2183 *
2184 * snd_hda_ctl_add() checks the control subdev id field whether
2185 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002186 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2187 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002188 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002189int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2190 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002191{
2192 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002193 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002194 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002195
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002196 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002197 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002198 if (nid == 0)
2199 nid = get_amp_nid_(kctl->private_value);
2200 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002201 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2202 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002203 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002204 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002205 err = snd_ctl_add(codec->bus->card, kctl);
2206 if (err < 0)
2207 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002208 item = snd_array_new(&codec->mixers);
2209 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002210 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002211 item->kctl = kctl;
2212 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002213 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002214 return 0;
2215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002216EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002219 * snd_hda_add_nid - Assign a NID to a control element
2220 * @codec: HD-audio codec
2221 * @nid: corresponding NID (optional)
2222 * @kctl: the control element to assign
2223 * @index: index to kctl
2224 *
2225 * Add the given control element to an array inside the codec instance.
2226 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2227 * NID:KCTL mapping - for example "Capture Source" selector.
2228 */
2229int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2230 unsigned int index, hda_nid_t nid)
2231{
2232 struct hda_nid_item *item;
2233
2234 if (nid > 0) {
2235 item = snd_array_new(&codec->nids);
2236 if (!item)
2237 return -ENOMEM;
2238 item->kctl = kctl;
2239 item->index = index;
2240 item->nid = nid;
2241 return 0;
2242 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002243 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2244 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002245 return -EINVAL;
2246}
2247EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2248
2249/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002250 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2251 * @codec: HD-audio codec
2252 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002253void snd_hda_ctls_clear(struct hda_codec *codec)
2254{
2255 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002256 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002257 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002258 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002259 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002260 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002261}
2262
Takashi Iwaia65d6292009-02-23 16:57:04 +01002263/* pseudo device locking
2264 * toggle card->shutdown to allow/disallow the device access (as a hack)
2265 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002266int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002267{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002268 struct snd_card *card = bus->card;
2269 struct hda_codec *codec;
2270
Takashi Iwaia65d6292009-02-23 16:57:04 +01002271 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002272 if (card->shutdown)
2273 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002274 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002275 if (!list_empty(&card->ctl_files))
2276 goto err_clear;
2277
2278 list_for_each_entry(codec, &bus->codec_list, list) {
2279 int pcm;
2280 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2281 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2282 if (!cpcm->pcm)
2283 continue;
2284 if (cpcm->pcm->streams[0].substream_opened ||
2285 cpcm->pcm->streams[1].substream_opened)
2286 goto err_clear;
2287 }
2288 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002289 spin_unlock(&card->files_lock);
2290 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002291
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002292 err_clear:
2293 card->shutdown = 0;
2294 err_unlock:
2295 spin_unlock(&card->files_lock);
2296 return -EINVAL;
2297}
2298EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2299
2300void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002301{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002302 struct snd_card *card = bus->card;
2303
2304 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002305 spin_lock(&card->files_lock);
2306 card->shutdown = 0;
2307 spin_unlock(&card->files_lock);
2308}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002309EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002310
Takashi Iwaid5191e52009-11-16 14:58:17 +01002311/**
2312 * snd_hda_codec_reset - Clear all objects assigned to the codec
2313 * @codec: HD-audio codec
2314 *
2315 * This frees the all PCM and control elements assigned to the codec, and
2316 * clears the caches and restores the pin default configurations.
2317 *
2318 * When a device is being used, it returns -EBSY. If successfully freed,
2319 * returns zero.
2320 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002321int snd_hda_codec_reset(struct hda_codec *codec)
2322{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323 struct hda_bus *bus = codec->bus;
2324 struct snd_card *card = bus->card;
2325 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002326
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002327 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002328 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002329
2330 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002331 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002332#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002333 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002334 codec->power_on = 0;
2335 codec->power_transition = 0;
2336 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002337 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002338#endif
2339 snd_hda_ctls_clear(codec);
2340 /* relase PCMs */
2341 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002342 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002343 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002344 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002345 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002346 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002347 }
2348 if (codec->patch_ops.free)
2349 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002350 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002351 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002352 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002353 codec->spec = NULL;
2354 free_hda_cache(&codec->amp_cache);
2355 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002356 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2357 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002358 /* free only driver_pins so that init_pins + user_pins are restored */
2359 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002360 snd_array_free(&codec->cvt_setups);
2361 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002362 codec->num_pcms = 0;
2363 codec->pcm_info = NULL;
2364 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002365 codec->slave_dig_outs = NULL;
2366 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002367 module_put(codec->owner);
2368 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002369
2370 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002371 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002372 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002373}
2374
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002375typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2376
2377/* apply the function to all matching slave ctls in the mixer list */
2378static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002379 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002380{
2381 struct hda_nid_item *items;
2382 const char * const *s;
2383 int i, err;
2384
2385 items = codec->mixers.list;
2386 for (i = 0; i < codec->mixers.used; i++) {
2387 struct snd_kcontrol *sctl = items[i].kctl;
2388 if (!sctl || !sctl->id.name ||
2389 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2390 continue;
2391 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002392 char tmpname[sizeof(sctl->id.name)];
2393 const char *name = *s;
2394 if (suffix) {
2395 snprintf(tmpname, sizeof(tmpname), "%s %s",
2396 name, suffix);
2397 name = tmpname;
2398 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002399 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002400 err = func(data, sctl);
2401 if (err)
2402 return err;
2403 break;
2404 }
2405 }
2406 }
2407 return 0;
2408}
2409
2410static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2411{
2412 return 1;
2413}
2414
Takashi Iwai18478e82012-03-09 17:51:10 +01002415/* guess the value corresponding to 0dB */
2416static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2417{
2418 int _tlv[4];
2419 const int *tlv = NULL;
2420 int val = -1;
2421
2422 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2423 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2424 mm_segment_t fs = get_fs();
2425 set_fs(get_ds());
2426 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2427 tlv = _tlv;
2428 set_fs(fs);
2429 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2430 tlv = kctl->tlv.p;
2431 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2432 val = -tlv[2] / tlv[3];
2433 return val;
2434}
2435
2436/* call kctl->put with the given value(s) */
2437static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2438{
2439 struct snd_ctl_elem_value *ucontrol;
2440 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2441 if (!ucontrol)
2442 return -ENOMEM;
2443 ucontrol->value.integer.value[0] = val;
2444 ucontrol->value.integer.value[1] = val;
2445 kctl->put(kctl, ucontrol);
2446 kfree(ucontrol);
2447 return 0;
2448}
2449
2450/* initialize the slave volume with 0dB */
2451static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2452{
2453 int offset = get_kctl_0dB_offset(slave);
2454 if (offset > 0)
2455 put_kctl_with_value(slave, offset);
2456 return 0;
2457}
2458
2459/* unmute the slave */
2460static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2461{
2462 return put_kctl_with_value(slave, 1);
2463}
2464
Takashi Iwaid5191e52009-11-16 14:58:17 +01002465/**
2466 * snd_hda_add_vmaster - create a virtual master control and add slaves
2467 * @codec: HD-audio codec
2468 * @name: vmaster control name
2469 * @tlv: TLV data (optional)
2470 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002471 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002472 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002473 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002474 *
2475 * Create a virtual master control with the given name. The TLV data
2476 * must be either NULL or a valid data.
2477 *
2478 * @slaves is a NULL-terminated array of strings, each of which is a
2479 * slave control name. All controls with these names are assigned to
2480 * the new virtual master control.
2481 *
2482 * This function returns zero if successful or a negative error code.
2483 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002484int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002485 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002486 const char *suffix, bool init_slave_vol,
2487 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002488{
2489 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002490 int err;
2491
Takashi Iwai29e58532012-03-12 12:25:03 +01002492 if (ctl_ret)
2493 *ctl_ret = NULL;
2494
Takashi Iwai9322ca52012-02-03 14:28:01 +01002495 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002496 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002497 snd_printdd("No slave found for %s\n", name);
2498 return 0;
2499 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002500 kctl = snd_ctl_make_virtual_master(name, tlv);
2501 if (!kctl)
2502 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002503 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002504 if (err < 0)
2505 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002506
Takashi Iwai9322ca52012-02-03 14:28:01 +01002507 err = map_slaves(codec, slaves, suffix,
2508 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002509 if (err < 0)
2510 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002511
2512 /* init with master mute & zero volume */
2513 put_kctl_with_value(kctl, 0);
2514 if (init_slave_vol)
2515 map_slaves(codec, slaves, suffix,
2516 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2517
Takashi Iwai29e58532012-03-12 12:25:03 +01002518 if (ctl_ret)
2519 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002520 return 0;
2521}
Takashi Iwai18478e82012-03-09 17:51:10 +01002522EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002523
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002524/*
2525 * mute-LED control using vmaster
2526 */
2527static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_info *uinfo)
2529{
2530 static const char * const texts[] = {
2531 "Off", "On", "Follow Master"
2532 };
2533 unsigned int index;
2534
2535 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2536 uinfo->count = 1;
2537 uinfo->value.enumerated.items = 3;
2538 index = uinfo->value.enumerated.item;
2539 if (index >= 3)
2540 index = 2;
2541 strcpy(uinfo->value.enumerated.name, texts[index]);
2542 return 0;
2543}
2544
2545static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
2548 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2549 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2550 return 0;
2551}
2552
2553static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_value *ucontrol)
2555{
2556 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2557 unsigned int old_mode = hook->mute_mode;
2558
2559 hook->mute_mode = ucontrol->value.enumerated.item[0];
2560 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2561 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2562 if (old_mode == hook->mute_mode)
2563 return 0;
2564 snd_hda_sync_vmaster_hook(hook);
2565 return 1;
2566}
2567
2568static struct snd_kcontrol_new vmaster_mute_mode = {
2569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2570 .name = "Mute-LED Mode",
2571 .info = vmaster_mute_mode_info,
2572 .get = vmaster_mute_mode_get,
2573 .put = vmaster_mute_mode_put,
2574};
2575
2576/*
2577 * Add a mute-LED hook with the given vmaster switch kctl
2578 * "Mute-LED Mode" control is automatically created and associated with
2579 * the given hook.
2580 */
2581int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002582 struct hda_vmaster_mute_hook *hook,
2583 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002584{
2585 struct snd_kcontrol *kctl;
2586
2587 if (!hook->hook || !hook->sw_kctl)
2588 return 0;
2589 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2590 hook->codec = codec;
2591 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002592 if (!expose_enum_ctl)
2593 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002594 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2595 if (!kctl)
2596 return -ENOMEM;
2597 return snd_hda_ctl_add(codec, 0, kctl);
2598}
2599EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2600
2601/*
2602 * Call the hook with the current value for synchronization
2603 * Should be called in init callback
2604 */
2605void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2606{
2607 if (!hook->hook || !hook->codec)
2608 return;
2609 switch (hook->mute_mode) {
2610 case HDA_VMUTE_FOLLOW_MASTER:
2611 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2612 break;
2613 default:
2614 hook->hook(hook->codec, hook->mute_mode);
2615 break;
2616 }
2617}
2618EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2619
2620
Takashi Iwaid5191e52009-11-16 14:58:17 +01002621/**
2622 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2623 *
2624 * The control element is supposed to have the private_value field
2625 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2626 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002627int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2628 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 int chs = get_amp_channels(kcontrol);
2631
2632 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2633 uinfo->count = chs == 3 ? 2 : 1;
2634 uinfo->value.integer.min = 0;
2635 uinfo->value.integer.max = 1;
2636 return 0;
2637}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002638EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Takashi Iwaid5191e52009-11-16 14:58:17 +01002640/**
2641 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2642 *
2643 * The control element is supposed to have the private_value field
2644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2650 hda_nid_t nid = get_amp_nid(kcontrol);
2651 int chs = get_amp_channels(kcontrol);
2652 int dir = get_amp_direction(kcontrol);
2653 int idx = get_amp_index(kcontrol);
2654 long *valp = ucontrol->value.integer.value;
2655
2656 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002657 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002658 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002660 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002661 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
2663}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002664EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Takashi Iwaid5191e52009-11-16 14:58:17 +01002666/**
2667 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2668 *
2669 * The control element is supposed to have the private_value field
2670 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2671 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 hda_nid_t nid = get_amp_nid(kcontrol);
2677 int chs = get_amp_channels(kcontrol);
2678 int dir = get_amp_direction(kcontrol);
2679 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 long *valp = ucontrol->value.integer.value;
2681 int change = 0;
2682
Takashi Iwaicb53c622007-08-10 17:21:45 +02002683 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002684 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002685 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002686 HDA_AMP_MUTE,
2687 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002688 valp++;
2689 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002690 if (chs & 2)
2691 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002692 HDA_AMP_MUTE,
2693 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002694 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002695 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return change;
2697}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002698EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700/*
Takashi Iwai985be542005-11-02 18:26:49 +01002701 * bound volume controls
2702 *
2703 * bind multiple volumes (# indices, from 0)
2704 */
2705
2706#define AMP_VAL_IDX_SHIFT 19
2707#define AMP_VAL_IDX_MASK (0x0f<<19)
2708
Takashi Iwaid5191e52009-11-16 14:58:17 +01002709/**
2710 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2711 *
2712 * The control element is supposed to have the private_value field
2713 * set up via HDA_BIND_MUTE*() macros.
2714 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2716 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002717{
2718 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2719 unsigned long pval;
2720 int err;
2721
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002722 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002723 pval = kcontrol->private_value;
2724 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2725 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2726 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002727 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002728 return err;
2729}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002730EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002731
Takashi Iwaid5191e52009-11-16 14:58:17 +01002732/**
2733 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2734 *
2735 * The control element is supposed to have the private_value field
2736 * set up via HDA_BIND_MUTE*() macros.
2737 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002740{
2741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2742 unsigned long pval;
2743 int i, indices, err = 0, change = 0;
2744
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002745 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002746 pval = kcontrol->private_value;
2747 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2748 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2750 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002751 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2752 if (err < 0)
2753 break;
2754 change |= err;
2755 }
2756 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002757 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002758 return err < 0 ? err : change;
2759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002760EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002761
Takashi Iwaid5191e52009-11-16 14:58:17 +01002762/**
2763 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2764 *
2765 * The control element is supposed to have the private_value field
2766 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002767 */
2768int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_info *uinfo)
2770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 struct hda_bind_ctls *c;
2773 int err;
2774
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002776 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 kcontrol->private_value = *c->values;
2778 err = c->ops->info(kcontrol, uinfo);
2779 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002780 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002781 return err;
2782}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002783EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002784
Takashi Iwaid5191e52009-11-16 14:58:17 +01002785/**
2786 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2787 *
2788 * The control element is supposed to have the private_value field
2789 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2790 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002791int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2792 struct snd_ctl_elem_value *ucontrol)
2793{
2794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2795 struct hda_bind_ctls *c;
2796 int err;
2797
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002798 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002799 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002800 kcontrol->private_value = *c->values;
2801 err = c->ops->get(kcontrol, ucontrol);
2802 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002804 return err;
2805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002806EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002807
Takashi Iwaid5191e52009-11-16 14:58:17 +01002808/**
2809 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2810 *
2811 * The control element is supposed to have the private_value field
2812 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2813 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002814int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2815 struct snd_ctl_elem_value *ucontrol)
2816{
2817 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2818 struct hda_bind_ctls *c;
2819 unsigned long *vals;
2820 int err = 0, change = 0;
2821
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002822 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002823 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002824 for (vals = c->values; *vals; vals++) {
2825 kcontrol->private_value = *vals;
2826 err = c->ops->put(kcontrol, ucontrol);
2827 if (err < 0)
2828 break;
2829 change |= err;
2830 }
2831 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002832 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002833 return err < 0 ? err : change;
2834}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002835EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002836
Takashi Iwaid5191e52009-11-16 14:58:17 +01002837/**
2838 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2839 *
2840 * The control element is supposed to have the private_value field
2841 * set up via HDA_BIND_VOL() macro.
2842 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002843int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2844 unsigned int size, unsigned int __user *tlv)
2845{
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 struct hda_bind_ctls *c;
2848 int err;
2849
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002850 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002851 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002852 kcontrol->private_value = *c->values;
2853 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2854 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002855 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002856 return err;
2857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002858EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002859
2860struct hda_ctl_ops snd_hda_bind_vol = {
2861 .info = snd_hda_mixer_amp_volume_info,
2862 .get = snd_hda_mixer_amp_volume_get,
2863 .put = snd_hda_mixer_amp_volume_put,
2864 .tlv = snd_hda_mixer_amp_tlv
2865};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002866EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002867
2868struct hda_ctl_ops snd_hda_bind_sw = {
2869 .info = snd_hda_mixer_amp_switch_info,
2870 .get = snd_hda_mixer_amp_switch_get,
2871 .put = snd_hda_mixer_amp_switch_put,
2872 .tlv = snd_hda_mixer_amp_tlv
2873};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002874EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002875
2876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 * SPDIF out controls
2878 */
2879
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2884 uinfo->count = 1;
2885 return 0;
2886}
2887
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2892 IEC958_AES0_NONAUDIO |
2893 IEC958_AES0_CON_EMPHASIS_5015 |
2894 IEC958_AES0_CON_NOT_COPYRIGHT;
2895 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2896 IEC958_AES1_CON_ORIGINAL;
2897 return 0;
2898}
2899
Takashi Iwai0ba21762007-04-16 11:29:14 +02002900static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2904 IEC958_AES0_NONAUDIO |
2905 IEC958_AES0_PRO_EMPHASIS_5015;
2906 return 0;
2907}
2908
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002913 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002914 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002916 mutex_lock(&codec->spdif_mutex);
2917 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002918 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2919 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2920 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2921 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002922 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 return 0;
2925}
2926
2927/* convert from SPDIF status bits to HDA SPDIF bits
2928 * bit 0 (DigEn) is always set zero (to be filled later)
2929 */
2930static unsigned short convert_from_spdif_status(unsigned int sbits)
2931{
2932 unsigned short val = 0;
2933
2934 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002937 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2940 IEC958_AES0_PRO_EMPHASIS_5015)
2941 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2944 IEC958_AES0_CON_EMPHASIS_5015)
2945 val |= AC_DIG1_EMPHASIS;
2946 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2947 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002949 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2951 }
2952 return val;
2953}
2954
2955/* convert to SPDIF status bits from HDA SPDIF bits
2956 */
2957static unsigned int convert_to_spdif_status(unsigned short val)
2958{
2959 unsigned int sbits = 0;
2960
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002963 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 sbits |= IEC958_AES0_PROFESSIONAL;
2965 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002966 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2968 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002969 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002973 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2975 sbits |= val & (0x7f << 8);
2976 }
2977 return sbits;
2978}
2979
Takashi Iwai2f728532008-09-25 16:32:41 +02002980/* set digital convert verbs both for the given NID and its slaves */
2981static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2982 int verb, int val)
2983{
Takashi Iwaidda14412011-05-02 11:29:30 +02002984 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002985
Takashi Iwai9e976972008-11-25 08:17:20 +01002986 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002987 d = codec->slave_dig_outs;
2988 if (!d)
2989 return;
2990 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002991 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002992}
2993
2994static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2995 int dig1, int dig2)
2996{
2997 if (dig1 != -1)
2998 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2999 if (dig2 != -1)
3000 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3001}
3002
Takashi Iwai0ba21762007-04-16 11:29:14 +02003003static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003007 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003008 struct hda_spdif_out *spdif;
3009 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 unsigned short val;
3011 int change;
3012
Ingo Molnar62932df2006-01-16 16:34:20 +01003013 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003014 spdif = snd_array_elem(&codec->spdif_out, idx);
3015 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003016 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3018 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3019 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003020 val = convert_from_spdif_status(spdif->status);
3021 val |= spdif->ctls & 1;
3022 change = spdif->ctls != val;
3023 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003024 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003025 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003026 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return change;
3028}
3029
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003030#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003036 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003037 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003039 mutex_lock(&codec->spdif_mutex);
3040 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003041 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003042 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
3044}
3045
Stephen Warren74b654c2011-06-01 11:14:18 -06003046static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3047 int dig1, int dig2)
3048{
3049 set_dig_out_convert(codec, nid, dig1, dig2);
3050 /* unmute amp switch (if any) */
3051 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3052 (dig1 & AC_DIG1_ENABLE))
3053 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3054 HDA_AMP_MUTE, 0);
3055}
3056
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003061 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003062 struct hda_spdif_out *spdif;
3063 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 unsigned short val;
3065 int change;
3066
Ingo Molnar62932df2006-01-16 16:34:20 +01003067 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003068 spdif = snd_array_elem(&codec->spdif_out, idx);
3069 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003070 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003073 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003074 spdif->ctls = val;
3075 if (change && nid != (u16)-1)
3076 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003077 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return change;
3079}
3080
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003081static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 {
3083 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3084 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003085 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 .info = snd_hda_spdif_mask_info,
3087 .get = snd_hda_spdif_cmask_get,
3088 },
3089 {
3090 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003092 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 .info = snd_hda_spdif_mask_info,
3094 .get = snd_hda_spdif_pmask_get,
3095 },
3096 {
3097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003098 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 .info = snd_hda_spdif_mask_info,
3100 .get = snd_hda_spdif_default_get,
3101 .put = snd_hda_spdif_default_put,
3102 },
3103 {
3104 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003105 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 .info = snd_hda_spdif_out_switch_info,
3107 .get = snd_hda_spdif_out_switch_get,
3108 .put = snd_hda_spdif_out_switch_put,
3109 },
3110 { } /* end */
3111};
3112
3113/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003114 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003116 * @associated_nid: NID that new ctls associated with
3117 * @cvt_nid: converter NID
3118 * @type: HDA_PCM_TYPE_*
3119 * Creates controls related with the digital output.
3120 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 *
3122 * Returns 0 if successful, or a negative error code.
3123 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003124int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3125 hda_nid_t associated_nid,
3126 hda_nid_t cvt_nid,
3127 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003130 struct snd_kcontrol *kctl;
3131 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003132 int idx, dev = 0;
3133 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003134 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Takashi Iwaidcda5802012-10-12 17:24:51 +02003136 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3137 type == HDA_PCM_TYPE_SPDIF) {
3138 dev = spdif_pcm_dev;
3139 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3140 type == HDA_PCM_TYPE_HDMI) {
3141 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3142 spdif = snd_array_elem(&codec->spdif_out, idx);
3143 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3144 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3145 if (!kctl)
3146 break;
3147 kctl->id.device = spdif_pcm_dev;
3148 }
3149 }
3150 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3151 }
3152 if (!codec->primary_dig_out_type)
3153 codec->primary_dig_out_type = type;
3154
3155 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003156 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003157 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3158 return -EBUSY;
3159 }
Stephen Warren7c935972011-06-01 11:14:17 -06003160 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3162 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003163 if (!kctl)
3164 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003165 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003166 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003167 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003168 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003169 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return err;
3171 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003172 spdif->nid = cvt_nid;
3173 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003174 AC_VERB_GET_DIGI_CONVERT_1, 0);
3175 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return 0;
3177}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003178EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003180/* get the hda_spdif_out entry from the given NID
3181 * call within spdif_mutex lock
3182 */
Stephen Warren7c935972011-06-01 11:14:17 -06003183struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3184 hda_nid_t nid)
3185{
3186 int i;
3187 for (i = 0; i < codec->spdif_out.used; i++) {
3188 struct hda_spdif_out *spdif =
3189 snd_array_elem(&codec->spdif_out, i);
3190 if (spdif->nid == nid)
3191 return spdif;
3192 }
3193 return NULL;
3194}
3195EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3196
Stephen Warren74b654c2011-06-01 11:14:18 -06003197void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3198{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003199 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003200
3201 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003202 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003203 spdif->nid = (u16)-1;
3204 mutex_unlock(&codec->spdif_mutex);
3205}
3206EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3207
3208void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3209{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003210 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003211 unsigned short val;
3212
3213 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003214 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003215 if (spdif->nid != nid) {
3216 spdif->nid = nid;
3217 val = spdif->ctls;
3218 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3219 }
3220 mutex_unlock(&codec->spdif_mutex);
3221}
3222EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003225 * SPDIF sharing with analog output
3226 */
3227static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
3230 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3231 ucontrol->value.integer.value[0] = mout->share_spdif;
3232 return 0;
3233}
3234
3235static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
3237{
3238 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3239 mout->share_spdif = !!ucontrol->value.integer.value[0];
3240 return 0;
3241}
3242
3243static struct snd_kcontrol_new spdif_share_sw = {
3244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3245 .name = "IEC958 Default PCM Playback Switch",
3246 .info = snd_ctl_boolean_mono_info,
3247 .get = spdif_share_sw_get,
3248 .put = spdif_share_sw_put,
3249};
3250
Takashi Iwaid5191e52009-11-16 14:58:17 +01003251/**
3252 * snd_hda_create_spdif_share_sw - create Default PCM switch
3253 * @codec: the HDA codec
3254 * @mout: multi-out instance
3255 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003256int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3257 struct hda_multi_out *mout)
3258{
3259 if (!mout->dig_out_nid)
3260 return 0;
3261 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003262 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3263 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003264}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003265EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003266
3267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 * SPDIF input
3269 */
3270
3271#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3272
Takashi Iwai0ba21762007-04-16 11:29:14 +02003273static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3277
3278 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3279 return 0;
3280}
3281
Takashi Iwai0ba21762007-04-16 11:29:14 +02003282static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3286 hda_nid_t nid = kcontrol->private_value;
3287 unsigned int val = !!ucontrol->value.integer.value[0];
3288 int change;
3289
Ingo Molnar62932df2006-01-16 16:34:20 +01003290 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003292 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003294 snd_hda_codec_write_cache(codec, nid, 0,
3295 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003297 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return change;
3299}
3300
Takashi Iwai0ba21762007-04-16 11:29:14 +02003301static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
3304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3305 hda_nid_t nid = kcontrol->private_value;
3306 unsigned short val;
3307 unsigned int sbits;
3308
Andrew Paprocki3982d172007-12-19 12:13:44 +01003309 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 sbits = convert_to_spdif_status(val);
3311 ucontrol->value.iec958.status[0] = sbits;
3312 ucontrol->value.iec958.status[1] = sbits >> 8;
3313 ucontrol->value.iec958.status[2] = sbits >> 16;
3314 ucontrol->value.iec958.status[3] = sbits >> 24;
3315 return 0;
3316}
3317
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003318static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 {
3320 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003321 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 .info = snd_hda_spdif_in_switch_info,
3323 .get = snd_hda_spdif_in_switch_get,
3324 .put = snd_hda_spdif_in_switch_put,
3325 },
3326 {
3327 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3328 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003329 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 .info = snd_hda_spdif_mask_info,
3331 .get = snd_hda_spdif_in_status_get,
3332 },
3333 { } /* end */
3334};
3335
3336/**
3337 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3338 * @codec: the HDA codec
3339 * @nid: audio in widget NID
3340 *
3341 * Creates controls related with the SPDIF input.
3342 * Called from each patch supporting the SPDIF in.
3343 *
3344 * Returns 0 if successful, or a negative error code.
3345 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003346int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
3348 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003349 struct snd_kcontrol *kctl;
3350 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003351 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Takashi Iwaidcda5802012-10-12 17:24:51 +02003353 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003354 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003355 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3356 return -EBUSY;
3357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3359 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003360 if (!kctl)
3361 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003363 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003364 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return err;
3366 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003367 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003368 snd_hda_codec_read(codec, nid, 0,
3369 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003370 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return 0;
3372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003373EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Takashi Iwai2a439522011-07-26 09:52:50 +02003375#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003376/*
3377 * command cache
3378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003380/* build a 32bit cache key with the widget id and the command parameter */
3381#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3382#define get_cmd_cache_nid(key) ((key) & 0xff)
3383#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3384
3385/**
3386 * snd_hda_codec_write_cache - send a single command with caching
3387 * @codec: the HDA codec
3388 * @nid: NID to send the command
3389 * @direct: direct flag
3390 * @verb: the verb to send
3391 * @parm: the parameter for the verb
3392 *
3393 * Send a single command without waiting for response.
3394 *
3395 * Returns 0 if successful, or a negative error code.
3396 */
3397int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3398 int direct, unsigned int verb, unsigned int parm)
3399{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003400 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3401 struct hda_cache_head *c;
3402 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003403
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003404 if (err < 0)
3405 return err;
3406 /* parm may contain the verb stuff for get/set amp */
3407 verb = verb | (parm >> 8);
3408 parm &= 0xff;
3409 key = build_cmd_cache_key(nid, verb);
3410 mutex_lock(&codec->bus->cmd_mutex);
3411 c = get_alloc_hash(&codec->cmd_cache, key);
3412 if (c)
3413 c->val = parm;
3414 mutex_unlock(&codec->bus->cmd_mutex);
3415 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003416}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003417EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003418
Takashi Iwaid5191e52009-11-16 14:58:17 +01003419/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003420 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3421 * @codec: the HDA codec
3422 * @nid: NID to send the command
3423 * @direct: direct flag
3424 * @verb: the verb to send
3425 * @parm: the parameter for the verb
3426 *
3427 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3428 * command if the parameter is already identical with the cached value.
3429 * If not, it sends the command and refreshes the cache.
3430 *
3431 * Returns 0 if successful, or a negative error code.
3432 */
3433int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3434 int direct, unsigned int verb, unsigned int parm)
3435{
3436 struct hda_cache_head *c;
3437 u32 key;
3438
3439 /* parm may contain the verb stuff for get/set amp */
3440 verb = verb | (parm >> 8);
3441 parm &= 0xff;
3442 key = build_cmd_cache_key(nid, verb);
3443 mutex_lock(&codec->bus->cmd_mutex);
3444 c = get_hash(&codec->cmd_cache, key);
3445 if (c && c->val == parm) {
3446 mutex_unlock(&codec->bus->cmd_mutex);
3447 return 0;
3448 }
3449 mutex_unlock(&codec->bus->cmd_mutex);
3450 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3451}
3452EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3453
3454/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003455 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3456 * @codec: HD-audio codec
3457 *
3458 * Execute all verbs recorded in the command caches to resume.
3459 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003460void snd_hda_codec_resume_cache(struct hda_codec *codec)
3461{
Takashi Iwai603c4012008-07-30 15:01:44 +02003462 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003463 int i;
3464
Takashi Iwai603c4012008-07-30 15:01:44 +02003465 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003466 u32 key = buffer->key;
3467 if (!key)
3468 continue;
3469 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3470 get_cmd_cache_cmd(key), buffer->val);
3471 }
3472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003473EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003474
3475/**
3476 * snd_hda_sequence_write_cache - sequence writes with caching
3477 * @codec: the HDA codec
3478 * @seq: VERB array to send
3479 *
3480 * Send the commands sequentially from the given array.
3481 * Thte commands are recorded on cache for power-save and resume.
3482 * The array must be terminated with NID=0.
3483 */
3484void snd_hda_sequence_write_cache(struct hda_codec *codec,
3485 const struct hda_verb *seq)
3486{
3487 for (; seq->nid; seq++)
3488 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3489 seq->param);
3490}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003491EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003492#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003493
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003494void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3495 unsigned int power_state,
3496 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003497{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003498 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003499 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003500
Takashi Iwaicb53c622007-08-10 17:21:45 +02003501 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003502 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003503 if (!(wcaps & AC_WCAP_POWER))
3504 continue;
3505 /* don't power down the widget if it controls eapd and
3506 * EAPD_BTLENABLE is set.
3507 */
3508 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3509 get_wcaps_type(wcaps) == AC_WID_PIN &&
3510 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3511 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003512 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003513 if (eapd & 0x02)
3514 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003515 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003516 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3517 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003518 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003519}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003520EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3521
3522/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003523 * supported power states check
3524 */
3525static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3526 unsigned int power_state)
3527{
3528 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3529
Mengdong Line037cb42012-08-10 14:11:58 +02003530 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003531 return false;
3532 if (sup & power_state)
3533 return true;
3534 else
3535 return false;
3536}
3537
3538/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003539 * wait until the state is reached, returns the current state
3540 */
3541static unsigned int hda_sync_power_state(struct hda_codec *codec,
3542 hda_nid_t fg,
3543 unsigned int power_state)
3544{
3545 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3546 unsigned int state, actual_state;
3547
3548 for (;;) {
3549 state = snd_hda_codec_read(codec, fg, 0,
3550 AC_VERB_GET_POWER_STATE, 0);
3551 if (state & AC_PWRST_ERROR)
3552 break;
3553 actual_state = (state >> 4) & 0x0f;
3554 if (actual_state == power_state)
3555 break;
3556 if (time_after_eq(jiffies, end_time))
3557 break;
3558 /* wait until the codec reachs to the target state */
3559 msleep(1);
3560 }
3561 return state;
3562}
3563
3564/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003565 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003566 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003567static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003568 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003569{
Takashi Iwaid8193872012-08-31 07:54:38 -07003570 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003571 int count;
3572 unsigned int state;
3573
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003574 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003575 if (power_state == AC_PWRST_D3) {
3576 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003577 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003578 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003579
3580 /* repeat power states setting at most 10 times*/
3581 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003582 if (codec->patch_ops.set_power_state)
3583 codec->patch_ops.set_power_state(codec, fg,
3584 power_state);
3585 else {
3586 snd_hda_codec_read(codec, fg, 0,
3587 AC_VERB_SET_POWER_STATE,
3588 power_state);
3589 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3590 true);
3591 }
3592 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003593 if (!(state & AC_PWRST_ERROR))
3594 break;
3595 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003596
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003597 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003598}
Takashi Iwai54d17402005-11-21 16:33:22 +01003599
Takashi Iwai11aeff02008-07-30 15:01:46 +02003600#ifdef CONFIG_SND_HDA_HWDEP
3601/* execute additional init verbs */
3602static void hda_exec_init_verbs(struct hda_codec *codec)
3603{
3604 if (codec->init_verbs.list)
3605 snd_hda_sequence_write(codec, codec->init_verbs.list);
3606}
3607#else
3608static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3609#endif
3610
Takashi Iwai2a439522011-07-26 09:52:50 +02003611#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003612/*
3613 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003614 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003615 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003616static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003617{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003618 unsigned int state;
3619
Takashi Iwai989c3182012-11-19 14:14:58 +01003620 codec->in_pm = 1;
3621
Takashi Iwaicb53c622007-08-10 17:21:45 +02003622 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003623 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003624 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003625 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003626 /* Cancel delayed work if we aren't currently running from it. */
3627 if (!in_wq)
3628 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003629 spin_lock(&codec->power_lock);
3630 snd_hda_update_power_acct(codec);
3631 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003632 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003633 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003634 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003635 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003636 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003637 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003638}
3639
3640/*
3641 * kick up codec; used both from PM and power-save
3642 */
3643static void hda_call_codec_resume(struct hda_codec *codec)
3644{
Takashi Iwai989c3182012-11-19 14:14:58 +01003645 codec->in_pm = 1;
3646
Takashi Iwai7f308302012-05-08 16:52:23 +02003647 /* set as if powered on for avoiding re-entering the resume
3648 * in the resume / power-save sequence
3649 */
3650 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003651 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003652 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003653 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003654 if (codec->patch_ops.resume)
3655 codec->patch_ops.resume(codec);
3656 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003657 if (codec->patch_ops.init)
3658 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003659 snd_hda_codec_resume_amp(codec);
3660 snd_hda_codec_resume_cache(codec);
3661 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003662
3663 if (codec->jackpoll_interval)
3664 hda_jackpoll_work(&codec->jackpoll_work.work);
3665 else {
3666 snd_hda_jack_set_dirty_all(codec);
3667 snd_hda_jack_report_sync(codec);
3668 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003669
3670 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003671 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003672}
Takashi Iwai2a439522011-07-26 09:52:50 +02003673#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003674
Takashi Iwai54d17402005-11-21 16:33:22 +01003675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676/**
3677 * snd_hda_build_controls - build mixer controls
3678 * @bus: the BUS
3679 *
3680 * Creates mixer controls for each codec included in the bus.
3681 *
3682 * Returns 0 if successful, otherwise a negative error code.
3683 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003684int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003686 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Takashi Iwai0ba21762007-04-16 11:29:14 +02003688 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003689 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003690 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003691 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003692 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003693 err = snd_hda_codec_reset(codec);
3694 if (err < 0) {
3695 printk(KERN_ERR
3696 "hda_codec: cannot revert codec\n");
3697 return err;
3698 }
3699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003701 return 0;
3702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003703EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003705/*
3706 * add standard channel maps if not specified
3707 */
3708static int add_std_chmaps(struct hda_codec *codec)
3709{
3710 int i, str, err;
3711
3712 for (i = 0; i < codec->num_pcms; i++) {
3713 for (str = 0; str < 2; str++) {
3714 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3715 struct hda_pcm_stream *hinfo =
3716 &codec->pcm_info[i].stream[str];
3717 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003718 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003719
3720 if (codec->pcm_info[i].own_chmap)
3721 continue;
3722 if (!pcm || !hinfo->substreams)
3723 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003724 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3725 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003726 hinfo->channels_max,
3727 0, &chmap);
3728 if (err < 0)
3729 return err;
3730 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3731 }
3732 }
3733 return 0;
3734}
3735
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003736/* default channel maps for 2.1 speakers;
3737 * since HD-audio supports only stereo, odd number channels are omitted
3738 */
3739const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3740 { .channels = 2,
3741 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3742 { .channels = 4,
3743 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3744 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3745 { }
3746};
3747EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3748
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003749int snd_hda_codec_build_controls(struct hda_codec *codec)
3750{
3751 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003752 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003753 /* continue to initialize... */
3754 if (codec->patch_ops.init)
3755 err = codec->patch_ops.init(codec);
3756 if (!err && codec->patch_ops.build_controls)
3757 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003758 if (err < 0)
3759 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003760
3761 /* we create chmaps here instead of build_pcms */
3762 err = add_std_chmaps(codec);
3763 if (err < 0)
3764 return err;
3765
David Henningsson26a6cb62012-10-09 15:04:21 +02003766 if (codec->jackpoll_interval)
3767 hda_jackpoll_work(&codec->jackpoll_work.work);
3768 else
3769 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return 0;
3771}
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773/*
3774 * stream formats
3775 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003776struct hda_rate_tbl {
3777 unsigned int hz;
3778 unsigned int alsa_bits;
3779 unsigned int hda_fmt;
3780};
3781
Takashi Iwai92f10b32010-08-03 14:21:00 +02003782/* rate = base * mult / div */
3783#define HDA_RATE(base, mult, div) \
3784 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3785 (((div) - 1) << AC_FMT_DIV_SHIFT))
3786
Takashi Iwaibefdf312005-08-22 13:57:55 +02003787static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003789
3790 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003791 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3792 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3793 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3794 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3795 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3796 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3797 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3798 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3799 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3800 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3801 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003802#define AC_PAR_PCM_RATE_BITS 11
3803 /* up to bits 10, 384kHZ isn't supported properly */
3804
3805 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003806 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003807
Takashi Iwaibefdf312005-08-22 13:57:55 +02003808 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809};
3810
3811/**
3812 * snd_hda_calc_stream_format - calculate format bitset
3813 * @rate: the sample rate
3814 * @channels: the number of channels
3815 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3816 * @maxbps: the max. bps
3817 *
3818 * Calculate the format bitset from the given rate, channels and th PCM format.
3819 *
3820 * Return zero if invalid.
3821 */
3822unsigned int snd_hda_calc_stream_format(unsigned int rate,
3823 unsigned int channels,
3824 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003825 unsigned int maxbps,
3826 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 int i;
3829 unsigned int val = 0;
3830
Takashi Iwaibefdf312005-08-22 13:57:55 +02003831 for (i = 0; rate_bits[i].hz; i++)
3832 if (rate_bits[i].hz == rate) {
3833 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 break;
3835 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003836 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 snd_printdd("invalid rate %d\n", rate);
3838 return 0;
3839 }
3840
3841 if (channels == 0 || channels > 8) {
3842 snd_printdd("invalid channels %d\n", channels);
3843 return 0;
3844 }
3845 val |= channels - 1;
3846
3847 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003848 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003849 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003850 break;
3851 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003852 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 case 20:
3855 case 24:
3856 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003857 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003858 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003860 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003862 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 break;
3864 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003865 snd_printdd("invalid format width %d\n",
3866 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return 0;
3868 }
3869
Anssi Hannula32c168c2010-08-03 13:28:57 +03003870 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003871 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return val;
3874}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003875EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003877static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3878 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003879{
3880 unsigned int val = 0;
3881 if (nid != codec->afg &&
3882 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3883 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3884 if (!val || val == -1)
3885 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3886 if (!val || val == -1)
3887 return 0;
3888 return val;
3889}
3890
3891static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3892{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003893 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003894 get_pcm_param);
3895}
3896
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003897static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3898 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003899{
3900 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3901 if (!streams || streams == -1)
3902 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3903 if (!streams || streams == -1)
3904 return 0;
3905 return streams;
3906}
3907
3908static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3909{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003910 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003911 get_stream_param);
3912}
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914/**
3915 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3916 * @codec: the HDA codec
3917 * @nid: NID to query
3918 * @ratesp: the pointer to store the detected rate bitflags
3919 * @formatsp: the pointer to store the detected formats
3920 * @bpsp: the pointer to store the detected format widths
3921 *
3922 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3923 * or @bsps argument is ignored.
3924 *
3925 * Returns 0 if successful, otherwise a negative error code.
3926 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003927int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3929{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003930 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003932 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003933 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 if (ratesp) {
3936 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003937 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003939 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003941 if (rates == 0) {
3942 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3943 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3944 nid, val,
3945 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3946 return -EIO;
3947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 *ratesp = rates;
3949 }
3950
3951 if (formatsp || bpsp) {
3952 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003953 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003955 streams = query_stream_param(codec, nid);
3956 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 bps = 0;
3960 if (streams & AC_SUPFMT_PCM) {
3961 if (val & AC_SUPPCM_BITS_8) {
3962 formats |= SNDRV_PCM_FMTBIT_U8;
3963 bps = 8;
3964 }
3965 if (val & AC_SUPPCM_BITS_16) {
3966 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3967 bps = 16;
3968 }
3969 if (wcaps & AC_WCAP_DIGITAL) {
3970 if (val & AC_SUPPCM_BITS_32)
3971 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3972 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3973 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3974 if (val & AC_SUPPCM_BITS_24)
3975 bps = 24;
3976 else if (val & AC_SUPPCM_BITS_20)
3977 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003978 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3979 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3981 if (val & AC_SUPPCM_BITS_32)
3982 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 else if (val & AC_SUPPCM_BITS_24)
3984 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003985 else if (val & AC_SUPPCM_BITS_20)
3986 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
3988 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003989#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003990 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003992 if (!bps)
3993 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003994 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003995#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003996 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003997 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 /* temporary hack: we have still no proper support
3999 * for the direct AC3 stream...
4000 */
4001 formats |= SNDRV_PCM_FMTBIT_U8;
4002 bps = 8;
4003 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004004 if (formats == 0) {
4005 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4006 "(nid=0x%x, val=0x%x, ovrd=%i, "
4007 "streams=0x%x)\n",
4008 nid, val,
4009 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4010 streams);
4011 return -EIO;
4012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (formatsp)
4014 *formatsp = formats;
4015 if (bpsp)
4016 *bpsp = bps;
4017 }
4018
4019 return 0;
4020}
Stephen Warren384a48d2011-06-01 11:14:21 -06004021EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004024 * snd_hda_is_supported_format - Check the validity of the format
4025 * @codec: HD-audio codec
4026 * @nid: NID to check
4027 * @format: the HD-audio format value to check
4028 *
4029 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 *
4031 * Returns 1 if supported, 0 if not.
4032 */
4033int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4034 unsigned int format)
4035{
4036 int i;
4037 unsigned int val = 0, rate, stream;
4038
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004039 val = query_pcm_param(codec, nid);
4040 if (!val)
4041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004044 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004045 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (val & (1 << i))
4047 break;
4048 return 0;
4049 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004050 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return 0;
4052
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004053 stream = query_stream_param(codec, nid);
4054 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return 0;
4056
4057 if (stream & AC_SUPFMT_PCM) {
4058 switch (format & 0xf0) {
4059 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004060 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return 0;
4062 break;
4063 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004064 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return 0;
4066 break;
4067 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004068 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return 0;
4070 break;
4071 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004072 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return 0;
4074 break;
4075 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004076 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return 0;
4078 break;
4079 default:
4080 return 0;
4081 }
4082 } else {
4083 /* FIXME: check for float32 and AC3? */
4084 }
4085
4086 return 1;
4087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004088EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090/*
4091 * PCM stuff
4092 */
4093static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4094 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004095 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 return 0;
4098}
4099
4100static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4101 struct hda_codec *codec,
4102 unsigned int stream_tag,
4103 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004104 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4107 return 0;
4108}
4109
4110static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4111 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004112 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Takashi Iwai888afa12008-03-18 09:57:50 +01004114 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 return 0;
4116}
4117
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004118static int set_pcm_default_values(struct hda_codec *codec,
4119 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004121 int err;
4122
Takashi Iwai0ba21762007-04-16 11:29:14 +02004123 /* query support PCM information from the given NID */
4124 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004125 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004126 info->rates ? NULL : &info->rates,
4127 info->formats ? NULL : &info->formats,
4128 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004129 if (err < 0)
4130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132 if (info->ops.open == NULL)
4133 info->ops.open = hda_pcm_default_open_close;
4134 if (info->ops.close == NULL)
4135 info->ops.close = hda_pcm_default_open_close;
4136 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004137 if (snd_BUG_ON(!info->nid))
4138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 info->ops.prepare = hda_pcm_default_prepare;
4140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004142 if (snd_BUG_ON(!info->nid))
4143 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 info->ops.cleanup = hda_pcm_default_cleanup;
4145 }
4146 return 0;
4147}
4148
Takashi Iwaieb541332010-08-06 13:48:11 +02004149/*
4150 * codec prepare/cleanup entries
4151 */
4152int snd_hda_codec_prepare(struct hda_codec *codec,
4153 struct hda_pcm_stream *hinfo,
4154 unsigned int stream,
4155 unsigned int format,
4156 struct snd_pcm_substream *substream)
4157{
4158 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004159 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004160 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4161 if (ret >= 0)
4162 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004163 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004164 return ret;
4165}
4166EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4167
4168void snd_hda_codec_cleanup(struct hda_codec *codec,
4169 struct hda_pcm_stream *hinfo,
4170 struct snd_pcm_substream *substream)
4171{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004172 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004173 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004174 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004175}
4176EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4177
Takashi Iwaid5191e52009-11-16 14:58:17 +01004178/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004179const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4180 "Audio", "SPDIF", "HDMI", "Modem"
4181};
4182
Takashi Iwai176d5332008-07-30 15:01:44 +02004183/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004184 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004185 *
4186 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004187 */
4188static int get_empty_pcm_device(struct hda_bus *bus, int type)
4189{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004190 /* audio device indices; not linear to keep compatibility */
4191 static int audio_idx[HDA_PCM_NTYPES][5] = {
4192 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4193 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004194 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004195 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004196 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004197 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004198
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004199 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004200 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4201 return -EINVAL;
4202 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004203
4204 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4205 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4206 return audio_idx[type][i];
4207
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004208 /* non-fixed slots starting from 10 */
4209 for (i = 10; i < 32; i++) {
4210 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4211 return i;
4212 }
4213
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004214 snd_printk(KERN_WARNING "Too many %s devices\n",
4215 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004216 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004217}
4218
4219/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004220 * attach a new PCM stream
4221 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004222static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004223{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004224 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004225 struct hda_pcm_stream *info;
4226 int stream, err;
4227
Takashi Iwaib91f0802008-11-04 08:43:08 +01004228 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004229 return -EINVAL;
4230 for (stream = 0; stream < 2; stream++) {
4231 info = &pcm->stream[stream];
4232 if (info->substreams) {
4233 err = set_pcm_default_values(codec, info);
4234 if (err < 0)
4235 return err;
4236 }
4237 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004238 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004239}
4240
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004241/* assign all PCMs of the given codec */
4242int snd_hda_codec_build_pcms(struct hda_codec *codec)
4243{
4244 unsigned int pcm;
4245 int err;
4246
4247 if (!codec->num_pcms) {
4248 if (!codec->patch_ops.build_pcms)
4249 return 0;
4250 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004251 if (err < 0) {
4252 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004253 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004254 err = snd_hda_codec_reset(codec);
4255 if (err < 0) {
4256 printk(KERN_ERR
4257 "hda_codec: cannot revert codec\n");
4258 return err;
4259 }
4260 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004261 }
4262 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4263 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4264 int dev;
4265
4266 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004267 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004268
4269 if (!cpcm->pcm) {
4270 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4271 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004272 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004273 cpcm->device = dev;
4274 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004275 if (err < 0) {
4276 printk(KERN_ERR "hda_codec: cannot attach "
4277 "PCM stream %d for codec #%d\n",
4278 dev, codec->addr);
4279 continue; /* no fatal error */
4280 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004281 }
4282 }
4283 return 0;
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/**
4287 * snd_hda_build_pcms - build PCM information
4288 * @bus: the BUS
4289 *
4290 * Create PCM information for each codec included in the bus.
4291 *
4292 * The build_pcms codec patch is requested to set up codec->num_pcms and
4293 * codec->pcm_info properly. The array is referred by the top-level driver
4294 * to create its PCM instances.
4295 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4296 * callback.
4297 *
4298 * At least, substreams, channels_min and channels_max must be filled for
4299 * each stream. substreams = 0 indicates that the stream doesn't exist.
4300 * When rates and/or formats are zero, the supported values are queried
4301 * from the given nid. The nid is used also by the default ops.prepare
4302 * and ops.cleanup callbacks.
4303 *
4304 * The driver needs to call ops.open in its open callback. Similarly,
4305 * ops.close is supposed to be called in the close callback.
4306 * ops.prepare should be called in the prepare or hw_params callback
4307 * with the proper parameters for set up.
4308 * ops.cleanup should be called in hw_free for clean up of streams.
4309 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004310 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004312int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004314 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Takashi Iwai0ba21762007-04-16 11:29:14 +02004316 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004317 int err = snd_hda_codec_build_pcms(codec);
4318 if (err < 0)
4319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321 return 0;
4322}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004323EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325/**
4326 * snd_hda_check_board_config - compare the current codec with the config table
4327 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004328 * @num_configs: number of config enums
4329 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 * @tbl: configuration table, terminated by null entries
4331 *
4332 * Compares the modelname or PCI subsystem id of the current codec with the
4333 * given configuration table. If a matching entry is found, returns its
4334 * config value (supposed to be 0 or positive).
4335 *
4336 * If no entries are matching, the function returns a negative value.
4337 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004338int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004339 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004340 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004342 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004343 int i;
4344 for (i = 0; i < num_configs; i++) {
4345 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004346 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004347 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4348 "selected\n", models[i]);
4349 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 }
4351 }
4352 }
4353
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004354 if (!codec->bus->pci || !tbl)
4355 return -1;
4356
4357 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4358 if (!tbl)
4359 return -1;
4360 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004361#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004362 char tmp[10];
4363 const char *model = NULL;
4364 if (models)
4365 model = models[tbl->value];
4366 if (!model) {
4367 sprintf(tmp, "#%d", tbl->value);
4368 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004370 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4371 "for config %x:%x (%s)\n",
4372 model, tbl->subvendor, tbl->subdevice,
4373 (tbl->name ? tbl->name : "Unknown device"));
4374#endif
4375 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
4377 return -1;
4378}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004379EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
4381/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004382 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004383 subsystem ID with the
4384 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004385
4386 This is important for Gateway notebooks with SB450 HDA Audio
4387 where the vendor ID of the PCI device is:
4388 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4389 and the vendor/subvendor are found only at the codec.
4390
4391 * @codec: the HDA codec
4392 * @num_configs: number of config enums
4393 * @models: array of model name strings
4394 * @tbl: configuration table, terminated by null entries
4395 *
4396 * Compares the modelname or PCI subsystem id of the current codec with the
4397 * given configuration table. If a matching entry is found, returns its
4398 * config value (supposed to be 0 or positive).
4399 *
4400 * If no entries are matching, the function returns a negative value.
4401 */
4402int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004403 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004404 const struct snd_pci_quirk *tbl)
4405{
4406 const struct snd_pci_quirk *q;
4407
4408 /* Search for codec ID */
4409 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004410 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4411 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4412 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004413 break;
4414 }
4415
4416 if (!q->subvendor)
4417 return -1;
4418
4419 tbl = q;
4420
4421 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004422#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004423 char tmp[10];
4424 const char *model = NULL;
4425 if (models)
4426 model = models[tbl->value];
4427 if (!model) {
4428 sprintf(tmp, "#%d", tbl->value);
4429 model = tmp;
4430 }
4431 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4432 "for config %x:%x (%s)\n",
4433 model, tbl->subvendor, tbl->subdevice,
4434 (tbl->name ? tbl->name : "Unknown device"));
4435#endif
4436 return tbl->value;
4437 }
4438 return -1;
4439}
4440EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4441
4442/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 * snd_hda_add_new_ctls - create controls from the array
4444 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004445 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 *
4447 * This helper function creates and add new controls in the given array.
4448 * The array must be terminated with an empty entry as terminator.
4449 *
4450 * Returns 0 if successful, or a negative error code.
4451 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004452int snd_hda_add_new_ctls(struct hda_codec *codec,
4453 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004455 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004458 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004459 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004460 if (knew->iface == -1) /* skip this codec private value */
4461 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004462 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004463 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004464 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004465 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004466 if (addr > 0)
4467 kctl->id.device = addr;
4468 if (idx > 0)
4469 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004470 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004471 if (!err)
4472 break;
4473 /* try first with another device index corresponding to
4474 * the codec addr; if it still fails (or it's the
4475 * primary codec), then try another control index
4476 */
4477 if (!addr && codec->addr)
4478 addr = codec->addr;
4479 else if (!idx && !knew->index) {
4480 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004481 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004482 if (idx <= 0)
4483 return err;
4484 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004485 return err;
4486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
4488 return 0;
4489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004490EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Takashi Iwai83012a72012-08-24 18:38:08 +02004492#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004493static void hda_power_work(struct work_struct *work)
4494{
4495 struct hda_codec *codec =
4496 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004497 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004498 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004499
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004500 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004501 if (codec->power_transition > 0) { /* during power-up sequence? */
4502 spin_unlock(&codec->power_lock);
4503 return;
4504 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004505 if (!codec->power_on || codec->power_count) {
4506 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004507 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004508 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004509 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004510 spin_unlock(&codec->power_lock);
4511
Dylan Reidd17344b2012-09-28 15:57:01 -07004512 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004513 codec->pm_down_notified = 0;
4514 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4515 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004516 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004517 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004518}
4519
4520static void hda_keep_power_on(struct hda_codec *codec)
4521{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004522 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004523 codec->power_count++;
4524 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004525 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004526 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004527}
4528
Takashi Iwaid5191e52009-11-16 14:58:17 +01004529/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004530void snd_hda_update_power_acct(struct hda_codec *codec)
4531{
4532 unsigned long delta = jiffies - codec->power_jiffies;
4533 if (codec->power_on)
4534 codec->power_on_acct += delta;
4535 else
4536 codec->power_off_acct += delta;
4537 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004538}
4539
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004540/* Transition to powered up, if wait_power_down then wait for a pending
4541 * transition to D3 to complete. A pending D3 transition is indicated
4542 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004543/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004544static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004545{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004546 struct hda_bus *bus = codec->bus;
4547
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004548 /* Return if power_on or transitioning to power_on, unless currently
4549 * powering down. */
4550 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004551 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004552 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004553 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004554
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004555 cancel_delayed_work_sync(&codec->power_work);
4556
4557 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004558 /* If the power down delayed work was cancelled above before starting,
4559 * then there is no need to go through power up here.
4560 */
4561 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004562 if (codec->power_transition < 0)
4563 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004564 return;
4565 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004566
Takashi Iwaid66fee52011-08-02 15:39:31 +02004567 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004568 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004569 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004570 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004571 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004572 spin_unlock(&codec->power_lock);
4573
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004574 if (codec->pm_down_notified) {
4575 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004576 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004577 }
4578
Takashi Iwaicb53c622007-08-10 17:21:45 +02004579 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004580
4581 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004582 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004583}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004584
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004585#define power_save(codec) \
4586 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004587
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004588/* Transition to powered down */
4589static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004590{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004591 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004592 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004593
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004594 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004595 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004596 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004597 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004598 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004599}
4600
4601/**
4602 * snd_hda_power_save - Power-up/down/sync the codec
4603 * @codec: HD-audio codec
4604 * @delta: the counter delta to change
4605 *
4606 * Change the power-up counter via @delta, and power up or down the hardware
4607 * appropriately. For the power-down, queue to the delayed action.
4608 * Passing zero to @delta means to synchronize the power state.
4609 */
4610void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4611{
4612 spin_lock(&codec->power_lock);
4613 codec->power_count += delta;
4614 trace_hda_power_count(codec);
4615 if (delta > 0)
4616 __snd_hda_power_up(codec, d3wait);
4617 else
4618 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004619 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004620}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004621EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004622
Takashi Iwaid5191e52009-11-16 14:58:17 +01004623/**
4624 * snd_hda_check_amp_list_power - Check the amp list and update the power
4625 * @codec: HD-audio codec
4626 * @check: the object containing an AMP list and the status
4627 * @nid: NID to check / update
4628 *
4629 * Check whether the given NID is in the amp list. If it's in the list,
4630 * check the current AMP status, and update the the power-status according
4631 * to the mute status.
4632 *
4633 * This function is supposed to be set or called from the check_power_status
4634 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004635 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004636int snd_hda_check_amp_list_power(struct hda_codec *codec,
4637 struct hda_loopback_check *check,
4638 hda_nid_t nid)
4639{
Takashi Iwai031024e2011-05-02 11:29:30 +02004640 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004641 int ch, v;
4642
4643 if (!check->amplist)
4644 return 0;
4645 for (p = check->amplist; p->nid; p++) {
4646 if (p->nid == nid)
4647 break;
4648 }
4649 if (!p->nid)
4650 return 0; /* nothing changed */
4651
4652 for (p = check->amplist; p->nid; p++) {
4653 for (ch = 0; ch < 2; ch++) {
4654 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4655 p->idx);
4656 if (!(v & HDA_AMP_MUTE) && v > 0) {
4657 if (!check->power_on) {
4658 check->power_on = 1;
4659 snd_hda_power_up(codec);
4660 }
4661 return 1;
4662 }
4663 }
4664 }
4665 if (check->power_on) {
4666 check->power_on = 0;
4667 snd_hda_power_down(codec);
4668 }
4669 return 0;
4670}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004671EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004674/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004675 * Channel mode helper
4676 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004677
4678/**
4679 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4680 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004681int snd_hda_ch_mode_info(struct hda_codec *codec,
4682 struct snd_ctl_elem_info *uinfo,
4683 const struct hda_channel_mode *chmode,
4684 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004685{
4686 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4687 uinfo->count = 1;
4688 uinfo->value.enumerated.items = num_chmodes;
4689 if (uinfo->value.enumerated.item >= num_chmodes)
4690 uinfo->value.enumerated.item = num_chmodes - 1;
4691 sprintf(uinfo->value.enumerated.name, "%dch",
4692 chmode[uinfo->value.enumerated.item].channels);
4693 return 0;
4694}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004695EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004696
Takashi Iwaid5191e52009-11-16 14:58:17 +01004697/**
4698 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4699 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004700int snd_hda_ch_mode_get(struct hda_codec *codec,
4701 struct snd_ctl_elem_value *ucontrol,
4702 const struct hda_channel_mode *chmode,
4703 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004704 int max_channels)
4705{
4706 int i;
4707
4708 for (i = 0; i < num_chmodes; i++) {
4709 if (max_channels == chmode[i].channels) {
4710 ucontrol->value.enumerated.item[0] = i;
4711 break;
4712 }
4713 }
4714 return 0;
4715}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004716EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004717
Takashi Iwaid5191e52009-11-16 14:58:17 +01004718/**
4719 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4720 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004721int snd_hda_ch_mode_put(struct hda_codec *codec,
4722 struct snd_ctl_elem_value *ucontrol,
4723 const struct hda_channel_mode *chmode,
4724 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004725 int *max_channelsp)
4726{
4727 unsigned int mode;
4728
4729 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004730 if (mode >= num_chmodes)
4731 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004732 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004733 return 0;
4734 /* change the current channel setting */
4735 *max_channelsp = chmode[mode].channels;
4736 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004737 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004738 return 1;
4739}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004740EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004741
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742/*
4743 * input MUX helper
4744 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004745
4746/**
4747 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4748 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004749int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4750 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
4752 unsigned int index;
4753
4754 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4755 uinfo->count = 1;
4756 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004757 if (!imux->num_items)
4758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 index = uinfo->value.enumerated.item;
4760 if (index >= imux->num_items)
4761 index = imux->num_items - 1;
4762 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4763 return 0;
4764}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004765EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Takashi Iwaid5191e52009-11-16 14:58:17 +01004767/**
4768 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4769 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004770int snd_hda_input_mux_put(struct hda_codec *codec,
4771 const struct hda_input_mux *imux,
4772 struct snd_ctl_elem_value *ucontrol,
4773 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 unsigned int *cur_val)
4775{
4776 unsigned int idx;
4777
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004778 if (!imux->num_items)
4779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 idx = ucontrol->value.enumerated.item[0];
4781 if (idx >= imux->num_items)
4782 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004783 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004785 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4786 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 *cur_val = idx;
4788 return 1;
4789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004790EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792
4793/*
4794 * Multi-channel / digital-out PCM helper functions
4795 */
4796
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004797/* setup SPDIF output stream */
4798static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4799 unsigned int stream_tag, unsigned int format)
4800{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004801 struct hda_spdif_out *spdif;
4802 unsigned int curr_fmt;
4803 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004804
Laurence Darby3bef1c32012-11-03 17:00:06 +00004805 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4806 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4807 AC_VERB_GET_STREAM_FORMAT, 0);
4808 reset = codec->spdif_status_reset &&
4809 (spdif->ctls & AC_DIG1_ENABLE) &&
4810 curr_fmt != format;
4811
4812 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4813 updated */
4814 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004815 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004816 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004817 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004818 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004819 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004820 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004821 for (d = codec->slave_dig_outs; *d; d++)
4822 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4823 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004824 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004825 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004826 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004827 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004828 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004829}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004830
Takashi Iwai2f728532008-09-25 16:32:41 +02004831static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4832{
4833 snd_hda_codec_cleanup_stream(codec, nid);
4834 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004835 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004836 for (d = codec->slave_dig_outs; *d; d++)
4837 snd_hda_codec_cleanup_stream(codec, *d);
4838 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004839}
4840
Takashi Iwaid5191e52009-11-16 14:58:17 +01004841/**
4842 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4843 * @bus: HD-audio bus
4844 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004845void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4846{
4847 struct hda_codec *codec;
4848
4849 if (!bus)
4850 return;
4851 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004852 if (hda_codec_is_power_on(codec) &&
4853 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004854 codec->patch_ops.reboot_notify(codec);
4855 }
4856}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004857EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004858
Takashi Iwaid5191e52009-11-16 14:58:17 +01004859/**
4860 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004862int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4863 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Ingo Molnar62932df2006-01-16 16:34:20 +01004865 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004866 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4867 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004868 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004870 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return 0;
4872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004873EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Takashi Iwaid5191e52009-11-16 14:58:17 +01004875/**
4876 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4877 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004878int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4879 struct hda_multi_out *mout,
4880 unsigned int stream_tag,
4881 unsigned int format,
4882 struct snd_pcm_substream *substream)
4883{
4884 mutex_lock(&codec->spdif_mutex);
4885 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4886 mutex_unlock(&codec->spdif_mutex);
4887 return 0;
4888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004889EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004890
Takashi Iwaid5191e52009-11-16 14:58:17 +01004891/**
4892 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4893 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004894int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4895 struct hda_multi_out *mout)
4896{
4897 mutex_lock(&codec->spdif_mutex);
4898 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4899 mutex_unlock(&codec->spdif_mutex);
4900 return 0;
4901}
4902EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4903
Takashi Iwaid5191e52009-11-16 14:58:17 +01004904/**
4905 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004907int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4908 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
Ingo Molnar62932df2006-01-16 16:34:20 +01004910 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004912 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return 0;
4914}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004915EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Takashi Iwaid5191e52009-11-16 14:58:17 +01004917/**
4918 * snd_hda_multi_out_analog_open - open analog outputs
4919 *
4920 * Open analog outputs and set up the hw-constraints.
4921 * If the digital outputs can be opened as slave, open the digital
4922 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004924int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4925 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004926 struct snd_pcm_substream *substream,
4927 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928{
Takashi Iwai9a081602008-02-12 18:37:26 +01004929 struct snd_pcm_runtime *runtime = substream->runtime;
4930 runtime->hw.channels_max = mout->max_channels;
4931 if (mout->dig_out_nid) {
4932 if (!mout->analog_rates) {
4933 mout->analog_rates = hinfo->rates;
4934 mout->analog_formats = hinfo->formats;
4935 mout->analog_maxbps = hinfo->maxbps;
4936 } else {
4937 runtime->hw.rates = mout->analog_rates;
4938 runtime->hw.formats = mout->analog_formats;
4939 hinfo->maxbps = mout->analog_maxbps;
4940 }
4941 if (!mout->spdif_rates) {
4942 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4943 &mout->spdif_rates,
4944 &mout->spdif_formats,
4945 &mout->spdif_maxbps);
4946 }
4947 mutex_lock(&codec->spdif_mutex);
4948 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004949 if ((runtime->hw.rates & mout->spdif_rates) &&
4950 (runtime->hw.formats & mout->spdif_formats)) {
4951 runtime->hw.rates &= mout->spdif_rates;
4952 runtime->hw.formats &= mout->spdif_formats;
4953 if (mout->spdif_maxbps < hinfo->maxbps)
4954 hinfo->maxbps = mout->spdif_maxbps;
4955 } else {
4956 mout->share_spdif = 0;
4957 /* FIXME: need notify? */
4958 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004959 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004960 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4963 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004965EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Takashi Iwaid5191e52009-11-16 14:58:17 +01004967/**
4968 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4969 *
4970 * Set up the i/o for analog out.
4971 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004973int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4974 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 unsigned int stream_tag,
4976 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004977 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
Takashi Iwaidda14412011-05-02 11:29:30 +02004979 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004981 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 int i;
4983
Ingo Molnar62932df2006-01-16 16:34:20 +01004984 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004985 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004986 if (mout->dig_out_nid && mout->share_spdif &&
4987 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004989 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4990 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004991 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004993 setup_dig_out_stream(codec, mout->dig_out_nid,
4994 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 } else {
4996 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004997 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
4999 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005000 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005003 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5004 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005005 if (!mout->no_share_stream &&
5006 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005008 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5009 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005010 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005011 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5012 if (!mout->no_share_stream && mout->hp_out_nid[i])
5013 snd_hda_codec_setup_stream(codec,
5014 mout->hp_out_nid[i],
5015 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005016 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005017 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005018 snd_hda_codec_setup_stream(codec,
5019 mout->extra_out_nid[i],
5020 stream_tag, 0, format);
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 /* surrounds */
5023 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005024 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005025 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5026 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005027 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005028 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5029 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
5031 return 0;
5032}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005033EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Takashi Iwaid5191e52009-11-16 14:58:17 +01005035/**
5036 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005038int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5039 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
Takashi Iwaidda14412011-05-02 11:29:30 +02005041 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 int i;
5043
5044 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005045 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005047 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005048 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5049 if (mout->hp_out_nid[i])
5050 snd_hda_codec_cleanup_stream(codec,
5051 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005052 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5053 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005054 snd_hda_codec_cleanup_stream(codec,
5055 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005056 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005058 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 mout->dig_out_used = 0;
5060 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005061 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return 0;
5063}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005064EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Takashi Iwai47408602012-04-20 13:06:53 +02005066/**
5067 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5068 *
5069 * Guess the suitable VREF pin bits to be set as the pin-control value.
5070 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5071 */
5072unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5073{
5074 unsigned int pincap;
5075 unsigned int oldval;
5076 oldval = snd_hda_codec_read(codec, pin, 0,
5077 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5078 pincap = snd_hda_query_pin_caps(codec, pin);
5079 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5080 /* Exception: if the default pin setup is vref50, we give it priority */
5081 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5082 return AC_PINCTL_VREF_80;
5083 else if (pincap & AC_PINCAP_VREF_50)
5084 return AC_PINCTL_VREF_50;
5085 else if (pincap & AC_PINCAP_VREF_100)
5086 return AC_PINCTL_VREF_100;
5087 else if (pincap & AC_PINCAP_VREF_GRD)
5088 return AC_PINCTL_VREF_GRD;
5089 return AC_PINCTL_VREF_HIZ;
5090}
5091EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5092
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005093int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5094 unsigned int val, bool cached)
5095{
5096 if (val) {
5097 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005098 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005099 if (!(cap & AC_PINCAP_OUT))
5100 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5101 else if ((val & AC_PINCTL_HP_EN) &&
5102 !(cap & AC_PINCAP_HP_DRV))
5103 val &= ~AC_PINCTL_HP_EN;
5104 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005105 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005106 if (!(cap & AC_PINCAP_IN))
5107 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5108 }
5109 }
5110 if (cached)
5111 return snd_hda_codec_update_cache(codec, pin, 0,
5112 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5113 else
5114 return snd_hda_codec_write(codec, pin, 0,
5115 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5116}
5117EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5118
Takashi Iwai990061c2010-09-09 22:08:44 +02005119/**
5120 * snd_hda_add_imux_item - Add an item to input_mux
5121 *
5122 * When the same label is used already in the existing items, the number
5123 * suffix is appended to the label. This label index number is stored
5124 * to type_idx when non-NULL pointer is given.
5125 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005126int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5127 int index, int *type_idx)
5128{
5129 int i, label_idx = 0;
5130 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5131 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5132 return -EINVAL;
5133 }
5134 for (i = 0; i < imux->num_items; i++) {
5135 if (!strncmp(label, imux->items[i].label, strlen(label)))
5136 label_idx++;
5137 }
5138 if (type_idx)
5139 *type_idx = label_idx;
5140 if (label_idx > 0)
5141 snprintf(imux->items[imux->num_items].label,
5142 sizeof(imux->items[imux->num_items].label),
5143 "%s %d", label, label_idx);
5144 else
5145 strlcpy(imux->items[imux->num_items].label, label,
5146 sizeof(imux->items[imux->num_items].label));
5147 imux->items[imux->num_items].index = index;
5148 imux->num_items++;
5149 return 0;
5150}
5151EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005152
Takashi Iwai4a471b72005-12-07 13:56:29 +01005153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154#ifdef CONFIG_PM
5155/*
5156 * power management
5157 */
5158
5159/**
5160 * snd_hda_suspend - suspend the codecs
5161 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 *
5163 * Returns 0 if successful.
5164 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005165int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005167 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Takashi Iwai0ba21762007-04-16 11:29:14 +02005169 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005170 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005171 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005172 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 }
5174 return 0;
5175}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005176EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
5178/**
5179 * snd_hda_resume - resume the codecs
5180 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 *
5182 * Returns 0 if successful.
5183 */
5184int snd_hda_resume(struct hda_bus *bus)
5185{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005186 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Takashi Iwai0ba21762007-04-16 11:29:14 +02005188 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005189 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 return 0;
5192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005193EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005194#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005195
5196/*
5197 * generic arrays
5198 */
5199
Takashi Iwaid5191e52009-11-16 14:58:17 +01005200/**
5201 * snd_array_new - get a new element from the given array
5202 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005203 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005204 * Get a new element from the given array. If it exceeds the
5205 * pre-allocated array size, re-allocate the array.
5206 *
5207 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005208 */
5209void *snd_array_new(struct snd_array *array)
5210{
Takashi Iwai12f17712012-10-10 08:59:14 +02005211 if (snd_BUG_ON(!array->elem_size))
5212 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005213 if (array->used >= array->alloced) {
5214 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005215 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005216 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005217 void *nlist;
5218 if (snd_BUG_ON(num >= 4096))
5219 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005220 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005221 if (!nlist)
5222 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005223 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005224 array->list = nlist;
5225 array->alloced = num;
5226 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005227 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005228}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005229EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005230
Takashi Iwaid5191e52009-11-16 14:58:17 +01005231/**
5232 * snd_array_free - free the given array elements
5233 * @array: the array object
5234 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005235void snd_array_free(struct snd_array *array)
5236{
5237 kfree(array->list);
5238 array->used = 0;
5239 array->alloced = 0;
5240 array->list = NULL;
5241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005242EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005243
Takashi Iwaid5191e52009-11-16 14:58:17 +01005244/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005245 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5246 * @pcm: PCM caps bits
5247 * @buf: the string buffer to write
5248 * @buflen: the max buffer length
5249 *
5250 * used by hda_proc.c and hda_eld.c
5251 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005252void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5253{
5254 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5255 int i, j;
5256
5257 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5258 if (pcm & (AC_SUPPCM_BITS_8 << i))
5259 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5260
5261 buf[j] = '\0'; /* necessary when j == 0 */
5262}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005263EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005264
5265MODULE_DESCRIPTION("HDA codec core");
5266MODULE_LICENSE("GPL");