blob: 36879a93eac477091ec2beb5991b84b75cf9a71d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * find a matching codec preset
393 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200394static const struct hda_codec_preset __devinit *
395find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 const struct hda_codec_preset **tbl, *preset;
398
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100399 if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
400 return NULL; /* use the generic parser */
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 for (tbl = hda_preset_tables; *tbl; tbl++) {
403 for (preset = *tbl; preset->id; preset++) {
404 u32 mask = preset->mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200405 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200407 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200408 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200409 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return preset;
411 }
412 }
413 return NULL;
414}
415
416/*
417 * snd_hda_get_codec_name - store the codec name
418 */
419void snd_hda_get_codec_name(struct hda_codec *codec,
420 char *name, int namelen)
421{
422 const struct hda_vendor_id *c;
423 const char *vendor = NULL;
424 u16 vendor_id = codec->vendor_id >> 16;
425 char tmp[16];
426
427 for (c = hda_vendor_ids; c->id; c++) {
428 if (c->id == vendor_id) {
429 vendor = c->name;
430 break;
431 }
432 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200433 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sprintf(tmp, "Generic %04x", vendor_id);
435 vendor = tmp;
436 }
437 if (codec->preset && codec->preset->name)
438 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
439 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200440 snprintf(name, namelen, "%s ID %x", vendor,
441 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200445 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200447static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int i, total_nodes;
450 hda_nid_t nid;
451
452 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
453 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200454 unsigned int func;
455 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
456 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200457 case AC_GRP_AUDIO_FUNCTION:
458 codec->afg = nid;
459 break;
460 case AC_GRP_MODEM_FUNCTION:
461 codec->mfg = nid;
462 break;
463 default:
464 break;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100470 * read widget caps for each widget and store in cache
471 */
472static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
473{
474 int i;
475 hda_nid_t nid;
476
477 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
478 &codec->start_nid);
479 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200480 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 return -ENOMEM;
482 nid = codec->start_nid;
483 for (i = 0; i < codec->num_nodes; i++, nid++)
484 codec->wcaps[i] = snd_hda_param_read(codec, nid,
485 AC_PAR_AUDIO_WIDGET_CAP);
486 return 0;
487}
488
489
490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * codec destructor
492 */
493static void snd_hda_codec_free(struct hda_codec *codec)
494{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200495 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
497 list_del(&codec->list);
498 codec->bus->caddr_tbl[codec->addr] = NULL;
499 if (codec->patch_ops.free)
500 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100501 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100502 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 kfree(codec);
504}
505
506static void init_amp_hash(struct hda_codec *codec);
507
508/**
509 * snd_hda_codec_new - create a HDA codec
510 * @bus: the bus to assign
511 * @codec_addr: the codec address
512 * @codecp: the pointer to store the generated codec
513 *
514 * Returns 0 if successful, or a negative error code.
515 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200516int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
517 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct hda_codec *codec;
520 char component[13];
521 int err;
522
523 snd_assert(bus, return -EINVAL);
524 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
525
526 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200527 snd_printk(KERN_ERR "hda_codec: "
528 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EBUSY;
530 }
531
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200532 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (codec == NULL) {
534 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
535 return -ENOMEM;
536 }
537
538 codec->bus = bus;
539 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100540 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 init_amp_hash(codec);
542
543 list_add_tail(&codec->list, &bus->codec_list);
544 bus->caddr_tbl[codec_addr] = codec;
545
Takashi Iwai0ba21762007-04-16 11:29:14 +0200546 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
547 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100548 if (codec->vendor_id == -1)
549 /* read again, hopefully the access method was corrected
550 * in the last read...
551 */
552 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
553 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200554 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
555 AC_PAR_SUBSYSTEM_ID);
556 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
557 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200559 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200560 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200561 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 snd_hda_codec_free(codec);
563 return -ENODEV;
564 }
565
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
567 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
568 snd_hda_codec_free(codec);
569 return -ENOMEM;
570 }
571
Takashi Iwai0ba21762007-04-16 11:29:14 +0200572 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200573 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 codec->subsystem_id =
575 snd_hda_codec_read(codec, nid, 0,
576 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200577 }
578
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100579 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200580 /* audio codec should override the mixer name */
581 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 snd_hda_get_codec_name(codec, bus->card->mixername,
583 sizeof(bus->card->mixername));
584
585 if (codec->preset && codec->preset->patch)
586 err = codec->preset->patch(codec);
587 else
588 err = snd_hda_parse_generic_codec(codec);
589 if (err < 0) {
590 snd_hda_codec_free(codec);
591 return err;
592 }
593
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100594 if (codec->patch_ops.unsol_event)
595 init_unsol_queue(bus);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200598#ifdef CONFIG_SND_HDA_HWDEP
599 snd_hda_create_hwdep(codec);
600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 sprintf(component, "HDA:%08x", codec->vendor_id);
603 snd_component_add(codec->bus->card, component);
604
605 if (codecp)
606 *codecp = codec;
607 return 0;
608}
609
610/**
611 * snd_hda_codec_setup_stream - set up the codec for streaming
612 * @codec: the CODEC to set up
613 * @nid: the NID to set up
614 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
615 * @channel_id: channel id to pass, zero based.
616 * @format: stream format.
617 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200618void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
619 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int channel_id, int format)
621{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200622 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200623 return;
624
Takashi Iwai0ba21762007-04-16 11:29:14 +0200625 snd_printdd("hda_codec_setup_stream: "
626 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 nid, stream_tag, channel_id, format);
628 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
629 (stream_tag << 4) | channel_id);
630 msleep(1);
631 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * amp access functions
636 */
637
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200638/* FIXME: more better hash key? */
639#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200641#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643/* initialize the hash table */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200644static void __devinit init_amp_hash(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
647 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100648 codec->amp_info_size = 0;
649 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/* query the hash. allocate an entry if not found. */
653static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
654{
655 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
656 u16 cur = codec->amp_hash[idx];
657 struct hda_amp_info *info;
658
659 while (cur != 0xffff) {
660 info = &codec->amp_info[cur];
661 if (info->key == key)
662 return info;
663 cur = info->next;
664 }
665
666 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100667 if (codec->num_amp_entries >= codec->amp_info_size) {
668 /* reallocate the array */
669 int new_size = codec->amp_info_size + 64;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200670 struct hda_amp_info *new_info;
671 new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
672 GFP_KERNEL);
673 if (!new_info) {
674 snd_printk(KERN_ERR "hda_codec: "
675 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100676 return NULL;
677 }
678 if (codec->amp_info) {
679 memcpy(new_info, codec->amp_info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200680 codec->amp_info_size *
681 sizeof(struct hda_amp_info));
Takashi Iwaid0311662005-11-07 14:38:44 +0100682 kfree(codec->amp_info);
683 }
684 codec->amp_info_size = new_size;
685 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 cur = codec->num_amp_entries++;
688 info = &codec->amp_info[cur];
689 info->key = key;
690 info->status = 0; /* not initialized yet */
691 info->next = codec->amp_hash[idx];
692 codec->amp_hash[idx] = cur;
693
694 return info;
695}
696
697/*
698 * query AMP capabilities for the given widget and direction
699 */
700static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
701{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200702 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Takashi Iwai0ba21762007-04-16 11:29:14 +0200704 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
705 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200707 if (!(info->status & INFO_AMP_CAPS)) {
708 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200710 info->amp_caps = snd_hda_param_read(codec, nid,
711 direction == HDA_OUTPUT ?
712 AC_PAR_AMP_OUT_CAP :
713 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200714 if (info->amp_caps)
715 info->status |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 return info->amp_caps;
718}
719
Takashi Iwai897cc182007-05-29 19:01:37 +0200720int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
721 unsigned int caps)
722{
723 struct hda_amp_info *info;
724
725 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
726 if (!info)
727 return -EINVAL;
728 info->amp_caps = caps;
729 info->status |= INFO_AMP_CAPS;
730 return 0;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200735 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200737static unsigned int get_vol_mute(struct hda_codec *codec,
738 struct hda_amp_info *info, hda_nid_t nid,
739 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 u32 val, parm;
742
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200743 if (info->status & INFO_AMP_VOL(ch))
744 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
747 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
748 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200749 val = snd_hda_codec_read(codec, nid, 0,
750 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200752 info->status |= INFO_AMP_VOL(ch);
753 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200757 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200759static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 hda_nid_t nid, int ch, int direction, int index,
761 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 u32 parm;
764
765 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
766 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
767 parm |= index << AC_AMP_SET_INDEX_SHIFT;
768 parm |= val;
769 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200770 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200774 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Takashi Iwai834be882006-03-01 14:16:17 +0100776int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
777 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200779 struct hda_amp_info *info;
780 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
781 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200783 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200786/*
787 * update the AMP value, mask = bit mask to set, val = the value
788 */
Takashi Iwai834be882006-03-01 14:16:17 +0100789int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
790 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200792 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200793
Takashi Iwai0ba21762007-04-16 11:29:14 +0200794 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
795 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200797 val &= mask;
798 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200799 if (info->vol[ch] == val && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200801 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 1;
803}
804
805
806/*
807 * AMP control callbacks
808 */
809/* retrieve parameters from private_value */
810#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
811#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
812#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
813#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
814
815/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200816int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
817 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
820 u16 nid = get_amp_nid(kcontrol);
821 u8 chs = get_amp_channels(kcontrol);
822 int dir = get_amp_direction(kcontrol);
823 u32 caps;
824
825 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200826 /* num steps */
827 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
828 if (!caps) {
829 printk(KERN_WARNING "hda_codec: "
830 "num_steps = 0 for NID=0x%x\n", nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -EINVAL;
832 }
833 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
834 uinfo->count = chs == 3 ? 2 : 1;
835 uinfo->value.integer.min = 0;
836 uinfo->value.integer.max = caps;
837 return 0;
838}
839
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
841 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
844 hda_nid_t nid = get_amp_nid(kcontrol);
845 int chs = get_amp_channels(kcontrol);
846 int dir = get_amp_direction(kcontrol);
847 int idx = get_amp_index(kcontrol);
848 long *valp = ucontrol->value.integer.value;
849
850 if (chs & 1)
851 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
852 if (chs & 2)
853 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
854 return 0;
855}
856
Takashi Iwai0ba21762007-04-16 11:29:14 +0200857int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
861 hda_nid_t nid = get_amp_nid(kcontrol);
862 int chs = get_amp_channels(kcontrol);
863 int dir = get_amp_direction(kcontrol);
864 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 long *valp = ucontrol->value.integer.value;
866 int change = 0;
867
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200868 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200869 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
870 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200871 valp++;
872 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200873 if (chs & 2)
874 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200875 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return change;
877}
878
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200879int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
880 unsigned int size, unsigned int __user *_tlv)
881{
882 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
883 hda_nid_t nid = get_amp_nid(kcontrol);
884 int dir = get_amp_direction(kcontrol);
885 u32 caps, val1, val2;
886
887 if (size < 4 * sizeof(unsigned int))
888 return -ENOMEM;
889 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200890 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
891 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200892 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
893 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200894 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
895 return -EFAULT;
896 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
897 return -EFAULT;
898 if (put_user(val1, _tlv + 2))
899 return -EFAULT;
900 if (put_user(val2, _tlv + 3))
901 return -EFAULT;
902 return 0;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
907 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int chs = get_amp_channels(kcontrol);
910
911 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
912 uinfo->count = chs == 3 ? 2 : 1;
913 uinfo->value.integer.min = 0;
914 uinfo->value.integer.max = 1;
915 return 0;
916}
917
Takashi Iwai0ba21762007-04-16 11:29:14 +0200918int snd_hda_mixer_amp_switch_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 Iwai0ba21762007-04-16 11:29:14 +0200929 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
930 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +0200932 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
933 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
Takashi Iwai0ba21762007-04-16 11:29:14 +0200937int snd_hda_mixer_amp_switch_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 0x80, *valp ? 0 : 0x80);
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 0x80, *valp ? 0 : 0x80);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return change;
958}
959
960/*
Takashi Iwai985be542005-11-02 18:26:49 +0100961 * bound volume controls
962 *
963 * bind multiple volumes (# indices, from 0)
964 */
965
966#define AMP_VAL_IDX_SHIFT 19
967#define AMP_VAL_IDX_MASK (0x0f<<19)
968
Takashi Iwai0ba21762007-04-16 11:29:14 +0200969int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100971{
972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
973 unsigned long pval;
974 int err;
975
Ingo Molnar62932df2006-01-16 16:34:20 +0100976 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100977 pval = kcontrol->private_value;
978 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
979 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
980 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100981 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100982 return err;
983}
984
Takashi Iwai0ba21762007-04-16 11:29:14 +0200985int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
986 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100987{
988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
989 unsigned long pval;
990 int i, indices, err = 0, change = 0;
991
Ingo Molnar62932df2006-01-16 16:34:20 +0100992 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100993 pval = kcontrol->private_value;
994 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
995 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200996 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
997 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +0100998 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
999 if (err < 0)
1000 break;
1001 change |= err;
1002 }
1003 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001004 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001005 return err < 0 ? err : change;
1006}
1007
1008/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001009 * generic bound volume/swtich controls
1010 */
1011int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1012 struct snd_ctl_elem_info *uinfo)
1013{
1014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1015 struct hda_bind_ctls *c;
1016 int err;
1017
1018 c = (struct hda_bind_ctls *)kcontrol->private_value;
1019 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1020 kcontrol->private_value = *c->values;
1021 err = c->ops->info(kcontrol, uinfo);
1022 kcontrol->private_value = (long)c;
1023 mutex_unlock(&codec->spdif_mutex);
1024 return err;
1025}
1026
1027int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1028 struct snd_ctl_elem_value *ucontrol)
1029{
1030 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1031 struct hda_bind_ctls *c;
1032 int err;
1033
1034 c = (struct hda_bind_ctls *)kcontrol->private_value;
1035 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1036 kcontrol->private_value = *c->values;
1037 err = c->ops->get(kcontrol, ucontrol);
1038 kcontrol->private_value = (long)c;
1039 mutex_unlock(&codec->spdif_mutex);
1040 return err;
1041}
1042
1043int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
1045{
1046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1047 struct hda_bind_ctls *c;
1048 unsigned long *vals;
1049 int err = 0, change = 0;
1050
1051 c = (struct hda_bind_ctls *)kcontrol->private_value;
1052 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1053 for (vals = c->values; *vals; vals++) {
1054 kcontrol->private_value = *vals;
1055 err = c->ops->put(kcontrol, ucontrol);
1056 if (err < 0)
1057 break;
1058 change |= err;
1059 }
1060 kcontrol->private_value = (long)c;
1061 mutex_unlock(&codec->spdif_mutex);
1062 return err < 0 ? err : change;
1063}
1064
1065int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1066 unsigned int size, unsigned int __user *tlv)
1067{
1068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1069 struct hda_bind_ctls *c;
1070 int err;
1071
1072 c = (struct hda_bind_ctls *)kcontrol->private_value;
1073 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1074 kcontrol->private_value = *c->values;
1075 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1076 kcontrol->private_value = (long)c;
1077 mutex_unlock(&codec->spdif_mutex);
1078 return err;
1079}
1080
1081struct hda_ctl_ops snd_hda_bind_vol = {
1082 .info = snd_hda_mixer_amp_volume_info,
1083 .get = snd_hda_mixer_amp_volume_get,
1084 .put = snd_hda_mixer_amp_volume_put,
1085 .tlv = snd_hda_mixer_amp_tlv
1086};
1087
1088struct hda_ctl_ops snd_hda_bind_sw = {
1089 .info = snd_hda_mixer_amp_switch_info,
1090 .get = snd_hda_mixer_amp_switch_get,
1091 .put = snd_hda_mixer_amp_switch_put,
1092 .tlv = snd_hda_mixer_amp_tlv
1093};
1094
1095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * SPDIF out controls
1097 */
1098
Takashi Iwai0ba21762007-04-16 11:29:14 +02001099static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1103 uinfo->count = 1;
1104 return 0;
1105}
1106
Takashi Iwai0ba21762007-04-16 11:29:14 +02001107static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1108 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1111 IEC958_AES0_NONAUDIO |
1112 IEC958_AES0_CON_EMPHASIS_5015 |
1113 IEC958_AES0_CON_NOT_COPYRIGHT;
1114 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1115 IEC958_AES1_CON_ORIGINAL;
1116 return 0;
1117}
1118
Takashi Iwai0ba21762007-04-16 11:29:14 +02001119static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1120 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1123 IEC958_AES0_NONAUDIO |
1124 IEC958_AES0_PRO_EMPHASIS_5015;
1125 return 0;
1126}
1127
Takashi Iwai0ba21762007-04-16 11:29:14 +02001128static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1129 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1132
1133 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1134 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1135 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1136 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1137
1138 return 0;
1139}
1140
1141/* convert from SPDIF status bits to HDA SPDIF bits
1142 * bit 0 (DigEn) is always set zero (to be filled later)
1143 */
1144static unsigned short convert_from_spdif_status(unsigned int sbits)
1145{
1146 unsigned short val = 0;
1147
1148 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001151 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001153 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1154 IEC958_AES0_PRO_EMPHASIS_5015)
1155 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001157 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1158 IEC958_AES0_CON_EMPHASIS_5015)
1159 val |= AC_DIG1_EMPHASIS;
1160 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1161 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001163 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1165 }
1166 return val;
1167}
1168
1169/* convert to SPDIF status bits from HDA SPDIF bits
1170 */
1171static unsigned int convert_to_spdif_status(unsigned short val)
1172{
1173 unsigned int sbits = 0;
1174
Takashi Iwai0ba21762007-04-16 11:29:14 +02001175 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001177 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 sbits |= IEC958_AES0_PROFESSIONAL;
1179 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001180 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1182 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001183 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001187 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1189 sbits |= val & (0x7f << 8);
1190 }
1191 return sbits;
1192}
1193
Takashi Iwai0ba21762007-04-16 11:29:14 +02001194static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1198 hda_nid_t nid = kcontrol->private_value;
1199 unsigned short val;
1200 int change;
1201
Ingo Molnar62932df2006-01-16 16:34:20 +01001202 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 codec->spdif_status = ucontrol->value.iec958.status[0] |
1204 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1205 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1206 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1207 val = convert_from_spdif_status(codec->spdif_status);
1208 val |= codec->spdif_ctls & 1;
1209 change = codec->spdif_ctls != val;
1210 codec->spdif_ctls = val;
1211
1212 if (change || codec->in_resume) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001213 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1214 val & 0xff);
1215 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1216 val >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
Ingo Molnar62932df2006-01-16 16:34:20 +01001219 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return change;
1221}
1222
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001223#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Takashi Iwai0ba21762007-04-16 11:29:14 +02001225static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1226 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1229
Takashi Iwai0ba21762007-04-16 11:29:14 +02001230 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232}
1233
Takashi Iwai0ba21762007-04-16 11:29:14 +02001234static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1235 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1238 hda_nid_t nid = kcontrol->private_value;
1239 unsigned short val;
1240 int change;
1241
Ingo Molnar62932df2006-01-16 16:34:20 +01001242 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001245 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 change = codec->spdif_ctls != val;
1247 if (change || codec->in_resume) {
1248 codec->spdif_ctls = val;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001249 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1250 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001251 /* unmute amp switch (if any) */
1252 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1253 (val & AC_DIG1_ENABLE))
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001254 snd_hda_codec_write(codec, nid, 0,
1255 AC_VERB_SET_AMP_GAIN_MUTE,
1256 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
Takashi Iwai0ba21762007-04-16 11:29:14 +02001257 AC_AMP_SET_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001259 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return change;
1261}
1262
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001263static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 {
1265 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1266 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1267 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1268 .info = snd_hda_spdif_mask_info,
1269 .get = snd_hda_spdif_cmask_get,
1270 },
1271 {
1272 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1274 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1275 .info = snd_hda_spdif_mask_info,
1276 .get = snd_hda_spdif_pmask_get,
1277 },
1278 {
1279 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1280 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1281 .info = snd_hda_spdif_mask_info,
1282 .get = snd_hda_spdif_default_get,
1283 .put = snd_hda_spdif_default_put,
1284 },
1285 {
1286 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1287 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1288 .info = snd_hda_spdif_out_switch_info,
1289 .get = snd_hda_spdif_out_switch_get,
1290 .put = snd_hda_spdif_out_switch_put,
1291 },
1292 { } /* end */
1293};
1294
1295/**
1296 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1297 * @codec: the HDA codec
1298 * @nid: audio out widget NID
1299 *
1300 * Creates controls related with the SPDIF output.
1301 * Called from each patch supporting the SPDIF out.
1302 *
1303 * Returns 0 if successful, or a negative error code.
1304 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001305int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
1306 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001309 struct snd_kcontrol *kctl;
1310 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1313 kctl = snd_ctl_new1(dig_mix, codec);
1314 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 err = snd_ctl_add(codec->bus->card, kctl);
1316 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return err;
1318 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319 codec->spdif_ctls =
1320 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1322 return 0;
1323}
1324
1325/*
1326 * SPDIF input
1327 */
1328
1329#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1330
Takashi Iwai0ba21762007-04-16 11:29:14 +02001331static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1335
1336 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1337 return 0;
1338}
1339
Takashi Iwai0ba21762007-04-16 11:29:14 +02001340static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1341 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1344 hda_nid_t nid = kcontrol->private_value;
1345 unsigned int val = !!ucontrol->value.integer.value[0];
1346 int change;
1347
Ingo Molnar62932df2006-01-16 16:34:20 +01001348 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 change = codec->spdif_in_enable != val;
1350 if (change || codec->in_resume) {
1351 codec->spdif_in_enable = val;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001352 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1353 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001355 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return change;
1357}
1358
Takashi Iwai0ba21762007-04-16 11:29:14 +02001359static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1360 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1363 hda_nid_t nid = kcontrol->private_value;
1364 unsigned short val;
1365 unsigned int sbits;
1366
1367 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1368 sbits = convert_to_spdif_status(val);
1369 ucontrol->value.iec958.status[0] = sbits;
1370 ucontrol->value.iec958.status[1] = sbits >> 8;
1371 ucontrol->value.iec958.status[2] = sbits >> 16;
1372 ucontrol->value.iec958.status[3] = sbits >> 24;
1373 return 0;
1374}
1375
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001376static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 {
1378 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1379 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1380 .info = snd_hda_spdif_in_switch_info,
1381 .get = snd_hda_spdif_in_switch_get,
1382 .put = snd_hda_spdif_in_switch_put,
1383 },
1384 {
1385 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1387 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1388 .info = snd_hda_spdif_mask_info,
1389 .get = snd_hda_spdif_in_status_get,
1390 },
1391 { } /* end */
1392};
1393
1394/**
1395 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1396 * @codec: the HDA codec
1397 * @nid: audio in widget NID
1398 *
1399 * Creates controls related with the SPDIF input.
1400 * Called from each patch supporting the SPDIF in.
1401 *
1402 * Returns 0 if successful, or a negative error code.
1403 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001404int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
1405 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001408 struct snd_kcontrol *kctl;
1409 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1412 kctl = snd_ctl_new1(dig_mix, codec);
1413 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414 err = snd_ctl_add(codec->bus->card, kctl);
1415 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return err;
1417 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 codec->spdif_in_enable =
1419 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1420 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return 0;
1422}
1423
1424
Takashi Iwai54d17402005-11-21 16:33:22 +01001425/*
1426 * set power state of the codec
1427 */
1428static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1429 unsigned int power_state)
1430{
1431 hda_nid_t nid, nid_start;
1432 int nodes;
1433
1434 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1435 power_state);
1436
1437 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1438 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1439 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1440 snd_hda_codec_write(codec, nid, 0,
1441 AC_VERB_SET_POWER_STATE,
1442 power_state);
1443 }
1444
1445 if (power_state == AC_PWRST_D0)
1446 msleep(10);
1447}
1448
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/**
1451 * snd_hda_build_controls - build mixer controls
1452 * @bus: the BUS
1453 *
1454 * Creates mixer controls for each codec included in the bus.
1455 *
1456 * Returns 0 if successful, otherwise a negative error code.
1457 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001458int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001460 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 /* build controls */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001463 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001465 if (!codec->patch_ops.build_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 continue;
1467 err = codec->patch_ops.build_controls(codec);
1468 if (err < 0)
1469 return err;
1470 }
1471
1472 /* initialize */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001473 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001475 hda_set_power_state(codec,
1476 codec->afg ? codec->afg : codec->mfg,
1477 AC_PWRST_D0);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001478 if (!codec->patch_ops.init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 continue;
1480 err = codec->patch_ops.init(codec);
1481 if (err < 0)
1482 return err;
1483 }
1484 return 0;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/*
1488 * stream formats
1489 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001490struct hda_rate_tbl {
1491 unsigned int hz;
1492 unsigned int alsa_bits;
1493 unsigned int hda_fmt;
1494};
1495
1496static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001498
1499 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1501 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1502 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1503 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1504 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1505 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1506 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1507 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1508 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1509 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1510 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001511#define AC_PAR_PCM_RATE_BITS 11
1512 /* up to bits 10, 384kHZ isn't supported properly */
1513
1514 /* not autodetected value */
1515 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001516
Takashi Iwaibefdf312005-08-22 13:57:55 +02001517 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518};
1519
1520/**
1521 * snd_hda_calc_stream_format - calculate format bitset
1522 * @rate: the sample rate
1523 * @channels: the number of channels
1524 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1525 * @maxbps: the max. bps
1526 *
1527 * Calculate the format bitset from the given rate, channels and th PCM format.
1528 *
1529 * Return zero if invalid.
1530 */
1531unsigned int snd_hda_calc_stream_format(unsigned int rate,
1532 unsigned int channels,
1533 unsigned int format,
1534 unsigned int maxbps)
1535{
1536 int i;
1537 unsigned int val = 0;
1538
Takashi Iwaibefdf312005-08-22 13:57:55 +02001539 for (i = 0; rate_bits[i].hz; i++)
1540 if (rate_bits[i].hz == rate) {
1541 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001544 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 snd_printdd("invalid rate %d\n", rate);
1546 return 0;
1547 }
1548
1549 if (channels == 0 || channels > 8) {
1550 snd_printdd("invalid channels %d\n", channels);
1551 return 0;
1552 }
1553 val |= channels - 1;
1554
1555 switch (snd_pcm_format_width(format)) {
1556 case 8: val |= 0x00; break;
1557 case 16: val |= 0x10; break;
1558 case 20:
1559 case 24:
1560 case 32:
1561 if (maxbps >= 32)
1562 val |= 0x40;
1563 else if (maxbps >= 24)
1564 val |= 0x30;
1565 else
1566 val |= 0x20;
1567 break;
1568 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569 snd_printdd("invalid format width %d\n",
1570 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572 }
1573
1574 return val;
1575}
1576
1577/**
1578 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1579 * @codec: the HDA codec
1580 * @nid: NID to query
1581 * @ratesp: the pointer to store the detected rate bitflags
1582 * @formatsp: the pointer to store the detected formats
1583 * @bpsp: the pointer to store the detected format widths
1584 *
1585 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1586 * or @bsps argument is ignored.
1587 *
1588 * Returns 0 if successful, otherwise a negative error code.
1589 */
1590int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1591 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1592{
1593 int i;
1594 unsigned int val, streams;
1595
1596 val = 0;
1597 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001598 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1600 if (val == -1)
1601 return -EIO;
1602 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001603 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1605
1606 if (ratesp) {
1607 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001608 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001610 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 *ratesp = rates;
1613 }
1614
1615 if (formatsp || bpsp) {
1616 u64 formats = 0;
1617 unsigned int bps;
1618 unsigned int wcaps;
1619
Takashi Iwai54d17402005-11-21 16:33:22 +01001620 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1622 if (streams == -1)
1623 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 if (!streams) {
1625 streams = snd_hda_param_read(codec, codec->afg,
1626 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (streams == -1)
1628 return -EIO;
1629 }
1630
1631 bps = 0;
1632 if (streams & AC_SUPFMT_PCM) {
1633 if (val & AC_SUPPCM_BITS_8) {
1634 formats |= SNDRV_PCM_FMTBIT_U8;
1635 bps = 8;
1636 }
1637 if (val & AC_SUPPCM_BITS_16) {
1638 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1639 bps = 16;
1640 }
1641 if (wcaps & AC_WCAP_DIGITAL) {
1642 if (val & AC_SUPPCM_BITS_32)
1643 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1644 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1645 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1646 if (val & AC_SUPPCM_BITS_24)
1647 bps = 24;
1648 else if (val & AC_SUPPCM_BITS_20)
1649 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001650 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1651 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1653 if (val & AC_SUPPCM_BITS_32)
1654 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 else if (val & AC_SUPPCM_BITS_24)
1656 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02001657 else if (val & AC_SUPPCM_BITS_20)
1658 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001661 else if (streams == AC_SUPFMT_FLOAT32) {
1662 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1664 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001665 } else if (streams == AC_SUPFMT_AC3) {
1666 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* temporary hack: we have still no proper support
1668 * for the direct AC3 stream...
1669 */
1670 formats |= SNDRV_PCM_FMTBIT_U8;
1671 bps = 8;
1672 }
1673 if (formatsp)
1674 *formatsp = formats;
1675 if (bpsp)
1676 *bpsp = bps;
1677 }
1678
1679 return 0;
1680}
1681
1682/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02001683 * snd_hda_is_supported_format - check whether the given node supports
1684 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
1686 * Returns 1 if supported, 0 if not.
1687 */
1688int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1689 unsigned int format)
1690{
1691 int i;
1692 unsigned int val = 0, rate, stream;
1693
1694 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001695 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1697 if (val == -1)
1698 return 0;
1699 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001700 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1702 if (val == -1)
1703 return 0;
1704 }
1705
1706 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001707 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02001708 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (val & (1 << i))
1710 break;
1711 return 0;
1712 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001713 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715
1716 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1717 if (stream == -1)
1718 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001719 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001721 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723
1724 if (stream & AC_SUPFMT_PCM) {
1725 switch (format & 0xf0) {
1726 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001727 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return 0;
1729 break;
1730 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001731 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 0;
1733 break;
1734 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001735 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
1737 break;
1738 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001739 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return 0;
1741 break;
1742 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001743 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return 0;
1745 break;
1746 default:
1747 return 0;
1748 }
1749 } else {
1750 /* FIXME: check for float32 and AC3? */
1751 }
1752
1753 return 1;
1754}
1755
1756/*
1757 * PCM stuff
1758 */
1759static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1760 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001761 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 return 0;
1764}
1765
1766static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1767 struct hda_codec *codec,
1768 unsigned int stream_tag,
1769 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001770 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1773 return 0;
1774}
1775
1776static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1777 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001778 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1781 return 0;
1782}
1783
Takashi Iwai0ba21762007-04-16 11:29:14 +02001784static int __devinit set_pcm_default_values(struct hda_codec *codec,
1785 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787 /* query support PCM information from the given NID */
1788 if (info->nid && (!info->rates || !info->formats)) {
1789 snd_hda_query_supported_pcm(codec, info->nid,
1790 info->rates ? NULL : &info->rates,
1791 info->formats ? NULL : &info->formats,
1792 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 if (info->ops.open == NULL)
1795 info->ops.open = hda_pcm_default_open_close;
1796 if (info->ops.close == NULL)
1797 info->ops.close = hda_pcm_default_open_close;
1798 if (info->ops.prepare == NULL) {
1799 snd_assert(info->nid, return -EINVAL);
1800 info->ops.prepare = hda_pcm_default_prepare;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (info->ops.cleanup == NULL) {
1803 snd_assert(info->nid, return -EINVAL);
1804 info->ops.cleanup = hda_pcm_default_cleanup;
1805 }
1806 return 0;
1807}
1808
1809/**
1810 * snd_hda_build_pcms - build PCM information
1811 * @bus: the BUS
1812 *
1813 * Create PCM information for each codec included in the bus.
1814 *
1815 * The build_pcms codec patch is requested to set up codec->num_pcms and
1816 * codec->pcm_info properly. The array is referred by the top-level driver
1817 * to create its PCM instances.
1818 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1819 * callback.
1820 *
1821 * At least, substreams, channels_min and channels_max must be filled for
1822 * each stream. substreams = 0 indicates that the stream doesn't exist.
1823 * When rates and/or formats are zero, the supported values are queried
1824 * from the given nid. The nid is used also by the default ops.prepare
1825 * and ops.cleanup callbacks.
1826 *
1827 * The driver needs to call ops.open in its open callback. Similarly,
1828 * ops.close is supposed to be called in the close callback.
1829 * ops.prepare should be called in the prepare or hw_params callback
1830 * with the proper parameters for set up.
1831 * ops.cleanup should be called in hw_free for clean up of streams.
1832 *
1833 * This function returns 0 if successfull, or a negative error code.
1834 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001835int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001837 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Takashi Iwai0ba21762007-04-16 11:29:14 +02001839 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 unsigned int pcm, s;
1841 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001842 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 continue;
1844 err = codec->patch_ops.build_pcms(codec);
1845 if (err < 0)
1846 return err;
1847 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1848 for (s = 0; s < 2; s++) {
1849 struct hda_pcm_stream *info;
1850 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02001851 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 continue;
1853 err = set_pcm_default_values(codec, info);
1854 if (err < 0)
1855 return err;
1856 }
1857 }
1858 }
1859 return 0;
1860}
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862/**
1863 * snd_hda_check_board_config - compare the current codec with the config table
1864 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001865 * @num_configs: number of config enums
1866 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * @tbl: configuration table, terminated by null entries
1868 *
1869 * Compares the modelname or PCI subsystem id of the current codec with the
1870 * given configuration table. If a matching entry is found, returns its
1871 * config value (supposed to be 0 or positive).
1872 *
1873 * If no entries are matching, the function returns a negative value.
1874 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001875int __devinit snd_hda_check_board_config(struct hda_codec *codec,
1876 int num_configs, const char **models,
1877 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001879 if (codec->bus->modelname && models) {
1880 int i;
1881 for (i = 0; i < num_configs; i++) {
1882 if (models[i] &&
1883 !strcmp(codec->bus->modelname, models[i])) {
1884 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1885 "selected\n", models[i]);
1886 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888 }
1889 }
1890
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001891 if (!codec->bus->pci || !tbl)
1892 return -1;
1893
1894 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1895 if (!tbl)
1896 return -1;
1897 if (tbl->value >= 0 && tbl->value < num_configs) {
1898#ifdef CONFIG_SND_DEBUG_DETECT
1899 char tmp[10];
1900 const char *model = NULL;
1901 if (models)
1902 model = models[tbl->value];
1903 if (!model) {
1904 sprintf(tmp, "#%d", tbl->value);
1905 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001907 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1908 "for config %x:%x (%s)\n",
1909 model, tbl->subvendor, tbl->subdevice,
1910 (tbl->name ? tbl->name : "Unknown device"));
1911#endif
1912 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 return -1;
1915}
1916
1917/**
1918 * snd_hda_add_new_ctls - create controls from the array
1919 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001920 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *
1922 * This helper function creates and add new controls in the given array.
1923 * The array must be terminated with an empty entry as terminator.
1924 *
1925 * Returns 0 if successful, or a negative error code.
1926 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001927int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
1928 struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int err;
1931
1932 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001933 struct snd_kcontrol *kctl;
1934 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001935 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001936 return -ENOMEM;
1937 err = snd_ctl_add(codec->bus->card, kctl);
1938 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001939 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01001940 return err;
1941 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001942 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001943 return -ENOMEM;
1944 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001945 err = snd_ctl_add(codec->bus->card, kctl);
1946 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01001947 return err;
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 return 0;
1951}
1952
1953
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001954/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001955 * Channel mode helper
1956 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001957int snd_hda_ch_mode_info(struct hda_codec *codec,
1958 struct snd_ctl_elem_info *uinfo,
1959 const struct hda_channel_mode *chmode,
1960 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001961{
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1963 uinfo->count = 1;
1964 uinfo->value.enumerated.items = num_chmodes;
1965 if (uinfo->value.enumerated.item >= num_chmodes)
1966 uinfo->value.enumerated.item = num_chmodes - 1;
1967 sprintf(uinfo->value.enumerated.name, "%dch",
1968 chmode[uinfo->value.enumerated.item].channels);
1969 return 0;
1970}
1971
Takashi Iwai0ba21762007-04-16 11:29:14 +02001972int snd_hda_ch_mode_get(struct hda_codec *codec,
1973 struct snd_ctl_elem_value *ucontrol,
1974 const struct hda_channel_mode *chmode,
1975 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001976 int max_channels)
1977{
1978 int i;
1979
1980 for (i = 0; i < num_chmodes; i++) {
1981 if (max_channels == chmode[i].channels) {
1982 ucontrol->value.enumerated.item[0] = i;
1983 break;
1984 }
1985 }
1986 return 0;
1987}
1988
Takashi Iwai0ba21762007-04-16 11:29:14 +02001989int snd_hda_ch_mode_put(struct hda_codec *codec,
1990 struct snd_ctl_elem_value *ucontrol,
1991 const struct hda_channel_mode *chmode,
1992 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001993 int *max_channelsp)
1994{
1995 unsigned int mode;
1996
1997 mode = ucontrol->value.enumerated.item[0];
1998 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001999 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002000 return 0;
2001 /* change the current channel setting */
2002 *max_channelsp = chmode[mode].channels;
2003 if (chmode[mode].sequence)
2004 snd_hda_sequence_write(codec, chmode[mode].sequence);
2005 return 1;
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/*
2009 * input MUX helper
2010 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002011int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2012 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 unsigned int index;
2015
2016 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2017 uinfo->count = 1;
2018 uinfo->value.enumerated.items = imux->num_items;
2019 index = uinfo->value.enumerated.item;
2020 if (index >= imux->num_items)
2021 index = imux->num_items - 1;
2022 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2023 return 0;
2024}
2025
Takashi Iwai0ba21762007-04-16 11:29:14 +02002026int snd_hda_input_mux_put(struct hda_codec *codec,
2027 const struct hda_input_mux *imux,
2028 struct snd_ctl_elem_value *ucontrol,
2029 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 unsigned int *cur_val)
2031{
2032 unsigned int idx;
2033
2034 idx = ucontrol->value.enumerated.item[0];
2035 if (idx >= imux->num_items)
2036 idx = imux->num_items - 1;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002037 if (*cur_val == idx && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2040 imux->items[idx].index);
2041 *cur_val = idx;
2042 return 1;
2043}
2044
2045
2046/*
2047 * Multi-channel / digital-out PCM helper functions
2048 */
2049
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002050/* setup SPDIF output stream */
2051static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2052 unsigned int stream_tag, unsigned int format)
2053{
2054 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2055 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2056 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2057 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2058 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2059 /* turn on again (if needed) */
2060 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2061 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2062 codec->spdif_ctls & 0xff);
2063}
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065/*
2066 * open the digital out in the exclusive mode
2067 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002068int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2069 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Ingo Molnar62932df2006-01-16 16:34:20 +01002071 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002072 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2073 /* already opened as analog dup; reset it once */
2074 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002076 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002080int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2081 struct hda_multi_out *mout,
2082 unsigned int stream_tag,
2083 unsigned int format,
2084 struct snd_pcm_substream *substream)
2085{
2086 mutex_lock(&codec->spdif_mutex);
2087 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2088 mutex_unlock(&codec->spdif_mutex);
2089 return 0;
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/*
2093 * release the digital out
2094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002095int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2096 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Ingo Molnar62932df2006-01-16 16:34:20 +01002098 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002100 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return 0;
2102}
2103
2104/*
2105 * set up more restrictions for analog out
2106 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002107int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2108 struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002109 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 substream->runtime->hw.channels_max = mout->max_channels;
2112 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2113 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2114}
2115
2116/*
2117 * set up the i/o for analog out
2118 * when the digital out is available, copy the front out to digital out, too.
2119 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002120int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2121 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 unsigned int stream_tag,
2123 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002124 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 hda_nid_t *nids = mout->dac_nids;
2127 int chs = substream->runtime->channels;
2128 int i;
2129
Ingo Molnar62932df2006-01-16 16:34:20 +01002130 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2132 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2134 format) &&
2135 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002137 setup_dig_out_stream(codec, mout->dig_out_nid,
2138 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else {
2140 mout->dig_out_used = 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002141 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2142 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002145 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002148 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2149 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02002150 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002152 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2153 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002154 /* extra outputs copied from front */
2155 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2156 if (mout->extra_out_nid[i])
2157 snd_hda_codec_setup_stream(codec,
2158 mout->extra_out_nid[i],
2159 stream_tag, 0, format);
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* surrounds */
2162 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002163 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002164 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2165 i * 2, format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002166 else /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002167 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2168 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170 return 0;
2171}
2172
2173/*
2174 * clean up the setting for analog out
2175 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002176int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2177 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
2179 hda_nid_t *nids = mout->dac_nids;
2180 int i;
2181
2182 for (i = 0; i < mout->num_dacs; i++)
2183 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2184 if (mout->hp_nid)
2185 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002186 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2187 if (mout->extra_out_nid[i])
2188 snd_hda_codec_setup_stream(codec,
2189 mout->extra_out_nid[i],
2190 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01002191 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2193 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2194 mout->dig_out_used = 0;
2195 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002196 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198}
2199
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002200/*
2201 * Helper for automatic ping configuration
2202 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002203
Takashi Iwai756e2b02007-04-16 11:27:07 +02002204static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002205{
2206 for (; *list; list++)
2207 if (*list == nid)
2208 return 1;
2209 return 0;
2210}
2211
Steve Longerbeam81937d32007-05-08 15:33:03 +02002212
2213/*
2214 * Sort an associated group of pins according to their sequence numbers.
2215 */
2216static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2217 int num_pins)
2218{
2219 int i, j;
2220 short seq;
2221 hda_nid_t nid;
2222
2223 for (i = 0; i < num_pins; i++) {
2224 for (j = i + 1; j < num_pins; j++) {
2225 if (sequences[i] > sequences[j]) {
2226 seq = sequences[i];
2227 sequences[i] = sequences[j];
2228 sequences[j] = seq;
2229 nid = pins[i];
2230 pins[i] = pins[j];
2231 pins[j] = nid;
2232 }
2233 }
2234 }
2235}
2236
2237
Takashi Iwai82bc9552006-03-21 11:24:42 +01002238/*
2239 * Parse all pin widgets and store the useful pin nids to cfg
2240 *
2241 * The number of line-outs or any primary output is stored in line_outs,
2242 * and the corresponding output pins are assigned to line_out_pins[],
2243 * in the order of front, rear, CLFE, side, ...
2244 *
2245 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002246 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002247 * is detected, one of speaker of HP pins is assigned as the primary
2248 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2249 * if any analog output exists.
2250 *
2251 * The analog input pins are assigned to input_pins array.
2252 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2253 * respectively.
2254 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002255int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
2256 struct auto_pin_cfg *cfg,
2257 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002258{
2259 hda_nid_t nid, nid_start;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002260 int nodes;
2261 short seq, assoc_line_out, assoc_speaker;
2262 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2263 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002264
2265 memset(cfg, 0, sizeof(*cfg));
2266
Steve Longerbeam81937d32007-05-08 15:33:03 +02002267 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2268 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2269 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002270
2271 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2272 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002273 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002274 unsigned int wid_type =
2275 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002276 unsigned int def_conf;
2277 short assoc, loc;
2278
2279 /* read all default configuration for pin complex */
2280 if (wid_type != AC_WID_PIN)
2281 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002282 /* ignore the given nids (e.g. pc-beep returns error) */
2283 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2284 continue;
2285
Takashi Iwai0ba21762007-04-16 11:29:14 +02002286 def_conf = snd_hda_codec_read(codec, nid, 0,
2287 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002288 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2289 continue;
2290 loc = get_defcfg_location(def_conf);
2291 switch (get_defcfg_device(def_conf)) {
2292 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002293 seq = get_defcfg_sequence(def_conf);
2294 assoc = get_defcfg_association(def_conf);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002295 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002296 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002297 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002298 assoc_line_out = assoc;
2299 else if (assoc_line_out != assoc)
2300 continue;
2301 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2302 continue;
2303 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002304 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002305 cfg->line_outs++;
2306 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002307 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002308 seq = get_defcfg_sequence(def_conf);
2309 assoc = get_defcfg_association(def_conf);
2310 if (! assoc)
2311 continue;
2312 if (! assoc_speaker)
2313 assoc_speaker = assoc;
2314 else if (assoc_speaker != assoc)
2315 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002316 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2317 continue;
2318 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002319 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002320 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002321 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002322 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002323 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2324 continue;
2325 cfg->hp_pins[cfg->hp_outs] = nid;
2326 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002327 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002328 case AC_JACK_MIC_IN: {
2329 int preferred, alt;
2330 if (loc == AC_JACK_LOC_FRONT) {
2331 preferred = AUTO_PIN_FRONT_MIC;
2332 alt = AUTO_PIN_MIC;
2333 } else {
2334 preferred = AUTO_PIN_MIC;
2335 alt = AUTO_PIN_FRONT_MIC;
2336 }
2337 if (!cfg->input_pins[preferred])
2338 cfg->input_pins[preferred] = nid;
2339 else if (!cfg->input_pins[alt])
2340 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002341 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002342 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002343 case AC_JACK_LINE_IN:
2344 if (loc == AC_JACK_LOC_FRONT)
2345 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2346 else
2347 cfg->input_pins[AUTO_PIN_LINE] = nid;
2348 break;
2349 case AC_JACK_CD:
2350 cfg->input_pins[AUTO_PIN_CD] = nid;
2351 break;
2352 case AC_JACK_AUX:
2353 cfg->input_pins[AUTO_PIN_AUX] = nid;
2354 break;
2355 case AC_JACK_SPDIF_OUT:
2356 cfg->dig_out_pin = nid;
2357 break;
2358 case AC_JACK_SPDIF_IN:
2359 cfg->dig_in_pin = nid;
2360 break;
2361 }
2362 }
2363
2364 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02002365 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2366 cfg->line_outs);
2367 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2368 cfg->speaker_outs);
2369
2370 /*
2371 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2372 * as a primary output
2373 */
2374 if (!cfg->line_outs) {
2375 if (cfg->speaker_outs) {
2376 cfg->line_outs = cfg->speaker_outs;
2377 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2378 sizeof(cfg->speaker_pins));
2379 cfg->speaker_outs = 0;
2380 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2381 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2382 } else if (cfg->hp_outs) {
2383 cfg->line_outs = cfg->hp_outs;
2384 memcpy(cfg->line_out_pins, cfg->hp_pins,
2385 sizeof(cfg->hp_pins));
2386 cfg->hp_outs = 0;
2387 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2388 cfg->line_out_type = AUTO_PIN_HP_OUT;
2389 }
2390 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002391
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002392 /* Reorder the surround channels
2393 * ALSA sequence is front/surr/clfe/side
2394 * HDA sequence is:
2395 * 4-ch: front/surr => OK as it is
2396 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02002397 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002398 */
2399 switch (cfg->line_outs) {
2400 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002401 case 4:
2402 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02002403 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002404 cfg->line_out_pins[2] = nid;
2405 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002406 }
2407
Takashi Iwai82bc9552006-03-21 11:24:42 +01002408 /*
2409 * debug prints of the parsed results
2410 */
2411 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2412 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2413 cfg->line_out_pins[2], cfg->line_out_pins[3],
2414 cfg->line_out_pins[4]);
2415 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2416 cfg->speaker_outs, cfg->speaker_pins[0],
2417 cfg->speaker_pins[1], cfg->speaker_pins[2],
2418 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002419 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2420 cfg->hp_outs, cfg->hp_pins[0],
2421 cfg->hp_pins[1], cfg->hp_pins[2],
2422 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002423 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2424 " cd=0x%x, aux=0x%x\n",
2425 cfg->input_pins[AUTO_PIN_MIC],
2426 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2427 cfg->input_pins[AUTO_PIN_LINE],
2428 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2429 cfg->input_pins[AUTO_PIN_CD],
2430 cfg->input_pins[AUTO_PIN_AUX]);
2431
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002432 return 0;
2433}
2434
Takashi Iwai4a471b72005-12-07 13:56:29 +01002435/* labels for input pins */
2436const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2437 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2438};
2439
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441#ifdef CONFIG_PM
2442/*
2443 * power management
2444 */
2445
2446/**
2447 * snd_hda_suspend - suspend the codecs
2448 * @bus: the HDA bus
2449 * @state: suspsend state
2450 *
2451 * Returns 0 if successful.
2452 */
2453int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2454{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002455 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /* FIXME: should handle power widget capabilities */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002458 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (codec->patch_ops.suspend)
2460 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002461 hda_set_power_state(codec,
2462 codec->afg ? codec->afg : codec->mfg,
2463 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465 return 0;
2466}
2467
2468/**
2469 * snd_hda_resume - resume the codecs
2470 * @bus: the HDA bus
2471 * @state: resume state
2472 *
2473 * Returns 0 if successful.
2474 */
2475int snd_hda_resume(struct hda_bus *bus)
2476{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002477 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Takashi Iwai0ba21762007-04-16 11:29:14 +02002479 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002480 hda_set_power_state(codec,
2481 codec->afg ? codec->afg : codec->mfg,
2482 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (codec->patch_ops.resume)
2484 codec->patch_ops.resume(codec);
2485 }
2486 return 0;
2487}
2488
2489/**
2490 * snd_hda_resume_ctls - resume controls in the new control list
2491 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002492 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002494 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 * originally for snd_hda_add_new_ctls().
2496 * The array must be terminated with an empty entry as terminator.
2497 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002498int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002500 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 val = kmalloc(sizeof(*val), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002503 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return -ENOMEM;
2505 codec->in_resume = 1;
2506 for (; knew->name; knew++) {
2507 int i, count;
2508 count = knew->count ? knew->count : 1;
2509 for (i = 0; i < count; i++) {
2510 memset(val, 0, sizeof(*val));
2511 val->id.iface = knew->iface;
2512 val->id.device = knew->device;
2513 val->id.subdevice = knew->subdevice;
2514 strcpy(val->id.name, knew->name);
2515 val->id.index = knew->index ? knew->index : i;
2516 /* Assume that get callback reads only from cache,
2517 * not accessing to the real hardware
2518 */
2519 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2520 continue;
2521 snd_ctl_elem_write(codec->bus->card, NULL, val);
2522 }
2523 }
2524 codec->in_resume = 0;
2525 kfree(val);
2526 return 0;
2527}
2528
2529/**
2530 * snd_hda_resume_spdif_out - resume the digital out
2531 * @codec: the HDA codec
2532 */
2533int snd_hda_resume_spdif_out(struct hda_codec *codec)
2534{
2535 return snd_hda_resume_ctls(codec, dig_mixes);
2536}
2537
2538/**
2539 * snd_hda_resume_spdif_in - resume the digital in
2540 * @codec: the HDA codec
2541 */
2542int snd_hda_resume_spdif_in(struct hda_codec *codec)
2543{
2544 return snd_hda_resume_ctls(codec, dig_in_ctls);
2545}
2546#endif