rx51.c 7.3 KB

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