rx1950_uda1380.c 7.3 KB

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