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>
18 #include <linux/delay.h>
21 * struct hda_regs - HDA registers
23 * https://wiki.osdev.org/Intel_High_Definition_Audio
24 * https://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html
42 HDA_ICII_BUSY = BIT(0),
43 HDA_ICII_VALID = BIT(1),
48 /* HDA verbs fields */
53 HDA_VERB_GET_PARAMS = 0xf00,
54 HDA_VERB_SET_BEEP = 0x70a,
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,
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,
67 /* Get Audio Function Group Capabilities fields */
68 AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000,
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,
75 BEEP_FREQ_BASE = 12000,
78 static inline uint hda_verb(uint nid, uint verb, uint param)
80 return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S |
81 param << HDA_VERB_PARAM_S;
84 int hda_wait_for_ready(struct hda_regs *regs)
86 int timeout = 1000; /* Use a 1msec timeout */
89 u32 reg32 = readl(®s->icii);
91 if (!(reg32 & HDA_ICII_BUSY))
99 static int wait_for_response(struct hda_regs *regs, uint *response)
104 /* Send the verb to the codec */
105 setbits_le32(®s->icii, HDA_ICII_BUSY | HDA_ICII_VALID);
107 /* Use a 1msec timeout */
109 reg32 = readl(®s->icii);
110 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
113 *response = readl(®s->resp);
122 int hda_wait_for_valid(struct hda_regs *regs)
124 return wait_for_response(regs, NULL);
127 static int set_bits(void *port, u32 mask, u32 val)
132 /* Write (val & mask) to port */
133 clrsetbits_le32(port, mask, val);
135 /* Wait for readback of register to match what was just written to it */
138 /* Wait 1ms based on BKDG wait time */
140 reg32 = readl(port) & mask;
141 } while (reg32 != val && --count);
143 /* Timeout occurred */
150 int hda_codec_detect(struct hda_regs *regs)
154 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
155 if (set_bits(®s->gctl, 1, 1))
158 /* Write back the value once reset bit is set */
159 writew(readw(®s->gcap), ®s->gcap);
161 /* Read in Codec location */
162 reg8 = readb(®s->statests) & 0xf;
169 /* Codec Not found - put HDA back in reset */
170 set_bits(®s->gctl, 1, 0);
171 log_debug("No codec\n");
176 static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep)
178 ofnode parent = dev_read_subnode(dev, "codecs");
180 u32 vendor_id, device_id;
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));
189 if (id != (vendor_id << 16 | device_id)) {
190 log_debug("Skip codec node '%s' for %08x\n",
191 ofnode_get_name(node), id);
195 log_debug("Found codec node '%s' for %08x\n",
196 ofnode_get_name(node), id);
204 static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs)
206 int ret, verb_size, i;
209 verb = ofnode_get_property(node, prop_name, &verb_size);
211 log_debug("No verb data\n");
214 log_debug("verb_size: %d\n", verb_size);
216 for (i = 0; i < verb_size / sizeof(*verb); i++) {
217 ret = hda_wait_for_ready(regs);
219 log_debug(" codec ready timeout\n");
223 writel(fdt32_to_cpu(verb[i]), ®s->cmd);
225 ret = hda_wait_for_valid(regs);
227 log_debug(" codec valid timeout\n");
235 static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr)
241 log_debug("Initializing codec #%d\n", addr);
242 ret = hda_wait_for_ready(regs);
244 log_debug(" codec not ready\n");
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);
254 log_debug(" codec not valid\n");
258 id = readl(®s->resp);
259 log_debug("codec vid/did: %08x\n", id);
260 ret = find_verb_data(dev, id, &node);
262 log_debug("No verb (err=%d)\n", ret);
265 ret = send_verbs(node, "verbs", regs);
267 log_debug("failed to send verbs (err=%d)\n", ret);
270 log_debug("verb loaded\n");
275 int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask)
280 for (i = 3; i >= 0; i--) {
281 if (codec_mask & (1 << i)) {
282 ret = codec_init(dev, regs, i);
288 ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs);
290 log_debug("failed to send beep verbs (err=%d)\n", ret);
293 log_debug("beep verbs loaded\n");
299 * exec_verb() - Write a verb to the codec
301 * @regs: HDA registers
302 * @val: Command to write
303 * @response: Set to response from codec
304 * @return 0 if OK, -ve on error
306 static int exec_verb(struct hda_regs *regs, uint val, uint *response)
310 ret = hda_wait_for_ready(regs);
314 writel(val, ®s->cmd);
316 return wait_for_response(regs, response);
320 * get_subnode_info() - Get subnode information
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
328 static int get_subnode_info(struct hda_regs *regs, uint nid,
329 uint *num_sub_nodesp, uint *start_sub_node_nidp)
334 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
335 GET_PARAMS_NODE_COUNT),
338 printf("Audio: Error reading sub-node info %d\n", nid);
342 *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S;
343 *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >>
350 * find_beep_node_in_group() - Finds the beeping node
352 * Searches the audio group for a node that supports beeping
354 * @regs: HDA registers
355 * @group_nid: Group node ID to check
356 * @return 0 if OK, -ve on error
358 static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid)
361 uint current_nid = 0;
366 ret = get_subnode_info(regs, group_nid, &node_count, ¤t_nid);
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),
377 printf("Audio: Error reading widget caps\n");
381 if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S ==
382 AUDIO_WIDGET_TYPE_BEEP)
388 return 0; /* no beep node found */
392 * audio_group_has_beep_node() - Check if group has a beep node
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
399 static int audio_group_has_beep_node(struct hda_regs *regs, uint nid)
404 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
405 GET_PARAMS_AUDIO_GROUP_CAPS),
408 printf("Audio: Error reading audio group caps %d\n", nid);
412 return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN);
416 * get_hda_beep_nid() - Finds the node ID of the beep node
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.
422 * If the device has a intel,beep-nid property, the value of that is used
426 * @return Node ID >0 if found, -ve error code otherwise
428 static int get_hda_beep_nid(struct udevice *dev)
430 struct hda_codec_priv *priv = dev_get_priv(dev);
431 uint current_nid = 0;
436 /* If the field exists, use the beep nid set in the fdt */
437 ret = dev_read_u32(dev, "intel,beep-nid", ¤t_nid);
441 ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count,
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,
453 /* no beep node found */
459 * set_beep_divisor() - Sets the beep divisor to set the pitch
461 * @priv: Device's private data
462 * @divider: Divider value (0 to disable the beep)
463 * @return 0 if OK, -ve on error
465 static int set_beep_divisor(struct hda_codec_priv *priv, uint divider)
467 return exec_verb(priv->regs,
468 hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider),
472 int hda_codec_init(struct udevice *dev)
474 struct hda_codec_priv *priv = dev_get_priv(dev);
477 base_addr = dm_pci_read_bar32(dev, 0);
478 log_debug("base = %08lx\n", base_addr);
482 priv->regs = (struct hda_regs *)base_addr;
487 int hda_codec_finish_init(struct udevice *dev)
489 struct hda_codec_priv *priv = dev_get_priv(dev);
492 ret = get_hda_beep_nid(dev);
494 log_warning("Could not find beep NID (err=%d)\n", ret);
495 return ret ? ret : -ENOENT;
497 priv->beep_nid = ret;
502 int hda_codec_start_beep(struct udevice *dev, int frequency_hz)
504 struct hda_codec_priv *priv = dev_get_priv(dev);
507 if (!priv->beep_nid) {
508 log_err("Failed to find a beep-capable node\n");
513 divider_val = 0; /* off */
514 else if (frequency_hz > BEEP_FREQ_BASE)
516 else if (frequency_hz < BEEP_FREQ_BASE / 0xff)
519 divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz);
521 return set_beep_divisor(priv, divider_val);
524 int hda_codec_stop_beep(struct udevice *dev)
526 struct hda_codec_priv *priv = dev_get_priv(dev);
528 return set_beep_divisor(priv, 0);
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,
537 U_BOOT_DRIVER(hda_codec) = {
540 .ops = &hda_codec_ops,
541 .priv_auto_alloc_size = sizeof(struct hda_codec_priv),
542 .probe = hda_codec_init,
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) },
552 * Note this driver is not necessarily generic, but it attempts to
553 * support any codec in the hd-audio class
555 { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) },
558 U_BOOT_PCI_DEVICE(hda_codec, hda_supported);