h1940_uda1380.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // h1940_uda1380.c - ALSA SoC Audio Layer
  4. //
  5. // Copyright (c) 2010 Arnaud Patard <[email protected]>
  6. // Copyright (c) 2010 Vasily Khoruzhick <[email protected]>
  7. //
  8. // Based on version from Arnaud Patard <[email protected]>
  9. #include <linux/types.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/module.h>
  12. #include <sound/soc.h>
  13. #include <sound/jack.h>
  14. #include "regs-iis.h"
  15. #include "s3c24xx-i2s.h"
  16. static const unsigned int rates[] = {
  17. 11025,
  18. 22050,
  19. 44100,
  20. };
  21. static const struct snd_pcm_hw_constraint_list hw_rates = {
  22. .count = ARRAY_SIZE(rates),
  23. .list = rates,
  24. };
  25. static struct gpio_desc *gpiod_speaker_power;
  26. static struct snd_soc_jack hp_jack;
  27. static struct snd_soc_jack_pin hp_jack_pins[] = {
  28. {
  29. .pin = "Headphone Jack",
  30. .mask = SND_JACK_HEADPHONE,
  31. },
  32. {
  33. .pin = "Speaker",
  34. .mask = SND_JACK_HEADPHONE,
  35. .invert = 1,
  36. },
  37. };
  38. static struct snd_soc_jack_gpio hp_jack_gpios[] = {
  39. {
  40. .name = "hp-gpio",
  41. .report = SND_JACK_HEADPHONE,
  42. .invert = 1,
  43. .debounce_time = 200,
  44. },
  45. };
  46. static int h1940_startup(struct snd_pcm_substream *substream)
  47. {
  48. struct snd_pcm_runtime *runtime = substream->runtime;
  49. return snd_pcm_hw_constraint_list(runtime, 0,
  50. SNDRV_PCM_HW_PARAM_RATE,
  51. &hw_rates);
  52. }
  53. static int h1940_hw_params(struct snd_pcm_substream *substream,
  54. struct snd_pcm_hw_params *params)
  55. {
  56. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  57. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  58. int div;
  59. int ret;
  60. unsigned int rate = params_rate(params);
  61. switch (rate) {
  62. case 11025:
  63. case 22050:
  64. case 44100:
  65. div = s3c24xx_i2s_get_clockrate() / (384 * rate);
  66. if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
  67. div++;
  68. break;
  69. default:
  70. dev_err(rtd->dev, "%s: rate %d is not supported\n",
  71. __func__, rate);
  72. return -EINVAL;
  73. }
  74. /* select clock source */
  75. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
  76. SND_SOC_CLOCK_OUT);
  77. if (ret < 0)
  78. return ret;
  79. /* set MCLK division for sample rate */
  80. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  81. S3C2410_IISMOD_384FS);
  82. if (ret < 0)
  83. return ret;
  84. /* set BCLK division for sample rate */
  85. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  86. S3C2410_IISMOD_32FS);
  87. if (ret < 0)
  88. return ret;
  89. /* set prescaler division for sample rate */
  90. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  91. S3C24XX_PRESCALE(div, div));
  92. if (ret < 0)
  93. return ret;
  94. return 0;
  95. }
  96. static const struct snd_soc_ops h1940_ops = {
  97. .startup = h1940_startup,
  98. .hw_params = h1940_hw_params,
  99. };
  100. static int h1940_spk_power(struct snd_soc_dapm_widget *w,
  101. struct snd_kcontrol *kcontrol, int event)
  102. {
  103. if (SND_SOC_DAPM_EVENT_ON(event))
  104. gpiod_set_value(gpiod_speaker_power, 1);
  105. else
  106. gpiod_set_value(gpiod_speaker_power, 0);
  107. return 0;
  108. }
  109. /* h1940 machine dapm widgets */
  110. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  111. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  112. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  113. SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
  114. };
  115. /* h1940 machine audio_map */
  116. static const struct snd_soc_dapm_route audio_map[] = {
  117. /* headphone connected to VOUTLHP, VOUTRHP */
  118. {"Headphone Jack", NULL, "VOUTLHP"},
  119. {"Headphone Jack", NULL, "VOUTRHP"},
  120. /* ext speaker connected to VOUTL, VOUTR */
  121. {"Speaker", NULL, "VOUTL"},
  122. {"Speaker", NULL, "VOUTR"},
  123. /* mic is connected to VINM */
  124. {"VINM", NULL, "Mic Jack"},
  125. };
  126. static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  127. {
  128. snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
  129. SND_JACK_HEADPHONE,
  130. &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
  131. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  132. hp_jack_gpios);
  133. return 0;
  134. }
  135. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  136. SND_SOC_DAILINK_DEFS(uda1380,
  137. DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
  138. DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a", "uda1380-hifi")),
  139. DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
  140. static struct snd_soc_dai_link h1940_uda1380_dai[] = {
  141. {
  142. .name = "uda1380",
  143. .stream_name = "UDA1380 Duplex",
  144. .init = h1940_uda1380_init,
  145. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  146. SND_SOC_DAIFMT_CBS_CFS,
  147. .ops = &h1940_ops,
  148. SND_SOC_DAILINK_REG(uda1380),
  149. },
  150. };
  151. static struct snd_soc_card h1940_asoc = {
  152. .name = "h1940",
  153. .owner = THIS_MODULE,
  154. .dai_link = h1940_uda1380_dai,
  155. .num_links = ARRAY_SIZE(h1940_uda1380_dai),
  156. .dapm_widgets = uda1380_dapm_widgets,
  157. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  158. .dapm_routes = audio_map,
  159. .num_dapm_routes = ARRAY_SIZE(audio_map),
  160. };
  161. static int h1940_probe(struct platform_device *pdev)
  162. {
  163. struct device *dev = &pdev->dev;
  164. h1940_asoc.dev = dev;
  165. hp_jack_gpios[0].gpiod_dev = dev;
  166. gpiod_speaker_power = devm_gpiod_get(&pdev->dev, "speaker-power",
  167. GPIOD_OUT_LOW);
  168. if (IS_ERR(gpiod_speaker_power)) {
  169. dev_err(dev, "Could not get gpio\n");
  170. return PTR_ERR(gpiod_speaker_power);
  171. }
  172. return devm_snd_soc_register_card(dev, &h1940_asoc);
  173. }
  174. static struct platform_driver h1940_audio_driver = {
  175. .driver = {
  176. .name = "h1940-audio",
  177. .pm = &snd_soc_pm_ops,
  178. },
  179. .probe = h1940_probe,
  180. };
  181. module_platform_driver(h1940_audio_driver);
  182. /* Module information */
  183. MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
  184. MODULE_DESCRIPTION("ALSA SoC H1940");
  185. MODULE_LICENSE("GPL");
  186. MODULE_ALIAS("platform:h1940-audio");