blob: e14faf5d50530ffc041cd747bb10dc18e135fed9 [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>
27#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
34#include "hda_local.h"
35
36
37MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
39MODULE_LICENSE("GPL");
40
41
42/*
43 * vendor / preset table
44 */
45
46struct hda_vendor_id {
47 unsigned int id;
48 const char *name;
49};
50
51/* codec vendor labels */
52static struct hda_vendor_id hda_vendor_ids[] = {
53 { 0x10ec, "Realtek" },
Takashi Iwaia9226252006-09-17 22:05:54 +020054 { 0x1057, "Motorola" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010055 { 0x1106, "VIA" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020056 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020058 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020060 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 {} /* terminator */
62};
63
64/* codec presets */
65#include "hda_patch.h"
66
67
68/**
69 * snd_hda_codec_read - send a command and get the response
70 * @codec: the HDA codec
71 * @nid: NID to send the command
72 * @direct: direct flag
73 * @verb: the verb to send
74 * @parm: the parameter for the verb
75 *
76 * Send a single command and read the corresponding response.
77 *
78 * Returns the obtained response value, or -1 for an error.
79 */
80unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
81 unsigned int verb, unsigned int parm)
82{
83 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010084 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
86 res = codec->bus->ops.get_response(codec);
87 else
88 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010089 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return res;
91}
92
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +020093EXPORT_SYMBOL(snd_hda_codec_read);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/**
96 * snd_hda_codec_write - send a single command without waiting for response
97 * @codec: the HDA codec
98 * @nid: NID to send the command
99 * @direct: direct flag
100 * @verb: the verb to send
101 * @parm: the parameter for the verb
102 *
103 * Send a single command without waiting for response.
104 *
105 * Returns 0 if successful, or a negative error code.
106 */
107int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
108 unsigned int verb, unsigned int parm)
109{
110 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100111 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100113 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return err;
115}
116
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200117EXPORT_SYMBOL(snd_hda_codec_write);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/**
120 * snd_hda_sequence_write - sequence writes
121 * @codec: the HDA codec
122 * @seq: VERB array to send
123 *
124 * Send the commands sequentially from the given array.
125 * The array must be terminated with NID=0.
126 */
127void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
128{
129 for (; seq->nid; seq++)
130 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
131}
132
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200133EXPORT_SYMBOL(snd_hda_sequence_write);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
136 * snd_hda_get_sub_nodes - get the range of sub nodes
137 * @codec: the HDA codec
138 * @nid: NID to parse
139 * @start_id: the pointer to store the start NID
140 *
141 * Parse the NID and store the start NID of its sub-nodes.
142 * Returns the number of sub-nodes.
143 */
144int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
145{
146 unsigned int parm;
147
148 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
149 *start_id = (parm >> 16) & 0x7fff;
150 return (int)(parm & 0x7fff);
151}
152
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200153EXPORT_SYMBOL(snd_hda_get_sub_nodes);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/**
156 * snd_hda_get_connections - get connection list
157 * @codec: the HDA codec
158 * @nid: NID to parse
159 * @conn_list: connection list array
160 * @max_conns: max. number of connections to store
161 *
162 * Parses the connection list of the given widget and stores the list
163 * of NIDs.
164 *
165 * Returns the number of connections, or a negative error code.
166 */
167int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
168 hda_nid_t *conn_list, int max_conns)
169{
170 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100171 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100173 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 snd_assert(conn_list && max_conns > 0, return -EINVAL);
176
177 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
178 if (parm & AC_CLIST_LONG) {
179 /* long form */
180 shift = 16;
181 num_elems = 2;
182 } else {
183 /* short form */
184 shift = 8;
185 num_elems = 4;
186 }
187 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 mask = (1 << (shift-1)) - 1;
189
190 if (! conn_len)
191 return 0; /* no connection */
192
193 if (conn_len == 1) {
194 /* single connection */
195 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
196 conn_list[0] = parm & mask;
197 return 1;
198 }
199
200 /* multi connection */
201 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100202 prev_nid = 0;
203 for (i = 0; i < conn_len; i++) {
204 int range_val;
205 hda_nid_t val, n;
206
207 if (i % num_elems == 0)
208 parm = snd_hda_codec_read(codec, nid, 0,
209 AC_VERB_GET_CONNECT_LIST, i);
210 range_val = !! (parm & (1 << (shift-1))); /* ranges */
211 val = parm & mask;
212 parm >>= shift;
213 if (range_val) {
214 /* ranges between the previous and this one */
215 if (! prev_nid || prev_nid >= val) {
216 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
217 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100219 for (n = prev_nid + 1; n <= val; n++) {
220 if (conns >= max_conns) {
221 snd_printk(KERN_ERR "Too many connections\n");
222 return -EINVAL;
223 }
224 conn_list[conns++] = n;
225 }
226 } else {
227 if (conns >= max_conns) {
228 snd_printk(KERN_ERR "Too many connections\n");
229 return -EINVAL;
230 }
231 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100233 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 return conns;
236}
237
238
239/**
240 * snd_hda_queue_unsol_event - add an unsolicited event to queue
241 * @bus: the BUS
242 * @res: unsolicited event (lower 32bit of RIRB entry)
243 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
244 *
245 * Adds the given event to the queue. The events are processed in
246 * the workqueue asynchronously. Call this function in the interrupt
247 * hanlder when RIRB receives an unsolicited event.
248 *
249 * Returns 0 if successful, or a negative error code.
250 */
251int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
252{
253 struct hda_bus_unsolicited *unsol;
254 unsigned int wp;
255
256 if ((unsol = bus->unsol) == NULL)
257 return 0;
258
259 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
260 unsol->wp = wp;
261
262 wp <<= 1;
263 unsol->queue[wp] = res;
264 unsol->queue[wp + 1] = res_ex;
265
266 queue_work(unsol->workq, &unsol->work);
267
268 return 0;
269}
270
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200271EXPORT_SYMBOL(snd_hda_queue_unsol_event);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/*
274 * process queueud unsolicited events
275 */
David Howellsc4028952006-11-22 14:57:56 +0000276static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
David Howellsc4028952006-11-22 14:57:56 +0000278 struct hda_bus_unsolicited *unsol =
279 container_of(work, struct hda_bus_unsolicited, work);
280 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct hda_codec *codec;
282 unsigned int rp, caddr, res;
283
284 while (unsol->rp != unsol->wp) {
285 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
286 unsol->rp = rp;
287 rp <<= 1;
288 res = unsol->queue[rp];
289 caddr = unsol->queue[rp + 1];
290 if (! (caddr & (1 << 4))) /* no unsolicited event? */
291 continue;
292 codec = bus->caddr_tbl[caddr & 0x0f];
293 if (codec && codec->patch_ops.unsol_event)
294 codec->patch_ops.unsol_event(codec, res);
295 }
296}
297
298/*
299 * initialize unsolicited queue
300 */
301static int init_unsol_queue(struct hda_bus *bus)
302{
303 struct hda_bus_unsolicited *unsol;
304
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100305 if (bus->unsol) /* already initialized */
306 return 0;
307
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200308 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (! unsol) {
310 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
311 return -ENOMEM;
312 }
OGAWA Hirofumice7415f2006-03-31 12:36:14 +0200313 unsol->workq = create_singlethread_workqueue("hda_codec");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (! unsol->workq) {
315 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
316 kfree(unsol);
317 return -ENOMEM;
318 }
David Howellsc4028952006-11-22 14:57:56 +0000319 INIT_WORK(&unsol->work, process_unsol_events);
320 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 bus->unsol = unsol;
322 return 0;
323}
324
325/*
326 * destructor
327 */
328static void snd_hda_codec_free(struct hda_codec *codec);
329
330static int snd_hda_bus_free(struct hda_bus *bus)
331{
332 struct list_head *p, *n;
333
334 if (! bus)
335 return 0;
336 if (bus->unsol) {
337 destroy_workqueue(bus->unsol->workq);
338 kfree(bus->unsol);
339 }
340 list_for_each_safe(p, n, &bus->codec_list) {
341 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
342 snd_hda_codec_free(codec);
343 }
344 if (bus->ops.private_free)
345 bus->ops.private_free(bus);
346 kfree(bus);
347 return 0;
348}
349
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100350static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct hda_bus *bus = device->device_data;
353 return snd_hda_bus_free(bus);
354}
355
356/**
357 * snd_hda_bus_new - create a HDA bus
358 * @card: the card entry
359 * @temp: the template for hda_bus information
360 * @busp: the pointer to store the created bus instance
361 *
362 * Returns 0 if successful, or a negative error code.
363 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100364int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct hda_bus **busp)
366{
367 struct hda_bus *bus;
368 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100369 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .dev_free = snd_hda_bus_dev_free,
371 };
372
373 snd_assert(temp, return -EINVAL);
374 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
375
376 if (busp)
377 *busp = NULL;
378
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200379 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (bus == NULL) {
381 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
382 return -ENOMEM;
383 }
384
385 bus->card = card;
386 bus->private_data = temp->private_data;
387 bus->pci = temp->pci;
388 bus->modelname = temp->modelname;
389 bus->ops = temp->ops;
390
Ingo Molnar62932df2006-01-16 16:34:20 +0100391 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 INIT_LIST_HEAD(&bus->codec_list);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
395 snd_hda_bus_free(bus);
396 return err;
397 }
398 if (busp)
399 *busp = bus;
400 return 0;
401}
402
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200403EXPORT_SYMBOL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * find a matching codec preset
407 */
408static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
409{
410 const struct hda_codec_preset **tbl, *preset;
411
412 for (tbl = hda_preset_tables; *tbl; tbl++) {
413 for (preset = *tbl; preset->id; preset++) {
414 u32 mask = preset->mask;
415 if (! mask)
416 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200417 if (preset->id == (codec->vendor_id & mask) &&
418 (! preset->rev ||
419 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return preset;
421 }
422 }
423 return NULL;
424}
425
426/*
427 * snd_hda_get_codec_name - store the codec name
428 */
429void snd_hda_get_codec_name(struct hda_codec *codec,
430 char *name, int namelen)
431{
432 const struct hda_vendor_id *c;
433 const char *vendor = NULL;
434 u16 vendor_id = codec->vendor_id >> 16;
435 char tmp[16];
436
437 for (c = hda_vendor_ids; c->id; c++) {
438 if (c->id == vendor_id) {
439 vendor = c->name;
440 break;
441 }
442 }
443 if (! vendor) {
444 sprintf(tmp, "Generic %04x", vendor_id);
445 vendor = tmp;
446 }
447 if (codec->preset && codec->preset->name)
448 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
449 else
450 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
451}
452
453/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200454 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200456static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int i, total_nodes;
459 hda_nid_t nid;
460
461 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
462 for (i = 0; i < total_nodes; i++, nid++) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200463 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
464 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);
487 if (! codec->wcaps)
488 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
497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * codec destructor
499 */
500static void snd_hda_codec_free(struct hda_codec *codec)
501{
502 if (! codec)
503 return;
504 list_del(&codec->list);
505 codec->bus->caddr_tbl[codec->addr] = NULL;
506 if (codec->patch_ops.free)
507 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100508 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100509 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 kfree(codec);
511}
512
513static void init_amp_hash(struct hda_codec *codec);
514
515/**
516 * snd_hda_codec_new - create a HDA codec
517 * @bus: the bus to assign
518 * @codec_addr: the codec address
519 * @codecp: the pointer to store the generated codec
520 *
521 * Returns 0 if successful, or a negative error code.
522 */
523int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
524 struct hda_codec **codecp)
525{
526 struct hda_codec *codec;
527 char component[13];
528 int err;
529
530 snd_assert(bus, return -EINVAL);
531 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
532
533 if (bus->caddr_tbl[codec_addr]) {
534 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
535 return -EBUSY;
536 }
537
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200538 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (codec == NULL) {
540 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
541 return -ENOMEM;
542 }
543
544 codec->bus = bus;
545 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100546 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 init_amp_hash(codec);
548
549 list_add_tail(&codec->list, &bus->codec_list);
550 bus->caddr_tbl[codec_addr] = codec;
551
552 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100553 if (codec->vendor_id == -1)
554 /* read again, hopefully the access method was corrected
555 * in the last read...
556 */
557 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
558 AC_PAR_VENDOR_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
560 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
561
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200562 setup_fg_nodes(codec);
563 if (! codec->afg && ! codec->mfg) {
564 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 snd_hda_codec_free(codec);
566 return -ENODEV;
567 }
568
Takashi Iwai54d17402005-11-21 16:33:22 +0100569 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
570 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
571 snd_hda_codec_free(codec);
572 return -ENOMEM;
573 }
574
Takashi Iwai86284e42005-10-11 15:05:54 +0200575 if (! codec->subsystem_id) {
576 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
577 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
578 AC_VERB_GET_SUBSYSTEM_ID,
579 0);
580 }
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 codec->preset = find_codec_preset(codec);
583 if (! *bus->card->mixername)
584 snd_hda_get_codec_name(codec, bus->card->mixername,
585 sizeof(bus->card->mixername));
586
587 if (codec->preset && codec->preset->patch)
588 err = codec->preset->patch(codec);
589 else
590 err = snd_hda_parse_generic_codec(codec);
591 if (err < 0) {
592 snd_hda_codec_free(codec);
593 return err;
594 }
595
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100596 if (codec->patch_ops.unsol_event)
597 init_unsol_queue(bus);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 snd_hda_codec_proc_new(codec);
600
601 sprintf(component, "HDA:%08x", codec->vendor_id);
602 snd_component_add(codec->bus->card, component);
603
604 if (codecp)
605 *codecp = codec;
606 return 0;
607}
608
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200609EXPORT_SYMBOL(snd_hda_codec_new);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
612 * snd_hda_codec_setup_stream - set up the codec for streaming
613 * @codec: the CODEC to set up
614 * @nid: the NID to set up
615 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
616 * @channel_id: channel id to pass, zero based.
617 * @format: stream format.
618 */
619void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
620 int channel_id, int format)
621{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200622 if (! nid)
623 return;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
626 nid, stream_tag, channel_id, format);
627 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
628 (stream_tag << 4) | channel_id);
629 msleep(1);
630 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
631}
632
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200633EXPORT_SYMBOL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/*
636 * amp access functions
637 */
638
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200639/* FIXME: more better hash key? */
640#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200642#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/* initialize the hash table */
645static void init_amp_hash(struct hda_codec *codec)
646{
647 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
648 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100649 codec->amp_info_size = 0;
650 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/* query the hash. allocate an entry if not found. */
654static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
655{
656 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
657 u16 cur = codec->amp_hash[idx];
658 struct hda_amp_info *info;
659
660 while (cur != 0xffff) {
661 info = &codec->amp_info[cur];
662 if (info->key == key)
663 return info;
664 cur = info->next;
665 }
666
667 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100668 if (codec->num_amp_entries >= codec->amp_info_size) {
669 /* reallocate the array */
670 int new_size = codec->amp_info_size + 64;
671 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
672 GFP_KERNEL);
673 if (! new_info) {
674 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
675 return NULL;
676 }
677 if (codec->amp_info) {
678 memcpy(new_info, codec->amp_info,
679 codec->amp_info_size * sizeof(struct hda_amp_info));
680 kfree(codec->amp_info);
681 }
682 codec->amp_info_size = new_size;
683 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 cur = codec->num_amp_entries++;
686 info = &codec->amp_info[cur];
687 info->key = key;
688 info->status = 0; /* not initialized yet */
689 info->next = codec->amp_hash[idx];
690 codec->amp_hash[idx] = cur;
691
692 return info;
693}
694
695/*
696 * query AMP capabilities for the given widget and direction
697 */
698static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
699{
700 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
701
702 if (! info)
703 return 0;
704 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100705 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 nid = codec->afg;
707 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
708 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
709 info->status |= INFO_AMP_CAPS;
710 }
711 return info->amp_caps;
712}
713
714/*
715 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200716 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200718static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 hda_nid_t nid, int ch, int direction, int index)
720{
721 u32 val, parm;
722
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200723 if (info->status & INFO_AMP_VOL(ch))
724 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
727 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
728 parm |= index;
729 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
730 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200731 info->status |= INFO_AMP_VOL(ch);
732 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200736 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200738static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 hda_nid_t nid, int ch, int direction, int index, int val)
740{
741 u32 parm;
742
743 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
744 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
745 parm |= index << AC_AMP_SET_INDEX_SHIFT;
746 parm |= val;
747 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200748 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200752 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Takashi Iwai834be882006-03-01 14:16:17 +0100754int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
755 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
758 if (! info)
759 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200760 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200763/*
764 * update the AMP value, mask = bit mask to set, val = the value
765 */
Takashi Iwai834be882006-03-01 14:16:17 +0100766int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
767 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (! info)
772 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200773 val &= mask;
774 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (info->vol[ch] == val && ! codec->in_resume)
776 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200777 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 1;
779}
780
781
782/*
783 * AMP control callbacks
784 */
785/* retrieve parameters from private_value */
786#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
787#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
788#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
789#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
790
791/* volume */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100792int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
795 u16 nid = get_amp_nid(kcontrol);
796 u8 chs = get_amp_channels(kcontrol);
797 int dir = get_amp_direction(kcontrol);
798 u32 caps;
799
800 caps = query_amp_caps(codec, nid, dir);
801 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
802 if (! caps) {
803 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
804 return -EINVAL;
805 }
806 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
807 uinfo->count = chs == 3 ? 2 : 1;
808 uinfo->value.integer.min = 0;
809 uinfo->value.integer.max = caps;
810 return 0;
811}
812
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100813int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
816 hda_nid_t nid = get_amp_nid(kcontrol);
817 int chs = get_amp_channels(kcontrol);
818 int dir = get_amp_direction(kcontrol);
819 int idx = get_amp_index(kcontrol);
820 long *valp = ucontrol->value.integer.value;
821
822 if (chs & 1)
823 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
824 if (chs & 2)
825 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
826 return 0;
827}
828
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100829int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
832 hda_nid_t nid = get_amp_nid(kcontrol);
833 int chs = get_amp_channels(kcontrol);
834 int dir = get_amp_direction(kcontrol);
835 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 long *valp = ucontrol->value.integer.value;
837 int change = 0;
838
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200839 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200840 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
841 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200842 valp++;
843 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200844 if (chs & 2)
845 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200846 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return change;
848}
849
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200850int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
851 unsigned int size, unsigned int __user *_tlv)
852{
853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
854 hda_nid_t nid = get_amp_nid(kcontrol);
855 int dir = get_amp_direction(kcontrol);
856 u32 caps, val1, val2;
857
858 if (size < 4 * sizeof(unsigned int))
859 return -ENOMEM;
860 caps = query_amp_caps(codec, nid, dir);
861 val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25;
862 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
863 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200864 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
865 return -EFAULT;
866 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
867 return -EFAULT;
868 if (put_user(val1, _tlv + 2))
869 return -EFAULT;
870 if (put_user(val2, _tlv + 3))
871 return -EFAULT;
872 return 0;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/* switch */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100876int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 int chs = get_amp_channels(kcontrol);
879
880 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
881 uinfo->count = chs == 3 ? 2 : 1;
882 uinfo->value.integer.min = 0;
883 uinfo->value.integer.max = 1;
884 return 0;
885}
886
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100887int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
890 hda_nid_t nid = get_amp_nid(kcontrol);
891 int chs = get_amp_channels(kcontrol);
892 int dir = get_amp_direction(kcontrol);
893 int idx = get_amp_index(kcontrol);
894 long *valp = ucontrol->value.integer.value;
895
896 if (chs & 1)
897 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
898 if (chs & 2)
899 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
900 return 0;
901}
902
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100903int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
906 hda_nid_t nid = get_amp_nid(kcontrol);
907 int chs = get_amp_channels(kcontrol);
908 int dir = get_amp_direction(kcontrol);
909 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 long *valp = ucontrol->value.integer.value;
911 int change = 0;
912
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200913 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200914 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
915 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200916 valp++;
917 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200918 if (chs & 2)
919 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200920 0x80, *valp ? 0 : 0x80);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return change;
923}
924
925/*
Takashi Iwai985be542005-11-02 18:26:49 +0100926 * bound volume controls
927 *
928 * bind multiple volumes (# indices, from 0)
929 */
930
931#define AMP_VAL_IDX_SHIFT 19
932#define AMP_VAL_IDX_MASK (0x0f<<19)
933
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100934int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100935{
936 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
937 unsigned long pval;
938 int err;
939
Ingo Molnar62932df2006-01-16 16:34:20 +0100940 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100941 pval = kcontrol->private_value;
942 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
943 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
944 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100945 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100946 return err;
947}
948
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100949int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100950{
951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952 unsigned long pval;
953 int i, indices, err = 0, change = 0;
954
Ingo Molnar62932df2006-01-16 16:34:20 +0100955 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100956 pval = kcontrol->private_value;
957 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
958 for (i = 0; i < indices; i++) {
959 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
960 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
961 if (err < 0)
962 break;
963 change |= err;
964 }
965 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100966 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100967 return err < 0 ? err : change;
968}
969
970/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * SPDIF out controls
972 */
973
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100974static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
977 uinfo->count = 1;
978 return 0;
979}
980
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100981static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
984 IEC958_AES0_NONAUDIO |
985 IEC958_AES0_CON_EMPHASIS_5015 |
986 IEC958_AES0_CON_NOT_COPYRIGHT;
987 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
988 IEC958_AES1_CON_ORIGINAL;
989 return 0;
990}
991
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100992static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
995 IEC958_AES0_NONAUDIO |
996 IEC958_AES0_PRO_EMPHASIS_5015;
997 return 0;
998}
999
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001000static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1003
1004 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1005 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1006 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1007 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1008
1009 return 0;
1010}
1011
1012/* convert from SPDIF status bits to HDA SPDIF bits
1013 * bit 0 (DigEn) is always set zero (to be filled later)
1014 */
1015static unsigned short convert_from_spdif_status(unsigned int sbits)
1016{
1017 unsigned short val = 0;
1018
1019 if (sbits & IEC958_AES0_PROFESSIONAL)
1020 val |= 1 << 6;
1021 if (sbits & IEC958_AES0_NONAUDIO)
1022 val |= 1 << 5;
1023 if (sbits & IEC958_AES0_PROFESSIONAL) {
1024 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1025 val |= 1 << 3;
1026 } else {
1027 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
1028 val |= 1 << 3;
1029 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1030 val |= 1 << 4;
1031 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1032 val |= 1 << 7;
1033 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1034 }
1035 return val;
1036}
1037
1038/* convert to SPDIF status bits from HDA SPDIF bits
1039 */
1040static unsigned int convert_to_spdif_status(unsigned short val)
1041{
1042 unsigned int sbits = 0;
1043
1044 if (val & (1 << 5))
1045 sbits |= IEC958_AES0_NONAUDIO;
1046 if (val & (1 << 6))
1047 sbits |= IEC958_AES0_PROFESSIONAL;
1048 if (sbits & IEC958_AES0_PROFESSIONAL) {
1049 if (sbits & (1 << 3))
1050 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1051 } else {
1052 if (val & (1 << 3))
1053 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1054 if (! (val & (1 << 4)))
1055 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1056 if (val & (1 << 7))
1057 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1058 sbits |= val & (0x7f << 8);
1059 }
1060 return sbits;
1061}
1062
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001063static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1066 hda_nid_t nid = kcontrol->private_value;
1067 unsigned short val;
1068 int change;
1069
Ingo Molnar62932df2006-01-16 16:34:20 +01001070 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 codec->spdif_status = ucontrol->value.iec958.status[0] |
1072 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1073 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1074 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1075 val = convert_from_spdif_status(codec->spdif_status);
1076 val |= codec->spdif_ctls & 1;
1077 change = codec->spdif_ctls != val;
1078 codec->spdif_ctls = val;
1079
1080 if (change || codec->in_resume) {
1081 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1082 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1083 }
1084
Ingo Molnar62932df2006-01-16 16:34:20 +01001085 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return change;
1087}
1088
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001089static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1092 uinfo->count = 1;
1093 uinfo->value.integer.min = 0;
1094 uinfo->value.integer.max = 1;
1095 return 0;
1096}
1097
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001098static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1101
1102 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1103 return 0;
1104}
1105
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001106static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1109 hda_nid_t nid = kcontrol->private_value;
1110 unsigned short val;
1111 int change;
1112
Ingo Molnar62932df2006-01-16 16:34:20 +01001113 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 val = codec->spdif_ctls & ~1;
1115 if (ucontrol->value.integer.value[0])
1116 val |= 1;
1117 change = codec->spdif_ctls != val;
1118 if (change || codec->in_resume) {
1119 codec->spdif_ctls = val;
1120 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1121 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1122 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1123 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1124 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001125 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return change;
1127}
1128
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001129static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 {
1131 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1132 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1133 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1134 .info = snd_hda_spdif_mask_info,
1135 .get = snd_hda_spdif_cmask_get,
1136 },
1137 {
1138 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1139 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1140 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1141 .info = snd_hda_spdif_mask_info,
1142 .get = snd_hda_spdif_pmask_get,
1143 },
1144 {
1145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1146 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1147 .info = snd_hda_spdif_mask_info,
1148 .get = snd_hda_spdif_default_get,
1149 .put = snd_hda_spdif_default_put,
1150 },
1151 {
1152 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1153 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1154 .info = snd_hda_spdif_out_switch_info,
1155 .get = snd_hda_spdif_out_switch_get,
1156 .put = snd_hda_spdif_out_switch_put,
1157 },
1158 { } /* end */
1159};
1160
1161/**
1162 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1163 * @codec: the HDA codec
1164 * @nid: audio out widget NID
1165 *
1166 * Creates controls related with the SPDIF output.
1167 * Called from each patch supporting the SPDIF out.
1168 *
1169 * Returns 0 if successful, or a negative error code.
1170 */
1171int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1172{
1173 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001174 struct snd_kcontrol *kctl;
1175 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1178 kctl = snd_ctl_new1(dig_mix, codec);
1179 kctl->private_value = nid;
1180 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1181 return err;
1182 }
1183 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1184 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1185 return 0;
1186}
1187
1188/*
1189 * SPDIF input
1190 */
1191
1192#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1193
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001194static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1197
1198 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1199 return 0;
1200}
1201
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001202static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1205 hda_nid_t nid = kcontrol->private_value;
1206 unsigned int val = !!ucontrol->value.integer.value[0];
1207 int change;
1208
Ingo Molnar62932df2006-01-16 16:34:20 +01001209 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 change = codec->spdif_in_enable != val;
1211 if (change || codec->in_resume) {
1212 codec->spdif_in_enable = val;
1213 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1214 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001215 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return change;
1217}
1218
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001219static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1222 hda_nid_t nid = kcontrol->private_value;
1223 unsigned short val;
1224 unsigned int sbits;
1225
1226 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1227 sbits = convert_to_spdif_status(val);
1228 ucontrol->value.iec958.status[0] = sbits;
1229 ucontrol->value.iec958.status[1] = sbits >> 8;
1230 ucontrol->value.iec958.status[2] = sbits >> 16;
1231 ucontrol->value.iec958.status[3] = sbits >> 24;
1232 return 0;
1233}
1234
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001235static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 {
1237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1238 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1239 .info = snd_hda_spdif_in_switch_info,
1240 .get = snd_hda_spdif_in_switch_get,
1241 .put = snd_hda_spdif_in_switch_put,
1242 },
1243 {
1244 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1245 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1246 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1247 .info = snd_hda_spdif_mask_info,
1248 .get = snd_hda_spdif_in_status_get,
1249 },
1250 { } /* end */
1251};
1252
1253/**
1254 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1255 * @codec: the HDA codec
1256 * @nid: audio in widget NID
1257 *
1258 * Creates controls related with the SPDIF input.
1259 * Called from each patch supporting the SPDIF in.
1260 *
1261 * Returns 0 if successful, or a negative error code.
1262 */
1263int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1264{
1265 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001266 struct snd_kcontrol *kctl;
1267 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1270 kctl = snd_ctl_new1(dig_mix, codec);
1271 kctl->private_value = nid;
1272 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1273 return err;
1274 }
1275 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1276 return 0;
1277}
1278
1279
Takashi Iwai54d17402005-11-21 16:33:22 +01001280/*
1281 * set power state of the codec
1282 */
1283static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1284 unsigned int power_state)
1285{
1286 hda_nid_t nid, nid_start;
1287 int nodes;
1288
1289 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1290 power_state);
1291
1292 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1293 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1294 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1295 snd_hda_codec_write(codec, nid, 0,
1296 AC_VERB_SET_POWER_STATE,
1297 power_state);
1298 }
1299
1300 if (power_state == AC_PWRST_D0)
1301 msleep(10);
1302}
1303
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/**
1306 * snd_hda_build_controls - build mixer controls
1307 * @bus: the BUS
1308 *
1309 * Creates mixer controls for each codec included in the bus.
1310 *
1311 * Returns 0 if successful, otherwise a negative error code.
1312 */
1313int snd_hda_build_controls(struct hda_bus *bus)
1314{
1315 struct list_head *p;
1316
1317 /* build controls */
1318 list_for_each(p, &bus->codec_list) {
1319 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1320 int err;
1321 if (! codec->patch_ops.build_controls)
1322 continue;
1323 err = codec->patch_ops.build_controls(codec);
1324 if (err < 0)
1325 return err;
1326 }
1327
1328 /* initialize */
1329 list_for_each(p, &bus->codec_list) {
1330 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1331 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001332 hda_set_power_state(codec,
1333 codec->afg ? codec->afg : codec->mfg,
1334 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (! codec->patch_ops.init)
1336 continue;
1337 err = codec->patch_ops.init(codec);
1338 if (err < 0)
1339 return err;
1340 }
1341 return 0;
1342}
1343
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001344EXPORT_SYMBOL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346/*
1347 * stream formats
1348 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001349struct hda_rate_tbl {
1350 unsigned int hz;
1351 unsigned int alsa_bits;
1352 unsigned int hda_fmt;
1353};
1354
1355static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001357
1358 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1360 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1361 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1362 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1363 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1364 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1365 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1366 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1367 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1368 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1369 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001370
Takashi Iwaibefdf312005-08-22 13:57:55 +02001371 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372};
1373
1374/**
1375 * snd_hda_calc_stream_format - calculate format bitset
1376 * @rate: the sample rate
1377 * @channels: the number of channels
1378 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1379 * @maxbps: the max. bps
1380 *
1381 * Calculate the format bitset from the given rate, channels and th PCM format.
1382 *
1383 * Return zero if invalid.
1384 */
1385unsigned int snd_hda_calc_stream_format(unsigned int rate,
1386 unsigned int channels,
1387 unsigned int format,
1388 unsigned int maxbps)
1389{
1390 int i;
1391 unsigned int val = 0;
1392
Takashi Iwaibefdf312005-08-22 13:57:55 +02001393 for (i = 0; rate_bits[i].hz; i++)
1394 if (rate_bits[i].hz == rate) {
1395 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001398 if (! rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 snd_printdd("invalid rate %d\n", rate);
1400 return 0;
1401 }
1402
1403 if (channels == 0 || channels > 8) {
1404 snd_printdd("invalid channels %d\n", channels);
1405 return 0;
1406 }
1407 val |= channels - 1;
1408
1409 switch (snd_pcm_format_width(format)) {
1410 case 8: val |= 0x00; break;
1411 case 16: val |= 0x10; break;
1412 case 20:
1413 case 24:
1414 case 32:
1415 if (maxbps >= 32)
1416 val |= 0x40;
1417 else if (maxbps >= 24)
1418 val |= 0x30;
1419 else
1420 val |= 0x20;
1421 break;
1422 default:
1423 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1424 return 0;
1425 }
1426
1427 return val;
1428}
1429
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001430EXPORT_SYMBOL(snd_hda_calc_stream_format);
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/**
1433 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1434 * @codec: the HDA codec
1435 * @nid: NID to query
1436 * @ratesp: the pointer to store the detected rate bitflags
1437 * @formatsp: the pointer to store the detected formats
1438 * @bpsp: the pointer to store the detected format widths
1439 *
1440 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1441 * or @bsps argument is ignored.
1442 *
1443 * Returns 0 if successful, otherwise a negative error code.
1444 */
1445int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1446 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1447{
1448 int i;
1449 unsigned int val, streams;
1450
1451 val = 0;
1452 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001453 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1455 if (val == -1)
1456 return -EIO;
1457 }
1458 if (! val)
1459 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1460
1461 if (ratesp) {
1462 u32 rates = 0;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001463 for (i = 0; rate_bits[i].hz; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001465 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 *ratesp = rates;
1468 }
1469
1470 if (formatsp || bpsp) {
1471 u64 formats = 0;
1472 unsigned int bps;
1473 unsigned int wcaps;
1474
Takashi Iwai54d17402005-11-21 16:33:22 +01001475 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1477 if (streams == -1)
1478 return -EIO;
1479 if (! streams) {
1480 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1481 if (streams == -1)
1482 return -EIO;
1483 }
1484
1485 bps = 0;
1486 if (streams & AC_SUPFMT_PCM) {
1487 if (val & AC_SUPPCM_BITS_8) {
1488 formats |= SNDRV_PCM_FMTBIT_U8;
1489 bps = 8;
1490 }
1491 if (val & AC_SUPPCM_BITS_16) {
1492 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1493 bps = 16;
1494 }
1495 if (wcaps & AC_WCAP_DIGITAL) {
1496 if (val & AC_SUPPCM_BITS_32)
1497 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1498 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1499 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1500 if (val & AC_SUPPCM_BITS_24)
1501 bps = 24;
1502 else if (val & AC_SUPPCM_BITS_20)
1503 bps = 20;
1504 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1505 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1506 if (val & AC_SUPPCM_BITS_32)
1507 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 else if (val & AC_SUPPCM_BITS_24)
1509 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02001510 else if (val & AC_SUPPCM_BITS_20)
1511 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 }
1514 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1515 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1516 bps = 32;
1517 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1518 /* temporary hack: we have still no proper support
1519 * for the direct AC3 stream...
1520 */
1521 formats |= SNDRV_PCM_FMTBIT_U8;
1522 bps = 8;
1523 }
1524 if (formatsp)
1525 *formatsp = formats;
1526 if (bpsp)
1527 *bpsp = bps;
1528 }
1529
1530 return 0;
1531}
1532
1533/**
1534 * snd_hda_is_supported_format - check whether the given node supports the format val
1535 *
1536 * Returns 1 if supported, 0 if not.
1537 */
1538int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1539 unsigned int format)
1540{
1541 int i;
1542 unsigned int val = 0, rate, stream;
1543
1544 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001545 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1547 if (val == -1)
1548 return 0;
1549 }
1550 if (! val) {
1551 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1552 if (val == -1)
1553 return 0;
1554 }
1555
1556 rate = format & 0xff00;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001557 for (i = 0; rate_bits[i].hz; i++)
1558 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (val & (1 << i))
1560 break;
1561 return 0;
1562 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001563 if (! rate_bits[i].hz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565
1566 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1567 if (stream == -1)
1568 return 0;
1569 if (! stream && nid != codec->afg)
1570 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1571 if (! stream || stream == -1)
1572 return 0;
1573
1574 if (stream & AC_SUPFMT_PCM) {
1575 switch (format & 0xf0) {
1576 case 0x00:
1577 if (! (val & AC_SUPPCM_BITS_8))
1578 return 0;
1579 break;
1580 case 0x10:
1581 if (! (val & AC_SUPPCM_BITS_16))
1582 return 0;
1583 break;
1584 case 0x20:
1585 if (! (val & AC_SUPPCM_BITS_20))
1586 return 0;
1587 break;
1588 case 0x30:
1589 if (! (val & AC_SUPPCM_BITS_24))
1590 return 0;
1591 break;
1592 case 0x40:
1593 if (! (val & AC_SUPPCM_BITS_32))
1594 return 0;
1595 break;
1596 default:
1597 return 0;
1598 }
1599 } else {
1600 /* FIXME: check for float32 and AC3? */
1601 }
1602
1603 return 1;
1604}
1605
1606/*
1607 * PCM stuff
1608 */
1609static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1610 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001611 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 return 0;
1614}
1615
1616static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1617 struct hda_codec *codec,
1618 unsigned int stream_tag,
1619 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001620 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1623 return 0;
1624}
1625
1626static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1627 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001628 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1631 return 0;
1632}
1633
1634static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1635{
1636 if (info->nid) {
1637 /* query support PCM information from the given NID */
1638 if (! info->rates || ! info->formats)
1639 snd_hda_query_supported_pcm(codec, info->nid,
1640 info->rates ? NULL : &info->rates,
1641 info->formats ? NULL : &info->formats,
1642 info->maxbps ? NULL : &info->maxbps);
1643 }
1644 if (info->ops.open == NULL)
1645 info->ops.open = hda_pcm_default_open_close;
1646 if (info->ops.close == NULL)
1647 info->ops.close = hda_pcm_default_open_close;
1648 if (info->ops.prepare == NULL) {
1649 snd_assert(info->nid, return -EINVAL);
1650 info->ops.prepare = hda_pcm_default_prepare;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (info->ops.cleanup == NULL) {
1653 snd_assert(info->nid, return -EINVAL);
1654 info->ops.cleanup = hda_pcm_default_cleanup;
1655 }
1656 return 0;
1657}
1658
1659/**
1660 * snd_hda_build_pcms - build PCM information
1661 * @bus: the BUS
1662 *
1663 * Create PCM information for each codec included in the bus.
1664 *
1665 * The build_pcms codec patch is requested to set up codec->num_pcms and
1666 * codec->pcm_info properly. The array is referred by the top-level driver
1667 * to create its PCM instances.
1668 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1669 * callback.
1670 *
1671 * At least, substreams, channels_min and channels_max must be filled for
1672 * each stream. substreams = 0 indicates that the stream doesn't exist.
1673 * When rates and/or formats are zero, the supported values are queried
1674 * from the given nid. The nid is used also by the default ops.prepare
1675 * and ops.cleanup callbacks.
1676 *
1677 * The driver needs to call ops.open in its open callback. Similarly,
1678 * ops.close is supposed to be called in the close callback.
1679 * ops.prepare should be called in the prepare or hw_params callback
1680 * with the proper parameters for set up.
1681 * ops.cleanup should be called in hw_free for clean up of streams.
1682 *
1683 * This function returns 0 if successfull, or a negative error code.
1684 */
1685int snd_hda_build_pcms(struct hda_bus *bus)
1686{
1687 struct list_head *p;
1688
1689 list_for_each(p, &bus->codec_list) {
1690 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1691 unsigned int pcm, s;
1692 int err;
1693 if (! codec->patch_ops.build_pcms)
1694 continue;
1695 err = codec->patch_ops.build_pcms(codec);
1696 if (err < 0)
1697 return err;
1698 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1699 for (s = 0; s < 2; s++) {
1700 struct hda_pcm_stream *info;
1701 info = &codec->pcm_info[pcm].stream[s];
1702 if (! info->substreams)
1703 continue;
1704 err = set_pcm_default_values(codec, info);
1705 if (err < 0)
1706 return err;
1707 }
1708 }
1709 }
1710 return 0;
1711}
1712
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001713EXPORT_SYMBOL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715/**
1716 * snd_hda_check_board_config - compare the current codec with the config table
1717 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001718 * @num_configs: number of config enums
1719 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * @tbl: configuration table, terminated by null entries
1721 *
1722 * Compares the modelname or PCI subsystem id of the current codec with the
1723 * given configuration table. If a matching entry is found, returns its
1724 * config value (supposed to be 0 or positive).
1725 *
1726 * If no entries are matching, the function returns a negative value.
1727 */
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001728int snd_hda_check_board_config(struct hda_codec *codec,
1729 int num_configs, const char **models,
1730 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001732 if (codec->bus->modelname && models) {
1733 int i;
1734 for (i = 0; i < num_configs; i++) {
1735 if (models[i] &&
1736 !strcmp(codec->bus->modelname, models[i])) {
1737 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1738 "selected\n", models[i]);
1739 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 }
1742 }
1743
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001744 if (!codec->bus->pci || !tbl)
1745 return -1;
1746
1747 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1748 if (!tbl)
1749 return -1;
1750 if (tbl->value >= 0 && tbl->value < num_configs) {
1751#ifdef CONFIG_SND_DEBUG_DETECT
1752 char tmp[10];
1753 const char *model = NULL;
1754 if (models)
1755 model = models[tbl->value];
1756 if (!model) {
1757 sprintf(tmp, "#%d", tbl->value);
1758 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001760 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1761 "for config %x:%x (%s)\n",
1762 model, tbl->subvendor, tbl->subdevice,
1763 (tbl->name ? tbl->name : "Unknown device"));
1764#endif
1765 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 return -1;
1768}
1769
1770/**
1771 * snd_hda_add_new_ctls - create controls from the array
1772 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001773 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
1775 * This helper function creates and add new controls in the given array.
1776 * The array must be terminated with an empty entry as terminator.
1777 *
1778 * Returns 0 if successful, or a negative error code.
1779 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001780int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 int err;
1783
1784 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001785 struct snd_kcontrol *kctl;
1786 kctl = snd_ctl_new1(knew, codec);
1787 if (! kctl)
1788 return -ENOMEM;
1789 err = snd_ctl_add(codec->bus->card, kctl);
1790 if (err < 0) {
1791 if (! codec->addr)
1792 return err;
1793 kctl = snd_ctl_new1(knew, codec);
1794 if (! kctl)
1795 return -ENOMEM;
1796 kctl->id.device = codec->addr;
1797 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1798 return err;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 return 0;
1802}
1803
1804
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001805/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001806 * Channel mode helper
1807 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001808int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001809 const struct hda_channel_mode *chmode, int num_chmodes)
1810{
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1812 uinfo->count = 1;
1813 uinfo->value.enumerated.items = num_chmodes;
1814 if (uinfo->value.enumerated.item >= num_chmodes)
1815 uinfo->value.enumerated.item = num_chmodes - 1;
1816 sprintf(uinfo->value.enumerated.name, "%dch",
1817 chmode[uinfo->value.enumerated.item].channels);
1818 return 0;
1819}
1820
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001821int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001822 const struct hda_channel_mode *chmode, int num_chmodes,
1823 int max_channels)
1824{
1825 int i;
1826
1827 for (i = 0; i < num_chmodes; i++) {
1828 if (max_channels == chmode[i].channels) {
1829 ucontrol->value.enumerated.item[0] = i;
1830 break;
1831 }
1832 }
1833 return 0;
1834}
1835
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001836int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001837 const struct hda_channel_mode *chmode, int num_chmodes,
1838 int *max_channelsp)
1839{
1840 unsigned int mode;
1841
1842 mode = ucontrol->value.enumerated.item[0];
1843 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001844 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001845 return 0;
1846 /* change the current channel setting */
1847 *max_channelsp = chmode[mode].channels;
1848 if (chmode[mode].sequence)
1849 snd_hda_sequence_write(codec, chmode[mode].sequence);
1850 return 1;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/*
1854 * input MUX helper
1855 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001856int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 unsigned int index;
1859
1860 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1861 uinfo->count = 1;
1862 uinfo->value.enumerated.items = imux->num_items;
1863 index = uinfo->value.enumerated.item;
1864 if (index >= imux->num_items)
1865 index = imux->num_items - 1;
1866 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1867 return 0;
1868}
1869
1870int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001871 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 unsigned int *cur_val)
1873{
1874 unsigned int idx;
1875
1876 idx = ucontrol->value.enumerated.item[0];
1877 if (idx >= imux->num_items)
1878 idx = imux->num_items - 1;
1879 if (*cur_val == idx && ! codec->in_resume)
1880 return 0;
1881 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1882 imux->items[idx].index);
1883 *cur_val = idx;
1884 return 1;
1885}
1886
1887
1888/*
1889 * Multi-channel / digital-out PCM helper functions
1890 */
1891
1892/*
1893 * open the digital out in the exclusive mode
1894 */
1895int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1896{
Ingo Molnar62932df2006-01-16 16:34:20 +01001897 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (mout->dig_out_used) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001899 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EBUSY; /* already being used */
1901 }
1902 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001903 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return 0;
1905}
1906
1907/*
1908 * release the digital out
1909 */
1910int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1911{
Ingo Molnar62932df2006-01-16 16:34:20 +01001912 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001914 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
1916}
1917
1918/*
1919 * set up more restrictions for analog out
1920 */
1921int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001922 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 substream->runtime->hw.channels_max = mout->max_channels;
1925 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1926 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1927}
1928
1929/*
1930 * set up the i/o for analog out
1931 * when the digital out is available, copy the front out to digital out, too.
1932 */
1933int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1934 unsigned int stream_tag,
1935 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001936 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 hda_nid_t *nids = mout->dac_nids;
1939 int chs = substream->runtime->channels;
1940 int i;
1941
Ingo Molnar62932df2006-01-16 16:34:20 +01001942 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1944 if (chs == 2 &&
1945 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1946 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1947 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1948 /* setup digital receiver */
1949 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1950 stream_tag, 0, format);
1951 } else {
1952 mout->dig_out_used = 0;
1953 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1954 }
1955 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001956 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* front */
1959 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02001960 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* headphone out will just decode front left/right (stereo) */
1962 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001963 /* extra outputs copied from front */
1964 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1965 if (mout->extra_out_nid[i])
1966 snd_hda_codec_setup_stream(codec,
1967 mout->extra_out_nid[i],
1968 stream_tag, 0, format);
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* surrounds */
1971 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001972 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1974 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001975 else /* copy front */
1976 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1977 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979 return 0;
1980}
1981
1982/*
1983 * clean up the setting for analog out
1984 */
1985int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1986{
1987 hda_nid_t *nids = mout->dac_nids;
1988 int i;
1989
1990 for (i = 0; i < mout->num_dacs; i++)
1991 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1992 if (mout->hp_nid)
1993 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001994 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1995 if (mout->extra_out_nid[i])
1996 snd_hda_codec_setup_stream(codec,
1997 mout->extra_out_nid[i],
1998 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01001999 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2001 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2002 mout->dig_out_used = 0;
2003 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002004 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006}
2007
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002008/*
2009 * Helper for automatic ping configuration
2010 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002011
2012static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2013{
2014 for (; *list; list++)
2015 if (*list == nid)
2016 return 1;
2017 return 0;
2018}
2019
Takashi Iwai82bc9552006-03-21 11:24:42 +01002020/*
2021 * Parse all pin widgets and store the useful pin nids to cfg
2022 *
2023 * The number of line-outs or any primary output is stored in line_outs,
2024 * and the corresponding output pins are assigned to line_out_pins[],
2025 * in the order of front, rear, CLFE, side, ...
2026 *
2027 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002028 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002029 * is detected, one of speaker of HP pins is assigned as the primary
2030 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2031 * if any analog output exists.
2032 *
2033 * The analog input pins are assigned to input_pins array.
2034 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2035 * respectively.
2036 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002037int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
2038 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002039{
2040 hda_nid_t nid, nid_start;
2041 int i, j, nodes;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002042 short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002043
2044 memset(cfg, 0, sizeof(*cfg));
2045
2046 memset(sequences, 0, sizeof(sequences));
2047 assoc_line_out = 0;
2048
2049 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2050 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002051 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002052 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2053 unsigned int def_conf;
2054 short assoc, loc;
2055
2056 /* read all default configuration for pin complex */
2057 if (wid_type != AC_WID_PIN)
2058 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002059 /* ignore the given nids (e.g. pc-beep returns error) */
2060 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2061 continue;
2062
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002063 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
2064 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2065 continue;
2066 loc = get_defcfg_location(def_conf);
2067 switch (get_defcfg_device(def_conf)) {
2068 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002069 seq = get_defcfg_sequence(def_conf);
2070 assoc = get_defcfg_association(def_conf);
2071 if (! assoc)
2072 continue;
2073 if (! assoc_line_out)
2074 assoc_line_out = assoc;
2075 else if (assoc_line_out != assoc)
2076 continue;
2077 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2078 continue;
2079 cfg->line_out_pins[cfg->line_outs] = nid;
2080 sequences[cfg->line_outs] = seq;
2081 cfg->line_outs++;
2082 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002083 case AC_JACK_SPEAKER:
Takashi Iwai82bc9552006-03-21 11:24:42 +01002084 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2085 continue;
2086 cfg->speaker_pins[cfg->speaker_outs] = nid;
2087 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002088 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002089 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002090 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2091 continue;
2092 cfg->hp_pins[cfg->hp_outs] = nid;
2093 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002094 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002095 case AC_JACK_MIC_IN: {
2096 int preferred, alt;
2097 if (loc == AC_JACK_LOC_FRONT) {
2098 preferred = AUTO_PIN_FRONT_MIC;
2099 alt = AUTO_PIN_MIC;
2100 } else {
2101 preferred = AUTO_PIN_MIC;
2102 alt = AUTO_PIN_FRONT_MIC;
2103 }
2104 if (!cfg->input_pins[preferred])
2105 cfg->input_pins[preferred] = nid;
2106 else if (!cfg->input_pins[alt])
2107 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002108 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002109 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002110 case AC_JACK_LINE_IN:
2111 if (loc == AC_JACK_LOC_FRONT)
2112 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2113 else
2114 cfg->input_pins[AUTO_PIN_LINE] = nid;
2115 break;
2116 case AC_JACK_CD:
2117 cfg->input_pins[AUTO_PIN_CD] = nid;
2118 break;
2119 case AC_JACK_AUX:
2120 cfg->input_pins[AUTO_PIN_AUX] = nid;
2121 break;
2122 case AC_JACK_SPDIF_OUT:
2123 cfg->dig_out_pin = nid;
2124 break;
2125 case AC_JACK_SPDIF_IN:
2126 cfg->dig_in_pin = nid;
2127 break;
2128 }
2129 }
2130
2131 /* sort by sequence */
2132 for (i = 0; i < cfg->line_outs; i++)
2133 for (j = i + 1; j < cfg->line_outs; j++)
2134 if (sequences[i] > sequences[j]) {
2135 seq = sequences[i];
2136 sequences[i] = sequences[j];
2137 sequences[j] = seq;
2138 nid = cfg->line_out_pins[i];
2139 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2140 cfg->line_out_pins[j] = nid;
2141 }
2142
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002143 /* Reorder the surround channels
2144 * ALSA sequence is front/surr/clfe/side
2145 * HDA sequence is:
2146 * 4-ch: front/surr => OK as it is
2147 * 6-ch: front/clfe/surr
2148 * 8-ch: front/clfe/side/surr
2149 */
2150 switch (cfg->line_outs) {
2151 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002152 nid = cfg->line_out_pins[1];
2153 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2154 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002155 break;
2156 case 4:
2157 nid = cfg->line_out_pins[1];
2158 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2159 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2160 cfg->line_out_pins[2] = nid;
2161 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002162 }
2163
Takashi Iwai82bc9552006-03-21 11:24:42 +01002164 /*
2165 * debug prints of the parsed results
2166 */
2167 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2168 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2169 cfg->line_out_pins[2], cfg->line_out_pins[3],
2170 cfg->line_out_pins[4]);
2171 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2172 cfg->speaker_outs, cfg->speaker_pins[0],
2173 cfg->speaker_pins[1], cfg->speaker_pins[2],
2174 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002175 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2176 cfg->hp_outs, cfg->hp_pins[0],
2177 cfg->hp_pins[1], cfg->hp_pins[2],
2178 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002179 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2180 " cd=0x%x, aux=0x%x\n",
2181 cfg->input_pins[AUTO_PIN_MIC],
2182 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2183 cfg->input_pins[AUTO_PIN_LINE],
2184 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2185 cfg->input_pins[AUTO_PIN_CD],
2186 cfg->input_pins[AUTO_PIN_AUX]);
2187
2188 /*
2189 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2190 * as a primary output
2191 */
2192 if (! cfg->line_outs) {
2193 if (cfg->speaker_outs) {
2194 cfg->line_outs = cfg->speaker_outs;
2195 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2196 sizeof(cfg->speaker_pins));
2197 cfg->speaker_outs = 0;
2198 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002199 } else if (cfg->hp_outs) {
2200 cfg->line_outs = cfg->hp_outs;
2201 memcpy(cfg->line_out_pins, cfg->hp_pins,
2202 sizeof(cfg->hp_pins));
2203 cfg->hp_outs = 0;
2204 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
Takashi Iwai82bc9552006-03-21 11:24:42 +01002205 }
2206 }
2207
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002208 return 0;
2209}
2210
Takashi Iwai4a471b72005-12-07 13:56:29 +01002211/* labels for input pins */
2212const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2213 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2214};
2215
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217#ifdef CONFIG_PM
2218/*
2219 * power management
2220 */
2221
2222/**
2223 * snd_hda_suspend - suspend the codecs
2224 * @bus: the HDA bus
2225 * @state: suspsend state
2226 *
2227 * Returns 0 if successful.
2228 */
2229int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2230{
2231 struct list_head *p;
2232
2233 /* FIXME: should handle power widget capabilities */
2234 list_for_each(p, &bus->codec_list) {
2235 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2236 if (codec->patch_ops.suspend)
2237 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002238 hda_set_power_state(codec,
2239 codec->afg ? codec->afg : codec->mfg,
2240 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 return 0;
2243}
2244
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002245EXPORT_SYMBOL(snd_hda_suspend);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247/**
2248 * snd_hda_resume - resume the codecs
2249 * @bus: the HDA bus
2250 * @state: resume state
2251 *
2252 * Returns 0 if successful.
2253 */
2254int snd_hda_resume(struct hda_bus *bus)
2255{
2256 struct list_head *p;
2257
2258 list_for_each(p, &bus->codec_list) {
2259 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002260 hda_set_power_state(codec,
2261 codec->afg ? codec->afg : codec->mfg,
2262 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (codec->patch_ops.resume)
2264 codec->patch_ops.resume(codec);
2265 }
2266 return 0;
2267}
2268
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002269EXPORT_SYMBOL(snd_hda_resume);
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/**
2272 * snd_hda_resume_ctls - resume controls in the new control list
2273 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002274 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002276 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * originally for snd_hda_add_new_ctls().
2278 * The array must be terminated with an empty entry as terminator.
2279 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002280int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002282 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 val = kmalloc(sizeof(*val), GFP_KERNEL);
2285 if (! val)
2286 return -ENOMEM;
2287 codec->in_resume = 1;
2288 for (; knew->name; knew++) {
2289 int i, count;
2290 count = knew->count ? knew->count : 1;
2291 for (i = 0; i < count; i++) {
2292 memset(val, 0, sizeof(*val));
2293 val->id.iface = knew->iface;
2294 val->id.device = knew->device;
2295 val->id.subdevice = knew->subdevice;
2296 strcpy(val->id.name, knew->name);
2297 val->id.index = knew->index ? knew->index : i;
2298 /* Assume that get callback reads only from cache,
2299 * not accessing to the real hardware
2300 */
2301 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2302 continue;
2303 snd_ctl_elem_write(codec->bus->card, NULL, val);
2304 }
2305 }
2306 codec->in_resume = 0;
2307 kfree(val);
2308 return 0;
2309}
2310
2311/**
2312 * snd_hda_resume_spdif_out - resume the digital out
2313 * @codec: the HDA codec
2314 */
2315int snd_hda_resume_spdif_out(struct hda_codec *codec)
2316{
2317 return snd_hda_resume_ctls(codec, dig_mixes);
2318}
2319
2320/**
2321 * snd_hda_resume_spdif_in - resume the digital in
2322 * @codec: the HDA codec
2323 */
2324int snd_hda_resume_spdif_in(struct hda_codec *codec)
2325{
2326 return snd_hda_resume_ctls(codec, dig_in_ctls);
2327}
2328#endif
2329
2330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * INIT part
2332 */
2333
2334static int __init alsa_hda_init(void)
2335{
2336 return 0;
2337}
2338
2339static void __exit alsa_hda_exit(void)
2340{
2341}
2342
2343module_init(alsa_hda_init)
2344module_exit(alsa_hda_exit)