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/bitops.h>
19 #include <linux/delay.h>
22 * struct hda_regs - HDA registers
24 * https://wiki.osdev.org/Intel_High_Definition_Audio
25 * https://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html
43 HDA_ICII_BUSY = BIT(0),
44 HDA_ICII_VALID = BIT(1),
49 /* HDA verbs fields */
54 HDA_VERB_GET_PARAMS = 0xf00,
55 HDA_VERB_SET_BEEP = 0x70a,
57 /* GET_PARAMS parameter IDs */
58 GET_PARAMS_NODE_COUNT = 0x04,
59 GET_PARAMS_AUDIO_GROUP_CAPS = 0x08,
60 GET_PARAMS_AUDIO_WIDGET_CAPS = 0x09,
64 NUM_SUB_NODES_M = 0xff << NUM_SUB_NODES_S,
65 FIRST_SUB_NODE_S = 16,
66 FIRST_SUB_NODE_M = 0xff << FIRST_SUB_NODE_S,
68 /* Get Audio Function Group Capabilities fields */
69 AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000,
71 /* Get Audio Widget Capabilities fields */
72 AUDIO_WIDGET_TYPE_BEEP = 0x7,
73 AUDIO_WIDGET_TYPE_S = 20,
74 AUDIO_WIDGET_TYPE_M = 0xf << AUDIO_WIDGET_TYPE_S,
76 BEEP_FREQ_BASE = 12000,
79 static inline uint hda_verb(uint nid, uint verb, uint param)
81 return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S |
82 param << HDA_VERB_PARAM_S;
85 int hda_wait_for_ready(struct hda_regs *regs)
87 int timeout = 1000; /* Use a 1msec timeout */
90 u32 reg32 = readl(®s->icii);
92 if (!(reg32 & HDA_ICII_BUSY))
100 static int wait_for_response(struct hda_regs *regs, uint *response)
105 /* Send the verb to the codec */
106 setbits_le32(®s->icii, HDA_ICII_BUSY | HDA_ICII_VALID);
108 /* Use a 1msec timeout */
110 reg32 = readl(®s->icii);
111 if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
114 *response = readl(®s->resp);
123 int hda_wait_for_valid(struct hda_regs *regs)
125 return wait_for_response(regs, NULL);
128 static int set_bits(void *port, u32 mask, u32 val)
133 /* Write (val & mask) to port */
134 clrsetbits_le32(port, mask, val);
136 /* Wait for readback of register to match what was just written to it */
139 /* Wait 1ms based on BKDG wait time */
141 reg32 = readl(port) & mask;
142 } while (reg32 != val && --count);
144 /* Timeout occurred */
151 int hda_codec_detect(struct hda_regs *regs)
155 /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
156 if (set_bits(®s->gctl, 1, 1))
159 /* Write back the value once reset bit is set */
160 writew(readw(®s->gcap), ®s->gcap);
162 /* Read in Codec location */
163 reg8 = readb(®s->statests) & 0xf;
170 /* Codec Not found - put HDA back in reset */
171 set_bits(®s->gctl, 1, 0);
172 log_debug("No codec\n");
177 static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep)
179 ofnode parent = dev_read_subnode(dev, "codecs");
181 u32 vendor_id, device_id;
183 ofnode_for_each_subnode(node, parent) {
184 if (ofnode_read_u32(node, "vendor-id", &vendor_id) ||
185 ofnode_read_u32(node, "device-id", &device_id)) {
186 log_debug("Cannot get IDs for '%s'\n",
187 ofnode_get_name(node));
190 if (id != (vendor_id << 16 | device_id)) {
191 log_debug("Skip codec node '%s' for %08x\n",
192 ofnode_get_name(node), id);
196 log_debug("Found codec node '%s' for %08x\n",
197 ofnode_get_name(node), id);
205 static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs)
207 int ret, verb_size, i;
210 verb = ofnode_get_property(node, prop_name, &verb_size);
212 log_debug("No verb data\n");
215 log_debug("verb_size: %d\n", verb_size);
217 for (i = 0; i < verb_size / sizeof(*verb); i++) {
218 ret = hda_wait_for_ready(regs);
220 log_debug(" codec ready timeout\n");
224 writel(fdt32_to_cpu(verb[i]), ®s->cmd);
226 ret = hda_wait_for_valid(regs);
228 log_debug(" codec valid timeout\n");
236 static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr)
242 log_debug("Initializing codec #%d\n", addr);
243 ret = hda_wait_for_ready(regs);
245 log_debug(" codec not ready\n");
249 /* Read the codec's vendor ID */
250 writel(addr << AZALIA_CODEC_SHIFT |
251 AZALIA_OPCODE_READ_PARAM << AZALIA_VERB_SHIFT |
252 AZALIA_PARAM_VENDOR_ID, ®s->cmd);
253 ret = hda_wait_for_valid(regs);
255 log_debug(" codec not valid\n");
259 id = readl(®s->resp);
260 log_debug("codec vid/did: %08x\n", id);
261 ret = find_verb_data(dev, id, &node);
263 log_debug("No verb (err=%d)\n", ret);
266 ret = send_verbs(node, "verbs", regs);
268 log_debug("failed to send verbs (err=%d)\n", ret);
271 log_debug("verb loaded\n");
276 int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask)
281 for (i = 3; i >= 0; i--) {
282 if (codec_mask & (1 << i)) {
283 ret = codec_init(dev, regs, i);
289 ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs);
291 log_debug("failed to send beep verbs (err=%d)\n", ret);
294 log_debug("beep verbs loaded\n");
300 * exec_verb() - Write a verb to the codec
302 * @regs: HDA registers
303 * @val: Command to write
304 * @response: Set to response from codec
305 * @return 0 if OK, -ve on error
307 static int exec_verb(struct hda_regs *regs, uint val, uint *response)
311 ret = hda_wait_for_ready(regs);
315 writel(val, ®s->cmd);
317 return wait_for_response(regs, response);
321 * get_subnode_info() - Get subnode information
323 * @regs: HDA registers
324 * @nid: Parent node ID to check
325 * @num_sub_nodesp: Returns number of subnodes
326 * @start_sub_node_nidp: Returns start subnode number
327 * @return 0 if OK, -ve on error
329 static int get_subnode_info(struct hda_regs *regs, uint nid,
330 uint *num_sub_nodesp, uint *start_sub_node_nidp)
335 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
336 GET_PARAMS_NODE_COUNT),
339 printf("Audio: Error reading sub-node info %d\n", nid);
343 *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S;
344 *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >>
351 * find_beep_node_in_group() - Finds the beeping node
353 * Searches the audio group for a node that supports beeping
355 * @regs: HDA registers
356 * @group_nid: Group node ID to check
357 * @return 0 if OK, -ve on error
359 static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid)
362 uint current_nid = 0;
367 ret = get_subnode_info(regs, group_nid, &node_count, ¤t_nid);
371 end_nid = current_nid + node_count;
372 while (current_nid < end_nid) {
373 ret = exec_verb(regs,
374 hda_verb(current_nid, HDA_VERB_GET_PARAMS,
375 GET_PARAMS_AUDIO_WIDGET_CAPS),
378 printf("Audio: Error reading widget caps\n");
382 if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S ==
383 AUDIO_WIDGET_TYPE_BEEP)
389 return 0; /* no beep node found */
393 * audio_group_has_beep_node() - Check if group has a beep node
395 * Checks if the given audio group contains a beep generator
396 * @regs: HDA registers
397 * @nid: Node ID to check
398 * @return 0 if OK, -ve on error
400 static int audio_group_has_beep_node(struct hda_regs *regs, uint nid)
405 ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
406 GET_PARAMS_AUDIO_GROUP_CAPS),
409 printf("Audio: Error reading audio group caps %d\n", nid);
413 return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN);
417 * get_hda_beep_nid() - Finds the node ID of the beep node
419 * Finds the nid of the beep node if it exists. Starts at the root node, for
420 * each sub-node checks if the group contains a beep node. If the group
421 * contains a beep node, polls each node in the group until it is found.
423 * If the device has a intel,beep-nid property, the value of that is used
427 * @return Node ID >0 if found, -ve error code otherwise
429 static int get_hda_beep_nid(struct udevice *dev)
431 struct hda_codec_priv *priv = dev_get_priv(dev);
432 uint current_nid = 0;
437 /* If the field exists, use the beep nid set in the fdt */
438 ret = dev_read_u32(dev, "intel,beep-nid", ¤t_nid);
442 ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count,
447 end_nid = current_nid + node_count;
448 while (current_nid < end_nid) {
449 if (audio_group_has_beep_node(priv->regs, current_nid))
450 return find_beep_node_in_group(priv->regs,
454 /* no beep node found */
460 * set_beep_divisor() - Sets the beep divisor to set the pitch
462 * @priv: Device's private data
463 * @divider: Divider value (0 to disable the beep)
464 * @return 0 if OK, -ve on error
466 static int set_beep_divisor(struct hda_codec_priv *priv, uint divider)
468 return exec_verb(priv->regs,
469 hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider),
473 int hda_codec_init(struct udevice *dev)
475 struct hda_codec_priv *priv = dev_get_priv(dev);
478 base_addr = dm_pci_read_bar32(dev, 0);
479 log_debug("base = %08lx\n", base_addr);
483 priv->regs = (struct hda_regs *)base_addr;
488 int hda_codec_finish_init(struct udevice *dev)
490 struct hda_codec_priv *priv = dev_get_priv(dev);
493 ret = get_hda_beep_nid(dev);
495 log_warning("Could not find beep NID (err=%d)\n", ret);
496 return ret ? ret : -ENOENT;
498 priv->beep_nid = ret;
503 int hda_codec_start_beep(struct udevice *dev, int frequency_hz)
505 struct hda_codec_priv *priv = dev_get_priv(dev);
508 if (!priv->beep_nid) {
509 log_err("Failed to find a beep-capable node\n");
514 divider_val = 0; /* off */
515 else if (frequency_hz > BEEP_FREQ_BASE)
517 else if (frequency_hz < BEEP_FREQ_BASE / 0xff)
520 divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz);
522 return set_beep_divisor(priv, divider_val);
525 int hda_codec_stop_beep(struct udevice *dev)
527 struct hda_codec_priv *priv = dev_get_priv(dev);
529 return set_beep_divisor(priv, 0);
532 static const struct sound_ops hda_codec_ops = {
533 .setup = hda_codec_finish_init,
534 .start_beep = hda_codec_start_beep,
535 .stop_beep = hda_codec_stop_beep,
538 U_BOOT_DRIVER(hda_codec) = {
541 .ops = &hda_codec_ops,
542 .priv_auto_alloc_size = sizeof(struct hda_codec_priv),
543 .probe = hda_codec_init,
546 static struct pci_device_id hda_supported[] = {
547 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_HDA},
548 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_HDA},
549 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
550 PCI_DEVICE_ID_INTEL_WILDCATPOINT_HDA) },
553 * Note this driver is not necessarily generic, but it attempts to
554 * support any codec in the hd-audio class
556 { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) },
559 U_BOOT_PCI_DEVICE(hda_codec, hda_supported);