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