blob: 043529308676acb407d7f1213b32e29296d483d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020031#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/initval.h>
33#include "hda_local.h"
Takashi Iwai28073142007-07-27 18:58:06 +020034#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x10ec, "Realtek" },
Takashi Iwaia9226252006-09-17 22:05:54 +020049 { 0x1057, "Motorola" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010050 { 0x1106, "VIA" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020051 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020053 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020055 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 {} /* terminator */
57};
58
59/* codec presets */
60#include "hda_patch.h"
61
62
63/**
64 * snd_hda_codec_read - send a command and get the response
65 * @codec: the HDA codec
66 * @nid: NID to send the command
67 * @direct: direct flag
68 * @verb: the verb to send
69 * @parm: the parameter for the verb
70 *
71 * Send a single command and read the corresponding response.
72 *
73 * Returns the obtained response value, or -1 for an error.
74 */
Takashi Iwai0ba21762007-04-16 11:29:14 +020075unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
76 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int verb, unsigned int parm)
78{
79 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010080 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +020081 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 res = codec->bus->ops.get_response(codec);
83 else
84 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010085 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return res;
87}
88
89/**
90 * snd_hda_codec_write - send a single command without waiting for response
91 * @codec: the HDA codec
92 * @nid: NID to send the command
93 * @direct: direct flag
94 * @verb: the verb to send
95 * @parm: the parameter for the verb
96 *
97 * Send a single command without waiting for response.
98 *
99 * Returns 0 if successful, or a negative error code.
100 */
101int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
102 unsigned int verb, unsigned int parm)
103{
104 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100105 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100107 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return err;
109}
110
111/**
112 * snd_hda_sequence_write - sequence writes
113 * @codec: the HDA codec
114 * @seq: VERB array to send
115 *
116 * Send the commands sequentially from the given array.
117 * The array must be terminated with NID=0.
118 */
119void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
120{
121 for (; seq->nid; seq++)
122 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
123}
124
125/**
126 * snd_hda_get_sub_nodes - get the range of sub nodes
127 * @codec: the HDA codec
128 * @nid: NID to parse
129 * @start_id: the pointer to store the start NID
130 *
131 * Parse the NID and store the start NID of its sub-nodes.
132 * Returns the number of sub-nodes.
133 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200134int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
135 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned int parm;
138
139 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
140 *start_id = (parm >> 16) & 0x7fff;
141 return (int)(parm & 0x7fff);
142}
143
144/**
145 * snd_hda_get_connections - get connection list
146 * @codec: the HDA codec
147 * @nid: NID to parse
148 * @conn_list: connection list array
149 * @max_conns: max. number of connections to store
150 *
151 * Parses the connection list of the given widget and stores the list
152 * of NIDs.
153 *
154 * Returns the number of connections, or a negative error code.
155 */
156int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
157 hda_nid_t *conn_list, int max_conns)
158{
159 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100160 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100162 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 snd_assert(conn_list && max_conns > 0, return -EINVAL);
165
166 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
167 if (parm & AC_CLIST_LONG) {
168 /* long form */
169 shift = 16;
170 num_elems = 2;
171 } else {
172 /* short form */
173 shift = 8;
174 num_elems = 4;
175 }
176 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mask = (1 << (shift-1)) - 1;
178
Takashi Iwai0ba21762007-04-16 11:29:14 +0200179 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0; /* no connection */
181
182 if (conn_len == 1) {
183 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200184 parm = snd_hda_codec_read(codec, nid, 0,
185 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 conn_list[0] = parm & mask;
187 return 1;
188 }
189
190 /* multi connection */
191 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100192 prev_nid = 0;
193 for (i = 0; i < conn_len; i++) {
194 int range_val;
195 hda_nid_t val, n;
196
197 if (i % num_elems == 0)
198 parm = snd_hda_codec_read(codec, nid, 0,
199 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200200 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100201 val = parm & mask;
202 parm >>= shift;
203 if (range_val) {
204 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200205 if (!prev_nid || prev_nid >= val) {
206 snd_printk(KERN_WARNING "hda_codec: "
207 "invalid dep_range_val %x:%x\n",
208 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100209 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100211 for (n = prev_nid + 1; n <= val; n++) {
212 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200213 snd_printk(KERN_ERR
214 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100215 return -EINVAL;
216 }
217 conn_list[conns++] = n;
218 }
219 } else {
220 if (conns >= max_conns) {
221 snd_printk(KERN_ERR "Too many connections\n");
222 return -EINVAL;
223 }
224 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100226 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 return conns;
229}
230
231
232/**
233 * snd_hda_queue_unsol_event - add an unsolicited event to queue
234 * @bus: the BUS
235 * @res: unsolicited event (lower 32bit of RIRB entry)
236 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
237 *
238 * Adds the given event to the queue. The events are processed in
239 * the workqueue asynchronously. Call this function in the interrupt
240 * hanlder when RIRB receives an unsolicited event.
241 *
242 * Returns 0 if successful, or a negative error code.
243 */
244int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
245{
246 struct hda_bus_unsolicited *unsol;
247 unsigned int wp;
248
Takashi Iwai0ba21762007-04-16 11:29:14 +0200249 unsol = bus->unsol;
250 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252
253 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
254 unsol->wp = wp;
255
256 wp <<= 1;
257 unsol->queue[wp] = res;
258 unsol->queue[wp + 1] = res_ex;
259
Takashi Iwaie250af22006-12-19 17:08:52 +0100260 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 return 0;
263}
264
265/*
266 * process queueud unsolicited events
267 */
David Howellsc4028952006-11-22 14:57:56 +0000268static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
David Howellsc4028952006-11-22 14:57:56 +0000270 struct hda_bus_unsolicited *unsol =
271 container_of(work, struct hda_bus_unsolicited, work);
272 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct hda_codec *codec;
274 unsigned int rp, caddr, res;
275
276 while (unsol->rp != unsol->wp) {
277 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
278 unsol->rp = rp;
279 rp <<= 1;
280 res = unsol->queue[rp];
281 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200282 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 continue;
284 codec = bus->caddr_tbl[caddr & 0x0f];
285 if (codec && codec->patch_ops.unsol_event)
286 codec->patch_ops.unsol_event(codec, res);
287 }
288}
289
290/*
291 * initialize unsolicited queue
292 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200293static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct hda_bus_unsolicited *unsol;
296
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100297 if (bus->unsol) /* already initialized */
298 return 0;
299
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200300 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200301 if (!unsol) {
302 snd_printk(KERN_ERR "hda_codec: "
303 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -ENOMEM;
305 }
David Howellsc4028952006-11-22 14:57:56 +0000306 INIT_WORK(&unsol->work, process_unsol_events);
307 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bus->unsol = unsol;
309 return 0;
310}
311
312/*
313 * destructor
314 */
315static void snd_hda_codec_free(struct hda_codec *codec);
316
317static int snd_hda_bus_free(struct hda_bus *bus)
318{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200319 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Takashi Iwai0ba21762007-04-16 11:29:14 +0200321 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100324 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 kfree(bus->unsol);
326 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200327 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 snd_hda_codec_free(codec);
329 }
330 if (bus->ops.private_free)
331 bus->ops.private_free(bus);
332 kfree(bus);
333 return 0;
334}
335
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100336static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct hda_bus *bus = device->device_data;
339 return snd_hda_bus_free(bus);
340}
341
342/**
343 * snd_hda_bus_new - create a HDA bus
344 * @card: the card entry
345 * @temp: the template for hda_bus information
346 * @busp: the pointer to store the created bus instance
347 *
348 * Returns 0 if successful, or a negative error code.
349 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200350int __devinit snd_hda_bus_new(struct snd_card *card,
351 const struct hda_bus_template *temp,
352 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct hda_bus *bus;
355 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100356 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .dev_free = snd_hda_bus_dev_free,
358 };
359
360 snd_assert(temp, return -EINVAL);
361 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
362
363 if (busp)
364 *busp = NULL;
365
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200366 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (bus == NULL) {
368 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
369 return -ENOMEM;
370 }
371
372 bus->card = card;
373 bus->private_data = temp->private_data;
374 bus->pci = temp->pci;
375 bus->modelname = temp->modelname;
376 bus->ops = temp->ops;
377
Ingo Molnar62932df2006-01-16 16:34:20 +0100378 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 INIT_LIST_HEAD(&bus->codec_list);
380
Takashi Iwai0ba21762007-04-16 11:29:14 +0200381 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
382 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_hda_bus_free(bus);
384 return err;
385 }
386 if (busp)
387 *busp = bus;
388 return 0;
389}
390
Takashi Iwai82467612007-07-27 19:15:54 +0200391#ifdef CONFIG_SND_HDA_GENERIC
392#define is_generic_config(codec) \
393 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
394#else
395#define is_generic_config(codec) 0
396#endif
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
399 * find a matching codec preset
400 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200401static const struct hda_codec_preset __devinit *
402find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 const struct hda_codec_preset **tbl, *preset;
405
Takashi Iwai82467612007-07-27 19:15:54 +0200406 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100407 return NULL; /* use the generic parser */
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 for (tbl = hda_preset_tables; *tbl; tbl++) {
410 for (preset = *tbl; preset->id; preset++) {
411 u32 mask = preset->mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200412 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200414 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200415 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200416 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return preset;
418 }
419 }
420 return NULL;
421}
422
423/*
424 * snd_hda_get_codec_name - store the codec name
425 */
426void snd_hda_get_codec_name(struct hda_codec *codec,
427 char *name, int namelen)
428{
429 const struct hda_vendor_id *c;
430 const char *vendor = NULL;
431 u16 vendor_id = codec->vendor_id >> 16;
432 char tmp[16];
433
434 for (c = hda_vendor_ids; c->id; c++) {
435 if (c->id == vendor_id) {
436 vendor = c->name;
437 break;
438 }
439 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200440 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sprintf(tmp, "Generic %04x", vendor_id);
442 vendor = tmp;
443 }
444 if (codec->preset && codec->preset->name)
445 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
446 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 snprintf(name, namelen, "%s ID %x", vendor,
448 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200452 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200454static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int i, total_nodes;
457 hda_nid_t nid;
458
459 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
460 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200461 unsigned int func;
462 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
463 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200464 case AC_GRP_AUDIO_FUNCTION:
465 codec->afg = nid;
466 break;
467 case AC_GRP_MODEM_FUNCTION:
468 codec->mfg = nid;
469 break;
470 default:
471 break;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 * read widget caps for each widget and store in cache
478 */
479static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
480{
481 int i;
482 hda_nid_t nid;
483
484 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
485 &codec->start_nid);
486 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 return -ENOMEM;
489 nid = codec->start_nid;
490 for (i = 0; i < codec->num_nodes; i++, nid++)
491 codec->wcaps[i] = snd_hda_param_read(codec, nid,
492 AC_PAR_AUDIO_WIDGET_CAP);
493 return 0;
494}
495
496
Takashi Iwai01751f52007-08-10 16:59:39 +0200497static void init_hda_cache(struct hda_cache_rec *cache,
498 unsigned int record_size);
499static inline void free_hda_cache(struct hda_cache_rec *cache);
500
Takashi Iwai54d17402005-11-21 16:33:22 +0100501/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * codec destructor
503 */
504static void snd_hda_codec_free(struct hda_codec *codec)
505{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200506 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return;
508 list_del(&codec->list);
509 codec->bus->caddr_tbl[codec->addr] = NULL;
510 if (codec->patch_ops.free)
511 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200512 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200513 free_hda_cache(&codec->cmd_cache);
Takashi Iwai54d17402005-11-21 16:33:22 +0100514 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 kfree(codec);
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/**
519 * snd_hda_codec_new - create a HDA codec
520 * @bus: the bus to assign
521 * @codec_addr: the codec address
522 * @codecp: the pointer to store the generated codec
523 *
524 * Returns 0 if successful, or a negative error code.
525 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200526int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
527 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct hda_codec *codec;
530 char component[13];
531 int err;
532
533 snd_assert(bus, return -EINVAL);
534 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
535
536 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 snd_printk(KERN_ERR "hda_codec: "
538 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EBUSY;
540 }
541
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200542 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (codec == NULL) {
544 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
545 return -ENOMEM;
546 }
547
548 codec->bus = bus;
549 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100550 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200551 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200552 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 list_add_tail(&codec->list, &bus->codec_list);
555 bus->caddr_tbl[codec_addr] = codec;
556
Takashi Iwai0ba21762007-04-16 11:29:14 +0200557 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
558 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100559 if (codec->vendor_id == -1)
560 /* read again, hopefully the access method was corrected
561 * in the last read...
562 */
563 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
564 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
566 AC_PAR_SUBSYSTEM_ID);
567 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
568 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200570 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200571 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200572 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 snd_hda_codec_free(codec);
574 return -ENODEV;
575 }
576
Takashi Iwai54d17402005-11-21 16:33:22 +0100577 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
578 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
579 snd_hda_codec_free(codec);
580 return -ENOMEM;
581 }
582
Takashi Iwai0ba21762007-04-16 11:29:14 +0200583 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200584 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200585 codec->subsystem_id =
586 snd_hda_codec_read(codec, nid, 0,
587 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200588 }
589
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100590 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200591 /* audio codec should override the mixer name */
592 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 snd_hda_get_codec_name(codec, bus->card->mixername,
594 sizeof(bus->card->mixername));
595
Takashi Iwai82467612007-07-27 19:15:54 +0200596#ifdef CONFIG_SND_HDA_GENERIC
597 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200599 goto patched;
600 }
601#endif
602 if (codec->preset && codec->preset->patch) {
603 err = codec->preset->patch(codec);
604 goto patched;
605 }
606
607 /* call the default parser */
608#ifdef CONFIG_SND_HDA_GENERIC
609 err = snd_hda_parse_generic_codec(codec);
610#else
611 printk(KERN_ERR "hda-codec: No codec parser is available\n");
612 err = -ENODEV;
613#endif
614
615 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (err < 0) {
617 snd_hda_codec_free(codec);
618 return err;
619 }
620
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100621 if (codec->patch_ops.unsol_event)
622 init_unsol_queue(bus);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200625#ifdef CONFIG_SND_HDA_HWDEP
626 snd_hda_create_hwdep(codec);
627#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 sprintf(component, "HDA:%08x", codec->vendor_id);
630 snd_component_add(codec->bus->card, component);
631
632 if (codecp)
633 *codecp = codec;
634 return 0;
635}
636
637/**
638 * snd_hda_codec_setup_stream - set up the codec for streaming
639 * @codec: the CODEC to set up
640 * @nid: the NID to set up
641 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
642 * @channel_id: channel id to pass, zero based.
643 * @format: stream format.
644 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200645void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
646 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int channel_id, int format)
648{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200649 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200650 return;
651
Takashi Iwai0ba21762007-04-16 11:29:14 +0200652 snd_printdd("hda_codec_setup_stream: "
653 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 nid, stream_tag, channel_id, format);
655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
656 (stream_tag << 4) | channel_id);
657 msleep(1);
658 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
662 * amp access functions
663 */
664
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200665/* FIXME: more better hash key? */
666#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200668#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200671static void __devinit init_hda_cache(struct hda_cache_rec *cache,
672 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai01751f52007-08-10 16:59:39 +0200674 memset(cache, 0, sizeof(*cache));
675 memset(cache->hash, 0xff, sizeof(cache->hash));
676 cache->record_size = record_size;
677}
678
679static inline void free_hda_cache(struct hda_cache_rec *cache)
680{
681 kfree(cache->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200685static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
686 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Takashi Iwai01751f52007-08-10 16:59:39 +0200688 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
689 u16 cur = cache->hash[idx];
690 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 while (cur != 0xffff) {
Takashi Iwai01751f52007-08-10 16:59:39 +0200693 info = (struct hda_cache_head *)(cache->buffer +
694 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (info->key == key)
696 return info;
697 cur = info->next;
698 }
699
700 /* add a new hash entry */
Takashi Iwai01751f52007-08-10 16:59:39 +0200701 if (cache->num_entries >= cache->size) {
Takashi Iwaid0311662005-11-07 14:38:44 +0100702 /* reallocate the array */
Takashi Iwai01751f52007-08-10 16:59:39 +0200703 unsigned int new_size = cache->size + 64;
704 void *new_buffer;
705 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
706 if (!new_buffer) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200707 snd_printk(KERN_ERR "hda_codec: "
708 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100709 return NULL;
710 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200711 if (cache->buffer) {
712 memcpy(new_buffer, cache->buffer,
713 cache->size * cache->record_size);
714 kfree(cache->buffer);
Takashi Iwaid0311662005-11-07 14:38:44 +0100715 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200716 cache->size = new_size;
717 cache->buffer = new_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200719 cur = cache->num_entries++;
720 info = (struct hda_cache_head *)(cache->buffer +
721 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200723 info->val = 0;
724 info->next = cache->hash[idx];
725 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 return info;
728}
729
Takashi Iwai01751f52007-08-10 16:59:39 +0200730/* query and allocate an amp hash entry */
731static inline struct hda_amp_info *
732get_alloc_amp_hash(struct hda_codec *codec, u32 key)
733{
734 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
738 * query AMP capabilities for the given widget and direction
739 */
740static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
741{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200742 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Takashi Iwai0ba21762007-04-16 11:29:14 +0200744 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
745 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200747 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200748 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200750 info->amp_caps = snd_hda_param_read(codec, nid,
751 direction == HDA_OUTPUT ?
752 AC_PAR_AMP_OUT_CAP :
753 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200754 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200755 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 return info->amp_caps;
758}
759
Takashi Iwai897cc182007-05-29 19:01:37 +0200760int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
761 unsigned int caps)
762{
763 struct hda_amp_info *info;
764
765 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
766 if (!info)
767 return -EINVAL;
768 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200769 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200770 return 0;
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
774 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200775 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200777static unsigned int get_vol_mute(struct hda_codec *codec,
778 struct hda_amp_info *info, hda_nid_t nid,
779 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 u32 val, parm;
782
Takashi Iwai01751f52007-08-10 16:59:39 +0200783 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200784 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
787 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
788 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200789 val = snd_hda_codec_read(codec, nid, 0,
790 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200792 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200793 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200797 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200799static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200800 hda_nid_t nid, int ch, int direction, int index,
801 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 u32 parm;
804
805 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
806 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
807 parm |= index << AC_AMP_SET_INDEX_SHIFT;
808 parm |= val;
809 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200810 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200814 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Takashi Iwai834be882006-03-01 14:16:17 +0100816int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
817 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200819 struct hda_amp_info *info;
820 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
821 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200823 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200826/*
827 * update the AMP value, mask = bit mask to set, val = the value
828 */
Takashi Iwai834be882006-03-01 14:16:17 +0100829int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
830 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200832 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200833
Takashi Iwai0ba21762007-04-16 11:29:14 +0200834 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
835 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200837 val &= mask;
838 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200839 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200841 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 1;
843}
844
Takashi Iwai47fd8302007-08-10 17:11:07 +0200845/*
846 * update the AMP stereo with the same mask and value
847 */
848int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
849 int direction, int idx, int mask, int val)
850{
851 int ch, ret = 0;
852 for (ch = 0; ch < 2; ch++)
853 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
854 idx, mask, val);
855 return ret;
856}
857
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200858#ifdef CONFIG_PM
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200859/* resume the all amp commands from the cache */
860void snd_hda_codec_resume_amp(struct hda_codec *codec)
861{
862 struct hda_amp_info *buffer = codec->amp_cache.buffer;
863 int i;
864
865 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
866 u32 key = buffer->head.key;
867 hda_nid_t nid;
868 unsigned int idx, dir, ch;
869 if (!key)
870 continue;
871 nid = key & 0xff;
872 idx = (key >> 16) & 0xff;
873 dir = (key >> 24) & 0xff;
874 for (ch = 0; ch < 2; ch++) {
875 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
876 continue;
877 put_vol_mute(codec, buffer, nid, ch, dir, idx,
878 buffer->vol[ch]);
879 }
880 }
881}
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200882#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884/*
885 * AMP control callbacks
886 */
887/* retrieve parameters from private_value */
888#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
889#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
890#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
891#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
892
893/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200894int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
898 u16 nid = get_amp_nid(kcontrol);
899 u8 chs = get_amp_channels(kcontrol);
900 int dir = get_amp_direction(kcontrol);
901 u32 caps;
902
903 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 /* num steps */
905 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
906 if (!caps) {
907 printk(KERN_WARNING "hda_codec: "
908 "num_steps = 0 for NID=0x%x\n", nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EINVAL;
910 }
911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
912 uinfo->count = chs == 3 ? 2 : 1;
913 uinfo->value.integer.min = 0;
914 uinfo->value.integer.max = caps;
915 return 0;
916}
917
Takashi Iwai0ba21762007-04-16 11:29:14 +0200918int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
919 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
922 hda_nid_t nid = get_amp_nid(kcontrol);
923 int chs = get_amp_channels(kcontrol);
924 int dir = get_amp_direction(kcontrol);
925 int idx = get_amp_index(kcontrol);
926 long *valp = ucontrol->value.integer.value;
927
928 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +0200929 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
930 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +0200932 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
933 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
Takashi Iwai0ba21762007-04-16 11:29:14 +0200937int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
938 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
941 hda_nid_t nid = get_amp_nid(kcontrol);
942 int chs = get_amp_channels(kcontrol);
943 int dir = get_amp_direction(kcontrol);
944 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 long *valp = ucontrol->value.integer.value;
946 int change = 0;
947
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200948 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200949 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
950 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200951 valp++;
952 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200953 if (chs & 2)
954 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200955 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return change;
957}
958
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200959int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
960 unsigned int size, unsigned int __user *_tlv)
961{
962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
963 hda_nid_t nid = get_amp_nid(kcontrol);
964 int dir = get_amp_direction(kcontrol);
965 u32 caps, val1, val2;
966
967 if (size < 4 * sizeof(unsigned int))
968 return -ENOMEM;
969 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200970 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
971 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200972 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
973 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200974 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
975 return -EFAULT;
976 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
977 return -EFAULT;
978 if (put_user(val1, _tlv + 2))
979 return -EFAULT;
980 if (put_user(val2, _tlv + 3))
981 return -EFAULT;
982 return 0;
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200986int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
987 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 int chs = get_amp_channels(kcontrol);
990
991 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
992 uinfo->count = chs == 3 ? 2 : 1;
993 uinfo->value.integer.min = 0;
994 uinfo->value.integer.max = 1;
995 return 0;
996}
997
Takashi Iwai0ba21762007-04-16 11:29:14 +0200998int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
999 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1002 hda_nid_t nid = get_amp_nid(kcontrol);
1003 int chs = get_amp_channels(kcontrol);
1004 int dir = get_amp_direction(kcontrol);
1005 int idx = get_amp_index(kcontrol);
1006 long *valp = ucontrol->value.integer.value;
1007
1008 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001009 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001010 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001013 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015}
1016
Takashi Iwai0ba21762007-04-16 11:29:14 +02001017int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1021 hda_nid_t nid = get_amp_nid(kcontrol);
1022 int chs = get_amp_channels(kcontrol);
1023 int dir = get_amp_direction(kcontrol);
1024 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 long *valp = ucontrol->value.integer.value;
1026 int change = 0;
1027
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001028 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001029 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001030 HDA_AMP_MUTE,
1031 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001032 valp++;
1033 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001034 if (chs & 2)
1035 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001036 HDA_AMP_MUTE,
1037 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return change;
1040}
1041
1042/*
Takashi Iwai985be542005-11-02 18:26:49 +01001043 * bound volume controls
1044 *
1045 * bind multiple volumes (# indices, from 0)
1046 */
1047
1048#define AMP_VAL_IDX_SHIFT 19
1049#define AMP_VAL_IDX_MASK (0x0f<<19)
1050
Takashi Iwai0ba21762007-04-16 11:29:14 +02001051int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1052 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001053{
1054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1055 unsigned long pval;
1056 int err;
1057
Ingo Molnar62932df2006-01-16 16:34:20 +01001058 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001059 pval = kcontrol->private_value;
1060 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1061 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1062 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001063 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001064 return err;
1065}
1066
Takashi Iwai0ba21762007-04-16 11:29:14 +02001067int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1068 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001069{
1070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1071 unsigned long pval;
1072 int i, indices, err = 0, change = 0;
1073
Ingo Molnar62932df2006-01-16 16:34:20 +01001074 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001075 pval = kcontrol->private_value;
1076 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1077 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001078 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1079 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001080 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1081 if (err < 0)
1082 break;
1083 change |= err;
1084 }
1085 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001086 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001087 return err < 0 ? err : change;
1088}
1089
1090/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001091 * generic bound volume/swtich controls
1092 */
1093int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1094 struct snd_ctl_elem_info *uinfo)
1095{
1096 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1097 struct hda_bind_ctls *c;
1098 int err;
1099
1100 c = (struct hda_bind_ctls *)kcontrol->private_value;
1101 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1102 kcontrol->private_value = *c->values;
1103 err = c->ops->info(kcontrol, uinfo);
1104 kcontrol->private_value = (long)c;
1105 mutex_unlock(&codec->spdif_mutex);
1106 return err;
1107}
1108
1109int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1110 struct snd_ctl_elem_value *ucontrol)
1111{
1112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1113 struct hda_bind_ctls *c;
1114 int err;
1115
1116 c = (struct hda_bind_ctls *)kcontrol->private_value;
1117 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1118 kcontrol->private_value = *c->values;
1119 err = c->ops->get(kcontrol, ucontrol);
1120 kcontrol->private_value = (long)c;
1121 mutex_unlock(&codec->spdif_mutex);
1122 return err;
1123}
1124
1125int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1126 struct snd_ctl_elem_value *ucontrol)
1127{
1128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1129 struct hda_bind_ctls *c;
1130 unsigned long *vals;
1131 int err = 0, change = 0;
1132
1133 c = (struct hda_bind_ctls *)kcontrol->private_value;
1134 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1135 for (vals = c->values; *vals; vals++) {
1136 kcontrol->private_value = *vals;
1137 err = c->ops->put(kcontrol, ucontrol);
1138 if (err < 0)
1139 break;
1140 change |= err;
1141 }
1142 kcontrol->private_value = (long)c;
1143 mutex_unlock(&codec->spdif_mutex);
1144 return err < 0 ? err : change;
1145}
1146
1147int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1148 unsigned int size, unsigned int __user *tlv)
1149{
1150 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1151 struct hda_bind_ctls *c;
1152 int err;
1153
1154 c = (struct hda_bind_ctls *)kcontrol->private_value;
1155 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1156 kcontrol->private_value = *c->values;
1157 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1158 kcontrol->private_value = (long)c;
1159 mutex_unlock(&codec->spdif_mutex);
1160 return err;
1161}
1162
1163struct hda_ctl_ops snd_hda_bind_vol = {
1164 .info = snd_hda_mixer_amp_volume_info,
1165 .get = snd_hda_mixer_amp_volume_get,
1166 .put = snd_hda_mixer_amp_volume_put,
1167 .tlv = snd_hda_mixer_amp_tlv
1168};
1169
1170struct hda_ctl_ops snd_hda_bind_sw = {
1171 .info = snd_hda_mixer_amp_switch_info,
1172 .get = snd_hda_mixer_amp_switch_get,
1173 .put = snd_hda_mixer_amp_switch_put,
1174 .tlv = snd_hda_mixer_amp_tlv
1175};
1176
1177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 * SPDIF out controls
1179 */
1180
Takashi Iwai0ba21762007-04-16 11:29:14 +02001181static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1182 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1185 uinfo->count = 1;
1186 return 0;
1187}
1188
Takashi Iwai0ba21762007-04-16 11:29:14 +02001189static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1190 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1193 IEC958_AES0_NONAUDIO |
1194 IEC958_AES0_CON_EMPHASIS_5015 |
1195 IEC958_AES0_CON_NOT_COPYRIGHT;
1196 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1197 IEC958_AES1_CON_ORIGINAL;
1198 return 0;
1199}
1200
Takashi Iwai0ba21762007-04-16 11:29:14 +02001201static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1202 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1205 IEC958_AES0_NONAUDIO |
1206 IEC958_AES0_PRO_EMPHASIS_5015;
1207 return 0;
1208}
1209
Takashi Iwai0ba21762007-04-16 11:29:14 +02001210static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1211 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1214
1215 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1216 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1217 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1218 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1219
1220 return 0;
1221}
1222
1223/* convert from SPDIF status bits to HDA SPDIF bits
1224 * bit 0 (DigEn) is always set zero (to be filled later)
1225 */
1226static unsigned short convert_from_spdif_status(unsigned int sbits)
1227{
1228 unsigned short val = 0;
1229
1230 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001231 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001233 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001235 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1236 IEC958_AES0_PRO_EMPHASIS_5015)
1237 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001239 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1240 IEC958_AES0_CON_EMPHASIS_5015)
1241 val |= AC_DIG1_EMPHASIS;
1242 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1243 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001245 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1247 }
1248 return val;
1249}
1250
1251/* convert to SPDIF status bits from HDA SPDIF bits
1252 */
1253static unsigned int convert_to_spdif_status(unsigned short val)
1254{
1255 unsigned int sbits = 0;
1256
Takashi Iwai0ba21762007-04-16 11:29:14 +02001257 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001259 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 sbits |= IEC958_AES0_PROFESSIONAL;
1261 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1264 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001265 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001267 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001269 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1271 sbits |= val & (0x7f << 8);
1272 }
1273 return sbits;
1274}
1275
Takashi Iwai0ba21762007-04-16 11:29:14 +02001276static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1280 hda_nid_t nid = kcontrol->private_value;
1281 unsigned short val;
1282 int change;
1283
Ingo Molnar62932df2006-01-16 16:34:20 +01001284 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 codec->spdif_status = ucontrol->value.iec958.status[0] |
1286 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1287 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1288 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1289 val = convert_from_spdif_status(codec->spdif_status);
1290 val |= codec->spdif_ctls & 1;
1291 change = codec->spdif_ctls != val;
1292 codec->spdif_ctls = val;
1293
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001294 if (change) {
1295 snd_hda_codec_write_cache(codec, nid, 0,
1296 AC_VERB_SET_DIGI_CONVERT_1,
1297 val & 0xff);
1298 snd_hda_codec_write_cache(codec, nid, 0,
1299 AC_VERB_SET_DIGI_CONVERT_2,
1300 val >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
Ingo Molnar62932df2006-01-16 16:34:20 +01001303 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return change;
1305}
1306
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001307#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Takashi Iwai0ba21762007-04-16 11:29:14 +02001309static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1310 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1313
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1319 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1322 hda_nid_t nid = kcontrol->private_value;
1323 unsigned short val;
1324 int change;
1325
Ingo Molnar62932df2006-01-16 16:34:20 +01001326 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001327 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001329 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001331 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 codec->spdif_ctls = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001333 snd_hda_codec_write_cache(codec, nid, 0,
1334 AC_VERB_SET_DIGI_CONVERT_1,
1335 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001336 /* unmute amp switch (if any) */
1337 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001338 (val & AC_DIG1_ENABLE))
1339 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1340 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001342 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return change;
1344}
1345
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001346static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 {
1348 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1349 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1350 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1351 .info = snd_hda_spdif_mask_info,
1352 .get = snd_hda_spdif_cmask_get,
1353 },
1354 {
1355 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1356 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1357 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1358 .info = snd_hda_spdif_mask_info,
1359 .get = snd_hda_spdif_pmask_get,
1360 },
1361 {
1362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1363 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1364 .info = snd_hda_spdif_mask_info,
1365 .get = snd_hda_spdif_default_get,
1366 .put = snd_hda_spdif_default_put,
1367 },
1368 {
1369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1370 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1371 .info = snd_hda_spdif_out_switch_info,
1372 .get = snd_hda_spdif_out_switch_get,
1373 .put = snd_hda_spdif_out_switch_put,
1374 },
1375 { } /* end */
1376};
1377
1378/**
1379 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1380 * @codec: the HDA codec
1381 * @nid: audio out widget NID
1382 *
1383 * Creates controls related with the SPDIF output.
1384 * Called from each patch supporting the SPDIF out.
1385 *
1386 * Returns 0 if successful, or a negative error code.
1387 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001388int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001391 struct snd_kcontrol *kctl;
1392 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1395 kctl = snd_ctl_new1(dig_mix, codec);
1396 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001397 err = snd_ctl_add(codec->bus->card, kctl);
1398 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return err;
1400 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401 codec->spdif_ctls =
1402 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1404 return 0;
1405}
1406
1407/*
1408 * SPDIF input
1409 */
1410
1411#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1412
Takashi Iwai0ba21762007-04-16 11:29:14 +02001413static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1414 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1417
1418 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1419 return 0;
1420}
1421
Takashi Iwai0ba21762007-04-16 11:29:14 +02001422static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1426 hda_nid_t nid = kcontrol->private_value;
1427 unsigned int val = !!ucontrol->value.integer.value[0];
1428 int change;
1429
Ingo Molnar62932df2006-01-16 16:34:20 +01001430 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001432 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001434 snd_hda_codec_write_cache(codec, nid, 0,
1435 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001437 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return change;
1439}
1440
Takashi Iwai0ba21762007-04-16 11:29:14 +02001441static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1442 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1445 hda_nid_t nid = kcontrol->private_value;
1446 unsigned short val;
1447 unsigned int sbits;
1448
1449 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1450 sbits = convert_to_spdif_status(val);
1451 ucontrol->value.iec958.status[0] = sbits;
1452 ucontrol->value.iec958.status[1] = sbits >> 8;
1453 ucontrol->value.iec958.status[2] = sbits >> 16;
1454 ucontrol->value.iec958.status[3] = sbits >> 24;
1455 return 0;
1456}
1457
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001458static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 {
1460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1461 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1462 .info = snd_hda_spdif_in_switch_info,
1463 .get = snd_hda_spdif_in_switch_get,
1464 .put = snd_hda_spdif_in_switch_put,
1465 },
1466 {
1467 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1468 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1469 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1470 .info = snd_hda_spdif_mask_info,
1471 .get = snd_hda_spdif_in_status_get,
1472 },
1473 { } /* end */
1474};
1475
1476/**
1477 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1478 * @codec: the HDA codec
1479 * @nid: audio in widget NID
1480 *
1481 * Creates controls related with the SPDIF input.
1482 * Called from each patch supporting the SPDIF in.
1483 *
1484 * Returns 0 if successful, or a negative error code.
1485 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001486int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001489 struct snd_kcontrol *kctl;
1490 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1493 kctl = snd_ctl_new1(dig_mix, codec);
1494 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495 err = snd_ctl_add(codec->bus->card, kctl);
1496 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return err;
1498 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 codec->spdif_in_enable =
1500 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1501 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503}
1504
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001505#ifdef CONFIG_PM
1506/*
1507 * command cache
1508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001510/* build a 32bit cache key with the widget id and the command parameter */
1511#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1512#define get_cmd_cache_nid(key) ((key) & 0xff)
1513#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1514
1515/**
1516 * snd_hda_codec_write_cache - send a single command with caching
1517 * @codec: the HDA codec
1518 * @nid: NID to send the command
1519 * @direct: direct flag
1520 * @verb: the verb to send
1521 * @parm: the parameter for the verb
1522 *
1523 * Send a single command without waiting for response.
1524 *
1525 * Returns 0 if successful, or a negative error code.
1526 */
1527int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1528 int direct, unsigned int verb, unsigned int parm)
1529{
1530 int err;
1531 mutex_lock(&codec->bus->cmd_mutex);
1532 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1533 if (!err) {
1534 struct hda_cache_head *c;
1535 u32 key = build_cmd_cache_key(nid, verb);
1536 c = get_alloc_hash(&codec->cmd_cache, key);
1537 if (c)
1538 c->val = parm;
1539 }
1540 mutex_unlock(&codec->bus->cmd_mutex);
1541 return err;
1542}
1543
1544/* resume the all commands from the cache */
1545void snd_hda_codec_resume_cache(struct hda_codec *codec)
1546{
1547 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1548 int i;
1549
1550 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1551 u32 key = buffer->key;
1552 if (!key)
1553 continue;
1554 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1555 get_cmd_cache_cmd(key), buffer->val);
1556 }
1557}
1558
1559/**
1560 * snd_hda_sequence_write_cache - sequence writes with caching
1561 * @codec: the HDA codec
1562 * @seq: VERB array to send
1563 *
1564 * Send the commands sequentially from the given array.
1565 * Thte commands are recorded on cache for power-save and resume.
1566 * The array must be terminated with NID=0.
1567 */
1568void snd_hda_sequence_write_cache(struct hda_codec *codec,
1569 const struct hda_verb *seq)
1570{
1571 for (; seq->nid; seq++)
1572 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1573 seq->param);
1574}
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001575#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001576
Takashi Iwai54d17402005-11-21 16:33:22 +01001577/*
1578 * set power state of the codec
1579 */
1580static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1581 unsigned int power_state)
1582{
1583 hda_nid_t nid, nid_start;
1584 int nodes;
1585
1586 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1587 power_state);
1588
1589 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1590 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1591 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1592 snd_hda_codec_write(codec, nid, 0,
1593 AC_VERB_SET_POWER_STATE,
1594 power_state);
1595 }
1596
1597 if (power_state == AC_PWRST_D0)
1598 msleep(10);
1599}
1600
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602/**
1603 * snd_hda_build_controls - build mixer controls
1604 * @bus: the BUS
1605 *
1606 * Creates mixer controls for each codec included in the bus.
1607 *
1608 * Returns 0 if successful, otherwise a negative error code.
1609 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001610int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001612 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* build controls */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001615 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001617 if (!codec->patch_ops.build_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 continue;
1619 err = codec->patch_ops.build_controls(codec);
1620 if (err < 0)
1621 return err;
1622 }
1623
1624 /* initialize */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001627 hda_set_power_state(codec,
1628 codec->afg ? codec->afg : codec->mfg,
1629 AC_PWRST_D0);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 if (!codec->patch_ops.init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 continue;
1632 err = codec->patch_ops.init(codec);
1633 if (err < 0)
1634 return err;
1635 }
1636 return 0;
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
1640 * stream formats
1641 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001642struct hda_rate_tbl {
1643 unsigned int hz;
1644 unsigned int alsa_bits;
1645 unsigned int hda_fmt;
1646};
1647
1648static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001650
1651 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1653 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1654 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1655 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1656 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1657 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1658 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1659 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1660 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1661 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1662 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001663#define AC_PAR_PCM_RATE_BITS 11
1664 /* up to bits 10, 384kHZ isn't supported properly */
1665
1666 /* not autodetected value */
1667 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001668
Takashi Iwaibefdf312005-08-22 13:57:55 +02001669 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670};
1671
1672/**
1673 * snd_hda_calc_stream_format - calculate format bitset
1674 * @rate: the sample rate
1675 * @channels: the number of channels
1676 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1677 * @maxbps: the max. bps
1678 *
1679 * Calculate the format bitset from the given rate, channels and th PCM format.
1680 *
1681 * Return zero if invalid.
1682 */
1683unsigned int snd_hda_calc_stream_format(unsigned int rate,
1684 unsigned int channels,
1685 unsigned int format,
1686 unsigned int maxbps)
1687{
1688 int i;
1689 unsigned int val = 0;
1690
Takashi Iwaibefdf312005-08-22 13:57:55 +02001691 for (i = 0; rate_bits[i].hz; i++)
1692 if (rate_bits[i].hz == rate) {
1693 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 snd_printdd("invalid rate %d\n", rate);
1698 return 0;
1699 }
1700
1701 if (channels == 0 || channels > 8) {
1702 snd_printdd("invalid channels %d\n", channels);
1703 return 0;
1704 }
1705 val |= channels - 1;
1706
1707 switch (snd_pcm_format_width(format)) {
1708 case 8: val |= 0x00; break;
1709 case 16: val |= 0x10; break;
1710 case 20:
1711 case 24:
1712 case 32:
1713 if (maxbps >= 32)
1714 val |= 0x40;
1715 else if (maxbps >= 24)
1716 val |= 0x30;
1717 else
1718 val |= 0x20;
1719 break;
1720 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001721 snd_printdd("invalid format width %d\n",
1722 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724 }
1725
1726 return val;
1727}
1728
1729/**
1730 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1731 * @codec: the HDA codec
1732 * @nid: NID to query
1733 * @ratesp: the pointer to store the detected rate bitflags
1734 * @formatsp: the pointer to store the detected formats
1735 * @bpsp: the pointer to store the detected format widths
1736 *
1737 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1738 * or @bsps argument is ignored.
1739 *
1740 * Returns 0 if successful, otherwise a negative error code.
1741 */
1742int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1743 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1744{
1745 int i;
1746 unsigned int val, streams;
1747
1748 val = 0;
1749 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001750 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1752 if (val == -1)
1753 return -EIO;
1754 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001755 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1757
1758 if (ratesp) {
1759 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001760 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001762 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764 *ratesp = rates;
1765 }
1766
1767 if (formatsp || bpsp) {
1768 u64 formats = 0;
1769 unsigned int bps;
1770 unsigned int wcaps;
1771
Takashi Iwai54d17402005-11-21 16:33:22 +01001772 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1774 if (streams == -1)
1775 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001776 if (!streams) {
1777 streams = snd_hda_param_read(codec, codec->afg,
1778 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (streams == -1)
1780 return -EIO;
1781 }
1782
1783 bps = 0;
1784 if (streams & AC_SUPFMT_PCM) {
1785 if (val & AC_SUPPCM_BITS_8) {
1786 formats |= SNDRV_PCM_FMTBIT_U8;
1787 bps = 8;
1788 }
1789 if (val & AC_SUPPCM_BITS_16) {
1790 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1791 bps = 16;
1792 }
1793 if (wcaps & AC_WCAP_DIGITAL) {
1794 if (val & AC_SUPPCM_BITS_32)
1795 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1796 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1797 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1798 if (val & AC_SUPPCM_BITS_24)
1799 bps = 24;
1800 else if (val & AC_SUPPCM_BITS_20)
1801 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001802 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1803 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1805 if (val & AC_SUPPCM_BITS_32)
1806 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 else if (val & AC_SUPPCM_BITS_24)
1808 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02001809 else if (val & AC_SUPPCM_BITS_20)
1810 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001813 else if (streams == AC_SUPFMT_FLOAT32) {
1814 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1816 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001817 } else if (streams == AC_SUPFMT_AC3) {
1818 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /* temporary hack: we have still no proper support
1820 * for the direct AC3 stream...
1821 */
1822 formats |= SNDRV_PCM_FMTBIT_U8;
1823 bps = 8;
1824 }
1825 if (formatsp)
1826 *formatsp = formats;
1827 if (bpsp)
1828 *bpsp = bps;
1829 }
1830
1831 return 0;
1832}
1833
1834/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02001835 * snd_hda_is_supported_format - check whether the given node supports
1836 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 *
1838 * Returns 1 if supported, 0 if not.
1839 */
1840int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1841 unsigned int format)
1842{
1843 int i;
1844 unsigned int val = 0, rate, stream;
1845
1846 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001847 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1849 if (val == -1)
1850 return 0;
1851 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001852 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1854 if (val == -1)
1855 return 0;
1856 }
1857
1858 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001859 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02001860 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (val & (1 << i))
1862 break;
1863 return 0;
1864 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001865 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return 0;
1867
1868 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1869 if (stream == -1)
1870 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001871 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001873 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
1875
1876 if (stream & AC_SUPFMT_PCM) {
1877 switch (format & 0xf0) {
1878 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001879 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
1881 break;
1882 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001883 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885 break;
1886 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001887 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889 break;
1890 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001891 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893 break;
1894 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001895 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return 0;
1897 break;
1898 default:
1899 return 0;
1900 }
1901 } else {
1902 /* FIXME: check for float32 and AC3? */
1903 }
1904
1905 return 1;
1906}
1907
1908/*
1909 * PCM stuff
1910 */
1911static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1912 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001913 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 return 0;
1916}
1917
1918static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1919 struct hda_codec *codec,
1920 unsigned int stream_tag,
1921 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001922 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1925 return 0;
1926}
1927
1928static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1929 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001930 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1933 return 0;
1934}
1935
Takashi Iwai0ba21762007-04-16 11:29:14 +02001936static int __devinit set_pcm_default_values(struct hda_codec *codec,
1937 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001939 /* query support PCM information from the given NID */
1940 if (info->nid && (!info->rates || !info->formats)) {
1941 snd_hda_query_supported_pcm(codec, info->nid,
1942 info->rates ? NULL : &info->rates,
1943 info->formats ? NULL : &info->formats,
1944 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 if (info->ops.open == NULL)
1947 info->ops.open = hda_pcm_default_open_close;
1948 if (info->ops.close == NULL)
1949 info->ops.close = hda_pcm_default_open_close;
1950 if (info->ops.prepare == NULL) {
1951 snd_assert(info->nid, return -EINVAL);
1952 info->ops.prepare = hda_pcm_default_prepare;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (info->ops.cleanup == NULL) {
1955 snd_assert(info->nid, return -EINVAL);
1956 info->ops.cleanup = hda_pcm_default_cleanup;
1957 }
1958 return 0;
1959}
1960
1961/**
1962 * snd_hda_build_pcms - build PCM information
1963 * @bus: the BUS
1964 *
1965 * Create PCM information for each codec included in the bus.
1966 *
1967 * The build_pcms codec patch is requested to set up codec->num_pcms and
1968 * codec->pcm_info properly. The array is referred by the top-level driver
1969 * to create its PCM instances.
1970 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1971 * callback.
1972 *
1973 * At least, substreams, channels_min and channels_max must be filled for
1974 * each stream. substreams = 0 indicates that the stream doesn't exist.
1975 * When rates and/or formats are zero, the supported values are queried
1976 * from the given nid. The nid is used also by the default ops.prepare
1977 * and ops.cleanup callbacks.
1978 *
1979 * The driver needs to call ops.open in its open callback. Similarly,
1980 * ops.close is supposed to be called in the close callback.
1981 * ops.prepare should be called in the prepare or hw_params callback
1982 * with the proper parameters for set up.
1983 * ops.cleanup should be called in hw_free for clean up of streams.
1984 *
1985 * This function returns 0 if successfull, or a negative error code.
1986 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001987int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001989 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Takashi Iwai0ba21762007-04-16 11:29:14 +02001991 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 unsigned int pcm, s;
1993 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001994 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 continue;
1996 err = codec->patch_ops.build_pcms(codec);
1997 if (err < 0)
1998 return err;
1999 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2000 for (s = 0; s < 2; s++) {
2001 struct hda_pcm_stream *info;
2002 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02002003 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 continue;
2005 err = set_pcm_default_values(codec, info);
2006 if (err < 0)
2007 return err;
2008 }
2009 }
2010 }
2011 return 0;
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/**
2015 * snd_hda_check_board_config - compare the current codec with the config table
2016 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002017 * @num_configs: number of config enums
2018 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * @tbl: configuration table, terminated by null entries
2020 *
2021 * Compares the modelname or PCI subsystem id of the current codec with the
2022 * given configuration table. If a matching entry is found, returns its
2023 * config value (supposed to be 0 or positive).
2024 *
2025 * If no entries are matching, the function returns a negative value.
2026 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002027int snd_hda_check_board_config(struct hda_codec *codec,
2028 int num_configs, const char **models,
2029 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002031 if (codec->bus->modelname && models) {
2032 int i;
2033 for (i = 0; i < num_configs; i++) {
2034 if (models[i] &&
2035 !strcmp(codec->bus->modelname, models[i])) {
2036 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2037 "selected\n", models[i]);
2038 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040 }
2041 }
2042
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002043 if (!codec->bus->pci || !tbl)
2044 return -1;
2045
2046 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2047 if (!tbl)
2048 return -1;
2049 if (tbl->value >= 0 && tbl->value < num_configs) {
2050#ifdef CONFIG_SND_DEBUG_DETECT
2051 char tmp[10];
2052 const char *model = NULL;
2053 if (models)
2054 model = models[tbl->value];
2055 if (!model) {
2056 sprintf(tmp, "#%d", tbl->value);
2057 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002059 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2060 "for config %x:%x (%s)\n",
2061 model, tbl->subvendor, tbl->subdevice,
2062 (tbl->name ? tbl->name : "Unknown device"));
2063#endif
2064 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066 return -1;
2067}
2068
2069/**
2070 * snd_hda_add_new_ctls - create controls from the array
2071 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002072 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 * This helper function creates and add new controls in the given array.
2075 * The array must be terminated with an empty entry as terminator.
2076 *
2077 * Returns 0 if successful, or a negative error code.
2078 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002079int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 int err;
2082
2083 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002084 struct snd_kcontrol *kctl;
2085 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002086 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002087 return -ENOMEM;
2088 err = snd_ctl_add(codec->bus->card, kctl);
2089 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002091 return err;
2092 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002093 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002094 return -ENOMEM;
2095 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002096 err = snd_ctl_add(codec->bus->card, kctl);
2097 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002098 return err;
2099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101 return 0;
2102}
2103
2104
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002105/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002106 * Channel mode helper
2107 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108int snd_hda_ch_mode_info(struct hda_codec *codec,
2109 struct snd_ctl_elem_info *uinfo,
2110 const struct hda_channel_mode *chmode,
2111 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002112{
2113 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2114 uinfo->count = 1;
2115 uinfo->value.enumerated.items = num_chmodes;
2116 if (uinfo->value.enumerated.item >= num_chmodes)
2117 uinfo->value.enumerated.item = num_chmodes - 1;
2118 sprintf(uinfo->value.enumerated.name, "%dch",
2119 chmode[uinfo->value.enumerated.item].channels);
2120 return 0;
2121}
2122
Takashi Iwai0ba21762007-04-16 11:29:14 +02002123int snd_hda_ch_mode_get(struct hda_codec *codec,
2124 struct snd_ctl_elem_value *ucontrol,
2125 const struct hda_channel_mode *chmode,
2126 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002127 int max_channels)
2128{
2129 int i;
2130
2131 for (i = 0; i < num_chmodes; i++) {
2132 if (max_channels == chmode[i].channels) {
2133 ucontrol->value.enumerated.item[0] = i;
2134 break;
2135 }
2136 }
2137 return 0;
2138}
2139
Takashi Iwai0ba21762007-04-16 11:29:14 +02002140int snd_hda_ch_mode_put(struct hda_codec *codec,
2141 struct snd_ctl_elem_value *ucontrol,
2142 const struct hda_channel_mode *chmode,
2143 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002144 int *max_channelsp)
2145{
2146 unsigned int mode;
2147
2148 mode = ucontrol->value.enumerated.item[0];
2149 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002150 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002151 return 0;
2152 /* change the current channel setting */
2153 *max_channelsp = chmode[mode].channels;
2154 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002155 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002156 return 1;
2157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159/*
2160 * input MUX helper
2161 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002162int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2163 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 unsigned int index;
2166
2167 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2168 uinfo->count = 1;
2169 uinfo->value.enumerated.items = imux->num_items;
2170 index = uinfo->value.enumerated.item;
2171 if (index >= imux->num_items)
2172 index = imux->num_items - 1;
2173 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2174 return 0;
2175}
2176
Takashi Iwai0ba21762007-04-16 11:29:14 +02002177int snd_hda_input_mux_put(struct hda_codec *codec,
2178 const struct hda_input_mux *imux,
2179 struct snd_ctl_elem_value *ucontrol,
2180 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 unsigned int *cur_val)
2182{
2183 unsigned int idx;
2184
2185 idx = ucontrol->value.enumerated.item[0];
2186 if (idx >= imux->num_items)
2187 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002188 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002190 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2191 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 *cur_val = idx;
2193 return 1;
2194}
2195
2196
2197/*
2198 * Multi-channel / digital-out PCM helper functions
2199 */
2200
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002201/* setup SPDIF output stream */
2202static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2203 unsigned int stream_tag, unsigned int format)
2204{
2205 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2206 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2207 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2208 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2209 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2210 /* turn on again (if needed) */
2211 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2212 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2213 codec->spdif_ctls & 0xff);
2214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216/*
2217 * open the digital out in the exclusive mode
2218 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002219int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2220 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Ingo Molnar62932df2006-01-16 16:34:20 +01002222 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002223 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2224 /* already opened as analog dup; reset it once */
2225 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002227 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
2230
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002231int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2232 struct hda_multi_out *mout,
2233 unsigned int stream_tag,
2234 unsigned int format,
2235 struct snd_pcm_substream *substream)
2236{
2237 mutex_lock(&codec->spdif_mutex);
2238 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2239 mutex_unlock(&codec->spdif_mutex);
2240 return 0;
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/*
2244 * release the digital out
2245 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002246int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2247 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Ingo Molnar62932df2006-01-16 16:34:20 +01002249 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002251 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return 0;
2253}
2254
2255/*
2256 * set up more restrictions for analog out
2257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002258int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2259 struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002260 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
2262 substream->runtime->hw.channels_max = mout->max_channels;
2263 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2264 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2265}
2266
2267/*
2268 * set up the i/o for analog out
2269 * when the digital out is available, copy the front out to digital out, too.
2270 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002271int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2272 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 unsigned int stream_tag,
2274 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002275 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 hda_nid_t *nids = mout->dac_nids;
2278 int chs = substream->runtime->channels;
2279 int i;
2280
Ingo Molnar62932df2006-01-16 16:34:20 +01002281 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2283 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002284 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2285 format) &&
2286 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002288 setup_dig_out_stream(codec, mout->dig_out_nid,
2289 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 } else {
2291 mout->dig_out_used = 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002292 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2293 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002296 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002299 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2300 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02002301 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002303 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2304 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002305 /* extra outputs copied from front */
2306 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2307 if (mout->extra_out_nid[i])
2308 snd_hda_codec_setup_stream(codec,
2309 mout->extra_out_nid[i],
2310 stream_tag, 0, format);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* surrounds */
2313 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002314 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002315 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2316 i * 2, format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002317 else /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002318 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2319 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 return 0;
2322}
2323
2324/*
2325 * clean up the setting for analog out
2326 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2328 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 hda_nid_t *nids = mout->dac_nids;
2331 int i;
2332
2333 for (i = 0; i < mout->num_dacs; i++)
2334 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2335 if (mout->hp_nid)
2336 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002337 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2338 if (mout->extra_out_nid[i])
2339 snd_hda_codec_setup_stream(codec,
2340 mout->extra_out_nid[i],
2341 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01002342 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2344 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2345 mout->dig_out_used = 0;
2346 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002347 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349}
2350
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002351/*
2352 * Helper for automatic ping configuration
2353 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002354
Takashi Iwai12f288b2007-08-02 15:51:59 +02002355static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002356{
2357 for (; *list; list++)
2358 if (*list == nid)
2359 return 1;
2360 return 0;
2361}
2362
Steve Longerbeam81937d32007-05-08 15:33:03 +02002363
2364/*
2365 * Sort an associated group of pins according to their sequence numbers.
2366 */
2367static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2368 int num_pins)
2369{
2370 int i, j;
2371 short seq;
2372 hda_nid_t nid;
2373
2374 for (i = 0; i < num_pins; i++) {
2375 for (j = i + 1; j < num_pins; j++) {
2376 if (sequences[i] > sequences[j]) {
2377 seq = sequences[i];
2378 sequences[i] = sequences[j];
2379 sequences[j] = seq;
2380 nid = pins[i];
2381 pins[i] = pins[j];
2382 pins[j] = nid;
2383 }
2384 }
2385 }
2386}
2387
2388
Takashi Iwai82bc9552006-03-21 11:24:42 +01002389/*
2390 * Parse all pin widgets and store the useful pin nids to cfg
2391 *
2392 * The number of line-outs or any primary output is stored in line_outs,
2393 * and the corresponding output pins are assigned to line_out_pins[],
2394 * in the order of front, rear, CLFE, side, ...
2395 *
2396 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002397 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002398 * is detected, one of speaker of HP pins is assigned as the primary
2399 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2400 * if any analog output exists.
2401 *
2402 * The analog input pins are assigned to input_pins array.
2403 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2404 * respectively.
2405 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002406int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2407 struct auto_pin_cfg *cfg,
2408 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002409{
2410 hda_nid_t nid, nid_start;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002411 int nodes;
2412 short seq, assoc_line_out, assoc_speaker;
2413 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2414 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002415
2416 memset(cfg, 0, sizeof(*cfg));
2417
Steve Longerbeam81937d32007-05-08 15:33:03 +02002418 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2419 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2420 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002421
2422 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2423 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002424 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425 unsigned int wid_type =
2426 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002427 unsigned int def_conf;
2428 short assoc, loc;
2429
2430 /* read all default configuration for pin complex */
2431 if (wid_type != AC_WID_PIN)
2432 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002433 /* ignore the given nids (e.g. pc-beep returns error) */
2434 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2435 continue;
2436
Takashi Iwai0ba21762007-04-16 11:29:14 +02002437 def_conf = snd_hda_codec_read(codec, nid, 0,
2438 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002439 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2440 continue;
2441 loc = get_defcfg_location(def_conf);
2442 switch (get_defcfg_device(def_conf)) {
2443 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002444 seq = get_defcfg_sequence(def_conf);
2445 assoc = get_defcfg_association(def_conf);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002446 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002447 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002449 assoc_line_out = assoc;
2450 else if (assoc_line_out != assoc)
2451 continue;
2452 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2453 continue;
2454 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002455 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002456 cfg->line_outs++;
2457 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002458 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002459 seq = get_defcfg_sequence(def_conf);
2460 assoc = get_defcfg_association(def_conf);
2461 if (! assoc)
2462 continue;
2463 if (! assoc_speaker)
2464 assoc_speaker = assoc;
2465 else if (assoc_speaker != assoc)
2466 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002467 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2468 continue;
2469 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002470 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002471 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002472 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002473 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002474 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2475 continue;
2476 cfg->hp_pins[cfg->hp_outs] = nid;
2477 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002478 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002479 case AC_JACK_MIC_IN: {
2480 int preferred, alt;
2481 if (loc == AC_JACK_LOC_FRONT) {
2482 preferred = AUTO_PIN_FRONT_MIC;
2483 alt = AUTO_PIN_MIC;
2484 } else {
2485 preferred = AUTO_PIN_MIC;
2486 alt = AUTO_PIN_FRONT_MIC;
2487 }
2488 if (!cfg->input_pins[preferred])
2489 cfg->input_pins[preferred] = nid;
2490 else if (!cfg->input_pins[alt])
2491 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002492 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002493 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002494 case AC_JACK_LINE_IN:
2495 if (loc == AC_JACK_LOC_FRONT)
2496 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2497 else
2498 cfg->input_pins[AUTO_PIN_LINE] = nid;
2499 break;
2500 case AC_JACK_CD:
2501 cfg->input_pins[AUTO_PIN_CD] = nid;
2502 break;
2503 case AC_JACK_AUX:
2504 cfg->input_pins[AUTO_PIN_AUX] = nid;
2505 break;
2506 case AC_JACK_SPDIF_OUT:
2507 cfg->dig_out_pin = nid;
2508 break;
2509 case AC_JACK_SPDIF_IN:
2510 cfg->dig_in_pin = nid;
2511 break;
2512 }
2513 }
2514
2515 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02002516 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2517 cfg->line_outs);
2518 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2519 cfg->speaker_outs);
2520
2521 /*
2522 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2523 * as a primary output
2524 */
2525 if (!cfg->line_outs) {
2526 if (cfg->speaker_outs) {
2527 cfg->line_outs = cfg->speaker_outs;
2528 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2529 sizeof(cfg->speaker_pins));
2530 cfg->speaker_outs = 0;
2531 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2532 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2533 } else if (cfg->hp_outs) {
2534 cfg->line_outs = cfg->hp_outs;
2535 memcpy(cfg->line_out_pins, cfg->hp_pins,
2536 sizeof(cfg->hp_pins));
2537 cfg->hp_outs = 0;
2538 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2539 cfg->line_out_type = AUTO_PIN_HP_OUT;
2540 }
2541 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002542
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002543 /* Reorder the surround channels
2544 * ALSA sequence is front/surr/clfe/side
2545 * HDA sequence is:
2546 * 4-ch: front/surr => OK as it is
2547 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02002548 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002549 */
2550 switch (cfg->line_outs) {
2551 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002552 case 4:
2553 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02002554 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002555 cfg->line_out_pins[2] = nid;
2556 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002557 }
2558
Takashi Iwai82bc9552006-03-21 11:24:42 +01002559 /*
2560 * debug prints of the parsed results
2561 */
2562 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2563 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2564 cfg->line_out_pins[2], cfg->line_out_pins[3],
2565 cfg->line_out_pins[4]);
2566 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2567 cfg->speaker_outs, cfg->speaker_pins[0],
2568 cfg->speaker_pins[1], cfg->speaker_pins[2],
2569 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002570 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2571 cfg->hp_outs, cfg->hp_pins[0],
2572 cfg->hp_pins[1], cfg->hp_pins[2],
2573 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002574 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2575 " cd=0x%x, aux=0x%x\n",
2576 cfg->input_pins[AUTO_PIN_MIC],
2577 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2578 cfg->input_pins[AUTO_PIN_LINE],
2579 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2580 cfg->input_pins[AUTO_PIN_CD],
2581 cfg->input_pins[AUTO_PIN_AUX]);
2582
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002583 return 0;
2584}
2585
Takashi Iwai4a471b72005-12-07 13:56:29 +01002586/* labels for input pins */
2587const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2588 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2589};
2590
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592#ifdef CONFIG_PM
2593/*
2594 * power management
2595 */
2596
2597/**
2598 * snd_hda_suspend - suspend the codecs
2599 * @bus: the HDA bus
2600 * @state: suspsend state
2601 *
2602 * Returns 0 if successful.
2603 */
2604int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2605{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 /* FIXME: should handle power widget capabilities */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (codec->patch_ops.suspend)
2611 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002612 hda_set_power_state(codec,
2613 codec->afg ? codec->afg : codec->mfg,
2614 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616 return 0;
2617}
2618
2619/**
2620 * snd_hda_resume - resume the codecs
2621 * @bus: the HDA bus
2622 * @state: resume state
2623 *
2624 * Returns 0 if successful.
2625 */
2626int snd_hda_resume(struct hda_bus *bus)
2627{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002628 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Takashi Iwai0ba21762007-04-16 11:29:14 +02002630 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002631 hda_set_power_state(codec,
2632 codec->afg ? codec->afg : codec->mfg,
2633 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (codec->patch_ops.resume)
2635 codec->patch_ops.resume(codec);
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002636 else {
2637 codec->patch_ops.init(codec);
2638 snd_hda_codec_resume_amp(codec);
2639 snd_hda_codec_resume_cache(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return 0;
2643}
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645#endif