blob: 3634bfebc0082d8786243b31cc9e17cc4b30bdce [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);
Takashi Iwai361dab32012-05-09 14:35:27 +02001253 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
David Henningsson26a6cb62012-10-09 15:04:21 +02001254 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Takashi Iwai83012a72012-08-24 18:38:08 +02001256#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001257 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001258 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1259 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1260 * the caller has to power down appropriatley after initialization
1261 * phase.
1262 */
1263 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001264 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001265#endif
1266
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001267 if (codec->bus->modelname) {
1268 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1269 if (!codec->modelname) {
1270 snd_hda_codec_free(codec);
1271 return -ENODEV;
1272 }
1273 }
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 list_add_tail(&codec->list, &bus->codec_list);
1276 bus->caddr_tbl[codec_addr] = codec;
1277
Takashi Iwai0ba21762007-04-16 11:29:14 +02001278 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1279 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001280 if (codec->vendor_id == -1)
1281 /* read again, hopefully the access method was corrected
1282 * in the last read...
1283 */
1284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001286 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1287 AC_PAR_SUBSYSTEM_ID);
1288 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001291 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001293 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001294 err = -ENODEV;
1295 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Takashi Iwaid8193872012-08-31 07:54:38 -07001298 fg = codec->afg ? codec->afg : codec->mfg;
1299 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001301 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001302 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001303 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = read_pin_defaults(codec);
1305 if (err < 0)
1306 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001307
Takashi Iwai0ba21762007-04-16 11:29:14 +02001308 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001309 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001310 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001312 }
1313
Takashi Iwai83012a72012-08-24 18:38:08 +02001314#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001315 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001316 AC_PWRST_CLKSTOP);
1317 if (!codec->d3_stop_clk)
1318 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001319#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001320 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001321 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001322
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001323 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001324 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001326 snd_hda_codec_proc_new(codec);
1327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001329
1330 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1331 codec->subsystem_id, codec->revision_id);
1332 snd_component_add(codec->bus->card, component);
1333
1334 if (codecp)
1335 *codecp = codec;
1336 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001337
1338 error:
1339 snd_hda_codec_free(codec);
1340 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001342EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001343
Takashi Iwaid5191e52009-11-16 14:58:17 +01001344/**
1345 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1346 * @codec: the HDA codec
1347 *
1348 * Start parsing of the given codec tree and (re-)initialize the whole
1349 * patch instance.
1350 *
1351 * Returns 0 if successful or a negative error code.
1352 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353int snd_hda_codec_configure(struct hda_codec *codec)
1354{
1355 int err;
1356
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001357 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001358 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001359 err = get_codec_name(codec);
1360 if (err < 0)
1361 return err;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Takashi Iwai82467612007-07-27 19:15:54 +02001364 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001366 goto patched;
1367 }
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (codec->preset && codec->preset->patch) {
1369 err = codec->preset->patch(codec);
1370 goto patched;
1371 }
1372
1373 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001374 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001375 if (err < 0)
1376 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001377
1378 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001379 if (!err && codec->patch_ops.unsol_event)
1380 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001381 /* audio codec should override the mixer name */
1382 if (!err && (codec->afg || !*codec->bus->card->mixername))
1383 snprintf(codec->bus->card->mixername,
1384 sizeof(codec->bus->card->mixername),
1385 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001388EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Takashi Iwaied360812012-08-08 17:12:52 +02001390/* update the stream-id if changed */
1391static void update_pcm_stream_id(struct hda_codec *codec,
1392 struct hda_cvt_setup *p, hda_nid_t nid,
1393 u32 stream_tag, int channel_id)
1394{
1395 unsigned int oldval, newval;
1396
1397 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1398 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1399 newval = (stream_tag << 4) | channel_id;
1400 if (oldval != newval)
1401 snd_hda_codec_write(codec, nid, 0,
1402 AC_VERB_SET_CHANNEL_STREAMID,
1403 newval);
1404 p->stream_tag = stream_tag;
1405 p->channel_id = channel_id;
1406 }
1407}
1408
1409/* update the format-id if changed */
1410static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1411 hda_nid_t nid, int format)
1412{
1413 unsigned int oldval;
1414
1415 if (p->format_id != format) {
1416 oldval = snd_hda_codec_read(codec, nid, 0,
1417 AC_VERB_GET_STREAM_FORMAT, 0);
1418 if (oldval != format) {
1419 msleep(1);
1420 snd_hda_codec_write(codec, nid, 0,
1421 AC_VERB_SET_STREAM_FORMAT,
1422 format);
1423 }
1424 p->format_id = format;
1425 }
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/**
1429 * snd_hda_codec_setup_stream - set up the codec for streaming
1430 * @codec: the CODEC to set up
1431 * @nid: the NID to set up
1432 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1433 * @channel_id: channel id to pass, zero based.
1434 * @format: stream format.
1435 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001436void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1437 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int channel_id, int format)
1439{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001440 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001441 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001442 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001443 int i;
1444
Takashi Iwai0ba21762007-04-16 11:29:14 +02001445 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001446 return;
1447
Takashi Iwai0ba21762007-04-16 11:29:14 +02001448 snd_printdd("hda_codec_setup_stream: "
1449 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 p = get_hda_cvt_setup(codec, nid);
1452 if (!p)
1453 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001454
1455 if (codec->pcm_format_first)
1456 update_pcm_format(codec, p, nid, format);
1457 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1458 if (!codec->pcm_format_first)
1459 update_pcm_format(codec, p, nid, format);
1460
Takashi Iwaieb541332010-08-06 13:48:11 +02001461 p->active = 1;
1462 p->dirty = 0;
1463
1464 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001465 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001466 list_for_each_entry(c, &codec->bus->codec_list, list) {
1467 for (i = 0; i < c->cvt_setups.used; i++) {
1468 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001469 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001470 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001471 p->dirty = 1;
1472 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001475EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Takashi Iwaif0cea792010-08-13 11:56:53 +02001477static void really_cleanup_stream(struct hda_codec *codec,
1478 struct hda_cvt_setup *q);
1479
Takashi Iwaid5191e52009-11-16 14:58:17 +01001480/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001481 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001482 * @codec: the CODEC to clean up
1483 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001484 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001485 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001486void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1487 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001488{
Takashi Iwaieb541332010-08-06 13:48:11 +02001489 struct hda_cvt_setup *p;
1490
Takashi Iwai888afa12008-03-18 09:57:50 +01001491 if (!nid)
1492 return;
1493
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001494 if (codec->no_sticky_stream)
1495 do_now = 1;
1496
Takashi Iwai888afa12008-03-18 09:57:50 +01001497 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001498 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001499 if (p) {
1500 /* here we just clear the active flag when do_now isn't set;
1501 * actual clean-ups will be done later in
1502 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1503 */
1504 if (do_now)
1505 really_cleanup_stream(codec, p);
1506 else
1507 p->active = 0;
1508 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001509}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001510EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001511
Takashi Iwaieb541332010-08-06 13:48:11 +02001512static void really_cleanup_stream(struct hda_codec *codec,
1513 struct hda_cvt_setup *q)
1514{
1515 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001516 if (q->stream_tag || q->channel_id)
1517 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1518 if (q->format_id)
1519 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1520);
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 memset(q, 0, sizeof(*q));
1522 q->nid = nid;
1523}
1524
1525/* clean up the all conflicting obsolete streams */
1526static void purify_inactive_streams(struct hda_codec *codec)
1527{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001528 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001529 int i;
1530
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001531 list_for_each_entry(c, &codec->bus->codec_list, list) {
1532 for (i = 0; i < c->cvt_setups.used; i++) {
1533 struct hda_cvt_setup *p;
1534 p = snd_array_elem(&c->cvt_setups, i);
1535 if (p->dirty)
1536 really_cleanup_stream(c, p);
1537 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001538 }
1539}
1540
Takashi Iwai2a439522011-07-26 09:52:50 +02001541#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001542/* clean up all streams; called from suspend */
1543static void hda_cleanup_all_streams(struct hda_codec *codec)
1544{
1545 int i;
1546
1547 for (i = 0; i < codec->cvt_setups.used; i++) {
1548 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1549 if (p->stream_tag)
1550 really_cleanup_stream(codec, p);
1551 }
1552}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001553#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * amp access functions
1557 */
1558
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001559/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001560#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001561#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001562#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1563#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001565#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001568static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001569 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Takashi Iwai01751f52007-08-10 16:59:39 +02001571 memset(cache, 0, sizeof(*cache));
1572 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001573 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001574}
1575
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001576static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001577{
Takashi Iwai603c4012008-07-30 15:01:44 +02001578 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001582static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwai01751f52007-08-10 16:59:39 +02001584 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1585 u16 cur = cache->hash[idx];
1586 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001589 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (info->key == key)
1591 return info;
1592 cur = info->next;
1593 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001594 return NULL;
1595}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001597/* query the hash. allocate an entry if not found. */
1598static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1599 u32 key)
1600{
1601 struct hda_cache_head *info = get_hash(cache, key);
1602 if (!info) {
1603 u16 idx, cur;
1604 /* add a new hash entry */
1605 info = snd_array_new(&cache->buf);
1606 if (!info)
1607 return NULL;
1608 cur = snd_array_index(&cache->buf, info);
1609 info->key = key;
1610 info->val = 0;
1611 idx = key % (u16)ARRAY_SIZE(cache->hash);
1612 info->next = cache->hash[idx];
1613 cache->hash[idx] = cur;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return info;
1616}
1617
Takashi Iwai01751f52007-08-10 16:59:39 +02001618/* query and allocate an amp hash entry */
1619static inline struct hda_amp_info *
1620get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1621{
1622 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1623}
1624
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001625/* overwrite the value with the key in the caps hash */
1626static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1627{
1628 struct hda_amp_info *info;
1629
1630 mutex_lock(&codec->hash_mutex);
1631 info = get_alloc_amp_hash(codec, key);
1632 if (!info) {
1633 mutex_unlock(&codec->hash_mutex);
1634 return -EINVAL;
1635 }
1636 info->amp_caps = val;
1637 info->head.val |= INFO_AMP_CAPS;
1638 mutex_unlock(&codec->hash_mutex);
1639 return 0;
1640}
1641
1642/* query the value from the caps hash; if not found, fetch the current
1643 * value from the given function and store in the hash
1644 */
1645static unsigned int
1646query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1647 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1648{
1649 struct hda_amp_info *info;
1650 unsigned int val;
1651
1652 mutex_lock(&codec->hash_mutex);
1653 info = get_alloc_amp_hash(codec, key);
1654 if (!info) {
1655 mutex_unlock(&codec->hash_mutex);
1656 return 0;
1657 }
1658 if (!(info->head.val & INFO_AMP_CAPS)) {
1659 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1660 val = func(codec, nid, dir);
1661 write_caps_hash(codec, key, val);
1662 } else {
1663 val = info->amp_caps;
1664 mutex_unlock(&codec->hash_mutex);
1665 }
1666 return val;
1667}
1668
1669static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1670 int direction)
1671{
1672 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1673 nid = codec->afg;
1674 return snd_hda_param_read(codec, nid,
1675 direction == HDA_OUTPUT ?
1676 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1677}
1678
Takashi Iwaid5191e52009-11-16 14:58:17 +01001679/**
1680 * query_amp_caps - query AMP capabilities
1681 * @codec: the HD-auio codec
1682 * @nid: the NID to query
1683 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1684 *
1685 * Query AMP capabilities for the given widget and direction.
1686 * Returns the obtained capability bits.
1687 *
1688 * When cap bits have been already read, this doesn't read again but
1689 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001691u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001693 return query_caps_hash(codec, nid, direction,
1694 HDA_HASH_KEY(nid, direction, 0),
1695 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001697EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Takashi Iwaid5191e52009-11-16 14:58:17 +01001699/**
1700 * snd_hda_override_amp_caps - Override the AMP capabilities
1701 * @codec: the CODEC to clean up
1702 * @nid: the NID to clean up
1703 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1704 * @caps: the capability bits to set
1705 *
1706 * Override the cached AMP caps bits value by the given one.
1707 * This function is useful if the driver needs to adjust the AMP ranges,
1708 * e.g. limit to 0dB, etc.
1709 *
1710 * Returns zero if successful or a negative error code.
1711 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001712int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1713 unsigned int caps)
1714{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001715 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001716}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001717EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001718
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001719static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1720 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001721{
1722 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1723}
1724
Takashi Iwaid5191e52009-11-16 14:58:17 +01001725/**
1726 * snd_hda_query_pin_caps - Query PIN capabilities
1727 * @codec: the HD-auio codec
1728 * @nid: the NID to query
1729 *
1730 * Query PIN capabilities for the given widget.
1731 * Returns the obtained capability bits.
1732 *
1733 * When cap bits have been already read, this doesn't read again but
1734 * returns the cached value.
1735 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001736u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1737{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001738 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001739 read_pin_cap);
1740}
Takashi Iwai1327a322009-03-23 13:07:47 +01001741EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1742
Wu Fengguang864f92b2009-11-18 12:38:02 +08001743/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001744 * snd_hda_override_pin_caps - Override the pin capabilities
1745 * @codec: the CODEC
1746 * @nid: the NID to override
1747 * @caps: the capability bits to set
1748 *
1749 * Override the cached PIN capabilitiy bits value by the given one.
1750 *
1751 * Returns zero if successful or a negative error code.
1752 */
1753int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1754 unsigned int caps)
1755{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001756 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001757}
1758EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1759
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001760/* read or sync the hash value with the current value;
1761 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001763static struct hda_amp_info *
1764update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1765 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001767 struct hda_amp_info *info;
1768 unsigned int parm, val = 0;
1769 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001771 retry:
1772 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1773 if (!info)
1774 return NULL;
1775 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1776 if (!val_read) {
1777 mutex_unlock(&codec->hash_mutex);
1778 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1779 parm |= direction == HDA_OUTPUT ?
1780 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1781 parm |= index;
1782 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001783 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001784 val &= 0xff;
1785 val_read = true;
1786 mutex_lock(&codec->hash_mutex);
1787 goto retry;
1788 }
1789 info->vol[ch] = val;
1790 info->head.val |= INFO_AMP_VOL(ch);
1791 }
1792 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001796 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001798static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001799 hda_nid_t nid, int ch, int direction, int index,
1800 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 u32 parm;
1803
1804 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1805 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1806 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001807 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1808 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1809 ; /* set the zero value as a fake mute */
1810 else
1811 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1813}
1814
Takashi Iwaid5191e52009-11-16 14:58:17 +01001815/**
1816 * snd_hda_codec_amp_read - Read AMP value
1817 * @codec: HD-audio codec
1818 * @nid: NID to read the AMP value
1819 * @ch: channel (left=0 or right=1)
1820 * @direction: #HDA_INPUT or #HDA_OUTPUT
1821 * @index: the index value (only for input direction)
1822 *
1823 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Takashi Iwai834be882006-03-01 14:16:17 +01001825int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1826 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001828 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001829 unsigned int val = 0;
1830
1831 mutex_lock(&codec->hash_mutex);
1832 info = update_amp_hash(codec, nid, ch, direction, index);
1833 if (info)
1834 val = info->vol[ch];
1835 mutex_unlock(&codec->hash_mutex);
1836 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001838EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Takashi Iwaid5191e52009-11-16 14:58:17 +01001840/**
1841 * snd_hda_codec_amp_update - update the AMP value
1842 * @codec: HD-audio codec
1843 * @nid: NID to read the AMP value
1844 * @ch: channel (left=0 or right=1)
1845 * @direction: #HDA_INPUT or #HDA_OUTPUT
1846 * @idx: the index value (only for input direction)
1847 * @mask: bit mask to set
1848 * @val: the bits value to set
1849 *
1850 * Update the AMP value with a bit mask.
1851 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001852 */
Takashi Iwai834be882006-03-01 14:16:17 +01001853int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1854 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001856 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001857
Takashi Iwai467126462010-03-29 09:19:38 +02001858 if (snd_BUG_ON(mask & ~0xff))
1859 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001860 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001861
1862 mutex_lock(&codec->hash_mutex);
1863 info = update_amp_hash(codec, nid, ch, direction, idx);
1864 if (!info) {
1865 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001867 }
1868 val |= info->vol[ch] & ~mask;
1869 if (info->vol[ch] == val) {
1870 mutex_unlock(&codec->hash_mutex);
1871 return 0;
1872 }
1873 info->vol[ch] = val;
1874 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001875 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 1;
1877}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001878EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Takashi Iwaid5191e52009-11-16 14:58:17 +01001880/**
1881 * snd_hda_codec_amp_stereo - update the AMP stereo values
1882 * @codec: HD-audio codec
1883 * @nid: NID to read the AMP value
1884 * @direction: #HDA_INPUT or #HDA_OUTPUT
1885 * @idx: the index value (only for input direction)
1886 * @mask: bit mask to set
1887 * @val: the bits value to set
1888 *
1889 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1890 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001891 */
1892int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1893 int direction, int idx, int mask, int val)
1894{
1895 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001896
1897 if (snd_BUG_ON(mask & ~0xff))
1898 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001899 for (ch = 0; ch < 2; ch++)
1900 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1901 idx, mask, val);
1902 return ret;
1903}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001904EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001905
Takashi Iwai2a439522011-07-26 09:52:50 +02001906#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001907/**
1908 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1909 * @codec: HD-audio codec
1910 *
1911 * Resume the all amp commands from the cache.
1912 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001913void snd_hda_codec_resume_amp(struct hda_codec *codec)
1914{
Takashi Iwai603c4012008-07-30 15:01:44 +02001915 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001916 int i;
1917
Takashi Iwai603c4012008-07-30 15:01:44 +02001918 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001919 u32 key = buffer->head.key;
1920 hda_nid_t nid;
1921 unsigned int idx, dir, ch;
1922 if (!key)
1923 continue;
1924 nid = key & 0xff;
1925 idx = (key >> 16) & 0xff;
1926 dir = (key >> 24) & 0xff;
1927 for (ch = 0; ch < 2; ch++) {
1928 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1929 continue;
1930 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1931 buffer->vol[ch]);
1932 }
1933 }
1934}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001935EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001936#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001938static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1939 unsigned int ofs)
1940{
1941 u32 caps = query_amp_caps(codec, nid, dir);
1942 /* get num steps */
1943 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1944 if (ofs < caps)
1945 caps -= ofs;
1946 return caps;
1947}
1948
Takashi Iwaid5191e52009-11-16 14:58:17 +01001949/**
1950 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1951 *
1952 * The control element is supposed to have the private_value field
1953 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1954 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001955int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1959 u16 nid = get_amp_nid(kcontrol);
1960 u8 chs = get_amp_channels(kcontrol);
1961 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001962 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1965 uinfo->count = chs == 3 ? 2 : 1;
1966 uinfo->value.integer.min = 0;
1967 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1968 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001969 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001970 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1971 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EINVAL;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return 0;
1975}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001976EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001978
1979static inline unsigned int
1980read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1981 int ch, int dir, int idx, unsigned int ofs)
1982{
1983 unsigned int val;
1984 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1985 val &= HDA_AMP_VOLMASK;
1986 if (val >= ofs)
1987 val -= ofs;
1988 else
1989 val = 0;
1990 return val;
1991}
1992
1993static inline int
1994update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1995 int ch, int dir, int idx, unsigned int ofs,
1996 unsigned int val)
1997{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001998 unsigned int maxval;
1999
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 if (val > 0)
2001 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002002 /* ofs = 0: raw max value */
2003 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002004 if (val > maxval)
2005 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2007 HDA_AMP_VOLMASK, val);
2008}
2009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2012 *
2013 * The control element is supposed to have the private_value field
2014 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2015 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002016int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2020 hda_nid_t nid = get_amp_nid(kcontrol);
2021 int chs = get_amp_channels(kcontrol);
2022 int dir = get_amp_direction(kcontrol);
2023 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 long *valp = ucontrol->value.integer.value;
2026
2027 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002028 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002030 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return 0;
2032}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002033EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Takashi Iwaid5191e52009-11-16 14:58:17 +01002035/**
2036 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2037 *
2038 * The control element is supposed to have the private_value field
2039 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2040 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002041int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2045 hda_nid_t nid = get_amp_nid(kcontrol);
2046 int chs = get_amp_channels(kcontrol);
2047 int dir = get_amp_direction(kcontrol);
2048 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002049 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 long *valp = ucontrol->value.integer.value;
2051 int change = 0;
2052
Takashi Iwaicb53c622007-08-10 17:21:45 +02002053 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002054 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002055 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002056 valp++;
2057 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002058 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002059 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002060 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return change;
2062}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002063EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Takashi Iwaid5191e52009-11-16 14:58:17 +01002065/**
2066 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2067 *
2068 * The control element is supposed to have the private_value field
2069 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2070 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002071int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2072 unsigned int size, unsigned int __user *_tlv)
2073{
2074 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2075 hda_nid_t nid = get_amp_nid(kcontrol);
2076 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002077 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002078 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002079 u32 caps, val1, val2;
2080
2081 if (size < 4 * sizeof(unsigned int))
2082 return -ENOMEM;
2083 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002084 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2085 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002086 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002087 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002088 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002089 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002090 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002091 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2092 return -EFAULT;
2093 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2094 return -EFAULT;
2095 if (put_user(val1, _tlv + 2))
2096 return -EFAULT;
2097 if (put_user(val2, _tlv + 3))
2098 return -EFAULT;
2099 return 0;
2100}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002101EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002102
Takashi Iwaid5191e52009-11-16 14:58:17 +01002103/**
2104 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2105 * @codec: HD-audio codec
2106 * @nid: NID of a reference widget
2107 * @dir: #HDA_INPUT or #HDA_OUTPUT
2108 * @tlv: TLV data to be stored, at least 4 elements
2109 *
2110 * Set (static) TLV data for a virtual master volume using the AMP caps
2111 * obtained from the reference NID.
2112 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002113 */
2114void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2115 unsigned int *tlv)
2116{
2117 u32 caps;
2118 int nums, step;
2119
2120 caps = query_amp_caps(codec, nid, dir);
2121 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2122 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2123 step = (step + 1) * 25;
2124 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2125 tlv[1] = 2 * sizeof(unsigned int);
2126 tlv[2] = -nums * step;
2127 tlv[3] = step;
2128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002129EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002130
2131/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002132static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002133find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002134{
2135 struct snd_ctl_elem_id id;
2136 memset(&id, 0, sizeof(id));
2137 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002138 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002139 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002140 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2141 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002142 strcpy(id.name, name);
2143 return snd_ctl_find_id(codec->bus->card, &id);
2144}
2145
Takashi Iwaid5191e52009-11-16 14:58:17 +01002146/**
2147 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2148 * @codec: HD-audio codec
2149 * @name: ctl id name string
2150 *
2151 * Get the control element with the given id string and IFACE_MIXER.
2152 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002153struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2154 const char *name)
2155{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002156 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002158EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002159
Takashi Iwaidcda5802012-10-12 17:24:51 +02002160static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2161 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002162{
2163 int idx;
2164 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002165 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002166 return idx;
2167 }
2168 return -EBUSY;
2169}
2170
Takashi Iwaid5191e52009-11-16 14:58:17 +01002171/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002172 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002173 * @codec: HD-audio codec
2174 * @nid: corresponding NID (optional)
2175 * @kctl: the control element to assign
2176 *
2177 * Add the given control element to an array inside the codec instance.
2178 * All control elements belonging to a codec are supposed to be added
2179 * by this function so that a proper clean-up works at the free or
2180 * reconfiguration time.
2181 *
2182 * If non-zero @nid is passed, the NID is assigned to the control element.
2183 * The assignment is shown in the codec proc file.
2184 *
2185 * snd_hda_ctl_add() checks the control subdev id field whether
2186 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002187 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2188 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002189 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002190int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2191 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002192{
2193 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002194 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002195 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002196
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002197 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002198 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002199 if (nid == 0)
2200 nid = get_amp_nid_(kctl->private_value);
2201 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002202 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2203 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002204 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002205 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206 err = snd_ctl_add(codec->bus->card, kctl);
2207 if (err < 0)
2208 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002209 item = snd_array_new(&codec->mixers);
2210 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002211 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002212 item->kctl = kctl;
2213 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002214 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002215 return 0;
2216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002217EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002218
Takashi Iwaid5191e52009-11-16 14:58:17 +01002219/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002220 * snd_hda_add_nid - Assign a NID to a control element
2221 * @codec: HD-audio codec
2222 * @nid: corresponding NID (optional)
2223 * @kctl: the control element to assign
2224 * @index: index to kctl
2225 *
2226 * Add the given control element to an array inside the codec instance.
2227 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2228 * NID:KCTL mapping - for example "Capture Source" selector.
2229 */
2230int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2231 unsigned int index, hda_nid_t nid)
2232{
2233 struct hda_nid_item *item;
2234
2235 if (nid > 0) {
2236 item = snd_array_new(&codec->nids);
2237 if (!item)
2238 return -ENOMEM;
2239 item->kctl = kctl;
2240 item->index = index;
2241 item->nid = nid;
2242 return 0;
2243 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002244 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2245 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002246 return -EINVAL;
2247}
2248EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2249
2250/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002251 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2252 * @codec: HD-audio codec
2253 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002254void snd_hda_ctls_clear(struct hda_codec *codec)
2255{
2256 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002257 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002258 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002259 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002260 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002261 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002262}
2263
Takashi Iwaia65d6292009-02-23 16:57:04 +01002264/* pseudo device locking
2265 * toggle card->shutdown to allow/disallow the device access (as a hack)
2266 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002267int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002268{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002269 struct snd_card *card = bus->card;
2270 struct hda_codec *codec;
2271
Takashi Iwaia65d6292009-02-23 16:57:04 +01002272 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002273 if (card->shutdown)
2274 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002275 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002276 if (!list_empty(&card->ctl_files))
2277 goto err_clear;
2278
2279 list_for_each_entry(codec, &bus->codec_list, list) {
2280 int pcm;
2281 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2282 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2283 if (!cpcm->pcm)
2284 continue;
2285 if (cpcm->pcm->streams[0].substream_opened ||
2286 cpcm->pcm->streams[1].substream_opened)
2287 goto err_clear;
2288 }
2289 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002290 spin_unlock(&card->files_lock);
2291 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002292
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002293 err_clear:
2294 card->shutdown = 0;
2295 err_unlock:
2296 spin_unlock(&card->files_lock);
2297 return -EINVAL;
2298}
2299EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2300
2301void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002302{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002303 struct snd_card *card = bus->card;
2304
2305 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002306 spin_lock(&card->files_lock);
2307 card->shutdown = 0;
2308 spin_unlock(&card->files_lock);
2309}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002310EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002311
Takashi Iwaid5191e52009-11-16 14:58:17 +01002312/**
2313 * snd_hda_codec_reset - Clear all objects assigned to the codec
2314 * @codec: HD-audio codec
2315 *
2316 * This frees the all PCM and control elements assigned to the codec, and
2317 * clears the caches and restores the pin default configurations.
2318 *
2319 * When a device is being used, it returns -EBSY. If successfully freed,
2320 * returns zero.
2321 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002322int snd_hda_codec_reset(struct hda_codec *codec)
2323{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002324 struct hda_bus *bus = codec->bus;
2325 struct snd_card *card = bus->card;
2326 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002327
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002328 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002329 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002330
2331 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002332 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002333#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002334 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002335 codec->power_on = 0;
2336 codec->power_transition = 0;
2337 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002338 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002339#endif
2340 snd_hda_ctls_clear(codec);
2341 /* relase PCMs */
2342 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002343 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002344 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002345 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002346 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002347 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002348 }
2349 if (codec->patch_ops.free)
2350 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002351 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002352 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002353 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002354 codec->spec = NULL;
2355 free_hda_cache(&codec->amp_cache);
2356 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002357 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2358 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002359 /* free only driver_pins so that init_pins + user_pins are restored */
2360 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002361 snd_array_free(&codec->cvt_setups);
2362 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002363 codec->num_pcms = 0;
2364 codec->pcm_info = NULL;
2365 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002366 codec->slave_dig_outs = NULL;
2367 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002368 module_put(codec->owner);
2369 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002370
2371 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002372 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002373 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002374}
2375
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002376typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2377
2378/* apply the function to all matching slave ctls in the mixer list */
2379static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002380 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002381{
2382 struct hda_nid_item *items;
2383 const char * const *s;
2384 int i, err;
2385
2386 items = codec->mixers.list;
2387 for (i = 0; i < codec->mixers.used; i++) {
2388 struct snd_kcontrol *sctl = items[i].kctl;
2389 if (!sctl || !sctl->id.name ||
2390 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2391 continue;
2392 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002393 char tmpname[sizeof(sctl->id.name)];
2394 const char *name = *s;
2395 if (suffix) {
2396 snprintf(tmpname, sizeof(tmpname), "%s %s",
2397 name, suffix);
2398 name = tmpname;
2399 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002400 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002401 err = func(data, sctl);
2402 if (err)
2403 return err;
2404 break;
2405 }
2406 }
2407 }
2408 return 0;
2409}
2410
2411static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2412{
2413 return 1;
2414}
2415
Takashi Iwai18478e82012-03-09 17:51:10 +01002416/* guess the value corresponding to 0dB */
2417static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2418{
2419 int _tlv[4];
2420 const int *tlv = NULL;
2421 int val = -1;
2422
2423 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2424 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2425 mm_segment_t fs = get_fs();
2426 set_fs(get_ds());
2427 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2428 tlv = _tlv;
2429 set_fs(fs);
2430 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2431 tlv = kctl->tlv.p;
2432 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2433 val = -tlv[2] / tlv[3];
2434 return val;
2435}
2436
2437/* call kctl->put with the given value(s) */
2438static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2439{
2440 struct snd_ctl_elem_value *ucontrol;
2441 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2442 if (!ucontrol)
2443 return -ENOMEM;
2444 ucontrol->value.integer.value[0] = val;
2445 ucontrol->value.integer.value[1] = val;
2446 kctl->put(kctl, ucontrol);
2447 kfree(ucontrol);
2448 return 0;
2449}
2450
2451/* initialize the slave volume with 0dB */
2452static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2453{
2454 int offset = get_kctl_0dB_offset(slave);
2455 if (offset > 0)
2456 put_kctl_with_value(slave, offset);
2457 return 0;
2458}
2459
2460/* unmute the slave */
2461static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2462{
2463 return put_kctl_with_value(slave, 1);
2464}
2465
Takashi Iwaid5191e52009-11-16 14:58:17 +01002466/**
2467 * snd_hda_add_vmaster - create a virtual master control and add slaves
2468 * @codec: HD-audio codec
2469 * @name: vmaster control name
2470 * @tlv: TLV data (optional)
2471 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002472 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002473 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002474 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002475 *
2476 * Create a virtual master control with the given name. The TLV data
2477 * must be either NULL or a valid data.
2478 *
2479 * @slaves is a NULL-terminated array of strings, each of which is a
2480 * slave control name. All controls with these names are assigned to
2481 * the new virtual master control.
2482 *
2483 * This function returns zero if successful or a negative error code.
2484 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002485int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002486 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002487 const char *suffix, bool init_slave_vol,
2488 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002489{
2490 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002491 int err;
2492
Takashi Iwai29e58532012-03-12 12:25:03 +01002493 if (ctl_ret)
2494 *ctl_ret = NULL;
2495
Takashi Iwai9322ca52012-02-03 14:28:01 +01002496 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002497 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002498 snd_printdd("No slave found for %s\n", name);
2499 return 0;
2500 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002501 kctl = snd_ctl_make_virtual_master(name, tlv);
2502 if (!kctl)
2503 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002504 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002505 if (err < 0)
2506 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002507
Takashi Iwai9322ca52012-02-03 14:28:01 +01002508 err = map_slaves(codec, slaves, suffix,
2509 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002510 if (err < 0)
2511 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002512
2513 /* init with master mute & zero volume */
2514 put_kctl_with_value(kctl, 0);
2515 if (init_slave_vol)
2516 map_slaves(codec, slaves, suffix,
2517 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2518
Takashi Iwai29e58532012-03-12 12:25:03 +01002519 if (ctl_ret)
2520 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002521 return 0;
2522}
Takashi Iwai18478e82012-03-09 17:51:10 +01002523EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002524
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002525/*
2526 * mute-LED control using vmaster
2527 */
2528static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_info *uinfo)
2530{
2531 static const char * const texts[] = {
2532 "Off", "On", "Follow Master"
2533 };
2534 unsigned int index;
2535
2536 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2537 uinfo->count = 1;
2538 uinfo->value.enumerated.items = 3;
2539 index = uinfo->value.enumerated.item;
2540 if (index >= 3)
2541 index = 2;
2542 strcpy(uinfo->value.enumerated.name, texts[index]);
2543 return 0;
2544}
2545
2546static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
2549 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2550 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2551 return 0;
2552}
2553
2554static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
2557 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2558 unsigned int old_mode = hook->mute_mode;
2559
2560 hook->mute_mode = ucontrol->value.enumerated.item[0];
2561 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2562 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2563 if (old_mode == hook->mute_mode)
2564 return 0;
2565 snd_hda_sync_vmaster_hook(hook);
2566 return 1;
2567}
2568
2569static struct snd_kcontrol_new vmaster_mute_mode = {
2570 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2571 .name = "Mute-LED Mode",
2572 .info = vmaster_mute_mode_info,
2573 .get = vmaster_mute_mode_get,
2574 .put = vmaster_mute_mode_put,
2575};
2576
2577/*
2578 * Add a mute-LED hook with the given vmaster switch kctl
2579 * "Mute-LED Mode" control is automatically created and associated with
2580 * the given hook.
2581 */
2582int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002583 struct hda_vmaster_mute_hook *hook,
2584 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002585{
2586 struct snd_kcontrol *kctl;
2587
2588 if (!hook->hook || !hook->sw_kctl)
2589 return 0;
2590 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2591 hook->codec = codec;
2592 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002593 if (!expose_enum_ctl)
2594 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002595 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2596 if (!kctl)
2597 return -ENOMEM;
2598 return snd_hda_ctl_add(codec, 0, kctl);
2599}
2600EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2601
2602/*
2603 * Call the hook with the current value for synchronization
2604 * Should be called in init callback
2605 */
2606void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2607{
2608 if (!hook->hook || !hook->codec)
2609 return;
2610 switch (hook->mute_mode) {
2611 case HDA_VMUTE_FOLLOW_MASTER:
2612 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2613 break;
2614 default:
2615 hook->hook(hook->codec, hook->mute_mode);
2616 break;
2617 }
2618}
2619EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2620
2621
Takashi Iwaid5191e52009-11-16 14:58:17 +01002622/**
2623 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2624 *
2625 * The control element is supposed to have the private_value field
2626 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2627 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002628int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 int chs = get_amp_channels(kcontrol);
2632
2633 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2634 uinfo->count = chs == 3 ? 2 : 1;
2635 uinfo->value.integer.min = 0;
2636 uinfo->value.integer.max = 1;
2637 return 0;
2638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002639EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Takashi Iwaid5191e52009-11-16 14:58:17 +01002641/**
2642 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2643 *
2644 * The control element is supposed to have the private_value field
2645 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2646 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002647int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
2650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2651 hda_nid_t nid = get_amp_nid(kcontrol);
2652 int chs = get_amp_channels(kcontrol);
2653 int dir = get_amp_direction(kcontrol);
2654 int idx = get_amp_index(kcontrol);
2655 long *valp = ucontrol->value.integer.value;
2656
2657 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002658 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002659 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002662 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return 0;
2664}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002665EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Takashi Iwaid5191e52009-11-16 14:58:17 +01002667/**
2668 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2669 *
2670 * The control element is supposed to have the private_value field
2671 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2672 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002673int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 hda_nid_t nid = get_amp_nid(kcontrol);
2678 int chs = get_amp_channels(kcontrol);
2679 int dir = get_amp_direction(kcontrol);
2680 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 long *valp = ucontrol->value.integer.value;
2682 int change = 0;
2683
Takashi Iwaicb53c622007-08-10 17:21:45 +02002684 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002685 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002686 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002687 HDA_AMP_MUTE,
2688 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002689 valp++;
2690 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002691 if (chs & 2)
2692 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002693 HDA_AMP_MUTE,
2694 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002695 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002696 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return change;
2698}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002699EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701/*
Takashi Iwai985be542005-11-02 18:26:49 +01002702 * bound volume controls
2703 *
2704 * bind multiple volumes (# indices, from 0)
2705 */
2706
2707#define AMP_VAL_IDX_SHIFT 19
2708#define AMP_VAL_IDX_MASK (0x0f<<19)
2709
Takashi Iwaid5191e52009-11-16 14:58:17 +01002710/**
2711 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2712 *
2713 * The control element is supposed to have the private_value field
2714 * set up via HDA_BIND_MUTE*() macros.
2715 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002718{
2719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2720 unsigned long pval;
2721 int err;
2722
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002723 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002724 pval = kcontrol->private_value;
2725 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2726 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2727 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002728 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002729 return err;
2730}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002731EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002732
Takashi Iwaid5191e52009-11-16 14:58:17 +01002733/**
2734 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2735 *
2736 * The control element is supposed to have the private_value field
2737 * set up via HDA_BIND_MUTE*() macros.
2738 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002739int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002741{
2742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2743 unsigned long pval;
2744 int i, indices, err = 0, change = 0;
2745
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002746 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002747 pval = kcontrol->private_value;
2748 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2749 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002750 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2751 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002752 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2753 if (err < 0)
2754 break;
2755 change |= err;
2756 }
2757 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002758 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002759 return err < 0 ? err : change;
2760}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002761EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002762
Takashi Iwaid5191e52009-11-16 14:58:17 +01002763/**
2764 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2765 *
2766 * The control element is supposed to have the private_value field
2767 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002768 */
2769int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_info *uinfo)
2771{
2772 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2773 struct hda_bind_ctls *c;
2774 int err;
2775
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002776 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002777 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002778 kcontrol->private_value = *c->values;
2779 err = c->ops->info(kcontrol, uinfo);
2780 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002781 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002782 return err;
2783}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002784EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002785
Takashi Iwaid5191e52009-11-16 14:58:17 +01002786/**
2787 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2788 *
2789 * The control element is supposed to have the private_value field
2790 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2791 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002792int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
2795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2796 struct hda_bind_ctls *c;
2797 int err;
2798
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002799 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002800 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002801 kcontrol->private_value = *c->values;
2802 err = c->ops->get(kcontrol, ucontrol);
2803 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002804 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002805 return err;
2806}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002807EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002808
Takashi Iwaid5191e52009-11-16 14:58:17 +01002809/**
2810 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2811 *
2812 * The control element is supposed to have the private_value field
2813 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2814 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002815int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2816 struct snd_ctl_elem_value *ucontrol)
2817{
2818 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2819 struct hda_bind_ctls *c;
2820 unsigned long *vals;
2821 int err = 0, change = 0;
2822
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002823 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002824 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002825 for (vals = c->values; *vals; vals++) {
2826 kcontrol->private_value = *vals;
2827 err = c->ops->put(kcontrol, ucontrol);
2828 if (err < 0)
2829 break;
2830 change |= err;
2831 }
2832 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002833 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002834 return err < 0 ? err : change;
2835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002836EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002837
Takashi Iwaid5191e52009-11-16 14:58:17 +01002838/**
2839 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2840 *
2841 * The control element is supposed to have the private_value field
2842 * set up via HDA_BIND_VOL() macro.
2843 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002844int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2845 unsigned int size, unsigned int __user *tlv)
2846{
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2848 struct hda_bind_ctls *c;
2849 int err;
2850
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002851 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002852 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002853 kcontrol->private_value = *c->values;
2854 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2855 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002856 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002857 return err;
2858}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002859EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002860
2861struct hda_ctl_ops snd_hda_bind_vol = {
2862 .info = snd_hda_mixer_amp_volume_info,
2863 .get = snd_hda_mixer_amp_volume_get,
2864 .put = snd_hda_mixer_amp_volume_put,
2865 .tlv = snd_hda_mixer_amp_tlv
2866};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002867EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002868
2869struct hda_ctl_ops snd_hda_bind_sw = {
2870 .info = snd_hda_mixer_amp_switch_info,
2871 .get = snd_hda_mixer_amp_switch_get,
2872 .put = snd_hda_mixer_amp_switch_put,
2873 .tlv = snd_hda_mixer_amp_tlv
2874};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002875EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002876
2877/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 * SPDIF out controls
2879 */
2880
Takashi Iwai0ba21762007-04-16 11:29:14 +02002881static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2882 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2885 uinfo->count = 1;
2886 return 0;
2887}
2888
Takashi Iwai0ba21762007-04-16 11:29:14 +02002889static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2890 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
2892 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2893 IEC958_AES0_NONAUDIO |
2894 IEC958_AES0_CON_EMPHASIS_5015 |
2895 IEC958_AES0_CON_NOT_COPYRIGHT;
2896 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2897 IEC958_AES1_CON_ORIGINAL;
2898 return 0;
2899}
2900
Takashi Iwai0ba21762007-04-16 11:29:14 +02002901static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2905 IEC958_AES0_NONAUDIO |
2906 IEC958_AES0_PRO_EMPHASIS_5015;
2907 return 0;
2908}
2909
Takashi Iwai0ba21762007-04-16 11:29:14 +02002910static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002914 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002915 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002917 mutex_lock(&codec->spdif_mutex);
2918 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002919 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2920 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2921 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2922 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002923 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 return 0;
2926}
2927
2928/* convert from SPDIF status bits to HDA SPDIF bits
2929 * bit 0 (DigEn) is always set zero (to be filled later)
2930 */
2931static unsigned short convert_from_spdif_status(unsigned int sbits)
2932{
2933 unsigned short val = 0;
2934
2935 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002936 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002940 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2941 IEC958_AES0_PRO_EMPHASIS_5015)
2942 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002944 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2945 IEC958_AES0_CON_EMPHASIS_5015)
2946 val |= AC_DIG1_EMPHASIS;
2947 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2948 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2952 }
2953 return val;
2954}
2955
2956/* convert to SPDIF status bits from HDA SPDIF bits
2957 */
2958static unsigned int convert_to_spdif_status(unsigned short val)
2959{
2960 unsigned int sbits = 0;
2961
Takashi Iwai0ba21762007-04-16 11:29:14 +02002962 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002964 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 sbits |= IEC958_AES0_PROFESSIONAL;
2966 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002967 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2969 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002970 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002972 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2976 sbits |= val & (0x7f << 8);
2977 }
2978 return sbits;
2979}
2980
Takashi Iwai2f728532008-09-25 16:32:41 +02002981/* set digital convert verbs both for the given NID and its slaves */
2982static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2983 int verb, int val)
2984{
Takashi Iwaidda14412011-05-02 11:29:30 +02002985 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002986
Takashi Iwai9e976972008-11-25 08:17:20 +01002987 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002988 d = codec->slave_dig_outs;
2989 if (!d)
2990 return;
2991 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002992 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002993}
2994
2995static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2996 int dig1, int dig2)
2997{
2998 if (dig1 != -1)
2999 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3000 if (dig2 != -1)
3001 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3002}
3003
Takashi Iwai0ba21762007-04-16 11:29:14 +02003004static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003008 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003009 struct hda_spdif_out *spdif;
3010 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 unsigned short val;
3012 int change;
3013
Ingo Molnar62932df2006-01-16 16:34:20 +01003014 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003015 spdif = snd_array_elem(&codec->spdif_out, idx);
3016 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003017 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3019 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3020 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003021 val = convert_from_spdif_status(spdif->status);
3022 val |= spdif->ctls & 1;
3023 change = spdif->ctls != val;
3024 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003025 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003026 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003027 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return change;
3029}
3030
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003031#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3034 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003037 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003038 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003040 mutex_lock(&codec->spdif_mutex);
3041 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003042 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003043 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045}
3046
Stephen Warren74b654c2011-06-01 11:14:18 -06003047static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3048 int dig1, int dig2)
3049{
3050 set_dig_out_convert(codec, nid, dig1, dig2);
3051 /* unmute amp switch (if any) */
3052 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3053 (dig1 & AC_DIG1_ENABLE))
3054 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3055 HDA_AMP_MUTE, 0);
3056}
3057
Takashi Iwai0ba21762007-04-16 11:29:14 +02003058static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3059 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003062 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003063 struct hda_spdif_out *spdif;
3064 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 unsigned short val;
3066 int change;
3067
Ingo Molnar62932df2006-01-16 16:34:20 +01003068 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003069 spdif = snd_array_elem(&codec->spdif_out, idx);
3070 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003071 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003073 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003074 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003075 spdif->ctls = val;
3076 if (change && nid != (u16)-1)
3077 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003078 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return change;
3080}
3081
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003082static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 {
3084 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003086 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 .info = snd_hda_spdif_mask_info,
3088 .get = snd_hda_spdif_cmask_get,
3089 },
3090 {
3091 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003093 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 .info = snd_hda_spdif_mask_info,
3095 .get = snd_hda_spdif_pmask_get,
3096 },
3097 {
3098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003099 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 .info = snd_hda_spdif_mask_info,
3101 .get = snd_hda_spdif_default_get,
3102 .put = snd_hda_spdif_default_put,
3103 },
3104 {
3105 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003106 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 .info = snd_hda_spdif_out_switch_info,
3108 .get = snd_hda_spdif_out_switch_get,
3109 .put = snd_hda_spdif_out_switch_put,
3110 },
3111 { } /* end */
3112};
3113
3114/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003115 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003117 * @associated_nid: NID that new ctls associated with
3118 * @cvt_nid: converter NID
3119 * @type: HDA_PCM_TYPE_*
3120 * Creates controls related with the digital output.
3121 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 *
3123 * Returns 0 if successful, or a negative error code.
3124 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003125int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3126 hda_nid_t associated_nid,
3127 hda_nid_t cvt_nid,
3128 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003131 struct snd_kcontrol *kctl;
3132 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003133 int idx, dev = 0;
3134 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003135 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Takashi Iwaidcda5802012-10-12 17:24:51 +02003137 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3138 type == HDA_PCM_TYPE_SPDIF) {
3139 dev = spdif_pcm_dev;
3140 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3141 type == HDA_PCM_TYPE_HDMI) {
3142 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3143 spdif = snd_array_elem(&codec->spdif_out, idx);
3144 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3145 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3146 if (!kctl)
3147 break;
3148 kctl->id.device = spdif_pcm_dev;
3149 }
3150 }
3151 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3152 }
3153 if (!codec->primary_dig_out_type)
3154 codec->primary_dig_out_type = type;
3155
3156 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003157 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003158 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3159 return -EBUSY;
3160 }
Stephen Warren7c935972011-06-01 11:14:17 -06003161 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3163 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003164 if (!kctl)
3165 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003166 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003167 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003168 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003169 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003170 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return err;
3172 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003173 spdif->nid = cvt_nid;
3174 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003175 AC_VERB_GET_DIGI_CONVERT_1, 0);
3176 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return 0;
3178}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003179EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003181/* get the hda_spdif_out entry from the given NID
3182 * call within spdif_mutex lock
3183 */
Stephen Warren7c935972011-06-01 11:14:17 -06003184struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3185 hda_nid_t nid)
3186{
3187 int i;
3188 for (i = 0; i < codec->spdif_out.used; i++) {
3189 struct hda_spdif_out *spdif =
3190 snd_array_elem(&codec->spdif_out, i);
3191 if (spdif->nid == nid)
3192 return spdif;
3193 }
3194 return NULL;
3195}
3196EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3197
Stephen Warren74b654c2011-06-01 11:14:18 -06003198void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3199{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003200 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003201
3202 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003203 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003204 spdif->nid = (u16)-1;
3205 mutex_unlock(&codec->spdif_mutex);
3206}
3207EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3208
3209void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3210{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003211 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003212 unsigned short val;
3213
3214 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003215 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003216 if (spdif->nid != nid) {
3217 spdif->nid = nid;
3218 val = spdif->ctls;
3219 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3220 }
3221 mutex_unlock(&codec->spdif_mutex);
3222}
3223EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003226 * SPDIF sharing with analog output
3227 */
3228static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3229 struct snd_ctl_elem_value *ucontrol)
3230{
3231 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3232 ucontrol->value.integer.value[0] = mout->share_spdif;
3233 return 0;
3234}
3235
3236static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3237 struct snd_ctl_elem_value *ucontrol)
3238{
3239 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3240 mout->share_spdif = !!ucontrol->value.integer.value[0];
3241 return 0;
3242}
3243
3244static struct snd_kcontrol_new spdif_share_sw = {
3245 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3246 .name = "IEC958 Default PCM Playback Switch",
3247 .info = snd_ctl_boolean_mono_info,
3248 .get = spdif_share_sw_get,
3249 .put = spdif_share_sw_put,
3250};
3251
Takashi Iwaid5191e52009-11-16 14:58:17 +01003252/**
3253 * snd_hda_create_spdif_share_sw - create Default PCM switch
3254 * @codec: the HDA codec
3255 * @mout: multi-out instance
3256 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003257int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3258 struct hda_multi_out *mout)
3259{
3260 if (!mout->dig_out_nid)
3261 return 0;
3262 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003263 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3264 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003265}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003266EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003267
3268/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 * SPDIF input
3270 */
3271
3272#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3273
Takashi Iwai0ba21762007-04-16 11:29:14 +02003274static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278
3279 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3280 return 0;
3281}
3282
Takashi Iwai0ba21762007-04-16 11:29:14 +02003283static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3284 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3287 hda_nid_t nid = kcontrol->private_value;
3288 unsigned int val = !!ucontrol->value.integer.value[0];
3289 int change;
3290
Ingo Molnar62932df2006-01-16 16:34:20 +01003291 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003293 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003295 snd_hda_codec_write_cache(codec, nid, 0,
3296 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003298 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return change;
3300}
3301
Takashi Iwai0ba21762007-04-16 11:29:14 +02003302static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3303 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3306 hda_nid_t nid = kcontrol->private_value;
3307 unsigned short val;
3308 unsigned int sbits;
3309
Andrew Paprocki3982d172007-12-19 12:13:44 +01003310 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 sbits = convert_to_spdif_status(val);
3312 ucontrol->value.iec958.status[0] = sbits;
3313 ucontrol->value.iec958.status[1] = sbits >> 8;
3314 ucontrol->value.iec958.status[2] = sbits >> 16;
3315 ucontrol->value.iec958.status[3] = sbits >> 24;
3316 return 0;
3317}
3318
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003319static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 {
3321 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003322 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 .info = snd_hda_spdif_in_switch_info,
3324 .get = snd_hda_spdif_in_switch_get,
3325 .put = snd_hda_spdif_in_switch_put,
3326 },
3327 {
3328 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3329 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003330 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 .info = snd_hda_spdif_mask_info,
3332 .get = snd_hda_spdif_in_status_get,
3333 },
3334 { } /* end */
3335};
3336
3337/**
3338 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3339 * @codec: the HDA codec
3340 * @nid: audio in widget NID
3341 *
3342 * Creates controls related with the SPDIF input.
3343 * Called from each patch supporting the SPDIF in.
3344 *
3345 * Returns 0 if successful, or a negative error code.
3346 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003347int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003350 struct snd_kcontrol *kctl;
3351 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003352 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Takashi Iwaidcda5802012-10-12 17:24:51 +02003354 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003355 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003356 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3357 return -EBUSY;
3358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3360 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003361 if (!kctl)
3362 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003364 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003365 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return err;
3367 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003368 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003369 snd_hda_codec_read(codec, nid, 0,
3370 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003371 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return 0;
3373}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003374EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Takashi Iwai2a439522011-07-26 09:52:50 +02003376#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003377/*
3378 * command cache
3379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003381/* build a 32bit cache key with the widget id and the command parameter */
3382#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3383#define get_cmd_cache_nid(key) ((key) & 0xff)
3384#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3385
3386/**
3387 * snd_hda_codec_write_cache - send a single command with caching
3388 * @codec: the HDA codec
3389 * @nid: NID to send the command
3390 * @direct: direct flag
3391 * @verb: the verb to send
3392 * @parm: the parameter for the verb
3393 *
3394 * Send a single command without waiting for response.
3395 *
3396 * Returns 0 if successful, or a negative error code.
3397 */
3398int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3399 int direct, unsigned int verb, unsigned int parm)
3400{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003401 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3402 struct hda_cache_head *c;
3403 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003404
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003405 if (err < 0)
3406 return err;
3407 /* parm may contain the verb stuff for get/set amp */
3408 verb = verb | (parm >> 8);
3409 parm &= 0xff;
3410 key = build_cmd_cache_key(nid, verb);
3411 mutex_lock(&codec->bus->cmd_mutex);
3412 c = get_alloc_hash(&codec->cmd_cache, key);
3413 if (c)
3414 c->val = parm;
3415 mutex_unlock(&codec->bus->cmd_mutex);
3416 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003417}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003418EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003419
Takashi Iwaid5191e52009-11-16 14:58:17 +01003420/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003421 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3422 * @codec: the HDA codec
3423 * @nid: NID to send the command
3424 * @direct: direct flag
3425 * @verb: the verb to send
3426 * @parm: the parameter for the verb
3427 *
3428 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3429 * command if the parameter is already identical with the cached value.
3430 * If not, it sends the command and refreshes the cache.
3431 *
3432 * Returns 0 if successful, or a negative error code.
3433 */
3434int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3435 int direct, unsigned int verb, unsigned int parm)
3436{
3437 struct hda_cache_head *c;
3438 u32 key;
3439
3440 /* parm may contain the verb stuff for get/set amp */
3441 verb = verb | (parm >> 8);
3442 parm &= 0xff;
3443 key = build_cmd_cache_key(nid, verb);
3444 mutex_lock(&codec->bus->cmd_mutex);
3445 c = get_hash(&codec->cmd_cache, key);
3446 if (c && c->val == parm) {
3447 mutex_unlock(&codec->bus->cmd_mutex);
3448 return 0;
3449 }
3450 mutex_unlock(&codec->bus->cmd_mutex);
3451 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3452}
3453EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3454
3455/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003456 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3457 * @codec: HD-audio codec
3458 *
3459 * Execute all verbs recorded in the command caches to resume.
3460 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003461void snd_hda_codec_resume_cache(struct hda_codec *codec)
3462{
Takashi Iwai603c4012008-07-30 15:01:44 +02003463 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003464 int i;
3465
Takashi Iwai603c4012008-07-30 15:01:44 +02003466 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003467 u32 key = buffer->key;
3468 if (!key)
3469 continue;
3470 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3471 get_cmd_cache_cmd(key), buffer->val);
3472 }
3473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003474EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003475
3476/**
3477 * snd_hda_sequence_write_cache - sequence writes with caching
3478 * @codec: the HDA codec
3479 * @seq: VERB array to send
3480 *
3481 * Send the commands sequentially from the given array.
3482 * Thte commands are recorded on cache for power-save and resume.
3483 * The array must be terminated with NID=0.
3484 */
3485void snd_hda_sequence_write_cache(struct hda_codec *codec,
3486 const struct hda_verb *seq)
3487{
3488 for (; seq->nid; seq++)
3489 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3490 seq->param);
3491}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003492EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003493#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003494
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003495void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3496 unsigned int power_state,
3497 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003498{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003499 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003500 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003501
Takashi Iwaicb53c622007-08-10 17:21:45 +02003502 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003503 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003504 if (!(wcaps & AC_WCAP_POWER))
3505 continue;
3506 /* don't power down the widget if it controls eapd and
3507 * EAPD_BTLENABLE is set.
3508 */
3509 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3510 get_wcaps_type(wcaps) == AC_WID_PIN &&
3511 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3512 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003513 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003514 if (eapd & 0x02)
3515 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003516 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003517 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3518 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003519 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003520}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003521EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3522
3523/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003524 * supported power states check
3525 */
3526static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3527 unsigned int power_state)
3528{
3529 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3530
Mengdong Line037cb42012-08-10 14:11:58 +02003531 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003532 return false;
3533 if (sup & power_state)
3534 return true;
3535 else
3536 return false;
3537}
3538
3539/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003540 * wait until the state is reached, returns the current state
3541 */
3542static unsigned int hda_sync_power_state(struct hda_codec *codec,
3543 hda_nid_t fg,
3544 unsigned int power_state)
3545{
3546 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3547 unsigned int state, actual_state;
3548
3549 for (;;) {
3550 state = snd_hda_codec_read(codec, fg, 0,
3551 AC_VERB_GET_POWER_STATE, 0);
3552 if (state & AC_PWRST_ERROR)
3553 break;
3554 actual_state = (state >> 4) & 0x0f;
3555 if (actual_state == power_state)
3556 break;
3557 if (time_after_eq(jiffies, end_time))
3558 break;
3559 /* wait until the codec reachs to the target state */
3560 msleep(1);
3561 }
3562 return state;
3563}
3564
3565/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003566 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003567 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003568static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003569 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003570{
Takashi Iwaid8193872012-08-31 07:54:38 -07003571 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003572 int count;
3573 unsigned int state;
3574
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003575 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003576 if (power_state == AC_PWRST_D3) {
3577 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003578 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003579 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003580
3581 /* repeat power states setting at most 10 times*/
3582 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003583 if (codec->patch_ops.set_power_state)
3584 codec->patch_ops.set_power_state(codec, fg,
3585 power_state);
3586 else {
3587 snd_hda_codec_read(codec, fg, 0,
3588 AC_VERB_SET_POWER_STATE,
3589 power_state);
3590 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3591 true);
3592 }
3593 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003594 if (!(state & AC_PWRST_ERROR))
3595 break;
3596 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003597
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003598 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003599}
Takashi Iwai54d17402005-11-21 16:33:22 +01003600
Takashi Iwai11aeff02008-07-30 15:01:46 +02003601#ifdef CONFIG_SND_HDA_HWDEP
3602/* execute additional init verbs */
3603static void hda_exec_init_verbs(struct hda_codec *codec)
3604{
3605 if (codec->init_verbs.list)
3606 snd_hda_sequence_write(codec, codec->init_verbs.list);
3607}
3608#else
3609static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3610#endif
3611
Takashi Iwai2a439522011-07-26 09:52:50 +02003612#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003613/*
3614 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003615 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003616 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003617static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003618{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003619 unsigned int state;
3620
Takashi Iwai989c3182012-11-19 14:14:58 +01003621 codec->in_pm = 1;
3622
Takashi Iwaicb53c622007-08-10 17:21:45 +02003623 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003624 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003625 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003626 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003627 /* Cancel delayed work if we aren't currently running from it. */
3628 if (!in_wq)
3629 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003630 spin_lock(&codec->power_lock);
3631 snd_hda_update_power_acct(codec);
3632 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003633 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003634 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003635 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003636 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003637 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003638 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003639}
3640
3641/*
3642 * kick up codec; used both from PM and power-save
3643 */
3644static void hda_call_codec_resume(struct hda_codec *codec)
3645{
Takashi Iwai989c3182012-11-19 14:14:58 +01003646 codec->in_pm = 1;
3647
Takashi Iwai7f308302012-05-08 16:52:23 +02003648 /* set as if powered on for avoiding re-entering the resume
3649 * in the resume / power-save sequence
3650 */
3651 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003652 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003653 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003654 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003655 if (codec->patch_ops.resume)
3656 codec->patch_ops.resume(codec);
3657 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003658 if (codec->patch_ops.init)
3659 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003660 snd_hda_codec_resume_amp(codec);
3661 snd_hda_codec_resume_cache(codec);
3662 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003663
3664 if (codec->jackpoll_interval)
3665 hda_jackpoll_work(&codec->jackpoll_work.work);
3666 else {
3667 snd_hda_jack_set_dirty_all(codec);
3668 snd_hda_jack_report_sync(codec);
3669 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003670
3671 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003672 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003673}
Takashi Iwai2a439522011-07-26 09:52:50 +02003674#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675
Takashi Iwai54d17402005-11-21 16:33:22 +01003676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677/**
3678 * snd_hda_build_controls - build mixer controls
3679 * @bus: the BUS
3680 *
3681 * Creates mixer controls for each codec included in the bus.
3682 *
3683 * Returns 0 if successful, otherwise a negative error code.
3684 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003685int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003687 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Takashi Iwai0ba21762007-04-16 11:29:14 +02003689 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003690 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003691 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003692 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003693 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003694 err = snd_hda_codec_reset(codec);
3695 if (err < 0) {
3696 printk(KERN_ERR
3697 "hda_codec: cannot revert codec\n");
3698 return err;
3699 }
3700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003702 return 0;
3703}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003704EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003706/*
3707 * add standard channel maps if not specified
3708 */
3709static int add_std_chmaps(struct hda_codec *codec)
3710{
3711 int i, str, err;
3712
3713 for (i = 0; i < codec->num_pcms; i++) {
3714 for (str = 0; str < 2; str++) {
3715 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3716 struct hda_pcm_stream *hinfo =
3717 &codec->pcm_info[i].stream[str];
3718 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003719 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003720
3721 if (codec->pcm_info[i].own_chmap)
3722 continue;
3723 if (!pcm || !hinfo->substreams)
3724 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003725 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3726 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003727 hinfo->channels_max,
3728 0, &chmap);
3729 if (err < 0)
3730 return err;
3731 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3732 }
3733 }
3734 return 0;
3735}
3736
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003737/* default channel maps for 2.1 speakers;
3738 * since HD-audio supports only stereo, odd number channels are omitted
3739 */
3740const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3741 { .channels = 2,
3742 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3743 { .channels = 4,
3744 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3745 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3746 { }
3747};
3748EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3749
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003750int snd_hda_codec_build_controls(struct hda_codec *codec)
3751{
3752 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003753 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003754 /* continue to initialize... */
3755 if (codec->patch_ops.init)
3756 err = codec->patch_ops.init(codec);
3757 if (!err && codec->patch_ops.build_controls)
3758 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003759 if (err < 0)
3760 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003761
3762 /* we create chmaps here instead of build_pcms */
3763 err = add_std_chmaps(codec);
3764 if (err < 0)
3765 return err;
3766
David Henningsson26a6cb62012-10-09 15:04:21 +02003767 if (codec->jackpoll_interval)
3768 hda_jackpoll_work(&codec->jackpoll_work.work);
3769 else
3770 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return 0;
3772}
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774/*
3775 * stream formats
3776 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003777struct hda_rate_tbl {
3778 unsigned int hz;
3779 unsigned int alsa_bits;
3780 unsigned int hda_fmt;
3781};
3782
Takashi Iwai92f10b32010-08-03 14:21:00 +02003783/* rate = base * mult / div */
3784#define HDA_RATE(base, mult, div) \
3785 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3786 (((div) - 1) << AC_FMT_DIV_SHIFT))
3787
Takashi Iwaibefdf312005-08-22 13:57:55 +02003788static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003790
3791 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003792 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3793 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3794 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3795 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3796 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3797 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3798 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3799 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3800 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3801 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3802 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003803#define AC_PAR_PCM_RATE_BITS 11
3804 /* up to bits 10, 384kHZ isn't supported properly */
3805
3806 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003807 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003808
Takashi Iwaibefdf312005-08-22 13:57:55 +02003809 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810};
3811
3812/**
3813 * snd_hda_calc_stream_format - calculate format bitset
3814 * @rate: the sample rate
3815 * @channels: the number of channels
3816 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3817 * @maxbps: the max. bps
3818 *
3819 * Calculate the format bitset from the given rate, channels and th PCM format.
3820 *
3821 * Return zero if invalid.
3822 */
3823unsigned int snd_hda_calc_stream_format(unsigned int rate,
3824 unsigned int channels,
3825 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003826 unsigned int maxbps,
3827 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
3829 int i;
3830 unsigned int val = 0;
3831
Takashi Iwaibefdf312005-08-22 13:57:55 +02003832 for (i = 0; rate_bits[i].hz; i++)
3833 if (rate_bits[i].hz == rate) {
3834 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 break;
3836 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003837 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 snd_printdd("invalid rate %d\n", rate);
3839 return 0;
3840 }
3841
3842 if (channels == 0 || channels > 8) {
3843 snd_printdd("invalid channels %d\n", channels);
3844 return 0;
3845 }
3846 val |= channels - 1;
3847
3848 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003849 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003850 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003851 break;
3852 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003853 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 case 20:
3856 case 24:
3857 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003858 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003859 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003861 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003863 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 break;
3865 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003866 snd_printdd("invalid format width %d\n",
3867 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return 0;
3869 }
3870
Anssi Hannula32c168c2010-08-03 13:28:57 +03003871 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003872 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return val;
3875}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003876EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003878static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3879 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003880{
3881 unsigned int val = 0;
3882 if (nid != codec->afg &&
3883 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3884 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3885 if (!val || val == -1)
3886 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3887 if (!val || val == -1)
3888 return 0;
3889 return val;
3890}
3891
3892static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3893{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003894 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003895 get_pcm_param);
3896}
3897
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003898static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3899 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003900{
3901 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3902 if (!streams || streams == -1)
3903 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3904 if (!streams || streams == -1)
3905 return 0;
3906 return streams;
3907}
3908
3909static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3910{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003911 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003912 get_stream_param);
3913}
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915/**
3916 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3917 * @codec: the HDA codec
3918 * @nid: NID to query
3919 * @ratesp: the pointer to store the detected rate bitflags
3920 * @formatsp: the pointer to store the detected formats
3921 * @bpsp: the pointer to store the detected format widths
3922 *
3923 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3924 * or @bsps argument is ignored.
3925 *
3926 * Returns 0 if successful, otherwise a negative error code.
3927 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003928int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3930{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003931 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003933 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003934 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 if (ratesp) {
3937 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003938 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003940 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003942 if (rates == 0) {
3943 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3944 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3945 nid, val,
3946 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3947 return -EIO;
3948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 *ratesp = rates;
3950 }
3951
3952 if (formatsp || bpsp) {
3953 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003954 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003956 streams = query_stream_param(codec, nid);
3957 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 bps = 0;
3961 if (streams & AC_SUPFMT_PCM) {
3962 if (val & AC_SUPPCM_BITS_8) {
3963 formats |= SNDRV_PCM_FMTBIT_U8;
3964 bps = 8;
3965 }
3966 if (val & AC_SUPPCM_BITS_16) {
3967 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3968 bps = 16;
3969 }
3970 if (wcaps & AC_WCAP_DIGITAL) {
3971 if (val & AC_SUPPCM_BITS_32)
3972 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3973 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3974 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3975 if (val & AC_SUPPCM_BITS_24)
3976 bps = 24;
3977 else if (val & AC_SUPPCM_BITS_20)
3978 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003979 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3980 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3982 if (val & AC_SUPPCM_BITS_32)
3983 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 else if (val & AC_SUPPCM_BITS_24)
3985 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003986 else if (val & AC_SUPPCM_BITS_20)
3987 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
3989 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003990#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003991 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003993 if (!bps)
3994 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003995 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003996#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003997 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003998 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 /* temporary hack: we have still no proper support
4000 * for the direct AC3 stream...
4001 */
4002 formats |= SNDRV_PCM_FMTBIT_U8;
4003 bps = 8;
4004 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004005 if (formats == 0) {
4006 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4007 "(nid=0x%x, val=0x%x, ovrd=%i, "
4008 "streams=0x%x)\n",
4009 nid, val,
4010 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4011 streams);
4012 return -EIO;
4013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (formatsp)
4015 *formatsp = formats;
4016 if (bpsp)
4017 *bpsp = bps;
4018 }
4019
4020 return 0;
4021}
Stephen Warren384a48d2011-06-01 11:14:21 -06004022EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004025 * snd_hda_is_supported_format - Check the validity of the format
4026 * @codec: HD-audio codec
4027 * @nid: NID to check
4028 * @format: the HD-audio format value to check
4029 *
4030 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 *
4032 * Returns 1 if supported, 0 if not.
4033 */
4034int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4035 unsigned int format)
4036{
4037 int i;
4038 unsigned int val = 0, rate, stream;
4039
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004040 val = query_pcm_param(codec, nid);
4041 if (!val)
4042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004045 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004046 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (val & (1 << i))
4048 break;
4049 return 0;
4050 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004051 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 return 0;
4053
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004054 stream = query_stream_param(codec, nid);
4055 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return 0;
4057
4058 if (stream & AC_SUPFMT_PCM) {
4059 switch (format & 0xf0) {
4060 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004061 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return 0;
4063 break;
4064 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004065 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return 0;
4067 break;
4068 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004069 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 return 0;
4071 break;
4072 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004073 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return 0;
4075 break;
4076 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004077 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return 0;
4079 break;
4080 default:
4081 return 0;
4082 }
4083 } else {
4084 /* FIXME: check for float32 and AC3? */
4085 }
4086
4087 return 1;
4088}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004089EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091/*
4092 * PCM stuff
4093 */
4094static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4095 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004096 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
4098 return 0;
4099}
4100
4101static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4102 struct hda_codec *codec,
4103 unsigned int stream_tag,
4104 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004105 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4108 return 0;
4109}
4110
4111static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4112 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004113 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Takashi Iwai888afa12008-03-18 09:57:50 +01004115 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 return 0;
4117}
4118
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004119static int set_pcm_default_values(struct hda_codec *codec,
4120 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004122 int err;
4123
Takashi Iwai0ba21762007-04-16 11:29:14 +02004124 /* query support PCM information from the given NID */
4125 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004126 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004127 info->rates ? NULL : &info->rates,
4128 info->formats ? NULL : &info->formats,
4129 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004130 if (err < 0)
4131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
4133 if (info->ops.open == NULL)
4134 info->ops.open = hda_pcm_default_open_close;
4135 if (info->ops.close == NULL)
4136 info->ops.close = hda_pcm_default_open_close;
4137 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004138 if (snd_BUG_ON(!info->nid))
4139 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 info->ops.prepare = hda_pcm_default_prepare;
4141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004143 if (snd_BUG_ON(!info->nid))
4144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 info->ops.cleanup = hda_pcm_default_cleanup;
4146 }
4147 return 0;
4148}
4149
Takashi Iwaieb541332010-08-06 13:48:11 +02004150/*
4151 * codec prepare/cleanup entries
4152 */
4153int snd_hda_codec_prepare(struct hda_codec *codec,
4154 struct hda_pcm_stream *hinfo,
4155 unsigned int stream,
4156 unsigned int format,
4157 struct snd_pcm_substream *substream)
4158{
4159 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004160 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004161 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4162 if (ret >= 0)
4163 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004164 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004165 return ret;
4166}
4167EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4168
4169void snd_hda_codec_cleanup(struct hda_codec *codec,
4170 struct hda_pcm_stream *hinfo,
4171 struct snd_pcm_substream *substream)
4172{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004173 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004174 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004175 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004176}
4177EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4178
Takashi Iwaid5191e52009-11-16 14:58:17 +01004179/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004180const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4181 "Audio", "SPDIF", "HDMI", "Modem"
4182};
4183
Takashi Iwai176d5332008-07-30 15:01:44 +02004184/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004185 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004186 *
4187 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004188 */
4189static int get_empty_pcm_device(struct hda_bus *bus, int type)
4190{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004191 /* audio device indices; not linear to keep compatibility */
4192 static int audio_idx[HDA_PCM_NTYPES][5] = {
4193 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4194 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004195 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004196 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004197 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004198 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004199
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004200 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004201 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4202 return -EINVAL;
4203 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004204
4205 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4206 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4207 return audio_idx[type][i];
4208
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004209 /* non-fixed slots starting from 10 */
4210 for (i = 10; i < 32; i++) {
4211 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4212 return i;
4213 }
4214
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004215 snd_printk(KERN_WARNING "Too many %s devices\n",
4216 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004217 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004218}
4219
4220/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004221 * attach a new PCM stream
4222 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004223static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004224{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004225 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004226 struct hda_pcm_stream *info;
4227 int stream, err;
4228
Takashi Iwaib91f0802008-11-04 08:43:08 +01004229 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004230 return -EINVAL;
4231 for (stream = 0; stream < 2; stream++) {
4232 info = &pcm->stream[stream];
4233 if (info->substreams) {
4234 err = set_pcm_default_values(codec, info);
4235 if (err < 0)
4236 return err;
4237 }
4238 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004239 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004240}
4241
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004242/* assign all PCMs of the given codec */
4243int snd_hda_codec_build_pcms(struct hda_codec *codec)
4244{
4245 unsigned int pcm;
4246 int err;
4247
4248 if (!codec->num_pcms) {
4249 if (!codec->patch_ops.build_pcms)
4250 return 0;
4251 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004252 if (err < 0) {
4253 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004254 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004255 err = snd_hda_codec_reset(codec);
4256 if (err < 0) {
4257 printk(KERN_ERR
4258 "hda_codec: cannot revert codec\n");
4259 return err;
4260 }
4261 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004262 }
4263 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4264 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4265 int dev;
4266
4267 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004268 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004269
4270 if (!cpcm->pcm) {
4271 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4272 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004273 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004274 cpcm->device = dev;
4275 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004276 if (err < 0) {
4277 printk(KERN_ERR "hda_codec: cannot attach "
4278 "PCM stream %d for codec #%d\n",
4279 dev, codec->addr);
4280 continue; /* no fatal error */
4281 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004282 }
4283 }
4284 return 0;
4285}
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/**
4288 * snd_hda_build_pcms - build PCM information
4289 * @bus: the BUS
4290 *
4291 * Create PCM information for each codec included in the bus.
4292 *
4293 * The build_pcms codec patch is requested to set up codec->num_pcms and
4294 * codec->pcm_info properly. The array is referred by the top-level driver
4295 * to create its PCM instances.
4296 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4297 * callback.
4298 *
4299 * At least, substreams, channels_min and channels_max must be filled for
4300 * each stream. substreams = 0 indicates that the stream doesn't exist.
4301 * When rates and/or formats are zero, the supported values are queried
4302 * from the given nid. The nid is used also by the default ops.prepare
4303 * and ops.cleanup callbacks.
4304 *
4305 * The driver needs to call ops.open in its open callback. Similarly,
4306 * ops.close is supposed to be called in the close callback.
4307 * ops.prepare should be called in the prepare or hw_params callback
4308 * with the proper parameters for set up.
4309 * ops.cleanup should be called in hw_free for clean up of streams.
4310 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004311 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004313int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004315 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Takashi Iwai0ba21762007-04-16 11:29:14 +02004317 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004318 int err = snd_hda_codec_build_pcms(codec);
4319 if (err < 0)
4320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322 return 0;
4323}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004324EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326/**
4327 * snd_hda_check_board_config - compare the current codec with the config table
4328 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004329 * @num_configs: number of config enums
4330 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 * @tbl: configuration table, terminated by null entries
4332 *
4333 * Compares the modelname or PCI subsystem id of the current codec with the
4334 * given configuration table. If a matching entry is found, returns its
4335 * config value (supposed to be 0 or positive).
4336 *
4337 * If no entries are matching, the function returns a negative value.
4338 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004339int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004340 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004341 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004343 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004344 int i;
4345 for (i = 0; i < num_configs; i++) {
4346 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004347 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004348 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4349 "selected\n", models[i]);
4350 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
4352 }
4353 }
4354
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004355 if (!codec->bus->pci || !tbl)
4356 return -1;
4357
4358 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4359 if (!tbl)
4360 return -1;
4361 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004362#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004363 char tmp[10];
4364 const char *model = NULL;
4365 if (models)
4366 model = models[tbl->value];
4367 if (!model) {
4368 sprintf(tmp, "#%d", tbl->value);
4369 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004371 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4372 "for config %x:%x (%s)\n",
4373 model, tbl->subvendor, tbl->subdevice,
4374 (tbl->name ? tbl->name : "Unknown device"));
4375#endif
4376 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378 return -1;
4379}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004380EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004383 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004384 subsystem ID with the
4385 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004386
4387 This is important for Gateway notebooks with SB450 HDA Audio
4388 where the vendor ID of the PCI device is:
4389 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4390 and the vendor/subvendor are found only at the codec.
4391
4392 * @codec: the HDA codec
4393 * @num_configs: number of config enums
4394 * @models: array of model name strings
4395 * @tbl: configuration table, terminated by null entries
4396 *
4397 * Compares the modelname or PCI subsystem id of the current codec with the
4398 * given configuration table. If a matching entry is found, returns its
4399 * config value (supposed to be 0 or positive).
4400 *
4401 * If no entries are matching, the function returns a negative value.
4402 */
4403int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004404 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004405 const struct snd_pci_quirk *tbl)
4406{
4407 const struct snd_pci_quirk *q;
4408
4409 /* Search for codec ID */
4410 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004411 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4412 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4413 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004414 break;
4415 }
4416
4417 if (!q->subvendor)
4418 return -1;
4419
4420 tbl = q;
4421
4422 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004423#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004424 char tmp[10];
4425 const char *model = NULL;
4426 if (models)
4427 model = models[tbl->value];
4428 if (!model) {
4429 sprintf(tmp, "#%d", tbl->value);
4430 model = tmp;
4431 }
4432 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4433 "for config %x:%x (%s)\n",
4434 model, tbl->subvendor, tbl->subdevice,
4435 (tbl->name ? tbl->name : "Unknown device"));
4436#endif
4437 return tbl->value;
4438 }
4439 return -1;
4440}
4441EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4442
4443/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 * snd_hda_add_new_ctls - create controls from the array
4445 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004446 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 *
4448 * This helper function creates and add new controls in the given array.
4449 * The array must be terminated with an empty entry as terminator.
4450 *
4451 * Returns 0 if successful, or a negative error code.
4452 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004453int snd_hda_add_new_ctls(struct hda_codec *codec,
4454 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004456 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004459 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004460 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004461 if (knew->iface == -1) /* skip this codec private value */
4462 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004463 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004464 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004465 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004466 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004467 if (addr > 0)
4468 kctl->id.device = addr;
4469 if (idx > 0)
4470 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004471 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004472 if (!err)
4473 break;
4474 /* try first with another device index corresponding to
4475 * the codec addr; if it still fails (or it's the
4476 * primary codec), then try another control index
4477 */
4478 if (!addr && codec->addr)
4479 addr = codec->addr;
4480 else if (!idx && !knew->index) {
4481 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004482 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004483 if (idx <= 0)
4484 return err;
4485 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004486 return err;
4487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 }
4489 return 0;
4490}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004491EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Takashi Iwai83012a72012-08-24 18:38:08 +02004493#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004494static void hda_power_work(struct work_struct *work)
4495{
4496 struct hda_codec *codec =
4497 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004498 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004499 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004500
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004501 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004502 if (codec->power_transition > 0) { /* during power-up sequence? */
4503 spin_unlock(&codec->power_lock);
4504 return;
4505 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004506 if (!codec->power_on || codec->power_count) {
4507 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004508 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004509 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004510 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004511 spin_unlock(&codec->power_lock);
4512
Dylan Reidd17344b2012-09-28 15:57:01 -07004513 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004514 codec->pm_down_notified = 0;
4515 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4516 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004517 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004518 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004519}
4520
4521static void hda_keep_power_on(struct hda_codec *codec)
4522{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004523 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004524 codec->power_count++;
4525 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004526 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004527 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004528}
4529
Takashi Iwaid5191e52009-11-16 14:58:17 +01004530/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004531void snd_hda_update_power_acct(struct hda_codec *codec)
4532{
4533 unsigned long delta = jiffies - codec->power_jiffies;
4534 if (codec->power_on)
4535 codec->power_on_acct += delta;
4536 else
4537 codec->power_off_acct += delta;
4538 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004539}
4540
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004541/* Transition to powered up, if wait_power_down then wait for a pending
4542 * transition to D3 to complete. A pending D3 transition is indicated
4543 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004544/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004545static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004546{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004547 struct hda_bus *bus = codec->bus;
4548
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004549 /* Return if power_on or transitioning to power_on, unless currently
4550 * powering down. */
4551 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004552 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004553 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004554 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004555
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004556 cancel_delayed_work_sync(&codec->power_work);
4557
4558 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004559 /* If the power down delayed work was cancelled above before starting,
4560 * then there is no need to go through power up here.
4561 */
4562 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004563 if (codec->power_transition < 0)
4564 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004565 return;
4566 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004567
Takashi Iwaid66fee52011-08-02 15:39:31 +02004568 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004569 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004570 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004571 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004572 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004573 spin_unlock(&codec->power_lock);
4574
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004575 if (codec->pm_down_notified) {
4576 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004577 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004578 }
4579
Takashi Iwaicb53c622007-08-10 17:21:45 +02004580 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004581
4582 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004583 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004584}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004585
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004586#define power_save(codec) \
4587 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004588
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004589/* Transition to powered down */
4590static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004591{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004592 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004593 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004594
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004595 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004596 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004597 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004598 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004599 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004600}
4601
4602/**
4603 * snd_hda_power_save - Power-up/down/sync the codec
4604 * @codec: HD-audio codec
4605 * @delta: the counter delta to change
4606 *
4607 * Change the power-up counter via @delta, and power up or down the hardware
4608 * appropriately. For the power-down, queue to the delayed action.
4609 * Passing zero to @delta means to synchronize the power state.
4610 */
4611void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4612{
4613 spin_lock(&codec->power_lock);
4614 codec->power_count += delta;
4615 trace_hda_power_count(codec);
4616 if (delta > 0)
4617 __snd_hda_power_up(codec, d3wait);
4618 else
4619 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004620 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004621}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004622EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004623
Takashi Iwaid5191e52009-11-16 14:58:17 +01004624/**
4625 * snd_hda_check_amp_list_power - Check the amp list and update the power
4626 * @codec: HD-audio codec
4627 * @check: the object containing an AMP list and the status
4628 * @nid: NID to check / update
4629 *
4630 * Check whether the given NID is in the amp list. If it's in the list,
4631 * check the current AMP status, and update the the power-status according
4632 * to the mute status.
4633 *
4634 * This function is supposed to be set or called from the check_power_status
4635 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004636 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004637int snd_hda_check_amp_list_power(struct hda_codec *codec,
4638 struct hda_loopback_check *check,
4639 hda_nid_t nid)
4640{
Takashi Iwai031024e2011-05-02 11:29:30 +02004641 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004642 int ch, v;
4643
4644 if (!check->amplist)
4645 return 0;
4646 for (p = check->amplist; p->nid; p++) {
4647 if (p->nid == nid)
4648 break;
4649 }
4650 if (!p->nid)
4651 return 0; /* nothing changed */
4652
4653 for (p = check->amplist; p->nid; p++) {
4654 for (ch = 0; ch < 2; ch++) {
4655 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4656 p->idx);
4657 if (!(v & HDA_AMP_MUTE) && v > 0) {
4658 if (!check->power_on) {
4659 check->power_on = 1;
4660 snd_hda_power_up(codec);
4661 }
4662 return 1;
4663 }
4664 }
4665 }
4666 if (check->power_on) {
4667 check->power_on = 0;
4668 snd_hda_power_down(codec);
4669 }
4670 return 0;
4671}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004672EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004675/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004676 * Channel mode helper
4677 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004678
4679/**
4680 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4681 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004682int snd_hda_ch_mode_info(struct hda_codec *codec,
4683 struct snd_ctl_elem_info *uinfo,
4684 const struct hda_channel_mode *chmode,
4685 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004686{
4687 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4688 uinfo->count = 1;
4689 uinfo->value.enumerated.items = num_chmodes;
4690 if (uinfo->value.enumerated.item >= num_chmodes)
4691 uinfo->value.enumerated.item = num_chmodes - 1;
4692 sprintf(uinfo->value.enumerated.name, "%dch",
4693 chmode[uinfo->value.enumerated.item].channels);
4694 return 0;
4695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004696EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004697
Takashi Iwaid5191e52009-11-16 14:58:17 +01004698/**
4699 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4700 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004701int snd_hda_ch_mode_get(struct hda_codec *codec,
4702 struct snd_ctl_elem_value *ucontrol,
4703 const struct hda_channel_mode *chmode,
4704 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004705 int max_channels)
4706{
4707 int i;
4708
4709 for (i = 0; i < num_chmodes; i++) {
4710 if (max_channels == chmode[i].channels) {
4711 ucontrol->value.enumerated.item[0] = i;
4712 break;
4713 }
4714 }
4715 return 0;
4716}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004717EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004718
Takashi Iwaid5191e52009-11-16 14:58:17 +01004719/**
4720 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4721 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004722int snd_hda_ch_mode_put(struct hda_codec *codec,
4723 struct snd_ctl_elem_value *ucontrol,
4724 const struct hda_channel_mode *chmode,
4725 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004726 int *max_channelsp)
4727{
4728 unsigned int mode;
4729
4730 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004731 if (mode >= num_chmodes)
4732 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004733 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004734 return 0;
4735 /* change the current channel setting */
4736 *max_channelsp = chmode[mode].channels;
4737 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004738 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004739 return 1;
4740}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004741EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/*
4744 * input MUX helper
4745 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004746
4747/**
4748 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4749 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004750int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4751 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
4753 unsigned int index;
4754
4755 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4756 uinfo->count = 1;
4757 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004758 if (!imux->num_items)
4759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 index = uinfo->value.enumerated.item;
4761 if (index >= imux->num_items)
4762 index = imux->num_items - 1;
4763 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4764 return 0;
4765}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004766EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Takashi Iwaid5191e52009-11-16 14:58:17 +01004768/**
4769 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4770 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004771int snd_hda_input_mux_put(struct hda_codec *codec,
4772 const struct hda_input_mux *imux,
4773 struct snd_ctl_elem_value *ucontrol,
4774 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 unsigned int *cur_val)
4776{
4777 unsigned int idx;
4778
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004779 if (!imux->num_items)
4780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 idx = ucontrol->value.enumerated.item[0];
4782 if (idx >= imux->num_items)
4783 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004784 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004786 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4787 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 *cur_val = idx;
4789 return 1;
4790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004791EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793
4794/*
4795 * Multi-channel / digital-out PCM helper functions
4796 */
4797
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004798/* setup SPDIF output stream */
4799static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4800 unsigned int stream_tag, unsigned int format)
4801{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004802 struct hda_spdif_out *spdif;
4803 unsigned int curr_fmt;
4804 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004805
Laurence Darby3bef1c32012-11-03 17:00:06 +00004806 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4807 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4808 AC_VERB_GET_STREAM_FORMAT, 0);
4809 reset = codec->spdif_status_reset &&
4810 (spdif->ctls & AC_DIG1_ENABLE) &&
4811 curr_fmt != format;
4812
4813 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4814 updated */
4815 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004816 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004817 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004818 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004819 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004820 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004821 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004822 for (d = codec->slave_dig_outs; *d; d++)
4823 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4824 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004825 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004826 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004827 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004828 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004829 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004830}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004831
Takashi Iwai2f728532008-09-25 16:32:41 +02004832static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4833{
4834 snd_hda_codec_cleanup_stream(codec, nid);
4835 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004836 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004837 for (d = codec->slave_dig_outs; *d; d++)
4838 snd_hda_codec_cleanup_stream(codec, *d);
4839 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004840}
4841
Takashi Iwaid5191e52009-11-16 14:58:17 +01004842/**
4843 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4844 * @bus: HD-audio bus
4845 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004846void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4847{
4848 struct hda_codec *codec;
4849
4850 if (!bus)
4851 return;
4852 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004853 if (hda_codec_is_power_on(codec) &&
4854 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004855 codec->patch_ops.reboot_notify(codec);
4856 }
4857}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004858EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004859
Takashi Iwaid5191e52009-11-16 14:58:17 +01004860/**
4861 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004863int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4864 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Ingo Molnar62932df2006-01-16 16:34:20 +01004866 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004867 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4868 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004869 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004871 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 return 0;
4873}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004874EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Takashi Iwaid5191e52009-11-16 14:58:17 +01004876/**
4877 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4878 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004879int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4880 struct hda_multi_out *mout,
4881 unsigned int stream_tag,
4882 unsigned int format,
4883 struct snd_pcm_substream *substream)
4884{
4885 mutex_lock(&codec->spdif_mutex);
4886 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4887 mutex_unlock(&codec->spdif_mutex);
4888 return 0;
4889}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004890EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004891
Takashi Iwaid5191e52009-11-16 14:58:17 +01004892/**
4893 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4894 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004895int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4896 struct hda_multi_out *mout)
4897{
4898 mutex_lock(&codec->spdif_mutex);
4899 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4900 mutex_unlock(&codec->spdif_mutex);
4901 return 0;
4902}
4903EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4904
Takashi Iwaid5191e52009-11-16 14:58:17 +01004905/**
4906 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004908int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4909 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
Ingo Molnar62932df2006-01-16 16:34:20 +01004911 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004913 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return 0;
4915}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004916EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Takashi Iwaid5191e52009-11-16 14:58:17 +01004918/**
4919 * snd_hda_multi_out_analog_open - open analog outputs
4920 *
4921 * Open analog outputs and set up the hw-constraints.
4922 * If the digital outputs can be opened as slave, open the digital
4923 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004925int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4926 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004927 struct snd_pcm_substream *substream,
4928 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
Takashi Iwai9a081602008-02-12 18:37:26 +01004930 struct snd_pcm_runtime *runtime = substream->runtime;
4931 runtime->hw.channels_max = mout->max_channels;
4932 if (mout->dig_out_nid) {
4933 if (!mout->analog_rates) {
4934 mout->analog_rates = hinfo->rates;
4935 mout->analog_formats = hinfo->formats;
4936 mout->analog_maxbps = hinfo->maxbps;
4937 } else {
4938 runtime->hw.rates = mout->analog_rates;
4939 runtime->hw.formats = mout->analog_formats;
4940 hinfo->maxbps = mout->analog_maxbps;
4941 }
4942 if (!mout->spdif_rates) {
4943 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4944 &mout->spdif_rates,
4945 &mout->spdif_formats,
4946 &mout->spdif_maxbps);
4947 }
4948 mutex_lock(&codec->spdif_mutex);
4949 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004950 if ((runtime->hw.rates & mout->spdif_rates) &&
4951 (runtime->hw.formats & mout->spdif_formats)) {
4952 runtime->hw.rates &= mout->spdif_rates;
4953 runtime->hw.formats &= mout->spdif_formats;
4954 if (mout->spdif_maxbps < hinfo->maxbps)
4955 hinfo->maxbps = mout->spdif_maxbps;
4956 } else {
4957 mout->share_spdif = 0;
4958 /* FIXME: need notify? */
4959 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004960 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004961 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4964 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4965}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004966EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Takashi Iwaid5191e52009-11-16 14:58:17 +01004968/**
4969 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4970 *
4971 * Set up the i/o for analog out.
4972 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004974int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4975 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 unsigned int stream_tag,
4977 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004978 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Takashi Iwaidda14412011-05-02 11:29:30 +02004980 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004982 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 int i;
4984
Ingo Molnar62932df2006-01-16 16:34:20 +01004985 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004986 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004987 if (mout->dig_out_nid && mout->share_spdif &&
4988 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004990 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4991 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004992 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004994 setup_dig_out_stream(codec, mout->dig_out_nid,
4995 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 } else {
4997 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004998 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005001 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005004 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5005 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005006 if (!mout->no_share_stream &&
5007 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005009 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5010 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005011 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005012 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5013 if (!mout->no_share_stream && mout->hp_out_nid[i])
5014 snd_hda_codec_setup_stream(codec,
5015 mout->hp_out_nid[i],
5016 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005017 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005018 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005019 snd_hda_codec_setup_stream(codec,
5020 mout->extra_out_nid[i],
5021 stream_tag, 0, format);
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /* surrounds */
5024 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005025 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005026 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5027 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005028 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005029 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5030 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
5032 return 0;
5033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005034EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Takashi Iwaid5191e52009-11-16 14:58:17 +01005036/**
5037 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005039int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5040 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Takashi Iwaidda14412011-05-02 11:29:30 +02005042 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int i;
5044
5045 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005046 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005048 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005049 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5050 if (mout->hp_out_nid[i])
5051 snd_hda_codec_cleanup_stream(codec,
5052 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005053 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5054 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005055 snd_hda_codec_cleanup_stream(codec,
5056 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005057 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005059 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 mout->dig_out_used = 0;
5061 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005062 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return 0;
5064}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005065EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Takashi Iwai47408602012-04-20 13:06:53 +02005067/**
5068 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5069 *
5070 * Guess the suitable VREF pin bits to be set as the pin-control value.
5071 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5072 */
5073unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5074{
5075 unsigned int pincap;
5076 unsigned int oldval;
5077 oldval = snd_hda_codec_read(codec, pin, 0,
5078 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5079 pincap = snd_hda_query_pin_caps(codec, pin);
5080 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5081 /* Exception: if the default pin setup is vref50, we give it priority */
5082 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5083 return AC_PINCTL_VREF_80;
5084 else if (pincap & AC_PINCAP_VREF_50)
5085 return AC_PINCTL_VREF_50;
5086 else if (pincap & AC_PINCAP_VREF_100)
5087 return AC_PINCTL_VREF_100;
5088 else if (pincap & AC_PINCAP_VREF_GRD)
5089 return AC_PINCTL_VREF_GRD;
5090 return AC_PINCTL_VREF_HIZ;
5091}
5092EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5093
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005094int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5095 unsigned int val, bool cached)
5096{
5097 if (val) {
5098 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005099 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005100 if (!(cap & AC_PINCAP_OUT))
5101 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5102 else if ((val & AC_PINCTL_HP_EN) &&
5103 !(cap & AC_PINCAP_HP_DRV))
5104 val &= ~AC_PINCTL_HP_EN;
5105 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005106 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005107 if (!(cap & AC_PINCAP_IN))
5108 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5109 }
5110 }
5111 if (cached)
5112 return snd_hda_codec_update_cache(codec, pin, 0,
5113 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5114 else
5115 return snd_hda_codec_write(codec, pin, 0,
5116 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5117}
5118EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5119
Takashi Iwai990061c2010-09-09 22:08:44 +02005120/**
5121 * snd_hda_add_imux_item - Add an item to input_mux
5122 *
5123 * When the same label is used already in the existing items, the number
5124 * suffix is appended to the label. This label index number is stored
5125 * to type_idx when non-NULL pointer is given.
5126 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005127int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5128 int index, int *type_idx)
5129{
5130 int i, label_idx = 0;
5131 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5132 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5133 return -EINVAL;
5134 }
5135 for (i = 0; i < imux->num_items; i++) {
5136 if (!strncmp(label, imux->items[i].label, strlen(label)))
5137 label_idx++;
5138 }
5139 if (type_idx)
5140 *type_idx = label_idx;
5141 if (label_idx > 0)
5142 snprintf(imux->items[imux->num_items].label,
5143 sizeof(imux->items[imux->num_items].label),
5144 "%s %d", label, label_idx);
5145 else
5146 strlcpy(imux->items[imux->num_items].label, label,
5147 sizeof(imux->items[imux->num_items].label));
5148 imux->items[imux->num_items].index = index;
5149 imux->num_items++;
5150 return 0;
5151}
5152EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005153
Takashi Iwai4a471b72005-12-07 13:56:29 +01005154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155#ifdef CONFIG_PM
5156/*
5157 * power management
5158 */
5159
5160/**
5161 * snd_hda_suspend - suspend the codecs
5162 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 *
5164 * Returns 0 if successful.
5165 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005166int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005168 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Takashi Iwai0ba21762007-04-16 11:29:14 +02005170 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005171 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005172 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005173 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175 return 0;
5176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005177EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
5179/**
5180 * snd_hda_resume - resume the codecs
5181 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 *
5183 * Returns 0 if successful.
5184 */
5185int snd_hda_resume(struct hda_bus *bus)
5186{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005187 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Takashi Iwai0ba21762007-04-16 11:29:14 +02005189 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005190 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 return 0;
5193}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005194EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005195#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005196
5197/*
5198 * generic arrays
5199 */
5200
Takashi Iwaid5191e52009-11-16 14:58:17 +01005201/**
5202 * snd_array_new - get a new element from the given array
5203 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005204 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005205 * Get a new element from the given array. If it exceeds the
5206 * pre-allocated array size, re-allocate the array.
5207 *
5208 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005209 */
5210void *snd_array_new(struct snd_array *array)
5211{
Takashi Iwai12f17712012-10-10 08:59:14 +02005212 if (snd_BUG_ON(!array->elem_size))
5213 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005214 if (array->used >= array->alloced) {
5215 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005216 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005217 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005218 void *nlist;
5219 if (snd_BUG_ON(num >= 4096))
5220 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005221 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005222 if (!nlist)
5223 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005224 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005225 array->list = nlist;
5226 array->alloced = num;
5227 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005228 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005229}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005230EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005231
Takashi Iwaid5191e52009-11-16 14:58:17 +01005232/**
5233 * snd_array_free - free the given array elements
5234 * @array: the array object
5235 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005236void snd_array_free(struct snd_array *array)
5237{
5238 kfree(array->list);
5239 array->used = 0;
5240 array->alloced = 0;
5241 array->list = NULL;
5242}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005243EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005244
Takashi Iwaid5191e52009-11-16 14:58:17 +01005245/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005246 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5247 * @pcm: PCM caps bits
5248 * @buf: the string buffer to write
5249 * @buflen: the max buffer length
5250 *
5251 * used by hda_proc.c and hda_eld.c
5252 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005253void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5254{
5255 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5256 int i, j;
5257
5258 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5259 if (pcm & (AC_SUPPCM_BITS_8 << i))
5260 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5261
5262 buf[j] = '\0'; /* necessary when j == 0 */
5263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005264EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005265
5266MODULE_DESCRIPTION("HDA codec core");
5267MODULE_LICENSE("GPL");