nau8315.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // nau8315.c -- NAU8315 ALSA SoC Audio Amplifier Driver
  4. //
  5. // Copyright 2020 Nuvoton Technology Crop.
  6. //
  7. // Author: David Lin <[email protected]>
  8. //
  9. // Based on MAX98357A.c
  10. #include <linux/acpi.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <sound/pcm.h>
  20. #include <sound/soc.h>
  21. #include <sound/soc-dai.h>
  22. #include <sound/soc-dapm.h>
  23. struct nau8315_priv {
  24. struct gpio_desc *enable;
  25. int enpin_switch;
  26. };
  27. static int nau8315_daiops_trigger(struct snd_pcm_substream *substream,
  28. int cmd, struct snd_soc_dai *dai)
  29. {
  30. struct snd_soc_component *component = dai->component;
  31. struct nau8315_priv *nau8315 =
  32. snd_soc_component_get_drvdata(component);
  33. if (!nau8315->enable)
  34. return 0;
  35. switch (cmd) {
  36. case SNDRV_PCM_TRIGGER_START:
  37. case SNDRV_PCM_TRIGGER_RESUME:
  38. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  39. if (nau8315->enpin_switch) {
  40. gpiod_set_value(nau8315->enable, 1);
  41. dev_dbg(component->dev, "set enable to 1");
  42. }
  43. break;
  44. case SNDRV_PCM_TRIGGER_STOP:
  45. case SNDRV_PCM_TRIGGER_SUSPEND:
  46. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  47. gpiod_set_value(nau8315->enable, 0);
  48. dev_dbg(component->dev, "set enable to 0");
  49. break;
  50. }
  51. return 0;
  52. }
  53. static int nau8315_enpin_event(struct snd_soc_dapm_widget *w,
  54. struct snd_kcontrol *kcontrol, int event)
  55. {
  56. struct snd_soc_component *component =
  57. snd_soc_dapm_to_component(w->dapm);
  58. struct nau8315_priv *nau8315 =
  59. snd_soc_component_get_drvdata(component);
  60. if (event & SND_SOC_DAPM_PRE_PMU)
  61. nau8315->enpin_switch = 1;
  62. else if (event & SND_SOC_DAPM_POST_PMD)
  63. nau8315->enpin_switch = 0;
  64. return 0;
  65. }
  66. static const struct snd_soc_dapm_widget nau8315_dapm_widgets[] = {
  67. SND_SOC_DAPM_OUTPUT("Speaker"),
  68. SND_SOC_DAPM_OUT_DRV_E("EN_Pin", SND_SOC_NOPM, 0, 0, NULL, 0,
  69. nau8315_enpin_event,
  70. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  71. };
  72. static const struct snd_soc_dapm_route nau8315_dapm_routes[] = {
  73. {"EN_Pin", NULL, "HiFi Playback"},
  74. {"Speaker", NULL, "EN_Pin"},
  75. };
  76. static const struct snd_soc_component_driver nau8315_component_driver = {
  77. .dapm_widgets = nau8315_dapm_widgets,
  78. .num_dapm_widgets = ARRAY_SIZE(nau8315_dapm_widgets),
  79. .dapm_routes = nau8315_dapm_routes,
  80. .num_dapm_routes = ARRAY_SIZE(nau8315_dapm_routes),
  81. .idle_bias_on = 1,
  82. .use_pmdown_time = 1,
  83. .endianness = 1,
  84. };
  85. static const struct snd_soc_dai_ops nau8315_dai_ops = {
  86. .trigger = nau8315_daiops_trigger,
  87. };
  88. #define NAU8315_RATES SNDRV_PCM_RATE_8000_96000
  89. #define NAU8315_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE)
  90. static struct snd_soc_dai_driver nau8315_dai_driver = {
  91. .name = "nau8315-hifi",
  92. .playback = {
  93. .stream_name = "HiFi Playback",
  94. .formats = NAU8315_FORMATS,
  95. .rates = NAU8315_RATES,
  96. .channels_min = 1,
  97. .channels_max = 2,
  98. },
  99. .ops = &nau8315_dai_ops,
  100. };
  101. static int nau8315_platform_probe(struct platform_device *pdev)
  102. {
  103. struct nau8315_priv *nau8315;
  104. nau8315 = devm_kzalloc(&pdev->dev, sizeof(*nau8315), GFP_KERNEL);
  105. if (!nau8315)
  106. return -ENOMEM;
  107. nau8315->enable = devm_gpiod_get_optional(&pdev->dev,
  108. "enable", GPIOD_OUT_LOW);
  109. if (IS_ERR(nau8315->enable))
  110. return PTR_ERR(nau8315->enable);
  111. dev_set_drvdata(&pdev->dev, nau8315);
  112. return devm_snd_soc_register_component(&pdev->dev,
  113. &nau8315_component_driver,
  114. &nau8315_dai_driver, 1);
  115. }
  116. #ifdef CONFIG_OF
  117. static const struct of_device_id nau8315_device_id[] = {
  118. { .compatible = "nuvoton,nau8315" },
  119. {}
  120. };
  121. MODULE_DEVICE_TABLE(of, nau8315_device_id);
  122. #endif
  123. #ifdef CONFIG_ACPI
  124. static const struct acpi_device_id nau8315_acpi_match[] = {
  125. { "NVTN2010", 0 },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(acpi, nau8315_acpi_match);
  129. #endif
  130. static struct platform_driver nau8315_platform_driver = {
  131. .driver = {
  132. .name = "nau8315",
  133. .of_match_table = of_match_ptr(nau8315_device_id),
  134. .acpi_match_table = ACPI_PTR(nau8315_acpi_match),
  135. },
  136. .probe = nau8315_platform_probe,
  137. };
  138. module_platform_driver(nau8315_platform_driver);
  139. MODULE_DESCRIPTION("ASoC NAU8315 Mono Class-D Amplifier Driver");
  140. MODULE_AUTHOR("David Lin <[email protected]>");
  141. MODULE_LICENSE("GPL v2");