blob: 8b2c080c85aed07d3c730683fda1d402f582bb91 [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" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020055 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020057 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020059 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 {} /* terminator */
61};
62
63/* codec presets */
64#include "hda_patch.h"
65
66
67/**
68 * snd_hda_codec_read - send a command and get the response
69 * @codec: the HDA codec
70 * @nid: NID to send the command
71 * @direct: direct flag
72 * @verb: the verb to send
73 * @parm: the parameter for the verb
74 *
75 * Send a single command and read the corresponding response.
76 *
77 * Returns the obtained response value, or -1 for an error.
78 */
79unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
80 unsigned int verb, unsigned int parm)
81{
82 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010083 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
85 res = codec->bus->ops.get_response(codec);
86 else
87 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010088 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return res;
90}
91
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +020092EXPORT_SYMBOL(snd_hda_codec_read);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/**
95 * snd_hda_codec_write - send a single command without waiting for response
96 * @codec: the HDA codec
97 * @nid: NID to send the command
98 * @direct: direct flag
99 * @verb: the verb to send
100 * @parm: the parameter for the verb
101 *
102 * Send a single command without waiting for response.
103 *
104 * Returns 0 if successful, or a negative error code.
105 */
106int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
107 unsigned int verb, unsigned int parm)
108{
109 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100110 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100112 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return err;
114}
115
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200116EXPORT_SYMBOL(snd_hda_codec_write);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/**
119 * snd_hda_sequence_write - sequence writes
120 * @codec: the HDA codec
121 * @seq: VERB array to send
122 *
123 * Send the commands sequentially from the given array.
124 * The array must be terminated with NID=0.
125 */
126void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
127{
128 for (; seq->nid; seq++)
129 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
130}
131
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200132EXPORT_SYMBOL(snd_hda_sequence_write);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/**
135 * snd_hda_get_sub_nodes - get the range of sub nodes
136 * @codec: the HDA codec
137 * @nid: NID to parse
138 * @start_id: the pointer to store the start NID
139 *
140 * Parse the NID and store the start NID of its sub-nodes.
141 * Returns the number of sub-nodes.
142 */
143int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
144{
145 unsigned int parm;
146
147 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
148 *start_id = (parm >> 16) & 0x7fff;
149 return (int)(parm & 0x7fff);
150}
151
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200152EXPORT_SYMBOL(snd_hda_get_sub_nodes);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/**
155 * snd_hda_get_connections - get connection list
156 * @codec: the HDA codec
157 * @nid: NID to parse
158 * @conn_list: connection list array
159 * @max_conns: max. number of connections to store
160 *
161 * Parses the connection list of the given widget and stores the list
162 * of NIDs.
163 *
164 * Returns the number of connections, or a negative error code.
165 */
166int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
167 hda_nid_t *conn_list, int max_conns)
168{
169 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100170 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100172 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 snd_assert(conn_list && max_conns > 0, return -EINVAL);
175
176 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
177 if (parm & AC_CLIST_LONG) {
178 /* long form */
179 shift = 16;
180 num_elems = 2;
181 } else {
182 /* short form */
183 shift = 8;
184 num_elems = 4;
185 }
186 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 mask = (1 << (shift-1)) - 1;
188
189 if (! conn_len)
190 return 0; /* no connection */
191
192 if (conn_len == 1) {
193 /* single connection */
194 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
195 conn_list[0] = parm & mask;
196 return 1;
197 }
198
199 /* multi connection */
200 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100201 prev_nid = 0;
202 for (i = 0; i < conn_len; i++) {
203 int range_val;
204 hda_nid_t val, n;
205
206 if (i % num_elems == 0)
207 parm = snd_hda_codec_read(codec, nid, 0,
208 AC_VERB_GET_CONNECT_LIST, i);
209 range_val = !! (parm & (1 << (shift-1))); /* ranges */
210 val = parm & mask;
211 parm >>= shift;
212 if (range_val) {
213 /* ranges between the previous and this one */
214 if (! prev_nid || prev_nid >= val) {
215 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
216 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100218 for (n = prev_nid + 1; n <= val; n++) {
219 if (conns >= max_conns) {
220 snd_printk(KERN_ERR "Too many connections\n");
221 return -EINVAL;
222 }
223 conn_list[conns++] = n;
224 }
225 } else {
226 if (conns >= max_conns) {
227 snd_printk(KERN_ERR "Too many connections\n");
228 return -EINVAL;
229 }
230 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100232 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 return conns;
235}
236
237
238/**
239 * snd_hda_queue_unsol_event - add an unsolicited event to queue
240 * @bus: the BUS
241 * @res: unsolicited event (lower 32bit of RIRB entry)
242 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
243 *
244 * Adds the given event to the queue. The events are processed in
245 * the workqueue asynchronously. Call this function in the interrupt
246 * hanlder when RIRB receives an unsolicited event.
247 *
248 * Returns 0 if successful, or a negative error code.
249 */
250int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
251{
252 struct hda_bus_unsolicited *unsol;
253 unsigned int wp;
254
255 if ((unsol = bus->unsol) == NULL)
256 return 0;
257
258 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
259 unsol->wp = wp;
260
261 wp <<= 1;
262 unsol->queue[wp] = res;
263 unsol->queue[wp + 1] = res_ex;
264
265 queue_work(unsol->workq, &unsol->work);
266
267 return 0;
268}
269
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200270EXPORT_SYMBOL(snd_hda_queue_unsol_event);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * process queueud unsolicited events
274 */
275static void process_unsol_events(void *data)
276{
277 struct hda_bus *bus = data;
278 struct hda_bus_unsolicited *unsol = bus->unsol;
279 struct hda_codec *codec;
280 unsigned int rp, caddr, res;
281
282 while (unsol->rp != unsol->wp) {
283 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
284 unsol->rp = rp;
285 rp <<= 1;
286 res = unsol->queue[rp];
287 caddr = unsol->queue[rp + 1];
288 if (! (caddr & (1 << 4))) /* no unsolicited event? */
289 continue;
290 codec = bus->caddr_tbl[caddr & 0x0f];
291 if (codec && codec->patch_ops.unsol_event)
292 codec->patch_ops.unsol_event(codec, res);
293 }
294}
295
296/*
297 * initialize unsolicited queue
298 */
299static int init_unsol_queue(struct hda_bus *bus)
300{
301 struct hda_bus_unsolicited *unsol;
302
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100303 if (bus->unsol) /* already initialized */
304 return 0;
305
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200306 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (! unsol) {
308 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
309 return -ENOMEM;
310 }
OGAWA Hirofumice7415f2006-03-31 12:36:14 +0200311 unsol->workq = create_singlethread_workqueue("hda_codec");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (! unsol->workq) {
313 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
314 kfree(unsol);
315 return -ENOMEM;
316 }
317 INIT_WORK(&unsol->work, process_unsol_events, bus);
318 bus->unsol = unsol;
319 return 0;
320}
321
322/*
323 * destructor
324 */
325static void snd_hda_codec_free(struct hda_codec *codec);
326
327static int snd_hda_bus_free(struct hda_bus *bus)
328{
329 struct list_head *p, *n;
330
331 if (! bus)
332 return 0;
333 if (bus->unsol) {
334 destroy_workqueue(bus->unsol->workq);
335 kfree(bus->unsol);
336 }
337 list_for_each_safe(p, n, &bus->codec_list) {
338 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
339 snd_hda_codec_free(codec);
340 }
341 if (bus->ops.private_free)
342 bus->ops.private_free(bus);
343 kfree(bus);
344 return 0;
345}
346
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100347static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct hda_bus *bus = device->device_data;
350 return snd_hda_bus_free(bus);
351}
352
353/**
354 * snd_hda_bus_new - create a HDA bus
355 * @card: the card entry
356 * @temp: the template for hda_bus information
357 * @busp: the pointer to store the created bus instance
358 *
359 * Returns 0 if successful, or a negative error code.
360 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100361int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct hda_bus **busp)
363{
364 struct hda_bus *bus;
365 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100366 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 .dev_free = snd_hda_bus_dev_free,
368 };
369
370 snd_assert(temp, return -EINVAL);
371 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
372
373 if (busp)
374 *busp = NULL;
375
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200376 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (bus == NULL) {
378 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
379 return -ENOMEM;
380 }
381
382 bus->card = card;
383 bus->private_data = temp->private_data;
384 bus->pci = temp->pci;
385 bus->modelname = temp->modelname;
386 bus->ops = temp->ops;
387
Ingo Molnar62932df2006-01-16 16:34:20 +0100388 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 INIT_LIST_HEAD(&bus->codec_list);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
392 snd_hda_bus_free(bus);
393 return err;
394 }
395 if (busp)
396 *busp = bus;
397 return 0;
398}
399
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200400EXPORT_SYMBOL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/*
403 * find a matching codec preset
404 */
405static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
406{
407 const struct hda_codec_preset **tbl, *preset;
408
409 for (tbl = hda_preset_tables; *tbl; tbl++) {
410 for (preset = *tbl; preset->id; preset++) {
411 u32 mask = preset->mask;
412 if (! mask)
413 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200414 if (preset->id == (codec->vendor_id & mask) &&
415 (! preset->rev ||
416 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return preset;
418 }
419 }
420 return NULL;
421}
422
423/*
424 * snd_hda_get_codec_name - store the codec name
425 */
426void snd_hda_get_codec_name(struct hda_codec *codec,
427 char *name, int namelen)
428{
429 const struct hda_vendor_id *c;
430 const char *vendor = NULL;
431 u16 vendor_id = codec->vendor_id >> 16;
432 char tmp[16];
433
434 for (c = hda_vendor_ids; c->id; c++) {
435 if (c->id == vendor_id) {
436 vendor = c->name;
437 break;
438 }
439 }
440 if (! vendor) {
441 sprintf(tmp, "Generic %04x", vendor_id);
442 vendor = tmp;
443 }
444 if (codec->preset && codec->preset->name)
445 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
446 else
447 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
448}
449
450/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200451 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200453static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int i, total_nodes;
456 hda_nid_t nid;
457
458 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
459 for (i = 0; i < total_nodes; i++, nid++) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200460 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
461 case AC_GRP_AUDIO_FUNCTION:
462 codec->afg = nid;
463 break;
464 case AC_GRP_MODEM_FUNCTION:
465 codec->mfg = nid;
466 break;
467 default:
468 break;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 * read widget caps for each widget and store in cache
475 */
476static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
477{
478 int i;
479 hda_nid_t nid;
480
481 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
482 &codec->start_nid);
483 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
484 if (! codec->wcaps)
485 return -ENOMEM;
486 nid = codec->start_nid;
487 for (i = 0; i < codec->num_nodes; i++, nid++)
488 codec->wcaps[i] = snd_hda_param_read(codec, nid,
489 AC_PAR_AUDIO_WIDGET_CAP);
490 return 0;
491}
492
493
494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * codec destructor
496 */
497static void snd_hda_codec_free(struct hda_codec *codec)
498{
499 if (! codec)
500 return;
501 list_del(&codec->list);
502 codec->bus->caddr_tbl[codec->addr] = NULL;
503 if (codec->patch_ops.free)
504 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100505 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 kfree(codec);
508}
509
510static void init_amp_hash(struct hda_codec *codec);
511
512/**
513 * snd_hda_codec_new - create a HDA codec
514 * @bus: the bus to assign
515 * @codec_addr: the codec address
516 * @codecp: the pointer to store the generated codec
517 *
518 * Returns 0 if successful, or a negative error code.
519 */
520int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
521 struct hda_codec **codecp)
522{
523 struct hda_codec *codec;
524 char component[13];
525 int err;
526
527 snd_assert(bus, return -EINVAL);
528 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
529
530 if (bus->caddr_tbl[codec_addr]) {
531 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
532 return -EBUSY;
533 }
534
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200535 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (codec == NULL) {
537 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
538 return -ENOMEM;
539 }
540
541 codec->bus = bus;
542 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100543 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 init_amp_hash(codec);
545
546 list_add_tail(&codec->list, &bus->codec_list);
547 bus->caddr_tbl[codec_addr] = codec;
548
549 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100550 if (codec->vendor_id == -1)
551 /* read again, hopefully the access method was corrected
552 * in the last read...
553 */
554 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
555 AC_PAR_VENDOR_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
557 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
558
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200559 setup_fg_nodes(codec);
560 if (! codec->afg && ! codec->mfg) {
561 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 snd_hda_codec_free(codec);
563 return -ENODEV;
564 }
565
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
567 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
568 snd_hda_codec_free(codec);
569 return -ENOMEM;
570 }
571
Takashi Iwai86284e42005-10-11 15:05:54 +0200572 if (! codec->subsystem_id) {
573 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
574 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
575 AC_VERB_GET_SUBSYSTEM_ID,
576 0);
577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 codec->preset = find_codec_preset(codec);
580 if (! *bus->card->mixername)
581 snd_hda_get_codec_name(codec, bus->card->mixername,
582 sizeof(bus->card->mixername));
583
584 if (codec->preset && codec->preset->patch)
585 err = codec->preset->patch(codec);
586 else
587 err = snd_hda_parse_generic_codec(codec);
588 if (err < 0) {
589 snd_hda_codec_free(codec);
590 return err;
591 }
592
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100593 if (codec->patch_ops.unsol_event)
594 init_unsol_queue(bus);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 snd_hda_codec_proc_new(codec);
597
598 sprintf(component, "HDA:%08x", codec->vendor_id);
599 snd_component_add(codec->bus->card, component);
600
601 if (codecp)
602 *codecp = codec;
603 return 0;
604}
605
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200606EXPORT_SYMBOL(snd_hda_codec_new);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/**
609 * snd_hda_codec_setup_stream - set up the codec for streaming
610 * @codec: the CODEC to set up
611 * @nid: the NID to set up
612 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
613 * @channel_id: channel id to pass, zero based.
614 * @format: stream format.
615 */
616void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
617 int channel_id, int format)
618{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200619 if (! nid)
620 return;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
623 nid, stream_tag, channel_id, format);
624 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
625 (stream_tag << 4) | channel_id);
626 msleep(1);
627 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
628}
629
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200630EXPORT_SYMBOL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/*
633 * amp access functions
634 */
635
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200636/* FIXME: more better hash key? */
637#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200639#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641/* initialize the hash table */
642static void init_amp_hash(struct hda_codec *codec)
643{
644 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
645 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100646 codec->amp_info_size = 0;
647 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/* query the hash. allocate an entry if not found. */
651static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
652{
653 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
654 u16 cur = codec->amp_hash[idx];
655 struct hda_amp_info *info;
656
657 while (cur != 0xffff) {
658 info = &codec->amp_info[cur];
659 if (info->key == key)
660 return info;
661 cur = info->next;
662 }
663
664 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100665 if (codec->num_amp_entries >= codec->amp_info_size) {
666 /* reallocate the array */
667 int new_size = codec->amp_info_size + 64;
668 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
669 GFP_KERNEL);
670 if (! new_info) {
671 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
672 return NULL;
673 }
674 if (codec->amp_info) {
675 memcpy(new_info, codec->amp_info,
676 codec->amp_info_size * sizeof(struct hda_amp_info));
677 kfree(codec->amp_info);
678 }
679 codec->amp_info_size = new_size;
680 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 cur = codec->num_amp_entries++;
683 info = &codec->amp_info[cur];
684 info->key = key;
685 info->status = 0; /* not initialized yet */
686 info->next = codec->amp_hash[idx];
687 codec->amp_hash[idx] = cur;
688
689 return info;
690}
691
692/*
693 * query AMP capabilities for the given widget and direction
694 */
695static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
696{
697 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
698
699 if (! info)
700 return 0;
701 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100702 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 nid = codec->afg;
704 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
705 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
706 info->status |= INFO_AMP_CAPS;
707 }
708 return info->amp_caps;
709}
710
711/*
712 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200713 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200715static unsigned int get_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)
717{
718 u32 val, parm;
719
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200720 if (info->status & INFO_AMP_VOL(ch))
721 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
724 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
725 parm |= index;
726 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
727 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200728 info->status |= INFO_AMP_VOL(ch);
729 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200733 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200735static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 hda_nid_t nid, int ch, int direction, int index, int val)
737{
738 u32 parm;
739
740 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
741 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
742 parm |= index << AC_AMP_SET_INDEX_SHIFT;
743 parm |= val;
744 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200745 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200749 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Takashi Iwai834be882006-03-01 14:16:17 +0100751int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
752 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
755 if (! info)
756 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200757 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200760/*
761 * update the AMP value, mask = bit mask to set, val = the value
762 */
Takashi Iwai834be882006-03-01 14:16:17 +0100763int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
764 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (! info)
769 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200770 val &= mask;
771 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (info->vol[ch] == val && ! codec->in_resume)
773 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200774 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 1;
776}
777
778
779/*
780 * AMP control callbacks
781 */
782/* retrieve parameters from private_value */
783#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
784#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
785#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
786#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
787
788/* volume */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100789int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
792 u16 nid = get_amp_nid(kcontrol);
793 u8 chs = get_amp_channels(kcontrol);
794 int dir = get_amp_direction(kcontrol);
795 u32 caps;
796
797 caps = query_amp_caps(codec, nid, dir);
798 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
799 if (! caps) {
800 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
801 return -EINVAL;
802 }
803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
804 uinfo->count = chs == 3 ? 2 : 1;
805 uinfo->value.integer.min = 0;
806 uinfo->value.integer.max = caps;
807 return 0;
808}
809
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100810int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
813 hda_nid_t nid = get_amp_nid(kcontrol);
814 int chs = get_amp_channels(kcontrol);
815 int dir = get_amp_direction(kcontrol);
816 int idx = get_amp_index(kcontrol);
817 long *valp = ucontrol->value.integer.value;
818
819 if (chs & 1)
820 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
821 if (chs & 2)
822 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
823 return 0;
824}
825
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100826int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
829 hda_nid_t nid = get_amp_nid(kcontrol);
830 int chs = get_amp_channels(kcontrol);
831 int dir = get_amp_direction(kcontrol);
832 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 long *valp = ucontrol->value.integer.value;
834 int change = 0;
835
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200836 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200837 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
838 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200839 valp++;
840 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200841 if (chs & 2)
842 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200843 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return change;
845}
846
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200847int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
848 unsigned int size, unsigned int __user *_tlv)
849{
850 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
851 hda_nid_t nid = get_amp_nid(kcontrol);
852 int dir = get_amp_direction(kcontrol);
853 u32 caps, val1, val2;
854
855 if (size < 4 * sizeof(unsigned int))
856 return -ENOMEM;
857 caps = query_amp_caps(codec, nid, dir);
858 val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25;
859 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
860 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200861 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
862 return -EFAULT;
863 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
864 return -EFAULT;
865 if (put_user(val1, _tlv + 2))
866 return -EFAULT;
867 if (put_user(val2, _tlv + 3))
868 return -EFAULT;
869 return 0;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/* switch */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100873int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 int chs = get_amp_channels(kcontrol);
876
877 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
878 uinfo->count = chs == 3 ? 2 : 1;
879 uinfo->value.integer.min = 0;
880 uinfo->value.integer.max = 1;
881 return 0;
882}
883
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100884int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
887 hda_nid_t nid = get_amp_nid(kcontrol);
888 int chs = get_amp_channels(kcontrol);
889 int dir = get_amp_direction(kcontrol);
890 int idx = get_amp_index(kcontrol);
891 long *valp = ucontrol->value.integer.value;
892
893 if (chs & 1)
894 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
895 if (chs & 2)
896 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
897 return 0;
898}
899
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100900int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
903 hda_nid_t nid = get_amp_nid(kcontrol);
904 int chs = get_amp_channels(kcontrol);
905 int dir = get_amp_direction(kcontrol);
906 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 long *valp = ucontrol->value.integer.value;
908 int change = 0;
909
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200910 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200911 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
912 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200913 valp++;
914 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200915 if (chs & 2)
916 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200917 0x80, *valp ? 0 : 0x80);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return change;
920}
921
922/*
Takashi Iwai985be542005-11-02 18:26:49 +0100923 * bound volume controls
924 *
925 * bind multiple volumes (# indices, from 0)
926 */
927
928#define AMP_VAL_IDX_SHIFT 19
929#define AMP_VAL_IDX_MASK (0x0f<<19)
930
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100931int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100932{
933 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
934 unsigned long pval;
935 int err;
936
Ingo Molnar62932df2006-01-16 16:34:20 +0100937 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100938 pval = kcontrol->private_value;
939 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
940 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
941 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100942 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100943 return err;
944}
945
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100946int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100947{
948 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
949 unsigned long pval;
950 int i, indices, err = 0, change = 0;
951
Ingo Molnar62932df2006-01-16 16:34:20 +0100952 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100953 pval = kcontrol->private_value;
954 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
955 for (i = 0; i < indices; i++) {
956 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
957 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
958 if (err < 0)
959 break;
960 change |= err;
961 }
962 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100963 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100964 return err < 0 ? err : change;
965}
966
967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * SPDIF out controls
969 */
970
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100971static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
974 uinfo->count = 1;
975 return 0;
976}
977
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100978static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
981 IEC958_AES0_NONAUDIO |
982 IEC958_AES0_CON_EMPHASIS_5015 |
983 IEC958_AES0_CON_NOT_COPYRIGHT;
984 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
985 IEC958_AES1_CON_ORIGINAL;
986 return 0;
987}
988
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100989static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
992 IEC958_AES0_NONAUDIO |
993 IEC958_AES0_PRO_EMPHASIS_5015;
994 return 0;
995}
996
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100997static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1000
1001 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1002 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1003 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1004 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1005
1006 return 0;
1007}
1008
1009/* convert from SPDIF status bits to HDA SPDIF bits
1010 * bit 0 (DigEn) is always set zero (to be filled later)
1011 */
1012static unsigned short convert_from_spdif_status(unsigned int sbits)
1013{
1014 unsigned short val = 0;
1015
1016 if (sbits & IEC958_AES0_PROFESSIONAL)
1017 val |= 1 << 6;
1018 if (sbits & IEC958_AES0_NONAUDIO)
1019 val |= 1 << 5;
1020 if (sbits & IEC958_AES0_PROFESSIONAL) {
1021 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1022 val |= 1 << 3;
1023 } else {
1024 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
1025 val |= 1 << 3;
1026 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1027 val |= 1 << 4;
1028 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1029 val |= 1 << 7;
1030 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1031 }
1032 return val;
1033}
1034
1035/* convert to SPDIF status bits from HDA SPDIF bits
1036 */
1037static unsigned int convert_to_spdif_status(unsigned short val)
1038{
1039 unsigned int sbits = 0;
1040
1041 if (val & (1 << 5))
1042 sbits |= IEC958_AES0_NONAUDIO;
1043 if (val & (1 << 6))
1044 sbits |= IEC958_AES0_PROFESSIONAL;
1045 if (sbits & IEC958_AES0_PROFESSIONAL) {
1046 if (sbits & (1 << 3))
1047 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1048 } else {
1049 if (val & (1 << 3))
1050 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1051 if (! (val & (1 << 4)))
1052 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1053 if (val & (1 << 7))
1054 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1055 sbits |= val & (0x7f << 8);
1056 }
1057 return sbits;
1058}
1059
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001060static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1063 hda_nid_t nid = kcontrol->private_value;
1064 unsigned short val;
1065 int change;
1066
Ingo Molnar62932df2006-01-16 16:34:20 +01001067 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 codec->spdif_status = ucontrol->value.iec958.status[0] |
1069 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1070 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1071 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1072 val = convert_from_spdif_status(codec->spdif_status);
1073 val |= codec->spdif_ctls & 1;
1074 change = codec->spdif_ctls != val;
1075 codec->spdif_ctls = val;
1076
1077 if (change || codec->in_resume) {
1078 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1079 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1080 }
1081
Ingo Molnar62932df2006-01-16 16:34:20 +01001082 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return change;
1084}
1085
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001086static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1089 uinfo->count = 1;
1090 uinfo->value.integer.min = 0;
1091 uinfo->value.integer.max = 1;
1092 return 0;
1093}
1094
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001095static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1098
1099 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1100 return 0;
1101}
1102
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001103static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1106 hda_nid_t nid = kcontrol->private_value;
1107 unsigned short val;
1108 int change;
1109
Ingo Molnar62932df2006-01-16 16:34:20 +01001110 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 val = codec->spdif_ctls & ~1;
1112 if (ucontrol->value.integer.value[0])
1113 val |= 1;
1114 change = codec->spdif_ctls != val;
1115 if (change || codec->in_resume) {
1116 codec->spdif_ctls = val;
1117 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1118 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1119 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1120 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1121 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001122 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return change;
1124}
1125
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001126static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 {
1128 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1129 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1130 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1131 .info = snd_hda_spdif_mask_info,
1132 .get = snd_hda_spdif_cmask_get,
1133 },
1134 {
1135 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1136 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1137 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1138 .info = snd_hda_spdif_mask_info,
1139 .get = snd_hda_spdif_pmask_get,
1140 },
1141 {
1142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1143 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1144 .info = snd_hda_spdif_mask_info,
1145 .get = snd_hda_spdif_default_get,
1146 .put = snd_hda_spdif_default_put,
1147 },
1148 {
1149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1150 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1151 .info = snd_hda_spdif_out_switch_info,
1152 .get = snd_hda_spdif_out_switch_get,
1153 .put = snd_hda_spdif_out_switch_put,
1154 },
1155 { } /* end */
1156};
1157
1158/**
1159 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1160 * @codec: the HDA codec
1161 * @nid: audio out widget NID
1162 *
1163 * Creates controls related with the SPDIF output.
1164 * Called from each patch supporting the SPDIF out.
1165 *
1166 * Returns 0 if successful, or a negative error code.
1167 */
1168int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1169{
1170 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001171 struct snd_kcontrol *kctl;
1172 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1175 kctl = snd_ctl_new1(dig_mix, codec);
1176 kctl->private_value = nid;
1177 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1178 return err;
1179 }
1180 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1181 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1182 return 0;
1183}
1184
1185/*
1186 * SPDIF input
1187 */
1188
1189#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1190
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001191static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1194
1195 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1196 return 0;
1197}
1198
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001199static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1202 hda_nid_t nid = kcontrol->private_value;
1203 unsigned int val = !!ucontrol->value.integer.value[0];
1204 int change;
1205
Ingo Molnar62932df2006-01-16 16:34:20 +01001206 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 change = codec->spdif_in_enable != val;
1208 if (change || codec->in_resume) {
1209 codec->spdif_in_enable = val;
1210 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1211 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001212 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return change;
1214}
1215
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001216static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1219 hda_nid_t nid = kcontrol->private_value;
1220 unsigned short val;
1221 unsigned int sbits;
1222
1223 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1224 sbits = convert_to_spdif_status(val);
1225 ucontrol->value.iec958.status[0] = sbits;
1226 ucontrol->value.iec958.status[1] = sbits >> 8;
1227 ucontrol->value.iec958.status[2] = sbits >> 16;
1228 ucontrol->value.iec958.status[3] = sbits >> 24;
1229 return 0;
1230}
1231
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001232static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 {
1234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1235 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1236 .info = snd_hda_spdif_in_switch_info,
1237 .get = snd_hda_spdif_in_switch_get,
1238 .put = snd_hda_spdif_in_switch_put,
1239 },
1240 {
1241 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1242 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1243 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1244 .info = snd_hda_spdif_mask_info,
1245 .get = snd_hda_spdif_in_status_get,
1246 },
1247 { } /* end */
1248};
1249
1250/**
1251 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1252 * @codec: the HDA codec
1253 * @nid: audio in widget NID
1254 *
1255 * Creates controls related with the SPDIF input.
1256 * Called from each patch supporting the SPDIF in.
1257 *
1258 * Returns 0 if successful, or a negative error code.
1259 */
1260int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1261{
1262 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001263 struct snd_kcontrol *kctl;
1264 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1267 kctl = snd_ctl_new1(dig_mix, codec);
1268 kctl->private_value = nid;
1269 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1270 return err;
1271 }
1272 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1273 return 0;
1274}
1275
1276
Takashi Iwai54d17402005-11-21 16:33:22 +01001277/*
1278 * set power state of the codec
1279 */
1280static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1281 unsigned int power_state)
1282{
1283 hda_nid_t nid, nid_start;
1284 int nodes;
1285
1286 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1287 power_state);
1288
1289 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1290 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1291 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1292 snd_hda_codec_write(codec, nid, 0,
1293 AC_VERB_SET_POWER_STATE,
1294 power_state);
1295 }
1296
1297 if (power_state == AC_PWRST_D0)
1298 msleep(10);
1299}
1300
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/**
1303 * snd_hda_build_controls - build mixer controls
1304 * @bus: the BUS
1305 *
1306 * Creates mixer controls for each codec included in the bus.
1307 *
1308 * Returns 0 if successful, otherwise a negative error code.
1309 */
1310int snd_hda_build_controls(struct hda_bus *bus)
1311{
1312 struct list_head *p;
1313
1314 /* build controls */
1315 list_for_each(p, &bus->codec_list) {
1316 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1317 int err;
1318 if (! codec->patch_ops.build_controls)
1319 continue;
1320 err = codec->patch_ops.build_controls(codec);
1321 if (err < 0)
1322 return err;
1323 }
1324
1325 /* initialize */
1326 list_for_each(p, &bus->codec_list) {
1327 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1328 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001329 hda_set_power_state(codec,
1330 codec->afg ? codec->afg : codec->mfg,
1331 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (! codec->patch_ops.init)
1333 continue;
1334 err = codec->patch_ops.init(codec);
1335 if (err < 0)
1336 return err;
1337 }
1338 return 0;
1339}
1340
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001341EXPORT_SYMBOL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343/*
1344 * stream formats
1345 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001346struct hda_rate_tbl {
1347 unsigned int hz;
1348 unsigned int alsa_bits;
1349 unsigned int hda_fmt;
1350};
1351
1352static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001354
1355 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1357 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1358 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1359 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1360 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1361 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1362 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1363 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1364 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1365 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1366 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001367
1368 /* not autodetected value */
1369 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001370
1371 { 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
1718 * @tbl: configuration table, terminated by null entries
1719 *
1720 * Compares the modelname or PCI subsystem id of the current codec with the
1721 * given configuration table. If a matching entry is found, returns its
1722 * config value (supposed to be 0 or positive).
1723 *
1724 * If no entries are matching, the function returns a negative value.
1725 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001726int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001728 const struct hda_board_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (codec->bus->modelname) {
Takashi Iwai72915482005-05-12 16:49:45 +02001731 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (c->modelname &&
1733 ! strcmp(codec->bus->modelname, c->modelname)) {
1734 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1735 return c->config;
1736 }
1737 }
1738 }
1739
1740 if (codec->bus->pci) {
1741 u16 subsystem_vendor, subsystem_device;
1742 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1743 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
Takashi Iwai72915482005-05-12 16:49:45 +02001744 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1745 if (c->pci_subvendor == subsystem_vendor &&
Takashi Iwai5ecd7022005-06-10 19:54:23 +02001746 (! c->pci_subdevice /* all match */||
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001747 (c->pci_subdevice == subsystem_device))) {
1748 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1749 subsystem_vendor, subsystem_device, c->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return c->config;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753 }
1754 return -1;
1755}
1756
1757/**
1758 * snd_hda_add_new_ctls - create controls from the array
1759 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001760 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 *
1762 * This helper function creates and add new controls in the given array.
1763 * The array must be terminated with an empty entry as terminator.
1764 *
1765 * Returns 0 if successful, or a negative error code.
1766 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001767int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 int err;
1770
1771 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001772 struct snd_kcontrol *kctl;
1773 kctl = snd_ctl_new1(knew, codec);
1774 if (! kctl)
1775 return -ENOMEM;
1776 err = snd_ctl_add(codec->bus->card, kctl);
1777 if (err < 0) {
1778 if (! codec->addr)
1779 return err;
1780 kctl = snd_ctl_new1(knew, codec);
1781 if (! kctl)
1782 return -ENOMEM;
1783 kctl->id.device = codec->addr;
1784 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1785 return err;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 return 0;
1789}
1790
1791
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001792/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001793 * Channel mode helper
1794 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001795int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001796 const struct hda_channel_mode *chmode, int num_chmodes)
1797{
1798 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1799 uinfo->count = 1;
1800 uinfo->value.enumerated.items = num_chmodes;
1801 if (uinfo->value.enumerated.item >= num_chmodes)
1802 uinfo->value.enumerated.item = num_chmodes - 1;
1803 sprintf(uinfo->value.enumerated.name, "%dch",
1804 chmode[uinfo->value.enumerated.item].channels);
1805 return 0;
1806}
1807
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001808int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001809 const struct hda_channel_mode *chmode, int num_chmodes,
1810 int max_channels)
1811{
1812 int i;
1813
1814 for (i = 0; i < num_chmodes; i++) {
1815 if (max_channels == chmode[i].channels) {
1816 ucontrol->value.enumerated.item[0] = i;
1817 break;
1818 }
1819 }
1820 return 0;
1821}
1822
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001823int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001824 const struct hda_channel_mode *chmode, int num_chmodes,
1825 int *max_channelsp)
1826{
1827 unsigned int mode;
1828
1829 mode = ucontrol->value.enumerated.item[0];
1830 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001831 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001832 return 0;
1833 /* change the current channel setting */
1834 *max_channelsp = chmode[mode].channels;
1835 if (chmode[mode].sequence)
1836 snd_hda_sequence_write(codec, chmode[mode].sequence);
1837 return 1;
1838}
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840/*
1841 * input MUX helper
1842 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001843int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 unsigned int index;
1846
1847 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1848 uinfo->count = 1;
1849 uinfo->value.enumerated.items = imux->num_items;
1850 index = uinfo->value.enumerated.item;
1851 if (index >= imux->num_items)
1852 index = imux->num_items - 1;
1853 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1854 return 0;
1855}
1856
1857int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001858 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned int *cur_val)
1860{
1861 unsigned int idx;
1862
1863 idx = ucontrol->value.enumerated.item[0];
1864 if (idx >= imux->num_items)
1865 idx = imux->num_items - 1;
1866 if (*cur_val == idx && ! codec->in_resume)
1867 return 0;
1868 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1869 imux->items[idx].index);
1870 *cur_val = idx;
1871 return 1;
1872}
1873
1874
1875/*
1876 * Multi-channel / digital-out PCM helper functions
1877 */
1878
1879/*
1880 * open the digital out in the exclusive mode
1881 */
1882int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1883{
Ingo Molnar62932df2006-01-16 16:34:20 +01001884 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (mout->dig_out_used) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001886 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return -EBUSY; /* already being used */
1888 }
1889 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001890 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
1894/*
1895 * release the digital out
1896 */
1897int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1898{
Ingo Molnar62932df2006-01-16 16:34:20 +01001899 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001901 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
1903}
1904
1905/*
1906 * set up more restrictions for analog out
1907 */
1908int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001909 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 substream->runtime->hw.channels_max = mout->max_channels;
1912 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1913 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1914}
1915
1916/*
1917 * set up the i/o for analog out
1918 * when the digital out is available, copy the front out to digital out, too.
1919 */
1920int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1921 unsigned int stream_tag,
1922 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001923 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 hda_nid_t *nids = mout->dac_nids;
1926 int chs = substream->runtime->channels;
1927 int i;
1928
Ingo Molnar62932df2006-01-16 16:34:20 +01001929 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1931 if (chs == 2 &&
1932 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1933 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1934 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1935 /* setup digital receiver */
1936 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1937 stream_tag, 0, format);
1938 } else {
1939 mout->dig_out_used = 0;
1940 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1941 }
1942 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001943 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 /* front */
1946 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02001947 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* headphone out will just decode front left/right (stereo) */
1949 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001950 /* extra outputs copied from front */
1951 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1952 if (mout->extra_out_nid[i])
1953 snd_hda_codec_setup_stream(codec,
1954 mout->extra_out_nid[i],
1955 stream_tag, 0, format);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* surrounds */
1958 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001959 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1961 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001962 else /* copy front */
1963 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1964 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 return 0;
1967}
1968
1969/*
1970 * clean up the setting for analog out
1971 */
1972int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1973{
1974 hda_nid_t *nids = mout->dac_nids;
1975 int i;
1976
1977 for (i = 0; i < mout->num_dacs; i++)
1978 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1979 if (mout->hp_nid)
1980 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001981 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1982 if (mout->extra_out_nid[i])
1983 snd_hda_codec_setup_stream(codec,
1984 mout->extra_out_nid[i],
1985 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01001986 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1988 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1989 mout->dig_out_used = 0;
1990 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001991 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return 0;
1993}
1994
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001995/*
1996 * Helper for automatic ping configuration
1997 */
Kailang Yangdf694da2005-12-05 19:42:22 +01001998
1999static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2000{
2001 for (; *list; list++)
2002 if (*list == nid)
2003 return 1;
2004 return 0;
2005}
2006
Takashi Iwai82bc9552006-03-21 11:24:42 +01002007/*
2008 * Parse all pin widgets and store the useful pin nids to cfg
2009 *
2010 * The number of line-outs or any primary output is stored in line_outs,
2011 * and the corresponding output pins are assigned to line_out_pins[],
2012 * in the order of front, rear, CLFE, side, ...
2013 *
2014 * If more extra outputs (speaker and headphone) are found, the pins are
2015 * assisnged to hp_pin and speaker_pins[], respectively. If no line-out jack
2016 * is detected, one of speaker of HP pins is assigned as the primary
2017 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2018 * if any analog output exists.
2019 *
2020 * The analog input pins are assigned to input_pins array.
2021 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2022 * respectively.
2023 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002024int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
2025 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002026{
2027 hda_nid_t nid, nid_start;
2028 int i, j, nodes;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002029 short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002030
2031 memset(cfg, 0, sizeof(*cfg));
2032
2033 memset(sequences, 0, sizeof(sequences));
2034 assoc_line_out = 0;
2035
2036 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2037 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002038 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002039 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2040 unsigned int def_conf;
2041 short assoc, loc;
2042
2043 /* read all default configuration for pin complex */
2044 if (wid_type != AC_WID_PIN)
2045 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002046 /* ignore the given nids (e.g. pc-beep returns error) */
2047 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2048 continue;
2049
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002050 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
2051 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2052 continue;
2053 loc = get_defcfg_location(def_conf);
2054 switch (get_defcfg_device(def_conf)) {
2055 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002056 seq = get_defcfg_sequence(def_conf);
2057 assoc = get_defcfg_association(def_conf);
2058 if (! assoc)
2059 continue;
2060 if (! assoc_line_out)
2061 assoc_line_out = assoc;
2062 else if (assoc_line_out != assoc)
2063 continue;
2064 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2065 continue;
2066 cfg->line_out_pins[cfg->line_outs] = nid;
2067 sequences[cfg->line_outs] = seq;
2068 cfg->line_outs++;
2069 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002070 case AC_JACK_SPEAKER:
Takashi Iwai82bc9552006-03-21 11:24:42 +01002071 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2072 continue;
2073 cfg->speaker_pins[cfg->speaker_outs] = nid;
2074 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002075 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002076 case AC_JACK_HP_OUT:
2077 cfg->hp_pin = nid;
2078 break;
2079 case AC_JACK_MIC_IN:
2080 if (loc == AC_JACK_LOC_FRONT)
2081 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
2082 else
2083 cfg->input_pins[AUTO_PIN_MIC] = nid;
2084 break;
2085 case AC_JACK_LINE_IN:
2086 if (loc == AC_JACK_LOC_FRONT)
2087 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2088 else
2089 cfg->input_pins[AUTO_PIN_LINE] = nid;
2090 break;
2091 case AC_JACK_CD:
2092 cfg->input_pins[AUTO_PIN_CD] = nid;
2093 break;
2094 case AC_JACK_AUX:
2095 cfg->input_pins[AUTO_PIN_AUX] = nid;
2096 break;
2097 case AC_JACK_SPDIF_OUT:
2098 cfg->dig_out_pin = nid;
2099 break;
2100 case AC_JACK_SPDIF_IN:
2101 cfg->dig_in_pin = nid;
2102 break;
2103 }
2104 }
2105
2106 /* sort by sequence */
2107 for (i = 0; i < cfg->line_outs; i++)
2108 for (j = i + 1; j < cfg->line_outs; j++)
2109 if (sequences[i] > sequences[j]) {
2110 seq = sequences[i];
2111 sequences[i] = sequences[j];
2112 sequences[j] = seq;
2113 nid = cfg->line_out_pins[i];
2114 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2115 cfg->line_out_pins[j] = nid;
2116 }
2117
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002118 /* Reorder the surround channels
2119 * ALSA sequence is front/surr/clfe/side
2120 * HDA sequence is:
2121 * 4-ch: front/surr => OK as it is
2122 * 6-ch: front/clfe/surr
2123 * 8-ch: front/clfe/side/surr
2124 */
2125 switch (cfg->line_outs) {
2126 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002127 nid = cfg->line_out_pins[1];
2128 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2129 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002130 break;
2131 case 4:
2132 nid = cfg->line_out_pins[1];
2133 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2134 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2135 cfg->line_out_pins[2] = nid;
2136 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002137 }
2138
Takashi Iwai82bc9552006-03-21 11:24:42 +01002139 /*
2140 * debug prints of the parsed results
2141 */
2142 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2143 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2144 cfg->line_out_pins[2], cfg->line_out_pins[3],
2145 cfg->line_out_pins[4]);
2146 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2147 cfg->speaker_outs, cfg->speaker_pins[0],
2148 cfg->speaker_pins[1], cfg->speaker_pins[2],
2149 cfg->speaker_pins[3], cfg->speaker_pins[4]);
2150 snd_printd(" hp=0x%x, dig_out=0x%x, din_in=0x%x\n",
2151 cfg->hp_pin, cfg->dig_out_pin, cfg->dig_in_pin);
2152 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2153 " cd=0x%x, aux=0x%x\n",
2154 cfg->input_pins[AUTO_PIN_MIC],
2155 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2156 cfg->input_pins[AUTO_PIN_LINE],
2157 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2158 cfg->input_pins[AUTO_PIN_CD],
2159 cfg->input_pins[AUTO_PIN_AUX]);
2160
2161 /*
2162 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2163 * as a primary output
2164 */
2165 if (! cfg->line_outs) {
2166 if (cfg->speaker_outs) {
2167 cfg->line_outs = cfg->speaker_outs;
2168 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2169 sizeof(cfg->speaker_pins));
2170 cfg->speaker_outs = 0;
2171 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2172 } else if (cfg->hp_pin) {
2173 cfg->line_outs = 1;
2174 cfg->line_out_pins[0] = cfg->hp_pin;
2175 cfg->hp_pin = 0;
2176 }
2177 }
2178
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002179 return 0;
2180}
2181
Takashi Iwai4a471b72005-12-07 13:56:29 +01002182/* labels for input pins */
2183const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2184 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2185};
2186
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifdef CONFIG_PM
2189/*
2190 * power management
2191 */
2192
2193/**
2194 * snd_hda_suspend - suspend the codecs
2195 * @bus: the HDA bus
2196 * @state: suspsend state
2197 *
2198 * Returns 0 if successful.
2199 */
2200int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2201{
2202 struct list_head *p;
2203
2204 /* FIXME: should handle power widget capabilities */
2205 list_for_each(p, &bus->codec_list) {
2206 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2207 if (codec->patch_ops.suspend)
2208 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002209 hda_set_power_state(codec,
2210 codec->afg ? codec->afg : codec->mfg,
2211 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213 return 0;
2214}
2215
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002216EXPORT_SYMBOL(snd_hda_suspend);
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/**
2219 * snd_hda_resume - resume the codecs
2220 * @bus: the HDA bus
2221 * @state: resume state
2222 *
2223 * Returns 0 if successful.
2224 */
2225int snd_hda_resume(struct hda_bus *bus)
2226{
2227 struct list_head *p;
2228
2229 list_for_each(p, &bus->codec_list) {
2230 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002231 hda_set_power_state(codec,
2232 codec->afg ? codec->afg : codec->mfg,
2233 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (codec->patch_ops.resume)
2235 codec->patch_ops.resume(codec);
2236 }
2237 return 0;
2238}
2239
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002240EXPORT_SYMBOL(snd_hda_resume);
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/**
2243 * snd_hda_resume_ctls - resume controls in the new control list
2244 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002245 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002247 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * originally for snd_hda_add_new_ctls().
2249 * The array must be terminated with an empty entry as terminator.
2250 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002251int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002253 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 val = kmalloc(sizeof(*val), GFP_KERNEL);
2256 if (! val)
2257 return -ENOMEM;
2258 codec->in_resume = 1;
2259 for (; knew->name; knew++) {
2260 int i, count;
2261 count = knew->count ? knew->count : 1;
2262 for (i = 0; i < count; i++) {
2263 memset(val, 0, sizeof(*val));
2264 val->id.iface = knew->iface;
2265 val->id.device = knew->device;
2266 val->id.subdevice = knew->subdevice;
2267 strcpy(val->id.name, knew->name);
2268 val->id.index = knew->index ? knew->index : i;
2269 /* Assume that get callback reads only from cache,
2270 * not accessing to the real hardware
2271 */
2272 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2273 continue;
2274 snd_ctl_elem_write(codec->bus->card, NULL, val);
2275 }
2276 }
2277 codec->in_resume = 0;
2278 kfree(val);
2279 return 0;
2280}
2281
2282/**
2283 * snd_hda_resume_spdif_out - resume the digital out
2284 * @codec: the HDA codec
2285 */
2286int snd_hda_resume_spdif_out(struct hda_codec *codec)
2287{
2288 return snd_hda_resume_ctls(codec, dig_mixes);
2289}
2290
2291/**
2292 * snd_hda_resume_spdif_in - resume the digital in
2293 * @codec: the HDA codec
2294 */
2295int snd_hda_resume_spdif_in(struct hda_codec *codec)
2296{
2297 return snd_hda_resume_ctls(codec, dig_in_ctls);
2298}
2299#endif
2300
2301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 * INIT part
2303 */
2304
2305static int __init alsa_hda_init(void)
2306{
2307 return 0;
2308}
2309
2310static void __exit alsa_hda_exit(void)
2311{
2312}
2313
2314module_init(alsa_hda_init)
2315module_exit(alsa_hda_exit)