blob: 46f8ab1df874803d4b931d28fc1bf4b795aadb24 [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
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020031#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/initval.h>
33#include "hda_local.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x10ec, "Realtek" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010050 { 0x1106, "VIA" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020051 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020053 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020055 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 {} /* terminator */
57};
58
59/* codec presets */
60#include "hda_patch.h"
61
62
63/**
64 * snd_hda_codec_read - send a command and get the response
65 * @codec: the HDA codec
66 * @nid: NID to send the command
67 * @direct: direct flag
68 * @verb: the verb to send
69 * @parm: the parameter for the verb
70 *
71 * Send a single command and read the corresponding response.
72 *
73 * Returns the obtained response value, or -1 for an error.
74 */
Takashi Iwai0ba21762007-04-16 11:29:14 +020075unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
76 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int verb, unsigned int parm)
78{
79 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010080 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +020081 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 res = codec->bus->ops.get_response(codec);
83 else
84 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010085 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return res;
87}
88
89/**
90 * snd_hda_codec_write - send a single command without waiting for response
91 * @codec: the HDA codec
92 * @nid: NID to send the command
93 * @direct: direct flag
94 * @verb: the verb to send
95 * @parm: the parameter for the verb
96 *
97 * Send a single command without waiting for response.
98 *
99 * Returns 0 if successful, or a negative error code.
100 */
101int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
102 unsigned int verb, unsigned int parm)
103{
104 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100105 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100107 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return err;
109}
110
111/**
112 * snd_hda_sequence_write - sequence writes
113 * @codec: the HDA codec
114 * @seq: VERB array to send
115 *
116 * Send the commands sequentially from the given array.
117 * The array must be terminated with NID=0.
118 */
119void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
120{
121 for (; seq->nid; seq++)
122 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
123}
124
125/**
126 * snd_hda_get_sub_nodes - get the range of sub nodes
127 * @codec: the HDA codec
128 * @nid: NID to parse
129 * @start_id: the pointer to store the start NID
130 *
131 * Parse the NID and store the start NID of its sub-nodes.
132 * Returns the number of sub-nodes.
133 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200134int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
135 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned int parm;
138
139 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
140 *start_id = (parm >> 16) & 0x7fff;
141 return (int)(parm & 0x7fff);
142}
143
144/**
145 * snd_hda_get_connections - get connection list
146 * @codec: the HDA codec
147 * @nid: NID to parse
148 * @conn_list: connection list array
149 * @max_conns: max. number of connections to store
150 *
151 * Parses the connection list of the given widget and stores the list
152 * of NIDs.
153 *
154 * Returns the number of connections, or a negative error code.
155 */
156int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
157 hda_nid_t *conn_list, int max_conns)
158{
159 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100160 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100162 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 snd_assert(conn_list && max_conns > 0, return -EINVAL);
165
166 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
167 if (parm & AC_CLIST_LONG) {
168 /* long form */
169 shift = 16;
170 num_elems = 2;
171 } else {
172 /* short form */
173 shift = 8;
174 num_elems = 4;
175 }
176 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mask = (1 << (shift-1)) - 1;
178
Takashi Iwai0ba21762007-04-16 11:29:14 +0200179 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0; /* no connection */
181
182 if (conn_len == 1) {
183 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200184 parm = snd_hda_codec_read(codec, nid, 0,
185 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 conn_list[0] = parm & mask;
187 return 1;
188 }
189
190 /* multi connection */
191 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100192 prev_nid = 0;
193 for (i = 0; i < conn_len; i++) {
194 int range_val;
195 hda_nid_t val, n;
196
197 if (i % num_elems == 0)
198 parm = snd_hda_codec_read(codec, nid, 0,
199 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200200 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100201 val = parm & mask;
202 parm >>= shift;
203 if (range_val) {
204 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200205 if (!prev_nid || prev_nid >= val) {
206 snd_printk(KERN_WARNING "hda_codec: "
207 "invalid dep_range_val %x:%x\n",
208 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100209 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100211 for (n = prev_nid + 1; n <= val; n++) {
212 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200213 snd_printk(KERN_ERR
214 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100215 return -EINVAL;
216 }
217 conn_list[conns++] = n;
218 }
219 } else {
220 if (conns >= max_conns) {
221 snd_printk(KERN_ERR "Too many connections\n");
222 return -EINVAL;
223 }
224 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100226 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 return conns;
229}
230
231
232/**
233 * snd_hda_queue_unsol_event - add an unsolicited event to queue
234 * @bus: the BUS
235 * @res: unsolicited event (lower 32bit of RIRB entry)
236 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
237 *
238 * Adds the given event to the queue. The events are processed in
239 * the workqueue asynchronously. Call this function in the interrupt
240 * hanlder when RIRB receives an unsolicited event.
241 *
242 * Returns 0 if successful, or a negative error code.
243 */
244int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
245{
246 struct hda_bus_unsolicited *unsol;
247 unsigned int wp;
248
Takashi Iwai0ba21762007-04-16 11:29:14 +0200249 unsol = bus->unsol;
250 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252
253 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
254 unsol->wp = wp;
255
256 wp <<= 1;
257 unsol->queue[wp] = res;
258 unsol->queue[wp + 1] = res_ex;
259
Takashi Iwaie250af22006-12-19 17:08:52 +0100260 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 return 0;
263}
264
265/*
266 * process queueud unsolicited events
267 */
David Howellsc4028952006-11-22 14:57:56 +0000268static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
David Howellsc4028952006-11-22 14:57:56 +0000270 struct hda_bus_unsolicited *unsol =
271 container_of(work, struct hda_bus_unsolicited, work);
272 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct hda_codec *codec;
274 unsigned int rp, caddr, res;
275
276 while (unsol->rp != unsol->wp) {
277 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
278 unsol->rp = rp;
279 rp <<= 1;
280 res = unsol->queue[rp];
281 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200282 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 continue;
284 codec = bus->caddr_tbl[caddr & 0x0f];
285 if (codec && codec->patch_ops.unsol_event)
286 codec->patch_ops.unsol_event(codec, res);
287 }
288}
289
290/*
291 * initialize unsolicited queue
292 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200293static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct hda_bus_unsolicited *unsol;
296
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100297 if (bus->unsol) /* already initialized */
298 return 0;
299
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200300 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200301 if (!unsol) {
302 snd_printk(KERN_ERR "hda_codec: "
303 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -ENOMEM;
305 }
David Howellsc4028952006-11-22 14:57:56 +0000306 INIT_WORK(&unsol->work, process_unsol_events);
307 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bus->unsol = unsol;
309 return 0;
310}
311
312/*
313 * destructor
314 */
315static void snd_hda_codec_free(struct hda_codec *codec);
316
317static int snd_hda_bus_free(struct hda_bus *bus)
318{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200319 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Takashi Iwai0ba21762007-04-16 11:29:14 +0200321 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100324 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 kfree(bus->unsol);
326 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200327 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 snd_hda_codec_free(codec);
329 }
330 if (bus->ops.private_free)
331 bus->ops.private_free(bus);
332 kfree(bus);
333 return 0;
334}
335
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100336static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct hda_bus *bus = device->device_data;
339 return snd_hda_bus_free(bus);
340}
341
342/**
343 * snd_hda_bus_new - create a HDA bus
344 * @card: the card entry
345 * @temp: the template for hda_bus information
346 * @busp: the pointer to store the created bus instance
347 *
348 * Returns 0 if successful, or a negative error code.
349 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200350int __devinit snd_hda_bus_new(struct snd_card *card,
351 const struct hda_bus_template *temp,
352 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct hda_bus *bus;
355 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100356 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .dev_free = snd_hda_bus_dev_free,
358 };
359
360 snd_assert(temp, return -EINVAL);
361 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
362
363 if (busp)
364 *busp = NULL;
365
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200366 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (bus == NULL) {
368 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
369 return -ENOMEM;
370 }
371
372 bus->card = card;
373 bus->private_data = temp->private_data;
374 bus->pci = temp->pci;
375 bus->modelname = temp->modelname;
376 bus->ops = temp->ops;
377
Ingo Molnar62932df2006-01-16 16:34:20 +0100378 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 INIT_LIST_HEAD(&bus->codec_list);
380
Takashi Iwai0ba21762007-04-16 11:29:14 +0200381 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
382 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_hda_bus_free(bus);
384 return err;
385 }
386 if (busp)
387 *busp = bus;
388 return 0;
389}
390
Takashi Iwai82467612007-07-27 19:15:54 +0200391#ifdef CONFIG_SND_HDA_GENERIC
392#define is_generic_config(codec) \
393 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
394#else
395#define is_generic_config(codec) 0
396#endif
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
399 * find a matching codec preset
400 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200401static const struct hda_codec_preset __devinit *
402find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 const struct hda_codec_preset **tbl, *preset;
405
Takashi Iwai82467612007-07-27 19:15:54 +0200406 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100407 return NULL; /* use the generic parser */
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 for (tbl = hda_preset_tables; *tbl; tbl++) {
410 for (preset = *tbl; preset->id; preset++) {
411 u32 mask = preset->mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200412 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200414 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200415 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200416 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return preset;
418 }
419 }
420 return NULL;
421}
422
423/*
424 * snd_hda_get_codec_name - store the codec name
425 */
426void snd_hda_get_codec_name(struct hda_codec *codec,
427 char *name, int namelen)
428{
429 const struct hda_vendor_id *c;
430 const char *vendor = NULL;
431 u16 vendor_id = codec->vendor_id >> 16;
432 char tmp[16];
433
434 for (c = hda_vendor_ids; c->id; c++) {
435 if (c->id == vendor_id) {
436 vendor = c->name;
437 break;
438 }
439 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200440 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sprintf(tmp, "Generic %04x", vendor_id);
442 vendor = tmp;
443 }
444 if (codec->preset && codec->preset->name)
445 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
446 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 snprintf(name, namelen, "%s ID %x", vendor,
448 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200452 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200454static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int i, total_nodes;
457 hda_nid_t nid;
458
459 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
460 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200461 unsigned int func;
462 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
463 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200464 case AC_GRP_AUDIO_FUNCTION:
465 codec->afg = nid;
466 break;
467 case AC_GRP_MODEM_FUNCTION:
468 codec->mfg = nid;
469 break;
470 default:
471 break;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 * read widget caps for each widget and store in cache
478 */
479static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
480{
481 int i;
482 hda_nid_t nid;
483
484 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
485 &codec->start_nid);
486 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 return -ENOMEM;
489 nid = codec->start_nid;
490 for (i = 0; i < codec->num_nodes; i++, nid++)
491 codec->wcaps[i] = snd_hda_param_read(codec, nid,
492 AC_PAR_AUDIO_WIDGET_CAP);
493 return 0;
494}
495
496
Takashi Iwai01751f52007-08-10 16:59:39 +0200497static void init_hda_cache(struct hda_cache_rec *cache,
498 unsigned int record_size);
499static inline void free_hda_cache(struct hda_cache_rec *cache);
500
Takashi Iwai54d17402005-11-21 16:33:22 +0100501/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * codec destructor
503 */
504static void snd_hda_codec_free(struct hda_codec *codec)
505{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200506 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return;
508 list_del(&codec->list);
509 codec->bus->caddr_tbl[codec->addr] = NULL;
510 if (codec->patch_ops.free)
511 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200512 free_hda_cache(&codec->amp_cache);
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(codec);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/**
518 * snd_hda_codec_new - create a HDA codec
519 * @bus: the bus to assign
520 * @codec_addr: the codec address
521 * @codecp: the pointer to store the generated codec
522 *
523 * Returns 0 if successful, or a negative error code.
524 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200525int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
526 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct hda_codec *codec;
529 char component[13];
530 int err;
531
532 snd_assert(bus, return -EINVAL);
533 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
534
535 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200536 snd_printk(KERN_ERR "hda_codec: "
537 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EBUSY;
539 }
540
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200541 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (codec == NULL) {
543 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
544 return -ENOMEM;
545 }
546
547 codec->bus = bus;
548 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100549 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200550 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 list_add_tail(&codec->list, &bus->codec_list);
553 bus->caddr_tbl[codec_addr] = codec;
554
Takashi Iwai0ba21762007-04-16 11:29:14 +0200555 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
556 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100557 if (codec->vendor_id == -1)
558 /* read again, hopefully the access method was corrected
559 * in the last read...
560 */
561 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
562 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
564 AC_PAR_SUBSYSTEM_ID);
565 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
566 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200568 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200569 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200570 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 snd_hda_codec_free(codec);
572 return -ENODEV;
573 }
574
Takashi Iwai54d17402005-11-21 16:33:22 +0100575 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
576 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
577 snd_hda_codec_free(codec);
578 return -ENOMEM;
579 }
580
Takashi Iwai0ba21762007-04-16 11:29:14 +0200581 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200582 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200583 codec->subsystem_id =
584 snd_hda_codec_read(codec, nid, 0,
585 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200586 }
587
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100588 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200589 /* audio codec should override the mixer name */
590 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 snd_hda_get_codec_name(codec, bus->card->mixername,
592 sizeof(bus->card->mixername));
593
Takashi Iwai82467612007-07-27 19:15:54 +0200594#ifdef CONFIG_SND_HDA_GENERIC
595 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200597 goto patched;
598 }
599#endif
600 if (codec->preset && codec->preset->patch) {
601 err = codec->preset->patch(codec);
602 goto patched;
603 }
604
605 /* call the default parser */
606#ifdef CONFIG_SND_HDA_GENERIC
607 err = snd_hda_parse_generic_codec(codec);
608#else
609 printk(KERN_ERR "hda-codec: No codec parser is available\n");
610 err = -ENODEV;
611#endif
612
613 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (err < 0) {
615 snd_hda_codec_free(codec);
616 return err;
617 }
618
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100619 if (codec->patch_ops.unsol_event)
620 init_unsol_queue(bus);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200623#ifdef CONFIG_SND_HDA_HWDEP
624 snd_hda_create_hwdep(codec);
625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 sprintf(component, "HDA:%08x", codec->vendor_id);
628 snd_component_add(codec->bus->card, component);
629
630 if (codecp)
631 *codecp = codec;
632 return 0;
633}
634
635/**
636 * snd_hda_codec_setup_stream - set up the codec for streaming
637 * @codec: the CODEC to set up
638 * @nid: the NID to set up
639 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
640 * @channel_id: channel id to pass, zero based.
641 * @format: stream format.
642 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200643void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
644 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int channel_id, int format)
646{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200647 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200648 return;
649
Takashi Iwai0ba21762007-04-16 11:29:14 +0200650 snd_printdd("hda_codec_setup_stream: "
651 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 nid, stream_tag, channel_id, format);
653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
654 (stream_tag << 4) | channel_id);
655 msleep(1);
656 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * amp access functions
661 */
662
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200663/* FIXME: more better hash key? */
664#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200666#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200669static void __devinit init_hda_cache(struct hda_cache_rec *cache,
670 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Takashi Iwai01751f52007-08-10 16:59:39 +0200672 memset(cache, 0, sizeof(*cache));
673 memset(cache->hash, 0xff, sizeof(cache->hash));
674 cache->record_size = record_size;
675}
676
677static inline void free_hda_cache(struct hda_cache_rec *cache)
678{
679 kfree(cache->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200683static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
684 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Takashi Iwai01751f52007-08-10 16:59:39 +0200686 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
687 u16 cur = cache->hash[idx];
688 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 while (cur != 0xffff) {
Takashi Iwai01751f52007-08-10 16:59:39 +0200691 info = (struct hda_cache_head *)(cache->buffer +
692 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (info->key == key)
694 return info;
695 cur = info->next;
696 }
697
698 /* add a new hash entry */
Takashi Iwai01751f52007-08-10 16:59:39 +0200699 if (cache->num_entries >= cache->size) {
Takashi Iwaid0311662005-11-07 14:38:44 +0100700 /* reallocate the array */
Takashi Iwai01751f52007-08-10 16:59:39 +0200701 unsigned int new_size = cache->size + 64;
702 void *new_buffer;
703 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
704 if (!new_buffer) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200705 snd_printk(KERN_ERR "hda_codec: "
706 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100707 return NULL;
708 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200709 if (cache->buffer) {
710 memcpy(new_buffer, cache->buffer,
711 cache->size * cache->record_size);
712 kfree(cache->buffer);
Takashi Iwaid0311662005-11-07 14:38:44 +0100713 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200714 cache->size = new_size;
715 cache->buffer = new_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200717 cur = cache->num_entries++;
718 info = (struct hda_cache_head *)(cache->buffer +
719 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200721 info->val = 0;
722 info->next = cache->hash[idx];
723 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 return info;
726}
727
Takashi Iwai01751f52007-08-10 16:59:39 +0200728/* query and allocate an amp hash entry */
729static inline struct hda_amp_info *
730get_alloc_amp_hash(struct hda_codec *codec, u32 key)
731{
732 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * query AMP capabilities for the given widget and direction
737 */
738static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
739{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200740 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Takashi Iwai0ba21762007-04-16 11:29:14 +0200742 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
743 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200745 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200746 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200748 info->amp_caps = snd_hda_param_read(codec, nid,
749 direction == HDA_OUTPUT ?
750 AC_PAR_AMP_OUT_CAP :
751 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200752 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200753 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 return info->amp_caps;
756}
757
Takashi Iwai897cc182007-05-29 19:01:37 +0200758int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
759 unsigned int caps)
760{
761 struct hda_amp_info *info;
762
763 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
764 if (!info)
765 return -EINVAL;
766 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200767 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200768 return 0;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
772 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200773 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200775static unsigned int get_vol_mute(struct hda_codec *codec,
776 struct hda_amp_info *info, hda_nid_t nid,
777 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 u32 val, parm;
780
Takashi Iwai01751f52007-08-10 16:59:39 +0200781 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200782 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
785 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
786 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 val = snd_hda_codec_read(codec, nid, 0,
788 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200790 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200791 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200795 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200797static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200798 hda_nid_t nid, int ch, int direction, int index,
799 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 u32 parm;
802
803 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
804 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
805 parm |= index << AC_AMP_SET_INDEX_SHIFT;
806 parm |= val;
807 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200808 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200812 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Takashi Iwai834be882006-03-01 14:16:17 +0100814int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
815 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200817 struct hda_amp_info *info;
818 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
819 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200821 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200824/*
825 * update the AMP value, mask = bit mask to set, val = the value
826 */
Takashi Iwai834be882006-03-01 14:16:17 +0100827int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
828 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200831
Takashi Iwai0ba21762007-04-16 11:29:14 +0200832 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
833 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200835 val &= mask;
836 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (info->vol[ch] == val && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200839 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 1;
841}
842
843
844/*
845 * AMP control callbacks
846 */
847/* retrieve parameters from private_value */
848#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
849#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
850#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
851#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
852
853/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200854int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
858 u16 nid = get_amp_nid(kcontrol);
859 u8 chs = get_amp_channels(kcontrol);
860 int dir = get_amp_direction(kcontrol);
861 u32 caps;
862
863 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200864 /* num steps */
865 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
866 if (!caps) {
867 printk(KERN_WARNING "hda_codec: "
868 "num_steps = 0 for NID=0x%x\n", nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EINVAL;
870 }
871 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
872 uinfo->count = chs == 3 ? 2 : 1;
873 uinfo->value.integer.min = 0;
874 uinfo->value.integer.max = caps;
875 return 0;
876}
877
Takashi Iwai0ba21762007-04-16 11:29:14 +0200878int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
879 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
882 hda_nid_t nid = get_amp_nid(kcontrol);
883 int chs = get_amp_channels(kcontrol);
884 int dir = get_amp_direction(kcontrol);
885 int idx = get_amp_index(kcontrol);
886 long *valp = ucontrol->value.integer.value;
887
888 if (chs & 1)
889 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
890 if (chs & 2)
891 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
892 return 0;
893}
894
Takashi Iwai0ba21762007-04-16 11:29:14 +0200895int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
896 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
899 hda_nid_t nid = get_amp_nid(kcontrol);
900 int chs = get_amp_channels(kcontrol);
901 int dir = get_amp_direction(kcontrol);
902 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 long *valp = ucontrol->value.integer.value;
904 int change = 0;
905
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200906 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200907 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
908 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200909 valp++;
910 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200911 if (chs & 2)
912 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200913 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return change;
915}
916
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200917int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
918 unsigned int size, unsigned int __user *_tlv)
919{
920 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
921 hda_nid_t nid = get_amp_nid(kcontrol);
922 int dir = get_amp_direction(kcontrol);
923 u32 caps, val1, val2;
924
925 if (size < 4 * sizeof(unsigned int))
926 return -ENOMEM;
927 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200928 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
929 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200930 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
931 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200932 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
933 return -EFAULT;
934 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
935 return -EFAULT;
936 if (put_user(val1, _tlv + 2))
937 return -EFAULT;
938 if (put_user(val2, _tlv + 3))
939 return -EFAULT;
940 return 0;
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200944int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 int chs = get_amp_channels(kcontrol);
948
949 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
950 uinfo->count = chs == 3 ? 2 : 1;
951 uinfo->value.integer.min = 0;
952 uinfo->value.integer.max = 1;
953 return 0;
954}
955
Takashi Iwai0ba21762007-04-16 11:29:14 +0200956int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
957 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
960 hda_nid_t nid = get_amp_nid(kcontrol);
961 int chs = get_amp_channels(kcontrol);
962 int dir = get_amp_direction(kcontrol);
963 int idx = get_amp_index(kcontrol);
964 long *valp = ucontrol->value.integer.value;
965
966 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +0200967 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
968 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +0200970 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
971 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
973}
974
Takashi Iwai0ba21762007-04-16 11:29:14 +0200975int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
979 hda_nid_t nid = get_amp_nid(kcontrol);
980 int chs = get_amp_channels(kcontrol);
981 int dir = get_amp_direction(kcontrol);
982 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 long *valp = ucontrol->value.integer.value;
984 int change = 0;
985
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200986 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200987 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
988 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200989 valp++;
990 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200991 if (chs & 2)
992 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200993 0x80, *valp ? 0 : 0x80);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return change;
996}
997
998/*
Takashi Iwai985be542005-11-02 18:26:49 +0100999 * bound volume controls
1000 *
1001 * bind multiple volumes (# indices, from 0)
1002 */
1003
1004#define AMP_VAL_IDX_SHIFT 19
1005#define AMP_VAL_IDX_MASK (0x0f<<19)
1006
Takashi Iwai0ba21762007-04-16 11:29:14 +02001007int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1008 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001009{
1010 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1011 unsigned long pval;
1012 int err;
1013
Ingo Molnar62932df2006-01-16 16:34:20 +01001014 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001015 pval = kcontrol->private_value;
1016 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1017 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1018 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001019 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001020 return err;
1021}
1022
Takashi Iwai0ba21762007-04-16 11:29:14 +02001023int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1024 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001025{
1026 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1027 unsigned long pval;
1028 int i, indices, err = 0, change = 0;
1029
Ingo Molnar62932df2006-01-16 16:34:20 +01001030 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001031 pval = kcontrol->private_value;
1032 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1033 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001034 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1035 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001036 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1037 if (err < 0)
1038 break;
1039 change |= err;
1040 }
1041 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001042 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001043 return err < 0 ? err : change;
1044}
1045
1046/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001047 * generic bound volume/swtich controls
1048 */
1049int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1050 struct snd_ctl_elem_info *uinfo)
1051{
1052 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1053 struct hda_bind_ctls *c;
1054 int err;
1055
1056 c = (struct hda_bind_ctls *)kcontrol->private_value;
1057 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1058 kcontrol->private_value = *c->values;
1059 err = c->ops->info(kcontrol, uinfo);
1060 kcontrol->private_value = (long)c;
1061 mutex_unlock(&codec->spdif_mutex);
1062 return err;
1063}
1064
1065int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1066 struct snd_ctl_elem_value *ucontrol)
1067{
1068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1069 struct hda_bind_ctls *c;
1070 int err;
1071
1072 c = (struct hda_bind_ctls *)kcontrol->private_value;
1073 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1074 kcontrol->private_value = *c->values;
1075 err = c->ops->get(kcontrol, ucontrol);
1076 kcontrol->private_value = (long)c;
1077 mutex_unlock(&codec->spdif_mutex);
1078 return err;
1079}
1080
1081int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1082 struct snd_ctl_elem_value *ucontrol)
1083{
1084 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1085 struct hda_bind_ctls *c;
1086 unsigned long *vals;
1087 int err = 0, change = 0;
1088
1089 c = (struct hda_bind_ctls *)kcontrol->private_value;
1090 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1091 for (vals = c->values; *vals; vals++) {
1092 kcontrol->private_value = *vals;
1093 err = c->ops->put(kcontrol, ucontrol);
1094 if (err < 0)
1095 break;
1096 change |= err;
1097 }
1098 kcontrol->private_value = (long)c;
1099 mutex_unlock(&codec->spdif_mutex);
1100 return err < 0 ? err : change;
1101}
1102
1103int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1104 unsigned int size, unsigned int __user *tlv)
1105{
1106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1107 struct hda_bind_ctls *c;
1108 int err;
1109
1110 c = (struct hda_bind_ctls *)kcontrol->private_value;
1111 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1112 kcontrol->private_value = *c->values;
1113 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1114 kcontrol->private_value = (long)c;
1115 mutex_unlock(&codec->spdif_mutex);
1116 return err;
1117}
1118
1119struct hda_ctl_ops snd_hda_bind_vol = {
1120 .info = snd_hda_mixer_amp_volume_info,
1121 .get = snd_hda_mixer_amp_volume_get,
1122 .put = snd_hda_mixer_amp_volume_put,
1123 .tlv = snd_hda_mixer_amp_tlv
1124};
1125
1126struct hda_ctl_ops snd_hda_bind_sw = {
1127 .info = snd_hda_mixer_amp_switch_info,
1128 .get = snd_hda_mixer_amp_switch_get,
1129 .put = snd_hda_mixer_amp_switch_put,
1130 .tlv = snd_hda_mixer_amp_tlv
1131};
1132
1133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * SPDIF out controls
1135 */
1136
Takashi Iwai0ba21762007-04-16 11:29:14 +02001137static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1138 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1141 uinfo->count = 1;
1142 return 0;
1143}
1144
Takashi Iwai0ba21762007-04-16 11:29:14 +02001145static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1146 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1149 IEC958_AES0_NONAUDIO |
1150 IEC958_AES0_CON_EMPHASIS_5015 |
1151 IEC958_AES0_CON_NOT_COPYRIGHT;
1152 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1153 IEC958_AES1_CON_ORIGINAL;
1154 return 0;
1155}
1156
Takashi Iwai0ba21762007-04-16 11:29:14 +02001157static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1158 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1161 IEC958_AES0_NONAUDIO |
1162 IEC958_AES0_PRO_EMPHASIS_5015;
1163 return 0;
1164}
1165
Takashi Iwai0ba21762007-04-16 11:29:14 +02001166static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1167 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1170
1171 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1172 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1173 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1174 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1175
1176 return 0;
1177}
1178
1179/* convert from SPDIF status bits to HDA SPDIF bits
1180 * bit 0 (DigEn) is always set zero (to be filled later)
1181 */
1182static unsigned short convert_from_spdif_status(unsigned int sbits)
1183{
1184 unsigned short val = 0;
1185
1186 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001187 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001189 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001191 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1192 IEC958_AES0_PRO_EMPHASIS_5015)
1193 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001195 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1196 IEC958_AES0_CON_EMPHASIS_5015)
1197 val |= AC_DIG1_EMPHASIS;
1198 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1199 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001201 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1203 }
1204 return val;
1205}
1206
1207/* convert to SPDIF status bits from HDA SPDIF bits
1208 */
1209static unsigned int convert_to_spdif_status(unsigned short val)
1210{
1211 unsigned int sbits = 0;
1212
Takashi Iwai0ba21762007-04-16 11:29:14 +02001213 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001215 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sbits |= IEC958_AES0_PROFESSIONAL;
1217 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001218 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1220 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001221 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001223 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001225 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1227 sbits |= val & (0x7f << 8);
1228 }
1229 return sbits;
1230}
1231
Takashi Iwai0ba21762007-04-16 11:29:14 +02001232static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1233 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1236 hda_nid_t nid = kcontrol->private_value;
1237 unsigned short val;
1238 int change;
1239
Ingo Molnar62932df2006-01-16 16:34:20 +01001240 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 codec->spdif_status = ucontrol->value.iec958.status[0] |
1242 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1243 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1244 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1245 val = convert_from_spdif_status(codec->spdif_status);
1246 val |= codec->spdif_ctls & 1;
1247 change = codec->spdif_ctls != val;
1248 codec->spdif_ctls = val;
1249
1250 if (change || codec->in_resume) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001251 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1252 val & 0xff);
1253 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1254 val >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Ingo Molnar62932df2006-01-16 16:34:20 +01001257 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return change;
1259}
1260
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001261#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Takashi Iwai0ba21762007-04-16 11:29:14 +02001263static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1264 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1267
Takashi Iwai0ba21762007-04-16 11:29:14 +02001268 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
Takashi Iwai0ba21762007-04-16 11:29:14 +02001272static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1276 hda_nid_t nid = kcontrol->private_value;
1277 unsigned short val;
1278 int change;
1279
Ingo Molnar62932df2006-01-16 16:34:20 +01001280 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001281 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001283 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 change = codec->spdif_ctls != val;
1285 if (change || codec->in_resume) {
1286 codec->spdif_ctls = val;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001287 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1288 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289 /* unmute amp switch (if any) */
1290 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1291 (val & AC_DIG1_ENABLE))
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001292 snd_hda_codec_write(codec, nid, 0,
1293 AC_VERB_SET_AMP_GAIN_MUTE,
1294 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 AC_AMP_SET_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001297 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return change;
1299}
1300
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001301static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 {
1303 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1305 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1306 .info = snd_hda_spdif_mask_info,
1307 .get = snd_hda_spdif_cmask_get,
1308 },
1309 {
1310 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1311 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1312 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1313 .info = snd_hda_spdif_mask_info,
1314 .get = snd_hda_spdif_pmask_get,
1315 },
1316 {
1317 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1318 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1319 .info = snd_hda_spdif_mask_info,
1320 .get = snd_hda_spdif_default_get,
1321 .put = snd_hda_spdif_default_put,
1322 },
1323 {
1324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1325 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1326 .info = snd_hda_spdif_out_switch_info,
1327 .get = snd_hda_spdif_out_switch_get,
1328 .put = snd_hda_spdif_out_switch_put,
1329 },
1330 { } /* end */
1331};
1332
1333/**
1334 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1335 * @codec: the HDA codec
1336 * @nid: audio out widget NID
1337 *
1338 * Creates controls related with the SPDIF output.
1339 * Called from each patch supporting the SPDIF out.
1340 *
1341 * Returns 0 if successful, or a negative error code.
1342 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001343int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001346 struct snd_kcontrol *kctl;
1347 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1350 kctl = snd_ctl_new1(dig_mix, codec);
1351 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001352 err = snd_ctl_add(codec->bus->card, kctl);
1353 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return err;
1355 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001356 codec->spdif_ctls =
1357 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1359 return 0;
1360}
1361
1362/*
1363 * SPDIF input
1364 */
1365
1366#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1367
Takashi Iwai0ba21762007-04-16 11:29:14 +02001368static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1372
1373 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1374 return 0;
1375}
1376
Takashi Iwai0ba21762007-04-16 11:29:14 +02001377static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1378 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1381 hda_nid_t nid = kcontrol->private_value;
1382 unsigned int val = !!ucontrol->value.integer.value[0];
1383 int change;
1384
Ingo Molnar62932df2006-01-16 16:34:20 +01001385 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 change = codec->spdif_in_enable != val;
1387 if (change || codec->in_resume) {
1388 codec->spdif_in_enable = val;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001389 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1390 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001392 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return change;
1394}
1395
Takashi Iwai0ba21762007-04-16 11:29:14 +02001396static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1400 hda_nid_t nid = kcontrol->private_value;
1401 unsigned short val;
1402 unsigned int sbits;
1403
1404 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1405 sbits = convert_to_spdif_status(val);
1406 ucontrol->value.iec958.status[0] = sbits;
1407 ucontrol->value.iec958.status[1] = sbits >> 8;
1408 ucontrol->value.iec958.status[2] = sbits >> 16;
1409 ucontrol->value.iec958.status[3] = sbits >> 24;
1410 return 0;
1411}
1412
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001413static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 {
1415 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1416 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1417 .info = snd_hda_spdif_in_switch_info,
1418 .get = snd_hda_spdif_in_switch_get,
1419 .put = snd_hda_spdif_in_switch_put,
1420 },
1421 {
1422 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1424 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1425 .info = snd_hda_spdif_mask_info,
1426 .get = snd_hda_spdif_in_status_get,
1427 },
1428 { } /* end */
1429};
1430
1431/**
1432 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1433 * @codec: the HDA codec
1434 * @nid: audio in widget NID
1435 *
1436 * Creates controls related with the SPDIF input.
1437 * Called from each patch supporting the SPDIF in.
1438 *
1439 * Returns 0 if successful, or a negative error code.
1440 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001441int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001444 struct snd_kcontrol *kctl;
1445 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1448 kctl = snd_ctl_new1(dig_mix, codec);
1449 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001450 err = snd_ctl_add(codec->bus->card, kctl);
1451 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return err;
1453 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001454 codec->spdif_in_enable =
1455 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1456 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
1458}
1459
1460
Takashi Iwai54d17402005-11-21 16:33:22 +01001461/*
1462 * set power state of the codec
1463 */
1464static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1465 unsigned int power_state)
1466{
1467 hda_nid_t nid, nid_start;
1468 int nodes;
1469
1470 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1471 power_state);
1472
1473 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1474 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1475 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1476 snd_hda_codec_write(codec, nid, 0,
1477 AC_VERB_SET_POWER_STATE,
1478 power_state);
1479 }
1480
1481 if (power_state == AC_PWRST_D0)
1482 msleep(10);
1483}
1484
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/**
1487 * snd_hda_build_controls - build mixer controls
1488 * @bus: the BUS
1489 *
1490 * Creates mixer controls for each codec included in the bus.
1491 *
1492 * Returns 0 if successful, otherwise a negative error code.
1493 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001494int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001496 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /* build controls */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 if (!codec->patch_ops.build_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 continue;
1503 err = codec->patch_ops.build_controls(codec);
1504 if (err < 0)
1505 return err;
1506 }
1507
1508 /* initialize */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001509 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001511 hda_set_power_state(codec,
1512 codec->afg ? codec->afg : codec->mfg,
1513 AC_PWRST_D0);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001514 if (!codec->patch_ops.init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 continue;
1516 err = codec->patch_ops.init(codec);
1517 if (err < 0)
1518 return err;
1519 }
1520 return 0;
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/*
1524 * stream formats
1525 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001526struct hda_rate_tbl {
1527 unsigned int hz;
1528 unsigned int alsa_bits;
1529 unsigned int hda_fmt;
1530};
1531
1532static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001534
1535 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1537 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1538 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1539 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1540 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1541 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1542 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1543 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1544 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1545 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1546 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001547#define AC_PAR_PCM_RATE_BITS 11
1548 /* up to bits 10, 384kHZ isn't supported properly */
1549
1550 /* not autodetected value */
1551 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001552
Takashi Iwaibefdf312005-08-22 13:57:55 +02001553 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554};
1555
1556/**
1557 * snd_hda_calc_stream_format - calculate format bitset
1558 * @rate: the sample rate
1559 * @channels: the number of channels
1560 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1561 * @maxbps: the max. bps
1562 *
1563 * Calculate the format bitset from the given rate, channels and th PCM format.
1564 *
1565 * Return zero if invalid.
1566 */
1567unsigned int snd_hda_calc_stream_format(unsigned int rate,
1568 unsigned int channels,
1569 unsigned int format,
1570 unsigned int maxbps)
1571{
1572 int i;
1573 unsigned int val = 0;
1574
Takashi Iwaibefdf312005-08-22 13:57:55 +02001575 for (i = 0; rate_bits[i].hz; i++)
1576 if (rate_bits[i].hz == rate) {
1577 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
1579 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001580 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 snd_printdd("invalid rate %d\n", rate);
1582 return 0;
1583 }
1584
1585 if (channels == 0 || channels > 8) {
1586 snd_printdd("invalid channels %d\n", channels);
1587 return 0;
1588 }
1589 val |= channels - 1;
1590
1591 switch (snd_pcm_format_width(format)) {
1592 case 8: val |= 0x00; break;
1593 case 16: val |= 0x10; break;
1594 case 20:
1595 case 24:
1596 case 32:
1597 if (maxbps >= 32)
1598 val |= 0x40;
1599 else if (maxbps >= 24)
1600 val |= 0x30;
1601 else
1602 val |= 0x20;
1603 break;
1604 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001605 snd_printdd("invalid format width %d\n",
1606 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return 0;
1608 }
1609
1610 return val;
1611}
1612
1613/**
1614 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1615 * @codec: the HDA codec
1616 * @nid: NID to query
1617 * @ratesp: the pointer to store the detected rate bitflags
1618 * @formatsp: the pointer to store the detected formats
1619 * @bpsp: the pointer to store the detected format widths
1620 *
1621 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1622 * or @bsps argument is ignored.
1623 *
1624 * Returns 0 if successful, otherwise a negative error code.
1625 */
1626int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1627 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1628{
1629 int i;
1630 unsigned int val, streams;
1631
1632 val = 0;
1633 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001634 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1636 if (val == -1)
1637 return -EIO;
1638 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001639 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1641
1642 if (ratesp) {
1643 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001644 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001646 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648 *ratesp = rates;
1649 }
1650
1651 if (formatsp || bpsp) {
1652 u64 formats = 0;
1653 unsigned int bps;
1654 unsigned int wcaps;
1655
Takashi Iwai54d17402005-11-21 16:33:22 +01001656 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1658 if (streams == -1)
1659 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001660 if (!streams) {
1661 streams = snd_hda_param_read(codec, codec->afg,
1662 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (streams == -1)
1664 return -EIO;
1665 }
1666
1667 bps = 0;
1668 if (streams & AC_SUPFMT_PCM) {
1669 if (val & AC_SUPPCM_BITS_8) {
1670 formats |= SNDRV_PCM_FMTBIT_U8;
1671 bps = 8;
1672 }
1673 if (val & AC_SUPPCM_BITS_16) {
1674 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1675 bps = 16;
1676 }
1677 if (wcaps & AC_WCAP_DIGITAL) {
1678 if (val & AC_SUPPCM_BITS_32)
1679 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1680 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1681 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1682 if (val & AC_SUPPCM_BITS_24)
1683 bps = 24;
1684 else if (val & AC_SUPPCM_BITS_20)
1685 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001686 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1687 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1689 if (val & AC_SUPPCM_BITS_32)
1690 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 else if (val & AC_SUPPCM_BITS_24)
1692 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02001693 else if (val & AC_SUPPCM_BITS_20)
1694 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001697 else if (streams == AC_SUPFMT_FLOAT32) {
1698 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1700 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001701 } else if (streams == AC_SUPFMT_AC3) {
1702 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* temporary hack: we have still no proper support
1704 * for the direct AC3 stream...
1705 */
1706 formats |= SNDRV_PCM_FMTBIT_U8;
1707 bps = 8;
1708 }
1709 if (formatsp)
1710 *formatsp = formats;
1711 if (bpsp)
1712 *bpsp = bps;
1713 }
1714
1715 return 0;
1716}
1717
1718/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02001719 * snd_hda_is_supported_format - check whether the given node supports
1720 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *
1722 * Returns 1 if supported, 0 if not.
1723 */
1724int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1725 unsigned int format)
1726{
1727 int i;
1728 unsigned int val = 0, rate, stream;
1729
1730 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001731 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1733 if (val == -1)
1734 return 0;
1735 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001736 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1738 if (val == -1)
1739 return 0;
1740 }
1741
1742 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001743 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02001744 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (val & (1 << i))
1746 break;
1747 return 0;
1748 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001749 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751
1752 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1753 if (stream == -1)
1754 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001755 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001757 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759
1760 if (stream & AC_SUPFMT_PCM) {
1761 switch (format & 0xf0) {
1762 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001763 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
1765 break;
1766 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001767 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769 break;
1770 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001771 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773 break;
1774 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001775 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
1777 break;
1778 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001779 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781 break;
1782 default:
1783 return 0;
1784 }
1785 } else {
1786 /* FIXME: check for float32 and AC3? */
1787 }
1788
1789 return 1;
1790}
1791
1792/*
1793 * PCM stuff
1794 */
1795static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1796 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001797 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 return 0;
1800}
1801
1802static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1803 struct hda_codec *codec,
1804 unsigned int stream_tag,
1805 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001806 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1809 return 0;
1810}
1811
1812static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1813 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001814 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1817 return 0;
1818}
1819
Takashi Iwai0ba21762007-04-16 11:29:14 +02001820static int __devinit set_pcm_default_values(struct hda_codec *codec,
1821 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001823 /* query support PCM information from the given NID */
1824 if (info->nid && (!info->rates || !info->formats)) {
1825 snd_hda_query_supported_pcm(codec, info->nid,
1826 info->rates ? NULL : &info->rates,
1827 info->formats ? NULL : &info->formats,
1828 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 if (info->ops.open == NULL)
1831 info->ops.open = hda_pcm_default_open_close;
1832 if (info->ops.close == NULL)
1833 info->ops.close = hda_pcm_default_open_close;
1834 if (info->ops.prepare == NULL) {
1835 snd_assert(info->nid, return -EINVAL);
1836 info->ops.prepare = hda_pcm_default_prepare;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (info->ops.cleanup == NULL) {
1839 snd_assert(info->nid, return -EINVAL);
1840 info->ops.cleanup = hda_pcm_default_cleanup;
1841 }
1842 return 0;
1843}
1844
1845/**
1846 * snd_hda_build_pcms - build PCM information
1847 * @bus: the BUS
1848 *
1849 * Create PCM information for each codec included in the bus.
1850 *
1851 * The build_pcms codec patch is requested to set up codec->num_pcms and
1852 * codec->pcm_info properly. The array is referred by the top-level driver
1853 * to create its PCM instances.
1854 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1855 * callback.
1856 *
1857 * At least, substreams, channels_min and channels_max must be filled for
1858 * each stream. substreams = 0 indicates that the stream doesn't exist.
1859 * When rates and/or formats are zero, the supported values are queried
1860 * from the given nid. The nid is used also by the default ops.prepare
1861 * and ops.cleanup callbacks.
1862 *
1863 * The driver needs to call ops.open in its open callback. Similarly,
1864 * ops.close is supposed to be called in the close callback.
1865 * ops.prepare should be called in the prepare or hw_params callback
1866 * with the proper parameters for set up.
1867 * ops.cleanup should be called in hw_free for clean up of streams.
1868 *
1869 * This function returns 0 if successfull, or a negative error code.
1870 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001871int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001873 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Takashi Iwai0ba21762007-04-16 11:29:14 +02001875 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 unsigned int pcm, s;
1877 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001878 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 continue;
1880 err = codec->patch_ops.build_pcms(codec);
1881 if (err < 0)
1882 return err;
1883 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1884 for (s = 0; s < 2; s++) {
1885 struct hda_pcm_stream *info;
1886 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02001887 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 continue;
1889 err = set_pcm_default_values(codec, info);
1890 if (err < 0)
1891 return err;
1892 }
1893 }
1894 }
1895 return 0;
1896}
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/**
1899 * snd_hda_check_board_config - compare the current codec with the config table
1900 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001901 * @num_configs: number of config enums
1902 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * @tbl: configuration table, terminated by null entries
1904 *
1905 * Compares the modelname or PCI subsystem id of the current codec with the
1906 * given configuration table. If a matching entry is found, returns its
1907 * config value (supposed to be 0 or positive).
1908 *
1909 * If no entries are matching, the function returns a negative value.
1910 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001911int snd_hda_check_board_config(struct hda_codec *codec,
1912 int num_configs, const char **models,
1913 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001915 if (codec->bus->modelname && models) {
1916 int i;
1917 for (i = 0; i < num_configs; i++) {
1918 if (models[i] &&
1919 !strcmp(codec->bus->modelname, models[i])) {
1920 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1921 "selected\n", models[i]);
1922 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924 }
1925 }
1926
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001927 if (!codec->bus->pci || !tbl)
1928 return -1;
1929
1930 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1931 if (!tbl)
1932 return -1;
1933 if (tbl->value >= 0 && tbl->value < num_configs) {
1934#ifdef CONFIG_SND_DEBUG_DETECT
1935 char tmp[10];
1936 const char *model = NULL;
1937 if (models)
1938 model = models[tbl->value];
1939 if (!model) {
1940 sprintf(tmp, "#%d", tbl->value);
1941 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001943 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1944 "for config %x:%x (%s)\n",
1945 model, tbl->subvendor, tbl->subdevice,
1946 (tbl->name ? tbl->name : "Unknown device"));
1947#endif
1948 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 return -1;
1951}
1952
1953/**
1954 * snd_hda_add_new_ctls - create controls from the array
1955 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001956 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 *
1958 * This helper function creates and add new controls in the given array.
1959 * The array must be terminated with an empty entry as terminator.
1960 *
1961 * Returns 0 if successful, or a negative error code.
1962 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001963int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int err;
1966
1967 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001968 struct snd_kcontrol *kctl;
1969 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001970 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001971 return -ENOMEM;
1972 err = snd_ctl_add(codec->bus->card, kctl);
1973 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001974 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01001975 return err;
1976 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001977 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001978 return -ENOMEM;
1979 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001980 err = snd_ctl_add(codec->bus->card, kctl);
1981 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01001982 return err;
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 return 0;
1986}
1987
1988
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001989/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001990 * Channel mode helper
1991 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001992int snd_hda_ch_mode_info(struct hda_codec *codec,
1993 struct snd_ctl_elem_info *uinfo,
1994 const struct hda_channel_mode *chmode,
1995 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001996{
1997 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1998 uinfo->count = 1;
1999 uinfo->value.enumerated.items = num_chmodes;
2000 if (uinfo->value.enumerated.item >= num_chmodes)
2001 uinfo->value.enumerated.item = num_chmodes - 1;
2002 sprintf(uinfo->value.enumerated.name, "%dch",
2003 chmode[uinfo->value.enumerated.item].channels);
2004 return 0;
2005}
2006
Takashi Iwai0ba21762007-04-16 11:29:14 +02002007int snd_hda_ch_mode_get(struct hda_codec *codec,
2008 struct snd_ctl_elem_value *ucontrol,
2009 const struct hda_channel_mode *chmode,
2010 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002011 int max_channels)
2012{
2013 int i;
2014
2015 for (i = 0; i < num_chmodes; i++) {
2016 if (max_channels == chmode[i].channels) {
2017 ucontrol->value.enumerated.item[0] = i;
2018 break;
2019 }
2020 }
2021 return 0;
2022}
2023
Takashi Iwai0ba21762007-04-16 11:29:14 +02002024int snd_hda_ch_mode_put(struct hda_codec *codec,
2025 struct snd_ctl_elem_value *ucontrol,
2026 const struct hda_channel_mode *chmode,
2027 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002028 int *max_channelsp)
2029{
2030 unsigned int mode;
2031
2032 mode = ucontrol->value.enumerated.item[0];
2033 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002034 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002035 return 0;
2036 /* change the current channel setting */
2037 *max_channelsp = chmode[mode].channels;
2038 if (chmode[mode].sequence)
2039 snd_hda_sequence_write(codec, chmode[mode].sequence);
2040 return 1;
2041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * input MUX helper
2045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002046int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2047 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 unsigned int index;
2050
2051 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2052 uinfo->count = 1;
2053 uinfo->value.enumerated.items = imux->num_items;
2054 index = uinfo->value.enumerated.item;
2055 if (index >= imux->num_items)
2056 index = imux->num_items - 1;
2057 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2058 return 0;
2059}
2060
Takashi Iwai0ba21762007-04-16 11:29:14 +02002061int snd_hda_input_mux_put(struct hda_codec *codec,
2062 const struct hda_input_mux *imux,
2063 struct snd_ctl_elem_value *ucontrol,
2064 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 unsigned int *cur_val)
2066{
2067 unsigned int idx;
2068
2069 idx = ucontrol->value.enumerated.item[0];
2070 if (idx >= imux->num_items)
2071 idx = imux->num_items - 1;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002072 if (*cur_val == idx && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
2074 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2075 imux->items[idx].index);
2076 *cur_val = idx;
2077 return 1;
2078}
2079
2080
2081/*
2082 * Multi-channel / digital-out PCM helper functions
2083 */
2084
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002085/* setup SPDIF output stream */
2086static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2087 unsigned int stream_tag, unsigned int format)
2088{
2089 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2090 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2091 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2092 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2093 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2094 /* turn on again (if needed) */
2095 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2096 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2097 codec->spdif_ctls & 0xff);
2098}
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/*
2101 * open the digital out in the exclusive mode
2102 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002103int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2104 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Ingo Molnar62932df2006-01-16 16:34:20 +01002106 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002107 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2108 /* already opened as analog dup; reset it once */
2109 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002111 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
2113}
2114
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002115int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2116 struct hda_multi_out *mout,
2117 unsigned int stream_tag,
2118 unsigned int format,
2119 struct snd_pcm_substream *substream)
2120{
2121 mutex_lock(&codec->spdif_mutex);
2122 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2123 mutex_unlock(&codec->spdif_mutex);
2124 return 0;
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
2128 * release the digital out
2129 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002130int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2131 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Ingo Molnar62932df2006-01-16 16:34:20 +01002133 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002135 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138
2139/*
2140 * set up more restrictions for analog out
2141 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002142int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2143 struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002144 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 substream->runtime->hw.channels_max = mout->max_channels;
2147 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2148 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2149}
2150
2151/*
2152 * set up the i/o for analog out
2153 * when the digital out is available, copy the front out to digital out, too.
2154 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2156 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 unsigned int stream_tag,
2158 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002159 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 hda_nid_t *nids = mout->dac_nids;
2162 int chs = substream->runtime->channels;
2163 int i;
2164
Ingo Molnar62932df2006-01-16 16:34:20 +01002165 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2167 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002168 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2169 format) &&
2170 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002172 setup_dig_out_stream(codec, mout->dig_out_nid,
2173 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 } else {
2175 mout->dig_out_used = 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002176 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2177 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002180 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2184 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02002185 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002187 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2188 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002189 /* extra outputs copied from front */
2190 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2191 if (mout->extra_out_nid[i])
2192 snd_hda_codec_setup_stream(codec,
2193 mout->extra_out_nid[i],
2194 stream_tag, 0, format);
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* surrounds */
2197 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002198 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002199 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2200 i * 2, format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002201 else /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002202 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2203 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 return 0;
2206}
2207
2208/*
2209 * clean up the setting for analog out
2210 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002211int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2212 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 hda_nid_t *nids = mout->dac_nids;
2215 int i;
2216
2217 for (i = 0; i < mout->num_dacs; i++)
2218 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2219 if (mout->hp_nid)
2220 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002221 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2222 if (mout->extra_out_nid[i])
2223 snd_hda_codec_setup_stream(codec,
2224 mout->extra_out_nid[i],
2225 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01002226 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2228 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2229 mout->dig_out_used = 0;
2230 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002231 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return 0;
2233}
2234
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002235/*
2236 * Helper for automatic ping configuration
2237 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002238
Takashi Iwai12f288b2007-08-02 15:51:59 +02002239static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002240{
2241 for (; *list; list++)
2242 if (*list == nid)
2243 return 1;
2244 return 0;
2245}
2246
Steve Longerbeam81937d32007-05-08 15:33:03 +02002247
2248/*
2249 * Sort an associated group of pins according to their sequence numbers.
2250 */
2251static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2252 int num_pins)
2253{
2254 int i, j;
2255 short seq;
2256 hda_nid_t nid;
2257
2258 for (i = 0; i < num_pins; i++) {
2259 for (j = i + 1; j < num_pins; j++) {
2260 if (sequences[i] > sequences[j]) {
2261 seq = sequences[i];
2262 sequences[i] = sequences[j];
2263 sequences[j] = seq;
2264 nid = pins[i];
2265 pins[i] = pins[j];
2266 pins[j] = nid;
2267 }
2268 }
2269 }
2270}
2271
2272
Takashi Iwai82bc9552006-03-21 11:24:42 +01002273/*
2274 * Parse all pin widgets and store the useful pin nids to cfg
2275 *
2276 * The number of line-outs or any primary output is stored in line_outs,
2277 * and the corresponding output pins are assigned to line_out_pins[],
2278 * in the order of front, rear, CLFE, side, ...
2279 *
2280 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002281 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002282 * is detected, one of speaker of HP pins is assigned as the primary
2283 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2284 * if any analog output exists.
2285 *
2286 * The analog input pins are assigned to input_pins array.
2287 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2288 * respectively.
2289 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002290int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2291 struct auto_pin_cfg *cfg,
2292 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002293{
2294 hda_nid_t nid, nid_start;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002295 int nodes;
2296 short seq, assoc_line_out, assoc_speaker;
2297 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2298 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002299
2300 memset(cfg, 0, sizeof(*cfg));
2301
Steve Longerbeam81937d32007-05-08 15:33:03 +02002302 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2303 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2304 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002305
2306 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2307 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002308 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002309 unsigned int wid_type =
2310 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002311 unsigned int def_conf;
2312 short assoc, loc;
2313
2314 /* read all default configuration for pin complex */
2315 if (wid_type != AC_WID_PIN)
2316 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002317 /* ignore the given nids (e.g. pc-beep returns error) */
2318 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2319 continue;
2320
Takashi Iwai0ba21762007-04-16 11:29:14 +02002321 def_conf = snd_hda_codec_read(codec, nid, 0,
2322 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002323 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2324 continue;
2325 loc = get_defcfg_location(def_conf);
2326 switch (get_defcfg_device(def_conf)) {
2327 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002328 seq = get_defcfg_sequence(def_conf);
2329 assoc = get_defcfg_association(def_conf);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002330 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002331 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002332 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002333 assoc_line_out = assoc;
2334 else if (assoc_line_out != assoc)
2335 continue;
2336 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2337 continue;
2338 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002339 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002340 cfg->line_outs++;
2341 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002342 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002343 seq = get_defcfg_sequence(def_conf);
2344 assoc = get_defcfg_association(def_conf);
2345 if (! assoc)
2346 continue;
2347 if (! assoc_speaker)
2348 assoc_speaker = assoc;
2349 else if (assoc_speaker != assoc)
2350 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002351 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2352 continue;
2353 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002354 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002355 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002356 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002357 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002358 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2359 continue;
2360 cfg->hp_pins[cfg->hp_outs] = nid;
2361 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002362 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002363 case AC_JACK_MIC_IN: {
2364 int preferred, alt;
2365 if (loc == AC_JACK_LOC_FRONT) {
2366 preferred = AUTO_PIN_FRONT_MIC;
2367 alt = AUTO_PIN_MIC;
2368 } else {
2369 preferred = AUTO_PIN_MIC;
2370 alt = AUTO_PIN_FRONT_MIC;
2371 }
2372 if (!cfg->input_pins[preferred])
2373 cfg->input_pins[preferred] = nid;
2374 else if (!cfg->input_pins[alt])
2375 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002376 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002377 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002378 case AC_JACK_LINE_IN:
2379 if (loc == AC_JACK_LOC_FRONT)
2380 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2381 else
2382 cfg->input_pins[AUTO_PIN_LINE] = nid;
2383 break;
2384 case AC_JACK_CD:
2385 cfg->input_pins[AUTO_PIN_CD] = nid;
2386 break;
2387 case AC_JACK_AUX:
2388 cfg->input_pins[AUTO_PIN_AUX] = nid;
2389 break;
2390 case AC_JACK_SPDIF_OUT:
2391 cfg->dig_out_pin = nid;
2392 break;
2393 case AC_JACK_SPDIF_IN:
2394 cfg->dig_in_pin = nid;
2395 break;
2396 }
2397 }
2398
2399 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02002400 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2401 cfg->line_outs);
2402 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2403 cfg->speaker_outs);
2404
2405 /*
2406 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2407 * as a primary output
2408 */
2409 if (!cfg->line_outs) {
2410 if (cfg->speaker_outs) {
2411 cfg->line_outs = cfg->speaker_outs;
2412 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2413 sizeof(cfg->speaker_pins));
2414 cfg->speaker_outs = 0;
2415 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2416 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2417 } else if (cfg->hp_outs) {
2418 cfg->line_outs = cfg->hp_outs;
2419 memcpy(cfg->line_out_pins, cfg->hp_pins,
2420 sizeof(cfg->hp_pins));
2421 cfg->hp_outs = 0;
2422 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2423 cfg->line_out_type = AUTO_PIN_HP_OUT;
2424 }
2425 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002426
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002427 /* Reorder the surround channels
2428 * ALSA sequence is front/surr/clfe/side
2429 * HDA sequence is:
2430 * 4-ch: front/surr => OK as it is
2431 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02002432 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002433 */
2434 switch (cfg->line_outs) {
2435 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002436 case 4:
2437 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02002438 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002439 cfg->line_out_pins[2] = nid;
2440 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002441 }
2442
Takashi Iwai82bc9552006-03-21 11:24:42 +01002443 /*
2444 * debug prints of the parsed results
2445 */
2446 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2447 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2448 cfg->line_out_pins[2], cfg->line_out_pins[3],
2449 cfg->line_out_pins[4]);
2450 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2451 cfg->speaker_outs, cfg->speaker_pins[0],
2452 cfg->speaker_pins[1], cfg->speaker_pins[2],
2453 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002454 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2455 cfg->hp_outs, cfg->hp_pins[0],
2456 cfg->hp_pins[1], cfg->hp_pins[2],
2457 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002458 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2459 " cd=0x%x, aux=0x%x\n",
2460 cfg->input_pins[AUTO_PIN_MIC],
2461 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2462 cfg->input_pins[AUTO_PIN_LINE],
2463 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2464 cfg->input_pins[AUTO_PIN_CD],
2465 cfg->input_pins[AUTO_PIN_AUX]);
2466
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002467 return 0;
2468}
2469
Takashi Iwai4a471b72005-12-07 13:56:29 +01002470/* labels for input pins */
2471const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2472 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2473};
2474
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476#ifdef CONFIG_PM
2477/*
2478 * power management
2479 */
2480
2481/**
2482 * snd_hda_suspend - suspend the codecs
2483 * @bus: the HDA bus
2484 * @state: suspsend state
2485 *
2486 * Returns 0 if successful.
2487 */
2488int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2489{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002490 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 /* FIXME: should handle power widget capabilities */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002493 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (codec->patch_ops.suspend)
2495 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002496 hda_set_power_state(codec,
2497 codec->afg ? codec->afg : codec->mfg,
2498 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500 return 0;
2501}
2502
2503/**
2504 * snd_hda_resume - resume the codecs
2505 * @bus: the HDA bus
2506 * @state: resume state
2507 *
2508 * Returns 0 if successful.
2509 */
2510int snd_hda_resume(struct hda_bus *bus)
2511{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002512 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Takashi Iwai0ba21762007-04-16 11:29:14 +02002514 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002515 hda_set_power_state(codec,
2516 codec->afg ? codec->afg : codec->mfg,
2517 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (codec->patch_ops.resume)
2519 codec->patch_ops.resume(codec);
2520 }
2521 return 0;
2522}
2523
2524/**
2525 * snd_hda_resume_ctls - resume controls in the new control list
2526 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002527 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002529 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 * originally for snd_hda_add_new_ctls().
2531 * The array must be terminated with an empty entry as terminator.
2532 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002533int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002535 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 val = kmalloc(sizeof(*val), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002538 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return -ENOMEM;
2540 codec->in_resume = 1;
2541 for (; knew->name; knew++) {
2542 int i, count;
2543 count = knew->count ? knew->count : 1;
2544 for (i = 0; i < count; i++) {
2545 memset(val, 0, sizeof(*val));
2546 val->id.iface = knew->iface;
2547 val->id.device = knew->device;
2548 val->id.subdevice = knew->subdevice;
2549 strcpy(val->id.name, knew->name);
2550 val->id.index = knew->index ? knew->index : i;
2551 /* Assume that get callback reads only from cache,
2552 * not accessing to the real hardware
2553 */
2554 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2555 continue;
2556 snd_ctl_elem_write(codec->bus->card, NULL, val);
2557 }
2558 }
2559 codec->in_resume = 0;
2560 kfree(val);
2561 return 0;
2562}
2563
2564/**
2565 * snd_hda_resume_spdif_out - resume the digital out
2566 * @codec: the HDA codec
2567 */
2568int snd_hda_resume_spdif_out(struct hda_codec *codec)
2569{
2570 return snd_hda_resume_ctls(codec, dig_mixes);
2571}
2572
2573/**
2574 * snd_hda_resume_spdif_in - resume the digital in
2575 * @codec: the HDA codec
2576 */
2577int snd_hda_resume_spdif_in(struct hda_codec *codec)
2578{
2579 return snd_hda_resume_ctls(codec, dig_in_ctls);
2580}
2581#endif