blob: 18bbc87e376f167c5beb9c823ab2ffb3cb0d4f0a [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 */
David Howellsc4028952006-11-22 14:57:56 +0000275static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
David Howellsc4028952006-11-22 14:57:56 +0000277 struct hda_bus_unsolicited *unsol =
278 container_of(work, struct hda_bus_unsolicited, work);
279 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct hda_codec *codec;
281 unsigned int rp, caddr, res;
282
283 while (unsol->rp != unsol->wp) {
284 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
285 unsol->rp = rp;
286 rp <<= 1;
287 res = unsol->queue[rp];
288 caddr = unsol->queue[rp + 1];
289 if (! (caddr & (1 << 4))) /* no unsolicited event? */
290 continue;
291 codec = bus->caddr_tbl[caddr & 0x0f];
292 if (codec && codec->patch_ops.unsol_event)
293 codec->patch_ops.unsol_event(codec, res);
294 }
295}
296
297/*
298 * initialize unsolicited queue
299 */
300static int init_unsol_queue(struct hda_bus *bus)
301{
302 struct hda_bus_unsolicited *unsol;
303
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100304 if (bus->unsol) /* already initialized */
305 return 0;
306
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200307 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (! unsol) {
309 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
310 return -ENOMEM;
311 }
OGAWA Hirofumice7415f2006-03-31 12:36:14 +0200312 unsol->workq = create_singlethread_workqueue("hda_codec");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (! unsol->workq) {
314 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
315 kfree(unsol);
316 return -ENOMEM;
317 }
David Howellsc4028952006-11-22 14:57:56 +0000318 INIT_WORK(&unsol->work, process_unsol_events);
319 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 bus->unsol = unsol;
321 return 0;
322}
323
324/*
325 * destructor
326 */
327static void snd_hda_codec_free(struct hda_codec *codec);
328
329static int snd_hda_bus_free(struct hda_bus *bus)
330{
331 struct list_head *p, *n;
332
333 if (! bus)
334 return 0;
335 if (bus->unsol) {
336 destroy_workqueue(bus->unsol->workq);
337 kfree(bus->unsol);
338 }
339 list_for_each_safe(p, n, &bus->codec_list) {
340 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
341 snd_hda_codec_free(codec);
342 }
343 if (bus->ops.private_free)
344 bus->ops.private_free(bus);
345 kfree(bus);
346 return 0;
347}
348
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100349static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct hda_bus *bus = device->device_data;
352 return snd_hda_bus_free(bus);
353}
354
355/**
356 * snd_hda_bus_new - create a HDA bus
357 * @card: the card entry
358 * @temp: the template for hda_bus information
359 * @busp: the pointer to store the created bus instance
360 *
361 * Returns 0 if successful, or a negative error code.
362 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100363int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct hda_bus **busp)
365{
366 struct hda_bus *bus;
367 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100368 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 .dev_free = snd_hda_bus_dev_free,
370 };
371
372 snd_assert(temp, return -EINVAL);
373 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
374
375 if (busp)
376 *busp = NULL;
377
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200378 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (bus == NULL) {
380 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
381 return -ENOMEM;
382 }
383
384 bus->card = card;
385 bus->private_data = temp->private_data;
386 bus->pci = temp->pci;
387 bus->modelname = temp->modelname;
388 bus->ops = temp->ops;
389
Ingo Molnar62932df2006-01-16 16:34:20 +0100390 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 INIT_LIST_HEAD(&bus->codec_list);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
394 snd_hda_bus_free(bus);
395 return err;
396 }
397 if (busp)
398 *busp = bus;
399 return 0;
400}
401
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200402EXPORT_SYMBOL(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/*
405 * find a matching codec preset
406 */
407static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
408{
409 const struct hda_codec_preset **tbl, *preset;
410
411 for (tbl = hda_preset_tables; *tbl; tbl++) {
412 for (preset = *tbl; preset->id; preset++) {
413 u32 mask = preset->mask;
414 if (! mask)
415 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200416 if (preset->id == (codec->vendor_id & mask) &&
417 (! preset->rev ||
418 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return preset;
420 }
421 }
422 return NULL;
423}
424
425/*
426 * snd_hda_get_codec_name - store the codec name
427 */
428void snd_hda_get_codec_name(struct hda_codec *codec,
429 char *name, int namelen)
430{
431 const struct hda_vendor_id *c;
432 const char *vendor = NULL;
433 u16 vendor_id = codec->vendor_id >> 16;
434 char tmp[16];
435
436 for (c = hda_vendor_ids; c->id; c++) {
437 if (c->id == vendor_id) {
438 vendor = c->name;
439 break;
440 }
441 }
442 if (! vendor) {
443 sprintf(tmp, "Generic %04x", vendor_id);
444 vendor = tmp;
445 }
446 if (codec->preset && codec->preset->name)
447 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
448 else
449 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
450}
451
452/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200453 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200455static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 int i, total_nodes;
458 hda_nid_t nid;
459
460 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
461 for (i = 0; i < total_nodes; i++, nid++) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200462 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
463 case AC_GRP_AUDIO_FUNCTION:
464 codec->afg = nid;
465 break;
466 case AC_GRP_MODEM_FUNCTION:
467 codec->mfg = nid;
468 break;
469 default:
470 break;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 * read widget caps for each widget and store in cache
477 */
478static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
479{
480 int i;
481 hda_nid_t nid;
482
483 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
484 &codec->start_nid);
485 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
486 if (! codec->wcaps)
487 return -ENOMEM;
488 nid = codec->start_nid;
489 for (i = 0; i < codec->num_nodes; i++, nid++)
490 codec->wcaps[i] = snd_hda_param_read(codec, nid,
491 AC_PAR_AUDIO_WIDGET_CAP);
492 return 0;
493}
494
495
496/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * codec destructor
498 */
499static void snd_hda_codec_free(struct hda_codec *codec)
500{
501 if (! codec)
502 return;
503 list_del(&codec->list);
504 codec->bus->caddr_tbl[codec->addr] = NULL;
505 if (codec->patch_ops.free)
506 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100507 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 kfree(codec);
510}
511
512static void init_amp_hash(struct hda_codec *codec);
513
514/**
515 * snd_hda_codec_new - create a HDA codec
516 * @bus: the bus to assign
517 * @codec_addr: the codec address
518 * @codecp: the pointer to store the generated codec
519 *
520 * Returns 0 if successful, or a negative error code.
521 */
522int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
523 struct hda_codec **codecp)
524{
525 struct hda_codec *codec;
526 char component[13];
527 int err;
528
529 snd_assert(bus, return -EINVAL);
530 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
531
532 if (bus->caddr_tbl[codec_addr]) {
533 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
534 return -EBUSY;
535 }
536
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200537 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (codec == NULL) {
539 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
540 return -ENOMEM;
541 }
542
543 codec->bus = bus;
544 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100545 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 init_amp_hash(codec);
547
548 list_add_tail(&codec->list, &bus->codec_list);
549 bus->caddr_tbl[codec_addr] = codec;
550
551 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100552 if (codec->vendor_id == -1)
553 /* read again, hopefully the access method was corrected
554 * in the last read...
555 */
556 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
557 AC_PAR_VENDOR_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
559 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
560
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200561 setup_fg_nodes(codec);
562 if (! codec->afg && ! codec->mfg) {
563 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 snd_hda_codec_free(codec);
565 return -ENODEV;
566 }
567
Takashi Iwai54d17402005-11-21 16:33:22 +0100568 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
569 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
570 snd_hda_codec_free(codec);
571 return -ENOMEM;
572 }
573
Takashi Iwai86284e42005-10-11 15:05:54 +0200574 if (! codec->subsystem_id) {
575 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
576 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
577 AC_VERB_GET_SUBSYSTEM_ID,
578 0);
579 }
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 codec->preset = find_codec_preset(codec);
582 if (! *bus->card->mixername)
583 snd_hda_get_codec_name(codec, bus->card->mixername,
584 sizeof(bus->card->mixername));
585
586 if (codec->preset && codec->preset->patch)
587 err = codec->preset->patch(codec);
588 else
589 err = snd_hda_parse_generic_codec(codec);
590 if (err < 0) {
591 snd_hda_codec_free(codec);
592 return err;
593 }
594
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100595 if (codec->patch_ops.unsol_event)
596 init_unsol_queue(bus);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 snd_hda_codec_proc_new(codec);
599
600 sprintf(component, "HDA:%08x", codec->vendor_id);
601 snd_component_add(codec->bus->card, component);
602
603 if (codecp)
604 *codecp = codec;
605 return 0;
606}
607
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200608EXPORT_SYMBOL(snd_hda_codec_new);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/**
611 * snd_hda_codec_setup_stream - set up the codec for streaming
612 * @codec: the CODEC to set up
613 * @nid: the NID to set up
614 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
615 * @channel_id: channel id to pass, zero based.
616 * @format: stream format.
617 */
618void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
619 int channel_id, int format)
620{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200621 if (! nid)
622 return;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
625 nid, stream_tag, channel_id, format);
626 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
627 (stream_tag << 4) | channel_id);
628 msleep(1);
629 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
630}
631
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +0200632EXPORT_SYMBOL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/*
635 * amp access functions
636 */
637
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200638/* FIXME: more better hash key? */
639#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200641#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643/* initialize the hash table */
644static void init_amp_hash(struct hda_codec *codec)
645{
646 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
647 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100648 codec->amp_info_size = 0;
649 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/* query the hash. allocate an entry if not found. */
653static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
654{
655 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
656 u16 cur = codec->amp_hash[idx];
657 struct hda_amp_info *info;
658
659 while (cur != 0xffff) {
660 info = &codec->amp_info[cur];
661 if (info->key == key)
662 return info;
663 cur = info->next;
664 }
665
666 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100667 if (codec->num_amp_entries >= codec->amp_info_size) {
668 /* reallocate the array */
669 int new_size = codec->amp_info_size + 64;
670 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
671 GFP_KERNEL);
672 if (! new_info) {
673 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
674 return NULL;
675 }
676 if (codec->amp_info) {
677 memcpy(new_info, codec->amp_info,
678 codec->amp_info_size * sizeof(struct hda_amp_info));
679 kfree(codec->amp_info);
680 }
681 codec->amp_info_size = new_size;
682 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 cur = codec->num_amp_entries++;
685 info = &codec->amp_info[cur];
686 info->key = key;
687 info->status = 0; /* not initialized yet */
688 info->next = codec->amp_hash[idx];
689 codec->amp_hash[idx] = cur;
690
691 return info;
692}
693
694/*
695 * query AMP capabilities for the given widget and direction
696 */
697static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
698{
699 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
700
701 if (! info)
702 return 0;
703 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100704 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 nid = codec->afg;
706 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
707 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
708 info->status |= INFO_AMP_CAPS;
709 }
710 return info->amp_caps;
711}
712
713/*
714 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200715 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200717static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 hda_nid_t nid, int ch, int direction, int index)
719{
720 u32 val, parm;
721
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200722 if (info->status & INFO_AMP_VOL(ch))
723 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
726 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
727 parm |= index;
728 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
729 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200730 info->status |= INFO_AMP_VOL(ch);
731 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200735 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200737static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hda_nid_t nid, int ch, int direction, int index, int val)
739{
740 u32 parm;
741
742 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
743 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
744 parm |= index << AC_AMP_SET_INDEX_SHIFT;
745 parm |= val;
746 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200747 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200751 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Takashi Iwai834be882006-03-01 14:16:17 +0100753int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
754 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
757 if (! info)
758 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200759 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200762/*
763 * update the AMP value, mask = bit mask to set, val = the value
764 */
Takashi Iwai834be882006-03-01 14:16:17 +0100765int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
766 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (! info)
771 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200772 val &= mask;
773 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (info->vol[ch] == val && ! codec->in_resume)
775 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200776 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 1;
778}
779
780
781/*
782 * AMP control callbacks
783 */
784/* retrieve parameters from private_value */
785#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
786#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
787#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
788#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
789
790/* volume */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100791int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
794 u16 nid = get_amp_nid(kcontrol);
795 u8 chs = get_amp_channels(kcontrol);
796 int dir = get_amp_direction(kcontrol);
797 u32 caps;
798
799 caps = query_amp_caps(codec, nid, dir);
800 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
801 if (! caps) {
802 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
803 return -EINVAL;
804 }
805 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
806 uinfo->count = chs == 3 ? 2 : 1;
807 uinfo->value.integer.min = 0;
808 uinfo->value.integer.max = caps;
809 return 0;
810}
811
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100812int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
815 hda_nid_t nid = get_amp_nid(kcontrol);
816 int chs = get_amp_channels(kcontrol);
817 int dir = get_amp_direction(kcontrol);
818 int idx = get_amp_index(kcontrol);
819 long *valp = ucontrol->value.integer.value;
820
821 if (chs & 1)
822 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
823 if (chs & 2)
824 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
825 return 0;
826}
827
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100828int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
831 hda_nid_t nid = get_amp_nid(kcontrol);
832 int chs = get_amp_channels(kcontrol);
833 int dir = get_amp_direction(kcontrol);
834 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 long *valp = ucontrol->value.integer.value;
836 int change = 0;
837
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200838 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200839 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
840 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200841 valp++;
842 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200843 if (chs & 2)
844 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200845 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return change;
847}
848
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200849int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
850 unsigned int size, unsigned int __user *_tlv)
851{
852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
853 hda_nid_t nid = get_amp_nid(kcontrol);
854 int dir = get_amp_direction(kcontrol);
855 u32 caps, val1, val2;
856
857 if (size < 4 * sizeof(unsigned int))
858 return -ENOMEM;
859 caps = query_amp_caps(codec, nid, dir);
860 val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25;
861 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
862 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200863 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
864 return -EFAULT;
865 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
866 return -EFAULT;
867 if (put_user(val1, _tlv + 2))
868 return -EFAULT;
869 if (put_user(val2, _tlv + 3))
870 return -EFAULT;
871 return 0;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/* switch */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100875int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 int chs = get_amp_channels(kcontrol);
878
879 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
880 uinfo->count = chs == 3 ? 2 : 1;
881 uinfo->value.integer.min = 0;
882 uinfo->value.integer.max = 1;
883 return 0;
884}
885
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100886int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
889 hda_nid_t nid = get_amp_nid(kcontrol);
890 int chs = get_amp_channels(kcontrol);
891 int dir = get_amp_direction(kcontrol);
892 int idx = get_amp_index(kcontrol);
893 long *valp = ucontrol->value.integer.value;
894
895 if (chs & 1)
896 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
897 if (chs & 2)
898 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
899 return 0;
900}
901
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100902int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
905 hda_nid_t nid = get_amp_nid(kcontrol);
906 int chs = get_amp_channels(kcontrol);
907 int dir = get_amp_direction(kcontrol);
908 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 long *valp = ucontrol->value.integer.value;
910 int change = 0;
911
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200912 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200913 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
914 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200915 valp++;
916 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200917 if (chs & 2)
918 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200919 0x80, *valp ? 0 : 0x80);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return change;
922}
923
924/*
Takashi Iwai985be542005-11-02 18:26:49 +0100925 * bound volume controls
926 *
927 * bind multiple volumes (# indices, from 0)
928 */
929
930#define AMP_VAL_IDX_SHIFT 19
931#define AMP_VAL_IDX_MASK (0x0f<<19)
932
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100933int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100934{
935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
936 unsigned long pval;
937 int err;
938
Ingo Molnar62932df2006-01-16 16:34:20 +0100939 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100940 pval = kcontrol->private_value;
941 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
942 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
943 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100944 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100945 return err;
946}
947
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100948int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100949{
950 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
951 unsigned long pval;
952 int i, indices, err = 0, change = 0;
953
Ingo Molnar62932df2006-01-16 16:34:20 +0100954 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100955 pval = kcontrol->private_value;
956 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
957 for (i = 0; i < indices; i++) {
958 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
959 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
960 if (err < 0)
961 break;
962 change |= err;
963 }
964 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100965 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100966 return err < 0 ? err : change;
967}
968
969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * SPDIF out controls
971 */
972
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100973static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
976 uinfo->count = 1;
977 return 0;
978}
979
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100980static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
983 IEC958_AES0_NONAUDIO |
984 IEC958_AES0_CON_EMPHASIS_5015 |
985 IEC958_AES0_CON_NOT_COPYRIGHT;
986 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
987 IEC958_AES1_CON_ORIGINAL;
988 return 0;
989}
990
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100991static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
994 IEC958_AES0_NONAUDIO |
995 IEC958_AES0_PRO_EMPHASIS_5015;
996 return 0;
997}
998
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100999static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1002
1003 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1004 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1005 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1006 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1007
1008 return 0;
1009}
1010
1011/* convert from SPDIF status bits to HDA SPDIF bits
1012 * bit 0 (DigEn) is always set zero (to be filled later)
1013 */
1014static unsigned short convert_from_spdif_status(unsigned int sbits)
1015{
1016 unsigned short val = 0;
1017
1018 if (sbits & IEC958_AES0_PROFESSIONAL)
1019 val |= 1 << 6;
1020 if (sbits & IEC958_AES0_NONAUDIO)
1021 val |= 1 << 5;
1022 if (sbits & IEC958_AES0_PROFESSIONAL) {
1023 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1024 val |= 1 << 3;
1025 } else {
1026 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
1027 val |= 1 << 3;
1028 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1029 val |= 1 << 4;
1030 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1031 val |= 1 << 7;
1032 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1033 }
1034 return val;
1035}
1036
1037/* convert to SPDIF status bits from HDA SPDIF bits
1038 */
1039static unsigned int convert_to_spdif_status(unsigned short val)
1040{
1041 unsigned int sbits = 0;
1042
1043 if (val & (1 << 5))
1044 sbits |= IEC958_AES0_NONAUDIO;
1045 if (val & (1 << 6))
1046 sbits |= IEC958_AES0_PROFESSIONAL;
1047 if (sbits & IEC958_AES0_PROFESSIONAL) {
1048 if (sbits & (1 << 3))
1049 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1050 } else {
1051 if (val & (1 << 3))
1052 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1053 if (! (val & (1 << 4)))
1054 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1055 if (val & (1 << 7))
1056 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1057 sbits |= val & (0x7f << 8);
1058 }
1059 return sbits;
1060}
1061
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001062static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1065 hda_nid_t nid = kcontrol->private_value;
1066 unsigned short val;
1067 int change;
1068
Ingo Molnar62932df2006-01-16 16:34:20 +01001069 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 codec->spdif_status = ucontrol->value.iec958.status[0] |
1071 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1072 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1073 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1074 val = convert_from_spdif_status(codec->spdif_status);
1075 val |= codec->spdif_ctls & 1;
1076 change = codec->spdif_ctls != val;
1077 codec->spdif_ctls = val;
1078
1079 if (change || codec->in_resume) {
1080 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1081 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1082 }
1083
Ingo Molnar62932df2006-01-16 16:34:20 +01001084 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return change;
1086}
1087
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001088static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1091 uinfo->count = 1;
1092 uinfo->value.integer.min = 0;
1093 uinfo->value.integer.max = 1;
1094 return 0;
1095}
1096
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001097static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1100
1101 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1102 return 0;
1103}
1104
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001105static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1108 hda_nid_t nid = kcontrol->private_value;
1109 unsigned short val;
1110 int change;
1111
Ingo Molnar62932df2006-01-16 16:34:20 +01001112 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 val = codec->spdif_ctls & ~1;
1114 if (ucontrol->value.integer.value[0])
1115 val |= 1;
1116 change = codec->spdif_ctls != val;
1117 if (change || codec->in_resume) {
1118 codec->spdif_ctls = val;
1119 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1120 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1121 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1122 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1123 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001124 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return change;
1126}
1127
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001128static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 {
1130 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1131 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1132 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1133 .info = snd_hda_spdif_mask_info,
1134 .get = snd_hda_spdif_cmask_get,
1135 },
1136 {
1137 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1138 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1139 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1140 .info = snd_hda_spdif_mask_info,
1141 .get = snd_hda_spdif_pmask_get,
1142 },
1143 {
1144 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1145 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1146 .info = snd_hda_spdif_mask_info,
1147 .get = snd_hda_spdif_default_get,
1148 .put = snd_hda_spdif_default_put,
1149 },
1150 {
1151 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1152 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1153 .info = snd_hda_spdif_out_switch_info,
1154 .get = snd_hda_spdif_out_switch_get,
1155 .put = snd_hda_spdif_out_switch_put,
1156 },
1157 { } /* end */
1158};
1159
1160/**
1161 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1162 * @codec: the HDA codec
1163 * @nid: audio out widget NID
1164 *
1165 * Creates controls related with the SPDIF output.
1166 * Called from each patch supporting the SPDIF out.
1167 *
1168 * Returns 0 if successful, or a negative error code.
1169 */
1170int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1171{
1172 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001173 struct snd_kcontrol *kctl;
1174 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1177 kctl = snd_ctl_new1(dig_mix, codec);
1178 kctl->private_value = nid;
1179 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1180 return err;
1181 }
1182 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1183 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1184 return 0;
1185}
1186
1187/*
1188 * SPDIF input
1189 */
1190
1191#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1192
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001193static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1196
1197 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1198 return 0;
1199}
1200
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001201static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1204 hda_nid_t nid = kcontrol->private_value;
1205 unsigned int val = !!ucontrol->value.integer.value[0];
1206 int change;
1207
Ingo Molnar62932df2006-01-16 16:34:20 +01001208 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 change = codec->spdif_in_enable != val;
1210 if (change || codec->in_resume) {
1211 codec->spdif_in_enable = val;
1212 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1213 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001214 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return change;
1216}
1217
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001218static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1221 hda_nid_t nid = kcontrol->private_value;
1222 unsigned short val;
1223 unsigned int sbits;
1224
1225 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1226 sbits = convert_to_spdif_status(val);
1227 ucontrol->value.iec958.status[0] = sbits;
1228 ucontrol->value.iec958.status[1] = sbits >> 8;
1229 ucontrol->value.iec958.status[2] = sbits >> 16;
1230 ucontrol->value.iec958.status[3] = sbits >> 24;
1231 return 0;
1232}
1233
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001234static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 {
1236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1237 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1238 .info = snd_hda_spdif_in_switch_info,
1239 .get = snd_hda_spdif_in_switch_get,
1240 .put = snd_hda_spdif_in_switch_put,
1241 },
1242 {
1243 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1245 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1246 .info = snd_hda_spdif_mask_info,
1247 .get = snd_hda_spdif_in_status_get,
1248 },
1249 { } /* end */
1250};
1251
1252/**
1253 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1254 * @codec: the HDA codec
1255 * @nid: audio in widget NID
1256 *
1257 * Creates controls related with the SPDIF input.
1258 * Called from each patch supporting the SPDIF in.
1259 *
1260 * Returns 0 if successful, or a negative error code.
1261 */
1262int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1263{
1264 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001265 struct snd_kcontrol *kctl;
1266 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1269 kctl = snd_ctl_new1(dig_mix, codec);
1270 kctl->private_value = nid;
1271 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1272 return err;
1273 }
1274 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1275 return 0;
1276}
1277
1278
Takashi Iwai54d17402005-11-21 16:33:22 +01001279/*
1280 * set power state of the codec
1281 */
1282static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1283 unsigned int power_state)
1284{
1285 hda_nid_t nid, nid_start;
1286 int nodes;
1287
1288 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1289 power_state);
1290
1291 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1292 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1293 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1294 snd_hda_codec_write(codec, nid, 0,
1295 AC_VERB_SET_POWER_STATE,
1296 power_state);
1297 }
1298
1299 if (power_state == AC_PWRST_D0)
1300 msleep(10);
1301}
1302
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304/**
1305 * snd_hda_build_controls - build mixer controls
1306 * @bus: the BUS
1307 *
1308 * Creates mixer controls for each codec included in the bus.
1309 *
1310 * Returns 0 if successful, otherwise a negative error code.
1311 */
1312int snd_hda_build_controls(struct hda_bus *bus)
1313{
1314 struct list_head *p;
1315
1316 /* build controls */
1317 list_for_each(p, &bus->codec_list) {
1318 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1319 int err;
1320 if (! codec->patch_ops.build_controls)
1321 continue;
1322 err = codec->patch_ops.build_controls(codec);
1323 if (err < 0)
1324 return err;
1325 }
1326
1327 /* initialize */
1328 list_for_each(p, &bus->codec_list) {
1329 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1330 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001331 hda_set_power_state(codec,
1332 codec->afg ? codec->afg : codec->mfg,
1333 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (! codec->patch_ops.init)
1335 continue;
1336 err = codec->patch_ops.init(codec);
1337 if (err < 0)
1338 return err;
1339 }
1340 return 0;
1341}
1342
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001343EXPORT_SYMBOL(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345/*
1346 * stream formats
1347 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001348struct hda_rate_tbl {
1349 unsigned int hz;
1350 unsigned int alsa_bits;
1351 unsigned int hda_fmt;
1352};
1353
1354static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001356
1357 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1359 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1360 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1361 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1362 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1363 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1364 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1365 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1366 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1367 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1368 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001369
Takashi Iwaibefdf312005-08-22 13:57:55 +02001370 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371};
1372
1373/**
1374 * snd_hda_calc_stream_format - calculate format bitset
1375 * @rate: the sample rate
1376 * @channels: the number of channels
1377 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1378 * @maxbps: the max. bps
1379 *
1380 * Calculate the format bitset from the given rate, channels and th PCM format.
1381 *
1382 * Return zero if invalid.
1383 */
1384unsigned int snd_hda_calc_stream_format(unsigned int rate,
1385 unsigned int channels,
1386 unsigned int format,
1387 unsigned int maxbps)
1388{
1389 int i;
1390 unsigned int val = 0;
1391
Takashi Iwaibefdf312005-08-22 13:57:55 +02001392 for (i = 0; rate_bits[i].hz; i++)
1393 if (rate_bits[i].hz == rate) {
1394 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001397 if (! rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 snd_printdd("invalid rate %d\n", rate);
1399 return 0;
1400 }
1401
1402 if (channels == 0 || channels > 8) {
1403 snd_printdd("invalid channels %d\n", channels);
1404 return 0;
1405 }
1406 val |= channels - 1;
1407
1408 switch (snd_pcm_format_width(format)) {
1409 case 8: val |= 0x00; break;
1410 case 16: val |= 0x10; break;
1411 case 20:
1412 case 24:
1413 case 32:
1414 if (maxbps >= 32)
1415 val |= 0x40;
1416 else if (maxbps >= 24)
1417 val |= 0x30;
1418 else
1419 val |= 0x20;
1420 break;
1421 default:
1422 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1423 return 0;
1424 }
1425
1426 return val;
1427}
1428
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001429EXPORT_SYMBOL(snd_hda_calc_stream_format);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/**
1432 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1433 * @codec: the HDA codec
1434 * @nid: NID to query
1435 * @ratesp: the pointer to store the detected rate bitflags
1436 * @formatsp: the pointer to store the detected formats
1437 * @bpsp: the pointer to store the detected format widths
1438 *
1439 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1440 * or @bsps argument is ignored.
1441 *
1442 * Returns 0 if successful, otherwise a negative error code.
1443 */
1444int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1445 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1446{
1447 int i;
1448 unsigned int val, streams;
1449
1450 val = 0;
1451 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001452 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1454 if (val == -1)
1455 return -EIO;
1456 }
1457 if (! val)
1458 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1459
1460 if (ratesp) {
1461 u32 rates = 0;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001462 for (i = 0; rate_bits[i].hz; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001464 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 *ratesp = rates;
1467 }
1468
1469 if (formatsp || bpsp) {
1470 u64 formats = 0;
1471 unsigned int bps;
1472 unsigned int wcaps;
1473
Takashi Iwai54d17402005-11-21 16:33:22 +01001474 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1476 if (streams == -1)
1477 return -EIO;
1478 if (! streams) {
1479 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1480 if (streams == -1)
1481 return -EIO;
1482 }
1483
1484 bps = 0;
1485 if (streams & AC_SUPFMT_PCM) {
1486 if (val & AC_SUPPCM_BITS_8) {
1487 formats |= SNDRV_PCM_FMTBIT_U8;
1488 bps = 8;
1489 }
1490 if (val & AC_SUPPCM_BITS_16) {
1491 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1492 bps = 16;
1493 }
1494 if (wcaps & AC_WCAP_DIGITAL) {
1495 if (val & AC_SUPPCM_BITS_32)
1496 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1497 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1498 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1499 if (val & AC_SUPPCM_BITS_24)
1500 bps = 24;
1501 else if (val & AC_SUPPCM_BITS_20)
1502 bps = 20;
1503 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1504 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1505 if (val & AC_SUPPCM_BITS_32)
1506 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 else if (val & AC_SUPPCM_BITS_24)
1508 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02001509 else if (val & AC_SUPPCM_BITS_20)
1510 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 }
1513 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1514 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1515 bps = 32;
1516 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1517 /* temporary hack: we have still no proper support
1518 * for the direct AC3 stream...
1519 */
1520 formats |= SNDRV_PCM_FMTBIT_U8;
1521 bps = 8;
1522 }
1523 if (formatsp)
1524 *formatsp = formats;
1525 if (bpsp)
1526 *bpsp = bps;
1527 }
1528
1529 return 0;
1530}
1531
1532/**
1533 * snd_hda_is_supported_format - check whether the given node supports the format val
1534 *
1535 * Returns 1 if supported, 0 if not.
1536 */
1537int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1538 unsigned int format)
1539{
1540 int i;
1541 unsigned int val = 0, rate, stream;
1542
1543 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001544 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1546 if (val == -1)
1547 return 0;
1548 }
1549 if (! val) {
1550 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1551 if (val == -1)
1552 return 0;
1553 }
1554
1555 rate = format & 0xff00;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001556 for (i = 0; rate_bits[i].hz; i++)
1557 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (val & (1 << i))
1559 break;
1560 return 0;
1561 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001562 if (! rate_bits[i].hz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564
1565 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1566 if (stream == -1)
1567 return 0;
1568 if (! stream && nid != codec->afg)
1569 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1570 if (! stream || stream == -1)
1571 return 0;
1572
1573 if (stream & AC_SUPFMT_PCM) {
1574 switch (format & 0xf0) {
1575 case 0x00:
1576 if (! (val & AC_SUPPCM_BITS_8))
1577 return 0;
1578 break;
1579 case 0x10:
1580 if (! (val & AC_SUPPCM_BITS_16))
1581 return 0;
1582 break;
1583 case 0x20:
1584 if (! (val & AC_SUPPCM_BITS_20))
1585 return 0;
1586 break;
1587 case 0x30:
1588 if (! (val & AC_SUPPCM_BITS_24))
1589 return 0;
1590 break;
1591 case 0x40:
1592 if (! (val & AC_SUPPCM_BITS_32))
1593 return 0;
1594 break;
1595 default:
1596 return 0;
1597 }
1598 } else {
1599 /* FIXME: check for float32 and AC3? */
1600 }
1601
1602 return 1;
1603}
1604
1605/*
1606 * PCM stuff
1607 */
1608static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1609 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001610 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 return 0;
1613}
1614
1615static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1616 struct hda_codec *codec,
1617 unsigned int stream_tag,
1618 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001619 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1622 return 0;
1623}
1624
1625static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1626 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001627 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1630 return 0;
1631}
1632
1633static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1634{
1635 if (info->nid) {
1636 /* query support PCM information from the given NID */
1637 if (! info->rates || ! info->formats)
1638 snd_hda_query_supported_pcm(codec, info->nid,
1639 info->rates ? NULL : &info->rates,
1640 info->formats ? NULL : &info->formats,
1641 info->maxbps ? NULL : &info->maxbps);
1642 }
1643 if (info->ops.open == NULL)
1644 info->ops.open = hda_pcm_default_open_close;
1645 if (info->ops.close == NULL)
1646 info->ops.close = hda_pcm_default_open_close;
1647 if (info->ops.prepare == NULL) {
1648 snd_assert(info->nid, return -EINVAL);
1649 info->ops.prepare = hda_pcm_default_prepare;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (info->ops.cleanup == NULL) {
1652 snd_assert(info->nid, return -EINVAL);
1653 info->ops.cleanup = hda_pcm_default_cleanup;
1654 }
1655 return 0;
1656}
1657
1658/**
1659 * snd_hda_build_pcms - build PCM information
1660 * @bus: the BUS
1661 *
1662 * Create PCM information for each codec included in the bus.
1663 *
1664 * The build_pcms codec patch is requested to set up codec->num_pcms and
1665 * codec->pcm_info properly. The array is referred by the top-level driver
1666 * to create its PCM instances.
1667 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1668 * callback.
1669 *
1670 * At least, substreams, channels_min and channels_max must be filled for
1671 * each stream. substreams = 0 indicates that the stream doesn't exist.
1672 * When rates and/or formats are zero, the supported values are queried
1673 * from the given nid. The nid is used also by the default ops.prepare
1674 * and ops.cleanup callbacks.
1675 *
1676 * The driver needs to call ops.open in its open callback. Similarly,
1677 * ops.close is supposed to be called in the close callback.
1678 * ops.prepare should be called in the prepare or hw_params callback
1679 * with the proper parameters for set up.
1680 * ops.cleanup should be called in hw_free for clean up of streams.
1681 *
1682 * This function returns 0 if successfull, or a negative error code.
1683 */
1684int snd_hda_build_pcms(struct hda_bus *bus)
1685{
1686 struct list_head *p;
1687
1688 list_for_each(p, &bus->codec_list) {
1689 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1690 unsigned int pcm, s;
1691 int err;
1692 if (! codec->patch_ops.build_pcms)
1693 continue;
1694 err = codec->patch_ops.build_pcms(codec);
1695 if (err < 0)
1696 return err;
1697 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1698 for (s = 0; s < 2; s++) {
1699 struct hda_pcm_stream *info;
1700 info = &codec->pcm_info[pcm].stream[s];
1701 if (! info->substreams)
1702 continue;
1703 err = set_pcm_default_values(codec, info);
1704 if (err < 0)
1705 return err;
1706 }
1707 }
1708 }
1709 return 0;
1710}
1711
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02001712EXPORT_SYMBOL(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714/**
1715 * snd_hda_check_board_config - compare the current codec with the config table
1716 * @codec: the HDA codec
1717 * @tbl: configuration table, terminated by null entries
1718 *
1719 * Compares the modelname or PCI subsystem id of the current codec with the
1720 * given configuration table. If a matching entry is found, returns its
1721 * config value (supposed to be 0 or positive).
1722 *
1723 * If no entries are matching, the function returns a negative value.
1724 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001725int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001727 const struct hda_board_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (codec->bus->modelname) {
Takashi Iwai72915482005-05-12 16:49:45 +02001730 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (c->modelname &&
1732 ! strcmp(codec->bus->modelname, c->modelname)) {
1733 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1734 return c->config;
1735 }
1736 }
1737 }
1738
1739 if (codec->bus->pci) {
1740 u16 subsystem_vendor, subsystem_device;
1741 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1742 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
Takashi Iwai72915482005-05-12 16:49:45 +02001743 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1744 if (c->pci_subvendor == subsystem_vendor &&
Takashi Iwai5ecd7022005-06-10 19:54:23 +02001745 (! c->pci_subdevice /* all match */||
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001746 (c->pci_subdevice == subsystem_device))) {
1747 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1748 subsystem_vendor, subsystem_device, c->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return c->config;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 }
1753 return -1;
1754}
1755
1756/**
1757 * snd_hda_add_new_ctls - create controls from the array
1758 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001759 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
1761 * This helper function creates and add new controls in the given array.
1762 * The array must be terminated with an empty entry as terminator.
1763 *
1764 * Returns 0 if successful, or a negative error code.
1765 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001766int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 int err;
1769
1770 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001771 struct snd_kcontrol *kctl;
1772 kctl = snd_ctl_new1(knew, codec);
1773 if (! kctl)
1774 return -ENOMEM;
1775 err = snd_ctl_add(codec->bus->card, kctl);
1776 if (err < 0) {
1777 if (! codec->addr)
1778 return err;
1779 kctl = snd_ctl_new1(knew, codec);
1780 if (! kctl)
1781 return -ENOMEM;
1782 kctl->id.device = codec->addr;
1783 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1784 return err;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 return 0;
1788}
1789
1790
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001791/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001792 * Channel mode helper
1793 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001794int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001795 const struct hda_channel_mode *chmode, int num_chmodes)
1796{
1797 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1798 uinfo->count = 1;
1799 uinfo->value.enumerated.items = num_chmodes;
1800 if (uinfo->value.enumerated.item >= num_chmodes)
1801 uinfo->value.enumerated.item = num_chmodes - 1;
1802 sprintf(uinfo->value.enumerated.name, "%dch",
1803 chmode[uinfo->value.enumerated.item].channels);
1804 return 0;
1805}
1806
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001807int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001808 const struct hda_channel_mode *chmode, int num_chmodes,
1809 int max_channels)
1810{
1811 int i;
1812
1813 for (i = 0; i < num_chmodes; i++) {
1814 if (max_channels == chmode[i].channels) {
1815 ucontrol->value.enumerated.item[0] = i;
1816 break;
1817 }
1818 }
1819 return 0;
1820}
1821
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001822int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001823 const struct hda_channel_mode *chmode, int num_chmodes,
1824 int *max_channelsp)
1825{
1826 unsigned int mode;
1827
1828 mode = ucontrol->value.enumerated.item[0];
1829 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001830 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001831 return 0;
1832 /* change the current channel setting */
1833 *max_channelsp = chmode[mode].channels;
1834 if (chmode[mode].sequence)
1835 snd_hda_sequence_write(codec, chmode[mode].sequence);
1836 return 1;
1837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839/*
1840 * input MUX helper
1841 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001842int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 unsigned int index;
1845
1846 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1847 uinfo->count = 1;
1848 uinfo->value.enumerated.items = imux->num_items;
1849 index = uinfo->value.enumerated.item;
1850 if (index >= imux->num_items)
1851 index = imux->num_items - 1;
1852 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1853 return 0;
1854}
1855
1856int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001857 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned int *cur_val)
1859{
1860 unsigned int idx;
1861
1862 idx = ucontrol->value.enumerated.item[0];
1863 if (idx >= imux->num_items)
1864 idx = imux->num_items - 1;
1865 if (*cur_val == idx && ! codec->in_resume)
1866 return 0;
1867 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1868 imux->items[idx].index);
1869 *cur_val = idx;
1870 return 1;
1871}
1872
1873
1874/*
1875 * Multi-channel / digital-out PCM helper functions
1876 */
1877
1878/*
1879 * open the digital out in the exclusive mode
1880 */
1881int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1882{
Ingo Molnar62932df2006-01-16 16:34:20 +01001883 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (mout->dig_out_used) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001885 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return -EBUSY; /* already being used */
1887 }
1888 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001889 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891}
1892
1893/*
1894 * release the digital out
1895 */
1896int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1897{
Ingo Molnar62932df2006-01-16 16:34:20 +01001898 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001900 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
1904/*
1905 * set up more restrictions for analog out
1906 */
1907int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001908 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 substream->runtime->hw.channels_max = mout->max_channels;
1911 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1912 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1913}
1914
1915/*
1916 * set up the i/o for analog out
1917 * when the digital out is available, copy the front out to digital out, too.
1918 */
1919int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1920 unsigned int stream_tag,
1921 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001922 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 hda_nid_t *nids = mout->dac_nids;
1925 int chs = substream->runtime->channels;
1926 int i;
1927
Ingo Molnar62932df2006-01-16 16:34:20 +01001928 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1930 if (chs == 2 &&
1931 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1932 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1933 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1934 /* setup digital receiver */
1935 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1936 stream_tag, 0, format);
1937 } else {
1938 mout->dig_out_used = 0;
1939 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1940 }
1941 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001942 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /* front */
1945 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02001946 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* headphone out will just decode front left/right (stereo) */
1948 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001949 /* extra outputs copied from front */
1950 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1951 if (mout->extra_out_nid[i])
1952 snd_hda_codec_setup_stream(codec,
1953 mout->extra_out_nid[i],
1954 stream_tag, 0, format);
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* surrounds */
1957 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001958 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1960 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001961 else /* copy front */
1962 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1963 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 return 0;
1966}
1967
1968/*
1969 * clean up the setting for analog out
1970 */
1971int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1972{
1973 hda_nid_t *nids = mout->dac_nids;
1974 int i;
1975
1976 for (i = 0; i < mout->num_dacs; i++)
1977 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1978 if (mout->hp_nid)
1979 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01001980 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1981 if (mout->extra_out_nid[i])
1982 snd_hda_codec_setup_stream(codec,
1983 mout->extra_out_nid[i],
1984 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01001985 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1987 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1988 mout->dig_out_used = 0;
1989 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001990 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992}
1993
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001994/*
1995 * Helper for automatic ping configuration
1996 */
Kailang Yangdf694da2005-12-05 19:42:22 +01001997
1998static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
1999{
2000 for (; *list; list++)
2001 if (*list == nid)
2002 return 1;
2003 return 0;
2004}
2005
Takashi Iwai82bc9552006-03-21 11:24:42 +01002006/*
2007 * Parse all pin widgets and store the useful pin nids to cfg
2008 *
2009 * The number of line-outs or any primary output is stored in line_outs,
2010 * and the corresponding output pins are assigned to line_out_pins[],
2011 * in the order of front, rear, CLFE, side, ...
2012 *
2013 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002014 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002015 * is detected, one of speaker of HP pins is assigned as the primary
2016 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2017 * if any analog output exists.
2018 *
2019 * The analog input pins are assigned to input_pins array.
2020 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2021 * respectively.
2022 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002023int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
2024 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002025{
2026 hda_nid_t nid, nid_start;
2027 int i, j, nodes;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002028 short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002029
2030 memset(cfg, 0, sizeof(*cfg));
2031
2032 memset(sequences, 0, sizeof(sequences));
2033 assoc_line_out = 0;
2034
2035 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2036 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002037 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002038 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2039 unsigned int def_conf;
2040 short assoc, loc;
2041
2042 /* read all default configuration for pin complex */
2043 if (wid_type != AC_WID_PIN)
2044 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002045 /* ignore the given nids (e.g. pc-beep returns error) */
2046 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2047 continue;
2048
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002049 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
2050 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2051 continue;
2052 loc = get_defcfg_location(def_conf);
2053 switch (get_defcfg_device(def_conf)) {
2054 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002055 seq = get_defcfg_sequence(def_conf);
2056 assoc = get_defcfg_association(def_conf);
2057 if (! assoc)
2058 continue;
2059 if (! assoc_line_out)
2060 assoc_line_out = assoc;
2061 else if (assoc_line_out != assoc)
2062 continue;
2063 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2064 continue;
2065 cfg->line_out_pins[cfg->line_outs] = nid;
2066 sequences[cfg->line_outs] = seq;
2067 cfg->line_outs++;
2068 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002069 case AC_JACK_SPEAKER:
Takashi Iwai82bc9552006-03-21 11:24:42 +01002070 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2071 continue;
2072 cfg->speaker_pins[cfg->speaker_outs] = nid;
2073 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002074 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002075 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002076 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2077 continue;
2078 cfg->hp_pins[cfg->hp_outs] = nid;
2079 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002080 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002081 case AC_JACK_MIC_IN: {
2082 int preferred, alt;
2083 if (loc == AC_JACK_LOC_FRONT) {
2084 preferred = AUTO_PIN_FRONT_MIC;
2085 alt = AUTO_PIN_MIC;
2086 } else {
2087 preferred = AUTO_PIN_MIC;
2088 alt = AUTO_PIN_FRONT_MIC;
2089 }
2090 if (!cfg->input_pins[preferred])
2091 cfg->input_pins[preferred] = nid;
2092 else if (!cfg->input_pins[alt])
2093 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002094 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002095 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002096 case AC_JACK_LINE_IN:
2097 if (loc == AC_JACK_LOC_FRONT)
2098 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2099 else
2100 cfg->input_pins[AUTO_PIN_LINE] = nid;
2101 break;
2102 case AC_JACK_CD:
2103 cfg->input_pins[AUTO_PIN_CD] = nid;
2104 break;
2105 case AC_JACK_AUX:
2106 cfg->input_pins[AUTO_PIN_AUX] = nid;
2107 break;
2108 case AC_JACK_SPDIF_OUT:
2109 cfg->dig_out_pin = nid;
2110 break;
2111 case AC_JACK_SPDIF_IN:
2112 cfg->dig_in_pin = nid;
2113 break;
2114 }
2115 }
2116
2117 /* sort by sequence */
2118 for (i = 0; i < cfg->line_outs; i++)
2119 for (j = i + 1; j < cfg->line_outs; j++)
2120 if (sequences[i] > sequences[j]) {
2121 seq = sequences[i];
2122 sequences[i] = sequences[j];
2123 sequences[j] = seq;
2124 nid = cfg->line_out_pins[i];
2125 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2126 cfg->line_out_pins[j] = nid;
2127 }
2128
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002129 /* Reorder the surround channels
2130 * ALSA sequence is front/surr/clfe/side
2131 * HDA sequence is:
2132 * 4-ch: front/surr => OK as it is
2133 * 6-ch: front/clfe/surr
2134 * 8-ch: front/clfe/side/surr
2135 */
2136 switch (cfg->line_outs) {
2137 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002138 nid = cfg->line_out_pins[1];
2139 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2140 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002141 break;
2142 case 4:
2143 nid = cfg->line_out_pins[1];
2144 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2145 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2146 cfg->line_out_pins[2] = nid;
2147 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002148 }
2149
Takashi Iwai82bc9552006-03-21 11:24:42 +01002150 /*
2151 * debug prints of the parsed results
2152 */
2153 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2154 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2155 cfg->line_out_pins[2], cfg->line_out_pins[3],
2156 cfg->line_out_pins[4]);
2157 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2158 cfg->speaker_outs, cfg->speaker_pins[0],
2159 cfg->speaker_pins[1], cfg->speaker_pins[2],
2160 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002161 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2162 cfg->hp_outs, cfg->hp_pins[0],
2163 cfg->hp_pins[1], cfg->hp_pins[2],
2164 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002165 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2166 " cd=0x%x, aux=0x%x\n",
2167 cfg->input_pins[AUTO_PIN_MIC],
2168 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2169 cfg->input_pins[AUTO_PIN_LINE],
2170 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2171 cfg->input_pins[AUTO_PIN_CD],
2172 cfg->input_pins[AUTO_PIN_AUX]);
2173
2174 /*
2175 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2176 * as a primary output
2177 */
2178 if (! cfg->line_outs) {
2179 if (cfg->speaker_outs) {
2180 cfg->line_outs = cfg->speaker_outs;
2181 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2182 sizeof(cfg->speaker_pins));
2183 cfg->speaker_outs = 0;
2184 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002185 } else if (cfg->hp_outs) {
2186 cfg->line_outs = cfg->hp_outs;
2187 memcpy(cfg->line_out_pins, cfg->hp_pins,
2188 sizeof(cfg->hp_pins));
2189 cfg->hp_outs = 0;
2190 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
Takashi Iwai82bc9552006-03-21 11:24:42 +01002191 }
2192 }
2193
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002194 return 0;
2195}
2196
Takashi Iwai4a471b72005-12-07 13:56:29 +01002197/* labels for input pins */
2198const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2199 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2200};
2201
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#ifdef CONFIG_PM
2204/*
2205 * power management
2206 */
2207
2208/**
2209 * snd_hda_suspend - suspend the codecs
2210 * @bus: the HDA bus
2211 * @state: suspsend state
2212 *
2213 * Returns 0 if successful.
2214 */
2215int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2216{
2217 struct list_head *p;
2218
2219 /* FIXME: should handle power widget capabilities */
2220 list_for_each(p, &bus->codec_list) {
2221 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2222 if (codec->patch_ops.suspend)
2223 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002224 hda_set_power_state(codec,
2225 codec->afg ? codec->afg : codec->mfg,
2226 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 return 0;
2229}
2230
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002231EXPORT_SYMBOL(snd_hda_suspend);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/**
2234 * snd_hda_resume - resume the codecs
2235 * @bus: the HDA bus
2236 * @state: resume state
2237 *
2238 * Returns 0 if successful.
2239 */
2240int snd_hda_resume(struct hda_bus *bus)
2241{
2242 struct list_head *p;
2243
2244 list_for_each(p, &bus->codec_list) {
2245 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002246 hda_set_power_state(codec,
2247 codec->afg ? codec->afg : codec->mfg,
2248 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (codec->patch_ops.resume)
2250 codec->patch_ops.resume(codec);
2251 }
2252 return 0;
2253}
2254
Takashi Iwaie5e8a1d2006-04-28 15:13:40 +02002255EXPORT_SYMBOL(snd_hda_resume);
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257/**
2258 * snd_hda_resume_ctls - resume controls in the new control list
2259 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002260 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002262 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * originally for snd_hda_add_new_ctls().
2264 * The array must be terminated with an empty entry as terminator.
2265 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002266int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002268 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 val = kmalloc(sizeof(*val), GFP_KERNEL);
2271 if (! val)
2272 return -ENOMEM;
2273 codec->in_resume = 1;
2274 for (; knew->name; knew++) {
2275 int i, count;
2276 count = knew->count ? knew->count : 1;
2277 for (i = 0; i < count; i++) {
2278 memset(val, 0, sizeof(*val));
2279 val->id.iface = knew->iface;
2280 val->id.device = knew->device;
2281 val->id.subdevice = knew->subdevice;
2282 strcpy(val->id.name, knew->name);
2283 val->id.index = knew->index ? knew->index : i;
2284 /* Assume that get callback reads only from cache,
2285 * not accessing to the real hardware
2286 */
2287 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2288 continue;
2289 snd_ctl_elem_write(codec->bus->card, NULL, val);
2290 }
2291 }
2292 codec->in_resume = 0;
2293 kfree(val);
2294 return 0;
2295}
2296
2297/**
2298 * snd_hda_resume_spdif_out - resume the digital out
2299 * @codec: the HDA codec
2300 */
2301int snd_hda_resume_spdif_out(struct hda_codec *codec)
2302{
2303 return snd_hda_resume_ctls(codec, dig_mixes);
2304}
2305
2306/**
2307 * snd_hda_resume_spdif_in - resume the digital in
2308 * @codec: the HDA codec
2309 */
2310int snd_hda_resume_spdif_in(struct hda_codec *codec)
2311{
2312 return snd_hda_resume_ctls(codec, dig_in_ctls);
2313}
2314#endif
2315
2316/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * INIT part
2318 */
2319
2320static int __init alsa_hda_init(void)
2321{
2322 return 0;
2323}
2324
2325static void __exit alsa_hda_exit(void)
2326{
2327}
2328
2329module_init(alsa_hda_init)
2330module_exit(alsa_hda_exit)