blob: e105b653130dc2772829f638365226939dc5894b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Takashi Iwaid66fee52011-08-02 15:39:31 +020037#define CREATE_TRACE_POINTS
38#include "hda_trace.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * vendor / preset table
42 */
43
44struct hda_vendor_id {
45 unsigned int id;
46 const char *name;
47};
48
49/* codec vendor labels */
50static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020052 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020053 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010055 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020057 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010058 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020059 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020061 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020063 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010064 { 0x17e8, "Chrontel" },
65 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000066 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010068 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020069 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {} /* terminator */
71};
72
Takashi Iwai1289e9e2008-11-27 15:47:11 +010073static DEFINE_MUTEX(preset_mutex);
74static LIST_HEAD(hda_preset_tables);
75
76int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
77{
78 mutex_lock(&preset_mutex);
79 list_add_tail(&preset->list, &hda_preset_tables);
80 mutex_unlock(&preset_mutex);
81 return 0;
82}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010083EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010084
85int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
86{
87 mutex_lock(&preset_mutex);
88 list_del(&preset->list);
89 mutex_unlock(&preset_mutex);
90 return 0;
91}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010092EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Takashi Iwaicb53c622007-08-10 17:21:45 +020094#ifdef CONFIG_SND_HDA_POWER_SAVE
95static void hda_power_work(struct work_struct *work);
96static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020097#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +020098#else
99static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#endif
102
Takashi Iwaid5191e52009-11-16 14:58:17 +0100103/**
104 * snd_hda_get_jack_location - Give a location string of the jack
105 * @cfg: pin default config value
106 *
107 * Parse the pin default config value and returns the string of the
108 * jack location, e.g. "Rear", "Front", etc.
109 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400110const char *snd_hda_get_jack_location(u32 cfg)
111{
112 static char *bases[7] = {
113 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
114 };
115 static unsigned char specials_idx[] = {
116 0x07, 0x08,
117 0x17, 0x18, 0x19,
118 0x37, 0x38
119 };
120 static char *specials[] = {
121 "Rear Panel", "Drive Bar",
122 "Riser", "HDMI", "ATAPI",
123 "Mobile-In", "Mobile-Out"
124 };
125 int i;
126 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
127 if ((cfg & 0x0f) < 7)
128 return bases[cfg & 0x0f];
129 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
130 if (cfg == specials_idx[i])
131 return specials[i];
132 }
133 return "UNKNOWN";
134}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100135EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400136
Takashi Iwaid5191e52009-11-16 14:58:17 +0100137/**
138 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
139 * @cfg: pin default config value
140 *
141 * Parse the pin default config value and returns the string of the
142 * jack connectivity, i.e. external or internal connection.
143 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400144const char *snd_hda_get_jack_connectivity(u32 cfg)
145{
146 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
147
148 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100150EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400151
Takashi Iwaid5191e52009-11-16 14:58:17 +0100152/**
153 * snd_hda_get_jack_type - Give a type string of the jack
154 * @cfg: pin default config value
155 *
156 * Parse the pin default config value and returns the string of the
157 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
158 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400159const char *snd_hda_get_jack_type(u32 cfg)
160{
161 static char *jack_types[16] = {
162 "Line Out", "Speaker", "HP Out", "CD",
163 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
164 "Line In", "Aux", "Mic", "Telephony",
165 "SPDIF In", "Digitial In", "Reserved", "Other"
166 };
167
168 return jack_types[(cfg & AC_DEFCFG_DEVICE)
169 >> AC_DEFCFG_DEVICE_SHIFT];
170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100171EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100173/*
174 * Compose a 32bit command word to be sent to the HD-audio controller
175 */
176static inline unsigned int
177make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
178 unsigned int verb, unsigned int parm)
179{
180 u32 val;
181
Takashi Iwai82e1b802009-07-17 12:47:34 +0200182 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
183 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800184 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
185 codec->addr, direct, nid, verb, parm);
186 return ~0;
187 }
188
189 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100190 val |= (u32)direct << 27;
191 val |= (u32)nid << 20;
192 val |= verb << 8;
193 val |= parm;
194 return val;
195}
196
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200197/*
198 * Send and receive a verb
199 */
200static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
201 unsigned int *res)
202{
203 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200204 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200205
Wu Fengguang6430aee2009-07-17 16:49:19 +0800206 if (cmd == ~0)
207 return -1;
208
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 if (res)
210 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200211 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 snd_hda_power_up(codec);
213 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200214 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200216 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800217 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200218 trace_hda_get_response(codec, *res);
219 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 mutex_unlock(&bus->cmd_mutex);
221 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 if (res && *res == -1 && bus->rirb_error) {
223 if (bus->response_reset) {
224 snd_printd("hda_codec: resetting BUS due to "
225 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200226 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200227 bus->ops.bus_reset(bus);
228 }
229 goto again;
230 }
231 /* clear reset-flag when the communication gets recovered */
232 if (!err)
233 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200234 return err;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * snd_hda_codec_read - send a command and get the response
239 * @codec: the HDA codec
240 * @nid: NID to send the command
241 * @direct: direct flag
242 * @verb: the verb to send
243 * @parm: the parameter for the verb
244 *
245 * Send a single command and read the corresponding response.
246 *
247 * Returns the obtained response value, or -1 for an error.
248 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200249unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
250 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int verb, unsigned int parm)
252{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200253 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
254 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700255 if (codec_exec_verb(codec, cmd, &res))
256 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return res;
258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100259EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/**
262 * snd_hda_codec_write - send a single command without waiting for response
263 * @codec: the HDA codec
264 * @nid: NID to send the command
265 * @direct: direct flag
266 * @verb: the verb to send
267 * @parm: the parameter for the verb
268 *
269 * Send a single command without waiting for response.
270 *
271 * Returns 0 if successful, or a negative error code.
272 */
273int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
274 unsigned int verb, unsigned int parm)
275{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200276 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100277 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200278 return codec_exec_verb(codec, cmd,
279 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100281EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283/**
284 * snd_hda_sequence_write - sequence writes
285 * @codec: the HDA codec
286 * @seq: VERB array to send
287 *
288 * Send the commands sequentially from the given array.
289 * The array must be terminated with NID=0.
290 */
291void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
292{
293 for (; seq->nid; seq++)
294 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
295}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100296EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/**
299 * snd_hda_get_sub_nodes - get the range of sub nodes
300 * @codec: the HDA codec
301 * @nid: NID to parse
302 * @start_id: the pointer to store the start NID
303 *
304 * Parse the NID and store the start NID of its sub-nodes.
305 * Returns the number of sub-nodes.
306 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200307int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
308 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned int parm;
311
312 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200313 if (parm == -1)
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *start_id = (parm >> 16) & 0x7fff;
316 return (int)(parm & 0x7fff);
317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100318EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200320/* look up the cached results */
321static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
322{
323 int i, len;
324 for (i = 0; i < array->used; ) {
325 hda_nid_t *p = snd_array_elem(array, i);
326 if (nid == *p)
327 return p;
328 len = p[1];
329 i += len + 2;
330 }
331 return NULL;
332}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200335 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @codec: the HDA codec
337 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200338 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
340 * Parses the connection list of the given widget and stores the list
341 * of NIDs.
342 *
343 * Returns the number of connections, or a negative error code.
344 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200345int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
346 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347{
348 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200349 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200351 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 again:
355 /* if the connection-list is already cached, read it */
356 p = lookup_conn_list(array, nid);
357 if (p) {
358 if (listp)
359 *listp = p + 2;
360 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200362 if (snd_BUG_ON(added))
363 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200366 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367 if (len < 0)
368 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200369 err = snd_hda_override_conn_list(codec, nid, len, list);
370 if (err < 0)
371 return err;
372 added = true;
373 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200374}
Takashi Iwaidce20792011-06-24 14:10:28 +0200375EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
376
377/**
378 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * @codec: the HDA codec
380 * @nid: NID to parse
381 * @conn_list: connection list array
382 * @max_conns: max. number of connections to store
383 *
384 * Parses the connection list of the given widget and stores the list
385 * of NIDs.
386 *
387 * Returns the number of connections, or a negative error code.
388 */
389int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390 hda_nid_t *conn_list, int max_conns)
391{
Takashi Iwaidce20792011-06-24 14:10:28 +0200392 const hda_nid_t *list;
393 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200394
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200396 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200397 if (len > max_conns) {
398 snd_printk(KERN_ERR "hda_codec: "
399 "Too many connections %d for NID 0x%x\n",
400 len, nid);
401 return -EINVAL;
402 }
403 memcpy(conn_list, list, len * sizeof(hda_nid_t));
404 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405}
406EXPORT_SYMBOL_HDA(snd_hda_get_connections);
407
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200408/**
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
411 * @nid: NID to parse
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
414 *
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
418 */
419int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
420 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100423 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200425 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Takashi Iwaida3cec32008-08-08 17:12:14 +0200428 if (snd_BUG_ON(!conn_list || max_conns <= 0))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200431 wcaps = get_wcaps(codec, nid);
432 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200433 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
434 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
437 if (parm & AC_CLIST_LONG) {
438 /* long form */
439 shift = 16;
440 num_elems = 2;
441 } else {
442 /* short form */
443 shift = 8;
444 num_elems = 4;
445 }
446 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 mask = (1 << (shift-1)) - 1;
448
Takashi Iwai0ba21762007-04-16 11:29:14 +0200449 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0; /* no connection */
451
452 if (conn_len == 1) {
453 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200454 parm = snd_hda_codec_read(codec, nid, 0,
455 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200456 if (parm == -1 && codec->bus->rirb_error)
457 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 conn_list[0] = parm & mask;
459 return 1;
460 }
461
462 /* multi connection */
463 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100464 prev_nid = 0;
465 for (i = 0; i < conn_len; i++) {
466 int range_val;
467 hda_nid_t val, n;
468
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200469 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (parm == -1 && codec->bus->rirb_error)
473 return -EIO;
474 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200475 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200477 if (val == 0) {
478 snd_printk(KERN_WARNING "hda_codec: "
479 "invalid CONNECT_LIST verb %x[%i]:%x\n",
480 nid, i, parm);
481 return 0;
482 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 parm >>= shift;
484 if (range_val) {
485 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200486 if (!prev_nid || prev_nid >= val) {
487 snd_printk(KERN_WARNING "hda_codec: "
488 "invalid dep_range_val %x:%x\n",
489 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100492 for (n = prev_nid + 1; n <= val; n++) {
493 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200494 snd_printk(KERN_ERR "hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
496 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 return -EINVAL;
498 }
499 conn_list[conns++] = n;
500 }
501 } else {
502 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200503 snd_printk(KERN_ERR "hda_codec: "
504 "Too many connections %d for NID 0x%x\n",
505 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 return -EINVAL;
507 }
508 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100510 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return conns;
513}
514
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200515static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
516{
517 hda_nid_t *p = snd_array_new(array);
518 if (!p)
519 return false;
520 *p = nid;
521 return true;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200525 * snd_hda_override_conn_list - add/modify the connection-list to cache
526 * @codec: the HDA codec
527 * @nid: NID to parse
528 * @len: number of connection list entries
529 * @list: the list of connection entries
530 *
531 * Add or modify the given connection-list to the cache. If the corresponding
532 * cache already exists, invalidate it and append a new one.
533 *
534 * Returns zero or a negative error code.
535 */
536int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
537 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200539 struct snd_array *array = &codec->conn_lists;
540 hda_nid_t *p;
541 int i, old_used;
542
543 p = lookup_conn_list(array, nid);
544 if (p)
545 *p = -1; /* invalidate the old entry */
546
547 old_used = array->used;
548 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
549 goto error_add;
550 for (i = 0; i < len; i++)
551 if (!add_conn_list(array, list[i]))
552 goto error_add;
553 return 0;
554
555 error_add:
556 array->used = old_used;
557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200559EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
560
561/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200562 * snd_hda_get_conn_index - get the connection index of the given NID
563 * @codec: the HDA codec
564 * @mux: NID containing the list
565 * @nid: NID to select
566 * @recursive: 1 when searching NID recursively, otherwise 0
567 *
568 * Parses the connection list of the widget @mux and checks whether the
569 * widget @nid is present. If it is, return the connection index.
570 * Otherwise it returns -1.
571 */
572int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
573 hda_nid_t nid, int recursive)
574{
575 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
576 int i, nums;
577
578 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
579 for (i = 0; i < nums; i++)
580 if (conn[i] == nid)
581 return i;
582 if (!recursive)
583 return -1;
584 if (recursive > 5) {
585 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
586 return -1;
587 }
588 recursive++;
589 for (i = 0; i < nums; i++)
590 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
591 return i;
592 return -1;
593}
594EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/**
597 * snd_hda_queue_unsol_event - add an unsolicited event to queue
598 * @bus: the BUS
599 * @res: unsolicited event (lower 32bit of RIRB entry)
600 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
601 *
602 * Adds the given event to the queue. The events are processed in
603 * the workqueue asynchronously. Call this function in the interrupt
604 * hanlder when RIRB receives an unsolicited event.
605 *
606 * Returns 0 if successful, or a negative error code.
607 */
608int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
609{
610 struct hda_bus_unsolicited *unsol;
611 unsigned int wp;
612
Takashi Iwai0ba21762007-04-16 11:29:14 +0200613 unsol = bus->unsol;
614 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616
617 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
618 unsol->wp = wp;
619
620 wp <<= 1;
621 unsol->queue[wp] = res;
622 unsol->queue[wp + 1] = res_ex;
623
Takashi Iwai6acaed32009-01-12 10:09:24 +0100624 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 0;
627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100628EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800631 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
David Howellsc4028952006-11-22 14:57:56 +0000633static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
David Howellsc4028952006-11-22 14:57:56 +0000635 struct hda_bus_unsolicited *unsol =
636 container_of(work, struct hda_bus_unsolicited, work);
637 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct hda_codec *codec;
639 unsigned int rp, caddr, res;
640
641 while (unsol->rp != unsol->wp) {
642 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
643 unsol->rp = rp;
644 rp <<= 1;
645 res = unsol->queue[rp];
646 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200647 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 codec = bus->caddr_tbl[caddr & 0x0f];
650 if (codec && codec->patch_ops.unsol_event)
651 codec->patch_ops.unsol_event(codec, res);
652 }
653}
654
655/*
656 * initialize unsolicited queue
657 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200658static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct hda_bus_unsolicited *unsol;
661
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100662 if (bus->unsol) /* already initialized */
663 return 0;
664
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200665 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200666 if (!unsol) {
667 snd_printk(KERN_ERR "hda_codec: "
668 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -ENOMEM;
670 }
David Howellsc4028952006-11-22 14:57:56 +0000671 INIT_WORK(&unsol->work, process_unsol_events);
672 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 bus->unsol = unsol;
674 return 0;
675}
676
677/*
678 * destructor
679 */
680static void snd_hda_codec_free(struct hda_codec *codec);
681
682static int snd_hda_bus_free(struct hda_bus *bus)
683{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200684 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Takashi Iwai0ba21762007-04-16 11:29:14 +0200686 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100688 if (bus->workq)
689 flush_workqueue(bus->workq);
690 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 snd_hda_codec_free(codec);
694 }
695 if (bus->ops.private_free)
696 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100697 if (bus->workq)
698 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus);
700 return 0;
701}
702
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100703static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100706 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return snd_hda_bus_free(bus);
708}
709
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200710#ifdef CONFIG_SND_HDA_HWDEP
711static int snd_hda_bus_dev_register(struct snd_device *device)
712{
713 struct hda_bus *bus = device->device_data;
714 struct hda_codec *codec;
715 list_for_each_entry(codec, &bus->codec_list, list) {
716 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100717 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718 }
719 return 0;
720}
721#else
722#define snd_hda_bus_dev_register NULL
723#endif
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/**
726 * snd_hda_bus_new - create a HDA bus
727 * @card: the card entry
728 * @temp: the template for hda_bus information
729 * @busp: the pointer to store the created bus instance
730 *
731 * Returns 0 if successful, or a negative error code.
732 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100733int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200734 const struct hda_bus_template *temp,
735 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hda_bus *bus;
738 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100739 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200740 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .dev_free = snd_hda_bus_dev_free,
742 };
743
Takashi Iwaida3cec32008-08-08 17:12:14 +0200744 if (snd_BUG_ON(!temp))
745 return -EINVAL;
746 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (busp)
750 *busp = NULL;
751
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200752 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (bus == NULL) {
754 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
755 return -ENOMEM;
756 }
757
758 bus->card = card;
759 bus->private_data = temp->private_data;
760 bus->pci = temp->pci;
761 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100762 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 bus->ops = temp->ops;
764
Ingo Molnar62932df2006-01-16 16:34:20 +0100765 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200766 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 INIT_LIST_HEAD(&bus->codec_list);
768
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100769 snprintf(bus->workq_name, sizeof(bus->workq_name),
770 "hd-audio%d", card->number);
771 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100772 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100773 snd_printk(KERN_ERR "cannot create workqueue %s\n",
774 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100775 kfree(bus);
776 return -ENOMEM;
777 }
778
Takashi Iwai0ba21762007-04-16 11:29:14 +0200779 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
780 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 snd_hda_bus_free(bus);
782 return err;
783 }
784 if (busp)
785 *busp = bus;
786 return 0;
787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100788EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Takashi Iwai82467612007-07-27 19:15:54 +0200790#ifdef CONFIG_SND_HDA_GENERIC
791#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200792 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200793#else
794#define is_generic_config(codec) 0
795#endif
796
Takashi Iwai645f10c2008-11-28 15:07:37 +0100797#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100798#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
799#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100800#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100801#endif
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * find a matching codec preset
805 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200806static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200807find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809 struct hda_codec_preset_list *tbl;
810 const struct hda_codec_preset *preset;
811 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Takashi Iwai82467612007-07-27 19:15:54 +0200813 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100814 return NULL; /* use the generic parser */
815
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816 again:
817 mutex_lock(&preset_mutex);
818 list_for_each_entry(tbl, &hda_preset_tables, list) {
819 if (!try_module_get(tbl->owner)) {
820 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
821 continue;
822 }
823 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100825 if (preset->afg && preset->afg != codec->afg)
826 continue;
827 if (preset->mfg && preset->mfg != codec->mfg)
828 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200829 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200831 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200832 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100833 preset->rev == codec->revision_id)) {
834 mutex_unlock(&preset_mutex);
835 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 module_put(tbl->owner);
840 }
841 mutex_unlock(&preset_mutex);
842
843 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
844 char name[32];
845 if (!mod_requested)
846 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
847 codec->vendor_id);
848 else
849 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
850 (codec->vendor_id >> 16) & 0xffff);
851 request_module(name);
852 mod_requested++;
853 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 return NULL;
856}
857
858/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200859 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200861static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 const struct hda_vendor_id *c;
864 const char *vendor = NULL;
865 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200866 char tmp[16];
867
868 if (codec->vendor_name)
869 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 for (c = hda_vendor_ids; c->id; c++) {
872 if (c->id == vendor_id) {
873 vendor = c->name;
874 break;
875 }
876 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sprintf(tmp, "Generic %04x", vendor_id);
879 vendor = tmp;
880 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200881 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
882 if (!codec->vendor_name)
883 return -ENOMEM;
884
885 get_chip_name:
886 if (codec->chip_name)
887 return 0;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200890 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
891 else {
892 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
893 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
894 }
895 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200896 return -ENOMEM;
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200901 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100903static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200905 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 hda_nid_t nid;
907
908 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
909 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200910 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200911 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200912 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200913 case AC_GRP_AUDIO_FUNCTION:
914 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200915 codec->afg_function_id = function_id & 0xff;
916 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200917 break;
918 case AC_GRP_MODEM_FUNCTION:
919 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 codec->mfg_function_id = function_id & 0xff;
921 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 break;
923 default:
924 break;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100930 * read widget caps for each widget and store in cache
931 */
932static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
933{
934 int i;
935 hda_nid_t nid;
936
937 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
938 &codec->start_nid);
939 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200940 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100941 return -ENOMEM;
942 nid = codec->start_nid;
943 for (i = 0; i < codec->num_nodes; i++, nid++)
944 codec->wcaps[i] = snd_hda_param_read(codec, nid,
945 AC_PAR_AUDIO_WIDGET_CAP);
946 return 0;
947}
948
Takashi Iwai3be14142009-02-20 14:11:16 +0100949/* read all pin default configurations and save codec->init_pins */
950static int read_pin_defaults(struct hda_codec *codec)
951{
952 int i;
953 hda_nid_t nid = codec->start_nid;
954
955 for (i = 0; i < codec->num_nodes; i++, nid++) {
956 struct hda_pincfg *pin;
957 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200958 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100959 if (wid_type != AC_WID_PIN)
960 continue;
961 pin = snd_array_new(&codec->init_pins);
962 if (!pin)
963 return -ENOMEM;
964 pin->nid = nid;
965 pin->cfg = snd_hda_codec_read(codec, nid, 0,
966 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200967 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
968 AC_VERB_GET_PIN_WIDGET_CONTROL,
969 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100970 }
971 return 0;
972}
973
974/* look up the given pin config list and return the item matching with NID */
975static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
976 struct snd_array *array,
977 hda_nid_t nid)
978{
979 int i;
980 for (i = 0; i < array->used; i++) {
981 struct hda_pincfg *pin = snd_array_elem(array, i);
982 if (pin->nid == nid)
983 return pin;
984 }
985 return NULL;
986}
987
988/* write a config value for the given NID */
989static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
990 unsigned int cfg)
991{
992 int i;
993 for (i = 0; i < 4; i++) {
994 snd_hda_codec_write(codec, nid, 0,
995 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
996 cfg & 0xff);
997 cfg >>= 8;
998 }
999}
1000
1001/* set the current pin config value for the given NID.
1002 * the value is cached, and read via snd_hda_codec_get_pincfg()
1003 */
1004int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1005 hda_nid_t nid, unsigned int cfg)
1006{
1007 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001008 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001009
Takashi Iwaib82855a2009-12-27 11:24:56 +01001010 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1011 return -EINVAL;
1012
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001013 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 pin = look_up_pincfg(codec, list, nid);
1015 if (!pin) {
1016 pin = snd_array_new(list);
1017 if (!pin)
1018 return -ENOMEM;
1019 pin->nid = nid;
1020 }
1021 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001022
1023 /* change only when needed; e.g. if the pincfg is already present
1024 * in user_pins[], don't write it
1025 */
1026 cfg = snd_hda_codec_get_pincfg(codec, nid);
1027 if (oldcfg != cfg)
1028 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001029 return 0;
1030}
1031
Takashi Iwaid5191e52009-11-16 14:58:17 +01001032/**
1033 * snd_hda_codec_set_pincfg - Override a pin default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to set the pin config
1036 * @cfg: the pin default config value
1037 *
1038 * Override a pin default configuration value in the cache.
1039 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1040 * priority than the real hardware value.
1041 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001042int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1043 hda_nid_t nid, unsigned int cfg)
1044{
Takashi Iwai346ff702009-02-23 09:42:57 +01001045 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001046}
1047EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1048
Takashi Iwaid5191e52009-11-16 14:58:17 +01001049/**
1050 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1051 * @codec: the HDA codec
1052 * @nid: NID to get the pin config
1053 *
1054 * Get the current pin config value of the given pin NID.
1055 * If the pincfg value is cached or overridden via sysfs or driver,
1056 * returns the cached value.
1057 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001058unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1059{
1060 struct hda_pincfg *pin;
1061
Takashi Iwai3be14142009-02-20 14:11:16 +01001062#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001063 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001064 if (pin)
1065 return pin->cfg;
1066#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001067 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1068 if (pin)
1069 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1071 if (pin)
1072 return pin->cfg;
1073 return 0;
1074}
1075EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1076
1077/* restore all current pin configs */
1078static void restore_pincfgs(struct hda_codec *codec)
1079{
1080 int i;
1081 for (i = 0; i < codec->init_pins.used; i++) {
1082 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1083 set_pincfg(codec, pin->nid,
1084 snd_hda_codec_get_pincfg(codec, pin->nid));
1085 }
1086}
Takashi Iwai54d17402005-11-21 16:33:22 +01001087
Takashi Iwai92ee6162009-12-27 11:18:59 +01001088/**
1089 * snd_hda_shutup_pins - Shut up all pins
1090 * @codec: the HDA codec
1091 *
1092 * Clear all pin controls to shup up before suspend for avoiding click noise.
1093 * The controls aren't cached so that they can be resumed properly.
1094 */
1095void snd_hda_shutup_pins(struct hda_codec *codec)
1096{
1097 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001098 /* don't shut up pins when unloading the driver; otherwise it breaks
1099 * the default pin setup at the next load of the driver
1100 */
1101 if (codec->bus->shutdown)
1102 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001103 for (i = 0; i < codec->init_pins.used; i++) {
1104 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1105 /* use read here for syncing after issuing each verb */
1106 snd_hda_codec_read(codec, pin->nid, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1108 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001109 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001110}
1111EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1112
Takashi Iwai2a439522011-07-26 09:52:50 +02001113#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001114/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1115static void restore_shutup_pins(struct hda_codec *codec)
1116{
1117 int i;
1118 if (!codec->pins_shutup)
1119 return;
1120 if (codec->bus->shutdown)
1121 return;
1122 for (i = 0; i < codec->init_pins.used; i++) {
1123 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1124 snd_hda_codec_write(codec, pin->nid, 0,
1125 AC_VERB_SET_PIN_WIDGET_CONTROL,
1126 pin->ctrl);
1127 }
1128 codec->pins_shutup = 0;
1129}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001130#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001131
Takashi Iwai01751f52007-08-10 16:59:39 +02001132static void init_hda_cache(struct hda_cache_rec *cache,
1133 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001134static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001135
Takashi Iwai3be14142009-02-20 14:11:16 +01001136/* restore the initial pin cfgs and release all pincfg lists */
1137static void restore_init_pincfgs(struct hda_codec *codec)
1138{
Takashi Iwai346ff702009-02-23 09:42:57 +01001139 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001140 * so that only the values in init_pins are restored
1141 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001142 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001143#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001144 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001145#endif
1146 restore_pincfgs(codec);
1147 snd_array_free(&codec->init_pins);
1148}
1149
Takashi Iwai54d17402005-11-21 16:33:22 +01001150/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001151 * audio-converter setup caches
1152 */
1153struct hda_cvt_setup {
1154 hda_nid_t nid;
1155 u8 stream_tag;
1156 u8 channel_id;
1157 u16 format_id;
1158 unsigned char active; /* cvt is currently used */
1159 unsigned char dirty; /* setups should be cleared */
1160};
1161
1162/* get or create a cache entry for the given audio converter NID */
1163static struct hda_cvt_setup *
1164get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1165{
1166 struct hda_cvt_setup *p;
1167 int i;
1168
1169 for (i = 0; i < codec->cvt_setups.used; i++) {
1170 p = snd_array_elem(&codec->cvt_setups, i);
1171 if (p->nid == nid)
1172 return p;
1173 }
1174 p = snd_array_new(&codec->cvt_setups);
1175 if (p)
1176 p->nid = nid;
1177 return p;
1178}
1179
1180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * codec destructor
1182 */
1183static void snd_hda_codec_free(struct hda_codec *codec)
1184{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001187 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001188#ifdef CONFIG_SND_HDA_POWER_SAVE
1189 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001190 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001193 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001194 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001195 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001196 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 codec->bus->caddr_tbl[codec->addr] = NULL;
1198 if (codec->patch_ops.free)
1199 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001200 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001201 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001202 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001203 kfree(codec->vendor_name);
1204 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001205 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001206 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 kfree(codec);
1208}
1209
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001210static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1211 unsigned int power_state);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/**
1214 * snd_hda_codec_new - create a HDA codec
1215 * @bus: the bus to assign
1216 * @codec_addr: the codec address
1217 * @codecp: the pointer to store the generated codec
1218 *
1219 * Returns 0 if successful, or a negative error code.
1220 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001221int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1222 unsigned int codec_addr,
1223 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001226 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int err;
1228
Takashi Iwaida3cec32008-08-08 17:12:14 +02001229 if (snd_BUG_ON(!bus))
1230 return -EINVAL;
1231 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001235 snd_printk(KERN_ERR "hda_codec: "
1236 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EBUSY;
1238 }
1239
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001240 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (codec == NULL) {
1242 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1243 return -ENOMEM;
1244 }
1245
1246 codec->bus = bus;
1247 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001248 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001249 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001250 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001251 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001252 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1253 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001254 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001255 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001256 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001257 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001258 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001259 if (codec->bus->modelname) {
1260 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1261 if (!codec->modelname) {
1262 snd_hda_codec_free(codec);
1263 return -ENODEV;
1264 }
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Takashi Iwaicb53c622007-08-10 17:21:45 +02001267#ifdef CONFIG_SND_HDA_POWER_SAVE
1268 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1269 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1270 * the caller has to power down appropriatley after initialization
1271 * phase.
1272 */
1273 hda_keep_power_on(codec);
1274#endif
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 list_add_tail(&codec->list, &bus->codec_list);
1277 bus->caddr_tbl[codec_addr] = codec;
1278
Takashi Iwai0ba21762007-04-16 11:29:14 +02001279 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1280 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001281 if (codec->vendor_id == -1)
1282 /* read again, hopefully the access method was corrected
1283 * in the last read...
1284 */
1285 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1286 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_SUBSYSTEM_ID);
1289 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001292 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001293 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001294 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001295 err = -ENODEV;
1296 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Takashi Iwai3be14142009-02-20 14:11:16 +01001299 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1300 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001301 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001302 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001303 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = read_pin_defaults(codec);
1305 if (err < 0)
1306 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001307
Takashi Iwai0ba21762007-04-16 11:29:14 +02001308 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001309 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001310 codec->subsystem_id =
1311 snd_hda_codec_read(codec, nid, 0,
1312 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001313 }
1314
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001315 /* power-up all before initialization */
1316 hda_set_power_state(codec,
1317 codec->afg ? codec->afg : codec->mfg,
1318 AC_PWRST_D0);
1319
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001320 snd_hda_codec_proc_new(codec);
1321
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001322 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001323
1324 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1325 codec->subsystem_id, codec->revision_id);
1326 snd_component_add(codec->bus->card, component);
1327
1328 if (codecp)
1329 *codecp = codec;
1330 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001331
1332 error:
1333 snd_hda_codec_free(codec);
1334 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001336EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001337
Takashi Iwaid5191e52009-11-16 14:58:17 +01001338/**
1339 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1340 * @codec: the HDA codec
1341 *
1342 * Start parsing of the given codec tree and (re-)initialize the whole
1343 * patch instance.
1344 *
1345 * Returns 0 if successful or a negative error code.
1346 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347int snd_hda_codec_configure(struct hda_codec *codec)
1348{
1349 int err;
1350
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001351 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001352 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001353 err = get_codec_name(codec);
1354 if (err < 0)
1355 return err;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Takashi Iwai82467612007-07-27 19:15:54 +02001358 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001360 goto patched;
1361 }
Takashi Iwai82467612007-07-27 19:15:54 +02001362 if (codec->preset && codec->preset->patch) {
1363 err = codec->preset->patch(codec);
1364 goto patched;
1365 }
1366
1367 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001368 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001369 if (err < 0)
1370 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001371
1372 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001373 if (!err && codec->patch_ops.unsol_event)
1374 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001375 /* audio codec should override the mixer name */
1376 if (!err && (codec->afg || !*codec->bus->card->mixername))
1377 snprintf(codec->bus->card->mixername,
1378 sizeof(codec->bus->card->mixername),
1379 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001382EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384/**
1385 * snd_hda_codec_setup_stream - set up the codec for streaming
1386 * @codec: the CODEC to set up
1387 * @nid: the NID to set up
1388 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1389 * @channel_id: channel id to pass, zero based.
1390 * @format: stream format.
1391 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001392void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1393 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int channel_id, int format)
1395{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001396 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001397 struct hda_cvt_setup *p;
1398 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001399 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001400 int i;
1401
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001403 return;
1404
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405 snd_printdd("hda_codec_setup_stream: "
1406 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001408 p = get_hda_cvt_setup(codec, nid);
1409 if (!p)
1410 return;
1411 /* update the stream-id if changed */
1412 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1413 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1414 newval = (stream_tag << 4) | channel_id;
1415 if (oldval != newval)
1416 snd_hda_codec_write(codec, nid, 0,
1417 AC_VERB_SET_CHANNEL_STREAMID,
1418 newval);
1419 p->stream_tag = stream_tag;
1420 p->channel_id = channel_id;
1421 }
1422 /* update the format-id if changed */
1423 if (p->format_id != format) {
1424 oldval = snd_hda_codec_read(codec, nid, 0,
1425 AC_VERB_GET_STREAM_FORMAT, 0);
1426 if (oldval != format) {
1427 msleep(1);
1428 snd_hda_codec_write(codec, nid, 0,
1429 AC_VERB_SET_STREAM_FORMAT,
1430 format);
1431 }
1432 p->format_id = format;
1433 }
1434 p->active = 1;
1435 p->dirty = 0;
1436
1437 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001438 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001439 list_for_each_entry(c, &codec->bus->codec_list, list) {
1440 for (i = 0; i < c->cvt_setups.used; i++) {
1441 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001442 if (!p->active && p->stream_tag == stream_tag &&
1443 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001444 p->dirty = 1;
1445 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001448EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Takashi Iwaif0cea792010-08-13 11:56:53 +02001450static void really_cleanup_stream(struct hda_codec *codec,
1451 struct hda_cvt_setup *q);
1452
Takashi Iwaid5191e52009-11-16 14:58:17 +01001453/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001454 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001455 * @codec: the CODEC to clean up
1456 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001457 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001458 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001459void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1460 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001461{
Takashi Iwaieb541332010-08-06 13:48:11 +02001462 struct hda_cvt_setup *p;
1463
Takashi Iwai888afa12008-03-18 09:57:50 +01001464 if (!nid)
1465 return;
1466
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001467 if (codec->no_sticky_stream)
1468 do_now = 1;
1469
Takashi Iwai888afa12008-03-18 09:57:50 +01001470 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001471 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001472 if (p) {
1473 /* here we just clear the active flag when do_now isn't set;
1474 * actual clean-ups will be done later in
1475 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1476 */
1477 if (do_now)
1478 really_cleanup_stream(codec, p);
1479 else
1480 p->active = 0;
1481 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001482}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001483EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001484
Takashi Iwaieb541332010-08-06 13:48:11 +02001485static void really_cleanup_stream(struct hda_codec *codec,
1486 struct hda_cvt_setup *q)
1487{
1488 hda_nid_t nid = q->nid;
1489 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1490 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1491 memset(q, 0, sizeof(*q));
1492 q->nid = nid;
1493}
1494
1495/* clean up the all conflicting obsolete streams */
1496static void purify_inactive_streams(struct hda_codec *codec)
1497{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001498 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001499 int i;
1500
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001501 list_for_each_entry(c, &codec->bus->codec_list, list) {
1502 for (i = 0; i < c->cvt_setups.used; i++) {
1503 struct hda_cvt_setup *p;
1504 p = snd_array_elem(&c->cvt_setups, i);
1505 if (p->dirty)
1506 really_cleanup_stream(c, p);
1507 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001508 }
1509}
1510
Takashi Iwai2a439522011-07-26 09:52:50 +02001511#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001512/* clean up all streams; called from suspend */
1513static void hda_cleanup_all_streams(struct hda_codec *codec)
1514{
1515 int i;
1516
1517 for (i = 0; i < codec->cvt_setups.used; i++) {
1518 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1519 if (p->stream_tag)
1520 really_cleanup_stream(codec, p);
1521 }
1522}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001523#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
1526 * amp access functions
1527 */
1528
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001529/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001530#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001531#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001532#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1533#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001535#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001538static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001539 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Takashi Iwai01751f52007-08-10 16:59:39 +02001541 memset(cache, 0, sizeof(*cache));
1542 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001543 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001544}
1545
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001546static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001547{
Takashi Iwai603c4012008-07-30 15:01:44 +02001548 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001552static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai01751f52007-08-10 16:59:39 +02001554 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1555 u16 cur = cache->hash[idx];
1556 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001559 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (info->key == key)
1561 return info;
1562 cur = info->next;
1563 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001564 return NULL;
1565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001567/* query the hash. allocate an entry if not found. */
1568static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1569 u32 key)
1570{
1571 struct hda_cache_head *info = get_hash(cache, key);
1572 if (!info) {
1573 u16 idx, cur;
1574 /* add a new hash entry */
1575 info = snd_array_new(&cache->buf);
1576 if (!info)
1577 return NULL;
1578 cur = snd_array_index(&cache->buf, info);
1579 info->key = key;
1580 info->val = 0;
1581 idx = key % (u16)ARRAY_SIZE(cache->hash);
1582 info->next = cache->hash[idx];
1583 cache->hash[idx] = cur;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return info;
1586}
1587
Takashi Iwai01751f52007-08-10 16:59:39 +02001588/* query and allocate an amp hash entry */
1589static inline struct hda_amp_info *
1590get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1591{
1592 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1593}
1594
Takashi Iwaid5191e52009-11-16 14:58:17 +01001595/**
1596 * query_amp_caps - query AMP capabilities
1597 * @codec: the HD-auio codec
1598 * @nid: the NID to query
1599 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1600 *
1601 * Query AMP capabilities for the given widget and direction.
1602 * Returns the obtained capability bits.
1603 *
1604 * When cap bits have been already read, this doesn't read again but
1605 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001607u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001609 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwai0ba21762007-04-16 11:29:14 +02001611 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1612 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001614 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001615 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001617 info->amp_caps = snd_hda_param_read(codec, nid,
1618 direction == HDA_OUTPUT ?
1619 AC_PAR_AMP_OUT_CAP :
1620 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001621 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001622 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 return info->amp_caps;
1625}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001626EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Takashi Iwaid5191e52009-11-16 14:58:17 +01001628/**
1629 * snd_hda_override_amp_caps - Override the AMP capabilities
1630 * @codec: the CODEC to clean up
1631 * @nid: the NID to clean up
1632 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1633 * @caps: the capability bits to set
1634 *
1635 * Override the cached AMP caps bits value by the given one.
1636 * This function is useful if the driver needs to adjust the AMP ranges,
1637 * e.g. limit to 0dB, etc.
1638 *
1639 * Returns zero if successful or a negative error code.
1640 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001641int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1642 unsigned int caps)
1643{
1644 struct hda_amp_info *info;
1645
1646 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1647 if (!info)
1648 return -EINVAL;
1649 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001650 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001651 return 0;
1652}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001653EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001654
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001655static unsigned int
1656query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1657 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001658{
1659 struct hda_amp_info *info;
1660
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001661 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001662 if (!info)
1663 return 0;
1664 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001665 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001666 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001667 }
1668 return info->amp_caps;
1669}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001670
1671static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1672{
1673 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1674}
1675
Takashi Iwaid5191e52009-11-16 14:58:17 +01001676/**
1677 * snd_hda_query_pin_caps - Query PIN capabilities
1678 * @codec: the HD-auio codec
1679 * @nid: the NID to query
1680 *
1681 * Query PIN capabilities for the given widget.
1682 * Returns the obtained capability bits.
1683 *
1684 * When cap bits have been already read, this doesn't read again but
1685 * returns the cached value.
1686 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001687u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1688{
1689 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1690 read_pin_cap);
1691}
Takashi Iwai1327a322009-03-23 13:07:47 +01001692EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1693
Wu Fengguang864f92b2009-11-18 12:38:02 +08001694/**
1695 * snd_hda_pin_sense - execute pin sense measurement
1696 * @codec: the CODEC to sense
1697 * @nid: the pin NID to sense
1698 *
1699 * Execute necessary pin sense measurement and return its Presence Detect,
1700 * Impedance, ELD Valid etc. status bits.
1701 */
1702u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1703{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001704 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001705
Takashi Iwai729d55b2009-12-25 22:49:01 +01001706 if (!codec->no_trigger_sense) {
1707 pincap = snd_hda_query_pin_caps(codec, nid);
1708 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001709 snd_hda_codec_read(codec, nid, 0,
1710 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001711 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001712 return snd_hda_codec_read(codec, nid, 0,
1713 AC_VERB_GET_PIN_SENSE, 0);
1714}
1715EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1716
1717/**
1718 * snd_hda_jack_detect - query pin Presence Detect status
1719 * @codec: the CODEC to sense
1720 * @nid: the pin NID to sense
1721 *
1722 * Query and return the pin's Presence Detect status.
1723 */
1724int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1725{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001726 u32 sense = snd_hda_pin_sense(codec, nid);
1727 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001728}
1729EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/*
1732 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001733 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001735static unsigned int get_vol_mute(struct hda_codec *codec,
1736 struct hda_amp_info *info, hda_nid_t nid,
1737 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 u32 val, parm;
1740
Takashi Iwai01751f52007-08-10 16:59:39 +02001741 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001742 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1745 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1746 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001747 val = snd_hda_codec_read(codec, nid, 0,
1748 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001750 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001751 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001755 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001757static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001758 hda_nid_t nid, int ch, int direction, int index,
1759 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 u32 parm;
1762
1763 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1764 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1765 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1766 parm |= val;
1767 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001768 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Takashi Iwaid5191e52009-11-16 14:58:17 +01001771/**
1772 * snd_hda_codec_amp_read - Read AMP value
1773 * @codec: HD-audio codec
1774 * @nid: NID to read the AMP value
1775 * @ch: channel (left=0 or right=1)
1776 * @direction: #HDA_INPUT or #HDA_OUTPUT
1777 * @index: the index value (only for input direction)
1778 *
1779 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Takashi Iwai834be882006-03-01 14:16:17 +01001781int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1782 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001784 struct hda_amp_info *info;
1785 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1786 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001788 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001790EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Takashi Iwaid5191e52009-11-16 14:58:17 +01001792/**
1793 * snd_hda_codec_amp_update - update the AMP value
1794 * @codec: HD-audio codec
1795 * @nid: NID to read the AMP value
1796 * @ch: channel (left=0 or right=1)
1797 * @direction: #HDA_INPUT or #HDA_OUTPUT
1798 * @idx: the index value (only for input direction)
1799 * @mask: bit mask to set
1800 * @val: the bits value to set
1801 *
1802 * Update the AMP value with a bit mask.
1803 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001804 */
Takashi Iwai834be882006-03-01 14:16:17 +01001805int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1806 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001808 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001809
Takashi Iwai0ba21762007-04-16 11:29:14 +02001810 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1811 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001813 if (snd_BUG_ON(mask & ~0xff))
1814 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001815 val &= mask;
1816 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001817 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001819 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return 1;
1821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001822EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Takashi Iwaid5191e52009-11-16 14:58:17 +01001824/**
1825 * snd_hda_codec_amp_stereo - update the AMP stereo values
1826 * @codec: HD-audio codec
1827 * @nid: NID to read the AMP value
1828 * @direction: #HDA_INPUT or #HDA_OUTPUT
1829 * @idx: the index value (only for input direction)
1830 * @mask: bit mask to set
1831 * @val: the bits value to set
1832 *
1833 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1834 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001835 */
1836int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1837 int direction, int idx, int mask, int val)
1838{
1839 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001840
1841 if (snd_BUG_ON(mask & ~0xff))
1842 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001843 for (ch = 0; ch < 2; ch++)
1844 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1845 idx, mask, val);
1846 return ret;
1847}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001848EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001849
Takashi Iwai2a439522011-07-26 09:52:50 +02001850#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001851/**
1852 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1853 * @codec: HD-audio codec
1854 *
1855 * Resume the all amp commands from the cache.
1856 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001857void snd_hda_codec_resume_amp(struct hda_codec *codec)
1858{
Takashi Iwai603c4012008-07-30 15:01:44 +02001859 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001860 int i;
1861
Takashi Iwai603c4012008-07-30 15:01:44 +02001862 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001863 u32 key = buffer->head.key;
1864 hda_nid_t nid;
1865 unsigned int idx, dir, ch;
1866 if (!key)
1867 continue;
1868 nid = key & 0xff;
1869 idx = (key >> 16) & 0xff;
1870 dir = (key >> 24) & 0xff;
1871 for (ch = 0; ch < 2; ch++) {
1872 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1873 continue;
1874 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1875 buffer->vol[ch]);
1876 }
1877 }
1878}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001879EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001880#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001882static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1883 unsigned int ofs)
1884{
1885 u32 caps = query_amp_caps(codec, nid, dir);
1886 /* get num steps */
1887 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1888 if (ofs < caps)
1889 caps -= ofs;
1890 return caps;
1891}
1892
Takashi Iwaid5191e52009-11-16 14:58:17 +01001893/**
1894 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1895 *
1896 * The control element is supposed to have the private_value field
1897 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1898 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001899int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1900 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1903 u16 nid = get_amp_nid(kcontrol);
1904 u8 chs = get_amp_channels(kcontrol);
1905 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001906 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001908 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1909 uinfo->count = chs == 3 ? 2 : 1;
1910 uinfo->value.integer.min = 0;
1911 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1912 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001913 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001914 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1915 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -EINVAL;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001920EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001922
1923static inline unsigned int
1924read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1925 int ch, int dir, int idx, unsigned int ofs)
1926{
1927 unsigned int val;
1928 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1929 val &= HDA_AMP_VOLMASK;
1930 if (val >= ofs)
1931 val -= ofs;
1932 else
1933 val = 0;
1934 return val;
1935}
1936
1937static inline int
1938update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1939 int ch, int dir, int idx, unsigned int ofs,
1940 unsigned int val)
1941{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001942 unsigned int maxval;
1943
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001944 if (val > 0)
1945 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001946 /* ofs = 0: raw max value */
1947 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001948 if (val > maxval)
1949 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001950 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1951 HDA_AMP_VOLMASK, val);
1952}
1953
Takashi Iwaid5191e52009-11-16 14:58:17 +01001954/**
1955 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1956 *
1957 * The control element is supposed to have the private_value field
1958 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1959 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001960int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1961 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1964 hda_nid_t nid = get_amp_nid(kcontrol);
1965 int chs = get_amp_channels(kcontrol);
1966 int dir = get_amp_direction(kcontrol);
1967 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001968 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 long *valp = ucontrol->value.integer.value;
1970
1971 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001972 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001974 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
1976}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001977EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Takashi Iwaid5191e52009-11-16 14:58:17 +01001979/**
1980 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1981 *
1982 * The control element is supposed to have the private_value field
1983 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1984 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001985int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1989 hda_nid_t nid = get_amp_nid(kcontrol);
1990 int chs = get_amp_channels(kcontrol);
1991 int dir = get_amp_direction(kcontrol);
1992 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001993 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 long *valp = ucontrol->value.integer.value;
1995 int change = 0;
1996
Takashi Iwaicb53c622007-08-10 17:21:45 +02001997 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001998 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002000 valp++;
2001 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002002 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002003 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002004 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return change;
2006}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002007EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Takashi Iwaid5191e52009-11-16 14:58:17 +01002009/**
2010 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2011 *
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2014 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002015int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2016 unsigned int size, unsigned int __user *_tlv)
2017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 hda_nid_t nid = get_amp_nid(kcontrol);
2020 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002021 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002022 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002023 u32 caps, val1, val2;
2024
2025 if (size < 4 * sizeof(unsigned int))
2026 return -ENOMEM;
2027 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002028 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2029 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002030 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002031 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002032 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002033 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002034 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002035 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2036 return -EFAULT;
2037 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2038 return -EFAULT;
2039 if (put_user(val1, _tlv + 2))
2040 return -EFAULT;
2041 if (put_user(val2, _tlv + 3))
2042 return -EFAULT;
2043 return 0;
2044}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002045EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002046
Takashi Iwaid5191e52009-11-16 14:58:17 +01002047/**
2048 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2049 * @codec: HD-audio codec
2050 * @nid: NID of a reference widget
2051 * @dir: #HDA_INPUT or #HDA_OUTPUT
2052 * @tlv: TLV data to be stored, at least 4 elements
2053 *
2054 * Set (static) TLV data for a virtual master volume using the AMP caps
2055 * obtained from the reference NID.
2056 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002057 */
2058void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2059 unsigned int *tlv)
2060{
2061 u32 caps;
2062 int nums, step;
2063
2064 caps = query_amp_caps(codec, nid, dir);
2065 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2066 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2067 step = (step + 1) * 25;
2068 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2069 tlv[1] = 2 * sizeof(unsigned int);
2070 tlv[2] = -nums * step;
2071 tlv[3] = step;
2072}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002073EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002074
2075/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002076static struct snd_kcontrol *
2077_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2078 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002079{
2080 struct snd_ctl_elem_id id;
2081 memset(&id, 0, sizeof(id));
2082 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002083 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002084 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2085 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002086 strcpy(id.name, name);
2087 return snd_ctl_find_id(codec->bus->card, &id);
2088}
2089
Takashi Iwaid5191e52009-11-16 14:58:17 +01002090/**
2091 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2092 * @codec: HD-audio codec
2093 * @name: ctl id name string
2094 *
2095 * Get the control element with the given id string and IFACE_MIXER.
2096 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002097struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2098 const char *name)
2099{
2100 return _snd_hda_find_mixer_ctl(codec, name, 0);
2101}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002102EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002103
Takashi Iwai1afe2062010-12-23 10:17:52 +01002104static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2105{
2106 int idx;
2107 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2108 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2109 return idx;
2110 }
2111 return -EBUSY;
2112}
2113
Takashi Iwaid5191e52009-11-16 14:58:17 +01002114/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002115 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002116 * @codec: HD-audio codec
2117 * @nid: corresponding NID (optional)
2118 * @kctl: the control element to assign
2119 *
2120 * Add the given control element to an array inside the codec instance.
2121 * All control elements belonging to a codec are supposed to be added
2122 * by this function so that a proper clean-up works at the free or
2123 * reconfiguration time.
2124 *
2125 * If non-zero @nid is passed, the NID is assigned to the control element.
2126 * The assignment is shown in the codec proc file.
2127 *
2128 * snd_hda_ctl_add() checks the control subdev id field whether
2129 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002130 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2131 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002132 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002133int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2134 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002135{
2136 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002137 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002138 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002139
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002140 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002141 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002142 if (nid == 0)
2143 nid = get_amp_nid_(kctl->private_value);
2144 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002145 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2146 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002147 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002148 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002149 err = snd_ctl_add(codec->bus->card, kctl);
2150 if (err < 0)
2151 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002152 item = snd_array_new(&codec->mixers);
2153 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002154 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002155 item->kctl = kctl;
2156 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002157 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002158 return 0;
2159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002160EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002161
Takashi Iwaid5191e52009-11-16 14:58:17 +01002162/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002163 * snd_hda_add_nid - Assign a NID to a control element
2164 * @codec: HD-audio codec
2165 * @nid: corresponding NID (optional)
2166 * @kctl: the control element to assign
2167 * @index: index to kctl
2168 *
2169 * Add the given control element to an array inside the codec instance.
2170 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2171 * NID:KCTL mapping - for example "Capture Source" selector.
2172 */
2173int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2174 unsigned int index, hda_nid_t nid)
2175{
2176 struct hda_nid_item *item;
2177
2178 if (nid > 0) {
2179 item = snd_array_new(&codec->nids);
2180 if (!item)
2181 return -ENOMEM;
2182 item->kctl = kctl;
2183 item->index = index;
2184 item->nid = nid;
2185 return 0;
2186 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002187 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2188 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002189 return -EINVAL;
2190}
2191EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2192
2193/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2195 * @codec: HD-audio codec
2196 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002197void snd_hda_ctls_clear(struct hda_codec *codec)
2198{
2199 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002200 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002201 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002202 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002203 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002204 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002205}
2206
Takashi Iwaia65d6292009-02-23 16:57:04 +01002207/* pseudo device locking
2208 * toggle card->shutdown to allow/disallow the device access (as a hack)
2209 */
2210static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002211{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002212 spin_lock(&card->files_lock);
2213 if (card->shutdown) {
2214 spin_unlock(&card->files_lock);
2215 return -EINVAL;
2216 }
2217 card->shutdown = 1;
2218 spin_unlock(&card->files_lock);
2219 return 0;
2220}
2221
2222static void hda_unlock_devices(struct snd_card *card)
2223{
2224 spin_lock(&card->files_lock);
2225 card->shutdown = 0;
2226 spin_unlock(&card->files_lock);
2227}
2228
Takashi Iwaid5191e52009-11-16 14:58:17 +01002229/**
2230 * snd_hda_codec_reset - Clear all objects assigned to the codec
2231 * @codec: HD-audio codec
2232 *
2233 * This frees the all PCM and control elements assigned to the codec, and
2234 * clears the caches and restores the pin default configurations.
2235 *
2236 * When a device is being used, it returns -EBSY. If successfully freed,
2237 * returns zero.
2238 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002239int snd_hda_codec_reset(struct hda_codec *codec)
2240{
2241 struct snd_card *card = codec->bus->card;
2242 int i, pcm;
2243
2244 if (hda_lock_devices(card) < 0)
2245 return -EBUSY;
2246 /* check whether the codec isn't used by any mixer or PCM streams */
2247 if (!list_empty(&card->ctl_files)) {
2248 hda_unlock_devices(card);
2249 return -EBUSY;
2250 }
2251 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2252 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2253 if (!cpcm->pcm)
2254 continue;
2255 if (cpcm->pcm->streams[0].substream_opened ||
2256 cpcm->pcm->streams[1].substream_opened) {
2257 hda_unlock_devices(card);
2258 return -EBUSY;
2259 }
2260 }
2261
2262 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002263
2264#ifdef CONFIG_SND_HDA_POWER_SAVE
2265 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002266 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002267#endif
2268 snd_hda_ctls_clear(codec);
2269 /* relase PCMs */
2270 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002271 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002272 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002273 clear_bit(codec->pcm_info[i].device,
2274 codec->bus->pcm_dev_bits);
2275 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002276 }
2277 if (codec->patch_ops.free)
2278 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002279 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002280 codec->spec = NULL;
2281 free_hda_cache(&codec->amp_cache);
2282 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002283 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2284 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002285 /* free only driver_pins so that init_pins + user_pins are restored */
2286 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002287 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002288 codec->num_pcms = 0;
2289 codec->pcm_info = NULL;
2290 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002291 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2292 codec->slave_dig_outs = NULL;
2293 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002294 module_put(codec->owner);
2295 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002296
2297 /* allow device access again */
2298 hda_unlock_devices(card);
2299 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002300}
2301
Takashi Iwaid5191e52009-11-16 14:58:17 +01002302/**
2303 * snd_hda_add_vmaster - create a virtual master control and add slaves
2304 * @codec: HD-audio codec
2305 * @name: vmaster control name
2306 * @tlv: TLV data (optional)
2307 * @slaves: slave control names (optional)
2308 *
2309 * Create a virtual master control with the given name. The TLV data
2310 * must be either NULL or a valid data.
2311 *
2312 * @slaves is a NULL-terminated array of strings, each of which is a
2313 * slave control name. All controls with these names are assigned to
2314 * the new virtual master control.
2315 *
2316 * This function returns zero if successful or a negative error code.
2317 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002318int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002319 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002320{
2321 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002322 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002323 int err;
2324
Takashi Iwai2f085542008-02-22 18:43:50 +01002325 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2326 ;
2327 if (!*s) {
2328 snd_printdd("No slave found for %s\n", name);
2329 return 0;
2330 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002331 kctl = snd_ctl_make_virtual_master(name, tlv);
2332 if (!kctl)
2333 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002334 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002335 if (err < 0)
2336 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002337
Takashi Iwai2134ea42008-01-10 16:53:55 +01002338 for (s = slaves; *s; s++) {
2339 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002340 int i = 0;
2341 for (;;) {
2342 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2343 if (!sctl) {
2344 if (!i)
2345 snd_printdd("Cannot find slave %s, "
2346 "skipped\n", *s);
2347 break;
2348 }
2349 err = snd_ctl_add_slave(kctl, sctl);
2350 if (err < 0)
2351 return err;
2352 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002353 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002354 }
2355 return 0;
2356}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002357EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002358
Takashi Iwaid5191e52009-11-16 14:58:17 +01002359/**
2360 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2361 *
2362 * The control element is supposed to have the private_value field
2363 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2364 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002365int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 int chs = get_amp_channels(kcontrol);
2369
2370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2371 uinfo->count = chs == 3 ? 2 : 1;
2372 uinfo->value.integer.min = 0;
2373 uinfo->value.integer.max = 1;
2374 return 0;
2375}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002376EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Takashi Iwaid5191e52009-11-16 14:58:17 +01002378/**
2379 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2380 *
2381 * The control element is supposed to have the private_value field
2382 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2383 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002384int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2388 hda_nid_t nid = get_amp_nid(kcontrol);
2389 int chs = get_amp_channels(kcontrol);
2390 int dir = get_amp_direction(kcontrol);
2391 int idx = get_amp_index(kcontrol);
2392 long *valp = ucontrol->value.integer.value;
2393
2394 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002395 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002396 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002398 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002399 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return 0;
2401}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002402EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Takashi Iwaid5191e52009-11-16 14:58:17 +01002404/**
2405 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2406 *
2407 * The control element is supposed to have the private_value field
2408 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2409 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002410int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2414 hda_nid_t nid = get_amp_nid(kcontrol);
2415 int chs = get_amp_channels(kcontrol);
2416 int dir = get_amp_direction(kcontrol);
2417 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 long *valp = ucontrol->value.integer.value;
2419 int change = 0;
2420
Takashi Iwaicb53c622007-08-10 17:21:45 +02002421 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002422 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002423 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002424 HDA_AMP_MUTE,
2425 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002426 valp++;
2427 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002428 if (chs & 2)
2429 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002430 HDA_AMP_MUTE,
2431 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002432 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002433 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return change;
2435}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002436EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Takashi Iwai67d634c2009-11-16 15:35:59 +01002438#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002439/**
2440 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2441 *
2442 * This function calls snd_hda_enable_beep_device(), which behaves differently
2443 * depending on beep_mode option.
2444 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002445int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_value *ucontrol)
2447{
2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449 long *valp = ucontrol->value.integer.value;
2450
2451 snd_hda_enable_beep_device(codec, *valp);
2452 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2453}
2454EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002455#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457/*
Takashi Iwai985be542005-11-02 18:26:49 +01002458 * bound volume controls
2459 *
2460 * bind multiple volumes (# indices, from 0)
2461 */
2462
2463#define AMP_VAL_IDX_SHIFT 19
2464#define AMP_VAL_IDX_MASK (0x0f<<19)
2465
Takashi Iwaid5191e52009-11-16 14:58:17 +01002466/**
2467 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2468 *
2469 * The control element is supposed to have the private_value field
2470 * set up via HDA_BIND_MUTE*() macros.
2471 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002472int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002474{
2475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2476 unsigned long pval;
2477 int err;
2478
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002479 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002480 pval = kcontrol->private_value;
2481 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2482 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2483 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002484 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002485 return err;
2486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002487EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002488
Takashi Iwaid5191e52009-11-16 14:58:17 +01002489/**
2490 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2491 *
2492 * The control element is supposed to have the private_value field
2493 * set up via HDA_BIND_MUTE*() macros.
2494 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002495int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002497{
2498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2499 unsigned long pval;
2500 int i, indices, err = 0, change = 0;
2501
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002502 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002503 pval = kcontrol->private_value;
2504 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2505 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002506 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2507 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002508 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2509 if (err < 0)
2510 break;
2511 change |= err;
2512 }
2513 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002514 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002515 return err < 0 ? err : change;
2516}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002517EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002518
Takashi Iwaid5191e52009-11-16 14:58:17 +01002519/**
2520 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2521 *
2522 * The control element is supposed to have the private_value field
2523 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002524 */
2525int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
2527{
2528 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2529 struct hda_bind_ctls *c;
2530 int err;
2531
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002532 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002533 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002534 kcontrol->private_value = *c->values;
2535 err = c->ops->info(kcontrol, uinfo);
2536 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002537 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002538 return err;
2539}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002540EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002541
Takashi Iwaid5191e52009-11-16 14:58:17 +01002542/**
2543 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2544 *
2545 * The control element is supposed to have the private_value field
2546 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2547 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002548int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2549 struct snd_ctl_elem_value *ucontrol)
2550{
2551 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2552 struct hda_bind_ctls *c;
2553 int err;
2554
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002555 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002556 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002557 kcontrol->private_value = *c->values;
2558 err = c->ops->get(kcontrol, ucontrol);
2559 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002560 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002561 return err;
2562}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002563EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002564
Takashi Iwaid5191e52009-11-16 14:58:17 +01002565/**
2566 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2567 *
2568 * The control element is supposed to have the private_value field
2569 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2570 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002571int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_value *ucontrol)
2573{
2574 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2575 struct hda_bind_ctls *c;
2576 unsigned long *vals;
2577 int err = 0, change = 0;
2578
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002579 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002580 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002581 for (vals = c->values; *vals; vals++) {
2582 kcontrol->private_value = *vals;
2583 err = c->ops->put(kcontrol, ucontrol);
2584 if (err < 0)
2585 break;
2586 change |= err;
2587 }
2588 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002589 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002590 return err < 0 ? err : change;
2591}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002592EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002593
Takashi Iwaid5191e52009-11-16 14:58:17 +01002594/**
2595 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2596 *
2597 * The control element is supposed to have the private_value field
2598 * set up via HDA_BIND_VOL() macro.
2599 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002600int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2601 unsigned int size, unsigned int __user *tlv)
2602{
2603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2604 struct hda_bind_ctls *c;
2605 int err;
2606
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002607 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002608 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002609 kcontrol->private_value = *c->values;
2610 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2611 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002612 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002613 return err;
2614}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002615EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002616
2617struct hda_ctl_ops snd_hda_bind_vol = {
2618 .info = snd_hda_mixer_amp_volume_info,
2619 .get = snd_hda_mixer_amp_volume_get,
2620 .put = snd_hda_mixer_amp_volume_put,
2621 .tlv = snd_hda_mixer_amp_tlv
2622};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002623EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002624
2625struct hda_ctl_ops snd_hda_bind_sw = {
2626 .info = snd_hda_mixer_amp_switch_info,
2627 .get = snd_hda_mixer_amp_switch_get,
2628 .put = snd_hda_mixer_amp_switch_put,
2629 .tlv = snd_hda_mixer_amp_tlv
2630};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002631EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002632
2633/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 * SPDIF out controls
2635 */
2636
Takashi Iwai0ba21762007-04-16 11:29:14 +02002637static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2638 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2641 uinfo->count = 1;
2642 return 0;
2643}
2644
Takashi Iwai0ba21762007-04-16 11:29:14 +02002645static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
2648 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2649 IEC958_AES0_NONAUDIO |
2650 IEC958_AES0_CON_EMPHASIS_5015 |
2651 IEC958_AES0_CON_NOT_COPYRIGHT;
2652 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2653 IEC958_AES1_CON_ORIGINAL;
2654 return 0;
2655}
2656
Takashi Iwai0ba21762007-04-16 11:29:14 +02002657static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2661 IEC958_AES0_NONAUDIO |
2662 IEC958_AES0_PRO_EMPHASIS_5015;
2663 return 0;
2664}
2665
Takashi Iwai0ba21762007-04-16 11:29:14 +02002666static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002670 int idx = kcontrol->private_value;
2671 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Stephen Warren7c9359762011-06-01 11:14:17 -06002673 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2674 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2675 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2676 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 return 0;
2679}
2680
2681/* convert from SPDIF status bits to HDA SPDIF bits
2682 * bit 0 (DigEn) is always set zero (to be filled later)
2683 */
2684static unsigned short convert_from_spdif_status(unsigned int sbits)
2685{
2686 unsigned short val = 0;
2687
2688 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002689 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002693 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2694 IEC958_AES0_PRO_EMPHASIS_5015)
2695 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2698 IEC958_AES0_CON_EMPHASIS_5015)
2699 val |= AC_DIG1_EMPHASIS;
2700 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2701 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002703 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2705 }
2706 return val;
2707}
2708
2709/* convert to SPDIF status bits from HDA SPDIF bits
2710 */
2711static unsigned int convert_to_spdif_status(unsigned short val)
2712{
2713 unsigned int sbits = 0;
2714
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 sbits |= IEC958_AES0_PROFESSIONAL;
2719 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002720 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2722 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002725 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002727 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2729 sbits |= val & (0x7f << 8);
2730 }
2731 return sbits;
2732}
2733
Takashi Iwai2f728532008-09-25 16:32:41 +02002734/* set digital convert verbs both for the given NID and its slaves */
2735static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2736 int verb, int val)
2737{
Takashi Iwaidda14412011-05-02 11:29:30 +02002738 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002739
Takashi Iwai9e976972008-11-25 08:17:20 +01002740 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002741 d = codec->slave_dig_outs;
2742 if (!d)
2743 return;
2744 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002745 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002746}
2747
2748static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2749 int dig1, int dig2)
2750{
2751 if (dig1 != -1)
2752 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2753 if (dig2 != -1)
2754 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2755}
2756
Takashi Iwai0ba21762007-04-16 11:29:14 +02002757static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002761 int idx = kcontrol->private_value;
2762 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2763 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 unsigned short val;
2765 int change;
2766
Ingo Molnar62932df2006-01-16 16:34:20 +01002767 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002768 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2770 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2771 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002772 val = convert_from_spdif_status(spdif->status);
2773 val |= spdif->ctls & 1;
2774 change = spdif->ctls != val;
2775 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002776 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002777 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002778 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return change;
2780}
2781
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002782#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Takashi Iwai0ba21762007-04-16 11:29:14 +02002784static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002788 int idx = kcontrol->private_value;
2789 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Stephen Warren7c9359762011-06-01 11:14:17 -06002791 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793}
2794
Stephen Warren74b654c2011-06-01 11:14:18 -06002795static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2796 int dig1, int dig2)
2797{
2798 set_dig_out_convert(codec, nid, dig1, dig2);
2799 /* unmute amp switch (if any) */
2800 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2801 (dig1 & AC_DIG1_ENABLE))
2802 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2803 HDA_AMP_MUTE, 0);
2804}
2805
Takashi Iwai0ba21762007-04-16 11:29:14 +02002806static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
2809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002810 int idx = kcontrol->private_value;
2811 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2812 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 unsigned short val;
2814 int change;
2815
Ingo Molnar62932df2006-01-16 16:34:20 +01002816 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002817 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002819 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002820 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002821 spdif->ctls = val;
2822 if (change && nid != (u16)-1)
2823 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002824 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return change;
2826}
2827
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002828static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 {
2830 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002832 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 .info = snd_hda_spdif_mask_info,
2834 .get = snd_hda_spdif_cmask_get,
2835 },
2836 {
2837 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2838 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002839 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 .info = snd_hda_spdif_mask_info,
2841 .get = snd_hda_spdif_pmask_get,
2842 },
2843 {
2844 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002845 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 .info = snd_hda_spdif_mask_info,
2847 .get = snd_hda_spdif_default_get,
2848 .put = snd_hda_spdif_default_put,
2849 },
2850 {
2851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002852 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 .info = snd_hda_spdif_out_switch_info,
2854 .get = snd_hda_spdif_out_switch_get,
2855 .put = snd_hda_spdif_out_switch_put,
2856 },
2857 { } /* end */
2858};
2859
2860/**
2861 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2862 * @codec: the HDA codec
2863 * @nid: audio out widget NID
2864 *
2865 * Creates controls related with the SPDIF output.
2866 * Called from each patch supporting the SPDIF out.
2867 *
2868 * Returns 0 if successful, or a negative error code.
2869 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002870int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2871 hda_nid_t associated_nid,
2872 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
2874 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002875 struct snd_kcontrol *kctl;
2876 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002877 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002878 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Takashi Iwai1afe2062010-12-23 10:17:52 +01002880 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2881 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002882 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2883 return -EBUSY;
2884 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002885 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2887 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002888 if (!kctl)
2889 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002890 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002891 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002892 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002893 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return err;
2895 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002896 spdif->nid = cvt_nid;
2897 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002898 AC_VERB_GET_DIGI_CONVERT_1, 0);
2899 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return 0;
2901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002902EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Stephen Warren7c9359762011-06-01 11:14:17 -06002904struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2905 hda_nid_t nid)
2906{
2907 int i;
2908 for (i = 0; i < codec->spdif_out.used; i++) {
2909 struct hda_spdif_out *spdif =
2910 snd_array_elem(&codec->spdif_out, i);
2911 if (spdif->nid == nid)
2912 return spdif;
2913 }
2914 return NULL;
2915}
2916EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2917
Stephen Warren74b654c2011-06-01 11:14:18 -06002918void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2919{
2920 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2921
2922 mutex_lock(&codec->spdif_mutex);
2923 spdif->nid = (u16)-1;
2924 mutex_unlock(&codec->spdif_mutex);
2925}
2926EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2927
2928void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2929{
2930 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2931 unsigned short val;
2932
2933 mutex_lock(&codec->spdif_mutex);
2934 if (spdif->nid != nid) {
2935 spdif->nid = nid;
2936 val = spdif->ctls;
2937 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2938 }
2939 mutex_unlock(&codec->spdif_mutex);
2940}
2941EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002944 * SPDIF sharing with analog output
2945 */
2946static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
2949 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2950 ucontrol->value.integer.value[0] = mout->share_spdif;
2951 return 0;
2952}
2953
2954static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_value *ucontrol)
2956{
2957 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2958 mout->share_spdif = !!ucontrol->value.integer.value[0];
2959 return 0;
2960}
2961
2962static struct snd_kcontrol_new spdif_share_sw = {
2963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2964 .name = "IEC958 Default PCM Playback Switch",
2965 .info = snd_ctl_boolean_mono_info,
2966 .get = spdif_share_sw_get,
2967 .put = spdif_share_sw_put,
2968};
2969
Takashi Iwaid5191e52009-11-16 14:58:17 +01002970/**
2971 * snd_hda_create_spdif_share_sw - create Default PCM switch
2972 * @codec: the HDA codec
2973 * @mout: multi-out instance
2974 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002975int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2976 struct hda_multi_out *mout)
2977{
2978 if (!mout->dig_out_nid)
2979 return 0;
2980 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002981 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2982 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002984EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002985
2986/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * SPDIF input
2988 */
2989
2990#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2991
Takashi Iwai0ba21762007-04-16 11:29:14 +02002992static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2996
2997 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2998 return 0;
2999}
3000
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3002 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3005 hda_nid_t nid = kcontrol->private_value;
3006 unsigned int val = !!ucontrol->value.integer.value[0];
3007 int change;
3008
Ingo Molnar62932df2006-01-16 16:34:20 +01003009 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003011 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003013 snd_hda_codec_write_cache(codec, nid, 0,
3014 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003016 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return change;
3018}
3019
Takashi Iwai0ba21762007-04-16 11:29:14 +02003020static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3024 hda_nid_t nid = kcontrol->private_value;
3025 unsigned short val;
3026 unsigned int sbits;
3027
Andrew Paprocki3982d172007-12-19 12:13:44 +01003028 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 sbits = convert_to_spdif_status(val);
3030 ucontrol->value.iec958.status[0] = sbits;
3031 ucontrol->value.iec958.status[1] = sbits >> 8;
3032 ucontrol->value.iec958.status[2] = sbits >> 16;
3033 ucontrol->value.iec958.status[3] = sbits >> 24;
3034 return 0;
3035}
3036
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003037static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 {
3039 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003040 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .info = snd_hda_spdif_in_switch_info,
3042 .get = snd_hda_spdif_in_switch_get,
3043 .put = snd_hda_spdif_in_switch_put,
3044 },
3045 {
3046 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3047 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003048 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 .info = snd_hda_spdif_mask_info,
3050 .get = snd_hda_spdif_in_status_get,
3051 },
3052 { } /* end */
3053};
3054
3055/**
3056 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3057 * @codec: the HDA codec
3058 * @nid: audio in widget NID
3059 *
3060 * Creates controls related with the SPDIF input.
3061 * Called from each patch supporting the SPDIF in.
3062 *
3063 * Returns 0 if successful, or a negative error code.
3064 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003065int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003068 struct snd_kcontrol *kctl;
3069 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003070 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Takashi Iwai1afe2062010-12-23 10:17:52 +01003072 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3073 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003074 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3075 return -EBUSY;
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3078 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003079 if (!kctl)
3080 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003082 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return err;
3085 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003086 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003087 snd_hda_codec_read(codec, nid, 0,
3088 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return 0;
3091}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003092EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Takashi Iwai2a439522011-07-26 09:52:50 +02003094#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003095/*
3096 * command cache
3097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003099/* build a 32bit cache key with the widget id and the command parameter */
3100#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3101#define get_cmd_cache_nid(key) ((key) & 0xff)
3102#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3103
3104/**
3105 * snd_hda_codec_write_cache - send a single command with caching
3106 * @codec: the HDA codec
3107 * @nid: NID to send the command
3108 * @direct: direct flag
3109 * @verb: the verb to send
3110 * @parm: the parameter for the verb
3111 *
3112 * Send a single command without waiting for response.
3113 *
3114 * Returns 0 if successful, or a negative error code.
3115 */
3116int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3117 int direct, unsigned int verb, unsigned int parm)
3118{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003119 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3120 struct hda_cache_head *c;
3121 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003122
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003123 if (err < 0)
3124 return err;
3125 /* parm may contain the verb stuff for get/set amp */
3126 verb = verb | (parm >> 8);
3127 parm &= 0xff;
3128 key = build_cmd_cache_key(nid, verb);
3129 mutex_lock(&codec->bus->cmd_mutex);
3130 c = get_alloc_hash(&codec->cmd_cache, key);
3131 if (c)
3132 c->val = parm;
3133 mutex_unlock(&codec->bus->cmd_mutex);
3134 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003136EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003137
Takashi Iwaid5191e52009-11-16 14:58:17 +01003138/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003139 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3140 * @codec: the HDA codec
3141 * @nid: NID to send the command
3142 * @direct: direct flag
3143 * @verb: the verb to send
3144 * @parm: the parameter for the verb
3145 *
3146 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3147 * command if the parameter is already identical with the cached value.
3148 * If not, it sends the command and refreshes the cache.
3149 *
3150 * Returns 0 if successful, or a negative error code.
3151 */
3152int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3153 int direct, unsigned int verb, unsigned int parm)
3154{
3155 struct hda_cache_head *c;
3156 u32 key;
3157
3158 /* parm may contain the verb stuff for get/set amp */
3159 verb = verb | (parm >> 8);
3160 parm &= 0xff;
3161 key = build_cmd_cache_key(nid, verb);
3162 mutex_lock(&codec->bus->cmd_mutex);
3163 c = get_hash(&codec->cmd_cache, key);
3164 if (c && c->val == parm) {
3165 mutex_unlock(&codec->bus->cmd_mutex);
3166 return 0;
3167 }
3168 mutex_unlock(&codec->bus->cmd_mutex);
3169 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3170}
3171EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3172
3173/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003174 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3175 * @codec: HD-audio codec
3176 *
3177 * Execute all verbs recorded in the command caches to resume.
3178 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003179void snd_hda_codec_resume_cache(struct hda_codec *codec)
3180{
Takashi Iwai603c4012008-07-30 15:01:44 +02003181 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003182 int i;
3183
Takashi Iwai603c4012008-07-30 15:01:44 +02003184 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003185 u32 key = buffer->key;
3186 if (!key)
3187 continue;
3188 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3189 get_cmd_cache_cmd(key), buffer->val);
3190 }
3191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003192EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003193
3194/**
3195 * snd_hda_sequence_write_cache - sequence writes with caching
3196 * @codec: the HDA codec
3197 * @seq: VERB array to send
3198 *
3199 * Send the commands sequentially from the given array.
3200 * Thte commands are recorded on cache for power-save and resume.
3201 * The array must be terminated with NID=0.
3202 */
3203void snd_hda_sequence_write_cache(struct hda_codec *codec,
3204 const struct hda_verb *seq)
3205{
3206 for (; seq->nid; seq++)
3207 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3208 seq->param);
3209}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003210EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003211#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003212
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003213void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3214 unsigned int power_state,
3215 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003216{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003217 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003218 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003219
Takashi Iwaicb53c622007-08-10 17:21:45 +02003220 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003221 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003222 if (!(wcaps & AC_WCAP_POWER))
3223 continue;
3224 /* don't power down the widget if it controls eapd and
3225 * EAPD_BTLENABLE is set.
3226 */
3227 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3228 get_wcaps_type(wcaps) == AC_WID_PIN &&
3229 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3230 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003231 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003232 if (eapd & 0x02)
3233 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003234 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003235 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3236 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003237 }
3238
Takashi Iwaicb53c622007-08-10 17:21:45 +02003239 if (power_state == AC_PWRST_D0) {
3240 unsigned long end_time;
3241 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003242 /* wait until the codec reachs to D0 */
3243 end_time = jiffies + msecs_to_jiffies(500);
3244 do {
3245 state = snd_hda_codec_read(codec, fg, 0,
3246 AC_VERB_GET_POWER_STATE, 0);
3247 if (state == power_state)
3248 break;
3249 msleep(1);
3250 } while (time_after_eq(end_time, jiffies));
3251 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003252}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003253EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3254
3255/*
3256 * set power state of the codec
3257 */
3258static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3259 unsigned int power_state)
3260{
3261 if (codec->patch_ops.set_power_state) {
3262 codec->patch_ops.set_power_state(codec, fg, power_state);
3263 return;
3264 }
3265
3266 /* this delay seems necessary to avoid click noise at power-down */
3267 if (power_state == AC_PWRST_D3)
3268 msleep(100);
3269 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3270 power_state);
3271 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3272}
Takashi Iwai54d17402005-11-21 16:33:22 +01003273
Takashi Iwai11aeff02008-07-30 15:01:46 +02003274#ifdef CONFIG_SND_HDA_HWDEP
3275/* execute additional init verbs */
3276static void hda_exec_init_verbs(struct hda_codec *codec)
3277{
3278 if (codec->init_verbs.list)
3279 snd_hda_sequence_write(codec, codec->init_verbs.list);
3280}
3281#else
3282static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3283#endif
3284
Takashi Iwai2a439522011-07-26 09:52:50 +02003285#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003286/*
3287 * call suspend and power-down; used both from PM and power-save
3288 */
3289static void hda_call_codec_suspend(struct hda_codec *codec)
3290{
3291 if (codec->patch_ops.suspend)
3292 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003293 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003294 hda_set_power_state(codec,
3295 codec->afg ? codec->afg : codec->mfg,
3296 AC_PWRST_D3);
3297#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003298 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003299 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003300 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003301 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003302 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003303#endif
3304}
3305
3306/*
3307 * kick up codec; used both from PM and power-save
3308 */
3309static void hda_call_codec_resume(struct hda_codec *codec)
3310{
3311 hda_set_power_state(codec,
3312 codec->afg ? codec->afg : codec->mfg,
3313 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003314 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003315 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003316 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003317 if (codec->patch_ops.resume)
3318 codec->patch_ops.resume(codec);
3319 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003320 if (codec->patch_ops.init)
3321 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003322 snd_hda_codec_resume_amp(codec);
3323 snd_hda_codec_resume_cache(codec);
3324 }
3325}
Takashi Iwai2a439522011-07-26 09:52:50 +02003326#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003327
Takashi Iwai54d17402005-11-21 16:33:22 +01003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/**
3330 * snd_hda_build_controls - build mixer controls
3331 * @bus: the BUS
3332 *
3333 * Creates mixer controls for each codec included in the bus.
3334 *
3335 * Returns 0 if successful, otherwise a negative error code.
3336 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003337int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003339 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Takashi Iwai0ba21762007-04-16 11:29:14 +02003341 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003342 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003343 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003344 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003345 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003346 err = snd_hda_codec_reset(codec);
3347 if (err < 0) {
3348 printk(KERN_ERR
3349 "hda_codec: cannot revert codec\n");
3350 return err;
3351 }
3352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003354 return 0;
3355}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003356EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003358int snd_hda_codec_build_controls(struct hda_codec *codec)
3359{
3360 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003361 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003362 /* continue to initialize... */
3363 if (codec->patch_ops.init)
3364 err = codec->patch_ops.init(codec);
3365 if (!err && codec->patch_ops.build_controls)
3366 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003367 if (err < 0)
3368 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return 0;
3370}
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372/*
3373 * stream formats
3374 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003375struct hda_rate_tbl {
3376 unsigned int hz;
3377 unsigned int alsa_bits;
3378 unsigned int hda_fmt;
3379};
3380
Takashi Iwai92f10b32010-08-03 14:21:00 +02003381/* rate = base * mult / div */
3382#define HDA_RATE(base, mult, div) \
3383 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3384 (((div) - 1) << AC_FMT_DIV_SHIFT))
3385
Takashi Iwaibefdf312005-08-22 13:57:55 +02003386static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003388
3389 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003390 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3391 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3392 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3393 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3394 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3395 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3396 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3397 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3398 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3399 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3400 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003401#define AC_PAR_PCM_RATE_BITS 11
3402 /* up to bits 10, 384kHZ isn't supported properly */
3403
3404 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003405 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003406
Takashi Iwaibefdf312005-08-22 13:57:55 +02003407 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408};
3409
3410/**
3411 * snd_hda_calc_stream_format - calculate format bitset
3412 * @rate: the sample rate
3413 * @channels: the number of channels
3414 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3415 * @maxbps: the max. bps
3416 *
3417 * Calculate the format bitset from the given rate, channels and th PCM format.
3418 *
3419 * Return zero if invalid.
3420 */
3421unsigned int snd_hda_calc_stream_format(unsigned int rate,
3422 unsigned int channels,
3423 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003424 unsigned int maxbps,
3425 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 int i;
3428 unsigned int val = 0;
3429
Takashi Iwaibefdf312005-08-22 13:57:55 +02003430 for (i = 0; rate_bits[i].hz; i++)
3431 if (rate_bits[i].hz == rate) {
3432 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 break;
3434 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003435 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 snd_printdd("invalid rate %d\n", rate);
3437 return 0;
3438 }
3439
3440 if (channels == 0 || channels > 8) {
3441 snd_printdd("invalid channels %d\n", channels);
3442 return 0;
3443 }
3444 val |= channels - 1;
3445
3446 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003447 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003448 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003449 break;
3450 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003451 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 case 20:
3454 case 24:
3455 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003456 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003457 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003459 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003461 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 break;
3463 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003464 snd_printdd("invalid format width %d\n",
3465 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return 0;
3467 }
3468
Anssi Hannula32c168c2010-08-03 13:28:57 +03003469 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003470 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return val;
3473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003474EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003476static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3477{
3478 unsigned int val = 0;
3479 if (nid != codec->afg &&
3480 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3481 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3482 if (!val || val == -1)
3483 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3484 if (!val || val == -1)
3485 return 0;
3486 return val;
3487}
3488
3489static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3490{
3491 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3492 get_pcm_param);
3493}
3494
3495static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3496{
3497 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3498 if (!streams || streams == -1)
3499 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3500 if (!streams || streams == -1)
3501 return 0;
3502 return streams;
3503}
3504
3505static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3506{
3507 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3508 get_stream_param);
3509}
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511/**
3512 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3513 * @codec: the HDA codec
3514 * @nid: NID to query
3515 * @ratesp: the pointer to store the detected rate bitflags
3516 * @formatsp: the pointer to store the detected formats
3517 * @bpsp: the pointer to store the detected format widths
3518 *
3519 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3520 * or @bsps argument is ignored.
3521 *
3522 * Returns 0 if successful, otherwise a negative error code.
3523 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003524int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3526{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003527 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003529 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003530 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 if (ratesp) {
3533 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003534 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003536 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003538 if (rates == 0) {
3539 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3540 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3541 nid, val,
3542 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3543 return -EIO;
3544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 *ratesp = rates;
3546 }
3547
3548 if (formatsp || bpsp) {
3549 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003550 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003552 streams = query_stream_param(codec, nid);
3553 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 bps = 0;
3557 if (streams & AC_SUPFMT_PCM) {
3558 if (val & AC_SUPPCM_BITS_8) {
3559 formats |= SNDRV_PCM_FMTBIT_U8;
3560 bps = 8;
3561 }
3562 if (val & AC_SUPPCM_BITS_16) {
3563 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3564 bps = 16;
3565 }
3566 if (wcaps & AC_WCAP_DIGITAL) {
3567 if (val & AC_SUPPCM_BITS_32)
3568 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3569 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3570 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3571 if (val & AC_SUPPCM_BITS_24)
3572 bps = 24;
3573 else if (val & AC_SUPPCM_BITS_20)
3574 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003575 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3576 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3578 if (val & AC_SUPPCM_BITS_32)
3579 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 else if (val & AC_SUPPCM_BITS_24)
3581 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003582 else if (val & AC_SUPPCM_BITS_20)
3583 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
3585 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003586 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003588 if (!bps)
3589 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003590 }
3591 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003592 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 /* temporary hack: we have still no proper support
3594 * for the direct AC3 stream...
3595 */
3596 formats |= SNDRV_PCM_FMTBIT_U8;
3597 bps = 8;
3598 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003599 if (formats == 0) {
3600 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3601 "(nid=0x%x, val=0x%x, ovrd=%i, "
3602 "streams=0x%x)\n",
3603 nid, val,
3604 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3605 streams);
3606 return -EIO;
3607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 if (formatsp)
3609 *formatsp = formats;
3610 if (bpsp)
3611 *bpsp = bps;
3612 }
3613
3614 return 0;
3615}
Stephen Warren384a48d2011-06-01 11:14:21 -06003616EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003619 * snd_hda_is_supported_format - Check the validity of the format
3620 * @codec: HD-audio codec
3621 * @nid: NID to check
3622 * @format: the HD-audio format value to check
3623 *
3624 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 *
3626 * Returns 1 if supported, 0 if not.
3627 */
3628int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3629 unsigned int format)
3630{
3631 int i;
3632 unsigned int val = 0, rate, stream;
3633
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003634 val = query_pcm_param(codec, nid);
3635 if (!val)
3636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003639 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003640 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (val & (1 << i))
3642 break;
3643 return 0;
3644 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003645 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return 0;
3647
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003648 stream = query_stream_param(codec, nid);
3649 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return 0;
3651
3652 if (stream & AC_SUPFMT_PCM) {
3653 switch (format & 0xf0) {
3654 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003655 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return 0;
3657 break;
3658 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003659 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return 0;
3661 break;
3662 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003663 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return 0;
3665 break;
3666 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003667 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return 0;
3669 break;
3670 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003671 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return 0;
3673 break;
3674 default:
3675 return 0;
3676 }
3677 } else {
3678 /* FIXME: check for float32 and AC3? */
3679 }
3680
3681 return 1;
3682}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003683EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685/*
3686 * PCM stuff
3687 */
3688static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3689 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003690 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 return 0;
3693}
3694
3695static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3696 struct hda_codec *codec,
3697 unsigned int stream_tag,
3698 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003699 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3702 return 0;
3703}
3704
3705static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3706 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003707 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
Takashi Iwai888afa12008-03-18 09:57:50 +01003709 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return 0;
3711}
3712
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003713static int set_pcm_default_values(struct hda_codec *codec,
3714 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003716 int err;
3717
Takashi Iwai0ba21762007-04-16 11:29:14 +02003718 /* query support PCM information from the given NID */
3719 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003720 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003721 info->rates ? NULL : &info->rates,
3722 info->formats ? NULL : &info->formats,
3723 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003724 if (err < 0)
3725 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
3727 if (info->ops.open == NULL)
3728 info->ops.open = hda_pcm_default_open_close;
3729 if (info->ops.close == NULL)
3730 info->ops.close = hda_pcm_default_open_close;
3731 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003732 if (snd_BUG_ON(!info->nid))
3733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 info->ops.prepare = hda_pcm_default_prepare;
3735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003737 if (snd_BUG_ON(!info->nid))
3738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 info->ops.cleanup = hda_pcm_default_cleanup;
3740 }
3741 return 0;
3742}
3743
Takashi Iwaieb541332010-08-06 13:48:11 +02003744/*
3745 * codec prepare/cleanup entries
3746 */
3747int snd_hda_codec_prepare(struct hda_codec *codec,
3748 struct hda_pcm_stream *hinfo,
3749 unsigned int stream,
3750 unsigned int format,
3751 struct snd_pcm_substream *substream)
3752{
3753 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003754 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003755 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3756 if (ret >= 0)
3757 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003758 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003759 return ret;
3760}
3761EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3762
3763void snd_hda_codec_cleanup(struct hda_codec *codec,
3764 struct hda_pcm_stream *hinfo,
3765 struct snd_pcm_substream *substream)
3766{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003767 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003768 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003769 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003770}
3771EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3772
Takashi Iwaid5191e52009-11-16 14:58:17 +01003773/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003774const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3775 "Audio", "SPDIF", "HDMI", "Modem"
3776};
3777
Takashi Iwai176d5332008-07-30 15:01:44 +02003778/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003779 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003780 *
3781 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003782 */
3783static int get_empty_pcm_device(struct hda_bus *bus, int type)
3784{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003785 /* audio device indices; not linear to keep compatibility */
3786 static int audio_idx[HDA_PCM_NTYPES][5] = {
3787 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3788 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003789 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003790 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003791 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003792 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003793
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003794 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003795 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3796 return -EINVAL;
3797 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003798
3799 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3800 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3801 return audio_idx[type][i];
3802
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003803 snd_printk(KERN_WARNING "Too many %s devices\n",
3804 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003805 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003806}
3807
3808/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003809 * attach a new PCM stream
3810 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003811static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003812{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003813 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003814 struct hda_pcm_stream *info;
3815 int stream, err;
3816
Takashi Iwaib91f0802008-11-04 08:43:08 +01003817 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003818 return -EINVAL;
3819 for (stream = 0; stream < 2; stream++) {
3820 info = &pcm->stream[stream];
3821 if (info->substreams) {
3822 err = set_pcm_default_values(codec, info);
3823 if (err < 0)
3824 return err;
3825 }
3826 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003827 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003828}
3829
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003830/* assign all PCMs of the given codec */
3831int snd_hda_codec_build_pcms(struct hda_codec *codec)
3832{
3833 unsigned int pcm;
3834 int err;
3835
3836 if (!codec->num_pcms) {
3837 if (!codec->patch_ops.build_pcms)
3838 return 0;
3839 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003840 if (err < 0) {
3841 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003842 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003843 err = snd_hda_codec_reset(codec);
3844 if (err < 0) {
3845 printk(KERN_ERR
3846 "hda_codec: cannot revert codec\n");
3847 return err;
3848 }
3849 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003850 }
3851 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3852 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3853 int dev;
3854
3855 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003856 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003857
3858 if (!cpcm->pcm) {
3859 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3860 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003861 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003862 cpcm->device = dev;
3863 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003864 if (err < 0) {
3865 printk(KERN_ERR "hda_codec: cannot attach "
3866 "PCM stream %d for codec #%d\n",
3867 dev, codec->addr);
3868 continue; /* no fatal error */
3869 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003870 }
3871 }
3872 return 0;
3873}
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875/**
3876 * snd_hda_build_pcms - build PCM information
3877 * @bus: the BUS
3878 *
3879 * Create PCM information for each codec included in the bus.
3880 *
3881 * The build_pcms codec patch is requested to set up codec->num_pcms and
3882 * codec->pcm_info properly. The array is referred by the top-level driver
3883 * to create its PCM instances.
3884 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3885 * callback.
3886 *
3887 * At least, substreams, channels_min and channels_max must be filled for
3888 * each stream. substreams = 0 indicates that the stream doesn't exist.
3889 * When rates and/or formats are zero, the supported values are queried
3890 * from the given nid. The nid is used also by the default ops.prepare
3891 * and ops.cleanup callbacks.
3892 *
3893 * The driver needs to call ops.open in its open callback. Similarly,
3894 * ops.close is supposed to be called in the close callback.
3895 * ops.prepare should be called in the prepare or hw_params callback
3896 * with the proper parameters for set up.
3897 * ops.cleanup should be called in hw_free for clean up of streams.
3898 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003899 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003901int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003903 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Takashi Iwai0ba21762007-04-16 11:29:14 +02003905 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003906 int err = snd_hda_codec_build_pcms(codec);
3907 if (err < 0)
3908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 }
3910 return 0;
3911}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003912EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914/**
3915 * snd_hda_check_board_config - compare the current codec with the config table
3916 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003917 * @num_configs: number of config enums
3918 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 * @tbl: configuration table, terminated by null entries
3920 *
3921 * Compares the modelname or PCI subsystem id of the current codec with the
3922 * given configuration table. If a matching entry is found, returns its
3923 * config value (supposed to be 0 or positive).
3924 *
3925 * If no entries are matching, the function returns a negative value.
3926 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003927int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003928 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003929 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003931 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003932 int i;
3933 for (i = 0; i < num_configs; i++) {
3934 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003935 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003936 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3937 "selected\n", models[i]);
3938 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
3940 }
3941 }
3942
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003943 if (!codec->bus->pci || !tbl)
3944 return -1;
3945
3946 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3947 if (!tbl)
3948 return -1;
3949 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003950#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003951 char tmp[10];
3952 const char *model = NULL;
3953 if (models)
3954 model = models[tbl->value];
3955 if (!model) {
3956 sprintf(tmp, "#%d", tbl->value);
3957 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003959 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3960 "for config %x:%x (%s)\n",
3961 model, tbl->subvendor, tbl->subdevice,
3962 (tbl->name ? tbl->name : "Unknown device"));
3963#endif
3964 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
3966 return -1;
3967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003968EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003971 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003972 subsystem ID with the
3973 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003974
3975 This is important for Gateway notebooks with SB450 HDA Audio
3976 where the vendor ID of the PCI device is:
3977 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3978 and the vendor/subvendor are found only at the codec.
3979
3980 * @codec: the HDA codec
3981 * @num_configs: number of config enums
3982 * @models: array of model name strings
3983 * @tbl: configuration table, terminated by null entries
3984 *
3985 * Compares the modelname or PCI subsystem id of the current codec with the
3986 * given configuration table. If a matching entry is found, returns its
3987 * config value (supposed to be 0 or positive).
3988 *
3989 * If no entries are matching, the function returns a negative value.
3990 */
3991int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003992 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003993 const struct snd_pci_quirk *tbl)
3994{
3995 const struct snd_pci_quirk *q;
3996
3997 /* Search for codec ID */
3998 for (q = tbl; q->subvendor; q++) {
3999 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
4000
4001 if (vendorid == codec->subsystem_id)
4002 break;
4003 }
4004
4005 if (!q->subvendor)
4006 return -1;
4007
4008 tbl = q;
4009
4010 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004011#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004012 char tmp[10];
4013 const char *model = NULL;
4014 if (models)
4015 model = models[tbl->value];
4016 if (!model) {
4017 sprintf(tmp, "#%d", tbl->value);
4018 model = tmp;
4019 }
4020 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4021 "for config %x:%x (%s)\n",
4022 model, tbl->subvendor, tbl->subdevice,
4023 (tbl->name ? tbl->name : "Unknown device"));
4024#endif
4025 return tbl->value;
4026 }
4027 return -1;
4028}
4029EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4030
4031/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 * snd_hda_add_new_ctls - create controls from the array
4033 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004034 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 *
4036 * This helper function creates and add new controls in the given array.
4037 * The array must be terminated with an empty entry as terminator.
4038 *
4039 * Returns 0 if successful, or a negative error code.
4040 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004041int snd_hda_add_new_ctls(struct hda_codec *codec,
4042 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004044 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004047 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004048 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004049 if (knew->iface == -1) /* skip this codec private value */
4050 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004051 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004052 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004053 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004054 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004055 if (addr > 0)
4056 kctl->id.device = addr;
4057 if (idx > 0)
4058 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004059 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004060 if (!err)
4061 break;
4062 /* try first with another device index corresponding to
4063 * the codec addr; if it still fails (or it's the
4064 * primary codec), then try another control index
4065 */
4066 if (!addr && codec->addr)
4067 addr = codec->addr;
4068 else if (!idx && !knew->index) {
4069 idx = find_empty_mixer_ctl_idx(codec,
4070 knew->name);
4071 if (idx <= 0)
4072 return err;
4073 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004074 return err;
4075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
4077 return 0;
4078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004079EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Takashi Iwaicb53c622007-08-10 17:21:45 +02004081#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004082static void hda_power_work(struct work_struct *work)
4083{
4084 struct hda_codec *codec =
4085 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004086 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004087
Maxim Levitsky2e492462007-09-03 15:26:57 +02004088 if (!codec->power_on || codec->power_count) {
4089 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004090 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004091 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004092
Takashi Iwaid66fee52011-08-02 15:39:31 +02004093 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004094 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004095 if (bus->ops.pm_notify)
4096 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004097}
4098
4099static void hda_keep_power_on(struct hda_codec *codec)
4100{
4101 codec->power_count++;
4102 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004103 codec->power_jiffies = jiffies;
4104}
4105
Takashi Iwaid5191e52009-11-16 14:58:17 +01004106/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004107void snd_hda_update_power_acct(struct hda_codec *codec)
4108{
4109 unsigned long delta = jiffies - codec->power_jiffies;
4110 if (codec->power_on)
4111 codec->power_on_acct += delta;
4112 else
4113 codec->power_off_acct += delta;
4114 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004115}
4116
Takashi Iwaid5191e52009-11-16 14:58:17 +01004117/**
4118 * snd_hda_power_up - Power-up the codec
4119 * @codec: HD-audio codec
4120 *
4121 * Increment the power-up counter and power up the hardware really when
4122 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004123 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004124void snd_hda_power_up(struct hda_codec *codec)
4125{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004126 struct hda_bus *bus = codec->bus;
4127
Takashi Iwaicb53c622007-08-10 17:21:45 +02004128 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004129 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004130 return;
4131
Takashi Iwaid66fee52011-08-02 15:39:31 +02004132 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004133 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004134 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004135 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004136 if (bus->ops.pm_notify)
4137 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004138 hda_call_codec_resume(codec);
4139 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004140 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004142EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004143
4144#define power_save(codec) \
4145 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004146
Takashi Iwaid5191e52009-11-16 14:58:17 +01004147/**
4148 * snd_hda_power_down - Power-down the codec
4149 * @codec: HD-audio codec
4150 *
4151 * Decrement the power-up counter and schedules the power-off work if
4152 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004153 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004154void snd_hda_power_down(struct hda_codec *codec)
4155{
4156 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004157 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004158 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004159 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004160 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004161 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004162 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004163 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004165EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004166
Takashi Iwaid5191e52009-11-16 14:58:17 +01004167/**
4168 * snd_hda_check_amp_list_power - Check the amp list and update the power
4169 * @codec: HD-audio codec
4170 * @check: the object containing an AMP list and the status
4171 * @nid: NID to check / update
4172 *
4173 * Check whether the given NID is in the amp list. If it's in the list,
4174 * check the current AMP status, and update the the power-status according
4175 * to the mute status.
4176 *
4177 * This function is supposed to be set or called from the check_power_status
4178 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004179 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004180int snd_hda_check_amp_list_power(struct hda_codec *codec,
4181 struct hda_loopback_check *check,
4182 hda_nid_t nid)
4183{
Takashi Iwai031024e2011-05-02 11:29:30 +02004184 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004185 int ch, v;
4186
4187 if (!check->amplist)
4188 return 0;
4189 for (p = check->amplist; p->nid; p++) {
4190 if (p->nid == nid)
4191 break;
4192 }
4193 if (!p->nid)
4194 return 0; /* nothing changed */
4195
4196 for (p = check->amplist; p->nid; p++) {
4197 for (ch = 0; ch < 2; ch++) {
4198 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4199 p->idx);
4200 if (!(v & HDA_AMP_MUTE) && v > 0) {
4201 if (!check->power_on) {
4202 check->power_on = 1;
4203 snd_hda_power_up(codec);
4204 }
4205 return 1;
4206 }
4207 }
4208 }
4209 if (check->power_on) {
4210 check->power_on = 0;
4211 snd_hda_power_down(codec);
4212 }
4213 return 0;
4214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004215EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004218/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004219 * Channel mode helper
4220 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004221
4222/**
4223 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4224 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004225int snd_hda_ch_mode_info(struct hda_codec *codec,
4226 struct snd_ctl_elem_info *uinfo,
4227 const struct hda_channel_mode *chmode,
4228 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004229{
4230 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4231 uinfo->count = 1;
4232 uinfo->value.enumerated.items = num_chmodes;
4233 if (uinfo->value.enumerated.item >= num_chmodes)
4234 uinfo->value.enumerated.item = num_chmodes - 1;
4235 sprintf(uinfo->value.enumerated.name, "%dch",
4236 chmode[uinfo->value.enumerated.item].channels);
4237 return 0;
4238}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004239EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004240
Takashi Iwaid5191e52009-11-16 14:58:17 +01004241/**
4242 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4243 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004244int snd_hda_ch_mode_get(struct hda_codec *codec,
4245 struct snd_ctl_elem_value *ucontrol,
4246 const struct hda_channel_mode *chmode,
4247 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004248 int max_channels)
4249{
4250 int i;
4251
4252 for (i = 0; i < num_chmodes; i++) {
4253 if (max_channels == chmode[i].channels) {
4254 ucontrol->value.enumerated.item[0] = i;
4255 break;
4256 }
4257 }
4258 return 0;
4259}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004260EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004261
Takashi Iwaid5191e52009-11-16 14:58:17 +01004262/**
4263 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4264 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004265int snd_hda_ch_mode_put(struct hda_codec *codec,
4266 struct snd_ctl_elem_value *ucontrol,
4267 const struct hda_channel_mode *chmode,
4268 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004269 int *max_channelsp)
4270{
4271 unsigned int mode;
4272
4273 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004274 if (mode >= num_chmodes)
4275 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004276 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004277 return 0;
4278 /* change the current channel setting */
4279 *max_channelsp = chmode[mode].channels;
4280 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004281 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004282 return 1;
4283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004284EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/*
4287 * input MUX helper
4288 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004289
4290/**
4291 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4292 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004293int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4294 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
4296 unsigned int index;
4297
4298 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4299 uinfo->count = 1;
4300 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004301 if (!imux->num_items)
4302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 index = uinfo->value.enumerated.item;
4304 if (index >= imux->num_items)
4305 index = imux->num_items - 1;
4306 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4307 return 0;
4308}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004309EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Takashi Iwaid5191e52009-11-16 14:58:17 +01004311/**
4312 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4313 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004314int snd_hda_input_mux_put(struct hda_codec *codec,
4315 const struct hda_input_mux *imux,
4316 struct snd_ctl_elem_value *ucontrol,
4317 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 unsigned int *cur_val)
4319{
4320 unsigned int idx;
4321
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004322 if (!imux->num_items)
4323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 idx = ucontrol->value.enumerated.item[0];
4325 if (idx >= imux->num_items)
4326 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004327 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004329 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4330 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 *cur_val = idx;
4332 return 1;
4333}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004334EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336
4337/*
4338 * Multi-channel / digital-out PCM helper functions
4339 */
4340
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004341/* setup SPDIF output stream */
4342static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4343 unsigned int stream_tag, unsigned int format)
4344{
Stephen Warren7c9359762011-06-01 11:14:17 -06004345 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4346
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004347 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004348 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004349 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004350 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004351 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004352 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004353 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004354 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004355 for (d = codec->slave_dig_outs; *d; d++)
4356 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4357 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004358 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004359 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004360 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004361 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004362 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004363}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004364
Takashi Iwai2f728532008-09-25 16:32:41 +02004365static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4366{
4367 snd_hda_codec_cleanup_stream(codec, nid);
4368 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004369 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004370 for (d = codec->slave_dig_outs; *d; d++)
4371 snd_hda_codec_cleanup_stream(codec, *d);
4372 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004373}
4374
Takashi Iwaid5191e52009-11-16 14:58:17 +01004375/**
4376 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4377 * @bus: HD-audio bus
4378 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004379void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4380{
4381 struct hda_codec *codec;
4382
4383 if (!bus)
4384 return;
4385 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004386 if (hda_codec_is_power_on(codec) &&
4387 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004388 codec->patch_ops.reboot_notify(codec);
4389 }
4390}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004391EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004392
Takashi Iwaid5191e52009-11-16 14:58:17 +01004393/**
4394 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004396int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4397 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Ingo Molnar62932df2006-01-16 16:34:20 +01004399 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004400 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4401 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004402 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004404 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return 0;
4406}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004407EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Takashi Iwaid5191e52009-11-16 14:58:17 +01004409/**
4410 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4411 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004412int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4413 struct hda_multi_out *mout,
4414 unsigned int stream_tag,
4415 unsigned int format,
4416 struct snd_pcm_substream *substream)
4417{
4418 mutex_lock(&codec->spdif_mutex);
4419 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4420 mutex_unlock(&codec->spdif_mutex);
4421 return 0;
4422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004423EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004424
Takashi Iwaid5191e52009-11-16 14:58:17 +01004425/**
4426 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4427 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004428int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4429 struct hda_multi_out *mout)
4430{
4431 mutex_lock(&codec->spdif_mutex);
4432 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4433 mutex_unlock(&codec->spdif_mutex);
4434 return 0;
4435}
4436EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4437
Takashi Iwaid5191e52009-11-16 14:58:17 +01004438/**
4439 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004441int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4442 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Ingo Molnar62932df2006-01-16 16:34:20 +01004444 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004446 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return 0;
4448}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004449EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Takashi Iwaid5191e52009-11-16 14:58:17 +01004451/**
4452 * snd_hda_multi_out_analog_open - open analog outputs
4453 *
4454 * Open analog outputs and set up the hw-constraints.
4455 * If the digital outputs can be opened as slave, open the digital
4456 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004458int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4459 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004460 struct snd_pcm_substream *substream,
4461 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
Takashi Iwai9a081602008-02-12 18:37:26 +01004463 struct snd_pcm_runtime *runtime = substream->runtime;
4464 runtime->hw.channels_max = mout->max_channels;
4465 if (mout->dig_out_nid) {
4466 if (!mout->analog_rates) {
4467 mout->analog_rates = hinfo->rates;
4468 mout->analog_formats = hinfo->formats;
4469 mout->analog_maxbps = hinfo->maxbps;
4470 } else {
4471 runtime->hw.rates = mout->analog_rates;
4472 runtime->hw.formats = mout->analog_formats;
4473 hinfo->maxbps = mout->analog_maxbps;
4474 }
4475 if (!mout->spdif_rates) {
4476 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4477 &mout->spdif_rates,
4478 &mout->spdif_formats,
4479 &mout->spdif_maxbps);
4480 }
4481 mutex_lock(&codec->spdif_mutex);
4482 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004483 if ((runtime->hw.rates & mout->spdif_rates) &&
4484 (runtime->hw.formats & mout->spdif_formats)) {
4485 runtime->hw.rates &= mout->spdif_rates;
4486 runtime->hw.formats &= mout->spdif_formats;
4487 if (mout->spdif_maxbps < hinfo->maxbps)
4488 hinfo->maxbps = mout->spdif_maxbps;
4489 } else {
4490 mout->share_spdif = 0;
4491 /* FIXME: need notify? */
4492 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004493 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004494 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4497 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4498}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004499EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Takashi Iwaid5191e52009-11-16 14:58:17 +01004501/**
4502 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4503 *
4504 * Set up the i/o for analog out.
4505 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004507int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4508 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 unsigned int stream_tag,
4510 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004511 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Takashi Iwaidda14412011-05-02 11:29:30 +02004513 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004515 struct hda_spdif_out *spdif =
4516 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 int i;
4518
Ingo Molnar62932df2006-01-16 16:34:20 +01004519 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004520 if (mout->dig_out_nid && mout->share_spdif &&
4521 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004523 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4524 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004525 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004527 setup_dig_out_stream(codec, mout->dig_out_nid,
4528 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 } else {
4530 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004531 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004534 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004537 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4538 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004539 if (!mout->no_share_stream &&
4540 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004542 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4543 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004544 /* extra outputs copied from front */
4545 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004546 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004547 snd_hda_codec_setup_stream(codec,
4548 mout->extra_out_nid[i],
4549 stream_tag, 0, format);
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 /* surrounds */
4552 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004553 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004554 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4555 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004556 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004557 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4558 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 }
4560 return 0;
4561}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004562EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Takashi Iwaid5191e52009-11-16 14:58:17 +01004564/**
4565 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004567int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4568 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Takashi Iwaidda14412011-05-02 11:29:30 +02004570 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 int i;
4572
4573 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004574 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004576 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004577 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4578 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004579 snd_hda_codec_cleanup_stream(codec,
4580 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004581 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004583 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 mout->dig_out_used = 0;
4585 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004586 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return 0;
4588}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004589EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004591/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004592 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004593 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004594
Takashi Iwaidda14412011-05-02 11:29:30 +02004595static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004596{
4597 for (; *list; list++)
4598 if (*list == nid)
4599 return 1;
4600 return 0;
4601}
4602
Steve Longerbeam81937d32007-05-08 15:33:03 +02004603
4604/*
4605 * Sort an associated group of pins according to their sequence numbers.
4606 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004607static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004608 int num_pins)
4609{
4610 int i, j;
4611 short seq;
4612 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004613
Steve Longerbeam81937d32007-05-08 15:33:03 +02004614 for (i = 0; i < num_pins; i++) {
4615 for (j = i + 1; j < num_pins; j++) {
4616 if (sequences[i] > sequences[j]) {
4617 seq = sequences[i];
4618 sequences[i] = sequences[j];
4619 sequences[j] = seq;
4620 nid = pins[i];
4621 pins[i] = pins[j];
4622 pins[j] = nid;
4623 }
4624 }
4625 }
4626}
4627
4628
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004629/* add the found input-pin to the cfg->inputs[] table */
4630static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4631 int type)
4632{
4633 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4634 cfg->inputs[cfg->num_inputs].pin = nid;
4635 cfg->inputs[cfg->num_inputs].type = type;
4636 cfg->num_inputs++;
4637 }
4638}
4639
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004640/* sort inputs in the order of AUTO_PIN_* type */
4641static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4642{
4643 int i, j;
4644
4645 for (i = 0; i < cfg->num_inputs; i++) {
4646 for (j = i + 1; j < cfg->num_inputs; j++) {
4647 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4648 struct auto_pin_cfg_item tmp;
4649 tmp = cfg->inputs[i];
4650 cfg->inputs[i] = cfg->inputs[j];
4651 cfg->inputs[j] = tmp;
4652 }
4653 }
4654 }
4655}
4656
Takashi Iwai82bc9552006-03-21 11:24:42 +01004657/*
4658 * Parse all pin widgets and store the useful pin nids to cfg
4659 *
4660 * The number of line-outs or any primary output is stored in line_outs,
4661 * and the corresponding output pins are assigned to line_out_pins[],
4662 * in the order of front, rear, CLFE, side, ...
4663 *
4664 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004665 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004666 * is detected, one of speaker of HP pins is assigned as the primary
4667 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4668 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004669 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004670 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004671 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4672 * respectively.
4673 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004674int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4675 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004676 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004677{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004678 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004679 short seq, assoc_line_out, assoc_speaker;
4680 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4681 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004682 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004683 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004684
4685 memset(cfg, 0, sizeof(*cfg));
4686
Steve Longerbeam81937d32007-05-08 15:33:03 +02004687 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4688 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004689 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004690 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004691
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004692 end_nid = codec->start_nid + codec->num_nodes;
4693 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004694 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004695 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004696 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004697 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004698
4699 /* read all default configuration for pin complex */
4700 if (wid_type != AC_WID_PIN)
4701 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004702 /* ignore the given nids (e.g. pc-beep returns error) */
4703 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4704 continue;
4705
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004706 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004707 conn = get_defcfg_connect(def_conf);
4708 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004709 continue;
4710 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004711 dev = get_defcfg_device(def_conf);
4712
4713 /* workaround for buggy BIOS setups */
4714 if (dev == AC_JACK_LINE_OUT) {
4715 if (conn == AC_JACK_PORT_FIXED)
4716 dev = AC_JACK_SPEAKER;
4717 }
4718
4719 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004720 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004721 seq = get_defcfg_sequence(def_conf);
4722 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004723
4724 if (!(wid_caps & AC_WCAP_STEREO))
4725 if (!cfg->mono_out_pin)
4726 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004727 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004728 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004729 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004730 assoc_line_out = assoc;
4731 else if (assoc_line_out != assoc)
4732 continue;
4733 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4734 continue;
4735 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004736 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004737 cfg->line_outs++;
4738 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004739 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004740 seq = get_defcfg_sequence(def_conf);
4741 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004742 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004743 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004744 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004745 assoc_speaker = assoc;
4746 else if (assoc_speaker != assoc)
4747 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004748 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4749 continue;
4750 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004751 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004752 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004753 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004754 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004755 seq = get_defcfg_sequence(def_conf);
4756 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004757 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4758 continue;
4759 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004760 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004761 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004762 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004763 case AC_JACK_MIC_IN:
4764 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004765 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004766 case AC_JACK_LINE_IN:
4767 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004768 break;
4769 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004770 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004771 break;
4772 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004773 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004774 break;
4775 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004776 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004777 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4778 continue;
4779 cfg->dig_out_pins[cfg->dig_outs] = nid;
4780 cfg->dig_out_type[cfg->dig_outs] =
4781 (loc == AC_JACK_LOC_HDMI) ?
4782 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4783 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004784 break;
4785 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004786 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004787 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004788 if (loc == AC_JACK_LOC_HDMI)
4789 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4790 else
4791 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004792 break;
4793 }
4794 }
4795
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004796 /* FIX-UP:
4797 * If no line-out is defined but multiple HPs are found,
4798 * some of them might be the real line-outs.
4799 */
4800 if (!cfg->line_outs && cfg->hp_outs > 1) {
4801 int i = 0;
4802 while (i < cfg->hp_outs) {
4803 /* The real HPs should have the sequence 0x0f */
4804 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4805 i++;
4806 continue;
4807 }
4808 /* Move it to the line-out table */
4809 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4810 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4811 cfg->line_outs++;
4812 cfg->hp_outs--;
4813 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4814 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004815 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004816 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4817 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004818 memset(cfg->hp_pins + cfg->hp_outs, 0,
4819 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004820 if (!cfg->hp_outs)
4821 cfg->line_out_type = AUTO_PIN_HP_OUT;
4822
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004823 }
4824
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004825 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004826 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4827 cfg->line_outs);
4828 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4829 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004830 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4831 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004832
Steve Longerbeam81937d32007-05-08 15:33:03 +02004833 /*
4834 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4835 * as a primary output
4836 */
4837 if (!cfg->line_outs) {
4838 if (cfg->speaker_outs) {
4839 cfg->line_outs = cfg->speaker_outs;
4840 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4841 sizeof(cfg->speaker_pins));
4842 cfg->speaker_outs = 0;
4843 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4844 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4845 } else if (cfg->hp_outs) {
4846 cfg->line_outs = cfg->hp_outs;
4847 memcpy(cfg->line_out_pins, cfg->hp_pins,
4848 sizeof(cfg->hp_pins));
4849 cfg->hp_outs = 0;
4850 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4851 cfg->line_out_type = AUTO_PIN_HP_OUT;
4852 }
4853 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004854
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004855 /* Reorder the surround channels
4856 * ALSA sequence is front/surr/clfe/side
4857 * HDA sequence is:
4858 * 4-ch: front/surr => OK as it is
4859 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004860 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004861 */
4862 switch (cfg->line_outs) {
4863 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004864 case 4:
4865 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004866 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004867 cfg->line_out_pins[2] = nid;
4868 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004869 }
4870
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004871 sort_autocfg_input_pins(cfg);
4872
Takashi Iwai82bc9552006-03-21 11:24:42 +01004873 /*
4874 * debug prints of the parsed results
4875 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004876 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai82bc9552006-03-21 11:24:42 +01004877 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4878 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004879 cfg->line_out_pins[4],
4880 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4881 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4882 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004883 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4884 cfg->speaker_outs, cfg->speaker_pins[0],
4885 cfg->speaker_pins[1], cfg->speaker_pins[2],
4886 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004887 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4888 cfg->hp_outs, cfg->hp_pins[0],
4889 cfg->hp_pins[1], cfg->hp_pins[2],
4890 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004891 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004892 if (cfg->dig_outs)
4893 snd_printd(" dig-out=0x%x/0x%x\n",
4894 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004895 snd_printd(" inputs:");
4896 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004897 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004898 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004899 cfg->inputs[i].pin);
4900 }
4901 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004902 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004903 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004904
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004905 return 0;
4906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004907EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004908
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004909int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004910{
4911 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004912 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004913 if (conn == AC_JACK_PORT_NONE)
4914 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004915 /* Windows may claim the internal mic to be BOTH, too */
4916 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004917 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004918 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004919 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004920 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004921 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004922 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004923 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004924 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004925 return INPUT_PIN_ATTR_FRONT;
4926 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004927}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004928EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004929
Takashi Iwai990061c2010-09-09 22:08:44 +02004930/**
4931 * hda_get_input_pin_label - Give a label for the given input pin
4932 *
4933 * When check_location is true, the function checks the pin location
4934 * for mic and line-in pins, and set an appropriate prefix like "Front",
4935 * "Rear", "Internal".
4936 */
4937
Takashi Iwai10a20af2010-09-09 16:28:02 +02004938const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4939 int check_location)
4940{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004941 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004942 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004943 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4944 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004945 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004946
Takashi Iwai10a20af2010-09-09 16:28:02 +02004947 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004948
4949 switch (get_defcfg_device(def_conf)) {
4950 case AC_JACK_MIC_IN:
4951 if (!check_location)
4952 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004953 attr = snd_hda_get_input_pin_attr(def_conf);
4954 if (!attr)
4955 return "None";
4956 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004957 case AC_JACK_LINE_IN:
4958 if (!check_location)
4959 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004960 attr = snd_hda_get_input_pin_attr(def_conf);
4961 if (!attr)
4962 return "None";
4963 if (attr == INPUT_PIN_ATTR_DOCK)
4964 return "Dock Line";
4965 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004966 case AC_JACK_AUX:
4967 return "Aux";
4968 case AC_JACK_CD:
4969 return "CD";
4970 case AC_JACK_SPDIF_IN:
4971 return "SPDIF In";
4972 case AC_JACK_DIG_OTHER_IN:
4973 return "Digital In";
4974 default:
4975 return "Misc";
4976 }
4977}
4978EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4979
Takashi Iwaia1c98512010-09-09 21:36:27 +02004980/* Check whether the location prefix needs to be added to the label.
4981 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4982 * have to put "Front" prefix to each label. In such a case, returns false.
4983 */
4984static int check_mic_location_need(struct hda_codec *codec,
4985 const struct auto_pin_cfg *cfg,
4986 int input)
4987{
4988 unsigned int defc;
4989 int i, attr, attr2;
4990
4991 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004992 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004993 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004994 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004995 return 1;
4996
4997 attr = 0;
4998 for (i = 0; i < cfg->num_inputs; i++) {
4999 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005000 attr2 = snd_hda_get_input_pin_attr(defc);
5001 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005002 if (attr && attr != attr2)
5003 return 1; /* different locations found */
5004 attr = attr2;
5005 }
5006 }
5007 return 0;
5008}
5009
Takashi Iwai990061c2010-09-09 22:08:44 +02005010/**
5011 * hda_get_autocfg_input_label - Get a label for the given input
5012 *
5013 * Get a label for the given input pin defined by the autocfg item.
5014 * Unlike hda_get_input_pin_label(), this function checks all inputs
5015 * defined in autocfg and avoids the redundant mic/line prefix as much as
5016 * possible.
5017 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005018const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5019 const struct auto_pin_cfg *cfg,
5020 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005021{
5022 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005023 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005024
Takashi Iwai10a20af2010-09-09 16:28:02 +02005025 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5026 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5027 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005028 if (has_multiple_pins && type == AUTO_PIN_MIC)
5029 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005030 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5031 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005032}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005033EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5034
Takashi Iwai990061c2010-09-09 22:08:44 +02005035/**
5036 * snd_hda_add_imux_item - Add an item to input_mux
5037 *
5038 * When the same label is used already in the existing items, the number
5039 * suffix is appended to the label. This label index number is stored
5040 * to type_idx when non-NULL pointer is given.
5041 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005042int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5043 int index, int *type_idx)
5044{
5045 int i, label_idx = 0;
5046 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5047 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5048 return -EINVAL;
5049 }
5050 for (i = 0; i < imux->num_items; i++) {
5051 if (!strncmp(label, imux->items[i].label, strlen(label)))
5052 label_idx++;
5053 }
5054 if (type_idx)
5055 *type_idx = label_idx;
5056 if (label_idx > 0)
5057 snprintf(imux->items[imux->num_items].label,
5058 sizeof(imux->items[imux->num_items].label),
5059 "%s %d", label, label_idx);
5060 else
5061 strlcpy(imux->items[imux->num_items].label, label,
5062 sizeof(imux->items[imux->num_items].label));
5063 imux->items[imux->num_items].index = index;
5064 imux->num_items++;
5065 return 0;
5066}
5067EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005068
Takashi Iwai4a471b72005-12-07 13:56:29 +01005069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070#ifdef CONFIG_PM
5071/*
5072 * power management
5073 */
5074
5075/**
5076 * snd_hda_suspend - suspend the codecs
5077 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 *
5079 * Returns 0 if successful.
5080 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005081int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005083 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Takashi Iwai0ba21762007-04-16 11:29:14 +02005085 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005086 if (hda_codec_is_power_on(codec))
5087 hda_call_codec_suspend(codec);
5088 if (codec->patch_ops.post_suspend)
5089 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
5091 return 0;
5092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005093EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
5095/**
5096 * snd_hda_resume - resume the codecs
5097 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 *
5099 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005100 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005101 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005102 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 */
5104int snd_hda_resume(struct hda_bus *bus)
5105{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005106 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Takashi Iwai0ba21762007-04-16 11:29:14 +02005108 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005109 if (codec->patch_ops.pre_resume)
5110 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005111 if (snd_hda_codec_needs_resume(codec))
5112 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 return 0;
5115}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005116EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005117#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005118
5119/*
5120 * generic arrays
5121 */
5122
Takashi Iwaid5191e52009-11-16 14:58:17 +01005123/**
5124 * snd_array_new - get a new element from the given array
5125 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005126 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005127 * Get a new element from the given array. If it exceeds the
5128 * pre-allocated array size, re-allocate the array.
5129 *
5130 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005131 */
5132void *snd_array_new(struct snd_array *array)
5133{
5134 if (array->used >= array->alloced) {
5135 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005136 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005137 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005138 void *nlist;
5139 if (snd_BUG_ON(num >= 4096))
5140 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005141 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005142 if (!nlist)
5143 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005144 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005145 array->list = nlist;
5146 array->alloced = num;
5147 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005148 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005150EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005151
Takashi Iwaid5191e52009-11-16 14:58:17 +01005152/**
5153 * snd_array_free - free the given array elements
5154 * @array: the array object
5155 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005156void snd_array_free(struct snd_array *array)
5157{
5158 kfree(array->list);
5159 array->used = 0;
5160 array->alloced = 0;
5161 array->list = NULL;
5162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005163EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005164
Takashi Iwaid5191e52009-11-16 14:58:17 +01005165/**
5166 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5167 * @pcm: PCM caps bits
5168 * @buf: the string buffer to write
5169 * @buflen: the max buffer length
5170 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005171 * used by hda_proc.c and hda_eld.c
5172 */
5173void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5174{
5175 static unsigned int rates[] = {
5176 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5177 96000, 176400, 192000, 384000
5178 };
5179 int i, j;
5180
5181 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5182 if (pcm & (1 << i))
5183 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5184
5185 buf[j] = '\0'; /* necessary when j == 0 */
5186}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005187EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005188
Takashi Iwaid5191e52009-11-16 14:58:17 +01005189/**
5190 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5191 * @pcm: PCM caps bits
5192 * @buf: the string buffer to write
5193 * @buflen: the max buffer length
5194 *
5195 * used by hda_proc.c and hda_eld.c
5196 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005197void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5198{
5199 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5200 int i, j;
5201
5202 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5203 if (pcm & (AC_SUPPCM_BITS_8 << i))
5204 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5205
5206 buf[j] = '\0'; /* necessary when j == 0 */
5207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005208EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005209
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005210#ifdef CONFIG_SND_HDA_INPUT_JACK
5211/*
5212 * Input-jack notification support
5213 */
5214struct hda_jack_item {
5215 hda_nid_t nid;
5216 int type;
5217 struct snd_jack *jack;
5218};
5219
5220static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5221 int type)
5222{
5223 switch (type) {
5224 case SND_JACK_HEADPHONE:
5225 return "Headphone";
5226 case SND_JACK_MICROPHONE:
5227 return "Mic";
5228 case SND_JACK_LINEOUT:
5229 return "Line-out";
5230 case SND_JACK_HEADSET:
5231 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005232 case SND_JACK_VIDEOOUT:
5233 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005234 default:
5235 return "Misc";
5236 }
5237}
5238
5239static void hda_free_jack_priv(struct snd_jack *jack)
5240{
5241 struct hda_jack_item *jacks = jack->private_data;
5242 jacks->nid = 0;
5243 jacks->jack = NULL;
5244}
5245
5246int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5247 const char *name)
5248{
5249 struct hda_jack_item *jack;
5250 int err;
5251
5252 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5253 jack = snd_array_new(&codec->jacks);
5254 if (!jack)
5255 return -ENOMEM;
5256
5257 jack->nid = nid;
5258 jack->type = type;
5259 if (!name)
5260 name = get_jack_default_name(codec, nid, type);
5261 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5262 if (err < 0) {
5263 jack->nid = 0;
5264 return err;
5265 }
5266 jack->jack->private_data = jack;
5267 jack->jack->private_free = hda_free_jack_priv;
5268 return 0;
5269}
5270EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5271
5272void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5273{
5274 struct hda_jack_item *jacks = codec->jacks.list;
5275 int i;
5276
5277 if (!jacks)
5278 return;
5279
5280 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5281 unsigned int pin_ctl;
5282 unsigned int present;
5283 int type;
5284
5285 if (jacks->nid != nid)
5286 continue;
5287 present = snd_hda_jack_detect(codec, nid);
5288 type = jacks->type;
5289 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5290 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5291 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5292 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5293 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5294 }
5295 snd_jack_report(jacks->jack, present ? type : 0);
5296 }
5297}
5298EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5299
5300/* free jack instances manually when clearing/reconfiguring */
5301void snd_hda_input_jack_free(struct hda_codec *codec)
5302{
5303 if (!codec->bus->shutdown && codec->jacks.list) {
5304 struct hda_jack_item *jacks = codec->jacks.list;
5305 int i;
5306 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5307 if (jacks->jack)
5308 snd_device_free(codec->bus->card, jacks->jack);
5309 }
5310 }
5311 snd_array_free(&codec->jacks);
5312}
5313EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5314#endif /* CONFIG_SND_HDA_INPUT_JACK */
5315
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005316MODULE_DESCRIPTION("HDA codec core");
5317MODULE_LICENSE("GPL");