rx1950_uda1380.c 6.4 KB

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