h1940_uda1380.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * h1940-uda1380.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  5. * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
  6. *
  7. * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio.h>
  20. #include <sound/soc.h>
  21. #include <sound/uda1380.h>
  22. #include <sound/jack.h>
  23. #include <plat/regs-iis.h>
  24. #include <mach/h1940-latch.h>
  25. #include <asm/mach-types.h>
  26. #include "dma.h"
  27. #include "s3c24xx-i2s.h"
  28. #include "../codecs/uda1380.h"
  29. static unsigned int rates[] = {
  30. 11025,
  31. 22050,
  32. 44100,
  33. };
  34. static struct snd_pcm_hw_constraint_list hw_rates = {
  35. .count = ARRAY_SIZE(rates),
  36. .list = rates,
  37. .mask = 0,
  38. };
  39. static struct snd_soc_jack hp_jack;
  40. static struct snd_soc_jack_pin hp_jack_pins[] = {
  41. {
  42. .pin = "Headphone Jack",
  43. .mask = SND_JACK_HEADPHONE,
  44. },
  45. {
  46. .pin = "Speaker",
  47. .mask = SND_JACK_HEADPHONE,
  48. .invert = 1,
  49. },
  50. };
  51. static struct snd_soc_jack_gpio hp_jack_gpios[] = {
  52. {
  53. .gpio = S3C2410_GPG(4),
  54. .name = "hp-gpio",
  55. .report = SND_JACK_HEADPHONE,
  56. .invert = 1,
  57. .debounce_time = 200,
  58. },
  59. };
  60. static int h1940_startup(struct snd_pcm_substream *substream)
  61. {
  62. struct snd_pcm_runtime *runtime = substream->runtime;
  63. runtime->hw.rate_min = hw_rates.list[0];
  64. runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
  65. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  66. return snd_pcm_hw_constraint_list(runtime, 0,
  67. SNDRV_PCM_HW_PARAM_RATE,
  68. &hw_rates);
  69. }
  70. static int h1940_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params)
  72. {
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  75. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  76. int div;
  77. int ret;
  78. unsigned int rate = params_rate(params);
  79. switch (rate) {
  80. case 11025:
  81. case 22050:
  82. case 44100:
  83. div = s3c24xx_i2s_get_clockrate() / (384 * rate);
  84. if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
  85. div++;
  86. break;
  87. default:
  88. dev_err(&rtd->dev, "%s: rate %d is not supported\n",
  89. __func__, rate);
  90. return -EINVAL;
  91. }
  92. /* set codec DAI configuration */
  93. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  94. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  95. if (ret < 0)
  96. return ret;
  97. /* set cpu DAI configuration */
  98. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  99. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  100. if (ret < 0)
  101. return ret;
  102. /* select clock source */
  103. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
  104. SND_SOC_CLOCK_OUT);
  105. if (ret < 0)
  106. return ret;
  107. /* set MCLK division for sample rate */
  108. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  109. S3C2410_IISMOD_384FS);
  110. if (ret < 0)
  111. return ret;
  112. /* set BCLK division for sample rate */
  113. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
  114. S3C2410_IISMOD_32FS);
  115. if (ret < 0)
  116. return ret;
  117. /* set prescaler division for sample rate */
  118. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  119. S3C24XX_PRESCALE(div, div));
  120. if (ret < 0)
  121. return ret;
  122. return 0;
  123. }
  124. static struct snd_soc_ops h1940_ops = {
  125. .startup = h1940_startup,
  126. .hw_params = h1940_hw_params,
  127. };
  128. static int h1940_spk_power(struct snd_soc_dapm_widget *w,
  129. struct snd_kcontrol *kcontrol, int event)
  130. {
  131. if (SND_SOC_DAPM_EVENT_ON(event))
  132. gpio_set_value(H1940_LATCH_AUDIO_POWER, 1);
  133. else
  134. gpio_set_value(H1940_LATCH_AUDIO_POWER, 0);
  135. return 0;
  136. }
  137. /* h1940 machine dapm widgets */
  138. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  139. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  140. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  141. SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
  142. };
  143. /* h1940 machine audio_map */
  144. static const struct snd_soc_dapm_route audio_map[] = {
  145. /* headphone connected to VOUTLHP, VOUTRHP */
  146. {"Headphone Jack", NULL, "VOUTLHP"},
  147. {"Headphone Jack", NULL, "VOUTRHP"},
  148. /* ext speaker connected to VOUTL, VOUTR */
  149. {"Speaker", NULL, "VOUTL"},
  150. {"Speaker", NULL, "VOUTR"},
  151. /* mic is connected to VINM */
  152. {"VINM", NULL, "Mic Jack"},
  153. };
  154. static struct platform_device *s3c24xx_snd_device;
  155. static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
  156. {
  157. struct snd_soc_codec *codec = rtd->codec;
  158. struct snd_soc_dapm_context *dapm = &codec->dapm;
  159. int err;
  160. /* Add h1940 specific widgets */
  161. err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
  162. ARRAY_SIZE(uda1380_dapm_widgets));
  163. if (err)
  164. return err;
  165. /* Set up h1940 specific audio path audio_mapnects */
  166. err = snd_soc_dapm_add_routes(dapm, audio_map,
  167. ARRAY_SIZE(audio_map));
  168. if (err)
  169. return err;
  170. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  171. snd_soc_dapm_enable_pin(dapm, "Speaker");
  172. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  173. snd_soc_dapm_sync(dapm);
  174. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  175. &hp_jack);
  176. snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
  177. hp_jack_pins);
  178. snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  179. hp_jack_gpios);
  180. return 0;
  181. }
  182. /* s3c24xx digital audio interface glue - connects codec <--> CPU */
  183. static struct snd_soc_dai_link h1940_uda1380_dai[] = {
  184. {
  185. .name = "uda1380",
  186. .stream_name = "UDA1380 Duplex",
  187. .cpu_dai_name = "s3c24xx-iis",
  188. .codec_dai_name = "uda1380-hifi",
  189. .init = h1940_uda1380_init,
  190. .platform_name = "samsung-audio",
  191. .codec_name = "uda1380-codec.0-001a",
  192. .ops = &h1940_ops,
  193. },
  194. };
  195. static struct snd_soc_card h1940_asoc = {
  196. .name = "h1940",
  197. .dai_link = h1940_uda1380_dai,
  198. .num_links = ARRAY_SIZE(h1940_uda1380_dai),
  199. };
  200. static int __init h1940_init(void)
  201. {
  202. int ret;
  203. if (!machine_is_h1940())
  204. return -ENODEV;
  205. /* configure some gpios */
  206. ret = gpio_request(H1940_LATCH_AUDIO_POWER, "speaker-power");
  207. if (ret)
  208. goto err_out;
  209. ret = gpio_direction_output(H1940_LATCH_AUDIO_POWER, 0);
  210. if (ret)
  211. goto err_gpio;
  212. s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
  213. if (!s3c24xx_snd_device) {
  214. ret = -ENOMEM;
  215. goto err_gpio;
  216. }
  217. platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
  218. ret = platform_device_add(s3c24xx_snd_device);
  219. if (ret)
  220. goto err_plat;
  221. return 0;
  222. err_plat:
  223. platform_device_put(s3c24xx_snd_device);
  224. err_gpio:
  225. gpio_free(H1940_LATCH_AUDIO_POWER);
  226. err_out:
  227. return ret;
  228. }
  229. static void __exit h1940_exit(void)
  230. {
  231. platform_device_unregister(s3c24xx_snd_device);
  232. snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
  233. hp_jack_gpios);
  234. gpio_free(H1940_LATCH_AUDIO_POWER);
  235. }
  236. module_init(h1940_init);
  237. module_exit(h1940_exit);
  238. /* Module information */
  239. MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
  240. MODULE_DESCRIPTION("ALSA SoC H1940");
  241. MODULE_LICENSE("GPL");