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