]>
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 | ||
1da177e4 LT |
22 | #include <linux/init.h> |
23 | #include <linux/delay.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/pci.h> | |
62932df8 | 26 | #include <linux/mutex.h> |
1da177e4 LT |
27 | #include <sound/core.h> |
28 | #include "hda_codec.h" | |
29 | #include <sound/asoundef.h> | |
302e9c5a | 30 | #include <sound/tlv.h> |
1da177e4 LT |
31 | #include <sound/initval.h> |
32 | #include "hda_local.h" | |
123c07ae | 33 | #include "hda_beep.h" |
2807314d | 34 | #include <sound/hda_hwdep.h> |
1da177e4 | 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[] = { | |
c8cd1281 | 47 | { 0x1002, "ATI" }, |
e5f14248 | 48 | { 0x1013, "Cirrus Logic" }, |
a9226251 | 49 | { 0x1057, "Motorola" }, |
c8cd1281 | 50 | { 0x1095, "Silicon Image" }, |
31117b78 | 51 | { 0x10de, "Nvidia" }, |
c8cd1281 | 52 | { 0x10ec, "Realtek" }, |
4e01f54b | 53 | { 0x1102, "Creative" }, |
c577b8a1 | 54 | { 0x1106, "VIA" }, |
7f16859a | 55 | { 0x111d, "IDT" }, |
c8cd1281 | 56 | { 0x11c1, "LSI" }, |
54b903ec | 57 | { 0x11d4, "Analog Devices" }, |
1da177e4 | 58 | { 0x13f6, "C-Media" }, |
a9226251 | 59 | { 0x14f1, "Conexant" }, |
c8cd1281 TI |
60 | { 0x17e8, "Chrontel" }, |
61 | { 0x1854, "LG" }, | |
8199de3b | 62 | { 0x1aec, "Wolfson Microelectronics" }, |
1da177e4 | 63 | { 0x434d, "C-Media" }, |
74c61133 | 64 | { 0x8086, "Intel" }, |
2f2f4251 | 65 | { 0x8384, "SigmaTel" }, |
1da177e4 LT |
66 | {} /* terminator */ |
67 | }; | |
68 | ||
1289e9e8 TI |
69 | static DEFINE_MUTEX(preset_mutex); |
70 | static LIST_HEAD(hda_preset_tables); | |
71 | ||
72 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) | |
73 | { | |
74 | mutex_lock(&preset_mutex); | |
75 | list_add_tail(&preset->list, &hda_preset_tables); | |
76 | mutex_unlock(&preset_mutex); | |
77 | return 0; | |
78 | } | |
ff7a3267 | 79 | EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset); |
1289e9e8 TI |
80 | |
81 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) | |
82 | { | |
83 | mutex_lock(&preset_mutex); | |
84 | list_del(&preset->list); | |
85 | mutex_unlock(&preset_mutex); | |
86 | return 0; | |
87 | } | |
ff7a3267 | 88 | EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset); |
1da177e4 | 89 | |
cb53c626 TI |
90 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
91 | static void hda_power_work(struct work_struct *work); | |
92 | static void hda_keep_power_on(struct hda_codec *codec); | |
93 | #else | |
94 | static inline void hda_keep_power_on(struct hda_codec *codec) {} | |
95 | #endif | |
96 | ||
d5191e50 TI |
97 | /** |
98 | * snd_hda_get_jack_location - Give a location string of the jack | |
99 | * @cfg: pin default config value | |
100 | * | |
101 | * Parse the pin default config value and returns the string of the | |
102 | * jack location, e.g. "Rear", "Front", etc. | |
103 | */ | |
50a9f790 MR |
104 | const char *snd_hda_get_jack_location(u32 cfg) |
105 | { | |
106 | static char *bases[7] = { | |
107 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", | |
108 | }; | |
109 | static unsigned char specials_idx[] = { | |
110 | 0x07, 0x08, | |
111 | 0x17, 0x18, 0x19, | |
112 | 0x37, 0x38 | |
113 | }; | |
114 | static char *specials[] = { | |
115 | "Rear Panel", "Drive Bar", | |
116 | "Riser", "HDMI", "ATAPI", | |
117 | "Mobile-In", "Mobile-Out" | |
118 | }; | |
119 | int i; | |
120 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; | |
121 | if ((cfg & 0x0f) < 7) | |
122 | return bases[cfg & 0x0f]; | |
123 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { | |
124 | if (cfg == specials_idx[i]) | |
125 | return specials[i]; | |
126 | } | |
127 | return "UNKNOWN"; | |
128 | } | |
ff7a3267 | 129 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
50a9f790 | 130 | |
d5191e50 TI |
131 | /** |
132 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack | |
133 | * @cfg: pin default config value | |
134 | * | |
135 | * Parse the pin default config value and returns the string of the | |
136 | * jack connectivity, i.e. external or internal connection. | |
137 | */ | |
50a9f790 MR |
138 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
139 | { | |
140 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; | |
141 | ||
142 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; | |
143 | } | |
ff7a3267 | 144 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
50a9f790 | 145 | |
d5191e50 TI |
146 | /** |
147 | * snd_hda_get_jack_type - Give a type string of the jack | |
148 | * @cfg: pin default config value | |
149 | * | |
150 | * Parse the pin default config value and returns the string of the | |
151 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. | |
152 | */ | |
50a9f790 MR |
153 | const char *snd_hda_get_jack_type(u32 cfg) |
154 | { | |
155 | static char *jack_types[16] = { | |
156 | "Line Out", "Speaker", "HP Out", "CD", | |
157 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", | |
158 | "Line In", "Aux", "Mic", "Telephony", | |
159 | "SPDIF In", "Digitial In", "Reserved", "Other" | |
160 | }; | |
161 | ||
162 | return jack_types[(cfg & AC_DEFCFG_DEVICE) | |
163 | >> AC_DEFCFG_DEVICE_SHIFT]; | |
164 | } | |
ff7a3267 | 165 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
50a9f790 | 166 | |
33fa35ed TI |
167 | /* |
168 | * Compose a 32bit command word to be sent to the HD-audio controller | |
169 | */ | |
170 | static inline unsigned int | |
171 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, | |
172 | unsigned int verb, unsigned int parm) | |
173 | { | |
174 | u32 val; | |
175 | ||
82e1b804 TI |
176 | if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || |
177 | (verb & ~0xfff) || (parm & ~0xffff)) { | |
6430aeeb WF |
178 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", |
179 | codec->addr, direct, nid, verb, parm); | |
180 | return ~0; | |
181 | } | |
182 | ||
183 | val = (u32)codec->addr << 28; | |
33fa35ed TI |
184 | val |= (u32)direct << 27; |
185 | val |= (u32)nid << 20; | |
186 | val |= verb << 8; | |
187 | val |= parm; | |
188 | return val; | |
189 | } | |
190 | ||
aa2936f5 TI |
191 | /* |
192 | * Send and receive a verb | |
193 | */ | |
194 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, | |
195 | unsigned int *res) | |
196 | { | |
197 | struct hda_bus *bus = codec->bus; | |
8dd78330 | 198 | int err; |
aa2936f5 | 199 | |
6430aeeb WF |
200 | if (cmd == ~0) |
201 | return -1; | |
202 | ||
aa2936f5 TI |
203 | if (res) |
204 | *res = -1; | |
8dd78330 | 205 | again: |
aa2936f5 TI |
206 | snd_hda_power_up(codec); |
207 | mutex_lock(&bus->cmd_mutex); | |
aa2936f5 | 208 | err = bus->ops.command(bus, cmd); |
8dd78330 | 209 | if (!err && res) |
deadff16 | 210 | *res = bus->ops.get_response(bus, codec->addr); |
aa2936f5 TI |
211 | mutex_unlock(&bus->cmd_mutex); |
212 | snd_hda_power_down(codec); | |
8dd78330 TI |
213 | if (res && *res == -1 && bus->rirb_error) { |
214 | if (bus->response_reset) { | |
215 | snd_printd("hda_codec: resetting BUS due to " | |
216 | "fatal communication error\n"); | |
217 | bus->ops.bus_reset(bus); | |
218 | } | |
219 | goto again; | |
220 | } | |
221 | /* clear reset-flag when the communication gets recovered */ | |
222 | if (!err) | |
223 | bus->response_reset = 0; | |
aa2936f5 TI |
224 | return err; |
225 | } | |
226 | ||
1da177e4 LT |
227 | /** |
228 | * snd_hda_codec_read - send a command and get the response | |
229 | * @codec: the HDA codec | |
230 | * @nid: NID to send the command | |
231 | * @direct: direct flag | |
232 | * @verb: the verb to send | |
233 | * @parm: the parameter for the verb | |
234 | * | |
235 | * Send a single command and read the corresponding response. | |
236 | * | |
237 | * Returns the obtained response value, or -1 for an error. | |
238 | */ | |
0ba21762 TI |
239 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
240 | int direct, | |
1da177e4 LT |
241 | unsigned int verb, unsigned int parm) |
242 | { | |
aa2936f5 TI |
243 | unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
244 | unsigned int res; | |
245 | codec_exec_verb(codec, cmd, &res); | |
1da177e4 LT |
246 | return res; |
247 | } | |
ff7a3267 | 248 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
1da177e4 LT |
249 | |
250 | /** | |
251 | * snd_hda_codec_write - send a single command without waiting for response | |
252 | * @codec: the HDA codec | |
253 | * @nid: NID to send the command | |
254 | * @direct: direct flag | |
255 | * @verb: the verb to send | |
256 | * @parm: the parameter for the verb | |
257 | * | |
258 | * Send a single command without waiting for response. | |
259 | * | |
260 | * Returns 0 if successful, or a negative error code. | |
261 | */ | |
262 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, | |
263 | unsigned int verb, unsigned int parm) | |
264 | { | |
aa2936f5 | 265 | unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
33fa35ed | 266 | unsigned int res; |
b20f3b83 TI |
267 | return codec_exec_verb(codec, cmd, |
268 | codec->bus->sync_write ? &res : NULL); | |
1da177e4 | 269 | } |
ff7a3267 | 270 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
1da177e4 LT |
271 | |
272 | /** | |
273 | * snd_hda_sequence_write - sequence writes | |
274 | * @codec: the HDA codec | |
275 | * @seq: VERB array to send | |
276 | * | |
277 | * Send the commands sequentially from the given array. | |
278 | * The array must be terminated with NID=0. | |
279 | */ | |
280 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) | |
281 | { | |
282 | for (; seq->nid; seq++) | |
283 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); | |
284 | } | |
ff7a3267 | 285 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
1da177e4 LT |
286 | |
287 | /** | |
288 | * snd_hda_get_sub_nodes - get the range of sub nodes | |
289 | * @codec: the HDA codec | |
290 | * @nid: NID to parse | |
291 | * @start_id: the pointer to store the start NID | |
292 | * | |
293 | * Parse the NID and store the start NID of its sub-nodes. | |
294 | * Returns the number of sub-nodes. | |
295 | */ | |
0ba21762 TI |
296 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
297 | hda_nid_t *start_id) | |
1da177e4 LT |
298 | { |
299 | unsigned int parm; | |
300 | ||
301 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); | |
e8a7f136 DT |
302 | if (parm == -1) |
303 | return 0; | |
1da177e4 LT |
304 | *start_id = (parm >> 16) & 0x7fff; |
305 | return (int)(parm & 0x7fff); | |
306 | } | |
ff7a3267 | 307 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
1da177e4 LT |
308 | |
309 | /** | |
310 | * snd_hda_get_connections - get connection list | |
311 | * @codec: the HDA codec | |
312 | * @nid: NID to parse | |
313 | * @conn_list: connection list array | |
314 | * @max_conns: max. number of connections to store | |
315 | * | |
316 | * Parses the connection list of the given widget and stores the list | |
317 | * of NIDs. | |
318 | * | |
319 | * Returns the number of connections, or a negative error code. | |
320 | */ | |
321 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, | |
322 | hda_nid_t *conn_list, int max_conns) | |
323 | { | |
324 | unsigned int parm; | |
54d17403 | 325 | int i, conn_len, conns; |
1da177e4 | 326 | unsigned int shift, num_elems, mask; |
1ba7a7c6 | 327 | unsigned int wcaps; |
54d17403 | 328 | hda_nid_t prev_nid; |
1da177e4 | 329 | |
da3cec35 TI |
330 | if (snd_BUG_ON(!conn_list || max_conns <= 0)) |
331 | return -EINVAL; | |
1da177e4 | 332 | |
1ba7a7c6 TI |
333 | wcaps = get_wcaps(codec, nid); |
334 | if (!(wcaps & AC_WCAP_CONN_LIST) && | |
335 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) { | |
16a433d8 JK |
336 | snd_printk(KERN_WARNING "hda_codec: " |
337 | "connection list not available for 0x%x\n", nid); | |
338 | return -EINVAL; | |
339 | } | |
340 | ||
1da177e4 LT |
341 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
342 | if (parm & AC_CLIST_LONG) { | |
343 | /* long form */ | |
344 | shift = 16; | |
345 | num_elems = 2; | |
346 | } else { | |
347 | /* short form */ | |
348 | shift = 8; | |
349 | num_elems = 4; | |
350 | } | |
351 | conn_len = parm & AC_CLIST_LENGTH; | |
1da177e4 LT |
352 | mask = (1 << (shift-1)) - 1; |
353 | ||
0ba21762 | 354 | if (!conn_len) |
1da177e4 LT |
355 | return 0; /* no connection */ |
356 | ||
357 | if (conn_len == 1) { | |
358 | /* single connection */ | |
0ba21762 TI |
359 | parm = snd_hda_codec_read(codec, nid, 0, |
360 | AC_VERB_GET_CONNECT_LIST, 0); | |
3c6aae44 TI |
361 | if (parm == -1 && codec->bus->rirb_error) |
362 | return -EIO; | |
1da177e4 LT |
363 | conn_list[0] = parm & mask; |
364 | return 1; | |
365 | } | |
366 | ||
367 | /* multi connection */ | |
368 | conns = 0; | |
54d17403 TI |
369 | prev_nid = 0; |
370 | for (i = 0; i < conn_len; i++) { | |
371 | int range_val; | |
372 | hda_nid_t val, n; | |
373 | ||
3c6aae44 | 374 | if (i % num_elems == 0) { |
54d17403 TI |
375 | parm = snd_hda_codec_read(codec, nid, 0, |
376 | AC_VERB_GET_CONNECT_LIST, i); | |
3c6aae44 TI |
377 | if (parm == -1 && codec->bus->rirb_error) |
378 | return -EIO; | |
379 | } | |
0ba21762 | 380 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
54d17403 | 381 | val = parm & mask; |
2e9bf247 JK |
382 | if (val == 0) { |
383 | snd_printk(KERN_WARNING "hda_codec: " | |
384 | "invalid CONNECT_LIST verb %x[%i]:%x\n", | |
385 | nid, i, parm); | |
386 | return 0; | |
387 | } | |
54d17403 TI |
388 | parm >>= shift; |
389 | if (range_val) { | |
390 | /* ranges between the previous and this one */ | |
0ba21762 TI |
391 | if (!prev_nid || prev_nid >= val) { |
392 | snd_printk(KERN_WARNING "hda_codec: " | |
393 | "invalid dep_range_val %x:%x\n", | |
394 | prev_nid, val); | |
54d17403 TI |
395 | continue; |
396 | } | |
397 | for (n = prev_nid + 1; n <= val; n++) { | |
398 | if (conns >= max_conns) { | |
0ba21762 TI |
399 | snd_printk(KERN_ERR |
400 | "Too many connections\n"); | |
1da177e4 | 401 | return -EINVAL; |
54d17403 TI |
402 | } |
403 | conn_list[conns++] = n; | |
1da177e4 | 404 | } |
54d17403 TI |
405 | } else { |
406 | if (conns >= max_conns) { | |
407 | snd_printk(KERN_ERR "Too many connections\n"); | |
408 | return -EINVAL; | |
409 | } | |
410 | conn_list[conns++] = val; | |
1da177e4 | 411 | } |
54d17403 | 412 | prev_nid = val; |
1da177e4 LT |
413 | } |
414 | return conns; | |
415 | } | |
ff7a3267 | 416 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
1da177e4 LT |
417 | |
418 | ||
419 | /** | |
420 | * snd_hda_queue_unsol_event - add an unsolicited event to queue | |
421 | * @bus: the BUS | |
422 | * @res: unsolicited event (lower 32bit of RIRB entry) | |
423 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) | |
424 | * | |
425 | * Adds the given event to the queue. The events are processed in | |
426 | * the workqueue asynchronously. Call this function in the interrupt | |
427 | * hanlder when RIRB receives an unsolicited event. | |
428 | * | |
429 | * Returns 0 if successful, or a negative error code. | |
430 | */ | |
431 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) | |
432 | { | |
433 | struct hda_bus_unsolicited *unsol; | |
434 | unsigned int wp; | |
435 | ||
0ba21762 TI |
436 | unsol = bus->unsol; |
437 | if (!unsol) | |
1da177e4 LT |
438 | return 0; |
439 | ||
440 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; | |
441 | unsol->wp = wp; | |
442 | ||
443 | wp <<= 1; | |
444 | unsol->queue[wp] = res; | |
445 | unsol->queue[wp + 1] = res_ex; | |
446 | ||
6acaed38 | 447 | queue_work(bus->workq, &unsol->work); |
1da177e4 LT |
448 | |
449 | return 0; | |
450 | } | |
ff7a3267 | 451 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
1da177e4 LT |
452 | |
453 | /* | |
5c1d1a98 | 454 | * process queued unsolicited events |
1da177e4 | 455 | */ |
c4028958 | 456 | static void process_unsol_events(struct work_struct *work) |
1da177e4 | 457 | { |
c4028958 DH |
458 | struct hda_bus_unsolicited *unsol = |
459 | container_of(work, struct hda_bus_unsolicited, work); | |
460 | struct hda_bus *bus = unsol->bus; | |
1da177e4 LT |
461 | struct hda_codec *codec; |
462 | unsigned int rp, caddr, res; | |
463 | ||
464 | while (unsol->rp != unsol->wp) { | |
465 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; | |
466 | unsol->rp = rp; | |
467 | rp <<= 1; | |
468 | res = unsol->queue[rp]; | |
469 | caddr = unsol->queue[rp + 1]; | |
0ba21762 | 470 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
1da177e4 LT |
471 | continue; |
472 | codec = bus->caddr_tbl[caddr & 0x0f]; | |
473 | if (codec && codec->patch_ops.unsol_event) | |
474 | codec->patch_ops.unsol_event(codec, res); | |
475 | } | |
476 | } | |
477 | ||
478 | /* | |
479 | * initialize unsolicited queue | |
480 | */ | |
6c1f45ea | 481 | static int init_unsol_queue(struct hda_bus *bus) |
1da177e4 LT |
482 | { |
483 | struct hda_bus_unsolicited *unsol; | |
484 | ||
9f146bb6 TI |
485 | if (bus->unsol) /* already initialized */ |
486 | return 0; | |
487 | ||
e560d8d8 | 488 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
0ba21762 TI |
489 | if (!unsol) { |
490 | snd_printk(KERN_ERR "hda_codec: " | |
491 | "can't allocate unsolicited queue\n"); | |
1da177e4 LT |
492 | return -ENOMEM; |
493 | } | |
c4028958 DH |
494 | INIT_WORK(&unsol->work, process_unsol_events); |
495 | unsol->bus = bus; | |
1da177e4 LT |
496 | bus->unsol = unsol; |
497 | return 0; | |
498 | } | |
499 | ||
500 | /* | |
501 | * destructor | |
502 | */ | |
503 | static void snd_hda_codec_free(struct hda_codec *codec); | |
504 | ||
505 | static int snd_hda_bus_free(struct hda_bus *bus) | |
506 | { | |
0ba21762 | 507 | struct hda_codec *codec, *n; |
1da177e4 | 508 | |
0ba21762 | 509 | if (!bus) |
1da177e4 | 510 | return 0; |
6acaed38 TI |
511 | if (bus->workq) |
512 | flush_workqueue(bus->workq); | |
513 | if (bus->unsol) | |
1da177e4 | 514 | kfree(bus->unsol); |
0ba21762 | 515 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
1da177e4 LT |
516 | snd_hda_codec_free(codec); |
517 | } | |
518 | if (bus->ops.private_free) | |
519 | bus->ops.private_free(bus); | |
6acaed38 TI |
520 | if (bus->workq) |
521 | destroy_workqueue(bus->workq); | |
1da177e4 LT |
522 | kfree(bus); |
523 | return 0; | |
524 | } | |
525 | ||
c8b6bf9b | 526 | static int snd_hda_bus_dev_free(struct snd_device *device) |
1da177e4 LT |
527 | { |
528 | struct hda_bus *bus = device->device_data; | |
b94d3539 | 529 | bus->shutdown = 1; |
1da177e4 LT |
530 | return snd_hda_bus_free(bus); |
531 | } | |
532 | ||
d7ffba19 TI |
533 | #ifdef CONFIG_SND_HDA_HWDEP |
534 | static int snd_hda_bus_dev_register(struct snd_device *device) | |
535 | { | |
536 | struct hda_bus *bus = device->device_data; | |
537 | struct hda_codec *codec; | |
538 | list_for_each_entry(codec, &bus->codec_list, list) { | |
539 | snd_hda_hwdep_add_sysfs(codec); | |
a2f6309e | 540 | snd_hda_hwdep_add_power_sysfs(codec); |
d7ffba19 TI |
541 | } |
542 | return 0; | |
543 | } | |
544 | #else | |
545 | #define snd_hda_bus_dev_register NULL | |
546 | #endif | |
547 | ||
1da177e4 LT |
548 | /** |
549 | * snd_hda_bus_new - create a HDA bus | |
550 | * @card: the card entry | |
551 | * @temp: the template for hda_bus information | |
552 | * @busp: the pointer to store the created bus instance | |
553 | * | |
554 | * Returns 0 if successful, or a negative error code. | |
555 | */ | |
1289e9e8 | 556 | int /*__devinit*/ snd_hda_bus_new(struct snd_card *card, |
756e2b01 TI |
557 | const struct hda_bus_template *temp, |
558 | struct hda_bus **busp) | |
1da177e4 LT |
559 | { |
560 | struct hda_bus *bus; | |
561 | int err; | |
c8b6bf9b | 562 | static struct snd_device_ops dev_ops = { |
d7ffba19 | 563 | .dev_register = snd_hda_bus_dev_register, |
1da177e4 LT |
564 | .dev_free = snd_hda_bus_dev_free, |
565 | }; | |
566 | ||
da3cec35 TI |
567 | if (snd_BUG_ON(!temp)) |
568 | return -EINVAL; | |
569 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) | |
570 | return -EINVAL; | |
1da177e4 LT |
571 | |
572 | if (busp) | |
573 | *busp = NULL; | |
574 | ||
e560d8d8 | 575 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
1da177e4 LT |
576 | if (bus == NULL) { |
577 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); | |
578 | return -ENOMEM; | |
579 | } | |
580 | ||
581 | bus->card = card; | |
582 | bus->private_data = temp->private_data; | |
583 | bus->pci = temp->pci; | |
584 | bus->modelname = temp->modelname; | |
fee2fba3 | 585 | bus->power_save = temp->power_save; |
1da177e4 LT |
586 | bus->ops = temp->ops; |
587 | ||
62932df8 | 588 | mutex_init(&bus->cmd_mutex); |
1da177e4 LT |
589 | INIT_LIST_HEAD(&bus->codec_list); |
590 | ||
e8c0ee5d TI |
591 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
592 | "hd-audio%d", card->number); | |
593 | bus->workq = create_singlethread_workqueue(bus->workq_name); | |
6acaed38 | 594 | if (!bus->workq) { |
e8c0ee5d TI |
595 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
596 | bus->workq_name); | |
6acaed38 TI |
597 | kfree(bus); |
598 | return -ENOMEM; | |
599 | } | |
600 | ||
0ba21762 TI |
601 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
602 | if (err < 0) { | |
1da177e4 LT |
603 | snd_hda_bus_free(bus); |
604 | return err; | |
605 | } | |
606 | if (busp) | |
607 | *busp = bus; | |
608 | return 0; | |
609 | } | |
ff7a3267 | 610 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
1da177e4 | 611 | |
82467611 TI |
612 | #ifdef CONFIG_SND_HDA_GENERIC |
613 | #define is_generic_config(codec) \ | |
f44ac837 | 614 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
82467611 TI |
615 | #else |
616 | #define is_generic_config(codec) 0 | |
617 | #endif | |
618 | ||
645f10c1 | 619 | #ifdef MODULE |
1289e9e8 TI |
620 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
621 | #else | |
645f10c1 | 622 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
1289e9e8 TI |
623 | #endif |
624 | ||
1da177e4 LT |
625 | /* |
626 | * find a matching codec preset | |
627 | */ | |
6c1f45ea | 628 | static const struct hda_codec_preset * |
756e2b01 | 629 | find_codec_preset(struct hda_codec *codec) |
1da177e4 | 630 | { |
1289e9e8 TI |
631 | struct hda_codec_preset_list *tbl; |
632 | const struct hda_codec_preset *preset; | |
633 | int mod_requested = 0; | |
1da177e4 | 634 | |
82467611 | 635 | if (is_generic_config(codec)) |
d5ad630b TI |
636 | return NULL; /* use the generic parser */ |
637 | ||
1289e9e8 TI |
638 | again: |
639 | mutex_lock(&preset_mutex); | |
640 | list_for_each_entry(tbl, &hda_preset_tables, list) { | |
641 | if (!try_module_get(tbl->owner)) { | |
642 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); | |
643 | continue; | |
644 | } | |
645 | for (preset = tbl->preset; preset->id; preset++) { | |
1da177e4 | 646 | u32 mask = preset->mask; |
ca7cfae9 MB |
647 | if (preset->afg && preset->afg != codec->afg) |
648 | continue; | |
649 | if (preset->mfg && preset->mfg != codec->mfg) | |
650 | continue; | |
0ba21762 | 651 | if (!mask) |
1da177e4 | 652 | mask = ~0; |
9c7f852e | 653 | if (preset->id == (codec->vendor_id & mask) && |
0ba21762 | 654 | (!preset->rev || |
1289e9e8 TI |
655 | preset->rev == codec->revision_id)) { |
656 | mutex_unlock(&preset_mutex); | |
657 | codec->owner = tbl->owner; | |
1da177e4 | 658 | return preset; |
1289e9e8 | 659 | } |
1da177e4 | 660 | } |
1289e9e8 TI |
661 | module_put(tbl->owner); |
662 | } | |
663 | mutex_unlock(&preset_mutex); | |
664 | ||
665 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { | |
666 | char name[32]; | |
667 | if (!mod_requested) | |
668 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", | |
669 | codec->vendor_id); | |
670 | else | |
671 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", | |
672 | (codec->vendor_id >> 16) & 0xffff); | |
673 | request_module(name); | |
674 | mod_requested++; | |
675 | goto again; | |
1da177e4 LT |
676 | } |
677 | return NULL; | |
678 | } | |
679 | ||
680 | /* | |
f44ac837 | 681 | * get_codec_name - store the codec name |
1da177e4 | 682 | */ |
f44ac837 | 683 | static int get_codec_name(struct hda_codec *codec) |
1da177e4 LT |
684 | { |
685 | const struct hda_vendor_id *c; | |
686 | const char *vendor = NULL; | |
687 | u16 vendor_id = codec->vendor_id >> 16; | |
812a2cca TI |
688 | char tmp[16]; |
689 | ||
690 | if (codec->vendor_name) | |
691 | goto get_chip_name; | |
1da177e4 LT |
692 | |
693 | for (c = hda_vendor_ids; c->id; c++) { | |
694 | if (c->id == vendor_id) { | |
695 | vendor = c->name; | |
696 | break; | |
697 | } | |
698 | } | |
0ba21762 | 699 | if (!vendor) { |
1da177e4 LT |
700 | sprintf(tmp, "Generic %04x", vendor_id); |
701 | vendor = tmp; | |
702 | } | |
812a2cca TI |
703 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
704 | if (!codec->vendor_name) | |
705 | return -ENOMEM; | |
706 | ||
707 | get_chip_name: | |
708 | if (codec->chip_name) | |
709 | return 0; | |
710 | ||
1da177e4 | 711 | if (codec->preset && codec->preset->name) |
812a2cca TI |
712 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
713 | else { | |
714 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); | |
715 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); | |
716 | } | |
717 | if (!codec->chip_name) | |
f44ac837 TI |
718 | return -ENOMEM; |
719 | return 0; | |
1da177e4 LT |
720 | } |
721 | ||
722 | /* | |
673b683a | 723 | * look for an AFG and MFG nodes |
1da177e4 | 724 | */ |
1289e9e8 | 725 | static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec) |
1da177e4 | 726 | { |
93e82ae7 | 727 | int i, total_nodes, function_id; |
1da177e4 LT |
728 | hda_nid_t nid; |
729 | ||
730 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | |
731 | for (i = 0; i < total_nodes; i++, nid++) { | |
93e82ae7 | 732 | function_id = snd_hda_param_read(codec, nid, |
79c944ad | 733 | AC_PAR_FUNCTION_TYPE); |
93e82ae7 | 734 | switch (function_id) { |
673b683a SK |
735 | case AC_GRP_AUDIO_FUNCTION: |
736 | codec->afg = nid; | |
79c944ad JK |
737 | codec->afg_function_id = function_id & 0xff; |
738 | codec->afg_unsol = (function_id >> 8) & 1; | |
673b683a SK |
739 | break; |
740 | case AC_GRP_MODEM_FUNCTION: | |
741 | codec->mfg = nid; | |
79c944ad JK |
742 | codec->mfg_function_id = function_id & 0xff; |
743 | codec->mfg_unsol = (function_id >> 8) & 1; | |
673b683a SK |
744 | break; |
745 | default: | |
746 | break; | |
747 | } | |
1da177e4 | 748 | } |
1da177e4 LT |
749 | } |
750 | ||
54d17403 TI |
751 | /* |
752 | * read widget caps for each widget and store in cache | |
753 | */ | |
754 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) | |
755 | { | |
756 | int i; | |
757 | hda_nid_t nid; | |
758 | ||
759 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, | |
760 | &codec->start_nid); | |
761 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); | |
0ba21762 | 762 | if (!codec->wcaps) |
54d17403 TI |
763 | return -ENOMEM; |
764 | nid = codec->start_nid; | |
765 | for (i = 0; i < codec->num_nodes; i++, nid++) | |
766 | codec->wcaps[i] = snd_hda_param_read(codec, nid, | |
767 | AC_PAR_AUDIO_WIDGET_CAP); | |
768 | return 0; | |
769 | } | |
770 | ||
3be14149 TI |
771 | /* read all pin default configurations and save codec->init_pins */ |
772 | static int read_pin_defaults(struct hda_codec *codec) | |
773 | { | |
774 | int i; | |
775 | hda_nid_t nid = codec->start_nid; | |
776 | ||
777 | for (i = 0; i < codec->num_nodes; i++, nid++) { | |
778 | struct hda_pincfg *pin; | |
779 | unsigned int wcaps = get_wcaps(codec, nid); | |
a22d543a | 780 | unsigned int wid_type = get_wcaps_type(wcaps); |
3be14149 TI |
781 | if (wid_type != AC_WID_PIN) |
782 | continue; | |
783 | pin = snd_array_new(&codec->init_pins); | |
784 | if (!pin) | |
785 | return -ENOMEM; | |
786 | pin->nid = nid; | |
787 | pin->cfg = snd_hda_codec_read(codec, nid, 0, | |
788 | AC_VERB_GET_CONFIG_DEFAULT, 0); | |
789 | } | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /* look up the given pin config list and return the item matching with NID */ | |
794 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, | |
795 | struct snd_array *array, | |
796 | hda_nid_t nid) | |
797 | { | |
798 | int i; | |
799 | for (i = 0; i < array->used; i++) { | |
800 | struct hda_pincfg *pin = snd_array_elem(array, i); | |
801 | if (pin->nid == nid) | |
802 | return pin; | |
803 | } | |
804 | return NULL; | |
805 | } | |
806 | ||
807 | /* write a config value for the given NID */ | |
808 | static void set_pincfg(struct hda_codec *codec, hda_nid_t nid, | |
809 | unsigned int cfg) | |
810 | { | |
811 | int i; | |
812 | for (i = 0; i < 4; i++) { | |
813 | snd_hda_codec_write(codec, nid, 0, | |
814 | AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i, | |
815 | cfg & 0xff); | |
816 | cfg >>= 8; | |
817 | } | |
818 | } | |
819 | ||
820 | /* set the current pin config value for the given NID. | |
821 | * the value is cached, and read via snd_hda_codec_get_pincfg() | |
822 | */ | |
823 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, | |
824 | hda_nid_t nid, unsigned int cfg) | |
825 | { | |
826 | struct hda_pincfg *pin; | |
5e7b8e0d | 827 | unsigned int oldcfg; |
3be14149 | 828 | |
b82855a0 TI |
829 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
830 | return -EINVAL; | |
831 | ||
5e7b8e0d | 832 | oldcfg = snd_hda_codec_get_pincfg(codec, nid); |
3be14149 TI |
833 | pin = look_up_pincfg(codec, list, nid); |
834 | if (!pin) { | |
835 | pin = snd_array_new(list); | |
836 | if (!pin) | |
837 | return -ENOMEM; | |
838 | pin->nid = nid; | |
839 | } | |
840 | pin->cfg = cfg; | |
5e7b8e0d TI |
841 | |
842 | /* change only when needed; e.g. if the pincfg is already present | |
843 | * in user_pins[], don't write it | |
844 | */ | |
845 | cfg = snd_hda_codec_get_pincfg(codec, nid); | |
846 | if (oldcfg != cfg) | |
847 | set_pincfg(codec, nid, cfg); | |
3be14149 TI |
848 | return 0; |
849 | } | |
850 | ||
d5191e50 TI |
851 | /** |
852 | * snd_hda_codec_set_pincfg - Override a pin default configuration | |
853 | * @codec: the HDA codec | |
854 | * @nid: NID to set the pin config | |
855 | * @cfg: the pin default config value | |
856 | * | |
857 | * Override a pin default configuration value in the cache. | |
858 | * This value can be read by snd_hda_codec_get_pincfg() in a higher | |
859 | * priority than the real hardware value. | |
860 | */ | |
3be14149 TI |
861 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
862 | hda_nid_t nid, unsigned int cfg) | |
863 | { | |
346ff70f | 864 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
3be14149 TI |
865 | } |
866 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); | |
867 | ||
d5191e50 TI |
868 | /** |
869 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration | |
870 | * @codec: the HDA codec | |
871 | * @nid: NID to get the pin config | |
872 | * | |
873 | * Get the current pin config value of the given pin NID. | |
874 | * If the pincfg value is cached or overridden via sysfs or driver, | |
875 | * returns the cached value. | |
876 | */ | |
3be14149 TI |
877 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
878 | { | |
879 | struct hda_pincfg *pin; | |
880 | ||
3be14149 | 881 | #ifdef CONFIG_SND_HDA_HWDEP |
346ff70f | 882 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
3be14149 TI |
883 | if (pin) |
884 | return pin->cfg; | |
885 | #endif | |
5e7b8e0d TI |
886 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
887 | if (pin) | |
888 | return pin->cfg; | |
3be14149 TI |
889 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
890 | if (pin) | |
891 | return pin->cfg; | |
892 | return 0; | |
893 | } | |
894 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); | |
895 | ||
896 | /* restore all current pin configs */ | |
897 | static void restore_pincfgs(struct hda_codec *codec) | |
898 | { | |
899 | int i; | |
900 | for (i = 0; i < codec->init_pins.used; i++) { | |
901 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); | |
902 | set_pincfg(codec, pin->nid, | |
903 | snd_hda_codec_get_pincfg(codec, pin->nid)); | |
904 | } | |
905 | } | |
54d17403 | 906 | |
92ee6162 TI |
907 | /** |
908 | * snd_hda_shutup_pins - Shut up all pins | |
909 | * @codec: the HDA codec | |
910 | * | |
911 | * Clear all pin controls to shup up before suspend for avoiding click noise. | |
912 | * The controls aren't cached so that they can be resumed properly. | |
913 | */ | |
914 | void snd_hda_shutup_pins(struct hda_codec *codec) | |
915 | { | |
916 | int i; | |
917 | for (i = 0; i < codec->init_pins.used; i++) { | |
918 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); | |
919 | /* use read here for syncing after issuing each verb */ | |
920 | snd_hda_codec_read(codec, pin->nid, 0, | |
921 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); | |
922 | } | |
923 | } | |
924 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); | |
925 | ||
01751f54 TI |
926 | static void init_hda_cache(struct hda_cache_rec *cache, |
927 | unsigned int record_size); | |
1fcaee6e | 928 | static void free_hda_cache(struct hda_cache_rec *cache); |
01751f54 | 929 | |
3be14149 TI |
930 | /* restore the initial pin cfgs and release all pincfg lists */ |
931 | static void restore_init_pincfgs(struct hda_codec *codec) | |
932 | { | |
346ff70f | 933 | /* first free driver_pins and user_pins, then call restore_pincfg |
3be14149 TI |
934 | * so that only the values in init_pins are restored |
935 | */ | |
346ff70f | 936 | snd_array_free(&codec->driver_pins); |
3be14149 | 937 | #ifdef CONFIG_SND_HDA_HWDEP |
346ff70f | 938 | snd_array_free(&codec->user_pins); |
3be14149 TI |
939 | #endif |
940 | restore_pincfgs(codec); | |
941 | snd_array_free(&codec->init_pins); | |
942 | } | |
943 | ||
1da177e4 LT |
944 | /* |
945 | * codec destructor | |
946 | */ | |
947 | static void snd_hda_codec_free(struct hda_codec *codec) | |
948 | { | |
0ba21762 | 949 | if (!codec) |
1da177e4 | 950 | return; |
3be14149 | 951 | restore_init_pincfgs(codec); |
cb53c626 TI |
952 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
953 | cancel_delayed_work(&codec->power_work); | |
6acaed38 | 954 | flush_workqueue(codec->bus->workq); |
cb53c626 | 955 | #endif |
1da177e4 | 956 | list_del(&codec->list); |
d13bd412 | 957 | snd_array_free(&codec->mixers); |
5b0cb1d8 | 958 | snd_array_free(&codec->nids); |
1da177e4 LT |
959 | codec->bus->caddr_tbl[codec->addr] = NULL; |
960 | if (codec->patch_ops.free) | |
961 | codec->patch_ops.free(codec); | |
1289e9e8 | 962 | module_put(codec->owner); |
01751f54 | 963 | free_hda_cache(&codec->amp_cache); |
b3ac5636 | 964 | free_hda_cache(&codec->cmd_cache); |
812a2cca TI |
965 | kfree(codec->vendor_name); |
966 | kfree(codec->chip_name); | |
f44ac837 | 967 | kfree(codec->modelname); |
54d17403 | 968 | kfree(codec->wcaps); |
1da177e4 LT |
969 | kfree(codec); |
970 | } | |
971 | ||
bb6ac72f TI |
972 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
973 | unsigned int power_state); | |
974 | ||
1da177e4 LT |
975 | /** |
976 | * snd_hda_codec_new - create a HDA codec | |
977 | * @bus: the bus to assign | |
978 | * @codec_addr: the codec address | |
979 | * @codecp: the pointer to store the generated codec | |
980 | * | |
981 | * Returns 0 if successful, or a negative error code. | |
982 | */ | |
28aedaf7 NL |
983 | int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, |
984 | unsigned int codec_addr, | |
985 | struct hda_codec **codecp) | |
1da177e4 LT |
986 | { |
987 | struct hda_codec *codec; | |
ba443687 | 988 | char component[31]; |
1da177e4 LT |
989 | int err; |
990 | ||
da3cec35 TI |
991 | if (snd_BUG_ON(!bus)) |
992 | return -EINVAL; | |
993 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) | |
994 | return -EINVAL; | |
1da177e4 LT |
995 | |
996 | if (bus->caddr_tbl[codec_addr]) { | |
0ba21762 TI |
997 | snd_printk(KERN_ERR "hda_codec: " |
998 | "address 0x%x is already occupied\n", codec_addr); | |
1da177e4 LT |
999 | return -EBUSY; |
1000 | } | |
1001 | ||
e560d8d8 | 1002 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
1da177e4 LT |
1003 | if (codec == NULL) { |
1004 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); | |
1005 | return -ENOMEM; | |
1006 | } | |
1007 | ||
1008 | codec->bus = bus; | |
1009 | codec->addr = codec_addr; | |
62932df8 | 1010 | mutex_init(&codec->spdif_mutex); |
5a9e02e9 | 1011 | mutex_init(&codec->control_mutex); |
01751f54 | 1012 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
b3ac5636 | 1013 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
5b0cb1d8 JK |
1014 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
1015 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); | |
3be14149 | 1016 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
346ff70f | 1017 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
6c1f45ea TI |
1018 | if (codec->bus->modelname) { |
1019 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); | |
1020 | if (!codec->modelname) { | |
1021 | snd_hda_codec_free(codec); | |
1022 | return -ENODEV; | |
1023 | } | |
1024 | } | |
1da177e4 | 1025 | |
cb53c626 TI |
1026 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
1027 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); | |
1028 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. | |
1029 | * the caller has to power down appropriatley after initialization | |
1030 | * phase. | |
1031 | */ | |
1032 | hda_keep_power_on(codec); | |
1033 | #endif | |
1034 | ||
1da177e4 LT |
1035 | list_add_tail(&codec->list, &bus->codec_list); |
1036 | bus->caddr_tbl[codec_addr] = codec; | |
1037 | ||
0ba21762 TI |
1038 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
1039 | AC_PAR_VENDOR_ID); | |
111d3af5 TI |
1040 | if (codec->vendor_id == -1) |
1041 | /* read again, hopefully the access method was corrected | |
1042 | * in the last read... | |
1043 | */ | |
1044 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, | |
1045 | AC_PAR_VENDOR_ID); | |
0ba21762 TI |
1046 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
1047 | AC_PAR_SUBSYSTEM_ID); | |
1048 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, | |
1049 | AC_PAR_REV_ID); | |
1da177e4 | 1050 | |
673b683a | 1051 | setup_fg_nodes(codec); |
0ba21762 | 1052 | if (!codec->afg && !codec->mfg) { |
673b683a | 1053 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
3be14149 TI |
1054 | err = -ENODEV; |
1055 | goto error; | |
1da177e4 LT |
1056 | } |
1057 | ||
3be14149 TI |
1058 | err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg); |
1059 | if (err < 0) { | |
54d17403 | 1060 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
3be14149 | 1061 | goto error; |
54d17403 | 1062 | } |
3be14149 TI |
1063 | err = read_pin_defaults(codec); |
1064 | if (err < 0) | |
1065 | goto error; | |
54d17403 | 1066 | |
0ba21762 | 1067 | if (!codec->subsystem_id) { |
86284e45 | 1068 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
0ba21762 TI |
1069 | codec->subsystem_id = |
1070 | snd_hda_codec_read(codec, nid, 0, | |
1071 | AC_VERB_GET_SUBSYSTEM_ID, 0); | |
86284e45 TI |
1072 | } |
1073 | ||
bb6ac72f TI |
1074 | /* power-up all before initialization */ |
1075 | hda_set_power_state(codec, | |
1076 | codec->afg ? codec->afg : codec->mfg, | |
1077 | AC_PWRST_D0); | |
1078 | ||
6c1f45ea TI |
1079 | snd_hda_codec_proc_new(codec); |
1080 | ||
6c1f45ea | 1081 | snd_hda_create_hwdep(codec); |
6c1f45ea TI |
1082 | |
1083 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, | |
1084 | codec->subsystem_id, codec->revision_id); | |
1085 | snd_component_add(codec->bus->card, component); | |
1086 | ||
1087 | if (codecp) | |
1088 | *codecp = codec; | |
1089 | return 0; | |
3be14149 TI |
1090 | |
1091 | error: | |
1092 | snd_hda_codec_free(codec); | |
1093 | return err; | |
6c1f45ea | 1094 | } |
ff7a3267 | 1095 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
6c1f45ea | 1096 | |
d5191e50 TI |
1097 | /** |
1098 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec | |
1099 | * @codec: the HDA codec | |
1100 | * | |
1101 | * Start parsing of the given codec tree and (re-)initialize the whole | |
1102 | * patch instance. | |
1103 | * | |
1104 | * Returns 0 if successful or a negative error code. | |
1105 | */ | |
6c1f45ea TI |
1106 | int snd_hda_codec_configure(struct hda_codec *codec) |
1107 | { | |
1108 | int err; | |
1109 | ||
d5ad630b | 1110 | codec->preset = find_codec_preset(codec); |
812a2cca | 1111 | if (!codec->vendor_name || !codec->chip_name) { |
f44ac837 TI |
1112 | err = get_codec_name(codec); |
1113 | if (err < 0) | |
1114 | return err; | |
1115 | } | |
1da177e4 | 1116 | |
82467611 | 1117 | if (is_generic_config(codec)) { |
1da177e4 | 1118 | err = snd_hda_parse_generic_codec(codec); |
82467611 TI |
1119 | goto patched; |
1120 | } | |
82467611 TI |
1121 | if (codec->preset && codec->preset->patch) { |
1122 | err = codec->preset->patch(codec); | |
1123 | goto patched; | |
1124 | } | |
1125 | ||
1126 | /* call the default parser */ | |
82467611 | 1127 | err = snd_hda_parse_generic_codec(codec); |
35a1e0cc TI |
1128 | if (err < 0) |
1129 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); | |
82467611 TI |
1130 | |
1131 | patched: | |
6c1f45ea TI |
1132 | if (!err && codec->patch_ops.unsol_event) |
1133 | err = init_unsol_queue(codec->bus); | |
f62faedb TI |
1134 | /* audio codec should override the mixer name */ |
1135 | if (!err && (codec->afg || !*codec->bus->card->mixername)) | |
1136 | snprintf(codec->bus->card->mixername, | |
1137 | sizeof(codec->bus->card->mixername), | |
1138 | "%s %s", codec->vendor_name, codec->chip_name); | |
6c1f45ea | 1139 | return err; |
1da177e4 | 1140 | } |
a1e21c90 | 1141 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
1da177e4 LT |
1142 | |
1143 | /** | |
1144 | * snd_hda_codec_setup_stream - set up the codec for streaming | |
1145 | * @codec: the CODEC to set up | |
1146 | * @nid: the NID to set up | |
1147 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. | |
1148 | * @channel_id: channel id to pass, zero based. | |
1149 | * @format: stream format. | |
1150 | */ | |
0ba21762 TI |
1151 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
1152 | u32 stream_tag, | |
1da177e4 LT |
1153 | int channel_id, int format) |
1154 | { | |
0ba21762 | 1155 | if (!nid) |
d21b37ea TI |
1156 | return; |
1157 | ||
0ba21762 TI |
1158 | snd_printdd("hda_codec_setup_stream: " |
1159 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", | |
1da177e4 LT |
1160 | nid, stream_tag, channel_id, format); |
1161 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, | |
1162 | (stream_tag << 4) | channel_id); | |
1163 | msleep(1); | |
1164 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format); | |
1165 | } | |
ff7a3267 | 1166 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
1da177e4 | 1167 | |
d5191e50 TI |
1168 | /** |
1169 | * snd_hda_codec_cleanup_stream - clean up the codec for closing | |
1170 | * @codec: the CODEC to clean up | |
1171 | * @nid: the NID to clean up | |
1172 | */ | |
888afa15 TI |
1173 | void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid) |
1174 | { | |
1175 | if (!nid) | |
1176 | return; | |
1177 | ||
1178 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); | |
1179 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); | |
1180 | #if 0 /* keep the format */ | |
1181 | msleep(1); | |
1182 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0); | |
1183 | #endif | |
1184 | } | |
ff7a3267 | 1185 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream); |
888afa15 | 1186 | |
1da177e4 LT |
1187 | /* |
1188 | * amp access functions | |
1189 | */ | |
1190 | ||
4a19faee | 1191 | /* FIXME: more better hash key? */ |
28aedaf7 | 1192 | #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) |
1327a32b | 1193 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
92c7c8a7 TI |
1194 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
1195 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) | |
1da177e4 | 1196 | #define INFO_AMP_CAPS (1<<0) |
4a19faee | 1197 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
1da177e4 LT |
1198 | |
1199 | /* initialize the hash table */ | |
1289e9e8 | 1200 | static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache, |
01751f54 TI |
1201 | unsigned int record_size) |
1202 | { | |
1203 | memset(cache, 0, sizeof(*cache)); | |
1204 | memset(cache->hash, 0xff, sizeof(cache->hash)); | |
603c4019 | 1205 | snd_array_init(&cache->buf, record_size, 64); |
01751f54 TI |
1206 | } |
1207 | ||
1fcaee6e | 1208 | static void free_hda_cache(struct hda_cache_rec *cache) |
1da177e4 | 1209 | { |
603c4019 | 1210 | snd_array_free(&cache->buf); |
1da177e4 LT |
1211 | } |
1212 | ||
1213 | /* query the hash. allocate an entry if not found. */ | |
a68d5a54 | 1214 | static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key) |
1da177e4 | 1215 | { |
01751f54 TI |
1216 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
1217 | u16 cur = cache->hash[idx]; | |
1218 | struct hda_cache_head *info; | |
1da177e4 LT |
1219 | |
1220 | while (cur != 0xffff) { | |
f43aa025 | 1221 | info = snd_array_elem(&cache->buf, cur); |
1da177e4 LT |
1222 | if (info->key == key) |
1223 | return info; | |
1224 | cur = info->next; | |
1225 | } | |
a68d5a54 TI |
1226 | return NULL; |
1227 | } | |
1da177e4 | 1228 | |
a68d5a54 TI |
1229 | /* query the hash. allocate an entry if not found. */ |
1230 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, | |
1231 | u32 key) | |
1232 | { | |
1233 | struct hda_cache_head *info = get_hash(cache, key); | |
1234 | if (!info) { | |
1235 | u16 idx, cur; | |
1236 | /* add a new hash entry */ | |
1237 | info = snd_array_new(&cache->buf); | |
1238 | if (!info) | |
1239 | return NULL; | |
1240 | cur = snd_array_index(&cache->buf, info); | |
1241 | info->key = key; | |
1242 | info->val = 0; | |
1243 | idx = key % (u16)ARRAY_SIZE(cache->hash); | |
1244 | info->next = cache->hash[idx]; | |
1245 | cache->hash[idx] = cur; | |
1246 | } | |
1da177e4 LT |
1247 | return info; |
1248 | } | |
1249 | ||
01751f54 TI |
1250 | /* query and allocate an amp hash entry */ |
1251 | static inline struct hda_amp_info * | |
1252 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) | |
1253 | { | |
1254 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); | |
1255 | } | |
1256 | ||
d5191e50 TI |
1257 | /** |
1258 | * query_amp_caps - query AMP capabilities | |
1259 | * @codec: the HD-auio codec | |
1260 | * @nid: the NID to query | |
1261 | * @direction: either #HDA_INPUT or #HDA_OUTPUT | |
1262 | * | |
1263 | * Query AMP capabilities for the given widget and direction. | |
1264 | * Returns the obtained capability bits. | |
1265 | * | |
1266 | * When cap bits have been already read, this doesn't read again but | |
1267 | * returns the cached value. | |
1da177e4 | 1268 | */ |
09a99959 | 1269 | u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) |
1da177e4 | 1270 | { |
0ba21762 | 1271 | struct hda_amp_info *info; |
1da177e4 | 1272 | |
0ba21762 TI |
1273 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
1274 | if (!info) | |
1da177e4 | 1275 | return 0; |
01751f54 | 1276 | if (!(info->head.val & INFO_AMP_CAPS)) { |
0ba21762 | 1277 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
1da177e4 | 1278 | nid = codec->afg; |
0ba21762 TI |
1279 | info->amp_caps = snd_hda_param_read(codec, nid, |
1280 | direction == HDA_OUTPUT ? | |
1281 | AC_PAR_AMP_OUT_CAP : | |
1282 | AC_PAR_AMP_IN_CAP); | |
b75e53f0 | 1283 | if (info->amp_caps) |
01751f54 | 1284 | info->head.val |= INFO_AMP_CAPS; |
1da177e4 LT |
1285 | } |
1286 | return info->amp_caps; | |
1287 | } | |
ff7a3267 | 1288 | EXPORT_SYMBOL_HDA(query_amp_caps); |
1da177e4 | 1289 | |
d5191e50 TI |
1290 | /** |
1291 | * snd_hda_override_amp_caps - Override the AMP capabilities | |
1292 | * @codec: the CODEC to clean up | |
1293 | * @nid: the NID to clean up | |
1294 | * @direction: either #HDA_INPUT or #HDA_OUTPUT | |
1295 | * @caps: the capability bits to set | |
1296 | * | |
1297 | * Override the cached AMP caps bits value by the given one. | |
1298 | * This function is useful if the driver needs to adjust the AMP ranges, | |
1299 | * e.g. limit to 0dB, etc. | |
1300 | * | |
1301 | * Returns zero if successful or a negative error code. | |
1302 | */ | |
897cc188 TI |
1303 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
1304 | unsigned int caps) | |
1305 | { | |
1306 | struct hda_amp_info *info; | |
1307 | ||
1308 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0)); | |
1309 | if (!info) | |
1310 | return -EINVAL; | |
1311 | info->amp_caps = caps; | |
01751f54 | 1312 | info->head.val |= INFO_AMP_CAPS; |
897cc188 TI |
1313 | return 0; |
1314 | } | |
ff7a3267 | 1315 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
1327a32b | 1316 | |
92c7c8a7 TI |
1317 | static unsigned int |
1318 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key, | |
1319 | unsigned int (*func)(struct hda_codec *, hda_nid_t)) | |
1327a32b TI |
1320 | { |
1321 | struct hda_amp_info *info; | |
1322 | ||
92c7c8a7 | 1323 | info = get_alloc_amp_hash(codec, key); |
1327a32b TI |
1324 | if (!info) |
1325 | return 0; | |
1326 | if (!info->head.val) { | |
1327a32b | 1327 | info->head.val |= INFO_AMP_CAPS; |
92c7c8a7 | 1328 | info->amp_caps = func(codec, nid); |
1327a32b TI |
1329 | } |
1330 | return info->amp_caps; | |
1331 | } | |
92c7c8a7 TI |
1332 | |
1333 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid) | |
1334 | { | |
1335 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); | |
1336 | } | |
1337 | ||
d5191e50 TI |
1338 | /** |
1339 | * snd_hda_query_pin_caps - Query PIN capabilities | |
1340 | * @codec: the HD-auio codec | |
1341 | * @nid: the NID to query | |
1342 | * | |
1343 | * Query PIN capabilities for the given widget. | |
1344 | * Returns the obtained capability bits. | |
1345 | * | |
1346 | * When cap bits have been already read, this doesn't read again but | |
1347 | * returns the cached value. | |
1348 | */ | |
92c7c8a7 TI |
1349 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
1350 | { | |
1351 | return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid), | |
1352 | read_pin_cap); | |
1353 | } | |
1327a32b | 1354 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
897cc188 | 1355 | |
864f92be WF |
1356 | /** |
1357 | * snd_hda_pin_sense - execute pin sense measurement | |
1358 | * @codec: the CODEC to sense | |
1359 | * @nid: the pin NID to sense | |
1360 | * | |
1361 | * Execute necessary pin sense measurement and return its Presence Detect, | |
1362 | * Impedance, ELD Valid etc. status bits. | |
1363 | */ | |
1364 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) | |
1365 | { | |
729d55ba | 1366 | u32 pincap; |
864f92be | 1367 | |
729d55ba TI |
1368 | if (!codec->no_trigger_sense) { |
1369 | pincap = snd_hda_query_pin_caps(codec, nid); | |
1370 | if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ | |
28aedaf7 NL |
1371 | snd_hda_codec_read(codec, nid, 0, |
1372 | AC_VERB_SET_PIN_SENSE, 0); | |
729d55ba | 1373 | } |
864f92be WF |
1374 | return snd_hda_codec_read(codec, nid, 0, |
1375 | AC_VERB_GET_PIN_SENSE, 0); | |
1376 | } | |
1377 | EXPORT_SYMBOL_HDA(snd_hda_pin_sense); | |
1378 | ||
1379 | /** | |
1380 | * snd_hda_jack_detect - query pin Presence Detect status | |
1381 | * @codec: the CODEC to sense | |
1382 | * @nid: the pin NID to sense | |
1383 | * | |
1384 | * Query and return the pin's Presence Detect status. | |
1385 | */ | |
1386 | int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) | |
1387 | { | |
28aedaf7 NL |
1388 | u32 sense = snd_hda_pin_sense(codec, nid); |
1389 | return !!(sense & AC_PINSENSE_PRESENCE); | |
864f92be WF |
1390 | } |
1391 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect); | |
1392 | ||
1da177e4 LT |
1393 | /* |
1394 | * read the current volume to info | |
4a19faee | 1395 | * if the cache exists, read the cache value. |
1da177e4 | 1396 | */ |
0ba21762 TI |
1397 | static unsigned int get_vol_mute(struct hda_codec *codec, |
1398 | struct hda_amp_info *info, hda_nid_t nid, | |
1399 | int ch, int direction, int index) | |
1da177e4 LT |
1400 | { |
1401 | u32 val, parm; | |
1402 | ||
01751f54 | 1403 | if (info->head.val & INFO_AMP_VOL(ch)) |
4a19faee | 1404 | return info->vol[ch]; |
1da177e4 LT |
1405 | |
1406 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; | |
1407 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; | |
1408 | parm |= index; | |
0ba21762 TI |
1409 | val = snd_hda_codec_read(codec, nid, 0, |
1410 | AC_VERB_GET_AMP_GAIN_MUTE, parm); | |
1da177e4 | 1411 | info->vol[ch] = val & 0xff; |
01751f54 | 1412 | info->head.val |= INFO_AMP_VOL(ch); |
4a19faee | 1413 | return info->vol[ch]; |
1da177e4 LT |
1414 | } |
1415 | ||
1416 | /* | |
4a19faee | 1417 | * write the current volume in info to the h/w and update the cache |
1da177e4 | 1418 | */ |
4a19faee | 1419 | static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, |
0ba21762 TI |
1420 | hda_nid_t nid, int ch, int direction, int index, |
1421 | int val) | |
1da177e4 LT |
1422 | { |
1423 | u32 parm; | |
1424 | ||
1425 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; | |
1426 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; | |
1427 | parm |= index << AC_AMP_SET_INDEX_SHIFT; | |
1428 | parm |= val; | |
1429 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); | |
4a19faee | 1430 | info->vol[ch] = val; |
1da177e4 LT |
1431 | } |
1432 | ||
d5191e50 TI |
1433 | /** |
1434 | * snd_hda_codec_amp_read - Read AMP value | |
1435 | * @codec: HD-audio codec | |
1436 | * @nid: NID to read the AMP value | |
1437 | * @ch: channel (left=0 or right=1) | |
1438 | * @direction: #HDA_INPUT or #HDA_OUTPUT | |
1439 | * @index: the index value (only for input direction) | |
1440 | * | |
1441 | * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit. | |
1da177e4 | 1442 | */ |
834be88d TI |
1443 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
1444 | int direction, int index) | |
1da177e4 | 1445 | { |
0ba21762 TI |
1446 | struct hda_amp_info *info; |
1447 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); | |
1448 | if (!info) | |
1da177e4 | 1449 | return 0; |
4a19faee | 1450 | return get_vol_mute(codec, info, nid, ch, direction, index); |
1da177e4 | 1451 | } |
ff7a3267 | 1452 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
1da177e4 | 1453 | |
d5191e50 TI |
1454 | /** |
1455 | * snd_hda_codec_amp_update - update the AMP value | |
1456 | * @codec: HD-audio codec | |
1457 | * @nid: NID to read the AMP value | |
1458 | * @ch: channel (left=0 or right=1) | |
1459 | * @direction: #HDA_INPUT or #HDA_OUTPUT | |
1460 | * @idx: the index value (only for input direction) | |
1461 | * @mask: bit mask to set | |
1462 | * @val: the bits value to set | |
1463 | * | |
1464 | * Update the AMP value with a bit mask. | |
1465 | * Returns 0 if the value is unchanged, 1 if changed. | |
4a19faee | 1466 | */ |
834be88d TI |
1467 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
1468 | int direction, int idx, int mask, int val) | |
1da177e4 | 1469 | { |
0ba21762 | 1470 | struct hda_amp_info *info; |
4a19faee | 1471 | |
0ba21762 TI |
1472 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
1473 | if (!info) | |
1da177e4 | 1474 | return 0; |
46712646 TI |
1475 | if (snd_BUG_ON(mask & ~0xff)) |
1476 | mask &= 0xff; | |
4a19faee TI |
1477 | val &= mask; |
1478 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; | |
82beb8fd | 1479 | if (info->vol[ch] == val) |
1da177e4 | 1480 | return 0; |
4a19faee | 1481 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
1da177e4 LT |
1482 | return 1; |
1483 | } | |
ff7a3267 | 1484 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
1da177e4 | 1485 | |
d5191e50 TI |
1486 | /** |
1487 | * snd_hda_codec_amp_stereo - update the AMP stereo values | |
1488 | * @codec: HD-audio codec | |
1489 | * @nid: NID to read the AMP value | |
1490 | * @direction: #HDA_INPUT or #HDA_OUTPUT | |
1491 | * @idx: the index value (only for input direction) | |
1492 | * @mask: bit mask to set | |
1493 | * @val: the bits value to set | |
1494 | * | |
1495 | * Update the AMP values like snd_hda_codec_amp_update(), but for a | |
1496 | * stereo widget with the same mask and value. | |
47fd830a TI |
1497 | */ |
1498 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, | |
1499 | int direction, int idx, int mask, int val) | |
1500 | { | |
1501 | int ch, ret = 0; | |
46712646 TI |
1502 | |
1503 | if (snd_BUG_ON(mask & ~0xff)) | |
1504 | mask &= 0xff; | |
47fd830a TI |
1505 | for (ch = 0; ch < 2; ch++) |
1506 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, | |
1507 | idx, mask, val); | |
1508 | return ret; | |
1509 | } | |
ff7a3267 | 1510 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
47fd830a | 1511 | |
cb53c626 | 1512 | #ifdef SND_HDA_NEEDS_RESUME |
d5191e50 TI |
1513 | /** |
1514 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache | |
1515 | * @codec: HD-audio codec | |
1516 | * | |
1517 | * Resume the all amp commands from the cache. | |
1518 | */ | |
b3ac5636 TI |
1519 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
1520 | { | |
603c4019 | 1521 | struct hda_amp_info *buffer = codec->amp_cache.buf.list; |
b3ac5636 TI |
1522 | int i; |
1523 | ||
603c4019 | 1524 | for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) { |
b3ac5636 TI |
1525 | u32 key = buffer->head.key; |
1526 | hda_nid_t nid; | |
1527 | unsigned int idx, dir, ch; | |
1528 | if (!key) | |
1529 | continue; | |
1530 | nid = key & 0xff; | |
1531 | idx = (key >> 16) & 0xff; | |
1532 | dir = (key >> 24) & 0xff; | |
1533 | for (ch = 0; ch < 2; ch++) { | |
1534 | if (!(buffer->head.val & INFO_AMP_VOL(ch))) | |
1535 | continue; | |
1536 | put_vol_mute(codec, buffer, nid, ch, dir, idx, | |
1537 | buffer->vol[ch]); | |
1538 | } | |
1539 | } | |
1540 | } | |
ff7a3267 | 1541 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
cb53c626 | 1542 | #endif /* SND_HDA_NEEDS_RESUME */ |
1da177e4 | 1543 | |
d5191e50 TI |
1544 | /** |
1545 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer | |
1546 | * | |
1547 | * The control element is supposed to have the private_value field | |
1548 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1549 | */ | |
0ba21762 TI |
1550 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
1551 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
1552 | { |
1553 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1554 | u16 nid = get_amp_nid(kcontrol); | |
1555 | u8 chs = get_amp_channels(kcontrol); | |
1556 | int dir = get_amp_direction(kcontrol); | |
29fdbec2 | 1557 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 LT |
1558 | u32 caps; |
1559 | ||
1560 | caps = query_amp_caps(codec, nid, dir); | |
0ba21762 TI |
1561 | /* num steps */ |
1562 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | |
1563 | if (!caps) { | |
1564 | printk(KERN_WARNING "hda_codec: " | |
9c8f2abd TI |
1565 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
1566 | kcontrol->id.name); | |
1da177e4 LT |
1567 | return -EINVAL; |
1568 | } | |
29fdbec2 TI |
1569 | if (ofs < caps) |
1570 | caps -= ofs; | |
1da177e4 LT |
1571 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1572 | uinfo->count = chs == 3 ? 2 : 1; | |
1573 | uinfo->value.integer.min = 0; | |
1574 | uinfo->value.integer.max = caps; | |
1575 | return 0; | |
1576 | } | |
ff7a3267 | 1577 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
1da177e4 | 1578 | |
29fdbec2 TI |
1579 | |
1580 | static inline unsigned int | |
1581 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, | |
1582 | int ch, int dir, int idx, unsigned int ofs) | |
1583 | { | |
1584 | unsigned int val; | |
1585 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); | |
1586 | val &= HDA_AMP_VOLMASK; | |
1587 | if (val >= ofs) | |
1588 | val -= ofs; | |
1589 | else | |
1590 | val = 0; | |
1591 | return val; | |
1592 | } | |
1593 | ||
1594 | static inline int | |
1595 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, | |
1596 | int ch, int dir, int idx, unsigned int ofs, | |
1597 | unsigned int val) | |
1598 | { | |
1599 | if (val > 0) | |
1600 | val += ofs; | |
1601 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, | |
1602 | HDA_AMP_VOLMASK, val); | |
1603 | } | |
1604 | ||
d5191e50 TI |
1605 | /** |
1606 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume | |
1607 | * | |
1608 | * The control element is supposed to have the private_value field | |
1609 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1610 | */ | |
0ba21762 TI |
1611 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
1612 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
1613 | { |
1614 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1615 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1616 | int chs = get_amp_channels(kcontrol); | |
1617 | int dir = get_amp_direction(kcontrol); | |
1618 | int idx = get_amp_index(kcontrol); | |
29fdbec2 | 1619 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 LT |
1620 | long *valp = ucontrol->value.integer.value; |
1621 | ||
1622 | if (chs & 1) | |
29fdbec2 | 1623 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
1da177e4 | 1624 | if (chs & 2) |
29fdbec2 | 1625 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
1da177e4 LT |
1626 | return 0; |
1627 | } | |
ff7a3267 | 1628 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
1da177e4 | 1629 | |
d5191e50 TI |
1630 | /** |
1631 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume | |
1632 | * | |
1633 | * The control element is supposed to have the private_value field | |
1634 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1635 | */ | |
0ba21762 TI |
1636 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
1637 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
1638 | { |
1639 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1640 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1641 | int chs = get_amp_channels(kcontrol); | |
1642 | int dir = get_amp_direction(kcontrol); | |
1643 | int idx = get_amp_index(kcontrol); | |
29fdbec2 | 1644 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 LT |
1645 | long *valp = ucontrol->value.integer.value; |
1646 | int change = 0; | |
1647 | ||
cb53c626 | 1648 | snd_hda_power_up(codec); |
b9f5a89c | 1649 | if (chs & 1) { |
29fdbec2 | 1650 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
b9f5a89c NG |
1651 | valp++; |
1652 | } | |
4a19faee | 1653 | if (chs & 2) |
29fdbec2 | 1654 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
cb53c626 | 1655 | snd_hda_power_down(codec); |
1da177e4 LT |
1656 | return change; |
1657 | } | |
ff7a3267 | 1658 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
1da177e4 | 1659 | |
d5191e50 TI |
1660 | /** |
1661 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume | |
1662 | * | |
1663 | * The control element is supposed to have the private_value field | |
1664 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1665 | */ | |
302e9c5a JK |
1666 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
1667 | unsigned int size, unsigned int __user *_tlv) | |
1668 | { | |
1669 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1670 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1671 | int dir = get_amp_direction(kcontrol); | |
29fdbec2 | 1672 | unsigned int ofs = get_amp_offset(kcontrol); |
302e9c5a JK |
1673 | u32 caps, val1, val2; |
1674 | ||
1675 | if (size < 4 * sizeof(unsigned int)) | |
1676 | return -ENOMEM; | |
1677 | caps = query_amp_caps(codec, nid, dir); | |
0ba21762 TI |
1678 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
1679 | val2 = (val2 + 1) * 25; | |
302e9c5a | 1680 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
29fdbec2 | 1681 | val1 += ofs; |
302e9c5a | 1682 | val1 = ((int)val1) * ((int)val2); |
302e9c5a JK |
1683 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
1684 | return -EFAULT; | |
1685 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) | |
1686 | return -EFAULT; | |
1687 | if (put_user(val1, _tlv + 2)) | |
1688 | return -EFAULT; | |
1689 | if (put_user(val2, _tlv + 3)) | |
1690 | return -EFAULT; | |
1691 | return 0; | |
1692 | } | |
ff7a3267 | 1693 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
302e9c5a | 1694 | |
d5191e50 TI |
1695 | /** |
1696 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control | |
1697 | * @codec: HD-audio codec | |
1698 | * @nid: NID of a reference widget | |
1699 | * @dir: #HDA_INPUT or #HDA_OUTPUT | |
1700 | * @tlv: TLV data to be stored, at least 4 elements | |
1701 | * | |
1702 | * Set (static) TLV data for a virtual master volume using the AMP caps | |
1703 | * obtained from the reference NID. | |
1704 | * The volume range is recalculated as if the max volume is 0dB. | |
2134ea4f TI |
1705 | */ |
1706 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, | |
1707 | unsigned int *tlv) | |
1708 | { | |
1709 | u32 caps; | |
1710 | int nums, step; | |
1711 | ||
1712 | caps = query_amp_caps(codec, nid, dir); | |
1713 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | |
1714 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; | |
1715 | step = (step + 1) * 25; | |
1716 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; | |
1717 | tlv[1] = 2 * sizeof(unsigned int); | |
1718 | tlv[2] = -nums * step; | |
1719 | tlv[3] = step; | |
1720 | } | |
ff7a3267 | 1721 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
2134ea4f TI |
1722 | |
1723 | /* find a mixer control element with the given name */ | |
09f99701 TI |
1724 | static struct snd_kcontrol * |
1725 | _snd_hda_find_mixer_ctl(struct hda_codec *codec, | |
1726 | const char *name, int idx) | |
2134ea4f TI |
1727 | { |
1728 | struct snd_ctl_elem_id id; | |
1729 | memset(&id, 0, sizeof(id)); | |
1730 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
09f99701 | 1731 | id.index = idx; |
18cb7109 TI |
1732 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
1733 | return NULL; | |
2134ea4f TI |
1734 | strcpy(id.name, name); |
1735 | return snd_ctl_find_id(codec->bus->card, &id); | |
1736 | } | |
1737 | ||
d5191e50 TI |
1738 | /** |
1739 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name | |
1740 | * @codec: HD-audio codec | |
1741 | * @name: ctl id name string | |
1742 | * | |
1743 | * Get the control element with the given id string and IFACE_MIXER. | |
1744 | */ | |
09f99701 TI |
1745 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
1746 | const char *name) | |
1747 | { | |
1748 | return _snd_hda_find_mixer_ctl(codec, name, 0); | |
1749 | } | |
ff7a3267 | 1750 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
09f99701 | 1751 | |
d5191e50 | 1752 | /** |
5b0cb1d8 | 1753 | * snd_hda_ctl_add - Add a control element and assign to the codec |
d5191e50 TI |
1754 | * @codec: HD-audio codec |
1755 | * @nid: corresponding NID (optional) | |
1756 | * @kctl: the control element to assign | |
1757 | * | |
1758 | * Add the given control element to an array inside the codec instance. | |
1759 | * All control elements belonging to a codec are supposed to be added | |
1760 | * by this function so that a proper clean-up works at the free or | |
1761 | * reconfiguration time. | |
1762 | * | |
1763 | * If non-zero @nid is passed, the NID is assigned to the control element. | |
1764 | * The assignment is shown in the codec proc file. | |
1765 | * | |
1766 | * snd_hda_ctl_add() checks the control subdev id field whether | |
1767 | * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower | |
9e3fd871 JK |
1768 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
1769 | * specifies if kctl->private_value is a HDA amplifier value. | |
d5191e50 | 1770 | */ |
3911a4c1 JK |
1771 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
1772 | struct snd_kcontrol *kctl) | |
d13bd412 TI |
1773 | { |
1774 | int err; | |
9e3fd871 | 1775 | unsigned short flags = 0; |
3911a4c1 | 1776 | struct hda_nid_item *item; |
d13bd412 | 1777 | |
5e26dfd0 | 1778 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
9e3fd871 | 1779 | flags |= HDA_NID_ITEM_AMP; |
5e26dfd0 JK |
1780 | if (nid == 0) |
1781 | nid = get_amp_nid_(kctl->private_value); | |
1782 | } | |
9e3fd871 JK |
1783 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
1784 | nid = kctl->id.subdevice & 0xffff; | |
5e26dfd0 | 1785 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
4d02d1b6 | 1786 | kctl->id.subdevice = 0; |
d13bd412 TI |
1787 | err = snd_ctl_add(codec->bus->card, kctl); |
1788 | if (err < 0) | |
1789 | return err; | |
3911a4c1 JK |
1790 | item = snd_array_new(&codec->mixers); |
1791 | if (!item) | |
d13bd412 | 1792 | return -ENOMEM; |
3911a4c1 JK |
1793 | item->kctl = kctl; |
1794 | item->nid = nid; | |
9e3fd871 | 1795 | item->flags = flags; |
d13bd412 TI |
1796 | return 0; |
1797 | } | |
ff7a3267 | 1798 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
d13bd412 | 1799 | |
5b0cb1d8 JK |
1800 | /** |
1801 | * snd_hda_add_nid - Assign a NID to a control element | |
1802 | * @codec: HD-audio codec | |
1803 | * @nid: corresponding NID (optional) | |
1804 | * @kctl: the control element to assign | |
1805 | * @index: index to kctl | |
1806 | * | |
1807 | * Add the given control element to an array inside the codec instance. | |
1808 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 | |
1809 | * NID:KCTL mapping - for example "Capture Source" selector. | |
1810 | */ | |
1811 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, | |
1812 | unsigned int index, hda_nid_t nid) | |
1813 | { | |
1814 | struct hda_nid_item *item; | |
1815 | ||
1816 | if (nid > 0) { | |
1817 | item = snd_array_new(&codec->nids); | |
1818 | if (!item) | |
1819 | return -ENOMEM; | |
1820 | item->kctl = kctl; | |
1821 | item->index = index; | |
1822 | item->nid = nid; | |
1823 | return 0; | |
1824 | } | |
28d1a85e TI |
1825 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
1826 | kctl->id.name, kctl->id.index, index); | |
5b0cb1d8 JK |
1827 | return -EINVAL; |
1828 | } | |
1829 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); | |
1830 | ||
d5191e50 TI |
1831 | /** |
1832 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec | |
1833 | * @codec: HD-audio codec | |
1834 | */ | |
d13bd412 TI |
1835 | void snd_hda_ctls_clear(struct hda_codec *codec) |
1836 | { | |
1837 | int i; | |
3911a4c1 | 1838 | struct hda_nid_item *items = codec->mixers.list; |
d13bd412 | 1839 | for (i = 0; i < codec->mixers.used; i++) |
3911a4c1 | 1840 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
d13bd412 | 1841 | snd_array_free(&codec->mixers); |
5b0cb1d8 | 1842 | snd_array_free(&codec->nids); |
d13bd412 TI |
1843 | } |
1844 | ||
a65d629c TI |
1845 | /* pseudo device locking |
1846 | * toggle card->shutdown to allow/disallow the device access (as a hack) | |
1847 | */ | |
1848 | static int hda_lock_devices(struct snd_card *card) | |
6c1f45ea | 1849 | { |
a65d629c TI |
1850 | spin_lock(&card->files_lock); |
1851 | if (card->shutdown) { | |
1852 | spin_unlock(&card->files_lock); | |
1853 | return -EINVAL; | |
1854 | } | |
1855 | card->shutdown = 1; | |
1856 | spin_unlock(&card->files_lock); | |
1857 | return 0; | |
1858 | } | |
1859 | ||
1860 | static void hda_unlock_devices(struct snd_card *card) | |
1861 | { | |
1862 | spin_lock(&card->files_lock); | |
1863 | card->shutdown = 0; | |
1864 | spin_unlock(&card->files_lock); | |
1865 | } | |
1866 | ||
d5191e50 TI |
1867 | /** |
1868 | * snd_hda_codec_reset - Clear all objects assigned to the codec | |
1869 | * @codec: HD-audio codec | |
1870 | * | |
1871 | * This frees the all PCM and control elements assigned to the codec, and | |
1872 | * clears the caches and restores the pin default configurations. | |
1873 | * | |
1874 | * When a device is being used, it returns -EBSY. If successfully freed, | |
1875 | * returns zero. | |
1876 | */ | |
a65d629c TI |
1877 | int snd_hda_codec_reset(struct hda_codec *codec) |
1878 | { | |
1879 | struct snd_card *card = codec->bus->card; | |
1880 | int i, pcm; | |
1881 | ||
1882 | if (hda_lock_devices(card) < 0) | |
1883 | return -EBUSY; | |
1884 | /* check whether the codec isn't used by any mixer or PCM streams */ | |
1885 | if (!list_empty(&card->ctl_files)) { | |
1886 | hda_unlock_devices(card); | |
1887 | return -EBUSY; | |
1888 | } | |
1889 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { | |
1890 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; | |
1891 | if (!cpcm->pcm) | |
1892 | continue; | |
1893 | if (cpcm->pcm->streams[0].substream_opened || | |
1894 | cpcm->pcm->streams[1].substream_opened) { | |
1895 | hda_unlock_devices(card); | |
1896 | return -EBUSY; | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | /* OK, let it free */ | |
6c1f45ea TI |
1901 | |
1902 | #ifdef CONFIG_SND_HDA_POWER_SAVE | |
1903 | cancel_delayed_work(&codec->power_work); | |
6acaed38 | 1904 | flush_workqueue(codec->bus->workq); |
6c1f45ea TI |
1905 | #endif |
1906 | snd_hda_ctls_clear(codec); | |
1907 | /* relase PCMs */ | |
1908 | for (i = 0; i < codec->num_pcms; i++) { | |
529bd6c4 | 1909 | if (codec->pcm_info[i].pcm) { |
a65d629c | 1910 | snd_device_free(card, codec->pcm_info[i].pcm); |
529bd6c4 TI |
1911 | clear_bit(codec->pcm_info[i].device, |
1912 | codec->bus->pcm_dev_bits); | |
1913 | } | |
6c1f45ea TI |
1914 | } |
1915 | if (codec->patch_ops.free) | |
1916 | codec->patch_ops.free(codec); | |
56d17712 | 1917 | codec->proc_widget_hook = NULL; |
6c1f45ea TI |
1918 | codec->spec = NULL; |
1919 | free_hda_cache(&codec->amp_cache); | |
1920 | free_hda_cache(&codec->cmd_cache); | |
827057f5 TI |
1921 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
1922 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); | |
346ff70f TI |
1923 | /* free only driver_pins so that init_pins + user_pins are restored */ |
1924 | snd_array_free(&codec->driver_pins); | |
3be14149 | 1925 | restore_pincfgs(codec); |
6c1f45ea TI |
1926 | codec->num_pcms = 0; |
1927 | codec->pcm_info = NULL; | |
1928 | codec->preset = NULL; | |
d1f1af2d TI |
1929 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
1930 | codec->slave_dig_outs = NULL; | |
1931 | codec->spdif_status_reset = 0; | |
1289e9e8 TI |
1932 | module_put(codec->owner); |
1933 | codec->owner = NULL; | |
a65d629c TI |
1934 | |
1935 | /* allow device access again */ | |
1936 | hda_unlock_devices(card); | |
1937 | return 0; | |
6c1f45ea TI |
1938 | } |
1939 | ||
d5191e50 TI |
1940 | /** |
1941 | * snd_hda_add_vmaster - create a virtual master control and add slaves | |
1942 | * @codec: HD-audio codec | |
1943 | * @name: vmaster control name | |
1944 | * @tlv: TLV data (optional) | |
1945 | * @slaves: slave control names (optional) | |
1946 | * | |
1947 | * Create a virtual master control with the given name. The TLV data | |
1948 | * must be either NULL or a valid data. | |
1949 | * | |
1950 | * @slaves is a NULL-terminated array of strings, each of which is a | |
1951 | * slave control name. All controls with these names are assigned to | |
1952 | * the new virtual master control. | |
1953 | * | |
1954 | * This function returns zero if successful or a negative error code. | |
1955 | */ | |
2134ea4f TI |
1956 | int snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
1957 | unsigned int *tlv, const char **slaves) | |
1958 | { | |
1959 | struct snd_kcontrol *kctl; | |
1960 | const char **s; | |
1961 | int err; | |
1962 | ||
2f085549 TI |
1963 | for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++) |
1964 | ; | |
1965 | if (!*s) { | |
1966 | snd_printdd("No slave found for %s\n", name); | |
1967 | return 0; | |
1968 | } | |
2134ea4f TI |
1969 | kctl = snd_ctl_make_virtual_master(name, tlv); |
1970 | if (!kctl) | |
1971 | return -ENOMEM; | |
3911a4c1 | 1972 | err = snd_hda_ctl_add(codec, 0, kctl); |
2134ea4f TI |
1973 | if (err < 0) |
1974 | return err; | |
28aedaf7 | 1975 | |
2134ea4f TI |
1976 | for (s = slaves; *s; s++) { |
1977 | struct snd_kcontrol *sctl; | |
7a411ee0 TI |
1978 | int i = 0; |
1979 | for (;;) { | |
1980 | sctl = _snd_hda_find_mixer_ctl(codec, *s, i); | |
1981 | if (!sctl) { | |
1982 | if (!i) | |
1983 | snd_printdd("Cannot find slave %s, " | |
1984 | "skipped\n", *s); | |
1985 | break; | |
1986 | } | |
1987 | err = snd_ctl_add_slave(kctl, sctl); | |
1988 | if (err < 0) | |
1989 | return err; | |
1990 | i++; | |
2134ea4f | 1991 | } |
2134ea4f TI |
1992 | } |
1993 | return 0; | |
1994 | } | |
ff7a3267 | 1995 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster); |
2134ea4f | 1996 | |
d5191e50 TI |
1997 | /** |
1998 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch | |
1999 | * | |
2000 | * The control element is supposed to have the private_value field | |
2001 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2002 | */ | |
0ba21762 TI |
2003 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
2004 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
2005 | { |
2006 | int chs = get_amp_channels(kcontrol); | |
2007 | ||
2008 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
2009 | uinfo->count = chs == 3 ? 2 : 1; | |
2010 | uinfo->value.integer.min = 0; | |
2011 | uinfo->value.integer.max = 1; | |
2012 | return 0; | |
2013 | } | |
ff7a3267 | 2014 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
1da177e4 | 2015 | |
d5191e50 TI |
2016 | /** |
2017 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch | |
2018 | * | |
2019 | * The control element is supposed to have the private_value field | |
2020 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2021 | */ | |
0ba21762 TI |
2022 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
2023 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2024 | { |
2025 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2026 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2027 | int chs = get_amp_channels(kcontrol); | |
2028 | int dir = get_amp_direction(kcontrol); | |
2029 | int idx = get_amp_index(kcontrol); | |
2030 | long *valp = ucontrol->value.integer.value; | |
2031 | ||
2032 | if (chs & 1) | |
0ba21762 | 2033 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
47fd830a | 2034 | HDA_AMP_MUTE) ? 0 : 1; |
1da177e4 | 2035 | if (chs & 2) |
0ba21762 | 2036 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
47fd830a | 2037 | HDA_AMP_MUTE) ? 0 : 1; |
1da177e4 LT |
2038 | return 0; |
2039 | } | |
ff7a3267 | 2040 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
1da177e4 | 2041 | |
d5191e50 TI |
2042 | /** |
2043 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch | |
2044 | * | |
2045 | * The control element is supposed to have the private_value field | |
2046 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2047 | */ | |
0ba21762 TI |
2048 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
2049 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2050 | { |
2051 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2052 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2053 | int chs = get_amp_channels(kcontrol); | |
2054 | int dir = get_amp_direction(kcontrol); | |
2055 | int idx = get_amp_index(kcontrol); | |
1da177e4 LT |
2056 | long *valp = ucontrol->value.integer.value; |
2057 | int change = 0; | |
2058 | ||
cb53c626 | 2059 | snd_hda_power_up(codec); |
b9f5a89c | 2060 | if (chs & 1) { |
4a19faee | 2061 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
47fd830a TI |
2062 | HDA_AMP_MUTE, |
2063 | *valp ? 0 : HDA_AMP_MUTE); | |
b9f5a89c NG |
2064 | valp++; |
2065 | } | |
4a19faee TI |
2066 | if (chs & 2) |
2067 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, | |
47fd830a TI |
2068 | HDA_AMP_MUTE, |
2069 | *valp ? 0 : HDA_AMP_MUTE); | |
cb53c626 TI |
2070 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
2071 | if (codec->patch_ops.check_power_status) | |
2072 | codec->patch_ops.check_power_status(codec, nid); | |
2073 | #endif | |
2074 | snd_hda_power_down(codec); | |
1da177e4 LT |
2075 | return change; |
2076 | } | |
ff7a3267 | 2077 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
1da177e4 | 2078 | |
67d634c0 | 2079 | #ifdef CONFIG_SND_HDA_INPUT_BEEP |
d5191e50 TI |
2080 | /** |
2081 | * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch | |
2082 | * | |
2083 | * This function calls snd_hda_enable_beep_device(), which behaves differently | |
2084 | * depending on beep_mode option. | |
2085 | */ | |
123c07ae JK |
2086 | int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol, |
2087 | struct snd_ctl_elem_value *ucontrol) | |
2088 | { | |
2089 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2090 | long *valp = ucontrol->value.integer.value; | |
2091 | ||
2092 | snd_hda_enable_beep_device(codec, *valp); | |
2093 | return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); | |
2094 | } | |
2095 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep); | |
67d634c0 | 2096 | #endif /* CONFIG_SND_HDA_INPUT_BEEP */ |
123c07ae | 2097 | |
985be54b TI |
2098 | /* |
2099 | * bound volume controls | |
2100 | * | |
2101 | * bind multiple volumes (# indices, from 0) | |
2102 | */ | |
2103 | ||
2104 | #define AMP_VAL_IDX_SHIFT 19 | |
2105 | #define AMP_VAL_IDX_MASK (0x0f<<19) | |
2106 | ||
d5191e50 TI |
2107 | /** |
2108 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control | |
2109 | * | |
2110 | * The control element is supposed to have the private_value field | |
2111 | * set up via HDA_BIND_MUTE*() macros. | |
2112 | */ | |
0ba21762 TI |
2113 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
2114 | struct snd_ctl_elem_value *ucontrol) | |
985be54b TI |
2115 | { |
2116 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2117 | unsigned long pval; | |
2118 | int err; | |
2119 | ||
5a9e02e9 | 2120 | mutex_lock(&codec->control_mutex); |
985be54b TI |
2121 | pval = kcontrol->private_value; |
2122 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ | |
2123 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); | |
2124 | kcontrol->private_value = pval; | |
5a9e02e9 | 2125 | mutex_unlock(&codec->control_mutex); |
985be54b TI |
2126 | return err; |
2127 | } | |
ff7a3267 | 2128 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
985be54b | 2129 | |
d5191e50 TI |
2130 | /** |
2131 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control | |
2132 | * | |
2133 | * The control element is supposed to have the private_value field | |
2134 | * set up via HDA_BIND_MUTE*() macros. | |
2135 | */ | |
0ba21762 TI |
2136 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
2137 | struct snd_ctl_elem_value *ucontrol) | |
985be54b TI |
2138 | { |
2139 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2140 | unsigned long pval; | |
2141 | int i, indices, err = 0, change = 0; | |
2142 | ||
5a9e02e9 | 2143 | mutex_lock(&codec->control_mutex); |
985be54b TI |
2144 | pval = kcontrol->private_value; |
2145 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; | |
2146 | for (i = 0; i < indices; i++) { | |
0ba21762 TI |
2147 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
2148 | (i << AMP_VAL_IDX_SHIFT); | |
985be54b TI |
2149 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
2150 | if (err < 0) | |
2151 | break; | |
2152 | change |= err; | |
2153 | } | |
2154 | kcontrol->private_value = pval; | |
5a9e02e9 | 2155 | mutex_unlock(&codec->control_mutex); |
985be54b TI |
2156 | return err < 0 ? err : change; |
2157 | } | |
ff7a3267 | 2158 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
985be54b | 2159 | |
d5191e50 TI |
2160 | /** |
2161 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control | |
2162 | * | |
2163 | * The control element is supposed to have the private_value field | |
2164 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | |
532d5381 TI |
2165 | */ |
2166 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, | |
2167 | struct snd_ctl_elem_info *uinfo) | |
2168 | { | |
2169 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2170 | struct hda_bind_ctls *c; | |
2171 | int err; | |
2172 | ||
5a9e02e9 | 2173 | mutex_lock(&codec->control_mutex); |
14c65f98 | 2174 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
532d5381 TI |
2175 | kcontrol->private_value = *c->values; |
2176 | err = c->ops->info(kcontrol, uinfo); | |
2177 | kcontrol->private_value = (long)c; | |
5a9e02e9 | 2178 | mutex_unlock(&codec->control_mutex); |
532d5381 TI |
2179 | return err; |
2180 | } | |
ff7a3267 | 2181 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
532d5381 | 2182 | |
d5191e50 TI |
2183 | /** |
2184 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control | |
2185 | * | |
2186 | * The control element is supposed to have the private_value field | |
2187 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | |
2188 | */ | |
532d5381 TI |
2189 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
2190 | struct snd_ctl_elem_value *ucontrol) | |
2191 | { | |
2192 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2193 | struct hda_bind_ctls *c; | |
2194 | int err; | |
2195 | ||
5a9e02e9 | 2196 | mutex_lock(&codec->control_mutex); |
14c65f98 | 2197 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
532d5381 TI |
2198 | kcontrol->private_value = *c->values; |
2199 | err = c->ops->get(kcontrol, ucontrol); | |
2200 | kcontrol->private_value = (long)c; | |
5a9e02e9 | 2201 | mutex_unlock(&codec->control_mutex); |
532d5381 TI |
2202 | return err; |
2203 | } | |
ff7a3267 | 2204 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
532d5381 | 2205 | |
d5191e50 TI |
2206 | /** |
2207 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control | |
2208 | * | |
2209 | * The control element is supposed to have the private_value field | |
2210 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | |
2211 | */ | |
532d5381 TI |
2212 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
2213 | struct snd_ctl_elem_value *ucontrol) | |
2214 | { | |
2215 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2216 | struct hda_bind_ctls *c; | |
2217 | unsigned long *vals; | |
2218 | int err = 0, change = 0; | |
2219 | ||
5a9e02e9 | 2220 | mutex_lock(&codec->control_mutex); |
14c65f98 | 2221 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
532d5381 TI |
2222 | for (vals = c->values; *vals; vals++) { |
2223 | kcontrol->private_value = *vals; | |
2224 | err = c->ops->put(kcontrol, ucontrol); | |
2225 | if (err < 0) | |
2226 | break; | |
2227 | change |= err; | |
2228 | } | |
2229 | kcontrol->private_value = (long)c; | |
5a9e02e9 | 2230 | mutex_unlock(&codec->control_mutex); |
532d5381 TI |
2231 | return err < 0 ? err : change; |
2232 | } | |
ff7a3267 | 2233 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
532d5381 | 2234 | |
d5191e50 TI |
2235 | /** |
2236 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control | |
2237 | * | |
2238 | * The control element is supposed to have the private_value field | |
2239 | * set up via HDA_BIND_VOL() macro. | |
2240 | */ | |
532d5381 TI |
2241 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
2242 | unsigned int size, unsigned int __user *tlv) | |
2243 | { | |
2244 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2245 | struct hda_bind_ctls *c; | |
2246 | int err; | |
2247 | ||
5a9e02e9 | 2248 | mutex_lock(&codec->control_mutex); |
14c65f98 | 2249 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
532d5381 TI |
2250 | kcontrol->private_value = *c->values; |
2251 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); | |
2252 | kcontrol->private_value = (long)c; | |
5a9e02e9 | 2253 | mutex_unlock(&codec->control_mutex); |
532d5381 TI |
2254 | return err; |
2255 | } | |
ff7a3267 | 2256 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
532d5381 TI |
2257 | |
2258 | struct hda_ctl_ops snd_hda_bind_vol = { | |
2259 | .info = snd_hda_mixer_amp_volume_info, | |
2260 | .get = snd_hda_mixer_amp_volume_get, | |
2261 | .put = snd_hda_mixer_amp_volume_put, | |
2262 | .tlv = snd_hda_mixer_amp_tlv | |
2263 | }; | |
ff7a3267 | 2264 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
532d5381 TI |
2265 | |
2266 | struct hda_ctl_ops snd_hda_bind_sw = { | |
2267 | .info = snd_hda_mixer_amp_switch_info, | |
2268 | .get = snd_hda_mixer_amp_switch_get, | |
2269 | .put = snd_hda_mixer_amp_switch_put, | |
2270 | .tlv = snd_hda_mixer_amp_tlv | |
2271 | }; | |
ff7a3267 | 2272 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
532d5381 | 2273 | |
1da177e4 LT |
2274 | /* |
2275 | * SPDIF out controls | |
2276 | */ | |
2277 | ||
0ba21762 TI |
2278 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
2279 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
2280 | { |
2281 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
2282 | uinfo->count = 1; | |
2283 | return 0; | |
2284 | } | |
2285 | ||
0ba21762 TI |
2286 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
2287 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2288 | { |
2289 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
2290 | IEC958_AES0_NONAUDIO | | |
2291 | IEC958_AES0_CON_EMPHASIS_5015 | | |
2292 | IEC958_AES0_CON_NOT_COPYRIGHT; | |
2293 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | | |
2294 | IEC958_AES1_CON_ORIGINAL; | |
2295 | return 0; | |
2296 | } | |
2297 | ||
0ba21762 TI |
2298 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
2299 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2300 | { |
2301 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
2302 | IEC958_AES0_NONAUDIO | | |
2303 | IEC958_AES0_PRO_EMPHASIS_5015; | |
2304 | return 0; | |
2305 | } | |
2306 | ||
0ba21762 TI |
2307 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
2308 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2309 | { |
2310 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2311 | ||
2312 | ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff; | |
2313 | ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff; | |
2314 | ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff; | |
2315 | ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff; | |
2316 | ||
2317 | return 0; | |
2318 | } | |
2319 | ||
2320 | /* convert from SPDIF status bits to HDA SPDIF bits | |
2321 | * bit 0 (DigEn) is always set zero (to be filled later) | |
2322 | */ | |
2323 | static unsigned short convert_from_spdif_status(unsigned int sbits) | |
2324 | { | |
2325 | unsigned short val = 0; | |
2326 | ||
2327 | if (sbits & IEC958_AES0_PROFESSIONAL) | |
0ba21762 | 2328 | val |= AC_DIG1_PROFESSIONAL; |
1da177e4 | 2329 | if (sbits & IEC958_AES0_NONAUDIO) |
0ba21762 | 2330 | val |= AC_DIG1_NONAUDIO; |
1da177e4 | 2331 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
0ba21762 TI |
2332 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
2333 | IEC958_AES0_PRO_EMPHASIS_5015) | |
2334 | val |= AC_DIG1_EMPHASIS; | |
1da177e4 | 2335 | } else { |
0ba21762 TI |
2336 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
2337 | IEC958_AES0_CON_EMPHASIS_5015) | |
2338 | val |= AC_DIG1_EMPHASIS; | |
2339 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) | |
2340 | val |= AC_DIG1_COPYRIGHT; | |
1da177e4 | 2341 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
0ba21762 | 2342 | val |= AC_DIG1_LEVEL; |
1da177e4 LT |
2343 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
2344 | } | |
2345 | return val; | |
2346 | } | |
2347 | ||
2348 | /* convert to SPDIF status bits from HDA SPDIF bits | |
2349 | */ | |
2350 | static unsigned int convert_to_spdif_status(unsigned short val) | |
2351 | { | |
2352 | unsigned int sbits = 0; | |
2353 | ||
0ba21762 | 2354 | if (val & AC_DIG1_NONAUDIO) |
1da177e4 | 2355 | sbits |= IEC958_AES0_NONAUDIO; |
0ba21762 | 2356 | if (val & AC_DIG1_PROFESSIONAL) |
1da177e4 LT |
2357 | sbits |= IEC958_AES0_PROFESSIONAL; |
2358 | if (sbits & IEC958_AES0_PROFESSIONAL) { | |
0ba21762 | 2359 | if (sbits & AC_DIG1_EMPHASIS) |
1da177e4 LT |
2360 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
2361 | } else { | |
0ba21762 | 2362 | if (val & AC_DIG1_EMPHASIS) |
1da177e4 | 2363 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
0ba21762 | 2364 | if (!(val & AC_DIG1_COPYRIGHT)) |
1da177e4 | 2365 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
0ba21762 | 2366 | if (val & AC_DIG1_LEVEL) |
1da177e4 LT |
2367 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
2368 | sbits |= val & (0x7f << 8); | |
2369 | } | |
2370 | return sbits; | |
2371 | } | |
2372 | ||
2f72853c TI |
2373 | /* set digital convert verbs both for the given NID and its slaves */ |
2374 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, | |
2375 | int verb, int val) | |
2376 | { | |
2377 | hda_nid_t *d; | |
2378 | ||
9e976976 | 2379 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
2f72853c TI |
2380 | d = codec->slave_dig_outs; |
2381 | if (!d) | |
2382 | return; | |
2383 | for (; *d; d++) | |
9e976976 | 2384 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
2f72853c TI |
2385 | } |
2386 | ||
2387 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, | |
2388 | int dig1, int dig2) | |
2389 | { | |
2390 | if (dig1 != -1) | |
2391 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); | |
2392 | if (dig2 != -1) | |
2393 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); | |
2394 | } | |
2395 | ||
0ba21762 TI |
2396 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
2397 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2398 | { |
2399 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2400 | hda_nid_t nid = kcontrol->private_value; | |
2401 | unsigned short val; | |
2402 | int change; | |
2403 | ||
62932df8 | 2404 | mutex_lock(&codec->spdif_mutex); |
1da177e4 LT |
2405 | codec->spdif_status = ucontrol->value.iec958.status[0] | |
2406 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | | |
2407 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | | |
2408 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); | |
2409 | val = convert_from_spdif_status(codec->spdif_status); | |
2410 | val |= codec->spdif_ctls & 1; | |
2411 | change = codec->spdif_ctls != val; | |
2412 | codec->spdif_ctls = val; | |
2413 | ||
2f72853c TI |
2414 | if (change) |
2415 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); | |
1da177e4 | 2416 | |
62932df8 | 2417 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2418 | return change; |
2419 | } | |
2420 | ||
a5ce8890 | 2421 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
1da177e4 | 2422 | |
0ba21762 TI |
2423 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
2424 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2425 | { |
2426 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2427 | ||
0ba21762 | 2428 | ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE; |
1da177e4 LT |
2429 | return 0; |
2430 | } | |
2431 | ||
0ba21762 TI |
2432 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
2433 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2434 | { |
2435 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2436 | hda_nid_t nid = kcontrol->private_value; | |
2437 | unsigned short val; | |
2438 | int change; | |
2439 | ||
62932df8 | 2440 | mutex_lock(&codec->spdif_mutex); |
0ba21762 | 2441 | val = codec->spdif_ctls & ~AC_DIG1_ENABLE; |
1da177e4 | 2442 | if (ucontrol->value.integer.value[0]) |
0ba21762 | 2443 | val |= AC_DIG1_ENABLE; |
1da177e4 | 2444 | change = codec->spdif_ctls != val; |
82beb8fd | 2445 | if (change) { |
1da177e4 | 2446 | codec->spdif_ctls = val; |
2f72853c | 2447 | set_dig_out_convert(codec, nid, val & 0xff, -1); |
0ba21762 TI |
2448 | /* unmute amp switch (if any) */ |
2449 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && | |
47fd830a TI |
2450 | (val & AC_DIG1_ENABLE)) |
2451 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, | |
2452 | HDA_AMP_MUTE, 0); | |
1da177e4 | 2453 | } |
62932df8 | 2454 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2455 | return change; |
2456 | } | |
2457 | ||
c8b6bf9b | 2458 | static struct snd_kcontrol_new dig_mixes[] = { |
1da177e4 LT |
2459 | { |
2460 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2461 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2462 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
1da177e4 LT |
2463 | .info = snd_hda_spdif_mask_info, |
2464 | .get = snd_hda_spdif_cmask_get, | |
2465 | }, | |
2466 | { | |
2467 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2468 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2469 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
1da177e4 LT |
2470 | .info = snd_hda_spdif_mask_info, |
2471 | .get = snd_hda_spdif_pmask_get, | |
2472 | }, | |
2473 | { | |
2474 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2475 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
1da177e4 LT |
2476 | .info = snd_hda_spdif_mask_info, |
2477 | .get = snd_hda_spdif_default_get, | |
2478 | .put = snd_hda_spdif_default_put, | |
2479 | }, | |
2480 | { | |
2481 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2482 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
1da177e4 LT |
2483 | .info = snd_hda_spdif_out_switch_info, |
2484 | .get = snd_hda_spdif_out_switch_get, | |
2485 | .put = snd_hda_spdif_out_switch_put, | |
2486 | }, | |
2487 | { } /* end */ | |
2488 | }; | |
2489 | ||
09f99701 TI |
2490 | #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */ |
2491 | ||
1da177e4 LT |
2492 | /** |
2493 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls | |
2494 | * @codec: the HDA codec | |
2495 | * @nid: audio out widget NID | |
2496 | * | |
2497 | * Creates controls related with the SPDIF output. | |
2498 | * Called from each patch supporting the SPDIF out. | |
2499 | * | |
2500 | * Returns 0 if successful, or a negative error code. | |
2501 | */ | |
12f288bf | 2502 | int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid) |
1da177e4 LT |
2503 | { |
2504 | int err; | |
c8b6bf9b TI |
2505 | struct snd_kcontrol *kctl; |
2506 | struct snd_kcontrol_new *dig_mix; | |
09f99701 | 2507 | int idx; |
1da177e4 | 2508 | |
09f99701 TI |
2509 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
2510 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch", | |
2511 | idx)) | |
2512 | break; | |
2513 | } | |
2514 | if (idx >= SPDIF_MAX_IDX) { | |
2515 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); | |
2516 | return -EBUSY; | |
2517 | } | |
1da177e4 LT |
2518 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
2519 | kctl = snd_ctl_new1(dig_mix, codec); | |
b91f080f TI |
2520 | if (!kctl) |
2521 | return -ENOMEM; | |
09f99701 | 2522 | kctl->id.index = idx; |
1da177e4 | 2523 | kctl->private_value = nid; |
3911a4c1 | 2524 | err = snd_hda_ctl_add(codec, nid, kctl); |
0ba21762 | 2525 | if (err < 0) |
1da177e4 LT |
2526 | return err; |
2527 | } | |
0ba21762 | 2528 | codec->spdif_ctls = |
3982d17e AP |
2529 | snd_hda_codec_read(codec, nid, 0, |
2530 | AC_VERB_GET_DIGI_CONVERT_1, 0); | |
1da177e4 LT |
2531 | codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls); |
2532 | return 0; | |
2533 | } | |
ff7a3267 | 2534 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls); |
1da177e4 | 2535 | |
9a08160b TI |
2536 | /* |
2537 | * SPDIF sharing with analog output | |
2538 | */ | |
2539 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, | |
2540 | struct snd_ctl_elem_value *ucontrol) | |
2541 | { | |
2542 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | |
2543 | ucontrol->value.integer.value[0] = mout->share_spdif; | |
2544 | return 0; | |
2545 | } | |
2546 | ||
2547 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, | |
2548 | struct snd_ctl_elem_value *ucontrol) | |
2549 | { | |
2550 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | |
2551 | mout->share_spdif = !!ucontrol->value.integer.value[0]; | |
2552 | return 0; | |
2553 | } | |
2554 | ||
2555 | static struct snd_kcontrol_new spdif_share_sw = { | |
2556 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
2557 | .name = "IEC958 Default PCM Playback Switch", | |
2558 | .info = snd_ctl_boolean_mono_info, | |
2559 | .get = spdif_share_sw_get, | |
2560 | .put = spdif_share_sw_put, | |
2561 | }; | |
2562 | ||
d5191e50 TI |
2563 | /** |
2564 | * snd_hda_create_spdif_share_sw - create Default PCM switch | |
2565 | * @codec: the HDA codec | |
2566 | * @mout: multi-out instance | |
2567 | */ | |
9a08160b TI |
2568 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
2569 | struct hda_multi_out *mout) | |
2570 | { | |
2571 | if (!mout->dig_out_nid) | |
2572 | return 0; | |
2573 | /* ATTENTION: here mout is passed as private_data, instead of codec */ | |
3911a4c1 JK |
2574 | return snd_hda_ctl_add(codec, mout->dig_out_nid, |
2575 | snd_ctl_new1(&spdif_share_sw, mout)); | |
9a08160b | 2576 | } |
ff7a3267 | 2577 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
9a08160b | 2578 | |
1da177e4 LT |
2579 | /* |
2580 | * SPDIF input | |
2581 | */ | |
2582 | ||
2583 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info | |
2584 | ||
0ba21762 TI |
2585 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
2586 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2587 | { |
2588 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2589 | ||
2590 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; | |
2591 | return 0; | |
2592 | } | |
2593 | ||
0ba21762 TI |
2594 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
2595 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2596 | { |
2597 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2598 | hda_nid_t nid = kcontrol->private_value; | |
2599 | unsigned int val = !!ucontrol->value.integer.value[0]; | |
2600 | int change; | |
2601 | ||
62932df8 | 2602 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 2603 | change = codec->spdif_in_enable != val; |
82beb8fd | 2604 | if (change) { |
1da177e4 | 2605 | codec->spdif_in_enable = val; |
82beb8fd TI |
2606 | snd_hda_codec_write_cache(codec, nid, 0, |
2607 | AC_VERB_SET_DIGI_CONVERT_1, val); | |
1da177e4 | 2608 | } |
62932df8 | 2609 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2610 | return change; |
2611 | } | |
2612 | ||
0ba21762 TI |
2613 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
2614 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2615 | { |
2616 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2617 | hda_nid_t nid = kcontrol->private_value; | |
2618 | unsigned short val; | |
2619 | unsigned int sbits; | |
2620 | ||
3982d17e | 2621 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0); |
1da177e4 LT |
2622 | sbits = convert_to_spdif_status(val); |
2623 | ucontrol->value.iec958.status[0] = sbits; | |
2624 | ucontrol->value.iec958.status[1] = sbits >> 8; | |
2625 | ucontrol->value.iec958.status[2] = sbits >> 16; | |
2626 | ucontrol->value.iec958.status[3] = sbits >> 24; | |
2627 | return 0; | |
2628 | } | |
2629 | ||
c8b6bf9b | 2630 | static struct snd_kcontrol_new dig_in_ctls[] = { |
1da177e4 LT |
2631 | { |
2632 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2633 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
1da177e4 LT |
2634 | .info = snd_hda_spdif_in_switch_info, |
2635 | .get = snd_hda_spdif_in_switch_get, | |
2636 | .put = snd_hda_spdif_in_switch_put, | |
2637 | }, | |
2638 | { | |
2639 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2640 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2641 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
1da177e4 LT |
2642 | .info = snd_hda_spdif_mask_info, |
2643 | .get = snd_hda_spdif_in_status_get, | |
2644 | }, | |
2645 | { } /* end */ | |
2646 | }; | |
2647 | ||
2648 | /** | |
2649 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls | |
2650 | * @codec: the HDA codec | |
2651 | * @nid: audio in widget NID | |
2652 | * | |
2653 | * Creates controls related with the SPDIF input. | |
2654 | * Called from each patch supporting the SPDIF in. | |
2655 | * | |
2656 | * Returns 0 if successful, or a negative error code. | |
2657 | */ | |
12f288bf | 2658 | int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) |
1da177e4 LT |
2659 | { |
2660 | int err; | |
c8b6bf9b TI |
2661 | struct snd_kcontrol *kctl; |
2662 | struct snd_kcontrol_new *dig_mix; | |
09f99701 | 2663 | int idx; |
1da177e4 | 2664 | |
09f99701 TI |
2665 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
2666 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch", | |
2667 | idx)) | |
2668 | break; | |
2669 | } | |
2670 | if (idx >= SPDIF_MAX_IDX) { | |
2671 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); | |
2672 | return -EBUSY; | |
2673 | } | |
1da177e4 LT |
2674 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
2675 | kctl = snd_ctl_new1(dig_mix, codec); | |
c8dcdf82 TI |
2676 | if (!kctl) |
2677 | return -ENOMEM; | |
1da177e4 | 2678 | kctl->private_value = nid; |
3911a4c1 | 2679 | err = snd_hda_ctl_add(codec, nid, kctl); |
0ba21762 | 2680 | if (err < 0) |
1da177e4 LT |
2681 | return err; |
2682 | } | |
0ba21762 | 2683 | codec->spdif_in_enable = |
3982d17e AP |
2684 | snd_hda_codec_read(codec, nid, 0, |
2685 | AC_VERB_GET_DIGI_CONVERT_1, 0) & | |
0ba21762 | 2686 | AC_DIG1_ENABLE; |
1da177e4 LT |
2687 | return 0; |
2688 | } | |
ff7a3267 | 2689 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
1da177e4 | 2690 | |
cb53c626 | 2691 | #ifdef SND_HDA_NEEDS_RESUME |
82beb8fd TI |
2692 | /* |
2693 | * command cache | |
2694 | */ | |
1da177e4 | 2695 | |
b3ac5636 TI |
2696 | /* build a 32bit cache key with the widget id and the command parameter */ |
2697 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) | |
2698 | #define get_cmd_cache_nid(key) ((key) & 0xff) | |
2699 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) | |
2700 | ||
2701 | /** | |
2702 | * snd_hda_codec_write_cache - send a single command with caching | |
2703 | * @codec: the HDA codec | |
2704 | * @nid: NID to send the command | |
2705 | * @direct: direct flag | |
2706 | * @verb: the verb to send | |
2707 | * @parm: the parameter for the verb | |
2708 | * | |
2709 | * Send a single command without waiting for response. | |
2710 | * | |
2711 | * Returns 0 if successful, or a negative error code. | |
2712 | */ | |
2713 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, | |
2714 | int direct, unsigned int verb, unsigned int parm) | |
2715 | { | |
aa2936f5 TI |
2716 | int err = snd_hda_codec_write(codec, nid, direct, verb, parm); |
2717 | struct hda_cache_head *c; | |
2718 | u32 key; | |
33fa35ed | 2719 | |
aa2936f5 TI |
2720 | if (err < 0) |
2721 | return err; | |
2722 | /* parm may contain the verb stuff for get/set amp */ | |
2723 | verb = verb | (parm >> 8); | |
2724 | parm &= 0xff; | |
2725 | key = build_cmd_cache_key(nid, verb); | |
2726 | mutex_lock(&codec->bus->cmd_mutex); | |
2727 | c = get_alloc_hash(&codec->cmd_cache, key); | |
2728 | if (c) | |
2729 | c->val = parm; | |
2730 | mutex_unlock(&codec->bus->cmd_mutex); | |
2731 | return 0; | |
b3ac5636 | 2732 | } |
ff7a3267 | 2733 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
b3ac5636 | 2734 | |
a68d5a54 TI |
2735 | /** |
2736 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed | |
2737 | * @codec: the HDA codec | |
2738 | * @nid: NID to send the command | |
2739 | * @direct: direct flag | |
2740 | * @verb: the verb to send | |
2741 | * @parm: the parameter for the verb | |
2742 | * | |
2743 | * This function works like snd_hda_codec_write_cache(), but it doesn't send | |
2744 | * command if the parameter is already identical with the cached value. | |
2745 | * If not, it sends the command and refreshes the cache. | |
2746 | * | |
2747 | * Returns 0 if successful, or a negative error code. | |
2748 | */ | |
2749 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, | |
2750 | int direct, unsigned int verb, unsigned int parm) | |
2751 | { | |
2752 | struct hda_cache_head *c; | |
2753 | u32 key; | |
2754 | ||
2755 | /* parm may contain the verb stuff for get/set amp */ | |
2756 | verb = verb | (parm >> 8); | |
2757 | parm &= 0xff; | |
2758 | key = build_cmd_cache_key(nid, verb); | |
2759 | mutex_lock(&codec->bus->cmd_mutex); | |
2760 | c = get_hash(&codec->cmd_cache, key); | |
2761 | if (c && c->val == parm) { | |
2762 | mutex_unlock(&codec->bus->cmd_mutex); | |
2763 | return 0; | |
2764 | } | |
2765 | mutex_unlock(&codec->bus->cmd_mutex); | |
2766 | return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); | |
2767 | } | |
2768 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); | |
2769 | ||
d5191e50 TI |
2770 | /** |
2771 | * snd_hda_codec_resume_cache - Resume the all commands from the cache | |
2772 | * @codec: HD-audio codec | |
2773 | * | |
2774 | * Execute all verbs recorded in the command caches to resume. | |
2775 | */ | |
b3ac5636 TI |
2776 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
2777 | { | |
603c4019 | 2778 | struct hda_cache_head *buffer = codec->cmd_cache.buf.list; |
b3ac5636 TI |
2779 | int i; |
2780 | ||
603c4019 | 2781 | for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) { |
b3ac5636 TI |
2782 | u32 key = buffer->key; |
2783 | if (!key) | |
2784 | continue; | |
2785 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, | |
2786 | get_cmd_cache_cmd(key), buffer->val); | |
2787 | } | |
2788 | } | |
ff7a3267 | 2789 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
b3ac5636 TI |
2790 | |
2791 | /** | |
2792 | * snd_hda_sequence_write_cache - sequence writes with caching | |
2793 | * @codec: the HDA codec | |
2794 | * @seq: VERB array to send | |
2795 | * | |
2796 | * Send the commands sequentially from the given array. | |
2797 | * Thte commands are recorded on cache for power-save and resume. | |
2798 | * The array must be terminated with NID=0. | |
2799 | */ | |
2800 | void snd_hda_sequence_write_cache(struct hda_codec *codec, | |
2801 | const struct hda_verb *seq) | |
2802 | { | |
2803 | for (; seq->nid; seq++) | |
2804 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, | |
2805 | seq->param); | |
2806 | } | |
ff7a3267 | 2807 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
cb53c626 | 2808 | #endif /* SND_HDA_NEEDS_RESUME */ |
b3ac5636 | 2809 | |
54d17403 TI |
2810 | /* |
2811 | * set power state of the codec | |
2812 | */ | |
2813 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, | |
2814 | unsigned int power_state) | |
2815 | { | |
cb53c626 TI |
2816 | hda_nid_t nid; |
2817 | int i; | |
54d17403 | 2818 | |
05ff7e11 TI |
2819 | /* this delay seems necessary to avoid click noise at power-down */ |
2820 | if (power_state == AC_PWRST_D3) | |
2821 | msleep(100); | |
2822 | snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, | |
54d17403 | 2823 | power_state); |
05ff7e11 | 2824 | /* partial workaround for "azx_get_response timeout" */ |
dd2b4a7a ZR |
2825 | if (power_state == AC_PWRST_D0 && |
2826 | (codec->vendor_id & 0xffff0000) == 0x14f10000) | |
05ff7e11 | 2827 | msleep(10); |
54d17403 | 2828 | |
cb53c626 TI |
2829 | nid = codec->start_nid; |
2830 | for (i = 0; i < codec->num_nodes; i++, nid++) { | |
7eba5c9d TI |
2831 | unsigned int wcaps = get_wcaps(codec, nid); |
2832 | if (wcaps & AC_WCAP_POWER) { | |
a22d543a | 2833 | unsigned int wid_type = get_wcaps_type(wcaps); |
a3b48c88 TI |
2834 | if (power_state == AC_PWRST_D3 && |
2835 | wid_type == AC_WID_PIN) { | |
7eba5c9d TI |
2836 | unsigned int pincap; |
2837 | /* | |
2838 | * don't power down the widget if it controls | |
2839 | * eapd and EAPD_BTLENABLE is set. | |
2840 | */ | |
14bafe32 | 2841 | pincap = snd_hda_query_pin_caps(codec, nid); |
7eba5c9d TI |
2842 | if (pincap & AC_PINCAP_EAPD) { |
2843 | int eapd = snd_hda_codec_read(codec, | |
2844 | nid, 0, | |
2845 | AC_VERB_GET_EAPD_BTLENABLE, 0); | |
2846 | eapd &= 0x02; | |
a3b48c88 | 2847 | if (eapd) |
7eba5c9d TI |
2848 | continue; |
2849 | } | |
1194b5b7 | 2850 | } |
54d17403 TI |
2851 | snd_hda_codec_write(codec, nid, 0, |
2852 | AC_VERB_SET_POWER_STATE, | |
2853 | power_state); | |
1194b5b7 | 2854 | } |
54d17403 TI |
2855 | } |
2856 | ||
cb53c626 TI |
2857 | if (power_state == AC_PWRST_D0) { |
2858 | unsigned long end_time; | |
2859 | int state; | |
cb53c626 TI |
2860 | /* wait until the codec reachs to D0 */ |
2861 | end_time = jiffies + msecs_to_jiffies(500); | |
2862 | do { | |
2863 | state = snd_hda_codec_read(codec, fg, 0, | |
2864 | AC_VERB_GET_POWER_STATE, 0); | |
2865 | if (state == power_state) | |
2866 | break; | |
2867 | msleep(1); | |
2868 | } while (time_after_eq(end_time, jiffies)); | |
2869 | } | |
2870 | } | |
2871 | ||
11aeff08 TI |
2872 | #ifdef CONFIG_SND_HDA_HWDEP |
2873 | /* execute additional init verbs */ | |
2874 | static void hda_exec_init_verbs(struct hda_codec *codec) | |
2875 | { | |
2876 | if (codec->init_verbs.list) | |
2877 | snd_hda_sequence_write(codec, codec->init_verbs.list); | |
2878 | } | |
2879 | #else | |
2880 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} | |
2881 | #endif | |
2882 | ||
cb53c626 TI |
2883 | #ifdef SND_HDA_NEEDS_RESUME |
2884 | /* | |
2885 | * call suspend and power-down; used both from PM and power-save | |
2886 | */ | |
2887 | static void hda_call_codec_suspend(struct hda_codec *codec) | |
2888 | { | |
2889 | if (codec->patch_ops.suspend) | |
2890 | codec->patch_ops.suspend(codec, PMSG_SUSPEND); | |
2891 | hda_set_power_state(codec, | |
2892 | codec->afg ? codec->afg : codec->mfg, | |
2893 | AC_PWRST_D3); | |
2894 | #ifdef CONFIG_SND_HDA_POWER_SAVE | |
a2f6309e | 2895 | snd_hda_update_power_acct(codec); |
cb53c626 | 2896 | cancel_delayed_work(&codec->power_work); |
95e99fda | 2897 | codec->power_on = 0; |
a221e287 | 2898 | codec->power_transition = 0; |
a2f6309e | 2899 | codec->power_jiffies = jiffies; |
cb53c626 | 2900 | #endif |
54d17403 TI |
2901 | } |
2902 | ||
cb53c626 TI |
2903 | /* |
2904 | * kick up codec; used both from PM and power-save | |
2905 | */ | |
2906 | static void hda_call_codec_resume(struct hda_codec *codec) | |
2907 | { | |
2908 | hda_set_power_state(codec, | |
2909 | codec->afg ? codec->afg : codec->mfg, | |
2910 | AC_PWRST_D0); | |
3be14149 | 2911 | restore_pincfgs(codec); /* restore all current pin configs */ |
11aeff08 | 2912 | hda_exec_init_verbs(codec); |
cb53c626 TI |
2913 | if (codec->patch_ops.resume) |
2914 | codec->patch_ops.resume(codec); | |
2915 | else { | |
9d99f312 TI |
2916 | if (codec->patch_ops.init) |
2917 | codec->patch_ops.init(codec); | |
cb53c626 TI |
2918 | snd_hda_codec_resume_amp(codec); |
2919 | snd_hda_codec_resume_cache(codec); | |
2920 | } | |
2921 | } | |
2922 | #endif /* SND_HDA_NEEDS_RESUME */ | |
2923 | ||
54d17403 | 2924 | |
1da177e4 LT |
2925 | /** |
2926 | * snd_hda_build_controls - build mixer controls | |
2927 | * @bus: the BUS | |
2928 | * | |
2929 | * Creates mixer controls for each codec included in the bus. | |
2930 | * | |
2931 | * Returns 0 if successful, otherwise a negative error code. | |
2932 | */ | |
1289e9e8 | 2933 | int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus) |
1da177e4 | 2934 | { |
0ba21762 | 2935 | struct hda_codec *codec; |
1da177e4 | 2936 | |
0ba21762 | 2937 | list_for_each_entry(codec, &bus->codec_list, list) { |
6c1f45ea | 2938 | int err = snd_hda_codec_build_controls(codec); |
f93d461b | 2939 | if (err < 0) { |
28d1a85e | 2940 | printk(KERN_ERR "hda_codec: cannot build controls " |
28aedaf7 | 2941 | "for #%d (error %d)\n", codec->addr, err); |
f93d461b TI |
2942 | err = snd_hda_codec_reset(codec); |
2943 | if (err < 0) { | |
2944 | printk(KERN_ERR | |
2945 | "hda_codec: cannot revert codec\n"); | |
2946 | return err; | |
2947 | } | |
2948 | } | |
1da177e4 | 2949 | } |
6c1f45ea TI |
2950 | return 0; |
2951 | } | |
ff7a3267 | 2952 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
cb53c626 | 2953 | |
6c1f45ea TI |
2954 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
2955 | { | |
2956 | int err = 0; | |
11aeff08 | 2957 | hda_exec_init_verbs(codec); |
6c1f45ea TI |
2958 | /* continue to initialize... */ |
2959 | if (codec->patch_ops.init) | |
2960 | err = codec->patch_ops.init(codec); | |
2961 | if (!err && codec->patch_ops.build_controls) | |
2962 | err = codec->patch_ops.build_controls(codec); | |
6c1f45ea TI |
2963 | if (err < 0) |
2964 | return err; | |
1da177e4 LT |
2965 | return 0; |
2966 | } | |
2967 | ||
1da177e4 LT |
2968 | /* |
2969 | * stream formats | |
2970 | */ | |
befdf316 TI |
2971 | struct hda_rate_tbl { |
2972 | unsigned int hz; | |
2973 | unsigned int alsa_bits; | |
2974 | unsigned int hda_fmt; | |
2975 | }; | |
2976 | ||
2977 | static struct hda_rate_tbl rate_bits[] = { | |
1da177e4 | 2978 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
9d8f53f2 NG |
2979 | |
2980 | /* autodetected value used in snd_hda_query_supported_pcm */ | |
1da177e4 LT |
2981 | { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */ |
2982 | { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */ | |
2983 | { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */ | |
2984 | { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */ | |
2985 | { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */ | |
2986 | { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */ | |
2987 | { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */ | |
2988 | { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */ | |
2989 | { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */ | |
2990 | { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */ | |
2991 | { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */ | |
a961f9fe TI |
2992 | #define AC_PAR_PCM_RATE_BITS 11 |
2993 | /* up to bits 10, 384kHZ isn't supported properly */ | |
2994 | ||
2995 | /* not autodetected value */ | |
2996 | { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */ | |
9d8f53f2 | 2997 | |
befdf316 | 2998 | { 0 } /* terminator */ |
1da177e4 LT |
2999 | }; |
3000 | ||
3001 | /** | |
3002 | * snd_hda_calc_stream_format - calculate format bitset | |
3003 | * @rate: the sample rate | |
3004 | * @channels: the number of channels | |
3005 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) | |
3006 | * @maxbps: the max. bps | |
3007 | * | |
3008 | * Calculate the format bitset from the given rate, channels and th PCM format. | |
3009 | * | |
3010 | * Return zero if invalid. | |
3011 | */ | |
3012 | unsigned int snd_hda_calc_stream_format(unsigned int rate, | |
3013 | unsigned int channels, | |
3014 | unsigned int format, | |
3015 | unsigned int maxbps) | |
3016 | { | |
3017 | int i; | |
3018 | unsigned int val = 0; | |
3019 | ||
befdf316 TI |
3020 | for (i = 0; rate_bits[i].hz; i++) |
3021 | if (rate_bits[i].hz == rate) { | |
3022 | val = rate_bits[i].hda_fmt; | |
1da177e4 LT |
3023 | break; |
3024 | } | |
0ba21762 | 3025 | if (!rate_bits[i].hz) { |
1da177e4 LT |
3026 | snd_printdd("invalid rate %d\n", rate); |
3027 | return 0; | |
3028 | } | |
3029 | ||
3030 | if (channels == 0 || channels > 8) { | |
3031 | snd_printdd("invalid channels %d\n", channels); | |
3032 | return 0; | |
3033 | } | |
3034 | val |= channels - 1; | |
3035 | ||
3036 | switch (snd_pcm_format_width(format)) { | |
28aedaf7 NL |
3037 | case 8: |
3038 | val |= 0x00; | |
3039 | break; | |
3040 | case 16: | |
3041 | val |= 0x10; | |
3042 | break; | |
1da177e4 LT |
3043 | case 20: |
3044 | case 24: | |
3045 | case 32: | |
b0bb3aa6 | 3046 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
1da177e4 LT |
3047 | val |= 0x40; |
3048 | else if (maxbps >= 24) | |
3049 | val |= 0x30; | |
3050 | else | |
3051 | val |= 0x20; | |
3052 | break; | |
3053 | default: | |
0ba21762 TI |
3054 | snd_printdd("invalid format width %d\n", |
3055 | snd_pcm_format_width(format)); | |
1da177e4 LT |
3056 | return 0; |
3057 | } | |
3058 | ||
3059 | return val; | |
3060 | } | |
ff7a3267 | 3061 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
1da177e4 | 3062 | |
92c7c8a7 TI |
3063 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
3064 | { | |
3065 | unsigned int val = 0; | |
3066 | if (nid != codec->afg && | |
3067 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) | |
3068 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); | |
3069 | if (!val || val == -1) | |
3070 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); | |
3071 | if (!val || val == -1) | |
3072 | return 0; | |
3073 | return val; | |
3074 | } | |
3075 | ||
3076 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) | |
3077 | { | |
3078 | return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid), | |
3079 | get_pcm_param); | |
3080 | } | |
3081 | ||
3082 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid) | |
3083 | { | |
3084 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); | |
3085 | if (!streams || streams == -1) | |
3086 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); | |
3087 | if (!streams || streams == -1) | |
3088 | return 0; | |
3089 | return streams; | |
3090 | } | |
3091 | ||
3092 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) | |
3093 | { | |
3094 | return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid), | |
3095 | get_stream_param); | |
3096 | } | |
3097 | ||
1da177e4 LT |
3098 | /** |
3099 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats | |
3100 | * @codec: the HDA codec | |
3101 | * @nid: NID to query | |
3102 | * @ratesp: the pointer to store the detected rate bitflags | |
3103 | * @formatsp: the pointer to store the detected formats | |
3104 | * @bpsp: the pointer to store the detected format widths | |
3105 | * | |
3106 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp | |
3107 | * or @bsps argument is ignored. | |
3108 | * | |
3109 | * Returns 0 if successful, otherwise a negative error code. | |
3110 | */ | |
986862bd | 3111 | static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
1da177e4 LT |
3112 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
3113 | { | |
ee504710 | 3114 | unsigned int i, val, wcaps; |
1da177e4 | 3115 | |
ee504710 | 3116 | wcaps = get_wcaps(codec, nid); |
92c7c8a7 | 3117 | val = query_pcm_param(codec, nid); |
1da177e4 LT |
3118 | |
3119 | if (ratesp) { | |
3120 | u32 rates = 0; | |
a961f9fe | 3121 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
1da177e4 | 3122 | if (val & (1 << i)) |
befdf316 | 3123 | rates |= rate_bits[i].alsa_bits; |
1da177e4 | 3124 | } |
ee504710 JK |
3125 | if (rates == 0) { |
3126 | snd_printk(KERN_ERR "hda_codec: rates == 0 " | |
3127 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", | |
3128 | nid, val, | |
3129 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); | |
3130 | return -EIO; | |
3131 | } | |
1da177e4 LT |
3132 | *ratesp = rates; |
3133 | } | |
3134 | ||
3135 | if (formatsp || bpsp) { | |
3136 | u64 formats = 0; | |
ee504710 | 3137 | unsigned int streams, bps; |
1da177e4 | 3138 | |
92c7c8a7 TI |
3139 | streams = query_stream_param(codec, nid); |
3140 | if (!streams) | |
1da177e4 | 3141 | return -EIO; |
1da177e4 LT |
3142 | |
3143 | bps = 0; | |
3144 | if (streams & AC_SUPFMT_PCM) { | |
3145 | if (val & AC_SUPPCM_BITS_8) { | |
3146 | formats |= SNDRV_PCM_FMTBIT_U8; | |
3147 | bps = 8; | |
3148 | } | |
3149 | if (val & AC_SUPPCM_BITS_16) { | |
3150 | formats |= SNDRV_PCM_FMTBIT_S16_LE; | |
3151 | bps = 16; | |
3152 | } | |
3153 | if (wcaps & AC_WCAP_DIGITAL) { | |
3154 | if (val & AC_SUPPCM_BITS_32) | |
3155 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; | |
3156 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) | |
3157 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
3158 | if (val & AC_SUPPCM_BITS_24) | |
3159 | bps = 24; | |
3160 | else if (val & AC_SUPPCM_BITS_20) | |
3161 | bps = 20; | |
0ba21762 TI |
3162 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
3163 | AC_SUPPCM_BITS_32)) { | |
1da177e4 LT |
3164 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
3165 | if (val & AC_SUPPCM_BITS_32) | |
3166 | bps = 32; | |
1da177e4 LT |
3167 | else if (val & AC_SUPPCM_BITS_24) |
3168 | bps = 24; | |
33ef7651 NG |
3169 | else if (val & AC_SUPPCM_BITS_20) |
3170 | bps = 20; | |
1da177e4 LT |
3171 | } |
3172 | } | |
b5025c50 | 3173 | if (streams & AC_SUPFMT_FLOAT32) { |
1da177e4 | 3174 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
b0bb3aa6 TI |
3175 | if (!bps) |
3176 | bps = 32; | |
b5025c50 TI |
3177 | } |
3178 | if (streams == AC_SUPFMT_AC3) { | |
0ba21762 | 3179 | /* should be exclusive */ |
1da177e4 LT |
3180 | /* temporary hack: we have still no proper support |
3181 | * for the direct AC3 stream... | |
3182 | */ | |
3183 | formats |= SNDRV_PCM_FMTBIT_U8; | |
3184 | bps = 8; | |
3185 | } | |
ee504710 JK |
3186 | if (formats == 0) { |
3187 | snd_printk(KERN_ERR "hda_codec: formats == 0 " | |
3188 | "(nid=0x%x, val=0x%x, ovrd=%i, " | |
3189 | "streams=0x%x)\n", | |
3190 | nid, val, | |
3191 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, | |
3192 | streams); | |
3193 | return -EIO; | |
3194 | } | |
1da177e4 LT |
3195 | if (formatsp) |
3196 | *formatsp = formats; | |
3197 | if (bpsp) | |
3198 | *bpsp = bps; | |
3199 | } | |
3200 | ||
3201 | return 0; | |
3202 | } | |
3203 | ||
3204 | /** | |
d5191e50 TI |
3205 | * snd_hda_is_supported_format - Check the validity of the format |
3206 | * @codec: HD-audio codec | |
3207 | * @nid: NID to check | |
3208 | * @format: the HD-audio format value to check | |
3209 | * | |
3210 | * Check whether the given node supports the format value. | |
1da177e4 LT |
3211 | * |
3212 | * Returns 1 if supported, 0 if not. | |
3213 | */ | |
3214 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, | |
3215 | unsigned int format) | |
3216 | { | |
3217 | int i; | |
3218 | unsigned int val = 0, rate, stream; | |
3219 | ||
92c7c8a7 TI |
3220 | val = query_pcm_param(codec, nid); |
3221 | if (!val) | |
3222 | return 0; | |
1da177e4 LT |
3223 | |
3224 | rate = format & 0xff00; | |
a961f9fe | 3225 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
befdf316 | 3226 | if (rate_bits[i].hda_fmt == rate) { |
1da177e4 LT |
3227 | if (val & (1 << i)) |
3228 | break; | |
3229 | return 0; | |
3230 | } | |
a961f9fe | 3231 | if (i >= AC_PAR_PCM_RATE_BITS) |
1da177e4 LT |
3232 | return 0; |
3233 | ||
92c7c8a7 TI |
3234 | stream = query_stream_param(codec, nid); |
3235 | if (!stream) | |
1da177e4 LT |
3236 | return 0; |
3237 | ||
3238 | if (stream & AC_SUPFMT_PCM) { | |
3239 | switch (format & 0xf0) { | |
3240 | case 0x00: | |
0ba21762 | 3241 | if (!(val & AC_SUPPCM_BITS_8)) |
1da177e4 LT |
3242 | return 0; |
3243 | break; | |
3244 | case 0x10: | |
0ba21762 | 3245 | if (!(val & AC_SUPPCM_BITS_16)) |
1da177e4 LT |
3246 | return 0; |
3247 | break; | |
3248 | case 0x20: | |
0ba21762 | 3249 | if (!(val & AC_SUPPCM_BITS_20)) |
1da177e4 LT |
3250 | return 0; |
3251 | break; | |
3252 | case 0x30: | |
0ba21762 | 3253 | if (!(val & AC_SUPPCM_BITS_24)) |
1da177e4 LT |
3254 | return 0; |
3255 | break; | |
3256 | case 0x40: | |
0ba21762 | 3257 | if (!(val & AC_SUPPCM_BITS_32)) |
1da177e4 LT |
3258 | return 0; |
3259 | break; | |
3260 | default: | |
3261 | return 0; | |
3262 | } | |
3263 | } else { | |
3264 | /* FIXME: check for float32 and AC3? */ | |
3265 | } | |
3266 | ||
3267 | return 1; | |
3268 | } | |
ff7a3267 | 3269 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
1da177e4 LT |
3270 | |
3271 | /* | |
3272 | * PCM stuff | |
3273 | */ | |
3274 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, | |
3275 | struct hda_codec *codec, | |
c8b6bf9b | 3276 | struct snd_pcm_substream *substream) |
1da177e4 LT |
3277 | { |
3278 | return 0; | |
3279 | } | |
3280 | ||
3281 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, | |
3282 | struct hda_codec *codec, | |
3283 | unsigned int stream_tag, | |
3284 | unsigned int format, | |
c8b6bf9b | 3285 | struct snd_pcm_substream *substream) |
1da177e4 LT |
3286 | { |
3287 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); | |
3288 | return 0; | |
3289 | } | |
3290 | ||
3291 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, | |
3292 | struct hda_codec *codec, | |
c8b6bf9b | 3293 | struct snd_pcm_substream *substream) |
1da177e4 | 3294 | { |
888afa15 | 3295 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
1da177e4 LT |
3296 | return 0; |
3297 | } | |
3298 | ||
6c1f45ea TI |
3299 | static int set_pcm_default_values(struct hda_codec *codec, |
3300 | struct hda_pcm_stream *info) | |
1da177e4 | 3301 | { |
ee504710 JK |
3302 | int err; |
3303 | ||
0ba21762 TI |
3304 | /* query support PCM information from the given NID */ |
3305 | if (info->nid && (!info->rates || !info->formats)) { | |
ee504710 | 3306 | err = snd_hda_query_supported_pcm(codec, info->nid, |
0ba21762 TI |
3307 | info->rates ? NULL : &info->rates, |
3308 | info->formats ? NULL : &info->formats, | |
3309 | info->maxbps ? NULL : &info->maxbps); | |
ee504710 JK |
3310 | if (err < 0) |
3311 | return err; | |
1da177e4 LT |
3312 | } |
3313 | if (info->ops.open == NULL) | |
3314 | info->ops.open = hda_pcm_default_open_close; | |
3315 | if (info->ops.close == NULL) | |
3316 | info->ops.close = hda_pcm_default_open_close; | |
3317 | if (info->ops.prepare == NULL) { | |
da3cec35 TI |
3318 | if (snd_BUG_ON(!info->nid)) |
3319 | return -EINVAL; | |
1da177e4 LT |
3320 | info->ops.prepare = hda_pcm_default_prepare; |
3321 | } | |
1da177e4 | 3322 | if (info->ops.cleanup == NULL) { |
da3cec35 TI |
3323 | if (snd_BUG_ON(!info->nid)) |
3324 | return -EINVAL; | |
1da177e4 LT |
3325 | info->ops.cleanup = hda_pcm_default_cleanup; |
3326 | } | |
3327 | return 0; | |
3328 | } | |
3329 | ||
d5191e50 | 3330 | /* global */ |
e3303235 JK |
3331 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
3332 | "Audio", "SPDIF", "HDMI", "Modem" | |
3333 | }; | |
3334 | ||
529bd6c4 TI |
3335 | /* |
3336 | * get the empty PCM device number to assign | |
c8936222 TI |
3337 | * |
3338 | * note the max device number is limited by HDA_MAX_PCMS, currently 10 | |
529bd6c4 TI |
3339 | */ |
3340 | static int get_empty_pcm_device(struct hda_bus *bus, int type) | |
3341 | { | |
f5d6def5 WF |
3342 | /* audio device indices; not linear to keep compatibility */ |
3343 | static int audio_idx[HDA_PCM_NTYPES][5] = { | |
3344 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, | |
3345 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, | |
92608bad | 3346 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
f5d6def5 | 3347 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
529bd6c4 | 3348 | }; |
f5d6def5 WF |
3349 | int i; |
3350 | ||
3351 | if (type >= HDA_PCM_NTYPES) { | |
529bd6c4 TI |
3352 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
3353 | return -EINVAL; | |
3354 | } | |
f5d6def5 WF |
3355 | |
3356 | for (i = 0; audio_idx[type][i] >= 0 ; i++) | |
3357 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) | |
3358 | return audio_idx[type][i]; | |
3359 | ||
28aedaf7 NL |
3360 | snd_printk(KERN_WARNING "Too many %s devices\n", |
3361 | snd_hda_pcm_type_name[type]); | |
f5d6def5 | 3362 | return -EAGAIN; |
529bd6c4 TI |
3363 | } |
3364 | ||
176d5335 TI |
3365 | /* |
3366 | * attach a new PCM stream | |
3367 | */ | |
529bd6c4 | 3368 | static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) |
176d5335 | 3369 | { |
33fa35ed | 3370 | struct hda_bus *bus = codec->bus; |
176d5335 TI |
3371 | struct hda_pcm_stream *info; |
3372 | int stream, err; | |
3373 | ||
b91f080f | 3374 | if (snd_BUG_ON(!pcm->name)) |
176d5335 TI |
3375 | return -EINVAL; |
3376 | for (stream = 0; stream < 2; stream++) { | |
3377 | info = &pcm->stream[stream]; | |
3378 | if (info->substreams) { | |
3379 | err = set_pcm_default_values(codec, info); | |
3380 | if (err < 0) | |
3381 | return err; | |
3382 | } | |
3383 | } | |
33fa35ed | 3384 | return bus->ops.attach_pcm(bus, codec, pcm); |
176d5335 TI |
3385 | } |
3386 | ||
529bd6c4 TI |
3387 | /* assign all PCMs of the given codec */ |
3388 | int snd_hda_codec_build_pcms(struct hda_codec *codec) | |
3389 | { | |
3390 | unsigned int pcm; | |
3391 | int err; | |
3392 | ||
3393 | if (!codec->num_pcms) { | |
3394 | if (!codec->patch_ops.build_pcms) | |
3395 | return 0; | |
3396 | err = codec->patch_ops.build_pcms(codec); | |
6e655bf2 TI |
3397 | if (err < 0) { |
3398 | printk(KERN_ERR "hda_codec: cannot build PCMs" | |
28aedaf7 | 3399 | "for #%d (error %d)\n", codec->addr, err); |
6e655bf2 TI |
3400 | err = snd_hda_codec_reset(codec); |
3401 | if (err < 0) { | |
3402 | printk(KERN_ERR | |
3403 | "hda_codec: cannot revert codec\n"); | |
3404 | return err; | |
3405 | } | |
3406 | } | |
529bd6c4 TI |
3407 | } |
3408 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { | |
3409 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; | |
3410 | int dev; | |
3411 | ||
3412 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) | |
41b5b01a | 3413 | continue; /* no substreams assigned */ |
529bd6c4 TI |
3414 | |
3415 | if (!cpcm->pcm) { | |
3416 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); | |
3417 | if (dev < 0) | |
6e655bf2 | 3418 | continue; /* no fatal error */ |
529bd6c4 TI |
3419 | cpcm->device = dev; |
3420 | err = snd_hda_attach_pcm(codec, cpcm); | |
6e655bf2 TI |
3421 | if (err < 0) { |
3422 | printk(KERN_ERR "hda_codec: cannot attach " | |
3423 | "PCM stream %d for codec #%d\n", | |
3424 | dev, codec->addr); | |
3425 | continue; /* no fatal error */ | |
3426 | } | |
529bd6c4 TI |
3427 | } |
3428 | } | |
3429 | return 0; | |
3430 | } | |
3431 | ||
1da177e4 LT |
3432 | /** |
3433 | * snd_hda_build_pcms - build PCM information | |
3434 | * @bus: the BUS | |
3435 | * | |
3436 | * Create PCM information for each codec included in the bus. | |
3437 | * | |
3438 | * The build_pcms codec patch is requested to set up codec->num_pcms and | |
3439 | * codec->pcm_info properly. The array is referred by the top-level driver | |
3440 | * to create its PCM instances. | |
3441 | * The allocated codec->pcm_info should be released in codec->patch_ops.free | |
3442 | * callback. | |
3443 | * | |
3444 | * At least, substreams, channels_min and channels_max must be filled for | |
3445 | * each stream. substreams = 0 indicates that the stream doesn't exist. | |
3446 | * When rates and/or formats are zero, the supported values are queried | |
3447 | * from the given nid. The nid is used also by the default ops.prepare | |
3448 | * and ops.cleanup callbacks. | |
3449 | * | |
3450 | * The driver needs to call ops.open in its open callback. Similarly, | |
3451 | * ops.close is supposed to be called in the close callback. | |
3452 | * ops.prepare should be called in the prepare or hw_params callback | |
3453 | * with the proper parameters for set up. | |
3454 | * ops.cleanup should be called in hw_free for clean up of streams. | |
3455 | * | |
3456 | * This function returns 0 if successfull, or a negative error code. | |
3457 | */ | |
529bd6c4 | 3458 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
1da177e4 | 3459 | { |
0ba21762 | 3460 | struct hda_codec *codec; |
1da177e4 | 3461 | |
0ba21762 | 3462 | list_for_each_entry(codec, &bus->codec_list, list) { |
529bd6c4 TI |
3463 | int err = snd_hda_codec_build_pcms(codec); |
3464 | if (err < 0) | |
3465 | return err; | |
1da177e4 LT |
3466 | } |
3467 | return 0; | |
3468 | } | |
ff7a3267 | 3469 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
1da177e4 | 3470 | |
1da177e4 LT |
3471 | /** |
3472 | * snd_hda_check_board_config - compare the current codec with the config table | |
3473 | * @codec: the HDA codec | |
f5fcc13c TI |
3474 | * @num_configs: number of config enums |
3475 | * @models: array of model name strings | |
1da177e4 LT |
3476 | * @tbl: configuration table, terminated by null entries |
3477 | * | |
3478 | * Compares the modelname or PCI subsystem id of the current codec with the | |
3479 | * given configuration table. If a matching entry is found, returns its | |
3480 | * config value (supposed to be 0 or positive). | |
3481 | * | |
3482 | * If no entries are matching, the function returns a negative value. | |
3483 | */ | |
12f288bf TI |
3484 | int snd_hda_check_board_config(struct hda_codec *codec, |
3485 | int num_configs, const char **models, | |
3486 | const struct snd_pci_quirk *tbl) | |
1da177e4 | 3487 | { |
f44ac837 | 3488 | if (codec->modelname && models) { |
f5fcc13c TI |
3489 | int i; |
3490 | for (i = 0; i < num_configs; i++) { | |
3491 | if (models[i] && | |
f44ac837 | 3492 | !strcmp(codec->modelname, models[i])) { |
f5fcc13c TI |
3493 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
3494 | "selected\n", models[i]); | |
3495 | return i; | |
1da177e4 LT |
3496 | } |
3497 | } | |
3498 | } | |
3499 | ||
f5fcc13c TI |
3500 | if (!codec->bus->pci || !tbl) |
3501 | return -1; | |
3502 | ||
3503 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); | |
3504 | if (!tbl) | |
3505 | return -1; | |
3506 | if (tbl->value >= 0 && tbl->value < num_configs) { | |
62cf872a | 3507 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
f5fcc13c TI |
3508 | char tmp[10]; |
3509 | const char *model = NULL; | |
3510 | if (models) | |
3511 | model = models[tbl->value]; | |
3512 | if (!model) { | |
3513 | sprintf(tmp, "#%d", tbl->value); | |
3514 | model = tmp; | |
1da177e4 | 3515 | } |
f5fcc13c TI |
3516 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
3517 | "for config %x:%x (%s)\n", | |
3518 | model, tbl->subvendor, tbl->subdevice, | |
3519 | (tbl->name ? tbl->name : "Unknown device")); | |
3520 | #endif | |
3521 | return tbl->value; | |
1da177e4 LT |
3522 | } |
3523 | return -1; | |
3524 | } | |
ff7a3267 | 3525 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
1da177e4 | 3526 | |
2eda3445 MCC |
3527 | /** |
3528 | * snd_hda_check_board_codec_sid_config - compare the current codec | |
28aedaf7 NL |
3529 | subsystem ID with the |
3530 | config table | |
2eda3445 MCC |
3531 | |
3532 | This is important for Gateway notebooks with SB450 HDA Audio | |
3533 | where the vendor ID of the PCI device is: | |
3534 | ATI Technologies Inc SB450 HDA Audio [1002:437b] | |
3535 | and the vendor/subvendor are found only at the codec. | |
3536 | ||
3537 | * @codec: the HDA codec | |
3538 | * @num_configs: number of config enums | |
3539 | * @models: array of model name strings | |
3540 | * @tbl: configuration table, terminated by null entries | |
3541 | * | |
3542 | * Compares the modelname or PCI subsystem id of the current codec with the | |
3543 | * given configuration table. If a matching entry is found, returns its | |
3544 | * config value (supposed to be 0 or positive). | |
3545 | * | |
3546 | * If no entries are matching, the function returns a negative value. | |
3547 | */ | |
3548 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, | |
3549 | int num_configs, const char **models, | |
3550 | const struct snd_pci_quirk *tbl) | |
3551 | { | |
3552 | const struct snd_pci_quirk *q; | |
3553 | ||
3554 | /* Search for codec ID */ | |
3555 | for (q = tbl; q->subvendor; q++) { | |
3556 | unsigned long vendorid = (q->subdevice) | (q->subvendor << 16); | |
3557 | ||
3558 | if (vendorid == codec->subsystem_id) | |
3559 | break; | |
3560 | } | |
3561 | ||
3562 | if (!q->subvendor) | |
3563 | return -1; | |
3564 | ||
3565 | tbl = q; | |
3566 | ||
3567 | if (tbl->value >= 0 && tbl->value < num_configs) { | |
d94ff6b7 | 3568 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
2eda3445 MCC |
3569 | char tmp[10]; |
3570 | const char *model = NULL; | |
3571 | if (models) | |
3572 | model = models[tbl->value]; | |
3573 | if (!model) { | |
3574 | sprintf(tmp, "#%d", tbl->value); | |
3575 | model = tmp; | |
3576 | } | |
3577 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " | |
3578 | "for config %x:%x (%s)\n", | |
3579 | model, tbl->subvendor, tbl->subdevice, | |
3580 | (tbl->name ? tbl->name : "Unknown device")); | |
3581 | #endif | |
3582 | return tbl->value; | |
3583 | } | |
3584 | return -1; | |
3585 | } | |
3586 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); | |
3587 | ||
1da177e4 LT |
3588 | /** |
3589 | * snd_hda_add_new_ctls - create controls from the array | |
3590 | * @codec: the HDA codec | |
c8b6bf9b | 3591 | * @knew: the array of struct snd_kcontrol_new |
1da177e4 LT |
3592 | * |
3593 | * This helper function creates and add new controls in the given array. | |
3594 | * The array must be terminated with an empty entry as terminator. | |
3595 | * | |
3596 | * Returns 0 if successful, or a negative error code. | |
3597 | */ | |
12f288bf | 3598 | int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew) |
1da177e4 | 3599 | { |
4d02d1b6 | 3600 | int err; |
1da177e4 LT |
3601 | |
3602 | for (; knew->name; knew++) { | |
54d17403 | 3603 | struct snd_kcontrol *kctl; |
5b0cb1d8 JK |
3604 | if (knew->iface == -1) /* skip this codec private value */ |
3605 | continue; | |
54d17403 | 3606 | kctl = snd_ctl_new1(knew, codec); |
0ba21762 | 3607 | if (!kctl) |
54d17403 | 3608 | return -ENOMEM; |
3911a4c1 | 3609 | err = snd_hda_ctl_add(codec, 0, kctl); |
54d17403 | 3610 | if (err < 0) { |
0ba21762 | 3611 | if (!codec->addr) |
54d17403 TI |
3612 | return err; |
3613 | kctl = snd_ctl_new1(knew, codec); | |
0ba21762 | 3614 | if (!kctl) |
54d17403 TI |
3615 | return -ENOMEM; |
3616 | kctl->id.device = codec->addr; | |
3911a4c1 | 3617 | err = snd_hda_ctl_add(codec, 0, kctl); |
0ba21762 | 3618 | if (err < 0) |
54d17403 TI |
3619 | return err; |
3620 | } | |
1da177e4 LT |
3621 | } |
3622 | return 0; | |
3623 | } | |
ff7a3267 | 3624 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
1da177e4 | 3625 | |
cb53c626 TI |
3626 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
3627 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, | |
3628 | unsigned int power_state); | |
3629 | ||
3630 | static void hda_power_work(struct work_struct *work) | |
3631 | { | |
3632 | struct hda_codec *codec = | |
3633 | container_of(work, struct hda_codec, power_work.work); | |
33fa35ed | 3634 | struct hda_bus *bus = codec->bus; |
cb53c626 | 3635 | |
2e492462 ML |
3636 | if (!codec->power_on || codec->power_count) { |
3637 | codec->power_transition = 0; | |
cb53c626 | 3638 | return; |
2e492462 | 3639 | } |
cb53c626 TI |
3640 | |
3641 | hda_call_codec_suspend(codec); | |
33fa35ed TI |
3642 | if (bus->ops.pm_notify) |
3643 | bus->ops.pm_notify(bus); | |
cb53c626 TI |
3644 | } |
3645 | ||
3646 | static void hda_keep_power_on(struct hda_codec *codec) | |
3647 | { | |
3648 | codec->power_count++; | |
3649 | codec->power_on = 1; | |
a2f6309e TI |
3650 | codec->power_jiffies = jiffies; |
3651 | } | |
3652 | ||
d5191e50 | 3653 | /* update the power on/off account with the current jiffies */ |
a2f6309e TI |
3654 | void snd_hda_update_power_acct(struct hda_codec *codec) |
3655 | { | |
3656 | unsigned long delta = jiffies - codec->power_jiffies; | |
3657 | if (codec->power_on) | |
3658 | codec->power_on_acct += delta; | |
3659 | else | |
3660 | codec->power_off_acct += delta; | |
3661 | codec->power_jiffies += delta; | |
cb53c626 TI |
3662 | } |
3663 | ||
d5191e50 TI |
3664 | /** |
3665 | * snd_hda_power_up - Power-up the codec | |
3666 | * @codec: HD-audio codec | |
3667 | * | |
3668 | * Increment the power-up counter and power up the hardware really when | |
3669 | * not turned on yet. | |
28aedaf7 | 3670 | */ |
cb53c626 TI |
3671 | void snd_hda_power_up(struct hda_codec *codec) |
3672 | { | |
33fa35ed TI |
3673 | struct hda_bus *bus = codec->bus; |
3674 | ||
cb53c626 | 3675 | codec->power_count++; |
a221e287 | 3676 | if (codec->power_on || codec->power_transition) |
cb53c626 TI |
3677 | return; |
3678 | ||
a2f6309e | 3679 | snd_hda_update_power_acct(codec); |
cb53c626 | 3680 | codec->power_on = 1; |
a2f6309e | 3681 | codec->power_jiffies = jiffies; |
33fa35ed TI |
3682 | if (bus->ops.pm_notify) |
3683 | bus->ops.pm_notify(bus); | |
cb53c626 TI |
3684 | hda_call_codec_resume(codec); |
3685 | cancel_delayed_work(&codec->power_work); | |
a221e287 | 3686 | codec->power_transition = 0; |
cb53c626 | 3687 | } |
ff7a3267 | 3688 | EXPORT_SYMBOL_HDA(snd_hda_power_up); |
1289e9e8 TI |
3689 | |
3690 | #define power_save(codec) \ | |
3691 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) | |
cb53c626 | 3692 | |
d5191e50 TI |
3693 | /** |
3694 | * snd_hda_power_down - Power-down the codec | |
3695 | * @codec: HD-audio codec | |
3696 | * | |
3697 | * Decrement the power-up counter and schedules the power-off work if | |
3698 | * the counter rearches to zero. | |
28aedaf7 | 3699 | */ |
cb53c626 TI |
3700 | void snd_hda_power_down(struct hda_codec *codec) |
3701 | { | |
3702 | --codec->power_count; | |
a221e287 | 3703 | if (!codec->power_on || codec->power_count || codec->power_transition) |
cb53c626 | 3704 | return; |
fee2fba3 | 3705 | if (power_save(codec)) { |
a221e287 | 3706 | codec->power_transition = 1; /* avoid reentrance */ |
c107b41c | 3707 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
fee2fba3 | 3708 | msecs_to_jiffies(power_save(codec) * 1000)); |
a221e287 | 3709 | } |
cb53c626 | 3710 | } |
ff7a3267 | 3711 | EXPORT_SYMBOL_HDA(snd_hda_power_down); |
cb53c626 | 3712 | |
d5191e50 TI |
3713 | /** |
3714 | * snd_hda_check_amp_list_power - Check the amp list and update the power | |
3715 | * @codec: HD-audio codec | |
3716 | * @check: the object containing an AMP list and the status | |
3717 | * @nid: NID to check / update | |
3718 | * | |
3719 | * Check whether the given NID is in the amp list. If it's in the list, | |
3720 | * check the current AMP status, and update the the power-status according | |
3721 | * to the mute status. | |
3722 | * | |
3723 | * This function is supposed to be set or called from the check_power_status | |
3724 | * patch ops. | |
28aedaf7 | 3725 | */ |
cb53c626 TI |
3726 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
3727 | struct hda_loopback_check *check, | |
3728 | hda_nid_t nid) | |
3729 | { | |
3730 | struct hda_amp_list *p; | |
3731 | int ch, v; | |
3732 | ||
3733 | if (!check->amplist) | |
3734 | return 0; | |
3735 | for (p = check->amplist; p->nid; p++) { | |
3736 | if (p->nid == nid) | |
3737 | break; | |
3738 | } | |
3739 | if (!p->nid) | |
3740 | return 0; /* nothing changed */ | |
3741 | ||
3742 | for (p = check->amplist; p->nid; p++) { | |
3743 | for (ch = 0; ch < 2; ch++) { | |
3744 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, | |
3745 | p->idx); | |
3746 | if (!(v & HDA_AMP_MUTE) && v > 0) { | |
3747 | if (!check->power_on) { | |
3748 | check->power_on = 1; | |
3749 | snd_hda_power_up(codec); | |
3750 | } | |
3751 | return 1; | |
3752 | } | |
3753 | } | |
3754 | } | |
3755 | if (check->power_on) { | |
3756 | check->power_on = 0; | |
3757 | snd_hda_power_down(codec); | |
3758 | } | |
3759 | return 0; | |
3760 | } | |
ff7a3267 | 3761 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
cb53c626 | 3762 | #endif |
1da177e4 | 3763 | |
c8b6bf9b | 3764 | /* |
d2a6d7dc TI |
3765 | * Channel mode helper |
3766 | */ | |
d5191e50 TI |
3767 | |
3768 | /** | |
3769 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum | |
3770 | */ | |
0ba21762 TI |
3771 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
3772 | struct snd_ctl_elem_info *uinfo, | |
3773 | const struct hda_channel_mode *chmode, | |
3774 | int num_chmodes) | |
d2a6d7dc TI |
3775 | { |
3776 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
3777 | uinfo->count = 1; | |
3778 | uinfo->value.enumerated.items = num_chmodes; | |
3779 | if (uinfo->value.enumerated.item >= num_chmodes) | |
3780 | uinfo->value.enumerated.item = num_chmodes - 1; | |
3781 | sprintf(uinfo->value.enumerated.name, "%dch", | |
3782 | chmode[uinfo->value.enumerated.item].channels); | |
3783 | return 0; | |
3784 | } | |
ff7a3267 | 3785 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
d2a6d7dc | 3786 | |
d5191e50 TI |
3787 | /** |
3788 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum | |
3789 | */ | |
0ba21762 TI |
3790 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
3791 | struct snd_ctl_elem_value *ucontrol, | |
3792 | const struct hda_channel_mode *chmode, | |
3793 | int num_chmodes, | |
d2a6d7dc TI |
3794 | int max_channels) |
3795 | { | |
3796 | int i; | |
3797 | ||
3798 | for (i = 0; i < num_chmodes; i++) { | |
3799 | if (max_channels == chmode[i].channels) { | |
3800 | ucontrol->value.enumerated.item[0] = i; | |
3801 | break; | |
3802 | } | |
3803 | } | |
3804 | return 0; | |
3805 | } | |
ff7a3267 | 3806 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
d2a6d7dc | 3807 | |
d5191e50 TI |
3808 | /** |
3809 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum | |
3810 | */ | |
0ba21762 TI |
3811 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
3812 | struct snd_ctl_elem_value *ucontrol, | |
3813 | const struct hda_channel_mode *chmode, | |
3814 | int num_chmodes, | |
d2a6d7dc TI |
3815 | int *max_channelsp) |
3816 | { | |
3817 | unsigned int mode; | |
3818 | ||
3819 | mode = ucontrol->value.enumerated.item[0]; | |
68ea7b2f TI |
3820 | if (mode >= num_chmodes) |
3821 | return -EINVAL; | |
82beb8fd | 3822 | if (*max_channelsp == chmode[mode].channels) |
d2a6d7dc TI |
3823 | return 0; |
3824 | /* change the current channel setting */ | |
3825 | *max_channelsp = chmode[mode].channels; | |
3826 | if (chmode[mode].sequence) | |
82beb8fd | 3827 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
d2a6d7dc TI |
3828 | return 1; |
3829 | } | |
ff7a3267 | 3830 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
d2a6d7dc | 3831 | |
1da177e4 LT |
3832 | /* |
3833 | * input MUX helper | |
3834 | */ | |
d5191e50 TI |
3835 | |
3836 | /** | |
3837 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum | |
3838 | */ | |
0ba21762 TI |
3839 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
3840 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
3841 | { |
3842 | unsigned int index; | |
3843 | ||
3844 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
3845 | uinfo->count = 1; | |
3846 | uinfo->value.enumerated.items = imux->num_items; | |
5513b0c5 TI |
3847 | if (!imux->num_items) |
3848 | return 0; | |
1da177e4 LT |
3849 | index = uinfo->value.enumerated.item; |
3850 | if (index >= imux->num_items) | |
3851 | index = imux->num_items - 1; | |
3852 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); | |
3853 | return 0; | |
3854 | } | |
ff7a3267 | 3855 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
1da177e4 | 3856 | |
d5191e50 TI |
3857 | /** |
3858 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum | |
3859 | */ | |
0ba21762 TI |
3860 | int snd_hda_input_mux_put(struct hda_codec *codec, |
3861 | const struct hda_input_mux *imux, | |
3862 | struct snd_ctl_elem_value *ucontrol, | |
3863 | hda_nid_t nid, | |
1da177e4 LT |
3864 | unsigned int *cur_val) |
3865 | { | |
3866 | unsigned int idx; | |
3867 | ||
5513b0c5 TI |
3868 | if (!imux->num_items) |
3869 | return 0; | |
1da177e4 LT |
3870 | idx = ucontrol->value.enumerated.item[0]; |
3871 | if (idx >= imux->num_items) | |
3872 | idx = imux->num_items - 1; | |
82beb8fd | 3873 | if (*cur_val == idx) |
1da177e4 | 3874 | return 0; |
82beb8fd TI |
3875 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
3876 | imux->items[idx].index); | |
1da177e4 LT |
3877 | *cur_val = idx; |
3878 | return 1; | |
3879 | } | |
ff7a3267 | 3880 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
1da177e4 LT |
3881 | |
3882 | ||
3883 | /* | |
3884 | * Multi-channel / digital-out PCM helper functions | |
3885 | */ | |
3886 | ||
6b97eb45 TI |
3887 | /* setup SPDIF output stream */ |
3888 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, | |
3889 | unsigned int stream_tag, unsigned int format) | |
3890 | { | |
3891 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ | |
2f72853c | 3892 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
28aedaf7 | 3893 | set_dig_out_convert(codec, nid, |
2f72853c TI |
3894 | codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff, |
3895 | -1); | |
6b97eb45 | 3896 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
2f72853c TI |
3897 | if (codec->slave_dig_outs) { |
3898 | hda_nid_t *d; | |
3899 | for (d = codec->slave_dig_outs; *d; d++) | |
3900 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, | |
3901 | format); | |
3902 | } | |
6b97eb45 | 3903 | /* turn on again (if needed) */ |
2f72853c TI |
3904 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
3905 | set_dig_out_convert(codec, nid, | |
3906 | codec->spdif_ctls & 0xff, -1); | |
3907 | } | |
de51ca12 | 3908 | |
2f72853c TI |
3909 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
3910 | { | |
3911 | snd_hda_codec_cleanup_stream(codec, nid); | |
3912 | if (codec->slave_dig_outs) { | |
3913 | hda_nid_t *d; | |
3914 | for (d = codec->slave_dig_outs; *d; d++) | |
3915 | snd_hda_codec_cleanup_stream(codec, *d); | |
de51ca12 | 3916 | } |
6b97eb45 TI |
3917 | } |
3918 | ||
d5191e50 TI |
3919 | /** |
3920 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec | |
3921 | * @bus: HD-audio bus | |
3922 | */ | |
fb8d1a34 TI |
3923 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
3924 | { | |
3925 | struct hda_codec *codec; | |
3926 | ||
3927 | if (!bus) | |
3928 | return; | |
3929 | list_for_each_entry(codec, &bus->codec_list, list) { | |
3930 | #ifdef CONFIG_SND_HDA_POWER_SAVE | |
3931 | if (!codec->power_on) | |
3932 | continue; | |
3933 | #endif | |
3934 | if (codec->patch_ops.reboot_notify) | |
3935 | codec->patch_ops.reboot_notify(codec); | |
3936 | } | |
3937 | } | |
8f217a22 | 3938 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
fb8d1a34 | 3939 | |
d5191e50 TI |
3940 | /** |
3941 | * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode | |
1da177e4 | 3942 | */ |
0ba21762 TI |
3943 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
3944 | struct hda_multi_out *mout) | |
1da177e4 | 3945 | { |
62932df8 | 3946 | mutex_lock(&codec->spdif_mutex); |
5930ca41 TI |
3947 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
3948 | /* already opened as analog dup; reset it once */ | |
2f72853c | 3949 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 | 3950 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
62932df8 | 3951 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3952 | return 0; |
3953 | } | |
ff7a3267 | 3954 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
1da177e4 | 3955 | |
d5191e50 TI |
3956 | /** |
3957 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream | |
3958 | */ | |
6b97eb45 TI |
3959 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
3960 | struct hda_multi_out *mout, | |
3961 | unsigned int stream_tag, | |
3962 | unsigned int format, | |
3963 | struct snd_pcm_substream *substream) | |
3964 | { | |
3965 | mutex_lock(&codec->spdif_mutex); | |
3966 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); | |
3967 | mutex_unlock(&codec->spdif_mutex); | |
3968 | return 0; | |
3969 | } | |
ff7a3267 | 3970 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
6b97eb45 | 3971 | |
d5191e50 TI |
3972 | /** |
3973 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream | |
3974 | */ | |
9411e21c TI |
3975 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
3976 | struct hda_multi_out *mout) | |
3977 | { | |
3978 | mutex_lock(&codec->spdif_mutex); | |
3979 | cleanup_dig_out_stream(codec, mout->dig_out_nid); | |
3980 | mutex_unlock(&codec->spdif_mutex); | |
3981 | return 0; | |
3982 | } | |
3983 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); | |
3984 | ||
d5191e50 TI |
3985 | /** |
3986 | * snd_hda_multi_out_dig_close - release the digital out stream | |
1da177e4 | 3987 | */ |
0ba21762 TI |
3988 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
3989 | struct hda_multi_out *mout) | |
1da177e4 | 3990 | { |
62932df8 | 3991 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 3992 | mout->dig_out_used = 0; |
62932df8 | 3993 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3994 | return 0; |
3995 | } | |
ff7a3267 | 3996 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
1da177e4 | 3997 | |
d5191e50 TI |
3998 | /** |
3999 | * snd_hda_multi_out_analog_open - open analog outputs | |
4000 | * | |
4001 | * Open analog outputs and set up the hw-constraints. | |
4002 | * If the digital outputs can be opened as slave, open the digital | |
4003 | * outputs, too. | |
1da177e4 | 4004 | */ |
0ba21762 TI |
4005 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
4006 | struct hda_multi_out *mout, | |
9a08160b TI |
4007 | struct snd_pcm_substream *substream, |
4008 | struct hda_pcm_stream *hinfo) | |
4009 | { | |
4010 | struct snd_pcm_runtime *runtime = substream->runtime; | |
4011 | runtime->hw.channels_max = mout->max_channels; | |
4012 | if (mout->dig_out_nid) { | |
4013 | if (!mout->analog_rates) { | |
4014 | mout->analog_rates = hinfo->rates; | |
4015 | mout->analog_formats = hinfo->formats; | |
4016 | mout->analog_maxbps = hinfo->maxbps; | |
4017 | } else { | |
4018 | runtime->hw.rates = mout->analog_rates; | |
4019 | runtime->hw.formats = mout->analog_formats; | |
4020 | hinfo->maxbps = mout->analog_maxbps; | |
4021 | } | |
4022 | if (!mout->spdif_rates) { | |
4023 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, | |
4024 | &mout->spdif_rates, | |
4025 | &mout->spdif_formats, | |
4026 | &mout->spdif_maxbps); | |
4027 | } | |
4028 | mutex_lock(&codec->spdif_mutex); | |
4029 | if (mout->share_spdif) { | |
022b466f TI |
4030 | if ((runtime->hw.rates & mout->spdif_rates) && |
4031 | (runtime->hw.formats & mout->spdif_formats)) { | |
4032 | runtime->hw.rates &= mout->spdif_rates; | |
4033 | runtime->hw.formats &= mout->spdif_formats; | |
4034 | if (mout->spdif_maxbps < hinfo->maxbps) | |
4035 | hinfo->maxbps = mout->spdif_maxbps; | |
4036 | } else { | |
4037 | mout->share_spdif = 0; | |
4038 | /* FIXME: need notify? */ | |
4039 | } | |
9a08160b | 4040 | } |
eaa9985b | 4041 | mutex_unlock(&codec->spdif_mutex); |
9a08160b | 4042 | } |
1da177e4 LT |
4043 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
4044 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); | |
4045 | } | |
ff7a3267 | 4046 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
1da177e4 | 4047 | |
d5191e50 TI |
4048 | /** |
4049 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. | |
4050 | * | |
4051 | * Set up the i/o for analog out. | |
4052 | * When the digital out is available, copy the front out to digital out, too. | |
1da177e4 | 4053 | */ |
0ba21762 TI |
4054 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
4055 | struct hda_multi_out *mout, | |
1da177e4 LT |
4056 | unsigned int stream_tag, |
4057 | unsigned int format, | |
c8b6bf9b | 4058 | struct snd_pcm_substream *substream) |
1da177e4 LT |
4059 | { |
4060 | hda_nid_t *nids = mout->dac_nids; | |
4061 | int chs = substream->runtime->channels; | |
4062 | int i; | |
4063 | ||
62932df8 | 4064 | mutex_lock(&codec->spdif_mutex); |
9a08160b TI |
4065 | if (mout->dig_out_nid && mout->share_spdif && |
4066 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { | |
1da177e4 | 4067 | if (chs == 2 && |
0ba21762 TI |
4068 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
4069 | format) && | |
4070 | !(codec->spdif_status & IEC958_AES0_NONAUDIO)) { | |
1da177e4 | 4071 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
6b97eb45 TI |
4072 | setup_dig_out_stream(codec, mout->dig_out_nid, |
4073 | stream_tag, format); | |
1da177e4 LT |
4074 | } else { |
4075 | mout->dig_out_used = 0; | |
2f72853c | 4076 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 LT |
4077 | } |
4078 | } | |
62932df8 | 4079 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
4080 | |
4081 | /* front */ | |
0ba21762 TI |
4082 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
4083 | 0, format); | |
d29240ce TI |
4084 | if (!mout->no_share_stream && |
4085 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) | |
1da177e4 | 4086 | /* headphone out will just decode front left/right (stereo) */ |
0ba21762 TI |
4087 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
4088 | 0, format); | |
82bc955f TI |
4089 | /* extra outputs copied from front */ |
4090 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) | |
d29240ce | 4091 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
82bc955f TI |
4092 | snd_hda_codec_setup_stream(codec, |
4093 | mout->extra_out_nid[i], | |
4094 | stream_tag, 0, format); | |
4095 | ||
1da177e4 LT |
4096 | /* surrounds */ |
4097 | for (i = 1; i < mout->num_dacs; i++) { | |
4b3acaf5 | 4098 | if (chs >= (i + 1) * 2) /* independent out */ |
0ba21762 TI |
4099 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
4100 | i * 2, format); | |
d29240ce | 4101 | else if (!mout->no_share_stream) /* copy front */ |
0ba21762 TI |
4102 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
4103 | 0, format); | |
1da177e4 LT |
4104 | } |
4105 | return 0; | |
4106 | } | |
ff7a3267 | 4107 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
1da177e4 | 4108 | |
d5191e50 TI |
4109 | /** |
4110 | * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out | |
1da177e4 | 4111 | */ |
0ba21762 TI |
4112 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
4113 | struct hda_multi_out *mout) | |
1da177e4 LT |
4114 | { |
4115 | hda_nid_t *nids = mout->dac_nids; | |
4116 | int i; | |
4117 | ||
4118 | for (i = 0; i < mout->num_dacs; i++) | |
888afa15 | 4119 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
1da177e4 | 4120 | if (mout->hp_nid) |
888afa15 | 4121 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
82bc955f TI |
4122 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
4123 | if (mout->extra_out_nid[i]) | |
888afa15 TI |
4124 | snd_hda_codec_cleanup_stream(codec, |
4125 | mout->extra_out_nid[i]); | |
62932df8 | 4126 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 4127 | if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { |
2f72853c | 4128 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 LT |
4129 | mout->dig_out_used = 0; |
4130 | } | |
62932df8 | 4131 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
4132 | return 0; |
4133 | } | |
ff7a3267 | 4134 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
1da177e4 | 4135 | |
e9edcee0 | 4136 | /* |
6b34500c | 4137 | * Helper for automatic pin configuration |
e9edcee0 | 4138 | */ |
df694daa | 4139 | |
12f288bf | 4140 | static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list) |
df694daa KY |
4141 | { |
4142 | for (; *list; list++) | |
4143 | if (*list == nid) | |
4144 | return 1; | |
4145 | return 0; | |
4146 | } | |
4147 | ||
81937d3b SL |
4148 | |
4149 | /* | |
4150 | * Sort an associated group of pins according to their sequence numbers. | |
4151 | */ | |
28aedaf7 | 4152 | static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences, |
81937d3b SL |
4153 | int num_pins) |
4154 | { | |
4155 | int i, j; | |
4156 | short seq; | |
4157 | hda_nid_t nid; | |
28aedaf7 | 4158 | |
81937d3b SL |
4159 | for (i = 0; i < num_pins; i++) { |
4160 | for (j = i + 1; j < num_pins; j++) { | |
4161 | if (sequences[i] > sequences[j]) { | |
4162 | seq = sequences[i]; | |
4163 | sequences[i] = sequences[j]; | |
4164 | sequences[j] = seq; | |
4165 | nid = pins[i]; | |
4166 | pins[i] = pins[j]; | |
4167 | pins[j] = nid; | |
4168 | } | |
4169 | } | |
4170 | } | |
4171 | } | |
4172 | ||
4173 | ||
82bc955f TI |
4174 | /* |
4175 | * Parse all pin widgets and store the useful pin nids to cfg | |
4176 | * | |
4177 | * The number of line-outs or any primary output is stored in line_outs, | |
4178 | * and the corresponding output pins are assigned to line_out_pins[], | |
4179 | * in the order of front, rear, CLFE, side, ... | |
4180 | * | |
4181 | * If more extra outputs (speaker and headphone) are found, the pins are | |
eb06ed8f | 4182 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack |
82bc955f TI |
4183 | * is detected, one of speaker of HP pins is assigned as the primary |
4184 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive | |
4185 | * if any analog output exists. | |
28aedaf7 | 4186 | * |
82bc955f TI |
4187 | * The analog input pins are assigned to input_pins array. |
4188 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, | |
4189 | * respectively. | |
4190 | */ | |
12f288bf TI |
4191 | int snd_hda_parse_pin_def_config(struct hda_codec *codec, |
4192 | struct auto_pin_cfg *cfg, | |
4193 | hda_nid_t *ignore_nids) | |
e9edcee0 | 4194 | { |
0ef6ce7b | 4195 | hda_nid_t nid, end_nid; |
81937d3b SL |
4196 | short seq, assoc_line_out, assoc_speaker; |
4197 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; | |
4198 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; | |
f889fa91 | 4199 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
e9edcee0 TI |
4200 | |
4201 | memset(cfg, 0, sizeof(*cfg)); | |
4202 | ||
81937d3b SL |
4203 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
4204 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); | |
f889fa91 | 4205 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
81937d3b | 4206 | assoc_line_out = assoc_speaker = 0; |
e9edcee0 | 4207 | |
0ef6ce7b TI |
4208 | end_nid = codec->start_nid + codec->num_nodes; |
4209 | for (nid = codec->start_nid; nid < end_nid; nid++) { | |
54d17403 | 4210 | unsigned int wid_caps = get_wcaps(codec, nid); |
a22d543a | 4211 | unsigned int wid_type = get_wcaps_type(wid_caps); |
e9edcee0 TI |
4212 | unsigned int def_conf; |
4213 | short assoc, loc; | |
4214 | ||
4215 | /* read all default configuration for pin complex */ | |
4216 | if (wid_type != AC_WID_PIN) | |
4217 | continue; | |
df694daa KY |
4218 | /* ignore the given nids (e.g. pc-beep returns error) */ |
4219 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) | |
4220 | continue; | |
4221 | ||
c17a1aba | 4222 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
e9edcee0 TI |
4223 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
4224 | continue; | |
4225 | loc = get_defcfg_location(def_conf); | |
4226 | switch (get_defcfg_device(def_conf)) { | |
4227 | case AC_JACK_LINE_OUT: | |
e9edcee0 TI |
4228 | seq = get_defcfg_sequence(def_conf); |
4229 | assoc = get_defcfg_association(def_conf); | |
90da78bf MR |
4230 | |
4231 | if (!(wid_caps & AC_WCAP_STEREO)) | |
4232 | if (!cfg->mono_out_pin) | |
4233 | cfg->mono_out_pin = nid; | |
0ba21762 | 4234 | if (!assoc) |
e9edcee0 | 4235 | continue; |
0ba21762 | 4236 | if (!assoc_line_out) |
e9edcee0 TI |
4237 | assoc_line_out = assoc; |
4238 | else if (assoc_line_out != assoc) | |
4239 | continue; | |
4240 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) | |
4241 | continue; | |
4242 | cfg->line_out_pins[cfg->line_outs] = nid; | |
81937d3b | 4243 | sequences_line_out[cfg->line_outs] = seq; |
e9edcee0 TI |
4244 | cfg->line_outs++; |
4245 | break; | |
8d88bc3d | 4246 | case AC_JACK_SPEAKER: |
81937d3b SL |
4247 | seq = get_defcfg_sequence(def_conf); |
4248 | assoc = get_defcfg_association(def_conf); | |
28aedaf7 | 4249 | if (!assoc) |
81937d3b | 4250 | continue; |
28aedaf7 | 4251 | if (!assoc_speaker) |
81937d3b SL |
4252 | assoc_speaker = assoc; |
4253 | else if (assoc_speaker != assoc) | |
4254 | continue; | |
82bc955f TI |
4255 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
4256 | continue; | |
4257 | cfg->speaker_pins[cfg->speaker_outs] = nid; | |
81937d3b | 4258 | sequences_speaker[cfg->speaker_outs] = seq; |
82bc955f | 4259 | cfg->speaker_outs++; |
8d88bc3d | 4260 | break; |
e9edcee0 | 4261 | case AC_JACK_HP_OUT: |
f889fa91 TI |
4262 | seq = get_defcfg_sequence(def_conf); |
4263 | assoc = get_defcfg_association(def_conf); | |
eb06ed8f TI |
4264 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
4265 | continue; | |
4266 | cfg->hp_pins[cfg->hp_outs] = nid; | |
f889fa91 | 4267 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
eb06ed8f | 4268 | cfg->hp_outs++; |
e9edcee0 | 4269 | break; |
314634bc TI |
4270 | case AC_JACK_MIC_IN: { |
4271 | int preferred, alt; | |
6ff86a3f KY |
4272 | if (loc == AC_JACK_LOC_FRONT || |
4273 | (loc & 0x30) == AC_JACK_LOC_INTERNAL) { | |
314634bc TI |
4274 | preferred = AUTO_PIN_FRONT_MIC; |
4275 | alt = AUTO_PIN_MIC; | |
4276 | } else { | |
4277 | preferred = AUTO_PIN_MIC; | |
4278 | alt = AUTO_PIN_FRONT_MIC; | |
4279 | } | |
4280 | if (!cfg->input_pins[preferred]) | |
4281 | cfg->input_pins[preferred] = nid; | |
4282 | else if (!cfg->input_pins[alt]) | |
4283 | cfg->input_pins[alt] = nid; | |
e9edcee0 | 4284 | break; |
314634bc | 4285 | } |
e9edcee0 TI |
4286 | case AC_JACK_LINE_IN: |
4287 | if (loc == AC_JACK_LOC_FRONT) | |
4288 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid; | |
4289 | else | |
4290 | cfg->input_pins[AUTO_PIN_LINE] = nid; | |
4291 | break; | |
4292 | case AC_JACK_CD: | |
4293 | cfg->input_pins[AUTO_PIN_CD] = nid; | |
4294 | break; | |
4295 | case AC_JACK_AUX: | |
4296 | cfg->input_pins[AUTO_PIN_AUX] = nid; | |
4297 | break; | |
4298 | case AC_JACK_SPDIF_OUT: | |
1b52ae70 | 4299 | case AC_JACK_DIG_OTHER_OUT: |
0852d7a6 TI |
4300 | if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) |
4301 | continue; | |
4302 | cfg->dig_out_pins[cfg->dig_outs] = nid; | |
4303 | cfg->dig_out_type[cfg->dig_outs] = | |
4304 | (loc == AC_JACK_LOC_HDMI) ? | |
4305 | HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF; | |
4306 | cfg->dig_outs++; | |
e9edcee0 TI |
4307 | break; |
4308 | case AC_JACK_SPDIF_IN: | |
1b52ae70 | 4309 | case AC_JACK_DIG_OTHER_IN: |
e9edcee0 | 4310 | cfg->dig_in_pin = nid; |
2297bd6e TI |
4311 | if (loc == AC_JACK_LOC_HDMI) |
4312 | cfg->dig_in_type = HDA_PCM_TYPE_HDMI; | |
4313 | else | |
4314 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; | |
e9edcee0 TI |
4315 | break; |
4316 | } | |
4317 | } | |
4318 | ||
5832fcf8 TI |
4319 | /* FIX-UP: |
4320 | * If no line-out is defined but multiple HPs are found, | |
4321 | * some of them might be the real line-outs. | |
4322 | */ | |
4323 | if (!cfg->line_outs && cfg->hp_outs > 1) { | |
4324 | int i = 0; | |
4325 | while (i < cfg->hp_outs) { | |
4326 | /* The real HPs should have the sequence 0x0f */ | |
4327 | if ((sequences_hp[i] & 0x0f) == 0x0f) { | |
4328 | i++; | |
4329 | continue; | |
4330 | } | |
4331 | /* Move it to the line-out table */ | |
4332 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; | |
4333 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; | |
4334 | cfg->line_outs++; | |
4335 | cfg->hp_outs--; | |
4336 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, | |
4337 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); | |
4338 | memmove(sequences_hp + i - 1, sequences_hp + i, | |
4339 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); | |
4340 | } | |
4341 | } | |
4342 | ||
e9edcee0 | 4343 | /* sort by sequence */ |
81937d3b SL |
4344 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
4345 | cfg->line_outs); | |
4346 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, | |
4347 | cfg->speaker_outs); | |
f889fa91 TI |
4348 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
4349 | cfg->hp_outs); | |
28aedaf7 | 4350 | |
f889fa91 TI |
4351 | /* if we have only one mic, make it AUTO_PIN_MIC */ |
4352 | if (!cfg->input_pins[AUTO_PIN_MIC] && | |
4353 | cfg->input_pins[AUTO_PIN_FRONT_MIC]) { | |
4354 | cfg->input_pins[AUTO_PIN_MIC] = | |
4355 | cfg->input_pins[AUTO_PIN_FRONT_MIC]; | |
4356 | cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0; | |
4357 | } | |
4358 | /* ditto for line-in */ | |
4359 | if (!cfg->input_pins[AUTO_PIN_LINE] && | |
4360 | cfg->input_pins[AUTO_PIN_FRONT_LINE]) { | |
4361 | cfg->input_pins[AUTO_PIN_LINE] = | |
4362 | cfg->input_pins[AUTO_PIN_FRONT_LINE]; | |
4363 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0; | |
4364 | } | |
4365 | ||
81937d3b SL |
4366 | /* |
4367 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin | |
4368 | * as a primary output | |
4369 | */ | |
4370 | if (!cfg->line_outs) { | |
4371 | if (cfg->speaker_outs) { | |
4372 | cfg->line_outs = cfg->speaker_outs; | |
4373 | memcpy(cfg->line_out_pins, cfg->speaker_pins, | |
4374 | sizeof(cfg->speaker_pins)); | |
4375 | cfg->speaker_outs = 0; | |
4376 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); | |
4377 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; | |
4378 | } else if (cfg->hp_outs) { | |
4379 | cfg->line_outs = cfg->hp_outs; | |
4380 | memcpy(cfg->line_out_pins, cfg->hp_pins, | |
4381 | sizeof(cfg->hp_pins)); | |
4382 | cfg->hp_outs = 0; | |
4383 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); | |
4384 | cfg->line_out_type = AUTO_PIN_HP_OUT; | |
4385 | } | |
4386 | } | |
e9edcee0 | 4387 | |
cb8e2f83 TI |
4388 | /* Reorder the surround channels |
4389 | * ALSA sequence is front/surr/clfe/side | |
4390 | * HDA sequence is: | |
4391 | * 4-ch: front/surr => OK as it is | |
4392 | * 6-ch: front/clfe/surr | |
9422db40 | 4393 | * 8-ch: front/clfe/rear/side|fc |
cb8e2f83 TI |
4394 | */ |
4395 | switch (cfg->line_outs) { | |
4396 | case 3: | |
cb8e2f83 TI |
4397 | case 4: |
4398 | nid = cfg->line_out_pins[1]; | |
9422db40 | 4399 | cfg->line_out_pins[1] = cfg->line_out_pins[2]; |
cb8e2f83 TI |
4400 | cfg->line_out_pins[2] = nid; |
4401 | break; | |
e9edcee0 TI |
4402 | } |
4403 | ||
82bc955f TI |
4404 | /* |
4405 | * debug prints of the parsed results | |
4406 | */ | |
4407 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
4408 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], | |
4409 | cfg->line_out_pins[2], cfg->line_out_pins[3], | |
4410 | cfg->line_out_pins[4]); | |
4411 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
4412 | cfg->speaker_outs, cfg->speaker_pins[0], | |
4413 | cfg->speaker_pins[1], cfg->speaker_pins[2], | |
4414 | cfg->speaker_pins[3], cfg->speaker_pins[4]); | |
eb06ed8f TI |
4415 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
4416 | cfg->hp_outs, cfg->hp_pins[0], | |
4417 | cfg->hp_pins[1], cfg->hp_pins[2], | |
4418 | cfg->hp_pins[3], cfg->hp_pins[4]); | |
90da78bf | 4419 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
0852d7a6 TI |
4420 | if (cfg->dig_outs) |
4421 | snd_printd(" dig-out=0x%x/0x%x\n", | |
4422 | cfg->dig_out_pins[0], cfg->dig_out_pins[1]); | |
82bc955f TI |
4423 | snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x," |
4424 | " cd=0x%x, aux=0x%x\n", | |
4425 | cfg->input_pins[AUTO_PIN_MIC], | |
4426 | cfg->input_pins[AUTO_PIN_FRONT_MIC], | |
4427 | cfg->input_pins[AUTO_PIN_LINE], | |
4428 | cfg->input_pins[AUTO_PIN_FRONT_LINE], | |
4429 | cfg->input_pins[AUTO_PIN_CD], | |
4430 | cfg->input_pins[AUTO_PIN_AUX]); | |
32d2c7fa | 4431 | if (cfg->dig_in_pin) |
89ce9e87 | 4432 | snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin); |
82bc955f | 4433 | |
e9edcee0 TI |
4434 | return 0; |
4435 | } | |
ff7a3267 | 4436 | EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config); |
e9edcee0 | 4437 | |
4a471b7d TI |
4438 | /* labels for input pins */ |
4439 | const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = { | |
4440 | "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux" | |
4441 | }; | |
ff7a3267 | 4442 | EXPORT_SYMBOL_HDA(auto_pin_cfg_labels); |
4a471b7d TI |
4443 | |
4444 | ||
1da177e4 LT |
4445 | #ifdef CONFIG_PM |
4446 | /* | |
4447 | * power management | |
4448 | */ | |
4449 | ||
4450 | /** | |
4451 | * snd_hda_suspend - suspend the codecs | |
4452 | * @bus: the HDA bus | |
1da177e4 LT |
4453 | * |
4454 | * Returns 0 if successful. | |
4455 | */ | |
8dd78330 | 4456 | int snd_hda_suspend(struct hda_bus *bus) |
1da177e4 | 4457 | { |
0ba21762 | 4458 | struct hda_codec *codec; |
1da177e4 | 4459 | |
0ba21762 | 4460 | list_for_each_entry(codec, &bus->codec_list, list) { |
0b7a2e9c TI |
4461 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
4462 | if (!codec->power_on) | |
4463 | continue; | |
4464 | #endif | |
cb53c626 | 4465 | hda_call_codec_suspend(codec); |
1da177e4 LT |
4466 | } |
4467 | return 0; | |
4468 | } | |
ff7a3267 | 4469 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
1da177e4 LT |
4470 | |
4471 | /** | |
4472 | * snd_hda_resume - resume the codecs | |
4473 | * @bus: the HDA bus | |
1da177e4 LT |
4474 | * |
4475 | * Returns 0 if successful. | |
cb53c626 TI |
4476 | * |
4477 | * This fucntion is defined only when POWER_SAVE isn't set. | |
4478 | * In the power-save mode, the codec is resumed dynamically. | |
1da177e4 LT |
4479 | */ |
4480 | int snd_hda_resume(struct hda_bus *bus) | |
4481 | { | |
0ba21762 | 4482 | struct hda_codec *codec; |
1da177e4 | 4483 | |
0ba21762 | 4484 | list_for_each_entry(codec, &bus->codec_list, list) { |
d804ad92 ML |
4485 | if (snd_hda_codec_needs_resume(codec)) |
4486 | hda_call_codec_resume(codec); | |
1da177e4 | 4487 | } |
1da177e4 LT |
4488 | return 0; |
4489 | } | |
ff7a3267 | 4490 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
1289e9e8 | 4491 | #endif /* CONFIG_PM */ |
b2e18597 TI |
4492 | |
4493 | /* | |
4494 | * generic arrays | |
4495 | */ | |
4496 | ||
d5191e50 TI |
4497 | /** |
4498 | * snd_array_new - get a new element from the given array | |
4499 | * @array: the array object | |
28aedaf7 | 4500 | * |
d5191e50 TI |
4501 | * Get a new element from the given array. If it exceeds the |
4502 | * pre-allocated array size, re-allocate the array. | |
4503 | * | |
4504 | * Returns NULL if allocation failed. | |
b2e18597 TI |
4505 | */ |
4506 | void *snd_array_new(struct snd_array *array) | |
4507 | { | |
4508 | if (array->used >= array->alloced) { | |
4509 | int num = array->alloced + array->alloc_align; | |
b910d9ae TI |
4510 | void *nlist; |
4511 | if (snd_BUG_ON(num >= 4096)) | |
4512 | return NULL; | |
4513 | nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL); | |
b2e18597 TI |
4514 | if (!nlist) |
4515 | return NULL; | |
4516 | if (array->list) { | |
4517 | memcpy(nlist, array->list, | |
4518 | array->elem_size * array->alloced); | |
4519 | kfree(array->list); | |
4520 | } | |
4521 | array->list = nlist; | |
4522 | array->alloced = num; | |
4523 | } | |
f43aa025 | 4524 | return snd_array_elem(array, array->used++); |
b2e18597 | 4525 | } |
ff7a3267 | 4526 | EXPORT_SYMBOL_HDA(snd_array_new); |
b2e18597 | 4527 | |
d5191e50 TI |
4528 | /** |
4529 | * snd_array_free - free the given array elements | |
4530 | * @array: the array object | |
4531 | */ | |
b2e18597 TI |
4532 | void snd_array_free(struct snd_array *array) |
4533 | { | |
4534 | kfree(array->list); | |
4535 | array->used = 0; | |
4536 | array->alloced = 0; | |
4537 | array->list = NULL; | |
4538 | } | |
ff7a3267 | 4539 | EXPORT_SYMBOL_HDA(snd_array_free); |
b2022266 | 4540 | |
d5191e50 TI |
4541 | /** |
4542 | * snd_print_pcm_rates - Print the supported PCM rates to the string buffer | |
4543 | * @pcm: PCM caps bits | |
4544 | * @buf: the string buffer to write | |
4545 | * @buflen: the max buffer length | |
4546 | * | |
b2022266 TI |
4547 | * used by hda_proc.c and hda_eld.c |
4548 | */ | |
4549 | void snd_print_pcm_rates(int pcm, char *buf, int buflen) | |
4550 | { | |
4551 | static unsigned int rates[] = { | |
4552 | 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200, | |
4553 | 96000, 176400, 192000, 384000 | |
4554 | }; | |
4555 | int i, j; | |
4556 | ||
4557 | for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++) | |
4558 | if (pcm & (1 << i)) | |
4559 | j += snprintf(buf + j, buflen - j, " %d", rates[i]); | |
4560 | ||
4561 | buf[j] = '\0'; /* necessary when j == 0 */ | |
4562 | } | |
ff7a3267 | 4563 | EXPORT_SYMBOL_HDA(snd_print_pcm_rates); |
b2022266 | 4564 | |
d5191e50 TI |
4565 | /** |
4566 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer | |
4567 | * @pcm: PCM caps bits | |
4568 | * @buf: the string buffer to write | |
4569 | * @buflen: the max buffer length | |
4570 | * | |
4571 | * used by hda_proc.c and hda_eld.c | |
4572 | */ | |
b2022266 TI |
4573 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
4574 | { | |
4575 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; | |
4576 | int i, j; | |
4577 | ||
4578 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) | |
4579 | if (pcm & (AC_SUPPCM_BITS_8 << i)) | |
4580 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); | |
4581 | ||
4582 | buf[j] = '\0'; /* necessary when j == 0 */ | |
4583 | } | |
ff7a3267 | 4584 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
1289e9e8 TI |
4585 | |
4586 | MODULE_DESCRIPTION("HDA codec core"); | |
4587 | MODULE_LICENSE("GPL"); |