rx51.c 9.0 KB

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