h1940_uda1380.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * h1940-uda1380.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  5. * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
  6. *
  7. * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/types.h>
  16. #include <linux/gpio.h>
  17. #include <linux/module.h>
  18. #include <sound/soc.h>
  19. #include <sound/jack.h>
  20. #include "regs-iis.h"
  21. #include <asm/mach-types.h>
  22. #include <mach/gpio-samsung.h>
  23. #include "s3c24xx-i2s.h"
  24. static const unsigned int rates[] = {
  25. 11025,
  26. 22050,
  27. 44100,
  28. };
  29. static const struct snd_pcm_hw_constraint_list hw_rates = {
  30. .count = ARRAY_SIZE(rates),
  31. .list = rates,
  32. };
  33. static struct snd_soc_jack hp_jack;
  34. static struct snd_soc_jack_pin hp_jack_pins[] = {
  35. {
  36. .pin = "Headphone Jack",
  37. .mask = SND_JACK_HEADPHONE,
  38. },
  39. {
  40. .pin = "Speaker",
  41. .mask = SND_JACK_HEADPHONE,
  42. .invert = 1,
  43. },
  44. };
  45. static struct snd_soc_jack_gpio hp_jack_gpios[] = {
  46. {
  47. .gpio = S3C2410_GPG(4),
  48. .name = "hp-gpio",
  49. .report = SND_JACK_HEADPHONE,
  50. .invert = 1,
  51. .debounce_time = 200,
  52. },
  53. };
  54. static int h1940_startup(struct snd_pcm_substream *substream)
  55. {
  56. struct snd_pcm_runtime *runtime = substream->runtime;
  57. return snd_pcm_hw_constraint_list(runtime, 0,
  58. SNDRV_PCM_HW_PARAM_RATE,
  59. &hw_rates);
  60. }
  61. static int h1940_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  66. int div;
  67. int ret;
  68. unsigned int rate = params_rate(params);
  69. switch (rate) {
  70. case 11025:
  71. case 22050:
  72. case 44100:
  73. div = s3c24xx_i2s_get_clockrate() / (384 * rate);
  74. if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
  75. div++;
  76. break;
  77. default:
  78. dev_err(rtd->dev, "%s: rate %d is not supported\n",
  79. __func__, rate);
  80. return -EINVAL;
  81. }
  82. /* select clock source */
  83. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
  84. SND_SOC_CLOCK_OUT);
  85. if (ret < 0)
  86. return ret;
  87. /* set MCLK division for sample rate */
  88. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  89. S3C2410_IISMOD_384FS);
  90. if (ret < 0)
  91. return ret;
  92. /* set BCLK division for sample rate */
  93. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  94. S3C2410_IISMOD_32FS);
  95. if (ret < 0)
  96. return ret;
  97. /* set prescaler division for sample rate */
  98. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  99. S3C24XX_PRESCALE(div, div));
  100. if (ret < 0)
  101. return ret;
  102. return 0;
  103. }
  104. static struct snd_soc_ops h1940_ops = {
  105. .startup = h1940_startup,
  106. .hw_params = h1940_hw_params,
  107. };
  108. static int h1940_spk_power(struct snd_soc_dapm_widget *w,
  109. struct snd_kcontrol *kcontrol, int event)
  110. {
  111. if (SND_SOC_DAPM_EVENT_ON(event))
  112. gpio_set_value(S3C_GPIO_END + 9, 1);
  113. else
  114. gpio_set_value(S3C_GPIO_END + 9, 0);
  115. return 0;
  116. }
  117. /* h1940 machine dapm widgets */
  118. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  119. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  120. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  121. SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
  122. };
  123. /* h1940 machine audio_map */
  124. static const struct snd_soc_dapm_route audio_map[] = {
  125. /* headphone connected to VOUTLHP, VOUTRHP */
  126. {"Headphone Jack", NULL, "VOUTLHP"},
  127. {"Headphone Jack", NULL, "VOUTRHP"},
  128. /* ext speaker connected to VOUTL, VOUTR */
  129. {"Speaker", NULL, "VOUTL"},
  130. {"Speaker", NULL, "VOUTR"},
  131. /* mic is connected to VINM */
  132. {"VINM", NULL, "Mic Jack"},
  133. };
  134. static struct platform_device *s3c24xx_snd_device;
  135. static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  136. {
  137. snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
  138. &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
  139. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  140. hp_jack_gpios);
  141. return 0;
  142. }
  143. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  144. static struct snd_soc_dai_link h1940_uda1380_dai[] = {
  145. {
  146. .name = "uda1380",
  147. .stream_name = "UDA1380 Duplex",
  148. .cpu_dai_name = "s3c24xx-iis",
  149. .codec_dai_name = "uda1380-hifi",
  150. .init = h1940_uda1380_init,
  151. .platform_name = "s3c24xx-iis",
  152. .codec_name = "uda1380-codec.0-001a",
  153. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  154. SND_SOC_DAIFMT_CBS_CFS,
  155. .ops = &h1940_ops,
  156. },
  157. };
  158. static struct snd_soc_card h1940_asoc = {
  159. .name = "h1940",
  160. .owner = THIS_MODULE,
  161. .dai_link = h1940_uda1380_dai,
  162. .num_links = ARRAY_SIZE(h1940_uda1380_dai),
  163. .dapm_widgets = uda1380_dapm_widgets,
  164. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  165. .dapm_routes = audio_map,
  166. .num_dapm_routes = ARRAY_SIZE(audio_map),
  167. };
  168. static int __init h1940_init(void)
  169. {
  170. int ret;
  171. if (!machine_is_h1940())
  172. return -ENODEV;
  173. /* configure some gpios */
  174. ret = gpio_request(S3C_GPIO_END + 9, "speaker-power");
  175. if (ret)
  176. goto err_out;
  177. ret = gpio_direction_output(S3C_GPIO_END + 9, 0);
  178. if (ret)
  179. goto err_gpio;
  180. s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
  181. if (!s3c24xx_snd_device) {
  182. ret = -ENOMEM;
  183. goto err_gpio;
  184. }
  185. platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
  186. ret = platform_device_add(s3c24xx_snd_device);
  187. if (ret)
  188. goto err_plat;
  189. return 0;
  190. err_plat:
  191. platform_device_put(s3c24xx_snd_device);
  192. err_gpio:
  193. gpio_free(S3C_GPIO_END + 9);
  194. err_out:
  195. return ret;
  196. }
  197. static void __exit h1940_exit(void)
  198. {
  199. platform_device_unregister(s3c24xx_snd_device);
  200. gpio_free(S3C_GPIO_END + 9);
  201. }
  202. module_init(h1940_init);
  203. module_exit(h1940_exit);
  204. /* Module information */
  205. MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
  206. MODULE_DESCRIPTION("ALSA SoC H1940");
  207. MODULE_LICENSE("GPL");