rx1950_uda1380.c 7.9 KB

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