rt1015p.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // rt1015p.c -- RT1015P ALSA SoC audio amplifier driver
  4. //
  5. // Copyright 2020 The Linux Foundation. All rights reserved.
  6. #include <linux/acpi.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <sound/pcm.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dai.h>
  19. #include <sound/soc-dapm.h>
  20. struct rt1015p_priv {
  21. struct gpio_desc *sdb;
  22. bool calib_done;
  23. };
  24. static int rt1015p_sdb_event(struct snd_soc_dapm_widget *w,
  25. struct snd_kcontrol *kcontrol, int event)
  26. {
  27. struct snd_soc_component *component =
  28. snd_soc_dapm_to_component(w->dapm);
  29. struct rt1015p_priv *rt1015p =
  30. snd_soc_component_get_drvdata(component);
  31. if (!rt1015p->sdb)
  32. return 0;
  33. switch (event) {
  34. case SND_SOC_DAPM_PRE_PMU:
  35. gpiod_set_value_cansleep(rt1015p->sdb, 1);
  36. dev_dbg(component->dev, "set sdb to 1");
  37. if (!rt1015p->calib_done) {
  38. msleep(300);
  39. rt1015p->calib_done = true;
  40. }
  41. break;
  42. case SND_SOC_DAPM_POST_PMD:
  43. gpiod_set_value_cansleep(rt1015p->sdb, 0);
  44. dev_dbg(component->dev, "set sdb to 0");
  45. break;
  46. default:
  47. break;
  48. }
  49. return 0;
  50. }
  51. static const struct snd_soc_dapm_widget rt1015p_dapm_widgets[] = {
  52. SND_SOC_DAPM_OUTPUT("Speaker"),
  53. SND_SOC_DAPM_OUT_DRV_E("SDB", SND_SOC_NOPM, 0, 0, NULL, 0,
  54. rt1015p_sdb_event,
  55. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  56. };
  57. static const struct snd_soc_dapm_route rt1015p_dapm_routes[] = {
  58. {"SDB", NULL, "HiFi Playback"},
  59. {"Speaker", NULL, "SDB"},
  60. };
  61. #ifdef CONFIG_PM
  62. static int rt1015p_suspend(struct snd_soc_component *component)
  63. {
  64. struct rt1015p_priv *rt1015p = snd_soc_component_get_drvdata(component);
  65. rt1015p->calib_done = false;
  66. return 0;
  67. }
  68. #else
  69. #define rt1015p_suspend NULL
  70. #endif
  71. static const struct snd_soc_component_driver rt1015p_component_driver = {
  72. .suspend = rt1015p_suspend,
  73. .dapm_widgets = rt1015p_dapm_widgets,
  74. .num_dapm_widgets = ARRAY_SIZE(rt1015p_dapm_widgets),
  75. .dapm_routes = rt1015p_dapm_routes,
  76. .num_dapm_routes = ARRAY_SIZE(rt1015p_dapm_routes),
  77. .idle_bias_on = 1,
  78. .use_pmdown_time = 1,
  79. .endianness = 1,
  80. };
  81. static struct snd_soc_dai_driver rt1015p_dai_driver = {
  82. .name = "HiFi",
  83. .playback = {
  84. .stream_name = "HiFi Playback",
  85. .formats = SNDRV_PCM_FMTBIT_S24 |
  86. SNDRV_PCM_FMTBIT_S32,
  87. .rates = SNDRV_PCM_RATE_48000,
  88. .channels_min = 1,
  89. .channels_max = 2,
  90. },
  91. };
  92. static int rt1015p_platform_probe(struct platform_device *pdev)
  93. {
  94. struct rt1015p_priv *rt1015p;
  95. rt1015p = devm_kzalloc(&pdev->dev, sizeof(*rt1015p), GFP_KERNEL);
  96. if (!rt1015p)
  97. return -ENOMEM;
  98. rt1015p->sdb = devm_gpiod_get_optional(&pdev->dev,
  99. "sdb", GPIOD_OUT_LOW);
  100. if (IS_ERR(rt1015p->sdb))
  101. return PTR_ERR(rt1015p->sdb);
  102. dev_set_drvdata(&pdev->dev, rt1015p);
  103. return devm_snd_soc_register_component(&pdev->dev,
  104. &rt1015p_component_driver,
  105. &rt1015p_dai_driver, 1);
  106. }
  107. #ifdef CONFIG_OF
  108. static const struct of_device_id rt1015p_device_id[] = {
  109. { .compatible = "realtek,rt1015p" },
  110. { .compatible = "realtek,rt1019p" },
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(of, rt1015p_device_id);
  114. #endif
  115. #ifdef CONFIG_ACPI
  116. static const struct acpi_device_id rt1015p_acpi_match[] = {
  117. { "RTL1015", 0},
  118. { "RTL1019", 0},
  119. { },
  120. };
  121. MODULE_DEVICE_TABLE(acpi, rt1015p_acpi_match);
  122. #endif
  123. static struct platform_driver rt1015p_platform_driver = {
  124. .driver = {
  125. .name = "rt1015p",
  126. .of_match_table = of_match_ptr(rt1015p_device_id),
  127. .acpi_match_table = ACPI_PTR(rt1015p_acpi_match),
  128. },
  129. .probe = rt1015p_platform_probe,
  130. };
  131. module_platform_driver(rt1015p_platform_driver);
  132. MODULE_DESCRIPTION("ASoC RT1015P driver");
  133. MODULE_LICENSE("GPL v2");