h1940_uda1380.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 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. return snd_pcm_hw_constraint_list(runtime, 0,
  59. SNDRV_PCM_HW_PARAM_RATE,
  60. &hw_rates);
  61. }
  62. static int h1940_hw_params(struct snd_pcm_substream *substream,
  63. struct snd_pcm_hw_params *params)
  64. {
  65. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  66. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  67. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  68. int div;
  69. int ret;
  70. unsigned int rate = params_rate(params);
  71. switch (rate) {
  72. case 11025:
  73. case 22050:
  74. case 44100:
  75. div = s3c24xx_i2s_get_clockrate() / (384 * rate);
  76. if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
  77. div++;
  78. break;
  79. default:
  80. dev_err(rtd->dev, "%s: rate %d is not supported\n",
  81. __func__, rate);
  82. return -EINVAL;
  83. }
  84. /* set codec DAI configuration */
  85. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  86. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  87. if (ret < 0)
  88. return ret;
  89. /* set cpu DAI configuration */
  90. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  91. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  92. if (ret < 0)
  93. return ret;
  94. /* select clock source */
  95. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
  96. SND_SOC_CLOCK_OUT);
  97. if (ret < 0)
  98. return ret;
  99. /* set MCLK division for sample rate */
  100. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  101. S3C2410_IISMOD_384FS);
  102. if (ret < 0)
  103. return ret;
  104. /* set BCLK division for sample rate */
  105. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  106. S3C2410_IISMOD_32FS);
  107. if (ret < 0)
  108. return ret;
  109. /* set prescaler division for sample rate */
  110. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  111. S3C24XX_PRESCALE(div, div));
  112. if (ret < 0)
  113. return ret;
  114. return 0;
  115. }
  116. static struct snd_soc_ops h1940_ops = {
  117. .startup = h1940_startup,
  118. .hw_params = h1940_hw_params,
  119. };
  120. static int h1940_spk_power(struct snd_soc_dapm_widget *w,
  121. struct snd_kcontrol *kcontrol, int event)
  122. {
  123. if (SND_SOC_DAPM_EVENT_ON(event))
  124. gpio_set_value(S3C_GPIO_END + 9, 1);
  125. else
  126. gpio_set_value(S3C_GPIO_END + 9, 0);
  127. return 0;
  128. }
  129. /* h1940 machine dapm widgets */
  130. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  131. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  132. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  133. SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
  134. };
  135. /* h1940 machine audio_map */
  136. static const struct snd_soc_dapm_route audio_map[] = {
  137. /* headphone connected to VOUTLHP, VOUTRHP */
  138. {"Headphone Jack", NULL, "VOUTLHP"},
  139. {"Headphone Jack", NULL, "VOUTRHP"},
  140. /* ext speaker connected to VOUTL, VOUTR */
  141. {"Speaker", NULL, "VOUTL"},
  142. {"Speaker", NULL, "VOUTR"},
  143. /* mic is connected to VINM */
  144. {"VINM", NULL, "Mic Jack"},
  145. };
  146. static struct platform_device *s3c24xx_snd_device;
  147. static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  148. {
  149. struct snd_soc_codec *codec = rtd->codec;
  150. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  151. &hp_jack);
  152. snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
  153. hp_jack_pins);
  154. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  155. hp_jack_gpios);
  156. return 0;
  157. }
  158. static int h1940_uda1380_card_remove(struct snd_soc_card *card)
  159. {
  160. snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  161. hp_jack_gpios);
  162. return 0;
  163. }
  164. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  165. static struct snd_soc_dai_link h1940_uda1380_dai[] = {
  166. {
  167. .name = "uda1380",
  168. .stream_name = "UDA1380 Duplex",
  169. .cpu_dai_name = "s3c24xx-iis",
  170. .codec_dai_name = "uda1380-hifi",
  171. .init = h1940_uda1380_init,
  172. .platform_name = "s3c24xx-iis",
  173. .codec_name = "uda1380-codec.0-001a",
  174. .ops = &h1940_ops,
  175. },
  176. };
  177. static struct snd_soc_card h1940_asoc = {
  178. .name = "h1940",
  179. .owner = THIS_MODULE,
  180. .remove = h1940_uda1380_card_remove,
  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. gpio_free(S3C_GPIO_END + 9);
  221. }
  222. module_init(h1940_init);
  223. module_exit(h1940_exit);
  224. /* Module information */
  225. MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
  226. MODULE_DESCRIPTION("ALSA SoC H1940");
  227. MODULE_LICENSE("GPL");