hda_codec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // 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. #define LOG_CATEGORY UCLASS_SOUND
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <hda_codec.h>
  11. #include <log.h>
  12. #include <pci.h>
  13. #include <sound.h>
  14. #include <asm/io.h>
  15. #include <dt-bindings/sound/azalia.h>
  16. #include <linux/bitops.h>
  17. #include <linux/delay.h>
  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. */
  24. struct 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. enum {
  39. HDA_ICII_BUSY = BIT(0),
  40. HDA_ICII_VALID = BIT(1),
  41. /* Common node IDs */
  42. HDA_ROOT_NODE = 0x00,
  43. /* HDA verbs fields */
  44. HDA_VERB_NID_S = 20,
  45. HDA_VERB_VERB_S = 8,
  46. HDA_VERB_PARAM_S = 0,
  47. HDA_VERB_GET_PARAMS = 0xf00,
  48. HDA_VERB_SET_BEEP = 0x70a,
  49. /* GET_PARAMS parameter IDs */
  50. GET_PARAMS_NODE_COUNT = 0x04,
  51. GET_PARAMS_AUDIO_GROUP_CAPS = 0x08,
  52. GET_PARAMS_AUDIO_WIDGET_CAPS = 0x09,
  53. /* Sub-node fields */
  54. NUM_SUB_NODES_S = 0,
  55. NUM_SUB_NODES_M = 0xff << NUM_SUB_NODES_S,
  56. FIRST_SUB_NODE_S = 16,
  57. FIRST_SUB_NODE_M = 0xff << FIRST_SUB_NODE_S,
  58. /* Get Audio Function Group Capabilities fields */
  59. AUDIO_GROUP_CAPS_BEEP_GEN = 0x10000,
  60. /* Get Audio Widget Capabilities fields */
  61. AUDIO_WIDGET_TYPE_BEEP = 0x7,
  62. AUDIO_WIDGET_TYPE_S = 20,
  63. AUDIO_WIDGET_TYPE_M = 0xf << AUDIO_WIDGET_TYPE_S,
  64. BEEP_FREQ_BASE = 12000,
  65. };
  66. static inline uint hda_verb(uint nid, uint verb, uint param)
  67. {
  68. return nid << HDA_VERB_NID_S | verb << HDA_VERB_VERB_S |
  69. param << HDA_VERB_PARAM_S;
  70. }
  71. int hda_wait_for_ready(struct hda_regs *regs)
  72. {
  73. int timeout = 1000; /* Use a 1msec timeout */
  74. while (timeout--) {
  75. u32 reg32 = readl(&regs->icii);
  76. if (!(reg32 & HDA_ICII_BUSY))
  77. return 0;
  78. udelay(1);
  79. }
  80. return -ETIMEDOUT;
  81. }
  82. static int wait_for_response(struct hda_regs *regs, uint *response)
  83. {
  84. int timeout = 1000;
  85. u32 reg32;
  86. /* Send the verb to the codec */
  87. setbits_le32(&regs->icii, HDA_ICII_BUSY | HDA_ICII_VALID);
  88. /* Use a 1msec timeout */
  89. while (timeout--) {
  90. reg32 = readl(&regs->icii);
  91. if ((reg32 & (HDA_ICII_VALID | HDA_ICII_BUSY)) ==
  92. HDA_ICII_VALID) {
  93. if (response)
  94. *response = readl(&regs->resp);
  95. return 0;
  96. }
  97. udelay(1);
  98. }
  99. return -ETIMEDOUT;
  100. }
  101. int hda_wait_for_valid(struct hda_regs *regs)
  102. {
  103. return wait_for_response(regs, NULL);
  104. }
  105. static int set_bits(void *port, u32 mask, u32 val)
  106. {
  107. u32 reg32;
  108. int count;
  109. /* Write (val & mask) to port */
  110. clrsetbits_le32(port, mask, val);
  111. /* Wait for readback of register to match what was just written to it */
  112. count = 50;
  113. do {
  114. /* Wait 1ms based on BKDG wait time */
  115. mdelay(1);
  116. reg32 = readl(port) & mask;
  117. } while (reg32 != val && --count);
  118. /* Timeout occurred */
  119. if (!count)
  120. return -ETIMEDOUT;
  121. return 0;
  122. }
  123. int hda_codec_detect(struct hda_regs *regs)
  124. {
  125. uint reg8;
  126. /* Set Bit 0 to 1 to exit reset state (BAR + 0x8)[0] */
  127. if (set_bits(&regs->gctl, 1, 1))
  128. goto no_codec;
  129. /* Write back the value once reset bit is set */
  130. writew(readw(&regs->gcap), &regs->gcap);
  131. /* Read in Codec location */
  132. reg8 = readb(&regs->statests) & 0xf;
  133. if (!reg8)
  134. goto no_codec;
  135. return reg8;
  136. no_codec:
  137. /* Codec Not found - put HDA back in reset */
  138. set_bits(&regs->gctl, 1, 0);
  139. log_debug("No codec\n");
  140. return 0;
  141. }
  142. static int find_verb_data(struct udevice *dev, uint id, ofnode *nodep)
  143. {
  144. ofnode parent = dev_read_subnode(dev, "codecs");
  145. ofnode node;
  146. u32 vendor_id, device_id;
  147. ofnode_for_each_subnode(node, parent) {
  148. if (ofnode_read_u32(node, "vendor-id", &vendor_id) ||
  149. ofnode_read_u32(node, "device-id", &device_id)) {
  150. log_debug("Cannot get IDs for '%s'\n",
  151. ofnode_get_name(node));
  152. return -EINVAL;
  153. }
  154. if (id != (vendor_id << 16 | device_id)) {
  155. log_debug("Skip codec node '%s' for %08x\n",
  156. ofnode_get_name(node), id);
  157. continue;
  158. }
  159. log_debug("Found codec node '%s' for %08x\n",
  160. ofnode_get_name(node), id);
  161. *nodep = node;
  162. return 0;
  163. }
  164. return -ENOENT;
  165. }
  166. static int send_verbs(ofnode node, const char *prop_name, struct hda_regs *regs)
  167. {
  168. int ret, verb_size, i;
  169. const u32 *verb;
  170. verb = ofnode_get_property(node, prop_name, &verb_size);
  171. if (verb_size < 0) {
  172. log_debug("No verb data\n");
  173. return -EINVAL;
  174. }
  175. log_debug("verb_size: %d\n", verb_size);
  176. for (i = 0; i < verb_size / sizeof(*verb); i++) {
  177. ret = hda_wait_for_ready(regs);
  178. if (ret) {
  179. log_debug(" codec ready timeout\n");
  180. return ret;
  181. }
  182. writel(fdt32_to_cpu(verb[i]), &regs->cmd);
  183. ret = hda_wait_for_valid(regs);
  184. if (ret) {
  185. log_debug(" codec valid timeout\n");
  186. return ret;
  187. }
  188. }
  189. return 0;
  190. }
  191. static int codec_init(struct udevice *dev, struct hda_regs *regs, uint addr)
  192. {
  193. ofnode node;
  194. uint id;
  195. int ret;
  196. log_debug("Initializing codec #%d\n", addr);
  197. ret = hda_wait_for_ready(regs);
  198. if (ret) {
  199. log_debug(" codec not ready\n");
  200. return ret;
  201. }
  202. /* Read the codec's vendor ID */
  203. writel(addr << AZALIA_CODEC_SHIFT |
  204. AZALIA_OPCODE_READ_PARAM << AZALIA_VERB_SHIFT |
  205. AZALIA_PARAM_VENDOR_ID, &regs->cmd);
  206. ret = hda_wait_for_valid(regs);
  207. if (ret) {
  208. log_debug(" codec not valid\n");
  209. return ret;
  210. }
  211. id = readl(&regs->resp);
  212. log_debug("codec vid/did: %08x\n", id);
  213. ret = find_verb_data(dev, id, &node);
  214. if (ret) {
  215. log_debug("No verb (err=%d)\n", ret);
  216. return ret;
  217. }
  218. ret = send_verbs(node, "verbs", regs);
  219. if (ret) {
  220. log_debug("failed to send verbs (err=%d)\n", ret);
  221. return ret;
  222. }
  223. log_debug("verb loaded\n");
  224. return 0;
  225. }
  226. int hda_codecs_init(struct udevice *dev, struct hda_regs *regs, u32 codec_mask)
  227. {
  228. int ret;
  229. int i;
  230. for (i = 3; i >= 0; i--) {
  231. if (codec_mask & (1 << i)) {
  232. ret = codec_init(dev, regs, i);
  233. if (ret)
  234. return ret;
  235. }
  236. }
  237. ret = send_verbs(dev_ofnode(dev), "beep-verbs", regs);
  238. if (ret) {
  239. log_debug("failed to send beep verbs (err=%d)\n", ret);
  240. return ret;
  241. }
  242. log_debug("beep verbs loaded\n");
  243. return 0;
  244. }
  245. /**
  246. * exec_verb() - Write a verb to the codec
  247. *
  248. * @regs: HDA registers
  249. * @val: Command to write
  250. * @response: Set to response from codec
  251. * @return 0 if OK, -ve on error
  252. */
  253. static int exec_verb(struct hda_regs *regs, uint val, uint *response)
  254. {
  255. int ret;
  256. ret = hda_wait_for_ready(regs);
  257. if (ret)
  258. return ret;
  259. writel(val, &regs->cmd);
  260. return wait_for_response(regs, response);
  261. }
  262. /**
  263. * get_subnode_info() - Get subnode information
  264. *
  265. * @regs: HDA registers
  266. * @nid: Parent node ID to check
  267. * @num_sub_nodesp: Returns number of subnodes
  268. * @start_sub_node_nidp: Returns start subnode number
  269. * @return 0 if OK, -ve on error
  270. */
  271. static int get_subnode_info(struct hda_regs *regs, uint nid,
  272. uint *num_sub_nodesp, uint *start_sub_node_nidp)
  273. {
  274. uint response;
  275. int ret;
  276. ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
  277. GET_PARAMS_NODE_COUNT),
  278. &response);
  279. if (ret < 0) {
  280. printf("Audio: Error reading sub-node info %d\n", nid);
  281. return ret;
  282. }
  283. *num_sub_nodesp = (response & NUM_SUB_NODES_M) >> NUM_SUB_NODES_S;
  284. *start_sub_node_nidp = (response & FIRST_SUB_NODE_M) >>
  285. FIRST_SUB_NODE_S;
  286. return 0;
  287. }
  288. /**
  289. * find_beep_node_in_group() - Finds the beeping node
  290. *
  291. * Searches the audio group for a node that supports beeping
  292. *
  293. * @regs: HDA registers
  294. * @group_nid: Group node ID to check
  295. * @return 0 if OK, -ve on error
  296. */
  297. static uint find_beep_node_in_group(struct hda_regs *regs, uint group_nid)
  298. {
  299. uint node_count = 0;
  300. uint current_nid = 0;
  301. uint response;
  302. uint end_nid;
  303. int ret;
  304. ret = get_subnode_info(regs, group_nid, &node_count, &current_nid);
  305. if (ret < 0)
  306. return 0;
  307. end_nid = current_nid + node_count;
  308. while (current_nid < end_nid) {
  309. ret = exec_verb(regs,
  310. hda_verb(current_nid, HDA_VERB_GET_PARAMS,
  311. GET_PARAMS_AUDIO_WIDGET_CAPS),
  312. &response);
  313. if (ret < 0) {
  314. printf("Audio: Error reading widget caps\n");
  315. return 0;
  316. }
  317. if ((response & AUDIO_WIDGET_TYPE_M) >> AUDIO_WIDGET_TYPE_S ==
  318. AUDIO_WIDGET_TYPE_BEEP)
  319. return current_nid;
  320. current_nid++;
  321. }
  322. return 0; /* no beep node found */
  323. }
  324. /**
  325. * audio_group_has_beep_node() - Check if group has a beep node
  326. *
  327. * Checks if the given audio group contains a beep generator
  328. * @regs: HDA registers
  329. * @nid: Node ID to check
  330. * @return 0 if OK, -ve on error
  331. */
  332. static int audio_group_has_beep_node(struct hda_regs *regs, uint nid)
  333. {
  334. uint response;
  335. int ret;
  336. ret = exec_verb(regs, hda_verb(nid, HDA_VERB_GET_PARAMS,
  337. GET_PARAMS_AUDIO_GROUP_CAPS),
  338. &response);
  339. if (ret < 0) {
  340. printf("Audio: Error reading audio group caps %d\n", nid);
  341. return 0;
  342. }
  343. return !!(response & AUDIO_GROUP_CAPS_BEEP_GEN);
  344. }
  345. /**
  346. * get_hda_beep_nid() - Finds the node ID of the beep node
  347. *
  348. * Finds the nid of the beep node if it exists. Starts at the root node, for
  349. * each sub-node checks if the group contains a beep node. If the group
  350. * contains a beep node, polls each node in the group until it is found.
  351. *
  352. * If the device has a intel,beep-nid property, the value of that is used
  353. * instead.
  354. *
  355. * @dev: Sound device
  356. * @return Node ID >0 if found, -ve error code otherwise
  357. */
  358. static int get_hda_beep_nid(struct udevice *dev)
  359. {
  360. struct hda_codec_priv *priv = dev_get_priv(dev);
  361. uint current_nid = 0;
  362. uint node_count = 0;
  363. uint end_nid;
  364. int ret;
  365. /* If the field exists, use the beep nid set in the fdt */
  366. ret = dev_read_u32(dev, "intel,beep-nid", &current_nid);
  367. if (!ret)
  368. return current_nid;
  369. ret = get_subnode_info(priv->regs, HDA_ROOT_NODE, &node_count,
  370. &current_nid);
  371. if (ret < 0)
  372. return ret;
  373. end_nid = current_nid + node_count;
  374. while (current_nid < end_nid) {
  375. if (audio_group_has_beep_node(priv->regs, current_nid))
  376. return find_beep_node_in_group(priv->regs,
  377. current_nid);
  378. current_nid++;
  379. }
  380. /* no beep node found */
  381. return -ENOENT;
  382. }
  383. /**
  384. * set_beep_divisor() - Sets the beep divisor to set the pitch
  385. *
  386. * @priv: Device's private data
  387. * @divider: Divider value (0 to disable the beep)
  388. * @return 0 if OK, -ve on error
  389. */
  390. static int set_beep_divisor(struct hda_codec_priv *priv, uint divider)
  391. {
  392. return exec_verb(priv->regs,
  393. hda_verb(priv->beep_nid, HDA_VERB_SET_BEEP, divider),
  394. NULL);
  395. }
  396. int hda_codec_init(struct udevice *dev)
  397. {
  398. struct hda_codec_priv *priv = dev_get_priv(dev);
  399. ulong base_addr;
  400. base_addr = dm_pci_read_bar32(dev, 0);
  401. log_debug("base = %08lx\n", base_addr);
  402. if (!base_addr)
  403. return -EINVAL;
  404. priv->regs = (struct hda_regs *)base_addr;
  405. return 0;
  406. }
  407. int hda_codec_finish_init(struct udevice *dev)
  408. {
  409. struct hda_codec_priv *priv = dev_get_priv(dev);
  410. int ret;
  411. ret = get_hda_beep_nid(dev);
  412. if (ret <= 0) {
  413. log_warning("Could not find beep NID (err=%d)\n", ret);
  414. return ret ? ret : -ENOENT;
  415. }
  416. priv->beep_nid = ret;
  417. return 0;
  418. }
  419. int hda_codec_start_beep(struct udevice *dev, int frequency_hz)
  420. {
  421. struct hda_codec_priv *priv = dev_get_priv(dev);
  422. uint divider_val;
  423. if (!priv->beep_nid) {
  424. log_err("Failed to find a beep-capable node\n");
  425. return -ENOENT;
  426. }
  427. if (!frequency_hz)
  428. divider_val = 0; /* off */
  429. else if (frequency_hz > BEEP_FREQ_BASE)
  430. divider_val = 1;
  431. else if (frequency_hz < BEEP_FREQ_BASE / 0xff)
  432. divider_val = 0xff;
  433. else
  434. divider_val = 0xff & (BEEP_FREQ_BASE / frequency_hz);
  435. return set_beep_divisor(priv, divider_val);
  436. }
  437. int hda_codec_stop_beep(struct udevice *dev)
  438. {
  439. struct hda_codec_priv *priv = dev_get_priv(dev);
  440. return set_beep_divisor(priv, 0);
  441. }
  442. static const struct sound_ops hda_codec_ops = {
  443. .setup = hda_codec_finish_init,
  444. .start_beep = hda_codec_start_beep,
  445. .stop_beep = hda_codec_stop_beep,
  446. };
  447. U_BOOT_DRIVER(hda_codec) = {
  448. .name = "hda_codec",
  449. .id = UCLASS_SOUND,
  450. .ops = &hda_codec_ops,
  451. .priv_auto_alloc_size = sizeof(struct hda_codec_priv),
  452. .probe = hda_codec_init,
  453. };
  454. static struct pci_device_id hda_supported[] = {
  455. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_HDA},
  456. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_HDA},
  457. { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
  458. PCI_DEVICE_ID_INTEL_WILDCATPOINT_HDA) },
  459. /*
  460. * Note this driver is not necessarily generic, but it attempts to
  461. * support any codec in the hd-audio class
  462. */
  463. { PCI_DEVICE_CLASS(PCI_CLASS_MULTIMEDIA_HD_AUDIO, 0xffffff) },
  464. };
  465. U_BOOT_PCI_DEVICE(hda_codec, hda_supported);