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