blob: 14649d54b49342bc55b5636fd63c75c1a522a585 [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"
34
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x10ec, "Realtek" },
Takashi Iwaia9226252006-09-17 22:05:54 +020048 { 0x1057, "Motorola" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010049 { 0x1106, "VIA" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020050 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020052 { 0x14f1, "Conexant" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020054 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 {} /* terminator */
56};
57
58/* codec presets */
59#include "hda_patch.h"
60
61
62/**
63 * snd_hda_codec_read - send a command and get the response
64 * @codec: the HDA codec
65 * @nid: NID to send the command
66 * @direct: direct flag
67 * @verb: the verb to send
68 * @parm: the parameter for the verb
69 *
70 * Send a single command and read the corresponding response.
71 *
72 * Returns the obtained response value, or -1 for an error.
73 */
Takashi Iwai0ba21762007-04-16 11:29:14 +020074unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
75 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned int verb, unsigned int parm)
77{
78 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010079 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +020080 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 res = codec->bus->ops.get_response(codec);
82 else
83 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010084 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return res;
86}
87
88/**
89 * snd_hda_codec_write - send a single command without waiting for response
90 * @codec: the HDA codec
91 * @nid: NID to send the command
92 * @direct: direct flag
93 * @verb: the verb to send
94 * @parm: the parameter for the verb
95 *
96 * Send a single command without waiting for response.
97 *
98 * Returns 0 if successful, or a negative error code.
99 */
100int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
101 unsigned int verb, unsigned int parm)
102{
103 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100104 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100106 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return err;
108}
109
110/**
111 * snd_hda_sequence_write - sequence writes
112 * @codec: the HDA codec
113 * @seq: VERB array to send
114 *
115 * Send the commands sequentially from the given array.
116 * The array must be terminated with NID=0.
117 */
118void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
119{
120 for (; seq->nid; seq++)
121 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
122}
123
124/**
125 * snd_hda_get_sub_nodes - get the range of sub nodes
126 * @codec: the HDA codec
127 * @nid: NID to parse
128 * @start_id: the pointer to store the start NID
129 *
130 * Parse the NID and store the start NID of its sub-nodes.
131 * Returns the number of sub-nodes.
132 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200133int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
134 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned int parm;
137
138 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
139 *start_id = (parm >> 16) & 0x7fff;
140 return (int)(parm & 0x7fff);
141}
142
143/**
144 * snd_hda_get_connections - get connection list
145 * @codec: the HDA codec
146 * @nid: NID to parse
147 * @conn_list: connection list array
148 * @max_conns: max. number of connections to store
149 *
150 * Parses the connection list of the given widget and stores the list
151 * of NIDs.
152 *
153 * Returns the number of connections, or a negative error code.
154 */
155int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
156 hda_nid_t *conn_list, int max_conns)
157{
158 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100159 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100161 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 snd_assert(conn_list && max_conns > 0, return -EINVAL);
164
165 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
166 if (parm & AC_CLIST_LONG) {
167 /* long form */
168 shift = 16;
169 num_elems = 2;
170 } else {
171 /* short form */
172 shift = 8;
173 num_elems = 4;
174 }
175 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 mask = (1 << (shift-1)) - 1;
177
Takashi Iwai0ba21762007-04-16 11:29:14 +0200178 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0; /* no connection */
180
181 if (conn_len == 1) {
182 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200183 parm = snd_hda_codec_read(codec, nid, 0,
184 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 conn_list[0] = parm & mask;
186 return 1;
187 }
188
189 /* multi connection */
190 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100191 prev_nid = 0;
192 for (i = 0; i < conn_len; i++) {
193 int range_val;
194 hda_nid_t val, n;
195
196 if (i % num_elems == 0)
197 parm = snd_hda_codec_read(codec, nid, 0,
198 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200199 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100200 val = parm & mask;
201 parm >>= shift;
202 if (range_val) {
203 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200204 if (!prev_nid || prev_nid >= val) {
205 snd_printk(KERN_WARNING "hda_codec: "
206 "invalid dep_range_val %x:%x\n",
207 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100208 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100210 for (n = prev_nid + 1; n <= val; n++) {
211 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200212 snd_printk(KERN_ERR
213 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100214 return -EINVAL;
215 }
216 conn_list[conns++] = n;
217 }
218 } else {
219 if (conns >= max_conns) {
220 snd_printk(KERN_ERR "Too many connections\n");
221 return -EINVAL;
222 }
223 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100225 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 return conns;
228}
229
230
231/**
232 * snd_hda_queue_unsol_event - add an unsolicited event to queue
233 * @bus: the BUS
234 * @res: unsolicited event (lower 32bit of RIRB entry)
235 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
236 *
237 * Adds the given event to the queue. The events are processed in
238 * the workqueue asynchronously. Call this function in the interrupt
239 * hanlder when RIRB receives an unsolicited event.
240 *
241 * Returns 0 if successful, or a negative error code.
242 */
243int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
244{
245 struct hda_bus_unsolicited *unsol;
246 unsigned int wp;
247
Takashi Iwai0ba21762007-04-16 11:29:14 +0200248 unsol = bus->unsol;
249 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251
252 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
253 unsol->wp = wp;
254
255 wp <<= 1;
256 unsol->queue[wp] = res;
257 unsol->queue[wp + 1] = res_ex;
258
Takashi Iwaie250af22006-12-19 17:08:52 +0100259 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return 0;
262}
263
264/*
265 * process queueud unsolicited events
266 */
David Howellsc4028952006-11-22 14:57:56 +0000267static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
David Howellsc4028952006-11-22 14:57:56 +0000269 struct hda_bus_unsolicited *unsol =
270 container_of(work, struct hda_bus_unsolicited, work);
271 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct hda_codec *codec;
273 unsigned int rp, caddr, res;
274
275 while (unsol->rp != unsol->wp) {
276 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
277 unsol->rp = rp;
278 rp <<= 1;
279 res = unsol->queue[rp];
280 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200281 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 continue;
283 codec = bus->caddr_tbl[caddr & 0x0f];
284 if (codec && codec->patch_ops.unsol_event)
285 codec->patch_ops.unsol_event(codec, res);
286 }
287}
288
289/*
290 * initialize unsolicited queue
291 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200292static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct hda_bus_unsolicited *unsol;
295
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100296 if (bus->unsol) /* already initialized */
297 return 0;
298
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200299 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200300 if (!unsol) {
301 snd_printk(KERN_ERR "hda_codec: "
302 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -ENOMEM;
304 }
David Howellsc4028952006-11-22 14:57:56 +0000305 INIT_WORK(&unsol->work, process_unsol_events);
306 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 bus->unsol = unsol;
308 return 0;
309}
310
311/*
312 * destructor
313 */
314static void snd_hda_codec_free(struct hda_codec *codec);
315
316static int snd_hda_bus_free(struct hda_bus *bus)
317{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200318 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Takashi Iwai0ba21762007-04-16 11:29:14 +0200320 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100323 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 kfree(bus->unsol);
325 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200326 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 snd_hda_codec_free(codec);
328 }
329 if (bus->ops.private_free)
330 bus->ops.private_free(bus);
331 kfree(bus);
332 return 0;
333}
334
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100335static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct hda_bus *bus = device->device_data;
338 return snd_hda_bus_free(bus);
339}
340
341/**
342 * snd_hda_bus_new - create a HDA bus
343 * @card: the card entry
344 * @temp: the template for hda_bus information
345 * @busp: the pointer to store the created bus instance
346 *
347 * Returns 0 if successful, or a negative error code.
348 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200349int __devinit snd_hda_bus_new(struct snd_card *card,
350 const struct hda_bus_template *temp,
351 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct hda_bus *bus;
354 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100355 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .dev_free = snd_hda_bus_dev_free,
357 };
358
359 snd_assert(temp, return -EINVAL);
360 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
361
362 if (busp)
363 *busp = NULL;
364
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200365 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (bus == NULL) {
367 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
368 return -ENOMEM;
369 }
370
371 bus->card = card;
372 bus->private_data = temp->private_data;
373 bus->pci = temp->pci;
374 bus->modelname = temp->modelname;
375 bus->ops = temp->ops;
376
Ingo Molnar62932df2006-01-16 16:34:20 +0100377 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 INIT_LIST_HEAD(&bus->codec_list);
379
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
381 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 snd_hda_bus_free(bus);
383 return err;
384 }
385 if (busp)
386 *busp = bus;
387 return 0;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
391 * find a matching codec preset
392 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200393static const struct hda_codec_preset __devinit *
394find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 const struct hda_codec_preset **tbl, *preset;
397
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100398 if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
399 return NULL; /* use the generic parser */
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 for (tbl = hda_preset_tables; *tbl; tbl++) {
402 for (preset = *tbl; preset->id; preset++) {
403 u32 mask = preset->mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200404 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200406 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200407 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200408 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return preset;
410 }
411 }
412 return NULL;
413}
414
415/*
416 * snd_hda_get_codec_name - store the codec name
417 */
418void snd_hda_get_codec_name(struct hda_codec *codec,
419 char *name, int namelen)
420{
421 const struct hda_vendor_id *c;
422 const char *vendor = NULL;
423 u16 vendor_id = codec->vendor_id >> 16;
424 char tmp[16];
425
426 for (c = hda_vendor_ids; c->id; c++) {
427 if (c->id == vendor_id) {
428 vendor = c->name;
429 break;
430 }
431 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200432 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sprintf(tmp, "Generic %04x", vendor_id);
434 vendor = tmp;
435 }
436 if (codec->preset && codec->preset->name)
437 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
438 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200439 snprintf(name, namelen, "%s ID %x", vendor,
440 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200444 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200446static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int i, total_nodes;
449 hda_nid_t nid;
450
451 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
452 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200453 unsigned int func;
454 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
455 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200456 case AC_GRP_AUDIO_FUNCTION:
457 codec->afg = nid;
458 break;
459 case AC_GRP_MODEM_FUNCTION:
460 codec->mfg = nid;
461 break;
462 default:
463 break;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100469 * read widget caps for each widget and store in cache
470 */
471static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
472{
473 int i;
474 hda_nid_t nid;
475
476 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
477 &codec->start_nid);
478 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200479 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100480 return -ENOMEM;
481 nid = codec->start_nid;
482 for (i = 0; i < codec->num_nodes; i++, nid++)
483 codec->wcaps[i] = snd_hda_param_read(codec, nid,
484 AC_PAR_AUDIO_WIDGET_CAP);
485 return 0;
486}
487
488
489/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * codec destructor
491 */
492static void snd_hda_codec_free(struct hda_codec *codec)
493{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200494 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return;
496 list_del(&codec->list);
497 codec->bus->caddr_tbl[codec->addr] = NULL;
498 if (codec->patch_ops.free)
499 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100500 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100501 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kfree(codec);
503}
504
505static void init_amp_hash(struct hda_codec *codec);
506
507/**
508 * snd_hda_codec_new - create a HDA codec
509 * @bus: the bus to assign
510 * @codec_addr: the codec address
511 * @codecp: the pointer to store the generated codec
512 *
513 * Returns 0 if successful, or a negative error code.
514 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200515int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
516 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct hda_codec *codec;
519 char component[13];
520 int err;
521
522 snd_assert(bus, return -EINVAL);
523 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
524
525 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200526 snd_printk(KERN_ERR "hda_codec: "
527 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EBUSY;
529 }
530
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200531 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (codec == NULL) {
533 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
534 return -ENOMEM;
535 }
536
537 codec->bus = bus;
538 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100539 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 init_amp_hash(codec);
541
542 list_add_tail(&codec->list, &bus->codec_list);
543 bus->caddr_tbl[codec_addr] = codec;
544
Takashi Iwai0ba21762007-04-16 11:29:14 +0200545 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
546 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100547 if (codec->vendor_id == -1)
548 /* read again, hopefully the access method was corrected
549 * in the last read...
550 */
551 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
552 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200553 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
554 AC_PAR_SUBSYSTEM_ID);
555 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
556 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200558 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200559 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200560 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 snd_hda_codec_free(codec);
562 return -ENODEV;
563 }
564
Takashi Iwai54d17402005-11-21 16:33:22 +0100565 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
566 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
567 snd_hda_codec_free(codec);
568 return -ENOMEM;
569 }
570
Takashi Iwai0ba21762007-04-16 11:29:14 +0200571 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200572 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200573 codec->subsystem_id =
574 snd_hda_codec_read(codec, nid, 0,
575 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200576 }
577
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100578 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200579 /* audio codec should override the mixer name */
580 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 snd_hda_get_codec_name(codec, bus->card->mixername,
582 sizeof(bus->card->mixername));
583
584 if (codec->preset && codec->preset->patch)
585 err = codec->preset->patch(codec);
586 else
587 err = snd_hda_parse_generic_codec(codec);
588 if (err < 0) {
589 snd_hda_codec_free(codec);
590 return err;
591 }
592
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100593 if (codec->patch_ops.unsol_event)
594 init_unsol_queue(bus);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 snd_hda_codec_proc_new(codec);
597
598 sprintf(component, "HDA:%08x", codec->vendor_id);
599 snd_component_add(codec->bus->card, component);
600
601 if (codecp)
602 *codecp = codec;
603 return 0;
604}
605
606/**
607 * snd_hda_codec_setup_stream - set up the codec for streaming
608 * @codec: the CODEC to set up
609 * @nid: the NID to set up
610 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
611 * @channel_id: channel id to pass, zero based.
612 * @format: stream format.
613 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200614void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
615 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int channel_id, int format)
617{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200618 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200619 return;
620
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 snd_printdd("hda_codec_setup_stream: "
622 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 nid, stream_tag, channel_id, format);
624 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
625 (stream_tag << 4) | channel_id);
626 msleep(1);
627 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * amp access functions
632 */
633
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200634/* FIXME: more better hash key? */
635#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200637#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/* initialize the hash table */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200640static void __devinit init_amp_hash(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
643 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100644 codec->amp_info_size = 0;
645 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/* query the hash. allocate an entry if not found. */
649static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
650{
651 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
652 u16 cur = codec->amp_hash[idx];
653 struct hda_amp_info *info;
654
655 while (cur != 0xffff) {
656 info = &codec->amp_info[cur];
657 if (info->key == key)
658 return info;
659 cur = info->next;
660 }
661
662 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100663 if (codec->num_amp_entries >= codec->amp_info_size) {
664 /* reallocate the array */
665 int new_size = codec->amp_info_size + 64;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200666 struct hda_amp_info *new_info;
667 new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
668 GFP_KERNEL);
669 if (!new_info) {
670 snd_printk(KERN_ERR "hda_codec: "
671 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100672 return NULL;
673 }
674 if (codec->amp_info) {
675 memcpy(new_info, codec->amp_info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200676 codec->amp_info_size *
677 sizeof(struct hda_amp_info));
Takashi Iwaid0311662005-11-07 14:38:44 +0100678 kfree(codec->amp_info);
679 }
680 codec->amp_info_size = new_size;
681 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 cur = codec->num_amp_entries++;
684 info = &codec->amp_info[cur];
685 info->key = key;
686 info->status = 0; /* not initialized yet */
687 info->next = codec->amp_hash[idx];
688 codec->amp_hash[idx] = cur;
689
690 return info;
691}
692
693/*
694 * query AMP capabilities for the given widget and direction
695 */
696static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
697{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200698 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
701 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200703 if (!(info->status & INFO_AMP_CAPS)) {
704 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200706 info->amp_caps = snd_hda_param_read(codec, nid,
707 direction == HDA_OUTPUT ?
708 AC_PAR_AMP_OUT_CAP :
709 AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 info->status |= INFO_AMP_CAPS;
711 }
712 return info->amp_caps;
713}
714
715/*
716 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200717 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719static unsigned int get_vol_mute(struct hda_codec *codec,
720 struct hda_amp_info *info, hda_nid_t nid,
721 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 u32 val, parm;
724
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200725 if (info->status & INFO_AMP_VOL(ch))
726 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
729 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
730 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200731 val = snd_hda_codec_read(codec, nid, 0,
732 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200734 info->status |= INFO_AMP_VOL(ch);
735 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200739 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200741static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200742 hda_nid_t nid, int ch, int direction, int index,
743 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 u32 parm;
746
747 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
748 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
749 parm |= index << AC_AMP_SET_INDEX_SHIFT;
750 parm |= val;
751 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200752 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200756 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
Takashi Iwai834be882006-03-01 14:16:17 +0100758int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
759 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200761 struct hda_amp_info *info;
762 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
763 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200765 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200768/*
769 * update the AMP value, mask = bit mask to set, val = the value
770 */
Takashi Iwai834be882006-03-01 14:16:17 +0100771int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
772 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200774 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200775
Takashi Iwai0ba21762007-04-16 11:29:14 +0200776 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
777 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200779 val &= mask;
780 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200781 if (info->vol[ch] == val && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200783 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 1;
785}
786
787
788/*
789 * AMP control callbacks
790 */
791/* retrieve parameters from private_value */
792#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
793#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
794#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
795#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
796
797/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200798int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
799 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
802 u16 nid = get_amp_nid(kcontrol);
803 u8 chs = get_amp_channels(kcontrol);
804 int dir = get_amp_direction(kcontrol);
805 u32 caps;
806
807 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200808 /* num steps */
809 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
810 if (!caps) {
811 printk(KERN_WARNING "hda_codec: "
812 "num_steps = 0 for NID=0x%x\n", nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -EINVAL;
814 }
815 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
816 uinfo->count = chs == 3 ? 2 : 1;
817 uinfo->value.integer.min = 0;
818 uinfo->value.integer.max = caps;
819 return 0;
820}
821
Takashi Iwai0ba21762007-04-16 11:29:14 +0200822int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
826 hda_nid_t nid = get_amp_nid(kcontrol);
827 int chs = get_amp_channels(kcontrol);
828 int dir = get_amp_direction(kcontrol);
829 int idx = get_amp_index(kcontrol);
830 long *valp = ucontrol->value.integer.value;
831
832 if (chs & 1)
833 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
834 if (chs & 2)
835 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
836 return 0;
837}
838
Takashi Iwai0ba21762007-04-16 11:29:14 +0200839int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
840 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
843 hda_nid_t nid = get_amp_nid(kcontrol);
844 int chs = get_amp_channels(kcontrol);
845 int dir = get_amp_direction(kcontrol);
846 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 long *valp = ucontrol->value.integer.value;
848 int change = 0;
849
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200850 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200851 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
852 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200853 valp++;
854 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200855 if (chs & 2)
856 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200857 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return change;
859}
860
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200861int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
862 unsigned int size, unsigned int __user *_tlv)
863{
864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
865 hda_nid_t nid = get_amp_nid(kcontrol);
866 int dir = get_amp_direction(kcontrol);
867 u32 caps, val1, val2;
868
869 if (size < 4 * sizeof(unsigned int))
870 return -ENOMEM;
871 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200872 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
873 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200874 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
875 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200876 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
877 return -EFAULT;
878 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
879 return -EFAULT;
880 if (put_user(val1, _tlv + 2))
881 return -EFAULT;
882 if (put_user(val2, _tlv + 3))
883 return -EFAULT;
884 return 0;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200888int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
889 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int chs = get_amp_channels(kcontrol);
892
893 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
894 uinfo->count = chs == 3 ? 2 : 1;
895 uinfo->value.integer.min = 0;
896 uinfo->value.integer.max = 1;
897 return 0;
898}
899
Takashi Iwai0ba21762007-04-16 11:29:14 +0200900int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
904 hda_nid_t nid = get_amp_nid(kcontrol);
905 int chs = get_amp_channels(kcontrol);
906 int dir = get_amp_direction(kcontrol);
907 int idx = get_amp_index(kcontrol);
908 long *valp = ucontrol->value.integer.value;
909
910 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +0200911 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
912 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +0200914 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
915 0x80) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917}
918
Takashi Iwai0ba21762007-04-16 11:29:14 +0200919int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
920 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
923 hda_nid_t nid = get_amp_nid(kcontrol);
924 int chs = get_amp_channels(kcontrol);
925 int dir = get_amp_direction(kcontrol);
926 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 long *valp = ucontrol->value.integer.value;
928 int change = 0;
929
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200930 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200931 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
932 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200933 valp++;
934 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200935 if (chs & 2)
936 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200937 0x80, *valp ? 0 : 0x80);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return change;
940}
941
942/*
Takashi Iwai985be542005-11-02 18:26:49 +0100943 * bound volume controls
944 *
945 * bind multiple volumes (# indices, from 0)
946 */
947
948#define AMP_VAL_IDX_SHIFT 19
949#define AMP_VAL_IDX_MASK (0x0f<<19)
950
Takashi Iwai0ba21762007-04-16 11:29:14 +0200951int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100953{
954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
955 unsigned long pval;
956 int err;
957
Ingo Molnar62932df2006-01-16 16:34:20 +0100958 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100959 pval = kcontrol->private_value;
960 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
961 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
962 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100963 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100964 return err;
965}
966
Takashi Iwai0ba21762007-04-16 11:29:14 +0200967int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
968 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100969{
970 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
971 unsigned long pval;
972 int i, indices, err = 0, change = 0;
973
Ingo Molnar62932df2006-01-16 16:34:20 +0100974 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100975 pval = kcontrol->private_value;
976 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
977 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200978 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
979 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +0100980 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
981 if (err < 0)
982 break;
983 change |= err;
984 }
985 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100986 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100987 return err < 0 ? err : change;
988}
989
990/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * SPDIF out controls
992 */
993
Takashi Iwai0ba21762007-04-16 11:29:14 +0200994static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
995 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
998 uinfo->count = 1;
999 return 0;
1000}
1001
Takashi Iwai0ba21762007-04-16 11:29:14 +02001002static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1006 IEC958_AES0_NONAUDIO |
1007 IEC958_AES0_CON_EMPHASIS_5015 |
1008 IEC958_AES0_CON_NOT_COPYRIGHT;
1009 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1010 IEC958_AES1_CON_ORIGINAL;
1011 return 0;
1012}
1013
Takashi Iwai0ba21762007-04-16 11:29:14 +02001014static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1015 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1018 IEC958_AES0_NONAUDIO |
1019 IEC958_AES0_PRO_EMPHASIS_5015;
1020 return 0;
1021}
1022
Takashi Iwai0ba21762007-04-16 11:29:14 +02001023static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1024 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1027
1028 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1029 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1030 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1031 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1032
1033 return 0;
1034}
1035
1036/* convert from SPDIF status bits to HDA SPDIF bits
1037 * bit 0 (DigEn) is always set zero (to be filled later)
1038 */
1039static unsigned short convert_from_spdif_status(unsigned int sbits)
1040{
1041 unsigned short val = 0;
1042
1043 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001044 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001046 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001048 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1049 IEC958_AES0_PRO_EMPHASIS_5015)
1050 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001052 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1053 IEC958_AES0_CON_EMPHASIS_5015)
1054 val |= AC_DIG1_EMPHASIS;
1055 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1056 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001058 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1060 }
1061 return val;
1062}
1063
1064/* convert to SPDIF status bits from HDA SPDIF bits
1065 */
1066static unsigned int convert_to_spdif_status(unsigned short val)
1067{
1068 unsigned int sbits = 0;
1069
Takashi Iwai0ba21762007-04-16 11:29:14 +02001070 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001072 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 sbits |= IEC958_AES0_PROFESSIONAL;
1074 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001075 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1077 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001078 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001080 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001082 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1084 sbits |= val & (0x7f << 8);
1085 }
1086 return sbits;
1087}
1088
Takashi Iwai0ba21762007-04-16 11:29:14 +02001089static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1090 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1093 hda_nid_t nid = kcontrol->private_value;
1094 unsigned short val;
1095 int change;
1096
Ingo Molnar62932df2006-01-16 16:34:20 +01001097 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 codec->spdif_status = ucontrol->value.iec958.status[0] |
1099 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1100 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1101 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1102 val = convert_from_spdif_status(codec->spdif_status);
1103 val |= codec->spdif_ctls & 1;
1104 change = codec->spdif_ctls != val;
1105 codec->spdif_ctls = val;
1106
1107 if (change || codec->in_resume) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001108 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1109 val & 0xff);
1110 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1111 val >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
Ingo Molnar62932df2006-01-16 16:34:20 +01001114 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return change;
1116}
1117
Takashi Iwai0ba21762007-04-16 11:29:14 +02001118static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol,
1119 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1122 uinfo->count = 1;
1123 uinfo->value.integer.min = 0;
1124 uinfo->value.integer.max = 1;
1125 return 0;
1126}
1127
Takashi Iwai0ba21762007-04-16 11:29:14 +02001128static int snd_hda_spdif_out_switch_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
Takashi Iwai0ba21762007-04-16 11:29:14 +02001133 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135}
1136
Takashi Iwai0ba21762007-04-16 11:29:14 +02001137static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1138 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1141 hda_nid_t nid = kcontrol->private_value;
1142 unsigned short val;
1143 int change;
1144
Ingo Molnar62932df2006-01-16 16:34:20 +01001145 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001146 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001148 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 change = codec->spdif_ctls != val;
1150 if (change || codec->in_resume) {
1151 codec->spdif_ctls = val;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001152 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1153 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001154 /* unmute amp switch (if any) */
1155 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1156 (val & AC_DIG1_ENABLE))
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001157 snd_hda_codec_write(codec, nid, 0,
1158 AC_VERB_SET_AMP_GAIN_MUTE,
1159 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
Takashi Iwai0ba21762007-04-16 11:29:14 +02001160 AC_AMP_SET_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001162 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return change;
1164}
1165
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001166static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 {
1168 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1169 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1170 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1171 .info = snd_hda_spdif_mask_info,
1172 .get = snd_hda_spdif_cmask_get,
1173 },
1174 {
1175 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1176 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1177 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1178 .info = snd_hda_spdif_mask_info,
1179 .get = snd_hda_spdif_pmask_get,
1180 },
1181 {
1182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1183 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1184 .info = snd_hda_spdif_mask_info,
1185 .get = snd_hda_spdif_default_get,
1186 .put = snd_hda_spdif_default_put,
1187 },
1188 {
1189 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1190 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1191 .info = snd_hda_spdif_out_switch_info,
1192 .get = snd_hda_spdif_out_switch_get,
1193 .put = snd_hda_spdif_out_switch_put,
1194 },
1195 { } /* end */
1196};
1197
1198/**
1199 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1200 * @codec: the HDA codec
1201 * @nid: audio out widget NID
1202 *
1203 * Creates controls related with the SPDIF output.
1204 * Called from each patch supporting the SPDIF out.
1205 *
1206 * Returns 0 if successful, or a negative error code.
1207 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001208int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
1209 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001212 struct snd_kcontrol *kctl;
1213 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1216 kctl = snd_ctl_new1(dig_mix, codec);
1217 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001218 err = snd_ctl_add(codec->bus->card, kctl);
1219 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return err;
1221 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001222 codec->spdif_ctls =
1223 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1225 return 0;
1226}
1227
1228/*
1229 * SPDIF input
1230 */
1231
1232#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1233
Takashi Iwai0ba21762007-04-16 11:29:14 +02001234static int snd_hda_spdif_in_switch_get(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
1239 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1240 return 0;
1241}
1242
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1244 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1247 hda_nid_t nid = kcontrol->private_value;
1248 unsigned int val = !!ucontrol->value.integer.value[0];
1249 int change;
1250
Ingo Molnar62932df2006-01-16 16:34:20 +01001251 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 change = codec->spdif_in_enable != val;
1253 if (change || codec->in_resume) {
1254 codec->spdif_in_enable = val;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001255 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1256 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001258 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return change;
1260}
1261
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1263 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1266 hda_nid_t nid = kcontrol->private_value;
1267 unsigned short val;
1268 unsigned int sbits;
1269
1270 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1271 sbits = convert_to_spdif_status(val);
1272 ucontrol->value.iec958.status[0] = sbits;
1273 ucontrol->value.iec958.status[1] = sbits >> 8;
1274 ucontrol->value.iec958.status[2] = sbits >> 16;
1275 ucontrol->value.iec958.status[3] = sbits >> 24;
1276 return 0;
1277}
1278
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001279static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 {
1281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1282 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1283 .info = snd_hda_spdif_in_switch_info,
1284 .get = snd_hda_spdif_in_switch_get,
1285 .put = snd_hda_spdif_in_switch_put,
1286 },
1287 {
1288 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1290 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1291 .info = snd_hda_spdif_mask_info,
1292 .get = snd_hda_spdif_in_status_get,
1293 },
1294 { } /* end */
1295};
1296
1297/**
1298 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1299 * @codec: the HDA codec
1300 * @nid: audio in widget NID
1301 *
1302 * Creates controls related with the SPDIF input.
1303 * Called from each patch supporting the SPDIF in.
1304 *
1305 * Returns 0 if successful, or a negative error code.
1306 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001307int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
1308 hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001311 struct snd_kcontrol *kctl;
1312 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1315 kctl = snd_ctl_new1(dig_mix, codec);
1316 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317 err = snd_ctl_add(codec->bus->card, kctl);
1318 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return err;
1320 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001321 codec->spdif_in_enable =
1322 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1323 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
1327
Takashi Iwai54d17402005-11-21 16:33:22 +01001328/*
1329 * set power state of the codec
1330 */
1331static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1332 unsigned int power_state)
1333{
1334 hda_nid_t nid, nid_start;
1335 int nodes;
1336
1337 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1338 power_state);
1339
1340 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1341 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1342 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1343 snd_hda_codec_write(codec, nid, 0,
1344 AC_VERB_SET_POWER_STATE,
1345 power_state);
1346 }
1347
1348 if (power_state == AC_PWRST_D0)
1349 msleep(10);
1350}
1351
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/**
1354 * snd_hda_build_controls - build mixer controls
1355 * @bus: the BUS
1356 *
1357 * Creates mixer controls for each codec included in the bus.
1358 *
1359 * Returns 0 if successful, otherwise a negative error code.
1360 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001361int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* build controls */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001366 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001368 if (!codec->patch_ops.build_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 continue;
1370 err = codec->patch_ops.build_controls(codec);
1371 if (err < 0)
1372 return err;
1373 }
1374
1375 /* initialize */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001376 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001378 hda_set_power_state(codec,
1379 codec->afg ? codec->afg : codec->mfg,
1380 AC_PWRST_D0);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001381 if (!codec->patch_ops.init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 continue;
1383 err = codec->patch_ops.init(codec);
1384 if (err < 0)
1385 return err;
1386 }
1387 return 0;
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/*
1391 * stream formats
1392 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001393struct hda_rate_tbl {
1394 unsigned int hz;
1395 unsigned int alsa_bits;
1396 unsigned int hda_fmt;
1397};
1398
1399static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001401
1402 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1404 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1405 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1406 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1407 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1408 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1409 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1410 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1411 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1412 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1413 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001414#define AC_PAR_PCM_RATE_BITS 11
1415 /* up to bits 10, 384kHZ isn't supported properly */
1416
1417 /* not autodetected value */
1418 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001419
Takashi Iwaibefdf312005-08-22 13:57:55 +02001420 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421};
1422
1423/**
1424 * snd_hda_calc_stream_format - calculate format bitset
1425 * @rate: the sample rate
1426 * @channels: the number of channels
1427 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1428 * @maxbps: the max. bps
1429 *
1430 * Calculate the format bitset from the given rate, channels and th PCM format.
1431 *
1432 * Return zero if invalid.
1433 */
1434unsigned int snd_hda_calc_stream_format(unsigned int rate,
1435 unsigned int channels,
1436 unsigned int format,
1437 unsigned int maxbps)
1438{
1439 int i;
1440 unsigned int val = 0;
1441
Takashi Iwaibefdf312005-08-22 13:57:55 +02001442 for (i = 0; rate_bits[i].hz; i++)
1443 if (rate_bits[i].hz == rate) {
1444 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001447 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 snd_printdd("invalid rate %d\n", rate);
1449 return 0;
1450 }
1451
1452 if (channels == 0 || channels > 8) {
1453 snd_printdd("invalid channels %d\n", channels);
1454 return 0;
1455 }
1456 val |= channels - 1;
1457
1458 switch (snd_pcm_format_width(format)) {
1459 case 8: val |= 0x00; break;
1460 case 16: val |= 0x10; break;
1461 case 20:
1462 case 24:
1463 case 32:
1464 if (maxbps >= 32)
1465 val |= 0x40;
1466 else if (maxbps >= 24)
1467 val |= 0x30;
1468 else
1469 val |= 0x20;
1470 break;
1471 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001472 snd_printdd("invalid format width %d\n",
1473 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475 }
1476
1477 return val;
1478}
1479
1480/**
1481 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1482 * @codec: the HDA codec
1483 * @nid: NID to query
1484 * @ratesp: the pointer to store the detected rate bitflags
1485 * @formatsp: the pointer to store the detected formats
1486 * @bpsp: the pointer to store the detected format widths
1487 *
1488 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1489 * or @bsps argument is ignored.
1490 *
1491 * Returns 0 if successful, otherwise a negative error code.
1492 */
1493int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1494 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1495{
1496 int i;
1497 unsigned int val, streams;
1498
1499 val = 0;
1500 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001501 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1503 if (val == -1)
1504 return -EIO;
1505 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001506 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1508
1509 if (ratesp) {
1510 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001511 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001513 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 *ratesp = rates;
1516 }
1517
1518 if (formatsp || bpsp) {
1519 u64 formats = 0;
1520 unsigned int bps;
1521 unsigned int wcaps;
1522
Takashi Iwai54d17402005-11-21 16:33:22 +01001523 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1525 if (streams == -1)
1526 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001527 if (!streams) {
1528 streams = snd_hda_param_read(codec, codec->afg,
1529 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (streams == -1)
1531 return -EIO;
1532 }
1533
1534 bps = 0;
1535 if (streams & AC_SUPFMT_PCM) {
1536 if (val & AC_SUPPCM_BITS_8) {
1537 formats |= SNDRV_PCM_FMTBIT_U8;
1538 bps = 8;
1539 }
1540 if (val & AC_SUPPCM_BITS_16) {
1541 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1542 bps = 16;
1543 }
1544 if (wcaps & AC_WCAP_DIGITAL) {
1545 if (val & AC_SUPPCM_BITS_32)
1546 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1547 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1548 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1549 if (val & AC_SUPPCM_BITS_24)
1550 bps = 24;
1551 else if (val & AC_SUPPCM_BITS_20)
1552 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1554 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1556 if (val & AC_SUPPCM_BITS_32)
1557 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 else if (val & AC_SUPPCM_BITS_24)
1559 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02001560 else if (val & AC_SUPPCM_BITS_20)
1561 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001564 else if (streams == AC_SUPFMT_FLOAT32) {
1565 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1567 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001568 } else if (streams == AC_SUPFMT_AC3) {
1569 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* temporary hack: we have still no proper support
1571 * for the direct AC3 stream...
1572 */
1573 formats |= SNDRV_PCM_FMTBIT_U8;
1574 bps = 8;
1575 }
1576 if (formatsp)
1577 *formatsp = formats;
1578 if (bpsp)
1579 *bpsp = bps;
1580 }
1581
1582 return 0;
1583}
1584
1585/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02001586 * snd_hda_is_supported_format - check whether the given node supports
1587 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 *
1589 * Returns 1 if supported, 0 if not.
1590 */
1591int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1592 unsigned int format)
1593{
1594 int i;
1595 unsigned int val = 0, rate, stream;
1596
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 0;
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 if (val == -1)
1606 return 0;
1607 }
1608
1609 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001610 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02001611 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (val & (1 << i))
1613 break;
1614 return 0;
1615 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001616 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
1618
1619 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1620 if (stream == -1)
1621 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return 0;
1626
1627 if (stream & AC_SUPFMT_PCM) {
1628 switch (format & 0xf0) {
1629 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632 break;
1633 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001634 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636 break;
1637 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001638 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return 0;
1640 break;
1641 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001642 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
1644 break;
1645 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02001646 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return 0;
1648 break;
1649 default:
1650 return 0;
1651 }
1652 } else {
1653 /* FIXME: check for float32 and AC3? */
1654 }
1655
1656 return 1;
1657}
1658
1659/*
1660 * PCM stuff
1661 */
1662static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1663 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001664 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 return 0;
1667}
1668
1669static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1670 struct hda_codec *codec,
1671 unsigned int stream_tag,
1672 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001673 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1676 return 0;
1677}
1678
1679static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1680 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001681 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1684 return 0;
1685}
1686
Takashi Iwai0ba21762007-04-16 11:29:14 +02001687static int __devinit set_pcm_default_values(struct hda_codec *codec,
1688 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001690 /* query support PCM information from the given NID */
1691 if (info->nid && (!info->rates || !info->formats)) {
1692 snd_hda_query_supported_pcm(codec, info->nid,
1693 info->rates ? NULL : &info->rates,
1694 info->formats ? NULL : &info->formats,
1695 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 if (info->ops.open == NULL)
1698 info->ops.open = hda_pcm_default_open_close;
1699 if (info->ops.close == NULL)
1700 info->ops.close = hda_pcm_default_open_close;
1701 if (info->ops.prepare == NULL) {
1702 snd_assert(info->nid, return -EINVAL);
1703 info->ops.prepare = hda_pcm_default_prepare;
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (info->ops.cleanup == NULL) {
1706 snd_assert(info->nid, return -EINVAL);
1707 info->ops.cleanup = hda_pcm_default_cleanup;
1708 }
1709 return 0;
1710}
1711
1712/**
1713 * snd_hda_build_pcms - build PCM information
1714 * @bus: the BUS
1715 *
1716 * Create PCM information for each codec included in the bus.
1717 *
1718 * The build_pcms codec patch is requested to set up codec->num_pcms and
1719 * codec->pcm_info properly. The array is referred by the top-level driver
1720 * to create its PCM instances.
1721 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1722 * callback.
1723 *
1724 * At least, substreams, channels_min and channels_max must be filled for
1725 * each stream. substreams = 0 indicates that the stream doesn't exist.
1726 * When rates and/or formats are zero, the supported values are queried
1727 * from the given nid. The nid is used also by the default ops.prepare
1728 * and ops.cleanup callbacks.
1729 *
1730 * The driver needs to call ops.open in its open callback. Similarly,
1731 * ops.close is supposed to be called in the close callback.
1732 * ops.prepare should be called in the prepare or hw_params callback
1733 * with the proper parameters for set up.
1734 * ops.cleanup should be called in hw_free for clean up of streams.
1735 *
1736 * This function returns 0 if successfull, or a negative error code.
1737 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001738int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001740 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Takashi Iwai0ba21762007-04-16 11:29:14 +02001742 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned int pcm, s;
1744 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001745 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 continue;
1747 err = codec->patch_ops.build_pcms(codec);
1748 if (err < 0)
1749 return err;
1750 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1751 for (s = 0; s < 2; s++) {
1752 struct hda_pcm_stream *info;
1753 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02001754 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 continue;
1756 err = set_pcm_default_values(codec, info);
1757 if (err < 0)
1758 return err;
1759 }
1760 }
1761 }
1762 return 0;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/**
1766 * snd_hda_check_board_config - compare the current codec with the config table
1767 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001768 * @num_configs: number of config enums
1769 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * @tbl: configuration table, terminated by null entries
1771 *
1772 * Compares the modelname or PCI subsystem id of the current codec with the
1773 * given configuration table. If a matching entry is found, returns its
1774 * config value (supposed to be 0 or positive).
1775 *
1776 * If no entries are matching, the function returns a negative value.
1777 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001778int __devinit snd_hda_check_board_config(struct hda_codec *codec,
1779 int num_configs, const char **models,
1780 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001782 if (codec->bus->modelname && models) {
1783 int i;
1784 for (i = 0; i < num_configs; i++) {
1785 if (models[i] &&
1786 !strcmp(codec->bus->modelname, models[i])) {
1787 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1788 "selected\n", models[i]);
1789 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 }
1792 }
1793
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001794 if (!codec->bus->pci || !tbl)
1795 return -1;
1796
1797 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1798 if (!tbl)
1799 return -1;
1800 if (tbl->value >= 0 && tbl->value < num_configs) {
1801#ifdef CONFIG_SND_DEBUG_DETECT
1802 char tmp[10];
1803 const char *model = NULL;
1804 if (models)
1805 model = models[tbl->value];
1806 if (!model) {
1807 sprintf(tmp, "#%d", tbl->value);
1808 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01001810 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1811 "for config %x:%x (%s)\n",
1812 model, tbl->subvendor, tbl->subdevice,
1813 (tbl->name ? tbl->name : "Unknown device"));
1814#endif
1815 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 return -1;
1818}
1819
1820/**
1821 * snd_hda_add_new_ctls - create controls from the array
1822 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001823 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
1825 * This helper function creates and add new controls in the given array.
1826 * The array must be terminated with an empty entry as terminator.
1827 *
1828 * Returns 0 if successful, or a negative error code.
1829 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001830int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
1831 struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 int err;
1834
1835 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001836 struct snd_kcontrol *kctl;
1837 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001838 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001839 return -ENOMEM;
1840 err = snd_ctl_add(codec->bus->card, kctl);
1841 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001842 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01001843 return err;
1844 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001845 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01001846 return -ENOMEM;
1847 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001848 err = snd_ctl_add(codec->bus->card, kctl);
1849 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01001850 return err;
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853 return 0;
1854}
1855
1856
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001857/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001858 * Channel mode helper
1859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001860int snd_hda_ch_mode_info(struct hda_codec *codec,
1861 struct snd_ctl_elem_info *uinfo,
1862 const struct hda_channel_mode *chmode,
1863 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001864{
1865 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1866 uinfo->count = 1;
1867 uinfo->value.enumerated.items = num_chmodes;
1868 if (uinfo->value.enumerated.item >= num_chmodes)
1869 uinfo->value.enumerated.item = num_chmodes - 1;
1870 sprintf(uinfo->value.enumerated.name, "%dch",
1871 chmode[uinfo->value.enumerated.item].channels);
1872 return 0;
1873}
1874
Takashi Iwai0ba21762007-04-16 11:29:14 +02001875int snd_hda_ch_mode_get(struct hda_codec *codec,
1876 struct snd_ctl_elem_value *ucontrol,
1877 const struct hda_channel_mode *chmode,
1878 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001879 int max_channels)
1880{
1881 int i;
1882
1883 for (i = 0; i < num_chmodes; i++) {
1884 if (max_channels == chmode[i].channels) {
1885 ucontrol->value.enumerated.item[0] = i;
1886 break;
1887 }
1888 }
1889 return 0;
1890}
1891
Takashi Iwai0ba21762007-04-16 11:29:14 +02001892int snd_hda_ch_mode_put(struct hda_codec *codec,
1893 struct snd_ctl_elem_value *ucontrol,
1894 const struct hda_channel_mode *chmode,
1895 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001896 int *max_channelsp)
1897{
1898 unsigned int mode;
1899
1900 mode = ucontrol->value.enumerated.item[0];
1901 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001902 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001903 return 0;
1904 /* change the current channel setting */
1905 *max_channelsp = chmode[mode].channels;
1906 if (chmode[mode].sequence)
1907 snd_hda_sequence_write(codec, chmode[mode].sequence);
1908 return 1;
1909}
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911/*
1912 * input MUX helper
1913 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001914int snd_hda_input_mux_info(const struct hda_input_mux *imux,
1915 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 unsigned int index;
1918
1919 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1920 uinfo->count = 1;
1921 uinfo->value.enumerated.items = imux->num_items;
1922 index = uinfo->value.enumerated.item;
1923 if (index >= imux->num_items)
1924 index = imux->num_items - 1;
1925 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1926 return 0;
1927}
1928
Takashi Iwai0ba21762007-04-16 11:29:14 +02001929int snd_hda_input_mux_put(struct hda_codec *codec,
1930 const struct hda_input_mux *imux,
1931 struct snd_ctl_elem_value *ucontrol,
1932 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 unsigned int *cur_val)
1934{
1935 unsigned int idx;
1936
1937 idx = ucontrol->value.enumerated.item[0];
1938 if (idx >= imux->num_items)
1939 idx = imux->num_items - 1;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001940 if (*cur_val == idx && !codec->in_resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1943 imux->items[idx].index);
1944 *cur_val = idx;
1945 return 1;
1946}
1947
1948
1949/*
1950 * Multi-channel / digital-out PCM helper functions
1951 */
1952
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001953/* setup SPDIF output stream */
1954static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
1955 unsigned int stream_tag, unsigned int format)
1956{
1957 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
1958 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1959 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1960 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
1961 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
1962 /* turn on again (if needed) */
1963 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1964 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1965 codec->spdif_ctls & 0xff);
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * open the digital out in the exclusive mode
1970 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001971int snd_hda_multi_out_dig_open(struct hda_codec *codec,
1972 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Ingo Molnar62932df2006-01-16 16:34:20 +01001974 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02001975 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
1976 /* already opened as analog dup; reset it once */
1977 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001979 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return 0;
1981}
1982
Takashi Iwai6b97eb42007-04-05 14:51:48 +02001983int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
1984 struct hda_multi_out *mout,
1985 unsigned int stream_tag,
1986 unsigned int format,
1987 struct snd_pcm_substream *substream)
1988{
1989 mutex_lock(&codec->spdif_mutex);
1990 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
1991 mutex_unlock(&codec->spdif_mutex);
1992 return 0;
1993}
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995/*
1996 * release the digital out
1997 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001998int snd_hda_multi_out_dig_close(struct hda_codec *codec,
1999 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Ingo Molnar62932df2006-01-16 16:34:20 +01002001 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002003 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005}
2006
2007/*
2008 * set up more restrictions for analog out
2009 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002010int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2011 struct hda_multi_out *mout,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002012 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 substream->runtime->hw.channels_max = mout->max_channels;
2015 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2016 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2017}
2018
2019/*
2020 * set up the i/o for analog out
2021 * when the digital out is available, copy the front out to digital out, too.
2022 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002023int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2024 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 unsigned int stream_tag,
2026 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002027 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 hda_nid_t *nids = mout->dac_nids;
2030 int chs = substream->runtime->channels;
2031 int i;
2032
Ingo Molnar62932df2006-01-16 16:34:20 +01002033 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2035 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002036 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2037 format) &&
2038 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002040 setup_dig_out_stream(codec, mout->dig_out_nid,
2041 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 } else {
2043 mout->dig_out_used = 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002044 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2045 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002048 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002051 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2052 0, format);
Takashi Iwai35aec4e2006-07-28 14:44:31 +02002053 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002055 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2056 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002057 /* extra outputs copied from front */
2058 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2059 if (mout->extra_out_nid[i])
2060 snd_hda_codec_setup_stream(codec,
2061 mout->extra_out_nid[i],
2062 stream_tag, 0, format);
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* surrounds */
2065 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002066 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002067 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2068 i * 2, format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002069 else /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002070 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2071 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 return 0;
2074}
2075
2076/*
2077 * clean up the setting for analog out
2078 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002079int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2080 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 hda_nid_t *nids = mout->dac_nids;
2083 int i;
2084
2085 for (i = 0; i < mout->num_dacs; i++)
2086 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2087 if (mout->hp_nid)
2088 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002089 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2090 if (mout->extra_out_nid[i])
2091 snd_hda_codec_setup_stream(codec,
2092 mout->extra_out_nid[i],
2093 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01002094 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2096 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2097 mout->dig_out_used = 0;
2098 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002099 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101}
2102
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002103/*
2104 * Helper for automatic ping configuration
2105 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002106
Takashi Iwai756e2b02007-04-16 11:27:07 +02002107static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002108{
2109 for (; *list; list++)
2110 if (*list == nid)
2111 return 1;
2112 return 0;
2113}
2114
Steve Longerbeam81937d32007-05-08 15:33:03 +02002115
2116/*
2117 * Sort an associated group of pins according to their sequence numbers.
2118 */
2119static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2120 int num_pins)
2121{
2122 int i, j;
2123 short seq;
2124 hda_nid_t nid;
2125
2126 for (i = 0; i < num_pins; i++) {
2127 for (j = i + 1; j < num_pins; j++) {
2128 if (sequences[i] > sequences[j]) {
2129 seq = sequences[i];
2130 sequences[i] = sequences[j];
2131 sequences[j] = seq;
2132 nid = pins[i];
2133 pins[i] = pins[j];
2134 pins[j] = nid;
2135 }
2136 }
2137 }
2138}
2139
2140
Takashi Iwai82bc9552006-03-21 11:24:42 +01002141/*
2142 * Parse all pin widgets and store the useful pin nids to cfg
2143 *
2144 * The number of line-outs or any primary output is stored in line_outs,
2145 * and the corresponding output pins are assigned to line_out_pins[],
2146 * in the order of front, rear, CLFE, side, ...
2147 *
2148 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002149 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002150 * is detected, one of speaker of HP pins is assigned as the primary
2151 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2152 * if any analog output exists.
2153 *
2154 * The analog input pins are assigned to input_pins array.
2155 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2156 * respectively.
2157 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002158int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
2159 struct auto_pin_cfg *cfg,
2160 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002161{
2162 hda_nid_t nid, nid_start;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002163 int nodes;
2164 short seq, assoc_line_out, assoc_speaker;
2165 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2166 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002167
2168 memset(cfg, 0, sizeof(*cfg));
2169
Steve Longerbeam81937d32007-05-08 15:33:03 +02002170 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2171 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2172 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002173
2174 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2175 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002176 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002177 unsigned int wid_type =
2178 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002179 unsigned int def_conf;
2180 short assoc, loc;
2181
2182 /* read all default configuration for pin complex */
2183 if (wid_type != AC_WID_PIN)
2184 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002185 /* ignore the given nids (e.g. pc-beep returns error) */
2186 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2187 continue;
2188
Takashi Iwai0ba21762007-04-16 11:29:14 +02002189 def_conf = snd_hda_codec_read(codec, nid, 0,
2190 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002191 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2192 continue;
2193 loc = get_defcfg_location(def_conf);
2194 switch (get_defcfg_device(def_conf)) {
2195 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002196 seq = get_defcfg_sequence(def_conf);
2197 assoc = get_defcfg_association(def_conf);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002198 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002199 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002200 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002201 assoc_line_out = assoc;
2202 else if (assoc_line_out != assoc)
2203 continue;
2204 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2205 continue;
2206 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002207 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002208 cfg->line_outs++;
2209 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002210 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002211 seq = get_defcfg_sequence(def_conf);
2212 assoc = get_defcfg_association(def_conf);
2213 if (! assoc)
2214 continue;
2215 if (! assoc_speaker)
2216 assoc_speaker = assoc;
2217 else if (assoc_speaker != assoc)
2218 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002219 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2220 continue;
2221 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002222 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002223 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002224 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002225 case AC_JACK_HP_OUT:
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002226 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2227 continue;
2228 cfg->hp_pins[cfg->hp_outs] = nid;
2229 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002230 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002231 case AC_JACK_MIC_IN: {
2232 int preferred, alt;
2233 if (loc == AC_JACK_LOC_FRONT) {
2234 preferred = AUTO_PIN_FRONT_MIC;
2235 alt = AUTO_PIN_MIC;
2236 } else {
2237 preferred = AUTO_PIN_MIC;
2238 alt = AUTO_PIN_FRONT_MIC;
2239 }
2240 if (!cfg->input_pins[preferred])
2241 cfg->input_pins[preferred] = nid;
2242 else if (!cfg->input_pins[alt])
2243 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002244 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002245 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002246 case AC_JACK_LINE_IN:
2247 if (loc == AC_JACK_LOC_FRONT)
2248 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2249 else
2250 cfg->input_pins[AUTO_PIN_LINE] = nid;
2251 break;
2252 case AC_JACK_CD:
2253 cfg->input_pins[AUTO_PIN_CD] = nid;
2254 break;
2255 case AC_JACK_AUX:
2256 cfg->input_pins[AUTO_PIN_AUX] = nid;
2257 break;
2258 case AC_JACK_SPDIF_OUT:
2259 cfg->dig_out_pin = nid;
2260 break;
2261 case AC_JACK_SPDIF_IN:
2262 cfg->dig_in_pin = nid;
2263 break;
2264 }
2265 }
2266
2267 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02002268 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2269 cfg->line_outs);
2270 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2271 cfg->speaker_outs);
2272
2273 /*
2274 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2275 * as a primary output
2276 */
2277 if (!cfg->line_outs) {
2278 if (cfg->speaker_outs) {
2279 cfg->line_outs = cfg->speaker_outs;
2280 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2281 sizeof(cfg->speaker_pins));
2282 cfg->speaker_outs = 0;
2283 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2284 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2285 } else if (cfg->hp_outs) {
2286 cfg->line_outs = cfg->hp_outs;
2287 memcpy(cfg->line_out_pins, cfg->hp_pins,
2288 sizeof(cfg->hp_pins));
2289 cfg->hp_outs = 0;
2290 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2291 cfg->line_out_type = AUTO_PIN_HP_OUT;
2292 }
2293 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002294
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002295 /* Reorder the surround channels
2296 * ALSA sequence is front/surr/clfe/side
2297 * HDA sequence is:
2298 * 4-ch: front/surr => OK as it is
2299 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02002300 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002301 */
2302 switch (cfg->line_outs) {
2303 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002304 case 4:
2305 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02002306 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002307 cfg->line_out_pins[2] = nid;
2308 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002309 }
2310
Takashi Iwai82bc9552006-03-21 11:24:42 +01002311 /*
2312 * debug prints of the parsed results
2313 */
2314 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2315 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2316 cfg->line_out_pins[2], cfg->line_out_pins[3],
2317 cfg->line_out_pins[4]);
2318 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2319 cfg->speaker_outs, cfg->speaker_pins[0],
2320 cfg->speaker_pins[1], cfg->speaker_pins[2],
2321 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002322 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2323 cfg->hp_outs, cfg->hp_pins[0],
2324 cfg->hp_pins[1], cfg->hp_pins[2],
2325 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002326 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2327 " cd=0x%x, aux=0x%x\n",
2328 cfg->input_pins[AUTO_PIN_MIC],
2329 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2330 cfg->input_pins[AUTO_PIN_LINE],
2331 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2332 cfg->input_pins[AUTO_PIN_CD],
2333 cfg->input_pins[AUTO_PIN_AUX]);
2334
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002335 return 0;
2336}
2337
Takashi Iwai4a471b72005-12-07 13:56:29 +01002338/* labels for input pins */
2339const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2340 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2341};
2342
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344#ifdef CONFIG_PM
2345/*
2346 * power management
2347 */
2348
2349/**
2350 * snd_hda_suspend - suspend the codecs
2351 * @bus: the HDA bus
2352 * @state: suspsend state
2353 *
2354 * Returns 0 if successful.
2355 */
2356int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2357{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002358 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 /* FIXME: should handle power widget capabilities */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002361 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (codec->patch_ops.suspend)
2363 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002364 hda_set_power_state(codec,
2365 codec->afg ? codec->afg : codec->mfg,
2366 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 return 0;
2369}
2370
2371/**
2372 * snd_hda_resume - resume the codecs
2373 * @bus: the HDA bus
2374 * @state: resume state
2375 *
2376 * Returns 0 if successful.
2377 */
2378int snd_hda_resume(struct hda_bus *bus)
2379{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002380 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Takashi Iwai0ba21762007-04-16 11:29:14 +02002382 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002383 hda_set_power_state(codec,
2384 codec->afg ? codec->afg : codec->mfg,
2385 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (codec->patch_ops.resume)
2387 codec->patch_ops.resume(codec);
2388 }
2389 return 0;
2390}
2391
2392/**
2393 * snd_hda_resume_ctls - resume controls in the new control list
2394 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002395 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 *
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002397 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 * originally for snd_hda_add_new_ctls().
2399 * The array must be terminated with an empty entry as terminator.
2400 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002401int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002403 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 val = kmalloc(sizeof(*val), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002406 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return -ENOMEM;
2408 codec->in_resume = 1;
2409 for (; knew->name; knew++) {
2410 int i, count;
2411 count = knew->count ? knew->count : 1;
2412 for (i = 0; i < count; i++) {
2413 memset(val, 0, sizeof(*val));
2414 val->id.iface = knew->iface;
2415 val->id.device = knew->device;
2416 val->id.subdevice = knew->subdevice;
2417 strcpy(val->id.name, knew->name);
2418 val->id.index = knew->index ? knew->index : i;
2419 /* Assume that get callback reads only from cache,
2420 * not accessing to the real hardware
2421 */
2422 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2423 continue;
2424 snd_ctl_elem_write(codec->bus->card, NULL, val);
2425 }
2426 }
2427 codec->in_resume = 0;
2428 kfree(val);
2429 return 0;
2430}
2431
2432/**
2433 * snd_hda_resume_spdif_out - resume the digital out
2434 * @codec: the HDA codec
2435 */
2436int snd_hda_resume_spdif_out(struct hda_codec *codec)
2437{
2438 return snd_hda_resume_ctls(codec, dig_mixes);
2439}
2440
2441/**
2442 * snd_hda_resume_spdif_in - resume the digital in
2443 * @codec: the HDA codec
2444 */
2445int snd_hda_resume_spdif_in(struct hda_codec *codec)
2446{
2447 return snd_hda_resume_ctls(codec, dig_in_ctls);
2448}
2449#endif