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> | |
12 | #include "local.h" | |
13 | ||
14 | static void setup_fg_nodes(struct hdac_device *codec); | |
15 | static int get_codec_vendor_name(struct hdac_device *codec); | |
16 | ||
17 | static void default_release(struct device *dev) | |
18 | { | |
19 | snd_hdac_device_exit(container_of(dev, struct hdac_device, dev)); | |
20 | } | |
21 | ||
22 | /** | |
23 | * snd_hdac_device_init - initialize the HD-audio codec base device | |
24 | * @codec: device to initialize | |
25 | * @bus: but to attach | |
26 | * @name: device name string | |
27 | * @addr: codec address | |
28 | * | |
29 | * Returns zero for success or a negative error code. | |
30 | * | |
31 | * This function increments the runtime PM counter and marks it active. | |
32 | * The caller needs to turn it off appropriately later. | |
33 | * | |
34 | * The caller needs to set the device's release op properly by itself. | |
35 | */ | |
36 | int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus, | |
37 | const char *name, unsigned int addr) | |
38 | { | |
39 | struct device *dev; | |
40 | hda_nid_t fg; | |
41 | int err; | |
42 | ||
43 | dev = &codec->dev; | |
44 | device_initialize(dev); | |
45 | dev->parent = bus->dev; | |
46 | dev->bus = &snd_hda_bus_type; | |
47 | dev->release = default_release; | |
3256be65 | 48 | dev->groups = hdac_dev_attr_groups; |
7639a06c TI |
49 | dev_set_name(dev, "%s", name); |
50 | device_enable_async_suspend(dev); | |
51 | ||
52 | codec->bus = bus; | |
53 | codec->addr = addr; | |
54 | codec->type = HDA_DEV_CORE; | |
55 | pm_runtime_set_active(&codec->dev); | |
56 | pm_runtime_get_noresume(&codec->dev); | |
57 | atomic_set(&codec->in_pm, 0); | |
58 | ||
59 | err = snd_hdac_bus_add_device(bus, codec); | |
60 | if (err < 0) | |
61 | goto error; | |
62 | ||
63 | /* fill parameters */ | |
64 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
65 | AC_PAR_VENDOR_ID); | |
66 | if (codec->vendor_id == -1) { | |
67 | /* read again, hopefully the access method was corrected | |
68 | * in the last read... | |
69 | */ | |
70 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
71 | AC_PAR_VENDOR_ID); | |
72 | } | |
73 | ||
74 | codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
75 | AC_PAR_SUBSYSTEM_ID); | |
76 | codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
77 | AC_PAR_REV_ID); | |
78 | ||
79 | setup_fg_nodes(codec); | |
80 | if (!codec->afg && !codec->mfg) { | |
81 | dev_err(dev, "no AFG or MFG node found\n"); | |
82 | err = -ENODEV; | |
83 | goto error; | |
84 | } | |
85 | ||
86 | fg = codec->afg ? codec->afg : codec->mfg; | |
87 | ||
88 | err = snd_hdac_refresh_widgets(codec); | |
89 | if (err < 0) | |
90 | goto error; | |
91 | ||
92 | codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE); | |
93 | /* reread ssid if not set by parameter */ | |
94 | if (codec->subsystem_id == -1) | |
95 | snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0, | |
96 | &codec->subsystem_id); | |
97 | ||
98 | err = get_codec_vendor_name(codec); | |
99 | if (err < 0) | |
100 | goto error; | |
101 | ||
102 | codec->chip_name = kasprintf(GFP_KERNEL, "ID %x", | |
103 | codec->vendor_id & 0xffff); | |
104 | if (!codec->chip_name) { | |
105 | err = -ENOMEM; | |
106 | goto error; | |
107 | } | |
108 | ||
109 | return 0; | |
110 | ||
111 | error: | |
112 | pm_runtime_put_noidle(&codec->dev); | |
113 | put_device(&codec->dev); | |
114 | return err; | |
115 | } | |
116 | EXPORT_SYMBOL_GPL(snd_hdac_device_init); | |
117 | ||
118 | /** | |
119 | * snd_hdac_device_exit - clean up the HD-audio codec base device | |
120 | * @codec: device to clean up | |
121 | */ | |
122 | void snd_hdac_device_exit(struct hdac_device *codec) | |
123 | { | |
124 | /* pm_runtime_put_noidle(&codec->dev); */ | |
125 | snd_hdac_bus_remove_device(codec->bus, codec); | |
126 | kfree(codec->vendor_name); | |
127 | kfree(codec->chip_name); | |
128 | } | |
129 | EXPORT_SYMBOL_GPL(snd_hdac_device_exit); | |
130 | ||
3256be65 TI |
131 | /** |
132 | * snd_hdac_device_register - register the hd-audio codec base device | |
133 | * codec: the device to register | |
134 | */ | |
135 | int snd_hdac_device_register(struct hdac_device *codec) | |
136 | { | |
137 | int err; | |
138 | ||
139 | err = device_add(&codec->dev); | |
140 | if (err < 0) | |
141 | return err; | |
142 | err = hda_widget_sysfs_init(codec); | |
143 | if (err < 0) { | |
144 | device_del(&codec->dev); | |
145 | return err; | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(snd_hdac_device_register); | |
151 | ||
152 | /** | |
153 | * snd_hdac_device_unregister - unregister the hd-audio codec base device | |
154 | * codec: the device to unregister | |
155 | */ | |
156 | void snd_hdac_device_unregister(struct hdac_device *codec) | |
157 | { | |
158 | if (device_is_registered(&codec->dev)) { | |
159 | hda_widget_sysfs_exit(codec); | |
160 | device_del(&codec->dev); | |
161 | } | |
162 | } | |
163 | EXPORT_SYMBOL_GPL(snd_hdac_device_unregister); | |
164 | ||
7639a06c TI |
165 | /** |
166 | * snd_hdac_make_cmd - compose a 32bit command word to be sent to the | |
167 | * HD-audio controller | |
168 | * @codec: the codec object | |
169 | * @nid: NID to encode | |
170 | * @verb: verb to encode | |
171 | * @parm: parameter to encode | |
172 | * | |
173 | * Return an encoded command verb or -1 for error. | |
174 | */ | |
175 | unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, | |
176 | unsigned int verb, unsigned int parm) | |
177 | { | |
178 | u32 val, addr; | |
179 | ||
180 | addr = codec->addr; | |
181 | if ((addr & ~0xf) || (nid & ~0x7f) || | |
182 | (verb & ~0xfff) || (parm & ~0xffff)) { | |
183 | dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n", | |
184 | addr, nid, verb, parm); | |
185 | return -1; | |
186 | } | |
187 | ||
188 | val = addr << 28; | |
189 | val |= (u32)nid << 20; | |
190 | val |= verb << 8; | |
191 | val |= parm; | |
192 | return val; | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(snd_hdac_make_cmd); | |
195 | ||
196 | /** | |
197 | * snd_hdac_read - execute a verb | |
198 | * @codec: the codec object | |
199 | * @nid: NID to execute a verb | |
200 | * @verb: verb to execute | |
201 | * @parm: parameter for a verb | |
202 | * @res: the pointer to store the result, NULL if running async | |
203 | * | |
204 | * Returns zero if successful, or a negative error code. | |
205 | */ | |
206 | int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid, | |
207 | unsigned int verb, unsigned int parm, unsigned int *res) | |
208 | { | |
209 | unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm); | |
210 | ||
211 | return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res); | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(snd_hdac_read); | |
214 | ||
215 | /** | |
216 | * snd_hdac_read_parm - read a codec parameter | |
217 | * @codec: the codec object | |
218 | * @nid: NID to read a parameter | |
219 | * @parm: parameter to read | |
220 | * | |
221 | * Returns -1 for error. If you need to distinguish the error more | |
222 | * strictly, use snd_hdac_read() directly. | |
223 | */ | |
224 | int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm) | |
225 | { | |
226 | int val; | |
227 | ||
228 | if (snd_hdac_read(codec, nid, AC_VERB_PARAMETERS, parm, &val)) | |
229 | return -1; | |
230 | return val; | |
231 | } | |
232 | EXPORT_SYMBOL_GPL(snd_hdac_read_parm); | |
233 | ||
234 | /** | |
235 | * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes | |
236 | * @codec: the codec object | |
237 | * @nid: NID to inspect | |
238 | * @start_id: the pointer to store the starting NID | |
239 | * | |
240 | * Returns the number of subtree nodes or zero if not found. | |
241 | */ | |
242 | int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid, | |
243 | hda_nid_t *start_id) | |
244 | { | |
245 | unsigned int parm; | |
246 | ||
247 | parm = snd_hdac_read_parm(codec, nid, AC_PAR_NODE_COUNT); | |
248 | if (parm == -1) { | |
249 | *start_id = 0; | |
250 | return 0; | |
251 | } | |
252 | *start_id = (parm >> 16) & 0x7fff; | |
253 | return (int)(parm & 0x7fff); | |
254 | } | |
255 | EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes); | |
256 | ||
257 | /* | |
258 | * look for an AFG and MFG nodes | |
259 | */ | |
260 | static void setup_fg_nodes(struct hdac_device *codec) | |
261 | { | |
262 | int i, total_nodes, function_id; | |
263 | hda_nid_t nid; | |
264 | ||
265 | total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | |
266 | for (i = 0; i < total_nodes; i++, nid++) { | |
267 | function_id = snd_hdac_read_parm(codec, nid, | |
268 | AC_PAR_FUNCTION_TYPE); | |
269 | switch (function_id & 0xff) { | |
270 | case AC_GRP_AUDIO_FUNCTION: | |
271 | codec->afg = nid; | |
272 | codec->afg_function_id = function_id & 0xff; | |
273 | codec->afg_unsol = (function_id >> 8) & 1; | |
274 | break; | |
275 | case AC_GRP_MODEM_FUNCTION: | |
276 | codec->mfg = nid; | |
277 | codec->mfg_function_id = function_id & 0xff; | |
278 | codec->mfg_unsol = (function_id >> 8) & 1; | |
279 | break; | |
280 | default: | |
281 | break; | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | /** | |
287 | * snd_hdac_refresh_widgets - Reset the widget start/end nodes | |
288 | * @codec: the codec object | |
289 | */ | |
290 | int snd_hdac_refresh_widgets(struct hdac_device *codec) | |
291 | { | |
292 | hda_nid_t start_nid; | |
293 | int nums; | |
294 | ||
295 | nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid); | |
296 | if (!start_nid || nums <= 0 || nums >= 0xff) { | |
297 | dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n", | |
298 | codec->afg); | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | codec->num_nodes = nums; | |
303 | codec->start_nid = start_nid; | |
304 | codec->end_nid = start_nid + nums; | |
305 | return 0; | |
306 | } | |
307 | EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets); | |
308 | ||
309 | /* return CONNLIST_LEN parameter of the given widget */ | |
310 | static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid) | |
311 | { | |
312 | unsigned int wcaps = get_wcaps(codec, nid); | |
313 | unsigned int parm; | |
314 | ||
315 | if (!(wcaps & AC_WCAP_CONN_LIST) && | |
316 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) | |
317 | return 0; | |
318 | ||
319 | parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN); | |
320 | if (parm == -1) | |
321 | parm = 0; | |
322 | return parm; | |
323 | } | |
324 | ||
325 | /** | |
326 | * snd_hdac_get_connections - get a widget connection list | |
327 | * @codec: the codec object | |
328 | * @nid: NID | |
329 | * @conn_list: the array to store the results, can be NULL | |
330 | * @max_conns: the max size of the given array | |
331 | * | |
332 | * Returns the number of connected widgets, zero for no connection, or a | |
333 | * negative error code. When the number of elements don't fit with the | |
334 | * given array size, it returns -ENOSPC. | |
335 | * | |
336 | * When @conn_list is NULL, it just checks the number of connections. | |
337 | */ | |
338 | int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid, | |
339 | hda_nid_t *conn_list, int max_conns) | |
340 | { | |
341 | unsigned int parm; | |
342 | int i, conn_len, conns, err; | |
343 | unsigned int shift, num_elems, mask; | |
344 | hda_nid_t prev_nid; | |
345 | int null_count = 0; | |
346 | ||
347 | parm = get_num_conns(codec, nid); | |
348 | if (!parm) | |
349 | return 0; | |
350 | ||
351 | if (parm & AC_CLIST_LONG) { | |
352 | /* long form */ | |
353 | shift = 16; | |
354 | num_elems = 2; | |
355 | } else { | |
356 | /* short form */ | |
357 | shift = 8; | |
358 | num_elems = 4; | |
359 | } | |
360 | conn_len = parm & AC_CLIST_LENGTH; | |
361 | mask = (1 << (shift-1)) - 1; | |
362 | ||
363 | if (!conn_len) | |
364 | return 0; /* no connection */ | |
365 | ||
366 | if (conn_len == 1) { | |
367 | /* single connection */ | |
368 | err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0, | |
369 | &parm); | |
370 | if (err < 0) | |
371 | return err; | |
372 | if (conn_list) | |
373 | conn_list[0] = parm & mask; | |
374 | return 1; | |
375 | } | |
376 | ||
377 | /* multi connection */ | |
378 | conns = 0; | |
379 | prev_nid = 0; | |
380 | for (i = 0; i < conn_len; i++) { | |
381 | int range_val; | |
382 | hda_nid_t val, n; | |
383 | ||
384 | if (i % num_elems == 0) { | |
385 | err = snd_hdac_read(codec, nid, | |
386 | AC_VERB_GET_CONNECT_LIST, i, | |
387 | &parm); | |
388 | if (err < 0) | |
389 | return -EIO; | |
390 | } | |
391 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ | |
392 | val = parm & mask; | |
393 | if (val == 0 && null_count++) { /* no second chance */ | |
394 | dev_dbg(&codec->dev, | |
395 | "invalid CONNECT_LIST verb %x[%i]:%x\n", | |
396 | nid, i, parm); | |
397 | return 0; | |
398 | } | |
399 | parm >>= shift; | |
400 | if (range_val) { | |
401 | /* ranges between the previous and this one */ | |
402 | if (!prev_nid || prev_nid >= val) { | |
403 | dev_warn(&codec->dev, | |
404 | "invalid dep_range_val %x:%x\n", | |
405 | prev_nid, val); | |
406 | continue; | |
407 | } | |
408 | for (n = prev_nid + 1; n <= val; n++) { | |
409 | if (conn_list) { | |
410 | if (conns >= max_conns) | |
411 | return -ENOSPC; | |
412 | conn_list[conns] = n; | |
413 | } | |
414 | conns++; | |
415 | } | |
416 | } else { | |
417 | if (conn_list) { | |
418 | if (conns >= max_conns) | |
419 | return -ENOSPC; | |
420 | conn_list[conns] = val; | |
421 | } | |
422 | conns++; | |
423 | } | |
424 | prev_nid = val; | |
425 | } | |
426 | return conns; | |
427 | } | |
428 | EXPORT_SYMBOL_GPL(snd_hdac_get_connections); | |
429 | ||
430 | #ifdef CONFIG_PM | |
431 | /** | |
432 | * snd_hdac_power_up - increment the runtime pm counter | |
433 | * @codec: the codec object | |
434 | */ | |
435 | void snd_hdac_power_up(struct hdac_device *codec) | |
436 | { | |
437 | struct device *dev = &codec->dev; | |
438 | ||
439 | if (atomic_read(&codec->in_pm)) | |
440 | return; | |
441 | pm_runtime_get_sync(dev); | |
442 | } | |
443 | EXPORT_SYMBOL_GPL(snd_hdac_power_up); | |
444 | ||
445 | /** | |
446 | * snd_hdac_power_up - decrement the runtime pm counter | |
447 | * @codec: the codec object | |
448 | */ | |
449 | void snd_hdac_power_down(struct hdac_device *codec) | |
450 | { | |
451 | struct device *dev = &codec->dev; | |
452 | ||
453 | if (atomic_read(&codec->in_pm)) | |
454 | return; | |
455 | pm_runtime_mark_last_busy(dev); | |
456 | pm_runtime_put_autosuspend(dev); | |
457 | } | |
458 | EXPORT_SYMBOL_GPL(snd_hdac_power_down); | |
459 | #endif | |
460 | ||
461 | /* codec vendor labels */ | |
462 | struct hda_vendor_id { | |
463 | unsigned int id; | |
464 | const char *name; | |
465 | }; | |
466 | ||
467 | static struct hda_vendor_id hda_vendor_ids[] = { | |
468 | { 0x1002, "ATI" }, | |
469 | { 0x1013, "Cirrus Logic" }, | |
470 | { 0x1057, "Motorola" }, | |
471 | { 0x1095, "Silicon Image" }, | |
472 | { 0x10de, "Nvidia" }, | |
473 | { 0x10ec, "Realtek" }, | |
474 | { 0x1102, "Creative" }, | |
475 | { 0x1106, "VIA" }, | |
476 | { 0x111d, "IDT" }, | |
477 | { 0x11c1, "LSI" }, | |
478 | { 0x11d4, "Analog Devices" }, | |
479 | { 0x13f6, "C-Media" }, | |
480 | { 0x14f1, "Conexant" }, | |
481 | { 0x17e8, "Chrontel" }, | |
482 | { 0x1854, "LG" }, | |
483 | { 0x1aec, "Wolfson Microelectronics" }, | |
484 | { 0x1af4, "QEMU" }, | |
485 | { 0x434d, "C-Media" }, | |
486 | { 0x8086, "Intel" }, | |
487 | { 0x8384, "SigmaTel" }, | |
488 | {} /* terminator */ | |
489 | }; | |
490 | ||
491 | /* store the codec vendor name */ | |
492 | static int get_codec_vendor_name(struct hdac_device *codec) | |
493 | { | |
494 | const struct hda_vendor_id *c; | |
495 | u16 vendor_id = codec->vendor_id >> 16; | |
496 | ||
497 | for (c = hda_vendor_ids; c->id; c++) { | |
498 | if (c->id == vendor_id) { | |
499 | codec->vendor_name = kstrdup(c->name, GFP_KERNEL); | |
500 | return codec->vendor_name ? 0 : -ENOMEM; | |
501 | } | |
502 | } | |
503 | ||
504 | codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id); | |
505 | return codec->vendor_name ? 0 : -ENOMEM; | |
506 | } |