]>
Commit | Line | Data |
---|---|---|
2ca47137 SG |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Implementation of per-board codec beeping | |
4 | * Copyright (c) 2011 The Chromium OS Authors. | |
5 | * Copyright 2018 Google LLC | |
6 | */ | |
7 | ||
8 | #define LOG_CATEGORY UCLASS_SOUND | |
9 | ||
10 | #include <common.h> | |
11 | #include <dm.h> | |
12 | #include <hda_codec.h> | |
f7ae49fc | 13 | #include <log.h> |
2ca47137 SG |
14 | #include <pci.h> |
15 | #include <sound.h> | |
16 | #include <asm/io.h> | |
17 | #include <dt-bindings/sound/azalia.h> | |
18 | ||
19 | /** | |
20 | * struct hda_regs - HDA registers | |
21 | * | |
22 | * https://wiki.osdev.org/Intel_High_Definition_Audio | |
23 | * https://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html | |
24 | */ | |
25 | struct hda_regs { | |
26 | u16 gcap; | |
27 | u8 vmin; | |
28 | u8 vmaj; | |
29 | u16 outpay; | |
30 | u16 inpay; | |
31 | u32 gctl; | |
32 | u16 wakeen; | |
33 | u16 statests; | |
34 | u8 reserved[0x50]; | |
35 | u32 cmd; /* 0x60 */ | |
36 | u32 resp; | |
37 | u32 icii; | |
38 | }; | |
39 | ||
40 | enum { | |
41 | HDA_ICII_BUSY = BIT(0), | |
42 | HDA_ICII_VALID = BIT(1), | |
43 | ||
44 | /* Common node IDs */ | |
45 | HDA_ROOT_NODE = 0x00, | |
46 | ||
47 | /* HDA verbs fields */ | |
48 | HDA_VERB_NID_S = 20, | |
49 | HDA_VERB_VERB_S = 8, | |
50 | HDA_VERB_PARAM_S = 0, | |
51 | ||
52 | HDA_VERB_GET_PARAMS = 0xf00, | |
53 | HDA_VERB_SET_BEEP = 0x70a, | |
54 | ||
55 | /* GET_PARAMS parameter IDs */ | |
56 | GET_PARAMS_NODE_COUNT = 0x04, | |
57 | GET_PARAMS_AUDIO_GROUP_CAPS = 0x08, | |
58 | GET_PARAMS_AUDIO_WIDGET_CAPS = 0x09, | |
59 | ||
60 | /* Sub-node fields */ | |
61 | NUM_SUB_NODES_S = 0, | |
62 | NUM_SUB_NODES_M = 0xff << NUM_SUB_NODES_S, | |
63 | FIRST_SUB_NODE_S = 16, | |
64 | FIRST_SUB_NODE_M = 0xff << FIRST_SUB_NODE_S, | |
65 | ||
66 | /* Get Audio Function Group Capabilities fields */ | |
67 | AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000, | |
68 | ||
69 | /* Get Audio Widget Capabilities fields */ | |
70 | AUDIO_WIDGET_TYPE_BEEP = 0x7, | |
71 | AUDIO_WIDGET_TYPE_S = 20, | |
72 | AUDIO_WIDGET_TYPE_M = 0xf << AUDIO_WIDGET_TYPE_S, | |
73 | ||
74 | BEEP_FREQ_BASE = 12000, | |
75 | }; | |
76 | ||
77 | static inline uint hda_verb(uint nid, uint verb, uint param) | |
78 | { | |
79 | return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S | | |
80 | param << HDA_VERB_PARAM_S; | |
81 | } | |
82 | ||
83 | int hda_wait_for_ready(struct hda_regs *regs) | |
84 | { | |
85 | int timeout = 1000; /* Use a 1msec timeout */ | |
86 | ||
87 | while (timeout--) { | |
88 | u32 reg32 = readl(®s->icii); | |
89 | ||
90 | if (!(reg32 & HDA_ICII_BUSY)) | |
91 | return 0; | |
92 | udelay(1); | |
93 | } | |
94 | ||
95 | return -ETIMEDOUT; | |
96 | } | |
97 | ||
98 | static int wait_for_response(struct hda_regs *regs, uint *response) | |
99 | { | |
100 | int timeout = 1000; | |
101 | u32 reg32; | |
102 | ||
103 | /* Send the verb to the codec */ | |
104 | setbits_le32(®s->icii, HDA_ICII_BUSY | HDA_ICII_VALID); | |
105 | ||
106 | /* Use a 1msec timeout */ | |
107 | while (timeout--) { | |
108 | reg32 = readl(®s->icii); | |
109 | if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) == | |
110 | HDA_ICII_VALID) { | |
111 | if (response) | |
112 | *response = readl(®s->resp); | |
113 | return 0; | |
114 | } | |
115 | udelay(1); | |
116 | } | |
117 | ||
118 | return -ETIMEDOUT; | |
119 | } | |
120 | ||
121 | int hda_wait_for_valid(struct hda_regs *regs) | |
122 | { | |
123 | return wait_for_response(regs, NULL); | |
124 | } | |
125 | ||
126 | static int set_bits(void *port, u32 mask, u32 val) | |
127 | { | |
128 | u32 reg32; | |
129 | int count; | |
130 | ||
131 | /* Write (val & mask) to port */ | |
132 | clrsetbits_le32(port, mask, val); | |
133 | ||
134 | /* Wait for readback of register to match what was just written to it */ | |
135 | count = 50; | |
136 | do { | |
137 | /* Wait 1ms based on BKDG wait time */ | |
138 | mdelay(1); | |
139 | reg32 = readl(port) & mask; | |
140 | } while (reg32 != val && --count); | |
141 | ||
142 | /* Timeout occurred */ | |
143 | if (!count) | |
144 | return -ETIMEDOUT; | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | int hda_codec_detect(struct hda_regs *regs) | |
150 | { | |
151 | uint reg8; | |
152 | ||
153 | /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */ | |
154 | if (set_bits(®s->gctl, 1, 1)) | |
155 | goto no_codec; | |
156 | ||
157 | /* Write back the value once reset bit is set */ | |
158 | writew(readw(®s->gcap), ®s->gcap); | |
159 | ||
160 | /* Read in Codec location */ | |
161 | reg8 = readb(®s->statests) & 0xf; | |
162 | if (!reg8) | |
163 | goto no_codec; | |
164 | ||
165 | return reg8; | |
166 | ||
167 | no_codec: | |
168 | /* Codec Not found - put HDA back in reset */ | |
169 | set_bits(®s->gctl, 1, 0); | |
170 | log_debug("No codec\n"); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep) | |
176 | { | |
177 | ofnode parent = dev_read_subnode(dev, "codecs"); | |
178 | ofnode node; | |
179 | u32 vendor_id, device_id; | |
180 | ||
181 | ofnode_for_each_subnode(node, parent) { | |
182 | if (ofnode_read_u32(node, "vendor-id", &vendor_id) || | |
183 | ofnode_read_u32(node, "device-id", &device_id)) { | |
184 | log_debug("Cannot get IDs for '%s'\n", | |
185 | ofnode_get_name(node)); | |
186 | return -EINVAL; | |
187 | } | |
188 | if (id != (vendor_id << 16 | device_id)) { | |
189 | log_debug("Skip codec node '%s' for %08x\n", | |
190 | ofnode_get_name(node), id); | |
191 | continue; | |
192 | } | |
193 | ||
194 | log_debug("Found codec node '%s' for %08x\n", | |
195 | ofnode_get_name(node), id); | |
196 | *nodep = node; | |
197 | return 0; | |
198 | } | |
199 | ||
200 | return -ENOENT; | |
201 | } | |
202 | ||
203 | static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs) | |
204 | { | |
205 | int ret, verb_size, i; | |
206 | const u32 *verb; | |
207 | ||
208 | verb = ofnode_get_property(node, prop_name, &verb_size); | |
209 | if (verb_size < 0) { | |
210 | log_debug("No verb data\n"); | |
211 | return -EINVAL; | |
212 | } | |
213 | log_debug("verb_size: %d\n", verb_size); | |
214 | ||
215 | for (i = 0; i < verb_size / sizeof(*verb); i++) { | |
216 | ret = hda_wait_for_ready(regs); | |
217 | if (ret) { | |
218 | log_debug(" codec ready timeout\n"); | |
219 | return ret; | |
220 | } | |
221 | ||
222 | writel(fdt32_to_cpu(verb[i]), ®s->cmd); | |
223 | ||
224 | ret = hda_wait_for_valid(regs); | |
225 | if (ret) { | |
226 | log_debug(" codec valid timeout\n"); | |
227 | return ret; | |
228 | } | |
229 | } | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr) | |
235 | { | |
236 | ofnode node; | |
237 | uint id; | |
238 | int ret; | |
239 | ||
240 | log_debug("Initializing codec #%d\n", addr); | |
241 | ret = hda_wait_for_ready(regs); | |
242 | if (ret) { | |
243 | log_debug(" codec not ready\n"); | |
244 | return ret; | |
245 | } | |
246 | ||
247 | /* Read the codec's vendor ID */ | |
248 | writel(addr << AZALIA_CODEC_SHIFT | | |
249 | AZALIA_OPCODE_READ_PARAM << AZALIA_VERB_SHIFT | | |
250 | AZALIA_PARAM_VENDOR_ID, ®s->cmd); | |
251 | ret = hda_wait_for_valid(regs); | |
252 | if (ret) { | |
253 | log_debug(" codec not valid\n"); | |
254 | return ret; | |
255 | } | |
256 | ||
257 | id = readl(®s->resp); | |
258 | log_debug("codec vid/did: %08x\n", id); | |
259 | ret = find_verb_data(dev, id, &node); | |
260 | if (ret) { | |
261 | log_debug("No verb (err=%d)\n", ret); | |
262 | return ret; | |
263 | } | |
264 | ret = send_verbs(node, "verbs", regs); | |
265 | if (ret) { | |
266 | log_debug("failed to send verbs (err=%d)\n", ret); | |
267 | return ret; | |
268 | } | |
269 | log_debug("verb loaded\n"); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask) | |
275 | { | |
276 | int ret; | |
277 | int i; | |
278 | ||
279 | for (i = 3; i >= 0; i--) { | |
280 | if (codec_mask & (1 << i)) { | |
281 | ret = codec_init(dev, regs, i); | |
282 | if (ret) | |
283 | return ret; | |
284 | } | |
285 | } | |
286 | ||
287 | ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs); | |
288 | if (ret) { | |
289 | log_debug("failed to send beep verbs (err=%d)\n", ret); | |
290 | return ret; | |
291 | } | |
292 | log_debug("beep verbs loaded\n"); | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | /** | |
298 | * exec_verb() - Write a verb to the codec | |
299 | * | |
300 | * @regs: HDA registers | |
301 | * @val: Command to write | |
302 | * @response: Set to response from codec | |
303 | * @return 0 if OK, -ve on error | |
304 | */ | |
305 | static int exec_verb(struct hda_regs *regs, uint val, uint *response) | |
306 | { | |
307 | int ret; | |
308 | ||
309 | ret = hda_wait_for_ready(regs); | |
310 | if (ret) | |
311 | return ret; | |
312 | ||
313 | writel(val, ®s->cmd); | |
314 | ||
315 | return wait_for_response(regs, response); | |
316 | } | |
317 | ||
318 | /** | |
319 | * get_subnode_info() - Get subnode information | |
320 | * | |
321 | * @regs: HDA registers | |
322 | * @nid: Parent node ID to check | |
323 | * @num_sub_nodesp: Returns number of subnodes | |
324 | * @start_sub_node_nidp: Returns start subnode number | |
325 | * @return 0 if OK, -ve on error | |
326 | */ | |
327 | static int get_subnode_info(struct hda_regs *regs, uint nid, | |
328 | uint *num_sub_nodesp, uint *start_sub_node_nidp) | |
329 | { | |
330 | uint response; | |
331 | int ret; | |
332 | ||
333 | ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS, | |
334 | GET_PARAMS_NODE_COUNT), | |
335 | &response); | |
336 | if (ret < 0) { | |
337 | printf("Audio: Error reading sub-node info %d\n", nid); | |
338 | return ret; | |
339 | } | |
340 | ||
341 | *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S; | |
342 | *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >> | |
343 | FIRST_SUB_NODE_S; | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | /** | |
349 | * find_beep_node_in_group() - Finds the beeping node | |
350 | * | |
351 | * Searches the audio group for a node that supports beeping | |
352 | * | |
353 | * @regs: HDA registers | |
354 | * @group_nid: Group node ID to check | |
355 | * @return 0 if OK, -ve on error | |
356 | */ | |
357 | static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid) | |
358 | { | |
359 | uint node_count = 0; | |
360 | uint current_nid = 0; | |
361 | uint response; | |
362 | uint end_nid; | |
363 | int ret; | |
364 | ||
365 | ret = get_subnode_info(regs, group_nid, &node_count, ¤t_nid); | |
366 | if (ret < 0) | |
367 | return 0; | |
368 | ||
369 | end_nid = current_nid + node_count; | |
370 | while (current_nid < end_nid) { | |
371 | ret = exec_verb(regs, | |
372 | hda_verb(current_nid, HDA_VERB_GET_PARAMS, | |
373 | GET_PARAMS_AUDIO_WIDGET_CAPS), | |
374 | &response); | |
375 | if (ret < 0) { | |
376 | printf("Audio: Error reading widget caps\n"); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S == | |
381 | AUDIO_WIDGET_TYPE_BEEP) | |
382 | return current_nid; | |
383 | ||
384 | current_nid++; | |
385 | } | |
386 | ||
387 | return 0; /* no beep node found */ | |
388 | } | |
389 | ||
390 | /** | |
391 | * audio_group_has_beep_node() - Check if group has a beep node | |
392 | * | |
393 | * Checks if the given audio group contains a beep generator | |
394 | * @regs: HDA registers | |
395 | * @nid: Node ID to check | |
396 | * @return 0 if OK, -ve on error | |
397 | */ | |
398 | static int audio_group_has_beep_node(struct hda_regs *regs, uint nid) | |
399 | { | |
400 | uint response; | |
401 | int ret; | |
402 | ||
403 | ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS, | |
404 | GET_PARAMS_AUDIO_GROUP_CAPS), | |
405 | &response); | |
406 | if (ret < 0) { | |
407 | printf("Audio: Error reading audio group caps %d\n", nid); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN); | |
412 | } | |
413 | ||
414 | /** | |
415 | * get_hda_beep_nid() - Finds the node ID of the beep node | |
416 | * | |
417 | * Finds the nid of the beep node if it exists. Starts at the root node, for | |
418 | * each sub-node checks if the group contains a beep node. If the group | |
419 | * contains a beep node, polls each node in the group until it is found. | |
420 | * | |
421 | * If the device has a intel,beep-nid property, the value of that is used | |
422 | * instead. | |
423 | * | |
424 | * @dev: Sound device | |
425 | * @return Node ID >0 if found, -ve error code otherwise | |
426 | */ | |
427 | static int get_hda_beep_nid(struct udevice *dev) | |
428 | { | |
429 | struct hda_codec_priv *priv = dev_get_priv(dev); | |
430 | uint current_nid = 0; | |
431 | uint node_count = 0; | |
432 | uint end_nid; | |
433 | int ret; | |
434 | ||
435 | /* If the field exists, use the beep nid set in the fdt */ | |
436 | ret = dev_read_u32(dev, "intel,beep-nid", ¤t_nid); | |
437 | if (!ret) | |
438 | return current_nid; | |
439 | ||
440 | ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count, | |
441 | ¤t_nid); | |
442 | if (ret < 0) | |
443 | return ret; | |
444 | ||
445 | end_nid = current_nid + node_count; | |
446 | while (current_nid < end_nid) { | |
447 | if (audio_group_has_beep_node(priv->regs, current_nid)) | |
448 | return find_beep_node_in_group(priv->regs, | |
449 | current_nid); | |
450 | current_nid++; | |
451 | } | |
452 | /* no beep node found */ | |
453 | ||
454 | return -ENOENT; | |
455 | } | |
456 | ||
457 | /** | |
458 | * set_beep_divisor() - Sets the beep divisor to set the pitch | |
459 | * | |
460 | * @priv: Device's private data | |
461 | * @divider: Divider value (0 to disable the beep) | |
462 | * @return 0 if OK, -ve on error | |
463 | */ | |
464 | static int set_beep_divisor(struct hda_codec_priv *priv, uint divider) | |
465 | { | |
466 | return exec_verb(priv->regs, | |
467 | hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider), | |
468 | NULL); | |
469 | } | |
470 | ||
471 | int hda_codec_init(struct udevice *dev) | |
472 | { | |
473 | struct hda_codec_priv *priv = dev_get_priv(dev); | |
474 | ulong base_addr; | |
475 | ||
476 | base_addr = dm_pci_read_bar32(dev, 0); | |
477 | log_debug("base = %08lx\n", base_addr); | |
478 | if (!base_addr) | |
479 | return -EINVAL; | |
480 | ||
481 | priv->regs = (struct hda_regs *)base_addr; | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | int hda_codec_finish_init(struct udevice *dev) | |
487 | { | |
488 | struct hda_codec_priv *priv = dev_get_priv(dev); | |
489 | int ret; | |
490 | ||
491 | ret = get_hda_beep_nid(dev); | |
492 | if (ret <= 0) { | |
493 | log_warning("Could not find beep NID (err=%d)\n", ret); | |
494 | return ret ? ret : -ENOENT; | |
495 | } | |
496 | priv->beep_nid = ret; | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | int hda_codec_start_beep(struct udevice *dev, int frequency_hz) | |
502 | { | |
503 | struct hda_codec_priv *priv = dev_get_priv(dev); | |
504 | uint divider_val; | |
505 | ||
506 | if (!priv->beep_nid) { | |
507 | log_err("Failed to find a beep-capable node\n"); | |
508 | return -ENOENT; | |
509 | } | |
510 | ||
511 | if (!frequency_hz) | |
512 | divider_val = 0; /* off */ | |
513 | else if (frequency_hz > BEEP_FREQ_BASE) | |
514 | divider_val = 1; | |
515 | else if (frequency_hz < BEEP_FREQ_BASE / 0xff) | |
516 | divider_val = 0xff; | |
517 | else | |
518 | divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz); | |
519 | ||
520 | return set_beep_divisor(priv, divider_val); | |
521 | } | |
522 | ||
523 | int hda_codec_stop_beep(struct udevice *dev) | |
524 | { | |
525 | struct hda_codec_priv *priv = dev_get_priv(dev); | |
526 | ||
527 | return set_beep_divisor(priv, 0); | |
528 | } | |
529 | ||
530 | static const struct sound_ops hda_codec_ops = { | |
531 | .setup = hda_codec_finish_init, | |
532 | .start_beep = hda_codec_start_beep, | |
533 | .stop_beep = hda_codec_stop_beep, | |
534 | }; | |
535 | ||
536 | U_BOOT_DRIVER(hda_codec) = { | |
537 | .name = "hda_codec", | |
538 | .id = UCLASS_SOUND, | |
539 | .ops = &hda_codec_ops, | |
540 | .priv_auto_alloc_size = sizeof(struct hda_codec_priv), | |
541 | .probe = hda_codec_init, | |
542 | }; | |
543 | ||
544 | static struct pci_device_id hda_supported[] = { | |
545 | { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_HDA}, | |
546 | { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_HDA}, | |
547 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, | |
548 | PCI_DEVICE_ID_INTEL_WILDCATPOINT_HDA) }, | |
549 | ||
550 | /* | |
551 | * Note this driver is not necessarily generic, but it attempts to | |
552 | * support any codec in the hd-audio class | |
553 | */ | |
554 | { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) }, | |
555 | }; | |
556 | ||
557 | U_BOOT_PCI_DEVICE(hda_codec, hda_supported); |