h1940_uda1380.c 6.5 KB

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