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