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