rx1950_uda1380.c 7.2 KB

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