blob: 6b611d50d03fb7a9a5fa4610a69e10d890dfd5ee [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++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200589 for (i = 0; i < nums; i++) {
590 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
591 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
592 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200593 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
594 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200595 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 return -1;
597}
598EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600/**
601 * snd_hda_queue_unsol_event - add an unsolicited event to queue
602 * @bus: the BUS
603 * @res: unsolicited event (lower 32bit of RIRB entry)
604 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
605 *
606 * Adds the given event to the queue. The events are processed in
607 * the workqueue asynchronously. Call this function in the interrupt
608 * hanlder when RIRB receives an unsolicited event.
609 *
610 * Returns 0 if successful, or a negative error code.
611 */
612int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
613{
614 struct hda_bus_unsolicited *unsol;
615 unsigned int wp;
616
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200617 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200618 unsol = bus->unsol;
619 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 0;
621
622 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
623 unsol->wp = wp;
624
625 wp <<= 1;
626 unsol->queue[wp] = res;
627 unsol->queue[wp + 1] = res_ex;
628
Takashi Iwai6acaed32009-01-12 10:09:24 +0100629 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 return 0;
632}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100633EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800636 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
David Howellsc4028952006-11-22 14:57:56 +0000638static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
David Howellsc4028952006-11-22 14:57:56 +0000640 struct hda_bus_unsolicited *unsol =
641 container_of(work, struct hda_bus_unsolicited, work);
642 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct hda_codec *codec;
644 unsigned int rp, caddr, res;
645
646 while (unsol->rp != unsol->wp) {
647 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
648 unsol->rp = rp;
649 rp <<= 1;
650 res = unsol->queue[rp];
651 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200652 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 codec = bus->caddr_tbl[caddr & 0x0f];
655 if (codec && codec->patch_ops.unsol_event)
656 codec->patch_ops.unsol_event(codec, res);
657 }
658}
659
660/*
661 * initialize unsolicited queue
662 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200663static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct hda_bus_unsolicited *unsol;
666
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100667 if (bus->unsol) /* already initialized */
668 return 0;
669
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200670 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200671 if (!unsol) {
672 snd_printk(KERN_ERR "hda_codec: "
673 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -ENOMEM;
675 }
David Howellsc4028952006-11-22 14:57:56 +0000676 INIT_WORK(&unsol->work, process_unsol_events);
677 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 bus->unsol = unsol;
679 return 0;
680}
681
682/*
683 * destructor
684 */
685static void snd_hda_codec_free(struct hda_codec *codec);
686
687static int snd_hda_bus_free(struct hda_bus *bus)
688{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200689 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Takashi Iwai0ba21762007-04-16 11:29:14 +0200691 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100693 if (bus->workq)
694 flush_workqueue(bus->workq);
695 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200697 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 snd_hda_codec_free(codec);
699 }
700 if (bus->ops.private_free)
701 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100702 if (bus->workq)
703 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 kfree(bus);
705 return 0;
706}
707
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100708static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100711 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return snd_hda_bus_free(bus);
713}
714
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200715#ifdef CONFIG_SND_HDA_HWDEP
716static int snd_hda_bus_dev_register(struct snd_device *device)
717{
718 struct hda_bus *bus = device->device_data;
719 struct hda_codec *codec;
720 list_for_each_entry(codec, &bus->codec_list, list) {
721 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100722 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200723 }
724 return 0;
725}
726#else
727#define snd_hda_bus_dev_register NULL
728#endif
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/**
731 * snd_hda_bus_new - create a HDA bus
732 * @card: the card entry
733 * @temp: the template for hda_bus information
734 * @busp: the pointer to store the created bus instance
735 *
736 * Returns 0 if successful, or a negative error code.
737 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100738int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200739 const struct hda_bus_template *temp,
740 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct hda_bus *bus;
743 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100744 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200745 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 .dev_free = snd_hda_bus_dev_free,
747 };
748
Takashi Iwaida3cec32008-08-08 17:12:14 +0200749 if (snd_BUG_ON(!temp))
750 return -EINVAL;
751 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (busp)
755 *busp = NULL;
756
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200757 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (bus == NULL) {
759 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
760 return -ENOMEM;
761 }
762
763 bus->card = card;
764 bus->private_data = temp->private_data;
765 bus->pci = temp->pci;
766 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100767 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 bus->ops = temp->ops;
769
Ingo Molnar62932df2006-01-16 16:34:20 +0100770 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200771 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 INIT_LIST_HEAD(&bus->codec_list);
773
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100774 snprintf(bus->workq_name, sizeof(bus->workq_name),
775 "hd-audio%d", card->number);
776 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100777 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100778 snd_printk(KERN_ERR "cannot create workqueue %s\n",
779 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 kfree(bus);
781 return -ENOMEM;
782 }
783
Takashi Iwai0ba21762007-04-16 11:29:14 +0200784 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
785 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 snd_hda_bus_free(bus);
787 return err;
788 }
789 if (busp)
790 *busp = bus;
791 return 0;
792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100793EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Takashi Iwai82467612007-07-27 19:15:54 +0200795#ifdef CONFIG_SND_HDA_GENERIC
796#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200797 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200798#else
799#define is_generic_config(codec) 0
800#endif
801
Takashi Iwai645f10c2008-11-28 15:07:37 +0100802#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100803#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
804#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#endif
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * find a matching codec preset
810 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200811static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200812find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100814 struct hda_codec_preset_list *tbl;
815 const struct hda_codec_preset *preset;
816 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Takashi Iwai82467612007-07-27 19:15:54 +0200818 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100819 return NULL; /* use the generic parser */
820
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100821 again:
822 mutex_lock(&preset_mutex);
823 list_for_each_entry(tbl, &hda_preset_tables, list) {
824 if (!try_module_get(tbl->owner)) {
825 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
826 continue;
827 }
828 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100830 if (preset->afg && preset->afg != codec->afg)
831 continue;
832 if (preset->mfg && preset->mfg != codec->mfg)
833 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200834 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200836 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100838 preset->rev == codec->revision_id)) {
839 mutex_unlock(&preset_mutex);
840 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100844 module_put(tbl->owner);
845 }
846 mutex_unlock(&preset_mutex);
847
848 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
849 char name[32];
850 if (!mod_requested)
851 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
852 codec->vendor_id);
853 else
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
855 (codec->vendor_id >> 16) & 0xffff);
856 request_module(name);
857 mod_requested++;
858 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 return NULL;
861}
862
863/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200864 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200866static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 const struct hda_vendor_id *c;
869 const char *vendor = NULL;
870 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200871 char tmp[16];
872
873 if (codec->vendor_name)
874 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 for (c = hda_vendor_ids; c->id; c++) {
877 if (c->id == vendor_id) {
878 vendor = c->name;
879 break;
880 }
881 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200882 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sprintf(tmp, "Generic %04x", vendor_id);
884 vendor = tmp;
885 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200886 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
887 if (!codec->vendor_name)
888 return -ENOMEM;
889
890 get_chip_name:
891 if (codec->chip_name)
892 return 0;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200895 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
896 else {
897 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
898 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
899 }
900 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200901 return -ENOMEM;
902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200906 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100908static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200910 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 hda_nid_t nid;
912
913 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
914 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200915 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200916 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200917 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200918 case AC_GRP_AUDIO_FUNCTION:
919 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 codec->afg_function_id = function_id & 0xff;
921 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 break;
923 case AC_GRP_MODEM_FUNCTION:
924 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200925 codec->mfg_function_id = function_id & 0xff;
926 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200927 break;
928 default:
929 break;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100935 * read widget caps for each widget and store in cache
936 */
937static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
938{
939 int i;
940 hda_nid_t nid;
941
942 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
943 &codec->start_nid);
944 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200945 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100946 return -ENOMEM;
947 nid = codec->start_nid;
948 for (i = 0; i < codec->num_nodes; i++, nid++)
949 codec->wcaps[i] = snd_hda_param_read(codec, nid,
950 AC_PAR_AUDIO_WIDGET_CAP);
951 return 0;
952}
953
Takashi Iwai3be14142009-02-20 14:11:16 +0100954/* read all pin default configurations and save codec->init_pins */
955static int read_pin_defaults(struct hda_codec *codec)
956{
957 int i;
958 hda_nid_t nid = codec->start_nid;
959
960 for (i = 0; i < codec->num_nodes; i++, nid++) {
961 struct hda_pincfg *pin;
962 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200963 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100964 if (wid_type != AC_WID_PIN)
965 continue;
966 pin = snd_array_new(&codec->init_pins);
967 if (!pin)
968 return -ENOMEM;
969 pin->nid = nid;
970 pin->cfg = snd_hda_codec_read(codec, nid, 0,
971 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200972 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
973 AC_VERB_GET_PIN_WIDGET_CONTROL,
974 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100975 }
976 return 0;
977}
978
979/* look up the given pin config list and return the item matching with NID */
980static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
981 struct snd_array *array,
982 hda_nid_t nid)
983{
984 int i;
985 for (i = 0; i < array->used; i++) {
986 struct hda_pincfg *pin = snd_array_elem(array, i);
987 if (pin->nid == nid)
988 return pin;
989 }
990 return NULL;
991}
992
993/* write a config value for the given NID */
994static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
995 unsigned int cfg)
996{
997 int i;
998 for (i = 0; i < 4; i++) {
999 snd_hda_codec_write(codec, nid, 0,
1000 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1001 cfg & 0xff);
1002 cfg >>= 8;
1003 }
1004}
1005
1006/* set the current pin config value for the given NID.
1007 * the value is cached, and read via snd_hda_codec_get_pincfg()
1008 */
1009int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1010 hda_nid_t nid, unsigned int cfg)
1011{
1012 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001013 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001014
Takashi Iwaib82855a2009-12-27 11:24:56 +01001015 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1016 return -EINVAL;
1017
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001018 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001019 pin = look_up_pincfg(codec, list, nid);
1020 if (!pin) {
1021 pin = snd_array_new(list);
1022 if (!pin)
1023 return -ENOMEM;
1024 pin->nid = nid;
1025 }
1026 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001027
1028 /* change only when needed; e.g. if the pincfg is already present
1029 * in user_pins[], don't write it
1030 */
1031 cfg = snd_hda_codec_get_pincfg(codec, nid);
1032 if (oldcfg != cfg)
1033 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001034 return 0;
1035}
1036
Takashi Iwaid5191e52009-11-16 14:58:17 +01001037/**
1038 * snd_hda_codec_set_pincfg - Override a pin default configuration
1039 * @codec: the HDA codec
1040 * @nid: NID to set the pin config
1041 * @cfg: the pin default config value
1042 *
1043 * Override a pin default configuration value in the cache.
1044 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1045 * priority than the real hardware value.
1046 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001047int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1048 hda_nid_t nid, unsigned int cfg)
1049{
Takashi Iwai346ff702009-02-23 09:42:57 +01001050 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001051}
1052EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1053
Takashi Iwaid5191e52009-11-16 14:58:17 +01001054/**
1055 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1056 * @codec: the HDA codec
1057 * @nid: NID to get the pin config
1058 *
1059 * Get the current pin config value of the given pin NID.
1060 * If the pincfg value is cached or overridden via sysfs or driver,
1061 * returns the cached value.
1062 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001063unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1064{
1065 struct hda_pincfg *pin;
1066
Takashi Iwai3be14142009-02-20 14:11:16 +01001067#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001068 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001069 if (pin)
1070 return pin->cfg;
1071#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001072 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1073 if (pin)
1074 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1076 if (pin)
1077 return pin->cfg;
1078 return 0;
1079}
1080EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1081
1082/* restore all current pin configs */
1083static void restore_pincfgs(struct hda_codec *codec)
1084{
1085 int i;
1086 for (i = 0; i < codec->init_pins.used; i++) {
1087 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1088 set_pincfg(codec, pin->nid,
1089 snd_hda_codec_get_pincfg(codec, pin->nid));
1090 }
1091}
Takashi Iwai54d17402005-11-21 16:33:22 +01001092
Takashi Iwai92ee6162009-12-27 11:18:59 +01001093/**
1094 * snd_hda_shutup_pins - Shut up all pins
1095 * @codec: the HDA codec
1096 *
1097 * Clear all pin controls to shup up before suspend for avoiding click noise.
1098 * The controls aren't cached so that they can be resumed properly.
1099 */
1100void snd_hda_shutup_pins(struct hda_codec *codec)
1101{
1102 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001103 /* don't shut up pins when unloading the driver; otherwise it breaks
1104 * the default pin setup at the next load of the driver
1105 */
1106 if (codec->bus->shutdown)
1107 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001108 for (i = 0; i < codec->init_pins.used; i++) {
1109 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1110 /* use read here for syncing after issuing each verb */
1111 snd_hda_codec_read(codec, pin->nid, 0,
1112 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1113 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001114 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001115}
1116EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1117
Takashi Iwai2a439522011-07-26 09:52:50 +02001118#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001119/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1120static void restore_shutup_pins(struct hda_codec *codec)
1121{
1122 int i;
1123 if (!codec->pins_shutup)
1124 return;
1125 if (codec->bus->shutdown)
1126 return;
1127 for (i = 0; i < codec->init_pins.used; i++) {
1128 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1129 snd_hda_codec_write(codec, pin->nid, 0,
1130 AC_VERB_SET_PIN_WIDGET_CONTROL,
1131 pin->ctrl);
1132 }
1133 codec->pins_shutup = 0;
1134}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001135#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001136
Takashi Iwai01751f52007-08-10 16:59:39 +02001137static void init_hda_cache(struct hda_cache_rec *cache,
1138 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001139static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001140
Takashi Iwai3be14142009-02-20 14:11:16 +01001141/* restore the initial pin cfgs and release all pincfg lists */
1142static void restore_init_pincfgs(struct hda_codec *codec)
1143{
Takashi Iwai346ff702009-02-23 09:42:57 +01001144 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001145 * so that only the values in init_pins are restored
1146 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001147 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001148#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001149 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001150#endif
1151 restore_pincfgs(codec);
1152 snd_array_free(&codec->init_pins);
1153}
1154
Takashi Iwai54d17402005-11-21 16:33:22 +01001155/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001156 * audio-converter setup caches
1157 */
1158struct hda_cvt_setup {
1159 hda_nid_t nid;
1160 u8 stream_tag;
1161 u8 channel_id;
1162 u16 format_id;
1163 unsigned char active; /* cvt is currently used */
1164 unsigned char dirty; /* setups should be cleared */
1165};
1166
1167/* get or create a cache entry for the given audio converter NID */
1168static struct hda_cvt_setup *
1169get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1170{
1171 struct hda_cvt_setup *p;
1172 int i;
1173
1174 for (i = 0; i < codec->cvt_setups.used; i++) {
1175 p = snd_array_elem(&codec->cvt_setups, i);
1176 if (p->nid == nid)
1177 return p;
1178 }
1179 p = snd_array_new(&codec->cvt_setups);
1180 if (p)
1181 p->nid = nid;
1182 return p;
1183}
1184
1185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * codec destructor
1187 */
1188static void snd_hda_codec_free(struct hda_codec *codec)
1189{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001190 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001192 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001193#ifdef CONFIG_SND_HDA_POWER_SAVE
1194 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001195 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001198 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001199 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001200 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001201 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 codec->bus->caddr_tbl[codec->addr] = NULL;
1203 if (codec->patch_ops.free)
1204 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001205 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001206 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001207 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001208 kfree(codec->vendor_name);
1209 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001210 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001211 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 kfree(codec);
1213}
1214
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001215static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1216 unsigned int power_state);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/**
1219 * snd_hda_codec_new - create a HDA codec
1220 * @bus: the bus to assign
1221 * @codec_addr: the codec address
1222 * @codecp: the pointer to store the generated codec
1223 *
1224 * Returns 0 if successful, or a negative error code.
1225 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001226int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1227 unsigned int codec_addr,
1228 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001231 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233
Takashi Iwaida3cec32008-08-08 17:12:14 +02001234 if (snd_BUG_ON(!bus))
1235 return -EINVAL;
1236 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1237 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001240 snd_printk(KERN_ERR "hda_codec: "
1241 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EBUSY;
1243 }
1244
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001245 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (codec == NULL) {
1247 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1248 return -ENOMEM;
1249 }
1250
1251 codec->bus = bus;
1252 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001253 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001254 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001255 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001256 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001257 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1258 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001259 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001260 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001261 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001262 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001263 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001264 if (codec->bus->modelname) {
1265 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1266 if (!codec->modelname) {
1267 snd_hda_codec_free(codec);
1268 return -ENODEV;
1269 }
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Takashi Iwaicb53c622007-08-10 17:21:45 +02001272#ifdef CONFIG_SND_HDA_POWER_SAVE
1273 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1274 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1275 * the caller has to power down appropriatley after initialization
1276 * phase.
1277 */
1278 hda_keep_power_on(codec);
1279#endif
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 list_add_tail(&codec->list, &bus->codec_list);
1282 bus->caddr_tbl[codec_addr] = codec;
1283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001286 if (codec->vendor_id == -1)
1287 /* read again, hopefully the access method was corrected
1288 * in the last read...
1289 */
1290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_SUBSYSTEM_ID);
1294 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001299 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = -ENODEV;
1301 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1305 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001306 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001308 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_pin_defaults(codec);
1310 if (err < 0)
1311 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001312
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001314 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 codec->subsystem_id =
1316 snd_hda_codec_read(codec, nid, 0,
1317 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001318 }
1319
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001320 /* power-up all before initialization */
1321 hda_set_power_state(codec,
1322 codec->afg ? codec->afg : codec->mfg,
1323 AC_PWRST_D0);
1324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001325 snd_hda_codec_proc_new(codec);
1326
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001327 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328
1329 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1330 codec->subsystem_id, codec->revision_id);
1331 snd_component_add(codec->bus->card, component);
1332
1333 if (codecp)
1334 *codecp = codec;
1335 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001336
1337 error:
1338 snd_hda_codec_free(codec);
1339 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001341EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001342
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343/**
1344 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1345 * @codec: the HDA codec
1346 *
1347 * Start parsing of the given codec tree and (re-)initialize the whole
1348 * patch instance.
1349 *
1350 * Returns 0 if successful or a negative error code.
1351 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352int snd_hda_codec_configure(struct hda_codec *codec)
1353{
1354 int err;
1355
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001356 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001357 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001358 err = get_codec_name(codec);
1359 if (err < 0)
1360 return err;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai82467612007-07-27 19:15:54 +02001363 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001365 goto patched;
1366 }
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (codec->preset && codec->preset->patch) {
1368 err = codec->preset->patch(codec);
1369 goto patched;
1370 }
1371
1372 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001373 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001374 if (err < 0)
1375 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001376
1377 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001378 if (!err && codec->patch_ops.unsol_event)
1379 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001380 /* audio codec should override the mixer name */
1381 if (!err && (codec->afg || !*codec->bus->card->mixername))
1382 snprintf(codec->bus->card->mixername,
1383 sizeof(codec->bus->card->mixername),
1384 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001387EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389/**
1390 * snd_hda_codec_setup_stream - set up the codec for streaming
1391 * @codec: the CODEC to set up
1392 * @nid: the NID to set up
1393 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1394 * @channel_id: channel id to pass, zero based.
1395 * @format: stream format.
1396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001397void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1398 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int channel_id, int format)
1400{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001401 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001402 struct hda_cvt_setup *p;
1403 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001404 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001405 int i;
1406
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001408 return;
1409
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 snd_printdd("hda_codec_setup_stream: "
1411 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001413 p = get_hda_cvt_setup(codec, nid);
1414 if (!p)
1415 return;
1416 /* update the stream-id if changed */
1417 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1418 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1419 newval = (stream_tag << 4) | channel_id;
1420 if (oldval != newval)
1421 snd_hda_codec_write(codec, nid, 0,
1422 AC_VERB_SET_CHANNEL_STREAMID,
1423 newval);
1424 p->stream_tag = stream_tag;
1425 p->channel_id = channel_id;
1426 }
1427 /* update the format-id if changed */
1428 if (p->format_id != format) {
1429 oldval = snd_hda_codec_read(codec, nid, 0,
1430 AC_VERB_GET_STREAM_FORMAT, 0);
1431 if (oldval != format) {
1432 msleep(1);
1433 snd_hda_codec_write(codec, nid, 0,
1434 AC_VERB_SET_STREAM_FORMAT,
1435 format);
1436 }
1437 p->format_id = format;
1438 }
1439 p->active = 1;
1440 p->dirty = 0;
1441
1442 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001443 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001444 list_for_each_entry(c, &codec->bus->codec_list, list) {
1445 for (i = 0; i < c->cvt_setups.used; i++) {
1446 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001447 if (!p->active && p->stream_tag == stream_tag &&
1448 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001449 p->dirty = 1;
1450 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001453EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Takashi Iwaif0cea792010-08-13 11:56:53 +02001455static void really_cleanup_stream(struct hda_codec *codec,
1456 struct hda_cvt_setup *q);
1457
Takashi Iwaid5191e52009-11-16 14:58:17 +01001458/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001459 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001460 * @codec: the CODEC to clean up
1461 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001462 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1465 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001466{
Takashi Iwaieb541332010-08-06 13:48:11 +02001467 struct hda_cvt_setup *p;
1468
Takashi Iwai888afa12008-03-18 09:57:50 +01001469 if (!nid)
1470 return;
1471
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001472 if (codec->no_sticky_stream)
1473 do_now = 1;
1474
Takashi Iwai888afa12008-03-18 09:57:50 +01001475 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001476 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001477 if (p) {
1478 /* here we just clear the active flag when do_now isn't set;
1479 * actual clean-ups will be done later in
1480 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1481 */
1482 if (do_now)
1483 really_cleanup_stream(codec, p);
1484 else
1485 p->active = 0;
1486 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001487}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001489
Takashi Iwaieb541332010-08-06 13:48:11 +02001490static void really_cleanup_stream(struct hda_codec *codec,
1491 struct hda_cvt_setup *q)
1492{
1493 hda_nid_t nid = q->nid;
1494 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1495 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1496 memset(q, 0, sizeof(*q));
1497 q->nid = nid;
1498}
1499
1500/* clean up the all conflicting obsolete streams */
1501static void purify_inactive_streams(struct hda_codec *codec)
1502{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001503 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001504 int i;
1505
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001506 list_for_each_entry(c, &codec->bus->codec_list, list) {
1507 for (i = 0; i < c->cvt_setups.used; i++) {
1508 struct hda_cvt_setup *p;
1509 p = snd_array_elem(&c->cvt_setups, i);
1510 if (p->dirty)
1511 really_cleanup_stream(c, p);
1512 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001513 }
1514}
1515
Takashi Iwai2a439522011-07-26 09:52:50 +02001516#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001517/* clean up all streams; called from suspend */
1518static void hda_cleanup_all_streams(struct hda_codec *codec)
1519{
1520 int i;
1521
1522 for (i = 0; i < codec->cvt_setups.used; i++) {
1523 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1524 if (p->stream_tag)
1525 really_cleanup_stream(codec, p);
1526 }
1527}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001528#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/*
1531 * amp access functions
1532 */
1533
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001534/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001535#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001536#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001537#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1538#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001540#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001543static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001544 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Takashi Iwai01751f52007-08-10 16:59:39 +02001546 memset(cache, 0, sizeof(*cache));
1547 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001548 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001549}
1550
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001551static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001552{
Takashi Iwai603c4012008-07-30 15:01:44 +02001553 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001557static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwai01751f52007-08-10 16:59:39 +02001559 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1560 u16 cur = cache->hash[idx];
1561 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001564 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (info->key == key)
1566 return info;
1567 cur = info->next;
1568 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001569 return NULL;
1570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001572/* query the hash. allocate an entry if not found. */
1573static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1574 u32 key)
1575{
1576 struct hda_cache_head *info = get_hash(cache, key);
1577 if (!info) {
1578 u16 idx, cur;
1579 /* add a new hash entry */
1580 info = snd_array_new(&cache->buf);
1581 if (!info)
1582 return NULL;
1583 cur = snd_array_index(&cache->buf, info);
1584 info->key = key;
1585 info->val = 0;
1586 idx = key % (u16)ARRAY_SIZE(cache->hash);
1587 info->next = cache->hash[idx];
1588 cache->hash[idx] = cur;
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return info;
1591}
1592
Takashi Iwai01751f52007-08-10 16:59:39 +02001593/* query and allocate an amp hash entry */
1594static inline struct hda_amp_info *
1595get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1596{
1597 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1598}
1599
Takashi Iwaid5191e52009-11-16 14:58:17 +01001600/**
1601 * query_amp_caps - query AMP capabilities
1602 * @codec: the HD-auio codec
1603 * @nid: the NID to query
1604 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1605 *
1606 * Query AMP capabilities for the given widget and direction.
1607 * Returns the obtained capability bits.
1608 *
1609 * When cap bits have been already read, this doesn't read again but
1610 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001612u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001614 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Takashi Iwai0ba21762007-04-16 11:29:14 +02001616 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1617 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001619 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001620 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 info->amp_caps = snd_hda_param_read(codec, nid,
1623 direction == HDA_OUTPUT ?
1624 AC_PAR_AMP_OUT_CAP :
1625 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001626 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001627 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 return info->amp_caps;
1630}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001631EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Takashi Iwaid5191e52009-11-16 14:58:17 +01001633/**
1634 * snd_hda_override_amp_caps - Override the AMP capabilities
1635 * @codec: the CODEC to clean up
1636 * @nid: the NID to clean up
1637 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1638 * @caps: the capability bits to set
1639 *
1640 * Override the cached AMP caps bits value by the given one.
1641 * This function is useful if the driver needs to adjust the AMP ranges,
1642 * e.g. limit to 0dB, etc.
1643 *
1644 * Returns zero if successful or a negative error code.
1645 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001646int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1647 unsigned int caps)
1648{
1649 struct hda_amp_info *info;
1650
1651 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1652 if (!info)
1653 return -EINVAL;
1654 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001655 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001656 return 0;
1657}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001658EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001659
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001660static unsigned int
1661query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1662 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001663{
1664 struct hda_amp_info *info;
1665
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001666 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001667 if (!info)
1668 return 0;
1669 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001670 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001671 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001672 }
1673 return info->amp_caps;
1674}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001675
1676static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1677{
1678 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1679}
1680
Takashi Iwaid5191e52009-11-16 14:58:17 +01001681/**
1682 * snd_hda_query_pin_caps - Query PIN capabilities
1683 * @codec: the HD-auio codec
1684 * @nid: the NID to query
1685 *
1686 * Query PIN capabilities for the given widget.
1687 * Returns the obtained capability bits.
1688 *
1689 * When cap bits have been already read, this doesn't read again but
1690 * returns the cached value.
1691 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001692u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1693{
1694 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1695 read_pin_cap);
1696}
Takashi Iwai1327a322009-03-23 13:07:47 +01001697EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1698
Wu Fengguang864f92b2009-11-18 12:38:02 +08001699/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001700 * snd_hda_override_pin_caps - Override the pin capabilities
1701 * @codec: the CODEC
1702 * @nid: the NID to override
1703 * @caps: the capability bits to set
1704 *
1705 * Override the cached PIN capabilitiy bits value by the given one.
1706 *
1707 * Returns zero if successful or a negative error code.
1708 */
1709int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1710 unsigned int caps)
1711{
1712 struct hda_amp_info *info;
1713 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1714 if (!info)
1715 return -ENOMEM;
1716 info->amp_caps = caps;
1717 info->head.val |= INFO_AMP_CAPS;
1718 return 0;
1719}
1720EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1721
1722/**
Wu Fengguang864f92b2009-11-18 12:38:02 +08001723 * snd_hda_pin_sense - execute pin sense measurement
1724 * @codec: the CODEC to sense
1725 * @nid: the pin NID to sense
1726 *
1727 * Execute necessary pin sense measurement and return its Presence Detect,
1728 * Impedance, ELD Valid etc. status bits.
1729 */
1730u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1731{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001732 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001733
Takashi Iwai729d55b2009-12-25 22:49:01 +01001734 if (!codec->no_trigger_sense) {
1735 pincap = snd_hda_query_pin_caps(codec, nid);
1736 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001737 snd_hda_codec_read(codec, nid, 0,
1738 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001739 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001740 return snd_hda_codec_read(codec, nid, 0,
1741 AC_VERB_GET_PIN_SENSE, 0);
1742}
1743EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1744
1745/**
1746 * snd_hda_jack_detect - query pin Presence Detect status
1747 * @codec: the CODEC to sense
1748 * @nid: the pin NID to sense
1749 *
1750 * Query and return the pin's Presence Detect status.
1751 */
1752int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1753{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001754 u32 sense = snd_hda_pin_sense(codec, nid);
1755 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001756}
1757EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/*
1760 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001761 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001763static unsigned int get_vol_mute(struct hda_codec *codec,
1764 struct hda_amp_info *info, hda_nid_t nid,
1765 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 u32 val, parm;
1768
Takashi Iwai01751f52007-08-10 16:59:39 +02001769 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001770 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1773 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1774 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775 val = snd_hda_codec_read(codec, nid, 0,
1776 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001778 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001779 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001783 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001785static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001786 hda_nid_t nid, int ch, int direction, int index,
1787 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 u32 parm;
1790
1791 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1792 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1793 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1794 parm |= val;
1795 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001796 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Takashi Iwaid5191e52009-11-16 14:58:17 +01001799/**
1800 * snd_hda_codec_amp_read - Read AMP value
1801 * @codec: HD-audio codec
1802 * @nid: NID to read the AMP value
1803 * @ch: channel (left=0 or right=1)
1804 * @direction: #HDA_INPUT or #HDA_OUTPUT
1805 * @index: the index value (only for input direction)
1806 *
1807 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
Takashi Iwai834be882006-03-01 14:16:17 +01001809int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1810 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001812 struct hda_amp_info *info;
1813 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1814 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001816 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001818EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Takashi Iwaid5191e52009-11-16 14:58:17 +01001820/**
1821 * snd_hda_codec_amp_update - update the AMP value
1822 * @codec: HD-audio codec
1823 * @nid: NID to read the AMP value
1824 * @ch: channel (left=0 or right=1)
1825 * @direction: #HDA_INPUT or #HDA_OUTPUT
1826 * @idx: the index value (only for input direction)
1827 * @mask: bit mask to set
1828 * @val: the bits value to set
1829 *
1830 * Update the AMP value with a bit mask.
1831 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001832 */
Takashi Iwai834be882006-03-01 14:16:17 +01001833int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1834 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001836 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001837
Takashi Iwai0ba21762007-04-16 11:29:14 +02001838 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1839 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001841 if (snd_BUG_ON(mask & ~0xff))
1842 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001843 val &= mask;
1844 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001845 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001847 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 1;
1849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001850EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Takashi Iwaid5191e52009-11-16 14:58:17 +01001852/**
1853 * snd_hda_codec_amp_stereo - update the AMP stereo values
1854 * @codec: HD-audio codec
1855 * @nid: NID to read the AMP value
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @idx: the index value (only for input direction)
1858 * @mask: bit mask to set
1859 * @val: the bits value to set
1860 *
1861 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1862 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001863 */
1864int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1865 int direction, int idx, int mask, int val)
1866{
1867 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001868
1869 if (snd_BUG_ON(mask & ~0xff))
1870 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001871 for (ch = 0; ch < 2; ch++)
1872 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1873 idx, mask, val);
1874 return ret;
1875}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001876EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001877
Takashi Iwai2a439522011-07-26 09:52:50 +02001878#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001879/**
1880 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1881 * @codec: HD-audio codec
1882 *
1883 * Resume the all amp commands from the cache.
1884 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001885void snd_hda_codec_resume_amp(struct hda_codec *codec)
1886{
Takashi Iwai603c4012008-07-30 15:01:44 +02001887 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001888 int i;
1889
Takashi Iwai603c4012008-07-30 15:01:44 +02001890 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001891 u32 key = buffer->head.key;
1892 hda_nid_t nid;
1893 unsigned int idx, dir, ch;
1894 if (!key)
1895 continue;
1896 nid = key & 0xff;
1897 idx = (key >> 16) & 0xff;
1898 dir = (key >> 24) & 0xff;
1899 for (ch = 0; ch < 2; ch++) {
1900 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1901 continue;
1902 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1903 buffer->vol[ch]);
1904 }
1905 }
1906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001907EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001908#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001910static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1911 unsigned int ofs)
1912{
1913 u32 caps = query_amp_caps(codec, nid, dir);
1914 /* get num steps */
1915 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1916 if (ofs < caps)
1917 caps -= ofs;
1918 return caps;
1919}
1920
Takashi Iwaid5191e52009-11-16 14:58:17 +01001921/**
1922 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1923 *
1924 * The control element is supposed to have the private_value field
1925 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1926 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001927int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1928 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1931 u16 nid = get_amp_nid(kcontrol);
1932 u8 chs = get_amp_channels(kcontrol);
1933 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001934 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001936 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1937 uinfo->count = chs == 3 ? 2 : 1;
1938 uinfo->value.integer.min = 0;
1939 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1940 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001941 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001942 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1943 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return -EINVAL;
1945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return 0;
1947}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001948EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001950
1951static inline unsigned int
1952read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1953 int ch, int dir, int idx, unsigned int ofs)
1954{
1955 unsigned int val;
1956 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1957 val &= HDA_AMP_VOLMASK;
1958 if (val >= ofs)
1959 val -= ofs;
1960 else
1961 val = 0;
1962 return val;
1963}
1964
1965static inline int
1966update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1967 int ch, int dir, int idx, unsigned int ofs,
1968 unsigned int val)
1969{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001970 unsigned int maxval;
1971
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001972 if (val > 0)
1973 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001974 /* ofs = 0: raw max value */
1975 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001976 if (val > maxval)
1977 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001978 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1979 HDA_AMP_VOLMASK, val);
1980}
1981
Takashi Iwaid5191e52009-11-16 14:58:17 +01001982/**
1983 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1984 *
1985 * The control element is supposed to have the private_value field
1986 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001988int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 hda_nid_t nid = get_amp_nid(kcontrol);
1993 int chs = get_amp_channels(kcontrol);
1994 int dir = get_amp_direction(kcontrol);
1995 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001996 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 long *valp = ucontrol->value.integer.value;
1998
1999 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002005EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Takashi Iwaid5191e52009-11-16 14:58:17 +01002007/**
2008 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2009 *
2010 * The control element is supposed to have the private_value field
2011 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2012 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002013int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2017 hda_nid_t nid = get_amp_nid(kcontrol);
2018 int chs = get_amp_channels(kcontrol);
2019 int dir = get_amp_direction(kcontrol);
2020 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002021 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 long *valp = ucontrol->value.integer.value;
2023 int change = 0;
2024
Takashi Iwaicb53c622007-08-10 17:21:45 +02002025 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002026 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002028 valp++;
2029 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002030 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002031 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002032 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return change;
2034}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002035EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Takashi Iwaid5191e52009-11-16 14:58:17 +01002037/**
2038 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2039 *
2040 * The control element is supposed to have the private_value field
2041 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2042 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002043int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2044 unsigned int size, unsigned int __user *_tlv)
2045{
2046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2047 hda_nid_t nid = get_amp_nid(kcontrol);
2048 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002049 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002050 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002051 u32 caps, val1, val2;
2052
2053 if (size < 4 * sizeof(unsigned int))
2054 return -ENOMEM;
2055 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002056 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2057 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002058 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002059 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002060 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002061 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002062 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002063 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2064 return -EFAULT;
2065 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2066 return -EFAULT;
2067 if (put_user(val1, _tlv + 2))
2068 return -EFAULT;
2069 if (put_user(val2, _tlv + 3))
2070 return -EFAULT;
2071 return 0;
2072}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002073EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002074
Takashi Iwaid5191e52009-11-16 14:58:17 +01002075/**
2076 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2077 * @codec: HD-audio codec
2078 * @nid: NID of a reference widget
2079 * @dir: #HDA_INPUT or #HDA_OUTPUT
2080 * @tlv: TLV data to be stored, at least 4 elements
2081 *
2082 * Set (static) TLV data for a virtual master volume using the AMP caps
2083 * obtained from the reference NID.
2084 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002085 */
2086void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2087 unsigned int *tlv)
2088{
2089 u32 caps;
2090 int nums, step;
2091
2092 caps = query_amp_caps(codec, nid, dir);
2093 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2094 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2095 step = (step + 1) * 25;
2096 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2097 tlv[1] = 2 * sizeof(unsigned int);
2098 tlv[2] = -nums * step;
2099 tlv[3] = step;
2100}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002101EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002102
2103/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002104static struct snd_kcontrol *
2105_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2106 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002107{
2108 struct snd_ctl_elem_id id;
2109 memset(&id, 0, sizeof(id));
2110 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002111 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002112 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2113 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002114 strcpy(id.name, name);
2115 return snd_ctl_find_id(codec->bus->card, &id);
2116}
2117
Takashi Iwaid5191e52009-11-16 14:58:17 +01002118/**
2119 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2120 * @codec: HD-audio codec
2121 * @name: ctl id name string
2122 *
2123 * Get the control element with the given id string and IFACE_MIXER.
2124 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002125struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2126 const char *name)
2127{
2128 return _snd_hda_find_mixer_ctl(codec, name, 0);
2129}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002130EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002131
Takashi Iwai1afe2062010-12-23 10:17:52 +01002132static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2133{
2134 int idx;
2135 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2136 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2137 return idx;
2138 }
2139 return -EBUSY;
2140}
2141
Takashi Iwaid5191e52009-11-16 14:58:17 +01002142/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002143 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002144 * @codec: HD-audio codec
2145 * @nid: corresponding NID (optional)
2146 * @kctl: the control element to assign
2147 *
2148 * Add the given control element to an array inside the codec instance.
2149 * All control elements belonging to a codec are supposed to be added
2150 * by this function so that a proper clean-up works at the free or
2151 * reconfiguration time.
2152 *
2153 * If non-zero @nid is passed, the NID is assigned to the control element.
2154 * The assignment is shown in the codec proc file.
2155 *
2156 * snd_hda_ctl_add() checks the control subdev id field whether
2157 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002158 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2159 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002161int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2162 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002163{
2164 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002165 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002166 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002167
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002168 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002169 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002170 if (nid == 0)
2171 nid = get_amp_nid_(kctl->private_value);
2172 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002173 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2174 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002175 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002176 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002177 err = snd_ctl_add(codec->bus->card, kctl);
2178 if (err < 0)
2179 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002180 item = snd_array_new(&codec->mixers);
2181 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002182 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002183 item->kctl = kctl;
2184 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002185 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002186 return 0;
2187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002188EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002189
Takashi Iwaid5191e52009-11-16 14:58:17 +01002190/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002191 * snd_hda_add_nid - Assign a NID to a control element
2192 * @codec: HD-audio codec
2193 * @nid: corresponding NID (optional)
2194 * @kctl: the control element to assign
2195 * @index: index to kctl
2196 *
2197 * Add the given control element to an array inside the codec instance.
2198 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2199 * NID:KCTL mapping - for example "Capture Source" selector.
2200 */
2201int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2202 unsigned int index, hda_nid_t nid)
2203{
2204 struct hda_nid_item *item;
2205
2206 if (nid > 0) {
2207 item = snd_array_new(&codec->nids);
2208 if (!item)
2209 return -ENOMEM;
2210 item->kctl = kctl;
2211 item->index = index;
2212 item->nid = nid;
2213 return 0;
2214 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002215 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2216 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002217 return -EINVAL;
2218}
2219EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2220
2221/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002222 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2223 * @codec: HD-audio codec
2224 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002225void snd_hda_ctls_clear(struct hda_codec *codec)
2226{
2227 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002228 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002229 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002230 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002231 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002232 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002233}
2234
Takashi Iwaia65d6292009-02-23 16:57:04 +01002235/* pseudo device locking
2236 * toggle card->shutdown to allow/disallow the device access (as a hack)
2237 */
2238static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002239{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002240 spin_lock(&card->files_lock);
2241 if (card->shutdown) {
2242 spin_unlock(&card->files_lock);
2243 return -EINVAL;
2244 }
2245 card->shutdown = 1;
2246 spin_unlock(&card->files_lock);
2247 return 0;
2248}
2249
2250static void hda_unlock_devices(struct snd_card *card)
2251{
2252 spin_lock(&card->files_lock);
2253 card->shutdown = 0;
2254 spin_unlock(&card->files_lock);
2255}
2256
Takashi Iwaid5191e52009-11-16 14:58:17 +01002257/**
2258 * snd_hda_codec_reset - Clear all objects assigned to the codec
2259 * @codec: HD-audio codec
2260 *
2261 * This frees the all PCM and control elements assigned to the codec, and
2262 * clears the caches and restores the pin default configurations.
2263 *
2264 * When a device is being used, it returns -EBSY. If successfully freed,
2265 * returns zero.
2266 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002267int snd_hda_codec_reset(struct hda_codec *codec)
2268{
2269 struct snd_card *card = codec->bus->card;
2270 int i, pcm;
2271
2272 if (hda_lock_devices(card) < 0)
2273 return -EBUSY;
2274 /* check whether the codec isn't used by any mixer or PCM streams */
2275 if (!list_empty(&card->ctl_files)) {
2276 hda_unlock_devices(card);
2277 return -EBUSY;
2278 }
2279 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2280 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2281 if (!cpcm->pcm)
2282 continue;
2283 if (cpcm->pcm->streams[0].substream_opened ||
2284 cpcm->pcm->streams[1].substream_opened) {
2285 hda_unlock_devices(card);
2286 return -EBUSY;
2287 }
2288 }
2289
2290 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002291
2292#ifdef CONFIG_SND_HDA_POWER_SAVE
2293 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002294 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002295#endif
2296 snd_hda_ctls_clear(codec);
2297 /* relase PCMs */
2298 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002299 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002300 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002301 clear_bit(codec->pcm_info[i].device,
2302 codec->bus->pcm_dev_bits);
2303 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002304 }
2305 if (codec->patch_ops.free)
2306 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002307 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002308 codec->spec = NULL;
2309 free_hda_cache(&codec->amp_cache);
2310 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002311 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2312 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002313 /* free only driver_pins so that init_pins + user_pins are restored */
2314 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002315 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002316 codec->num_pcms = 0;
2317 codec->pcm_info = NULL;
2318 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002319 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2320 codec->slave_dig_outs = NULL;
2321 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002322 module_put(codec->owner);
2323 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002324
2325 /* allow device access again */
2326 hda_unlock_devices(card);
2327 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002328}
2329
Takashi Iwaid5191e52009-11-16 14:58:17 +01002330/**
2331 * snd_hda_add_vmaster - create a virtual master control and add slaves
2332 * @codec: HD-audio codec
2333 * @name: vmaster control name
2334 * @tlv: TLV data (optional)
2335 * @slaves: slave control names (optional)
2336 *
2337 * Create a virtual master control with the given name. The TLV data
2338 * must be either NULL or a valid data.
2339 *
2340 * @slaves is a NULL-terminated array of strings, each of which is a
2341 * slave control name. All controls with these names are assigned to
2342 * the new virtual master control.
2343 *
2344 * This function returns zero if successful or a negative error code.
2345 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002346int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002347 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002348{
2349 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002350 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002351 int err;
2352
Takashi Iwai2f085542008-02-22 18:43:50 +01002353 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2354 ;
2355 if (!*s) {
2356 snd_printdd("No slave found for %s\n", name);
2357 return 0;
2358 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002359 kctl = snd_ctl_make_virtual_master(name, tlv);
2360 if (!kctl)
2361 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002362 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002363 if (err < 0)
2364 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002365
Takashi Iwai2134ea42008-01-10 16:53:55 +01002366 for (s = slaves; *s; s++) {
2367 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002368 int i = 0;
2369 for (;;) {
2370 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2371 if (!sctl) {
2372 if (!i)
2373 snd_printdd("Cannot find slave %s, "
2374 "skipped\n", *s);
2375 break;
2376 }
2377 err = snd_ctl_add_slave(kctl, sctl);
2378 if (err < 0)
2379 return err;
2380 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002381 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002382 }
2383 return 0;
2384}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002385EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002386
Takashi Iwaid5191e52009-11-16 14:58:17 +01002387/**
2388 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2389 *
2390 * The control element is supposed to have the private_value field
2391 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2392 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002393int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 int chs = get_amp_channels(kcontrol);
2397
2398 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2399 uinfo->count = chs == 3 ? 2 : 1;
2400 uinfo->value.integer.min = 0;
2401 uinfo->value.integer.max = 1;
2402 return 0;
2403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002404EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Takashi Iwaid5191e52009-11-16 14:58:17 +01002406/**
2407 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2408 *
2409 * The control element is supposed to have the private_value field
2410 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2411 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002412int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2416 hda_nid_t nid = get_amp_nid(kcontrol);
2417 int chs = get_amp_channels(kcontrol);
2418 int dir = get_amp_direction(kcontrol);
2419 int idx = get_amp_index(kcontrol);
2420 long *valp = ucontrol->value.integer.value;
2421
2422 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002424 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002426 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002427 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002430EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Takashi Iwaid5191e52009-11-16 14:58:17 +01002432/**
2433 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2434 *
2435 * The control element is supposed to have the private_value field
2436 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2437 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002438int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442 hda_nid_t nid = get_amp_nid(kcontrol);
2443 int chs = get_amp_channels(kcontrol);
2444 int dir = get_amp_direction(kcontrol);
2445 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 long *valp = ucontrol->value.integer.value;
2447 int change = 0;
2448
Takashi Iwaicb53c622007-08-10 17:21:45 +02002449 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002450 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002451 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002452 HDA_AMP_MUTE,
2453 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002454 valp++;
2455 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002456 if (chs & 2)
2457 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002458 HDA_AMP_MUTE,
2459 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002460 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002461 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return change;
2463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002464EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Takashi Iwai67d634c2009-11-16 15:35:59 +01002466#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002467/**
2468 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2469 *
2470 * This function calls snd_hda_enable_beep_device(), which behaves differently
2471 * depending on beep_mode option.
2472 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002473int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol)
2475{
2476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2477 long *valp = ucontrol->value.integer.value;
2478
2479 snd_hda_enable_beep_device(codec, *valp);
2480 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2481}
2482EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002483#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485/*
Takashi Iwai985be542005-11-02 18:26:49 +01002486 * bound volume controls
2487 *
2488 * bind multiple volumes (# indices, from 0)
2489 */
2490
2491#define AMP_VAL_IDX_SHIFT 19
2492#define AMP_VAL_IDX_MASK (0x0f<<19)
2493
Takashi Iwaid5191e52009-11-16 14:58:17 +01002494/**
2495 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2496 *
2497 * The control element is supposed to have the private_value field
2498 * set up via HDA_BIND_MUTE*() macros.
2499 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002500int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002502{
2503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2504 unsigned long pval;
2505 int err;
2506
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002507 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002508 pval = kcontrol->private_value;
2509 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2510 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2511 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002512 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002513 return err;
2514}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002515EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002516
Takashi Iwaid5191e52009-11-16 14:58:17 +01002517/**
2518 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2519 *
2520 * The control element is supposed to have the private_value field
2521 * set up via HDA_BIND_MUTE*() macros.
2522 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002523int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2524 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002525{
2526 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2527 unsigned long pval;
2528 int i, indices, err = 0, change = 0;
2529
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002530 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002531 pval = kcontrol->private_value;
2532 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2533 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002534 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2535 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002536 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2537 if (err < 0)
2538 break;
2539 change |= err;
2540 }
2541 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002542 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002543 return err < 0 ? err : change;
2544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002545EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002546
Takashi Iwaid5191e52009-11-16 14:58:17 +01002547/**
2548 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2549 *
2550 * The control element is supposed to have the private_value field
2551 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002552 */
2553int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_info *uinfo)
2555{
2556 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2557 struct hda_bind_ctls *c;
2558 int err;
2559
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002560 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002561 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002562 kcontrol->private_value = *c->values;
2563 err = c->ops->info(kcontrol, uinfo);
2564 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002565 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002566 return err;
2567}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002568EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002569
Takashi Iwaid5191e52009-11-16 14:58:17 +01002570/**
2571 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2572 *
2573 * The control element is supposed to have the private_value field
2574 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2575 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002576int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2577 struct snd_ctl_elem_value *ucontrol)
2578{
2579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2580 struct hda_bind_ctls *c;
2581 int err;
2582
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002583 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002584 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002585 kcontrol->private_value = *c->values;
2586 err = c->ops->get(kcontrol, ucontrol);
2587 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002588 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002589 return err;
2590}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002591EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002592
Takashi Iwaid5191e52009-11-16 14:58:17 +01002593/**
2594 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2595 *
2596 * The control element is supposed to have the private_value field
2597 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2598 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002599int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2601{
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 struct hda_bind_ctls *c;
2604 unsigned long *vals;
2605 int err = 0, change = 0;
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 for (vals = c->values; *vals; vals++) {
2610 kcontrol->private_value = *vals;
2611 err = c->ops->put(kcontrol, ucontrol);
2612 if (err < 0)
2613 break;
2614 change |= err;
2615 }
2616 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002617 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002618 return err < 0 ? err : change;
2619}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002620EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002621
Takashi Iwaid5191e52009-11-16 14:58:17 +01002622/**
2623 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2624 *
2625 * The control element is supposed to have the private_value field
2626 * set up via HDA_BIND_VOL() macro.
2627 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002628int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2629 unsigned int size, unsigned int __user *tlv)
2630{
2631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2632 struct hda_bind_ctls *c;
2633 int err;
2634
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002635 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002636 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002637 kcontrol->private_value = *c->values;
2638 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2639 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002640 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002641 return err;
2642}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002643EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002644
2645struct hda_ctl_ops snd_hda_bind_vol = {
2646 .info = snd_hda_mixer_amp_volume_info,
2647 .get = snd_hda_mixer_amp_volume_get,
2648 .put = snd_hda_mixer_amp_volume_put,
2649 .tlv = snd_hda_mixer_amp_tlv
2650};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002651EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002652
2653struct hda_ctl_ops snd_hda_bind_sw = {
2654 .info = snd_hda_mixer_amp_switch_info,
2655 .get = snd_hda_mixer_amp_switch_get,
2656 .put = snd_hda_mixer_amp_switch_put,
2657 .tlv = snd_hda_mixer_amp_tlv
2658};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002659EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002660
2661/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * SPDIF out controls
2663 */
2664
Takashi Iwai0ba21762007-04-16 11:29:14 +02002665static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2669 uinfo->count = 1;
2670 return 0;
2671}
2672
Takashi Iwai0ba21762007-04-16 11:29:14 +02002673static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2677 IEC958_AES0_NONAUDIO |
2678 IEC958_AES0_CON_EMPHASIS_5015 |
2679 IEC958_AES0_CON_NOT_COPYRIGHT;
2680 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2681 IEC958_AES1_CON_ORIGINAL;
2682 return 0;
2683}
2684
Takashi Iwai0ba21762007-04-16 11:29:14 +02002685static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
2688 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2689 IEC958_AES0_NONAUDIO |
2690 IEC958_AES0_PRO_EMPHASIS_5015;
2691 return 0;
2692}
2693
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002698 int idx = kcontrol->private_value;
2699 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Stephen Warren7c935972011-06-01 11:14:17 -06002701 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2702 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2703 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2704 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 return 0;
2707}
2708
2709/* convert from SPDIF status bits to HDA SPDIF bits
2710 * bit 0 (DigEn) is always set zero (to be filled later)
2711 */
2712static unsigned short convert_from_spdif_status(unsigned int sbits)
2713{
2714 unsigned short val = 0;
2715
2716 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002719 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2722 IEC958_AES0_PRO_EMPHASIS_5015)
2723 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002725 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2726 IEC958_AES0_CON_EMPHASIS_5015)
2727 val |= AC_DIG1_EMPHASIS;
2728 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2729 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002731 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2733 }
2734 return val;
2735}
2736
2737/* convert to SPDIF status bits from HDA SPDIF bits
2738 */
2739static unsigned int convert_to_spdif_status(unsigned short val)
2740{
2741 unsigned int sbits = 0;
2742
Takashi Iwai0ba21762007-04-16 11:29:14 +02002743 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002745 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 sbits |= IEC958_AES0_PROFESSIONAL;
2747 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002748 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2750 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002751 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002753 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002755 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2757 sbits |= val & (0x7f << 8);
2758 }
2759 return sbits;
2760}
2761
Takashi Iwai2f728532008-09-25 16:32:41 +02002762/* set digital convert verbs both for the given NID and its slaves */
2763static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2764 int verb, int val)
2765{
Takashi Iwaidda14412011-05-02 11:29:30 +02002766 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002767
Takashi Iwai9e976972008-11-25 08:17:20 +01002768 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002769 d = codec->slave_dig_outs;
2770 if (!d)
2771 return;
2772 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002773 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002774}
2775
2776static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2777 int dig1, int dig2)
2778{
2779 if (dig1 != -1)
2780 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2781 if (dig2 != -1)
2782 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2783}
2784
Takashi Iwai0ba21762007-04-16 11:29:14 +02002785static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002789 int idx = kcontrol->private_value;
2790 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2791 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 unsigned short val;
2793 int change;
2794
Ingo Molnar62932df2006-01-16 16:34:20 +01002795 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002796 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2798 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2799 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002800 val = convert_from_spdif_status(spdif->status);
2801 val |= spdif->ctls & 1;
2802 change = spdif->ctls != val;
2803 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002804 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002805 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002806 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return change;
2808}
2809
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002810#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Takashi Iwai0ba21762007-04-16 11:29:14 +02002812static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2813 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
2815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002816 int idx = kcontrol->private_value;
2817 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Stephen Warren7c935972011-06-01 11:14:17 -06002819 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return 0;
2821}
2822
Stephen Warren74b654c2011-06-01 11:14:18 -06002823static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2824 int dig1, int dig2)
2825{
2826 set_dig_out_convert(codec, nid, dig1, dig2);
2827 /* unmute amp switch (if any) */
2828 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2829 (dig1 & AC_DIG1_ENABLE))
2830 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2831 HDA_AMP_MUTE, 0);
2832}
2833
Takashi Iwai0ba21762007-04-16 11:29:14 +02002834static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002838 int idx = kcontrol->private_value;
2839 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2840 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 unsigned short val;
2842 int change;
2843
Ingo Molnar62932df2006-01-16 16:34:20 +01002844 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c935972011-06-01 11:14:17 -06002845 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002848 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002849 spdif->ctls = val;
2850 if (change && nid != (u16)-1)
2851 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002852 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return change;
2854}
2855
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002856static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 {
2858 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002860 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 .info = snd_hda_spdif_mask_info,
2862 .get = snd_hda_spdif_cmask_get,
2863 },
2864 {
2865 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002867 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 .info = snd_hda_spdif_mask_info,
2869 .get = snd_hda_spdif_pmask_get,
2870 },
2871 {
2872 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002873 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 .info = snd_hda_spdif_mask_info,
2875 .get = snd_hda_spdif_default_get,
2876 .put = snd_hda_spdif_default_put,
2877 },
2878 {
2879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002880 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 .info = snd_hda_spdif_out_switch_info,
2882 .get = snd_hda_spdif_out_switch_get,
2883 .put = snd_hda_spdif_out_switch_put,
2884 },
2885 { } /* end */
2886};
2887
2888/**
2889 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2890 * @codec: the HDA codec
2891 * @nid: audio out widget NID
2892 *
2893 * Creates controls related with the SPDIF output.
2894 * Called from each patch supporting the SPDIF out.
2895 *
2896 * Returns 0 if successful, or a negative error code.
2897 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002898int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2899 hda_nid_t associated_nid,
2900 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002903 struct snd_kcontrol *kctl;
2904 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002905 int idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002906 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Takashi Iwai1afe2062010-12-23 10:17:52 +01002908 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2909 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002910 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2911 return -EBUSY;
2912 }
Stephen Warren7c935972011-06-01 11:14:17 -06002913 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2915 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002916 if (!kctl)
2917 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002918 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002919 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002920 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return err;
2923 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002924 spdif->nid = cvt_nid;
2925 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06002926 AC_VERB_GET_DIGI_CONVERT_1, 0);
2927 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002930EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Stephen Warren7c935972011-06-01 11:14:17 -06002932struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2933 hda_nid_t nid)
2934{
2935 int i;
2936 for (i = 0; i < codec->spdif_out.used; i++) {
2937 struct hda_spdif_out *spdif =
2938 snd_array_elem(&codec->spdif_out, i);
2939 if (spdif->nid == nid)
2940 return spdif;
2941 }
2942 return NULL;
2943}
2944EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2945
Stephen Warren74b654c2011-06-01 11:14:18 -06002946void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2947{
2948 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2949
2950 mutex_lock(&codec->spdif_mutex);
2951 spdif->nid = (u16)-1;
2952 mutex_unlock(&codec->spdif_mutex);
2953}
2954EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2955
2956void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2957{
2958 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2959 unsigned short val;
2960
2961 mutex_lock(&codec->spdif_mutex);
2962 if (spdif->nid != nid) {
2963 spdif->nid = nid;
2964 val = spdif->ctls;
2965 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2966 }
2967 mutex_unlock(&codec->spdif_mutex);
2968}
2969EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002972 * SPDIF sharing with analog output
2973 */
2974static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2976{
2977 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2978 ucontrol->value.integer.value[0] = mout->share_spdif;
2979 return 0;
2980}
2981
2982static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2984{
2985 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2986 mout->share_spdif = !!ucontrol->value.integer.value[0];
2987 return 0;
2988}
2989
2990static struct snd_kcontrol_new spdif_share_sw = {
2991 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2992 .name = "IEC958 Default PCM Playback Switch",
2993 .info = snd_ctl_boolean_mono_info,
2994 .get = spdif_share_sw_get,
2995 .put = spdif_share_sw_put,
2996};
2997
Takashi Iwaid5191e52009-11-16 14:58:17 +01002998/**
2999 * snd_hda_create_spdif_share_sw - create Default PCM switch
3000 * @codec: the HDA codec
3001 * @mout: multi-out instance
3002 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003003int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3004 struct hda_multi_out *mout)
3005{
3006 if (!mout->dig_out_nid)
3007 return 0;
3008 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003009 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3010 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003011}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003012EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003013
3014/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 * SPDIF input
3016 */
3017
3018#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3019
Takashi Iwai0ba21762007-04-16 11:29:14 +02003020static int snd_hda_spdif_in_switch_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
3025 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3026 return 0;
3027}
3028
Takashi Iwai0ba21762007-04-16 11:29:14 +02003029static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3033 hda_nid_t nid = kcontrol->private_value;
3034 unsigned int val = !!ucontrol->value.integer.value[0];
3035 int change;
3036
Ingo Molnar62932df2006-01-16 16:34:20 +01003037 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003039 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003041 snd_hda_codec_write_cache(codec, nid, 0,
3042 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003044 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return change;
3046}
3047
Takashi Iwai0ba21762007-04-16 11:29:14 +02003048static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3049 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3052 hda_nid_t nid = kcontrol->private_value;
3053 unsigned short val;
3054 unsigned int sbits;
3055
Andrew Paprocki3982d172007-12-19 12:13:44 +01003056 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 sbits = convert_to_spdif_status(val);
3058 ucontrol->value.iec958.status[0] = sbits;
3059 ucontrol->value.iec958.status[1] = sbits >> 8;
3060 ucontrol->value.iec958.status[2] = sbits >> 16;
3061 ucontrol->value.iec958.status[3] = sbits >> 24;
3062 return 0;
3063}
3064
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003065static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 {
3067 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003068 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 .info = snd_hda_spdif_in_switch_info,
3070 .get = snd_hda_spdif_in_switch_get,
3071 .put = snd_hda_spdif_in_switch_put,
3072 },
3073 {
3074 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003076 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 .info = snd_hda_spdif_mask_info,
3078 .get = snd_hda_spdif_in_status_get,
3079 },
3080 { } /* end */
3081};
3082
3083/**
3084 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3085 * @codec: the HDA codec
3086 * @nid: audio in widget NID
3087 *
3088 * Creates controls related with the SPDIF input.
3089 * Called from each patch supporting the SPDIF in.
3090 *
3091 * Returns 0 if successful, or a negative error code.
3092 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003093int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003096 struct snd_kcontrol *kctl;
3097 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003098 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwai1afe2062010-12-23 10:17:52 +01003100 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3101 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003102 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3103 return -EBUSY;
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3106 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003107 if (!kctl)
3108 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003110 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003111 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return err;
3113 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003114 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003115 snd_hda_codec_read(codec, nid, 0,
3116 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return 0;
3119}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003120EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Takashi Iwai2a439522011-07-26 09:52:50 +02003122#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003123/*
3124 * command cache
3125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003127/* build a 32bit cache key with the widget id and the command parameter */
3128#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3129#define get_cmd_cache_nid(key) ((key) & 0xff)
3130#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3131
3132/**
3133 * snd_hda_codec_write_cache - send a single command with caching
3134 * @codec: the HDA codec
3135 * @nid: NID to send the command
3136 * @direct: direct flag
3137 * @verb: the verb to send
3138 * @parm: the parameter for the verb
3139 *
3140 * Send a single command without waiting for response.
3141 *
3142 * Returns 0 if successful, or a negative error code.
3143 */
3144int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3145 int direct, unsigned int verb, unsigned int parm)
3146{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003147 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3148 struct hda_cache_head *c;
3149 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003150
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003151 if (err < 0)
3152 return err;
3153 /* parm may contain the verb stuff for get/set amp */
3154 verb = verb | (parm >> 8);
3155 parm &= 0xff;
3156 key = build_cmd_cache_key(nid, verb);
3157 mutex_lock(&codec->bus->cmd_mutex);
3158 c = get_alloc_hash(&codec->cmd_cache, key);
3159 if (c)
3160 c->val = parm;
3161 mutex_unlock(&codec->bus->cmd_mutex);
3162 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003164EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003165
Takashi Iwaid5191e52009-11-16 14:58:17 +01003166/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003167 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3168 * @codec: the HDA codec
3169 * @nid: NID to send the command
3170 * @direct: direct flag
3171 * @verb: the verb to send
3172 * @parm: the parameter for the verb
3173 *
3174 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3175 * command if the parameter is already identical with the cached value.
3176 * If not, it sends the command and refreshes the cache.
3177 *
3178 * Returns 0 if successful, or a negative error code.
3179 */
3180int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3181 int direct, unsigned int verb, unsigned int parm)
3182{
3183 struct hda_cache_head *c;
3184 u32 key;
3185
3186 /* parm may contain the verb stuff for get/set amp */
3187 verb = verb | (parm >> 8);
3188 parm &= 0xff;
3189 key = build_cmd_cache_key(nid, verb);
3190 mutex_lock(&codec->bus->cmd_mutex);
3191 c = get_hash(&codec->cmd_cache, key);
3192 if (c && c->val == parm) {
3193 mutex_unlock(&codec->bus->cmd_mutex);
3194 return 0;
3195 }
3196 mutex_unlock(&codec->bus->cmd_mutex);
3197 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3198}
3199EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3200
3201/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003202 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3203 * @codec: HD-audio codec
3204 *
3205 * Execute all verbs recorded in the command caches to resume.
3206 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003207void snd_hda_codec_resume_cache(struct hda_codec *codec)
3208{
Takashi Iwai603c4012008-07-30 15:01:44 +02003209 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003210 int i;
3211
Takashi Iwai603c4012008-07-30 15:01:44 +02003212 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003213 u32 key = buffer->key;
3214 if (!key)
3215 continue;
3216 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3217 get_cmd_cache_cmd(key), buffer->val);
3218 }
3219}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003220EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003221
3222/**
3223 * snd_hda_sequence_write_cache - sequence writes with caching
3224 * @codec: the HDA codec
3225 * @seq: VERB array to send
3226 *
3227 * Send the commands sequentially from the given array.
3228 * Thte commands are recorded on cache for power-save and resume.
3229 * The array must be terminated with NID=0.
3230 */
3231void snd_hda_sequence_write_cache(struct hda_codec *codec,
3232 const struct hda_verb *seq)
3233{
3234 for (; seq->nid; seq++)
3235 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3236 seq->param);
3237}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003238EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003239#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003240
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003241void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3242 unsigned int power_state,
3243 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003244{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003245 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003246 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003247
Takashi Iwaicb53c622007-08-10 17:21:45 +02003248 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003249 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003250 if (!(wcaps & AC_WCAP_POWER))
3251 continue;
3252 /* don't power down the widget if it controls eapd and
3253 * EAPD_BTLENABLE is set.
3254 */
3255 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3256 get_wcaps_type(wcaps) == AC_WID_PIN &&
3257 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3258 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003259 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003260 if (eapd & 0x02)
3261 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003262 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003263 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3264 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003265 }
3266
Takashi Iwaicb53c622007-08-10 17:21:45 +02003267 if (power_state == AC_PWRST_D0) {
3268 unsigned long end_time;
3269 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003270 /* wait until the codec reachs to D0 */
3271 end_time = jiffies + msecs_to_jiffies(500);
3272 do {
3273 state = snd_hda_codec_read(codec, fg, 0,
3274 AC_VERB_GET_POWER_STATE, 0);
3275 if (state == power_state)
3276 break;
3277 msleep(1);
3278 } while (time_after_eq(end_time, jiffies));
3279 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003280}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003281EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3282
3283/*
3284 * set power state of the codec
3285 */
3286static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3287 unsigned int power_state)
3288{
3289 if (codec->patch_ops.set_power_state) {
3290 codec->patch_ops.set_power_state(codec, fg, power_state);
3291 return;
3292 }
3293
3294 /* this delay seems necessary to avoid click noise at power-down */
3295 if (power_state == AC_PWRST_D3)
3296 msleep(100);
3297 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3298 power_state);
3299 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3300}
Takashi Iwai54d17402005-11-21 16:33:22 +01003301
Takashi Iwai11aeff02008-07-30 15:01:46 +02003302#ifdef CONFIG_SND_HDA_HWDEP
3303/* execute additional init verbs */
3304static void hda_exec_init_verbs(struct hda_codec *codec)
3305{
3306 if (codec->init_verbs.list)
3307 snd_hda_sequence_write(codec, codec->init_verbs.list);
3308}
3309#else
3310static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3311#endif
3312
Takashi Iwai2a439522011-07-26 09:52:50 +02003313#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003314/*
3315 * call suspend and power-down; used both from PM and power-save
3316 */
3317static void hda_call_codec_suspend(struct hda_codec *codec)
3318{
3319 if (codec->patch_ops.suspend)
3320 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003321 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003322 hda_set_power_state(codec,
3323 codec->afg ? codec->afg : codec->mfg,
3324 AC_PWRST_D3);
3325#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003326 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003327 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003328 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003329 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003330 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003331#endif
3332}
3333
3334/*
3335 * kick up codec; used both from PM and power-save
3336 */
3337static void hda_call_codec_resume(struct hda_codec *codec)
3338{
3339 hda_set_power_state(codec,
3340 codec->afg ? codec->afg : codec->mfg,
3341 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003342 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003343 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003344 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003345 if (codec->patch_ops.resume)
3346 codec->patch_ops.resume(codec);
3347 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003348 if (codec->patch_ops.init)
3349 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003350 snd_hda_codec_resume_amp(codec);
3351 snd_hda_codec_resume_cache(codec);
3352 }
3353}
Takashi Iwai2a439522011-07-26 09:52:50 +02003354#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003355
Takashi Iwai54d17402005-11-21 16:33:22 +01003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357/**
3358 * snd_hda_build_controls - build mixer controls
3359 * @bus: the BUS
3360 *
3361 * Creates mixer controls for each codec included in the bus.
3362 *
3363 * Returns 0 if successful, otherwise a negative error code.
3364 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003365int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003367 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003370 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003371 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003372 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003373 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003374 err = snd_hda_codec_reset(codec);
3375 if (err < 0) {
3376 printk(KERN_ERR
3377 "hda_codec: cannot revert codec\n");
3378 return err;
3379 }
3380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003382 return 0;
3383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003384EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003386int snd_hda_codec_build_controls(struct hda_codec *codec)
3387{
3388 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003389 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003390 /* continue to initialize... */
3391 if (codec->patch_ops.init)
3392 err = codec->patch_ops.init(codec);
3393 if (!err && codec->patch_ops.build_controls)
3394 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003395 if (err < 0)
3396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return 0;
3398}
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400/*
3401 * stream formats
3402 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003403struct hda_rate_tbl {
3404 unsigned int hz;
3405 unsigned int alsa_bits;
3406 unsigned int hda_fmt;
3407};
3408
Takashi Iwai92f10b32010-08-03 14:21:00 +02003409/* rate = base * mult / div */
3410#define HDA_RATE(base, mult, div) \
3411 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3412 (((div) - 1) << AC_FMT_DIV_SHIFT))
3413
Takashi Iwaibefdf312005-08-22 13:57:55 +02003414static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003416
3417 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003418 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3419 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3420 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3421 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3422 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3423 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3424 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3425 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3426 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3427 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3428 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003429#define AC_PAR_PCM_RATE_BITS 11
3430 /* up to bits 10, 384kHZ isn't supported properly */
3431
3432 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003433 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003434
Takashi Iwaibefdf312005-08-22 13:57:55 +02003435 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436};
3437
3438/**
3439 * snd_hda_calc_stream_format - calculate format bitset
3440 * @rate: the sample rate
3441 * @channels: the number of channels
3442 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3443 * @maxbps: the max. bps
3444 *
3445 * Calculate the format bitset from the given rate, channels and th PCM format.
3446 *
3447 * Return zero if invalid.
3448 */
3449unsigned int snd_hda_calc_stream_format(unsigned int rate,
3450 unsigned int channels,
3451 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003452 unsigned int maxbps,
3453 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455 int i;
3456 unsigned int val = 0;
3457
Takashi Iwaibefdf312005-08-22 13:57:55 +02003458 for (i = 0; rate_bits[i].hz; i++)
3459 if (rate_bits[i].hz == rate) {
3460 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 break;
3462 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 snd_printdd("invalid rate %d\n", rate);
3465 return 0;
3466 }
3467
3468 if (channels == 0 || channels > 8) {
3469 snd_printdd("invalid channels %d\n", channels);
3470 return 0;
3471 }
3472 val |= channels - 1;
3473
3474 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003475 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003476 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003477 break;
3478 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003479 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 case 20:
3482 case 24:
3483 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003484 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003485 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003487 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003489 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 break;
3491 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003492 snd_printdd("invalid format width %d\n",
3493 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return 0;
3495 }
3496
Anssi Hannula32c168c2010-08-03 13:28:57 +03003497 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003498 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return val;
3501}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003502EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003504static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3505{
3506 unsigned int val = 0;
3507 if (nid != codec->afg &&
3508 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3509 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3510 if (!val || val == -1)
3511 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3512 if (!val || val == -1)
3513 return 0;
3514 return val;
3515}
3516
3517static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3518{
3519 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3520 get_pcm_param);
3521}
3522
3523static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3524{
3525 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3526 if (!streams || streams == -1)
3527 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3528 if (!streams || streams == -1)
3529 return 0;
3530 return streams;
3531}
3532
3533static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3534{
3535 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3536 get_stream_param);
3537}
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539/**
3540 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3541 * @codec: the HDA codec
3542 * @nid: NID to query
3543 * @ratesp: the pointer to store the detected rate bitflags
3544 * @formatsp: the pointer to store the detected formats
3545 * @bpsp: the pointer to store the detected format widths
3546 *
3547 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3548 * or @bsps argument is ignored.
3549 *
3550 * Returns 0 if successful, otherwise a negative error code.
3551 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003552int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3554{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003555 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003557 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003558 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 if (ratesp) {
3561 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003562 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003564 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003566 if (rates == 0) {
3567 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3568 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3569 nid, val,
3570 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3571 return -EIO;
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 *ratesp = rates;
3574 }
3575
3576 if (formatsp || bpsp) {
3577 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003578 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003580 streams = query_stream_param(codec, nid);
3581 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 bps = 0;
3585 if (streams & AC_SUPFMT_PCM) {
3586 if (val & AC_SUPPCM_BITS_8) {
3587 formats |= SNDRV_PCM_FMTBIT_U8;
3588 bps = 8;
3589 }
3590 if (val & AC_SUPPCM_BITS_16) {
3591 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3592 bps = 16;
3593 }
3594 if (wcaps & AC_WCAP_DIGITAL) {
3595 if (val & AC_SUPPCM_BITS_32)
3596 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3597 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3598 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3599 if (val & AC_SUPPCM_BITS_24)
3600 bps = 24;
3601 else if (val & AC_SUPPCM_BITS_20)
3602 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003603 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3604 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3606 if (val & AC_SUPPCM_BITS_32)
3607 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 else if (val & AC_SUPPCM_BITS_24)
3609 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003610 else if (val & AC_SUPPCM_BITS_20)
3611 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
3613 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003614 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003616 if (!bps)
3617 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003618 }
3619 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003620 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* temporary hack: we have still no proper support
3622 * for the direct AC3 stream...
3623 */
3624 formats |= SNDRV_PCM_FMTBIT_U8;
3625 bps = 8;
3626 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003627 if (formats == 0) {
3628 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3629 "(nid=0x%x, val=0x%x, ovrd=%i, "
3630 "streams=0x%x)\n",
3631 nid, val,
3632 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3633 streams);
3634 return -EIO;
3635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if (formatsp)
3637 *formatsp = formats;
3638 if (bpsp)
3639 *bpsp = bps;
3640 }
3641
3642 return 0;
3643}
Stephen Warren384a48d2011-06-01 11:14:21 -06003644EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003647 * snd_hda_is_supported_format - Check the validity of the format
3648 * @codec: HD-audio codec
3649 * @nid: NID to check
3650 * @format: the HD-audio format value to check
3651 *
3652 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 *
3654 * Returns 1 if supported, 0 if not.
3655 */
3656int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3657 unsigned int format)
3658{
3659 int i;
3660 unsigned int val = 0, rate, stream;
3661
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003662 val = query_pcm_param(codec, nid);
3663 if (!val)
3664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
3666 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003667 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003668 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if (val & (1 << i))
3670 break;
3671 return 0;
3672 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003673 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return 0;
3675
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003676 stream = query_stream_param(codec, nid);
3677 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
3679
3680 if (stream & AC_SUPFMT_PCM) {
3681 switch (format & 0xf0) {
3682 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003683 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return 0;
3685 break;
3686 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003687 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return 0;
3689 break;
3690 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003691 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return 0;
3693 break;
3694 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003695 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return 0;
3697 break;
3698 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003699 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return 0;
3701 break;
3702 default:
3703 return 0;
3704 }
3705 } else {
3706 /* FIXME: check for float32 and AC3? */
3707 }
3708
3709 return 1;
3710}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003711EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713/*
3714 * PCM stuff
3715 */
3716static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3717 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003718 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
3720 return 0;
3721}
3722
3723static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3724 struct hda_codec *codec,
3725 unsigned int stream_tag,
3726 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003727 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3730 return 0;
3731}
3732
3733static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3734 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003735 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Takashi Iwai888afa12008-03-18 09:57:50 +01003737 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 return 0;
3739}
3740
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003741static int set_pcm_default_values(struct hda_codec *codec,
3742 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003744 int err;
3745
Takashi Iwai0ba21762007-04-16 11:29:14 +02003746 /* query support PCM information from the given NID */
3747 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003748 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003749 info->rates ? NULL : &info->rates,
3750 info->formats ? NULL : &info->formats,
3751 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003752 if (err < 0)
3753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
3755 if (info->ops.open == NULL)
3756 info->ops.open = hda_pcm_default_open_close;
3757 if (info->ops.close == NULL)
3758 info->ops.close = hda_pcm_default_open_close;
3759 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003760 if (snd_BUG_ON(!info->nid))
3761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 info->ops.prepare = hda_pcm_default_prepare;
3763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003765 if (snd_BUG_ON(!info->nid))
3766 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 info->ops.cleanup = hda_pcm_default_cleanup;
3768 }
3769 return 0;
3770}
3771
Takashi Iwaieb541332010-08-06 13:48:11 +02003772/*
3773 * codec prepare/cleanup entries
3774 */
3775int snd_hda_codec_prepare(struct hda_codec *codec,
3776 struct hda_pcm_stream *hinfo,
3777 unsigned int stream,
3778 unsigned int format,
3779 struct snd_pcm_substream *substream)
3780{
3781 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003782 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003783 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3784 if (ret >= 0)
3785 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003786 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003787 return ret;
3788}
3789EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3790
3791void snd_hda_codec_cleanup(struct hda_codec *codec,
3792 struct hda_pcm_stream *hinfo,
3793 struct snd_pcm_substream *substream)
3794{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003795 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003796 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003797 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003798}
3799EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3800
Takashi Iwaid5191e52009-11-16 14:58:17 +01003801/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003802const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3803 "Audio", "SPDIF", "HDMI", "Modem"
3804};
3805
Takashi Iwai176d5332008-07-30 15:01:44 +02003806/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003807 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003808 *
3809 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003810 */
3811static int get_empty_pcm_device(struct hda_bus *bus, int type)
3812{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003813 /* audio device indices; not linear to keep compatibility */
3814 static int audio_idx[HDA_PCM_NTYPES][5] = {
3815 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3816 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003817 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003818 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003819 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003820 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003821
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003822 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003823 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3824 return -EINVAL;
3825 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003826
3827 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3828 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3829 return audio_idx[type][i];
3830
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003831 snd_printk(KERN_WARNING "Too many %s devices\n",
3832 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003833 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003834}
3835
3836/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003837 * attach a new PCM stream
3838 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003839static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003840{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003841 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003842 struct hda_pcm_stream *info;
3843 int stream, err;
3844
Takashi Iwaib91f0802008-11-04 08:43:08 +01003845 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003846 return -EINVAL;
3847 for (stream = 0; stream < 2; stream++) {
3848 info = &pcm->stream[stream];
3849 if (info->substreams) {
3850 err = set_pcm_default_values(codec, info);
3851 if (err < 0)
3852 return err;
3853 }
3854 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003855 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003856}
3857
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003858/* assign all PCMs of the given codec */
3859int snd_hda_codec_build_pcms(struct hda_codec *codec)
3860{
3861 unsigned int pcm;
3862 int err;
3863
3864 if (!codec->num_pcms) {
3865 if (!codec->patch_ops.build_pcms)
3866 return 0;
3867 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003868 if (err < 0) {
3869 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003870 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003871 err = snd_hda_codec_reset(codec);
3872 if (err < 0) {
3873 printk(KERN_ERR
3874 "hda_codec: cannot revert codec\n");
3875 return err;
3876 }
3877 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003878 }
3879 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3880 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3881 int dev;
3882
3883 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003884 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003885
3886 if (!cpcm->pcm) {
3887 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3888 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003889 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003890 cpcm->device = dev;
3891 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003892 if (err < 0) {
3893 printk(KERN_ERR "hda_codec: cannot attach "
3894 "PCM stream %d for codec #%d\n",
3895 dev, codec->addr);
3896 continue; /* no fatal error */
3897 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003898 }
3899 }
3900 return 0;
3901}
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903/**
3904 * snd_hda_build_pcms - build PCM information
3905 * @bus: the BUS
3906 *
3907 * Create PCM information for each codec included in the bus.
3908 *
3909 * The build_pcms codec patch is requested to set up codec->num_pcms and
3910 * codec->pcm_info properly. The array is referred by the top-level driver
3911 * to create its PCM instances.
3912 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3913 * callback.
3914 *
3915 * At least, substreams, channels_min and channels_max must be filled for
3916 * each stream. substreams = 0 indicates that the stream doesn't exist.
3917 * When rates and/or formats are zero, the supported values are queried
3918 * from the given nid. The nid is used also by the default ops.prepare
3919 * and ops.cleanup callbacks.
3920 *
3921 * The driver needs to call ops.open in its open callback. Similarly,
3922 * ops.close is supposed to be called in the close callback.
3923 * ops.prepare should be called in the prepare or hw_params callback
3924 * with the proper parameters for set up.
3925 * ops.cleanup should be called in hw_free for clean up of streams.
3926 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003927 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003929int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003931 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Takashi Iwai0ba21762007-04-16 11:29:14 +02003933 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003934 int err = snd_hda_codec_build_pcms(codec);
3935 if (err < 0)
3936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
3938 return 0;
3939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003940EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942/**
3943 * snd_hda_check_board_config - compare the current codec with the config table
3944 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003945 * @num_configs: number of config enums
3946 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * @tbl: configuration table, terminated by null entries
3948 *
3949 * Compares the modelname or PCI subsystem id of the current codec with the
3950 * given configuration table. If a matching entry is found, returns its
3951 * config value (supposed to be 0 or positive).
3952 *
3953 * If no entries are matching, the function returns a negative value.
3954 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003955int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003956 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003957 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003959 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003960 int i;
3961 for (i = 0; i < num_configs; i++) {
3962 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003963 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003964 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3965 "selected\n", models[i]);
3966 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 }
3968 }
3969 }
3970
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003971 if (!codec->bus->pci || !tbl)
3972 return -1;
3973
3974 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3975 if (!tbl)
3976 return -1;
3977 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003978#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003979 char tmp[10];
3980 const char *model = NULL;
3981 if (models)
3982 model = models[tbl->value];
3983 if (!model) {
3984 sprintf(tmp, "#%d", tbl->value);
3985 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003987 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3988 "for config %x:%x (%s)\n",
3989 model, tbl->subvendor, tbl->subdevice,
3990 (tbl->name ? tbl->name : "Unknown device"));
3991#endif
3992 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 }
3994 return -1;
3995}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003996EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003999 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004000 subsystem ID with the
4001 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004002
4003 This is important for Gateway notebooks with SB450 HDA Audio
4004 where the vendor ID of the PCI device is:
4005 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4006 and the vendor/subvendor are found only at the codec.
4007
4008 * @codec: the HDA codec
4009 * @num_configs: number of config enums
4010 * @models: array of model name strings
4011 * @tbl: configuration table, terminated by null entries
4012 *
4013 * Compares the modelname or PCI subsystem id of the current codec with the
4014 * given configuration table. If a matching entry is found, returns its
4015 * config value (supposed to be 0 or positive).
4016 *
4017 * If no entries are matching, the function returns a negative value.
4018 */
4019int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004020 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004021 const struct snd_pci_quirk *tbl)
4022{
4023 const struct snd_pci_quirk *q;
4024
4025 /* Search for codec ID */
4026 for (q = tbl; q->subvendor; q++) {
4027 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
4028
4029 if (vendorid == codec->subsystem_id)
4030 break;
4031 }
4032
4033 if (!q->subvendor)
4034 return -1;
4035
4036 tbl = q;
4037
4038 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004039#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004040 char tmp[10];
4041 const char *model = NULL;
4042 if (models)
4043 model = models[tbl->value];
4044 if (!model) {
4045 sprintf(tmp, "#%d", tbl->value);
4046 model = tmp;
4047 }
4048 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4049 "for config %x:%x (%s)\n",
4050 model, tbl->subvendor, tbl->subdevice,
4051 (tbl->name ? tbl->name : "Unknown device"));
4052#endif
4053 return tbl->value;
4054 }
4055 return -1;
4056}
4057EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4058
4059/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 * snd_hda_add_new_ctls - create controls from the array
4061 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004062 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 *
4064 * This helper function creates and add new controls in the given array.
4065 * The array must be terminated with an empty entry as terminator.
4066 *
4067 * Returns 0 if successful, or a negative error code.
4068 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004069int snd_hda_add_new_ctls(struct hda_codec *codec,
4070 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
4074 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004075 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004076 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004077 if (knew->iface == -1) /* skip this codec private value */
4078 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004079 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004080 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004081 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004082 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004083 if (addr > 0)
4084 kctl->id.device = addr;
4085 if (idx > 0)
4086 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004087 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004088 if (!err)
4089 break;
4090 /* try first with another device index corresponding to
4091 * the codec addr; if it still fails (or it's the
4092 * primary codec), then try another control index
4093 */
4094 if (!addr && codec->addr)
4095 addr = codec->addr;
4096 else if (!idx && !knew->index) {
4097 idx = find_empty_mixer_ctl_idx(codec,
4098 knew->name);
4099 if (idx <= 0)
4100 return err;
4101 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004102 return err;
4103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
4105 return 0;
4106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004107EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Takashi Iwaicb53c622007-08-10 17:21:45 +02004109#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004110static void hda_power_work(struct work_struct *work)
4111{
4112 struct hda_codec *codec =
4113 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004114 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004115
Maxim Levitsky2e492462007-09-03 15:26:57 +02004116 if (!codec->power_on || codec->power_count) {
4117 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004118 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004119 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004120
Takashi Iwaid66fee52011-08-02 15:39:31 +02004121 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004122 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004123 if (bus->ops.pm_notify)
4124 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004125}
4126
4127static void hda_keep_power_on(struct hda_codec *codec)
4128{
4129 codec->power_count++;
4130 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004131 codec->power_jiffies = jiffies;
4132}
4133
Takashi Iwaid5191e52009-11-16 14:58:17 +01004134/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004135void snd_hda_update_power_acct(struct hda_codec *codec)
4136{
4137 unsigned long delta = jiffies - codec->power_jiffies;
4138 if (codec->power_on)
4139 codec->power_on_acct += delta;
4140 else
4141 codec->power_off_acct += delta;
4142 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004143}
4144
Takashi Iwaid5191e52009-11-16 14:58:17 +01004145/**
4146 * snd_hda_power_up - Power-up the codec
4147 * @codec: HD-audio codec
4148 *
4149 * Increment the power-up counter and power up the hardware really when
4150 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004151 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004152void snd_hda_power_up(struct hda_codec *codec)
4153{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004154 struct hda_bus *bus = codec->bus;
4155
Takashi Iwaicb53c622007-08-10 17:21:45 +02004156 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004157 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004158 return;
4159
Takashi Iwaid66fee52011-08-02 15:39:31 +02004160 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004161 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004162 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004163 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004164 if (bus->ops.pm_notify)
4165 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004166 hda_call_codec_resume(codec);
4167 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004168 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004169}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004170EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004171
4172#define power_save(codec) \
4173 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004174
Takashi Iwaid5191e52009-11-16 14:58:17 +01004175/**
4176 * snd_hda_power_down - Power-down the codec
4177 * @codec: HD-audio codec
4178 *
4179 * Decrement the power-up counter and schedules the power-off work if
4180 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004181 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004182void snd_hda_power_down(struct hda_codec *codec)
4183{
4184 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004185 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004186 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004187 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004188 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004189 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004190 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004191 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004193EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004194
Takashi Iwaid5191e52009-11-16 14:58:17 +01004195/**
4196 * snd_hda_check_amp_list_power - Check the amp list and update the power
4197 * @codec: HD-audio codec
4198 * @check: the object containing an AMP list and the status
4199 * @nid: NID to check / update
4200 *
4201 * Check whether the given NID is in the amp list. If it's in the list,
4202 * check the current AMP status, and update the the power-status according
4203 * to the mute status.
4204 *
4205 * This function is supposed to be set or called from the check_power_status
4206 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004207 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004208int snd_hda_check_amp_list_power(struct hda_codec *codec,
4209 struct hda_loopback_check *check,
4210 hda_nid_t nid)
4211{
Takashi Iwai031024e2011-05-02 11:29:30 +02004212 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004213 int ch, v;
4214
4215 if (!check->amplist)
4216 return 0;
4217 for (p = check->amplist; p->nid; p++) {
4218 if (p->nid == nid)
4219 break;
4220 }
4221 if (!p->nid)
4222 return 0; /* nothing changed */
4223
4224 for (p = check->amplist; p->nid; p++) {
4225 for (ch = 0; ch < 2; ch++) {
4226 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4227 p->idx);
4228 if (!(v & HDA_AMP_MUTE) && v > 0) {
4229 if (!check->power_on) {
4230 check->power_on = 1;
4231 snd_hda_power_up(codec);
4232 }
4233 return 1;
4234 }
4235 }
4236 }
4237 if (check->power_on) {
4238 check->power_on = 0;
4239 snd_hda_power_down(codec);
4240 }
4241 return 0;
4242}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004243EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004246/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004247 * Channel mode helper
4248 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004249
4250/**
4251 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4252 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004253int snd_hda_ch_mode_info(struct hda_codec *codec,
4254 struct snd_ctl_elem_info *uinfo,
4255 const struct hda_channel_mode *chmode,
4256 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004257{
4258 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4259 uinfo->count = 1;
4260 uinfo->value.enumerated.items = num_chmodes;
4261 if (uinfo->value.enumerated.item >= num_chmodes)
4262 uinfo->value.enumerated.item = num_chmodes - 1;
4263 sprintf(uinfo->value.enumerated.name, "%dch",
4264 chmode[uinfo->value.enumerated.item].channels);
4265 return 0;
4266}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004267EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004268
Takashi Iwaid5191e52009-11-16 14:58:17 +01004269/**
4270 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4271 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004272int snd_hda_ch_mode_get(struct hda_codec *codec,
4273 struct snd_ctl_elem_value *ucontrol,
4274 const struct hda_channel_mode *chmode,
4275 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004276 int max_channels)
4277{
4278 int i;
4279
4280 for (i = 0; i < num_chmodes; i++) {
4281 if (max_channels == chmode[i].channels) {
4282 ucontrol->value.enumerated.item[0] = i;
4283 break;
4284 }
4285 }
4286 return 0;
4287}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004288EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004289
Takashi Iwaid5191e52009-11-16 14:58:17 +01004290/**
4291 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4292 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004293int snd_hda_ch_mode_put(struct hda_codec *codec,
4294 struct snd_ctl_elem_value *ucontrol,
4295 const struct hda_channel_mode *chmode,
4296 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004297 int *max_channelsp)
4298{
4299 unsigned int mode;
4300
4301 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004302 if (mode >= num_chmodes)
4303 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004304 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004305 return 0;
4306 /* change the current channel setting */
4307 *max_channelsp = chmode[mode].channels;
4308 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004309 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004310 return 1;
4311}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004312EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314/*
4315 * input MUX helper
4316 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004317
4318/**
4319 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4320 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004321int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4322 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
4324 unsigned int index;
4325
4326 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4327 uinfo->count = 1;
4328 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004329 if (!imux->num_items)
4330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 index = uinfo->value.enumerated.item;
4332 if (index >= imux->num_items)
4333 index = imux->num_items - 1;
4334 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4335 return 0;
4336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004337EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Takashi Iwaid5191e52009-11-16 14:58:17 +01004339/**
4340 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4341 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004342int snd_hda_input_mux_put(struct hda_codec *codec,
4343 const struct hda_input_mux *imux,
4344 struct snd_ctl_elem_value *ucontrol,
4345 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 unsigned int *cur_val)
4347{
4348 unsigned int idx;
4349
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004350 if (!imux->num_items)
4351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 idx = ucontrol->value.enumerated.item[0];
4353 if (idx >= imux->num_items)
4354 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004355 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004357 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4358 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 *cur_val = idx;
4360 return 1;
4361}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004362EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364
4365/*
4366 * Multi-channel / digital-out PCM helper functions
4367 */
4368
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004369/* setup SPDIF output stream */
4370static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4371 unsigned int stream_tag, unsigned int format)
4372{
Stephen Warren7c935972011-06-01 11:14:17 -06004373 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4374
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004375 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06004376 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004377 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004378 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004379 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004380 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004381 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004382 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004383 for (d = codec->slave_dig_outs; *d; d++)
4384 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4385 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004386 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004387 /* turn on again (if needed) */
Stephen Warren7c935972011-06-01 11:14:17 -06004388 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004389 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004390 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004391}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004392
Takashi Iwai2f728532008-09-25 16:32:41 +02004393static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4394{
4395 snd_hda_codec_cleanup_stream(codec, nid);
4396 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004397 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004398 for (d = codec->slave_dig_outs; *d; d++)
4399 snd_hda_codec_cleanup_stream(codec, *d);
4400 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004401}
4402
Takashi Iwaid5191e52009-11-16 14:58:17 +01004403/**
4404 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4405 * @bus: HD-audio bus
4406 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004407void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4408{
4409 struct hda_codec *codec;
4410
4411 if (!bus)
4412 return;
4413 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004414 if (hda_codec_is_power_on(codec) &&
4415 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004416 codec->patch_ops.reboot_notify(codec);
4417 }
4418}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004419EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004420
Takashi Iwaid5191e52009-11-16 14:58:17 +01004421/**
4422 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004424int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4425 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Ingo Molnar62932df2006-01-16 16:34:20 +01004427 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004428 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4429 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004430 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004432 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 return 0;
4434}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004435EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Takashi Iwaid5191e52009-11-16 14:58:17 +01004437/**
4438 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4439 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004440int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4441 struct hda_multi_out *mout,
4442 unsigned int stream_tag,
4443 unsigned int format,
4444 struct snd_pcm_substream *substream)
4445{
4446 mutex_lock(&codec->spdif_mutex);
4447 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4448 mutex_unlock(&codec->spdif_mutex);
4449 return 0;
4450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004451EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004452
Takashi Iwaid5191e52009-11-16 14:58:17 +01004453/**
4454 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4455 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004456int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4457 struct hda_multi_out *mout)
4458{
4459 mutex_lock(&codec->spdif_mutex);
4460 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4461 mutex_unlock(&codec->spdif_mutex);
4462 return 0;
4463}
4464EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4465
Takashi Iwaid5191e52009-11-16 14:58:17 +01004466/**
4467 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004469int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4470 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Ingo Molnar62932df2006-01-16 16:34:20 +01004472 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004474 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return 0;
4476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004477EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Takashi Iwaid5191e52009-11-16 14:58:17 +01004479/**
4480 * snd_hda_multi_out_analog_open - open analog outputs
4481 *
4482 * Open analog outputs and set up the hw-constraints.
4483 * If the digital outputs can be opened as slave, open the digital
4484 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004486int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4487 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004488 struct snd_pcm_substream *substream,
4489 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Takashi Iwai9a081602008-02-12 18:37:26 +01004491 struct snd_pcm_runtime *runtime = substream->runtime;
4492 runtime->hw.channels_max = mout->max_channels;
4493 if (mout->dig_out_nid) {
4494 if (!mout->analog_rates) {
4495 mout->analog_rates = hinfo->rates;
4496 mout->analog_formats = hinfo->formats;
4497 mout->analog_maxbps = hinfo->maxbps;
4498 } else {
4499 runtime->hw.rates = mout->analog_rates;
4500 runtime->hw.formats = mout->analog_formats;
4501 hinfo->maxbps = mout->analog_maxbps;
4502 }
4503 if (!mout->spdif_rates) {
4504 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4505 &mout->spdif_rates,
4506 &mout->spdif_formats,
4507 &mout->spdif_maxbps);
4508 }
4509 mutex_lock(&codec->spdif_mutex);
4510 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004511 if ((runtime->hw.rates & mout->spdif_rates) &&
4512 (runtime->hw.formats & mout->spdif_formats)) {
4513 runtime->hw.rates &= mout->spdif_rates;
4514 runtime->hw.formats &= mout->spdif_formats;
4515 if (mout->spdif_maxbps < hinfo->maxbps)
4516 hinfo->maxbps = mout->spdif_maxbps;
4517 } else {
4518 mout->share_spdif = 0;
4519 /* FIXME: need notify? */
4520 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004521 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004522 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4525 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4526}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004527EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Takashi Iwaid5191e52009-11-16 14:58:17 +01004529/**
4530 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4531 *
4532 * Set up the i/o for analog out.
4533 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004535int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4536 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 unsigned int stream_tag,
4538 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004539 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Takashi Iwaidda14412011-05-02 11:29:30 +02004541 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 int chs = substream->runtime->channels;
Stephen Warren7c935972011-06-01 11:14:17 -06004543 struct hda_spdif_out *spdif =
4544 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 int i;
4546
Ingo Molnar62932df2006-01-16 16:34:20 +01004547 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004548 if (mout->dig_out_nid && mout->share_spdif &&
4549 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004551 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4552 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06004553 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004555 setup_dig_out_stream(codec, mout->dig_out_nid,
4556 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 } else {
4558 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004559 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
4561 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004562 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004565 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4566 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004567 if (!mout->no_share_stream &&
4568 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004570 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4571 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004572 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004573 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4574 if (!mout->no_share_stream && mout->hp_out_nid[i])
4575 snd_hda_codec_setup_stream(codec,
4576 mout->hp_out_nid[i],
4577 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004578 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004579 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004580 snd_hda_codec_setup_stream(codec,
4581 mout->extra_out_nid[i],
4582 stream_tag, 0, format);
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* surrounds */
4585 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004586 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004587 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4588 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004589 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004590 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4591 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
4593 return 0;
4594}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004595EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Takashi Iwaid5191e52009-11-16 14:58:17 +01004597/**
4598 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004600int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4601 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Takashi Iwaidda14412011-05-02 11:29:30 +02004603 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 int i;
4605
4606 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004607 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004609 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004610 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4611 if (mout->hp_out_nid[i])
4612 snd_hda_codec_cleanup_stream(codec,
4613 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004614 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4615 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004616 snd_hda_codec_cleanup_stream(codec,
4617 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004618 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004620 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 mout->dig_out_used = 0;
4622 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004623 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 return 0;
4625}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004626EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004628/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004629 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004630 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004631
Takashi Iwaidda14412011-05-02 11:29:30 +02004632static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004633{
4634 for (; *list; list++)
4635 if (*list == nid)
4636 return 1;
4637 return 0;
4638}
4639
Steve Longerbeam81937d32007-05-08 15:33:03 +02004640
4641/*
4642 * Sort an associated group of pins according to their sequence numbers.
4643 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004644static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004645 int num_pins)
4646{
4647 int i, j;
4648 short seq;
4649 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004650
Steve Longerbeam81937d32007-05-08 15:33:03 +02004651 for (i = 0; i < num_pins; i++) {
4652 for (j = i + 1; j < num_pins; j++) {
4653 if (sequences[i] > sequences[j]) {
4654 seq = sequences[i];
4655 sequences[i] = sequences[j];
4656 sequences[j] = seq;
4657 nid = pins[i];
4658 pins[i] = pins[j];
4659 pins[j] = nid;
4660 }
4661 }
4662 }
4663}
4664
4665
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004666/* add the found input-pin to the cfg->inputs[] table */
4667static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4668 int type)
4669{
4670 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4671 cfg->inputs[cfg->num_inputs].pin = nid;
4672 cfg->inputs[cfg->num_inputs].type = type;
4673 cfg->num_inputs++;
4674 }
4675}
4676
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004677/* sort inputs in the order of AUTO_PIN_* type */
4678static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4679{
4680 int i, j;
4681
4682 for (i = 0; i < cfg->num_inputs; i++) {
4683 for (j = i + 1; j < cfg->num_inputs; j++) {
4684 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4685 struct auto_pin_cfg_item tmp;
4686 tmp = cfg->inputs[i];
4687 cfg->inputs[i] = cfg->inputs[j];
4688 cfg->inputs[j] = tmp;
4689 }
4690 }
4691 }
4692}
4693
Takashi Iwai82bc9552006-03-21 11:24:42 +01004694/*
4695 * Parse all pin widgets and store the useful pin nids to cfg
4696 *
4697 * The number of line-outs or any primary output is stored in line_outs,
4698 * and the corresponding output pins are assigned to line_out_pins[],
4699 * in the order of front, rear, CLFE, side, ...
4700 *
4701 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004702 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004703 * is detected, one of speaker of HP pins is assigned as the primary
4704 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4705 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004706 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004707 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004708 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4709 * respectively.
4710 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004711int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4712 struct auto_pin_cfg *cfg,
4713 const hda_nid_t *ignore_nids,
4714 unsigned int cond_flags)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004715{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004716 hda_nid_t nid, end_nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004717 short seq, assoc_line_out;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004718 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4719 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004720 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004721 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004722
4723 memset(cfg, 0, sizeof(*cfg));
4724
Steve Longerbeam81937d32007-05-08 15:33:03 +02004725 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4726 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004727 memset(sequences_hp, 0, sizeof(sequences_hp));
Takashi Iwai965f1b22011-08-19 09:10:29 +02004728 assoc_line_out = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004729
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004730 end_nid = codec->start_nid + codec->num_nodes;
4731 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004732 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004733 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004734 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004735 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004736
4737 /* read all default configuration for pin complex */
4738 if (wid_type != AC_WID_PIN)
4739 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004740 /* ignore the given nids (e.g. pc-beep returns error) */
4741 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4742 continue;
4743
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004744 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004745 conn = get_defcfg_connect(def_conf);
4746 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004747 continue;
4748 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004749 dev = get_defcfg_device(def_conf);
4750
4751 /* workaround for buggy BIOS setups */
4752 if (dev == AC_JACK_LINE_OUT) {
4753 if (conn == AC_JACK_PORT_FIXED)
4754 dev = AC_JACK_SPEAKER;
4755 }
4756
4757 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004758 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004759 seq = get_defcfg_sequence(def_conf);
4760 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004761
4762 if (!(wid_caps & AC_WCAP_STEREO))
4763 if (!cfg->mono_out_pin)
4764 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004765 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004766 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004767 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004768 assoc_line_out = assoc;
4769 else if (assoc_line_out != assoc)
4770 continue;
4771 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4772 continue;
4773 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004774 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004775 cfg->line_outs++;
4776 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004777 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004778 seq = get_defcfg_sequence(def_conf);
4779 assoc = get_defcfg_association(def_conf);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004780 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4781 continue;
4782 cfg->speaker_pins[cfg->speaker_outs] = nid;
Takashi Iwai965f1b22011-08-19 09:10:29 +02004783 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004784 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004785 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004786 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004787 seq = get_defcfg_sequence(def_conf);
4788 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004789 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4790 continue;
4791 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004792 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004793 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004794 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004795 case AC_JACK_MIC_IN:
4796 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004797 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004798 case AC_JACK_LINE_IN:
4799 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004800 break;
4801 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004802 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004803 break;
4804 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004805 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004806 break;
4807 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004808 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004809 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4810 continue;
4811 cfg->dig_out_pins[cfg->dig_outs] = nid;
4812 cfg->dig_out_type[cfg->dig_outs] =
4813 (loc == AC_JACK_LOC_HDMI) ?
4814 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4815 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004816 break;
4817 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004818 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004819 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004820 if (loc == AC_JACK_LOC_HDMI)
4821 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4822 else
4823 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004824 break;
4825 }
4826 }
4827
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004828 /* FIX-UP:
4829 * If no line-out is defined but multiple HPs are found,
4830 * some of them might be the real line-outs.
4831 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004832 if (!cfg->line_outs && cfg->hp_outs > 1 &&
4833 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004834 int i = 0;
4835 while (i < cfg->hp_outs) {
4836 /* The real HPs should have the sequence 0x0f */
4837 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4838 i++;
4839 continue;
4840 }
4841 /* Move it to the line-out table */
4842 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4843 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4844 cfg->line_outs++;
4845 cfg->hp_outs--;
4846 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4847 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004848 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004849 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4850 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004851 memset(cfg->hp_pins + cfg->hp_outs, 0,
4852 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004853 if (!cfg->hp_outs)
4854 cfg->line_out_type = AUTO_PIN_HP_OUT;
4855
Takashi Iwai5832fcf82008-02-12 18:30:12 +01004856 }
4857
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004858 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004859 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4860 cfg->line_outs);
4861 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4862 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004863 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4864 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004865
Steve Longerbeam81937d32007-05-08 15:33:03 +02004866 /*
4867 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4868 * as a primary output
4869 */
Takashi Iwaid025feb2011-08-23 15:24:39 +02004870 if (!cfg->line_outs &&
4871 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
Steve Longerbeam81937d32007-05-08 15:33:03 +02004872 if (cfg->speaker_outs) {
4873 cfg->line_outs = cfg->speaker_outs;
4874 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4875 sizeof(cfg->speaker_pins));
4876 cfg->speaker_outs = 0;
4877 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4878 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4879 } else if (cfg->hp_outs) {
4880 cfg->line_outs = cfg->hp_outs;
4881 memcpy(cfg->line_out_pins, cfg->hp_pins,
4882 sizeof(cfg->hp_pins));
4883 cfg->hp_outs = 0;
4884 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4885 cfg->line_out_type = AUTO_PIN_HP_OUT;
4886 }
4887 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004888
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004889 /* Reorder the surround channels
4890 * ALSA sequence is front/surr/clfe/side
4891 * HDA sequence is:
4892 * 4-ch: front/surr => OK as it is
4893 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004894 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004895 */
4896 switch (cfg->line_outs) {
4897 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004898 case 4:
4899 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004900 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004901 cfg->line_out_pins[2] = nid;
4902 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004903 }
4904
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004905 sort_autocfg_input_pins(cfg);
4906
Takashi Iwai82bc9552006-03-21 11:24:42 +01004907 /*
4908 * debug prints of the parsed results
4909 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004910 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 +01004911 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4912 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004913 cfg->line_out_pins[4],
4914 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4915 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4916 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004917 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4918 cfg->speaker_outs, cfg->speaker_pins[0],
4919 cfg->speaker_pins[1], cfg->speaker_pins[2],
4920 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004921 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4922 cfg->hp_outs, cfg->hp_pins[0],
4923 cfg->hp_pins[1], cfg->hp_pins[2],
4924 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004925 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004926 if (cfg->dig_outs)
4927 snd_printd(" dig-out=0x%x/0x%x\n",
4928 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004929 snd_printd(" inputs:");
4930 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004931 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004932 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004933 cfg->inputs[i].pin);
4934 }
4935 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004936 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004937 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004938
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004939 return 0;
4940}
Takashi Iwaid025feb2011-08-23 15:24:39 +02004941EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004942
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004943int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004944{
4945 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004946 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004947 if (conn == AC_JACK_PORT_NONE)
4948 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004949 /* Windows may claim the internal mic to be BOTH, too */
4950 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004951 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004952 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004953 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004954 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004955 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004956 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004957 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004958 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004959 return INPUT_PIN_ATTR_FRONT;
4960 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004961}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004962EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004963
Takashi Iwai990061c2010-09-09 22:08:44 +02004964/**
4965 * hda_get_input_pin_label - Give a label for the given input pin
4966 *
4967 * When check_location is true, the function checks the pin location
4968 * for mic and line-in pins, and set an appropriate prefix like "Front",
4969 * "Rear", "Internal".
4970 */
4971
Takashi Iwai10a20af2010-09-09 16:28:02 +02004972const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4973 int check_location)
4974{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004975 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004976 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004977 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4978 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004979 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004980
Takashi Iwai10a20af2010-09-09 16:28:02 +02004981 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004982
4983 switch (get_defcfg_device(def_conf)) {
4984 case AC_JACK_MIC_IN:
4985 if (!check_location)
4986 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004987 attr = snd_hda_get_input_pin_attr(def_conf);
4988 if (!attr)
4989 return "None";
4990 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004991 case AC_JACK_LINE_IN:
4992 if (!check_location)
4993 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004994 attr = snd_hda_get_input_pin_attr(def_conf);
4995 if (!attr)
4996 return "None";
4997 if (attr == INPUT_PIN_ATTR_DOCK)
4998 return "Dock Line";
4999 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02005000 case AC_JACK_AUX:
5001 return "Aux";
5002 case AC_JACK_CD:
5003 return "CD";
5004 case AC_JACK_SPDIF_IN:
5005 return "SPDIF In";
5006 case AC_JACK_DIG_OTHER_IN:
5007 return "Digital In";
5008 default:
5009 return "Misc";
5010 }
5011}
5012EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
5013
Takashi Iwaia1c98512010-09-09 21:36:27 +02005014/* Check whether the location prefix needs to be added to the label.
5015 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5016 * have to put "Front" prefix to each label. In such a case, returns false.
5017 */
5018static int check_mic_location_need(struct hda_codec *codec,
5019 const struct auto_pin_cfg *cfg,
5020 int input)
5021{
5022 unsigned int defc;
5023 int i, attr, attr2;
5024
5025 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005026 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02005027 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005028 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02005029 return 1;
5030
5031 attr = 0;
5032 for (i = 0; i < cfg->num_inputs; i++) {
5033 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005034 attr2 = snd_hda_get_input_pin_attr(defc);
5035 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005036 if (attr && attr != attr2)
5037 return 1; /* different locations found */
5038 attr = attr2;
5039 }
5040 }
5041 return 0;
5042}
5043
Takashi Iwai990061c2010-09-09 22:08:44 +02005044/**
5045 * hda_get_autocfg_input_label - Get a label for the given input
5046 *
5047 * Get a label for the given input pin defined by the autocfg item.
5048 * Unlike hda_get_input_pin_label(), this function checks all inputs
5049 * defined in autocfg and avoids the redundant mic/line prefix as much as
5050 * possible.
5051 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005052const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5053 const struct auto_pin_cfg *cfg,
5054 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005055{
5056 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005057 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005058
Takashi Iwai10a20af2010-09-09 16:28:02 +02005059 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5060 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5061 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005062 if (has_multiple_pins && type == AUTO_PIN_MIC)
5063 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005064 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5065 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005066}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005067EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5068
Takashi Iwai990061c2010-09-09 22:08:44 +02005069/**
5070 * snd_hda_add_imux_item - Add an item to input_mux
5071 *
5072 * When the same label is used already in the existing items, the number
5073 * suffix is appended to the label. This label index number is stored
5074 * to type_idx when non-NULL pointer is given.
5075 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005076int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5077 int index, int *type_idx)
5078{
5079 int i, label_idx = 0;
5080 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5081 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5082 return -EINVAL;
5083 }
5084 for (i = 0; i < imux->num_items; i++) {
5085 if (!strncmp(label, imux->items[i].label, strlen(label)))
5086 label_idx++;
5087 }
5088 if (type_idx)
5089 *type_idx = label_idx;
5090 if (label_idx > 0)
5091 snprintf(imux->items[imux->num_items].label,
5092 sizeof(imux->items[imux->num_items].label),
5093 "%s %d", label, label_idx);
5094 else
5095 strlcpy(imux->items[imux->num_items].label, label,
5096 sizeof(imux->items[imux->num_items].label));
5097 imux->items[imux->num_items].index = index;
5098 imux->num_items++;
5099 return 0;
5100}
5101EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005102
Takashi Iwai4a471b72005-12-07 13:56:29 +01005103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104#ifdef CONFIG_PM
5105/*
5106 * power management
5107 */
5108
5109/**
5110 * snd_hda_suspend - suspend the codecs
5111 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 *
5113 * Returns 0 if successful.
5114 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005115int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005117 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Takashi Iwai0ba21762007-04-16 11:29:14 +02005119 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005120 if (hda_codec_is_power_on(codec))
5121 hda_call_codec_suspend(codec);
5122 if (codec->patch_ops.post_suspend)
5123 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125 return 0;
5126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005127EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129/**
5130 * snd_hda_resume - resume the codecs
5131 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 *
5133 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005134 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005135 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005136 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 */
5138int snd_hda_resume(struct hda_bus *bus)
5139{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005140 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Takashi Iwai0ba21762007-04-16 11:29:14 +02005142 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005143 if (codec->patch_ops.pre_resume)
5144 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005145 if (snd_hda_codec_needs_resume(codec))
5146 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 return 0;
5149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005150EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005151#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005152
5153/*
5154 * generic arrays
5155 */
5156
Takashi Iwaid5191e52009-11-16 14:58:17 +01005157/**
5158 * snd_array_new - get a new element from the given array
5159 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005160 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005161 * Get a new element from the given array. If it exceeds the
5162 * pre-allocated array size, re-allocate the array.
5163 *
5164 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005165 */
5166void *snd_array_new(struct snd_array *array)
5167{
5168 if (array->used >= array->alloced) {
5169 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005170 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005171 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005172 void *nlist;
5173 if (snd_BUG_ON(num >= 4096))
5174 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005175 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005176 if (!nlist)
5177 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005178 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005179 array->list = nlist;
5180 array->alloced = num;
5181 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005182 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005183}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005184EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005185
Takashi Iwaid5191e52009-11-16 14:58:17 +01005186/**
5187 * snd_array_free - free the given array elements
5188 * @array: the array object
5189 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005190void snd_array_free(struct snd_array *array)
5191{
5192 kfree(array->list);
5193 array->used = 0;
5194 array->alloced = 0;
5195 array->list = NULL;
5196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005197EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005198
Takashi Iwaid5191e52009-11-16 14:58:17 +01005199/**
5200 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5201 * @pcm: PCM caps bits
5202 * @buf: the string buffer to write
5203 * @buflen: the max buffer length
5204 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005205 * used by hda_proc.c and hda_eld.c
5206 */
5207void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5208{
5209 static unsigned int rates[] = {
5210 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5211 96000, 176400, 192000, 384000
5212 };
5213 int i, j;
5214
5215 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5216 if (pcm & (1 << i))
5217 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5218
5219 buf[j] = '\0'; /* necessary when j == 0 */
5220}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005221EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005222
Takashi Iwaid5191e52009-11-16 14:58:17 +01005223/**
5224 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5225 * @pcm: PCM caps bits
5226 * @buf: the string buffer to write
5227 * @buflen: the max buffer length
5228 *
5229 * used by hda_proc.c and hda_eld.c
5230 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005231void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5232{
5233 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5234 int i, j;
5235
5236 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5237 if (pcm & (AC_SUPPCM_BITS_8 << i))
5238 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5239
5240 buf[j] = '\0'; /* necessary when j == 0 */
5241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005242EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005243
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005244#ifdef CONFIG_SND_HDA_INPUT_JACK
5245/*
5246 * Input-jack notification support
5247 */
5248struct hda_jack_item {
5249 hda_nid_t nid;
5250 int type;
5251 struct snd_jack *jack;
5252};
5253
5254static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5255 int type)
5256{
5257 switch (type) {
5258 case SND_JACK_HEADPHONE:
5259 return "Headphone";
5260 case SND_JACK_MICROPHONE:
5261 return "Mic";
5262 case SND_JACK_LINEOUT:
5263 return "Line-out";
5264 case SND_JACK_HEADSET:
5265 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005266 case SND_JACK_VIDEOOUT:
5267 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005268 default:
5269 return "Misc";
5270 }
5271}
5272
5273static void hda_free_jack_priv(struct snd_jack *jack)
5274{
5275 struct hda_jack_item *jacks = jack->private_data;
5276 jacks->nid = 0;
5277 jacks->jack = NULL;
5278}
5279
5280int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5281 const char *name)
5282{
5283 struct hda_jack_item *jack;
5284 int err;
5285
5286 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5287 jack = snd_array_new(&codec->jacks);
5288 if (!jack)
5289 return -ENOMEM;
5290
5291 jack->nid = nid;
5292 jack->type = type;
5293 if (!name)
5294 name = get_jack_default_name(codec, nid, type);
5295 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5296 if (err < 0) {
5297 jack->nid = 0;
5298 return err;
5299 }
5300 jack->jack->private_data = jack;
5301 jack->jack->private_free = hda_free_jack_priv;
5302 return 0;
5303}
5304EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5305
5306void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5307{
5308 struct hda_jack_item *jacks = codec->jacks.list;
5309 int i;
5310
5311 if (!jacks)
5312 return;
5313
5314 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5315 unsigned int pin_ctl;
5316 unsigned int present;
5317 int type;
5318
5319 if (jacks->nid != nid)
5320 continue;
5321 present = snd_hda_jack_detect(codec, nid);
5322 type = jacks->type;
5323 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5324 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5325 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5326 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5327 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5328 }
5329 snd_jack_report(jacks->jack, present ? type : 0);
5330 }
5331}
5332EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5333
5334/* free jack instances manually when clearing/reconfiguring */
5335void snd_hda_input_jack_free(struct hda_codec *codec)
5336{
5337 if (!codec->bus->shutdown && codec->jacks.list) {
5338 struct hda_jack_item *jacks = codec->jacks.list;
5339 int i;
5340 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5341 if (jacks->jack)
5342 snd_device_free(codec->bus->card, jacks->jack);
5343 }
5344 }
5345 snd_array_free(&codec->jacks);
5346}
5347EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5348#endif /* CONFIG_SND_HDA_INPUT_JACK */
5349
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005350MODULE_DESCRIPTION("HDA codec core");
5351MODULE_LICENSE("GPL");