Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
62932df8 | 27 | #include <linux/mutex.h> |
1da177e4 LT |
28 | #include <sound/core.h> |
29 | #include "hda_codec.h" | |
30 | #include <sound/asoundef.h> | |
302e9c5a | 31 | #include <sound/tlv.h> |
1da177e4 LT |
32 | #include <sound/initval.h> |
33 | #include "hda_local.h" | |
34 | ||
35 | ||
1da177e4 LT |
36 | /* |
37 | * vendor / preset table | |
38 | */ | |
39 | ||
40 | struct hda_vendor_id { | |
41 | unsigned int id; | |
42 | const char *name; | |
43 | }; | |
44 | ||
45 | /* codec vendor labels */ | |
46 | static struct hda_vendor_id hda_vendor_ids[] = { | |
47 | { 0x10ec, "Realtek" }, | |
a9226251 | 48 | { 0x1057, "Motorola" }, |
c577b8a1 | 49 | { 0x1106, "VIA" }, |
54b903ec | 50 | { 0x11d4, "Analog Devices" }, |
1da177e4 | 51 | { 0x13f6, "C-Media" }, |
a9226251 | 52 | { 0x14f1, "Conexant" }, |
1da177e4 | 53 | { 0x434d, "C-Media" }, |
2f2f4251 | 54 | { 0x8384, "SigmaTel" }, |
1da177e4 LT |
55 | {} /* 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 | */ | |
74 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct, | |
75 | unsigned int verb, unsigned int parm) | |
76 | { | |
77 | unsigned int res; | |
62932df8 | 78 | mutex_lock(&codec->bus->cmd_mutex); |
1da177e4 LT |
79 | if (! codec->bus->ops.command(codec, nid, direct, verb, parm)) |
80 | res = codec->bus->ops.get_response(codec); | |
81 | else | |
82 | res = (unsigned int)-1; | |
62932df8 | 83 | mutex_unlock(&codec->bus->cmd_mutex); |
1da177e4 LT |
84 | return res; |
85 | } | |
86 | ||
87 | /** | |
88 | * snd_hda_codec_write - send a single command without waiting for response | |
89 | * @codec: the HDA codec | |
90 | * @nid: NID to send the command | |
91 | * @direct: direct flag | |
92 | * @verb: the verb to send | |
93 | * @parm: the parameter for the verb | |
94 | * | |
95 | * Send a single command without waiting for response. | |
96 | * | |
97 | * Returns 0 if successful, or a negative error code. | |
98 | */ | |
99 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, | |
100 | unsigned int verb, unsigned int parm) | |
101 | { | |
102 | int err; | |
62932df8 | 103 | mutex_lock(&codec->bus->cmd_mutex); |
1da177e4 | 104 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
62932df8 | 105 | mutex_unlock(&codec->bus->cmd_mutex); |
1da177e4 LT |
106 | return err; |
107 | } | |
108 | ||
109 | /** | |
110 | * snd_hda_sequence_write - sequence writes | |
111 | * @codec: the HDA codec | |
112 | * @seq: VERB array to send | |
113 | * | |
114 | * Send the commands sequentially from the given array. | |
115 | * The array must be terminated with NID=0. | |
116 | */ | |
117 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) | |
118 | { | |
119 | for (; seq->nid; seq++) | |
120 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); | |
121 | } | |
122 | ||
123 | /** | |
124 | * snd_hda_get_sub_nodes - get the range of sub nodes | |
125 | * @codec: the HDA codec | |
126 | * @nid: NID to parse | |
127 | * @start_id: the pointer to store the start NID | |
128 | * | |
129 | * Parse the NID and store the start NID of its sub-nodes. | |
130 | * Returns the number of sub-nodes. | |
131 | */ | |
132 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id) | |
133 | { | |
134 | unsigned int parm; | |
135 | ||
136 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); | |
137 | *start_id = (parm >> 16) & 0x7fff; | |
138 | return (int)(parm & 0x7fff); | |
139 | } | |
140 | ||
141 | /** | |
142 | * snd_hda_get_connections - get connection list | |
143 | * @codec: the HDA codec | |
144 | * @nid: NID to parse | |
145 | * @conn_list: connection list array | |
146 | * @max_conns: max. number of connections to store | |
147 | * | |
148 | * Parses the connection list of the given widget and stores the list | |
149 | * of NIDs. | |
150 | * | |
151 | * Returns the number of connections, or a negative error code. | |
152 | */ | |
153 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, | |
154 | hda_nid_t *conn_list, int max_conns) | |
155 | { | |
156 | unsigned int parm; | |
54d17403 | 157 | int i, conn_len, conns; |
1da177e4 | 158 | unsigned int shift, num_elems, mask; |
54d17403 | 159 | hda_nid_t prev_nid; |
1da177e4 LT |
160 | |
161 | snd_assert(conn_list && max_conns > 0, return -EINVAL); | |
162 | ||
163 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); | |
164 | if (parm & AC_CLIST_LONG) { | |
165 | /* long form */ | |
166 | shift = 16; | |
167 | num_elems = 2; | |
168 | } else { | |
169 | /* short form */ | |
170 | shift = 8; | |
171 | num_elems = 4; | |
172 | } | |
173 | conn_len = parm & AC_CLIST_LENGTH; | |
1da177e4 LT |
174 | mask = (1 << (shift-1)) - 1; |
175 | ||
176 | if (! conn_len) | |
177 | return 0; /* no connection */ | |
178 | ||
179 | if (conn_len == 1) { | |
180 | /* single connection */ | |
181 | parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0); | |
182 | conn_list[0] = parm & mask; | |
183 | return 1; | |
184 | } | |
185 | ||
186 | /* multi connection */ | |
187 | conns = 0; | |
54d17403 TI |
188 | prev_nid = 0; |
189 | for (i = 0; i < conn_len; i++) { | |
190 | int range_val; | |
191 | hda_nid_t val, n; | |
192 | ||
193 | if (i % num_elems == 0) | |
194 | parm = snd_hda_codec_read(codec, nid, 0, | |
195 | AC_VERB_GET_CONNECT_LIST, i); | |
196 | range_val = !! (parm & (1 << (shift-1))); /* ranges */ | |
197 | val = parm & mask; | |
198 | parm >>= shift; | |
199 | if (range_val) { | |
200 | /* ranges between the previous and this one */ | |
201 | if (! prev_nid || prev_nid >= val) { | |
202 | snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val); | |
203 | continue; | |
204 | } | |
205 | for (n = prev_nid + 1; n <= val; n++) { | |
206 | if (conns >= max_conns) { | |
207 | snd_printk(KERN_ERR "Too many connections\n"); | |
1da177e4 | 208 | return -EINVAL; |
54d17403 TI |
209 | } |
210 | conn_list[conns++] = n; | |
1da177e4 | 211 | } |
54d17403 TI |
212 | } else { |
213 | if (conns >= max_conns) { | |
214 | snd_printk(KERN_ERR "Too many connections\n"); | |
215 | return -EINVAL; | |
216 | } | |
217 | conn_list[conns++] = val; | |
1da177e4 | 218 | } |
54d17403 | 219 | prev_nid = val; |
1da177e4 LT |
220 | } |
221 | return conns; | |
222 | } | |
223 | ||
224 | ||
225 | /** | |
226 | * snd_hda_queue_unsol_event - add an unsolicited event to queue | |
227 | * @bus: the BUS | |
228 | * @res: unsolicited event (lower 32bit of RIRB entry) | |
229 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) | |
230 | * | |
231 | * Adds the given event to the queue. The events are processed in | |
232 | * the workqueue asynchronously. Call this function in the interrupt | |
233 | * hanlder when RIRB receives an unsolicited event. | |
234 | * | |
235 | * Returns 0 if successful, or a negative error code. | |
236 | */ | |
237 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) | |
238 | { | |
239 | struct hda_bus_unsolicited *unsol; | |
240 | unsigned int wp; | |
241 | ||
242 | if ((unsol = bus->unsol) == NULL) | |
243 | return 0; | |
244 | ||
245 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; | |
246 | unsol->wp = wp; | |
247 | ||
248 | wp <<= 1; | |
249 | unsol->queue[wp] = res; | |
250 | unsol->queue[wp + 1] = res_ex; | |
251 | ||
e250af29 | 252 | schedule_work(&unsol->work); |
1da177e4 LT |
253 | |
254 | return 0; | |
255 | } | |
256 | ||
257 | /* | |
258 | * process queueud unsolicited events | |
259 | */ | |
c4028958 | 260 | static void process_unsol_events(struct work_struct *work) |
1da177e4 | 261 | { |
c4028958 DH |
262 | struct hda_bus_unsolicited *unsol = |
263 | container_of(work, struct hda_bus_unsolicited, work); | |
264 | struct hda_bus *bus = unsol->bus; | |
1da177e4 LT |
265 | struct hda_codec *codec; |
266 | unsigned int rp, caddr, res; | |
267 | ||
268 | while (unsol->rp != unsol->wp) { | |
269 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; | |
270 | unsol->rp = rp; | |
271 | rp <<= 1; | |
272 | res = unsol->queue[rp]; | |
273 | caddr = unsol->queue[rp + 1]; | |
274 | if (! (caddr & (1 << 4))) /* no unsolicited event? */ | |
275 | continue; | |
276 | codec = bus->caddr_tbl[caddr & 0x0f]; | |
277 | if (codec && codec->patch_ops.unsol_event) | |
278 | codec->patch_ops.unsol_event(codec, res); | |
279 | } | |
280 | } | |
281 | ||
282 | /* | |
283 | * initialize unsolicited queue | |
284 | */ | |
756e2b01 | 285 | static int __devinit init_unsol_queue(struct hda_bus *bus) |
1da177e4 LT |
286 | { |
287 | struct hda_bus_unsolicited *unsol; | |
288 | ||
9f146bb6 TI |
289 | if (bus->unsol) /* already initialized */ |
290 | return 0; | |
291 | ||
e560d8d8 | 292 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
1da177e4 LT |
293 | if (! unsol) { |
294 | snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n"); | |
295 | return -ENOMEM; | |
296 | } | |
c4028958 DH |
297 | INIT_WORK(&unsol->work, process_unsol_events); |
298 | unsol->bus = bus; | |
1da177e4 LT |
299 | bus->unsol = unsol; |
300 | return 0; | |
301 | } | |
302 | ||
303 | /* | |
304 | * destructor | |
305 | */ | |
306 | static void snd_hda_codec_free(struct hda_codec *codec); | |
307 | ||
308 | static int snd_hda_bus_free(struct hda_bus *bus) | |
309 | { | |
310 | struct list_head *p, *n; | |
311 | ||
312 | if (! bus) | |
313 | return 0; | |
314 | if (bus->unsol) { | |
e250af29 | 315 | flush_scheduled_work(); |
1da177e4 LT |
316 | kfree(bus->unsol); |
317 | } | |
318 | list_for_each_safe(p, n, &bus->codec_list) { | |
319 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
320 | snd_hda_codec_free(codec); | |
321 | } | |
322 | if (bus->ops.private_free) | |
323 | bus->ops.private_free(bus); | |
324 | kfree(bus); | |
325 | return 0; | |
326 | } | |
327 | ||
c8b6bf9b | 328 | static int snd_hda_bus_dev_free(struct snd_device *device) |
1da177e4 LT |
329 | { |
330 | struct hda_bus *bus = device->device_data; | |
331 | return snd_hda_bus_free(bus); | |
332 | } | |
333 | ||
334 | /** | |
335 | * snd_hda_bus_new - create a HDA bus | |
336 | * @card: the card entry | |
337 | * @temp: the template for hda_bus information | |
338 | * @busp: the pointer to store the created bus instance | |
339 | * | |
340 | * Returns 0 if successful, or a negative error code. | |
341 | */ | |
756e2b01 TI |
342 | int __devinit snd_hda_bus_new(struct snd_card *card, |
343 | const struct hda_bus_template *temp, | |
344 | struct hda_bus **busp) | |
1da177e4 LT |
345 | { |
346 | struct hda_bus *bus; | |
347 | int err; | |
c8b6bf9b | 348 | static struct snd_device_ops dev_ops = { |
1da177e4 LT |
349 | .dev_free = snd_hda_bus_dev_free, |
350 | }; | |
351 | ||
352 | snd_assert(temp, return -EINVAL); | |
353 | snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL); | |
354 | ||
355 | if (busp) | |
356 | *busp = NULL; | |
357 | ||
e560d8d8 | 358 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
1da177e4 LT |
359 | if (bus == NULL) { |
360 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); | |
361 | return -ENOMEM; | |
362 | } | |
363 | ||
364 | bus->card = card; | |
365 | bus->private_data = temp->private_data; | |
366 | bus->pci = temp->pci; | |
367 | bus->modelname = temp->modelname; | |
368 | bus->ops = temp->ops; | |
369 | ||
62932df8 | 370 | mutex_init(&bus->cmd_mutex); |
1da177e4 LT |
371 | INIT_LIST_HEAD(&bus->codec_list); |
372 | ||
1da177e4 LT |
373 | if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) { |
374 | snd_hda_bus_free(bus); | |
375 | return err; | |
376 | } | |
377 | if (busp) | |
378 | *busp = bus; | |
379 | return 0; | |
380 | } | |
381 | ||
1da177e4 LT |
382 | /* |
383 | * find a matching codec preset | |
384 | */ | |
756e2b01 TI |
385 | static const struct hda_codec_preset __devinit * |
386 | find_codec_preset(struct hda_codec *codec) | |
1da177e4 LT |
387 | { |
388 | const struct hda_codec_preset **tbl, *preset; | |
389 | ||
d5ad630b TI |
390 | if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic")) |
391 | return NULL; /* use the generic parser */ | |
392 | ||
1da177e4 LT |
393 | for (tbl = hda_preset_tables; *tbl; tbl++) { |
394 | for (preset = *tbl; preset->id; preset++) { | |
395 | u32 mask = preset->mask; | |
396 | if (! mask) | |
397 | mask = ~0; | |
9c7f852e TI |
398 | if (preset->id == (codec->vendor_id & mask) && |
399 | (! preset->rev || | |
400 | preset->rev == codec->revision_id)) | |
1da177e4 LT |
401 | return preset; |
402 | } | |
403 | } | |
404 | return NULL; | |
405 | } | |
406 | ||
407 | /* | |
408 | * snd_hda_get_codec_name - store the codec name | |
409 | */ | |
410 | void snd_hda_get_codec_name(struct hda_codec *codec, | |
411 | char *name, int namelen) | |
412 | { | |
413 | const struct hda_vendor_id *c; | |
414 | const char *vendor = NULL; | |
415 | u16 vendor_id = codec->vendor_id >> 16; | |
416 | char tmp[16]; | |
417 | ||
418 | for (c = hda_vendor_ids; c->id; c++) { | |
419 | if (c->id == vendor_id) { | |
420 | vendor = c->name; | |
421 | break; | |
422 | } | |
423 | } | |
424 | if (! vendor) { | |
425 | sprintf(tmp, "Generic %04x", vendor_id); | |
426 | vendor = tmp; | |
427 | } | |
428 | if (codec->preset && codec->preset->name) | |
429 | snprintf(name, namelen, "%s %s", vendor, codec->preset->name); | |
430 | else | |
431 | snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff); | |
432 | } | |
433 | ||
434 | /* | |
673b683a | 435 | * look for an AFG and MFG nodes |
1da177e4 | 436 | */ |
756e2b01 | 437 | static void __devinit setup_fg_nodes(struct hda_codec *codec) |
1da177e4 LT |
438 | { |
439 | int i, total_nodes; | |
440 | hda_nid_t nid; | |
441 | ||
442 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | |
443 | for (i = 0; i < total_nodes; i++, nid++) { | |
673b683a SK |
444 | switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) { |
445 | case AC_GRP_AUDIO_FUNCTION: | |
446 | codec->afg = nid; | |
447 | break; | |
448 | case AC_GRP_MODEM_FUNCTION: | |
449 | codec->mfg = nid; | |
450 | break; | |
451 | default: | |
452 | break; | |
453 | } | |
1da177e4 | 454 | } |
1da177e4 LT |
455 | } |
456 | ||
54d17403 TI |
457 | /* |
458 | * read widget caps for each widget and store in cache | |
459 | */ | |
460 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) | |
461 | { | |
462 | int i; | |
463 | hda_nid_t nid; | |
464 | ||
465 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, | |
466 | &codec->start_nid); | |
467 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); | |
468 | if (! codec->wcaps) | |
469 | return -ENOMEM; | |
470 | nid = codec->start_nid; | |
471 | for (i = 0; i < codec->num_nodes; i++, nid++) | |
472 | codec->wcaps[i] = snd_hda_param_read(codec, nid, | |
473 | AC_PAR_AUDIO_WIDGET_CAP); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | ||
1da177e4 LT |
478 | /* |
479 | * codec destructor | |
480 | */ | |
481 | static void snd_hda_codec_free(struct hda_codec *codec) | |
482 | { | |
483 | if (! codec) | |
484 | return; | |
485 | list_del(&codec->list); | |
486 | codec->bus->caddr_tbl[codec->addr] = NULL; | |
487 | if (codec->patch_ops.free) | |
488 | codec->patch_ops.free(codec); | |
d031166f | 489 | kfree(codec->amp_info); |
54d17403 | 490 | kfree(codec->wcaps); |
1da177e4 LT |
491 | kfree(codec); |
492 | } | |
493 | ||
494 | static void init_amp_hash(struct hda_codec *codec); | |
495 | ||
496 | /** | |
497 | * snd_hda_codec_new - create a HDA codec | |
498 | * @bus: the bus to assign | |
499 | * @codec_addr: the codec address | |
500 | * @codecp: the pointer to store the generated codec | |
501 | * | |
502 | * Returns 0 if successful, or a negative error code. | |
503 | */ | |
756e2b01 TI |
504 | int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr, |
505 | struct hda_codec **codecp) | |
1da177e4 LT |
506 | { |
507 | struct hda_codec *codec; | |
508 | char component[13]; | |
509 | int err; | |
510 | ||
511 | snd_assert(bus, return -EINVAL); | |
512 | snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL); | |
513 | ||
514 | if (bus->caddr_tbl[codec_addr]) { | |
515 | snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr); | |
516 | return -EBUSY; | |
517 | } | |
518 | ||
e560d8d8 | 519 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
1da177e4 LT |
520 | if (codec == NULL) { |
521 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); | |
522 | return -ENOMEM; | |
523 | } | |
524 | ||
525 | codec->bus = bus; | |
526 | codec->addr = codec_addr; | |
62932df8 | 527 | mutex_init(&codec->spdif_mutex); |
1da177e4 LT |
528 | init_amp_hash(codec); |
529 | ||
530 | list_add_tail(&codec->list, &bus->codec_list); | |
531 | bus->caddr_tbl[codec_addr] = codec; | |
532 | ||
533 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID); | |
111d3af5 TI |
534 | if (codec->vendor_id == -1) |
535 | /* read again, hopefully the access method was corrected | |
536 | * in the last read... | |
537 | */ | |
538 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, | |
539 | AC_PAR_VENDOR_ID); | |
1da177e4 LT |
540 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID); |
541 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID); | |
542 | ||
673b683a SK |
543 | setup_fg_nodes(codec); |
544 | if (! codec->afg && ! codec->mfg) { | |
545 | snd_printdd("hda_codec: no AFG or MFG node found\n"); | |
1da177e4 LT |
546 | snd_hda_codec_free(codec); |
547 | return -ENODEV; | |
548 | } | |
549 | ||
54d17403 TI |
550 | if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) { |
551 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); | |
552 | snd_hda_codec_free(codec); | |
553 | return -ENOMEM; | |
554 | } | |
555 | ||
86284e45 TI |
556 | if (! codec->subsystem_id) { |
557 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; | |
558 | codec->subsystem_id = snd_hda_codec_read(codec, nid, 0, | |
559 | AC_VERB_GET_SUBSYSTEM_ID, | |
560 | 0); | |
561 | } | |
562 | ||
d5ad630b | 563 | codec->preset = find_codec_preset(codec); |
1da177e4 LT |
564 | if (! *bus->card->mixername) |
565 | snd_hda_get_codec_name(codec, bus->card->mixername, | |
566 | sizeof(bus->card->mixername)); | |
567 | ||
568 | if (codec->preset && codec->preset->patch) | |
569 | err = codec->preset->patch(codec); | |
570 | else | |
571 | err = snd_hda_parse_generic_codec(codec); | |
572 | if (err < 0) { | |
573 | snd_hda_codec_free(codec); | |
574 | return err; | |
575 | } | |
576 | ||
9f146bb6 TI |
577 | if (codec->patch_ops.unsol_event) |
578 | init_unsol_queue(bus); | |
579 | ||
1da177e4 LT |
580 | snd_hda_codec_proc_new(codec); |
581 | ||
582 | sprintf(component, "HDA:%08x", codec->vendor_id); | |
583 | snd_component_add(codec->bus->card, component); | |
584 | ||
585 | if (codecp) | |
586 | *codecp = codec; | |
587 | return 0; | |
588 | } | |
589 | ||
590 | /** | |
591 | * snd_hda_codec_setup_stream - set up the codec for streaming | |
592 | * @codec: the CODEC to set up | |
593 | * @nid: the NID to set up | |
594 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. | |
595 | * @channel_id: channel id to pass, zero based. | |
596 | * @format: stream format. | |
597 | */ | |
598 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag, | |
599 | int channel_id, int format) | |
600 | { | |
d21b37ea TI |
601 | if (! nid) |
602 | return; | |
603 | ||
1da177e4 LT |
604 | snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
605 | nid, stream_tag, channel_id, format); | |
606 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, | |
607 | (stream_tag << 4) | channel_id); | |
608 | msleep(1); | |
609 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format); | |
610 | } | |
611 | ||
1da177e4 LT |
612 | /* |
613 | * amp access functions | |
614 | */ | |
615 | ||
4a19faee TI |
616 | /* FIXME: more better hash key? */ |
617 | #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) | |
1da177e4 | 618 | #define INFO_AMP_CAPS (1<<0) |
4a19faee | 619 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
1da177e4 LT |
620 | |
621 | /* initialize the hash table */ | |
756e2b01 | 622 | static void __devinit init_amp_hash(struct hda_codec *codec) |
1da177e4 LT |
623 | { |
624 | memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash)); | |
625 | codec->num_amp_entries = 0; | |
d031166f TI |
626 | codec->amp_info_size = 0; |
627 | codec->amp_info = NULL; | |
1da177e4 LT |
628 | } |
629 | ||
630 | /* query the hash. allocate an entry if not found. */ | |
631 | static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key) | |
632 | { | |
633 | u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash); | |
634 | u16 cur = codec->amp_hash[idx]; | |
635 | struct hda_amp_info *info; | |
636 | ||
637 | while (cur != 0xffff) { | |
638 | info = &codec->amp_info[cur]; | |
639 | if (info->key == key) | |
640 | return info; | |
641 | cur = info->next; | |
642 | } | |
643 | ||
644 | /* add a new hash entry */ | |
d031166f TI |
645 | if (codec->num_amp_entries >= codec->amp_info_size) { |
646 | /* reallocate the array */ | |
647 | int new_size = codec->amp_info_size + 64; | |
648 | struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info), | |
649 | GFP_KERNEL); | |
650 | if (! new_info) { | |
651 | snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n"); | |
652 | return NULL; | |
653 | } | |
654 | if (codec->amp_info) { | |
655 | memcpy(new_info, codec->amp_info, | |
656 | codec->amp_info_size * sizeof(struct hda_amp_info)); | |
657 | kfree(codec->amp_info); | |
658 | } | |
659 | codec->amp_info_size = new_size; | |
660 | codec->amp_info = new_info; | |
1da177e4 LT |
661 | } |
662 | cur = codec->num_amp_entries++; | |
663 | info = &codec->amp_info[cur]; | |
664 | info->key = key; | |
665 | info->status = 0; /* not initialized yet */ | |
666 | info->next = codec->amp_hash[idx]; | |
667 | codec->amp_hash[idx] = cur; | |
668 | ||
669 | return info; | |
670 | } | |
671 | ||
672 | /* | |
673 | * query AMP capabilities for the given widget and direction | |
674 | */ | |
675 | static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) | |
676 | { | |
677 | struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); | |
678 | ||
679 | if (! info) | |
680 | return 0; | |
681 | if (! (info->status & INFO_AMP_CAPS)) { | |
54d17403 | 682 | if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
1da177e4 LT |
683 | nid = codec->afg; |
684 | info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ? | |
685 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); | |
686 | info->status |= INFO_AMP_CAPS; | |
687 | } | |
688 | return info->amp_caps; | |
689 | } | |
690 | ||
691 | /* | |
692 | * read the current volume to info | |
4a19faee | 693 | * if the cache exists, read the cache value. |
1da177e4 | 694 | */ |
4a19faee | 695 | static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, |
1da177e4 LT |
696 | hda_nid_t nid, int ch, int direction, int index) |
697 | { | |
698 | u32 val, parm; | |
699 | ||
4a19faee TI |
700 | if (info->status & INFO_AMP_VOL(ch)) |
701 | return info->vol[ch]; | |
1da177e4 LT |
702 | |
703 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; | |
704 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; | |
705 | parm |= index; | |
706 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm); | |
707 | info->vol[ch] = val & 0xff; | |
4a19faee TI |
708 | info->status |= INFO_AMP_VOL(ch); |
709 | return info->vol[ch]; | |
1da177e4 LT |
710 | } |
711 | ||
712 | /* | |
4a19faee | 713 | * write the current volume in info to the h/w and update the cache |
1da177e4 | 714 | */ |
4a19faee | 715 | static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, |
1da177e4 LT |
716 | hda_nid_t nid, int ch, int direction, int index, int val) |
717 | { | |
718 | u32 parm; | |
719 | ||
720 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; | |
721 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; | |
722 | parm |= index << AC_AMP_SET_INDEX_SHIFT; | |
723 | parm |= val; | |
724 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); | |
4a19faee | 725 | info->vol[ch] = val; |
1da177e4 LT |
726 | } |
727 | ||
728 | /* | |
4a19faee | 729 | * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit. |
1da177e4 | 730 | */ |
834be88d TI |
731 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
732 | int direction, int index) | |
1da177e4 LT |
733 | { |
734 | struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); | |
735 | if (! info) | |
736 | return 0; | |
4a19faee | 737 | return get_vol_mute(codec, info, nid, ch, direction, index); |
1da177e4 LT |
738 | } |
739 | ||
4a19faee TI |
740 | /* |
741 | * update the AMP value, mask = bit mask to set, val = the value | |
742 | */ | |
834be88d TI |
743 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
744 | int direction, int idx, int mask, int val) | |
1da177e4 LT |
745 | { |
746 | struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); | |
4a19faee | 747 | |
1da177e4 LT |
748 | if (! info) |
749 | return 0; | |
4a19faee TI |
750 | val &= mask; |
751 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; | |
1da177e4 LT |
752 | if (info->vol[ch] == val && ! codec->in_resume) |
753 | return 0; | |
4a19faee | 754 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
1da177e4 LT |
755 | return 1; |
756 | } | |
757 | ||
758 | ||
759 | /* | |
760 | * AMP control callbacks | |
761 | */ | |
762 | /* retrieve parameters from private_value */ | |
763 | #define get_amp_nid(kc) ((kc)->private_value & 0xffff) | |
764 | #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3) | |
765 | #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1) | |
766 | #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf) | |
767 | ||
768 | /* volume */ | |
c8b6bf9b | 769 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
770 | { |
771 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
772 | u16 nid = get_amp_nid(kcontrol); | |
773 | u8 chs = get_amp_channels(kcontrol); | |
774 | int dir = get_amp_direction(kcontrol); | |
775 | u32 caps; | |
776 | ||
777 | caps = query_amp_caps(codec, nid, dir); | |
778 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */ | |
779 | if (! caps) { | |
780 | printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid); | |
781 | return -EINVAL; | |
782 | } | |
783 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
784 | uinfo->count = chs == 3 ? 2 : 1; | |
785 | uinfo->value.integer.min = 0; | |
786 | uinfo->value.integer.max = caps; | |
787 | return 0; | |
788 | } | |
789 | ||
c8b6bf9b | 790 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
791 | { |
792 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
793 | hda_nid_t nid = get_amp_nid(kcontrol); | |
794 | int chs = get_amp_channels(kcontrol); | |
795 | int dir = get_amp_direction(kcontrol); | |
796 | int idx = get_amp_index(kcontrol); | |
797 | long *valp = ucontrol->value.integer.value; | |
798 | ||
799 | if (chs & 1) | |
800 | *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f; | |
801 | if (chs & 2) | |
802 | *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f; | |
803 | return 0; | |
804 | } | |
805 | ||
c8b6bf9b | 806 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
807 | { |
808 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
809 | hda_nid_t nid = get_amp_nid(kcontrol); | |
810 | int chs = get_amp_channels(kcontrol); | |
811 | int dir = get_amp_direction(kcontrol); | |
812 | int idx = get_amp_index(kcontrol); | |
1da177e4 LT |
813 | long *valp = ucontrol->value.integer.value; |
814 | int change = 0; | |
815 | ||
b9f5a89c | 816 | if (chs & 1) { |
4a19faee TI |
817 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
818 | 0x7f, *valp); | |
b9f5a89c NG |
819 | valp++; |
820 | } | |
4a19faee TI |
821 | if (chs & 2) |
822 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, | |
b9f5a89c | 823 | 0x7f, *valp); |
1da177e4 LT |
824 | return change; |
825 | } | |
826 | ||
302e9c5a JK |
827 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
828 | unsigned int size, unsigned int __user *_tlv) | |
829 | { | |
830 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
831 | hda_nid_t nid = get_amp_nid(kcontrol); | |
832 | int dir = get_amp_direction(kcontrol); | |
833 | u32 caps, val1, val2; | |
834 | ||
835 | if (size < 4 * sizeof(unsigned int)) | |
836 | return -ENOMEM; | |
837 | caps = query_amp_caps(codec, nid, dir); | |
838 | val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25; | |
839 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); | |
840 | val1 = ((int)val1) * ((int)val2); | |
302e9c5a JK |
841 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
842 | return -EFAULT; | |
843 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) | |
844 | return -EFAULT; | |
845 | if (put_user(val1, _tlv + 2)) | |
846 | return -EFAULT; | |
847 | if (put_user(val2, _tlv + 3)) | |
848 | return -EFAULT; | |
849 | return 0; | |
850 | } | |
851 | ||
1da177e4 | 852 | /* switch */ |
c8b6bf9b | 853 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
854 | { |
855 | int chs = get_amp_channels(kcontrol); | |
856 | ||
857 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
858 | uinfo->count = chs == 3 ? 2 : 1; | |
859 | uinfo->value.integer.min = 0; | |
860 | uinfo->value.integer.max = 1; | |
861 | return 0; | |
862 | } | |
863 | ||
c8b6bf9b | 864 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
865 | { |
866 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
867 | hda_nid_t nid = get_amp_nid(kcontrol); | |
868 | int chs = get_amp_channels(kcontrol); | |
869 | int dir = get_amp_direction(kcontrol); | |
870 | int idx = get_amp_index(kcontrol); | |
871 | long *valp = ucontrol->value.integer.value; | |
872 | ||
873 | if (chs & 1) | |
874 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1; | |
875 | if (chs & 2) | |
876 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1; | |
877 | return 0; | |
878 | } | |
879 | ||
c8b6bf9b | 880 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
881 | { |
882 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
883 | hda_nid_t nid = get_amp_nid(kcontrol); | |
884 | int chs = get_amp_channels(kcontrol); | |
885 | int dir = get_amp_direction(kcontrol); | |
886 | int idx = get_amp_index(kcontrol); | |
1da177e4 LT |
887 | long *valp = ucontrol->value.integer.value; |
888 | int change = 0; | |
889 | ||
b9f5a89c | 890 | if (chs & 1) { |
4a19faee TI |
891 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
892 | 0x80, *valp ? 0 : 0x80); | |
b9f5a89c NG |
893 | valp++; |
894 | } | |
4a19faee TI |
895 | if (chs & 2) |
896 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, | |
b9f5a89c NG |
897 | 0x80, *valp ? 0 : 0x80); |
898 | ||
1da177e4 LT |
899 | return change; |
900 | } | |
901 | ||
985be54b TI |
902 | /* |
903 | * bound volume controls | |
904 | * | |
905 | * bind multiple volumes (# indices, from 0) | |
906 | */ | |
907 | ||
908 | #define AMP_VAL_IDX_SHIFT 19 | |
909 | #define AMP_VAL_IDX_MASK (0x0f<<19) | |
910 | ||
c8b6bf9b | 911 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
985be54b TI |
912 | { |
913 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
914 | unsigned long pval; | |
915 | int err; | |
916 | ||
62932df8 | 917 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
985be54b TI |
918 | pval = kcontrol->private_value; |
919 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ | |
920 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); | |
921 | kcontrol->private_value = pval; | |
62932df8 | 922 | mutex_unlock(&codec->spdif_mutex); |
985be54b TI |
923 | return err; |
924 | } | |
925 | ||
c8b6bf9b | 926 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
985be54b TI |
927 | { |
928 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
929 | unsigned long pval; | |
930 | int i, indices, err = 0, change = 0; | |
931 | ||
62932df8 | 932 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
985be54b TI |
933 | pval = kcontrol->private_value; |
934 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; | |
935 | for (i = 0; i < indices; i++) { | |
936 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT); | |
937 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); | |
938 | if (err < 0) | |
939 | break; | |
940 | change |= err; | |
941 | } | |
942 | kcontrol->private_value = pval; | |
62932df8 | 943 | mutex_unlock(&codec->spdif_mutex); |
985be54b TI |
944 | return err < 0 ? err : change; |
945 | } | |
946 | ||
1da177e4 LT |
947 | /* |
948 | * SPDIF out controls | |
949 | */ | |
950 | ||
c8b6bf9b | 951 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
952 | { |
953 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
954 | uinfo->count = 1; | |
955 | return 0; | |
956 | } | |
957 | ||
c8b6bf9b | 958 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
959 | { |
960 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
961 | IEC958_AES0_NONAUDIO | | |
962 | IEC958_AES0_CON_EMPHASIS_5015 | | |
963 | IEC958_AES0_CON_NOT_COPYRIGHT; | |
964 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | | |
965 | IEC958_AES1_CON_ORIGINAL; | |
966 | return 0; | |
967 | } | |
968 | ||
c8b6bf9b | 969 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
970 | { |
971 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
972 | IEC958_AES0_NONAUDIO | | |
973 | IEC958_AES0_PRO_EMPHASIS_5015; | |
974 | return 0; | |
975 | } | |
976 | ||
c8b6bf9b | 977 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
978 | { |
979 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
980 | ||
981 | ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff; | |
982 | ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff; | |
983 | ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff; | |
984 | ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff; | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | /* convert from SPDIF status bits to HDA SPDIF bits | |
990 | * bit 0 (DigEn) is always set zero (to be filled later) | |
991 | */ | |
992 | static unsigned short convert_from_spdif_status(unsigned int sbits) | |
993 | { | |
994 | unsigned short val = 0; | |
995 | ||
996 | if (sbits & IEC958_AES0_PROFESSIONAL) | |
997 | val |= 1 << 6; | |
998 | if (sbits & IEC958_AES0_NONAUDIO) | |
999 | val |= 1 << 5; | |
1000 | if (sbits & IEC958_AES0_PROFESSIONAL) { | |
1001 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015) | |
1002 | val |= 1 << 3; | |
1003 | } else { | |
1004 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015) | |
1005 | val |= 1 << 3; | |
1006 | if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) | |
1007 | val |= 1 << 4; | |
1008 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) | |
1009 | val |= 1 << 7; | |
1010 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); | |
1011 | } | |
1012 | return val; | |
1013 | } | |
1014 | ||
1015 | /* convert to SPDIF status bits from HDA SPDIF bits | |
1016 | */ | |
1017 | static unsigned int convert_to_spdif_status(unsigned short val) | |
1018 | { | |
1019 | unsigned int sbits = 0; | |
1020 | ||
1021 | if (val & (1 << 5)) | |
1022 | sbits |= IEC958_AES0_NONAUDIO; | |
1023 | if (val & (1 << 6)) | |
1024 | sbits |= IEC958_AES0_PROFESSIONAL; | |
1025 | if (sbits & IEC958_AES0_PROFESSIONAL) { | |
1026 | if (sbits & (1 << 3)) | |
1027 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; | |
1028 | } else { | |
1029 | if (val & (1 << 3)) | |
1030 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; | |
1031 | if (! (val & (1 << 4))) | |
1032 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; | |
1033 | if (val & (1 << 7)) | |
1034 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); | |
1035 | sbits |= val & (0x7f << 8); | |
1036 | } | |
1037 | return sbits; | |
1038 | } | |
1039 | ||
c8b6bf9b | 1040 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1041 | { |
1042 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1043 | hda_nid_t nid = kcontrol->private_value; | |
1044 | unsigned short val; | |
1045 | int change; | |
1046 | ||
62932df8 | 1047 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
1048 | codec->spdif_status = ucontrol->value.iec958.status[0] | |
1049 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | | |
1050 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | | |
1051 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); | |
1052 | val = convert_from_spdif_status(codec->spdif_status); | |
1053 | val |= codec->spdif_ctls & 1; | |
1054 | change = codec->spdif_ctls != val; | |
1055 | codec->spdif_ctls = val; | |
1056 | ||
1057 | if (change || codec->in_resume) { | |
1058 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff); | |
1059 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8); | |
1060 | } | |
1061 | ||
62932df8 | 1062 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1063 | return change; |
1064 | } | |
1065 | ||
c8b6bf9b | 1066 | static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1067 | { |
1068 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1069 | uinfo->count = 1; | |
1070 | uinfo->value.integer.min = 0; | |
1071 | uinfo->value.integer.max = 1; | |
1072 | return 0; | |
1073 | } | |
1074 | ||
c8b6bf9b | 1075 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1076 | { |
1077 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1078 | ||
1079 | ucontrol->value.integer.value[0] = codec->spdif_ctls & 1; | |
1080 | return 0; | |
1081 | } | |
1082 | ||
c8b6bf9b | 1083 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1084 | { |
1085 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1086 | hda_nid_t nid = kcontrol->private_value; | |
1087 | unsigned short val; | |
1088 | int change; | |
1089 | ||
62932df8 | 1090 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
1091 | val = codec->spdif_ctls & ~1; |
1092 | if (ucontrol->value.integer.value[0]) | |
1093 | val |= 1; | |
1094 | change = codec->spdif_ctls != val; | |
1095 | if (change || codec->in_resume) { | |
1096 | codec->spdif_ctls = val; | |
6b97eb45 TI |
1097 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
1098 | val & 0xff); | |
1099 | if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) | |
1100 | snd_hda_codec_write(codec, nid, 0, | |
1101 | AC_VERB_SET_AMP_GAIN_MUTE, | |
1102 | AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | | |
1103 | AC_AMP_SET_OUTPUT | | |
1104 | ((val & 1) ? 0 : 0x80)); | |
1da177e4 | 1105 | } |
62932df8 | 1106 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1107 | return change; |
1108 | } | |
1109 | ||
c8b6bf9b | 1110 | static struct snd_kcontrol_new dig_mixes[] = { |
1da177e4 LT |
1111 | { |
1112 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1113 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1114 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), | |
1115 | .info = snd_hda_spdif_mask_info, | |
1116 | .get = snd_hda_spdif_cmask_get, | |
1117 | }, | |
1118 | { | |
1119 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1120 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1121 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), | |
1122 | .info = snd_hda_spdif_mask_info, | |
1123 | .get = snd_hda_spdif_pmask_get, | |
1124 | }, | |
1125 | { | |
1126 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1127 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), | |
1128 | .info = snd_hda_spdif_mask_info, | |
1129 | .get = snd_hda_spdif_default_get, | |
1130 | .put = snd_hda_spdif_default_put, | |
1131 | }, | |
1132 | { | |
1133 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1134 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), | |
1135 | .info = snd_hda_spdif_out_switch_info, | |
1136 | .get = snd_hda_spdif_out_switch_get, | |
1137 | .put = snd_hda_spdif_out_switch_put, | |
1138 | }, | |
1139 | { } /* end */ | |
1140 | }; | |
1141 | ||
1142 | /** | |
1143 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls | |
1144 | * @codec: the HDA codec | |
1145 | * @nid: audio out widget NID | |
1146 | * | |
1147 | * Creates controls related with the SPDIF output. | |
1148 | * Called from each patch supporting the SPDIF out. | |
1149 | * | |
1150 | * Returns 0 if successful, or a negative error code. | |
1151 | */ | |
756e2b01 TI |
1152 | int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec, |
1153 | hda_nid_t nid) | |
1da177e4 LT |
1154 | { |
1155 | int err; | |
c8b6bf9b TI |
1156 | struct snd_kcontrol *kctl; |
1157 | struct snd_kcontrol_new *dig_mix; | |
1da177e4 LT |
1158 | |
1159 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { | |
1160 | kctl = snd_ctl_new1(dig_mix, codec); | |
1161 | kctl->private_value = nid; | |
1162 | if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0) | |
1163 | return err; | |
1164 | } | |
1165 | codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0); | |
1166 | codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls); | |
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | /* | |
1171 | * SPDIF input | |
1172 | */ | |
1173 | ||
1174 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info | |
1175 | ||
c8b6bf9b | 1176 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1177 | { |
1178 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1179 | ||
1180 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; | |
1181 | return 0; | |
1182 | } | |
1183 | ||
c8b6bf9b | 1184 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1185 | { |
1186 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1187 | hda_nid_t nid = kcontrol->private_value; | |
1188 | unsigned int val = !!ucontrol->value.integer.value[0]; | |
1189 | int change; | |
1190 | ||
62932df8 | 1191 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
1192 | change = codec->spdif_in_enable != val; |
1193 | if (change || codec->in_resume) { | |
1194 | codec->spdif_in_enable = val; | |
1195 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val); | |
1196 | } | |
62932df8 | 1197 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1198 | return change; |
1199 | } | |
1200 | ||
c8b6bf9b | 1201 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
1da177e4 LT |
1202 | { |
1203 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1204 | hda_nid_t nid = kcontrol->private_value; | |
1205 | unsigned short val; | |
1206 | unsigned int sbits; | |
1207 | ||
1208 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0); | |
1209 | sbits = convert_to_spdif_status(val); | |
1210 | ucontrol->value.iec958.status[0] = sbits; | |
1211 | ucontrol->value.iec958.status[1] = sbits >> 8; | |
1212 | ucontrol->value.iec958.status[2] = sbits >> 16; | |
1213 | ucontrol->value.iec958.status[3] = sbits >> 24; | |
1214 | return 0; | |
1215 | } | |
1216 | ||
c8b6bf9b | 1217 | static struct snd_kcontrol_new dig_in_ctls[] = { |
1da177e4 LT |
1218 | { |
1219 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1220 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), | |
1221 | .info = snd_hda_spdif_in_switch_info, | |
1222 | .get = snd_hda_spdif_in_switch_get, | |
1223 | .put = snd_hda_spdif_in_switch_put, | |
1224 | }, | |
1225 | { | |
1226 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
1227 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1228 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), | |
1229 | .info = snd_hda_spdif_mask_info, | |
1230 | .get = snd_hda_spdif_in_status_get, | |
1231 | }, | |
1232 | { } /* end */ | |
1233 | }; | |
1234 | ||
1235 | /** | |
1236 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls | |
1237 | * @codec: the HDA codec | |
1238 | * @nid: audio in widget NID | |
1239 | * | |
1240 | * Creates controls related with the SPDIF input. | |
1241 | * Called from each patch supporting the SPDIF in. | |
1242 | * | |
1243 | * Returns 0 if successful, or a negative error code. | |
1244 | */ | |
756e2b01 TI |
1245 | int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec, |
1246 | hda_nid_t nid) | |
1da177e4 LT |
1247 | { |
1248 | int err; | |
c8b6bf9b TI |
1249 | struct snd_kcontrol *kctl; |
1250 | struct snd_kcontrol_new *dig_mix; | |
1da177e4 LT |
1251 | |
1252 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { | |
1253 | kctl = snd_ctl_new1(dig_mix, codec); | |
1254 | kctl->private_value = nid; | |
1255 | if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0) | |
1256 | return err; | |
1257 | } | |
1258 | codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1; | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | ||
54d17403 TI |
1263 | /* |
1264 | * set power state of the codec | |
1265 | */ | |
1266 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, | |
1267 | unsigned int power_state) | |
1268 | { | |
1269 | hda_nid_t nid, nid_start; | |
1270 | int nodes; | |
1271 | ||
1272 | snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE, | |
1273 | power_state); | |
1274 | ||
1275 | nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start); | |
1276 | for (nid = nid_start; nid < nodes + nid_start; nid++) { | |
1277 | if (get_wcaps(codec, nid) & AC_WCAP_POWER) | |
1278 | snd_hda_codec_write(codec, nid, 0, | |
1279 | AC_VERB_SET_POWER_STATE, | |
1280 | power_state); | |
1281 | } | |
1282 | ||
1283 | if (power_state == AC_PWRST_D0) | |
1284 | msleep(10); | |
1285 | } | |
1286 | ||
1287 | ||
1da177e4 LT |
1288 | /** |
1289 | * snd_hda_build_controls - build mixer controls | |
1290 | * @bus: the BUS | |
1291 | * | |
1292 | * Creates mixer controls for each codec included in the bus. | |
1293 | * | |
1294 | * Returns 0 if successful, otherwise a negative error code. | |
1295 | */ | |
756e2b01 | 1296 | int __devinit snd_hda_build_controls(struct hda_bus *bus) |
1da177e4 LT |
1297 | { |
1298 | struct list_head *p; | |
1299 | ||
1300 | /* build controls */ | |
1301 | list_for_each(p, &bus->codec_list) { | |
1302 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
1303 | int err; | |
1304 | if (! codec->patch_ops.build_controls) | |
1305 | continue; | |
1306 | err = codec->patch_ops.build_controls(codec); | |
1307 | if (err < 0) | |
1308 | return err; | |
1309 | } | |
1310 | ||
1311 | /* initialize */ | |
1312 | list_for_each(p, &bus->codec_list) { | |
1313 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
1314 | int err; | |
54d17403 TI |
1315 | hda_set_power_state(codec, |
1316 | codec->afg ? codec->afg : codec->mfg, | |
1317 | AC_PWRST_D0); | |
1da177e4 LT |
1318 | if (! codec->patch_ops.init) |
1319 | continue; | |
1320 | err = codec->patch_ops.init(codec); | |
1321 | if (err < 0) | |
1322 | return err; | |
1323 | } | |
1324 | return 0; | |
1325 | } | |
1326 | ||
1da177e4 LT |
1327 | /* |
1328 | * stream formats | |
1329 | */ | |
befdf316 TI |
1330 | struct hda_rate_tbl { |
1331 | unsigned int hz; | |
1332 | unsigned int alsa_bits; | |
1333 | unsigned int hda_fmt; | |
1334 | }; | |
1335 | ||
1336 | static struct hda_rate_tbl rate_bits[] = { | |
1da177e4 | 1337 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
9d8f53f2 NG |
1338 | |
1339 | /* autodetected value used in snd_hda_query_supported_pcm */ | |
1da177e4 LT |
1340 | { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */ |
1341 | { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */ | |
1342 | { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */ | |
1343 | { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */ | |
1344 | { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */ | |
1345 | { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */ | |
1346 | { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */ | |
1347 | { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */ | |
1348 | { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */ | |
1349 | { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */ | |
1350 | { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */ | |
a961f9fe TI |
1351 | #define AC_PAR_PCM_RATE_BITS 11 |
1352 | /* up to bits 10, 384kHZ isn't supported properly */ | |
1353 | ||
1354 | /* not autodetected value */ | |
1355 | { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */ | |
9d8f53f2 | 1356 | |
befdf316 | 1357 | { 0 } /* terminator */ |
1da177e4 LT |
1358 | }; |
1359 | ||
1360 | /** | |
1361 | * snd_hda_calc_stream_format - calculate format bitset | |
1362 | * @rate: the sample rate | |
1363 | * @channels: the number of channels | |
1364 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) | |
1365 | * @maxbps: the max. bps | |
1366 | * | |
1367 | * Calculate the format bitset from the given rate, channels and th PCM format. | |
1368 | * | |
1369 | * Return zero if invalid. | |
1370 | */ | |
1371 | unsigned int snd_hda_calc_stream_format(unsigned int rate, | |
1372 | unsigned int channels, | |
1373 | unsigned int format, | |
1374 | unsigned int maxbps) | |
1375 | { | |
1376 | int i; | |
1377 | unsigned int val = 0; | |
1378 | ||
befdf316 TI |
1379 | for (i = 0; rate_bits[i].hz; i++) |
1380 | if (rate_bits[i].hz == rate) { | |
1381 | val = rate_bits[i].hda_fmt; | |
1da177e4 LT |
1382 | break; |
1383 | } | |
befdf316 | 1384 | if (! rate_bits[i].hz) { |
1da177e4 LT |
1385 | snd_printdd("invalid rate %d\n", rate); |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | if (channels == 0 || channels > 8) { | |
1390 | snd_printdd("invalid channels %d\n", channels); | |
1391 | return 0; | |
1392 | } | |
1393 | val |= channels - 1; | |
1394 | ||
1395 | switch (snd_pcm_format_width(format)) { | |
1396 | case 8: val |= 0x00; break; | |
1397 | case 16: val |= 0x10; break; | |
1398 | case 20: | |
1399 | case 24: | |
1400 | case 32: | |
1401 | if (maxbps >= 32) | |
1402 | val |= 0x40; | |
1403 | else if (maxbps >= 24) | |
1404 | val |= 0x30; | |
1405 | else | |
1406 | val |= 0x20; | |
1407 | break; | |
1408 | default: | |
1409 | snd_printdd("invalid format width %d\n", snd_pcm_format_width(format)); | |
1410 | return 0; | |
1411 | } | |
1412 | ||
1413 | return val; | |
1414 | } | |
1415 | ||
1416 | /** | |
1417 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats | |
1418 | * @codec: the HDA codec | |
1419 | * @nid: NID to query | |
1420 | * @ratesp: the pointer to store the detected rate bitflags | |
1421 | * @formatsp: the pointer to store the detected formats | |
1422 | * @bpsp: the pointer to store the detected format widths | |
1423 | * | |
1424 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp | |
1425 | * or @bsps argument is ignored. | |
1426 | * | |
1427 | * Returns 0 if successful, otherwise a negative error code. | |
1428 | */ | |
1429 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, | |
1430 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) | |
1431 | { | |
1432 | int i; | |
1433 | unsigned int val, streams; | |
1434 | ||
1435 | val = 0; | |
1436 | if (nid != codec->afg && | |
54d17403 | 1437 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
1da177e4 LT |
1438 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
1439 | if (val == -1) | |
1440 | return -EIO; | |
1441 | } | |
1442 | if (! val) | |
1443 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); | |
1444 | ||
1445 | if (ratesp) { | |
1446 | u32 rates = 0; | |
a961f9fe | 1447 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
1da177e4 | 1448 | if (val & (1 << i)) |
befdf316 | 1449 | rates |= rate_bits[i].alsa_bits; |
1da177e4 LT |
1450 | } |
1451 | *ratesp = rates; | |
1452 | } | |
1453 | ||
1454 | if (formatsp || bpsp) { | |
1455 | u64 formats = 0; | |
1456 | unsigned int bps; | |
1457 | unsigned int wcaps; | |
1458 | ||
54d17403 | 1459 | wcaps = get_wcaps(codec, nid); |
1da177e4 LT |
1460 | streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
1461 | if (streams == -1) | |
1462 | return -EIO; | |
1463 | if (! streams) { | |
1464 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); | |
1465 | if (streams == -1) | |
1466 | return -EIO; | |
1467 | } | |
1468 | ||
1469 | bps = 0; | |
1470 | if (streams & AC_SUPFMT_PCM) { | |
1471 | if (val & AC_SUPPCM_BITS_8) { | |
1472 | formats |= SNDRV_PCM_FMTBIT_U8; | |
1473 | bps = 8; | |
1474 | } | |
1475 | if (val & AC_SUPPCM_BITS_16) { | |
1476 | formats |= SNDRV_PCM_FMTBIT_S16_LE; | |
1477 | bps = 16; | |
1478 | } | |
1479 | if (wcaps & AC_WCAP_DIGITAL) { | |
1480 | if (val & AC_SUPPCM_BITS_32) | |
1481 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; | |
1482 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) | |
1483 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
1484 | if (val & AC_SUPPCM_BITS_24) | |
1485 | bps = 24; | |
1486 | else if (val & AC_SUPPCM_BITS_20) | |
1487 | bps = 20; | |
1488 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) { | |
1489 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
1490 | if (val & AC_SUPPCM_BITS_32) | |
1491 | bps = 32; | |
1da177e4 LT |
1492 | else if (val & AC_SUPPCM_BITS_24) |
1493 | bps = 24; | |
33ef7651 NG |
1494 | else if (val & AC_SUPPCM_BITS_20) |
1495 | bps = 20; | |
1da177e4 LT |
1496 | } |
1497 | } | |
1498 | else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */ | |
1499 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; | |
1500 | bps = 32; | |
1501 | } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */ | |
1502 | /* temporary hack: we have still no proper support | |
1503 | * for the direct AC3 stream... | |
1504 | */ | |
1505 | formats |= SNDRV_PCM_FMTBIT_U8; | |
1506 | bps = 8; | |
1507 | } | |
1508 | if (formatsp) | |
1509 | *formatsp = formats; | |
1510 | if (bpsp) | |
1511 | *bpsp = bps; | |
1512 | } | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | /** | |
1518 | * snd_hda_is_supported_format - check whether the given node supports the format val | |
1519 | * | |
1520 | * Returns 1 if supported, 0 if not. | |
1521 | */ | |
1522 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, | |
1523 | unsigned int format) | |
1524 | { | |
1525 | int i; | |
1526 | unsigned int val = 0, rate, stream; | |
1527 | ||
1528 | if (nid != codec->afg && | |
54d17403 | 1529 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
1da177e4 LT |
1530 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
1531 | if (val == -1) | |
1532 | return 0; | |
1533 | } | |
1534 | if (! val) { | |
1535 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); | |
1536 | if (val == -1) | |
1537 | return 0; | |
1538 | } | |
1539 | ||
1540 | rate = format & 0xff00; | |
a961f9fe | 1541 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
befdf316 | 1542 | if (rate_bits[i].hda_fmt == rate) { |
1da177e4 LT |
1543 | if (val & (1 << i)) |
1544 | break; | |
1545 | return 0; | |
1546 | } | |
a961f9fe | 1547 | if (i >= AC_PAR_PCM_RATE_BITS) |
1da177e4 LT |
1548 | return 0; |
1549 | ||
1550 | stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM); | |
1551 | if (stream == -1) | |
1552 | return 0; | |
1553 | if (! stream && nid != codec->afg) | |
1554 | stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); | |
1555 | if (! stream || stream == -1) | |
1556 | return 0; | |
1557 | ||
1558 | if (stream & AC_SUPFMT_PCM) { | |
1559 | switch (format & 0xf0) { | |
1560 | case 0x00: | |
1561 | if (! (val & AC_SUPPCM_BITS_8)) | |
1562 | return 0; | |
1563 | break; | |
1564 | case 0x10: | |
1565 | if (! (val & AC_SUPPCM_BITS_16)) | |
1566 | return 0; | |
1567 | break; | |
1568 | case 0x20: | |
1569 | if (! (val & AC_SUPPCM_BITS_20)) | |
1570 | return 0; | |
1571 | break; | |
1572 | case 0x30: | |
1573 | if (! (val & AC_SUPPCM_BITS_24)) | |
1574 | return 0; | |
1575 | break; | |
1576 | case 0x40: | |
1577 | if (! (val & AC_SUPPCM_BITS_32)) | |
1578 | return 0; | |
1579 | break; | |
1580 | default: | |
1581 | return 0; | |
1582 | } | |
1583 | } else { | |
1584 | /* FIXME: check for float32 and AC3? */ | |
1585 | } | |
1586 | ||
1587 | return 1; | |
1588 | } | |
1589 | ||
1590 | /* | |
1591 | * PCM stuff | |
1592 | */ | |
1593 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, | |
1594 | struct hda_codec *codec, | |
c8b6bf9b | 1595 | struct snd_pcm_substream *substream) |
1da177e4 LT |
1596 | { |
1597 | return 0; | |
1598 | } | |
1599 | ||
1600 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, | |
1601 | struct hda_codec *codec, | |
1602 | unsigned int stream_tag, | |
1603 | unsigned int format, | |
c8b6bf9b | 1604 | struct snd_pcm_substream *substream) |
1da177e4 LT |
1605 | { |
1606 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); | |
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, | |
1611 | struct hda_codec *codec, | |
c8b6bf9b | 1612 | struct snd_pcm_substream *substream) |
1da177e4 LT |
1613 | { |
1614 | snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0); | |
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info) | |
1619 | { | |
1620 | if (info->nid) { | |
1621 | /* query support PCM information from the given NID */ | |
1622 | if (! info->rates || ! info->formats) | |
1623 | snd_hda_query_supported_pcm(codec, info->nid, | |
1624 | info->rates ? NULL : &info->rates, | |
1625 | info->formats ? NULL : &info->formats, | |
1626 | info->maxbps ? NULL : &info->maxbps); | |
1627 | } | |
1628 | if (info->ops.open == NULL) | |
1629 | info->ops.open = hda_pcm_default_open_close; | |
1630 | if (info->ops.close == NULL) | |
1631 | info->ops.close = hda_pcm_default_open_close; | |
1632 | if (info->ops.prepare == NULL) { | |
1633 | snd_assert(info->nid, return -EINVAL); | |
1634 | info->ops.prepare = hda_pcm_default_prepare; | |
1635 | } | |
1da177e4 LT |
1636 | if (info->ops.cleanup == NULL) { |
1637 | snd_assert(info->nid, return -EINVAL); | |
1638 | info->ops.cleanup = hda_pcm_default_cleanup; | |
1639 | } | |
1640 | return 0; | |
1641 | } | |
1642 | ||
1643 | /** | |
1644 | * snd_hda_build_pcms - build PCM information | |
1645 | * @bus: the BUS | |
1646 | * | |
1647 | * Create PCM information for each codec included in the bus. | |
1648 | * | |
1649 | * The build_pcms codec patch is requested to set up codec->num_pcms and | |
1650 | * codec->pcm_info properly. The array is referred by the top-level driver | |
1651 | * to create its PCM instances. | |
1652 | * The allocated codec->pcm_info should be released in codec->patch_ops.free | |
1653 | * callback. | |
1654 | * | |
1655 | * At least, substreams, channels_min and channels_max must be filled for | |
1656 | * each stream. substreams = 0 indicates that the stream doesn't exist. | |
1657 | * When rates and/or formats are zero, the supported values are queried | |
1658 | * from the given nid. The nid is used also by the default ops.prepare | |
1659 | * and ops.cleanup callbacks. | |
1660 | * | |
1661 | * The driver needs to call ops.open in its open callback. Similarly, | |
1662 | * ops.close is supposed to be called in the close callback. | |
1663 | * ops.prepare should be called in the prepare or hw_params callback | |
1664 | * with the proper parameters for set up. | |
1665 | * ops.cleanup should be called in hw_free for clean up of streams. | |
1666 | * | |
1667 | * This function returns 0 if successfull, or a negative error code. | |
1668 | */ | |
756e2b01 | 1669 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
1da177e4 LT |
1670 | { |
1671 | struct list_head *p; | |
1672 | ||
1673 | list_for_each(p, &bus->codec_list) { | |
1674 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
1675 | unsigned int pcm, s; | |
1676 | int err; | |
1677 | if (! codec->patch_ops.build_pcms) | |
1678 | continue; | |
1679 | err = codec->patch_ops.build_pcms(codec); | |
1680 | if (err < 0) | |
1681 | return err; | |
1682 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { | |
1683 | for (s = 0; s < 2; s++) { | |
1684 | struct hda_pcm_stream *info; | |
1685 | info = &codec->pcm_info[pcm].stream[s]; | |
1686 | if (! info->substreams) | |
1687 | continue; | |
1688 | err = set_pcm_default_values(codec, info); | |
1689 | if (err < 0) | |
1690 | return err; | |
1691 | } | |
1692 | } | |
1693 | } | |
1694 | return 0; | |
1695 | } | |
1696 | ||
1da177e4 LT |
1697 | /** |
1698 | * snd_hda_check_board_config - compare the current codec with the config table | |
1699 | * @codec: the HDA codec | |
f5fcc13c TI |
1700 | * @num_configs: number of config enums |
1701 | * @models: array of model name strings | |
1da177e4 LT |
1702 | * @tbl: configuration table, terminated by null entries |
1703 | * | |
1704 | * Compares the modelname or PCI subsystem id of the current codec with the | |
1705 | * given configuration table. If a matching entry is found, returns its | |
1706 | * config value (supposed to be 0 or positive). | |
1707 | * | |
1708 | * If no entries are matching, the function returns a negative value. | |
1709 | */ | |
756e2b01 TI |
1710 | int __devinit snd_hda_check_board_config(struct hda_codec *codec, |
1711 | int num_configs, const char **models, | |
1712 | const struct snd_pci_quirk *tbl) | |
1da177e4 | 1713 | { |
f5fcc13c TI |
1714 | if (codec->bus->modelname && models) { |
1715 | int i; | |
1716 | for (i = 0; i < num_configs; i++) { | |
1717 | if (models[i] && | |
1718 | !strcmp(codec->bus->modelname, models[i])) { | |
1719 | snd_printd(KERN_INFO "hda_codec: model '%s' is " | |
1720 | "selected\n", models[i]); | |
1721 | return i; | |
1da177e4 LT |
1722 | } |
1723 | } | |
1724 | } | |
1725 | ||
f5fcc13c TI |
1726 | if (!codec->bus->pci || !tbl) |
1727 | return -1; | |
1728 | ||
1729 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); | |
1730 | if (!tbl) | |
1731 | return -1; | |
1732 | if (tbl->value >= 0 && tbl->value < num_configs) { | |
1733 | #ifdef CONFIG_SND_DEBUG_DETECT | |
1734 | char tmp[10]; | |
1735 | const char *model = NULL; | |
1736 | if (models) | |
1737 | model = models[tbl->value]; | |
1738 | if (!model) { | |
1739 | sprintf(tmp, "#%d", tbl->value); | |
1740 | model = tmp; | |
1da177e4 | 1741 | } |
f5fcc13c TI |
1742 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
1743 | "for config %x:%x (%s)\n", | |
1744 | model, tbl->subvendor, tbl->subdevice, | |
1745 | (tbl->name ? tbl->name : "Unknown device")); | |
1746 | #endif | |
1747 | return tbl->value; | |
1da177e4 LT |
1748 | } |
1749 | return -1; | |
1750 | } | |
1751 | ||
1752 | /** | |
1753 | * snd_hda_add_new_ctls - create controls from the array | |
1754 | * @codec: the HDA codec | |
c8b6bf9b | 1755 | * @knew: the array of struct snd_kcontrol_new |
1da177e4 LT |
1756 | * |
1757 | * This helper function creates and add new controls in the given array. | |
1758 | * The array must be terminated with an empty entry as terminator. | |
1759 | * | |
1760 | * Returns 0 if successful, or a negative error code. | |
1761 | */ | |
756e2b01 TI |
1762 | int __devinit snd_hda_add_new_ctls(struct hda_codec *codec, |
1763 | struct snd_kcontrol_new *knew) | |
1da177e4 LT |
1764 | { |
1765 | int err; | |
1766 | ||
1767 | for (; knew->name; knew++) { | |
54d17403 TI |
1768 | struct snd_kcontrol *kctl; |
1769 | kctl = snd_ctl_new1(knew, codec); | |
1770 | if (! kctl) | |
1771 | return -ENOMEM; | |
1772 | err = snd_ctl_add(codec->bus->card, kctl); | |
1773 | if (err < 0) { | |
1774 | if (! codec->addr) | |
1775 | return err; | |
1776 | kctl = snd_ctl_new1(knew, codec); | |
1777 | if (! kctl) | |
1778 | return -ENOMEM; | |
1779 | kctl->id.device = codec->addr; | |
1780 | if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0) | |
1781 | return err; | |
1782 | } | |
1da177e4 LT |
1783 | } |
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | ||
c8b6bf9b | 1788 | /* |
d2a6d7dc TI |
1789 | * Channel mode helper |
1790 | */ | |
c8b6bf9b | 1791 | int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo, |
d2a6d7dc TI |
1792 | const struct hda_channel_mode *chmode, int num_chmodes) |
1793 | { | |
1794 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1795 | uinfo->count = 1; | |
1796 | uinfo->value.enumerated.items = num_chmodes; | |
1797 | if (uinfo->value.enumerated.item >= num_chmodes) | |
1798 | uinfo->value.enumerated.item = num_chmodes - 1; | |
1799 | sprintf(uinfo->value.enumerated.name, "%dch", | |
1800 | chmode[uinfo->value.enumerated.item].channels); | |
1801 | return 0; | |
1802 | } | |
1803 | ||
c8b6bf9b | 1804 | int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol, |
d2a6d7dc TI |
1805 | const struct hda_channel_mode *chmode, int num_chmodes, |
1806 | int max_channels) | |
1807 | { | |
1808 | int i; | |
1809 | ||
1810 | for (i = 0; i < num_chmodes; i++) { | |
1811 | if (max_channels == chmode[i].channels) { | |
1812 | ucontrol->value.enumerated.item[0] = i; | |
1813 | break; | |
1814 | } | |
1815 | } | |
1816 | return 0; | |
1817 | } | |
1818 | ||
c8b6bf9b | 1819 | int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol, |
d2a6d7dc TI |
1820 | const struct hda_channel_mode *chmode, int num_chmodes, |
1821 | int *max_channelsp) | |
1822 | { | |
1823 | unsigned int mode; | |
1824 | ||
1825 | mode = ucontrol->value.enumerated.item[0]; | |
1826 | snd_assert(mode < num_chmodes, return -EINVAL); | |
b2ec6423 | 1827 | if (*max_channelsp == chmode[mode].channels && ! codec->in_resume) |
d2a6d7dc TI |
1828 | return 0; |
1829 | /* change the current channel setting */ | |
1830 | *max_channelsp = chmode[mode].channels; | |
1831 | if (chmode[mode].sequence) | |
1832 | snd_hda_sequence_write(codec, chmode[mode].sequence); | |
1833 | return 1; | |
1834 | } | |
1835 | ||
1da177e4 LT |
1836 | /* |
1837 | * input MUX helper | |
1838 | */ | |
c8b6bf9b | 1839 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo) |
1da177e4 LT |
1840 | { |
1841 | unsigned int index; | |
1842 | ||
1843 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1844 | uinfo->count = 1; | |
1845 | uinfo->value.enumerated.items = imux->num_items; | |
1846 | index = uinfo->value.enumerated.item; | |
1847 | if (index >= imux->num_items) | |
1848 | index = imux->num_items - 1; | |
1849 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); | |
1850 | return 0; | |
1851 | } | |
1852 | ||
1853 | int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux, | |
c8b6bf9b | 1854 | struct snd_ctl_elem_value *ucontrol, hda_nid_t nid, |
1da177e4 LT |
1855 | unsigned int *cur_val) |
1856 | { | |
1857 | unsigned int idx; | |
1858 | ||
1859 | idx = ucontrol->value.enumerated.item[0]; | |
1860 | if (idx >= imux->num_items) | |
1861 | idx = imux->num_items - 1; | |
1862 | if (*cur_val == idx && ! codec->in_resume) | |
1863 | return 0; | |
1864 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, | |
1865 | imux->items[idx].index); | |
1866 | *cur_val = idx; | |
1867 | return 1; | |
1868 | } | |
1869 | ||
1870 | ||
1871 | /* | |
1872 | * Multi-channel / digital-out PCM helper functions | |
1873 | */ | |
1874 | ||
6b97eb45 TI |
1875 | /* setup SPDIF output stream */ |
1876 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, | |
1877 | unsigned int stream_tag, unsigned int format) | |
1878 | { | |
1879 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ | |
1880 | if (codec->spdif_ctls & AC_DIG1_ENABLE) | |
1881 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, | |
1882 | codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff); | |
1883 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); | |
1884 | /* turn on again (if needed) */ | |
1885 | if (codec->spdif_ctls & AC_DIG1_ENABLE) | |
1886 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, | |
1887 | codec->spdif_ctls & 0xff); | |
1888 | } | |
1889 | ||
1da177e4 LT |
1890 | /* |
1891 | * open the digital out in the exclusive mode | |
1892 | */ | |
1893 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout) | |
1894 | { | |
62932df8 | 1895 | mutex_lock(&codec->spdif_mutex); |
5930ca41 TI |
1896 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
1897 | /* already opened as analog dup; reset it once */ | |
1898 | snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0); | |
1da177e4 | 1899 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
62932df8 | 1900 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1901 | return 0; |
1902 | } | |
1903 | ||
6b97eb45 TI |
1904 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
1905 | struct hda_multi_out *mout, | |
1906 | unsigned int stream_tag, | |
1907 | unsigned int format, | |
1908 | struct snd_pcm_substream *substream) | |
1909 | { | |
1910 | mutex_lock(&codec->spdif_mutex); | |
1911 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); | |
1912 | mutex_unlock(&codec->spdif_mutex); | |
1913 | return 0; | |
1914 | } | |
1915 | ||
1da177e4 LT |
1916 | /* |
1917 | * release the digital out | |
1918 | */ | |
1919 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout) | |
1920 | { | |
62932df8 | 1921 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 1922 | mout->dig_out_used = 0; |
62932df8 | 1923 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1924 | return 0; |
1925 | } | |
1926 | ||
1927 | /* | |
1928 | * set up more restrictions for analog out | |
1929 | */ | |
1930 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout, | |
c8b6bf9b | 1931 | struct snd_pcm_substream *substream) |
1da177e4 LT |
1932 | { |
1933 | substream->runtime->hw.channels_max = mout->max_channels; | |
1934 | return snd_pcm_hw_constraint_step(substream->runtime, 0, | |
1935 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); | |
1936 | } | |
1937 | ||
1938 | /* | |
1939 | * set up the i/o for analog out | |
1940 | * when the digital out is available, copy the front out to digital out, too. | |
1941 | */ | |
1942 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout, | |
1943 | unsigned int stream_tag, | |
1944 | unsigned int format, | |
c8b6bf9b | 1945 | struct snd_pcm_substream *substream) |
1da177e4 LT |
1946 | { |
1947 | hda_nid_t *nids = mout->dac_nids; | |
1948 | int chs = substream->runtime->channels; | |
1949 | int i; | |
1950 | ||
62932df8 | 1951 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
1952 | if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
1953 | if (chs == 2 && | |
1954 | snd_hda_is_supported_format(codec, mout->dig_out_nid, format) && | |
1955 | ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) { | |
1956 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; | |
6b97eb45 TI |
1957 | setup_dig_out_stream(codec, mout->dig_out_nid, |
1958 | stream_tag, format); | |
1da177e4 LT |
1959 | } else { |
1960 | mout->dig_out_used = 0; | |
1961 | snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0); | |
1962 | } | |
1963 | } | |
62932df8 | 1964 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
1965 | |
1966 | /* front */ | |
1967 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format); | |
35aec4e2 | 1968 | if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
1da177e4 LT |
1969 | /* headphone out will just decode front left/right (stereo) */ |
1970 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format); | |
82bc955f TI |
1971 | /* extra outputs copied from front */ |
1972 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) | |
1973 | if (mout->extra_out_nid[i]) | |
1974 | snd_hda_codec_setup_stream(codec, | |
1975 | mout->extra_out_nid[i], | |
1976 | stream_tag, 0, format); | |
1977 | ||
1da177e4 LT |
1978 | /* surrounds */ |
1979 | for (i = 1; i < mout->num_dacs; i++) { | |
4b3acaf5 | 1980 | if (chs >= (i + 1) * 2) /* independent out */ |
1da177e4 LT |
1981 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2, |
1982 | format); | |
4b3acaf5 TI |
1983 | else /* copy front */ |
1984 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0, | |
1985 | format); | |
1da177e4 LT |
1986 | } |
1987 | return 0; | |
1988 | } | |
1989 | ||
1990 | /* | |
1991 | * clean up the setting for analog out | |
1992 | */ | |
1993 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout) | |
1994 | { | |
1995 | hda_nid_t *nids = mout->dac_nids; | |
1996 | int i; | |
1997 | ||
1998 | for (i = 0; i < mout->num_dacs; i++) | |
1999 | snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0); | |
2000 | if (mout->hp_nid) | |
2001 | snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0); | |
82bc955f TI |
2002 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
2003 | if (mout->extra_out_nid[i]) | |
2004 | snd_hda_codec_setup_stream(codec, | |
2005 | mout->extra_out_nid[i], | |
2006 | 0, 0, 0); | |
62932df8 | 2007 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
2008 | if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { |
2009 | snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0); | |
2010 | mout->dig_out_used = 0; | |
2011 | } | |
62932df8 | 2012 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2013 | return 0; |
2014 | } | |
2015 | ||
e9edcee0 TI |
2016 | /* |
2017 | * Helper for automatic ping configuration | |
2018 | */ | |
df694daa | 2019 | |
756e2b01 | 2020 | static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list) |
df694daa KY |
2021 | { |
2022 | for (; *list; list++) | |
2023 | if (*list == nid) | |
2024 | return 1; | |
2025 | return 0; | |
2026 | } | |
2027 | ||
82bc955f TI |
2028 | /* |
2029 | * Parse all pin widgets and store the useful pin nids to cfg | |
2030 | * | |
2031 | * The number of line-outs or any primary output is stored in line_outs, | |
2032 | * and the corresponding output pins are assigned to line_out_pins[], | |
2033 | * in the order of front, rear, CLFE, side, ... | |
2034 | * | |
2035 | * If more extra outputs (speaker and headphone) are found, the pins are | |
eb06ed8f | 2036 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack |
82bc955f TI |
2037 | * is detected, one of speaker of HP pins is assigned as the primary |
2038 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive | |
2039 | * if any analog output exists. | |
2040 | * | |
2041 | * The analog input pins are assigned to input_pins array. | |
2042 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, | |
2043 | * respectively. | |
2044 | */ | |
756e2b01 TI |
2045 | int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec, |
2046 | struct auto_pin_cfg *cfg, | |
2047 | hda_nid_t *ignore_nids) | |
e9edcee0 TI |
2048 | { |
2049 | hda_nid_t nid, nid_start; | |
2050 | int i, j, nodes; | |
82bc955f | 2051 | short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)]; |
e9edcee0 TI |
2052 | |
2053 | memset(cfg, 0, sizeof(*cfg)); | |
2054 | ||
2055 | memset(sequences, 0, sizeof(sequences)); | |
2056 | assoc_line_out = 0; | |
2057 | ||
2058 | nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start); | |
2059 | for (nid = nid_start; nid < nodes + nid_start; nid++) { | |
54d17403 | 2060 | unsigned int wid_caps = get_wcaps(codec, nid); |
e9edcee0 TI |
2061 | unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; |
2062 | unsigned int def_conf; | |
2063 | short assoc, loc; | |
2064 | ||
2065 | /* read all default configuration for pin complex */ | |
2066 | if (wid_type != AC_WID_PIN) | |
2067 | continue; | |
df694daa KY |
2068 | /* ignore the given nids (e.g. pc-beep returns error) */ |
2069 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) | |
2070 | continue; | |
2071 | ||
e9edcee0 TI |
2072 | def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0); |
2073 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) | |
2074 | continue; | |
2075 | loc = get_defcfg_location(def_conf); | |
2076 | switch (get_defcfg_device(def_conf)) { | |
2077 | case AC_JACK_LINE_OUT: | |
e9edcee0 TI |
2078 | seq = get_defcfg_sequence(def_conf); |
2079 | assoc = get_defcfg_association(def_conf); | |
2080 | if (! assoc) | |
2081 | continue; | |
2082 | if (! assoc_line_out) | |
2083 | assoc_line_out = assoc; | |
2084 | else if (assoc_line_out != assoc) | |
2085 | continue; | |
2086 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) | |
2087 | continue; | |
2088 | cfg->line_out_pins[cfg->line_outs] = nid; | |
2089 | sequences[cfg->line_outs] = seq; | |
2090 | cfg->line_outs++; | |
2091 | break; | |
8d88bc3d | 2092 | case AC_JACK_SPEAKER: |
82bc955f TI |
2093 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
2094 | continue; | |
2095 | cfg->speaker_pins[cfg->speaker_outs] = nid; | |
2096 | cfg->speaker_outs++; | |
8d88bc3d | 2097 | break; |
e9edcee0 | 2098 | case AC_JACK_HP_OUT: |
eb06ed8f TI |
2099 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
2100 | continue; | |
2101 | cfg->hp_pins[cfg->hp_outs] = nid; | |
2102 | cfg->hp_outs++; | |
e9edcee0 | 2103 | break; |
314634bc TI |
2104 | case AC_JACK_MIC_IN: { |
2105 | int preferred, alt; | |
2106 | if (loc == AC_JACK_LOC_FRONT) { | |
2107 | preferred = AUTO_PIN_FRONT_MIC; | |
2108 | alt = AUTO_PIN_MIC; | |
2109 | } else { | |
2110 | preferred = AUTO_PIN_MIC; | |
2111 | alt = AUTO_PIN_FRONT_MIC; | |
2112 | } | |
2113 | if (!cfg->input_pins[preferred]) | |
2114 | cfg->input_pins[preferred] = nid; | |
2115 | else if (!cfg->input_pins[alt]) | |
2116 | cfg->input_pins[alt] = nid; | |
e9edcee0 | 2117 | break; |
314634bc | 2118 | } |
e9edcee0 TI |
2119 | case AC_JACK_LINE_IN: |
2120 | if (loc == AC_JACK_LOC_FRONT) | |
2121 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid; | |
2122 | else | |
2123 | cfg->input_pins[AUTO_PIN_LINE] = nid; | |
2124 | break; | |
2125 | case AC_JACK_CD: | |
2126 | cfg->input_pins[AUTO_PIN_CD] = nid; | |
2127 | break; | |
2128 | case AC_JACK_AUX: | |
2129 | cfg->input_pins[AUTO_PIN_AUX] = nid; | |
2130 | break; | |
2131 | case AC_JACK_SPDIF_OUT: | |
2132 | cfg->dig_out_pin = nid; | |
2133 | break; | |
2134 | case AC_JACK_SPDIF_IN: | |
2135 | cfg->dig_in_pin = nid; | |
2136 | break; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | /* sort by sequence */ | |
2141 | for (i = 0; i < cfg->line_outs; i++) | |
2142 | for (j = i + 1; j < cfg->line_outs; j++) | |
2143 | if (sequences[i] > sequences[j]) { | |
2144 | seq = sequences[i]; | |
2145 | sequences[i] = sequences[j]; | |
2146 | sequences[j] = seq; | |
2147 | nid = cfg->line_out_pins[i]; | |
2148 | cfg->line_out_pins[i] = cfg->line_out_pins[j]; | |
2149 | cfg->line_out_pins[j] = nid; | |
2150 | } | |
2151 | ||
cb8e2f83 TI |
2152 | /* Reorder the surround channels |
2153 | * ALSA sequence is front/surr/clfe/side | |
2154 | * HDA sequence is: | |
2155 | * 4-ch: front/surr => OK as it is | |
2156 | * 6-ch: front/clfe/surr | |
2157 | * 8-ch: front/clfe/side/surr | |
2158 | */ | |
2159 | switch (cfg->line_outs) { | |
2160 | case 3: | |
e9edcee0 TI |
2161 | nid = cfg->line_out_pins[1]; |
2162 | cfg->line_out_pins[1] = cfg->line_out_pins[2]; | |
2163 | cfg->line_out_pins[2] = nid; | |
cb8e2f83 TI |
2164 | break; |
2165 | case 4: | |
2166 | nid = cfg->line_out_pins[1]; | |
2167 | cfg->line_out_pins[1] = cfg->line_out_pins[3]; | |
2168 | cfg->line_out_pins[3] = cfg->line_out_pins[2]; | |
2169 | cfg->line_out_pins[2] = nid; | |
2170 | break; | |
e9edcee0 TI |
2171 | } |
2172 | ||
82bc955f TI |
2173 | /* |
2174 | * debug prints of the parsed results | |
2175 | */ | |
2176 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
2177 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], | |
2178 | cfg->line_out_pins[2], cfg->line_out_pins[3], | |
2179 | cfg->line_out_pins[4]); | |
2180 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
2181 | cfg->speaker_outs, cfg->speaker_pins[0], | |
2182 | cfg->speaker_pins[1], cfg->speaker_pins[2], | |
2183 | cfg->speaker_pins[3], cfg->speaker_pins[4]); | |
eb06ed8f TI |
2184 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
2185 | cfg->hp_outs, cfg->hp_pins[0], | |
2186 | cfg->hp_pins[1], cfg->hp_pins[2], | |
2187 | cfg->hp_pins[3], cfg->hp_pins[4]); | |
82bc955f TI |
2188 | snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x," |
2189 | " cd=0x%x, aux=0x%x\n", | |
2190 | cfg->input_pins[AUTO_PIN_MIC], | |
2191 | cfg->input_pins[AUTO_PIN_FRONT_MIC], | |
2192 | cfg->input_pins[AUTO_PIN_LINE], | |
2193 | cfg->input_pins[AUTO_PIN_FRONT_LINE], | |
2194 | cfg->input_pins[AUTO_PIN_CD], | |
2195 | cfg->input_pins[AUTO_PIN_AUX]); | |
2196 | ||
2197 | /* | |
2198 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin | |
2199 | * as a primary output | |
2200 | */ | |
2201 | if (! cfg->line_outs) { | |
2202 | if (cfg->speaker_outs) { | |
2203 | cfg->line_outs = cfg->speaker_outs; | |
2204 | memcpy(cfg->line_out_pins, cfg->speaker_pins, | |
2205 | sizeof(cfg->speaker_pins)); | |
2206 | cfg->speaker_outs = 0; | |
2207 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); | |
eb06ed8f TI |
2208 | } else if (cfg->hp_outs) { |
2209 | cfg->line_outs = cfg->hp_outs; | |
2210 | memcpy(cfg->line_out_pins, cfg->hp_pins, | |
2211 | sizeof(cfg->hp_pins)); | |
2212 | cfg->hp_outs = 0; | |
2213 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); | |
82bc955f TI |
2214 | } |
2215 | } | |
2216 | ||
e9edcee0 TI |
2217 | return 0; |
2218 | } | |
2219 | ||
4a471b7d TI |
2220 | /* labels for input pins */ |
2221 | const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = { | |
2222 | "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux" | |
2223 | }; | |
2224 | ||
2225 | ||
1da177e4 LT |
2226 | #ifdef CONFIG_PM |
2227 | /* | |
2228 | * power management | |
2229 | */ | |
2230 | ||
2231 | /** | |
2232 | * snd_hda_suspend - suspend the codecs | |
2233 | * @bus: the HDA bus | |
2234 | * @state: suspsend state | |
2235 | * | |
2236 | * Returns 0 if successful. | |
2237 | */ | |
2238 | int snd_hda_suspend(struct hda_bus *bus, pm_message_t state) | |
2239 | { | |
2240 | struct list_head *p; | |
2241 | ||
2242 | /* FIXME: should handle power widget capabilities */ | |
2243 | list_for_each(p, &bus->codec_list) { | |
2244 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
2245 | if (codec->patch_ops.suspend) | |
2246 | codec->patch_ops.suspend(codec, state); | |
54d17403 TI |
2247 | hda_set_power_state(codec, |
2248 | codec->afg ? codec->afg : codec->mfg, | |
2249 | AC_PWRST_D3); | |
1da177e4 LT |
2250 | } |
2251 | return 0; | |
2252 | } | |
2253 | ||
2254 | /** | |
2255 | * snd_hda_resume - resume the codecs | |
2256 | * @bus: the HDA bus | |
2257 | * @state: resume state | |
2258 | * | |
2259 | * Returns 0 if successful. | |
2260 | */ | |
2261 | int snd_hda_resume(struct hda_bus *bus) | |
2262 | { | |
2263 | struct list_head *p; | |
2264 | ||
2265 | list_for_each(p, &bus->codec_list) { | |
2266 | struct hda_codec *codec = list_entry(p, struct hda_codec, list); | |
54d17403 TI |
2267 | hda_set_power_state(codec, |
2268 | codec->afg ? codec->afg : codec->mfg, | |
2269 | AC_PWRST_D0); | |
1da177e4 LT |
2270 | if (codec->patch_ops.resume) |
2271 | codec->patch_ops.resume(codec); | |
2272 | } | |
2273 | return 0; | |
2274 | } | |
2275 | ||
2276 | /** | |
2277 | * snd_hda_resume_ctls - resume controls in the new control list | |
2278 | * @codec: the HDA codec | |
c8b6bf9b | 2279 | * @knew: the array of struct snd_kcontrol_new |
1da177e4 | 2280 | * |
c8b6bf9b | 2281 | * This function resumes the mixer controls in the struct snd_kcontrol_new array, |
1da177e4 LT |
2282 | * originally for snd_hda_add_new_ctls(). |
2283 | * The array must be terminated with an empty entry as terminator. | |
2284 | */ | |
c8b6bf9b | 2285 | int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew) |
1da177e4 | 2286 | { |
c8b6bf9b | 2287 | struct snd_ctl_elem_value *val; |
1da177e4 LT |
2288 | |
2289 | val = kmalloc(sizeof(*val), GFP_KERNEL); | |
2290 | if (! val) | |
2291 | return -ENOMEM; | |
2292 | codec->in_resume = 1; | |
2293 | for (; knew->name; knew++) { | |
2294 | int i, count; | |
2295 | count = knew->count ? knew->count : 1; | |
2296 | for (i = 0; i < count; i++) { | |
2297 | memset(val, 0, sizeof(*val)); | |
2298 | val->id.iface = knew->iface; | |
2299 | val->id.device = knew->device; | |
2300 | val->id.subdevice = knew->subdevice; | |
2301 | strcpy(val->id.name, knew->name); | |
2302 | val->id.index = knew->index ? knew->index : i; | |
2303 | /* Assume that get callback reads only from cache, | |
2304 | * not accessing to the real hardware | |
2305 | */ | |
2306 | if (snd_ctl_elem_read(codec->bus->card, val) < 0) | |
2307 | continue; | |
2308 | snd_ctl_elem_write(codec->bus->card, NULL, val); | |
2309 | } | |
2310 | } | |
2311 | codec->in_resume = 0; | |
2312 | kfree(val); | |
2313 | return 0; | |
2314 | } | |
2315 | ||
2316 | /** | |
2317 | * snd_hda_resume_spdif_out - resume the digital out | |
2318 | * @codec: the HDA codec | |
2319 | */ | |
2320 | int snd_hda_resume_spdif_out(struct hda_codec *codec) | |
2321 | { | |
2322 | return snd_hda_resume_ctls(codec, dig_mixes); | |
2323 | } | |
2324 | ||
2325 | /** | |
2326 | * snd_hda_resume_spdif_in - resume the digital in | |
2327 | * @codec: the HDA codec | |
2328 | */ | |
2329 | int snd_hda_resume_spdif_in(struct hda_codec *codec) | |
2330 | { | |
2331 | return snd_hda_resume_ctls(codec, dig_in_ctls); | |
2332 | } | |
2333 | #endif |