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