]>
Commit | Line | Data |
---|---|---|
7639a06c TI |
1 | /* |
2 | * HD-audio codec core device | |
3 | */ | |
4 | ||
5 | #include <linux/init.h> | |
6 | #include <linux/device.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/export.h> | |
10 | #include <linux/pm_runtime.h> | |
11 | #include <sound/hdaudio.h> | |
01ed3c06 | 12 | #include <sound/hda_regmap.h> |
b7d023e1 | 13 | #include <sound/pcm.h> |
7639a06c TI |
14 | #include "local.h" |
15 | ||
16 | static void setup_fg_nodes(struct hdac_device *codec); | |
17 | static int get_codec_vendor_name(struct hdac_device *codec); | |
18 | ||
19 | static void default_release(struct device *dev) | |
20 | { | |
21 | snd_hdac_device_exit(container_of(dev, struct hdac_device, dev)); | |
22 | } | |
23 | ||
24 | /** | |
25 | * snd_hdac_device_init - initialize the HD-audio codec base device | |
26 | * @codec: device to initialize | |
27 | * @bus: but to attach | |
28 | * @name: device name string | |
29 | * @addr: codec address | |
30 | * | |
31 | * Returns zero for success or a negative error code. | |
32 | * | |
33 | * This function increments the runtime PM counter and marks it active. | |
34 | * The caller needs to turn it off appropriately later. | |
35 | * | |
36 | * The caller needs to set the device's release op properly by itself. | |
37 | */ | |
38 | int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus, | |
39 | const char *name, unsigned int addr) | |
40 | { | |
41 | struct device *dev; | |
42 | hda_nid_t fg; | |
43 | int err; | |
44 | ||
45 | dev = &codec->dev; | |
46 | device_initialize(dev); | |
47 | dev->parent = bus->dev; | |
48 | dev->bus = &snd_hda_bus_type; | |
49 | dev->release = default_release; | |
3256be65 | 50 | dev->groups = hdac_dev_attr_groups; |
7639a06c TI |
51 | dev_set_name(dev, "%s", name); |
52 | device_enable_async_suspend(dev); | |
53 | ||
54 | codec->bus = bus; | |
55 | codec->addr = addr; | |
56 | codec->type = HDA_DEV_CORE; | |
57 | pm_runtime_set_active(&codec->dev); | |
58 | pm_runtime_get_noresume(&codec->dev); | |
59 | atomic_set(&codec->in_pm, 0); | |
60 | ||
61 | err = snd_hdac_bus_add_device(bus, codec); | |
62 | if (err < 0) | |
63 | goto error; | |
64 | ||
65 | /* fill parameters */ | |
66 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
67 | AC_PAR_VENDOR_ID); | |
68 | if (codec->vendor_id == -1) { | |
69 | /* read again, hopefully the access method was corrected | |
70 | * in the last read... | |
71 | */ | |
72 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
73 | AC_PAR_VENDOR_ID); | |
74 | } | |
75 | ||
76 | codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
77 | AC_PAR_SUBSYSTEM_ID); | |
78 | codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
79 | AC_PAR_REV_ID); | |
80 | ||
81 | setup_fg_nodes(codec); | |
82 | if (!codec->afg && !codec->mfg) { | |
83 | dev_err(dev, "no AFG or MFG node found\n"); | |
84 | err = -ENODEV; | |
85 | goto error; | |
86 | } | |
87 | ||
88 | fg = codec->afg ? codec->afg : codec->mfg; | |
89 | ||
90 | err = snd_hdac_refresh_widgets(codec); | |
91 | if (err < 0) | |
92 | goto error; | |
93 | ||
94 | codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE); | |
95 | /* reread ssid if not set by parameter */ | |
ffda568e | 96 | if (codec->subsystem_id == -1 || codec->subsystem_id == 0) |
7639a06c TI |
97 | snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0, |
98 | &codec->subsystem_id); | |
99 | ||
100 | err = get_codec_vendor_name(codec); | |
101 | if (err < 0) | |
102 | goto error; | |
103 | ||
104 | codec->chip_name = kasprintf(GFP_KERNEL, "ID %x", | |
105 | codec->vendor_id & 0xffff); | |
106 | if (!codec->chip_name) { | |
107 | err = -ENOMEM; | |
108 | goto error; | |
109 | } | |
110 | ||
111 | return 0; | |
112 | ||
113 | error: | |
7639a06c TI |
114 | put_device(&codec->dev); |
115 | return err; | |
116 | } | |
117 | EXPORT_SYMBOL_GPL(snd_hdac_device_init); | |
118 | ||
119 | /** | |
120 | * snd_hdac_device_exit - clean up the HD-audio codec base device | |
121 | * @codec: device to clean up | |
122 | */ | |
123 | void snd_hdac_device_exit(struct hdac_device *codec) | |
124 | { | |
c4c2533f | 125 | pm_runtime_put_noidle(&codec->dev); |
7639a06c TI |
126 | snd_hdac_bus_remove_device(codec->bus, codec); |
127 | kfree(codec->vendor_name); | |
128 | kfree(codec->chip_name); | |
129 | } | |
130 | EXPORT_SYMBOL_GPL(snd_hdac_device_exit); | |
131 | ||
3256be65 TI |
132 | /** |
133 | * snd_hdac_device_register - register the hd-audio codec base device | |
134 | * codec: the device to register | |
135 | */ | |
136 | int snd_hdac_device_register(struct hdac_device *codec) | |
137 | { | |
138 | int err; | |
139 | ||
140 | err = device_add(&codec->dev); | |
141 | if (err < 0) | |
142 | return err; | |
143 | err = hda_widget_sysfs_init(codec); | |
144 | if (err < 0) { | |
145 | device_del(&codec->dev); | |
146 | return err; | |
147 | } | |
148 | ||
149 | return 0; | |
150 | } | |
151 | EXPORT_SYMBOL_GPL(snd_hdac_device_register); | |
152 | ||
153 | /** | |
154 | * snd_hdac_device_unregister - unregister the hd-audio codec base device | |
155 | * codec: the device to unregister | |
156 | */ | |
157 | void snd_hdac_device_unregister(struct hdac_device *codec) | |
158 | { | |
159 | if (device_is_registered(&codec->dev)) { | |
160 | hda_widget_sysfs_exit(codec); | |
161 | device_del(&codec->dev); | |
eb8d0eaa | 162 | snd_hdac_bus_remove_device(codec->bus, codec); |
3256be65 TI |
163 | } |
164 | } | |
165 | EXPORT_SYMBOL_GPL(snd_hdac_device_unregister); | |
166 | ||
ded255be TI |
167 | /** |
168 | * snd_hdac_device_set_chip_name - set/update the codec name | |
169 | * @codec: the HDAC device | |
170 | * @name: name string to set | |
171 | * | |
172 | * Returns 0 if the name is set or updated, or a negative error code. | |
173 | */ | |
174 | int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name) | |
175 | { | |
176 | char *newname; | |
177 | ||
178 | if (!name) | |
179 | return 0; | |
180 | newname = kstrdup(name, GFP_KERNEL); | |
181 | if (!newname) | |
182 | return -ENOMEM; | |
183 | kfree(codec->chip_name); | |
184 | codec->chip_name = newname; | |
185 | return 0; | |
186 | } | |
187 | EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name); | |
188 | ||
4f9e0c38 TI |
189 | /** |
190 | * snd_hdac_codec_modalias - give the module alias name | |
191 | * @codec: HDAC device | |
192 | * @buf: string buffer to store | |
193 | * @size: string buffer size | |
194 | * | |
195 | * Returns the size of string, like snprintf(), or a negative error code. | |
196 | */ | |
197 | int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size) | |
198 | { | |
199 | return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n", | |
200 | codec->vendor_id, codec->revision_id, codec->type); | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias); | |
203 | ||
7639a06c TI |
204 | /** |
205 | * snd_hdac_make_cmd - compose a 32bit command word to be sent to the | |
206 | * HD-audio controller | |
207 | * @codec: the codec object | |
208 | * @nid: NID to encode | |
209 | * @verb: verb to encode | |
210 | * @parm: parameter to encode | |
211 | * | |
212 | * Return an encoded command verb or -1 for error. | |
213 | */ | |
214 | unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, | |
215 | unsigned int verb, unsigned int parm) | |
216 | { | |
217 | u32 val, addr; | |
218 | ||
219 | addr = codec->addr; | |
220 | if ((addr & ~0xf) || (nid & ~0x7f) || | |
221 | (verb & ~0xfff) || (parm & ~0xffff)) { | |
222 | dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n", | |
223 | addr, nid, verb, parm); | |
224 | return -1; | |
225 | } | |
226 | ||
227 | val = addr << 28; | |
228 | val |= (u32)nid << 20; | |
229 | val |= verb << 8; | |
230 | val |= parm; | |
231 | return val; | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(snd_hdac_make_cmd); | |
234 | ||
05852448 TI |
235 | /** |
236 | * snd_hdac_exec_verb - execute an encoded verb | |
237 | * @codec: the codec object | |
238 | * @cmd: encoded verb to execute | |
239 | * @flags: optional flags, pass zero for default | |
240 | * @res: the pointer to store the result, NULL if running async | |
241 | * | |
242 | * Returns zero if successful, or a negative error code. | |
243 | * | |
244 | * This calls the exec_verb op when set in hdac_codec. If not, | |
245 | * call the default snd_hdac_bus_exec_verb(). | |
246 | */ | |
247 | int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd, | |
248 | unsigned int flags, unsigned int *res) | |
249 | { | |
250 | if (codec->exec_verb) | |
251 | return codec->exec_verb(codec, cmd, flags, res); | |
252 | return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res); | |
253 | } | |
254 | EXPORT_SYMBOL_GPL(snd_hdac_exec_verb); | |
255 | ||
256 | ||
7639a06c TI |
257 | /** |
258 | * snd_hdac_read - execute a verb | |
259 | * @codec: the codec object | |
260 | * @nid: NID to execute a verb | |
261 | * @verb: verb to execute | |
262 | * @parm: parameter for a verb | |
263 | * @res: the pointer to store the result, NULL if running async | |
264 | * | |
265 | * Returns zero if successful, or a negative error code. | |
266 | */ | |
267 | int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid, | |
268 | unsigned int verb, unsigned int parm, unsigned int *res) | |
269 | { | |
270 | unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm); | |
271 | ||
05852448 | 272 | return snd_hdac_exec_verb(codec, cmd, 0, res); |
7639a06c TI |
273 | } |
274 | EXPORT_SYMBOL_GPL(snd_hdac_read); | |
275 | ||
276 | /** | |
01ed3c06 | 277 | * _snd_hdac_read_parm - read a parmeter |
7639a06c | 278 | * |
01ed3c06 | 279 | * This function returns zero or an error unlike snd_hdac_read_parm(). |
7639a06c | 280 | */ |
01ed3c06 TI |
281 | int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm, |
282 | unsigned int *res) | |
7639a06c | 283 | { |
01ed3c06 | 284 | unsigned int cmd; |
7639a06c | 285 | |
01ed3c06 TI |
286 | cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; |
287 | return snd_hdac_regmap_read_raw(codec, cmd, res); | |
7639a06c | 288 | } |
01ed3c06 | 289 | EXPORT_SYMBOL_GPL(_snd_hdac_read_parm); |
7639a06c | 290 | |
9ba17b4d TI |
291 | /** |
292 | * snd_hdac_read_parm_uncached - read a codec parameter without caching | |
7639a06c TI |
293 | * @codec: the codec object |
294 | * @nid: NID to read a parameter | |
295 | * @parm: parameter to read | |
296 | * | |
297 | * Returns -1 for error. If you need to distinguish the error more | |
298 | * strictly, use snd_hdac_read() directly. | |
299 | */ | |
9ba17b4d TI |
300 | int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid, |
301 | int parm) | |
7639a06c | 302 | { |
3194ed49 | 303 | unsigned int cmd, val; |
7639a06c | 304 | |
3194ed49 TI |
305 | cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; |
306 | if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0) | |
307 | return -1; | |
7639a06c TI |
308 | return val; |
309 | } | |
9ba17b4d TI |
310 | EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached); |
311 | ||
faa75f8a TI |
312 | /** |
313 | * snd_hdac_override_parm - override read-only parameters | |
314 | * @codec: the codec object | |
315 | * @nid: NID for the parameter | |
316 | * @parm: the parameter to change | |
317 | * @val: the parameter value to overwrite | |
318 | */ | |
319 | int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid, | |
320 | unsigned int parm, unsigned int val) | |
321 | { | |
322 | unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm; | |
323 | int err; | |
324 | ||
325 | if (!codec->regmap) | |
326 | return -EINVAL; | |
327 | ||
328 | codec->caps_overwriting = true; | |
329 | err = snd_hdac_regmap_write_raw(codec, verb, val); | |
330 | codec->caps_overwriting = false; | |
331 | return err; | |
332 | } | |
333 | EXPORT_SYMBOL_GPL(snd_hdac_override_parm); | |
7639a06c TI |
334 | |
335 | /** | |
336 | * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes | |
337 | * @codec: the codec object | |
338 | * @nid: NID to inspect | |
339 | * @start_id: the pointer to store the starting NID | |
340 | * | |
341 | * Returns the number of subtree nodes or zero if not found. | |
9ba17b4d | 342 | * This function reads parameters always without caching. |
7639a06c TI |
343 | */ |
344 | int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid, | |
345 | hda_nid_t *start_id) | |
346 | { | |
347 | unsigned int parm; | |
348 | ||
9ba17b4d | 349 | parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT); |
7639a06c TI |
350 | if (parm == -1) { |
351 | *start_id = 0; | |
352 | return 0; | |
353 | } | |
354 | *start_id = (parm >> 16) & 0x7fff; | |
355 | return (int)(parm & 0x7fff); | |
356 | } | |
357 | EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes); | |
358 | ||
359 | /* | |
360 | * look for an AFG and MFG nodes | |
361 | */ | |
362 | static void setup_fg_nodes(struct hdac_device *codec) | |
363 | { | |
364 | int i, total_nodes, function_id; | |
365 | hda_nid_t nid; | |
366 | ||
367 | total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | |
368 | for (i = 0; i < total_nodes; i++, nid++) { | |
369 | function_id = snd_hdac_read_parm(codec, nid, | |
370 | AC_PAR_FUNCTION_TYPE); | |
371 | switch (function_id & 0xff) { | |
372 | case AC_GRP_AUDIO_FUNCTION: | |
373 | codec->afg = nid; | |
374 | codec->afg_function_id = function_id & 0xff; | |
375 | codec->afg_unsol = (function_id >> 8) & 1; | |
376 | break; | |
377 | case AC_GRP_MODEM_FUNCTION: | |
378 | codec->mfg = nid; | |
379 | codec->mfg_function_id = function_id & 0xff; | |
380 | codec->mfg_unsol = (function_id >> 8) & 1; | |
381 | break; | |
382 | default: | |
383 | break; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | /** | |
389 | * snd_hdac_refresh_widgets - Reset the widget start/end nodes | |
390 | * @codec: the codec object | |
391 | */ | |
392 | int snd_hdac_refresh_widgets(struct hdac_device *codec) | |
393 | { | |
394 | hda_nid_t start_nid; | |
395 | int nums; | |
396 | ||
397 | nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid); | |
398 | if (!start_nid || nums <= 0 || nums >= 0xff) { | |
399 | dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n", | |
400 | codec->afg); | |
401 | return -EINVAL; | |
402 | } | |
403 | ||
404 | codec->num_nodes = nums; | |
405 | codec->start_nid = start_nid; | |
406 | codec->end_nid = start_nid + nums; | |
407 | return 0; | |
408 | } | |
409 | EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets); | |
410 | ||
18dfd79d VK |
411 | /** |
412 | * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the | |
413 | * codec sysfs | |
414 | * @codec: the codec object | |
415 | * | |
416 | * first we need to remove sysfs, then refresh widgets and lastly | |
417 | * recreate it | |
418 | */ | |
419 | int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec) | |
420 | { | |
421 | int ret; | |
422 | ||
a92d5ee8 TI |
423 | if (device_is_registered(&codec->dev)) |
424 | hda_widget_sysfs_exit(codec); | |
18dfd79d VK |
425 | ret = snd_hdac_refresh_widgets(codec); |
426 | if (ret) { | |
427 | dev_err(&codec->dev, "failed to refresh widget: %d\n", ret); | |
428 | return ret; | |
429 | } | |
a92d5ee8 TI |
430 | if (device_is_registered(&codec->dev)) { |
431 | ret = hda_widget_sysfs_init(codec); | |
432 | if (ret) { | |
433 | dev_err(&codec->dev, "failed to init sysfs: %d\n", ret); | |
434 | return ret; | |
435 | } | |
18dfd79d | 436 | } |
18dfd79d VK |
437 | return ret; |
438 | } | |
439 | EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs); | |
440 | ||
7639a06c TI |
441 | /* return CONNLIST_LEN parameter of the given widget */ |
442 | static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid) | |
443 | { | |
444 | unsigned int wcaps = get_wcaps(codec, nid); | |
445 | unsigned int parm; | |
446 | ||
447 | if (!(wcaps & AC_WCAP_CONN_LIST) && | |
448 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) | |
449 | return 0; | |
450 | ||
451 | parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN); | |
452 | if (parm == -1) | |
453 | parm = 0; | |
454 | return parm; | |
455 | } | |
456 | ||
457 | /** | |
458 | * snd_hdac_get_connections - get a widget connection list | |
459 | * @codec: the codec object | |
460 | * @nid: NID | |
461 | * @conn_list: the array to store the results, can be NULL | |
462 | * @max_conns: the max size of the given array | |
463 | * | |
464 | * Returns the number of connected widgets, zero for no connection, or a | |
465 | * negative error code. When the number of elements don't fit with the | |
466 | * given array size, it returns -ENOSPC. | |
467 | * | |
468 | * When @conn_list is NULL, it just checks the number of connections. | |
469 | */ | |
470 | int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid, | |
471 | hda_nid_t *conn_list, int max_conns) | |
472 | { | |
473 | unsigned int parm; | |
474 | int i, conn_len, conns, err; | |
475 | unsigned int shift, num_elems, mask; | |
476 | hda_nid_t prev_nid; | |
477 | int null_count = 0; | |
478 | ||
479 | parm = get_num_conns(codec, nid); | |
480 | if (!parm) | |
481 | return 0; | |
482 | ||
483 | if (parm & AC_CLIST_LONG) { | |
484 | /* long form */ | |
485 | shift = 16; | |
486 | num_elems = 2; | |
487 | } else { | |
488 | /* short form */ | |
489 | shift = 8; | |
490 | num_elems = 4; | |
491 | } | |
492 | conn_len = parm & AC_CLIST_LENGTH; | |
493 | mask = (1 << (shift-1)) - 1; | |
494 | ||
495 | if (!conn_len) | |
496 | return 0; /* no connection */ | |
497 | ||
498 | if (conn_len == 1) { | |
499 | /* single connection */ | |
500 | err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0, | |
501 | &parm); | |
502 | if (err < 0) | |
503 | return err; | |
504 | if (conn_list) | |
505 | conn_list[0] = parm & mask; | |
506 | return 1; | |
507 | } | |
508 | ||
509 | /* multi connection */ | |
510 | conns = 0; | |
511 | prev_nid = 0; | |
512 | for (i = 0; i < conn_len; i++) { | |
513 | int range_val; | |
514 | hda_nid_t val, n; | |
515 | ||
516 | if (i % num_elems == 0) { | |
517 | err = snd_hdac_read(codec, nid, | |
518 | AC_VERB_GET_CONNECT_LIST, i, | |
519 | &parm); | |
520 | if (err < 0) | |
521 | return -EIO; | |
522 | } | |
523 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ | |
524 | val = parm & mask; | |
525 | if (val == 0 && null_count++) { /* no second chance */ | |
526 | dev_dbg(&codec->dev, | |
527 | "invalid CONNECT_LIST verb %x[%i]:%x\n", | |
528 | nid, i, parm); | |
529 | return 0; | |
530 | } | |
531 | parm >>= shift; | |
532 | if (range_val) { | |
533 | /* ranges between the previous and this one */ | |
534 | if (!prev_nid || prev_nid >= val) { | |
535 | dev_warn(&codec->dev, | |
536 | "invalid dep_range_val %x:%x\n", | |
537 | prev_nid, val); | |
538 | continue; | |
539 | } | |
540 | for (n = prev_nid + 1; n <= val; n++) { | |
541 | if (conn_list) { | |
542 | if (conns >= max_conns) | |
543 | return -ENOSPC; | |
544 | conn_list[conns] = n; | |
545 | } | |
546 | conns++; | |
547 | } | |
548 | } else { | |
549 | if (conn_list) { | |
550 | if (conns >= max_conns) | |
551 | return -ENOSPC; | |
552 | conn_list[conns] = val; | |
553 | } | |
554 | conns++; | |
555 | } | |
556 | prev_nid = val; | |
557 | } | |
558 | return conns; | |
559 | } | |
560 | EXPORT_SYMBOL_GPL(snd_hdac_get_connections); | |
561 | ||
562 | #ifdef CONFIG_PM | |
563 | /** | |
664c7155 | 564 | * snd_hdac_power_up - power up the codec |
7639a06c | 565 | * @codec: the codec object |
664c7155 TI |
566 | * |
567 | * This function calls the runtime PM helper to power up the given codec. | |
568 | * Unlike snd_hdac_power_up_pm(), you should call this only for the code | |
569 | * path that isn't included in PM path. Otherwise it gets stuck. | |
fbce23a0 TI |
570 | * |
571 | * Returns zero if successful, or a negative error code. | |
7639a06c | 572 | */ |
fbce23a0 | 573 | int snd_hdac_power_up(struct hdac_device *codec) |
7639a06c | 574 | { |
fbce23a0 | 575 | return pm_runtime_get_sync(&codec->dev); |
7639a06c TI |
576 | } |
577 | EXPORT_SYMBOL_GPL(snd_hdac_power_up); | |
578 | ||
579 | /** | |
664c7155 | 580 | * snd_hdac_power_down - power down the codec |
7639a06c | 581 | * @codec: the codec object |
fbce23a0 TI |
582 | * |
583 | * Returns zero if successful, or a negative error code. | |
7639a06c | 584 | */ |
fbce23a0 | 585 | int snd_hdac_power_down(struct hdac_device *codec) |
7639a06c TI |
586 | { |
587 | struct device *dev = &codec->dev; | |
588 | ||
7639a06c | 589 | pm_runtime_mark_last_busy(dev); |
fbce23a0 | 590 | return pm_runtime_put_autosuspend(dev); |
7639a06c TI |
591 | } |
592 | EXPORT_SYMBOL_GPL(snd_hdac_power_down); | |
c3aeda62 TI |
593 | |
594 | /** | |
595 | * snd_hdac_power_up_pm - power up the codec | |
596 | * @codec: the codec object | |
597 | * | |
598 | * This function can be called in a recursive code path like init code | |
599 | * which may be called by PM suspend/resume again. OTOH, if a power-up | |
600 | * call must wake up the sleeper (e.g. in a kctl callback), use | |
601 | * snd_hdac_power_up() instead. | |
fbce23a0 TI |
602 | * |
603 | * Returns zero if successful, or a negative error code. | |
c3aeda62 | 604 | */ |
fbce23a0 | 605 | int snd_hdac_power_up_pm(struct hdac_device *codec) |
c3aeda62 TI |
606 | { |
607 | if (!atomic_inc_not_zero(&codec->in_pm)) | |
fbce23a0 TI |
608 | return snd_hdac_power_up(codec); |
609 | return 0; | |
c3aeda62 TI |
610 | } |
611 | EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm); | |
612 | ||
fc4f000b TI |
613 | /* like snd_hdac_power_up_pm(), but only increment the pm count when |
614 | * already powered up. Returns -1 if not powered up, 1 if incremented | |
615 | * or 0 if unchanged. Only used in hdac_regmap.c | |
616 | */ | |
617 | int snd_hdac_keep_power_up(struct hdac_device *codec) | |
618 | { | |
619 | if (!atomic_inc_not_zero(&codec->in_pm)) { | |
620 | int ret = pm_runtime_get_if_in_use(&codec->dev); | |
621 | if (!ret) | |
622 | return -1; | |
623 | if (ret < 0) | |
624 | return 0; | |
625 | } | |
626 | return 1; | |
627 | } | |
628 | ||
c3aeda62 TI |
629 | /** |
630 | * snd_hdac_power_down_pm - power down the codec | |
631 | * @codec: the codec object | |
632 | * | |
633 | * Like snd_hdac_power_up_pm(), this function is used in a recursive | |
634 | * code path like init code which may be called by PM suspend/resume again. | |
fbce23a0 TI |
635 | * |
636 | * Returns zero if successful, or a negative error code. | |
c3aeda62 | 637 | */ |
fbce23a0 | 638 | int snd_hdac_power_down_pm(struct hdac_device *codec) |
c3aeda62 TI |
639 | { |
640 | if (atomic_dec_if_positive(&codec->in_pm) < 0) | |
fbce23a0 TI |
641 | return snd_hdac_power_down(codec); |
642 | return 0; | |
c3aeda62 TI |
643 | } |
644 | EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm); | |
7639a06c TI |
645 | #endif |
646 | ||
78dd5e21 TI |
647 | /** |
648 | * snd_hdac_link_power - Enable/disable the link power for a codec | |
649 | * @codec: the codec object | |
650 | * @bool: enable or disable the link power | |
a5e7e07c ML |
651 | */ |
652 | int snd_hdac_link_power(struct hdac_device *codec, bool enable) | |
653 | { | |
654 | if (!codec->link_power_control) | |
655 | return 0; | |
656 | ||
657 | if (codec->bus->ops->link_power) | |
658 | return codec->bus->ops->link_power(codec->bus, enable); | |
659 | else | |
660 | return -EINVAL; | |
661 | } | |
662 | EXPORT_SYMBOL_GPL(snd_hdac_link_power); | |
663 | ||
7639a06c TI |
664 | /* codec vendor labels */ |
665 | struct hda_vendor_id { | |
666 | unsigned int id; | |
667 | const char *name; | |
668 | }; | |
669 | ||
670 | static struct hda_vendor_id hda_vendor_ids[] = { | |
671 | { 0x1002, "ATI" }, | |
672 | { 0x1013, "Cirrus Logic" }, | |
673 | { 0x1057, "Motorola" }, | |
674 | { 0x1095, "Silicon Image" }, | |
675 | { 0x10de, "Nvidia" }, | |
676 | { 0x10ec, "Realtek" }, | |
677 | { 0x1102, "Creative" }, | |
678 | { 0x1106, "VIA" }, | |
679 | { 0x111d, "IDT" }, | |
680 | { 0x11c1, "LSI" }, | |
681 | { 0x11d4, "Analog Devices" }, | |
682 | { 0x13f6, "C-Media" }, | |
683 | { 0x14f1, "Conexant" }, | |
684 | { 0x17e8, "Chrontel" }, | |
685 | { 0x1854, "LG" }, | |
686 | { 0x1aec, "Wolfson Microelectronics" }, | |
687 | { 0x1af4, "QEMU" }, | |
688 | { 0x434d, "C-Media" }, | |
689 | { 0x8086, "Intel" }, | |
690 | { 0x8384, "SigmaTel" }, | |
691 | {} /* terminator */ | |
692 | }; | |
693 | ||
694 | /* store the codec vendor name */ | |
695 | static int get_codec_vendor_name(struct hdac_device *codec) | |
696 | { | |
697 | const struct hda_vendor_id *c; | |
698 | u16 vendor_id = codec->vendor_id >> 16; | |
699 | ||
700 | for (c = hda_vendor_ids; c->id; c++) { | |
701 | if (c->id == vendor_id) { | |
702 | codec->vendor_name = kstrdup(c->name, GFP_KERNEL); | |
703 | return codec->vendor_name ? 0 : -ENOMEM; | |
704 | } | |
705 | } | |
706 | ||
707 | codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id); | |
708 | return codec->vendor_name ? 0 : -ENOMEM; | |
709 | } | |
b7d023e1 TI |
710 | |
711 | /* | |
712 | * stream formats | |
713 | */ | |
714 | struct hda_rate_tbl { | |
715 | unsigned int hz; | |
716 | unsigned int alsa_bits; | |
717 | unsigned int hda_fmt; | |
718 | }; | |
719 | ||
720 | /* rate = base * mult / div */ | |
721 | #define HDA_RATE(base, mult, div) \ | |
722 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ | |
723 | (((div) - 1) << AC_FMT_DIV_SHIFT)) | |
724 | ||
725 | static struct hda_rate_tbl rate_bits[] = { | |
726 | /* rate in Hz, ALSA rate bitmask, HDA format value */ | |
727 | ||
728 | /* autodetected value used in snd_hda_query_supported_pcm */ | |
729 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, | |
730 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, | |
731 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, | |
732 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, | |
733 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, | |
734 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, | |
735 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, | |
736 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, | |
737 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, | |
738 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, | |
739 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, | |
740 | #define AC_PAR_PCM_RATE_BITS 11 | |
741 | /* up to bits 10, 384kHZ isn't supported properly */ | |
742 | ||
743 | /* not autodetected value */ | |
744 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, | |
745 | ||
746 | { 0 } /* terminator */ | |
747 | }; | |
748 | ||
749 | /** | |
750 | * snd_hdac_calc_stream_format - calculate the format bitset | |
751 | * @rate: the sample rate | |
752 | * @channels: the number of channels | |
753 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) | |
754 | * @maxbps: the max. bps | |
755 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) | |
756 | * | |
757 | * Calculate the format bitset from the given rate, channels and th PCM format. | |
758 | * | |
759 | * Return zero if invalid. | |
760 | */ | |
761 | unsigned int snd_hdac_calc_stream_format(unsigned int rate, | |
762 | unsigned int channels, | |
763 | unsigned int format, | |
764 | unsigned int maxbps, | |
765 | unsigned short spdif_ctls) | |
766 | { | |
767 | int i; | |
768 | unsigned int val = 0; | |
769 | ||
770 | for (i = 0; rate_bits[i].hz; i++) | |
771 | if (rate_bits[i].hz == rate) { | |
772 | val = rate_bits[i].hda_fmt; | |
773 | break; | |
774 | } | |
775 | if (!rate_bits[i].hz) | |
776 | return 0; | |
777 | ||
778 | if (channels == 0 || channels > 8) | |
779 | return 0; | |
780 | val |= channels - 1; | |
781 | ||
782 | switch (snd_pcm_format_width(format)) { | |
783 | case 8: | |
784 | val |= AC_FMT_BITS_8; | |
785 | break; | |
786 | case 16: | |
787 | val |= AC_FMT_BITS_16; | |
788 | break; | |
789 | case 20: | |
790 | case 24: | |
791 | case 32: | |
792 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) | |
793 | val |= AC_FMT_BITS_32; | |
794 | else if (maxbps >= 24) | |
795 | val |= AC_FMT_BITS_24; | |
796 | else | |
797 | val |= AC_FMT_BITS_20; | |
798 | break; | |
799 | default: | |
800 | return 0; | |
801 | } | |
802 | ||
803 | if (spdif_ctls & AC_DIG1_NONAUDIO) | |
804 | val |= AC_FMT_TYPE_NON_PCM; | |
805 | ||
806 | return val; | |
807 | } | |
808 | EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format); | |
809 | ||
810 | static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid) | |
811 | { | |
812 | unsigned int val = 0; | |
813 | ||
814 | if (nid != codec->afg && | |
815 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) | |
816 | val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM); | |
817 | if (!val || val == -1) | |
818 | val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM); | |
819 | if (!val || val == -1) | |
820 | return 0; | |
821 | return val; | |
822 | } | |
823 | ||
824 | static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid) | |
825 | { | |
826 | unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM); | |
827 | ||
828 | if (!streams || streams == -1) | |
829 | streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM); | |
830 | if (!streams || streams == -1) | |
831 | return 0; | |
832 | return streams; | |
833 | } | |
834 | ||
835 | /** | |
836 | * snd_hdac_query_supported_pcm - query the supported PCM rates and formats | |
837 | * @codec: the codec object | |
838 | * @nid: NID to query | |
839 | * @ratesp: the pointer to store the detected rate bitflags | |
840 | * @formatsp: the pointer to store the detected formats | |
841 | * @bpsp: the pointer to store the detected format widths | |
842 | * | |
843 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp | |
844 | * or @bsps argument is ignored. | |
845 | * | |
846 | * Returns 0 if successful, otherwise a negative error code. | |
847 | */ | |
848 | int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid, | |
849 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) | |
850 | { | |
851 | unsigned int i, val, wcaps; | |
852 | ||
853 | wcaps = get_wcaps(codec, nid); | |
854 | val = query_pcm_param(codec, nid); | |
855 | ||
856 | if (ratesp) { | |
857 | u32 rates = 0; | |
858 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { | |
859 | if (val & (1 << i)) | |
860 | rates |= rate_bits[i].alsa_bits; | |
861 | } | |
862 | if (rates == 0) { | |
863 | dev_err(&codec->dev, | |
864 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", | |
865 | nid, val, | |
866 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); | |
867 | return -EIO; | |
868 | } | |
869 | *ratesp = rates; | |
870 | } | |
871 | ||
872 | if (formatsp || bpsp) { | |
873 | u64 formats = 0; | |
874 | unsigned int streams, bps; | |
875 | ||
876 | streams = query_stream_param(codec, nid); | |
877 | if (!streams) | |
878 | return -EIO; | |
879 | ||
880 | bps = 0; | |
881 | if (streams & AC_SUPFMT_PCM) { | |
882 | if (val & AC_SUPPCM_BITS_8) { | |
883 | formats |= SNDRV_PCM_FMTBIT_U8; | |
884 | bps = 8; | |
885 | } | |
886 | if (val & AC_SUPPCM_BITS_16) { | |
887 | formats |= SNDRV_PCM_FMTBIT_S16_LE; | |
888 | bps = 16; | |
889 | } | |
890 | if (wcaps & AC_WCAP_DIGITAL) { | |
891 | if (val & AC_SUPPCM_BITS_32) | |
892 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; | |
893 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) | |
894 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
895 | if (val & AC_SUPPCM_BITS_24) | |
896 | bps = 24; | |
897 | else if (val & AC_SUPPCM_BITS_20) | |
898 | bps = 20; | |
899 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| | |
900 | AC_SUPPCM_BITS_32)) { | |
901 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
902 | if (val & AC_SUPPCM_BITS_32) | |
903 | bps = 32; | |
904 | else if (val & AC_SUPPCM_BITS_24) | |
905 | bps = 24; | |
906 | else if (val & AC_SUPPCM_BITS_20) | |
907 | bps = 20; | |
908 | } | |
909 | } | |
910 | #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ | |
911 | if (streams & AC_SUPFMT_FLOAT32) { | |
912 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; | |
913 | if (!bps) | |
914 | bps = 32; | |
915 | } | |
916 | #endif | |
917 | if (streams == AC_SUPFMT_AC3) { | |
918 | /* should be exclusive */ | |
919 | /* temporary hack: we have still no proper support | |
920 | * for the direct AC3 stream... | |
921 | */ | |
922 | formats |= SNDRV_PCM_FMTBIT_U8; | |
923 | bps = 8; | |
924 | } | |
925 | if (formats == 0) { | |
926 | dev_err(&codec->dev, | |
927 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", | |
928 | nid, val, | |
929 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, | |
930 | streams); | |
931 | return -EIO; | |
932 | } | |
933 | if (formatsp) | |
934 | *formatsp = formats; | |
935 | if (bpsp) | |
936 | *bpsp = bps; | |
937 | } | |
938 | ||
939 | return 0; | |
940 | } | |
941 | EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm); | |
942 | ||
943 | /** | |
944 | * snd_hdac_is_supported_format - Check the validity of the format | |
945 | * @codec: the codec object | |
946 | * @nid: NID to check | |
947 | * @format: the HD-audio format value to check | |
948 | * | |
949 | * Check whether the given node supports the format value. | |
950 | * | |
951 | * Returns true if supported, false if not. | |
952 | */ | |
953 | bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid, | |
954 | unsigned int format) | |
955 | { | |
956 | int i; | |
957 | unsigned int val = 0, rate, stream; | |
958 | ||
959 | val = query_pcm_param(codec, nid); | |
960 | if (!val) | |
961 | return false; | |
962 | ||
963 | rate = format & 0xff00; | |
964 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) | |
965 | if (rate_bits[i].hda_fmt == rate) { | |
966 | if (val & (1 << i)) | |
967 | break; | |
968 | return false; | |
969 | } | |
970 | if (i >= AC_PAR_PCM_RATE_BITS) | |
971 | return false; | |
972 | ||
973 | stream = query_stream_param(codec, nid); | |
974 | if (!stream) | |
975 | return false; | |
976 | ||
977 | if (stream & AC_SUPFMT_PCM) { | |
978 | switch (format & 0xf0) { | |
979 | case 0x00: | |
980 | if (!(val & AC_SUPPCM_BITS_8)) | |
981 | return false; | |
982 | break; | |
983 | case 0x10: | |
984 | if (!(val & AC_SUPPCM_BITS_16)) | |
985 | return false; | |
986 | break; | |
987 | case 0x20: | |
988 | if (!(val & AC_SUPPCM_BITS_20)) | |
989 | return false; | |
990 | break; | |
991 | case 0x30: | |
992 | if (!(val & AC_SUPPCM_BITS_24)) | |
993 | return false; | |
994 | break; | |
995 | case 0x40: | |
996 | if (!(val & AC_SUPPCM_BITS_32)) | |
997 | return false; | |
998 | break; | |
999 | default: | |
1000 | return false; | |
1001 | } | |
1002 | } else { | |
1003 | /* FIXME: check for float32 and AC3? */ | |
1004 | } | |
1005 | ||
1006 | return true; | |
1007 | } | |
1008 | EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format); | |
1b5e6167 SP |
1009 | |
1010 | static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid, | |
1011 | int flags, unsigned int verb, unsigned int parm) | |
1012 | { | |
1013 | unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); | |
1014 | unsigned int res; | |
1015 | ||
1016 | if (snd_hdac_exec_verb(hdac, cmd, flags, &res)) | |
1017 | return -1; | |
1018 | ||
1019 | return res; | |
1020 | } | |
1021 | ||
1022 | static int codec_write(struct hdac_device *hdac, hda_nid_t nid, | |
1023 | int flags, unsigned int verb, unsigned int parm) | |
1024 | { | |
1025 | unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); | |
1026 | ||
1027 | return snd_hdac_exec_verb(hdac, cmd, flags, NULL); | |
1028 | } | |
1029 | ||
1030 | /** | |
1031 | * snd_hdac_codec_read - send a command and get the response | |
1032 | * @hdac: the HDAC device | |
1033 | * @nid: NID to send the command | |
1034 | * @flags: optional bit flags | |
1035 | * @verb: the verb to send | |
1036 | * @parm: the parameter for the verb | |
1037 | * | |
1038 | * Send a single command and read the corresponding response. | |
1039 | * | |
1040 | * Returns the obtained response value, or -1 for an error. | |
1041 | */ | |
1042 | int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid, | |
1043 | int flags, unsigned int verb, unsigned int parm) | |
1044 | { | |
1045 | return codec_read(hdac, nid, flags, verb, parm); | |
1046 | } | |
1047 | EXPORT_SYMBOL_GPL(snd_hdac_codec_read); | |
1048 | ||
1049 | /** | |
1050 | * snd_hdac_codec_write - send a single command without waiting for response | |
1051 | * @hdac: the HDAC device | |
1052 | * @nid: NID to send the command | |
1053 | * @flags: optional bit flags | |
1054 | * @verb: the verb to send | |
1055 | * @parm: the parameter for the verb | |
1056 | * | |
1057 | * Send a single command without waiting for response. | |
1058 | * | |
1059 | * Returns 0 if successful, or a negative error code. | |
1060 | */ | |
1061 | int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid, | |
1062 | int flags, unsigned int verb, unsigned int parm) | |
1063 | { | |
1064 | return codec_write(hdac, nid, flags, verb, parm); | |
1065 | } | |
1066 | EXPORT_SYMBOL_GPL(snd_hdac_codec_write); | |
1067 | ||
78dd5e21 TI |
1068 | /** |
1069 | * snd_hdac_check_power_state - check whether the actual power state matches | |
1b5e6167 SP |
1070 | * with the target state |
1071 | * | |
1072 | * @hdac: the HDAC device | |
1073 | * @nid: NID to send the command | |
1074 | * @target_state: target state to check for | |
1075 | * | |
1076 | * Return true if state matches, false if not | |
1077 | */ | |
1078 | bool snd_hdac_check_power_state(struct hdac_device *hdac, | |
1079 | hda_nid_t nid, unsigned int target_state) | |
1080 | { | |
1081 | unsigned int state = codec_read(hdac, nid, 0, | |
1082 | AC_VERB_GET_POWER_STATE, 0); | |
1083 | ||
1084 | if (state & AC_PWRST_ERROR) | |
1085 | return true; | |
1086 | state = (state >> 4) & 0x0f; | |
1087 | return (state == target_state); | |
1088 | } | |
1089 | EXPORT_SYMBOL_GPL(snd_hdac_check_power_state); |