simple-amplifier.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017 BayLibre, SAS.
  4. * Author: Jerome Brunet <jbrunet@baylibre.com>
  5. */
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/module.h>
  8. #include <linux/regulator/consumer.h>
  9. #include <sound/soc.h>
  10. #define DRV_NAME "simple-amplifier"
  11. struct simple_amp {
  12. struct gpio_desc *gpiod_enable;
  13. };
  14. static int drv_event(struct snd_soc_dapm_widget *w,
  15. struct snd_kcontrol *control, int event)
  16. {
  17. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  18. struct simple_amp *priv = snd_soc_component_get_drvdata(c);
  19. int val;
  20. switch (event) {
  21. case SND_SOC_DAPM_POST_PMU:
  22. val = 1;
  23. break;
  24. case SND_SOC_DAPM_PRE_PMD:
  25. val = 0;
  26. break;
  27. default:
  28. WARN(1, "Unexpected event");
  29. return -EINVAL;
  30. }
  31. gpiod_set_value_cansleep(priv->gpiod_enable, val);
  32. return 0;
  33. }
  34. static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
  35. SND_SOC_DAPM_INPUT("INL"),
  36. SND_SOC_DAPM_INPUT("INR"),
  37. SND_SOC_DAPM_OUT_DRV_E("DRV", SND_SOC_NOPM, 0, 0, NULL, 0, drv_event,
  38. (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
  39. SND_SOC_DAPM_OUTPUT("OUTL"),
  40. SND_SOC_DAPM_OUTPUT("OUTR"),
  41. SND_SOC_DAPM_REGULATOR_SUPPLY("VCC", 20, 0),
  42. };
  43. static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
  44. { "DRV", NULL, "INL" },
  45. { "DRV", NULL, "INR" },
  46. { "OUTL", NULL, "VCC" },
  47. { "OUTR", NULL, "VCC" },
  48. { "OUTL", NULL, "DRV" },
  49. { "OUTR", NULL, "DRV" },
  50. };
  51. static const struct snd_soc_component_driver simple_amp_component_driver = {
  52. .dapm_widgets = simple_amp_dapm_widgets,
  53. .num_dapm_widgets = ARRAY_SIZE(simple_amp_dapm_widgets),
  54. .dapm_routes = simple_amp_dapm_routes,
  55. .num_dapm_routes = ARRAY_SIZE(simple_amp_dapm_routes),
  56. };
  57. static int simple_amp_probe(struct platform_device *pdev)
  58. {
  59. struct device *dev = &pdev->dev;
  60. struct simple_amp *priv;
  61. int err;
  62. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  63. if (priv == NULL)
  64. return -ENOMEM;
  65. platform_set_drvdata(pdev, priv);
  66. priv->gpiod_enable = devm_gpiod_get_optional(dev, "enable",
  67. GPIOD_OUT_LOW);
  68. if (IS_ERR(priv->gpiod_enable)) {
  69. err = PTR_ERR(priv->gpiod_enable);
  70. if (err != -EPROBE_DEFER)
  71. dev_err(dev, "Failed to get 'enable' gpio: %d", err);
  72. return err;
  73. }
  74. return devm_snd_soc_register_component(dev,
  75. &simple_amp_component_driver,
  76. NULL, 0);
  77. }
  78. #ifdef CONFIG_OF
  79. static const struct of_device_id simple_amp_ids[] = {
  80. { .compatible = "dioo,dio2125", },
  81. { .compatible = "simple-audio-amplifier", },
  82. { }
  83. };
  84. MODULE_DEVICE_TABLE(of, simple_amp_ids);
  85. #endif
  86. static struct platform_driver simple_amp_driver = {
  87. .driver = {
  88. .name = DRV_NAME,
  89. .of_match_table = of_match_ptr(simple_amp_ids),
  90. },
  91. .probe = simple_amp_probe,
  92. };
  93. module_platform_driver(simple_amp_driver);
  94. MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
  95. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  96. MODULE_LICENSE("GPL");