rx1950_uda1380.c 7.4 KB

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