blob: 70d4848b5cd0ad25ce272877f9c759ed010dfc58 [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 Iwai8dd78332009-06-02 01:16:07 +0200231 if (res && *res == -1 && bus->rirb_error) {
232 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 */
241 if (!err)
242 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
994/* write a config value for the given NID */
995static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
996 unsigned int cfg)
997{
998 int i;
999 for (i = 0; i < 4; i++) {
1000 snd_hda_codec_write(codec, nid, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1002 cfg & 0xff);
1003 cfg >>= 8;
1004 }
1005}
1006
1007/* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1009 */
1010int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1011 hda_nid_t nid, unsigned int cfg)
1012{
1013 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001014 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015
Takashi Iwaib82855a2009-12-27 11:24:56 +01001016 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1017 return -EINVAL;
1018
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001019 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001020 pin = look_up_pincfg(codec, list, nid);
1021 if (!pin) {
1022 pin = snd_array_new(list);
1023 if (!pin)
1024 return -ENOMEM;
1025 pin->nid = nid;
1026 }
1027 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001028
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1031 */
1032 cfg = snd_hda_codec_get_pincfg(codec, nid);
1033 if (oldcfg != cfg)
1034 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035 return 0;
1036}
1037
Takashi Iwaid5191e52009-11-16 14:58:17 +01001038/**
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1043 *
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1047 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001048int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1049 hda_nid_t nid, unsigned int cfg)
1050{
Takashi Iwai346ff702009-02-23 09:42:57 +01001051 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001052}
1053EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1054
Takashi Iwaid5191e52009-11-16 14:58:17 +01001055/**
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1059 *
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1063 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001064unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwai3be14142009-02-20 14:11:16 +01001068#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001069 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 if (pin)
1071 return pin->cfg;
1072#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001073 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1074 if (pin)
1075 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1077 if (pin)
1078 return pin->cfg;
1079 return 0;
1080}
1081EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1082
1083/* restore all current pin configs */
1084static void restore_pincfgs(struct hda_codec *codec)
1085{
1086 int i;
1087 for (i = 0; i < codec->init_pins.used; i++) {
1088 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1089 set_pincfg(codec, pin->nid,
1090 snd_hda_codec_get_pincfg(codec, pin->nid));
1091 }
1092}
Takashi Iwai54d17402005-11-21 16:33:22 +01001093
Takashi Iwai92ee6162009-12-27 11:18:59 +01001094/**
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1097 *
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1100 */
1101void snd_hda_shutup_pins(struct hda_codec *codec)
1102{
1103 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1106 */
1107 if (codec->bus->shutdown)
1108 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001109 for (i = 0; i < codec->init_pins.used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec, pin->nid, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1114 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001115 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001116}
1117EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1118
Takashi Iwai2a439522011-07-26 09:52:50 +02001119#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001120/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121static void restore_shutup_pins(struct hda_codec *codec)
1122{
1123 int i;
1124 if (!codec->pins_shutup)
1125 return;
1126 if (codec->bus->shutdown)
1127 return;
1128 for (i = 0; i < codec->init_pins.used; i++) {
1129 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1130 snd_hda_codec_write(codec, pin->nid, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL,
1132 pin->ctrl);
1133 }
1134 codec->pins_shutup = 0;
1135}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001136#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001137
Takashi Iwai01751f52007-08-10 16:59:39 +02001138static void init_hda_cache(struct hda_cache_rec *cache,
1139 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001140static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001141
Takashi Iwai3be14142009-02-20 14:11:16 +01001142/* restore the initial pin cfgs and release all pincfg lists */
1143static void restore_init_pincfgs(struct hda_codec *codec)
1144{
Takashi Iwai346ff702009-02-23 09:42:57 +01001145 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001146 * so that only the values in init_pins are restored
1147 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001148 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001149#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#endif
1152 restore_pincfgs(codec);
1153 snd_array_free(&codec->init_pins);
1154}
1155
Takashi Iwai54d17402005-11-21 16:33:22 +01001156/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001157 * audio-converter setup caches
1158 */
1159struct hda_cvt_setup {
1160 hda_nid_t nid;
1161 u8 stream_tag;
1162 u8 channel_id;
1163 u16 format_id;
1164 unsigned char active; /* cvt is currently used */
1165 unsigned char dirty; /* setups should be cleared */
1166};
1167
1168/* get or create a cache entry for the given audio converter NID */
1169static struct hda_cvt_setup *
1170get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1171{
1172 struct hda_cvt_setup *p;
1173 int i;
1174
1175 for (i = 0; i < codec->cvt_setups.used; i++) {
1176 p = snd_array_elem(&codec->cvt_setups, i);
1177 if (p->nid == nid)
1178 return p;
1179 }
1180 p = snd_array_new(&codec->cvt_setups);
1181 if (p)
1182 p->nid = nid;
1183 return p;
1184}
1185
1186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * codec destructor
1188 */
1189static void snd_hda_codec_free(struct hda_codec *codec)
1190{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return;
Takashi Iwai59cad162012-06-26 15:00:20 +02001193 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001194 restore_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001195#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001197 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001200 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001201 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001202 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001208#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001209 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001210 hda_call_pm_notify(codec->bus, false);
1211#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001212 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001213 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001214 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001215 kfree(codec->vendor_name);
1216 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001217 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001218 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 kfree(codec);
1220}
1221
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001222static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1223 hda_nid_t fg, unsigned int power_state);
1224
Takashi Iwaid8193872012-08-31 07:54:38 -07001225static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001226 unsigned int power_state);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/**
1229 * snd_hda_codec_new - create a HDA codec
1230 * @bus: the bus to assign
1231 * @codec_addr: the codec address
1232 * @codecp: the pointer to store the generated codec
1233 *
1234 * Returns 0 if successful, or a negative error code.
1235 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001236int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1237 unsigned int codec_addr,
1238 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001241 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001242 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int err;
1244
Takashi Iwaida3cec32008-08-08 17:12:14 +02001245 if (snd_BUG_ON(!bus))
1246 return -EINVAL;
1247 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1248 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001251 snd_printk(KERN_ERR "hda_codec: "
1252 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EBUSY;
1254 }
1255
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001256 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (codec == NULL) {
1258 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1259 return -ENOMEM;
1260 }
1261
1262 codec->bus = bus;
1263 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001264 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001265 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001266 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001267 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001268 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001269 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1270 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001271 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001272 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001273 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001274 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001275 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Takashi Iwai83012a72012-08-24 18:38:08 +02001277#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001278 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001279 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1280 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1281 * the caller has to power down appropriatley after initialization
1282 * phase.
1283 */
1284 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001285 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001286#endif
1287
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001288 if (codec->bus->modelname) {
1289 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1290 if (!codec->modelname) {
1291 snd_hda_codec_free(codec);
1292 return -ENODEV;
1293 }
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 list_add_tail(&codec->list, &bus->codec_list);
1297 bus->caddr_tbl[codec_addr] = codec;
1298
Takashi Iwai0ba21762007-04-16 11:29:14 +02001299 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1300 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001301 if (codec->vendor_id == -1)
1302 /* read again, hopefully the access method was corrected
1303 * in the last read...
1304 */
1305 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1306 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001307 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1308 AC_PAR_SUBSYSTEM_ID);
1309 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1310 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001312 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001314 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001315 err = -ENODEV;
1316 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 fg = codec->afg ? codec->afg : codec->mfg;
1320 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001321 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001322 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001323 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001324 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001325 err = read_pin_defaults(codec);
1326 if (err < 0)
1327 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001328
Takashi Iwai0ba21762007-04-16 11:29:14 +02001329 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001330 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001331 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001332 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001333 }
1334
Takashi Iwai83012a72012-08-24 18:38:08 +02001335#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001336 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001337 AC_PWRST_CLKSTOP);
1338 if (!codec->d3_stop_clk)
1339 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001340#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001341 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001342 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001343
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001344 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001345 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001346
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347 snd_hda_codec_proc_new(codec);
1348
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001349 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001350
1351 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1352 codec->subsystem_id, codec->revision_id);
1353 snd_component_add(codec->bus->card, component);
1354
1355 if (codecp)
1356 *codecp = codec;
1357 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001358
1359 error:
1360 snd_hda_codec_free(codec);
1361 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001362}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001363EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001364
Takashi Iwaid5191e52009-11-16 14:58:17 +01001365/**
1366 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1367 * @codec: the HDA codec
1368 *
1369 * Start parsing of the given codec tree and (re-)initialize the whole
1370 * patch instance.
1371 *
1372 * Returns 0 if successful or a negative error code.
1373 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001374int snd_hda_codec_configure(struct hda_codec *codec)
1375{
1376 int err;
1377
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001378 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001379 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001380 err = get_codec_name(codec);
1381 if (err < 0)
1382 return err;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Takashi Iwai82467612007-07-27 19:15:54 +02001385 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001387 goto patched;
1388 }
Takashi Iwai82467612007-07-27 19:15:54 +02001389 if (codec->preset && codec->preset->patch) {
1390 err = codec->preset->patch(codec);
1391 goto patched;
1392 }
1393
1394 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001395 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001396 if (err < 0)
1397 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001398
1399 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001400 if (!err && codec->patch_ops.unsol_event)
1401 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001402 /* audio codec should override the mixer name */
1403 if (!err && (codec->afg || !*codec->bus->card->mixername))
1404 snprintf(codec->bus->card->mixername,
1405 sizeof(codec->bus->card->mixername),
1406 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001409EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Takashi Iwaied360812012-08-08 17:12:52 +02001411/* update the stream-id if changed */
1412static void update_pcm_stream_id(struct hda_codec *codec,
1413 struct hda_cvt_setup *p, hda_nid_t nid,
1414 u32 stream_tag, int channel_id)
1415{
1416 unsigned int oldval, newval;
1417
1418 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1419 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1420 newval = (stream_tag << 4) | channel_id;
1421 if (oldval != newval)
1422 snd_hda_codec_write(codec, nid, 0,
1423 AC_VERB_SET_CHANNEL_STREAMID,
1424 newval);
1425 p->stream_tag = stream_tag;
1426 p->channel_id = channel_id;
1427 }
1428}
1429
1430/* update the format-id if changed */
1431static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1432 hda_nid_t nid, int format)
1433{
1434 unsigned int oldval;
1435
1436 if (p->format_id != format) {
1437 oldval = snd_hda_codec_read(codec, nid, 0,
1438 AC_VERB_GET_STREAM_FORMAT, 0);
1439 if (oldval != format) {
1440 msleep(1);
1441 snd_hda_codec_write(codec, nid, 0,
1442 AC_VERB_SET_STREAM_FORMAT,
1443 format);
1444 }
1445 p->format_id = format;
1446 }
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/**
1450 * snd_hda_codec_setup_stream - set up the codec for streaming
1451 * @codec: the CODEC to set up
1452 * @nid: the NID to set up
1453 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1454 * @channel_id: channel id to pass, zero based.
1455 * @format: stream format.
1456 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001457void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1458 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int channel_id, int format)
1460{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001461 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001462 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001463 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001464 int i;
1465
Takashi Iwai0ba21762007-04-16 11:29:14 +02001466 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001467 return;
1468
Takashi Iwai0ba21762007-04-16 11:29:14 +02001469 snd_printdd("hda_codec_setup_stream: "
1470 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 p = get_hda_cvt_setup(codec, nid);
1473 if (!p)
1474 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001475
1476 if (codec->pcm_format_first)
1477 update_pcm_format(codec, p, nid, format);
1478 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1479 if (!codec->pcm_format_first)
1480 update_pcm_format(codec, p, nid, format);
1481
Takashi Iwaieb541332010-08-06 13:48:11 +02001482 p->active = 1;
1483 p->dirty = 0;
1484
1485 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001486 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001487 list_for_each_entry(c, &codec->bus->codec_list, list) {
1488 for (i = 0; i < c->cvt_setups.used; i++) {
1489 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001490 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001491 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001492 p->dirty = 1;
1493 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001496EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Takashi Iwaif0cea792010-08-13 11:56:53 +02001498static void really_cleanup_stream(struct hda_codec *codec,
1499 struct hda_cvt_setup *q);
1500
Takashi Iwaid5191e52009-11-16 14:58:17 +01001501/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001502 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001503 * @codec: the CODEC to clean up
1504 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001505 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001506 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001507void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1508 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001509{
Takashi Iwaieb541332010-08-06 13:48:11 +02001510 struct hda_cvt_setup *p;
1511
Takashi Iwai888afa12008-03-18 09:57:50 +01001512 if (!nid)
1513 return;
1514
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001515 if (codec->no_sticky_stream)
1516 do_now = 1;
1517
Takashi Iwai888afa12008-03-18 09:57:50 +01001518 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001519 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001520 if (p) {
1521 /* here we just clear the active flag when do_now isn't set;
1522 * actual clean-ups will be done later in
1523 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1524 */
1525 if (do_now)
1526 really_cleanup_stream(codec, p);
1527 else
1528 p->active = 0;
1529 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001530}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001531EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001532
Takashi Iwaieb541332010-08-06 13:48:11 +02001533static void really_cleanup_stream(struct hda_codec *codec,
1534 struct hda_cvt_setup *q)
1535{
1536 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001537 if (q->stream_tag || q->channel_id)
1538 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1539 if (q->format_id)
1540 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1541);
Takashi Iwaieb541332010-08-06 13:48:11 +02001542 memset(q, 0, sizeof(*q));
1543 q->nid = nid;
1544}
1545
1546/* clean up the all conflicting obsolete streams */
1547static void purify_inactive_streams(struct hda_codec *codec)
1548{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001549 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001550 int i;
1551
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001552 list_for_each_entry(c, &codec->bus->codec_list, list) {
1553 for (i = 0; i < c->cvt_setups.used; i++) {
1554 struct hda_cvt_setup *p;
1555 p = snd_array_elem(&c->cvt_setups, i);
1556 if (p->dirty)
1557 really_cleanup_stream(c, p);
1558 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001559 }
1560}
1561
Takashi Iwai2a439522011-07-26 09:52:50 +02001562#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001563/* clean up all streams; called from suspend */
1564static void hda_cleanup_all_streams(struct hda_codec *codec)
1565{
1566 int i;
1567
1568 for (i = 0; i < codec->cvt_setups.used; i++) {
1569 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1570 if (p->stream_tag)
1571 really_cleanup_stream(codec, p);
1572 }
1573}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001574#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
1577 * amp access functions
1578 */
1579
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001580/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001581#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001582#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001583#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1584#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001586#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001589static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001590 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai01751f52007-08-10 16:59:39 +02001592 memset(cache, 0, sizeof(*cache));
1593 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001594 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001595}
1596
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001597static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001598{
Takashi Iwai603c4012008-07-30 15:01:44 +02001599 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001603static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Takashi Iwai01751f52007-08-10 16:59:39 +02001605 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1606 u16 cur = cache->hash[idx];
1607 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001610 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (info->key == key)
1612 return info;
1613 cur = info->next;
1614 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001615 return NULL;
1616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001618/* query the hash. allocate an entry if not found. */
1619static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1620 u32 key)
1621{
1622 struct hda_cache_head *info = get_hash(cache, key);
1623 if (!info) {
1624 u16 idx, cur;
1625 /* add a new hash entry */
1626 info = snd_array_new(&cache->buf);
1627 if (!info)
1628 return NULL;
1629 cur = snd_array_index(&cache->buf, info);
1630 info->key = key;
1631 info->val = 0;
1632 idx = key % (u16)ARRAY_SIZE(cache->hash);
1633 info->next = cache->hash[idx];
1634 cache->hash[idx] = cur;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return info;
1637}
1638
Takashi Iwai01751f52007-08-10 16:59:39 +02001639/* query and allocate an amp hash entry */
1640static inline struct hda_amp_info *
1641get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1642{
1643 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1644}
1645
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001646/* overwrite the value with the key in the caps hash */
1647static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1648{
1649 struct hda_amp_info *info;
1650
1651 mutex_lock(&codec->hash_mutex);
1652 info = get_alloc_amp_hash(codec, key);
1653 if (!info) {
1654 mutex_unlock(&codec->hash_mutex);
1655 return -EINVAL;
1656 }
1657 info->amp_caps = val;
1658 info->head.val |= INFO_AMP_CAPS;
1659 mutex_unlock(&codec->hash_mutex);
1660 return 0;
1661}
1662
1663/* query the value from the caps hash; if not found, fetch the current
1664 * value from the given function and store in the hash
1665 */
1666static unsigned int
1667query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1668 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1669{
1670 struct hda_amp_info *info;
1671 unsigned int val;
1672
1673 mutex_lock(&codec->hash_mutex);
1674 info = get_alloc_amp_hash(codec, key);
1675 if (!info) {
1676 mutex_unlock(&codec->hash_mutex);
1677 return 0;
1678 }
1679 if (!(info->head.val & INFO_AMP_CAPS)) {
1680 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1681 val = func(codec, nid, dir);
1682 write_caps_hash(codec, key, val);
1683 } else {
1684 val = info->amp_caps;
1685 mutex_unlock(&codec->hash_mutex);
1686 }
1687 return val;
1688}
1689
1690static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1691 int direction)
1692{
1693 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1694 nid = codec->afg;
1695 return snd_hda_param_read(codec, nid,
1696 direction == HDA_OUTPUT ?
1697 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1698}
1699
Takashi Iwaid5191e52009-11-16 14:58:17 +01001700/**
1701 * query_amp_caps - query AMP capabilities
1702 * @codec: the HD-auio codec
1703 * @nid: the NID to query
1704 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1705 *
1706 * Query AMP capabilities for the given widget and direction.
1707 * Returns the obtained capability bits.
1708 *
1709 * When cap bits have been already read, this doesn't read again but
1710 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001712u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001714 return query_caps_hash(codec, nid, direction,
1715 HDA_HASH_KEY(nid, direction, 0),
1716 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001718EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Takashi Iwaid5191e52009-11-16 14:58:17 +01001720/**
1721 * snd_hda_override_amp_caps - Override the AMP capabilities
1722 * @codec: the CODEC to clean up
1723 * @nid: the NID to clean up
1724 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1725 * @caps: the capability bits to set
1726 *
1727 * Override the cached AMP caps bits value by the given one.
1728 * This function is useful if the driver needs to adjust the AMP ranges,
1729 * e.g. limit to 0dB, etc.
1730 *
1731 * Returns zero if successful or a negative error code.
1732 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001733int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1734 unsigned int caps)
1735{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001736 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001737}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001738EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001739
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001740static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1741 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001742{
1743 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1744}
1745
Takashi Iwaid5191e52009-11-16 14:58:17 +01001746/**
1747 * snd_hda_query_pin_caps - Query PIN capabilities
1748 * @codec: the HD-auio codec
1749 * @nid: the NID to query
1750 *
1751 * Query PIN capabilities for the given widget.
1752 * Returns the obtained capability bits.
1753 *
1754 * When cap bits have been already read, this doesn't read again but
1755 * returns the cached value.
1756 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001757u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1758{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001759 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001760 read_pin_cap);
1761}
Takashi Iwai1327a322009-03-23 13:07:47 +01001762EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1763
Wu Fengguang864f92b2009-11-18 12:38:02 +08001764/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001765 * snd_hda_override_pin_caps - Override the pin capabilities
1766 * @codec: the CODEC
1767 * @nid: the NID to override
1768 * @caps: the capability bits to set
1769 *
1770 * Override the cached PIN capabilitiy bits value by the given one.
1771 *
1772 * Returns zero if successful or a negative error code.
1773 */
1774int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1775 unsigned int caps)
1776{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001777 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001778}
1779EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1780
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001781/* read or sync the hash value with the current value;
1782 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001784static struct hda_amp_info *
1785update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1786 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001788 struct hda_amp_info *info;
1789 unsigned int parm, val = 0;
1790 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001792 retry:
1793 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1794 if (!info)
1795 return NULL;
1796 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1797 if (!val_read) {
1798 mutex_unlock(&codec->hash_mutex);
1799 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1800 parm |= direction == HDA_OUTPUT ?
1801 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1802 parm |= index;
1803 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001805 val &= 0xff;
1806 val_read = true;
1807 mutex_lock(&codec->hash_mutex);
1808 goto retry;
1809 }
1810 info->vol[ch] = val;
1811 info->head.val |= INFO_AMP_VOL(ch);
1812 }
1813 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001817 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001819static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001820 hda_nid_t nid, int ch, int direction, int index,
1821 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 u32 parm;
1824
1825 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1826 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1827 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001828 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1829 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1830 ; /* set the zero value as a fake mute */
1831 else
1832 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1834}
1835
Takashi Iwaid5191e52009-11-16 14:58:17 +01001836/**
1837 * snd_hda_codec_amp_read - Read AMP value
1838 * @codec: HD-audio codec
1839 * @nid: NID to read the AMP value
1840 * @ch: channel (left=0 or right=1)
1841 * @direction: #HDA_INPUT or #HDA_OUTPUT
1842 * @index: the index value (only for input direction)
1843 *
1844 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Takashi Iwai834be882006-03-01 14:16:17 +01001846int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1847 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001849 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001850 unsigned int val = 0;
1851
1852 mutex_lock(&codec->hash_mutex);
1853 info = update_amp_hash(codec, nid, ch, direction, index);
1854 if (info)
1855 val = info->vol[ch];
1856 mutex_unlock(&codec->hash_mutex);
1857 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001859EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Takashi Iwaid5191e52009-11-16 14:58:17 +01001861/**
1862 * snd_hda_codec_amp_update - update the AMP value
1863 * @codec: HD-audio codec
1864 * @nid: NID to read the AMP value
1865 * @ch: channel (left=0 or right=1)
1866 * @direction: #HDA_INPUT or #HDA_OUTPUT
1867 * @idx: the index value (only for input direction)
1868 * @mask: bit mask to set
1869 * @val: the bits value to set
1870 *
1871 * Update the AMP value with a bit mask.
1872 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001873 */
Takashi Iwai834be882006-03-01 14:16:17 +01001874int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1875 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001877 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001878
Takashi Iwai467126462010-03-29 09:19:38 +02001879 if (snd_BUG_ON(mask & ~0xff))
1880 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001881 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001882
1883 mutex_lock(&codec->hash_mutex);
1884 info = update_amp_hash(codec, nid, ch, direction, idx);
1885 if (!info) {
1886 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001888 }
1889 val |= info->vol[ch] & ~mask;
1890 if (info->vol[ch] == val) {
1891 mutex_unlock(&codec->hash_mutex);
1892 return 0;
1893 }
1894 info->vol[ch] = val;
1895 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001896 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 1;
1898}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001899EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Takashi Iwaid5191e52009-11-16 14:58:17 +01001901/**
1902 * snd_hda_codec_amp_stereo - update the AMP stereo values
1903 * @codec: HD-audio codec
1904 * @nid: NID to read the AMP value
1905 * @direction: #HDA_INPUT or #HDA_OUTPUT
1906 * @idx: the index value (only for input direction)
1907 * @mask: bit mask to set
1908 * @val: the bits value to set
1909 *
1910 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1911 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001912 */
1913int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1914 int direction, int idx, int mask, int val)
1915{
1916 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001917
1918 if (snd_BUG_ON(mask & ~0xff))
1919 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001920 for (ch = 0; ch < 2; ch++)
1921 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1922 idx, mask, val);
1923 return ret;
1924}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001925EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001926
Takashi Iwai2a439522011-07-26 09:52:50 +02001927#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001928/**
1929 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1930 * @codec: HD-audio codec
1931 *
1932 * Resume the all amp commands from the cache.
1933 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001934void snd_hda_codec_resume_amp(struct hda_codec *codec)
1935{
Takashi Iwai603c4012008-07-30 15:01:44 +02001936 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001937 int i;
1938
Takashi Iwai603c4012008-07-30 15:01:44 +02001939 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001940 u32 key = buffer->head.key;
1941 hda_nid_t nid;
1942 unsigned int idx, dir, ch;
1943 if (!key)
1944 continue;
1945 nid = key & 0xff;
1946 idx = (key >> 16) & 0xff;
1947 dir = (key >> 24) & 0xff;
1948 for (ch = 0; ch < 2; ch++) {
1949 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1950 continue;
1951 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1952 buffer->vol[ch]);
1953 }
1954 }
1955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001956EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001957#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001959static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1960 unsigned int ofs)
1961{
1962 u32 caps = query_amp_caps(codec, nid, dir);
1963 /* get num steps */
1964 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1965 if (ofs < caps)
1966 caps -= ofs;
1967 return caps;
1968}
1969
Takashi Iwaid5191e52009-11-16 14:58:17 +01001970/**
1971 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1972 *
1973 * The control element is supposed to have the private_value field
1974 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1975 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001976int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1977 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1980 u16 nid = get_amp_nid(kcontrol);
1981 u8 chs = get_amp_channels(kcontrol);
1982 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001983 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001985 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1986 uinfo->count = chs == 3 ? 2 : 1;
1987 uinfo->value.integer.min = 0;
1988 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1989 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001990 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001991 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1992 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EINVAL;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001997EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999
2000static inline unsigned int
2001read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2002 int ch, int dir, int idx, unsigned int ofs)
2003{
2004 unsigned int val;
2005 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2006 val &= HDA_AMP_VOLMASK;
2007 if (val >= ofs)
2008 val -= ofs;
2009 else
2010 val = 0;
2011 return val;
2012}
2013
2014static inline int
2015update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2016 int ch, int dir, int idx, unsigned int ofs,
2017 unsigned int val)
2018{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002019 unsigned int maxval;
2020
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002021 if (val > 0)
2022 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002023 /* ofs = 0: raw max value */
2024 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002025 if (val > maxval)
2026 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2028 HDA_AMP_VOLMASK, val);
2029}
2030
Takashi Iwaid5191e52009-11-16 14:58:17 +01002031/**
2032 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2033 *
2034 * The control element is supposed to have the private_value field
2035 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2036 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002037int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2041 hda_nid_t nid = get_amp_nid(kcontrol);
2042 int chs = get_amp_channels(kcontrol);
2043 int dir = get_amp_direction(kcontrol);
2044 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002045 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 long *valp = ucontrol->value.integer.value;
2047
2048 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002049 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002051 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return 0;
2053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002054EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Takashi Iwaid5191e52009-11-16 14:58:17 +01002056/**
2057 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2058 *
2059 * The control element is supposed to have the private_value field
2060 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2061 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002062int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2063 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2066 hda_nid_t nid = get_amp_nid(kcontrol);
2067 int chs = get_amp_channels(kcontrol);
2068 int dir = get_amp_direction(kcontrol);
2069 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002070 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 long *valp = ucontrol->value.integer.value;
2072 int change = 0;
2073
Takashi Iwaicb53c622007-08-10 17:21:45 +02002074 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002075 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002076 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002077 valp++;
2078 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002079 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002080 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002081 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return change;
2083}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002084EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwaid5191e52009-11-16 14:58:17 +01002086/**
2087 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2088 *
2089 * The control element is supposed to have the private_value field
2090 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2091 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002092int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2093 unsigned int size, unsigned int __user *_tlv)
2094{
2095 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2096 hda_nid_t nid = get_amp_nid(kcontrol);
2097 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002098 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002099 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002100 u32 caps, val1, val2;
2101
2102 if (size < 4 * sizeof(unsigned int))
2103 return -ENOMEM;
2104 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002105 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2106 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002107 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002108 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002109 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002110 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002111 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002112 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2113 return -EFAULT;
2114 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2115 return -EFAULT;
2116 if (put_user(val1, _tlv + 2))
2117 return -EFAULT;
2118 if (put_user(val2, _tlv + 3))
2119 return -EFAULT;
2120 return 0;
2121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002122EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002123
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124/**
2125 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2126 * @codec: HD-audio codec
2127 * @nid: NID of a reference widget
2128 * @dir: #HDA_INPUT or #HDA_OUTPUT
2129 * @tlv: TLV data to be stored, at least 4 elements
2130 *
2131 * Set (static) TLV data for a virtual master volume using the AMP caps
2132 * obtained from the reference NID.
2133 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002134 */
2135void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2136 unsigned int *tlv)
2137{
2138 u32 caps;
2139 int nums, step;
2140
2141 caps = query_amp_caps(codec, nid, dir);
2142 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2143 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2144 step = (step + 1) * 25;
2145 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2146 tlv[1] = 2 * sizeof(unsigned int);
2147 tlv[2] = -nums * step;
2148 tlv[3] = step;
2149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002150EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002151
2152/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002153static struct snd_kcontrol *
2154_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2155 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002156{
2157 struct snd_ctl_elem_id id;
2158 memset(&id, 0, sizeof(id));
2159 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002160 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002161 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2162 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002163 strcpy(id.name, name);
2164 return snd_ctl_find_id(codec->bus->card, &id);
2165}
2166
Takashi Iwaid5191e52009-11-16 14:58:17 +01002167/**
2168 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2169 * @codec: HD-audio codec
2170 * @name: ctl id name string
2171 *
2172 * Get the control element with the given id string and IFACE_MIXER.
2173 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002174struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2175 const char *name)
2176{
2177 return _snd_hda_find_mixer_ctl(codec, name, 0);
2178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002179EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002180
Takashi Iwai1afe2062010-12-23 10:17:52 +01002181static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2182{
2183 int idx;
2184 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2185 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2186 return idx;
2187 }
2188 return -EBUSY;
2189}
2190
Takashi Iwaid5191e52009-11-16 14:58:17 +01002191/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002192 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002193 * @codec: HD-audio codec
2194 * @nid: corresponding NID (optional)
2195 * @kctl: the control element to assign
2196 *
2197 * Add the given control element to an array inside the codec instance.
2198 * All control elements belonging to a codec are supposed to be added
2199 * by this function so that a proper clean-up works at the free or
2200 * reconfiguration time.
2201 *
2202 * If non-zero @nid is passed, the NID is assigned to the control element.
2203 * The assignment is shown in the codec proc file.
2204 *
2205 * snd_hda_ctl_add() checks the control subdev id field whether
2206 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002207 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2208 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002209 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002210int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2211 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002212{
2213 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002214 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002215 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002216
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002217 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002218 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002219 if (nid == 0)
2220 nid = get_amp_nid_(kctl->private_value);
2221 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002222 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2223 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002224 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002225 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002226 err = snd_ctl_add(codec->bus->card, kctl);
2227 if (err < 0)
2228 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002229 item = snd_array_new(&codec->mixers);
2230 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002231 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002232 item->kctl = kctl;
2233 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002234 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002235 return 0;
2236}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002237EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002238
Takashi Iwaid5191e52009-11-16 14:58:17 +01002239/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002240 * snd_hda_add_nid - Assign a NID to a control element
2241 * @codec: HD-audio codec
2242 * @nid: corresponding NID (optional)
2243 * @kctl: the control element to assign
2244 * @index: index to kctl
2245 *
2246 * Add the given control element to an array inside the codec instance.
2247 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2248 * NID:KCTL mapping - for example "Capture Source" selector.
2249 */
2250int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2251 unsigned int index, hda_nid_t nid)
2252{
2253 struct hda_nid_item *item;
2254
2255 if (nid > 0) {
2256 item = snd_array_new(&codec->nids);
2257 if (!item)
2258 return -ENOMEM;
2259 item->kctl = kctl;
2260 item->index = index;
2261 item->nid = nid;
2262 return 0;
2263 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002264 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2265 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002266 return -EINVAL;
2267}
2268EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2269
2270/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002271 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2272 * @codec: HD-audio codec
2273 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002274void snd_hda_ctls_clear(struct hda_codec *codec)
2275{
2276 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002277 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002278 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002279 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002280 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002281 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002282}
2283
Takashi Iwaia65d6292009-02-23 16:57:04 +01002284/* pseudo device locking
2285 * toggle card->shutdown to allow/disallow the device access (as a hack)
2286 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002287int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002288{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002289 struct snd_card *card = bus->card;
2290 struct hda_codec *codec;
2291
Takashi Iwaia65d6292009-02-23 16:57:04 +01002292 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002293 if (card->shutdown)
2294 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002295 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002296 if (!list_empty(&card->ctl_files))
2297 goto err_clear;
2298
2299 list_for_each_entry(codec, &bus->codec_list, list) {
2300 int pcm;
2301 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2302 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2303 if (!cpcm->pcm)
2304 continue;
2305 if (cpcm->pcm->streams[0].substream_opened ||
2306 cpcm->pcm->streams[1].substream_opened)
2307 goto err_clear;
2308 }
2309 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002310 spin_unlock(&card->files_lock);
2311 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002312
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002313 err_clear:
2314 card->shutdown = 0;
2315 err_unlock:
2316 spin_unlock(&card->files_lock);
2317 return -EINVAL;
2318}
2319EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2320
2321void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002322{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323 struct snd_card *card = bus->card;
2324
2325 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002326 spin_lock(&card->files_lock);
2327 card->shutdown = 0;
2328 spin_unlock(&card->files_lock);
2329}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002330EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002331
Takashi Iwaid5191e52009-11-16 14:58:17 +01002332/**
2333 * snd_hda_codec_reset - Clear all objects assigned to the codec
2334 * @codec: HD-audio codec
2335 *
2336 * This frees the all PCM and control elements assigned to the codec, and
2337 * clears the caches and restores the pin default configurations.
2338 *
2339 * When a device is being used, it returns -EBSY. If successfully freed,
2340 * returns zero.
2341 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002342int snd_hda_codec_reset(struct hda_codec *codec)
2343{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002344 struct hda_bus *bus = codec->bus;
2345 struct snd_card *card = bus->card;
2346 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002347
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002348 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002349 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002350
2351 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002352
Takashi Iwai83012a72012-08-24 18:38:08 +02002353#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002354 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002355 codec->power_on = 0;
2356 codec->power_transition = 0;
2357 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002358 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002359#endif
2360 snd_hda_ctls_clear(codec);
2361 /* relase PCMs */
2362 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002363 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002364 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002365 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002366 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002367 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002368 }
2369 if (codec->patch_ops.free)
2370 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002371 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002372 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002373 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002374 codec->spec = NULL;
2375 free_hda_cache(&codec->amp_cache);
2376 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002377 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2378 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002379 /* free only driver_pins so that init_pins + user_pins are restored */
2380 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002381 restore_pincfgs(codec);
Takashi Iwai09a60712012-06-26 15:01:33 +02002382 snd_array_free(&codec->cvt_setups);
2383 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002384 codec->num_pcms = 0;
2385 codec->pcm_info = NULL;
2386 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002387 codec->slave_dig_outs = NULL;
2388 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002389 module_put(codec->owner);
2390 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002391
2392 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002393 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002394 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002395}
2396
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002397typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2398
2399/* apply the function to all matching slave ctls in the mixer list */
2400static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002401 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002402{
2403 struct hda_nid_item *items;
2404 const char * const *s;
2405 int i, err;
2406
2407 items = codec->mixers.list;
2408 for (i = 0; i < codec->mixers.used; i++) {
2409 struct snd_kcontrol *sctl = items[i].kctl;
2410 if (!sctl || !sctl->id.name ||
2411 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2412 continue;
2413 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002414 char tmpname[sizeof(sctl->id.name)];
2415 const char *name = *s;
2416 if (suffix) {
2417 snprintf(tmpname, sizeof(tmpname), "%s %s",
2418 name, suffix);
2419 name = tmpname;
2420 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002421 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002422 err = func(data, sctl);
2423 if (err)
2424 return err;
2425 break;
2426 }
2427 }
2428 }
2429 return 0;
2430}
2431
2432static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2433{
2434 return 1;
2435}
2436
Takashi Iwai18478e82012-03-09 17:51:10 +01002437/* guess the value corresponding to 0dB */
2438static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2439{
2440 int _tlv[4];
2441 const int *tlv = NULL;
2442 int val = -1;
2443
2444 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2445 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2446 mm_segment_t fs = get_fs();
2447 set_fs(get_ds());
2448 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2449 tlv = _tlv;
2450 set_fs(fs);
2451 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2452 tlv = kctl->tlv.p;
2453 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2454 val = -tlv[2] / tlv[3];
2455 return val;
2456}
2457
2458/* call kctl->put with the given value(s) */
2459static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2460{
2461 struct snd_ctl_elem_value *ucontrol;
2462 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2463 if (!ucontrol)
2464 return -ENOMEM;
2465 ucontrol->value.integer.value[0] = val;
2466 ucontrol->value.integer.value[1] = val;
2467 kctl->put(kctl, ucontrol);
2468 kfree(ucontrol);
2469 return 0;
2470}
2471
2472/* initialize the slave volume with 0dB */
2473static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2474{
2475 int offset = get_kctl_0dB_offset(slave);
2476 if (offset > 0)
2477 put_kctl_with_value(slave, offset);
2478 return 0;
2479}
2480
2481/* unmute the slave */
2482static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2483{
2484 return put_kctl_with_value(slave, 1);
2485}
2486
Takashi Iwaid5191e52009-11-16 14:58:17 +01002487/**
2488 * snd_hda_add_vmaster - create a virtual master control and add slaves
2489 * @codec: HD-audio codec
2490 * @name: vmaster control name
2491 * @tlv: TLV data (optional)
2492 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002493 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002494 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002495 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002496 *
2497 * Create a virtual master control with the given name. The TLV data
2498 * must be either NULL or a valid data.
2499 *
2500 * @slaves is a NULL-terminated array of strings, each of which is a
2501 * slave control name. All controls with these names are assigned to
2502 * the new virtual master control.
2503 *
2504 * This function returns zero if successful or a negative error code.
2505 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002506int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002507 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002508 const char *suffix, bool init_slave_vol,
2509 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002510{
2511 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002512 int err;
2513
Takashi Iwai29e58532012-03-12 12:25:03 +01002514 if (ctl_ret)
2515 *ctl_ret = NULL;
2516
Takashi Iwai9322ca52012-02-03 14:28:01 +01002517 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002518 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002519 snd_printdd("No slave found for %s\n", name);
2520 return 0;
2521 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002522 kctl = snd_ctl_make_virtual_master(name, tlv);
2523 if (!kctl)
2524 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002525 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002526 if (err < 0)
2527 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002528
Takashi Iwai9322ca52012-02-03 14:28:01 +01002529 err = map_slaves(codec, slaves, suffix,
2530 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002531 if (err < 0)
2532 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002533
2534 /* init with master mute & zero volume */
2535 put_kctl_with_value(kctl, 0);
2536 if (init_slave_vol)
2537 map_slaves(codec, slaves, suffix,
2538 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2539
Takashi Iwai29e58532012-03-12 12:25:03 +01002540 if (ctl_ret)
2541 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002542 return 0;
2543}
Takashi Iwai18478e82012-03-09 17:51:10 +01002544EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002545
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002546/*
2547 * mute-LED control using vmaster
2548 */
2549static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_info *uinfo)
2551{
2552 static const char * const texts[] = {
2553 "Off", "On", "Follow Master"
2554 };
2555 unsigned int index;
2556
2557 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2558 uinfo->count = 1;
2559 uinfo->value.enumerated.items = 3;
2560 index = uinfo->value.enumerated.item;
2561 if (index >= 3)
2562 index = 2;
2563 strcpy(uinfo->value.enumerated.name, texts[index]);
2564 return 0;
2565}
2566
2567static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2569{
2570 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2571 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2572 return 0;
2573}
2574
2575static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_value *ucontrol)
2577{
2578 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2579 unsigned int old_mode = hook->mute_mode;
2580
2581 hook->mute_mode = ucontrol->value.enumerated.item[0];
2582 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2583 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2584 if (old_mode == hook->mute_mode)
2585 return 0;
2586 snd_hda_sync_vmaster_hook(hook);
2587 return 1;
2588}
2589
2590static struct snd_kcontrol_new vmaster_mute_mode = {
2591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2592 .name = "Mute-LED Mode",
2593 .info = vmaster_mute_mode_info,
2594 .get = vmaster_mute_mode_get,
2595 .put = vmaster_mute_mode_put,
2596};
2597
2598/*
2599 * Add a mute-LED hook with the given vmaster switch kctl
2600 * "Mute-LED Mode" control is automatically created and associated with
2601 * the given hook.
2602 */
2603int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002604 struct hda_vmaster_mute_hook *hook,
2605 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002606{
2607 struct snd_kcontrol *kctl;
2608
2609 if (!hook->hook || !hook->sw_kctl)
2610 return 0;
2611 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2612 hook->codec = codec;
2613 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002614 if (!expose_enum_ctl)
2615 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002616 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2617 if (!kctl)
2618 return -ENOMEM;
2619 return snd_hda_ctl_add(codec, 0, kctl);
2620}
2621EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2622
2623/*
2624 * Call the hook with the current value for synchronization
2625 * Should be called in init callback
2626 */
2627void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2628{
2629 if (!hook->hook || !hook->codec)
2630 return;
2631 switch (hook->mute_mode) {
2632 case HDA_VMUTE_FOLLOW_MASTER:
2633 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2634 break;
2635 default:
2636 hook->hook(hook->codec, hook->mute_mode);
2637 break;
2638 }
2639}
2640EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2641
2642
Takashi Iwaid5191e52009-11-16 14:58:17 +01002643/**
2644 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2645 *
2646 * The control element is supposed to have the private_value field
2647 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2648 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002649int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 int chs = get_amp_channels(kcontrol);
2653
2654 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2655 uinfo->count = chs == 3 ? 2 : 1;
2656 uinfo->value.integer.min = 0;
2657 uinfo->value.integer.max = 1;
2658 return 0;
2659}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002660EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Takashi Iwaid5191e52009-11-16 14:58:17 +01002662/**
2663 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2664 *
2665 * The control element is supposed to have the private_value field
2666 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002668int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 hda_nid_t nid = get_amp_nid(kcontrol);
2673 int chs = get_amp_channels(kcontrol);
2674 int dir = get_amp_direction(kcontrol);
2675 int idx = get_amp_index(kcontrol);
2676 long *valp = ucontrol->value.integer.value;
2677
2678 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002679 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002680 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002682 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002683 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return 0;
2685}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002686EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Takashi Iwaid5191e52009-11-16 14:58:17 +01002688/**
2689 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2690 *
2691 * The control element is supposed to have the private_value field
2692 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2693 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2698 hda_nid_t nid = get_amp_nid(kcontrol);
2699 int chs = get_amp_channels(kcontrol);
2700 int dir = get_amp_direction(kcontrol);
2701 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 long *valp = ucontrol->value.integer.value;
2703 int change = 0;
2704
Takashi Iwaicb53c622007-08-10 17:21:45 +02002705 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002706 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002707 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002708 HDA_AMP_MUTE,
2709 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002710 valp++;
2711 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002712 if (chs & 2)
2713 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002714 HDA_AMP_MUTE,
2715 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002716 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002717 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return change;
2719}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002720EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722/*
Takashi Iwai985be542005-11-02 18:26:49 +01002723 * bound volume controls
2724 *
2725 * bind multiple volumes (# indices, from 0)
2726 */
2727
2728#define AMP_VAL_IDX_SHIFT 19
2729#define AMP_VAL_IDX_MASK (0x0f<<19)
2730
Takashi Iwaid5191e52009-11-16 14:58:17 +01002731/**
2732 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2733 *
2734 * The control element is supposed to have the private_value field
2735 * set up via HDA_BIND_MUTE*() macros.
2736 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002737int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002739{
2740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2741 unsigned long pval;
2742 int err;
2743
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002744 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002745 pval = kcontrol->private_value;
2746 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2747 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2748 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002749 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002750 return err;
2751}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002752EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002753
Takashi Iwaid5191e52009-11-16 14:58:17 +01002754/**
2755 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2756 *
2757 * The control element is supposed to have the private_value field
2758 * set up via HDA_BIND_MUTE*() macros.
2759 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002760int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2761 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002762{
2763 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2764 unsigned long pval;
2765 int i, indices, err = 0, change = 0;
2766
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002767 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002768 pval = kcontrol->private_value;
2769 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2770 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002771 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2772 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002773 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2774 if (err < 0)
2775 break;
2776 change |= err;
2777 }
2778 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002779 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002780 return err < 0 ? err : change;
2781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002782EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002783
Takashi Iwaid5191e52009-11-16 14:58:17 +01002784/**
2785 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2786 *
2787 * The control element is supposed to have the private_value field
2788 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002789 */
2790int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_info *uinfo)
2792{
2793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2794 struct hda_bind_ctls *c;
2795 int err;
2796
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002797 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002798 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002799 kcontrol->private_value = *c->values;
2800 err = c->ops->info(kcontrol, uinfo);
2801 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002802 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002803 return err;
2804}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002805EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002806
Takashi Iwaid5191e52009-11-16 14:58:17 +01002807/**
2808 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2809 *
2810 * The control element is supposed to have the private_value field
2811 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2812 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002813int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_value *ucontrol)
2815{
2816 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2817 struct hda_bind_ctls *c;
2818 int err;
2819
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002820 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002821 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002822 kcontrol->private_value = *c->values;
2823 err = c->ops->get(kcontrol, ucontrol);
2824 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002825 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002826 return err;
2827}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002828EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002829
Takashi Iwaid5191e52009-11-16 14:58:17 +01002830/**
2831 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2832 *
2833 * The control element is supposed to have the private_value field
2834 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2835 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002836int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2838{
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 struct hda_bind_ctls *c;
2841 unsigned long *vals;
2842 int err = 0, change = 0;
2843
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002844 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002845 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002846 for (vals = c->values; *vals; vals++) {
2847 kcontrol->private_value = *vals;
2848 err = c->ops->put(kcontrol, ucontrol);
2849 if (err < 0)
2850 break;
2851 change |= err;
2852 }
2853 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002854 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002855 return err < 0 ? err : change;
2856}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002857EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002858
Takashi Iwaid5191e52009-11-16 14:58:17 +01002859/**
2860 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2861 *
2862 * The control element is supposed to have the private_value field
2863 * set up via HDA_BIND_VOL() macro.
2864 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002865int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2866 unsigned int size, unsigned int __user *tlv)
2867{
2868 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2869 struct hda_bind_ctls *c;
2870 int err;
2871
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002872 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002873 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002874 kcontrol->private_value = *c->values;
2875 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2876 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002877 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002878 return err;
2879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002880EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002881
2882struct hda_ctl_ops snd_hda_bind_vol = {
2883 .info = snd_hda_mixer_amp_volume_info,
2884 .get = snd_hda_mixer_amp_volume_get,
2885 .put = snd_hda_mixer_amp_volume_put,
2886 .tlv = snd_hda_mixer_amp_tlv
2887};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002888EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002889
2890struct hda_ctl_ops snd_hda_bind_sw = {
2891 .info = snd_hda_mixer_amp_switch_info,
2892 .get = snd_hda_mixer_amp_switch_get,
2893 .put = snd_hda_mixer_amp_switch_put,
2894 .tlv = snd_hda_mixer_amp_tlv
2895};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002896EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002897
2898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 * SPDIF out controls
2900 */
2901
Takashi Iwai0ba21762007-04-16 11:29:14 +02002902static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2906 uinfo->count = 1;
2907 return 0;
2908}
2909
Takashi Iwai0ba21762007-04-16 11:29:14 +02002910static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2914 IEC958_AES0_NONAUDIO |
2915 IEC958_AES0_CON_EMPHASIS_5015 |
2916 IEC958_AES0_CON_NOT_COPYRIGHT;
2917 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2918 IEC958_AES1_CON_ORIGINAL;
2919 return 0;
2920}
2921
Takashi Iwai0ba21762007-04-16 11:29:14 +02002922static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2923 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2926 IEC958_AES0_NONAUDIO |
2927 IEC958_AES0_PRO_EMPHASIS_5015;
2928 return 0;
2929}
2930
Takashi Iwai0ba21762007-04-16 11:29:14 +02002931static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002935 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002936 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002938 mutex_lock(&codec->spdif_mutex);
2939 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002940 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2941 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2942 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2943 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002944 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 return 0;
2947}
2948
2949/* convert from SPDIF status bits to HDA SPDIF bits
2950 * bit 0 (DigEn) is always set zero (to be filled later)
2951 */
2952static unsigned short convert_from_spdif_status(unsigned int sbits)
2953{
2954 unsigned short val = 0;
2955
2956 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002957 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002959 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2962 IEC958_AES0_PRO_EMPHASIS_5015)
2963 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002965 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2966 IEC958_AES0_CON_EMPHASIS_5015)
2967 val |= AC_DIG1_EMPHASIS;
2968 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2969 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2973 }
2974 return val;
2975}
2976
2977/* convert to SPDIF status bits from HDA SPDIF bits
2978 */
2979static unsigned int convert_to_spdif_status(unsigned short val)
2980{
2981 unsigned int sbits = 0;
2982
Takashi Iwai0ba21762007-04-16 11:29:14 +02002983 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 sbits |= IEC958_AES0_PROFESSIONAL;
2987 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2990 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002995 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2997 sbits |= val & (0x7f << 8);
2998 }
2999 return sbits;
3000}
3001
Takashi Iwai2f728532008-09-25 16:32:41 +02003002/* set digital convert verbs both for the given NID and its slaves */
3003static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3004 int verb, int val)
3005{
Takashi Iwaidda14412011-05-02 11:29:30 +02003006 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003007
Takashi Iwai9e976972008-11-25 08:17:20 +01003008 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003009 d = codec->slave_dig_outs;
3010 if (!d)
3011 return;
3012 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003013 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003014}
3015
3016static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3017 int dig1, int dig2)
3018{
3019 if (dig1 != -1)
3020 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3021 if (dig2 != -1)
3022 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3023}
3024
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
3028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003029 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003030 struct hda_spdif_out *spdif;
3031 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 unsigned short val;
3033 int change;
3034
Ingo Molnar62932df2006-01-16 16:34:20 +01003035 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003036 spdif = snd_array_elem(&codec->spdif_out, idx);
3037 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003038 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3040 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3041 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003042 val = convert_from_spdif_status(spdif->status);
3043 val |= spdif->ctls & 1;
3044 change = spdif->ctls != val;
3045 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003046 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003047 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003048 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return change;
3050}
3051
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003052#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3055 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003058 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003059 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003061 mutex_lock(&codec->spdif_mutex);
3062 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003063 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return 0;
3066}
3067
Stephen Warren74b654c2011-06-01 11:14:18 -06003068static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3069 int dig1, int dig2)
3070{
3071 set_dig_out_convert(codec, nid, dig1, dig2);
3072 /* unmute amp switch (if any) */
3073 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3074 (dig1 & AC_DIG1_ENABLE))
3075 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3076 HDA_AMP_MUTE, 0);
3077}
3078
Takashi Iwai0ba21762007-04-16 11:29:14 +02003079static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003083 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003084 struct hda_spdif_out *spdif;
3085 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 unsigned short val;
3087 int change;
3088
Ingo Molnar62932df2006-01-16 16:34:20 +01003089 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003090 spdif = snd_array_elem(&codec->spdif_out, idx);
3091 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003092 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003094 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003095 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003096 spdif->ctls = val;
3097 if (change && nid != (u16)-1)
3098 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003099 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return change;
3101}
3102
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003103static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 {
3105 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003107 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .info = snd_hda_spdif_mask_info,
3109 .get = snd_hda_spdif_cmask_get,
3110 },
3111 {
3112 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003114 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 .info = snd_hda_spdif_mask_info,
3116 .get = snd_hda_spdif_pmask_get,
3117 },
3118 {
3119 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003120 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 .info = snd_hda_spdif_mask_info,
3122 .get = snd_hda_spdif_default_get,
3123 .put = snd_hda_spdif_default_put,
3124 },
3125 {
3126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003127 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 .info = snd_hda_spdif_out_switch_info,
3129 .get = snd_hda_spdif_out_switch_get,
3130 .put = snd_hda_spdif_out_switch_put,
3131 },
3132 { } /* end */
3133};
3134
3135/**
3136 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3137 * @codec: the HDA codec
3138 * @nid: audio out widget NID
3139 *
3140 * Creates controls related with the SPDIF output.
3141 * Called from each patch supporting the SPDIF out.
3142 *
3143 * Returns 0 if successful, or a negative error code.
3144 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003145int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3146 hda_nid_t associated_nid,
3147 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003150 struct snd_kcontrol *kctl;
3151 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003152 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003153 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Takashi Iwai1afe2062010-12-23 10:17:52 +01003155 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3156 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003157 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3158 return -EBUSY;
3159 }
Stephen Warren7c935972011-06-01 11:14:17 -06003160 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3162 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003163 if (!kctl)
3164 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003165 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003166 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003167 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003168 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return err;
3170 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003171 spdif->nid = cvt_nid;
3172 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003173 AC_VERB_GET_DIGI_CONVERT_1, 0);
3174 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
3176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003177EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003179/* get the hda_spdif_out entry from the given NID
3180 * call within spdif_mutex lock
3181 */
Stephen Warren7c935972011-06-01 11:14:17 -06003182struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3183 hda_nid_t nid)
3184{
3185 int i;
3186 for (i = 0; i < codec->spdif_out.used; i++) {
3187 struct hda_spdif_out *spdif =
3188 snd_array_elem(&codec->spdif_out, i);
3189 if (spdif->nid == nid)
3190 return spdif;
3191 }
3192 return NULL;
3193}
3194EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3195
Stephen Warren74b654c2011-06-01 11:14:18 -06003196void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3197{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003198 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003199
3200 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003201 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003202 spdif->nid = (u16)-1;
3203 mutex_unlock(&codec->spdif_mutex);
3204}
3205EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3206
3207void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3208{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003209 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003210 unsigned short val;
3211
3212 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003213 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003214 if (spdif->nid != nid) {
3215 spdif->nid = nid;
3216 val = spdif->ctls;
3217 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3218 }
3219 mutex_unlock(&codec->spdif_mutex);
3220}
3221EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003224 * SPDIF sharing with analog output
3225 */
3226static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3227 struct snd_ctl_elem_value *ucontrol)
3228{
3229 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3230 ucontrol->value.integer.value[0] = mout->share_spdif;
3231 return 0;
3232}
3233
3234static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3235 struct snd_ctl_elem_value *ucontrol)
3236{
3237 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3238 mout->share_spdif = !!ucontrol->value.integer.value[0];
3239 return 0;
3240}
3241
3242static struct snd_kcontrol_new spdif_share_sw = {
3243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3244 .name = "IEC958 Default PCM Playback Switch",
3245 .info = snd_ctl_boolean_mono_info,
3246 .get = spdif_share_sw_get,
3247 .put = spdif_share_sw_put,
3248};
3249
Takashi Iwaid5191e52009-11-16 14:58:17 +01003250/**
3251 * snd_hda_create_spdif_share_sw - create Default PCM switch
3252 * @codec: the HDA codec
3253 * @mout: multi-out instance
3254 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003255int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3256 struct hda_multi_out *mout)
3257{
3258 if (!mout->dig_out_nid)
3259 return 0;
3260 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003261 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3262 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003264EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003265
3266/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 * SPDIF input
3268 */
3269
3270#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3271
Takashi Iwai0ba21762007-04-16 11:29:14 +02003272static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3276
3277 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3278 return 0;
3279}
3280
Takashi Iwai0ba21762007-04-16 11:29:14 +02003281static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3282 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3285 hda_nid_t nid = kcontrol->private_value;
3286 unsigned int val = !!ucontrol->value.integer.value[0];
3287 int change;
3288
Ingo Molnar62932df2006-01-16 16:34:20 +01003289 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003291 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003293 snd_hda_codec_write_cache(codec, nid, 0,
3294 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003296 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return change;
3298}
3299
Takashi Iwai0ba21762007-04-16 11:29:14 +02003300static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3301 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3304 hda_nid_t nid = kcontrol->private_value;
3305 unsigned short val;
3306 unsigned int sbits;
3307
Andrew Paprocki3982d172007-12-19 12:13:44 +01003308 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 sbits = convert_to_spdif_status(val);
3310 ucontrol->value.iec958.status[0] = sbits;
3311 ucontrol->value.iec958.status[1] = sbits >> 8;
3312 ucontrol->value.iec958.status[2] = sbits >> 16;
3313 ucontrol->value.iec958.status[3] = sbits >> 24;
3314 return 0;
3315}
3316
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003317static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 {
3319 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003320 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 .info = snd_hda_spdif_in_switch_info,
3322 .get = snd_hda_spdif_in_switch_get,
3323 .put = snd_hda_spdif_in_switch_put,
3324 },
3325 {
3326 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3327 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003328 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 .info = snd_hda_spdif_mask_info,
3330 .get = snd_hda_spdif_in_status_get,
3331 },
3332 { } /* end */
3333};
3334
3335/**
3336 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3337 * @codec: the HDA codec
3338 * @nid: audio in widget NID
3339 *
3340 * Creates controls related with the SPDIF input.
3341 * Called from each patch supporting the SPDIF in.
3342 *
3343 * Returns 0 if successful, or a negative error code.
3344 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003345int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003348 struct snd_kcontrol *kctl;
3349 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003350 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Takashi Iwai1afe2062010-12-23 10:17:52 +01003352 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3353 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003354 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3355 return -EBUSY;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3358 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003359 if (!kctl)
3360 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003362 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003363 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return err;
3365 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003366 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003367 snd_hda_codec_read(codec, nid, 0,
3368 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return 0;
3371}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003372EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Takashi Iwai2a439522011-07-26 09:52:50 +02003374#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003375/*
3376 * command cache
3377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003379/* build a 32bit cache key with the widget id and the command parameter */
3380#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3381#define get_cmd_cache_nid(key) ((key) & 0xff)
3382#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3383
3384/**
3385 * snd_hda_codec_write_cache - send a single command with caching
3386 * @codec: the HDA codec
3387 * @nid: NID to send the command
3388 * @direct: direct flag
3389 * @verb: the verb to send
3390 * @parm: the parameter for the verb
3391 *
3392 * Send a single command without waiting for response.
3393 *
3394 * Returns 0 if successful, or a negative error code.
3395 */
3396int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3397 int direct, unsigned int verb, unsigned int parm)
3398{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003399 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3400 struct hda_cache_head *c;
3401 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003402
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003403 if (err < 0)
3404 return err;
3405 /* parm may contain the verb stuff for get/set amp */
3406 verb = verb | (parm >> 8);
3407 parm &= 0xff;
3408 key = build_cmd_cache_key(nid, verb);
3409 mutex_lock(&codec->bus->cmd_mutex);
3410 c = get_alloc_hash(&codec->cmd_cache, key);
3411 if (c)
3412 c->val = parm;
3413 mutex_unlock(&codec->bus->cmd_mutex);
3414 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003416EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003417
Takashi Iwaid5191e52009-11-16 14:58:17 +01003418/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003419 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3420 * @codec: the HDA codec
3421 * @nid: NID to send the command
3422 * @direct: direct flag
3423 * @verb: the verb to send
3424 * @parm: the parameter for the verb
3425 *
3426 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3427 * command if the parameter is already identical with the cached value.
3428 * If not, it sends the command and refreshes the cache.
3429 *
3430 * Returns 0 if successful, or a negative error code.
3431 */
3432int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3433 int direct, unsigned int verb, unsigned int parm)
3434{
3435 struct hda_cache_head *c;
3436 u32 key;
3437
3438 /* parm may contain the verb stuff for get/set amp */
3439 verb = verb | (parm >> 8);
3440 parm &= 0xff;
3441 key = build_cmd_cache_key(nid, verb);
3442 mutex_lock(&codec->bus->cmd_mutex);
3443 c = get_hash(&codec->cmd_cache, key);
3444 if (c && c->val == parm) {
3445 mutex_unlock(&codec->bus->cmd_mutex);
3446 return 0;
3447 }
3448 mutex_unlock(&codec->bus->cmd_mutex);
3449 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3450}
3451EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3452
3453/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003454 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3455 * @codec: HD-audio codec
3456 *
3457 * Execute all verbs recorded in the command caches to resume.
3458 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003459void snd_hda_codec_resume_cache(struct hda_codec *codec)
3460{
Takashi Iwai603c4012008-07-30 15:01:44 +02003461 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003462 int i;
3463
Takashi Iwai603c4012008-07-30 15:01:44 +02003464 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003465 u32 key = buffer->key;
3466 if (!key)
3467 continue;
3468 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3469 get_cmd_cache_cmd(key), buffer->val);
3470 }
3471}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003472EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003473
3474/**
3475 * snd_hda_sequence_write_cache - sequence writes with caching
3476 * @codec: the HDA codec
3477 * @seq: VERB array to send
3478 *
3479 * Send the commands sequentially from the given array.
3480 * Thte commands are recorded on cache for power-save and resume.
3481 * The array must be terminated with NID=0.
3482 */
3483void snd_hda_sequence_write_cache(struct hda_codec *codec,
3484 const struct hda_verb *seq)
3485{
3486 for (; seq->nid; seq++)
3487 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3488 seq->param);
3489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003490EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003491#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003492
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003493void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3494 unsigned int power_state,
3495 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003496{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003497 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003498 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003499
Takashi Iwaicb53c622007-08-10 17:21:45 +02003500 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003501 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003502 if (!(wcaps & AC_WCAP_POWER))
3503 continue;
3504 /* don't power down the widget if it controls eapd and
3505 * EAPD_BTLENABLE is set.
3506 */
3507 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3508 get_wcaps_type(wcaps) == AC_WID_PIN &&
3509 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3510 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003511 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003512 if (eapd & 0x02)
3513 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003514 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003515 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3516 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003517 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003518}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003519EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3520
3521/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003522 * supported power states check
3523 */
3524static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3525 unsigned int power_state)
3526{
3527 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3528
Mengdong Line037cb42012-08-10 14:11:58 +02003529 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003530 return false;
3531 if (sup & power_state)
3532 return true;
3533 else
3534 return false;
3535}
3536
3537/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003538 * wait until the state is reached, returns the current state
3539 */
3540static unsigned int hda_sync_power_state(struct hda_codec *codec,
3541 hda_nid_t fg,
3542 unsigned int power_state)
3543{
3544 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3545 unsigned int state, actual_state;
3546
3547 for (;;) {
3548 state = snd_hda_codec_read(codec, fg, 0,
3549 AC_VERB_GET_POWER_STATE, 0);
3550 if (state & AC_PWRST_ERROR)
3551 break;
3552 actual_state = (state >> 4) & 0x0f;
3553 if (actual_state == power_state)
3554 break;
3555 if (time_after_eq(jiffies, end_time))
3556 break;
3557 /* wait until the codec reachs to the target state */
3558 msleep(1);
3559 }
3560 return state;
3561}
3562
3563/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003564 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003565 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003566static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003567 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003568{
Takashi Iwaid8193872012-08-31 07:54:38 -07003569 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003570 int count;
3571 unsigned int state;
3572
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003573 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003574 if (power_state == AC_PWRST_D3) {
3575 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003576 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003577 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003578
3579 /* repeat power states setting at most 10 times*/
3580 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003581 if (codec->patch_ops.set_power_state)
3582 codec->patch_ops.set_power_state(codec, fg,
3583 power_state);
3584 else {
3585 snd_hda_codec_read(codec, fg, 0,
3586 AC_VERB_SET_POWER_STATE,
3587 power_state);
3588 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3589 true);
3590 }
3591 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003592 if (!(state & AC_PWRST_ERROR))
3593 break;
3594 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003595
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003596 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003597}
Takashi Iwai54d17402005-11-21 16:33:22 +01003598
Takashi Iwai11aeff02008-07-30 15:01:46 +02003599#ifdef CONFIG_SND_HDA_HWDEP
3600/* execute additional init verbs */
3601static void hda_exec_init_verbs(struct hda_codec *codec)
3602{
3603 if (codec->init_verbs.list)
3604 snd_hda_sequence_write(codec, codec->init_verbs.list);
3605}
3606#else
3607static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3608#endif
3609
Takashi Iwai2a439522011-07-26 09:52:50 +02003610#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003611/*
3612 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003613 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003614 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003615static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003616{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003617 unsigned int state;
3618
Takashi Iwaicb53c622007-08-10 17:21:45 +02003619 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003620 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003621 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003622 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003623 /* Cancel delayed work if we aren't currently running from it. */
3624 if (!in_wq)
3625 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003626 spin_lock(&codec->power_lock);
3627 snd_hda_update_power_acct(codec);
3628 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003629 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003630 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003631 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003632 spin_unlock(&codec->power_lock);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003633 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003634}
3635
3636/*
3637 * kick up codec; used both from PM and power-save
3638 */
3639static void hda_call_codec_resume(struct hda_codec *codec)
3640{
Takashi Iwai7f308302012-05-08 16:52:23 +02003641 /* set as if powered on for avoiding re-entering the resume
3642 * in the resume / power-save sequence
3643 */
3644 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003645 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003646 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003647 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003648 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003649 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003650 if (codec->patch_ops.resume)
3651 codec->patch_ops.resume(codec);
3652 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003653 if (codec->patch_ops.init)
3654 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003655 snd_hda_codec_resume_amp(codec);
3656 snd_hda_codec_resume_cache(codec);
3657 }
Takashi Iwai8a535412012-08-22 16:40:24 +02003658 snd_hda_jack_report_sync(codec);
Takashi Iwai7f308302012-05-08 16:52:23 +02003659 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003660}
Takashi Iwai2a439522011-07-26 09:52:50 +02003661#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003662
Takashi Iwai54d17402005-11-21 16:33:22 +01003663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664/**
3665 * snd_hda_build_controls - build mixer controls
3666 * @bus: the BUS
3667 *
3668 * Creates mixer controls for each codec included in the bus.
3669 *
3670 * Returns 0 if successful, otherwise a negative error code.
3671 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003672int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003674 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Takashi Iwai0ba21762007-04-16 11:29:14 +02003676 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003677 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003678 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003679 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003680 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003681 err = snd_hda_codec_reset(codec);
3682 if (err < 0) {
3683 printk(KERN_ERR
3684 "hda_codec: cannot revert codec\n");
3685 return err;
3686 }
3687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003689 return 0;
3690}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003691EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003693/*
3694 * add standard channel maps if not specified
3695 */
3696static int add_std_chmaps(struct hda_codec *codec)
3697{
3698 int i, str, err;
3699
3700 for (i = 0; i < codec->num_pcms; i++) {
3701 for (str = 0; str < 2; str++) {
3702 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3703 struct hda_pcm_stream *hinfo =
3704 &codec->pcm_info[i].stream[str];
3705 struct snd_pcm_chmap *chmap;
3706
3707 if (codec->pcm_info[i].own_chmap)
3708 continue;
3709 if (!pcm || !hinfo->substreams)
3710 continue;
3711 err = snd_pcm_add_chmap_ctls(pcm, str,
3712 snd_pcm_std_chmaps,
3713 hinfo->channels_max,
3714 0, &chmap);
3715 if (err < 0)
3716 return err;
3717 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3718 }
3719 }
3720 return 0;
3721}
3722
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003723int snd_hda_codec_build_controls(struct hda_codec *codec)
3724{
3725 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003726 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003727 /* continue to initialize... */
3728 if (codec->patch_ops.init)
3729 err = codec->patch_ops.init(codec);
3730 if (!err && codec->patch_ops.build_controls)
3731 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003732 if (err < 0)
3733 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003734
3735 /* we create chmaps here instead of build_pcms */
3736 err = add_std_chmaps(codec);
3737 if (err < 0)
3738 return err;
3739
Takashi Iwai8a535412012-08-22 16:40:24 +02003740 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return 0;
3742}
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744/*
3745 * stream formats
3746 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003747struct hda_rate_tbl {
3748 unsigned int hz;
3749 unsigned int alsa_bits;
3750 unsigned int hda_fmt;
3751};
3752
Takashi Iwai92f10b32010-08-03 14:21:00 +02003753/* rate = base * mult / div */
3754#define HDA_RATE(base, mult, div) \
3755 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3756 (((div) - 1) << AC_FMT_DIV_SHIFT))
3757
Takashi Iwaibefdf312005-08-22 13:57:55 +02003758static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003760
3761 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003762 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3763 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3764 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3765 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3766 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3767 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3768 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3769 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3770 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3771 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3772 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003773#define AC_PAR_PCM_RATE_BITS 11
3774 /* up to bits 10, 384kHZ isn't supported properly */
3775
3776 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003777 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003778
Takashi Iwaibefdf312005-08-22 13:57:55 +02003779 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780};
3781
3782/**
3783 * snd_hda_calc_stream_format - calculate format bitset
3784 * @rate: the sample rate
3785 * @channels: the number of channels
3786 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3787 * @maxbps: the max. bps
3788 *
3789 * Calculate the format bitset from the given rate, channels and th PCM format.
3790 *
3791 * Return zero if invalid.
3792 */
3793unsigned int snd_hda_calc_stream_format(unsigned int rate,
3794 unsigned int channels,
3795 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003796 unsigned int maxbps,
3797 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 int i;
3800 unsigned int val = 0;
3801
Takashi Iwaibefdf312005-08-22 13:57:55 +02003802 for (i = 0; rate_bits[i].hz; i++)
3803 if (rate_bits[i].hz == rate) {
3804 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 break;
3806 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003807 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 snd_printdd("invalid rate %d\n", rate);
3809 return 0;
3810 }
3811
3812 if (channels == 0 || channels > 8) {
3813 snd_printdd("invalid channels %d\n", channels);
3814 return 0;
3815 }
3816 val |= channels - 1;
3817
3818 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003819 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003820 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003821 break;
3822 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003823 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 case 20:
3826 case 24:
3827 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003828 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003829 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003831 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003833 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 break;
3835 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003836 snd_printdd("invalid format width %d\n",
3837 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return 0;
3839 }
3840
Anssi Hannula32c168c2010-08-03 13:28:57 +03003841 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003842 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return val;
3845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003846EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003848static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3849 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003850{
3851 unsigned int val = 0;
3852 if (nid != codec->afg &&
3853 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3854 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3855 if (!val || val == -1)
3856 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3857 if (!val || val == -1)
3858 return 0;
3859 return val;
3860}
3861
3862static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3863{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003864 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003865 get_pcm_param);
3866}
3867
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003868static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3869 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003870{
3871 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3872 if (!streams || streams == -1)
3873 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3874 if (!streams || streams == -1)
3875 return 0;
3876 return streams;
3877}
3878
3879static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3880{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003881 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003882 get_stream_param);
3883}
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885/**
3886 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3887 * @codec: the HDA codec
3888 * @nid: NID to query
3889 * @ratesp: the pointer to store the detected rate bitflags
3890 * @formatsp: the pointer to store the detected formats
3891 * @bpsp: the pointer to store the detected format widths
3892 *
3893 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3894 * or @bsps argument is ignored.
3895 *
3896 * Returns 0 if successful, otherwise a negative error code.
3897 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003898int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3900{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003901 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003903 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003904 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 if (ratesp) {
3907 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003908 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003910 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003912 if (rates == 0) {
3913 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3914 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3915 nid, val,
3916 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3917 return -EIO;
3918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 *ratesp = rates;
3920 }
3921
3922 if (formatsp || bpsp) {
3923 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003924 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003926 streams = query_stream_param(codec, nid);
3927 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 bps = 0;
3931 if (streams & AC_SUPFMT_PCM) {
3932 if (val & AC_SUPPCM_BITS_8) {
3933 formats |= SNDRV_PCM_FMTBIT_U8;
3934 bps = 8;
3935 }
3936 if (val & AC_SUPPCM_BITS_16) {
3937 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3938 bps = 16;
3939 }
3940 if (wcaps & AC_WCAP_DIGITAL) {
3941 if (val & AC_SUPPCM_BITS_32)
3942 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3943 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3944 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3945 if (val & AC_SUPPCM_BITS_24)
3946 bps = 24;
3947 else if (val & AC_SUPPCM_BITS_20)
3948 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003949 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3950 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3952 if (val & AC_SUPPCM_BITS_32)
3953 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 else if (val & AC_SUPPCM_BITS_24)
3955 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003956 else if (val & AC_SUPPCM_BITS_20)
3957 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
3959 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003960#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003961 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003963 if (!bps)
3964 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003965 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003966#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003967 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003968 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 /* temporary hack: we have still no proper support
3970 * for the direct AC3 stream...
3971 */
3972 formats |= SNDRV_PCM_FMTBIT_U8;
3973 bps = 8;
3974 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003975 if (formats == 0) {
3976 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3977 "(nid=0x%x, val=0x%x, ovrd=%i, "
3978 "streams=0x%x)\n",
3979 nid, val,
3980 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3981 streams);
3982 return -EIO;
3983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (formatsp)
3985 *formatsp = formats;
3986 if (bpsp)
3987 *bpsp = bps;
3988 }
3989
3990 return 0;
3991}
Stephen Warren384a48d2011-06-01 11:14:21 -06003992EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003995 * snd_hda_is_supported_format - Check the validity of the format
3996 * @codec: HD-audio codec
3997 * @nid: NID to check
3998 * @format: the HD-audio format value to check
3999 *
4000 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 *
4002 * Returns 1 if supported, 0 if not.
4003 */
4004int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4005 unsigned int format)
4006{
4007 int i;
4008 unsigned int val = 0, rate, stream;
4009
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004010 val = query_pcm_param(codec, nid);
4011 if (!val)
4012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004015 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004016 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 if (val & (1 << i))
4018 break;
4019 return 0;
4020 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004021 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return 0;
4023
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004024 stream = query_stream_param(codec, nid);
4025 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 return 0;
4027
4028 if (stream & AC_SUPFMT_PCM) {
4029 switch (format & 0xf0) {
4030 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004031 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return 0;
4033 break;
4034 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004035 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return 0;
4037 break;
4038 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004039 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return 0;
4041 break;
4042 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004043 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return 0;
4045 break;
4046 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004047 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return 0;
4049 break;
4050 default:
4051 return 0;
4052 }
4053 } else {
4054 /* FIXME: check for float32 and AC3? */
4055 }
4056
4057 return 1;
4058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004059EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061/*
4062 * PCM stuff
4063 */
4064static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4065 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004066 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 return 0;
4069}
4070
4071static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4072 struct hda_codec *codec,
4073 unsigned int stream_tag,
4074 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004075 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
4077 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4078 return 0;
4079}
4080
4081static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4082 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004083 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Takashi Iwai888afa12008-03-18 09:57:50 +01004085 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return 0;
4087}
4088
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004089static int set_pcm_default_values(struct hda_codec *codec,
4090 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004092 int err;
4093
Takashi Iwai0ba21762007-04-16 11:29:14 +02004094 /* query support PCM information from the given NID */
4095 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004096 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004097 info->rates ? NULL : &info->rates,
4098 info->formats ? NULL : &info->formats,
4099 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004100 if (err < 0)
4101 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103 if (info->ops.open == NULL)
4104 info->ops.open = hda_pcm_default_open_close;
4105 if (info->ops.close == NULL)
4106 info->ops.close = hda_pcm_default_open_close;
4107 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004108 if (snd_BUG_ON(!info->nid))
4109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 info->ops.prepare = hda_pcm_default_prepare;
4111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004113 if (snd_BUG_ON(!info->nid))
4114 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 info->ops.cleanup = hda_pcm_default_cleanup;
4116 }
4117 return 0;
4118}
4119
Takashi Iwaieb541332010-08-06 13:48:11 +02004120/*
4121 * codec prepare/cleanup entries
4122 */
4123int snd_hda_codec_prepare(struct hda_codec *codec,
4124 struct hda_pcm_stream *hinfo,
4125 unsigned int stream,
4126 unsigned int format,
4127 struct snd_pcm_substream *substream)
4128{
4129 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004130 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004131 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4132 if (ret >= 0)
4133 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004134 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004135 return ret;
4136}
4137EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4138
4139void snd_hda_codec_cleanup(struct hda_codec *codec,
4140 struct hda_pcm_stream *hinfo,
4141 struct snd_pcm_substream *substream)
4142{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004143 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004144 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004145 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004146}
4147EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4148
Takashi Iwaid5191e52009-11-16 14:58:17 +01004149/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004150const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4151 "Audio", "SPDIF", "HDMI", "Modem"
4152};
4153
Takashi Iwai176d5332008-07-30 15:01:44 +02004154/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004155 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004156 *
4157 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004158 */
4159static int get_empty_pcm_device(struct hda_bus *bus, int type)
4160{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004161 /* audio device indices; not linear to keep compatibility */
4162 static int audio_idx[HDA_PCM_NTYPES][5] = {
4163 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4164 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004165 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004166 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004167 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004168 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004169
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004170 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004171 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4172 return -EINVAL;
4173 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004174
4175 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4176 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4177 return audio_idx[type][i];
4178
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004179 /* non-fixed slots starting from 10 */
4180 for (i = 10; i < 32; i++) {
4181 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4182 return i;
4183 }
4184
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004185 snd_printk(KERN_WARNING "Too many %s devices\n",
4186 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004187 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004188}
4189
4190/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004191 * attach a new PCM stream
4192 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004193static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004194{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004195 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004196 struct hda_pcm_stream *info;
4197 int stream, err;
4198
Takashi Iwaib91f0802008-11-04 08:43:08 +01004199 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004200 return -EINVAL;
4201 for (stream = 0; stream < 2; stream++) {
4202 info = &pcm->stream[stream];
4203 if (info->substreams) {
4204 err = set_pcm_default_values(codec, info);
4205 if (err < 0)
4206 return err;
4207 }
4208 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004209 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004210}
4211
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004212/* assign all PCMs of the given codec */
4213int snd_hda_codec_build_pcms(struct hda_codec *codec)
4214{
4215 unsigned int pcm;
4216 int err;
4217
4218 if (!codec->num_pcms) {
4219 if (!codec->patch_ops.build_pcms)
4220 return 0;
4221 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004222 if (err < 0) {
4223 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004224 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004225 err = snd_hda_codec_reset(codec);
4226 if (err < 0) {
4227 printk(KERN_ERR
4228 "hda_codec: cannot revert codec\n");
4229 return err;
4230 }
4231 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004232 }
4233 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4234 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4235 int dev;
4236
4237 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004238 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004239
4240 if (!cpcm->pcm) {
4241 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4242 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004243 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004244 cpcm->device = dev;
4245 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004246 if (err < 0) {
4247 printk(KERN_ERR "hda_codec: cannot attach "
4248 "PCM stream %d for codec #%d\n",
4249 dev, codec->addr);
4250 continue; /* no fatal error */
4251 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004252 }
4253 }
4254 return 0;
4255}
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257/**
4258 * snd_hda_build_pcms - build PCM information
4259 * @bus: the BUS
4260 *
4261 * Create PCM information for each codec included in the bus.
4262 *
4263 * The build_pcms codec patch is requested to set up codec->num_pcms and
4264 * codec->pcm_info properly. The array is referred by the top-level driver
4265 * to create its PCM instances.
4266 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4267 * callback.
4268 *
4269 * At least, substreams, channels_min and channels_max must be filled for
4270 * each stream. substreams = 0 indicates that the stream doesn't exist.
4271 * When rates and/or formats are zero, the supported values are queried
4272 * from the given nid. The nid is used also by the default ops.prepare
4273 * and ops.cleanup callbacks.
4274 *
4275 * The driver needs to call ops.open in its open callback. Similarly,
4276 * ops.close is supposed to be called in the close callback.
4277 * ops.prepare should be called in the prepare or hw_params callback
4278 * with the proper parameters for set up.
4279 * ops.cleanup should be called in hw_free for clean up of streams.
4280 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004281 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004283int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004285 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Takashi Iwai0ba21762007-04-16 11:29:14 +02004287 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004288 int err = snd_hda_codec_build_pcms(codec);
4289 if (err < 0)
4290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292 return 0;
4293}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004294EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296/**
4297 * snd_hda_check_board_config - compare the current codec with the config table
4298 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004299 * @num_configs: number of config enums
4300 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 * @tbl: configuration table, terminated by null entries
4302 *
4303 * Compares the modelname or PCI subsystem id of the current codec with the
4304 * given configuration table. If a matching entry is found, returns its
4305 * config value (supposed to be 0 or positive).
4306 *
4307 * If no entries are matching, the function returns a negative value.
4308 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004309int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004310 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004311 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004313 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004314 int i;
4315 for (i = 0; i < num_configs; i++) {
4316 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004317 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004318 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4319 "selected\n", models[i]);
4320 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322 }
4323 }
4324
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004325 if (!codec->bus->pci || !tbl)
4326 return -1;
4327
4328 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4329 if (!tbl)
4330 return -1;
4331 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004332#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004333 char tmp[10];
4334 const char *model = NULL;
4335 if (models)
4336 model = models[tbl->value];
4337 if (!model) {
4338 sprintf(tmp, "#%d", tbl->value);
4339 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004341 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4342 "for config %x:%x (%s)\n",
4343 model, tbl->subvendor, tbl->subdevice,
4344 (tbl->name ? tbl->name : "Unknown device"));
4345#endif
4346 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
4348 return -1;
4349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004350EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004353 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004354 subsystem ID with the
4355 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004356
4357 This is important for Gateway notebooks with SB450 HDA Audio
4358 where the vendor ID of the PCI device is:
4359 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4360 and the vendor/subvendor are found only at the codec.
4361
4362 * @codec: the HDA codec
4363 * @num_configs: number of config enums
4364 * @models: array of model name strings
4365 * @tbl: configuration table, terminated by null entries
4366 *
4367 * Compares the modelname or PCI subsystem id of the current codec with the
4368 * given configuration table. If a matching entry is found, returns its
4369 * config value (supposed to be 0 or positive).
4370 *
4371 * If no entries are matching, the function returns a negative value.
4372 */
4373int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004374 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004375 const struct snd_pci_quirk *tbl)
4376{
4377 const struct snd_pci_quirk *q;
4378
4379 /* Search for codec ID */
4380 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004381 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4382 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4383 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004384 break;
4385 }
4386
4387 if (!q->subvendor)
4388 return -1;
4389
4390 tbl = q;
4391
4392 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004393#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004394 char tmp[10];
4395 const char *model = NULL;
4396 if (models)
4397 model = models[tbl->value];
4398 if (!model) {
4399 sprintf(tmp, "#%d", tbl->value);
4400 model = tmp;
4401 }
4402 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4403 "for config %x:%x (%s)\n",
4404 model, tbl->subvendor, tbl->subdevice,
4405 (tbl->name ? tbl->name : "Unknown device"));
4406#endif
4407 return tbl->value;
4408 }
4409 return -1;
4410}
4411EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4412
4413/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 * snd_hda_add_new_ctls - create controls from the array
4415 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004416 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 *
4418 * This helper function creates and add new controls in the given array.
4419 * The array must be terminated with an empty entry as terminator.
4420 *
4421 * Returns 0 if successful, or a negative error code.
4422 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004423int snd_hda_add_new_ctls(struct hda_codec *codec,
4424 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004426 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004429 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004430 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004431 if (knew->iface == -1) /* skip this codec private value */
4432 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004433 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004434 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004435 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004436 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004437 if (addr > 0)
4438 kctl->id.device = addr;
4439 if (idx > 0)
4440 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004441 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004442 if (!err)
4443 break;
4444 /* try first with another device index corresponding to
4445 * the codec addr; if it still fails (or it's the
4446 * primary codec), then try another control index
4447 */
4448 if (!addr && codec->addr)
4449 addr = codec->addr;
4450 else if (!idx && !knew->index) {
4451 idx = find_empty_mixer_ctl_idx(codec,
4452 knew->name);
4453 if (idx <= 0)
4454 return err;
4455 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004456 return err;
4457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459 return 0;
4460}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004461EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Takashi Iwai83012a72012-08-24 18:38:08 +02004463#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004464static void hda_power_work(struct work_struct *work)
4465{
4466 struct hda_codec *codec =
4467 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004468 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004469 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004470
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004471 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004472 if (codec->power_transition > 0) { /* during power-up sequence? */
4473 spin_unlock(&codec->power_lock);
4474 return;
4475 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004476 if (!codec->power_on || codec->power_count) {
4477 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004478 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004479 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004480 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004481 spin_unlock(&codec->power_lock);
4482
Dylan Reidd17344b2012-09-28 15:57:01 -07004483 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004484 codec->pm_down_notified = 0;
4485 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4486 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004487 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004488 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004489}
4490
4491static void hda_keep_power_on(struct hda_codec *codec)
4492{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004493 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004494 codec->power_count++;
4495 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004496 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004497 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004498}
4499
Takashi Iwaid5191e52009-11-16 14:58:17 +01004500/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004501void snd_hda_update_power_acct(struct hda_codec *codec)
4502{
4503 unsigned long delta = jiffies - codec->power_jiffies;
4504 if (codec->power_on)
4505 codec->power_on_acct += delta;
4506 else
4507 codec->power_off_acct += delta;
4508 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004509}
4510
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004511/* Transition to powered up, if wait_power_down then wait for a pending
4512 * transition to D3 to complete. A pending D3 transition is indicated
4513 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004514/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004515static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004516{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004517 struct hda_bus *bus = codec->bus;
4518
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004519 /* Return if power_on or transitioning to power_on, unless currently
4520 * powering down. */
4521 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004522 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004523 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004524 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004525
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004526 cancel_delayed_work_sync(&codec->power_work);
4527
4528 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004529 /* If the power down delayed work was cancelled above before starting,
4530 * then there is no need to go through power up here.
4531 */
4532 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004533 if (codec->power_transition < 0)
4534 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004535 return;
4536 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004537
Takashi Iwaid66fee52011-08-02 15:39:31 +02004538 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004539 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004540 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004541 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004542 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004543 spin_unlock(&codec->power_lock);
4544
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004545 if (codec->pm_down_notified) {
4546 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004547 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004548 }
4549
Takashi Iwaicb53c622007-08-10 17:21:45 +02004550 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004551
4552 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004553 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004554}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004555
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004556#define power_save(codec) \
4557 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004558
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004559/* Transition to powered down */
4560static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004561{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004562 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004563 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004564
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004565 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004566 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004567 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004568 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004569 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004570}
4571
4572/**
4573 * snd_hda_power_save - Power-up/down/sync the codec
4574 * @codec: HD-audio codec
4575 * @delta: the counter delta to change
4576 *
4577 * Change the power-up counter via @delta, and power up or down the hardware
4578 * appropriately. For the power-down, queue to the delayed action.
4579 * Passing zero to @delta means to synchronize the power state.
4580 */
4581void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4582{
4583 spin_lock(&codec->power_lock);
4584 codec->power_count += delta;
4585 trace_hda_power_count(codec);
4586 if (delta > 0)
4587 __snd_hda_power_up(codec, d3wait);
4588 else
4589 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004590 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004591}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004592EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004593
Takashi Iwaid5191e52009-11-16 14:58:17 +01004594/**
4595 * snd_hda_check_amp_list_power - Check the amp list and update the power
4596 * @codec: HD-audio codec
4597 * @check: the object containing an AMP list and the status
4598 * @nid: NID to check / update
4599 *
4600 * Check whether the given NID is in the amp list. If it's in the list,
4601 * check the current AMP status, and update the the power-status according
4602 * to the mute status.
4603 *
4604 * This function is supposed to be set or called from the check_power_status
4605 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004606 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004607int snd_hda_check_amp_list_power(struct hda_codec *codec,
4608 struct hda_loopback_check *check,
4609 hda_nid_t nid)
4610{
Takashi Iwai031024e2011-05-02 11:29:30 +02004611 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004612 int ch, v;
4613
4614 if (!check->amplist)
4615 return 0;
4616 for (p = check->amplist; p->nid; p++) {
4617 if (p->nid == nid)
4618 break;
4619 }
4620 if (!p->nid)
4621 return 0; /* nothing changed */
4622
4623 for (p = check->amplist; p->nid; p++) {
4624 for (ch = 0; ch < 2; ch++) {
4625 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4626 p->idx);
4627 if (!(v & HDA_AMP_MUTE) && v > 0) {
4628 if (!check->power_on) {
4629 check->power_on = 1;
4630 snd_hda_power_up(codec);
4631 }
4632 return 1;
4633 }
4634 }
4635 }
4636 if (check->power_on) {
4637 check->power_on = 0;
4638 snd_hda_power_down(codec);
4639 }
4640 return 0;
4641}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004642EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004645/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004646 * Channel mode helper
4647 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004648
4649/**
4650 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4651 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004652int snd_hda_ch_mode_info(struct hda_codec *codec,
4653 struct snd_ctl_elem_info *uinfo,
4654 const struct hda_channel_mode *chmode,
4655 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004656{
4657 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4658 uinfo->count = 1;
4659 uinfo->value.enumerated.items = num_chmodes;
4660 if (uinfo->value.enumerated.item >= num_chmodes)
4661 uinfo->value.enumerated.item = num_chmodes - 1;
4662 sprintf(uinfo->value.enumerated.name, "%dch",
4663 chmode[uinfo->value.enumerated.item].channels);
4664 return 0;
4665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004666EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004667
Takashi Iwaid5191e52009-11-16 14:58:17 +01004668/**
4669 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4670 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004671int snd_hda_ch_mode_get(struct hda_codec *codec,
4672 struct snd_ctl_elem_value *ucontrol,
4673 const struct hda_channel_mode *chmode,
4674 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004675 int max_channels)
4676{
4677 int i;
4678
4679 for (i = 0; i < num_chmodes; i++) {
4680 if (max_channels == chmode[i].channels) {
4681 ucontrol->value.enumerated.item[0] = i;
4682 break;
4683 }
4684 }
4685 return 0;
4686}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004687EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004688
Takashi Iwaid5191e52009-11-16 14:58:17 +01004689/**
4690 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4691 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004692int snd_hda_ch_mode_put(struct hda_codec *codec,
4693 struct snd_ctl_elem_value *ucontrol,
4694 const struct hda_channel_mode *chmode,
4695 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004696 int *max_channelsp)
4697{
4698 unsigned int mode;
4699
4700 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004701 if (mode >= num_chmodes)
4702 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004703 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004704 return 0;
4705 /* change the current channel setting */
4706 *max_channelsp = chmode[mode].channels;
4707 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004708 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004709 return 1;
4710}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004711EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713/*
4714 * input MUX helper
4715 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004716
4717/**
4718 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4719 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004720int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4721 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
4723 unsigned int index;
4724
4725 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4726 uinfo->count = 1;
4727 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004728 if (!imux->num_items)
4729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 index = uinfo->value.enumerated.item;
4731 if (index >= imux->num_items)
4732 index = imux->num_items - 1;
4733 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4734 return 0;
4735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004736EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Takashi Iwaid5191e52009-11-16 14:58:17 +01004738/**
4739 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4740 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004741int snd_hda_input_mux_put(struct hda_codec *codec,
4742 const struct hda_input_mux *imux,
4743 struct snd_ctl_elem_value *ucontrol,
4744 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 unsigned int *cur_val)
4746{
4747 unsigned int idx;
4748
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004749 if (!imux->num_items)
4750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 idx = ucontrol->value.enumerated.item[0];
4752 if (idx >= imux->num_items)
4753 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004754 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004756 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4757 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 *cur_val = idx;
4759 return 1;
4760}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004761EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763
4764/*
4765 * Multi-channel / digital-out PCM helper functions
4766 */
4767
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004768/* setup SPDIF output stream */
4769static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4770 unsigned int stream_tag, unsigned int format)
4771{
Stephen Warren7c935972011-06-01 11:14:17 -06004772 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4773
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004774 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004775 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004776 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004777 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004778 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004779 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004780 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004781 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004782 for (d = codec->slave_dig_outs; *d; d++)
4783 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4784 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004785 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004786 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004787 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004788 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004789 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004790}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004791
Takashi Iwai2f728532008-09-25 16:32:41 +02004792static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4793{
4794 snd_hda_codec_cleanup_stream(codec, nid);
4795 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004796 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004797 for (d = codec->slave_dig_outs; *d; d++)
4798 snd_hda_codec_cleanup_stream(codec, *d);
4799 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004800}
4801
Takashi Iwaid5191e52009-11-16 14:58:17 +01004802/**
4803 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4804 * @bus: HD-audio bus
4805 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004806void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4807{
4808 struct hda_codec *codec;
4809
4810 if (!bus)
4811 return;
4812 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004813 if (hda_codec_is_power_on(codec) &&
4814 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004815 codec->patch_ops.reboot_notify(codec);
4816 }
4817}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004818EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004819
Takashi Iwaid5191e52009-11-16 14:58:17 +01004820/**
4821 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004823int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4824 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Ingo Molnar62932df2006-01-16 16:34:20 +01004826 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004827 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4828 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004829 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004831 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return 0;
4833}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004834EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Takashi Iwaid5191e52009-11-16 14:58:17 +01004836/**
4837 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4838 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004839int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4840 struct hda_multi_out *mout,
4841 unsigned int stream_tag,
4842 unsigned int format,
4843 struct snd_pcm_substream *substream)
4844{
4845 mutex_lock(&codec->spdif_mutex);
4846 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4847 mutex_unlock(&codec->spdif_mutex);
4848 return 0;
4849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004850EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004851
Takashi Iwaid5191e52009-11-16 14:58:17 +01004852/**
4853 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4854 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004855int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4856 struct hda_multi_out *mout)
4857{
4858 mutex_lock(&codec->spdif_mutex);
4859 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4860 mutex_unlock(&codec->spdif_mutex);
4861 return 0;
4862}
4863EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4864
Takashi Iwaid5191e52009-11-16 14:58:17 +01004865/**
4866 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004868int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4869 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
Ingo Molnar62932df2006-01-16 16:34:20 +01004871 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004873 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return 0;
4875}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004876EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Takashi Iwaid5191e52009-11-16 14:58:17 +01004878/**
4879 * snd_hda_multi_out_analog_open - open analog outputs
4880 *
4881 * Open analog outputs and set up the hw-constraints.
4882 * If the digital outputs can be opened as slave, open the digital
4883 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004885int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4886 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004887 struct snd_pcm_substream *substream,
4888 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Takashi Iwai9a081602008-02-12 18:37:26 +01004890 struct snd_pcm_runtime *runtime = substream->runtime;
4891 runtime->hw.channels_max = mout->max_channels;
4892 if (mout->dig_out_nid) {
4893 if (!mout->analog_rates) {
4894 mout->analog_rates = hinfo->rates;
4895 mout->analog_formats = hinfo->formats;
4896 mout->analog_maxbps = hinfo->maxbps;
4897 } else {
4898 runtime->hw.rates = mout->analog_rates;
4899 runtime->hw.formats = mout->analog_formats;
4900 hinfo->maxbps = mout->analog_maxbps;
4901 }
4902 if (!mout->spdif_rates) {
4903 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4904 &mout->spdif_rates,
4905 &mout->spdif_formats,
4906 &mout->spdif_maxbps);
4907 }
4908 mutex_lock(&codec->spdif_mutex);
4909 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004910 if ((runtime->hw.rates & mout->spdif_rates) &&
4911 (runtime->hw.formats & mout->spdif_formats)) {
4912 runtime->hw.rates &= mout->spdif_rates;
4913 runtime->hw.formats &= mout->spdif_formats;
4914 if (mout->spdif_maxbps < hinfo->maxbps)
4915 hinfo->maxbps = mout->spdif_maxbps;
4916 } else {
4917 mout->share_spdif = 0;
4918 /* FIXME: need notify? */
4919 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004920 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004921 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4924 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4925}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004926EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Takashi Iwaid5191e52009-11-16 14:58:17 +01004928/**
4929 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4930 *
4931 * Set up the i/o for analog out.
4932 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4935 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 unsigned int stream_tag,
4937 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004938 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
Takashi Iwaidda14412011-05-02 11:29:30 +02004940 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004942 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 int i;
4944
Ingo Molnar62932df2006-01-16 16:34:20 +01004945 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004946 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004947 if (mout->dig_out_nid && mout->share_spdif &&
4948 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004950 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4951 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004952 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004954 setup_dig_out_stream(codec, mout->dig_out_nid,
4955 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 } else {
4957 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004958 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004961 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004964 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4965 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004966 if (!mout->no_share_stream &&
4967 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004969 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4970 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004971 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004972 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4973 if (!mout->no_share_stream && mout->hp_out_nid[i])
4974 snd_hda_codec_setup_stream(codec,
4975 mout->hp_out_nid[i],
4976 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004977 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004978 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004979 snd_hda_codec_setup_stream(codec,
4980 mout->extra_out_nid[i],
4981 stream_tag, 0, format);
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 /* surrounds */
4984 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004985 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004986 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4987 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004988 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004989 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4990 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
4992 return 0;
4993}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004994EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Takashi Iwaid5191e52009-11-16 14:58:17 +01004996/**
4997 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004999int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5000 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
Takashi Iwaidda14412011-05-02 11:29:30 +02005002 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 int i;
5004
5005 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005006 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005008 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005009 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5010 if (mout->hp_out_nid[i])
5011 snd_hda_codec_cleanup_stream(codec,
5012 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005013 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5014 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005015 snd_hda_codec_cleanup_stream(codec,
5016 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005017 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005019 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 mout->dig_out_used = 0;
5021 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005022 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return 0;
5024}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005025EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Takashi Iwai47408602012-04-20 13:06:53 +02005027/**
5028 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5029 *
5030 * Guess the suitable VREF pin bits to be set as the pin-control value.
5031 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5032 */
5033unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5034{
5035 unsigned int pincap;
5036 unsigned int oldval;
5037 oldval = snd_hda_codec_read(codec, pin, 0,
5038 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5039 pincap = snd_hda_query_pin_caps(codec, pin);
5040 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5041 /* Exception: if the default pin setup is vref50, we give it priority */
5042 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5043 return AC_PINCTL_VREF_80;
5044 else if (pincap & AC_PINCAP_VREF_50)
5045 return AC_PINCTL_VREF_50;
5046 else if (pincap & AC_PINCAP_VREF_100)
5047 return AC_PINCTL_VREF_100;
5048 else if (pincap & AC_PINCAP_VREF_GRD)
5049 return AC_PINCTL_VREF_GRD;
5050 return AC_PINCTL_VREF_HIZ;
5051}
5052EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5053
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005054int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5055 unsigned int val, bool cached)
5056{
5057 if (val) {
5058 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005059 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005060 if (!(cap & AC_PINCAP_OUT))
5061 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5062 else if ((val & AC_PINCTL_HP_EN) &&
5063 !(cap & AC_PINCAP_HP_DRV))
5064 val &= ~AC_PINCTL_HP_EN;
5065 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005066 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005067 if (!(cap & AC_PINCAP_IN))
5068 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5069 }
5070 }
5071 if (cached)
5072 return snd_hda_codec_update_cache(codec, pin, 0,
5073 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5074 else
5075 return snd_hda_codec_write(codec, pin, 0,
5076 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5077}
5078EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5079
Takashi Iwai990061c2010-09-09 22:08:44 +02005080/**
5081 * snd_hda_add_imux_item - Add an item to input_mux
5082 *
5083 * When the same label is used already in the existing items, the number
5084 * suffix is appended to the label. This label index number is stored
5085 * to type_idx when non-NULL pointer is given.
5086 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005087int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5088 int index, int *type_idx)
5089{
5090 int i, label_idx = 0;
5091 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5092 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5093 return -EINVAL;
5094 }
5095 for (i = 0; i < imux->num_items; i++) {
5096 if (!strncmp(label, imux->items[i].label, strlen(label)))
5097 label_idx++;
5098 }
5099 if (type_idx)
5100 *type_idx = label_idx;
5101 if (label_idx > 0)
5102 snprintf(imux->items[imux->num_items].label,
5103 sizeof(imux->items[imux->num_items].label),
5104 "%s %d", label, label_idx);
5105 else
5106 strlcpy(imux->items[imux->num_items].label, label,
5107 sizeof(imux->items[imux->num_items].label));
5108 imux->items[imux->num_items].index = index;
5109 imux->num_items++;
5110 return 0;
5111}
5112EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005113
Takashi Iwai4a471b72005-12-07 13:56:29 +01005114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115#ifdef CONFIG_PM
5116/*
5117 * power management
5118 */
5119
5120/**
5121 * snd_hda_suspend - suspend the codecs
5122 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 *
5124 * Returns 0 if successful.
5125 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005126int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005128 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Takashi Iwai0ba21762007-04-16 11:29:14 +02005130 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005131 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005132 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134 return 0;
5135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005136EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138/**
5139 * snd_hda_resume - resume the codecs
5140 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 *
5142 * Returns 0 if successful.
5143 */
5144int snd_hda_resume(struct hda_bus *bus)
5145{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005146 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Takashi Iwai0ba21762007-04-16 11:29:14 +02005148 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005149 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 return 0;
5152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005153EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005154#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005155
5156/*
5157 * generic arrays
5158 */
5159
Takashi Iwaid5191e52009-11-16 14:58:17 +01005160/**
5161 * snd_array_new - get a new element from the given array
5162 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005163 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005164 * Get a new element from the given array. If it exceeds the
5165 * pre-allocated array size, re-allocate the array.
5166 *
5167 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005168 */
5169void *snd_array_new(struct snd_array *array)
5170{
Takashi Iwai12f17712012-10-10 08:59:14 +02005171 if (snd_BUG_ON(!array->elem_size))
5172 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005173 if (array->used >= array->alloced) {
5174 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005175 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005176 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005177 void *nlist;
5178 if (snd_BUG_ON(num >= 4096))
5179 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005180 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005181 if (!nlist)
5182 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005183 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005184 array->list = nlist;
5185 array->alloced = num;
5186 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005187 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005189EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005190
Takashi Iwaid5191e52009-11-16 14:58:17 +01005191/**
5192 * snd_array_free - free the given array elements
5193 * @array: the array object
5194 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005195void snd_array_free(struct snd_array *array)
5196{
5197 kfree(array->list);
5198 array->used = 0;
5199 array->alloced = 0;
5200 array->list = NULL;
5201}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005202EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005203
Takashi Iwaid5191e52009-11-16 14:58:17 +01005204/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005205 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5206 * @pcm: PCM caps bits
5207 * @buf: the string buffer to write
5208 * @buflen: the max buffer length
5209 *
5210 * used by hda_proc.c and hda_eld.c
5211 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005212void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5213{
5214 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5215 int i, j;
5216
5217 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5218 if (pcm & (AC_SUPPCM_BITS_8 << i))
5219 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5220
5221 buf[j] = '\0'; /* necessary when j == 0 */
5222}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005223EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005224
5225MODULE_DESCRIPTION("HDA codec core");
5226MODULE_LICENSE("GPL");