rx1950_uda1380.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * rx1950.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
  5. *
  6. * Based on smdk2440.c and magician.c
  7. *
  8. * Authors: Graeme Gregory graeme.gregory@wolfsonmicro.com
  9. * Philipp Zabel <philipp.zabel@gmail.com>
  10. * Denis Grigoriev <dgreenday@gmail.com>
  11. * Vasily Khoruzhick <anarsoul@gmail.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. */
  19. #include <linux/types.h>
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <sound/soc.h>
  23. #include <sound/jack.h>
  24. #include <plat/regs-iis.h>
  25. #include <asm/mach-types.h>
  26. #include "s3c24xx-i2s.h"
  27. static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
  28. static int rx1950_startup(struct snd_pcm_substream *substream);
  29. static int rx1950_hw_params(struct snd_pcm_substream *substream,
  30. struct snd_pcm_hw_params *params);
  31. static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
  32. struct snd_kcontrol *kcontrol, int event);
  33. static unsigned int rates[] = {
  34. 16000,
  35. 44100,
  36. 48000,
  37. };
  38. static struct snd_pcm_hw_constraint_list hw_rates = {
  39. .count = ARRAY_SIZE(rates),
  40. .list = rates,
  41. .mask = 0,
  42. };
  43. static struct snd_soc_jack hp_jack;
  44. static struct snd_soc_jack_pin hp_jack_pins[] = {
  45. {
  46. .pin = "Headphone Jack",
  47. .mask = SND_JACK_HEADPHONE,
  48. },
  49. {
  50. .pin = "Speaker",
  51. .mask = SND_JACK_HEADPHONE,
  52. .invert = 1,
  53. },
  54. };
  55. static struct snd_soc_jack_gpio hp_jack_gpios[] = {
  56. [0] = {
  57. .gpio = S3C2410_GPG(12),
  58. .name = "hp-gpio",
  59. .report = SND_JACK_HEADPHONE,
  60. .invert = 1,
  61. .debounce_time = 200,
  62. },
  63. };
  64. static struct snd_soc_ops rx1950_ops = {
  65. .startup = rx1950_startup,
  66. .hw_params = rx1950_hw_params,
  67. };
  68. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  69. static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
  70. {
  71. .name = "uda1380",
  72. .stream_name = "UDA1380 Duplex",
  73. .cpu_dai_name = "s3c24xx-iis",
  74. .codec_dai_name = "uda1380-hifi",
  75. .init = rx1950_uda1380_init,
  76. .platform_name = "samsung-audio",
  77. .codec_name = "uda1380-codec.0-001a",
  78. .ops = &rx1950_ops,
  79. },
  80. };
  81. /* rx1950 machine dapm widgets */
  82. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  83. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  84. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  85. SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
  86. };
  87. /* rx1950 machine audio_map */
  88. static const struct snd_soc_dapm_route audio_map[] = {
  89. /* headphone connected to VOUTLHP, VOUTRHP */
  90. {"Headphone Jack", NULL, "VOUTLHP"},
  91. {"Headphone Jack", NULL, "VOUTRHP"},
  92. /* ext speaker connected to VOUTL, VOUTR */
  93. {"Speaker", NULL, "VOUTL"},
  94. {"Speaker", NULL, "VOUTR"},
  95. /* mic is connected to VINM */
  96. {"VINM", NULL, "Mic Jack"},
  97. };
  98. static struct snd_soc_card rx1950_asoc = {
  99. .name = "rx1950",
  100. .owner = THIS_MODULE,
  101. .dai_link = rx1950_uda1380_dai,
  102. .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
  103. .dapm_widgets = uda1380_dapm_widgets,
  104. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  105. .dapm_routes = audio_map,
  106. .num_dapm_routes = ARRAY_SIZE(audio_map),
  107. };
  108. static struct platform_device *s3c24xx_snd_device;
  109. static int rx1950_startup(struct snd_pcm_substream *substream)
  110. {
  111. struct snd_pcm_runtime *runtime = substream->runtime;
  112. runtime->hw.rate_min = hw_rates.list[0];
  113. runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
  114. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  115. return snd_pcm_hw_constraint_list(runtime, 0,
  116. SNDRV_PCM_HW_PARAM_RATE,
  117. &hw_rates);
  118. }
  119. static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
  120. struct snd_kcontrol *kcontrol, int event)
  121. {
  122. if (SND_SOC_DAPM_EVENT_ON(event))
  123. gpio_set_value(S3C2410_GPA(1), 1);
  124. else
  125. gpio_set_value(S3C2410_GPA(1), 0);
  126. return 0;
  127. }
  128. static int rx1950_hw_params(struct snd_pcm_substream *substream,
  129. struct snd_pcm_hw_params *params)
  130. {
  131. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  132. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  133. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  134. int div;
  135. int ret;
  136. unsigned int rate = params_rate(params);
  137. int clk_source, fs_mode;
  138. switch (rate) {
  139. case 16000:
  140. case 48000:
  141. clk_source = S3C24XX_CLKSRC_PCLK;
  142. fs_mode = S3C2410_IISMOD_256FS;
  143. div = s3c24xx_i2s_get_clockrate() / (256 * rate);
  144. if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
  145. div++;
  146. break;
  147. case 44100:
  148. case 88200:
  149. clk_source = S3C24XX_CLKSRC_MPLL;
  150. fs_mode = S3C2410_IISMOD_384FS;
  151. div = 1;
  152. break;
  153. default:
  154. printk(KERN_ERR "%s: rate %d is not supported\n",
  155. __func__, rate);
  156. return -EINVAL;
  157. }
  158. /* set codec DAI configuration */
  159. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  160. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  161. if (ret < 0)
  162. return ret;
  163. /* set cpu DAI configuration */
  164. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  165. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  166. if (ret < 0)
  167. return ret;
  168. /* select clock source */
  169. ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
  170. SND_SOC_CLOCK_OUT);
  171. if (ret < 0)
  172. return ret;
  173. /* set MCLK division for sample rate */
  174. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  175. fs_mode);
  176. if (ret < 0)
  177. return ret;
  178. /* set BCLK division for sample rate */
  179. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  180. S3C2410_IISMOD_32FS);
  181. if (ret < 0)
  182. return ret;
  183. /* set prescaler division for sample rate */
  184. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  185. S3C24XX_PRESCALE(div, div));
  186. if (ret < 0)
  187. return ret;
  188. return 0;
  189. }
  190. static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  191. {
  192. struct snd_soc_codec *codec = rtd->codec;
  193. struct snd_soc_dapm_context *dapm = &codec->dapm;
  194. int err;
  195. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  196. snd_soc_dapm_enable_pin(dapm, "Speaker");
  197. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  198. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  199. &hp_jack);
  200. snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
  201. hp_jack_pins);
  202. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  203. hp_jack_gpios);
  204. return 0;
  205. }
  206. static int __init rx1950_init(void)
  207. {
  208. int ret;
  209. if (!machine_is_rx1950())
  210. return -ENODEV;
  211. /* configure some gpios */
  212. ret = gpio_request(S3C2410_GPA(1), "speaker-power");
  213. if (ret)
  214. goto err_gpio;
  215. ret = gpio_direction_output(S3C2410_GPA(1), 0);
  216. if (ret)
  217. goto err_gpio_conf;
  218. s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
  219. if (!s3c24xx_snd_device) {
  220. ret = -ENOMEM;
  221. goto err_plat_alloc;
  222. }
  223. platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
  224. ret = platform_device_add(s3c24xx_snd_device);
  225. if (ret) {
  226. platform_device_put(s3c24xx_snd_device);
  227. goto err_plat_add;
  228. }
  229. return 0;
  230. err_plat_add:
  231. err_plat_alloc:
  232. err_gpio_conf:
  233. gpio_free(S3C2410_GPA(1));
  234. err_gpio:
  235. return ret;
  236. }
  237. static void __exit rx1950_exit(void)
  238. {
  239. platform_device_unregister(s3c24xx_snd_device);
  240. snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  241. hp_jack_gpios);
  242. gpio_free(S3C2410_GPA(1));
  243. }
  244. module_init(rx1950_init);
  245. module_exit(rx1950_exit);
  246. /* Module information */
  247. MODULE_AUTHOR("Vasily Khoruzhick");
  248. MODULE_DESCRIPTION("ALSA SoC RX1950");
  249. MODULE_LICENSE("GPL");