max98357a.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
  3. *
  4. * max98357a.c -- MAX98357A ALSA SoC Codec driver
  5. */
  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/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <sound/pcm.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-dai.h>
  20. #include <sound/soc-dapm.h>
  21. struct max98357a_priv {
  22. struct gpio_desc *sdmode;
  23. unsigned int sdmode_delay;
  24. int sdmode_switch;
  25. };
  26. static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
  27. int cmd, struct snd_soc_dai *dai)
  28. {
  29. struct snd_soc_component *component = dai->component;
  30. struct max98357a_priv *max98357a =
  31. snd_soc_component_get_drvdata(component);
  32. if (!max98357a->sdmode)
  33. return 0;
  34. switch (cmd) {
  35. case SNDRV_PCM_TRIGGER_START:
  36. case SNDRV_PCM_TRIGGER_RESUME:
  37. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  38. mdelay(max98357a->sdmode_delay);
  39. if (max98357a->sdmode_switch) {
  40. gpiod_set_value(max98357a->sdmode, 1);
  41. dev_dbg(component->dev, "set sdmode 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(max98357a->sdmode, 0);
  48. dev_dbg(component->dev, "set sdmode to 0");
  49. break;
  50. }
  51. return 0;
  52. }
  53. static int max98357a_sdmode_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 max98357a_priv *max98357a =
  59. snd_soc_component_get_drvdata(component);
  60. if (event & SND_SOC_DAPM_POST_PMU)
  61. max98357a->sdmode_switch = 1;
  62. else if (event & SND_SOC_DAPM_POST_PMD)
  63. max98357a->sdmode_switch = 0;
  64. return 0;
  65. }
  66. static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
  67. SND_SOC_DAPM_OUTPUT("Speaker"),
  68. SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0,
  69. max98357a_sdmode_event,
  70. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  71. };
  72. static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
  73. {"SD_MODE", NULL, "HiFi Playback"},
  74. {"Speaker", NULL, "SD_MODE"},
  75. };
  76. static const struct snd_soc_component_driver max98357a_component_driver = {
  77. .dapm_widgets = max98357a_dapm_widgets,
  78. .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets),
  79. .dapm_routes = max98357a_dapm_routes,
  80. .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes),
  81. .idle_bias_on = 1,
  82. .use_pmdown_time = 1,
  83. .endianness = 1,
  84. .non_legacy_dai_naming = 1,
  85. };
  86. static const struct snd_soc_dai_ops max98357a_dai_ops = {
  87. .trigger = max98357a_daiops_trigger,
  88. };
  89. static struct snd_soc_dai_driver max98357a_dai_driver = {
  90. .name = "HiFi",
  91. .playback = {
  92. .stream_name = "HiFi Playback",
  93. .formats = SNDRV_PCM_FMTBIT_S16 |
  94. SNDRV_PCM_FMTBIT_S24 |
  95. SNDRV_PCM_FMTBIT_S32,
  96. .rates = SNDRV_PCM_RATE_8000 |
  97. SNDRV_PCM_RATE_16000 |
  98. SNDRV_PCM_RATE_32000 |
  99. SNDRV_PCM_RATE_44100 |
  100. SNDRV_PCM_RATE_48000 |
  101. SNDRV_PCM_RATE_88200 |
  102. SNDRV_PCM_RATE_96000,
  103. .rate_min = 8000,
  104. .rate_max = 96000,
  105. .channels_min = 1,
  106. .channels_max = 2,
  107. },
  108. .ops = &max98357a_dai_ops,
  109. };
  110. static int max98357a_platform_probe(struct platform_device *pdev)
  111. {
  112. struct max98357a_priv *max98357a;
  113. int ret;
  114. max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL);
  115. if (!max98357a)
  116. return -ENOMEM;
  117. max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev,
  118. "sdmode", GPIOD_OUT_LOW);
  119. if (IS_ERR(max98357a->sdmode))
  120. return PTR_ERR(max98357a->sdmode);
  121. ret = device_property_read_u32(&pdev->dev, "sdmode-delay",
  122. &max98357a->sdmode_delay);
  123. if (ret) {
  124. max98357a->sdmode_delay = 0;
  125. dev_dbg(&pdev->dev,
  126. "no optional property 'sdmode-delay' found, "
  127. "default: no delay\n");
  128. }
  129. dev_set_drvdata(&pdev->dev, max98357a);
  130. return devm_snd_soc_register_component(&pdev->dev,
  131. &max98357a_component_driver,
  132. &max98357a_dai_driver, 1);
  133. }
  134. #ifdef CONFIG_OF
  135. static const struct of_device_id max98357a_device_id[] = {
  136. { .compatible = "maxim,max98357a" },
  137. { .compatible = "maxim,max98360a" },
  138. {}
  139. };
  140. MODULE_DEVICE_TABLE(of, max98357a_device_id);
  141. #endif
  142. #ifdef CONFIG_ACPI
  143. static const struct acpi_device_id max98357a_acpi_match[] = {
  144. { "MX98357A", 0 },
  145. { "MX98360A", 0 },
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
  149. #endif
  150. static struct platform_driver max98357a_platform_driver = {
  151. .driver = {
  152. .name = "max98357a",
  153. .of_match_table = of_match_ptr(max98357a_device_id),
  154. .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
  155. },
  156. .probe = max98357a_platform_probe,
  157. };
  158. module_platform_driver(max98357a_platform_driver);
  159. MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
  160. MODULE_LICENSE("GPL v2");