h1940_uda1380.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <plat/regs-iis.h>
  21. #include <asm/mach-types.h>
  22. #include "s3c24xx-i2s.h"
  23. static unsigned int rates[] = {
  24. 11025,
  25. 22050,
  26. 44100,
  27. };
  28. static struct snd_pcm_hw_constraint_list hw_rates = {
  29. .count = ARRAY_SIZE(rates),
  30. .list = rates,
  31. .mask = 0,
  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. runtime->hw.rate_min = hw_rates.list[0];
  58. runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
  59. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  60. return snd_pcm_hw_constraint_list(runtime, 0,
  61. SNDRV_PCM_HW_PARAM_RATE,
  62. &hw_rates);
  63. }
  64. static int h1940_hw_params(struct snd_pcm_substream *substream,
  65. struct snd_pcm_hw_params *params)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  68. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  69. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  70. int div;
  71. int ret;
  72. unsigned int rate = params_rate(params);
  73. switch (rate) {
  74. case 11025:
  75. case 22050:
  76. case 44100:
  77. div = s3c24xx_i2s_get_clockrate() / (384 * rate);
  78. if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
  79. div++;
  80. break;
  81. default:
  82. dev_err(&rtd->dev, "%s: rate %d is not supported\n",
  83. __func__, rate);
  84. return -EINVAL;
  85. }
  86. /* set codec DAI configuration */
  87. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  88. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  89. if (ret < 0)
  90. return ret;
  91. /* set cpu DAI configuration */
  92. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  93. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  94. if (ret < 0)
  95. return ret;
  96. /* select clock source */
  97. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
  98. SND_SOC_CLOCK_OUT);
  99. if (ret < 0)
  100. return ret;
  101. /* set MCLK division for sample rate */
  102. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  103. S3C2410_IISMOD_384FS);
  104. if (ret < 0)
  105. return ret;
  106. /* set BCLK division for sample rate */
  107. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  108. S3C2410_IISMOD_32FS);
  109. if (ret < 0)
  110. return ret;
  111. /* set prescaler division for sample rate */
  112. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  113. S3C24XX_PRESCALE(div, div));
  114. if (ret < 0)
  115. return ret;
  116. return 0;
  117. }
  118. static struct snd_soc_ops h1940_ops = {
  119. .startup = h1940_startup,
  120. .hw_params = h1940_hw_params,
  121. };
  122. static int h1940_spk_power(struct snd_soc_dapm_widget *w,
  123. struct snd_kcontrol *kcontrol, int event)
  124. {
  125. if (SND_SOC_DAPM_EVENT_ON(event))
  126. gpio_set_value(S3C_GPIO_END + 9, 1);
  127. else
  128. gpio_set_value(S3C_GPIO_END + 9, 0);
  129. return 0;
  130. }
  131. /* h1940 machine dapm widgets */
  132. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  133. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  134. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  135. SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
  136. };
  137. /* h1940 machine audio_map */
  138. static const struct snd_soc_dapm_route audio_map[] = {
  139. /* headphone connected to VOUTLHP, VOUTRHP */
  140. {"Headphone Jack", NULL, "VOUTLHP"},
  141. {"Headphone Jack", NULL, "VOUTRHP"},
  142. /* ext speaker connected to VOUTL, VOUTR */
  143. {"Speaker", NULL, "VOUTL"},
  144. {"Speaker", NULL, "VOUTR"},
  145. /* mic is connected to VINM */
  146. {"VINM", NULL, "Mic Jack"},
  147. };
  148. static struct platform_device *s3c24xx_snd_device;
  149. static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  150. {
  151. struct snd_soc_codec *codec = rtd->codec;
  152. struct snd_soc_dapm_context *dapm = &codec->dapm;
  153. int err;
  154. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  155. snd_soc_dapm_enable_pin(dapm, "Speaker");
  156. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  157. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  158. &hp_jack);
  159. snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
  160. hp_jack_pins);
  161. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  162. hp_jack_gpios);
  163. return 0;
  164. }
  165. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  166. static struct snd_soc_dai_link h1940_uda1380_dai[] = {
  167. {
  168. .name = "uda1380",
  169. .stream_name = "UDA1380 Duplex",
  170. .cpu_dai_name = "s3c24xx-iis",
  171. .codec_dai_name = "uda1380-hifi",
  172. .init = h1940_uda1380_init,
  173. .platform_name = "s3c24xx-iis",
  174. .codec_name = "uda1380-codec.0-001a",
  175. .ops = &h1940_ops,
  176. },
  177. };
  178. static struct snd_soc_card h1940_asoc = {
  179. .name = "h1940",
  180. .owner = THIS_MODULE,
  181. .dai_link = h1940_uda1380_dai,
  182. .num_links = ARRAY_SIZE(h1940_uda1380_dai),
  183. .dapm_widgets = uda1380_dapm_widgets,
  184. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  185. .dapm_routes = audio_map,
  186. .num_dapm_routes = ARRAY_SIZE(audio_map),
  187. };
  188. static int __init h1940_init(void)
  189. {
  190. int ret;
  191. if (!machine_is_h1940())
  192. return -ENODEV;
  193. /* configure some gpios */
  194. ret = gpio_request(S3C_GPIO_END + 9, "speaker-power");
  195. if (ret)
  196. goto err_out;
  197. ret = gpio_direction_output(S3C_GPIO_END + 9, 0);
  198. if (ret)
  199. goto err_gpio;
  200. s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
  201. if (!s3c24xx_snd_device) {
  202. ret = -ENOMEM;
  203. goto err_gpio;
  204. }
  205. platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
  206. ret = platform_device_add(s3c24xx_snd_device);
  207. if (ret)
  208. goto err_plat;
  209. return 0;
  210. err_plat:
  211. platform_device_put(s3c24xx_snd_device);
  212. err_gpio:
  213. gpio_free(S3C_GPIO_END + 9);
  214. err_out:
  215. return ret;
  216. }
  217. static void __exit h1940_exit(void)
  218. {
  219. platform_device_unregister(s3c24xx_snd_device);
  220. snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  221. hp_jack_gpios);
  222. gpio_free(S3C_GPIO_END + 9);
  223. }
  224. module_init(h1940_init);
  225. module_exit(h1940_exit);
  226. /* Module information */
  227. MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
  228. MODULE_DESCRIPTION("ALSA SoC H1940");
  229. MODULE_LICENSE("GPL");