blob: 2a8d447c8ed6f9dc8cc9ad707276f60f3e948bf3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020034#include "hda_beep.h"
Takashi Iwai28073142007-07-27 18:58:06 +020035#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Takashi Iwaid66fee52011-08-02 15:39:31 +020037#define CREATE_TRACE_POINTS
38#include "hda_trace.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * vendor / preset table
42 */
43
44struct hda_vendor_id {
45 unsigned int id;
46 const char *name;
47};
48
49/* codec vendor labels */
50static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010051 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020052 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020053 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010055 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010056 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020057 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010058 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020059 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010060 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020061 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020063 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010064 { 0x17e8, "Chrontel" },
65 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000066 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010068 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020069 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {} /* terminator */
71};
72
Takashi Iwai1289e9e2008-11-27 15:47:11 +010073static DEFINE_MUTEX(preset_mutex);
74static LIST_HEAD(hda_preset_tables);
75
76int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
77{
78 mutex_lock(&preset_mutex);
79 list_add_tail(&preset->list, &hda_preset_tables);
80 mutex_unlock(&preset_mutex);
81 return 0;
82}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010083EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010084
85int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
86{
87 mutex_lock(&preset_mutex);
88 list_del(&preset->list);
89 mutex_unlock(&preset_mutex);
90 return 0;
91}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010092EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Takashi Iwaicb53c622007-08-10 17:21:45 +020094#ifdef CONFIG_SND_HDA_POWER_SAVE
95static void hda_power_work(struct work_struct *work);
96static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +020097#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +020098#else
99static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#endif
102
Takashi Iwaid5191e52009-11-16 14:58:17 +0100103/**
104 * snd_hda_get_jack_location - Give a location string of the jack
105 * @cfg: pin default config value
106 *
107 * Parse the pin default config value and returns the string of the
108 * jack location, e.g. "Rear", "Front", etc.
109 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400110const char *snd_hda_get_jack_location(u32 cfg)
111{
112 static char *bases[7] = {
113 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
114 };
115 static unsigned char specials_idx[] = {
116 0x07, 0x08,
117 0x17, 0x18, 0x19,
118 0x37, 0x38
119 };
120 static char *specials[] = {
121 "Rear Panel", "Drive Bar",
122 "Riser", "HDMI", "ATAPI",
123 "Mobile-In", "Mobile-Out"
124 };
125 int i;
126 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
127 if ((cfg & 0x0f) < 7)
128 return bases[cfg & 0x0f];
129 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
130 if (cfg == specials_idx[i])
131 return specials[i];
132 }
133 return "UNKNOWN";
134}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100135EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400136
Takashi Iwaid5191e52009-11-16 14:58:17 +0100137/**
138 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
139 * @cfg: pin default config value
140 *
141 * Parse the pin default config value and returns the string of the
142 * jack connectivity, i.e. external or internal connection.
143 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400144const char *snd_hda_get_jack_connectivity(u32 cfg)
145{
146 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
147
148 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100150EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400151
Takashi Iwaid5191e52009-11-16 14:58:17 +0100152/**
153 * snd_hda_get_jack_type - Give a type string of the jack
154 * @cfg: pin default config value
155 *
156 * Parse the pin default config value and returns the string of the
157 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
158 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400159const char *snd_hda_get_jack_type(u32 cfg)
160{
161 static char *jack_types[16] = {
162 "Line Out", "Speaker", "HP Out", "CD",
163 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
164 "Line In", "Aux", "Mic", "Telephony",
165 "SPDIF In", "Digitial In", "Reserved", "Other"
166 };
167
168 return jack_types[(cfg & AC_DEFCFG_DEVICE)
169 >> AC_DEFCFG_DEVICE_SHIFT];
170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100171EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400172
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100173/*
174 * Compose a 32bit command word to be sent to the HD-audio controller
175 */
176static inline unsigned int
177make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
178 unsigned int verb, unsigned int parm)
179{
180 u32 val;
181
Takashi Iwai82e1b802009-07-17 12:47:34 +0200182 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
183 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800184 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
185 codec->addr, direct, nid, verb, parm);
186 return ~0;
187 }
188
189 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100190 val |= (u32)direct << 27;
191 val |= (u32)nid << 20;
192 val |= verb << 8;
193 val |= parm;
194 return val;
195}
196
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200197/*
198 * Send and receive a verb
199 */
200static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
201 unsigned int *res)
202{
203 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200204 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200205
Wu Fengguang6430aee2009-07-17 16:49:19 +0800206 if (cmd == ~0)
207 return -1;
208
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200209 if (res)
210 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200211 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 snd_hda_power_up(codec);
213 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200214 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200216 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800217 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200218 trace_hda_get_response(codec, *res);
219 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 mutex_unlock(&bus->cmd_mutex);
221 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 if (res && *res == -1 && bus->rirb_error) {
223 if (bus->response_reset) {
224 snd_printd("hda_codec: resetting BUS due to "
225 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200226 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200227 bus->ops.bus_reset(bus);
228 }
229 goto again;
230 }
231 /* clear reset-flag when the communication gets recovered */
232 if (!err)
233 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200234 return err;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * snd_hda_codec_read - send a command and get the response
239 * @codec: the HDA codec
240 * @nid: NID to send the command
241 * @direct: direct flag
242 * @verb: the verb to send
243 * @parm: the parameter for the verb
244 *
245 * Send a single command and read the corresponding response.
246 *
247 * Returns the obtained response value, or -1 for an error.
248 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200249unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
250 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int verb, unsigned int parm)
252{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200253 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
254 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700255 if (codec_exec_verb(codec, cmd, &res))
256 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return res;
258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100259EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/**
262 * snd_hda_codec_write - send a single command without waiting for response
263 * @codec: the HDA codec
264 * @nid: NID to send the command
265 * @direct: direct flag
266 * @verb: the verb to send
267 * @parm: the parameter for the verb
268 *
269 * Send a single command without waiting for response.
270 *
271 * Returns 0 if successful, or a negative error code.
272 */
273int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
274 unsigned int verb, unsigned int parm)
275{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200276 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100277 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200278 return codec_exec_verb(codec, cmd,
279 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100281EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283/**
284 * snd_hda_sequence_write - sequence writes
285 * @codec: the HDA codec
286 * @seq: VERB array to send
287 *
288 * Send the commands sequentially from the given array.
289 * The array must be terminated with NID=0.
290 */
291void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
292{
293 for (; seq->nid; seq++)
294 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
295}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100296EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/**
299 * snd_hda_get_sub_nodes - get the range of sub nodes
300 * @codec: the HDA codec
301 * @nid: NID to parse
302 * @start_id: the pointer to store the start NID
303 *
304 * Parse the NID and store the start NID of its sub-nodes.
305 * Returns the number of sub-nodes.
306 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200307int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
308 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned int parm;
311
312 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200313 if (parm == -1)
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *start_id = (parm >> 16) & 0x7fff;
316 return (int)(parm & 0x7fff);
317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100318EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200320/* look up the cached results */
321static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
322{
323 int i, len;
324 for (i = 0; i < array->used; ) {
325 hda_nid_t *p = snd_array_elem(array, i);
326 if (nid == *p)
327 return p;
328 len = p[1];
329 i += len + 2;
330 }
331 return NULL;
332}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200335 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @codec: the HDA codec
337 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200338 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
340 * Parses the connection list of the given widget and stores the list
341 * of NIDs.
342 *
343 * Returns the number of connections, or a negative error code.
344 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200345int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
346 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347{
348 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200349 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200351 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 again:
355 /* if the connection-list is already cached, read it */
356 p = lookup_conn_list(array, nid);
357 if (p) {
358 if (listp)
359 *listp = p + 2;
360 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200362 if (snd_BUG_ON(added))
363 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200366 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367 if (len < 0)
368 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200369 err = snd_hda_override_conn_list(codec, nid, len, list);
370 if (err < 0)
371 return err;
372 added = true;
373 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200374}
Takashi Iwaidce20792011-06-24 14:10:28 +0200375EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
376
377/**
378 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * @codec: the HDA codec
380 * @nid: NID to parse
381 * @conn_list: connection list array
382 * @max_conns: max. number of connections to store
383 *
384 * Parses the connection list of the given widget and stores the list
385 * of NIDs.
386 *
387 * Returns the number of connections, or a negative error code.
388 */
389int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390 hda_nid_t *conn_list, int max_conns)
391{
Takashi Iwaidce20792011-06-24 14:10:28 +0200392 const hda_nid_t *list;
393 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200394
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200396 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200397 if (len > max_conns) {
398 snd_printk(KERN_ERR "hda_codec: "
399 "Too many connections %d for NID 0x%x\n",
400 len, nid);
401 return -EINVAL;
402 }
403 memcpy(conn_list, list, len * sizeof(hda_nid_t));
404 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405}
406EXPORT_SYMBOL_HDA(snd_hda_get_connections);
407
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200408/**
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
411 * @nid: NID to parse
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
414 *
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
418 */
419int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
420 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100423 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200425 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Takashi Iwaida3cec32008-08-08 17:12:14 +0200428 if (snd_BUG_ON(!conn_list || max_conns <= 0))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200431 wcaps = get_wcaps(codec, nid);
432 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200433 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
434 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
437 if (parm & AC_CLIST_LONG) {
438 /* long form */
439 shift = 16;
440 num_elems = 2;
441 } else {
442 /* short form */
443 shift = 8;
444 num_elems = 4;
445 }
446 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 mask = (1 << (shift-1)) - 1;
448
Takashi Iwai0ba21762007-04-16 11:29:14 +0200449 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0; /* no connection */
451
452 if (conn_len == 1) {
453 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200454 parm = snd_hda_codec_read(codec, nid, 0,
455 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200456 if (parm == -1 && codec->bus->rirb_error)
457 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 conn_list[0] = parm & mask;
459 return 1;
460 }
461
462 /* multi connection */
463 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100464 prev_nid = 0;
465 for (i = 0; i < conn_len; i++) {
466 int range_val;
467 hda_nid_t val, n;
468
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200469 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (parm == -1 && codec->bus->rirb_error)
473 return -EIO;
474 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200475 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200477 if (val == 0) {
478 snd_printk(KERN_WARNING "hda_codec: "
479 "invalid CONNECT_LIST verb %x[%i]:%x\n",
480 nid, i, parm);
481 return 0;
482 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 parm >>= shift;
484 if (range_val) {
485 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200486 if (!prev_nid || prev_nid >= val) {
487 snd_printk(KERN_WARNING "hda_codec: "
488 "invalid dep_range_val %x:%x\n",
489 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100492 for (n = prev_nid + 1; n <= val; n++) {
493 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200494 snd_printk(KERN_ERR "hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
496 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 return -EINVAL;
498 }
499 conn_list[conns++] = n;
500 }
501 } else {
502 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200503 snd_printk(KERN_ERR "hda_codec: "
504 "Too many connections %d for NID 0x%x\n",
505 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 return -EINVAL;
507 }
508 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100510 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return conns;
513}
514
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200515static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
516{
517 hda_nid_t *p = snd_array_new(array);
518 if (!p)
519 return false;
520 *p = nid;
521 return true;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200525 * snd_hda_override_conn_list - add/modify the connection-list to cache
526 * @codec: the HDA codec
527 * @nid: NID to parse
528 * @len: number of connection list entries
529 * @list: the list of connection entries
530 *
531 * Add or modify the given connection-list to the cache. If the corresponding
532 * cache already exists, invalidate it and append a new one.
533 *
534 * Returns zero or a negative error code.
535 */
536int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
537 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200539 struct snd_array *array = &codec->conn_lists;
540 hda_nid_t *p;
541 int i, old_used;
542
543 p = lookup_conn_list(array, nid);
544 if (p)
545 *p = -1; /* invalidate the old entry */
546
547 old_used = array->used;
548 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
549 goto error_add;
550 for (i = 0; i < len; i++)
551 if (!add_conn_list(array, list[i]))
552 goto error_add;
553 return 0;
554
555 error_add:
556 array->used = old_used;
557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200559EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
560
561/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200562 * snd_hda_get_conn_index - get the connection index of the given NID
563 * @codec: the HDA codec
564 * @mux: NID containing the list
565 * @nid: NID to select
566 * @recursive: 1 when searching NID recursively, otherwise 0
567 *
568 * Parses the connection list of the widget @mux and checks whether the
569 * widget @nid is present. If it is, return the connection index.
570 * Otherwise it returns -1.
571 */
572int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
573 hda_nid_t nid, int recursive)
574{
575 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
576 int i, nums;
577
578 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
579 for (i = 0; i < nums; i++)
580 if (conn[i] == nid)
581 return i;
582 if (!recursive)
583 return -1;
584 if (recursive > 5) {
585 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
586 return -1;
587 }
588 recursive++;
589 for (i = 0; i < nums; i++)
590 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
591 return i;
592 return -1;
593}
594EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/**
597 * snd_hda_queue_unsol_event - add an unsolicited event to queue
598 * @bus: the BUS
599 * @res: unsolicited event (lower 32bit of RIRB entry)
600 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
601 *
602 * Adds the given event to the queue. The events are processed in
603 * the workqueue asynchronously. Call this function in the interrupt
604 * hanlder when RIRB receives an unsolicited event.
605 *
606 * Returns 0 if successful, or a negative error code.
607 */
608int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
609{
610 struct hda_bus_unsolicited *unsol;
611 unsigned int wp;
612
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200613 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200614 unsol = bus->unsol;
615 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
617
618 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
619 unsol->wp = wp;
620
621 wp <<= 1;
622 unsol->queue[wp] = res;
623 unsol->queue[wp + 1] = res_ex;
624
Takashi Iwai6acaed32009-01-12 10:09:24 +0100625 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return 0;
628}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100629EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800632 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
David Howellsc4028952006-11-22 14:57:56 +0000634static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
David Howellsc4028952006-11-22 14:57:56 +0000636 struct hda_bus_unsolicited *unsol =
637 container_of(work, struct hda_bus_unsolicited, work);
638 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct hda_codec *codec;
640 unsigned int rp, caddr, res;
641
642 while (unsol->rp != unsol->wp) {
643 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
644 unsol->rp = rp;
645 rp <<= 1;
646 res = unsol->queue[rp];
647 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200648 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 continue;
650 codec = bus->caddr_tbl[caddr & 0x0f];
651 if (codec && codec->patch_ops.unsol_event)
652 codec->patch_ops.unsol_event(codec, res);
653 }
654}
655
656/*
657 * initialize unsolicited queue
658 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200659static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct hda_bus_unsolicited *unsol;
662
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100663 if (bus->unsol) /* already initialized */
664 return 0;
665
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200666 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200667 if (!unsol) {
668 snd_printk(KERN_ERR "hda_codec: "
669 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -ENOMEM;
671 }
David Howellsc4028952006-11-22 14:57:56 +0000672 INIT_WORK(&unsol->work, process_unsol_events);
673 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 bus->unsol = unsol;
675 return 0;
676}
677
678/*
679 * destructor
680 */
681static void snd_hda_codec_free(struct hda_codec *codec);
682
683static int snd_hda_bus_free(struct hda_bus *bus)
684{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200685 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Takashi Iwai0ba21762007-04-16 11:29:14 +0200687 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100689 if (bus->workq)
690 flush_workqueue(bus->workq);
691 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200693 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 snd_hda_codec_free(codec);
695 }
696 if (bus->ops.private_free)
697 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100698 if (bus->workq)
699 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 kfree(bus);
701 return 0;
702}
703
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100704static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100707 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return snd_hda_bus_free(bus);
709}
710
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200711#ifdef CONFIG_SND_HDA_HWDEP
712static int snd_hda_bus_dev_register(struct snd_device *device)
713{
714 struct hda_bus *bus = device->device_data;
715 struct hda_codec *codec;
716 list_for_each_entry(codec, &bus->codec_list, list) {
717 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100718 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200719 }
720 return 0;
721}
722#else
723#define snd_hda_bus_dev_register NULL
724#endif
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/**
727 * snd_hda_bus_new - create a HDA bus
728 * @card: the card entry
729 * @temp: the template for hda_bus information
730 * @busp: the pointer to store the created bus instance
731 *
732 * Returns 0 if successful, or a negative error code.
733 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100734int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200735 const struct hda_bus_template *temp,
736 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct hda_bus *bus;
739 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100740 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200741 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .dev_free = snd_hda_bus_dev_free,
743 };
744
Takashi Iwaida3cec32008-08-08 17:12:14 +0200745 if (snd_BUG_ON(!temp))
746 return -EINVAL;
747 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (busp)
751 *busp = NULL;
752
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200753 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (bus == NULL) {
755 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
756 return -ENOMEM;
757 }
758
759 bus->card = card;
760 bus->private_data = temp->private_data;
761 bus->pci = temp->pci;
762 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100763 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 bus->ops = temp->ops;
765
Ingo Molnar62932df2006-01-16 16:34:20 +0100766 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200767 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 INIT_LIST_HEAD(&bus->codec_list);
769
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100770 snprintf(bus->workq_name, sizeof(bus->workq_name),
771 "hd-audio%d", card->number);
772 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100773 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100774 snd_printk(KERN_ERR "cannot create workqueue %s\n",
775 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100776 kfree(bus);
777 return -ENOMEM;
778 }
779
Takashi Iwai0ba21762007-04-16 11:29:14 +0200780 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
781 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 snd_hda_bus_free(bus);
783 return err;
784 }
785 if (busp)
786 *busp = bus;
787 return 0;
788}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100789EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Takashi Iwai82467612007-07-27 19:15:54 +0200791#ifdef CONFIG_SND_HDA_GENERIC
792#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200793 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200794#else
795#define is_generic_config(codec) 0
796#endif
797
Takashi Iwai645f10c2008-11-28 15:07:37 +0100798#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100799#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
800#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100801#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100802#endif
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/*
805 * find a matching codec preset
806 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200807static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200808find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100810 struct hda_codec_preset_list *tbl;
811 const struct hda_codec_preset *preset;
812 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Takashi Iwai82467612007-07-27 19:15:54 +0200814 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100815 return NULL; /* use the generic parser */
816
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 again:
818 mutex_lock(&preset_mutex);
819 list_for_each_entry(tbl, &hda_preset_tables, list) {
820 if (!try_module_get(tbl->owner)) {
821 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
822 continue;
823 }
824 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100826 if (preset->afg && preset->afg != codec->afg)
827 continue;
828 if (preset->mfg && preset->mfg != codec->mfg)
829 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200832 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200833 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100834 preset->rev == codec->revision_id)) {
835 mutex_unlock(&preset_mutex);
836 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100840 module_put(tbl->owner);
841 }
842 mutex_unlock(&preset_mutex);
843
844 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
845 char name[32];
846 if (!mod_requested)
847 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
848 codec->vendor_id);
849 else
850 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
851 (codec->vendor_id >> 16) & 0xffff);
852 request_module(name);
853 mod_requested++;
854 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 return NULL;
857}
858
859/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200860 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200862static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 const struct hda_vendor_id *c;
865 const char *vendor = NULL;
866 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200867 char tmp[16];
868
869 if (codec->vendor_name)
870 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 for (c = hda_vendor_ids; c->id; c++) {
873 if (c->id == vendor_id) {
874 vendor = c->name;
875 break;
876 }
877 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200878 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sprintf(tmp, "Generic %04x", vendor_id);
880 vendor = tmp;
881 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200882 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
883 if (!codec->vendor_name)
884 return -ENOMEM;
885
886 get_chip_name:
887 if (codec->chip_name)
888 return 0;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200891 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
892 else {
893 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
894 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
895 }
896 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200897 return -ENOMEM;
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200902 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100904static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200906 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 hda_nid_t nid;
908
909 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
910 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200911 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200912 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200913 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200914 case AC_GRP_AUDIO_FUNCTION:
915 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200916 codec->afg_function_id = function_id & 0xff;
917 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200918 break;
919 case AC_GRP_MODEM_FUNCTION:
920 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200921 codec->mfg_function_id = function_id & 0xff;
922 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200923 break;
924 default:
925 break;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100931 * read widget caps for each widget and store in cache
932 */
933static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
934{
935 int i;
936 hda_nid_t nid;
937
938 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
939 &codec->start_nid);
940 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200941 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100942 return -ENOMEM;
943 nid = codec->start_nid;
944 for (i = 0; i < codec->num_nodes; i++, nid++)
945 codec->wcaps[i] = snd_hda_param_read(codec, nid,
946 AC_PAR_AUDIO_WIDGET_CAP);
947 return 0;
948}
949
Takashi Iwai3be14142009-02-20 14:11:16 +0100950/* read all pin default configurations and save codec->init_pins */
951static int read_pin_defaults(struct hda_codec *codec)
952{
953 int i;
954 hda_nid_t nid = codec->start_nid;
955
956 for (i = 0; i < codec->num_nodes; i++, nid++) {
957 struct hda_pincfg *pin;
958 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200959 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100960 if (wid_type != AC_WID_PIN)
961 continue;
962 pin = snd_array_new(&codec->init_pins);
963 if (!pin)
964 return -ENOMEM;
965 pin->nid = nid;
966 pin->cfg = snd_hda_codec_read(codec, nid, 0,
967 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200968 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
969 AC_VERB_GET_PIN_WIDGET_CONTROL,
970 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100971 }
972 return 0;
973}
974
975/* look up the given pin config list and return the item matching with NID */
976static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
977 struct snd_array *array,
978 hda_nid_t nid)
979{
980 int i;
981 for (i = 0; i < array->used; i++) {
982 struct hda_pincfg *pin = snd_array_elem(array, i);
983 if (pin->nid == nid)
984 return pin;
985 }
986 return NULL;
987}
988
989/* write a config value for the given NID */
990static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
991 unsigned int cfg)
992{
993 int i;
994 for (i = 0; i < 4; i++) {
995 snd_hda_codec_write(codec, nid, 0,
996 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
997 cfg & 0xff);
998 cfg >>= 8;
999 }
1000}
1001
1002/* set the current pin config value for the given NID.
1003 * the value is cached, and read via snd_hda_codec_get_pincfg()
1004 */
1005int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1006 hda_nid_t nid, unsigned int cfg)
1007{
1008 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001009 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001010
Takashi Iwaib82855a2009-12-27 11:24:56 +01001011 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1012 return -EINVAL;
1013
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001014 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001015 pin = look_up_pincfg(codec, list, nid);
1016 if (!pin) {
1017 pin = snd_array_new(list);
1018 if (!pin)
1019 return -ENOMEM;
1020 pin->nid = nid;
1021 }
1022 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001023
1024 /* change only when needed; e.g. if the pincfg is already present
1025 * in user_pins[], don't write it
1026 */
1027 cfg = snd_hda_codec_get_pincfg(codec, nid);
1028 if (oldcfg != cfg)
1029 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001030 return 0;
1031}
1032
Takashi Iwaid5191e52009-11-16 14:58:17 +01001033/**
1034 * snd_hda_codec_set_pincfg - Override a pin default configuration
1035 * @codec: the HDA codec
1036 * @nid: NID to set the pin config
1037 * @cfg: the pin default config value
1038 *
1039 * Override a pin default configuration value in the cache.
1040 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1041 * priority than the real hardware value.
1042 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001043int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1044 hda_nid_t nid, unsigned int cfg)
1045{
Takashi Iwai346ff702009-02-23 09:42:57 +01001046 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001047}
1048EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1049
Takashi Iwaid5191e52009-11-16 14:58:17 +01001050/**
1051 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1052 * @codec: the HDA codec
1053 * @nid: NID to get the pin config
1054 *
1055 * Get the current pin config value of the given pin NID.
1056 * If the pincfg value is cached or overridden via sysfs or driver,
1057 * returns the cached value.
1058 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001059unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1060{
1061 struct hda_pincfg *pin;
1062
Takashi Iwai3be14142009-02-20 14:11:16 +01001063#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001064 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001065 if (pin)
1066 return pin->cfg;
1067#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001068 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1069 if (pin)
1070 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1072 if (pin)
1073 return pin->cfg;
1074 return 0;
1075}
1076EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1077
1078/* restore all current pin configs */
1079static void restore_pincfgs(struct hda_codec *codec)
1080{
1081 int i;
1082 for (i = 0; i < codec->init_pins.used; i++) {
1083 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1084 set_pincfg(codec, pin->nid,
1085 snd_hda_codec_get_pincfg(codec, pin->nid));
1086 }
1087}
Takashi Iwai54d17402005-11-21 16:33:22 +01001088
Takashi Iwai92ee6162009-12-27 11:18:59 +01001089/**
1090 * snd_hda_shutup_pins - Shut up all pins
1091 * @codec: the HDA codec
1092 *
1093 * Clear all pin controls to shup up before suspend for avoiding click noise.
1094 * The controls aren't cached so that they can be resumed properly.
1095 */
1096void snd_hda_shutup_pins(struct hda_codec *codec)
1097{
1098 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001099 /* don't shut up pins when unloading the driver; otherwise it breaks
1100 * the default pin setup at the next load of the driver
1101 */
1102 if (codec->bus->shutdown)
1103 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001104 for (i = 0; i < codec->init_pins.used; i++) {
1105 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1106 /* use read here for syncing after issuing each verb */
1107 snd_hda_codec_read(codec, pin->nid, 0,
1108 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1109 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001110 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001111}
1112EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1113
Takashi Iwai2a439522011-07-26 09:52:50 +02001114#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001115/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1116static void restore_shutup_pins(struct hda_codec *codec)
1117{
1118 int i;
1119 if (!codec->pins_shutup)
1120 return;
1121 if (codec->bus->shutdown)
1122 return;
1123 for (i = 0; i < codec->init_pins.used; i++) {
1124 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1125 snd_hda_codec_write(codec, pin->nid, 0,
1126 AC_VERB_SET_PIN_WIDGET_CONTROL,
1127 pin->ctrl);
1128 }
1129 codec->pins_shutup = 0;
1130}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001131#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001132
Takashi Iwai01751f52007-08-10 16:59:39 +02001133static void init_hda_cache(struct hda_cache_rec *cache,
1134 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001135static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001136
Takashi Iwai3be14142009-02-20 14:11:16 +01001137/* restore the initial pin cfgs and release all pincfg lists */
1138static void restore_init_pincfgs(struct hda_codec *codec)
1139{
Takashi Iwai346ff702009-02-23 09:42:57 +01001140 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001141 * so that only the values in init_pins are restored
1142 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001143 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001144#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001145 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001146#endif
1147 restore_pincfgs(codec);
1148 snd_array_free(&codec->init_pins);
1149}
1150
Takashi Iwai54d17402005-11-21 16:33:22 +01001151/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001152 * audio-converter setup caches
1153 */
1154struct hda_cvt_setup {
1155 hda_nid_t nid;
1156 u8 stream_tag;
1157 u8 channel_id;
1158 u16 format_id;
1159 unsigned char active; /* cvt is currently used */
1160 unsigned char dirty; /* setups should be cleared */
1161};
1162
1163/* get or create a cache entry for the given audio converter NID */
1164static struct hda_cvt_setup *
1165get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1166{
1167 struct hda_cvt_setup *p;
1168 int i;
1169
1170 for (i = 0; i < codec->cvt_setups.used; i++) {
1171 p = snd_array_elem(&codec->cvt_setups, i);
1172 if (p->nid == nid)
1173 return p;
1174 }
1175 p = snd_array_new(&codec->cvt_setups);
1176 if (p)
1177 p->nid = nid;
1178 return p;
1179}
1180
1181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * codec destructor
1183 */
1184static void snd_hda_codec_free(struct hda_codec *codec)
1185{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001186 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001188 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001189#ifdef CONFIG_SND_HDA_POWER_SAVE
1190 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001191 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001194 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001195 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001196 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001197 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 codec->bus->caddr_tbl[codec->addr] = NULL;
1199 if (codec->patch_ops.free)
1200 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001201 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001202 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001203 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001204 kfree(codec->vendor_name);
1205 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001206 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001207 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 kfree(codec);
1209}
1210
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001211static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1212 unsigned int power_state);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/**
1215 * snd_hda_codec_new - create a HDA codec
1216 * @bus: the bus to assign
1217 * @codec_addr: the codec address
1218 * @codecp: the pointer to store the generated codec
1219 *
1220 * Returns 0 if successful, or a negative error code.
1221 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001222int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1223 unsigned int codec_addr,
1224 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001227 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int err;
1229
Takashi Iwaida3cec32008-08-08 17:12:14 +02001230 if (snd_BUG_ON(!bus))
1231 return -EINVAL;
1232 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001236 snd_printk(KERN_ERR "hda_codec: "
1237 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EBUSY;
1239 }
1240
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001241 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (codec == NULL) {
1243 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1244 return -ENOMEM;
1245 }
1246
1247 codec->bus = bus;
1248 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001249 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001250 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001251 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001252 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001253 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1254 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001255 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001256 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001257 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001258 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001259 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001260 if (codec->bus->modelname) {
1261 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1262 if (!codec->modelname) {
1263 snd_hda_codec_free(codec);
1264 return -ENODEV;
1265 }
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#ifdef CONFIG_SND_HDA_POWER_SAVE
1269 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1270 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1271 * the caller has to power down appropriatley after initialization
1272 * phase.
1273 */
1274 hda_keep_power_on(codec);
1275#endif
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 list_add_tail(&codec->list, &bus->codec_list);
1278 bus->caddr_tbl[codec_addr] = codec;
1279
Takashi Iwai0ba21762007-04-16 11:29:14 +02001280 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1281 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001282 if (codec->vendor_id == -1)
1283 /* read again, hopefully the access method was corrected
1284 * in the last read...
1285 */
1286 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1287 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_SUBSYSTEM_ID);
1290 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001293 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001294 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001295 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001296 err = -ENODEV;
1297 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1301 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001302 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001304 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 err = read_pin_defaults(codec);
1306 if (err < 0)
1307 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001308
Takashi Iwai0ba21762007-04-16 11:29:14 +02001309 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001310 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 codec->subsystem_id =
1312 snd_hda_codec_read(codec, nid, 0,
1313 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001314 }
1315
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001316 /* power-up all before initialization */
1317 hda_set_power_state(codec,
1318 codec->afg ? codec->afg : codec->mfg,
1319 AC_PWRST_D0);
1320
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001321 snd_hda_codec_proc_new(codec);
1322
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001323 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001324
1325 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1326 codec->subsystem_id, codec->revision_id);
1327 snd_component_add(codec->bus->card, component);
1328
1329 if (codecp)
1330 *codecp = codec;
1331 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001332
1333 error:
1334 snd_hda_codec_free(codec);
1335 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001337EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001338
Takashi Iwaid5191e52009-11-16 14:58:17 +01001339/**
1340 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1341 * @codec: the HDA codec
1342 *
1343 * Start parsing of the given codec tree and (re-)initialize the whole
1344 * patch instance.
1345 *
1346 * Returns 0 if successful or a negative error code.
1347 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001348int snd_hda_codec_configure(struct hda_codec *codec)
1349{
1350 int err;
1351
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001352 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001353 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001354 err = get_codec_name(codec);
1355 if (err < 0)
1356 return err;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Takashi Iwai82467612007-07-27 19:15:54 +02001359 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001361 goto patched;
1362 }
Takashi Iwai82467612007-07-27 19:15:54 +02001363 if (codec->preset && codec->preset->patch) {
1364 err = codec->preset->patch(codec);
1365 goto patched;
1366 }
1367
1368 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001370 if (err < 0)
1371 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001372
1373 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001374 if (!err && codec->patch_ops.unsol_event)
1375 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001376 /* audio codec should override the mixer name */
1377 if (!err && (codec->afg || !*codec->bus->card->mixername))
1378 snprintf(codec->bus->card->mixername,
1379 sizeof(codec->bus->card->mixername),
1380 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001383EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385/**
1386 * snd_hda_codec_setup_stream - set up the codec for streaming
1387 * @codec: the CODEC to set up
1388 * @nid: the NID to set up
1389 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1390 * @channel_id: channel id to pass, zero based.
1391 * @format: stream format.
1392 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1394 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int channel_id, int format)
1396{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001397 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001398 struct hda_cvt_setup *p;
1399 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001400 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001401 int i;
1402
Takashi Iwai0ba21762007-04-16 11:29:14 +02001403 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001404 return;
1405
Takashi Iwai0ba21762007-04-16 11:29:14 +02001406 snd_printdd("hda_codec_setup_stream: "
1407 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001409 p = get_hda_cvt_setup(codec, nid);
1410 if (!p)
1411 return;
1412 /* update the stream-id if changed */
1413 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1414 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1415 newval = (stream_tag << 4) | channel_id;
1416 if (oldval != newval)
1417 snd_hda_codec_write(codec, nid, 0,
1418 AC_VERB_SET_CHANNEL_STREAMID,
1419 newval);
1420 p->stream_tag = stream_tag;
1421 p->channel_id = channel_id;
1422 }
1423 /* update the format-id if changed */
1424 if (p->format_id != format) {
1425 oldval = snd_hda_codec_read(codec, nid, 0,
1426 AC_VERB_GET_STREAM_FORMAT, 0);
1427 if (oldval != format) {
1428 msleep(1);
1429 snd_hda_codec_write(codec, nid, 0,
1430 AC_VERB_SET_STREAM_FORMAT,
1431 format);
1432 }
1433 p->format_id = format;
1434 }
1435 p->active = 1;
1436 p->dirty = 0;
1437
1438 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001439 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001440 list_for_each_entry(c, &codec->bus->codec_list, list) {
1441 for (i = 0; i < c->cvt_setups.used; i++) {
1442 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001443 if (!p->active && p->stream_tag == stream_tag &&
1444 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001445 p->dirty = 1;
1446 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001449EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Takashi Iwaif0cea792010-08-13 11:56:53 +02001451static void really_cleanup_stream(struct hda_codec *codec,
1452 struct hda_cvt_setup *q);
1453
Takashi Iwaid5191e52009-11-16 14:58:17 +01001454/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001455 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001456 * @codec: the CODEC to clean up
1457 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001458 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001459 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1461 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001462{
Takashi Iwaieb541332010-08-06 13:48:11 +02001463 struct hda_cvt_setup *p;
1464
Takashi Iwai888afa12008-03-18 09:57:50 +01001465 if (!nid)
1466 return;
1467
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001468 if (codec->no_sticky_stream)
1469 do_now = 1;
1470
Takashi Iwai888afa12008-03-18 09:57:50 +01001471 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001473 if (p) {
1474 /* here we just clear the active flag when do_now isn't set;
1475 * actual clean-ups will be done later in
1476 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1477 */
1478 if (do_now)
1479 really_cleanup_stream(codec, p);
1480 else
1481 p->active = 0;
1482 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001483}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001484EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001485
Takashi Iwaieb541332010-08-06 13:48:11 +02001486static void really_cleanup_stream(struct hda_codec *codec,
1487 struct hda_cvt_setup *q)
1488{
1489 hda_nid_t nid = q->nid;
1490 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1491 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1492 memset(q, 0, sizeof(*q));
1493 q->nid = nid;
1494}
1495
1496/* clean up the all conflicting obsolete streams */
1497static void purify_inactive_streams(struct hda_codec *codec)
1498{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001499 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001500 int i;
1501
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001502 list_for_each_entry(c, &codec->bus->codec_list, list) {
1503 for (i = 0; i < c->cvt_setups.used; i++) {
1504 struct hda_cvt_setup *p;
1505 p = snd_array_elem(&c->cvt_setups, i);
1506 if (p->dirty)
1507 really_cleanup_stream(c, p);
1508 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001509 }
1510}
1511
Takashi Iwai2a439522011-07-26 09:52:50 +02001512#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001513/* clean up all streams; called from suspend */
1514static void hda_cleanup_all_streams(struct hda_codec *codec)
1515{
1516 int i;
1517
1518 for (i = 0; i < codec->cvt_setups.used; i++) {
1519 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1520 if (p->stream_tag)
1521 really_cleanup_stream(codec, p);
1522 }
1523}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001524#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/*
1527 * amp access functions
1528 */
1529
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001530/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001531#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001532#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001533#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1534#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001536#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001539static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001540 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Takashi Iwai01751f52007-08-10 16:59:39 +02001542 memset(cache, 0, sizeof(*cache));
1543 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001544 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001545}
1546
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001547static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001548{
Takashi Iwai603c4012008-07-30 15:01:44 +02001549 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001553static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwai01751f52007-08-10 16:59:39 +02001555 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1556 u16 cur = cache->hash[idx];
1557 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001560 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (info->key == key)
1562 return info;
1563 cur = info->next;
1564 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001565 return NULL;
1566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001568/* query the hash. allocate an entry if not found. */
1569static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1570 u32 key)
1571{
1572 struct hda_cache_head *info = get_hash(cache, key);
1573 if (!info) {
1574 u16 idx, cur;
1575 /* add a new hash entry */
1576 info = snd_array_new(&cache->buf);
1577 if (!info)
1578 return NULL;
1579 cur = snd_array_index(&cache->buf, info);
1580 info->key = key;
1581 info->val = 0;
1582 idx = key % (u16)ARRAY_SIZE(cache->hash);
1583 info->next = cache->hash[idx];
1584 cache->hash[idx] = cur;
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return info;
1587}
1588
Takashi Iwai01751f52007-08-10 16:59:39 +02001589/* query and allocate an amp hash entry */
1590static inline struct hda_amp_info *
1591get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1592{
1593 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1594}
1595
Takashi Iwaid5191e52009-11-16 14:58:17 +01001596/**
1597 * query_amp_caps - query AMP capabilities
1598 * @codec: the HD-auio codec
1599 * @nid: the NID to query
1600 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1601 *
1602 * Query AMP capabilities for the given widget and direction.
1603 * Returns the obtained capability bits.
1604 *
1605 * When cap bits have been already read, this doesn't read again but
1606 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001608u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001610 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Takashi Iwai0ba21762007-04-16 11:29:14 +02001612 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1613 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001615 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001616 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001618 info->amp_caps = snd_hda_param_read(codec, nid,
1619 direction == HDA_OUTPUT ?
1620 AC_PAR_AMP_OUT_CAP :
1621 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001622 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001623 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 return info->amp_caps;
1626}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001627EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Takashi Iwaid5191e52009-11-16 14:58:17 +01001629/**
1630 * snd_hda_override_amp_caps - Override the AMP capabilities
1631 * @codec: the CODEC to clean up
1632 * @nid: the NID to clean up
1633 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1634 * @caps: the capability bits to set
1635 *
1636 * Override the cached AMP caps bits value by the given one.
1637 * This function is useful if the driver needs to adjust the AMP ranges,
1638 * e.g. limit to 0dB, etc.
1639 *
1640 * Returns zero if successful or a negative error code.
1641 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001642int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1643 unsigned int caps)
1644{
1645 struct hda_amp_info *info;
1646
1647 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1648 if (!info)
1649 return -EINVAL;
1650 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001651 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001652 return 0;
1653}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001654EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001655
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001656static unsigned int
1657query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1658 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001659{
1660 struct hda_amp_info *info;
1661
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001662 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001663 if (!info)
1664 return 0;
1665 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001666 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001667 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001668 }
1669 return info->amp_caps;
1670}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001671
1672static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1673{
1674 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1675}
1676
Takashi Iwaid5191e52009-11-16 14:58:17 +01001677/**
1678 * snd_hda_query_pin_caps - Query PIN capabilities
1679 * @codec: the HD-auio codec
1680 * @nid: the NID to query
1681 *
1682 * Query PIN capabilities for the given widget.
1683 * Returns the obtained capability bits.
1684 *
1685 * When cap bits have been already read, this doesn't read again but
1686 * returns the cached value.
1687 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001688u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1689{
1690 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1691 read_pin_cap);
1692}
Takashi Iwai1327a322009-03-23 13:07:47 +01001693EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1694
Wu Fengguang864f92b2009-11-18 12:38:02 +08001695/**
1696 * snd_hda_pin_sense - execute pin sense measurement
1697 * @codec: the CODEC to sense
1698 * @nid: the pin NID to sense
1699 *
1700 * Execute necessary pin sense measurement and return its Presence Detect,
1701 * Impedance, ELD Valid etc. status bits.
1702 */
1703u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1704{
Takashi Iwai729d55b2009-12-25 22:49:01 +01001705 u32 pincap;
Wu Fengguang864f92b2009-11-18 12:38:02 +08001706
Takashi Iwai729d55b2009-12-25 22:49:01 +01001707 if (!codec->no_trigger_sense) {
1708 pincap = snd_hda_query_pin_caps(codec, nid);
1709 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001710 snd_hda_codec_read(codec, nid, 0,
1711 AC_VERB_SET_PIN_SENSE, 0);
Takashi Iwai729d55b2009-12-25 22:49:01 +01001712 }
Wu Fengguang864f92b2009-11-18 12:38:02 +08001713 return snd_hda_codec_read(codec, nid, 0,
1714 AC_VERB_GET_PIN_SENSE, 0);
1715}
1716EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1717
1718/**
1719 * snd_hda_jack_detect - query pin Presence Detect status
1720 * @codec: the CODEC to sense
1721 * @nid: the pin NID to sense
1722 *
1723 * Query and return the pin's Presence Detect status.
1724 */
1725int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1726{
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001727 u32 sense = snd_hda_pin_sense(codec, nid);
1728 return !!(sense & AC_PINSENSE_PRESENCE);
Wu Fengguang864f92b2009-11-18 12:38:02 +08001729}
1730EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/*
1733 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001734 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001736static unsigned int get_vol_mute(struct hda_codec *codec,
1737 struct hda_amp_info *info, hda_nid_t nid,
1738 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 u32 val, parm;
1741
Takashi Iwai01751f52007-08-10 16:59:39 +02001742 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001743 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1746 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1747 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001748 val = snd_hda_codec_read(codec, nid, 0,
1749 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001751 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001752 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001756 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001758static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001759 hda_nid_t nid, int ch, int direction, int index,
1760 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 u32 parm;
1763
1764 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1765 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1766 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1767 parm |= val;
1768 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001769 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Takashi Iwaid5191e52009-11-16 14:58:17 +01001772/**
1773 * snd_hda_codec_amp_read - Read AMP value
1774 * @codec: HD-audio codec
1775 * @nid: NID to read the AMP value
1776 * @ch: channel (left=0 or right=1)
1777 * @direction: #HDA_INPUT or #HDA_OUTPUT
1778 * @index: the index value (only for input direction)
1779 *
1780 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Takashi Iwai834be882006-03-01 14:16:17 +01001782int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1783 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001785 struct hda_amp_info *info;
1786 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1787 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001789 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001791EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Takashi Iwaid5191e52009-11-16 14:58:17 +01001793/**
1794 * snd_hda_codec_amp_update - update the AMP value
1795 * @codec: HD-audio codec
1796 * @nid: NID to read the AMP value
1797 * @ch: channel (left=0 or right=1)
1798 * @direction: #HDA_INPUT or #HDA_OUTPUT
1799 * @idx: the index value (only for input direction)
1800 * @mask: bit mask to set
1801 * @val: the bits value to set
1802 *
1803 * Update the AMP value with a bit mask.
1804 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001805 */
Takashi Iwai834be882006-03-01 14:16:17 +01001806int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1807 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001809 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001810
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1812 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001814 if (snd_BUG_ON(mask & ~0xff))
1815 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001816 val &= mask;
1817 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001818 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001820 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 1;
1822}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001823EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Takashi Iwaid5191e52009-11-16 14:58:17 +01001825/**
1826 * snd_hda_codec_amp_stereo - update the AMP stereo values
1827 * @codec: HD-audio codec
1828 * @nid: NID to read the AMP value
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1833 *
1834 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1835 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001836 */
1837int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1838 int direction, int idx, int mask, int val)
1839{
1840 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001841
1842 if (snd_BUG_ON(mask & ~0xff))
1843 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001844 for (ch = 0; ch < 2; ch++)
1845 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1846 idx, mask, val);
1847 return ret;
1848}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001849EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001850
Takashi Iwai2a439522011-07-26 09:52:50 +02001851#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001852/**
1853 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1854 * @codec: HD-audio codec
1855 *
1856 * Resume the all amp commands from the cache.
1857 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001858void snd_hda_codec_resume_amp(struct hda_codec *codec)
1859{
Takashi Iwai603c4012008-07-30 15:01:44 +02001860 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001861 int i;
1862
Takashi Iwai603c4012008-07-30 15:01:44 +02001863 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001864 u32 key = buffer->head.key;
1865 hda_nid_t nid;
1866 unsigned int idx, dir, ch;
1867 if (!key)
1868 continue;
1869 nid = key & 0xff;
1870 idx = (key >> 16) & 0xff;
1871 dir = (key >> 24) & 0xff;
1872 for (ch = 0; ch < 2; ch++) {
1873 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1874 continue;
1875 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1876 buffer->vol[ch]);
1877 }
1878 }
1879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001880EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001881#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001883static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1884 unsigned int ofs)
1885{
1886 u32 caps = query_amp_caps(codec, nid, dir);
1887 /* get num steps */
1888 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1889 if (ofs < caps)
1890 caps -= ofs;
1891 return caps;
1892}
1893
Takashi Iwaid5191e52009-11-16 14:58:17 +01001894/**
1895 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1896 *
1897 * The control element is supposed to have the private_value field
1898 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1899 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001900int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 u16 nid = get_amp_nid(kcontrol);
1905 u8 chs = get_amp_channels(kcontrol);
1906 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001907 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1910 uinfo->count = chs == 3 ? 2 : 1;
1911 uinfo->value.integer.min = 0;
1912 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1913 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001914 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001915 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1916 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return -EINVAL;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001921EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001923
1924static inline unsigned int
1925read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1926 int ch, int dir, int idx, unsigned int ofs)
1927{
1928 unsigned int val;
1929 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1930 val &= HDA_AMP_VOLMASK;
1931 if (val >= ofs)
1932 val -= ofs;
1933 else
1934 val = 0;
1935 return val;
1936}
1937
1938static inline int
1939update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1940 int ch, int dir, int idx, unsigned int ofs,
1941 unsigned int val)
1942{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001943 unsigned int maxval;
1944
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001945 if (val > 0)
1946 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001947 /* ofs = 0: raw max value */
1948 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001949 if (val > maxval)
1950 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001951 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1952 HDA_AMP_VOLMASK, val);
1953}
1954
Takashi Iwaid5191e52009-11-16 14:58:17 +01001955/**
1956 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1957 *
1958 * The control element is supposed to have the private_value field
1959 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1960 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001961int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1962 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1965 hda_nid_t nid = get_amp_nid(kcontrol);
1966 int chs = get_amp_channels(kcontrol);
1967 int dir = get_amp_direction(kcontrol);
1968 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001969 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 long *valp = ucontrol->value.integer.value;
1971
1972 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001973 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001978EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Takashi Iwaid5191e52009-11-16 14:58:17 +01001980/**
1981 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1982 *
1983 * The control element is supposed to have the private_value field
1984 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1985 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001986int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990 hda_nid_t nid = get_amp_nid(kcontrol);
1991 int chs = get_amp_channels(kcontrol);
1992 int dir = get_amp_direction(kcontrol);
1993 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001994 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 long *valp = ucontrol->value.integer.value;
1996 int change = 0;
1997
Takashi Iwaicb53c622007-08-10 17:21:45 +02001998 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001999 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002000 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002001 valp++;
2002 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002003 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002004 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002005 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return change;
2007}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002008EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2012 *
2013 * The control element is supposed to have the private_value field
2014 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2015 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002016int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2017 unsigned int size, unsigned int __user *_tlv)
2018{
2019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2020 hda_nid_t nid = get_amp_nid(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002022 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002023 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002024 u32 caps, val1, val2;
2025
2026 if (size < 4 * sizeof(unsigned int))
2027 return -ENOMEM;
2028 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002029 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2030 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002031 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002032 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002033 val1 = ((int)val1) * ((int)val2);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002034 if (min_mute)
Takashi Iwaic08d9162010-10-17 10:40:53 +02002035 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002036 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2037 return -EFAULT;
2038 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2039 return -EFAULT;
2040 if (put_user(val1, _tlv + 2))
2041 return -EFAULT;
2042 if (put_user(val2, _tlv + 3))
2043 return -EFAULT;
2044 return 0;
2045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002046EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002047
Takashi Iwaid5191e52009-11-16 14:58:17 +01002048/**
2049 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2050 * @codec: HD-audio codec
2051 * @nid: NID of a reference widget
2052 * @dir: #HDA_INPUT or #HDA_OUTPUT
2053 * @tlv: TLV data to be stored, at least 4 elements
2054 *
2055 * Set (static) TLV data for a virtual master volume using the AMP caps
2056 * obtained from the reference NID.
2057 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002058 */
2059void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2060 unsigned int *tlv)
2061{
2062 u32 caps;
2063 int nums, step;
2064
2065 caps = query_amp_caps(codec, nid, dir);
2066 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2067 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2068 step = (step + 1) * 25;
2069 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2070 tlv[1] = 2 * sizeof(unsigned int);
2071 tlv[2] = -nums * step;
2072 tlv[3] = step;
2073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002074EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002075
2076/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002077static struct snd_kcontrol *
2078_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2079 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002080{
2081 struct snd_ctl_elem_id id;
2082 memset(&id, 0, sizeof(id));
2083 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002084 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002085 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2086 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002087 strcpy(id.name, name);
2088 return snd_ctl_find_id(codec->bus->card, &id);
2089}
2090
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091/**
2092 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2093 * @codec: HD-audio codec
2094 * @name: ctl id name string
2095 *
2096 * Get the control element with the given id string and IFACE_MIXER.
2097 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002098struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2099 const char *name)
2100{
2101 return _snd_hda_find_mixer_ctl(codec, name, 0);
2102}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002103EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002104
Takashi Iwai1afe2062010-12-23 10:17:52 +01002105static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2106{
2107 int idx;
2108 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2109 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2110 return idx;
2111 }
2112 return -EBUSY;
2113}
2114
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002116 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117 * @codec: HD-audio codec
2118 * @nid: corresponding NID (optional)
2119 * @kctl: the control element to assign
2120 *
2121 * Add the given control element to an array inside the codec instance.
2122 * All control elements belonging to a codec are supposed to be added
2123 * by this function so that a proper clean-up works at the free or
2124 * reconfiguration time.
2125 *
2126 * If non-zero @nid is passed, the NID is assigned to the control element.
2127 * The assignment is shown in the codec proc file.
2128 *
2129 * snd_hda_ctl_add() checks the control subdev id field whether
2130 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002131 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2132 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002133 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002134int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2135 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002136{
2137 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002138 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002139 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002140
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002141 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002142 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (nid == 0)
2144 nid = get_amp_nid_(kctl->private_value);
2145 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002146 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2147 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002148 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002149 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002150 err = snd_ctl_add(codec->bus->card, kctl);
2151 if (err < 0)
2152 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002153 item = snd_array_new(&codec->mixers);
2154 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002155 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002156 item->kctl = kctl;
2157 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002158 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002159 return 0;
2160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002161EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002162
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002164 * snd_hda_add_nid - Assign a NID to a control element
2165 * @codec: HD-audio codec
2166 * @nid: corresponding NID (optional)
2167 * @kctl: the control element to assign
2168 * @index: index to kctl
2169 *
2170 * Add the given control element to an array inside the codec instance.
2171 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2172 * NID:KCTL mapping - for example "Capture Source" selector.
2173 */
2174int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2175 unsigned int index, hda_nid_t nid)
2176{
2177 struct hda_nid_item *item;
2178
2179 if (nid > 0) {
2180 item = snd_array_new(&codec->nids);
2181 if (!item)
2182 return -ENOMEM;
2183 item->kctl = kctl;
2184 item->index = index;
2185 item->nid = nid;
2186 return 0;
2187 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002188 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2189 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002190 return -EINVAL;
2191}
2192EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2193
2194/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2196 * @codec: HD-audio codec
2197 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002198void snd_hda_ctls_clear(struct hda_codec *codec)
2199{
2200 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002201 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002202 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002203 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002205 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206}
2207
Takashi Iwaia65d6292009-02-23 16:57:04 +01002208/* pseudo device locking
2209 * toggle card->shutdown to allow/disallow the device access (as a hack)
2210 */
2211static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002212{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002213 spin_lock(&card->files_lock);
2214 if (card->shutdown) {
2215 spin_unlock(&card->files_lock);
2216 return -EINVAL;
2217 }
2218 card->shutdown = 1;
2219 spin_unlock(&card->files_lock);
2220 return 0;
2221}
2222
2223static void hda_unlock_devices(struct snd_card *card)
2224{
2225 spin_lock(&card->files_lock);
2226 card->shutdown = 0;
2227 spin_unlock(&card->files_lock);
2228}
2229
Takashi Iwaid5191e52009-11-16 14:58:17 +01002230/**
2231 * snd_hda_codec_reset - Clear all objects assigned to the codec
2232 * @codec: HD-audio codec
2233 *
2234 * This frees the all PCM and control elements assigned to the codec, and
2235 * clears the caches and restores the pin default configurations.
2236 *
2237 * When a device is being used, it returns -EBSY. If successfully freed,
2238 * returns zero.
2239 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002240int snd_hda_codec_reset(struct hda_codec *codec)
2241{
2242 struct snd_card *card = codec->bus->card;
2243 int i, pcm;
2244
2245 if (hda_lock_devices(card) < 0)
2246 return -EBUSY;
2247 /* check whether the codec isn't used by any mixer or PCM streams */
2248 if (!list_empty(&card->ctl_files)) {
2249 hda_unlock_devices(card);
2250 return -EBUSY;
2251 }
2252 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2253 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2254 if (!cpcm->pcm)
2255 continue;
2256 if (cpcm->pcm->streams[0].substream_opened ||
2257 cpcm->pcm->streams[1].substream_opened) {
2258 hda_unlock_devices(card);
2259 return -EBUSY;
2260 }
2261 }
2262
2263 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002264
2265#ifdef CONFIG_SND_HDA_POWER_SAVE
2266 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01002267 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002268#endif
2269 snd_hda_ctls_clear(codec);
2270 /* relase PCMs */
2271 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002272 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002273 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002274 clear_bit(codec->pcm_info[i].device,
2275 codec->bus->pcm_dev_bits);
2276 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002277 }
2278 if (codec->patch_ops.free)
2279 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002280 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002281 codec->spec = NULL;
2282 free_hda_cache(&codec->amp_cache);
2283 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002284 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2285 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002286 /* free only driver_pins so that init_pins + user_pins are restored */
2287 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002288 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002289 codec->num_pcms = 0;
2290 codec->pcm_info = NULL;
2291 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002292 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2293 codec->slave_dig_outs = NULL;
2294 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002295 module_put(codec->owner);
2296 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002297
2298 /* allow device access again */
2299 hda_unlock_devices(card);
2300 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002301}
2302
Takashi Iwaid5191e52009-11-16 14:58:17 +01002303/**
2304 * snd_hda_add_vmaster - create a virtual master control and add slaves
2305 * @codec: HD-audio codec
2306 * @name: vmaster control name
2307 * @tlv: TLV data (optional)
2308 * @slaves: slave control names (optional)
2309 *
2310 * Create a virtual master control with the given name. The TLV data
2311 * must be either NULL or a valid data.
2312 *
2313 * @slaves is a NULL-terminated array of strings, each of which is a
2314 * slave control name. All controls with these names are assigned to
2315 * the new virtual master control.
2316 *
2317 * This function returns zero if successful or a negative error code.
2318 */
Takashi Iwai2134ea42008-01-10 16:53:55 +01002319int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwaiea734962011-01-17 11:29:34 +01002320 unsigned int *tlv, const char * const *slaves)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002321{
2322 struct snd_kcontrol *kctl;
Takashi Iwaiea734962011-01-17 11:29:34 +01002323 const char * const *s;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002324 int err;
2325
Takashi Iwai2f085542008-02-22 18:43:50 +01002326 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2327 ;
2328 if (!*s) {
2329 snd_printdd("No slave found for %s\n", name);
2330 return 0;
2331 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002332 kctl = snd_ctl_make_virtual_master(name, tlv);
2333 if (!kctl)
2334 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002335 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002336 if (err < 0)
2337 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002338
Takashi Iwai2134ea42008-01-10 16:53:55 +01002339 for (s = slaves; *s; s++) {
2340 struct snd_kcontrol *sctl;
Takashi Iwai7a411ee2009-03-06 10:08:14 +01002341 int i = 0;
2342 for (;;) {
2343 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2344 if (!sctl) {
2345 if (!i)
2346 snd_printdd("Cannot find slave %s, "
2347 "skipped\n", *s);
2348 break;
2349 }
2350 err = snd_ctl_add_slave(kctl, sctl);
2351 if (err < 0)
2352 return err;
2353 i++;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002354 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002355 }
2356 return 0;
2357}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002358EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002359
Takashi Iwaid5191e52009-11-16 14:58:17 +01002360/**
2361 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2362 *
2363 * The control element is supposed to have the private_value field
2364 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2365 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002366int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2367 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 int chs = get_amp_channels(kcontrol);
2370
2371 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2372 uinfo->count = chs == 3 ? 2 : 1;
2373 uinfo->value.integer.min = 0;
2374 uinfo->value.integer.max = 1;
2375 return 0;
2376}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002377EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Takashi Iwaid5191e52009-11-16 14:58:17 +01002379/**
2380 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2381 *
2382 * The control element is supposed to have the private_value field
2383 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2384 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002385int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 hda_nid_t nid = get_amp_nid(kcontrol);
2390 int chs = get_amp_channels(kcontrol);
2391 int dir = get_amp_direction(kcontrol);
2392 int idx = get_amp_index(kcontrol);
2393 long *valp = ucontrol->value.integer.value;
2394
2395 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002396 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002397 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002399 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002400 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return 0;
2402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002403EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Takashi Iwaid5191e52009-11-16 14:58:17 +01002405/**
2406 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2407 *
2408 * The control element is supposed to have the private_value field
2409 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2410 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002411int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2415 hda_nid_t nid = get_amp_nid(kcontrol);
2416 int chs = get_amp_channels(kcontrol);
2417 int dir = get_amp_direction(kcontrol);
2418 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 long *valp = ucontrol->value.integer.value;
2420 int change = 0;
2421
Takashi Iwaicb53c622007-08-10 17:21:45 +02002422 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002423 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002424 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002425 HDA_AMP_MUTE,
2426 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002427 valp++;
2428 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002429 if (chs & 2)
2430 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002431 HDA_AMP_MUTE,
2432 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002433 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002434 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return change;
2436}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002437EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Takashi Iwai67d634c2009-11-16 15:35:59 +01002439#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002440/**
2441 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2442 *
2443 * This function calls snd_hda_enable_beep_device(), which behaves differently
2444 * depending on beep_mode option.
2445 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002446int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 long *valp = ucontrol->value.integer.value;
2451
2452 snd_hda_enable_beep_device(codec, *valp);
2453 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2454}
2455EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002456#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458/*
Takashi Iwai985be542005-11-02 18:26:49 +01002459 * bound volume controls
2460 *
2461 * bind multiple volumes (# indices, from 0)
2462 */
2463
2464#define AMP_VAL_IDX_SHIFT 19
2465#define AMP_VAL_IDX_MASK (0x0f<<19)
2466
Takashi Iwaid5191e52009-11-16 14:58:17 +01002467/**
2468 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2469 *
2470 * The control element is supposed to have the private_value field
2471 * set up via HDA_BIND_MUTE*() macros.
2472 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002473int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002475{
2476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2477 unsigned long pval;
2478 int err;
2479
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002480 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002481 pval = kcontrol->private_value;
2482 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2483 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2484 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002485 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002486 return err;
2487}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002488EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002489
Takashi Iwaid5191e52009-11-16 14:58:17 +01002490/**
2491 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2492 *
2493 * The control element is supposed to have the private_value field
2494 * set up via HDA_BIND_MUTE*() macros.
2495 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002496int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002498{
2499 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2500 unsigned long pval;
2501 int i, indices, err = 0, change = 0;
2502
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002503 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002504 pval = kcontrol->private_value;
2505 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2506 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002507 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2508 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002509 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2510 if (err < 0)
2511 break;
2512 change |= err;
2513 }
2514 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002515 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002516 return err < 0 ? err : change;
2517}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002518EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002519
Takashi Iwaid5191e52009-11-16 14:58:17 +01002520/**
2521 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2522 *
2523 * The control element is supposed to have the private_value field
2524 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002525 */
2526int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_info *uinfo)
2528{
2529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2530 struct hda_bind_ctls *c;
2531 int err;
2532
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002533 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002534 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002535 kcontrol->private_value = *c->values;
2536 err = c->ops->info(kcontrol, uinfo);
2537 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002538 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002539 return err;
2540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002541EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002542
Takashi Iwaid5191e52009-11-16 14:58:17 +01002543/**
2544 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2545 *
2546 * The control element is supposed to have the private_value field
2547 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2548 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002549int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_value *ucontrol)
2551{
2552 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2553 struct hda_bind_ctls *c;
2554 int err;
2555
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002556 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002557 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002558 kcontrol->private_value = *c->values;
2559 err = c->ops->get(kcontrol, ucontrol);
2560 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002561 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002562 return err;
2563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002564EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002565
Takashi Iwaid5191e52009-11-16 14:58:17 +01002566/**
2567 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2568 *
2569 * The control element is supposed to have the private_value field
2570 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2571 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002572int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
2574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 struct hda_bind_ctls *c;
2577 unsigned long *vals;
2578 int err = 0, change = 0;
2579
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002580 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002581 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002582 for (vals = c->values; *vals; vals++) {
2583 kcontrol->private_value = *vals;
2584 err = c->ops->put(kcontrol, ucontrol);
2585 if (err < 0)
2586 break;
2587 change |= err;
2588 }
2589 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002590 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002591 return err < 0 ? err : change;
2592}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002593EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002594
Takashi Iwaid5191e52009-11-16 14:58:17 +01002595/**
2596 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2597 *
2598 * The control element is supposed to have the private_value field
2599 * set up via HDA_BIND_VOL() macro.
2600 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002601int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2602 unsigned int size, unsigned int __user *tlv)
2603{
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605 struct hda_bind_ctls *c;
2606 int err;
2607
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002608 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002609 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002610 kcontrol->private_value = *c->values;
2611 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2612 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002613 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002614 return err;
2615}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002616EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002617
2618struct hda_ctl_ops snd_hda_bind_vol = {
2619 .info = snd_hda_mixer_amp_volume_info,
2620 .get = snd_hda_mixer_amp_volume_get,
2621 .put = snd_hda_mixer_amp_volume_put,
2622 .tlv = snd_hda_mixer_amp_tlv
2623};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002624EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002625
2626struct hda_ctl_ops snd_hda_bind_sw = {
2627 .info = snd_hda_mixer_amp_switch_info,
2628 .get = snd_hda_mixer_amp_switch_get,
2629 .put = snd_hda_mixer_amp_switch_put,
2630 .tlv = snd_hda_mixer_amp_tlv
2631};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002632EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002633
2634/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * SPDIF out controls
2636 */
2637
Takashi Iwai0ba21762007-04-16 11:29:14 +02002638static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2642 uinfo->count = 1;
2643 return 0;
2644}
2645
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2650 IEC958_AES0_NONAUDIO |
2651 IEC958_AES0_CON_EMPHASIS_5015 |
2652 IEC958_AES0_CON_NOT_COPYRIGHT;
2653 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2654 IEC958_AES1_CON_ORIGINAL;
2655 return 0;
2656}
2657
Takashi Iwai0ba21762007-04-16 11:29:14 +02002658static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2659 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
2661 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2662 IEC958_AES0_NONAUDIO |
2663 IEC958_AES0_PRO_EMPHASIS_5015;
2664 return 0;
2665}
2666
Takashi Iwai0ba21762007-04-16 11:29:14 +02002667static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002671 int idx = kcontrol->private_value;
2672 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Stephen Warren7c9359762011-06-01 11:14:17 -06002674 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2675 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2676 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2677 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 return 0;
2680}
2681
2682/* convert from SPDIF status bits to HDA SPDIF bits
2683 * bit 0 (DigEn) is always set zero (to be filled later)
2684 */
2685static unsigned short convert_from_spdif_status(unsigned int sbits)
2686{
2687 unsigned short val = 0;
2688
2689 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002692 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2695 IEC958_AES0_PRO_EMPHASIS_5015)
2696 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002698 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2699 IEC958_AES0_CON_EMPHASIS_5015)
2700 val |= AC_DIG1_EMPHASIS;
2701 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2702 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002704 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2706 }
2707 return val;
2708}
2709
2710/* convert to SPDIF status bits from HDA SPDIF bits
2711 */
2712static unsigned int convert_to_spdif_status(unsigned short val)
2713{
2714 unsigned int sbits = 0;
2715
Takashi Iwai0ba21762007-04-16 11:29:14 +02002716 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 sbits |= IEC958_AES0_PROFESSIONAL;
2720 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2723 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002724 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002726 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002728 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2730 sbits |= val & (0x7f << 8);
2731 }
2732 return sbits;
2733}
2734
Takashi Iwai2f728532008-09-25 16:32:41 +02002735/* set digital convert verbs both for the given NID and its slaves */
2736static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2737 int verb, int val)
2738{
Takashi Iwaidda14412011-05-02 11:29:30 +02002739 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002740
Takashi Iwai9e976972008-11-25 08:17:20 +01002741 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002742 d = codec->slave_dig_outs;
2743 if (!d)
2744 return;
2745 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002746 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002747}
2748
2749static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2750 int dig1, int dig2)
2751{
2752 if (dig1 != -1)
2753 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2754 if (dig2 != -1)
2755 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2756}
2757
Takashi Iwai0ba21762007-04-16 11:29:14 +02002758static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002762 int idx = kcontrol->private_value;
2763 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2764 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 unsigned short val;
2766 int change;
2767
Ingo Molnar62932df2006-01-16 16:34:20 +01002768 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002769 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2771 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2772 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002773 val = convert_from_spdif_status(spdif->status);
2774 val |= spdif->ctls & 1;
2775 change = spdif->ctls != val;
2776 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002777 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002778 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002779 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return change;
2781}
2782
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002783#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Takashi Iwai0ba21762007-04-16 11:29:14 +02002785static int snd_hda_spdif_out_switch_get(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 Warren7c9359762011-06-01 11:14:17 -06002789 int idx = kcontrol->private_value;
2790 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Stephen Warren7c9359762011-06-01 11:14:17 -06002792 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return 0;
2794}
2795
Stephen Warren74b654c2011-06-01 11:14:18 -06002796static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2797 int dig1, int dig2)
2798{
2799 set_dig_out_convert(codec, nid, dig1, dig2);
2800 /* unmute amp switch (if any) */
2801 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2802 (dig1 & AC_DIG1_ENABLE))
2803 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2804 HDA_AMP_MUTE, 0);
2805}
2806
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002811 int idx = kcontrol->private_value;
2812 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2813 hda_nid_t nid = spdif->nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 unsigned short val;
2815 int change;
2816
Ingo Molnar62932df2006-01-16 16:34:20 +01002817 mutex_lock(&codec->spdif_mutex);
Stephen Warren7c9359762011-06-01 11:14:17 -06002818 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002820 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002821 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002822 spdif->ctls = val;
2823 if (change && nid != (u16)-1)
2824 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002825 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return change;
2827}
2828
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002829static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 {
2831 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2832 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002833 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 .info = snd_hda_spdif_mask_info,
2835 .get = snd_hda_spdif_cmask_get,
2836 },
2837 {
2838 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002840 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 .info = snd_hda_spdif_mask_info,
2842 .get = snd_hda_spdif_pmask_get,
2843 },
2844 {
2845 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002846 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 .info = snd_hda_spdif_mask_info,
2848 .get = snd_hda_spdif_default_get,
2849 .put = snd_hda_spdif_default_put,
2850 },
2851 {
2852 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002853 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 .info = snd_hda_spdif_out_switch_info,
2855 .get = snd_hda_spdif_out_switch_get,
2856 .put = snd_hda_spdif_out_switch_put,
2857 },
2858 { } /* end */
2859};
2860
2861/**
2862 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2863 * @codec: the HDA codec
2864 * @nid: audio out widget NID
2865 *
2866 * Creates controls related with the SPDIF output.
2867 * Called from each patch supporting the SPDIF out.
2868 *
2869 * Returns 0 if successful, or a negative error code.
2870 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002871int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2872 hda_nid_t associated_nid,
2873 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002876 struct snd_kcontrol *kctl;
2877 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002878 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002879 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Takashi Iwai1afe2062010-12-23 10:17:52 +01002881 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2882 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01002883 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2884 return -EBUSY;
2885 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002886 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2888 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002889 if (!kctl)
2890 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002891 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002892 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002893 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002894 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return err;
2896 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002897 spdif->nid = cvt_nid;
2898 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06002899 AC_VERB_GET_DIGI_CONVERT_1, 0);
2900 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return 0;
2902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002903EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Stephen Warren7c9359762011-06-01 11:14:17 -06002905struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2906 hda_nid_t nid)
2907{
2908 int i;
2909 for (i = 0; i < codec->spdif_out.used; i++) {
2910 struct hda_spdif_out *spdif =
2911 snd_array_elem(&codec->spdif_out, i);
2912 if (spdif->nid == nid)
2913 return spdif;
2914 }
2915 return NULL;
2916}
2917EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2918
Stephen Warren74b654c2011-06-01 11:14:18 -06002919void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2920{
2921 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2922
2923 mutex_lock(&codec->spdif_mutex);
2924 spdif->nid = (u16)-1;
2925 mutex_unlock(&codec->spdif_mutex);
2926}
2927EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2928
2929void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2930{
2931 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2932 unsigned short val;
2933
2934 mutex_lock(&codec->spdif_mutex);
2935 if (spdif->nid != nid) {
2936 spdif->nid = nid;
2937 val = spdif->ctls;
2938 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2939 }
2940 mutex_unlock(&codec->spdif_mutex);
2941}
2942EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002945 * SPDIF sharing with analog output
2946 */
2947static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_value *ucontrol)
2949{
2950 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2951 ucontrol->value.integer.value[0] = mout->share_spdif;
2952 return 0;
2953}
2954
2955static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2957{
2958 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2959 mout->share_spdif = !!ucontrol->value.integer.value[0];
2960 return 0;
2961}
2962
2963static struct snd_kcontrol_new spdif_share_sw = {
2964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2965 .name = "IEC958 Default PCM Playback Switch",
2966 .info = snd_ctl_boolean_mono_info,
2967 .get = spdif_share_sw_get,
2968 .put = spdif_share_sw_put,
2969};
2970
Takashi Iwaid5191e52009-11-16 14:58:17 +01002971/**
2972 * snd_hda_create_spdif_share_sw - create Default PCM switch
2973 * @codec: the HDA codec
2974 * @mout: multi-out instance
2975 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002976int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2977 struct hda_multi_out *mout)
2978{
2979 if (!mout->dig_out_nid)
2980 return 0;
2981 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002982 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2983 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01002984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002985EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002986
2987/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * SPDIF input
2989 */
2990
2991#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2992
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2997
2998 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2999 return 0;
3000}
3001
Takashi Iwai0ba21762007-04-16 11:29:14 +02003002static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3006 hda_nid_t nid = kcontrol->private_value;
3007 unsigned int val = !!ucontrol->value.integer.value[0];
3008 int change;
3009
Ingo Molnar62932df2006-01-16 16:34:20 +01003010 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003012 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003014 snd_hda_codec_write_cache(codec, nid, 0,
3015 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003017 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return change;
3019}
3020
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3022 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3025 hda_nid_t nid = kcontrol->private_value;
3026 unsigned short val;
3027 unsigned int sbits;
3028
Andrew Paprocki3982d172007-12-19 12:13:44 +01003029 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 sbits = convert_to_spdif_status(val);
3031 ucontrol->value.iec958.status[0] = sbits;
3032 ucontrol->value.iec958.status[1] = sbits >> 8;
3033 ucontrol->value.iec958.status[2] = sbits >> 16;
3034 ucontrol->value.iec958.status[3] = sbits >> 24;
3035 return 0;
3036}
3037
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003038static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 {
3040 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003041 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .info = snd_hda_spdif_in_switch_info,
3043 .get = snd_hda_spdif_in_switch_get,
3044 .put = snd_hda_spdif_in_switch_put,
3045 },
3046 {
3047 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3048 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003049 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 .info = snd_hda_spdif_mask_info,
3051 .get = snd_hda_spdif_in_status_get,
3052 },
3053 { } /* end */
3054};
3055
3056/**
3057 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3058 * @codec: the HDA codec
3059 * @nid: audio in widget NID
3060 *
3061 * Creates controls related with the SPDIF input.
3062 * Called from each patch supporting the SPDIF in.
3063 *
3064 * Returns 0 if successful, or a negative error code.
3065 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003066int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003069 struct snd_kcontrol *kctl;
3070 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003071 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Takashi Iwai1afe2062010-12-23 10:17:52 +01003073 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3074 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003075 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3076 return -EBUSY;
3077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3079 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003080 if (!kctl)
3081 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003083 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003084 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return err;
3086 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003088 snd_hda_codec_read(codec, nid, 0,
3089 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return 0;
3092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003093EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Takashi Iwai2a439522011-07-26 09:52:50 +02003095#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003096/*
3097 * command cache
3098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003100/* build a 32bit cache key with the widget id and the command parameter */
3101#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3102#define get_cmd_cache_nid(key) ((key) & 0xff)
3103#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3104
3105/**
3106 * snd_hda_codec_write_cache - send a single command with caching
3107 * @codec: the HDA codec
3108 * @nid: NID to send the command
3109 * @direct: direct flag
3110 * @verb: the verb to send
3111 * @parm: the parameter for the verb
3112 *
3113 * Send a single command without waiting for response.
3114 *
3115 * Returns 0 if successful, or a negative error code.
3116 */
3117int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3118 int direct, unsigned int verb, unsigned int parm)
3119{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003120 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3121 struct hda_cache_head *c;
3122 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003123
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003124 if (err < 0)
3125 return err;
3126 /* parm may contain the verb stuff for get/set amp */
3127 verb = verb | (parm >> 8);
3128 parm &= 0xff;
3129 key = build_cmd_cache_key(nid, verb);
3130 mutex_lock(&codec->bus->cmd_mutex);
3131 c = get_alloc_hash(&codec->cmd_cache, key);
3132 if (c)
3133 c->val = parm;
3134 mutex_unlock(&codec->bus->cmd_mutex);
3135 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003137EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003138
Takashi Iwaid5191e52009-11-16 14:58:17 +01003139/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003140 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3141 * @codec: the HDA codec
3142 * @nid: NID to send the command
3143 * @direct: direct flag
3144 * @verb: the verb to send
3145 * @parm: the parameter for the verb
3146 *
3147 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3148 * command if the parameter is already identical with the cached value.
3149 * If not, it sends the command and refreshes the cache.
3150 *
3151 * Returns 0 if successful, or a negative error code.
3152 */
3153int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3154 int direct, unsigned int verb, unsigned int parm)
3155{
3156 struct hda_cache_head *c;
3157 u32 key;
3158
3159 /* parm may contain the verb stuff for get/set amp */
3160 verb = verb | (parm >> 8);
3161 parm &= 0xff;
3162 key = build_cmd_cache_key(nid, verb);
3163 mutex_lock(&codec->bus->cmd_mutex);
3164 c = get_hash(&codec->cmd_cache, key);
3165 if (c && c->val == parm) {
3166 mutex_unlock(&codec->bus->cmd_mutex);
3167 return 0;
3168 }
3169 mutex_unlock(&codec->bus->cmd_mutex);
3170 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3171}
3172EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3173
3174/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003175 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3176 * @codec: HD-audio codec
3177 *
3178 * Execute all verbs recorded in the command caches to resume.
3179 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003180void snd_hda_codec_resume_cache(struct hda_codec *codec)
3181{
Takashi Iwai603c4012008-07-30 15:01:44 +02003182 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003183 int i;
3184
Takashi Iwai603c4012008-07-30 15:01:44 +02003185 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003186 u32 key = buffer->key;
3187 if (!key)
3188 continue;
3189 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3190 get_cmd_cache_cmd(key), buffer->val);
3191 }
3192}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003193EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003194
3195/**
3196 * snd_hda_sequence_write_cache - sequence writes with caching
3197 * @codec: the HDA codec
3198 * @seq: VERB array to send
3199 *
3200 * Send the commands sequentially from the given array.
3201 * Thte commands are recorded on cache for power-save and resume.
3202 * The array must be terminated with NID=0.
3203 */
3204void snd_hda_sequence_write_cache(struct hda_codec *codec,
3205 const struct hda_verb *seq)
3206{
3207 for (; seq->nid; seq++)
3208 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3209 seq->param);
3210}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003211EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003212#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003213
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003214void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3215 unsigned int power_state,
3216 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003217{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003218 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003219 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003220
Takashi Iwaicb53c622007-08-10 17:21:45 +02003221 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003222 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003223 if (!(wcaps & AC_WCAP_POWER))
3224 continue;
3225 /* don't power down the widget if it controls eapd and
3226 * EAPD_BTLENABLE is set.
3227 */
3228 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3229 get_wcaps_type(wcaps) == AC_WID_PIN &&
3230 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3231 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003232 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003233 if (eapd & 0x02)
3234 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003235 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003236 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3237 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003238 }
3239
Takashi Iwaicb53c622007-08-10 17:21:45 +02003240 if (power_state == AC_PWRST_D0) {
3241 unsigned long end_time;
3242 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003243 /* wait until the codec reachs to D0 */
3244 end_time = jiffies + msecs_to_jiffies(500);
3245 do {
3246 state = snd_hda_codec_read(codec, fg, 0,
3247 AC_VERB_GET_POWER_STATE, 0);
3248 if (state == power_state)
3249 break;
3250 msleep(1);
3251 } while (time_after_eq(end_time, jiffies));
3252 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003253}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003254EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3255
3256/*
3257 * set power state of the codec
3258 */
3259static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3260 unsigned int power_state)
3261{
3262 if (codec->patch_ops.set_power_state) {
3263 codec->patch_ops.set_power_state(codec, fg, power_state);
3264 return;
3265 }
3266
3267 /* this delay seems necessary to avoid click noise at power-down */
3268 if (power_state == AC_PWRST_D3)
3269 msleep(100);
3270 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3271 power_state);
3272 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3273}
Takashi Iwai54d17402005-11-21 16:33:22 +01003274
Takashi Iwai11aeff02008-07-30 15:01:46 +02003275#ifdef CONFIG_SND_HDA_HWDEP
3276/* execute additional init verbs */
3277static void hda_exec_init_verbs(struct hda_codec *codec)
3278{
3279 if (codec->init_verbs.list)
3280 snd_hda_sequence_write(codec, codec->init_verbs.list);
3281}
3282#else
3283static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3284#endif
3285
Takashi Iwai2a439522011-07-26 09:52:50 +02003286#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003287/*
3288 * call suspend and power-down; used both from PM and power-save
3289 */
3290static void hda_call_codec_suspend(struct hda_codec *codec)
3291{
3292 if (codec->patch_ops.suspend)
3293 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003294 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003295 hda_set_power_state(codec,
3296 codec->afg ? codec->afg : codec->mfg,
3297 AC_PWRST_D3);
3298#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2f63092009-11-11 09:34:25 +01003299 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003300 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003301 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003302 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003303 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003304#endif
3305}
3306
3307/*
3308 * kick up codec; used both from PM and power-save
3309 */
3310static void hda_call_codec_resume(struct hda_codec *codec)
3311{
3312 hda_set_power_state(codec,
3313 codec->afg ? codec->afg : codec->mfg,
3314 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003315 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003316 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003317 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003318 if (codec->patch_ops.resume)
3319 codec->patch_ops.resume(codec);
3320 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003321 if (codec->patch_ops.init)
3322 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003323 snd_hda_codec_resume_amp(codec);
3324 snd_hda_codec_resume_cache(codec);
3325 }
3326}
Takashi Iwai2a439522011-07-26 09:52:50 +02003327#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003328
Takashi Iwai54d17402005-11-21 16:33:22 +01003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330/**
3331 * snd_hda_build_controls - build mixer controls
3332 * @bus: the BUS
3333 *
3334 * Creates mixer controls for each codec included in the bus.
3335 *
3336 * Returns 0 if successful, otherwise a negative error code.
3337 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003338int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003340 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Takashi Iwai0ba21762007-04-16 11:29:14 +02003342 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003343 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003344 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003345 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003346 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003347 err = snd_hda_codec_reset(codec);
3348 if (err < 0) {
3349 printk(KERN_ERR
3350 "hda_codec: cannot revert codec\n");
3351 return err;
3352 }
3353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003355 return 0;
3356}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003357EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003359int snd_hda_codec_build_controls(struct hda_codec *codec)
3360{
3361 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003362 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003363 /* continue to initialize... */
3364 if (codec->patch_ops.init)
3365 err = codec->patch_ops.init(codec);
3366 if (!err && codec->patch_ops.build_controls)
3367 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003368 if (err < 0)
3369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return 0;
3371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373/*
3374 * stream formats
3375 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003376struct hda_rate_tbl {
3377 unsigned int hz;
3378 unsigned int alsa_bits;
3379 unsigned int hda_fmt;
3380};
3381
Takashi Iwai92f10b32010-08-03 14:21:00 +02003382/* rate = base * mult / div */
3383#define HDA_RATE(base, mult, div) \
3384 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3385 (((div) - 1) << AC_FMT_DIV_SHIFT))
3386
Takashi Iwaibefdf312005-08-22 13:57:55 +02003387static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003389
3390 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003391 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3392 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3393 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3394 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3395 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3396 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3397 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3398 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3399 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3400 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3401 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003402#define AC_PAR_PCM_RATE_BITS 11
3403 /* up to bits 10, 384kHZ isn't supported properly */
3404
3405 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003406 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003407
Takashi Iwaibefdf312005-08-22 13:57:55 +02003408 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409};
3410
3411/**
3412 * snd_hda_calc_stream_format - calculate format bitset
3413 * @rate: the sample rate
3414 * @channels: the number of channels
3415 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3416 * @maxbps: the max. bps
3417 *
3418 * Calculate the format bitset from the given rate, channels and th PCM format.
3419 *
3420 * Return zero if invalid.
3421 */
3422unsigned int snd_hda_calc_stream_format(unsigned int rate,
3423 unsigned int channels,
3424 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003425 unsigned int maxbps,
3426 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 int i;
3429 unsigned int val = 0;
3430
Takashi Iwaibefdf312005-08-22 13:57:55 +02003431 for (i = 0; rate_bits[i].hz; i++)
3432 if (rate_bits[i].hz == rate) {
3433 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 break;
3435 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003436 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 snd_printdd("invalid rate %d\n", rate);
3438 return 0;
3439 }
3440
3441 if (channels == 0 || channels > 8) {
3442 snd_printdd("invalid channels %d\n", channels);
3443 return 0;
3444 }
3445 val |= channels - 1;
3446
3447 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003448 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003449 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003450 break;
3451 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003452 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 case 20:
3455 case 24:
3456 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003457 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003458 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003460 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003462 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 break;
3464 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003465 snd_printdd("invalid format width %d\n",
3466 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return 0;
3468 }
3469
Anssi Hannula32c168c2010-08-03 13:28:57 +03003470 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003471 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 return val;
3474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003475EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003477static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3478{
3479 unsigned int val = 0;
3480 if (nid != codec->afg &&
3481 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3482 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3483 if (!val || val == -1)
3484 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3485 if (!val || val == -1)
3486 return 0;
3487 return val;
3488}
3489
3490static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3491{
3492 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3493 get_pcm_param);
3494}
3495
3496static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3497{
3498 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3499 if (!streams || streams == -1)
3500 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3501 if (!streams || streams == -1)
3502 return 0;
3503 return streams;
3504}
3505
3506static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3507{
3508 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3509 get_stream_param);
3510}
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512/**
3513 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3514 * @codec: the HDA codec
3515 * @nid: NID to query
3516 * @ratesp: the pointer to store the detected rate bitflags
3517 * @formatsp: the pointer to store the detected formats
3518 * @bpsp: the pointer to store the detected format widths
3519 *
3520 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3521 * or @bsps argument is ignored.
3522 *
3523 * Returns 0 if successful, otherwise a negative error code.
3524 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003525int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3527{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003528 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003530 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003531 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 if (ratesp) {
3534 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003535 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003537 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003539 if (rates == 0) {
3540 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3541 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3542 nid, val,
3543 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3544 return -EIO;
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 *ratesp = rates;
3547 }
3548
3549 if (formatsp || bpsp) {
3550 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003551 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003553 streams = query_stream_param(codec, nid);
3554 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 bps = 0;
3558 if (streams & AC_SUPFMT_PCM) {
3559 if (val & AC_SUPPCM_BITS_8) {
3560 formats |= SNDRV_PCM_FMTBIT_U8;
3561 bps = 8;
3562 }
3563 if (val & AC_SUPPCM_BITS_16) {
3564 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3565 bps = 16;
3566 }
3567 if (wcaps & AC_WCAP_DIGITAL) {
3568 if (val & AC_SUPPCM_BITS_32)
3569 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3570 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3571 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3572 if (val & AC_SUPPCM_BITS_24)
3573 bps = 24;
3574 else if (val & AC_SUPPCM_BITS_20)
3575 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003576 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3577 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3579 if (val & AC_SUPPCM_BITS_32)
3580 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 else if (val & AC_SUPPCM_BITS_24)
3582 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003583 else if (val & AC_SUPPCM_BITS_20)
3584 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003587 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003589 if (!bps)
3590 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003591 }
3592 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003593 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 /* temporary hack: we have still no proper support
3595 * for the direct AC3 stream...
3596 */
3597 formats |= SNDRV_PCM_FMTBIT_U8;
3598 bps = 8;
3599 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003600 if (formats == 0) {
3601 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3602 "(nid=0x%x, val=0x%x, ovrd=%i, "
3603 "streams=0x%x)\n",
3604 nid, val,
3605 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3606 streams);
3607 return -EIO;
3608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (formatsp)
3610 *formatsp = formats;
3611 if (bpsp)
3612 *bpsp = bps;
3613 }
3614
3615 return 0;
3616}
Stephen Warren384a48d2011-06-01 11:14:21 -06003617EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003620 * snd_hda_is_supported_format - Check the validity of the format
3621 * @codec: HD-audio codec
3622 * @nid: NID to check
3623 * @format: the HD-audio format value to check
3624 *
3625 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 *
3627 * Returns 1 if supported, 0 if not.
3628 */
3629int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3630 unsigned int format)
3631{
3632 int i;
3633 unsigned int val = 0, rate, stream;
3634
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003635 val = query_pcm_param(codec, nid);
3636 if (!val)
3637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003640 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003641 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (val & (1 << i))
3643 break;
3644 return 0;
3645 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003646 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return 0;
3648
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003649 stream = query_stream_param(codec, nid);
3650 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return 0;
3652
3653 if (stream & AC_SUPFMT_PCM) {
3654 switch (format & 0xf0) {
3655 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003656 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return 0;
3658 break;
3659 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003660 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return 0;
3662 break;
3663 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003664 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 return 0;
3666 break;
3667 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003668 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return 0;
3670 break;
3671 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003672 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return 0;
3674 break;
3675 default:
3676 return 0;
3677 }
3678 } else {
3679 /* FIXME: check for float32 and AC3? */
3680 }
3681
3682 return 1;
3683}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003684EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686/*
3687 * PCM stuff
3688 */
3689static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3690 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003691 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 return 0;
3694}
3695
3696static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3697 struct hda_codec *codec,
3698 unsigned int stream_tag,
3699 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003700 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
3702 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3703 return 0;
3704}
3705
3706static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3707 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003708 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
Takashi Iwai888afa12008-03-18 09:57:50 +01003710 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return 0;
3712}
3713
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003714static int set_pcm_default_values(struct hda_codec *codec,
3715 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003717 int err;
3718
Takashi Iwai0ba21762007-04-16 11:29:14 +02003719 /* query support PCM information from the given NID */
3720 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003721 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003722 info->rates ? NULL : &info->rates,
3723 info->formats ? NULL : &info->formats,
3724 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003725 if (err < 0)
3726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 }
3728 if (info->ops.open == NULL)
3729 info->ops.open = hda_pcm_default_open_close;
3730 if (info->ops.close == NULL)
3731 info->ops.close = hda_pcm_default_open_close;
3732 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003733 if (snd_BUG_ON(!info->nid))
3734 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 info->ops.prepare = hda_pcm_default_prepare;
3736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003738 if (snd_BUG_ON(!info->nid))
3739 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 info->ops.cleanup = hda_pcm_default_cleanup;
3741 }
3742 return 0;
3743}
3744
Takashi Iwaieb541332010-08-06 13:48:11 +02003745/*
3746 * codec prepare/cleanup entries
3747 */
3748int snd_hda_codec_prepare(struct hda_codec *codec,
3749 struct hda_pcm_stream *hinfo,
3750 unsigned int stream,
3751 unsigned int format,
3752 struct snd_pcm_substream *substream)
3753{
3754 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003755 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003756 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3757 if (ret >= 0)
3758 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003759 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003760 return ret;
3761}
3762EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3763
3764void snd_hda_codec_cleanup(struct hda_codec *codec,
3765 struct hda_pcm_stream *hinfo,
3766 struct snd_pcm_substream *substream)
3767{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003768 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003769 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003770 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003771}
3772EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3773
Takashi Iwaid5191e52009-11-16 14:58:17 +01003774/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003775const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3776 "Audio", "SPDIF", "HDMI", "Modem"
3777};
3778
Takashi Iwai176d5332008-07-30 15:01:44 +02003779/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003780 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01003781 *
3782 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003783 */
3784static int get_empty_pcm_device(struct hda_bus *bus, int type)
3785{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003786 /* audio device indices; not linear to keep compatibility */
3787 static int audio_idx[HDA_PCM_NTYPES][5] = {
3788 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3789 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003790 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003791 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003792 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003793 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003794
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003795 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003796 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3797 return -EINVAL;
3798 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003799
3800 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3801 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3802 return audio_idx[type][i];
3803
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003804 snd_printk(KERN_WARNING "Too many %s devices\n",
3805 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003806 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003807}
3808
3809/*
Takashi Iwai176d5332008-07-30 15:01:44 +02003810 * attach a new PCM stream
3811 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003812static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02003813{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003814 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02003815 struct hda_pcm_stream *info;
3816 int stream, err;
3817
Takashi Iwaib91f0802008-11-04 08:43:08 +01003818 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02003819 return -EINVAL;
3820 for (stream = 0; stream < 2; stream++) {
3821 info = &pcm->stream[stream];
3822 if (info->substreams) {
3823 err = set_pcm_default_values(codec, info);
3824 if (err < 0)
3825 return err;
3826 }
3827 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003828 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02003829}
3830
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003831/* assign all PCMs of the given codec */
3832int snd_hda_codec_build_pcms(struct hda_codec *codec)
3833{
3834 unsigned int pcm;
3835 int err;
3836
3837 if (!codec->num_pcms) {
3838 if (!codec->patch_ops.build_pcms)
3839 return 0;
3840 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003841 if (err < 0) {
3842 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003843 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003844 err = snd_hda_codec_reset(codec);
3845 if (err < 0) {
3846 printk(KERN_ERR
3847 "hda_codec: cannot revert codec\n");
3848 return err;
3849 }
3850 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003851 }
3852 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3853 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3854 int dev;
3855
3856 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003857 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003858
3859 if (!cpcm->pcm) {
3860 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3861 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003862 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003863 cpcm->device = dev;
3864 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01003865 if (err < 0) {
3866 printk(KERN_ERR "hda_codec: cannot attach "
3867 "PCM stream %d for codec #%d\n",
3868 dev, codec->addr);
3869 continue; /* no fatal error */
3870 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003871 }
3872 }
3873 return 0;
3874}
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876/**
3877 * snd_hda_build_pcms - build PCM information
3878 * @bus: the BUS
3879 *
3880 * Create PCM information for each codec included in the bus.
3881 *
3882 * The build_pcms codec patch is requested to set up codec->num_pcms and
3883 * codec->pcm_info properly. The array is referred by the top-level driver
3884 * to create its PCM instances.
3885 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3886 * callback.
3887 *
3888 * At least, substreams, channels_min and channels_max must be filled for
3889 * each stream. substreams = 0 indicates that the stream doesn't exist.
3890 * When rates and/or formats are zero, the supported values are queried
3891 * from the given nid. The nid is used also by the default ops.prepare
3892 * and ops.cleanup callbacks.
3893 *
3894 * The driver needs to call ops.open in its open callback. Similarly,
3895 * ops.close is supposed to be called in the close callback.
3896 * ops.prepare should be called in the prepare or hw_params callback
3897 * with the proper parameters for set up.
3898 * ops.cleanup should be called in hw_free for clean up of streams.
3899 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003900 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003902int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003904 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Takashi Iwai0ba21762007-04-16 11:29:14 +02003906 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003907 int err = snd_hda_codec_build_pcms(codec);
3908 if (err < 0)
3909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911 return 0;
3912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003913EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915/**
3916 * snd_hda_check_board_config - compare the current codec with the config table
3917 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003918 * @num_configs: number of config enums
3919 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 * @tbl: configuration table, terminated by null entries
3921 *
3922 * Compares the modelname or PCI subsystem id of the current codec with the
3923 * given configuration table. If a matching entry is found, returns its
3924 * config value (supposed to be 0 or positive).
3925 *
3926 * If no entries are matching, the function returns a negative value.
3927 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003928int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003929 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02003930 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Takashi Iwaif44ac832008-07-30 15:01:45 +02003932 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003933 int i;
3934 for (i = 0; i < num_configs; i++) {
3935 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02003936 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003937 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3938 "selected\n", models[i]);
3939 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
3941 }
3942 }
3943
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003944 if (!codec->bus->pci || !tbl)
3945 return -1;
3946
3947 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3948 if (!tbl)
3949 return -1;
3950 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02003951#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003952 char tmp[10];
3953 const char *model = NULL;
3954 if (models)
3955 model = models[tbl->value];
3956 if (!model) {
3957 sprintf(tmp, "#%d", tbl->value);
3958 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01003960 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3961 "for config %x:%x (%s)\n",
3962 model, tbl->subvendor, tbl->subdevice,
3963 (tbl->name ? tbl->name : "Unknown device"));
3964#endif
3965 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
3967 return -1;
3968}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003969EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003972 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003973 subsystem ID with the
3974 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003975
3976 This is important for Gateway notebooks with SB450 HDA Audio
3977 where the vendor ID of the PCI device is:
3978 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3979 and the vendor/subvendor are found only at the codec.
3980
3981 * @codec: the HDA codec
3982 * @num_configs: number of config enums
3983 * @models: array of model name strings
3984 * @tbl: configuration table, terminated by null entries
3985 *
3986 * Compares the modelname or PCI subsystem id of the current codec with the
3987 * given configuration table. If a matching entry is found, returns its
3988 * config value (supposed to be 0 or positive).
3989 *
3990 * If no entries are matching, the function returns a negative value.
3991 */
3992int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01003993 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02003994 const struct snd_pci_quirk *tbl)
3995{
3996 const struct snd_pci_quirk *q;
3997
3998 /* Search for codec ID */
3999 for (q = tbl; q->subvendor; q++) {
4000 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
4001
4002 if (vendorid == codec->subsystem_id)
4003 break;
4004 }
4005
4006 if (!q->subvendor)
4007 return -1;
4008
4009 tbl = q;
4010
4011 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004012#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004013 char tmp[10];
4014 const char *model = NULL;
4015 if (models)
4016 model = models[tbl->value];
4017 if (!model) {
4018 sprintf(tmp, "#%d", tbl->value);
4019 model = tmp;
4020 }
4021 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4022 "for config %x:%x (%s)\n",
4023 model, tbl->subvendor, tbl->subdevice,
4024 (tbl->name ? tbl->name : "Unknown device"));
4025#endif
4026 return tbl->value;
4027 }
4028 return -1;
4029}
4030EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4031
4032/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 * snd_hda_add_new_ctls - create controls from the array
4034 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004035 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 *
4037 * This helper function creates and add new controls in the given array.
4038 * The array must be terminated with an empty entry as terminator.
4039 *
4040 * Returns 0 if successful, or a negative error code.
4041 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004042int snd_hda_add_new_ctls(struct hda_codec *codec,
4043 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004045 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
4047 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004048 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004049 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004050 if (knew->iface == -1) /* skip this codec private value */
4051 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004052 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004053 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004054 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004055 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004056 if (addr > 0)
4057 kctl->id.device = addr;
4058 if (idx > 0)
4059 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004060 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004061 if (!err)
4062 break;
4063 /* try first with another device index corresponding to
4064 * the codec addr; if it still fails (or it's the
4065 * primary codec), then try another control index
4066 */
4067 if (!addr && codec->addr)
4068 addr = codec->addr;
4069 else if (!idx && !knew->index) {
4070 idx = find_empty_mixer_ctl_idx(codec,
4071 knew->name);
4072 if (idx <= 0)
4073 return err;
4074 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004075 return err;
4076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
4078 return 0;
4079}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004080EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Takashi Iwaicb53c622007-08-10 17:21:45 +02004082#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004083static void hda_power_work(struct work_struct *work)
4084{
4085 struct hda_codec *codec =
4086 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004087 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004088
Maxim Levitsky2e492462007-09-03 15:26:57 +02004089 if (!codec->power_on || codec->power_count) {
4090 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004091 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004092 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004093
Takashi Iwaid66fee52011-08-02 15:39:31 +02004094 trace_hda_power_down(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004095 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004096 if (bus->ops.pm_notify)
4097 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004098}
4099
4100static void hda_keep_power_on(struct hda_codec *codec)
4101{
4102 codec->power_count++;
4103 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004104 codec->power_jiffies = jiffies;
4105}
4106
Takashi Iwaid5191e52009-11-16 14:58:17 +01004107/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004108void snd_hda_update_power_acct(struct hda_codec *codec)
4109{
4110 unsigned long delta = jiffies - codec->power_jiffies;
4111 if (codec->power_on)
4112 codec->power_on_acct += delta;
4113 else
4114 codec->power_off_acct += delta;
4115 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004116}
4117
Takashi Iwaid5191e52009-11-16 14:58:17 +01004118/**
4119 * snd_hda_power_up - Power-up the codec
4120 * @codec: HD-audio codec
4121 *
4122 * Increment the power-up counter and power up the hardware really when
4123 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004124 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004125void snd_hda_power_up(struct hda_codec *codec)
4126{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004127 struct hda_bus *bus = codec->bus;
4128
Takashi Iwaicb53c622007-08-10 17:21:45 +02004129 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02004130 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004131 return;
4132
Takashi Iwaid66fee52011-08-02 15:39:31 +02004133 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004134 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004135 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004136 codec->power_jiffies = jiffies;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004137 if (bus->ops.pm_notify)
4138 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004139 hda_call_codec_resume(codec);
4140 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02004141 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004143EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004144
4145#define power_save(codec) \
4146 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004147
Takashi Iwaid5191e52009-11-16 14:58:17 +01004148/**
4149 * snd_hda_power_down - Power-down the codec
4150 * @codec: HD-audio codec
4151 *
4152 * Decrement the power-up counter and schedules the power-off work if
4153 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004154 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004155void snd_hda_power_down(struct hda_codec *codec)
4156{
4157 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02004158 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004159 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004160 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02004161 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004162 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004163 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004164 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004166EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004167
Takashi Iwaid5191e52009-11-16 14:58:17 +01004168/**
4169 * snd_hda_check_amp_list_power - Check the amp list and update the power
4170 * @codec: HD-audio codec
4171 * @check: the object containing an AMP list and the status
4172 * @nid: NID to check / update
4173 *
4174 * Check whether the given NID is in the amp list. If it's in the list,
4175 * check the current AMP status, and update the the power-status according
4176 * to the mute status.
4177 *
4178 * This function is supposed to be set or called from the check_power_status
4179 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004180 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004181int snd_hda_check_amp_list_power(struct hda_codec *codec,
4182 struct hda_loopback_check *check,
4183 hda_nid_t nid)
4184{
Takashi Iwai031024e2011-05-02 11:29:30 +02004185 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004186 int ch, v;
4187
4188 if (!check->amplist)
4189 return 0;
4190 for (p = check->amplist; p->nid; p++) {
4191 if (p->nid == nid)
4192 break;
4193 }
4194 if (!p->nid)
4195 return 0; /* nothing changed */
4196
4197 for (p = check->amplist; p->nid; p++) {
4198 for (ch = 0; ch < 2; ch++) {
4199 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4200 p->idx);
4201 if (!(v & HDA_AMP_MUTE) && v > 0) {
4202 if (!check->power_on) {
4203 check->power_on = 1;
4204 snd_hda_power_up(codec);
4205 }
4206 return 1;
4207 }
4208 }
4209 }
4210 if (check->power_on) {
4211 check->power_on = 0;
4212 snd_hda_power_down(codec);
4213 }
4214 return 0;
4215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004216EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004219/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004220 * Channel mode helper
4221 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004222
4223/**
4224 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4225 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004226int snd_hda_ch_mode_info(struct hda_codec *codec,
4227 struct snd_ctl_elem_info *uinfo,
4228 const struct hda_channel_mode *chmode,
4229 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004230{
4231 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4232 uinfo->count = 1;
4233 uinfo->value.enumerated.items = num_chmodes;
4234 if (uinfo->value.enumerated.item >= num_chmodes)
4235 uinfo->value.enumerated.item = num_chmodes - 1;
4236 sprintf(uinfo->value.enumerated.name, "%dch",
4237 chmode[uinfo->value.enumerated.item].channels);
4238 return 0;
4239}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004240EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004241
Takashi Iwaid5191e52009-11-16 14:58:17 +01004242/**
4243 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4244 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004245int snd_hda_ch_mode_get(struct hda_codec *codec,
4246 struct snd_ctl_elem_value *ucontrol,
4247 const struct hda_channel_mode *chmode,
4248 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004249 int max_channels)
4250{
4251 int i;
4252
4253 for (i = 0; i < num_chmodes; i++) {
4254 if (max_channels == chmode[i].channels) {
4255 ucontrol->value.enumerated.item[0] = i;
4256 break;
4257 }
4258 }
4259 return 0;
4260}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004261EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004262
Takashi Iwaid5191e52009-11-16 14:58:17 +01004263/**
4264 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004266int snd_hda_ch_mode_put(struct hda_codec *codec,
4267 struct snd_ctl_elem_value *ucontrol,
4268 const struct hda_channel_mode *chmode,
4269 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004270 int *max_channelsp)
4271{
4272 unsigned int mode;
4273
4274 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004275 if (mode >= num_chmodes)
4276 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004277 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004278 return 0;
4279 /* change the current channel setting */
4280 *max_channelsp = chmode[mode].channels;
4281 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004282 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004283 return 1;
4284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004285EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/*
4288 * input MUX helper
4289 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004290
4291/**
4292 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4293 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004294int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4295 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
4297 unsigned int index;
4298
4299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4300 uinfo->count = 1;
4301 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004302 if (!imux->num_items)
4303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 index = uinfo->value.enumerated.item;
4305 if (index >= imux->num_items)
4306 index = imux->num_items - 1;
4307 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4308 return 0;
4309}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004310EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Takashi Iwaid5191e52009-11-16 14:58:17 +01004312/**
4313 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4314 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004315int snd_hda_input_mux_put(struct hda_codec *codec,
4316 const struct hda_input_mux *imux,
4317 struct snd_ctl_elem_value *ucontrol,
4318 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 unsigned int *cur_val)
4320{
4321 unsigned int idx;
4322
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004323 if (!imux->num_items)
4324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 idx = ucontrol->value.enumerated.item[0];
4326 if (idx >= imux->num_items)
4327 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004328 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004330 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4331 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 *cur_val = idx;
4333 return 1;
4334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004335EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337
4338/*
4339 * Multi-channel / digital-out PCM helper functions
4340 */
4341
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004342/* setup SPDIF output stream */
4343static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4344 unsigned int stream_tag, unsigned int format)
4345{
Stephen Warren7c9359762011-06-01 11:14:17 -06004346 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4347
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004348 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004349 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004350 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004351 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004352 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004353 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004354 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004355 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004356 for (d = codec->slave_dig_outs; *d; d++)
4357 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4358 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004359 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004360 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004361 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004362 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004363 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004364}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004365
Takashi Iwai2f728532008-09-25 16:32:41 +02004366static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4367{
4368 snd_hda_codec_cleanup_stream(codec, nid);
4369 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004370 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004371 for (d = codec->slave_dig_outs; *d; d++)
4372 snd_hda_codec_cleanup_stream(codec, *d);
4373 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004374}
4375
Takashi Iwaid5191e52009-11-16 14:58:17 +01004376/**
4377 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4378 * @bus: HD-audio bus
4379 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004380void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4381{
4382 struct hda_codec *codec;
4383
4384 if (!bus)
4385 return;
4386 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004387 if (hda_codec_is_power_on(codec) &&
4388 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004389 codec->patch_ops.reboot_notify(codec);
4390 }
4391}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004392EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004393
Takashi Iwaid5191e52009-11-16 14:58:17 +01004394/**
4395 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004397int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4398 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Ingo Molnar62932df2006-01-16 16:34:20 +01004400 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004401 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4402 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004403 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004405 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 return 0;
4407}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004408EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Takashi Iwaid5191e52009-11-16 14:58:17 +01004410/**
4411 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4412 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004413int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4414 struct hda_multi_out *mout,
4415 unsigned int stream_tag,
4416 unsigned int format,
4417 struct snd_pcm_substream *substream)
4418{
4419 mutex_lock(&codec->spdif_mutex);
4420 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4421 mutex_unlock(&codec->spdif_mutex);
4422 return 0;
4423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004424EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004425
Takashi Iwaid5191e52009-11-16 14:58:17 +01004426/**
4427 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4428 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004429int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4430 struct hda_multi_out *mout)
4431{
4432 mutex_lock(&codec->spdif_mutex);
4433 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4434 mutex_unlock(&codec->spdif_mutex);
4435 return 0;
4436}
4437EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4438
Takashi Iwaid5191e52009-11-16 14:58:17 +01004439/**
4440 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004442int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4443 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
Ingo Molnar62932df2006-01-16 16:34:20 +01004445 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004447 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return 0;
4449}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004450EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Takashi Iwaid5191e52009-11-16 14:58:17 +01004452/**
4453 * snd_hda_multi_out_analog_open - open analog outputs
4454 *
4455 * Open analog outputs and set up the hw-constraints.
4456 * If the digital outputs can be opened as slave, open the digital
4457 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004459int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4460 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004461 struct snd_pcm_substream *substream,
4462 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
Takashi Iwai9a081602008-02-12 18:37:26 +01004464 struct snd_pcm_runtime *runtime = substream->runtime;
4465 runtime->hw.channels_max = mout->max_channels;
4466 if (mout->dig_out_nid) {
4467 if (!mout->analog_rates) {
4468 mout->analog_rates = hinfo->rates;
4469 mout->analog_formats = hinfo->formats;
4470 mout->analog_maxbps = hinfo->maxbps;
4471 } else {
4472 runtime->hw.rates = mout->analog_rates;
4473 runtime->hw.formats = mout->analog_formats;
4474 hinfo->maxbps = mout->analog_maxbps;
4475 }
4476 if (!mout->spdif_rates) {
4477 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4478 &mout->spdif_rates,
4479 &mout->spdif_formats,
4480 &mout->spdif_maxbps);
4481 }
4482 mutex_lock(&codec->spdif_mutex);
4483 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004484 if ((runtime->hw.rates & mout->spdif_rates) &&
4485 (runtime->hw.formats & mout->spdif_formats)) {
4486 runtime->hw.rates &= mout->spdif_rates;
4487 runtime->hw.formats &= mout->spdif_formats;
4488 if (mout->spdif_maxbps < hinfo->maxbps)
4489 hinfo->maxbps = mout->spdif_maxbps;
4490 } else {
4491 mout->share_spdif = 0;
4492 /* FIXME: need notify? */
4493 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004494 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004495 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4498 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4499}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004500EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Takashi Iwaid5191e52009-11-16 14:58:17 +01004502/**
4503 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4504 *
4505 * Set up the i/o for analog out.
4506 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004508int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4509 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 unsigned int stream_tag,
4511 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004512 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
Takashi Iwaidda14412011-05-02 11:29:30 +02004514 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 int chs = substream->runtime->channels;
Stephen Warren7c9359762011-06-01 11:14:17 -06004516 struct hda_spdif_out *spdif =
4517 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 int i;
4519
Ingo Molnar62932df2006-01-16 16:34:20 +01004520 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004521 if (mout->dig_out_nid && mout->share_spdif &&
4522 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004524 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4525 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004526 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004528 setup_dig_out_stream(codec, mout->dig_out_nid,
4529 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 } else {
4531 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004532 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004535 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004538 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4539 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004540 if (!mout->no_share_stream &&
4541 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004543 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4544 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004545 /* extra outputs copied from front */
4546 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004547 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004548 snd_hda_codec_setup_stream(codec,
4549 mout->extra_out_nid[i],
4550 stream_tag, 0, format);
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 /* surrounds */
4553 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004554 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004555 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4556 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004557 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004558 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4559 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
4561 return 0;
4562}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004563EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Takashi Iwaid5191e52009-11-16 14:58:17 +01004565/**
4566 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004568int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4569 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
Takashi Iwaidda14412011-05-02 11:29:30 +02004571 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 int i;
4573
4574 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004575 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004577 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004578 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4579 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004580 snd_hda_codec_cleanup_stream(codec,
4581 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004582 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004584 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 mout->dig_out_used = 0;
4586 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004587 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 return 0;
4589}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004590EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004592/*
Wu Fengguang6b345002008-10-07 14:21:41 +08004593 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004594 */
Kailang Yangdf694da2005-12-05 19:42:22 +01004595
Takashi Iwaidda14412011-05-02 11:29:30 +02004596static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01004597{
4598 for (; *list; list++)
4599 if (*list == nid)
4600 return 1;
4601 return 0;
4602}
4603
Steve Longerbeam81937d32007-05-08 15:33:03 +02004604
4605/*
4606 * Sort an associated group of pins according to their sequence numbers.
4607 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004608static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
Steve Longerbeam81937d32007-05-08 15:33:03 +02004609 int num_pins)
4610{
4611 int i, j;
4612 short seq;
4613 hda_nid_t nid;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004614
Steve Longerbeam81937d32007-05-08 15:33:03 +02004615 for (i = 0; i < num_pins; i++) {
4616 for (j = i + 1; j < num_pins; j++) {
4617 if (sequences[i] > sequences[j]) {
4618 seq = sequences[i];
4619 sequences[i] = sequences[j];
4620 sequences[j] = seq;
4621 nid = pins[i];
4622 pins[i] = pins[j];
4623 pins[j] = nid;
4624 }
4625 }
4626 }
4627}
4628
4629
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004630/* add the found input-pin to the cfg->inputs[] table */
4631static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4632 int type)
4633{
4634 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4635 cfg->inputs[cfg->num_inputs].pin = nid;
4636 cfg->inputs[cfg->num_inputs].type = type;
4637 cfg->num_inputs++;
4638 }
4639}
4640
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004641/* sort inputs in the order of AUTO_PIN_* type */
4642static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4643{
4644 int i, j;
4645
4646 for (i = 0; i < cfg->num_inputs; i++) {
4647 for (j = i + 1; j < cfg->num_inputs; j++) {
4648 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4649 struct auto_pin_cfg_item tmp;
4650 tmp = cfg->inputs[i];
4651 cfg->inputs[i] = cfg->inputs[j];
4652 cfg->inputs[j] = tmp;
4653 }
4654 }
4655 }
4656}
4657
Takashi Iwai82bc9552006-03-21 11:24:42 +01004658/*
4659 * Parse all pin widgets and store the useful pin nids to cfg
4660 *
4661 * The number of line-outs or any primary output is stored in line_outs,
4662 * and the corresponding output pins are assigned to line_out_pins[],
4663 * in the order of front, rear, CLFE, side, ...
4664 *
4665 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004666 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01004667 * is detected, one of speaker of HP pins is assigned as the primary
4668 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4669 * if any analog output exists.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004670 *
Takashi Iwai86e29592010-09-09 14:50:17 +02004671 * The analog input pins are assigned to inputs array.
Takashi Iwai82bc9552006-03-21 11:24:42 +01004672 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4673 * respectively.
4674 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004675int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4676 struct auto_pin_cfg *cfg,
Takashi Iwaidda14412011-05-02 11:29:30 +02004677 const hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004678{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004679 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004680 short seq, assoc_line_out, assoc_speaker;
4681 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4682 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01004683 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004684 int i;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004685
4686 memset(cfg, 0, sizeof(*cfg));
4687
Steve Longerbeam81937d32007-05-08 15:33:03 +02004688 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4689 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01004690 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02004691 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004692
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01004693 end_nid = codec->start_nid + codec->num_nodes;
4694 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004695 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02004696 unsigned int wid_type = get_wcaps_type(wid_caps);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004697 unsigned int def_conf;
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004698 short assoc, loc, conn, dev;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004699
4700 /* read all default configuration for pin complex */
4701 if (wid_type != AC_WID_PIN)
4702 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01004703 /* ignore the given nids (e.g. pc-beep returns error) */
4704 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4705 continue;
4706
Takashi Iwaic17a1ab2009-02-23 09:28:12 +01004707 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004708 conn = get_defcfg_connect(def_conf);
4709 if (conn == AC_JACK_PORT_NONE)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004710 continue;
4711 loc = get_defcfg_location(def_conf);
Takashi Iwai1af7c5f2011-06-24 10:43:03 +02004712 dev = get_defcfg_device(def_conf);
4713
4714 /* workaround for buggy BIOS setups */
4715 if (dev == AC_JACK_LINE_OUT) {
4716 if (conn == AC_JACK_PORT_FIXED)
4717 dev = AC_JACK_SPEAKER;
4718 }
4719
4720 switch (dev) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004721 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004722 seq = get_defcfg_sequence(def_conf);
4723 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004724
4725 if (!(wid_caps & AC_WCAP_STEREO))
4726 if (!cfg->mono_out_pin)
4727 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004728 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004729 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004730 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004731 assoc_line_out = assoc;
4732 else if (assoc_line_out != assoc)
4733 continue;
4734 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4735 continue;
4736 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004737 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004738 cfg->line_outs++;
4739 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004740 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02004741 seq = get_defcfg_sequence(def_conf);
4742 assoc = get_defcfg_association(def_conf);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004743 if (!assoc)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004744 continue;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004745 if (!assoc_speaker)
Steve Longerbeam81937d32007-05-08 15:33:03 +02004746 assoc_speaker = assoc;
4747 else if (assoc_speaker != assoc)
4748 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004749 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4750 continue;
4751 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02004752 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01004753 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01004754 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004755 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01004756 seq = get_defcfg_sequence(def_conf);
4757 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004758 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4759 continue;
4760 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01004761 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004762 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004763 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004764 case AC_JACK_MIC_IN:
4765 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004766 break;
Takashi Iwai86e29592010-09-09 14:50:17 +02004767 case AC_JACK_LINE_IN:
4768 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004769 break;
4770 case AC_JACK_CD:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004771 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004772 break;
4773 case AC_JACK_AUX:
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004774 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004775 break;
4776 case AC_JACK_SPDIF_OUT:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004777 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004778 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4779 continue;
4780 cfg->dig_out_pins[cfg->dig_outs] = nid;
4781 cfg->dig_out_type[cfg->dig_outs] =
4782 (loc == AC_JACK_LOC_HDMI) ?
4783 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4784 cfg->dig_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004785 break;
4786 case AC_JACK_SPDIF_IN:
Takashi Iwai1b52ae72009-01-20 17:17:29 +01004787 case AC_JACK_DIG_OTHER_IN:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004788 cfg->dig_in_pin = nid;
Takashi Iwai2297bd62009-01-20 18:24:13 +01004789 if (loc == AC_JACK_LOC_HDMI)
4790 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4791 else
4792 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004793 break;
4794 }
4795 }
4796
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004797 /* FIX-UP:
4798 * If no line-out is defined but multiple HPs are found,
4799 * some of them might be the real line-outs.
4800 */
4801 if (!cfg->line_outs && cfg->hp_outs > 1) {
4802 int i = 0;
4803 while (i < cfg->hp_outs) {
4804 /* The real HPs should have the sequence 0x0f */
4805 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4806 i++;
4807 continue;
4808 }
4809 /* Move it to the line-out table */
4810 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4811 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4812 cfg->line_outs++;
4813 cfg->hp_outs--;
4814 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4815 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
Takashi Iwai122661b2010-09-08 14:57:04 +02004816 memmove(sequences_hp + i, sequences_hp + i + 1,
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004817 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4818 }
Takashi Iwai03642c92010-09-08 15:28:19 +02004819 memset(cfg->hp_pins + cfg->hp_outs, 0,
4820 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwaib2d05762011-01-10 14:47:35 +01004821 if (!cfg->hp_outs)
4822 cfg->line_out_type = AUTO_PIN_HP_OUT;
4823
Takashi Iwai5832fcf2008-02-12 18:30:12 +01004824 }
4825
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004826 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02004827 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4828 cfg->line_outs);
4829 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4830 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01004831 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4832 cfg->hp_outs);
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004833
Steve Longerbeam81937d32007-05-08 15:33:03 +02004834 /*
4835 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4836 * as a primary output
4837 */
4838 if (!cfg->line_outs) {
4839 if (cfg->speaker_outs) {
4840 cfg->line_outs = cfg->speaker_outs;
4841 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4842 sizeof(cfg->speaker_pins));
4843 cfg->speaker_outs = 0;
4844 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4845 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4846 } else if (cfg->hp_outs) {
4847 cfg->line_outs = cfg->hp_outs;
4848 memcpy(cfg->line_out_pins, cfg->hp_pins,
4849 sizeof(cfg->hp_pins));
4850 cfg->hp_outs = 0;
4851 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4852 cfg->line_out_type = AUTO_PIN_HP_OUT;
4853 }
4854 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004855
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004856 /* Reorder the surround channels
4857 * ALSA sequence is front/surr/clfe/side
4858 * HDA sequence is:
4859 * 4-ch: front/surr => OK as it is
4860 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02004861 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004862 */
4863 switch (cfg->line_outs) {
4864 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004865 case 4:
4866 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02004867 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02004868 cfg->line_out_pins[2] = nid;
4869 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004870 }
4871
Takashi Iwai4a4d4a62010-09-09 22:22:02 +02004872 sort_autocfg_input_pins(cfg);
4873
Takashi Iwai82bc9552006-03-21 11:24:42 +01004874 /*
4875 * debug prints of the parsed results
4876 */
Takashi Iwaic2de1872011-04-29 13:00:40 +02004877 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 +01004878 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4879 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwaic2de1872011-04-29 13:00:40 +02004880 cfg->line_out_pins[4],
4881 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4882 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4883 "speaker" : "line"));
Takashi Iwai82bc9552006-03-21 11:24:42 +01004884 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4885 cfg->speaker_outs, cfg->speaker_pins[0],
4886 cfg->speaker_pins[1], cfg->speaker_pins[2],
4887 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02004888 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4889 cfg->hp_outs, cfg->hp_pins[0],
4890 cfg->hp_pins[1], cfg->hp_pins[2],
4891 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01004892 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai0852d7a2009-02-11 11:35:15 +01004893 if (cfg->dig_outs)
4894 snd_printd(" dig-out=0x%x/0x%x\n",
4895 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004896 snd_printd(" inputs:");
4897 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0b626732011-05-26 14:10:44 +02004898 snd_printd(" %s=0x%x",
Takashi Iwai10a20af2010-09-09 16:28:02 +02004899 hda_get_autocfg_input_label(codec, cfg, i),
Takashi Iwai75e0eb22010-08-30 12:56:55 +02004900 cfg->inputs[i].pin);
4901 }
4902 snd_printd("\n");
Takashi Iwai32d2c7f2009-02-11 11:33:13 +01004903 if (cfg->dig_in_pin)
Takashi Iwai89ce9e82009-01-20 17:15:57 +01004904 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004905
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004906 return 0;
4907}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004908EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02004909
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004910int snd_hda_get_input_pin_attr(unsigned int def_conf)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004911{
4912 unsigned int loc = get_defcfg_location(def_conf);
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004913 unsigned int conn = get_defcfg_connect(def_conf);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004914 if (conn == AC_JACK_PORT_NONE)
4915 return INPUT_PIN_ATTR_UNUSED;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004916 /* Windows may claim the internal mic to be BOTH, too */
4917 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004918 return INPUT_PIN_ATTR_INT;
Takashi Iwai41c89ef2010-09-17 10:26:37 +02004919 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004920 return INPUT_PIN_ATTR_INT;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004921 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004922 return INPUT_PIN_ATTR_DOCK;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004923 if (loc == AC_JACK_LOC_REAR)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004924 return INPUT_PIN_ATTR_REAR;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004925 if (loc == AC_JACK_LOC_FRONT)
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004926 return INPUT_PIN_ATTR_FRONT;
4927 return INPUT_PIN_ATTR_NORMAL;
Takashi Iwaia1c98512010-09-09 21:36:27 +02004928}
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004929EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004930
Takashi Iwai990061c2010-09-09 22:08:44 +02004931/**
4932 * hda_get_input_pin_label - Give a label for the given input pin
4933 *
4934 * When check_location is true, the function checks the pin location
4935 * for mic and line-in pins, and set an appropriate prefix like "Front",
4936 * "Rear", "Internal".
4937 */
4938
Takashi Iwai10a20af2010-09-09 16:28:02 +02004939const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4940 int check_location)
4941{
Takashi Iwaia1c98512010-09-09 21:36:27 +02004942 unsigned int def_conf;
Takashi Iwaiea734962011-01-17 11:29:34 +01004943 static const char * const mic_names[] = {
Takashi Iwaia1c98512010-09-09 21:36:27 +02004944 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4945 };
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004946 int attr;
Takashi Iwai4a471b72005-12-07 13:56:29 +01004947
Takashi Iwai10a20af2010-09-09 16:28:02 +02004948 def_conf = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai10a20af2010-09-09 16:28:02 +02004949
4950 switch (get_defcfg_device(def_conf)) {
4951 case AC_JACK_MIC_IN:
4952 if (!check_location)
4953 return "Mic";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004954 attr = snd_hda_get_input_pin_attr(def_conf);
4955 if (!attr)
4956 return "None";
4957 return mic_names[attr - 1];
Takashi Iwai10a20af2010-09-09 16:28:02 +02004958 case AC_JACK_LINE_IN:
4959 if (!check_location)
4960 return "Line";
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004961 attr = snd_hda_get_input_pin_attr(def_conf);
4962 if (!attr)
4963 return "None";
4964 if (attr == INPUT_PIN_ATTR_DOCK)
4965 return "Dock Line";
4966 return "Line";
Takashi Iwai10a20af2010-09-09 16:28:02 +02004967 case AC_JACK_AUX:
4968 return "Aux";
4969 case AC_JACK_CD:
4970 return "CD";
4971 case AC_JACK_SPDIF_IN:
4972 return "SPDIF In";
4973 case AC_JACK_DIG_OTHER_IN:
4974 return "Digital In";
4975 default:
4976 return "Misc";
4977 }
4978}
4979EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4980
Takashi Iwaia1c98512010-09-09 21:36:27 +02004981/* Check whether the location prefix needs to be added to the label.
4982 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4983 * have to put "Front" prefix to each label. In such a case, returns false.
4984 */
4985static int check_mic_location_need(struct hda_codec *codec,
4986 const struct auto_pin_cfg *cfg,
4987 int input)
4988{
4989 unsigned int defc;
4990 int i, attr, attr2;
4991
4992 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004993 attr = snd_hda_get_input_pin_attr(defc);
Takashi Iwaia1c98512010-09-09 21:36:27 +02004994 /* for internal or docking mics, we need locations */
Takashi Iwai99ae28b2010-09-17 14:42:34 +02004995 if (attr <= INPUT_PIN_ATTR_NORMAL)
Takashi Iwaia1c98512010-09-09 21:36:27 +02004996 return 1;
4997
4998 attr = 0;
4999 for (i = 0; i < cfg->num_inputs; i++) {
5000 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +02005001 attr2 = snd_hda_get_input_pin_attr(defc);
5002 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
Takashi Iwaia1c98512010-09-09 21:36:27 +02005003 if (attr && attr != attr2)
5004 return 1; /* different locations found */
5005 attr = attr2;
5006 }
5007 }
5008 return 0;
5009}
5010
Takashi Iwai990061c2010-09-09 22:08:44 +02005011/**
5012 * hda_get_autocfg_input_label - Get a label for the given input
5013 *
5014 * Get a label for the given input pin defined by the autocfg item.
5015 * Unlike hda_get_input_pin_label(), this function checks all inputs
5016 * defined in autocfg and avoids the redundant mic/line prefix as much as
5017 * possible.
5018 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005019const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5020 const struct auto_pin_cfg *cfg,
5021 int input)
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005022{
5023 int type = cfg->inputs[input].type;
Takashi Iwai10a20af2010-09-09 16:28:02 +02005024 int has_multiple_pins = 0;
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005025
Takashi Iwai10a20af2010-09-09 16:28:02 +02005026 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5027 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5028 has_multiple_pins = 1;
Takashi Iwaia1c98512010-09-09 21:36:27 +02005029 if (has_multiple_pins && type == AUTO_PIN_MIC)
5030 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai10a20af2010-09-09 16:28:02 +02005031 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5032 has_multiple_pins);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005033}
Takashi Iwai10a20af2010-09-09 16:28:02 +02005034EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5035
Takashi Iwai990061c2010-09-09 22:08:44 +02005036/**
5037 * snd_hda_add_imux_item - Add an item to input_mux
5038 *
5039 * When the same label is used already in the existing items, the number
5040 * suffix is appended to the label. This label index number is stored
5041 * to type_idx when non-NULL pointer is given.
5042 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005043int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5044 int index, int *type_idx)
5045{
5046 int i, label_idx = 0;
5047 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5048 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5049 return -EINVAL;
5050 }
5051 for (i = 0; i < imux->num_items; i++) {
5052 if (!strncmp(label, imux->items[i].label, strlen(label)))
5053 label_idx++;
5054 }
5055 if (type_idx)
5056 *type_idx = label_idx;
5057 if (label_idx > 0)
5058 snprintf(imux->items[imux->num_items].label,
5059 sizeof(imux->items[imux->num_items].label),
5060 "%s %d", label, label_idx);
5061 else
5062 strlcpy(imux->items[imux->num_items].label, label,
5063 sizeof(imux->items[imux->num_items].label));
5064 imux->items[imux->num_items].index = index;
5065 imux->num_items++;
5066 return 0;
5067}
5068EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005069
Takashi Iwai4a471b72005-12-07 13:56:29 +01005070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071#ifdef CONFIG_PM
5072/*
5073 * power management
5074 */
5075
5076/**
5077 * snd_hda_suspend - suspend the codecs
5078 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 *
5080 * Returns 0 if successful.
5081 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005082int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005084 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Takashi Iwai0ba21762007-04-16 11:29:14 +02005086 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005087 if (hda_codec_is_power_on(codec))
5088 hda_call_codec_suspend(codec);
5089 if (codec->patch_ops.post_suspend)
5090 codec->patch_ops.post_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 }
5092 return 0;
5093}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005094EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096/**
5097 * snd_hda_resume - resume the codecs
5098 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 *
5100 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005101 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005102 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005103 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 */
5105int snd_hda_resume(struct hda_bus *bus)
5106{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005107 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Takashi Iwai0ba21762007-04-16 11:29:14 +02005109 list_for_each_entry(codec, &bus->codec_list, list) {
Vitaliy Kulikovd02667e2011-07-22 18:18:15 -05005110 if (codec->patch_ops.pre_resume)
5111 codec->patch_ops.pre_resume(codec);
Maxim Levitskyd804ad92007-09-03 15:28:04 +02005112 if (snd_hda_codec_needs_resume(codec))
5113 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return 0;
5116}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005117EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005118#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005119
5120/*
5121 * generic arrays
5122 */
5123
Takashi Iwaid5191e52009-11-16 14:58:17 +01005124/**
5125 * snd_array_new - get a new element from the given array
5126 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005127 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005128 * Get a new element from the given array. If it exceeds the
5129 * pre-allocated array size, re-allocate the array.
5130 *
5131 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005132 */
5133void *snd_array_new(struct snd_array *array)
5134{
5135 if (array->used >= array->alloced) {
5136 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005137 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005138 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005139 void *nlist;
5140 if (snd_BUG_ON(num >= 4096))
5141 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005142 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005143 if (!nlist)
5144 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005145 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005146 array->list = nlist;
5147 array->alloced = num;
5148 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005149 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005151EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005152
Takashi Iwaid5191e52009-11-16 14:58:17 +01005153/**
5154 * snd_array_free - free the given array elements
5155 * @array: the array object
5156 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005157void snd_array_free(struct snd_array *array)
5158{
5159 kfree(array->list);
5160 array->used = 0;
5161 array->alloced = 0;
5162 array->list = NULL;
5163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005164EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005165
Takashi Iwaid5191e52009-11-16 14:58:17 +01005166/**
5167 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5168 * @pcm: PCM caps bits
5169 * @buf: the string buffer to write
5170 * @buflen: the max buffer length
5171 *
Takashi Iwaib2022262008-11-21 21:24:03 +01005172 * used by hda_proc.c and hda_eld.c
5173 */
5174void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5175{
5176 static unsigned int rates[] = {
5177 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5178 96000, 176400, 192000, 384000
5179 };
5180 int i, j;
5181
5182 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5183 if (pcm & (1 << i))
5184 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5185
5186 buf[j] = '\0'; /* necessary when j == 0 */
5187}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005188EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01005189
Takashi Iwaid5191e52009-11-16 14:58:17 +01005190/**
5191 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5192 * @pcm: PCM caps bits
5193 * @buf: the string buffer to write
5194 * @buflen: the max buffer length
5195 *
5196 * used by hda_proc.c and hda_eld.c
5197 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005198void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5199{
5200 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5201 int i, j;
5202
5203 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5204 if (pcm & (AC_SUPPCM_BITS_8 << i))
5205 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5206
5207 buf[j] = '\0'; /* necessary when j == 0 */
5208}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005209EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005210
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005211#ifdef CONFIG_SND_HDA_INPUT_JACK
5212/*
5213 * Input-jack notification support
5214 */
5215struct hda_jack_item {
5216 hda_nid_t nid;
5217 int type;
5218 struct snd_jack *jack;
5219};
5220
5221static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5222 int type)
5223{
5224 switch (type) {
5225 case SND_JACK_HEADPHONE:
5226 return "Headphone";
5227 case SND_JACK_MICROPHONE:
5228 return "Mic";
5229 case SND_JACK_LINEOUT:
5230 return "Line-out";
5231 case SND_JACK_HEADSET:
5232 return "Headset";
David Henningsson07acecc2011-05-19 11:46:03 +02005233 case SND_JACK_VIDEOOUT:
5234 return "HDMI/DP";
Takashi Iwaicd372fb2011-03-03 14:40:14 +01005235 default:
5236 return "Misc";
5237 }
5238}
5239
5240static void hda_free_jack_priv(struct snd_jack *jack)
5241{
5242 struct hda_jack_item *jacks = jack->private_data;
5243 jacks->nid = 0;
5244 jacks->jack = NULL;
5245}
5246
5247int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5248 const char *name)
5249{
5250 struct hda_jack_item *jack;
5251 int err;
5252
5253 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5254 jack = snd_array_new(&codec->jacks);
5255 if (!jack)
5256 return -ENOMEM;
5257
5258 jack->nid = nid;
5259 jack->type = type;
5260 if (!name)
5261 name = get_jack_default_name(codec, nid, type);
5262 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5263 if (err < 0) {
5264 jack->nid = 0;
5265 return err;
5266 }
5267 jack->jack->private_data = jack;
5268 jack->jack->private_free = hda_free_jack_priv;
5269 return 0;
5270}
5271EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5272
5273void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5274{
5275 struct hda_jack_item *jacks = codec->jacks.list;
5276 int i;
5277
5278 if (!jacks)
5279 return;
5280
5281 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5282 unsigned int pin_ctl;
5283 unsigned int present;
5284 int type;
5285
5286 if (jacks->nid != nid)
5287 continue;
5288 present = snd_hda_jack_detect(codec, nid);
5289 type = jacks->type;
5290 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5291 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5292 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5293 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5294 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5295 }
5296 snd_jack_report(jacks->jack, present ? type : 0);
5297 }
5298}
5299EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5300
5301/* free jack instances manually when clearing/reconfiguring */
5302void snd_hda_input_jack_free(struct hda_codec *codec)
5303{
5304 if (!codec->bus->shutdown && codec->jacks.list) {
5305 struct hda_jack_item *jacks = codec->jacks.list;
5306 int i;
5307 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5308 if (jacks->jack)
5309 snd_device_free(codec->bus->card, jacks->jack);
5310 }
5311 }
5312 snd_array_free(&codec->jacks);
5313}
5314EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5315#endif /* CONFIG_SND_HDA_INPUT_JACK */
5316
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005317MODULE_DESCRIPTION("HDA codec core");
5318MODULE_LICENSE("GPL");