Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Universal Interface for Intel High Definition Audio Codec | |
3 | * | |
4 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> | |
5 | * | |
6 | * | |
7 | * This driver is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This driver is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
1da177e4 LT |
22 | #include <linux/init.h> |
23 | #include <linux/delay.h> | |
24 | #include <linux/slab.h> | |
62932df8 | 25 | #include <linux/mutex.h> |
da155d5b | 26 | #include <linux/module.h> |
cc72da7d TI |
27 | #include <linux/pm.h> |
28 | #include <linux/pm_runtime.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 | |
83012a7c | 40 | #ifdef CONFIG_PM |
7639a06c | 41 | #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm) |
cc72da7d TI |
42 | #define hda_codec_is_power_on(codec) \ |
43 | (!pm_runtime_suspended(hda_codec_dev(codec))) | |
cb53c626 | 44 | #else |
d846b174 | 45 | #define codec_in_pm(codec) 0 |
e581f3db | 46 | #define hda_codec_is_power_on(codec) 1 |
cb53c626 TI |
47 | #endif |
48 | ||
7639a06c TI |
49 | #define codec_has_epss(codec) \ |
50 | ((codec)->core.power_caps & AC_PWRST_EPSS) | |
51 | #define codec_has_clkstop(codec) \ | |
52 | ((codec)->core.power_caps & AC_PWRST_CLKSTOP) | |
53 | ||
aa2936f5 | 54 | /* |
05852448 | 55 | * Send and receive a verb - passed to exec_verb override for hdac_device |
aa2936f5 | 56 | */ |
05852448 TI |
57 | static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd, |
58 | unsigned int flags, unsigned int *res) | |
aa2936f5 | 59 | { |
05852448 | 60 | struct hda_codec *codec = container_of(dev, struct hda_codec, core); |
aa2936f5 | 61 | struct hda_bus *bus = codec->bus; |
8dd78330 | 62 | int err; |
aa2936f5 | 63 | |
6430aeeb WF |
64 | if (cmd == ~0) |
65 | return -1; | |
66 | ||
8dd78330 | 67 | again: |
664c7155 | 68 | snd_hda_power_up_pm(codec); |
d068ebc2 | 69 | mutex_lock(&bus->core.cmd_mutex); |
63e51fd7 TI |
70 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
71 | bus->no_response_fallback = 1; | |
d068ebc2 TI |
72 | err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr, |
73 | cmd, res); | |
63e51fd7 | 74 | bus->no_response_fallback = 0; |
d068ebc2 | 75 | mutex_unlock(&bus->core.cmd_mutex); |
664c7155 | 76 | snd_hda_power_down_pm(codec); |
cad372f1 | 77 | if (!codec_in_pm(codec) && res && err == -EAGAIN) { |
8dd78330 | 78 | if (bus->response_reset) { |
4e76a883 TI |
79 | codec_dbg(codec, |
80 | "resetting BUS due to fatal communication error\n"); | |
0a50575b | 81 | snd_hda_bus_reset(bus); |
8dd78330 TI |
82 | } |
83 | goto again; | |
84 | } | |
85 | /* clear reset-flag when the communication gets recovered */ | |
d846b174 | 86 | if (!err || codec_in_pm(codec)) |
8dd78330 | 87 | bus->response_reset = 0; |
aa2936f5 TI |
88 | return err; |
89 | } | |
90 | ||
1da177e4 LT |
91 | /** |
92 | * snd_hda_sequence_write - sequence writes | |
93 | * @codec: the HDA codec | |
94 | * @seq: VERB array to send | |
95 | * | |
96 | * Send the commands sequentially from the given array. | |
97 | * The array must be terminated with NID=0. | |
98 | */ | |
99 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) | |
100 | { | |
101 | for (; seq->nid; seq++) | |
102 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); | |
103 | } | |
2698ea98 | 104 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
1da177e4 | 105 | |
ee8e765b TI |
106 | /* connection list element */ |
107 | struct hda_conn_list { | |
108 | struct list_head list; | |
109 | int len; | |
110 | hda_nid_t nid; | |
111 | hda_nid_t conns[0]; | |
112 | }; | |
113 | ||
b2f934a0 | 114 | /* look up the cached results */ |
ee8e765b TI |
115 | static struct hda_conn_list * |
116 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) | |
b2f934a0 | 117 | { |
ee8e765b TI |
118 | struct hda_conn_list *p; |
119 | list_for_each_entry(p, &codec->conn_list, list) { | |
120 | if (p->nid == nid) | |
b2f934a0 | 121 | return p; |
b2f934a0 TI |
122 | } |
123 | return NULL; | |
124 | } | |
a12d3e1e | 125 | |
ee8e765b TI |
126 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
127 | const hda_nid_t *list) | |
128 | { | |
129 | struct hda_conn_list *p; | |
130 | ||
131 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); | |
132 | if (!p) | |
133 | return -ENOMEM; | |
134 | p->len = len; | |
135 | p->nid = nid; | |
136 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); | |
137 | list_add(&p->list, &codec->conn_list); | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static void remove_conn_list(struct hda_codec *codec) | |
142 | { | |
143 | while (!list_empty(&codec->conn_list)) { | |
144 | struct hda_conn_list *p; | |
145 | p = list_first_entry(&codec->conn_list, typeof(*p), list); | |
146 | list_del(&p->list); | |
147 | kfree(p); | |
148 | } | |
149 | } | |
150 | ||
09cf03b8 TI |
151 | /* read the connection and add to the cache */ |
152 | static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) | |
153 | { | |
4eea3091 TI |
154 | hda_nid_t list[32]; |
155 | hda_nid_t *result = list; | |
09cf03b8 TI |
156 | int len; |
157 | ||
158 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); | |
4eea3091 TI |
159 | if (len == -ENOSPC) { |
160 | len = snd_hda_get_num_raw_conns(codec, nid); | |
6da2ec56 | 161 | result = kmalloc_array(len, sizeof(hda_nid_t), GFP_KERNEL); |
4eea3091 TI |
162 | if (!result) |
163 | return -ENOMEM; | |
164 | len = snd_hda_get_raw_connections(codec, nid, result, len); | |
165 | } | |
166 | if (len >= 0) | |
167 | len = snd_hda_override_conn_list(codec, nid, len, result); | |
168 | if (result != list) | |
169 | kfree(result); | |
170 | return len; | |
09cf03b8 TI |
171 | } |
172 | ||
ee8e765b TI |
173 | /** |
174 | * snd_hda_get_conn_list - get connection list | |
175 | * @codec: the HDA codec | |
176 | * @nid: NID to parse | |
ee8e765b TI |
177 | * @listp: the pointer to store NID list |
178 | * | |
179 | * Parses the connection list of the given widget and stores the pointer | |
180 | * to the list of NIDs. | |
181 | * | |
182 | * Returns the number of connections, or a negative error code. | |
183 | * | |
184 | * Note that the returned pointer isn't protected against the list | |
185 | * modification. If snd_hda_override_conn_list() might be called | |
186 | * concurrently, protect with a mutex appropriately. | |
187 | */ | |
188 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, | |
189 | const hda_nid_t **listp) | |
190 | { | |
191 | bool added = false; | |
192 | ||
193 | for (;;) { | |
194 | int err; | |
195 | const struct hda_conn_list *p; | |
196 | ||
197 | /* if the connection-list is already cached, read it */ | |
198 | p = lookup_conn_list(codec, nid); | |
199 | if (p) { | |
200 | if (listp) | |
201 | *listp = p->conns; | |
202 | return p->len; | |
203 | } | |
204 | if (snd_BUG_ON(added)) | |
205 | return -EINVAL; | |
206 | ||
207 | err = read_and_add_raw_conns(codec, nid); | |
208 | if (err < 0) | |
209 | return err; | |
210 | added = true; | |
211 | } | |
212 | } | |
2698ea98 | 213 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
ee8e765b | 214 | |
1da177e4 | 215 | /** |
09cf03b8 | 216 | * snd_hda_get_connections - copy connection list |
1da177e4 LT |
217 | * @codec: the HDA codec |
218 | * @nid: NID to parse | |
09cf03b8 TI |
219 | * @conn_list: connection list array; when NULL, checks only the size |
220 | * @max_conns: max. number of connections to store | |
1da177e4 LT |
221 | * |
222 | * Parses the connection list of the given widget and stores the list | |
223 | * of NIDs. | |
224 | * | |
225 | * Returns the number of connections, or a negative error code. | |
226 | */ | |
09cf03b8 TI |
227 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
228 | hda_nid_t *conn_list, int max_conns) | |
a12d3e1e | 229 | { |
ee8e765b TI |
230 | const hda_nid_t *list; |
231 | int len = snd_hda_get_conn_list(codec, nid, &list); | |
a12d3e1e | 232 | |
ee8e765b TI |
233 | if (len > 0 && conn_list) { |
234 | if (len > max_conns) { | |
4e76a883 | 235 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
09cf03b8 | 236 | len, nid); |
09cf03b8 TI |
237 | return -EINVAL; |
238 | } | |
ee8e765b | 239 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
a12d3e1e TI |
240 | } |
241 | ||
ee8e765b | 242 | return len; |
a12d3e1e | 243 | } |
2698ea98 | 244 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
a12d3e1e | 245 | |
b2f934a0 TI |
246 | /** |
247 | * snd_hda_override_conn_list - add/modify the connection-list to cache | |
248 | * @codec: the HDA codec | |
249 | * @nid: NID to parse | |
250 | * @len: number of connection list entries | |
251 | * @list: the list of connection entries | |
252 | * | |
253 | * Add or modify the given connection-list to the cache. If the corresponding | |
254 | * cache already exists, invalidate it and append a new one. | |
255 | * | |
256 | * Returns zero or a negative error code. | |
257 | */ | |
258 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, | |
259 | const hda_nid_t *list) | |
260 | { | |
ee8e765b | 261 | struct hda_conn_list *p; |
b2f934a0 | 262 | |
ee8e765b TI |
263 | p = lookup_conn_list(codec, nid); |
264 | if (p) { | |
265 | list_del(&p->list); | |
266 | kfree(p); | |
267 | } | |
b2f934a0 | 268 | |
ee8e765b | 269 | return add_conn_list(codec, nid, len, list); |
b2f934a0 | 270 | } |
2698ea98 | 271 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
b2f934a0 | 272 | |
8d087c76 TI |
273 | /** |
274 | * snd_hda_get_conn_index - get the connection index of the given NID | |
275 | * @codec: the HDA codec | |
276 | * @mux: NID containing the list | |
277 | * @nid: NID to select | |
278 | * @recursive: 1 when searching NID recursively, otherwise 0 | |
279 | * | |
280 | * Parses the connection list of the widget @mux and checks whether the | |
281 | * widget @nid is present. If it is, return the connection index. | |
282 | * Otherwise it returns -1. | |
283 | */ | |
284 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, | |
285 | hda_nid_t nid, int recursive) | |
a12d3e1e | 286 | { |
ee8e765b | 287 | const hda_nid_t *conn; |
8d087c76 TI |
288 | int i, nums; |
289 | ||
ee8e765b | 290 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
8d087c76 TI |
291 | for (i = 0; i < nums; i++) |
292 | if (conn[i] == nid) | |
293 | return i; | |
294 | if (!recursive) | |
295 | return -1; | |
d94ddd85 | 296 | if (recursive > 10) { |
4e76a883 | 297 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
8d087c76 | 298 | return -1; |
a12d3e1e | 299 | } |
8d087c76 | 300 | recursive++; |
99e14c9d TI |
301 | for (i = 0; i < nums; i++) { |
302 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); | |
303 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) | |
304 | continue; | |
8d087c76 TI |
305 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
306 | return i; | |
99e14c9d | 307 | } |
8d087c76 | 308 | return -1; |
a12d3e1e | 309 | } |
2698ea98 | 310 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
1da177e4 | 311 | |
13800f39 LY |
312 | /** |
313 | * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget | |
314 | * @codec: the HDA codec | |
315 | * @nid: NID of the pin to parse | |
316 | * | |
317 | * Get the device entry number on the given widget. This is a feature of | |
318 | * DP MST audio. Each pin can have several device entries in it. | |
319 | */ | |
320 | unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid) | |
f1aa0684 ML |
321 | { |
322 | unsigned int wcaps = get_wcaps(codec, nid); | |
323 | unsigned int parm; | |
324 | ||
325 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || | |
326 | get_wcaps_type(wcaps) != AC_WID_PIN) | |
327 | return 0; | |
328 | ||
132bd96b | 329 | parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN); |
8654844c | 330 | if (parm == -1) |
f1aa0684 ML |
331 | parm = 0; |
332 | return parm & AC_DEV_LIST_LEN_MASK; | |
333 | } | |
13800f39 | 334 | EXPORT_SYMBOL_GPL(snd_hda_get_num_devices); |
f1aa0684 ML |
335 | |
336 | /** | |
337 | * snd_hda_get_devices - copy device list without cache | |
338 | * @codec: the HDA codec | |
339 | * @nid: NID of the pin to parse | |
340 | * @dev_list: device list array | |
341 | * @max_devices: max. number of devices to store | |
342 | * | |
343 | * Copy the device list. This info is dynamic and so not cached. | |
344 | * Currently called only from hda_proc.c, so not exported. | |
345 | */ | |
346 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, | |
347 | u8 *dev_list, int max_devices) | |
348 | { | |
349 | unsigned int parm; | |
350 | int i, dev_len, devices; | |
351 | ||
13800f39 | 352 | parm = snd_hda_get_num_devices(codec, nid); |
f1aa0684 ML |
353 | if (!parm) /* not multi-stream capable */ |
354 | return 0; | |
355 | ||
356 | dev_len = parm + 1; | |
357 | dev_len = dev_len < max_devices ? dev_len : max_devices; | |
358 | ||
359 | devices = 0; | |
360 | while (devices < dev_len) { | |
cad372f1 TI |
361 | if (snd_hdac_read(&codec->core, nid, |
362 | AC_VERB_GET_DEVICE_LIST, devices, &parm)) | |
363 | break; /* error */ | |
f1aa0684 ML |
364 | |
365 | for (i = 0; i < 8; i++) { | |
366 | dev_list[devices] = (u8)parm; | |
367 | parm >>= 4; | |
368 | devices++; | |
369 | if (devices >= dev_len) | |
370 | break; | |
371 | } | |
372 | } | |
373 | return devices; | |
374 | } | |
375 | ||
13800f39 LY |
376 | /** |
377 | * snd_hda_get_dev_select - get device entry select on the pin | |
378 | * @codec: the HDA codec | |
379 | * @nid: NID of the pin to get device entry select | |
380 | * | |
381 | * Get the devcie entry select on the pin. Return the device entry | |
382 | * id selected on the pin. Return 0 means the first device entry | |
383 | * is selected or MST is not supported. | |
384 | */ | |
385 | int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid) | |
386 | { | |
387 | /* not support dp_mst will always return 0, using first dev_entry */ | |
388 | if (!codec->dp_mst) | |
389 | return 0; | |
390 | ||
391 | return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0); | |
392 | } | |
393 | EXPORT_SYMBOL_GPL(snd_hda_get_dev_select); | |
394 | ||
395 | /** | |
396 | * snd_hda_set_dev_select - set device entry select on the pin | |
397 | * @codec: the HDA codec | |
398 | * @nid: NID of the pin to set device entry select | |
399 | * @dev_id: device entry id to be set | |
400 | * | |
401 | * Set the device entry select on the pin nid. | |
402 | */ | |
403 | int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id) | |
404 | { | |
405 | int ret, num_devices; | |
406 | ||
407 | /* not support dp_mst will always return 0, using first dev_entry */ | |
408 | if (!codec->dp_mst) | |
409 | return 0; | |
410 | ||
411 | /* AC_PAR_DEVLIST_LEN is 0 based. */ | |
412 | num_devices = snd_hda_get_num_devices(codec, nid) + 1; | |
413 | /* If Device List Length is 0 (num_device = 1), | |
414 | * the pin is not multi stream capable. | |
415 | * Do nothing in this case. | |
416 | */ | |
417 | if (num_devices == 1) | |
418 | return 0; | |
419 | ||
420 | /* Behavior of setting index being equal to or greater than | |
421 | * Device List Length is not predictable | |
422 | */ | |
423 | if (num_devices <= dev_id) | |
424 | return -EINVAL; | |
425 | ||
426 | ret = snd_hda_codec_write(codec, nid, 0, | |
427 | AC_VERB_SET_DEVICE_SEL, dev_id); | |
428 | ||
429 | return ret; | |
430 | } | |
431 | EXPORT_SYMBOL_GPL(snd_hda_set_dev_select); | |
432 | ||
54d17403 TI |
433 | /* |
434 | * read widget caps for each widget and store in cache | |
435 | */ | |
436 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) | |
437 | { | |
438 | int i; | |
439 | hda_nid_t nid; | |
440 | ||
6da2ec56 | 441 | codec->wcaps = kmalloc_array(codec->core.num_nodes, 4, GFP_KERNEL); |
0ba21762 | 442 | if (!codec->wcaps) |
54d17403 | 443 | return -ENOMEM; |
7639a06c TI |
444 | nid = codec->core.start_nid; |
445 | for (i = 0; i < codec->core.num_nodes; i++, nid++) | |
9ba17b4d TI |
446 | codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core, |
447 | nid, AC_PAR_AUDIO_WIDGET_CAP); | |
54d17403 TI |
448 | return 0; |
449 | } | |
450 | ||
3be14149 TI |
451 | /* read all pin default configurations and save codec->init_pins */ |
452 | static int read_pin_defaults(struct hda_codec *codec) | |
453 | { | |
7639a06c | 454 | hda_nid_t nid; |
3be14149 | 455 | |
7639a06c | 456 | for_each_hda_codec_node(nid, codec) { |
3be14149 TI |
457 | struct hda_pincfg *pin; |
458 | unsigned int wcaps = get_wcaps(codec, nid); | |
a22d543a | 459 | unsigned int wid_type = get_wcaps_type(wcaps); |
3be14149 TI |
460 | if (wid_type != AC_WID_PIN) |
461 | continue; | |
462 | pin = snd_array_new(&codec->init_pins); | |
463 | if (!pin) | |
464 | return -ENOMEM; | |
465 | pin->nid = nid; | |
466 | pin->cfg = snd_hda_codec_read(codec, nid, 0, | |
467 | AC_VERB_GET_CONFIG_DEFAULT, 0); | |
9152085d LY |
468 | /* |
469 | * all device entries are the same widget control so far | |
470 | * fixme: if any codec is different, need fix here | |
471 | */ | |
ac0547dc TI |
472 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
473 | AC_VERB_GET_PIN_WIDGET_CONTROL, | |
474 | 0); | |
3be14149 TI |
475 | } |
476 | return 0; | |
477 | } | |
478 | ||
479 | /* look up the given pin config list and return the item matching with NID */ | |
480 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, | |
481 | struct snd_array *array, | |
482 | hda_nid_t nid) | |
483 | { | |
a9c2dfc8 | 484 | struct hda_pincfg *pin; |
3be14149 | 485 | int i; |
a9c2dfc8 TI |
486 | |
487 | snd_array_for_each(array, i, pin) { | |
3be14149 TI |
488 | if (pin->nid == nid) |
489 | return pin; | |
490 | } | |
491 | return NULL; | |
492 | } | |
493 | ||
3be14149 TI |
494 | /* set the current pin config value for the given NID. |
495 | * the value is cached, and read via snd_hda_codec_get_pincfg() | |
496 | */ | |
497 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, | |
498 | hda_nid_t nid, unsigned int cfg) | |
499 | { | |
500 | struct hda_pincfg *pin; | |
501 | ||
d5657ec9 TI |
502 | /* the check below may be invalid when pins are added by a fixup |
503 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled | |
504 | * for now | |
505 | */ | |
506 | /* | |
b82855a0 TI |
507 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
508 | return -EINVAL; | |
d5657ec9 | 509 | */ |
b82855a0 | 510 | |
3be14149 TI |
511 | pin = look_up_pincfg(codec, list, nid); |
512 | if (!pin) { | |
513 | pin = snd_array_new(list); | |
514 | if (!pin) | |
515 | return -ENOMEM; | |
516 | pin->nid = nid; | |
517 | } | |
518 | pin->cfg = cfg; | |
3be14149 TI |
519 | return 0; |
520 | } | |
521 | ||
d5191e50 TI |
522 | /** |
523 | * snd_hda_codec_set_pincfg - Override a pin default configuration | |
524 | * @codec: the HDA codec | |
525 | * @nid: NID to set the pin config | |
526 | * @cfg: the pin default config value | |
527 | * | |
528 | * Override a pin default configuration value in the cache. | |
529 | * This value can be read by snd_hda_codec_get_pincfg() in a higher | |
530 | * priority than the real hardware value. | |
531 | */ | |
3be14149 TI |
532 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
533 | hda_nid_t nid, unsigned int cfg) | |
534 | { | |
346ff70f | 535 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
3be14149 | 536 | } |
2698ea98 | 537 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
3be14149 | 538 | |
d5191e50 TI |
539 | /** |
540 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration | |
541 | * @codec: the HDA codec | |
542 | * @nid: NID to get the pin config | |
543 | * | |
544 | * Get the current pin config value of the given pin NID. | |
545 | * If the pincfg value is cached or overridden via sysfs or driver, | |
546 | * returns the cached value. | |
547 | */ | |
3be14149 TI |
548 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
549 | { | |
550 | struct hda_pincfg *pin; | |
551 | ||
648a8d27 | 552 | #ifdef CONFIG_SND_HDA_RECONFIG |
09b70e85 TI |
553 | { |
554 | unsigned int cfg = 0; | |
555 | mutex_lock(&codec->user_mutex); | |
556 | pin = look_up_pincfg(codec, &codec->user_pins, nid); | |
557 | if (pin) | |
558 | cfg = pin->cfg; | |
559 | mutex_unlock(&codec->user_mutex); | |
560 | if (cfg) | |
561 | return cfg; | |
562 | } | |
3be14149 | 563 | #endif |
5e7b8e0d TI |
564 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
565 | if (pin) | |
566 | return pin->cfg; | |
3be14149 TI |
567 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
568 | if (pin) | |
569 | return pin->cfg; | |
570 | return 0; | |
571 | } | |
2698ea98 | 572 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
3be14149 | 573 | |
95a962c3 TI |
574 | /** |
575 | * snd_hda_codec_set_pin_target - remember the current pinctl target value | |
576 | * @codec: the HDA codec | |
577 | * @nid: pin NID | |
578 | * @val: assigned pinctl value | |
579 | * | |
580 | * This function stores the given value to a pinctl target value in the | |
581 | * pincfg table. This isn't always as same as the actually written value | |
582 | * but can be referred at any time via snd_hda_codec_get_pin_target(). | |
583 | */ | |
d7fdc00a TI |
584 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
585 | unsigned int val) | |
586 | { | |
587 | struct hda_pincfg *pin; | |
588 | ||
589 | pin = look_up_pincfg(codec, &codec->init_pins, nid); | |
590 | if (!pin) | |
591 | return -EINVAL; | |
592 | pin->target = val; | |
593 | return 0; | |
594 | } | |
2698ea98 | 595 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
d7fdc00a | 596 | |
95a962c3 TI |
597 | /** |
598 | * snd_hda_codec_get_pin_target - return the current pinctl target value | |
599 | * @codec: the HDA codec | |
600 | * @nid: pin NID | |
601 | */ | |
d7fdc00a TI |
602 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
603 | { | |
604 | struct hda_pincfg *pin; | |
605 | ||
606 | pin = look_up_pincfg(codec, &codec->init_pins, nid); | |
607 | if (!pin) | |
608 | return 0; | |
609 | return pin->target; | |
610 | } | |
2698ea98 | 611 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
d7fdc00a | 612 | |
92ee6162 TI |
613 | /** |
614 | * snd_hda_shutup_pins - Shut up all pins | |
615 | * @codec: the HDA codec | |
616 | * | |
617 | * Clear all pin controls to shup up before suspend for avoiding click noise. | |
618 | * The controls aren't cached so that they can be resumed properly. | |
619 | */ | |
620 | void snd_hda_shutup_pins(struct hda_codec *codec) | |
621 | { | |
a9c2dfc8 | 622 | const struct hda_pincfg *pin; |
92ee6162 | 623 | int i; |
a9c2dfc8 | 624 | |
ac0547dc TI |
625 | /* don't shut up pins when unloading the driver; otherwise it breaks |
626 | * the default pin setup at the next load of the driver | |
627 | */ | |
628 | if (codec->bus->shutdown) | |
629 | return; | |
a9c2dfc8 | 630 | snd_array_for_each(&codec->init_pins, i, pin) { |
92ee6162 TI |
631 | /* use read here for syncing after issuing each verb */ |
632 | snd_hda_codec_read(codec, pin->nid, 0, | |
633 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); | |
634 | } | |
ac0547dc | 635 | codec->pins_shutup = 1; |
92ee6162 | 636 | } |
2698ea98 | 637 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
92ee6162 | 638 | |
2a43952a | 639 | #ifdef CONFIG_PM |
ac0547dc TI |
640 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
641 | static void restore_shutup_pins(struct hda_codec *codec) | |
642 | { | |
a9c2dfc8 | 643 | const struct hda_pincfg *pin; |
ac0547dc | 644 | int i; |
a9c2dfc8 | 645 | |
ac0547dc TI |
646 | if (!codec->pins_shutup) |
647 | return; | |
648 | if (codec->bus->shutdown) | |
649 | return; | |
a9c2dfc8 | 650 | snd_array_for_each(&codec->init_pins, i, pin) { |
ac0547dc TI |
651 | snd_hda_codec_write(codec, pin->nid, 0, |
652 | AC_VERB_SET_PIN_WIDGET_CONTROL, | |
653 | pin->ctrl); | |
654 | } | |
655 | codec->pins_shutup = 0; | |
656 | } | |
1c7276cf | 657 | #endif |
ac0547dc | 658 | |
26a6cb6c DH |
659 | static void hda_jackpoll_work(struct work_struct *work) |
660 | { | |
661 | struct hda_codec *codec = | |
662 | container_of(work, struct hda_codec, jackpoll_work.work); | |
26a6cb6c DH |
663 | |
664 | snd_hda_jack_set_dirty_all(codec); | |
665 | snd_hda_jack_poll_all(codec); | |
18e60627 WX |
666 | |
667 | if (!codec->jackpoll_interval) | |
668 | return; | |
669 | ||
2f35c630 TI |
670 | schedule_delayed_work(&codec->jackpoll_work, |
671 | codec->jackpoll_interval); | |
26a6cb6c DH |
672 | } |
673 | ||
3fdf1469 TI |
674 | /* release all pincfg lists */ |
675 | static void free_init_pincfgs(struct hda_codec *codec) | |
3be14149 | 676 | { |
346ff70f | 677 | snd_array_free(&codec->driver_pins); |
648a8d27 | 678 | #ifdef CONFIG_SND_HDA_RECONFIG |
346ff70f | 679 | snd_array_free(&codec->user_pins); |
3be14149 | 680 | #endif |
3be14149 TI |
681 | snd_array_free(&codec->init_pins); |
682 | } | |
683 | ||
eb541337 TI |
684 | /* |
685 | * audio-converter setup caches | |
686 | */ | |
687 | struct hda_cvt_setup { | |
688 | hda_nid_t nid; | |
689 | u8 stream_tag; | |
690 | u8 channel_id; | |
691 | u16 format_id; | |
692 | unsigned char active; /* cvt is currently used */ | |
693 | unsigned char dirty; /* setups should be cleared */ | |
694 | }; | |
695 | ||
696 | /* get or create a cache entry for the given audio converter NID */ | |
697 | static struct hda_cvt_setup * | |
698 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) | |
699 | { | |
700 | struct hda_cvt_setup *p; | |
701 | int i; | |
702 | ||
a9c2dfc8 | 703 | snd_array_for_each(&codec->cvt_setups, i, p) { |
eb541337 TI |
704 | if (p->nid == nid) |
705 | return p; | |
706 | } | |
707 | p = snd_array_new(&codec->cvt_setups); | |
708 | if (p) | |
709 | p->nid = nid; | |
710 | return p; | |
711 | } | |
712 | ||
bbbc7e85 TI |
713 | /* |
714 | * PCM device | |
715 | */ | |
716 | static void release_pcm(struct kref *kref) | |
717 | { | |
718 | struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref); | |
719 | ||
720 | if (pcm->pcm) | |
721 | snd_device_free(pcm->codec->card, pcm->pcm); | |
722 | clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits); | |
723 | kfree(pcm->name); | |
724 | kfree(pcm); | |
725 | } | |
726 | ||
727 | void snd_hda_codec_pcm_put(struct hda_pcm *pcm) | |
728 | { | |
729 | kref_put(&pcm->kref, release_pcm); | |
730 | } | |
731 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put); | |
732 | ||
733 | struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec, | |
734 | const char *fmt, ...) | |
735 | { | |
736 | struct hda_pcm *pcm; | |
737 | va_list args; | |
738 | ||
bbbc7e85 TI |
739 | pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); |
740 | if (!pcm) | |
741 | return NULL; | |
742 | ||
743 | pcm->codec = codec; | |
744 | kref_init(&pcm->kref); | |
30e5f003 | 745 | va_start(args, fmt); |
bbbc7e85 | 746 | pcm->name = kvasprintf(GFP_KERNEL, fmt, args); |
30e5f003 | 747 | va_end(args); |
bbbc7e85 TI |
748 | if (!pcm->name) { |
749 | kfree(pcm); | |
750 | return NULL; | |
751 | } | |
752 | ||
753 | list_add_tail(&pcm->list, &codec->pcm_list_head); | |
754 | return pcm; | |
755 | } | |
756 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new); | |
757 | ||
9a6246ff TI |
758 | /* |
759 | * codec destructor | |
760 | */ | |
bbbc7e85 TI |
761 | static void codec_release_pcms(struct hda_codec *codec) |
762 | { | |
763 | struct hda_pcm *pcm, *n; | |
764 | ||
765 | list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) { | |
766 | list_del_init(&pcm->list); | |
9a6246ff TI |
767 | if (pcm->pcm) |
768 | snd_device_disconnect(codec->card, pcm->pcm); | |
bbbc7e85 TI |
769 | snd_hda_codec_pcm_put(pcm); |
770 | } | |
771 | } | |
772 | ||
9a6246ff TI |
773 | void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec) |
774 | { | |
c4c2533f TI |
775 | if (codec->registered) { |
776 | /* pm_runtime_put() is called in snd_hdac_device_exit() */ | |
777 | pm_runtime_get_noresume(hda_codec_dev(codec)); | |
778 | pm_runtime_disable(hda_codec_dev(codec)); | |
779 | codec->registered = 0; | |
780 | } | |
781 | ||
9a6246ff | 782 | cancel_delayed_work_sync(&codec->jackpoll_work); |
9a6246ff TI |
783 | if (!codec->in_freeing) |
784 | snd_hda_ctls_clear(codec); | |
785 | codec_release_pcms(codec); | |
786 | snd_hda_detach_beep_device(codec); | |
787 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); | |
788 | snd_hda_jack_tbl_clear(codec); | |
789 | codec->proc_widget_hook = NULL; | |
790 | codec->spec = NULL; | |
791 | ||
9a6246ff TI |
792 | /* free only driver_pins so that init_pins + user_pins are restored */ |
793 | snd_array_free(&codec->driver_pins); | |
794 | snd_array_free(&codec->cvt_setups); | |
795 | snd_array_free(&codec->spdif_out); | |
796 | snd_array_free(&codec->verbs); | |
797 | codec->preset = NULL; | |
798 | codec->slave_dig_outs = NULL; | |
799 | codec->spdif_status_reset = 0; | |
800 | snd_array_free(&codec->mixers); | |
801 | snd_array_free(&codec->nids); | |
802 | remove_conn_list(codec); | |
4d75faa0 | 803 | snd_hdac_regmap_exit(&codec->core); |
9a6246ff TI |
804 | } |
805 | ||
d819387e | 806 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
bb6ac72f TI |
807 | unsigned int power_state); |
808 | ||
c4c2533f TI |
809 | /* also called from hda_bind.c */ |
810 | void snd_hda_codec_register(struct hda_codec *codec) | |
13aeaf68 | 811 | { |
c4c2533f TI |
812 | if (codec->registered) |
813 | return; | |
814 | if (device_is_registered(hda_codec_dev(codec))) { | |
815 | snd_hda_register_beep_device(codec); | |
a5e7e07c | 816 | snd_hdac_link_power(&codec->core, true); |
cc72da7d | 817 | pm_runtime_enable(hda_codec_dev(codec)); |
c4c2533f TI |
818 | /* it was powered up in snd_hda_codec_new(), now all done */ |
819 | snd_hda_power_down(codec); | |
820 | codec->registered = 1; | |
821 | } | |
822 | } | |
823 | ||
824 | static int snd_hda_codec_dev_register(struct snd_device *device) | |
825 | { | |
826 | snd_hda_codec_register(device->device_data); | |
d604b399 | 827 | return 0; |
13aeaf68 TI |
828 | } |
829 | ||
830 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) | |
831 | { | |
832 | struct hda_codec *codec = device->device_data; | |
833 | ||
d604b399 | 834 | snd_hda_detach_beep_device(codec); |
13aeaf68 TI |
835 | return 0; |
836 | } | |
837 | ||
2565c899 TI |
838 | static int snd_hda_codec_dev_free(struct snd_device *device) |
839 | { | |
d56db741 TI |
840 | struct hda_codec *codec = device->device_data; |
841 | ||
842 | codec->in_freeing = 1; | |
3256be65 | 843 | snd_hdac_device_unregister(&codec->core); |
a457782a | 844 | snd_hdac_link_power(&codec->core, false); |
d56db741 | 845 | put_device(hda_codec_dev(codec)); |
2565c899 TI |
846 | return 0; |
847 | } | |
848 | ||
13aeaf68 TI |
849 | static void snd_hda_codec_dev_release(struct device *dev) |
850 | { | |
d56db741 TI |
851 | struct hda_codec *codec = dev_to_hda_codec(dev); |
852 | ||
853 | free_init_pincfgs(codec); | |
7639a06c | 854 | snd_hdac_device_exit(&codec->core); |
d56db741 | 855 | snd_hda_sysfs_clear(codec); |
d56db741 TI |
856 | kfree(codec->modelname); |
857 | kfree(codec->wcaps); | |
d56db741 | 858 | kfree(codec); |
13aeaf68 TI |
859 | } |
860 | ||
1da177e4 LT |
861 | /** |
862 | * snd_hda_codec_new - create a HDA codec | |
863 | * @bus: the bus to assign | |
864 | * @codec_addr: the codec address | |
865 | * @codecp: the pointer to store the generated codec | |
866 | * | |
867 | * Returns 0 if successful, or a negative error code. | |
868 | */ | |
6efdd851 TI |
869 | int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card, |
870 | unsigned int codec_addr, struct hda_codec **codecp) | |
1da177e4 LT |
871 | { |
872 | struct hda_codec *codec; | |
ba443687 | 873 | char component[31]; |
d819387e | 874 | hda_nid_t fg; |
1da177e4 | 875 | int err; |
2565c899 | 876 | static struct snd_device_ops dev_ops = { |
13aeaf68 TI |
877 | .dev_register = snd_hda_codec_dev_register, |
878 | .dev_disconnect = snd_hda_codec_dev_disconnect, | |
2565c899 TI |
879 | .dev_free = snd_hda_codec_dev_free, |
880 | }; | |
1da177e4 | 881 | |
da3cec35 TI |
882 | if (snd_BUG_ON(!bus)) |
883 | return -EINVAL; | |
884 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) | |
885 | return -EINVAL; | |
1da177e4 | 886 | |
e560d8d8 | 887 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
f4de8fe6 | 888 | if (!codec) |
1da177e4 | 889 | return -ENOMEM; |
1da177e4 | 890 | |
7639a06c TI |
891 | sprintf(component, "hdaudioC%dD%d", card->number, codec_addr); |
892 | err = snd_hdac_device_init(&codec->core, &bus->core, component, | |
893 | codec_addr); | |
894 | if (err < 0) { | |
895 | kfree(codec); | |
896 | return err; | |
897 | } | |
d068ebc2 | 898 | |
7639a06c TI |
899 | codec->core.dev.release = snd_hda_codec_dev_release; |
900 | codec->core.type = HDA_DEV_LEGACY; | |
05852448 | 901 | codec->core.exec_verb = codec_exec_verb; |
13aeaf68 | 902 | |
1da177e4 | 903 | codec->bus = bus; |
6efdd851 | 904 | codec->card = card; |
1da177e4 | 905 | codec->addr = codec_addr; |
62932df8 | 906 | mutex_init(&codec->spdif_mutex); |
5a9e02e9 | 907 | mutex_init(&codec->control_mutex); |
5b0cb1d8 JK |
908 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
909 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); | |
3be14149 | 910 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
346ff70f | 911 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
eb541337 | 912 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
7c935976 | 913 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
361dab3e | 914 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
c9ce6b26 | 915 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
ee8e765b | 916 | INIT_LIST_HEAD(&codec->conn_list); |
bbbc7e85 | 917 | INIT_LIST_HEAD(&codec->pcm_list_head); |
ee8e765b | 918 | |
26a6cb6c | 919 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
7f132927 | 920 | codec->depop_delay = -1; |
f5662e1c | 921 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
1da177e4 | 922 | |
83012a7c | 923 | #ifdef CONFIG_PM |
cc72da7d | 924 | codec->power_jiffies = jiffies; |
cb53c626 TI |
925 | #endif |
926 | ||
648a8d27 TI |
927 | snd_hda_sysfs_init(codec); |
928 | ||
c382a9f0 TI |
929 | if (codec->bus->modelname) { |
930 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); | |
931 | if (!codec->modelname) { | |
6986a0e2 | 932 | err = -ENOMEM; |
13aeaf68 | 933 | goto error; |
c382a9f0 TI |
934 | } |
935 | } | |
936 | ||
7639a06c | 937 | fg = codec->core.afg ? codec->core.afg : codec->core.mfg; |
d819387e | 938 | err = read_widget_caps(codec, fg); |
f4de8fe6 | 939 | if (err < 0) |
3be14149 | 940 | goto error; |
3be14149 TI |
941 | err = read_pin_defaults(codec); |
942 | if (err < 0) | |
943 | goto error; | |
54d17403 | 944 | |
bb6ac72f | 945 | /* power-up all before initialization */ |
d819387e | 946 | hda_set_power_state(codec, AC_PWRST_D0); |
bb6ac72f | 947 | |
6c1f45ea TI |
948 | snd_hda_codec_proc_new(codec); |
949 | ||
6c1f45ea | 950 | snd_hda_create_hwdep(codec); |
6c1f45ea | 951 | |
7639a06c TI |
952 | sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id, |
953 | codec->core.subsystem_id, codec->core.revision_id); | |
6efdd851 | 954 | snd_component_add(card, component); |
6c1f45ea | 955 | |
6efdd851 | 956 | err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops); |
2565c899 TI |
957 | if (err < 0) |
958 | goto error; | |
959 | ||
6c1f45ea TI |
960 | if (codecp) |
961 | *codecp = codec; | |
962 | return 0; | |
3be14149 TI |
963 | |
964 | error: | |
d56db741 | 965 | put_device(hda_codec_dev(codec)); |
3be14149 | 966 | return err; |
6c1f45ea | 967 | } |
2698ea98 | 968 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
6c1f45ea | 969 | |
95a962c3 TI |
970 | /** |
971 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults | |
972 | * @codec: the HDA codec | |
973 | * | |
974 | * Forcibly refresh the all widget caps and the init pin configurations of | |
975 | * the given codec. | |
976 | */ | |
a15d05db ML |
977 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
978 | { | |
979 | hda_nid_t fg; | |
980 | int err; | |
981 | ||
9780ded3 | 982 | err = snd_hdac_refresh_widgets(&codec->core, true); |
7639a06c TI |
983 | if (err < 0) |
984 | return err; | |
985 | ||
a15d05db ML |
986 | /* Assume the function group node does not change, |
987 | * only the widget nodes may change. | |
988 | */ | |
989 | kfree(codec->wcaps); | |
7639a06c | 990 | fg = codec->core.afg ? codec->core.afg : codec->core.mfg; |
a15d05db | 991 | err = read_widget_caps(codec, fg); |
f4de8fe6 | 992 | if (err < 0) |
a15d05db | 993 | return err; |
a15d05db ML |
994 | |
995 | snd_array_free(&codec->init_pins); | |
996 | err = read_pin_defaults(codec); | |
997 | ||
998 | return err; | |
999 | } | |
2698ea98 | 1000 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
a15d05db | 1001 | |
ed360813 TI |
1002 | /* update the stream-id if changed */ |
1003 | static void update_pcm_stream_id(struct hda_codec *codec, | |
1004 | struct hda_cvt_setup *p, hda_nid_t nid, | |
1005 | u32 stream_tag, int channel_id) | |
1006 | { | |
1007 | unsigned int oldval, newval; | |
1008 | ||
1009 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { | |
1010 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); | |
1011 | newval = (stream_tag << 4) | channel_id; | |
1012 | if (oldval != newval) | |
1013 | snd_hda_codec_write(codec, nid, 0, | |
1014 | AC_VERB_SET_CHANNEL_STREAMID, | |
1015 | newval); | |
1016 | p->stream_tag = stream_tag; | |
1017 | p->channel_id = channel_id; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | /* update the format-id if changed */ | |
1022 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, | |
1023 | hda_nid_t nid, int format) | |
1024 | { | |
1025 | unsigned int oldval; | |
1026 | ||
1027 | if (p->format_id != format) { | |
1028 | oldval = snd_hda_codec_read(codec, nid, 0, | |
1029 | AC_VERB_GET_STREAM_FORMAT, 0); | |
1030 | if (oldval != format) { | |
1031 | msleep(1); | |
1032 | snd_hda_codec_write(codec, nid, 0, | |
1033 | AC_VERB_SET_STREAM_FORMAT, | |
1034 | format); | |
1035 | } | |
1036 | p->format_id = format; | |
1037 | } | |
1038 | } | |
1039 | ||
1da177e4 LT |
1040 | /** |
1041 | * snd_hda_codec_setup_stream - set up the codec for streaming | |
1042 | * @codec: the CODEC to set up | |
1043 | * @nid: the NID to set up | |
1044 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. | |
1045 | * @channel_id: channel id to pass, zero based. | |
1046 | * @format: stream format. | |
1047 | */ | |
0ba21762 TI |
1048 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
1049 | u32 stream_tag, | |
1da177e4 LT |
1050 | int channel_id, int format) |
1051 | { | |
3f50ac6a | 1052 | struct hda_codec *c; |
eb541337 | 1053 | struct hda_cvt_setup *p; |
62b7e5e0 | 1054 | int type; |
eb541337 TI |
1055 | int i; |
1056 | ||
0ba21762 | 1057 | if (!nid) |
d21b37ea TI |
1058 | return; |
1059 | ||
4e76a883 TI |
1060 | codec_dbg(codec, |
1061 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", | |
1062 | nid, stream_tag, channel_id, format); | |
eb541337 | 1063 | p = get_hda_cvt_setup(codec, nid); |
6c35ae3c | 1064 | if (!p) |
eb541337 | 1065 | return; |
ed360813 | 1066 | |
e6feb5d0 TI |
1067 | if (codec->patch_ops.stream_pm) |
1068 | codec->patch_ops.stream_pm(codec, nid, true); | |
ed360813 TI |
1069 | if (codec->pcm_format_first) |
1070 | update_pcm_format(codec, p, nid, format); | |
1071 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); | |
1072 | if (!codec->pcm_format_first) | |
1073 | update_pcm_format(codec, p, nid, format); | |
1074 | ||
eb541337 TI |
1075 | p->active = 1; |
1076 | p->dirty = 0; | |
1077 | ||
1078 | /* make other inactive cvts with the same stream-tag dirty */ | |
62b7e5e0 | 1079 | type = get_wcaps_type(get_wcaps(codec, nid)); |
d068ebc2 | 1080 | list_for_each_codec(c, codec->bus) { |
a9c2dfc8 | 1081 | snd_array_for_each(&c->cvt_setups, i, p) { |
62b7e5e0 | 1082 | if (!p->active && p->stream_tag == stream_tag && |
54c2a89f | 1083 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
3f50ac6a TI |
1084 | p->dirty = 1; |
1085 | } | |
eb541337 | 1086 | } |
1da177e4 | 1087 | } |
2698ea98 | 1088 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
1da177e4 | 1089 | |
f0cea797 TI |
1090 | static void really_cleanup_stream(struct hda_codec *codec, |
1091 | struct hda_cvt_setup *q); | |
1092 | ||
d5191e50 | 1093 | /** |
f0cea797 | 1094 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
d5191e50 TI |
1095 | * @codec: the CODEC to clean up |
1096 | * @nid: the NID to clean up | |
f0cea797 | 1097 | * @do_now: really clean up the stream instead of clearing the active flag |
d5191e50 | 1098 | */ |
f0cea797 TI |
1099 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
1100 | int do_now) | |
888afa15 | 1101 | { |
eb541337 TI |
1102 | struct hda_cvt_setup *p; |
1103 | ||
888afa15 TI |
1104 | if (!nid) |
1105 | return; | |
1106 | ||
0e7adbe2 TI |
1107 | if (codec->no_sticky_stream) |
1108 | do_now = 1; | |
1109 | ||
4e76a883 | 1110 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
eb541337 | 1111 | p = get_hda_cvt_setup(codec, nid); |
6c35ae3c | 1112 | if (p) { |
f0cea797 TI |
1113 | /* here we just clear the active flag when do_now isn't set; |
1114 | * actual clean-ups will be done later in | |
1115 | * purify_inactive_streams() called from snd_hda_codec_prpapre() | |
1116 | */ | |
1117 | if (do_now) | |
1118 | really_cleanup_stream(codec, p); | |
1119 | else | |
1120 | p->active = 0; | |
1121 | } | |
eb541337 | 1122 | } |
2698ea98 | 1123 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
eb541337 TI |
1124 | |
1125 | static void really_cleanup_stream(struct hda_codec *codec, | |
1126 | struct hda_cvt_setup *q) | |
1127 | { | |
1128 | hda_nid_t nid = q->nid; | |
218264ae TI |
1129 | if (q->stream_tag || q->channel_id) |
1130 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); | |
1131 | if (q->format_id) | |
1132 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 | |
1133 | ); | |
eb541337 TI |
1134 | memset(q, 0, sizeof(*q)); |
1135 | q->nid = nid; | |
e6feb5d0 TI |
1136 | if (codec->patch_ops.stream_pm) |
1137 | codec->patch_ops.stream_pm(codec, nid, false); | |
eb541337 TI |
1138 | } |
1139 | ||
1140 | /* clean up the all conflicting obsolete streams */ | |
1141 | static void purify_inactive_streams(struct hda_codec *codec) | |
1142 | { | |
3f50ac6a | 1143 | struct hda_codec *c; |
a9c2dfc8 | 1144 | struct hda_cvt_setup *p; |
eb541337 TI |
1145 | int i; |
1146 | ||
d068ebc2 | 1147 | list_for_each_codec(c, codec->bus) { |
a9c2dfc8 | 1148 | snd_array_for_each(&c->cvt_setups, i, p) { |
3f50ac6a TI |
1149 | if (p->dirty) |
1150 | really_cleanup_stream(c, p); | |
1151 | } | |
eb541337 TI |
1152 | } |
1153 | } | |
1154 | ||
2a43952a | 1155 | #ifdef CONFIG_PM |
eb541337 TI |
1156 | /* clean up all streams; called from suspend */ |
1157 | static void hda_cleanup_all_streams(struct hda_codec *codec) | |
1158 | { | |
a9c2dfc8 | 1159 | struct hda_cvt_setup *p; |
eb541337 TI |
1160 | int i; |
1161 | ||
a9c2dfc8 | 1162 | snd_array_for_each(&codec->cvt_setups, i, p) { |
eb541337 TI |
1163 | if (p->stream_tag) |
1164 | really_cleanup_stream(codec, p); | |
1165 | } | |
888afa15 | 1166 | } |
1c7276cf | 1167 | #endif |
888afa15 | 1168 | |
1da177e4 LT |
1169 | /* |
1170 | * amp access functions | |
1171 | */ | |
1172 | ||
d5191e50 TI |
1173 | /** |
1174 | * query_amp_caps - query AMP capabilities | |
1175 | * @codec: the HD-auio codec | |
1176 | * @nid: the NID to query | |
1177 | * @direction: either #HDA_INPUT or #HDA_OUTPUT | |
1178 | * | |
1179 | * Query AMP capabilities for the given widget and direction. | |
1180 | * Returns the obtained capability bits. | |
1181 | * | |
1182 | * When cap bits have been already read, this doesn't read again but | |
1183 | * returns the cached value. | |
1da177e4 | 1184 | */ |
09a99959 | 1185 | u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) |
1da177e4 | 1186 | { |
faa75f8a TI |
1187 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
1188 | nid = codec->core.afg; | |
1189 | return snd_hda_param_read(codec, nid, | |
1190 | direction == HDA_OUTPUT ? | |
1191 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); | |
1da177e4 | 1192 | } |
2698ea98 | 1193 | EXPORT_SYMBOL_GPL(query_amp_caps); |
1da177e4 | 1194 | |
861a04ed DH |
1195 | /** |
1196 | * snd_hda_check_amp_caps - query AMP capabilities | |
1197 | * @codec: the HD-audio codec | |
1198 | * @nid: the NID to query | |
1199 | * @dir: either #HDA_INPUT or #HDA_OUTPUT | |
a11e9b16 | 1200 | * @bits: bit mask to check the result |
861a04ed DH |
1201 | * |
1202 | * Check whether the widget has the given amp capability for the direction. | |
1203 | */ | |
1204 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, | |
1205 | int dir, unsigned int bits) | |
1206 | { | |
1207 | if (!nid) | |
1208 | return false; | |
1209 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) | |
1210 | if (query_amp_caps(codec, nid, dir) & bits) | |
1211 | return true; | |
1212 | return false; | |
1213 | } | |
1214 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); | |
1215 | ||
d5191e50 TI |
1216 | /** |
1217 | * snd_hda_override_amp_caps - Override the AMP capabilities | |
1218 | * @codec: the CODEC to clean up | |
1219 | * @nid: the NID to clean up | |
a11e9b16 | 1220 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
d5191e50 TI |
1221 | * @caps: the capability bits to set |
1222 | * | |
1223 | * Override the cached AMP caps bits value by the given one. | |
1224 | * This function is useful if the driver needs to adjust the AMP ranges, | |
1225 | * e.g. limit to 0dB, etc. | |
1226 | * | |
1227 | * Returns zero if successful or a negative error code. | |
1228 | */ | |
897cc188 TI |
1229 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
1230 | unsigned int caps) | |
1231 | { | |
faa75f8a | 1232 | unsigned int parm; |
897cc188 | 1233 | |
faa75f8a TI |
1234 | snd_hda_override_wcaps(codec, nid, |
1235 | get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD); | |
1236 | parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP; | |
1237 | return snd_hdac_override_parm(&codec->core, nid, parm, caps); | |
f57c2565 | 1238 | } |
faa75f8a | 1239 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
f57c2565 | 1240 | |
a686ec4c TI |
1241 | /** |
1242 | * snd_hda_codec_amp_update - update the AMP mono value | |
1243 | * @codec: HD-audio codec | |
1244 | * @nid: NID to read the AMP value | |
1245 | * @ch: channel to update (0 or 1) | |
1246 | * @dir: #HDA_INPUT or #HDA_OUTPUT | |
1247 | * @idx: the index value (only for input direction) | |
1248 | * @mask: bit mask to set | |
1249 | * @val: the bits value to set | |
1250 | * | |
1251 | * Update the AMP values for the given channel, direction and index. | |
1252 | */ | |
1253 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, | |
1254 | int ch, int dir, int idx, int mask, int val) | |
1255 | { | |
1256 | unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx); | |
1257 | ||
1258 | /* enable fake mute if no h/w mute but min=mute */ | |
1259 | if ((query_amp_caps(codec, nid, dir) & | |
1260 | (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE) | |
1261 | cmd |= AC_AMP_FAKE_MUTE; | |
1262 | return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val); | |
1263 | } | |
1264 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); | |
1265 | ||
d5191e50 TI |
1266 | /** |
1267 | * snd_hda_codec_amp_stereo - update the AMP stereo values | |
1268 | * @codec: HD-audio codec | |
1269 | * @nid: NID to read the AMP value | |
1270 | * @direction: #HDA_INPUT or #HDA_OUTPUT | |
1271 | * @idx: the index value (only for input direction) | |
1272 | * @mask: bit mask to set | |
1273 | * @val: the bits value to set | |
1274 | * | |
1275 | * Update the AMP values like snd_hda_codec_amp_update(), but for a | |
1276 | * stereo widget with the same mask and value. | |
47fd830a TI |
1277 | */ |
1278 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, | |
1279 | int direction, int idx, int mask, int val) | |
1280 | { | |
1281 | int ch, ret = 0; | |
46712646 TI |
1282 | |
1283 | if (snd_BUG_ON(mask & ~0xff)) | |
1284 | mask &= 0xff; | |
47fd830a TI |
1285 | for (ch = 0; ch < 2; ch++) |
1286 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, | |
1287 | idx, mask, val); | |
1288 | return ret; | |
1289 | } | |
2698ea98 | 1290 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
47fd830a | 1291 | |
95a962c3 TI |
1292 | /** |
1293 | * snd_hda_codec_amp_init - initialize the AMP value | |
1294 | * @codec: the HDA codec | |
1295 | * @nid: NID to read the AMP value | |
1296 | * @ch: channel (left=0 or right=1) | |
1297 | * @dir: #HDA_INPUT or #HDA_OUTPUT | |
1298 | * @idx: the index value (only for input direction) | |
1299 | * @mask: bit mask to set | |
1300 | * @val: the bits value to set | |
1301 | * | |
1302 | * Works like snd_hda_codec_amp_update() but it writes the value only at | |
280e57d5 TI |
1303 | * the first access. If the amp was already initialized / updated beforehand, |
1304 | * this does nothing. | |
1305 | */ | |
1306 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, | |
1307 | int dir, int idx, int mask, int val) | |
1308 | { | |
eeecd9d1 TI |
1309 | int orig; |
1310 | ||
1311 | if (!codec->core.regmap) | |
1312 | return -EINVAL; | |
1313 | regcache_cache_only(codec->core.regmap, true); | |
1314 | orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); | |
1315 | regcache_cache_only(codec->core.regmap, false); | |
1316 | if (orig >= 0) | |
1317 | return 0; | |
1318 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val); | |
280e57d5 | 1319 | } |
2698ea98 | 1320 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
280e57d5 | 1321 | |
95a962c3 TI |
1322 | /** |
1323 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value | |
1324 | * @codec: the HDA codec | |
1325 | * @nid: NID to read the AMP value | |
1326 | * @dir: #HDA_INPUT or #HDA_OUTPUT | |
1327 | * @idx: the index value (only for input direction) | |
1328 | * @mask: bit mask to set | |
1329 | * @val: the bits value to set | |
1330 | * | |
1331 | * Call snd_hda_codec_amp_init() for both stereo channels. | |
1332 | */ | |
280e57d5 TI |
1333 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
1334 | int dir, int idx, int mask, int val) | |
1335 | { | |
1336 | int ch, ret = 0; | |
1337 | ||
1338 | if (snd_BUG_ON(mask & ~0xff)) | |
1339 | mask &= 0xff; | |
1340 | for (ch = 0; ch < 2; ch++) | |
1341 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, | |
1342 | idx, mask, val); | |
1343 | return ret; | |
1344 | } | |
2698ea98 | 1345 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
280e57d5 | 1346 | |
afbd9b84 TI |
1347 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
1348 | unsigned int ofs) | |
1349 | { | |
1350 | u32 caps = query_amp_caps(codec, nid, dir); | |
1351 | /* get num steps */ | |
1352 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | |
1353 | if (ofs < caps) | |
1354 | caps -= ofs; | |
1355 | return caps; | |
1356 | } | |
1357 | ||
d5191e50 TI |
1358 | /** |
1359 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer | |
a11e9b16 TI |
1360 | * @kcontrol: referred ctl element |
1361 | * @uinfo: pointer to get/store the data | |
d5191e50 TI |
1362 | * |
1363 | * The control element is supposed to have the private_value field | |
1364 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1365 | */ | |
0ba21762 TI |
1366 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
1367 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
1368 | { |
1369 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1370 | u16 nid = get_amp_nid(kcontrol); | |
1371 | u8 chs = get_amp_channels(kcontrol); | |
1372 | int dir = get_amp_direction(kcontrol); | |
29fdbec2 | 1373 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 | 1374 | |
afbd9b84 TI |
1375 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
1376 | uinfo->count = chs == 3 ? 2 : 1; | |
1377 | uinfo->value.integer.min = 0; | |
1378 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); | |
1379 | if (!uinfo->value.integer.max) { | |
4e76a883 TI |
1380 | codec_warn(codec, |
1381 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", | |
1382 | nid, kcontrol->id.name); | |
1da177e4 LT |
1383 | return -EINVAL; |
1384 | } | |
1da177e4 LT |
1385 | return 0; |
1386 | } | |
2698ea98 | 1387 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
1da177e4 | 1388 | |
29fdbec2 TI |
1389 | |
1390 | static inline unsigned int | |
1391 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, | |
1392 | int ch, int dir, int idx, unsigned int ofs) | |
1393 | { | |
1394 | unsigned int val; | |
1395 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); | |
1396 | val &= HDA_AMP_VOLMASK; | |
1397 | if (val >= ofs) | |
1398 | val -= ofs; | |
1399 | else | |
1400 | val = 0; | |
1401 | return val; | |
1402 | } | |
1403 | ||
1404 | static inline int | |
1405 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, | |
1406 | int ch, int dir, int idx, unsigned int ofs, | |
1407 | unsigned int val) | |
1408 | { | |
afbd9b84 TI |
1409 | unsigned int maxval; |
1410 | ||
29fdbec2 TI |
1411 | if (val > 0) |
1412 | val += ofs; | |
7ccc3efa TI |
1413 | /* ofs = 0: raw max value */ |
1414 | maxval = get_amp_max_value(codec, nid, dir, 0); | |
afbd9b84 TI |
1415 | if (val > maxval) |
1416 | val = maxval; | |
eeecd9d1 TI |
1417 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
1418 | HDA_AMP_VOLMASK, val); | |
29fdbec2 TI |
1419 | } |
1420 | ||
d5191e50 TI |
1421 | /** |
1422 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume | |
a11e9b16 TI |
1423 | * @kcontrol: ctl element |
1424 | * @ucontrol: pointer to get/store the data | |
d5191e50 TI |
1425 | * |
1426 | * The control element is supposed to have the private_value field | |
1427 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1428 | */ | |
0ba21762 TI |
1429 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
1430 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
1431 | { |
1432 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1433 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1434 | int chs = get_amp_channels(kcontrol); | |
1435 | int dir = get_amp_direction(kcontrol); | |
1436 | int idx = get_amp_index(kcontrol); | |
29fdbec2 | 1437 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 LT |
1438 | long *valp = ucontrol->value.integer.value; |
1439 | ||
1440 | if (chs & 1) | |
29fdbec2 | 1441 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
1da177e4 | 1442 | if (chs & 2) |
29fdbec2 | 1443 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
1da177e4 LT |
1444 | return 0; |
1445 | } | |
2698ea98 | 1446 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
1da177e4 | 1447 | |
d5191e50 TI |
1448 | /** |
1449 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume | |
a11e9b16 TI |
1450 | * @kcontrol: ctl element |
1451 | * @ucontrol: pointer to get/store the data | |
d5191e50 TI |
1452 | * |
1453 | * The control element is supposed to have the private_value field | |
1454 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1455 | */ | |
0ba21762 TI |
1456 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
1457 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
1458 | { |
1459 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1460 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1461 | int chs = get_amp_channels(kcontrol); | |
1462 | int dir = get_amp_direction(kcontrol); | |
1463 | int idx = get_amp_index(kcontrol); | |
29fdbec2 | 1464 | unsigned int ofs = get_amp_offset(kcontrol); |
1da177e4 LT |
1465 | long *valp = ucontrol->value.integer.value; |
1466 | int change = 0; | |
1467 | ||
b9f5a89c | 1468 | if (chs & 1) { |
29fdbec2 | 1469 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
b9f5a89c NG |
1470 | valp++; |
1471 | } | |
4a19faee | 1472 | if (chs & 2) |
29fdbec2 | 1473 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
1da177e4 LT |
1474 | return change; |
1475 | } | |
2698ea98 | 1476 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
1da177e4 | 1477 | |
99b5c5bb TI |
1478 | /* inquiry the amp caps and convert to TLV */ |
1479 | static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv) | |
302e9c5a JK |
1480 | { |
1481 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
1482 | hda_nid_t nid = get_amp_nid(kcontrol); | |
1483 | int dir = get_amp_direction(kcontrol); | |
29fdbec2 | 1484 | unsigned int ofs = get_amp_offset(kcontrol); |
de8c85f7 | 1485 | bool min_mute = get_amp_min_mute(kcontrol); |
302e9c5a JK |
1486 | u32 caps, val1, val2; |
1487 | ||
302e9c5a | 1488 | caps = query_amp_caps(codec, nid, dir); |
0ba21762 TI |
1489 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
1490 | val2 = (val2 + 1) * 25; | |
302e9c5a | 1491 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
29fdbec2 | 1492 | val1 += ofs; |
302e9c5a | 1493 | val1 = ((int)val1) * ((int)val2); |
3868137e | 1494 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
c08d9169 | 1495 | val2 |= TLV_DB_SCALE_MUTE; |
51cdc8b6 TS |
1496 | tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE; |
1497 | tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int); | |
1498 | tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = val1; | |
1499 | tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = val2; | |
99b5c5bb TI |
1500 | } |
1501 | ||
1502 | /** | |
f0049e16 | 1503 | * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume |
99b5c5bb TI |
1504 | * @kcontrol: ctl element |
1505 | * @op_flag: operation flag | |
1506 | * @size: byte size of input TLV | |
1507 | * @_tlv: TLV data | |
1508 | * | |
1509 | * The control element is supposed to have the private_value field | |
1510 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
1511 | */ | |
1512 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, | |
1513 | unsigned int size, unsigned int __user *_tlv) | |
1514 | { | |
1515 | unsigned int tlv[4]; | |
1516 | ||
1517 | if (size < 4 * sizeof(unsigned int)) | |
1518 | return -ENOMEM; | |
1519 | get_ctl_amp_tlv(kcontrol, tlv); | |
1520 | if (copy_to_user(_tlv, tlv, sizeof(tlv))) | |
302e9c5a JK |
1521 | return -EFAULT; |
1522 | return 0; | |
1523 | } | |
2698ea98 | 1524 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
302e9c5a | 1525 | |
d5191e50 TI |
1526 | /** |
1527 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control | |
1528 | * @codec: HD-audio codec | |
1529 | * @nid: NID of a reference widget | |
1530 | * @dir: #HDA_INPUT or #HDA_OUTPUT | |
1531 | * @tlv: TLV data to be stored, at least 4 elements | |
1532 | * | |
1533 | * Set (static) TLV data for a virtual master volume using the AMP caps | |
1534 | * obtained from the reference NID. | |
1535 | * The volume range is recalculated as if the max volume is 0dB. | |
2134ea4f TI |
1536 | */ |
1537 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, | |
1538 | unsigned int *tlv) | |
1539 | { | |
1540 | u32 caps; | |
1541 | int nums, step; | |
1542 | ||
1543 | caps = query_amp_caps(codec, nid, dir); | |
1544 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | |
1545 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; | |
1546 | step = (step + 1) * 25; | |
51cdc8b6 TS |
1547 | tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE; |
1548 | tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int); | |
1549 | tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = -nums * step; | |
1550 | tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = step; | |
2134ea4f | 1551 | } |
2698ea98 | 1552 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
2134ea4f TI |
1553 | |
1554 | /* find a mixer control element with the given name */ | |
09f99701 | 1555 | static struct snd_kcontrol * |
dcda5806 | 1556 | find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) |
2134ea4f TI |
1557 | { |
1558 | struct snd_ctl_elem_id id; | |
1559 | memset(&id, 0, sizeof(id)); | |
1560 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
dcda5806 | 1561 | id.device = dev; |
09f99701 | 1562 | id.index = idx; |
18cb7109 TI |
1563 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
1564 | return NULL; | |
2134ea4f | 1565 | strcpy(id.name, name); |
6efdd851 | 1566 | return snd_ctl_find_id(codec->card, &id); |
2134ea4f TI |
1567 | } |
1568 | ||
d5191e50 TI |
1569 | /** |
1570 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name | |
1571 | * @codec: HD-audio codec | |
1572 | * @name: ctl id name string | |
1573 | * | |
1574 | * Get the control element with the given id string and IFACE_MIXER. | |
1575 | */ | |
09f99701 TI |
1576 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
1577 | const char *name) | |
1578 | { | |
dcda5806 | 1579 | return find_mixer_ctl(codec, name, 0, 0); |
09f99701 | 1580 | } |
2698ea98 | 1581 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
09f99701 | 1582 | |
dcda5806 | 1583 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, |
ea9b43ad | 1584 | int start_idx) |
1afe206a | 1585 | { |
ea9b43ad TI |
1586 | int i, idx; |
1587 | /* 16 ctlrs should be large enough */ | |
1588 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { | |
1589 | if (!find_mixer_ctl(codec, name, 0, idx)) | |
1afe206a TI |
1590 | return idx; |
1591 | } | |
1592 | return -EBUSY; | |
1593 | } | |
1594 | ||
d5191e50 | 1595 | /** |
5b0cb1d8 | 1596 | * snd_hda_ctl_add - Add a control element and assign to the codec |
d5191e50 TI |
1597 | * @codec: HD-audio codec |
1598 | * @nid: corresponding NID (optional) | |
1599 | * @kctl: the control element to assign | |
1600 | * | |
1601 | * Add the given control element to an array inside the codec instance. | |
1602 | * All control elements belonging to a codec are supposed to be added | |
1603 | * by this function so that a proper clean-up works at the free or | |
1604 | * reconfiguration time. | |
1605 | * | |
1606 | * If non-zero @nid is passed, the NID is assigned to the control element. | |
1607 | * The assignment is shown in the codec proc file. | |
1608 | * | |
1609 | * snd_hda_ctl_add() checks the control subdev id field whether | |
1610 | * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower | |
9e3fd871 JK |
1611 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
1612 | * specifies if kctl->private_value is a HDA amplifier value. | |
d5191e50 | 1613 | */ |
3911a4c1 JK |
1614 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
1615 | struct snd_kcontrol *kctl) | |
d13bd412 TI |
1616 | { |
1617 | int err; | |
9e3fd871 | 1618 | unsigned short flags = 0; |
3911a4c1 | 1619 | struct hda_nid_item *item; |
d13bd412 | 1620 | |
5e26dfd0 | 1621 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
9e3fd871 | 1622 | flags |= HDA_NID_ITEM_AMP; |
5e26dfd0 JK |
1623 | if (nid == 0) |
1624 | nid = get_amp_nid_(kctl->private_value); | |
1625 | } | |
9e3fd871 JK |
1626 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
1627 | nid = kctl->id.subdevice & 0xffff; | |
5e26dfd0 | 1628 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
4d02d1b6 | 1629 | kctl->id.subdevice = 0; |
6efdd851 | 1630 | err = snd_ctl_add(codec->card, kctl); |
d13bd412 TI |
1631 | if (err < 0) |
1632 | return err; | |
3911a4c1 JK |
1633 | item = snd_array_new(&codec->mixers); |
1634 | if (!item) | |
d13bd412 | 1635 | return -ENOMEM; |
3911a4c1 JK |
1636 | item->kctl = kctl; |
1637 | item->nid = nid; | |
9e3fd871 | 1638 | item->flags = flags; |
d13bd412 TI |
1639 | return 0; |
1640 | } | |
2698ea98 | 1641 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
d13bd412 | 1642 | |
5b0cb1d8 JK |
1643 | /** |
1644 | * snd_hda_add_nid - Assign a NID to a control element | |
1645 | * @codec: HD-audio codec | |
1646 | * @nid: corresponding NID (optional) | |
1647 | * @kctl: the control element to assign | |
1648 | * @index: index to kctl | |
1649 | * | |
1650 | * Add the given control element to an array inside the codec instance. | |
1651 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 | |
1652 | * NID:KCTL mapping - for example "Capture Source" selector. | |
1653 | */ | |
1654 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, | |
1655 | unsigned int index, hda_nid_t nid) | |
1656 | { | |
1657 | struct hda_nid_item *item; | |
1658 | ||
1659 | if (nid > 0) { | |
1660 | item = snd_array_new(&codec->nids); | |
1661 | if (!item) | |
1662 | return -ENOMEM; | |
1663 | item->kctl = kctl; | |
1664 | item->index = index; | |
1665 | item->nid = nid; | |
1666 | return 0; | |
1667 | } | |
4e76a883 TI |
1668 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
1669 | kctl->id.name, kctl->id.index, index); | |
5b0cb1d8 JK |
1670 | return -EINVAL; |
1671 | } | |
2698ea98 | 1672 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
5b0cb1d8 | 1673 | |
d5191e50 TI |
1674 | /** |
1675 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec | |
1676 | * @codec: HD-audio codec | |
1677 | */ | |
d13bd412 TI |
1678 | void snd_hda_ctls_clear(struct hda_codec *codec) |
1679 | { | |
1680 | int i; | |
3911a4c1 | 1681 | struct hda_nid_item *items = codec->mixers.list; |
d13bd412 | 1682 | for (i = 0; i < codec->mixers.used; i++) |
6efdd851 | 1683 | snd_ctl_remove(codec->card, items[i].kctl); |
d13bd412 | 1684 | snd_array_free(&codec->mixers); |
5b0cb1d8 | 1685 | snd_array_free(&codec->nids); |
d13bd412 TI |
1686 | } |
1687 | ||
95a962c3 TI |
1688 | /** |
1689 | * snd_hda_lock_devices - pseudo device locking | |
1690 | * @bus: the BUS | |
1691 | * | |
a65d629c TI |
1692 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
1693 | */ | |
d3d020bd | 1694 | int snd_hda_lock_devices(struct hda_bus *bus) |
6c1f45ea | 1695 | { |
d3d020bd TI |
1696 | struct snd_card *card = bus->card; |
1697 | struct hda_codec *codec; | |
1698 | ||
a65d629c | 1699 | spin_lock(&card->files_lock); |
d3d020bd TI |
1700 | if (card->shutdown) |
1701 | goto err_unlock; | |
a65d629c | 1702 | card->shutdown = 1; |
d3d020bd TI |
1703 | if (!list_empty(&card->ctl_files)) |
1704 | goto err_clear; | |
1705 | ||
d068ebc2 | 1706 | list_for_each_codec(codec, bus) { |
bbbc7e85 TI |
1707 | struct hda_pcm *cpcm; |
1708 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { | |
d3d020bd TI |
1709 | if (!cpcm->pcm) |
1710 | continue; | |
1711 | if (cpcm->pcm->streams[0].substream_opened || | |
1712 | cpcm->pcm->streams[1].substream_opened) | |
1713 | goto err_clear; | |
1714 | } | |
1715 | } | |
a65d629c TI |
1716 | spin_unlock(&card->files_lock); |
1717 | return 0; | |
d3d020bd TI |
1718 | |
1719 | err_clear: | |
1720 | card->shutdown = 0; | |
1721 | err_unlock: | |
1722 | spin_unlock(&card->files_lock); | |
1723 | return -EINVAL; | |
a65d629c | 1724 | } |
2698ea98 | 1725 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
a65d629c | 1726 | |
95a962c3 TI |
1727 | /** |
1728 | * snd_hda_unlock_devices - pseudo device unlocking | |
1729 | * @bus: the BUS | |
1730 | */ | |
d3d020bd | 1731 | void snd_hda_unlock_devices(struct hda_bus *bus) |
a65d629c | 1732 | { |
d3d020bd TI |
1733 | struct snd_card *card = bus->card; |
1734 | ||
a65d629c TI |
1735 | spin_lock(&card->files_lock); |
1736 | card->shutdown = 0; | |
1737 | spin_unlock(&card->files_lock); | |
1738 | } | |
2698ea98 | 1739 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
a65d629c | 1740 | |
d5191e50 TI |
1741 | /** |
1742 | * snd_hda_codec_reset - Clear all objects assigned to the codec | |
1743 | * @codec: HD-audio codec | |
1744 | * | |
1745 | * This frees the all PCM and control elements assigned to the codec, and | |
1746 | * clears the caches and restores the pin default configurations. | |
1747 | * | |
1748 | * When a device is being used, it returns -EBSY. If successfully freed, | |
1749 | * returns zero. | |
1750 | */ | |
a65d629c TI |
1751 | int snd_hda_codec_reset(struct hda_codec *codec) |
1752 | { | |
d3d020bd | 1753 | struct hda_bus *bus = codec->bus; |
a65d629c | 1754 | |
d3d020bd | 1755 | if (snd_hda_lock_devices(bus) < 0) |
a65d629c | 1756 | return -EBUSY; |
a65d629c TI |
1757 | |
1758 | /* OK, let it free */ | |
3256be65 | 1759 | snd_hdac_device_unregister(&codec->core); |
d8a766a1 | 1760 | |
a65d629c | 1761 | /* allow device access again */ |
d3d020bd | 1762 | snd_hda_unlock_devices(bus); |
a65d629c | 1763 | return 0; |
6c1f45ea TI |
1764 | } |
1765 | ||
6194b99d | 1766 | typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); |
aeb4b88e TI |
1767 | |
1768 | /* apply the function to all matching slave ctls in the mixer list */ | |
1769 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, | |
9322ca54 | 1770 | const char *suffix, map_slave_func_t func, void *data) |
aeb4b88e TI |
1771 | { |
1772 | struct hda_nid_item *items; | |
1773 | const char * const *s; | |
1774 | int i, err; | |
1775 | ||
1776 | items = codec->mixers.list; | |
1777 | for (i = 0; i < codec->mixers.used; i++) { | |
1778 | struct snd_kcontrol *sctl = items[i].kctl; | |
ca16ec02 | 1779 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
aeb4b88e TI |
1780 | continue; |
1781 | for (s = slaves; *s; s++) { | |
9322ca54 TI |
1782 | char tmpname[sizeof(sctl->id.name)]; |
1783 | const char *name = *s; | |
1784 | if (suffix) { | |
1785 | snprintf(tmpname, sizeof(tmpname), "%s %s", | |
1786 | name, suffix); | |
1787 | name = tmpname; | |
1788 | } | |
9322ca54 | 1789 | if (!strcmp(sctl->id.name, name)) { |
6194b99d | 1790 | err = func(codec, data, sctl); |
aeb4b88e TI |
1791 | if (err) |
1792 | return err; | |
1793 | break; | |
1794 | } | |
1795 | } | |
1796 | } | |
1797 | return 0; | |
1798 | } | |
1799 | ||
6194b99d TI |
1800 | static int check_slave_present(struct hda_codec *codec, |
1801 | void *data, struct snd_kcontrol *sctl) | |
aeb4b88e TI |
1802 | { |
1803 | return 1; | |
1804 | } | |
1805 | ||
18478e8b TI |
1806 | /* call kctl->put with the given value(s) */ |
1807 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) | |
1808 | { | |
1809 | struct snd_ctl_elem_value *ucontrol; | |
1810 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); | |
1811 | if (!ucontrol) | |
1812 | return -ENOMEM; | |
1813 | ucontrol->value.integer.value[0] = val; | |
1814 | ucontrol->value.integer.value[1] = val; | |
1815 | kctl->put(kctl, ucontrol); | |
1816 | kfree(ucontrol); | |
1817 | return 0; | |
1818 | } | |
1819 | ||
a91d6612 TI |
1820 | struct slave_init_arg { |
1821 | struct hda_codec *codec; | |
1822 | int step; | |
1823 | }; | |
1824 | ||
1825 | /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */ | |
d6c0615f TI |
1826 | static int init_slave_0dB(struct snd_kcontrol *slave, |
1827 | struct snd_kcontrol *kctl, | |
1828 | void *_arg) | |
18478e8b | 1829 | { |
a91d6612 TI |
1830 | struct slave_init_arg *arg = _arg; |
1831 | int _tlv[4]; | |
1832 | const int *tlv = NULL; | |
1833 | int step; | |
1834 | int val; | |
1835 | ||
1836 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { | |
1837 | if (kctl->tlv.c != snd_hda_mixer_amp_tlv) { | |
1838 | codec_err(arg->codec, | |
1839 | "Unexpected TLV callback for slave %s:%d\n", | |
1840 | kctl->id.name, kctl->id.index); | |
1841 | return 0; /* ignore */ | |
1842 | } | |
1843 | get_ctl_amp_tlv(kctl, _tlv); | |
1844 | tlv = _tlv; | |
1845 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) | |
1846 | tlv = kctl->tlv.p; | |
1847 | ||
51cdc8b6 | 1848 | if (!tlv || tlv[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE) |
a91d6612 TI |
1849 | return 0; |
1850 | ||
51cdc8b6 | 1851 | step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP]; |
a91d6612 TI |
1852 | step &= ~TLV_DB_SCALE_MUTE; |
1853 | if (!step) | |
1854 | return 0; | |
1855 | if (arg->step && arg->step != step) { | |
1856 | codec_err(arg->codec, | |
1857 | "Mismatching dB step for vmaster slave (%d!=%d)\n", | |
1858 | arg->step, step); | |
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | arg->step = step; | |
51cdc8b6 | 1863 | val = -tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] / step; |
a91d6612 | 1864 | if (val > 0) { |
d6c0615f | 1865 | put_kctl_with_value(slave, val); |
a91d6612 TI |
1866 | return val; |
1867 | } | |
1868 | ||
18478e8b TI |
1869 | return 0; |
1870 | } | |
1871 | ||
a91d6612 | 1872 | /* unmute the slave via snd_ctl_apply_vmaster_slaves() */ |
d6c0615f TI |
1873 | static int init_slave_unmute(struct snd_kcontrol *slave, |
1874 | struct snd_kcontrol *kctl, | |
1875 | void *_arg) | |
18478e8b TI |
1876 | { |
1877 | return put_kctl_with_value(slave, 1); | |
1878 | } | |
1879 | ||
e8750940 TI |
1880 | static int add_slave(struct hda_codec *codec, |
1881 | void *data, struct snd_kcontrol *slave) | |
1882 | { | |
1883 | return snd_ctl_add_slave(data, slave); | |
1884 | } | |
1885 | ||
d5191e50 | 1886 | /** |
95a962c3 | 1887 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
d5191e50 TI |
1888 | * @codec: HD-audio codec |
1889 | * @name: vmaster control name | |
1890 | * @tlv: TLV data (optional) | |
1891 | * @slaves: slave control names (optional) | |
9322ca54 | 1892 | * @suffix: suffix string to each slave name (optional) |
18478e8b | 1893 | * @init_slave_vol: initialize slaves to unmute/0dB |
29e5853d | 1894 | * @ctl_ret: store the vmaster kcontrol in return |
d5191e50 TI |
1895 | * |
1896 | * Create a virtual master control with the given name. The TLV data | |
1897 | * must be either NULL or a valid data. | |
1898 | * | |
1899 | * @slaves is a NULL-terminated array of strings, each of which is a | |
1900 | * slave control name. All controls with these names are assigned to | |
1901 | * the new virtual master control. | |
1902 | * | |
1903 | * This function returns zero if successful or a negative error code. | |
1904 | */ | |
18478e8b | 1905 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
9322ca54 | 1906 | unsigned int *tlv, const char * const *slaves, |
29e5853d TI |
1907 | const char *suffix, bool init_slave_vol, |
1908 | struct snd_kcontrol **ctl_ret) | |
2134ea4f TI |
1909 | { |
1910 | struct snd_kcontrol *kctl; | |
2134ea4f TI |
1911 | int err; |
1912 | ||
29e5853d TI |
1913 | if (ctl_ret) |
1914 | *ctl_ret = NULL; | |
1915 | ||
9322ca54 | 1916 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
aeb4b88e | 1917 | if (err != 1) { |
4e76a883 | 1918 | codec_dbg(codec, "No slave found for %s\n", name); |
2f085549 TI |
1919 | return 0; |
1920 | } | |
2134ea4f TI |
1921 | kctl = snd_ctl_make_virtual_master(name, tlv); |
1922 | if (!kctl) | |
1923 | return -ENOMEM; | |
3911a4c1 | 1924 | err = snd_hda_ctl_add(codec, 0, kctl); |
2134ea4f TI |
1925 | if (err < 0) |
1926 | return err; | |
28aedaf7 | 1927 | |
e8750940 | 1928 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
aeb4b88e TI |
1929 | if (err < 0) |
1930 | return err; | |
18478e8b TI |
1931 | |
1932 | /* init with master mute & zero volume */ | |
1933 | put_kctl_with_value(kctl, 0); | |
485e3e0c | 1934 | if (init_slave_vol) { |
a91d6612 TI |
1935 | struct slave_init_arg arg = { |
1936 | .codec = codec, | |
1937 | .step = 0, | |
1938 | }; | |
1939 | snd_ctl_apply_vmaster_slaves(kctl, | |
1940 | tlv ? init_slave_0dB : init_slave_unmute, | |
1941 | &arg); | |
485e3e0c | 1942 | } |
18478e8b | 1943 | |
29e5853d TI |
1944 | if (ctl_ret) |
1945 | *ctl_ret = kctl; | |
2134ea4f TI |
1946 | return 0; |
1947 | } | |
2698ea98 | 1948 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
2134ea4f | 1949 | |
d2f344b5 TI |
1950 | /* |
1951 | * mute-LED control using vmaster | |
1952 | */ | |
1953 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, | |
1954 | struct snd_ctl_elem_info *uinfo) | |
1955 | { | |
1956 | static const char * const texts[] = { | |
c86c2d44 | 1957 | "On", "Off", "Follow Master" |
d2f344b5 | 1958 | }; |
d2f344b5 | 1959 | |
3ff72219 | 1960 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
d2f344b5 TI |
1961 | } |
1962 | ||
1963 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, | |
1964 | struct snd_ctl_elem_value *ucontrol) | |
1965 | { | |
1966 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); | |
1967 | ucontrol->value.enumerated.item[0] = hook->mute_mode; | |
1968 | return 0; | |
1969 | } | |
1970 | ||
1971 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, | |
1972 | struct snd_ctl_elem_value *ucontrol) | |
1973 | { | |
1974 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); | |
1975 | unsigned int old_mode = hook->mute_mode; | |
1976 | ||
1977 | hook->mute_mode = ucontrol->value.enumerated.item[0]; | |
1978 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) | |
1979 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; | |
1980 | if (old_mode == hook->mute_mode) | |
1981 | return 0; | |
1982 | snd_hda_sync_vmaster_hook(hook); | |
1983 | return 1; | |
1984 | } | |
1985 | ||
f3b827e0 | 1986 | static const struct snd_kcontrol_new vmaster_mute_mode = { |
d2f344b5 TI |
1987 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1988 | .name = "Mute-LED Mode", | |
1989 | .info = vmaster_mute_mode_info, | |
1990 | .get = vmaster_mute_mode_get, | |
1991 | .put = vmaster_mute_mode_put, | |
1992 | }; | |
1993 | ||
ee52e56e TI |
1994 | /* meta hook to call each driver's vmaster hook */ |
1995 | static void vmaster_hook(void *private_data, int enabled) | |
1996 | { | |
1997 | struct hda_vmaster_mute_hook *hook = private_data; | |
1998 | ||
1999 | if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER) | |
2000 | enabled = hook->mute_mode; | |
2001 | hook->hook(hook->codec, enabled); | |
2002 | } | |
2003 | ||
95a962c3 TI |
2004 | /** |
2005 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED | |
2006 | * @codec: the HDA codec | |
2007 | * @hook: the vmaster hook object | |
2008 | * @expose_enum_ctl: flag to create an enum ctl | |
2009 | * | |
2010 | * Add a mute-LED hook with the given vmaster switch kctl. | |
2011 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically | |
2012 | * created and associated with the given hook. | |
d2f344b5 TI |
2013 | */ |
2014 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, | |
f29735cb TI |
2015 | struct hda_vmaster_mute_hook *hook, |
2016 | bool expose_enum_ctl) | |
d2f344b5 TI |
2017 | { |
2018 | struct snd_kcontrol *kctl; | |
2019 | ||
2020 | if (!hook->hook || !hook->sw_kctl) | |
2021 | return 0; | |
d2f344b5 TI |
2022 | hook->codec = codec; |
2023 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; | |
ee52e56e | 2024 | snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook); |
f29735cb TI |
2025 | if (!expose_enum_ctl) |
2026 | return 0; | |
d2f344b5 TI |
2027 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
2028 | if (!kctl) | |
2029 | return -ENOMEM; | |
2030 | return snd_hda_ctl_add(codec, 0, kctl); | |
2031 | } | |
2698ea98 | 2032 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
d2f344b5 | 2033 | |
95a962c3 TI |
2034 | /** |
2035 | * snd_hda_sync_vmaster_hook - Sync vmaster hook | |
2036 | * @hook: the vmaster hook | |
2037 | * | |
2038 | * Call the hook with the current value for synchronization. | |
2039 | * Should be called in init callback. | |
d2f344b5 TI |
2040 | */ |
2041 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) | |
2042 | { | |
2043 | if (!hook->hook || !hook->codec) | |
2044 | return; | |
594813ff TI |
2045 | /* don't call vmaster hook in the destructor since it might have |
2046 | * been already destroyed | |
2047 | */ | |
2048 | if (hook->codec->bus->shutdown) | |
2049 | return; | |
ee52e56e | 2050 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
d2f344b5 | 2051 | } |
2698ea98 | 2052 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
d2f344b5 TI |
2053 | |
2054 | ||
d5191e50 TI |
2055 | /** |
2056 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch | |
a11e9b16 TI |
2057 | * @kcontrol: referred ctl element |
2058 | * @uinfo: pointer to get/store the data | |
d5191e50 TI |
2059 | * |
2060 | * The control element is supposed to have the private_value field | |
2061 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2062 | */ | |
0ba21762 TI |
2063 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
2064 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
2065 | { |
2066 | int chs = get_amp_channels(kcontrol); | |
2067 | ||
2068 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
2069 | uinfo->count = chs == 3 ? 2 : 1; | |
2070 | uinfo->value.integer.min = 0; | |
2071 | uinfo->value.integer.max = 1; | |
2072 | return 0; | |
2073 | } | |
2698ea98 | 2074 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
1da177e4 | 2075 | |
d5191e50 TI |
2076 | /** |
2077 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch | |
a11e9b16 TI |
2078 | * @kcontrol: ctl element |
2079 | * @ucontrol: pointer to get/store the data | |
d5191e50 TI |
2080 | * |
2081 | * The control element is supposed to have the private_value field | |
2082 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2083 | */ | |
0ba21762 TI |
2084 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
2085 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2086 | { |
2087 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2088 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2089 | int chs = get_amp_channels(kcontrol); | |
2090 | int dir = get_amp_direction(kcontrol); | |
2091 | int idx = get_amp_index(kcontrol); | |
2092 | long *valp = ucontrol->value.integer.value; | |
2093 | ||
2094 | if (chs & 1) | |
0ba21762 | 2095 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
47fd830a | 2096 | HDA_AMP_MUTE) ? 0 : 1; |
1da177e4 | 2097 | if (chs & 2) |
0ba21762 | 2098 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
47fd830a | 2099 | HDA_AMP_MUTE) ? 0 : 1; |
1da177e4 LT |
2100 | return 0; |
2101 | } | |
2698ea98 | 2102 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
1da177e4 | 2103 | |
d5191e50 TI |
2104 | /** |
2105 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch | |
a11e9b16 TI |
2106 | * @kcontrol: ctl element |
2107 | * @ucontrol: pointer to get/store the data | |
d5191e50 TI |
2108 | * |
2109 | * The control element is supposed to have the private_value field | |
2110 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | |
2111 | */ | |
0ba21762 TI |
2112 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
2113 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2114 | { |
2115 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2116 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2117 | int chs = get_amp_channels(kcontrol); | |
2118 | int dir = get_amp_direction(kcontrol); | |
2119 | int idx = get_amp_index(kcontrol); | |
1da177e4 LT |
2120 | long *valp = ucontrol->value.integer.value; |
2121 | int change = 0; | |
2122 | ||
b9f5a89c | 2123 | if (chs & 1) { |
eeecd9d1 TI |
2124 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
2125 | HDA_AMP_MUTE, | |
2126 | *valp ? 0 : HDA_AMP_MUTE); | |
b9f5a89c NG |
2127 | valp++; |
2128 | } | |
4a19faee | 2129 | if (chs & 2) |
eeecd9d1 TI |
2130 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
2131 | HDA_AMP_MUTE, | |
2132 | *valp ? 0 : HDA_AMP_MUTE); | |
9e5341b9 | 2133 | hda_call_check_power_status(codec, nid); |
1da177e4 LT |
2134 | return change; |
2135 | } | |
2698ea98 | 2136 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
1da177e4 LT |
2137 | |
2138 | /* | |
2139 | * SPDIF out controls | |
2140 | */ | |
2141 | ||
0ba21762 TI |
2142 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
2143 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
2144 | { |
2145 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | |
2146 | uinfo->count = 1; | |
2147 | return 0; | |
2148 | } | |
2149 | ||
0ba21762 TI |
2150 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
2151 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2152 | { |
2153 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
2154 | IEC958_AES0_NONAUDIO | | |
2155 | IEC958_AES0_CON_EMPHASIS_5015 | | |
2156 | IEC958_AES0_CON_NOT_COPYRIGHT; | |
2157 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | | |
2158 | IEC958_AES1_CON_ORIGINAL; | |
2159 | return 0; | |
2160 | } | |
2161 | ||
0ba21762 TI |
2162 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
2163 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2164 | { |
2165 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | |
2166 | IEC958_AES0_NONAUDIO | | |
2167 | IEC958_AES0_PRO_EMPHASIS_5015; | |
2168 | return 0; | |
2169 | } | |
2170 | ||
0ba21762 TI |
2171 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
2172 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2173 | { |
2174 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
7c935976 | 2175 | int idx = kcontrol->private_value; |
e3245cdd | 2176 | struct hda_spdif_out *spdif; |
1da177e4 | 2177 | |
08605068 TI |
2178 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2179 | return -EINVAL; | |
e3245cdd TI |
2180 | mutex_lock(&codec->spdif_mutex); |
2181 | spdif = snd_array_elem(&codec->spdif_out, idx); | |
7c935976 SW |
2182 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
2183 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; | |
2184 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; | |
2185 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; | |
e3245cdd | 2186 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2187 | |
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | /* convert from SPDIF status bits to HDA SPDIF bits | |
2192 | * bit 0 (DigEn) is always set zero (to be filled later) | |
2193 | */ | |
2194 | static unsigned short convert_from_spdif_status(unsigned int sbits) | |
2195 | { | |
2196 | unsigned short val = 0; | |
2197 | ||
2198 | if (sbits & IEC958_AES0_PROFESSIONAL) | |
0ba21762 | 2199 | val |= AC_DIG1_PROFESSIONAL; |
1da177e4 | 2200 | if (sbits & IEC958_AES0_NONAUDIO) |
0ba21762 | 2201 | val |= AC_DIG1_NONAUDIO; |
1da177e4 | 2202 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
0ba21762 TI |
2203 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
2204 | IEC958_AES0_PRO_EMPHASIS_5015) | |
2205 | val |= AC_DIG1_EMPHASIS; | |
1da177e4 | 2206 | } else { |
0ba21762 TI |
2207 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
2208 | IEC958_AES0_CON_EMPHASIS_5015) | |
2209 | val |= AC_DIG1_EMPHASIS; | |
2210 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) | |
2211 | val |= AC_DIG1_COPYRIGHT; | |
1da177e4 | 2212 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
0ba21762 | 2213 | val |= AC_DIG1_LEVEL; |
1da177e4 LT |
2214 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
2215 | } | |
2216 | return val; | |
2217 | } | |
2218 | ||
2219 | /* convert to SPDIF status bits from HDA SPDIF bits | |
2220 | */ | |
2221 | static unsigned int convert_to_spdif_status(unsigned short val) | |
2222 | { | |
2223 | unsigned int sbits = 0; | |
2224 | ||
0ba21762 | 2225 | if (val & AC_DIG1_NONAUDIO) |
1da177e4 | 2226 | sbits |= IEC958_AES0_NONAUDIO; |
0ba21762 | 2227 | if (val & AC_DIG1_PROFESSIONAL) |
1da177e4 LT |
2228 | sbits |= IEC958_AES0_PROFESSIONAL; |
2229 | if (sbits & IEC958_AES0_PROFESSIONAL) { | |
a686fd14 | 2230 | if (val & AC_DIG1_EMPHASIS) |
1da177e4 LT |
2231 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
2232 | } else { | |
0ba21762 | 2233 | if (val & AC_DIG1_EMPHASIS) |
1da177e4 | 2234 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
0ba21762 | 2235 | if (!(val & AC_DIG1_COPYRIGHT)) |
1da177e4 | 2236 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
0ba21762 | 2237 | if (val & AC_DIG1_LEVEL) |
1da177e4 LT |
2238 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
2239 | sbits |= val & (0x7f << 8); | |
2240 | } | |
2241 | return sbits; | |
2242 | } | |
2243 | ||
2f72853c TI |
2244 | /* set digital convert verbs both for the given NID and its slaves */ |
2245 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, | |
a551d914 | 2246 | int mask, int val) |
2f72853c | 2247 | { |
dda14410 | 2248 | const hda_nid_t *d; |
2f72853c | 2249 | |
a551d914 TI |
2250 | snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1, |
2251 | mask, val); | |
2f72853c TI |
2252 | d = codec->slave_dig_outs; |
2253 | if (!d) | |
2254 | return; | |
2255 | for (; *d; d++) | |
7d4b5e97 | 2256 | snd_hdac_regmap_update(&codec->core, *d, |
a551d914 | 2257 | AC_VERB_SET_DIGI_CONVERT_1, mask, val); |
2f72853c TI |
2258 | } |
2259 | ||
2260 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, | |
2261 | int dig1, int dig2) | |
2262 | { | |
a551d914 TI |
2263 | unsigned int mask = 0; |
2264 | unsigned int val = 0; | |
2265 | ||
2266 | if (dig1 != -1) { | |
2267 | mask |= 0xff; | |
2268 | val = dig1; | |
2269 | } | |
2270 | if (dig2 != -1) { | |
2271 | mask |= 0xff00; | |
2272 | val |= dig2 << 8; | |
2273 | } | |
2274 | set_dig_out(codec, nid, mask, val); | |
2f72853c TI |
2275 | } |
2276 | ||
0ba21762 TI |
2277 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
2278 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2279 | { |
2280 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
7c935976 | 2281 | int idx = kcontrol->private_value; |
e3245cdd TI |
2282 | struct hda_spdif_out *spdif; |
2283 | hda_nid_t nid; | |
1da177e4 LT |
2284 | unsigned short val; |
2285 | int change; | |
2286 | ||
08605068 TI |
2287 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2288 | return -EINVAL; | |
62932df8 | 2289 | mutex_lock(&codec->spdif_mutex); |
e3245cdd TI |
2290 | spdif = snd_array_elem(&codec->spdif_out, idx); |
2291 | nid = spdif->nid; | |
7c935976 | 2292 | spdif->status = ucontrol->value.iec958.status[0] | |
1da177e4 LT |
2293 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
2294 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | | |
2295 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); | |
7c935976 SW |
2296 | val = convert_from_spdif_status(spdif->status); |
2297 | val |= spdif->ctls & 1; | |
2298 | change = spdif->ctls != val; | |
2299 | spdif->ctls = val; | |
74b654c9 | 2300 | if (change && nid != (u16)-1) |
2f72853c | 2301 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
62932df8 | 2302 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2303 | return change; |
2304 | } | |
2305 | ||
a5ce8890 | 2306 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
1da177e4 | 2307 | |
0ba21762 TI |
2308 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
2309 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2310 | { |
2311 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
7c935976 | 2312 | int idx = kcontrol->private_value; |
e3245cdd | 2313 | struct hda_spdif_out *spdif; |
1da177e4 | 2314 | |
08605068 TI |
2315 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2316 | return -EINVAL; | |
e3245cdd TI |
2317 | mutex_lock(&codec->spdif_mutex); |
2318 | spdif = snd_array_elem(&codec->spdif_out, idx); | |
7c935976 | 2319 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
e3245cdd | 2320 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2321 | return 0; |
2322 | } | |
2323 | ||
74b654c9 SW |
2324 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
2325 | int dig1, int dig2) | |
2326 | { | |
2327 | set_dig_out_convert(codec, nid, dig1, dig2); | |
2328 | /* unmute amp switch (if any) */ | |
2329 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && | |
2330 | (dig1 & AC_DIG1_ENABLE)) | |
2331 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, | |
2332 | HDA_AMP_MUTE, 0); | |
2333 | } | |
2334 | ||
0ba21762 TI |
2335 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
2336 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2337 | { |
2338 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
7c935976 | 2339 | int idx = kcontrol->private_value; |
e3245cdd TI |
2340 | struct hda_spdif_out *spdif; |
2341 | hda_nid_t nid; | |
1da177e4 LT |
2342 | unsigned short val; |
2343 | int change; | |
2344 | ||
08605068 TI |
2345 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2346 | return -EINVAL; | |
62932df8 | 2347 | mutex_lock(&codec->spdif_mutex); |
e3245cdd TI |
2348 | spdif = snd_array_elem(&codec->spdif_out, idx); |
2349 | nid = spdif->nid; | |
7c935976 | 2350 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
1da177e4 | 2351 | if (ucontrol->value.integer.value[0]) |
0ba21762 | 2352 | val |= AC_DIG1_ENABLE; |
7c935976 | 2353 | change = spdif->ctls != val; |
74b654c9 SW |
2354 | spdif->ctls = val; |
2355 | if (change && nid != (u16)-1) | |
2356 | set_spdif_ctls(codec, nid, val & 0xff, -1); | |
62932df8 | 2357 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2358 | return change; |
2359 | } | |
2360 | ||
c8b6bf9b | 2361 | static struct snd_kcontrol_new dig_mixes[] = { |
1da177e4 LT |
2362 | { |
2363 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2364 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2365 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
1da177e4 LT |
2366 | .info = snd_hda_spdif_mask_info, |
2367 | .get = snd_hda_spdif_cmask_get, | |
2368 | }, | |
2369 | { | |
2370 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2371 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2372 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
1da177e4 LT |
2373 | .info = snd_hda_spdif_mask_info, |
2374 | .get = snd_hda_spdif_pmask_get, | |
2375 | }, | |
2376 | { | |
2377 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2378 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
1da177e4 LT |
2379 | .info = snd_hda_spdif_mask_info, |
2380 | .get = snd_hda_spdif_default_get, | |
2381 | .put = snd_hda_spdif_default_put, | |
2382 | }, | |
2383 | { | |
2384 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2385 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
1da177e4 LT |
2386 | .info = snd_hda_spdif_out_switch_info, |
2387 | .get = snd_hda_spdif_out_switch_get, | |
2388 | .put = snd_hda_spdif_out_switch_put, | |
2389 | }, | |
2390 | { } /* end */ | |
2391 | }; | |
2392 | ||
2393 | /** | |
dcda5806 | 2394 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
1da177e4 | 2395 | * @codec: the HDA codec |
dcda5806 TI |
2396 | * @associated_nid: NID that new ctls associated with |
2397 | * @cvt_nid: converter NID | |
2398 | * @type: HDA_PCM_TYPE_* | |
2399 | * Creates controls related with the digital output. | |
2400 | * Called from each patch supporting the digital out. | |
1da177e4 LT |
2401 | * |
2402 | * Returns 0 if successful, or a negative error code. | |
2403 | */ | |
dcda5806 TI |
2404 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
2405 | hda_nid_t associated_nid, | |
2406 | hda_nid_t cvt_nid, | |
2407 | int type) | |
1da177e4 LT |
2408 | { |
2409 | int err; | |
c8b6bf9b TI |
2410 | struct snd_kcontrol *kctl; |
2411 | struct snd_kcontrol_new *dig_mix; | |
ea9b43ad | 2412 | int idx = 0; |
a551d914 | 2413 | int val = 0; |
ea9b43ad | 2414 | const int spdif_index = 16; |
7c935976 | 2415 | struct hda_spdif_out *spdif; |
ea9b43ad | 2416 | struct hda_bus *bus = codec->bus; |
1da177e4 | 2417 | |
ea9b43ad | 2418 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
dcda5806 | 2419 | type == HDA_PCM_TYPE_SPDIF) { |
ea9b43ad TI |
2420 | idx = spdif_index; |
2421 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && | |
dcda5806 | 2422 | type == HDA_PCM_TYPE_HDMI) { |
ea9b43ad TI |
2423 | /* suppose a single SPDIF device */ |
2424 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { | |
2425 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); | |
2426 | if (!kctl) | |
2427 | break; | |
2428 | kctl->id.index = spdif_index; | |
dcda5806 | 2429 | } |
ea9b43ad | 2430 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
dcda5806 | 2431 | } |
ea9b43ad TI |
2432 | if (!bus->primary_dig_out_type) |
2433 | bus->primary_dig_out_type = type; | |
dcda5806 | 2434 | |
ea9b43ad | 2435 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
1afe206a | 2436 | if (idx < 0) { |
4e76a883 | 2437 | codec_err(codec, "too many IEC958 outputs\n"); |
09f99701 TI |
2438 | return -EBUSY; |
2439 | } | |
7c935976 | 2440 | spdif = snd_array_new(&codec->spdif_out); |
25336e8a ML |
2441 | if (!spdif) |
2442 | return -ENOMEM; | |
1da177e4 LT |
2443 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
2444 | kctl = snd_ctl_new1(dig_mix, codec); | |
b91f080f TI |
2445 | if (!kctl) |
2446 | return -ENOMEM; | |
09f99701 | 2447 | kctl->id.index = idx; |
7c935976 | 2448 | kctl->private_value = codec->spdif_out.used - 1; |
74b654c9 | 2449 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
0ba21762 | 2450 | if (err < 0) |
1da177e4 LT |
2451 | return err; |
2452 | } | |
74b654c9 | 2453 | spdif->nid = cvt_nid; |
a551d914 TI |
2454 | snd_hdac_regmap_read(&codec->core, cvt_nid, |
2455 | AC_VERB_GET_DIGI_CONVERT_1, &val); | |
2456 | spdif->ctls = val; | |
7c935976 | 2457 | spdif->status = convert_to_spdif_status(spdif->ctls); |
1da177e4 LT |
2458 | return 0; |
2459 | } | |
2698ea98 | 2460 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
1da177e4 | 2461 | |
95a962c3 TI |
2462 | /** |
2463 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID | |
2464 | * @codec: the HDA codec | |
2465 | * @nid: widget NID | |
2466 | * | |
e3245cdd TI |
2467 | * call within spdif_mutex lock |
2468 | */ | |
7c935976 SW |
2469 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
2470 | hda_nid_t nid) | |
2471 | { | |
a9c2dfc8 | 2472 | struct hda_spdif_out *spdif; |
7c935976 | 2473 | int i; |
a9c2dfc8 TI |
2474 | |
2475 | snd_array_for_each(&codec->spdif_out, i, spdif) { | |
7c935976 SW |
2476 | if (spdif->nid == nid) |
2477 | return spdif; | |
2478 | } | |
2479 | return NULL; | |
2480 | } | |
2698ea98 | 2481 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
7c935976 | 2482 | |
95a962c3 TI |
2483 | /** |
2484 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl | |
2485 | * @codec: the HDA codec | |
2486 | * @idx: the SPDIF ctl index | |
2487 | * | |
2488 | * Unassign the widget from the given SPDIF control. | |
2489 | */ | |
74b654c9 SW |
2490 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
2491 | { | |
e3245cdd | 2492 | struct hda_spdif_out *spdif; |
74b654c9 | 2493 | |
08605068 TI |
2494 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2495 | return; | |
74b654c9 | 2496 | mutex_lock(&codec->spdif_mutex); |
e3245cdd | 2497 | spdif = snd_array_elem(&codec->spdif_out, idx); |
74b654c9 SW |
2498 | spdif->nid = (u16)-1; |
2499 | mutex_unlock(&codec->spdif_mutex); | |
2500 | } | |
2698ea98 | 2501 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
74b654c9 | 2502 | |
95a962c3 TI |
2503 | /** |
2504 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID | |
2505 | * @codec: the HDA codec | |
2506 | * @idx: the SPDIF ctl idx | |
2507 | * @nid: widget NID | |
2508 | * | |
2509 | * Assign the widget to the SPDIF control with the given index. | |
2510 | */ | |
74b654c9 SW |
2511 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
2512 | { | |
e3245cdd | 2513 | struct hda_spdif_out *spdif; |
74b654c9 SW |
2514 | unsigned short val; |
2515 | ||
08605068 TI |
2516 | if (WARN_ON(codec->spdif_out.used <= idx)) |
2517 | return; | |
74b654c9 | 2518 | mutex_lock(&codec->spdif_mutex); |
e3245cdd | 2519 | spdif = snd_array_elem(&codec->spdif_out, idx); |
74b654c9 SW |
2520 | if (spdif->nid != nid) { |
2521 | spdif->nid = nid; | |
2522 | val = spdif->ctls; | |
2523 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); | |
2524 | } | |
2525 | mutex_unlock(&codec->spdif_mutex); | |
2526 | } | |
2698ea98 | 2527 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
74b654c9 | 2528 | |
9a08160b TI |
2529 | /* |
2530 | * SPDIF sharing with analog output | |
2531 | */ | |
2532 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, | |
2533 | struct snd_ctl_elem_value *ucontrol) | |
2534 | { | |
2535 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | |
2536 | ucontrol->value.integer.value[0] = mout->share_spdif; | |
2537 | return 0; | |
2538 | } | |
2539 | ||
2540 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, | |
2541 | struct snd_ctl_elem_value *ucontrol) | |
2542 | { | |
2543 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | |
2544 | mout->share_spdif = !!ucontrol->value.integer.value[0]; | |
2545 | return 0; | |
2546 | } | |
2547 | ||
f3b827e0 | 2548 | static const struct snd_kcontrol_new spdif_share_sw = { |
9a08160b TI |
2549 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
2550 | .name = "IEC958 Default PCM Playback Switch", | |
2551 | .info = snd_ctl_boolean_mono_info, | |
2552 | .get = spdif_share_sw_get, | |
2553 | .put = spdif_share_sw_put, | |
2554 | }; | |
2555 | ||
d5191e50 TI |
2556 | /** |
2557 | * snd_hda_create_spdif_share_sw - create Default PCM switch | |
2558 | * @codec: the HDA codec | |
2559 | * @mout: multi-out instance | |
2560 | */ | |
9a08160b TI |
2561 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
2562 | struct hda_multi_out *mout) | |
2563 | { | |
4c7a548a ML |
2564 | struct snd_kcontrol *kctl; |
2565 | ||
9a08160b TI |
2566 | if (!mout->dig_out_nid) |
2567 | return 0; | |
4c7a548a ML |
2568 | |
2569 | kctl = snd_ctl_new1(&spdif_share_sw, mout); | |
2570 | if (!kctl) | |
2571 | return -ENOMEM; | |
9a08160b | 2572 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
4c7a548a | 2573 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
9a08160b | 2574 | } |
2698ea98 | 2575 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
9a08160b | 2576 | |
1da177e4 LT |
2577 | /* |
2578 | * SPDIF input | |
2579 | */ | |
2580 | ||
2581 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info | |
2582 | ||
0ba21762 TI |
2583 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
2584 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2585 | { |
2586 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2587 | ||
2588 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; | |
2589 | return 0; | |
2590 | } | |
2591 | ||
0ba21762 TI |
2592 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
2593 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2594 | { |
2595 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2596 | hda_nid_t nid = kcontrol->private_value; | |
2597 | unsigned int val = !!ucontrol->value.integer.value[0]; | |
2598 | int change; | |
2599 | ||
62932df8 | 2600 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 2601 | change = codec->spdif_in_enable != val; |
82beb8fd | 2602 | if (change) { |
1da177e4 | 2603 | codec->spdif_in_enable = val; |
a551d914 TI |
2604 | snd_hdac_regmap_write(&codec->core, nid, |
2605 | AC_VERB_SET_DIGI_CONVERT_1, val); | |
1da177e4 | 2606 | } |
62932df8 | 2607 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
2608 | return change; |
2609 | } | |
2610 | ||
0ba21762 TI |
2611 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
2612 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
2613 | { |
2614 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2615 | hda_nid_t nid = kcontrol->private_value; | |
a551d914 | 2616 | unsigned int val; |
1da177e4 LT |
2617 | unsigned int sbits; |
2618 | ||
a551d914 TI |
2619 | snd_hdac_regmap_read(&codec->core, nid, |
2620 | AC_VERB_GET_DIGI_CONVERT_1, &val); | |
1da177e4 LT |
2621 | sbits = convert_to_spdif_status(val); |
2622 | ucontrol->value.iec958.status[0] = sbits; | |
2623 | ucontrol->value.iec958.status[1] = sbits >> 8; | |
2624 | ucontrol->value.iec958.status[2] = sbits >> 16; | |
2625 | ucontrol->value.iec958.status[3] = sbits >> 24; | |
2626 | return 0; | |
2627 | } | |
2628 | ||
c8b6bf9b | 2629 | static struct snd_kcontrol_new dig_in_ctls[] = { |
1da177e4 LT |
2630 | { |
2631 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2632 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
1da177e4 LT |
2633 | .info = snd_hda_spdif_in_switch_info, |
2634 | .get = snd_hda_spdif_in_switch_get, | |
2635 | .put = snd_hda_spdif_in_switch_put, | |
2636 | }, | |
2637 | { | |
2638 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
2639 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
28aedaf7 | 2640 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
1da177e4 LT |
2641 | .info = snd_hda_spdif_mask_info, |
2642 | .get = snd_hda_spdif_in_status_get, | |
2643 | }, | |
2644 | { } /* end */ | |
2645 | }; | |
2646 | ||
2647 | /** | |
2648 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls | |
2649 | * @codec: the HDA codec | |
2650 | * @nid: audio in widget NID | |
2651 | * | |
2652 | * Creates controls related with the SPDIF input. | |
2653 | * Called from each patch supporting the SPDIF in. | |
2654 | * | |
2655 | * Returns 0 if successful, or a negative error code. | |
2656 | */ | |
12f288bf | 2657 | int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) |
1da177e4 LT |
2658 | { |
2659 | int err; | |
c8b6bf9b TI |
2660 | struct snd_kcontrol *kctl; |
2661 | struct snd_kcontrol_new *dig_mix; | |
09f99701 | 2662 | int idx; |
1da177e4 | 2663 | |
dcda5806 | 2664 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
1afe206a | 2665 | if (idx < 0) { |
4e76a883 | 2666 | codec_err(codec, "too many IEC958 inputs\n"); |
09f99701 TI |
2667 | return -EBUSY; |
2668 | } | |
1da177e4 LT |
2669 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
2670 | kctl = snd_ctl_new1(dig_mix, codec); | |
c8dcdf82 TI |
2671 | if (!kctl) |
2672 | return -ENOMEM; | |
1da177e4 | 2673 | kctl->private_value = nid; |
3911a4c1 | 2674 | err = snd_hda_ctl_add(codec, nid, kctl); |
0ba21762 | 2675 | if (err < 0) |
1da177e4 LT |
2676 | return err; |
2677 | } | |
0ba21762 | 2678 | codec->spdif_in_enable = |
3982d17e AP |
2679 | snd_hda_codec_read(codec, nid, 0, |
2680 | AC_VERB_GET_DIGI_CONVERT_1, 0) & | |
0ba21762 | 2681 | AC_DIG1_ENABLE; |
1da177e4 LT |
2682 | return 0; |
2683 | } | |
2698ea98 | 2684 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
1da177e4 | 2685 | |
95a962c3 TI |
2686 | /** |
2687 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets | |
2688 | * @codec: the HDA codec | |
2689 | * @fg: function group (not used now) | |
2690 | * @power_state: the power state to set (AC_PWRST_*) | |
2691 | * | |
2692 | * Set the given power state to all widgets that have the power control. | |
2693 | * If the codec has power_filter set, it evaluates the power state and | |
2694 | * filter out if it's unchanged as D3. | |
2695 | */ | |
4d7fbdbc | 2696 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
9419ab6b | 2697 | unsigned int power_state) |
54d17403 | 2698 | { |
7639a06c | 2699 | hda_nid_t nid; |
54d17403 | 2700 | |
7639a06c | 2701 | for_each_hda_codec_node(nid, codec) { |
7eba5c9d | 2702 | unsigned int wcaps = get_wcaps(codec, nid); |
9419ab6b | 2703 | unsigned int state = power_state; |
4d7fbdbc TI |
2704 | if (!(wcaps & AC_WCAP_POWER)) |
2705 | continue; | |
9419ab6b TI |
2706 | if (codec->power_filter) { |
2707 | state = codec->power_filter(codec, nid, power_state); | |
2708 | if (state != power_state && power_state == AC_PWRST_D3) | |
4d7fbdbc | 2709 | continue; |
1194b5b7 | 2710 | } |
4d7fbdbc | 2711 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
9419ab6b | 2712 | state); |
54d17403 | 2713 | } |
cb53c626 | 2714 | } |
2698ea98 | 2715 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
4d7fbdbc | 2716 | |
95a962c3 TI |
2717 | /** |
2718 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD | |
2719 | * @codec: the HDA codec | |
2720 | * @nid: widget NID | |
2721 | * @power_state: power state to evalue | |
2722 | * | |
2723 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. | |
2724 | * This can be used a codec power_filter callback. | |
2725 | */ | |
ba615b86 TI |
2726 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
2727 | hda_nid_t nid, | |
2728 | unsigned int power_state) | |
9419ab6b | 2729 | { |
7639a06c | 2730 | if (nid == codec->core.afg || nid == codec->core.mfg) |
dfc6e469 | 2731 | return power_state; |
9419ab6b TI |
2732 | if (power_state == AC_PWRST_D3 && |
2733 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && | |
2734 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { | |
2735 | int eapd = snd_hda_codec_read(codec, nid, 0, | |
2736 | AC_VERB_GET_EAPD_BTLENABLE, 0); | |
2737 | if (eapd & 0x02) | |
2738 | return AC_PWRST_D0; | |
2739 | } | |
2740 | return power_state; | |
2741 | } | |
2698ea98 | 2742 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
9419ab6b | 2743 | |
4d7fbdbc | 2744 | /* |
08fa20ae | 2745 | * set power state of the codec, and return the power state |
4d7fbdbc | 2746 | */ |
d819387e | 2747 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
08fa20ae | 2748 | unsigned int power_state) |
4d7fbdbc | 2749 | { |
7639a06c | 2750 | hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg; |
09617ce4 WX |
2751 | int count; |
2752 | unsigned int state; | |
63e51fd7 | 2753 | int flags = 0; |
09617ce4 | 2754 | |
4d7fbdbc | 2755 | /* this delay seems necessary to avoid click noise at power-down */ |
0f4ccbb0 | 2756 | if (power_state == AC_PWRST_D3) { |
7f132927 | 2757 | if (codec->depop_delay < 0) |
7639a06c | 2758 | msleep(codec_has_epss(codec) ? 10 : 100); |
7f132927 ML |
2759 | else if (codec->depop_delay > 0) |
2760 | msleep(codec->depop_delay); | |
63e51fd7 | 2761 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
0f4ccbb0 | 2762 | } |
09617ce4 WX |
2763 | |
2764 | /* repeat power states setting at most 10 times*/ | |
2765 | for (count = 0; count < 10; count++) { | |
432c641e TI |
2766 | if (codec->patch_ops.set_power_state) |
2767 | codec->patch_ops.set_power_state(codec, fg, | |
2768 | power_state); | |
2769 | else { | |
dfc6e469 TI |
2770 | state = power_state; |
2771 | if (codec->power_filter) | |
2772 | state = codec->power_filter(codec, fg, state); | |
2773 | if (state == power_state || power_state != AC_PWRST_D3) | |
2774 | snd_hda_codec_read(codec, fg, flags, | |
2775 | AC_VERB_SET_POWER_STATE, | |
2776 | state); | |
9419ab6b | 2777 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
432c641e | 2778 | } |
3b5b899c | 2779 | state = snd_hda_sync_power_state(codec, fg, power_state); |
09617ce4 WX |
2780 | if (!(state & AC_PWRST_ERROR)) |
2781 | break; | |
2782 | } | |
b8dfc462 | 2783 | |
08fa20ae | 2784 | return state; |
4d7fbdbc | 2785 | } |
cb53c626 | 2786 | |
b9c590bb TI |
2787 | /* sync power states of all widgets; |
2788 | * this is called at the end of codec parsing | |
2789 | */ | |
2790 | static void sync_power_up_states(struct hda_codec *codec) | |
2791 | { | |
7639a06c | 2792 | hda_nid_t nid; |
b9c590bb | 2793 | |
ba615b86 TI |
2794 | /* don't care if no filter is used */ |
2795 | if (!codec->power_filter) | |
b9c590bb TI |
2796 | return; |
2797 | ||
7639a06c | 2798 | for_each_hda_codec_node(nid, codec) { |
b9c590bb | 2799 | unsigned int wcaps = get_wcaps(codec, nid); |
9040d102 | 2800 | unsigned int target; |
b9c590bb TI |
2801 | if (!(wcaps & AC_WCAP_POWER)) |
2802 | continue; | |
2803 | target = codec->power_filter(codec, nid, AC_PWRST_D0); | |
2804 | if (target == AC_PWRST_D0) | |
2805 | continue; | |
9040d102 | 2806 | if (!snd_hda_check_power_state(codec, nid, target)) |
b9c590bb TI |
2807 | snd_hda_codec_write(codec, nid, 0, |
2808 | AC_VERB_SET_POWER_STATE, target); | |
2809 | } | |
2810 | } | |
2811 | ||
648a8d27 | 2812 | #ifdef CONFIG_SND_HDA_RECONFIG |
11aeff08 TI |
2813 | /* execute additional init verbs */ |
2814 | static void hda_exec_init_verbs(struct hda_codec *codec) | |
2815 | { | |
2816 | if (codec->init_verbs.list) | |
2817 | snd_hda_sequence_write(codec, codec->init_verbs.list); | |
2818 | } | |
2819 | #else | |
2820 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} | |
2821 | #endif | |
2822 | ||
2a43952a | 2823 | #ifdef CONFIG_PM |
cc72da7d TI |
2824 | /* update the power on/off account with the current jiffies */ |
2825 | static void update_power_acct(struct hda_codec *codec, bool on) | |
2826 | { | |
2827 | unsigned long delta = jiffies - codec->power_jiffies; | |
2828 | ||
2829 | if (on) | |
2830 | codec->power_on_acct += delta; | |
2831 | else | |
2832 | codec->power_off_acct += delta; | |
2833 | codec->power_jiffies += delta; | |
2834 | } | |
2835 | ||
2836 | void snd_hda_update_power_acct(struct hda_codec *codec) | |
2837 | { | |
2838 | update_power_acct(codec, hda_codec_is_power_on(codec)); | |
2839 | } | |
2840 | ||
cb53c626 TI |
2841 | /* |
2842 | * call suspend and power-down; used both from PM and power-save | |
08fa20ae | 2843 | * this function returns the power state in the end |
cb53c626 | 2844 | */ |
cc72da7d | 2845 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
cb53c626 | 2846 | { |
08fa20ae TI |
2847 | unsigned int state; |
2848 | ||
7639a06c | 2849 | atomic_inc(&codec->core.in_pm); |
989c3187 | 2850 | |
cb53c626 | 2851 | if (codec->patch_ops.suspend) |
68cb2b55 | 2852 | codec->patch_ops.suspend(codec); |
eb541337 | 2853 | hda_cleanup_all_streams(codec); |
d819387e | 2854 | state = hda_set_power_state(codec, AC_PWRST_D3); |
cc72da7d | 2855 | update_power_acct(codec, true); |
7639a06c | 2856 | atomic_dec(&codec->core.in_pm); |
08fa20ae | 2857 | return state; |
54d17403 TI |
2858 | } |
2859 | ||
cb53c626 TI |
2860 | /* |
2861 | * kick up codec; used both from PM and power-save | |
2862 | */ | |
2863 | static void hda_call_codec_resume(struct hda_codec *codec) | |
2864 | { | |
7639a06c | 2865 | atomic_inc(&codec->core.in_pm); |
989c3187 | 2866 | |
eeecd9d1 TI |
2867 | if (codec->core.regmap) |
2868 | regcache_mark_dirty(codec->core.regmap); | |
2869 | ||
cc72da7d | 2870 | codec->power_jiffies = jiffies; |
cc72da7d | 2871 | |
d819387e | 2872 | hda_set_power_state(codec, AC_PWRST_D0); |
ac0547dc | 2873 | restore_shutup_pins(codec); |
11aeff08 | 2874 | hda_exec_init_verbs(codec); |
31614bb8 | 2875 | snd_hda_jack_set_dirty_all(codec); |
cb53c626 TI |
2876 | if (codec->patch_ops.resume) |
2877 | codec->patch_ops.resume(codec); | |
2878 | else { | |
9d99f312 TI |
2879 | if (codec->patch_ops.init) |
2880 | codec->patch_ops.init(codec); | |
eeecd9d1 TI |
2881 | if (codec->core.regmap) |
2882 | regcache_sync(codec->core.regmap); | |
cb53c626 | 2883 | } |
26a6cb6c DH |
2884 | |
2885 | if (codec->jackpoll_interval) | |
2886 | hda_jackpoll_work(&codec->jackpoll_work.work); | |
31614bb8 | 2887 | else |
26a6cb6c | 2888 | snd_hda_jack_report_sync(codec); |
7639a06c | 2889 | atomic_dec(&codec->core.in_pm); |
cb53c626 | 2890 | } |
59ed1ead | 2891 | |
cc72da7d | 2892 | static int hda_codec_runtime_suspend(struct device *dev) |
59ed1ead TI |
2893 | { |
2894 | struct hda_codec *codec = dev_to_hda_codec(dev); | |
bbbc7e85 | 2895 | struct hda_pcm *pcm; |
cc72da7d | 2896 | unsigned int state; |
59ed1ead TI |
2897 | |
2898 | cancel_delayed_work_sync(&codec->jackpoll_work); | |
bbbc7e85 TI |
2899 | list_for_each_entry(pcm, &codec->pcm_list_head, list) |
2900 | snd_pcm_suspend_all(pcm->pcm); | |
cc72da7d | 2901 | state = hda_call_codec_suspend(codec); |
7639a06c TI |
2902 | if (codec_has_clkstop(codec) && codec_has_epss(codec) && |
2903 | (state & AC_PWRST_CLK_STOP_OK)) | |
2904 | snd_hdac_codec_link_down(&codec->core); | |
a5e7e07c | 2905 | snd_hdac_link_power(&codec->core, false); |
59ed1ead TI |
2906 | return 0; |
2907 | } | |
2908 | ||
cc72da7d | 2909 | static int hda_codec_runtime_resume(struct device *dev) |
59ed1ead | 2910 | { |
55ed9cd1 TI |
2911 | struct hda_codec *codec = dev_to_hda_codec(dev); |
2912 | ||
a5e7e07c | 2913 | snd_hdac_link_power(&codec->core, true); |
7639a06c | 2914 | snd_hdac_codec_link_up(&codec->core); |
55ed9cd1 | 2915 | hda_call_codec_resume(codec); |
cc72da7d | 2916 | pm_runtime_mark_last_busy(dev); |
59ed1ead TI |
2917 | return 0; |
2918 | } | |
2a43952a | 2919 | #endif /* CONFIG_PM */ |
cb53c626 | 2920 | |
59ed1ead TI |
2921 | /* referred in hda_bind.c */ |
2922 | const struct dev_pm_ops hda_codec_driver_pm = { | |
cc72da7d TI |
2923 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
2924 | pm_runtime_force_resume) | |
2925 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, | |
2926 | NULL) | |
59ed1ead | 2927 | }; |
54d17403 | 2928 | |
9c9a5175 TI |
2929 | /* |
2930 | * add standard channel maps if not specified | |
2931 | */ | |
2932 | static int add_std_chmaps(struct hda_codec *codec) | |
2933 | { | |
bbbc7e85 TI |
2934 | struct hda_pcm *pcm; |
2935 | int str, err; | |
9c9a5175 | 2936 | |
bbbc7e85 | 2937 | list_for_each_entry(pcm, &codec->pcm_list_head, list) { |
9c9a5175 | 2938 | for (str = 0; str < 2; str++) { |
bbbc7e85 | 2939 | struct hda_pcm_stream *hinfo = &pcm->stream[str]; |
9c9a5175 | 2940 | struct snd_pcm_chmap *chmap; |
ee81abb6 | 2941 | const struct snd_pcm_chmap_elem *elem; |
9c9a5175 | 2942 | |
b25cf30a | 2943 | if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams) |
9c9a5175 | 2944 | continue; |
ee81abb6 | 2945 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
bbbc7e85 | 2946 | err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem, |
9c9a5175 TI |
2947 | hinfo->channels_max, |
2948 | 0, &chmap); | |
2949 | if (err < 0) | |
2950 | return err; | |
2951 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; | |
2952 | } | |
2953 | } | |
2954 | return 0; | |
2955 | } | |
2956 | ||
ee81abb6 TI |
2957 | /* default channel maps for 2.1 speakers; |
2958 | * since HD-audio supports only stereo, odd number channels are omitted | |
2959 | */ | |
2960 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { | |
2961 | { .channels = 2, | |
2962 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, | |
2963 | { .channels = 4, | |
2964 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | |
2965 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, | |
2966 | { } | |
2967 | }; | |
2968 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); | |
2969 | ||
6c1f45ea TI |
2970 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
2971 | { | |
2972 | int err = 0; | |
11aeff08 | 2973 | hda_exec_init_verbs(codec); |
6c1f45ea TI |
2974 | /* continue to initialize... */ |
2975 | if (codec->patch_ops.init) | |
2976 | err = codec->patch_ops.init(codec); | |
2977 | if (!err && codec->patch_ops.build_controls) | |
2978 | err = codec->patch_ops.build_controls(codec); | |
6c1f45ea TI |
2979 | if (err < 0) |
2980 | return err; | |
9c9a5175 TI |
2981 | |
2982 | /* we create chmaps here instead of build_pcms */ | |
2983 | err = add_std_chmaps(codec); | |
2984 | if (err < 0) | |
2985 | return err; | |
2986 | ||
26a6cb6c DH |
2987 | if (codec->jackpoll_interval) |
2988 | hda_jackpoll_work(&codec->jackpoll_work.work); | |
2989 | else | |
2990 | snd_hda_jack_report_sync(codec); /* call at the last init point */ | |
b9c590bb | 2991 | sync_power_up_states(codec); |
1da177e4 LT |
2992 | return 0; |
2993 | } | |
2994 | ||
1da177e4 LT |
2995 | /* |
2996 | * PCM stuff | |
2997 | */ | |
2998 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, | |
2999 | struct hda_codec *codec, | |
c8b6bf9b | 3000 | struct snd_pcm_substream *substream) |
1da177e4 LT |
3001 | { |
3002 | return 0; | |
3003 | } | |
3004 | ||
3005 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, | |
3006 | struct hda_codec *codec, | |
3007 | unsigned int stream_tag, | |
3008 | unsigned int format, | |
c8b6bf9b | 3009 | struct snd_pcm_substream *substream) |
1da177e4 LT |
3010 | { |
3011 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); | |
3012 | return 0; | |
3013 | } | |
3014 | ||
3015 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, | |
3016 | struct hda_codec *codec, | |
c8b6bf9b | 3017 | struct snd_pcm_substream *substream) |
1da177e4 | 3018 | { |
888afa15 | 3019 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
1da177e4 LT |
3020 | return 0; |
3021 | } | |
3022 | ||
6c1f45ea TI |
3023 | static int set_pcm_default_values(struct hda_codec *codec, |
3024 | struct hda_pcm_stream *info) | |
1da177e4 | 3025 | { |
ee504710 JK |
3026 | int err; |
3027 | ||
0ba21762 TI |
3028 | /* query support PCM information from the given NID */ |
3029 | if (info->nid && (!info->rates || !info->formats)) { | |
ee504710 | 3030 | err = snd_hda_query_supported_pcm(codec, info->nid, |
0ba21762 TI |
3031 | info->rates ? NULL : &info->rates, |
3032 | info->formats ? NULL : &info->formats, | |
3033 | info->maxbps ? NULL : &info->maxbps); | |
ee504710 JK |
3034 | if (err < 0) |
3035 | return err; | |
1da177e4 LT |
3036 | } |
3037 | if (info->ops.open == NULL) | |
3038 | info->ops.open = hda_pcm_default_open_close; | |
3039 | if (info->ops.close == NULL) | |
3040 | info->ops.close = hda_pcm_default_open_close; | |
3041 | if (info->ops.prepare == NULL) { | |
da3cec35 TI |
3042 | if (snd_BUG_ON(!info->nid)) |
3043 | return -EINVAL; | |
1da177e4 LT |
3044 | info->ops.prepare = hda_pcm_default_prepare; |
3045 | } | |
1da177e4 | 3046 | if (info->ops.cleanup == NULL) { |
da3cec35 TI |
3047 | if (snd_BUG_ON(!info->nid)) |
3048 | return -EINVAL; | |
1da177e4 LT |
3049 | info->ops.cleanup = hda_pcm_default_cleanup; |
3050 | } | |
3051 | return 0; | |
3052 | } | |
3053 | ||
eb541337 TI |
3054 | /* |
3055 | * codec prepare/cleanup entries | |
3056 | */ | |
95a962c3 TI |
3057 | /** |
3058 | * snd_hda_codec_prepare - Prepare a stream | |
3059 | * @codec: the HDA codec | |
3060 | * @hinfo: PCM information | |
3061 | * @stream: stream tag to assign | |
3062 | * @format: format id to assign | |
3063 | * @substream: PCM substream to assign | |
3064 | * | |
3065 | * Calls the prepare callback set by the codec with the given arguments. | |
3066 | * Clean up the inactive streams when successful. | |
3067 | */ | |
eb541337 TI |
3068 | int snd_hda_codec_prepare(struct hda_codec *codec, |
3069 | struct hda_pcm_stream *hinfo, | |
3070 | unsigned int stream, | |
3071 | unsigned int format, | |
3072 | struct snd_pcm_substream *substream) | |
3073 | { | |
3074 | int ret; | |
3f50ac6a | 3075 | mutex_lock(&codec->bus->prepare_mutex); |
61ca4107 TI |
3076 | if (hinfo->ops.prepare) |
3077 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, | |
3078 | substream); | |
3079 | else | |
3080 | ret = -ENODEV; | |
eb541337 TI |
3081 | if (ret >= 0) |
3082 | purify_inactive_streams(codec); | |
3f50ac6a | 3083 | mutex_unlock(&codec->bus->prepare_mutex); |
eb541337 TI |
3084 | return ret; |
3085 | } | |
2698ea98 | 3086 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
eb541337 | 3087 | |
95a962c3 TI |
3088 | /** |
3089 | * snd_hda_codec_cleanup - Prepare a stream | |
3090 | * @codec: the HDA codec | |
3091 | * @hinfo: PCM information | |
3092 | * @substream: PCM substream | |
3093 | * | |
3094 | * Calls the cleanup callback set by the codec with the given arguments. | |
3095 | */ | |
eb541337 TI |
3096 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
3097 | struct hda_pcm_stream *hinfo, | |
3098 | struct snd_pcm_substream *substream) | |
3099 | { | |
3f50ac6a | 3100 | mutex_lock(&codec->bus->prepare_mutex); |
61ca4107 TI |
3101 | if (hinfo->ops.cleanup) |
3102 | hinfo->ops.cleanup(hinfo, codec, substream); | |
3f50ac6a | 3103 | mutex_unlock(&codec->bus->prepare_mutex); |
eb541337 | 3104 | } |
2698ea98 | 3105 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
eb541337 | 3106 | |
d5191e50 | 3107 | /* global */ |
e3303235 JK |
3108 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
3109 | "Audio", "SPDIF", "HDMI", "Modem" | |
3110 | }; | |
3111 | ||
529bd6c4 TI |
3112 | /* |
3113 | * get the empty PCM device number to assign | |
3114 | */ | |
36bb00d4 | 3115 | static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) |
529bd6c4 | 3116 | { |
f5d6def5 | 3117 | /* audio device indices; not linear to keep compatibility */ |
36bb00d4 TI |
3118 | /* assigned to static slots up to dev#10; if more needed, assign |
3119 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) | |
3120 | */ | |
f5d6def5 WF |
3121 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
3122 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, | |
3123 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, | |
92608bad | 3124 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
f5d6def5 | 3125 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
529bd6c4 | 3126 | }; |
f5d6def5 WF |
3127 | int i; |
3128 | ||
3129 | if (type >= HDA_PCM_NTYPES) { | |
4e76a883 | 3130 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
529bd6c4 TI |
3131 | return -EINVAL; |
3132 | } | |
f5d6def5 | 3133 | |
36bb00d4 TI |
3134 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
3135 | #ifndef CONFIG_SND_DYNAMIC_MINORS | |
3136 | if (audio_idx[type][i] >= 8) | |
3137 | break; | |
3138 | #endif | |
f5d6def5 WF |
3139 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
3140 | return audio_idx[type][i]; | |
36bb00d4 | 3141 | } |
f5d6def5 | 3142 | |
36bb00d4 | 3143 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
01b65bfb TI |
3144 | /* non-fixed slots starting from 10 */ |
3145 | for (i = 10; i < 32; i++) { | |
3146 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) | |
3147 | return i; | |
3148 | } | |
36bb00d4 | 3149 | #endif |
01b65bfb | 3150 | |
4e76a883 | 3151 | dev_warn(bus->card->dev, "Too many %s devices\n", |
28aedaf7 | 3152 | snd_hda_pcm_type_name[type]); |
36bb00d4 | 3153 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
4e76a883 TI |
3154 | dev_warn(bus->card->dev, |
3155 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); | |
36bb00d4 | 3156 | #endif |
f5d6def5 | 3157 | return -EAGAIN; |
529bd6c4 TI |
3158 | } |
3159 | ||
1a4ba30c TI |
3160 | /* call build_pcms ops of the given codec and set up the default parameters */ |
3161 | int snd_hda_codec_parse_pcms(struct hda_codec *codec) | |
176d5335 | 3162 | { |
bbbc7e85 | 3163 | struct hda_pcm *cpcm; |
1a4ba30c | 3164 | int err; |
176d5335 | 3165 | |
bbbc7e85 | 3166 | if (!list_empty(&codec->pcm_list_head)) |
1a4ba30c TI |
3167 | return 0; /* already parsed */ |
3168 | ||
3169 | if (!codec->patch_ops.build_pcms) | |
3170 | return 0; | |
3171 | ||
3172 | err = codec->patch_ops.build_pcms(codec); | |
3173 | if (err < 0) { | |
3174 | codec_err(codec, "cannot build PCMs for #%d (error %d)\n", | |
d068ebc2 | 3175 | codec->core.addr, err); |
1a4ba30c TI |
3176 | return err; |
3177 | } | |
3178 | ||
bbbc7e85 | 3179 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
1a4ba30c TI |
3180 | int stream; |
3181 | ||
3182 | for (stream = 0; stream < 2; stream++) { | |
3183 | struct hda_pcm_stream *info = &cpcm->stream[stream]; | |
3184 | ||
3185 | if (!info->substreams) | |
3186 | continue; | |
176d5335 | 3187 | err = set_pcm_default_values(codec, info); |
1a4ba30c TI |
3188 | if (err < 0) { |
3189 | codec_warn(codec, | |
3190 | "fail to setup default for PCM %s\n", | |
3191 | cpcm->name); | |
176d5335 | 3192 | return err; |
1a4ba30c | 3193 | } |
176d5335 TI |
3194 | } |
3195 | } | |
1a4ba30c TI |
3196 | |
3197 | return 0; | |
176d5335 TI |
3198 | } |
3199 | ||
529bd6c4 TI |
3200 | /* assign all PCMs of the given codec */ |
3201 | int snd_hda_codec_build_pcms(struct hda_codec *codec) | |
3202 | { | |
1a4ba30c | 3203 | struct hda_bus *bus = codec->bus; |
bbbc7e85 | 3204 | struct hda_pcm *cpcm; |
1a4ba30c | 3205 | int dev, err; |
529bd6c4 | 3206 | |
1a4ba30c | 3207 | err = snd_hda_codec_parse_pcms(codec); |
d289619a | 3208 | if (err < 0) |
1a4ba30c | 3209 | return err; |
1a4ba30c TI |
3210 | |
3211 | /* attach a new PCM streams */ | |
bbbc7e85 | 3212 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
1a4ba30c TI |
3213 | if (cpcm->pcm) |
3214 | continue; /* already attached */ | |
529bd6c4 | 3215 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
41b5b01a | 3216 | continue; /* no substreams assigned */ |
529bd6c4 | 3217 | |
1a4ba30c | 3218 | dev = get_empty_pcm_device(bus, cpcm->pcm_type); |
1f7f51a6 WY |
3219 | if (dev < 0) { |
3220 | cpcm->device = SNDRV_PCM_INVALID_DEVICE; | |
1a4ba30c | 3221 | continue; /* no fatal error */ |
1f7f51a6 | 3222 | } |
1a4ba30c | 3223 | cpcm->device = dev; |
0a50575b | 3224 | err = snd_hda_attach_pcm_stream(bus, codec, cpcm); |
1a4ba30c TI |
3225 | if (err < 0) { |
3226 | codec_err(codec, | |
3227 | "cannot attach PCM stream %d for codec #%d\n", | |
d068ebc2 | 3228 | dev, codec->core.addr); |
1a4ba30c | 3229 | continue; /* no fatal error */ |
529bd6c4 TI |
3230 | } |
3231 | } | |
1a4ba30c | 3232 | |
529bd6c4 TI |
3233 | return 0; |
3234 | } | |
3235 | ||
1da177e4 LT |
3236 | /** |
3237 | * snd_hda_add_new_ctls - create controls from the array | |
3238 | * @codec: the HDA codec | |
c8b6bf9b | 3239 | * @knew: the array of struct snd_kcontrol_new |
1da177e4 LT |
3240 | * |
3241 | * This helper function creates and add new controls in the given array. | |
3242 | * The array must be terminated with an empty entry as terminator. | |
3243 | * | |
3244 | * Returns 0 if successful, or a negative error code. | |
3245 | */ | |
031024ee TI |
3246 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
3247 | const struct snd_kcontrol_new *knew) | |
1da177e4 | 3248 | { |
4d02d1b6 | 3249 | int err; |
1da177e4 LT |
3250 | |
3251 | for (; knew->name; knew++) { | |
54d17403 | 3252 | struct snd_kcontrol *kctl; |
1afe206a | 3253 | int addr = 0, idx = 0; |
5b0cb1d8 JK |
3254 | if (knew->iface == -1) /* skip this codec private value */ |
3255 | continue; | |
1afe206a | 3256 | for (;;) { |
54d17403 | 3257 | kctl = snd_ctl_new1(knew, codec); |
0ba21762 | 3258 | if (!kctl) |
54d17403 | 3259 | return -ENOMEM; |
1afe206a TI |
3260 | if (addr > 0) |
3261 | kctl->id.device = addr; | |
3262 | if (idx > 0) | |
3263 | kctl->id.index = idx; | |
3911a4c1 | 3264 | err = snd_hda_ctl_add(codec, 0, kctl); |
1afe206a TI |
3265 | if (!err) |
3266 | break; | |
3267 | /* try first with another device index corresponding to | |
3268 | * the codec addr; if it still fails (or it's the | |
3269 | * primary codec), then try another control index | |
3270 | */ | |
d068ebc2 TI |
3271 | if (!addr && codec->core.addr) |
3272 | addr = codec->core.addr; | |
1afe206a TI |
3273 | else if (!idx && !knew->index) { |
3274 | idx = find_empty_mixer_ctl_idx(codec, | |
dcda5806 | 3275 | knew->name, 0); |
1afe206a TI |
3276 | if (idx <= 0) |
3277 | return err; | |
3278 | } else | |
54d17403 TI |
3279 | return err; |
3280 | } | |
1da177e4 LT |
3281 | } |
3282 | return 0; | |
3283 | } | |
2698ea98 | 3284 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
1da177e4 | 3285 | |
83012a7c | 3286 | #ifdef CONFIG_PM |
bb573928 | 3287 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
cb53c626 | 3288 | { |
cc72da7d | 3289 | struct device *dev = hda_codec_dev(codec); |
cc72da7d | 3290 | |
2377c3c3 LH |
3291 | if (delay == 0 && codec->auto_runtime_pm) |
3292 | delay = 3000; | |
3293 | ||
cc72da7d TI |
3294 | if (delay > 0) { |
3295 | pm_runtime_set_autosuspend_delay(dev, delay); | |
3296 | pm_runtime_use_autosuspend(dev); | |
3297 | pm_runtime_allow(dev); | |
3298 | if (!pm_runtime_suspended(dev)) | |
3299 | pm_runtime_mark_last_busy(dev); | |
3300 | } else { | |
3301 | pm_runtime_dont_use_autosuspend(dev); | |
3302 | pm_runtime_forbid(dev); | |
3303 | } | |
cb53c626 | 3304 | } |
bb573928 TI |
3305 | |
3306 | /** | |
3307 | * snd_hda_set_power_save - reprogram autosuspend for the given delay | |
3308 | * @bus: HD-audio bus | |
3309 | * @delay: autosuspend delay in msec, 0 = off | |
3310 | * | |
3311 | * Synchronize the runtime PM autosuspend state from the power_save option. | |
3312 | */ | |
3313 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) | |
3314 | { | |
3315 | struct hda_codec *c; | |
3316 | ||
d068ebc2 | 3317 | list_for_each_codec(c, bus) |
bb573928 TI |
3318 | codec_set_power_save(c, delay); |
3319 | } | |
3320 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); | |
cb53c626 | 3321 | |
d5191e50 TI |
3322 | /** |
3323 | * snd_hda_check_amp_list_power - Check the amp list and update the power | |
3324 | * @codec: HD-audio codec | |
3325 | * @check: the object containing an AMP list and the status | |
3326 | * @nid: NID to check / update | |
3327 | * | |
3328 | * Check whether the given NID is in the amp list. If it's in the list, | |
3329 | * check the current AMP status, and update the the power-status according | |
3330 | * to the mute status. | |
3331 | * | |
3332 | * This function is supposed to be set or called from the check_power_status | |
3333 | * patch ops. | |
28aedaf7 | 3334 | */ |
cb53c626 TI |
3335 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
3336 | struct hda_loopback_check *check, | |
3337 | hda_nid_t nid) | |
3338 | { | |
031024ee | 3339 | const struct hda_amp_list *p; |
cb53c626 TI |
3340 | int ch, v; |
3341 | ||
3342 | if (!check->amplist) | |
3343 | return 0; | |
3344 | for (p = check->amplist; p->nid; p++) { | |
3345 | if (p->nid == nid) | |
3346 | break; | |
3347 | } | |
3348 | if (!p->nid) | |
3349 | return 0; /* nothing changed */ | |
3350 | ||
3351 | for (p = check->amplist; p->nid; p++) { | |
3352 | for (ch = 0; ch < 2; ch++) { | |
3353 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, | |
3354 | p->idx); | |
3355 | if (!(v & HDA_AMP_MUTE) && v > 0) { | |
3356 | if (!check->power_on) { | |
3357 | check->power_on = 1; | |
664c7155 | 3358 | snd_hda_power_up_pm(codec); |
cb53c626 TI |
3359 | } |
3360 | return 1; | |
3361 | } | |
3362 | } | |
3363 | } | |
3364 | if (check->power_on) { | |
3365 | check->power_on = 0; | |
664c7155 | 3366 | snd_hda_power_down_pm(codec); |
cb53c626 TI |
3367 | } |
3368 | return 0; | |
3369 | } | |
2698ea98 | 3370 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
cb53c626 | 3371 | #endif |
1da177e4 LT |
3372 | |
3373 | /* | |
3374 | * input MUX helper | |
3375 | */ | |
d5191e50 TI |
3376 | |
3377 | /** | |
3378 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum | |
a11e9b16 TI |
3379 | * @imux: imux helper object |
3380 | * @uinfo: pointer to get/store the data | |
d5191e50 | 3381 | */ |
0ba21762 TI |
3382 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
3383 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
3384 | { |
3385 | unsigned int index; | |
3386 | ||
3387 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
3388 | uinfo->count = 1; | |
3389 | uinfo->value.enumerated.items = imux->num_items; | |
5513b0c5 TI |
3390 | if (!imux->num_items) |
3391 | return 0; | |
1da177e4 LT |
3392 | index = uinfo->value.enumerated.item; |
3393 | if (index >= imux->num_items) | |
3394 | index = imux->num_items - 1; | |
3395 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); | |
3396 | return 0; | |
3397 | } | |
2698ea98 | 3398 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
1da177e4 | 3399 | |
d5191e50 TI |
3400 | /** |
3401 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum | |
a11e9b16 TI |
3402 | * @codec: the HDA codec |
3403 | * @imux: imux helper object | |
3404 | * @ucontrol: pointer to get/store the data | |
3405 | * @nid: input mux NID | |
3406 | * @cur_val: pointer to get/store the current imux value | |
d5191e50 | 3407 | */ |
0ba21762 TI |
3408 | int snd_hda_input_mux_put(struct hda_codec *codec, |
3409 | const struct hda_input_mux *imux, | |
3410 | struct snd_ctl_elem_value *ucontrol, | |
3411 | hda_nid_t nid, | |
1da177e4 LT |
3412 | unsigned int *cur_val) |
3413 | { | |
3414 | unsigned int idx; | |
3415 | ||
5513b0c5 TI |
3416 | if (!imux->num_items) |
3417 | return 0; | |
1da177e4 LT |
3418 | idx = ucontrol->value.enumerated.item[0]; |
3419 | if (idx >= imux->num_items) | |
3420 | idx = imux->num_items - 1; | |
82beb8fd | 3421 | if (*cur_val == idx) |
1da177e4 | 3422 | return 0; |
82beb8fd TI |
3423 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
3424 | imux->items[idx].index); | |
1da177e4 LT |
3425 | *cur_val = idx; |
3426 | return 1; | |
3427 | } | |
2698ea98 | 3428 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
1da177e4 LT |
3429 | |
3430 | ||
a11e9b16 TI |
3431 | /** |
3432 | * snd_hda_enum_helper_info - Helper for simple enum ctls | |
3433 | * @kcontrol: ctl element | |
3434 | * @uinfo: pointer to get/store the data | |
3435 | * @num_items: number of enum items | |
3436 | * @texts: enum item string array | |
3437 | * | |
dda415d4 TI |
3438 | * process kcontrol info callback of a simple string enum array |
3439 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array | |
3440 | */ | |
3441 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, | |
3442 | struct snd_ctl_elem_info *uinfo, | |
3443 | int num_items, const char * const *texts) | |
3444 | { | |
3445 | static const char * const texts_default[] = { | |
3446 | "Disabled", "Enabled" | |
3447 | }; | |
3448 | ||
3449 | if (!texts || !num_items) { | |
3450 | num_items = 2; | |
3451 | texts = texts_default; | |
3452 | } | |
3453 | ||
3ff72219 | 3454 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
dda415d4 | 3455 | } |
2698ea98 | 3456 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
dda415d4 | 3457 | |
1da177e4 LT |
3458 | /* |
3459 | * Multi-channel / digital-out PCM helper functions | |
3460 | */ | |
3461 | ||
6b97eb45 TI |
3462 | /* setup SPDIF output stream */ |
3463 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, | |
3464 | unsigned int stream_tag, unsigned int format) | |
3465 | { | |
3bef1c37 LD |
3466 | struct hda_spdif_out *spdif; |
3467 | unsigned int curr_fmt; | |
3468 | bool reset; | |
3469 | ||
3470 | spdif = snd_hda_spdif_out_of_nid(codec, nid); | |
960a581e LY |
3471 | /* Add sanity check to pass klockwork check. |
3472 | * This should never happen. | |
3473 | */ | |
3474 | if (WARN_ON(spdif == NULL)) | |
3475 | return; | |
3476 | ||
3bef1c37 LD |
3477 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
3478 | AC_VERB_GET_STREAM_FORMAT, 0); | |
3479 | reset = codec->spdif_status_reset && | |
3480 | (spdif->ctls & AC_DIG1_ENABLE) && | |
3481 | curr_fmt != format; | |
3482 | ||
3483 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be | |
3484 | updated */ | |
3485 | if (reset) | |
28aedaf7 | 3486 | set_dig_out_convert(codec, nid, |
7c935976 | 3487 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
2f72853c | 3488 | -1); |
6b97eb45 | 3489 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
2f72853c | 3490 | if (codec->slave_dig_outs) { |
dda14410 | 3491 | const hda_nid_t *d; |
2f72853c TI |
3492 | for (d = codec->slave_dig_outs; *d; d++) |
3493 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, | |
3494 | format); | |
3495 | } | |
6b97eb45 | 3496 | /* turn on again (if needed) */ |
3bef1c37 | 3497 | if (reset) |
2f72853c | 3498 | set_dig_out_convert(codec, nid, |
7c935976 | 3499 | spdif->ctls & 0xff, -1); |
2f72853c | 3500 | } |
de51ca12 | 3501 | |
2f72853c TI |
3502 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
3503 | { | |
3504 | snd_hda_codec_cleanup_stream(codec, nid); | |
3505 | if (codec->slave_dig_outs) { | |
dda14410 | 3506 | const hda_nid_t *d; |
2f72853c TI |
3507 | for (d = codec->slave_dig_outs; *d; d++) |
3508 | snd_hda_codec_cleanup_stream(codec, *d); | |
de51ca12 | 3509 | } |
6b97eb45 TI |
3510 | } |
3511 | ||
d5191e50 TI |
3512 | /** |
3513 | * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode | |
a11e9b16 TI |
3514 | * @codec: the HDA codec |
3515 | * @mout: hda_multi_out object | |
1da177e4 | 3516 | */ |
0ba21762 TI |
3517 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
3518 | struct hda_multi_out *mout) | |
1da177e4 | 3519 | { |
62932df8 | 3520 | mutex_lock(&codec->spdif_mutex); |
5930ca41 TI |
3521 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
3522 | /* already opened as analog dup; reset it once */ | |
2f72853c | 3523 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 | 3524 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
62932df8 | 3525 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3526 | return 0; |
3527 | } | |
2698ea98 | 3528 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
1da177e4 | 3529 | |
d5191e50 TI |
3530 | /** |
3531 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream | |
a11e9b16 TI |
3532 | * @codec: the HDA codec |
3533 | * @mout: hda_multi_out object | |
3534 | * @stream_tag: stream tag to assign | |
3535 | * @format: format id to assign | |
3536 | * @substream: PCM substream to assign | |
d5191e50 | 3537 | */ |
6b97eb45 TI |
3538 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
3539 | struct hda_multi_out *mout, | |
3540 | unsigned int stream_tag, | |
3541 | unsigned int format, | |
3542 | struct snd_pcm_substream *substream) | |
3543 | { | |
3544 | mutex_lock(&codec->spdif_mutex); | |
3545 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); | |
3546 | mutex_unlock(&codec->spdif_mutex); | |
3547 | return 0; | |
3548 | } | |
2698ea98 | 3549 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
6b97eb45 | 3550 | |
d5191e50 TI |
3551 | /** |
3552 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream | |
a11e9b16 TI |
3553 | * @codec: the HDA codec |
3554 | * @mout: hda_multi_out object | |
d5191e50 | 3555 | */ |
9411e21c TI |
3556 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
3557 | struct hda_multi_out *mout) | |
3558 | { | |
3559 | mutex_lock(&codec->spdif_mutex); | |
3560 | cleanup_dig_out_stream(codec, mout->dig_out_nid); | |
3561 | mutex_unlock(&codec->spdif_mutex); | |
3562 | return 0; | |
3563 | } | |
2698ea98 | 3564 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
9411e21c | 3565 | |
d5191e50 TI |
3566 | /** |
3567 | * snd_hda_multi_out_dig_close - release the digital out stream | |
a11e9b16 TI |
3568 | * @codec: the HDA codec |
3569 | * @mout: hda_multi_out object | |
1da177e4 | 3570 | */ |
0ba21762 TI |
3571 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
3572 | struct hda_multi_out *mout) | |
1da177e4 | 3573 | { |
62932df8 | 3574 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 3575 | mout->dig_out_used = 0; |
62932df8 | 3576 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3577 | return 0; |
3578 | } | |
2698ea98 | 3579 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
1da177e4 | 3580 | |
d5191e50 TI |
3581 | /** |
3582 | * snd_hda_multi_out_analog_open - open analog outputs | |
a11e9b16 TI |
3583 | * @codec: the HDA codec |
3584 | * @mout: hda_multi_out object | |
3585 | * @substream: PCM substream to assign | |
3586 | * @hinfo: PCM information to assign | |
d5191e50 TI |
3587 | * |
3588 | * Open analog outputs and set up the hw-constraints. | |
3589 | * If the digital outputs can be opened as slave, open the digital | |
3590 | * outputs, too. | |
1da177e4 | 3591 | */ |
0ba21762 TI |
3592 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
3593 | struct hda_multi_out *mout, | |
9a08160b TI |
3594 | struct snd_pcm_substream *substream, |
3595 | struct hda_pcm_stream *hinfo) | |
3596 | { | |
3597 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3598 | runtime->hw.channels_max = mout->max_channels; | |
3599 | if (mout->dig_out_nid) { | |
3600 | if (!mout->analog_rates) { | |
3601 | mout->analog_rates = hinfo->rates; | |
3602 | mout->analog_formats = hinfo->formats; | |
3603 | mout->analog_maxbps = hinfo->maxbps; | |
3604 | } else { | |
3605 | runtime->hw.rates = mout->analog_rates; | |
3606 | runtime->hw.formats = mout->analog_formats; | |
3607 | hinfo->maxbps = mout->analog_maxbps; | |
3608 | } | |
3609 | if (!mout->spdif_rates) { | |
3610 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, | |
3611 | &mout->spdif_rates, | |
3612 | &mout->spdif_formats, | |
3613 | &mout->spdif_maxbps); | |
3614 | } | |
3615 | mutex_lock(&codec->spdif_mutex); | |
3616 | if (mout->share_spdif) { | |
022b466f TI |
3617 | if ((runtime->hw.rates & mout->spdif_rates) && |
3618 | (runtime->hw.formats & mout->spdif_formats)) { | |
3619 | runtime->hw.rates &= mout->spdif_rates; | |
3620 | runtime->hw.formats &= mout->spdif_formats; | |
3621 | if (mout->spdif_maxbps < hinfo->maxbps) | |
3622 | hinfo->maxbps = mout->spdif_maxbps; | |
3623 | } else { | |
3624 | mout->share_spdif = 0; | |
3625 | /* FIXME: need notify? */ | |
3626 | } | |
9a08160b | 3627 | } |
eaa9985b | 3628 | mutex_unlock(&codec->spdif_mutex); |
9a08160b | 3629 | } |
1da177e4 LT |
3630 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
3631 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); | |
3632 | } | |
2698ea98 | 3633 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
1da177e4 | 3634 | |
d5191e50 TI |
3635 | /** |
3636 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. | |
a11e9b16 TI |
3637 | * @codec: the HDA codec |
3638 | * @mout: hda_multi_out object | |
3639 | * @stream_tag: stream tag to assign | |
3640 | * @format: format id to assign | |
3641 | * @substream: PCM substream to assign | |
d5191e50 TI |
3642 | * |
3643 | * Set up the i/o for analog out. | |
3644 | * When the digital out is available, copy the front out to digital out, too. | |
1da177e4 | 3645 | */ |
0ba21762 TI |
3646 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
3647 | struct hda_multi_out *mout, | |
1da177e4 LT |
3648 | unsigned int stream_tag, |
3649 | unsigned int format, | |
c8b6bf9b | 3650 | struct snd_pcm_substream *substream) |
1da177e4 | 3651 | { |
dda14410 | 3652 | const hda_nid_t *nids = mout->dac_nids; |
1da177e4 | 3653 | int chs = substream->runtime->channels; |
e3245cdd | 3654 | struct hda_spdif_out *spdif; |
1da177e4 LT |
3655 | int i; |
3656 | ||
62932df8 | 3657 | mutex_lock(&codec->spdif_mutex); |
e3245cdd | 3658 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
9a08160b TI |
3659 | if (mout->dig_out_nid && mout->share_spdif && |
3660 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { | |
960a581e | 3661 | if (chs == 2 && spdif != NULL && |
0ba21762 TI |
3662 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
3663 | format) && | |
7c935976 | 3664 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
1da177e4 | 3665 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
6b97eb45 TI |
3666 | setup_dig_out_stream(codec, mout->dig_out_nid, |
3667 | stream_tag, format); | |
1da177e4 LT |
3668 | } else { |
3669 | mout->dig_out_used = 0; | |
2f72853c | 3670 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 LT |
3671 | } |
3672 | } | |
62932df8 | 3673 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3674 | |
3675 | /* front */ | |
0ba21762 TI |
3676 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
3677 | 0, format); | |
d29240ce TI |
3678 | if (!mout->no_share_stream && |
3679 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) | |
1da177e4 | 3680 | /* headphone out will just decode front left/right (stereo) */ |
0ba21762 TI |
3681 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
3682 | 0, format); | |
82bc955f | 3683 | /* extra outputs copied from front */ |
a06dbfc2 TI |
3684 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
3685 | if (!mout->no_share_stream && mout->hp_out_nid[i]) | |
3686 | snd_hda_codec_setup_stream(codec, | |
3687 | mout->hp_out_nid[i], | |
3688 | stream_tag, 0, format); | |
82bc955f | 3689 | |
1da177e4 LT |
3690 | /* surrounds */ |
3691 | for (i = 1; i < mout->num_dacs; i++) { | |
4b3acaf5 | 3692 | if (chs >= (i + 1) * 2) /* independent out */ |
0ba21762 TI |
3693 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
3694 | i * 2, format); | |
d29240ce | 3695 | else if (!mout->no_share_stream) /* copy front */ |
0ba21762 TI |
3696 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
3697 | 0, format); | |
1da177e4 | 3698 | } |
cd4035e8 DH |
3699 | |
3700 | /* extra surrounds */ | |
3701 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { | |
3702 | int ch = 0; | |
3703 | if (!mout->extra_out_nid[i]) | |
3704 | break; | |
3705 | if (chs >= (i + 1) * 2) | |
3706 | ch = i * 2; | |
3707 | else if (!mout->no_share_stream) | |
3708 | break; | |
3709 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], | |
3710 | stream_tag, ch, format); | |
3711 | } | |
3712 | ||
1da177e4 LT |
3713 | return 0; |
3714 | } | |
2698ea98 | 3715 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
1da177e4 | 3716 | |
d5191e50 TI |
3717 | /** |
3718 | * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out | |
a11e9b16 TI |
3719 | * @codec: the HDA codec |
3720 | * @mout: hda_multi_out object | |
1da177e4 | 3721 | */ |
0ba21762 TI |
3722 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
3723 | struct hda_multi_out *mout) | |
1da177e4 | 3724 | { |
dda14410 | 3725 | const hda_nid_t *nids = mout->dac_nids; |
1da177e4 LT |
3726 | int i; |
3727 | ||
3728 | for (i = 0; i < mout->num_dacs; i++) | |
888afa15 | 3729 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
1da177e4 | 3730 | if (mout->hp_nid) |
888afa15 | 3731 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
a06dbfc2 TI |
3732 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
3733 | if (mout->hp_out_nid[i]) | |
3734 | snd_hda_codec_cleanup_stream(codec, | |
3735 | mout->hp_out_nid[i]); | |
82bc955f TI |
3736 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
3737 | if (mout->extra_out_nid[i]) | |
888afa15 TI |
3738 | snd_hda_codec_cleanup_stream(codec, |
3739 | mout->extra_out_nid[i]); | |
62932df8 | 3740 | mutex_lock(&codec->spdif_mutex); |
1da177e4 | 3741 | if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { |
2f72853c | 3742 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
1da177e4 LT |
3743 | mout->dig_out_used = 0; |
3744 | } | |
62932df8 | 3745 | mutex_unlock(&codec->spdif_mutex); |
1da177e4 LT |
3746 | return 0; |
3747 | } | |
2698ea98 | 3748 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
1da177e4 | 3749 | |
4740860b TI |
3750 | /** |
3751 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits | |
a11e9b16 TI |
3752 | * @codec: the HDA codec |
3753 | * @pin: referred pin NID | |
4740860b TI |
3754 | * |
3755 | * Guess the suitable VREF pin bits to be set as the pin-control value. | |
3756 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. | |
3757 | */ | |
3758 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) | |
3759 | { | |
3760 | unsigned int pincap; | |
3761 | unsigned int oldval; | |
3762 | oldval = snd_hda_codec_read(codec, pin, 0, | |
3763 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | |
3764 | pincap = snd_hda_query_pin_caps(codec, pin); | |
3765 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; | |
3766 | /* Exception: if the default pin setup is vref50, we give it priority */ | |
3767 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) | |
3768 | return AC_PINCTL_VREF_80; | |
3769 | else if (pincap & AC_PINCAP_VREF_50) | |
3770 | return AC_PINCTL_VREF_50; | |
3771 | else if (pincap & AC_PINCAP_VREF_100) | |
3772 | return AC_PINCTL_VREF_100; | |
3773 | else if (pincap & AC_PINCAP_VREF_GRD) | |
3774 | return AC_PINCTL_VREF_GRD; | |
3775 | return AC_PINCTL_VREF_HIZ; | |
3776 | } | |
2698ea98 | 3777 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
4740860b | 3778 | |
a11e9b16 TI |
3779 | /** |
3780 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap | |
3781 | * @codec: the HDA codec | |
3782 | * @pin: referred pin NID | |
3783 | * @val: pin ctl value to audit | |
3784 | */ | |
62f3a2f7 TI |
3785 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
3786 | hda_nid_t pin, unsigned int val) | |
3787 | { | |
3788 | static unsigned int cap_lists[][2] = { | |
3789 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, | |
3790 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, | |
3791 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, | |
3792 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, | |
3793 | }; | |
3794 | unsigned int cap; | |
3795 | ||
3796 | if (!val) | |
3797 | return 0; | |
3798 | cap = snd_hda_query_pin_caps(codec, pin); | |
3799 | if (!cap) | |
3800 | return val; /* don't know what to do... */ | |
3801 | ||
3802 | if (val & AC_PINCTL_OUT_EN) { | |
3803 | if (!(cap & AC_PINCAP_OUT)) | |
3804 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); | |
3805 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) | |
3806 | val &= ~AC_PINCTL_HP_EN; | |
3807 | } | |
3808 | ||
3809 | if (val & AC_PINCTL_IN_EN) { | |
3810 | if (!(cap & AC_PINCAP_IN)) | |
3811 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); | |
3812 | else { | |
3813 | unsigned int vcap, vref; | |
3814 | int i; | |
3815 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; | |
3816 | vref = val & AC_PINCTL_VREFEN; | |
3817 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { | |
3818 | if (vref == cap_lists[i][0] && | |
3819 | !(vcap & cap_lists[i][1])) { | |
3820 | if (i == ARRAY_SIZE(cap_lists) - 1) | |
3821 | vref = AC_PINCTL_VREF_HIZ; | |
3822 | else | |
3823 | vref = cap_lists[i + 1][0]; | |
3824 | } | |
3825 | } | |
3826 | val &= ~AC_PINCTL_VREFEN; | |
3827 | val |= vref; | |
3828 | } | |
3829 | } | |
3830 | ||
3831 | return val; | |
3832 | } | |
2698ea98 | 3833 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
62f3a2f7 | 3834 | |
a11e9b16 TI |
3835 | /** |
3836 | * _snd_hda_pin_ctl - Helper to set pin ctl value | |
3837 | * @codec: the HDA codec | |
3838 | * @pin: referred pin NID | |
3839 | * @val: pin control value to set | |
3840 | * @cached: access over codec pinctl cache or direct write | |
3841 | * | |
3842 | * This function is a helper to set a pin ctl value more safely. | |
3843 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the | |
3844 | * value in pin target array via snd_hda_codec_set_pin_target(), then | |
3845 | * actually writes the value via either snd_hda_codec_update_cache() or | |
3846 | * snd_hda_codec_write() depending on @cached flag. | |
3847 | */ | |
cdd03ced TI |
3848 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
3849 | unsigned int val, bool cached) | |
3850 | { | |
62f3a2f7 | 3851 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
d7fdc00a | 3852 | snd_hda_codec_set_pin_target(codec, pin, val); |
cdd03ced TI |
3853 | if (cached) |
3854 | return snd_hda_codec_update_cache(codec, pin, 0, | |
3855 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); | |
3856 | else | |
3857 | return snd_hda_codec_write(codec, pin, 0, | |
3858 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); | |
3859 | } | |
2698ea98 | 3860 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
cdd03ced | 3861 | |
990061c2 TI |
3862 | /** |
3863 | * snd_hda_add_imux_item - Add an item to input_mux | |
a11e9b16 TI |
3864 | * @codec: the HDA codec |
3865 | * @imux: imux helper object | |
3866 | * @label: the name of imux item to assign | |
3867 | * @index: index number of imux item to assign | |
3868 | * @type_idx: pointer to store the resultant label index | |
990061c2 TI |
3869 | * |
3870 | * When the same label is used already in the existing items, the number | |
3871 | * suffix is appended to the label. This label index number is stored | |
3872 | * to type_idx when non-NULL pointer is given. | |
3873 | */ | |
6194b99d TI |
3874 | int snd_hda_add_imux_item(struct hda_codec *codec, |
3875 | struct hda_input_mux *imux, const char *label, | |
10a20af7 TI |
3876 | int index, int *type_idx) |
3877 | { | |
3878 | int i, label_idx = 0; | |
3879 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { | |
6194b99d | 3880 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
10a20af7 TI |
3881 | return -EINVAL; |
3882 | } | |
3883 | for (i = 0; i < imux->num_items; i++) { | |
3884 | if (!strncmp(label, imux->items[i].label, strlen(label))) | |
3885 | label_idx++; | |
d7b1ae9d | 3886 | } |
10a20af7 TI |
3887 | if (type_idx) |
3888 | *type_idx = label_idx; | |
3889 | if (label_idx > 0) | |
3890 | snprintf(imux->items[imux->num_items].label, | |
3891 | sizeof(imux->items[imux->num_items].label), | |
3892 | "%s %d", label, label_idx); | |
b5786e85 | 3893 | else |
10a20af7 TI |
3894 | strlcpy(imux->items[imux->num_items].label, label, |
3895 | sizeof(imux->items[imux->num_items].label)); | |
3896 | imux->items[imux->num_items].index = index; | |
3897 | imux->num_items++; | |
3898 | return 0; | |
d7b1ae9d | 3899 | } |
2698ea98 | 3900 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
d7b1ae9d | 3901 | |
1da177e4 | 3902 | /** |
0a50575b | 3903 | * snd_hda_bus_reset_codecs - Reset the bus |
59ed1ead | 3904 | * @bus: HD-audio bus |
1da177e4 | 3905 | */ |
0a50575b | 3906 | void snd_hda_bus_reset_codecs(struct hda_bus *bus) |
1da177e4 | 3907 | { |
0ba21762 | 3908 | struct hda_codec *codec; |
1da177e4 | 3909 | |
d068ebc2 | 3910 | list_for_each_codec(codec, bus) { |
59ed1ead | 3911 | /* FIXME: maybe a better way needed for forced reset */ |
26a6cb6c | 3912 | cancel_delayed_work_sync(&codec->jackpoll_work); |
59ed1ead | 3913 | #ifdef CONFIG_PM |
0e24dbb7 | 3914 | if (hda_codec_is_power_on(codec)) { |
cc72da7d | 3915 | hda_call_codec_suspend(codec); |
12edb893 | 3916 | hda_call_codec_resume(codec); |
59ed1ead TI |
3917 | } |
3918 | #endif | |
1da177e4 | 3919 | } |
1da177e4 | 3920 | } |
b2e18597 | 3921 | |
d5191e50 TI |
3922 | /** |
3923 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer | |
3924 | * @pcm: PCM caps bits | |
3925 | * @buf: the string buffer to write | |
3926 | * @buflen: the max buffer length | |
3927 | * | |
3928 | * used by hda_proc.c and hda_eld.c | |
3929 | */ | |
b2022266 TI |
3930 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
3931 | { | |
3932 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; | |
3933 | int i, j; | |
3934 | ||
3935 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) | |
3936 | if (pcm & (AC_SUPPCM_BITS_8 << i)) | |
3937 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); | |
3938 | ||
3939 | buf[j] = '\0'; /* necessary when j == 0 */ | |
3940 | } | |
2698ea98 | 3941 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
1289e9e8 TI |
3942 | |
3943 | MODULE_DESCRIPTION("HDA codec core"); | |
3944 | MODULE_LICENSE("GPL"); |