rx51.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/pcm.h>
  30. #include <sound/soc.h>
  31. #include <sound/soc-dapm.h>
  32. #include <asm/mach-types.h>
  33. #include "omap-mcbsp.h"
  34. #include "omap-pcm.h"
  35. #include "../codecs/tlv320aic3x.h"
  36. #define RX51_TVOUT_SEL_GPIO 40
  37. /*
  38. * REVISIT: TWL4030 GPIO base in RX-51. Now statically defined to 192. This
  39. * gpio is reserved in arch/arm/mach-omap2/board-rx51-peripherals.c
  40. */
  41. #define RX51_SPEAKER_AMP_TWL_GPIO (192 + 7)
  42. enum {
  43. RX51_JACK_DISABLED,
  44. RX51_JACK_TVOUT, /* tv-out */
  45. };
  46. static int rx51_spk_func;
  47. static int rx51_dmic_func;
  48. static int rx51_jack_func;
  49. static void rx51_ext_control(struct snd_soc_codec *codec)
  50. {
  51. if (rx51_spk_func)
  52. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  53. else
  54. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  55. if (rx51_dmic_func)
  56. snd_soc_dapm_enable_pin(codec, "DMic");
  57. else
  58. snd_soc_dapm_disable_pin(codec, "DMic");
  59. gpio_set_value(RX51_TVOUT_SEL_GPIO,
  60. rx51_jack_func == RX51_JACK_TVOUT);
  61. snd_soc_dapm_sync(codec);
  62. }
  63. static int rx51_startup(struct snd_pcm_substream *substream)
  64. {
  65. struct snd_pcm_runtime *runtime = substream->runtime;
  66. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  67. struct snd_soc_codec *codec = rtd->socdev->card->codec;
  68. snd_pcm_hw_constraint_minmax(runtime,
  69. SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
  70. rx51_ext_control(codec);
  71. return 0;
  72. }
  73. static int rx51_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params)
  75. {
  76. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  77. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  78. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  79. int err;
  80. /* Set codec DAI configuration */
  81. err = snd_soc_dai_set_fmt(codec_dai,
  82. SND_SOC_DAIFMT_DSP_A |
  83. SND_SOC_DAIFMT_IB_NF |
  84. SND_SOC_DAIFMT_CBM_CFM);
  85. if (err < 0)
  86. return err;
  87. /* Set cpu DAI configuration */
  88. err = snd_soc_dai_set_fmt(cpu_dai,
  89. SND_SOC_DAIFMT_DSP_A |
  90. SND_SOC_DAIFMT_IB_NF |
  91. SND_SOC_DAIFMT_CBM_CFM);
  92. if (err < 0)
  93. return err;
  94. /* Set the codec system clock for DAC and ADC */
  95. return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
  96. SND_SOC_CLOCK_IN);
  97. }
  98. static struct snd_soc_ops rx51_ops = {
  99. .startup = rx51_startup,
  100. .hw_params = rx51_hw_params,
  101. };
  102. static int rx51_get_spk(struct snd_kcontrol *kcontrol,
  103. struct snd_ctl_elem_value *ucontrol)
  104. {
  105. ucontrol->value.integer.value[0] = rx51_spk_func;
  106. return 0;
  107. }
  108. static int rx51_set_spk(struct snd_kcontrol *kcontrol,
  109. struct snd_ctl_elem_value *ucontrol)
  110. {
  111. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  112. if (rx51_spk_func == ucontrol->value.integer.value[0])
  113. return 0;
  114. rx51_spk_func = ucontrol->value.integer.value[0];
  115. rx51_ext_control(codec);
  116. return 1;
  117. }
  118. static int rx51_spk_event(struct snd_soc_dapm_widget *w,
  119. struct snd_kcontrol *k, int event)
  120. {
  121. if (SND_SOC_DAPM_EVENT_ON(event))
  122. gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 1);
  123. else
  124. gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 0);
  125. return 0;
  126. }
  127. static int rx51_get_input(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_value *ucontrol)
  129. {
  130. ucontrol->value.integer.value[0] = rx51_dmic_func;
  131. return 0;
  132. }
  133. static int rx51_set_input(struct snd_kcontrol *kcontrol,
  134. struct snd_ctl_elem_value *ucontrol)
  135. {
  136. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  137. if (rx51_dmic_func == ucontrol->value.integer.value[0])
  138. return 0;
  139. rx51_dmic_func = ucontrol->value.integer.value[0];
  140. rx51_ext_control(codec);
  141. return 1;
  142. }
  143. static int rx51_get_jack(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. ucontrol->value.integer.value[0] = rx51_jack_func;
  147. return 0;
  148. }
  149. static int rx51_set_jack(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  153. if (rx51_jack_func == ucontrol->value.integer.value[0])
  154. return 0;
  155. rx51_jack_func = ucontrol->value.integer.value[0];
  156. rx51_ext_control(codec);
  157. return 1;
  158. }
  159. static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
  160. SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
  161. SND_SOC_DAPM_MIC("DMic", NULL),
  162. };
  163. static const struct snd_soc_dapm_route audio_map[] = {
  164. {"Ext Spk", NULL, "HPLOUT"},
  165. {"Ext Spk", NULL, "HPROUT"},
  166. {"DMic Rate 64", NULL, "Mic Bias 2V"},
  167. {"Mic Bias 2V", NULL, "DMic"},
  168. };
  169. static const char *spk_function[] = {"Off", "On"};
  170. static const char *input_function[] = {"ADC", "Digital Mic"};
  171. static const char *jack_function[] = {"Off", "TV-OUT"};
  172. static const struct soc_enum rx51_enum[] = {
  173. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  174. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  175. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  176. };
  177. static const struct snd_kcontrol_new aic34_rx51_controls[] = {
  178. SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
  179. rx51_get_spk, rx51_set_spk),
  180. SOC_ENUM_EXT("Input Select", rx51_enum[1],
  181. rx51_get_input, rx51_set_input),
  182. SOC_ENUM_EXT("Jack Function", rx51_enum[2],
  183. rx51_get_jack, rx51_set_jack),
  184. };
  185. static int rx51_aic34_init(struct snd_soc_codec *codec)
  186. {
  187. int err;
  188. /* Set up NC codec pins */
  189. snd_soc_dapm_nc_pin(codec, "MIC3L");
  190. snd_soc_dapm_nc_pin(codec, "MIC3R");
  191. snd_soc_dapm_nc_pin(codec, "LINE1R");
  192. /* Add RX-51 specific controls */
  193. err = snd_soc_add_controls(codec, aic34_rx51_controls,
  194. ARRAY_SIZE(aic34_rx51_controls));
  195. if (err < 0)
  196. return err;
  197. /* Add RX-51 specific widgets */
  198. snd_soc_dapm_new_controls(codec, aic34_dapm_widgets,
  199. ARRAY_SIZE(aic34_dapm_widgets));
  200. /* Set up RX-51 specific audio path audio_map */
  201. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  202. snd_soc_dapm_sync(codec);
  203. return 0;
  204. }
  205. /* Digital audio interface glue - connects codec <--> CPU */
  206. static struct snd_soc_dai_link rx51_dai[] = {
  207. {
  208. .name = "TLV320AIC34",
  209. .stream_name = "AIC34",
  210. .cpu_dai = &omap_mcbsp_dai[0],
  211. .codec_dai = &aic3x_dai,
  212. .init = rx51_aic34_init,
  213. .ops = &rx51_ops,
  214. },
  215. };
  216. /* Audio private data */
  217. static struct aic3x_setup_data rx51_aic34_setup = {
  218. .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
  219. .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
  220. };
  221. /* Audio card */
  222. static struct snd_soc_card rx51_sound_card = {
  223. .name = "RX-51",
  224. .dai_link = rx51_dai,
  225. .num_links = ARRAY_SIZE(rx51_dai),
  226. .platform = &omap_soc_platform,
  227. };
  228. /* Audio subsystem */
  229. static struct snd_soc_device rx51_snd_devdata = {
  230. .card = &rx51_sound_card,
  231. .codec_dev = &soc_codec_dev_aic3x,
  232. .codec_data = &rx51_aic34_setup,
  233. };
  234. static struct platform_device *rx51_snd_device;
  235. static int __init rx51_soc_init(void)
  236. {
  237. int err;
  238. if (!machine_is_nokia_rx51())
  239. return -ENODEV;
  240. err = gpio_request(RX51_TVOUT_SEL_GPIO, "tvout_sel");
  241. if (err)
  242. goto err_gpio_tvout_sel;
  243. gpio_direction_output(RX51_TVOUT_SEL_GPIO, 0);
  244. rx51_snd_device = platform_device_alloc("soc-audio", -1);
  245. if (!rx51_snd_device) {
  246. err = -ENOMEM;
  247. goto err1;
  248. }
  249. platform_set_drvdata(rx51_snd_device, &rx51_snd_devdata);
  250. rx51_snd_devdata.dev = &rx51_snd_device->dev;
  251. *(unsigned int *)rx51_dai[0].cpu_dai->private_data = 1; /* McBSP2 */
  252. err = platform_device_add(rx51_snd_device);
  253. if (err)
  254. goto err2;
  255. return 0;
  256. err2:
  257. platform_device_put(rx51_snd_device);
  258. err1:
  259. gpio_free(RX51_TVOUT_SEL_GPIO);
  260. err_gpio_tvout_sel:
  261. return err;
  262. }
  263. static void __exit rx51_soc_exit(void)
  264. {
  265. platform_device_unregister(rx51_snd_device);
  266. gpio_free(RX51_TVOUT_SEL_GPIO);
  267. }
  268. module_init(rx51_soc_init);
  269. module_exit(rx51_soc_exit);
  270. MODULE_AUTHOR("Nokia Corporation");
  271. MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
  272. MODULE_LICENSE("GPL");