1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of per-board codec beeping
4 * Copyright (c) 2011 The Chromium OS Authors.
5 * Copyright 2018 Google LLC
8 #define LOG_CATEGORY UCLASS_SOUND
12 #include <hda_codec.h>
17 #include <dt-bindings/sound/azalia.h>
20 * struct hda_regs - HDA registers
22 * https://wiki.osdev.org/Intel_High_Definition_Audio
23 * https://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html
41 HDA_ICII_BUSY = BIT(0),
42 HDA_ICII_VALID = BIT(1),
47 /* HDA verbs fields */
52 HDA_VERB_GET_PARAMS = 0xf00,
53 HDA_VERB_SET_BEEP = 0x70a,
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,
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,
66 /* Get Audio Function Group Capabilities fields */
67 AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000,
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,
74 BEEP_FREQ_BASE = 12000,
77 static inline uint hda_verb(uint nid, uint verb, uint param)
79 return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S |
80 param << HDA_VERB_PARAM_S;
83 int hda_wait_for_ready(struct hda_regs *regs)
85 int timeout = 1000; /* Use a 1msec timeout */
88 u32 reg32 = readl(®s->icii);
90 if (!(reg32 & HDA_ICII_BUSY))
98 static int wait_for_response(struct hda_regs *regs, uint *response)
103 /* Send the verb to the codec */
104 setbits_le32(®s->icii, HDA_ICII_BUSY | HDA_ICII_VALID);
106 /* Use a 1msec timeout */
108 reg32 = readl(®s->icii);
109 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
112 *response = readl(®s->resp);
121 int hda_wait_for_valid(struct hda_regs *regs)
123 return wait_for_response(regs, NULL);
126 static int set_bits(void *port, u32 mask, u32 val)
131 /* Write (val & mask) to port */
132 clrsetbits_le32(port, mask, val);
134 /* Wait for readback of register to match what was just written to it */
137 /* Wait 1ms based on BKDG wait time */
139 reg32 = readl(port) & mask;
140 } while (reg32 != val && --count);
142 /* Timeout occurred */
149 int hda_codec_detect(struct hda_regs *regs)
153 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
154 if (set_bits(®s->gctl, 1, 1))
157 /* Write back the value once reset bit is set */
158 writew(readw(®s->gcap), ®s->gcap);
160 /* Read in Codec location */
161 reg8 = readb(®s->statests) & 0xf;
168 /* Codec Not found - put HDA back in reset */
169 set_bits(®s->gctl, 1, 0);
170 log_debug("No codec\n");
175 static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep)
177 ofnode parent = dev_read_subnode(dev, "codecs");
179 u32 vendor_id, device_id;
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));
188 if (id != (vendor_id << 16 | device_id)) {
189 log_debug("Skip codec node '%s' for %08x\n",
190 ofnode_get_name(node), id);
194 log_debug("Found codec node '%s' for %08x\n",
195 ofnode_get_name(node), id);
203 static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs)
205 int ret, verb_size, i;
208 verb = ofnode_get_property(node, prop_name, &verb_size);
210 log_debug("No verb data\n");
213 log_debug("verb_size: %d\n", verb_size);
215 for (i = 0; i < verb_size / sizeof(*verb); i++) {
216 ret = hda_wait_for_ready(regs);
218 log_debug(" codec ready timeout\n");
222 writel(fdt32_to_cpu(verb[i]), ®s->cmd);
224 ret = hda_wait_for_valid(regs);
226 log_debug(" codec valid timeout\n");
234 static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr)
240 log_debug("Initializing codec #%d\n", addr);
241 ret = hda_wait_for_ready(regs);
243 log_debug(" codec not ready\n");
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);
253 log_debug(" codec not valid\n");
257 id = readl(®s->resp);
258 log_debug("codec vid/did: %08x\n", id);
259 ret = find_verb_data(dev, id, &node);
261 log_debug("No verb (err=%d)\n", ret);
264 ret = send_verbs(node, "verbs", regs);
266 log_debug("failed to send verbs (err=%d)\n", ret);
269 log_debug("verb loaded\n");
274 int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask)
279 for (i = 3; i >= 0; i--) {
280 if (codec_mask & (1 << i)) {
281 ret = codec_init(dev, regs, i);
287 ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs);
289 log_debug("failed to send beep verbs (err=%d)\n", ret);
292 log_debug("beep verbs loaded\n");
298 * exec_verb() - Write a verb to the codec
300 * @regs: HDA registers
301 * @val: Command to write
302 * @response: Set to response from codec
303 * @return 0 if OK, -ve on error
305 static int exec_verb(struct hda_regs *regs, uint val, uint *response)
309 ret = hda_wait_for_ready(regs);
313 writel(val, ®s->cmd);
315 return wait_for_response(regs, response);
319 * get_subnode_info() - Get subnode information
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
327 static int get_subnode_info(struct hda_regs *regs, uint nid,
328 uint *num_sub_nodesp, uint *start_sub_node_nidp)
333 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
334 GET_PARAMS_NODE_COUNT),
337 printf("Audio: Error reading sub-node info %d\n", nid);
341 *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S;
342 *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >>
349 * find_beep_node_in_group() - Finds the beeping node
351 * Searches the audio group for a node that supports beeping
353 * @regs: HDA registers
354 * @group_nid: Group node ID to check
355 * @return 0 if OK, -ve on error
357 static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid)
360 uint current_nid = 0;
365 ret = get_subnode_info(regs, group_nid, &node_count, ¤t_nid);
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),
376 printf("Audio: Error reading widget caps\n");
380 if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S ==
381 AUDIO_WIDGET_TYPE_BEEP)
387 return 0; /* no beep node found */
391 * audio_group_has_beep_node() - Check if group has a beep node
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
398 static int audio_group_has_beep_node(struct hda_regs *regs, uint nid)
403 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
404 GET_PARAMS_AUDIO_GROUP_CAPS),
407 printf("Audio: Error reading audio group caps %d\n", nid);
411 return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN);
415 * get_hda_beep_nid() - Finds the node ID of the beep node
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.
421 * If the device has a intel,beep-nid property, the value of that is used
425 * @return Node ID >0 if found, -ve error code otherwise
427 static int get_hda_beep_nid(struct udevice *dev)
429 struct hda_codec_priv *priv = dev_get_priv(dev);
430 uint current_nid = 0;
435 /* If the field exists, use the beep nid set in the fdt */
436 ret = dev_read_u32(dev, "intel,beep-nid", ¤t_nid);
440 ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count,
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,
452 /* no beep node found */
458 * set_beep_divisor() - Sets the beep divisor to set the pitch
460 * @priv: Device's private data
461 * @divider: Divider value (0 to disable the beep)
462 * @return 0 if OK, -ve on error
464 static int set_beep_divisor(struct hda_codec_priv *priv, uint divider)
466 return exec_verb(priv->regs,
467 hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider),
471 int hda_codec_init(struct udevice *dev)
473 struct hda_codec_priv *priv = dev_get_priv(dev);
476 base_addr = dm_pci_read_bar32(dev, 0);
477 log_debug("base = %08lx\n", base_addr);
481 priv->regs = (struct hda_regs *)base_addr;
486 int hda_codec_finish_init(struct udevice *dev)
488 struct hda_codec_priv *priv = dev_get_priv(dev);
491 ret = get_hda_beep_nid(dev);
493 log_warning("Could not find beep NID (err=%d)\n", ret);
494 return ret ? ret : -ENOENT;
496 priv->beep_nid = ret;
501 int hda_codec_start_beep(struct udevice *dev, int frequency_hz)
503 struct hda_codec_priv *priv = dev_get_priv(dev);
506 if (!priv->beep_nid) {
507 log_err("Failed to find a beep-capable node\n");
512 divider_val = 0; /* off */
513 else if (frequency_hz > BEEP_FREQ_BASE)
515 else if (frequency_hz < BEEP_FREQ_BASE / 0xff)
518 divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz);
520 return set_beep_divisor(priv, divider_val);
523 int hda_codec_stop_beep(struct udevice *dev)
525 struct hda_codec_priv *priv = dev_get_priv(dev);
527 return set_beep_divisor(priv, 0);
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,
536 U_BOOT_DRIVER(hda_codec) = {
539 .ops = &hda_codec_ops,
540 .priv_auto_alloc_size = sizeof(struct hda_codec_priv),
541 .probe = hda_codec_init,
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) },
551 * Note this driver is not necessarily generic, but it attempts to
552 * support any codec in the hd-audio class
554 { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) },
557 U_BOOT_PCI_DEVICE(hda_codec, hda_supported);