rx51.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * rx51.c -- SoC audio for Nokia RX-51
  3. *
  4. * Copyright (C) 2008 - 2009 Nokia Corporation
  5. *
  6. * Contact: Peter Ujfalusi <peter.ujfalusi@nokia.com>
  7. * Eduardo Valentin <eduardo.valentin@nokia.com>
  8. * Jarkko Nikula <jhnikula@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/gpio.h>
  27. #include <linux/platform_device.h>
  28. #include <sound/core.h>
  29. #include <sound/jack.h>
  30. #include <sound/pcm.h>
  31. #include <sound/soc.h>
  32. #include <sound/soc-dapm.h>
  33. #include <plat/mcbsp.h>
  34. #include <asm/mach-types.h>
  35. #include "omap-mcbsp.h"
  36. #include "omap-pcm.h"
  37. #include "../codecs/tlv320aic3x.h"
  38. #define RX51_TVOUT_SEL_GPIO 40
  39. #define RX51_JACK_DETECT_GPIO 177
  40. /*
  41. * REVISIT: TWL4030 GPIO base in RX-51. Now statically defined to 192. This
  42. * gpio is reserved in arch/arm/mach-omap2/board-rx51-peripherals.c
  43. */
  44. #define RX51_SPEAKER_AMP_TWL_GPIO (192 + 7)
  45. enum {
  46. RX51_JACK_DISABLED,
  47. RX51_JACK_TVOUT, /* tv-out */
  48. };
  49. static int rx51_spk_func;
  50. static int rx51_dmic_func;
  51. static int rx51_jack_func;
  52. static void rx51_ext_control(struct snd_soc_codec *codec)
  53. {
  54. if (rx51_spk_func)
  55. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  56. else
  57. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  58. if (rx51_dmic_func)
  59. snd_soc_dapm_enable_pin(codec, "DMic");
  60. else
  61. snd_soc_dapm_disable_pin(codec, "DMic");
  62. gpio_set_value(RX51_TVOUT_SEL_GPIO,
  63. rx51_jack_func == RX51_JACK_TVOUT);
  64. snd_soc_dapm_sync(codec);
  65. }
  66. static int rx51_startup(struct snd_pcm_substream *substream)
  67. {
  68. struct snd_pcm_runtime *runtime = substream->runtime;
  69. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  70. struct snd_soc_codec *codec = rtd->codec;
  71. snd_pcm_hw_constraint_minmax(runtime,
  72. SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
  73. rx51_ext_control(codec);
  74. return 0;
  75. }
  76. static int rx51_hw_params(struct snd_pcm_substream *substream,
  77. struct snd_pcm_hw_params *params)
  78. {
  79. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  80. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  81. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  82. int err;
  83. /* Set codec DAI configuration */
  84. err = snd_soc_dai_set_fmt(codec_dai,
  85. SND_SOC_DAIFMT_DSP_A |
  86. SND_SOC_DAIFMT_IB_NF |
  87. SND_SOC_DAIFMT_CBM_CFM);
  88. if (err < 0)
  89. return err;
  90. /* Set cpu DAI configuration */
  91. err = snd_soc_dai_set_fmt(cpu_dai,
  92. SND_SOC_DAIFMT_DSP_A |
  93. SND_SOC_DAIFMT_IB_NF |
  94. SND_SOC_DAIFMT_CBM_CFM);
  95. if (err < 0)
  96. return err;
  97. /* Set the codec system clock for DAC and ADC */
  98. return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
  99. SND_SOC_CLOCK_IN);
  100. }
  101. static struct snd_soc_ops rx51_ops = {
  102. .startup = rx51_startup,
  103. .hw_params = rx51_hw_params,
  104. };
  105. static int rx51_get_spk(struct snd_kcontrol *kcontrol,
  106. struct snd_ctl_elem_value *ucontrol)
  107. {
  108. ucontrol->value.integer.value[0] = rx51_spk_func;
  109. return 0;
  110. }
  111. static int rx51_set_spk(struct snd_kcontrol *kcontrol,
  112. struct snd_ctl_elem_value *ucontrol)
  113. {
  114. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  115. if (rx51_spk_func == ucontrol->value.integer.value[0])
  116. return 0;
  117. rx51_spk_func = ucontrol->value.integer.value[0];
  118. rx51_ext_control(codec);
  119. return 1;
  120. }
  121. static int rx51_spk_event(struct snd_soc_dapm_widget *w,
  122. struct snd_kcontrol *k, int event)
  123. {
  124. if (SND_SOC_DAPM_EVENT_ON(event))
  125. gpio_set_value_cansleep(RX51_SPEAKER_AMP_TWL_GPIO, 1);
  126. else
  127. gpio_set_value_cansleep(RX51_SPEAKER_AMP_TWL_GPIO, 0);
  128. return 0;
  129. }
  130. static int rx51_get_input(struct snd_kcontrol *kcontrol,
  131. struct snd_ctl_elem_value *ucontrol)
  132. {
  133. ucontrol->value.integer.value[0] = rx51_dmic_func;
  134. return 0;
  135. }
  136. static int rx51_set_input(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  140. if (rx51_dmic_func == ucontrol->value.integer.value[0])
  141. return 0;
  142. rx51_dmic_func = ucontrol->value.integer.value[0];
  143. rx51_ext_control(codec);
  144. return 1;
  145. }
  146. static int rx51_get_jack(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_value *ucontrol)
  148. {
  149. ucontrol->value.integer.value[0] = rx51_jack_func;
  150. return 0;
  151. }
  152. static int rx51_set_jack(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  156. if (rx51_jack_func == ucontrol->value.integer.value[0])
  157. return 0;
  158. rx51_jack_func = ucontrol->value.integer.value[0];
  159. rx51_ext_control(codec);
  160. return 1;
  161. }
  162. static struct snd_soc_jack rx51_av_jack;
  163. static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
  164. {
  165. .gpio = RX51_JACK_DETECT_GPIO,
  166. .name = "avdet-gpio",
  167. .report = SND_JACK_VIDEOOUT,
  168. .invert = 1,
  169. .debounce_time = 200,
  170. },
  171. };
  172. static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
  173. SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
  174. SND_SOC_DAPM_MIC("DMic", NULL),
  175. };
  176. static const struct snd_soc_dapm_route audio_map[] = {
  177. {"Ext Spk", NULL, "HPLOUT"},
  178. {"Ext Spk", NULL, "HPROUT"},
  179. {"DMic Rate 64", NULL, "Mic Bias 2V"},
  180. {"Mic Bias 2V", NULL, "DMic"},
  181. };
  182. static const char *spk_function[] = {"Off", "On"};
  183. static const char *input_function[] = {"ADC", "Digital Mic"};
  184. static const char *jack_function[] = {"Off", "TV-OUT"};
  185. static const struct soc_enum rx51_enum[] = {
  186. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  187. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  188. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  189. };
  190. static const struct snd_kcontrol_new aic34_rx51_controls[] = {
  191. SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
  192. rx51_get_spk, rx51_set_spk),
  193. SOC_ENUM_EXT("Input Select", rx51_enum[1],
  194. rx51_get_input, rx51_set_input),
  195. SOC_ENUM_EXT("Jack Function", rx51_enum[2],
  196. rx51_get_jack, rx51_set_jack),
  197. };
  198. static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
  199. {
  200. struct snd_soc_codec *codec = rtd->codec;
  201. int err;
  202. /* Set up NC codec pins */
  203. snd_soc_dapm_nc_pin(codec, "MIC3L");
  204. snd_soc_dapm_nc_pin(codec, "MIC3R");
  205. snd_soc_dapm_nc_pin(codec, "LINE1R");
  206. /* Add RX-51 specific controls */
  207. err = snd_soc_add_controls(codec, aic34_rx51_controls,
  208. ARRAY_SIZE(aic34_rx51_controls));
  209. if (err < 0)
  210. return err;
  211. /* Add RX-51 specific widgets */
  212. snd_soc_dapm_new_controls(codec, aic34_dapm_widgets,
  213. ARRAY_SIZE(aic34_dapm_widgets));
  214. /* Set up RX-51 specific audio path audio_map */
  215. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  216. snd_soc_dapm_sync(codec);
  217. /* AV jack detection */
  218. err = snd_soc_jack_new(codec, "AV Jack",
  219. SND_JACK_VIDEOOUT, &rx51_av_jack);
  220. if (err)
  221. return err;
  222. err = snd_soc_jack_add_gpios(&rx51_av_jack,
  223. ARRAY_SIZE(rx51_av_jack_gpios),
  224. rx51_av_jack_gpios);
  225. return err;
  226. }
  227. /* Digital audio interface glue - connects codec <--> CPU */
  228. static struct snd_soc_dai_link rx51_dai[] = {
  229. {
  230. .name = "TLV320AIC34",
  231. .stream_name = "AIC34",
  232. .cpu_dai_name = "omap-mcbsp-dai.1",
  233. .codec_dai_name = "tlv320aic3x-hifi",
  234. .platform_name = "omap-pcm-audio",
  235. .codec_name = "tlv320aic3x-codec.2-0018",
  236. .init = rx51_aic34_init,
  237. .ops = &rx51_ops,
  238. },
  239. };
  240. /* Audio card */
  241. static struct snd_soc_card rx51_sound_card = {
  242. .name = "RX-51",
  243. .dai_link = rx51_dai,
  244. .num_links = ARRAY_SIZE(rx51_dai),
  245. };
  246. static struct platform_device *rx51_snd_device;
  247. static int __init rx51_soc_init(void)
  248. {
  249. int err;
  250. if (!machine_is_nokia_rx51())
  251. return -ENODEV;
  252. err = gpio_request(RX51_TVOUT_SEL_GPIO, "tvout_sel");
  253. if (err)
  254. goto err_gpio_tvout_sel;
  255. gpio_direction_output(RX51_TVOUT_SEL_GPIO, 0);
  256. rx51_snd_device = platform_device_alloc("soc-audio", -1);
  257. if (!rx51_snd_device) {
  258. err = -ENOMEM;
  259. goto err1;
  260. }
  261. platform_set_drvdata(rx51_snd_device, &rx51_sound_card);
  262. err = platform_device_add(rx51_snd_device);
  263. if (err)
  264. goto err2;
  265. return 0;
  266. err2:
  267. platform_device_put(rx51_snd_device);
  268. err1:
  269. gpio_free(RX51_TVOUT_SEL_GPIO);
  270. err_gpio_tvout_sel:
  271. return err;
  272. }
  273. static void __exit rx51_soc_exit(void)
  274. {
  275. snd_soc_jack_free_gpios(&rx51_av_jack, ARRAY_SIZE(rx51_av_jack_gpios),
  276. rx51_av_jack_gpios);
  277. platform_device_unregister(rx51_snd_device);
  278. gpio_free(RX51_TVOUT_SEL_GPIO);
  279. }
  280. module_init(rx51_soc_init);
  281. module_exit(rx51_soc_exit);
  282. MODULE_AUTHOR("Nokia Corporation");
  283. MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
  284. MODULE_LICENSE("GPL");