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