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>
16 #include <dt-bindings/sound/azalia.h>
19 * struct hda_regs - HDA registers
21 * https://wiki.osdev.org/Intel_High_Definition_Audio
22 * https://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html
40 HDA_ICII_BUSY = BIT(0),
41 HDA_ICII_VALID = BIT(1),
46 /* HDA verbs fields */
51 HDA_VERB_GET_PARAMS = 0xf00,
52 HDA_VERB_SET_BEEP = 0x70a,
54 /* GET_PARAMS parameter IDs */
55 GET_PARAMS_NODE_COUNT = 0x04,
56 GET_PARAMS_AUDIO_GROUP_CAPS = 0x08,
57 GET_PARAMS_AUDIO_WIDGET_CAPS = 0x09,
61 NUM_SUB_NODES_M = 0xff << NUM_SUB_NODES_S,
62 FIRST_SUB_NODE_S = 16,
63 FIRST_SUB_NODE_M = 0xff << FIRST_SUB_NODE_S,
65 /* Get Audio Function Group Capabilities fields */
66 AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000,
68 /* Get Audio Widget Capabilities fields */
69 AUDIO_WIDGET_TYPE_BEEP = 0x7,
70 AUDIO_WIDGET_TYPE_S = 20,
71 AUDIO_WIDGET_TYPE_M = 0xf << AUDIO_WIDGET_TYPE_S,
73 BEEP_FREQ_BASE = 12000,
76 static inline uint hda_verb(uint nid, uint verb, uint param)
78 return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S |
79 param << HDA_VERB_PARAM_S;
82 int hda_wait_for_ready(struct hda_regs *regs)
84 int timeout = 1000; /* Use a 1msec timeout */
87 u32 reg32 = readl(®s->icii);
89 if (!(reg32 & HDA_ICII_BUSY))
97 static int wait_for_response(struct hda_regs *regs, uint *response)
102 /* Send the verb to the codec */
103 setbits_le32(®s->icii, HDA_ICII_BUSY | HDA_ICII_VALID);
105 /* Use a 1msec timeout */
107 reg32 = readl(®s->icii);
108 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
111 *response = readl(®s->resp);
120 int hda_wait_for_valid(struct hda_regs *regs)
122 return wait_for_response(regs, NULL);
125 static int set_bits(void *port, u32 mask, u32 val)
130 /* Write (val & mask) to port */
131 clrsetbits_le32(port, mask, val);
133 /* Wait for readback of register to match what was just written to it */
136 /* Wait 1ms based on BKDG wait time */
138 reg32 = readl(port) & mask;
139 } while (reg32 != val && --count);
141 /* Timeout occurred */
148 int hda_codec_detect(struct hda_regs *regs)
152 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
153 if (set_bits(®s->gctl, 1, 1))
156 /* Write back the value once reset bit is set */
157 writew(readw(®s->gcap), ®s->gcap);
159 /* Read in Codec location */
160 reg8 = readb(®s->statests) & 0xf;
167 /* Codec Not found - put HDA back in reset */
168 set_bits(®s->gctl, 1, 0);
169 log_debug("No codec\n");
174 static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep)
176 ofnode parent = dev_read_subnode(dev, "codecs");
178 u32 vendor_id, device_id;
180 ofnode_for_each_subnode(node, parent) {
181 if (ofnode_read_u32(node, "vendor-id", &vendor_id) ||
182 ofnode_read_u32(node, "device-id", &device_id)) {
183 log_debug("Cannot get IDs for '%s'\n",
184 ofnode_get_name(node));
187 if (id != (vendor_id << 16 | device_id)) {
188 log_debug("Skip codec node '%s' for %08x\n",
189 ofnode_get_name(node), id);
193 log_debug("Found codec node '%s' for %08x\n",
194 ofnode_get_name(node), id);
202 static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs)
204 int ret, verb_size, i;
207 verb = ofnode_get_property(node, prop_name, &verb_size);
209 log_debug("No verb data\n");
212 log_debug("verb_size: %d\n", verb_size);
214 for (i = 0; i < verb_size / sizeof(*verb); i++) {
215 ret = hda_wait_for_ready(regs);
217 log_debug(" codec ready timeout\n");
221 writel(fdt32_to_cpu(verb[i]), ®s->cmd);
223 ret = hda_wait_for_valid(regs);
225 log_debug(" codec valid timeout\n");
233 static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr)
239 log_debug("Initializing codec #%d\n", addr);
240 ret = hda_wait_for_ready(regs);
242 log_debug(" codec not ready\n");
246 /* Read the codec's vendor ID */
247 writel(addr << AZALIA_CODEC_SHIFT |
248 AZALIA_OPCODE_READ_PARAM << AZALIA_VERB_SHIFT |
249 AZALIA_PARAM_VENDOR_ID, ®s->cmd);
250 ret = hda_wait_for_valid(regs);
252 log_debug(" codec not valid\n");
256 id = readl(®s->resp);
257 log_debug("codec vid/did: %08x\n", id);
258 ret = find_verb_data(dev, id, &node);
260 log_debug("No verb (err=%d)\n", ret);
263 ret = send_verbs(node, "verbs", regs);
265 log_debug("failed to send verbs (err=%d)\n", ret);
268 log_debug("verb loaded\n");
273 int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask)
278 for (i = 3; i >= 0; i--) {
279 if (codec_mask & (1 << i)) {
280 ret = codec_init(dev, regs, i);
286 ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs);
288 log_debug("failed to send beep verbs (err=%d)\n", ret);
291 log_debug("beep verbs loaded\n");
297 * exec_verb() - Write a verb to the codec
299 * @regs: HDA registers
300 * @val: Command to write
301 * @response: Set to response from codec
302 * @return 0 if OK, -ve on error
304 static int exec_verb(struct hda_regs *regs, uint val, uint *response)
308 ret = hda_wait_for_ready(regs);
312 writel(val, ®s->cmd);
314 return wait_for_response(regs, response);
318 * get_subnode_info() - Get subnode information
320 * @regs: HDA registers
321 * @nid: Parent node ID to check
322 * @num_sub_nodesp: Returns number of subnodes
323 * @start_sub_node_nidp: Returns start subnode number
324 * @return 0 if OK, -ve on error
326 static int get_subnode_info(struct hda_regs *regs, uint nid,
327 uint *num_sub_nodesp, uint *start_sub_node_nidp)
332 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
333 GET_PARAMS_NODE_COUNT),
336 printf("Audio: Error reading sub-node info %d\n", nid);
340 *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S;
341 *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >>
348 * find_beep_node_in_group() - Finds the beeping node
350 * Searches the audio group for a node that supports beeping
352 * @regs: HDA registers
353 * @group_nid: Group node ID to check
354 * @return 0 if OK, -ve on error
356 static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid)
359 uint current_nid = 0;
364 ret = get_subnode_info(regs, group_nid, &node_count, ¤t_nid);
368 end_nid = current_nid + node_count;
369 while (current_nid < end_nid) {
370 ret = exec_verb(regs,
371 hda_verb(current_nid, HDA_VERB_GET_PARAMS,
372 GET_PARAMS_AUDIO_WIDGET_CAPS),
375 printf("Audio: Error reading widget caps\n");
379 if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S ==
380 AUDIO_WIDGET_TYPE_BEEP)
386 return 0; /* no beep node found */
390 * audio_group_has_beep_node() - Check if group has a beep node
392 * Checks if the given audio group contains a beep generator
393 * @regs: HDA registers
394 * @nid: Node ID to check
395 * @return 0 if OK, -ve on error
397 static int audio_group_has_beep_node(struct hda_regs *regs, uint nid)
402 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
403 GET_PARAMS_AUDIO_GROUP_CAPS),
406 printf("Audio: Error reading audio group caps %d\n", nid);
410 return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN);
414 * get_hda_beep_nid() - Finds the node ID of the beep node
416 * Finds the nid of the beep node if it exists. Starts at the root node, for
417 * each sub-node checks if the group contains a beep node. If the group
418 * contains a beep node, polls each node in the group until it is found.
420 * If the device has a intel,beep-nid property, the value of that is used
424 * @return Node ID >0 if found, -ve error code otherwise
426 static int get_hda_beep_nid(struct udevice *dev)
428 struct hda_codec_priv *priv = dev_get_priv(dev);
429 uint current_nid = 0;
434 /* If the field exists, use the beep nid set in the fdt */
435 ret = dev_read_u32(dev, "intel,beep-nid", ¤t_nid);
439 ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count,
444 end_nid = current_nid + node_count;
445 while (current_nid < end_nid) {
446 if (audio_group_has_beep_node(priv->regs, current_nid))
447 return find_beep_node_in_group(priv->regs,
451 /* no beep node found */
457 * set_beep_divisor() - Sets the beep divisor to set the pitch
459 * @priv: Device's private data
460 * @divider: Divider value (0 to disable the beep)
461 * @return 0 if OK, -ve on error
463 static int set_beep_divisor(struct hda_codec_priv *priv, uint divider)
465 return exec_verb(priv->regs,
466 hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider),
470 int hda_codec_init(struct udevice *dev)
472 struct hda_codec_priv *priv = dev_get_priv(dev);
475 base_addr = dm_pci_read_bar32(dev, 0);
476 log_debug("base = %08lx\n", base_addr);
480 priv->regs = (struct hda_regs *)base_addr;
485 int hda_codec_finish_init(struct udevice *dev)
487 struct hda_codec_priv *priv = dev_get_priv(dev);
490 ret = get_hda_beep_nid(dev);
492 log_warning("Could not find beep NID (err=%d)\n", ret);
493 return ret ? ret : -ENOENT;
495 priv->beep_nid = ret;
500 int hda_codec_start_beep(struct udevice *dev, int frequency_hz)
502 struct hda_codec_priv *priv = dev_get_priv(dev);
505 if (!priv->beep_nid) {
506 log_err("Failed to find a beep-capable node\n");
511 divider_val = 0; /* off */
512 else if (frequency_hz > BEEP_FREQ_BASE)
514 else if (frequency_hz < BEEP_FREQ_BASE / 0xff)
517 divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz);
519 return set_beep_divisor(priv, divider_val);
522 int hda_codec_stop_beep(struct udevice *dev)
524 struct hda_codec_priv *priv = dev_get_priv(dev);
526 return set_beep_divisor(priv, 0);
529 static const struct sound_ops hda_codec_ops = {
530 .setup = hda_codec_finish_init,
531 .start_beep = hda_codec_start_beep,
532 .stop_beep = hda_codec_stop_beep,
535 U_BOOT_DRIVER(hda_codec) = {
538 .ops = &hda_codec_ops,
539 .priv_auto_alloc_size = sizeof(struct hda_codec_priv),
540 .probe = hda_codec_init,
543 static struct pci_device_id hda_supported[] = {
544 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_HDA},
545 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_HDA},
546 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
547 PCI_DEVICE_ID_INTEL_WILDCATPOINT_HDA) },
550 * Note this driver is not necessarily generic, but it attempts to
551 * support any codec in the hd-audio class
553 { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) },
556 U_BOOT_PCI_DEVICE(hda_codec, hda_supported);