pcm3008.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA Soc PCM3008 codec support
  4. *
  5. * Author: Hugo Villeneuve
  6. * Copyright (C) 2008 Lyrtech inc
  7. *
  8. * Based on AC97 Soc codec, original copyright follow:
  9. * Copyright 2005 Wolfson Microelectronics PLC.
  10. *
  11. * Generic PCM3008 support.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/gpio.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/initval.h>
  22. #include <sound/soc.h>
  23. #include "pcm3008.h"
  24. static int pcm3008_dac_ev(struct snd_soc_dapm_widget *w,
  25. struct snd_kcontrol *kcontrol,
  26. int event)
  27. {
  28. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  29. struct pcm3008_setup_data *setup = component->dev->platform_data;
  30. gpio_set_value_cansleep(setup->pdda_pin,
  31. SND_SOC_DAPM_EVENT_ON(event));
  32. return 0;
  33. }
  34. static int pcm3008_adc_ev(struct snd_soc_dapm_widget *w,
  35. struct snd_kcontrol *kcontrol,
  36. int event)
  37. {
  38. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  39. struct pcm3008_setup_data *setup = component->dev->platform_data;
  40. gpio_set_value_cansleep(setup->pdad_pin,
  41. SND_SOC_DAPM_EVENT_ON(event));
  42. return 0;
  43. }
  44. static const struct snd_soc_dapm_widget pcm3008_dapm_widgets[] = {
  45. SND_SOC_DAPM_INPUT("VINL"),
  46. SND_SOC_DAPM_INPUT("VINR"),
  47. SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, pcm3008_dac_ev,
  48. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  49. SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0, pcm3008_adc_ev,
  50. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  51. SND_SOC_DAPM_OUTPUT("VOUTL"),
  52. SND_SOC_DAPM_OUTPUT("VOUTR"),
  53. };
  54. static const struct snd_soc_dapm_route pcm3008_dapm_routes[] = {
  55. { "PCM3008 Capture", NULL, "ADC" },
  56. { "ADC", NULL, "VINL" },
  57. { "ADC", NULL, "VINR" },
  58. { "DAC", NULL, "PCM3008 Playback" },
  59. { "VOUTL", NULL, "DAC" },
  60. { "VOUTR", NULL, "DAC" },
  61. };
  62. #define PCM3008_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  63. SNDRV_PCM_RATE_48000)
  64. static struct snd_soc_dai_driver pcm3008_dai = {
  65. .name = "pcm3008-hifi",
  66. .playback = {
  67. .stream_name = "PCM3008 Playback",
  68. .channels_min = 1,
  69. .channels_max = 2,
  70. .rates = PCM3008_RATES,
  71. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  72. },
  73. .capture = {
  74. .stream_name = "PCM3008 Capture",
  75. .channels_min = 1,
  76. .channels_max = 2,
  77. .rates = PCM3008_RATES,
  78. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  79. },
  80. };
  81. static const struct snd_soc_component_driver soc_component_dev_pcm3008 = {
  82. .dapm_widgets = pcm3008_dapm_widgets,
  83. .num_dapm_widgets = ARRAY_SIZE(pcm3008_dapm_widgets),
  84. .dapm_routes = pcm3008_dapm_routes,
  85. .num_dapm_routes = ARRAY_SIZE(pcm3008_dapm_routes),
  86. .idle_bias_on = 1,
  87. .use_pmdown_time = 1,
  88. .endianness = 1,
  89. .non_legacy_dai_naming = 1,
  90. };
  91. static int pcm3008_codec_probe(struct platform_device *pdev)
  92. {
  93. struct pcm3008_setup_data *setup = pdev->dev.platform_data;
  94. int ret;
  95. if (!setup)
  96. return -EINVAL;
  97. /* DEM1 DEM0 DE-EMPHASIS_MODE
  98. * Low Low De-emphasis 44.1 kHz ON
  99. * Low High De-emphasis OFF
  100. * High Low De-emphasis 48 kHz ON
  101. * High High De-emphasis 32 kHz ON
  102. */
  103. /* Configure DEM0 GPIO (turning OFF DAC De-emphasis). */
  104. ret = devm_gpio_request_one(&pdev->dev, setup->dem0_pin,
  105. GPIOF_OUT_INIT_HIGH, "codec_dem0");
  106. if (ret != 0)
  107. return ret;
  108. /* Configure DEM1 GPIO (turning OFF DAC De-emphasis). */
  109. ret = devm_gpio_request_one(&pdev->dev, setup->dem1_pin,
  110. GPIOF_OUT_INIT_LOW, "codec_dem1");
  111. if (ret != 0)
  112. return ret;
  113. /* Configure PDAD GPIO. */
  114. ret = devm_gpio_request_one(&pdev->dev, setup->pdad_pin,
  115. GPIOF_OUT_INIT_LOW, "codec_pdad");
  116. if (ret != 0)
  117. return ret;
  118. /* Configure PDDA GPIO. */
  119. ret = devm_gpio_request_one(&pdev->dev, setup->pdda_pin,
  120. GPIOF_OUT_INIT_LOW, "codec_pdda");
  121. if (ret != 0)
  122. return ret;
  123. return devm_snd_soc_register_component(&pdev->dev,
  124. &soc_component_dev_pcm3008, &pcm3008_dai, 1);
  125. }
  126. MODULE_ALIAS("platform:pcm3008-codec");
  127. static struct platform_driver pcm3008_codec_driver = {
  128. .probe = pcm3008_codec_probe,
  129. .driver = {
  130. .name = "pcm3008-codec",
  131. },
  132. };
  133. module_platform_driver(pcm3008_codec_driver);
  134. MODULE_DESCRIPTION("Soc PCM3008 driver");
  135. MODULE_AUTHOR("Hugo Villeneuve");
  136. MODULE_LICENSE("GPL");