blob: fe4acfa9e4e6b2a375628f1439220973cf0ee702 [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"
34
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x10ec, "Realtek" },
Takashi Iwaia9226252006-09-17 22:05:54 +020048 { 0x1057, "Motorola" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010049 { 0x1106, "VIA" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020050 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020052 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020054 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 {} /* terminator */
56};
57
58/* codec presets */
59#include "hda_patch.h"
60
61
62/**
63 * snd_hda_codec_read - send a command and get the response
64 * @codec: the HDA codec
65 * @nid: NID to send the command
66 * @direct: direct flag
67 * @verb: the verb to send
68 * @parm: the parameter for the verb
69 *
70 * Send a single command and read the corresponding response.
71 *
72 * Returns the obtained response value, or -1 for an error.
73 */
74unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
75 unsigned int verb, unsigned int parm)
76{
77 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010078 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
80 res = codec->bus->ops.get_response(codec);
81 else
82 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010083 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return res;
85}
86
87/**
88 * snd_hda_codec_write - send a single command without waiting for response
89 * @codec: the HDA codec
90 * @nid: NID to send the command
91 * @direct: direct flag
92 * @verb: the verb to send
93 * @parm: the parameter for the verb
94 *
95 * Send a single command without waiting for response.
96 *
97 * Returns 0 if successful, or a negative error code.
98 */
99int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
100 unsigned int verb, unsigned int parm)
101{
102 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100103 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100105 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return err;
107}
108
109/**
110 * snd_hda_sequence_write - sequence writes
111 * @codec: the HDA codec
112 * @seq: VERB array to send
113 *
114 * Send the commands sequentially from the given array.
115 * The array must be terminated with NID=0.
116 */
117void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
118{
119 for (; seq->nid; seq++)
120 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
121}
122
123/**
124 * snd_hda_get_sub_nodes - get the range of sub nodes
125 * @codec: the HDA codec
126 * @nid: NID to parse
127 * @start_id: the pointer to store the start NID
128 *
129 * Parse the NID and store the start NID of its sub-nodes.
130 * Returns the number of sub-nodes.
131 */
132int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
133{
134 unsigned int parm;
135
136 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
137 *start_id = (parm >> 16) & 0x7fff;
138 return (int)(parm & 0x7fff);
139}
140
141/**
142 * snd_hda_get_connections - get connection list
143 * @codec: the HDA codec
144 * @nid: NID to parse
145 * @conn_list: connection list array
146 * @max_conns: max. number of connections to store
147 *
148 * Parses the connection list of the given widget and stores the list
149 * of NIDs.
150 *
151 * Returns the number of connections, or a negative error code.
152 */
153int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
154 hda_nid_t *conn_list, int max_conns)
155{
156 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100157 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100159 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 snd_assert(conn_list && max_conns > 0, return -EINVAL);
162
163 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
164 if (parm & AC_CLIST_LONG) {
165 /* long form */
166 shift = 16;
167 num_elems = 2;
168 } else {
169 /* short form */
170 shift = 8;
171 num_elems = 4;
172 }
173 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mask = (1 << (shift-1)) - 1;
175
176 if (! conn_len)
177 return 0; /* no connection */
178
179 if (conn_len == 1) {
180 /* single connection */
181 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
182 conn_list[0] = parm & mask;
183 return 1;
184 }
185
186 /* multi connection */
187 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100188 prev_nid = 0;
189 for (i = 0; i < conn_len; i++) {
190 int range_val;
191 hda_nid_t val, n;
192
193 if (i % num_elems == 0)
194 parm = snd_hda_codec_read(codec, nid, 0,
195 AC_VERB_GET_CONNECT_LIST, i);
196 range_val = !! (parm & (1 << (shift-1))); /* ranges */
197 val = parm & mask;
198 parm >>= shift;
199 if (range_val) {
200 /* ranges between the previous and this one */
201 if (! prev_nid || prev_nid >= val) {
202 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100205 for (n = prev_nid + 1; n <= val; n++) {
206 if (conns >= max_conns) {
207 snd_printk(KERN_ERR "Too many connections\n");
208 return -EINVAL;
209 }
210 conn_list[conns++] = n;
211 }
212 } else {
213 if (conns >= max_conns) {
214 snd_printk(KERN_ERR "Too many connections\n");
215 return -EINVAL;
216 }
217 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100219 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221 return conns;
222}
223
224
225/**
226 * snd_hda_queue_unsol_event - add an unsolicited event to queue
227 * @bus: the BUS
228 * @res: unsolicited event (lower 32bit of RIRB entry)
229 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
230 *
231 * Adds the given event to the queue. The events are processed in
232 * the workqueue asynchronously. Call this function in the interrupt
233 * hanlder when RIRB receives an unsolicited event.
234 *
235 * Returns 0 if successful, or a negative error code.
236 */
237int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
238{
239 struct hda_bus_unsolicited *unsol;
240 unsigned int wp;
241
242 if ((unsol = bus->unsol) == NULL)
243 return 0;
244
245 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
246 unsol->wp = wp;
247
248 wp <<= 1;
249 unsol->queue[wp] = res;
250 unsol->queue[wp + 1] = res_ex;
251
Takashi Iwaie250af22006-12-19 17:08:52 +0100252 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return 0;
255}
256
257/*
258 * process queueud unsolicited events
259 */
David Howellsc4028952006-11-22 14:57:56 +0000260static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howellsc4028952006-11-22 14:57:56 +0000262 struct hda_bus_unsolicited *unsol =
263 container_of(work, struct hda_bus_unsolicited, work);
264 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct hda_codec *codec;
266 unsigned int rp, caddr, res;
267
268 while (unsol->rp != unsol->wp) {
269 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
270 unsol->rp = rp;
271 rp <<= 1;
272 res = unsol->queue[rp];
273 caddr = unsol->queue[rp + 1];
274 if (! (caddr & (1 << 4))) /* no unsolicited event? */
275 continue;
276 codec = bus->caddr_tbl[caddr & 0x0f];
277 if (codec && codec->patch_ops.unsol_event)
278 codec->patch_ops.unsol_event(codec, res);
279 }
280}
281
282/*
283 * initialize unsolicited queue
284 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200285static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct hda_bus_unsolicited *unsol;
288
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100289 if (bus->unsol) /* already initialized */
290 return 0;
291
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200292 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (! unsol) {
294 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
295 return -ENOMEM;
296 }
David Howellsc4028952006-11-22 14:57:56 +0000297 INIT_WORK(&unsol->work, process_unsol_events);
298 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 bus->unsol = unsol;
300 return 0;
301}
302
303/*
304 * destructor
305 */
306static void snd_hda_codec_free(struct hda_codec *codec);
307
308static int snd_hda_bus_free(struct hda_bus *bus)
309{
310 struct list_head *p, *n;
311
312 if (! bus)
313 return 0;
314 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100315 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kfree(bus->unsol);
317 }
318 list_for_each_safe(p, n, &bus->codec_list) {
319 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
320 snd_hda_codec_free(codec);
321 }
322 if (bus->ops.private_free)
323 bus->ops.private_free(bus);
324 kfree(bus);
325 return 0;
326}
327
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100328static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct hda_bus *bus = device->device_data;
331 return snd_hda_bus_free(bus);
332}
333
334/**
335 * snd_hda_bus_new - create a HDA bus
336 * @card: the card entry
337 * @temp: the template for hda_bus information
338 * @busp: the pointer to store the created bus instance
339 *
340 * Returns 0 if successful, or a negative error code.
341 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200342int __devinit snd_hda_bus_new(struct snd_card *card,
343 const struct hda_bus_template *temp,
344 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct hda_bus *bus;
347 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100348 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 .dev_free = snd_hda_bus_dev_free,
350 };
351
352 snd_assert(temp, return -EINVAL);
353 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
354
355 if (busp)
356 *busp = NULL;
357
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200358 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (bus == NULL) {
360 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
361 return -ENOMEM;
362 }
363
364 bus->card = card;
365 bus->private_data = temp->private_data;
366 bus->pci = temp->pci;
367 bus->modelname = temp->modelname;
368 bus->ops = temp->ops;
369
Ingo Molnar62932df2006-01-16 16:34:20 +0100370 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 INIT_LIST_HEAD(&bus->codec_list);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
374 snd_hda_bus_free(bus);
375 return err;
376 }
377 if (busp)
378 *busp = bus;
379 return 0;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * find a matching codec preset
384 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200385static const struct hda_codec_preset __devinit *
386find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 const struct hda_codec_preset **tbl, *preset;
389
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100390 if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
391 return NULL; /* use the generic parser */
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 for (tbl = hda_preset_tables; *tbl; tbl++) {
394 for (preset = *tbl; preset->id; preset++) {
395 u32 mask = preset->mask;
396 if (! mask)
397 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200398 if (preset->id == (codec->vendor_id & mask) &&
399 (! preset->rev ||
400 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return preset;
402 }
403 }
404 return NULL;
405}
406
407/*
408 * snd_hda_get_codec_name - store the codec name
409 */
410void snd_hda_get_codec_name(struct hda_codec *codec,
411 char *name, int namelen)
412{
413 const struct hda_vendor_id *c;
414 const char *vendor = NULL;
415 u16 vendor_id = codec->vendor_id >> 16;
416 char tmp[16];
417
418 for (c = hda_vendor_ids; c->id; c++) {
419 if (c->id == vendor_id) {
420 vendor = c->name;
421 break;
422 }
423 }
424 if (! vendor) {
425 sprintf(tmp, "Generic %04x", vendor_id);
426 vendor = tmp;
427 }
428 if (codec->preset && codec->preset->name)
429 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
430 else
431 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
432}
433
434/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200435 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200437static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 int i, total_nodes;
440 hda_nid_t nid;
441
442 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
443 for (i = 0; i < total_nodes; i++, nid++) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200444 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
445 case AC_GRP_AUDIO_FUNCTION:
446 codec->afg = nid;
447 break;
448 case AC_GRP_MODEM_FUNCTION:
449 codec->mfg = nid;
450 break;
451 default:
452 break;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100458 * read widget caps for each widget and store in cache
459 */
460static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
461{
462 int i;
463 hda_nid_t nid;
464
465 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
466 &codec->start_nid);
467 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
468 if (! codec->wcaps)
469 return -ENOMEM;
470 nid = codec->start_nid;
471 for (i = 0; i < codec->num_nodes; i++, nid++)
472 codec->wcaps[i] = snd_hda_param_read(codec, nid,
473 AC_PAR_AUDIO_WIDGET_CAP);
474 return 0;
475}
476
477
478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * codec destructor
480 */
481static void snd_hda_codec_free(struct hda_codec *codec)
482{
483 if (! codec)
484 return;
485 list_del(&codec->list);
486 codec->bus->caddr_tbl[codec->addr] = NULL;
487 if (codec->patch_ops.free)
488 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100489 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 kfree(codec);
492}
493
494static void init_amp_hash(struct hda_codec *codec);
495
496/**
497 * snd_hda_codec_new - create a HDA codec
498 * @bus: the bus to assign
499 * @codec_addr: the codec address
500 * @codecp: the pointer to store the generated codec
501 *
502 * Returns 0 if successful, or a negative error code.
503 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200504int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
505 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct hda_codec *codec;
508 char component[13];
509 int err;
510
511 snd_assert(bus, return -EINVAL);
512 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
513
514 if (bus->caddr_tbl[codec_addr]) {
515 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
516 return -EBUSY;
517 }
518
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200519 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (codec == NULL) {
521 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
522 return -ENOMEM;
523 }
524
525 codec->bus = bus;
526 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100527 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 init_amp_hash(codec);
529
530 list_add_tail(&codec->list, &bus->codec_list);
531 bus->caddr_tbl[codec_addr] = codec;
532
533 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100534 if (codec->vendor_id == -1)
535 /* read again, hopefully the access method was corrected
536 * in the last read...
537 */
538 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
539 AC_PAR_VENDOR_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
541 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
542
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200543 setup_fg_nodes(codec);
544 if (! codec->afg && ! codec->mfg) {
545 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 snd_hda_codec_free(codec);
547 return -ENODEV;
548 }
549
Takashi Iwai54d17402005-11-21 16:33:22 +0100550 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
551 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
552 snd_hda_codec_free(codec);
553 return -ENOMEM;
554 }
555
Takashi Iwai86284e42005-10-11 15:05:54 +0200556 if (! codec->subsystem_id) {
557 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
558 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_SUBSYSTEM_ID,
560 0);
561 }
562
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100563 codec->preset = find_codec_preset(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (! *bus->card->mixername)
565 snd_hda_get_codec_name(codec, bus->card->mixername,
566 sizeof(bus->card->mixername));
567
568 if (codec->preset && codec->preset->patch)
569 err = codec->preset->patch(codec);
570 else
571 err = snd_hda_parse_generic_codec(codec);
572 if (err < 0) {
573 snd_hda_codec_free(codec);
574 return err;
575 }
576
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100577 if (codec->patch_ops.unsol_event)
578 init_unsol_queue(bus);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 snd_hda_codec_proc_new(codec);
581
582 sprintf(component, "HDA:%08x", codec->vendor_id);
583 snd_component_add(codec->bus->card, component);
584
585 if (codecp)
586 *codecp = codec;
587 return 0;
588}
589
590/**
591 * snd_hda_codec_setup_stream - set up the codec for streaming
592 * @codec: the CODEC to set up
593 * @nid: the NID to set up
594 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
595 * @channel_id: channel id to pass, zero based.
596 * @format: stream format.
597 */
598void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
599 int channel_id, int format)
600{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200601 if (! nid)
602 return;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
605 nid, stream_tag, channel_id, format);
606 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
607 (stream_tag << 4) | channel_id);
608 msleep(1);
609 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * amp access functions
614 */
615
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200616/* FIXME: more better hash key? */
617#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200619#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/* initialize the hash table */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200622static void __devinit init_amp_hash(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
625 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100626 codec->amp_info_size = 0;
627 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/* query the hash. allocate an entry if not found. */
631static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
632{
633 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
634 u16 cur = codec->amp_hash[idx];
635 struct hda_amp_info *info;
636
637 while (cur != 0xffff) {
638 info = &codec->amp_info[cur];
639 if (info->key == key)
640 return info;
641 cur = info->next;
642 }
643
644 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100645 if (codec->num_amp_entries >= codec->amp_info_size) {
646 /* reallocate the array */
647 int new_size = codec->amp_info_size + 64;
648 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
649 GFP_KERNEL);
650 if (! new_info) {
651 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
652 return NULL;
653 }
654 if (codec->amp_info) {
655 memcpy(new_info, codec->amp_info,
656 codec->amp_info_size * sizeof(struct hda_amp_info));
657 kfree(codec->amp_info);
658 }
659 codec->amp_info_size = new_size;
660 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 cur = codec->num_amp_entries++;
663 info = &codec->amp_info[cur];
664 info->key = key;
665 info->status = 0; /* not initialized yet */
666 info->next = codec->amp_hash[idx];
667 codec->amp_hash[idx] = cur;
668
669 return info;
670}
671
672/*
673 * query AMP capabilities for the given widget and direction
674 */
675static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
676{
677 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
678
679 if (! info)
680 return 0;
681 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100682 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 nid = codec->afg;
684 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
685 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
686 info->status |= INFO_AMP_CAPS;
687 }
688 return info->amp_caps;
689}
690
691/*
692 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200693 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200695static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 hda_nid_t nid, int ch, int direction, int index)
697{
698 u32 val, parm;
699
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200700 if (info->status & INFO_AMP_VOL(ch))
701 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
704 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
705 parm |= index;
706 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
707 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200708 info->status |= INFO_AMP_VOL(ch);
709 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200713 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200715static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 hda_nid_t nid, int ch, int direction, int index, int val)
717{
718 u32 parm;
719
720 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
721 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
722 parm |= index << AC_AMP_SET_INDEX_SHIFT;
723 parm |= val;
724 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200725 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200729 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Takashi Iwai834be882006-03-01 14:16:17 +0100731int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
732 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
735 if (! info)
736 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200737 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200740/*
741 * update the AMP value, mask = bit mask to set, val = the value
742 */
Takashi Iwai834be882006-03-01 14:16:17 +0100743int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
744 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (! info)
749 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200750 val &= mask;
751 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (info->vol[ch] == val && ! codec->in_resume)
753 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200754 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 1;
756}
757
758
759/*
760 * AMP control callbacks
761 */
762/* retrieve parameters from private_value */
763#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
764#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
765#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
766#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
767
768/* volume */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100769int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
772 u16 nid = get_amp_nid(kcontrol);
773 u8 chs = get_amp_channels(kcontrol);
774 int dir = get_amp_direction(kcontrol);
775 u32 caps;
776
777 caps = query_amp_caps(codec, nid, dir);
778 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
779 if (! caps) {
780 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
781 return -EINVAL;
782 }
783 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
784 uinfo->count = chs == 3 ? 2 : 1;
785 uinfo->value.integer.min = 0;
786 uinfo->value.integer.max = caps;
787 return 0;
788}
789
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100790int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
793 hda_nid_t nid = get_amp_nid(kcontrol);
794 int chs = get_amp_channels(kcontrol);
795 int dir = get_amp_direction(kcontrol);
796 int idx = get_amp_index(kcontrol);
797 long *valp = ucontrol->value.integer.value;
798
799 if (chs & 1)
800 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
801 if (chs & 2)
802 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
803 return 0;
804}
805
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100806int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
809 hda_nid_t nid = get_amp_nid(kcontrol);
810 int chs = get_amp_channels(kcontrol);
811 int dir = get_amp_direction(kcontrol);
812 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 long *valp = ucontrol->value.integer.value;
814 int change = 0;
815
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200816 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200817 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
818 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200819 valp++;
820 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200821 if (chs & 2)
822 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200823 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return change;
825}
826
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200827int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
828 unsigned int size, unsigned int __user *_tlv)
829{
830 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
831 hda_nid_t nid = get_amp_nid(kcontrol);
832 int dir = get_amp_direction(kcontrol);
833 u32 caps, val1, val2;
834
835 if (size < 4 * sizeof(unsigned int))
836 return -ENOMEM;
837 caps = query_amp_caps(codec, nid, dir);
838 val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25;
839 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
840 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200841 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
842 return -EFAULT;
843 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
844 return -EFAULT;
845 if (put_user(val1, _tlv + 2))
846 return -EFAULT;
847 if (put_user(val2, _tlv + 3))
848 return -EFAULT;
849 return 0;
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/* switch */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100853int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 int chs = get_amp_channels(kcontrol);
856
857 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
858 uinfo->count = chs == 3 ? 2 : 1;
859 uinfo->value.integer.min = 0;
860 uinfo->value.integer.max = 1;
861 return 0;
862}
863
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100864int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
867 hda_nid_t nid = get_amp_nid(kcontrol);
868 int chs = get_amp_channels(kcontrol);
869 int dir = get_amp_direction(kcontrol);
870 int idx = get_amp_index(kcontrol);
871 long *valp = ucontrol->value.integer.value;
872
873 if (chs & 1)
874 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
875 if (chs & 2)
876 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
877 return 0;
878}
879
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100880int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
883 hda_nid_t nid = get_amp_nid(kcontrol);
884 int chs = get_amp_channels(kcontrol);
885 int dir = get_amp_direction(kcontrol);
886 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 long *valp = ucontrol->value.integer.value;
888 int change = 0;
889
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200890 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200891 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
892 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200893 valp++;
894 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200895 if (chs & 2)
896 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200897 0x80, *valp ? 0 : 0x80);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return change;
900}
901
902/*
Takashi Iwai985be542005-11-02 18:26:49 +0100903 * bound volume controls
904 *
905 * bind multiple volumes (# indices, from 0)
906 */
907
908#define AMP_VAL_IDX_SHIFT 19
909#define AMP_VAL_IDX_MASK (0x0f<<19)
910
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100911int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100912{
913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
914 unsigned long pval;
915 int err;
916
Ingo Molnar62932df2006-01-16 16:34:20 +0100917 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100918 pval = kcontrol->private_value;
919 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
920 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
921 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100922 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100923 return err;
924}
925
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100926int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100927{
928 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
929 unsigned long pval;
930 int i, indices, err = 0, change = 0;
931
Ingo Molnar62932df2006-01-16 16:34:20 +0100932 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100933 pval = kcontrol->private_value;
934 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
935 for (i = 0; i < indices; i++) {
936 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
937 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
938 if (err < 0)
939 break;
940 change |= err;
941 }
942 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100943 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100944 return err < 0 ? err : change;
945}
946
947/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * SPDIF out controls
949 */
950
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100951static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
954 uinfo->count = 1;
955 return 0;
956}
957
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100958static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
961 IEC958_AES0_NONAUDIO |
962 IEC958_AES0_CON_EMPHASIS_5015 |
963 IEC958_AES0_CON_NOT_COPYRIGHT;
964 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
965 IEC958_AES1_CON_ORIGINAL;
966 return 0;
967}
968
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100969static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
972 IEC958_AES0_NONAUDIO |
973 IEC958_AES0_PRO_EMPHASIS_5015;
974 return 0;
975}
976
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100977static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
980
981 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
982 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
983 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
984 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
985
986 return 0;
987}
988
989/* convert from SPDIF status bits to HDA SPDIF bits
990 * bit 0 (DigEn) is always set zero (to be filled later)
991 */
992static unsigned short convert_from_spdif_status(unsigned int sbits)
993{
994 unsigned short val = 0;
995
996 if (sbits & IEC958_AES0_PROFESSIONAL)
997 val |= 1 << 6;
998 if (sbits & IEC958_AES0_NONAUDIO)
999 val |= 1 << 5;
1000 if (sbits & IEC958_AES0_PROFESSIONAL) {
1001 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1002 val |= 1 << 3;
1003 } else {
1004 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
1005 val |= 1 << 3;
1006 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1007 val |= 1 << 4;
1008 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1009 val |= 1 << 7;
1010 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1011 }
1012 return val;
1013}
1014
1015/* convert to SPDIF status bits from HDA SPDIF bits
1016 */
1017static unsigned int convert_to_spdif_status(unsigned short val)
1018{
1019 unsigned int sbits = 0;
1020
1021 if (val & (1 << 5))
1022 sbits |= IEC958_AES0_NONAUDIO;
1023 if (val & (1 << 6))
1024 sbits |= IEC958_AES0_PROFESSIONAL;
1025 if (sbits & IEC958_AES0_PROFESSIONAL) {
1026 if (sbits & (1 << 3))
1027 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1028 } else {
1029 if (val & (1 << 3))
1030 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1031 if (! (val & (1 << 4)))
1032 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1033 if (val & (1 << 7))
1034 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1035 sbits |= val & (0x7f << 8);
1036 }
1037 return sbits;
1038}
1039
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001040static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1043 hda_nid_t nid = kcontrol->private_value;
1044 unsigned short val;
1045 int change;
1046
Ingo Molnar62932df2006-01-16 16:34:20 +01001047 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 codec->spdif_status = ucontrol->value.iec958.status[0] |
1049 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1050 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1051 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1052 val = convert_from_spdif_status(codec->spdif_status);
1053 val |= codec->spdif_ctls & 1;
1054 change = codec->spdif_ctls != val;
1055 codec->spdif_ctls = val;
1056
1057 if (change || codec->in_resume) {
1058 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1059 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1060 }
1061
Ingo Molnar62932df2006-01-16 16:34:20 +01001062 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return change;
1064}
1065
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001066static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1069 uinfo->count = 1;
1070 uinfo->value.integer.min = 0;
1071 uinfo->value.integer.max = 1;
1072 return 0;
1073}
1074
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001075static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1078
1079 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1080 return 0;
1081}
1082
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001083static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1086 hda_nid_t nid = kcontrol->private_value;
1087 unsigned short val;
1088 int change;
1089
Ingo Molnar62932df2006-01-16 16:34:20 +01001090 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 val = codec->spdif_ctls & ~1;
1092 if (ucontrol->value.integer.value[0])
1093 val |= 1;
1094 change = codec->spdif_ctls != val;
1095 if (change || codec->in_resume) {
1096 codec->spdif_ctls = val;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001097 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1098 val & 0xff);
1099 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
1100 snd_hda_codec_write(codec, nid, 0,
1101 AC_VERB_SET_AMP_GAIN_MUTE,
1102 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1103 AC_AMP_SET_OUTPUT |
1104 ((val & 1) ? 0 : 0x80));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001106 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return change;
1108}
1109
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001110static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 {
1112 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1114 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1115 .info = snd_hda_spdif_mask_info,
1116 .get = snd_hda_spdif_cmask_get,
1117 },
1118 {
1119 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1120 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1121 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1122 .info = snd_hda_spdif_mask_info,
1123 .get = snd_hda_spdif_pmask_get,
1124 },
1125 {
1126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1127 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1128 .info = snd_hda_spdif_mask_info,
1129 .get = snd_hda_spdif_default_get,
1130 .put = snd_hda_spdif_default_put,
1131 },
1132 {
1133 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1135 .info = snd_hda_spdif_out_switch_info,
1136 .get = snd_hda_spdif_out_switch_get,
1137 .put = snd_hda_spdif_out_switch_put,
1138 },
1139 { } /* end */
1140};
1141
1142/**
1143 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1144 * @codec: the HDA codec
1145 * @nid: audio out widget NID
1146 *
1147 * Creates controls related with the SPDIF output.
1148 * Called from each patch supporting the SPDIF out.
1149 *
1150 * Returns 0 if successful, or a negative error code.
1151 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001152int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
1153 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001156 struct snd_kcontrol *kctl;
1157 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1160 kctl = snd_ctl_new1(dig_mix, codec);
1161 kctl->private_value = nid;
1162 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1163 return err;
1164 }
1165 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1166 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1167 return 0;
1168}
1169
1170/*
1171 * SPDIF input
1172 */
1173
1174#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1175
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001176static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1179
1180 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1181 return 0;
1182}
1183
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001184static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1187 hda_nid_t nid = kcontrol->private_value;
1188 unsigned int val = !!ucontrol->value.integer.value[0];
1189 int change;
1190
Ingo Molnar62932df2006-01-16 16:34:20 +01001191 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 change = codec->spdif_in_enable != val;
1193 if (change || codec->in_resume) {
1194 codec->spdif_in_enable = val;
1195 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1196 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001197 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return change;
1199}
1200
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001201static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1204 hda_nid_t nid = kcontrol->private_value;
1205 unsigned short val;
1206 unsigned int sbits;
1207
1208 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1209 sbits = convert_to_spdif_status(val);
1210 ucontrol->value.iec958.status[0] = sbits;
1211 ucontrol->value.iec958.status[1] = sbits >> 8;
1212 ucontrol->value.iec958.status[2] = sbits >> 16;
1213 ucontrol->value.iec958.status[3] = sbits >> 24;
1214 return 0;
1215}
1216
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001217static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 {
1219 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1220 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1221 .info = snd_hda_spdif_in_switch_info,
1222 .get = snd_hda_spdif_in_switch_get,
1223 .put = snd_hda_spdif_in_switch_put,
1224 },
1225 {
1226 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1227 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1228 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1229 .info = snd_hda_spdif_mask_info,
1230 .get = snd_hda_spdif_in_status_get,
1231 },
1232 { } /* end */
1233};
1234
1235/**
1236 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1237 * @codec: the HDA codec
1238 * @nid: audio in widget NID
1239 *
1240 * Creates controls related with the SPDIF input.
1241 * Called from each patch supporting the SPDIF in.
1242 *
1243 * Returns 0 if successful, or a negative error code.
1244 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001245int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
1246 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001249 struct snd_kcontrol *kctl;
1250 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1253 kctl = snd_ctl_new1(dig_mix, codec);
1254 kctl->private_value = nid;
1255 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1256 return err;
1257 }
1258 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1259 return 0;
1260}
1261
1262
Takashi Iwai54d17402005-11-21 16:33:22 +01001263/*
1264 * set power state of the codec
1265 */
1266static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1267 unsigned int power_state)
1268{
1269 hda_nid_t nid, nid_start;
1270 int nodes;
1271
1272 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1273 power_state);
1274
1275 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1276 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1277 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1278 snd_hda_codec_write(codec, nid, 0,
1279 AC_VERB_SET_POWER_STATE,
1280 power_state);
1281 }
1282
1283 if (power_state == AC_PWRST_D0)
1284 msleep(10);
1285}
1286
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/**
1289 * snd_hda_build_controls - build mixer controls
1290 * @bus: the BUS
1291 *
1292 * Creates mixer controls for each codec included in the bus.
1293 *
1294 * Returns 0 if successful, otherwise a negative error code.
1295 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001296int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct list_head *p;
1299
1300 /* build controls */
1301 list_for_each(p, &bus->codec_list) {
1302 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1303 int err;
1304 if (! codec->patch_ops.build_controls)
1305 continue;
1306 err = codec->patch_ops.build_controls(codec);
1307 if (err < 0)
1308 return err;
1309 }
1310
1311 /* initialize */
1312 list_for_each(p, &bus->codec_list) {
1313 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1314 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001315 hda_set_power_state(codec,
1316 codec->afg ? codec->afg : codec->mfg,
1317 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (! codec->patch_ops.init)
1319 continue;
1320 err = codec->patch_ops.init(codec);
1321 if (err < 0)
1322 return err;
1323 }
1324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * stream formats
1329 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001330struct hda_rate_tbl {
1331 unsigned int hz;
1332 unsigned int alsa_bits;
1333 unsigned int hda_fmt;
1334};
1335
1336static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001338
1339 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1341 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1342 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1343 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1344 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1345 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1346 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1347 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1348 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1349 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1350 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001351#define AC_PAR_PCM_RATE_BITS 11
1352 /* up to bits 10, 384kHZ isn't supported properly */
1353
1354 /* not autodetected value */
1355 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001356
Takashi Iwaibefdf312005-08-22 13:57:55 +02001357 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358};
1359
1360/**
1361 * snd_hda_calc_stream_format - calculate format bitset
1362 * @rate: the sample rate
1363 * @channels: the number of channels
1364 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1365 * @maxbps: the max. bps
1366 *
1367 * Calculate the format bitset from the given rate, channels and th PCM format.
1368 *
1369 * Return zero if invalid.
1370 */
1371unsigned int snd_hda_calc_stream_format(unsigned int rate,
1372 unsigned int channels,
1373 unsigned int format,
1374 unsigned int maxbps)
1375{
1376 int i;
1377 unsigned int val = 0;
1378
Takashi Iwaibefdf312005-08-22 13:57:55 +02001379 for (i = 0; rate_bits[i].hz; i++)
1380 if (rate_bits[i].hz == rate) {
1381 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001384 if (! rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 snd_printdd("invalid rate %d\n", rate);
1386 return 0;
1387 }
1388
1389 if (channels == 0 || channels > 8) {
1390 snd_printdd("invalid channels %d\n", channels);
1391 return 0;
1392 }
1393 val |= channels - 1;
1394
1395 switch (snd_pcm_format_width(format)) {
1396 case 8: val |= 0x00; break;
1397 case 16: val |= 0x10; break;
1398 case 20:
1399 case 24:
1400 case 32:
1401 if (maxbps >= 32)
1402 val |= 0x40;
1403 else if (maxbps >= 24)
1404 val |= 0x30;
1405 else
1406 val |= 0x20;
1407 break;
1408 default:
1409 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1410 return 0;
1411 }
1412
1413 return val;
1414}
1415
1416/**
1417 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1418 * @codec: the HDA codec
1419 * @nid: NID to query
1420 * @ratesp: the pointer to store the detected rate bitflags
1421 * @formatsp: the pointer to store the detected formats
1422 * @bpsp: the pointer to store the detected format widths
1423 *
1424 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1425 * or @bsps argument is ignored.
1426 *
1427 * Returns 0 if successful, otherwise a negative error code.
1428 */
1429int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1430 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1431{
1432 int i;
1433 unsigned int val, streams;
1434
1435 val = 0;
1436 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001437 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1439 if (val == -1)
1440 return -EIO;
1441 }
1442 if (! val)
1443 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1444
1445 if (ratesp) {
1446 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001447 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001449 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 *ratesp = rates;
1452 }
1453
1454 if (formatsp || bpsp) {
1455 u64 formats = 0;
1456 unsigned int bps;
1457 unsigned int wcaps;
1458
Takashi Iwai54d17402005-11-21 16:33:22 +01001459 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1461 if (streams == -1)
1462 return -EIO;
1463 if (! streams) {
1464 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1465 if (streams == -1)
1466 return -EIO;
1467 }
1468
1469 bps = 0;
1470 if (streams & AC_SUPFMT_PCM) {
1471 if (val & AC_SUPPCM_BITS_8) {
1472 formats |= SNDRV_PCM_FMTBIT_U8;
1473 bps = 8;
1474 }
1475 if (val & AC_SUPPCM_BITS_16) {
1476 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1477 bps = 16;
1478 }
1479 if (wcaps & AC_WCAP_DIGITAL) {
1480 if (val & AC_SUPPCM_BITS_32)
1481 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1482 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1483 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1484 if (val & AC_SUPPCM_BITS_24)
1485 bps = 24;
1486 else if (val & AC_SUPPCM_BITS_20)
1487 bps = 20;
1488 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1489 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1490 if (val & AC_SUPPCM_BITS_32)
1491 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 else if (val & AC_SUPPCM_BITS_24)
1493 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02001494 else if (val & AC_SUPPCM_BITS_20)
1495 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 }
1498 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1499 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1500 bps = 32;
1501 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1502 /* temporary hack: we have still no proper support
1503 * for the direct AC3 stream...
1504 */
1505 formats |= SNDRV_PCM_FMTBIT_U8;
1506 bps = 8;
1507 }
1508 if (formatsp)
1509 *formatsp = formats;
1510 if (bpsp)
1511 *bpsp = bps;
1512 }
1513
1514 return 0;
1515}
1516
1517/**
1518 * snd_hda_is_supported_format - check whether the given node supports the format val
1519 *
1520 * Returns 1 if supported, 0 if not.
1521 */
1522int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1523 unsigned int format)
1524{
1525 int i;
1526 unsigned int val = 0, rate, stream;
1527
1528 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001529 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1531 if (val == -1)
1532 return 0;
1533 }
1534 if (! val) {
1535 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1536 if (val == -1)
1537 return 0;
1538 }
1539
1540 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001541 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02001542 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (val & (1 << i))
1544 break;
1545 return 0;
1546 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001547 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return 0;
1549
1550 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1551 if (stream == -1)
1552 return 0;
1553 if (! stream && nid != codec->afg)
1554 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1555 if (! stream || stream == -1)
1556 return 0;
1557
1558 if (stream & AC_SUPFMT_PCM) {
1559 switch (format & 0xf0) {
1560 case 0x00:
1561 if (! (val & AC_SUPPCM_BITS_8))
1562 return 0;
1563 break;
1564 case 0x10:
1565 if (! (val & AC_SUPPCM_BITS_16))
1566 return 0;
1567 break;
1568 case 0x20:
1569 if (! (val & AC_SUPPCM_BITS_20))
1570 return 0;
1571 break;
1572 case 0x30:
1573 if (! (val & AC_SUPPCM_BITS_24))
1574 return 0;
1575 break;
1576 case 0x40:
1577 if (! (val & AC_SUPPCM_BITS_32))
1578 return 0;
1579 break;
1580 default:
1581 return 0;
1582 }
1583 } else {
1584 /* FIXME: check for float32 and AC3? */
1585 }
1586
1587 return 1;
1588}
1589
1590/*
1591 * PCM stuff
1592 */
1593static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1594 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001595 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 return 0;
1598}
1599
1600static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1601 struct hda_codec *codec,
1602 unsigned int stream_tag,
1603 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001604 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1607 return 0;
1608}
1609
1610static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1611 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001612 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1615 return 0;
1616}
1617
1618static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1619{
1620 if (info->nid) {
1621 /* query support PCM information from the given NID */
1622 if (! info->rates || ! info->formats)
1623 snd_hda_query_supported_pcm(codec, info->nid,
1624 info->rates ? NULL : &info->rates,
1625 info->formats ? NULL : &info->formats,
1626 info->maxbps ? NULL : &info->maxbps);
1627 }
1628 if (info->ops.open == NULL)
1629 info->ops.open = hda_pcm_default_open_close;
1630 if (info->ops.close == NULL)
1631 info->ops.close = hda_pcm_default_open_close;
1632 if (info->ops.prepare == NULL) {
1633 snd_assert(info->nid, return -EINVAL);
1634 info->ops.prepare = hda_pcm_default_prepare;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (info->ops.cleanup == NULL) {
1637 snd_assert(info->nid, return -EINVAL);
1638 info->ops.cleanup = hda_pcm_default_cleanup;
1639 }
1640 return 0;
1641}
1642
1643/**
1644 * snd_hda_build_pcms - build PCM information
1645 * @bus: the BUS
1646 *
1647 * Create PCM information for each codec included in the bus.
1648 *
1649 * The build_pcms codec patch is requested to set up codec->num_pcms and
1650 * codec->pcm_info properly. The array is referred by the top-level driver
1651 * to create its PCM instances.
1652 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1653 * callback.
1654 *
1655 * At least, substreams, channels_min and channels_max must be filled for
1656 * each stream. substreams = 0 indicates that the stream doesn't exist.
1657 * When rates and/or formats are zero, the supported values are queried
1658 * from the given nid. The nid is used also by the default ops.prepare
1659 * and ops.cleanup callbacks.
1660 *
1661 * The driver needs to call ops.open in its open callback. Similarly,
1662 * ops.close is supposed to be called in the close callback.
1663 * ops.prepare should be called in the prepare or hw_params callback
1664 * with the proper parameters for set up.
1665 * ops.cleanup should be called in hw_free for clean up of streams.
1666 *
1667 * This function returns 0 if successfull, or a negative error code.
1668 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001669int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct list_head *p;
1672
1673 list_for_each(p, &bus->codec_list) {
1674 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1675 unsigned int pcm, s;
1676 int err;
1677 if (! codec->patch_ops.build_pcms)
1678 continue;
1679 err = codec->patch_ops.build_pcms(codec);
1680 if (err < 0)
1681 return err;
1682 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1683 for (s = 0; s < 2; s++) {
1684 struct hda_pcm_stream *info;
1685 info = &codec->pcm_info[pcm].stream[s];
1686 if (! info->substreams)
1687 continue;
1688 err = set_pcm_default_values(codec, info);
1689 if (err < 0)
1690 return err;
1691 }
1692 }
1693 }
1694 return 0;
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/**
1698 * snd_hda_check_board_config - compare the current codec with the config table
1699 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001700 * @num_configs: number of config enums
1701 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * @tbl: configuration table, terminated by null entries
1703 *
1704 * Compares the modelname or PCI subsystem id of the current codec with the
1705 * given configuration table. If a matching entry is found, returns its
1706 * config value (supposed to be 0 or positive).
1707 *
1708 * If no entries are matching, the function returns a negative value.
1709 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001710int __devinit snd_hda_check_board_config(struct hda_codec *codec,
1711 int num_configs, const char **models,
1712 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001714 if (codec->bus->modelname && models) {
1715 int i;
1716 for (i = 0; i < num_configs; i++) {
1717 if (models[i] &&
1718 !strcmp(codec->bus->modelname, models[i])) {
1719 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1720 "selected\n", models[i]);
1721 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 }
1724 }
1725
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001726 if (!codec->bus->pci || !tbl)
1727 return -1;
1728
1729 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1730 if (!tbl)
1731 return -1;
1732 if (tbl->value >= 0 && tbl->value < num_configs) {
1733#ifdef CONFIG_SND_DEBUG_DETECT
1734 char tmp[10];
1735 const char *model = NULL;
1736 if (models)
1737 model = models[tbl->value];
1738 if (!model) {
1739 sprintf(tmp, "#%d", tbl->value);
1740 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001742 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1743 "for config %x:%x (%s)\n",
1744 model, tbl->subvendor, tbl->subdevice,
1745 (tbl->name ? tbl->name : "Unknown device"));
1746#endif
1747 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 return -1;
1750}
1751
1752/**
1753 * snd_hda_add_new_ctls - create controls from the array
1754 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001755 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 *
1757 * This helper function creates and add new controls in the given array.
1758 * The array must be terminated with an empty entry as terminator.
1759 *
1760 * Returns 0 if successful, or a negative error code.
1761 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001762int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
1763 struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 int err;
1766
1767 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001768 struct snd_kcontrol *kctl;
1769 kctl = snd_ctl_new1(knew, codec);
1770 if (! kctl)
1771 return -ENOMEM;
1772 err = snd_ctl_add(codec->bus->card, kctl);
1773 if (err < 0) {
1774 if (! codec->addr)
1775 return err;
1776 kctl = snd_ctl_new1(knew, codec);
1777 if (! kctl)
1778 return -ENOMEM;
1779 kctl->id.device = codec->addr;
1780 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1781 return err;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 return 0;
1785}
1786
1787
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001788/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001789 * Channel mode helper
1790 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001791int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001792 const struct hda_channel_mode *chmode, int num_chmodes)
1793{
1794 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1795 uinfo->count = 1;
1796 uinfo->value.enumerated.items = num_chmodes;
1797 if (uinfo->value.enumerated.item >= num_chmodes)
1798 uinfo->value.enumerated.item = num_chmodes - 1;
1799 sprintf(uinfo->value.enumerated.name, "%dch",
1800 chmode[uinfo->value.enumerated.item].channels);
1801 return 0;
1802}
1803
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001804int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001805 const struct hda_channel_mode *chmode, int num_chmodes,
1806 int max_channels)
1807{
1808 int i;
1809
1810 for (i = 0; i < num_chmodes; i++) {
1811 if (max_channels == chmode[i].channels) {
1812 ucontrol->value.enumerated.item[0] = i;
1813 break;
1814 }
1815 }
1816 return 0;
1817}
1818
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001819int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001820 const struct hda_channel_mode *chmode, int num_chmodes,
1821 int *max_channelsp)
1822{
1823 unsigned int mode;
1824
1825 mode = ucontrol->value.enumerated.item[0];
1826 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001827 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001828 return 0;
1829 /* change the current channel setting */
1830 *max_channelsp = chmode[mode].channels;
1831 if (chmode[mode].sequence)
1832 snd_hda_sequence_write(codec, chmode[mode].sequence);
1833 return 1;
1834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/*
1837 * input MUX helper
1838 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001839int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 unsigned int index;
1842
1843 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1844 uinfo->count = 1;
1845 uinfo->value.enumerated.items = imux->num_items;
1846 index = uinfo->value.enumerated.item;
1847 if (index >= imux->num_items)
1848 index = imux->num_items - 1;
1849 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1850 return 0;
1851}
1852
1853int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001854 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 unsigned int *cur_val)
1856{
1857 unsigned int idx;
1858
1859 idx = ucontrol->value.enumerated.item[0];
1860 if (idx >= imux->num_items)
1861 idx = imux->num_items - 1;
1862 if (*cur_val == idx && ! codec->in_resume)
1863 return 0;
1864 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1865 imux->items[idx].index);
1866 *cur_val = idx;
1867 return 1;
1868}
1869
1870
1871/*
1872 * Multi-channel / digital-out PCM helper functions
1873 */
1874
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001875/* setup SPDIF output stream */
1876static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
1877 unsigned int stream_tag, unsigned int format)
1878{
1879 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
1880 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1881 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1882 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
1883 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
1884 /* turn on again (if needed) */
1885 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1886 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1887 codec->spdif_ctls & 0xff);
1888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*
1891 * open the digital out in the exclusive mode
1892 */
1893int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1894{
Ingo Molnar62932df2006-01-16 16:34:20 +01001895 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02001896 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
1897 /* already opened as analog dup; reset it once */
1898 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001900 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001904int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
1905 struct hda_multi_out *mout,
1906 unsigned int stream_tag,
1907 unsigned int format,
1908 struct snd_pcm_substream *substream)
1909{
1910 mutex_lock(&codec->spdif_mutex);
1911 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
1912 mutex_unlock(&codec->spdif_mutex);
1913 return 0;
1914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916/*
1917 * release the digital out
1918 */
1919int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1920{
Ingo Molnar62932df2006-01-16 16:34:20 +01001921 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001923 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
1927/*
1928 * set up more restrictions for analog out
1929 */
1930int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001931 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 substream->runtime->hw.channels_max = mout->max_channels;
1934 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1935 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1936}
1937
1938/*
1939 * set up the i/o for analog out
1940 * when the digital out is available, copy the front out to digital out, too.
1941 */
1942int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1943 unsigned int stream_tag,
1944 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001945 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 hda_nid_t *nids = mout->dac_nids;
1948 int chs = substream->runtime->channels;
1949 int i;
1950
Ingo Molnar62932df2006-01-16 16:34:20 +01001951 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1953 if (chs == 2 &&
1954 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1955 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1956 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001957 setup_dig_out_stream(codec, mout->dig_out_nid,
1958 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 } else {
1960 mout->dig_out_used = 0;
1961 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1962 }
1963 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001964 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* front */
1967 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02001968 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* headphone out will just decode front left/right (stereo) */
1970 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001971 /* extra outputs copied from front */
1972 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1973 if (mout->extra_out_nid[i])
1974 snd_hda_codec_setup_stream(codec,
1975 mout->extra_out_nid[i],
1976 stream_tag, 0, format);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* surrounds */
1979 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001980 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1982 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001983 else /* copy front */
1984 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1985 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 return 0;
1988}
1989
1990/*
1991 * clean up the setting for analog out
1992 */
1993int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1994{
1995 hda_nid_t *nids = mout->dac_nids;
1996 int i;
1997
1998 for (i = 0; i < mout->num_dacs; i++)
1999 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2000 if (mout->hp_nid)
2001 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002002 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2003 if (mout->extra_out_nid[i])
2004 snd_hda_codec_setup_stream(codec,
2005 mout->extra_out_nid[i],
2006 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01002007 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2009 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2010 mout->dig_out_used = 0;
2011 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002012 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
2014}
2015
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002016/*
2017 * Helper for automatic ping configuration
2018 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002019
Takashi Iwai756e2b02007-04-16 11:27:07 +02002020static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002021{
2022 for (; *list; list++)
2023 if (*list == nid)
2024 return 1;
2025 return 0;
2026}
2027
Takashi Iwai82bc9552006-03-21 11:24:42 +01002028/*
2029 * Parse all pin widgets and store the useful pin nids to cfg
2030 *
2031 * The number of line-outs or any primary output is stored in line_outs,
2032 * and the corresponding output pins are assigned to line_out_pins[],
2033 * in the order of front, rear, CLFE, side, ...
2034 *
2035 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002036 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002037 * is detected, one of speaker of HP pins is assigned as the primary
2038 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2039 * if any analog output exists.
2040 *
2041 * The analog input pins are assigned to input_pins array.
2042 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2043 * respectively.
2044 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002045int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
2046 struct auto_pin_cfg *cfg,
2047 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002048{
2049 hda_nid_t nid, nid_start;
2050 int i, j, nodes;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002051 short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002052
2053 memset(cfg, 0, sizeof(*cfg));
2054
2055 memset(sequences, 0, sizeof(sequences));
2056 assoc_line_out = 0;
2057
2058 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2059 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002060 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002061 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2062 unsigned int def_conf;
2063 short assoc, loc;
2064
2065 /* read all default configuration for pin complex */
2066 if (wid_type != AC_WID_PIN)
2067 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002068 /* ignore the given nids (e.g. pc-beep returns error) */
2069 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2070 continue;
2071
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002072 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
2073 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2074 continue;
2075 loc = get_defcfg_location(def_conf);
2076 switch (get_defcfg_device(def_conf)) {
2077 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002078 seq = get_defcfg_sequence(def_conf);
2079 assoc = get_defcfg_association(def_conf);
2080 if (! assoc)
2081 continue;
2082 if (! assoc_line_out)
2083 assoc_line_out = assoc;
2084 else if (assoc_line_out != assoc)
2085 continue;
2086 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2087 continue;
2088 cfg->line_out_pins[cfg->line_outs] = nid;
2089 sequences[cfg->line_outs] = seq;
2090 cfg->line_outs++;
2091 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002092 case AC_JACK_SPEAKER:
Takashi Iwai82bc9552006-03-21 11:24:42 +01002093 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2094 continue;
2095 cfg->speaker_pins[cfg->speaker_outs] = nid;
2096 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002097 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002098 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002099 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2100 continue;
2101 cfg->hp_pins[cfg->hp_outs] = nid;
2102 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002103 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002104 case AC_JACK_MIC_IN: {
2105 int preferred, alt;
2106 if (loc == AC_JACK_LOC_FRONT) {
2107 preferred = AUTO_PIN_FRONT_MIC;
2108 alt = AUTO_PIN_MIC;
2109 } else {
2110 preferred = AUTO_PIN_MIC;
2111 alt = AUTO_PIN_FRONT_MIC;
2112 }
2113 if (!cfg->input_pins[preferred])
2114 cfg->input_pins[preferred] = nid;
2115 else if (!cfg->input_pins[alt])
2116 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002117 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002118 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002119 case AC_JACK_LINE_IN:
2120 if (loc == AC_JACK_LOC_FRONT)
2121 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2122 else
2123 cfg->input_pins[AUTO_PIN_LINE] = nid;
2124 break;
2125 case AC_JACK_CD:
2126 cfg->input_pins[AUTO_PIN_CD] = nid;
2127 break;
2128 case AC_JACK_AUX:
2129 cfg->input_pins[AUTO_PIN_AUX] = nid;
2130 break;
2131 case AC_JACK_SPDIF_OUT:
2132 cfg->dig_out_pin = nid;
2133 break;
2134 case AC_JACK_SPDIF_IN:
2135 cfg->dig_in_pin = nid;
2136 break;
2137 }
2138 }
2139
2140 /* sort by sequence */
2141 for (i = 0; i < cfg->line_outs; i++)
2142 for (j = i + 1; j < cfg->line_outs; j++)
2143 if (sequences[i] > sequences[j]) {
2144 seq = sequences[i];
2145 sequences[i] = sequences[j];
2146 sequences[j] = seq;
2147 nid = cfg->line_out_pins[i];
2148 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2149 cfg->line_out_pins[j] = nid;
2150 }
2151
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002152 /* Reorder the surround channels
2153 * ALSA sequence is front/surr/clfe/side
2154 * HDA sequence is:
2155 * 4-ch: front/surr => OK as it is
2156 * 6-ch: front/clfe/surr
2157 * 8-ch: front/clfe/side/surr
2158 */
2159 switch (cfg->line_outs) {
2160 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002161 nid = cfg->line_out_pins[1];
2162 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2163 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002164 break;
2165 case 4:
2166 nid = cfg->line_out_pins[1];
2167 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2168 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2169 cfg->line_out_pins[2] = nid;
2170 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002171 }
2172
Takashi Iwai82bc9552006-03-21 11:24:42 +01002173 /*
2174 * debug prints of the parsed results
2175 */
2176 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2177 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2178 cfg->line_out_pins[2], cfg->line_out_pins[3],
2179 cfg->line_out_pins[4]);
2180 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2181 cfg->speaker_outs, cfg->speaker_pins[0],
2182 cfg->speaker_pins[1], cfg->speaker_pins[2],
2183 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002184 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2185 cfg->hp_outs, cfg->hp_pins[0],
2186 cfg->hp_pins[1], cfg->hp_pins[2],
2187 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002188 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2189 " cd=0x%x, aux=0x%x\n",
2190 cfg->input_pins[AUTO_PIN_MIC],
2191 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2192 cfg->input_pins[AUTO_PIN_LINE],
2193 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2194 cfg->input_pins[AUTO_PIN_CD],
2195 cfg->input_pins[AUTO_PIN_AUX]);
2196
2197 /*
2198 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2199 * as a primary output
2200 */
2201 if (! cfg->line_outs) {
2202 if (cfg->speaker_outs) {
2203 cfg->line_outs = cfg->speaker_outs;
2204 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2205 sizeof(cfg->speaker_pins));
2206 cfg->speaker_outs = 0;
2207 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002208 } else if (cfg->hp_outs) {
2209 cfg->line_outs = cfg->hp_outs;
2210 memcpy(cfg->line_out_pins, cfg->hp_pins,
2211 sizeof(cfg->hp_pins));
2212 cfg->hp_outs = 0;
2213 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
Takashi Iwai82bc9552006-03-21 11:24:42 +01002214 }
2215 }
2216
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002217 return 0;
2218}
2219
Takashi Iwai4a471b72005-12-07 13:56:29 +01002220/* labels for input pins */
2221const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2222 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2223};
2224
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226#ifdef CONFIG_PM
2227/*
2228 * power management
2229 */
2230
2231/**
2232 * snd_hda_suspend - suspend the codecs
2233 * @bus: the HDA bus
2234 * @state: suspsend state
2235 *
2236 * Returns 0 if successful.
2237 */
2238int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2239{
2240 struct list_head *p;
2241
2242 /* FIXME: should handle power widget capabilities */
2243 list_for_each(p, &bus->codec_list) {
2244 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2245 if (codec->patch_ops.suspend)
2246 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002247 hda_set_power_state(codec,
2248 codec->afg ? codec->afg : codec->mfg,
2249 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251 return 0;
2252}
2253
2254/**
2255 * snd_hda_resume - resume the codecs
2256 * @bus: the HDA bus
2257 * @state: resume state
2258 *
2259 * Returns 0 if successful.
2260 */
2261int snd_hda_resume(struct hda_bus *bus)
2262{
2263 struct list_head *p;
2264
2265 list_for_each(p, &bus->codec_list) {
2266 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002267 hda_set_power_state(codec,
2268 codec->afg ? codec->afg : codec->mfg,
2269 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (codec->patch_ops.resume)
2271 codec->patch_ops.resume(codec);
2272 }
2273 return 0;
2274}
2275
2276/**
2277 * snd_hda_resume_ctls - resume controls in the new control list
2278 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002279 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002281 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * originally for snd_hda_add_new_ctls().
2283 * The array must be terminated with an empty entry as terminator.
2284 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002285int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002287 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 val = kmalloc(sizeof(*val), GFP_KERNEL);
2290 if (! val)
2291 return -ENOMEM;
2292 codec->in_resume = 1;
2293 for (; knew->name; knew++) {
2294 int i, count;
2295 count = knew->count ? knew->count : 1;
2296 for (i = 0; i < count; i++) {
2297 memset(val, 0, sizeof(*val));
2298 val->id.iface = knew->iface;
2299 val->id.device = knew->device;
2300 val->id.subdevice = knew->subdevice;
2301 strcpy(val->id.name, knew->name);
2302 val->id.index = knew->index ? knew->index : i;
2303 /* Assume that get callback reads only from cache,
2304 * not accessing to the real hardware
2305 */
2306 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2307 continue;
2308 snd_ctl_elem_write(codec->bus->card, NULL, val);
2309 }
2310 }
2311 codec->in_resume = 0;
2312 kfree(val);
2313 return 0;
2314}
2315
2316/**
2317 * snd_hda_resume_spdif_out - resume the digital out
2318 * @codec: the HDA codec
2319 */
2320int snd_hda_resume_spdif_out(struct hda_codec *codec)
2321{
2322 return snd_hda_resume_ctls(codec, dig_mixes);
2323}
2324
2325/**
2326 * snd_hda_resume_spdif_in - resume the digital in
2327 * @codec: the HDA codec
2328 */
2329int snd_hda_resume_spdif_in(struct hda_codec *codec)
2330{
2331 return snd_hda_resume_ctls(codec, dig_in_ctls);
2332}
2333#endif