da7219.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ACPI driver for DA7219 codec
  4. *
  5. * Copyright 2019 Google LLC
  6. * Parts taken from coreboot
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <i2c.h>
  11. #include <irq.h>
  12. #include <log.h>
  13. #include <acpi/acpigen.h>
  14. #include <acpi/acpi_device.h>
  15. #include <acpi/acpi_dp.h>
  16. #ifdef CONFIG_X86
  17. #include <asm/acpi_nhlt.h>
  18. #endif
  19. #include <asm-generic/gpio.h>
  20. #include <dt-bindings/sound/nhlt.h>
  21. #include <dm/acpi.h>
  22. #define DA7219_ACPI_HID "DLGS7219"
  23. static int da7219_acpi_fill_ssdt(const struct udevice *dev,
  24. struct acpi_ctx *ctx)
  25. {
  26. char scope[ACPI_PATH_MAX];
  27. char name[ACPI_NAME_MAX];
  28. struct acpi_dp *dsd, *aad;
  29. ofnode node;
  30. u32 val;
  31. int ret;
  32. ret = acpi_device_scope(dev, scope, sizeof(scope));
  33. if (ret)
  34. return log_msg_ret("scope", ret);
  35. ret = acpi_get_name(dev, name);
  36. if (ret)
  37. return log_msg_ret("name", ret);
  38. /* Device */
  39. acpigen_write_scope(ctx, scope);
  40. acpigen_write_device(ctx, name);
  41. acpigen_write_name_string(ctx, "_HID", DA7219_ACPI_HID);
  42. acpigen_write_name_integer(ctx, "_UID", 1);
  43. acpigen_write_name_string(ctx, "_DDN",
  44. dev_read_string(dev, "acpi,ddn"));
  45. acpigen_write_name_integer(ctx, "_S0W", 4);
  46. acpigen_write_sta(ctx, acpi_device_status(dev));
  47. /* Resources */
  48. acpigen_write_name(ctx, "_CRS");
  49. acpigen_write_resourcetemplate_header(ctx);
  50. ret = acpi_device_write_i2c_dev(ctx, dev);
  51. if (ret < 0)
  52. return log_msg_ret("i2c", ret);
  53. /* Use either Interrupt() or GpioInt() */
  54. ret = acpi_device_write_interrupt_or_gpio(ctx, (struct udevice *)dev,
  55. "req-gpios");
  56. if (ret < 0)
  57. return log_msg_ret("irq_gpio", ret);
  58. acpigen_write_resourcetemplate_footer(ctx);
  59. /* AAD Child Device Properties */
  60. aad = acpi_dp_new_table("DAAD");
  61. if (!aad)
  62. return log_msg_ret("aad", -ENOMEM);
  63. node = ofnode_find_subnode(dev_ofnode(dev), "da7219_aad");
  64. if (!ofnode_valid(node))
  65. return log_msg_ret("da7219_aad", -EINVAL);
  66. acpi_dp_ofnode_copy_int(node, aad, "dlg,btn-cfg");
  67. acpi_dp_ofnode_copy_int(node, aad, "dlg,mic-det-thr");
  68. acpi_dp_ofnode_copy_int(node, aad, "dlg,jack-ins-deb");
  69. acpi_dp_ofnode_copy_str(node, aad, "dlg,jack-det-rate");
  70. acpi_dp_ofnode_copy_int(node, aad, "dlg,jack-rem-deb");
  71. acpi_dp_ofnode_copy_int(node, aad, "dlg,a-d-btn-thr");
  72. acpi_dp_ofnode_copy_int(node, aad, "dlg,d-b-btn-thr");
  73. acpi_dp_ofnode_copy_int(node, aad, "dlg,b-c-btn-thr");
  74. acpi_dp_ofnode_copy_int(node, aad, "dlg,c-mic-btn-thr");
  75. acpi_dp_ofnode_copy_int(node, aad, "dlg,btn-avg");
  76. acpi_dp_ofnode_copy_int(node, aad, "dlg,adc-1bit-rpt");
  77. if (!ofnode_read_u32(node, "dlg,micbias-pulse-lvl", &val)) {
  78. acpi_dp_ofnode_copy_int(node, aad, "dlg,micbias-pulse-lvl");
  79. acpi_dp_ofnode_copy_int(node, aad, "dlg,micbias-pulse-time");
  80. }
  81. /* DA7219 Properties */
  82. dsd = acpi_dp_new_table("_DSD");
  83. if (!dsd)
  84. return log_msg_ret("dsd", -ENOMEM);
  85. acpi_dp_dev_copy_int(dev, dsd, "dlg,micbias-lvl");
  86. acpi_dp_dev_copy_str(dev, dsd, "dlg,mic-amp-in-sel");
  87. acpi_dp_dev_copy_str(dev, dsd, "dlg,mclk-name");
  88. acpi_dp_add_child(dsd, "da7219_aad", aad);
  89. /* Write Device Property Hierarchy */
  90. acpi_dp_write(ctx, dsd);
  91. acpigen_pop_len(ctx); /* Device */
  92. acpigen_pop_len(ctx); /* Scope */
  93. return 0;
  94. }
  95. /* For now only X86 boards support NHLT */
  96. #ifdef CONFIG_X86
  97. static const struct nhlt_format_config da7219_formats[] = {
  98. /* 48 KHz 24-bits per sample. */
  99. {
  100. .num_channels = 2,
  101. .sample_freq_khz = 48,
  102. .container_bits_per_sample = 32,
  103. .valid_bits_per_sample = 24,
  104. .settings_file = "dialog-2ch-48khz-24b.dat",
  105. },
  106. };
  107. static const struct nhlt_tdm_config tdm_config = {
  108. .virtual_slot = 0,
  109. .config_type = NHLT_TDM_BASIC,
  110. };
  111. static const struct nhlt_endp_descriptor da7219_descriptors[] = {
  112. /* Render Endpoint */
  113. {
  114. .link = NHLT_LINK_SSP,
  115. .device = NHLT_SSP_DEV_I2S,
  116. .direction = NHLT_DIR_RENDER,
  117. .vid = NHLT_VID,
  118. .did = NHLT_DID_SSP,
  119. .cfg = &tdm_config,
  120. .cfg_size = sizeof(tdm_config),
  121. .formats = da7219_formats,
  122. .num_formats = ARRAY_SIZE(da7219_formats),
  123. },
  124. /* Capture Endpoint */
  125. {
  126. .link = NHLT_LINK_SSP,
  127. .device = NHLT_SSP_DEV_I2S,
  128. .direction = NHLT_DIR_CAPTURE,
  129. .vid = NHLT_VID,
  130. .did = NHLT_DID_SSP,
  131. .cfg = &tdm_config,
  132. .cfg_size = sizeof(tdm_config),
  133. .formats = da7219_formats,
  134. .num_formats = ARRAY_SIZE(da7219_formats),
  135. },
  136. };
  137. static int da7219_acpi_setup_nhlt(const struct udevice *dev,
  138. struct acpi_ctx *ctx)
  139. {
  140. u32 hwlink;
  141. int ret;
  142. if (dev_read_u32(dev, "acpi,audio-link", &hwlink))
  143. return log_msg_ret("link", -EINVAL);
  144. /* Virtual bus id of SSP links are the hardware port ids proper. */
  145. ret = nhlt_add_ssp_endpoints(ctx->nhlt, hwlink, da7219_descriptors,
  146. ARRAY_SIZE(da7219_descriptors));
  147. if (ret)
  148. return log_msg_ret("add", ret);
  149. return 0;
  150. }
  151. #endif
  152. struct acpi_ops da7219_acpi_ops = {
  153. .fill_ssdt = da7219_acpi_fill_ssdt,
  154. #ifdef CONFIG_X86
  155. .setup_nhlt = da7219_acpi_setup_nhlt,
  156. #endif
  157. };
  158. static const struct udevice_id da7219_ids[] = {
  159. { .compatible = "dlg,da7219" },
  160. { }
  161. };
  162. U_BOOT_DRIVER(da7219) = {
  163. .name = "da7219",
  164. .id = UCLASS_MISC,
  165. .of_match = da7219_ids,
  166. ACPI_OPS_PTR(&da7219_acpi_ops)
  167. };