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