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/soc-dapm.h>
  27. #include <sound/uda1380.h>
  28. #include <sound/jack.h>
  29. #include <plat/regs-iis.h>
  30. #include <mach/regs-clock.h>
  31. #include <asm/mach-types.h>
  32. #include "s3c-dma.h"
  33. #include "s3c24xx-i2s.h"
  34. #include "../codecs/uda1380.h"
  35. static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
  36. static int rx1950_startup(struct snd_pcm_substream *substream);
  37. static int rx1950_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params);
  39. static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
  40. struct snd_kcontrol *kcontrol, int event);
  41. static unsigned int rates[] = {
  42. 16000,
  43. 44100,
  44. 48000,
  45. };
  46. static struct snd_pcm_hw_constraint_list hw_rates = {
  47. .count = ARRAY_SIZE(rates),
  48. .list = rates,
  49. .mask = 0,
  50. };
  51. static struct snd_soc_jack hp_jack;
  52. static struct snd_soc_jack_pin hp_jack_pins[] = {
  53. {
  54. .pin = "Headphone Jack",
  55. .mask = SND_JACK_HEADPHONE,
  56. },
  57. {
  58. .pin = "Speaker",
  59. .mask = SND_JACK_HEADPHONE,
  60. .invert = 1,
  61. },
  62. };
  63. static struct snd_soc_jack_gpio hp_jack_gpios[] = {
  64. [0] = {
  65. .gpio = S3C2410_GPG(12),
  66. .name = "hp-gpio",
  67. .report = SND_JACK_HEADPHONE,
  68. .invert = 1,
  69. .debounce_time = 200,
  70. },
  71. };
  72. static struct snd_soc_ops rx1950_ops = {
  73. .startup = rx1950_startup,
  74. .hw_params = rx1950_hw_params,
  75. };
  76. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  77. static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
  78. {
  79. .name = "uda1380",
  80. .stream_name = "UDA1380 Duplex",
  81. .cpu_dai_name = "s3c24xx-iis",
  82. .codec_dai_name = "uda1380-hifi",
  83. .init = rx1950_uda1380_init,
  84. .platform_name = "s3c24xx-pcm-audio",
  85. .codec_name = "uda1380-codec.0-001a",
  86. .ops = &rx1950_ops,
  87. },
  88. };
  89. static struct snd_soc_card rx1950_asoc = {
  90. .name = "rx1950",
  91. .dai_link = rx1950_uda1380_dai,
  92. .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
  93. };
  94. /* rx1950 machine dapm widgets */
  95. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  96. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  97. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  98. SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
  99. };
  100. /* rx1950 machine audio_map */
  101. static const struct snd_soc_dapm_route audio_map[] = {
  102. /* headphone connected to VOUTLHP, VOUTRHP */
  103. {"Headphone Jack", NULL, "VOUTLHP"},
  104. {"Headphone Jack", NULL, "VOUTRHP"},
  105. /* ext speaker connected to VOUTL, VOUTR */
  106. {"Speaker", NULL, "VOUTL"},
  107. {"Speaker", NULL, "VOUTR"},
  108. /* mic is connected to VINM */
  109. {"VINM", NULL, "Mic Jack"},
  110. };
  111. static struct platform_device *s3c24xx_snd_device;
  112. static int rx1950_startup(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_pcm_runtime *runtime = substream->runtime;
  115. runtime->hw.rate_min = hw_rates.list[0];
  116. runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
  117. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  118. return snd_pcm_hw_constraint_list(runtime, 0,
  119. SNDRV_PCM_HW_PARAM_RATE,
  120. &hw_rates);
  121. }
  122. static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
  123. struct snd_kcontrol *kcontrol, int event)
  124. {
  125. if (SND_SOC_DAPM_EVENT_ON(event))
  126. gpio_set_value(S3C2410_GPA(1), 1);
  127. else
  128. gpio_set_value(S3C2410_GPA(1), 0);
  129. return 0;
  130. }
  131. static int rx1950_hw_params(struct snd_pcm_substream *substream,
  132. struct snd_pcm_hw_params *params)
  133. {
  134. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  135. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  136. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  137. int div;
  138. int ret;
  139. unsigned int rate = params_rate(params);
  140. int clk_source, fs_mode;
  141. switch (rate) {
  142. case 16000:
  143. case 48000:
  144. clk_source = S3C24XX_CLKSRC_PCLK;
  145. fs_mode = S3C2410_IISMOD_256FS;
  146. div = s3c24xx_i2s_get_clockrate() / (256 * rate);
  147. if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
  148. div++;
  149. break;
  150. case 44100:
  151. case 88200:
  152. clk_source = S3C24XX_CLKSRC_MPLL;
  153. fs_mode = S3C2410_IISMOD_384FS;
  154. div = 1;
  155. break;
  156. default:
  157. printk(KERN_ERR "%s: rate %d is not supported\n",
  158. __func__, rate);
  159. return -EINVAL;
  160. }
  161. /* set codec DAI configuration */
  162. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  163. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  164. if (ret < 0)
  165. return ret;
  166. /* set cpu DAI configuration */
  167. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  168. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  169. if (ret < 0)
  170. return ret;
  171. /* select clock source */
  172. ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
  173. SND_SOC_CLOCK_OUT);
  174. if (ret < 0)
  175. return ret;
  176. /* set MCLK division for sample rate */
  177. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  178. fs_mode);
  179. if (ret < 0)
  180. return ret;
  181. /* set BCLK division for sample rate */
  182. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  183. S3C2410_IISMOD_32FS);
  184. if (ret < 0)
  185. return ret;
  186. /* set prescaler division for sample rate */
  187. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  188. S3C24XX_PRESCALE(div, div));
  189. if (ret < 0)
  190. return ret;
  191. return 0;
  192. }
  193. static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  194. {
  195. struct snd_soc_codec *codec = rtd->codec;
  196. int err;
  197. /* Add rx1950 specific widgets */
  198. err = snd_soc_dapm_new_controls(codec, 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(codec, audio_map,
  204. ARRAY_SIZE(audio_map));
  205. if (err)
  206. return err;
  207. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  208. snd_soc_dapm_enable_pin(codec, "Speaker");
  209. snd_soc_dapm_sync(codec);
  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");