max98357a.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * max98357a.c -- MAX98357A ALSA SoC Codec driver
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <sound/pcm.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dai.h>
  27. #include <sound/soc-dapm.h>
  28. static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
  29. int cmd, struct snd_soc_dai *dai)
  30. {
  31. struct gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai);
  32. if (!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. gpiod_set_value(sdmode, 1);
  39. break;
  40. case SNDRV_PCM_TRIGGER_STOP:
  41. case SNDRV_PCM_TRIGGER_SUSPEND:
  42. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  43. gpiod_set_value(sdmode, 0);
  44. break;
  45. }
  46. return 0;
  47. }
  48. static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
  49. SND_SOC_DAPM_OUTPUT("Speaker"),
  50. };
  51. static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
  52. {"Speaker", NULL, "HiFi Playback"},
  53. };
  54. static int max98357a_codec_probe(struct snd_soc_codec *codec)
  55. {
  56. struct gpio_desc *sdmode;
  57. sdmode = devm_gpiod_get_optional(codec->dev, "sdmode", GPIOD_OUT_LOW);
  58. if (IS_ERR(sdmode))
  59. return PTR_ERR(sdmode);
  60. snd_soc_codec_set_drvdata(codec, sdmode);
  61. return 0;
  62. }
  63. static struct snd_soc_codec_driver max98357a_codec_driver = {
  64. .probe = max98357a_codec_probe,
  65. .component_driver = {
  66. .dapm_widgets = max98357a_dapm_widgets,
  67. .num_dapm_widgets = ARRAY_SIZE(max98357a_dapm_widgets),
  68. .dapm_routes = max98357a_dapm_routes,
  69. .num_dapm_routes = ARRAY_SIZE(max98357a_dapm_routes),
  70. },
  71. };
  72. static const struct snd_soc_dai_ops max98357a_dai_ops = {
  73. .trigger = max98357a_daiops_trigger,
  74. };
  75. static struct snd_soc_dai_driver max98357a_dai_driver = {
  76. .name = "HiFi",
  77. .playback = {
  78. .stream_name = "HiFi Playback",
  79. .formats = SNDRV_PCM_FMTBIT_S16 |
  80. SNDRV_PCM_FMTBIT_S24 |
  81. SNDRV_PCM_FMTBIT_S32,
  82. .rates = SNDRV_PCM_RATE_8000 |
  83. SNDRV_PCM_RATE_16000 |
  84. SNDRV_PCM_RATE_48000 |
  85. SNDRV_PCM_RATE_96000,
  86. .rate_min = 8000,
  87. .rate_max = 96000,
  88. .channels_min = 1,
  89. .channels_max = 2,
  90. },
  91. .ops = &max98357a_dai_ops,
  92. };
  93. static int max98357a_platform_probe(struct platform_device *pdev)
  94. {
  95. return snd_soc_register_codec(&pdev->dev, &max98357a_codec_driver,
  96. &max98357a_dai_driver, 1);
  97. }
  98. static int max98357a_platform_remove(struct platform_device *pdev)
  99. {
  100. snd_soc_unregister_codec(&pdev->dev);
  101. return 0;
  102. }
  103. #ifdef CONFIG_OF
  104. static const struct of_device_id max98357a_device_id[] = {
  105. { .compatible = "maxim,max98357a" },
  106. {}
  107. };
  108. MODULE_DEVICE_TABLE(of, max98357a_device_id);
  109. #endif
  110. #ifdef CONFIG_ACPI
  111. static const struct acpi_device_id max98357a_acpi_match[] = {
  112. { "MX98357A", 0 },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
  116. #endif
  117. static struct platform_driver max98357a_platform_driver = {
  118. .driver = {
  119. .name = "max98357a",
  120. .of_match_table = of_match_ptr(max98357a_device_id),
  121. .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
  122. },
  123. .probe = max98357a_platform_probe,
  124. .remove = max98357a_platform_remove,
  125. };
  126. module_platform_driver(max98357a_platform_driver);
  127. MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
  128. MODULE_LICENSE("GPL v2");