n810.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * n810.c -- SoC audio for Nokia N810
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/platform_device.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <asm/mach-types.h>
  30. #include <asm/arch/hardware.h>
  31. #include <asm/arch/gpio.h>
  32. #include <asm/arch/mcbsp.h>
  33. #include "omap-mcbsp.h"
  34. #include "omap-pcm.h"
  35. #include "../codecs/tlv320aic3x.h"
  36. #define RX44_HEADSET_AMP_GPIO 10
  37. #define RX44_SPEAKER_AMP_GPIO 101
  38. static struct clk *sys_clkout2;
  39. static struct clk *sys_clkout2_src;
  40. static struct clk *func96m_clk;
  41. static int n810_spk_func;
  42. static int n810_jack_func;
  43. static void n810_ext_control(struct snd_soc_codec *codec)
  44. {
  45. snd_soc_dapm_set_endpoint(codec, "Ext Spk", n810_spk_func);
  46. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", n810_jack_func);
  47. snd_soc_dapm_sync_endpoints(codec);
  48. }
  49. static int n810_startup(struct snd_pcm_substream *substream)
  50. {
  51. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  52. struct snd_soc_codec *codec = rtd->socdev->codec;
  53. n810_ext_control(codec);
  54. return clk_enable(sys_clkout2);
  55. }
  56. static void n810_shutdown(struct snd_pcm_substream *substream)
  57. {
  58. clk_disable(sys_clkout2);
  59. }
  60. static int n810_hw_params(struct snd_pcm_substream *substream,
  61. struct snd_pcm_hw_params *params)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  64. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  65. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  66. int err;
  67. /* Set codec DAI configuration */
  68. err = codec_dai->dai_ops.set_fmt(codec_dai,
  69. SND_SOC_DAIFMT_I2S |
  70. SND_SOC_DAIFMT_NB_NF |
  71. SND_SOC_DAIFMT_CBM_CFM);
  72. if (err < 0)
  73. return err;
  74. /* Set cpu DAI configuration */
  75. err = cpu_dai->dai_ops.set_fmt(cpu_dai,
  76. SND_SOC_DAIFMT_I2S |
  77. SND_SOC_DAIFMT_NB_NF |
  78. SND_SOC_DAIFMT_CBM_CFM);
  79. if (err < 0)
  80. return err;
  81. /* Set the codec system clock for DAC and ADC */
  82. err = codec_dai->dai_ops.set_sysclk(codec_dai, 0, 12000000,
  83. SND_SOC_CLOCK_IN);
  84. return err;
  85. }
  86. static struct snd_soc_ops n810_ops = {
  87. .startup = n810_startup,
  88. .hw_params = n810_hw_params,
  89. .shutdown = n810_shutdown,
  90. };
  91. static int n810_get_spk(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. ucontrol->value.integer.value[0] = n810_spk_func;
  95. return 0;
  96. }
  97. static int n810_set_spk(struct snd_kcontrol *kcontrol,
  98. struct snd_ctl_elem_value *ucontrol)
  99. {
  100. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  101. if (n810_spk_func == ucontrol->value.integer.value[0])
  102. return 0;
  103. n810_spk_func = ucontrol->value.integer.value[0];
  104. n810_ext_control(codec);
  105. return 1;
  106. }
  107. static int n810_get_jack(struct snd_kcontrol *kcontrol,
  108. struct snd_ctl_elem_value *ucontrol)
  109. {
  110. ucontrol->value.integer.value[0] = n810_jack_func;
  111. return 0;
  112. }
  113. static int n810_set_jack(struct snd_kcontrol *kcontrol,
  114. struct snd_ctl_elem_value *ucontrol)
  115. {
  116. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  117. if (n810_jack_func == ucontrol->value.integer.value[0])
  118. return 0;
  119. n810_jack_func = ucontrol->value.integer.value[0];
  120. n810_ext_control(codec);
  121. return 1;
  122. }
  123. static int n810_spk_event(struct snd_soc_dapm_widget *w,
  124. struct snd_kcontrol *k, int event)
  125. {
  126. if (SND_SOC_DAPM_EVENT_ON(event))
  127. omap_set_gpio_dataout(RX44_SPEAKER_AMP_GPIO, 1);
  128. else
  129. omap_set_gpio_dataout(RX44_SPEAKER_AMP_GPIO, 0);
  130. return 0;
  131. }
  132. static int n810_jack_event(struct snd_soc_dapm_widget *w,
  133. struct snd_kcontrol *k, int event)
  134. {
  135. if (SND_SOC_DAPM_EVENT_ON(event))
  136. omap_set_gpio_dataout(RX44_HEADSET_AMP_GPIO, 1);
  137. else
  138. omap_set_gpio_dataout(RX44_HEADSET_AMP_GPIO, 0);
  139. return 0;
  140. }
  141. static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
  142. SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
  143. SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
  144. };
  145. static const char *audio_map[][3] = {
  146. {"Headphone Jack", NULL, "HPLOUT"},
  147. {"Headphone Jack", NULL, "HPROUT"},
  148. {"Ext Spk", NULL, "LLOUT"},
  149. {"Ext Spk", NULL, "RLOUT"},
  150. };
  151. static const char *spk_function[] = {"Off", "On"};
  152. static const char *jack_function[] = {"Off", "Headphone"};
  153. static const struct soc_enum n810_enum[] = {
  154. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  155. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  156. };
  157. static const struct snd_kcontrol_new aic33_n810_controls[] = {
  158. SOC_ENUM_EXT("Speaker Function", n810_enum[0],
  159. n810_get_spk, n810_set_spk),
  160. SOC_ENUM_EXT("Jack Function", n810_enum[1],
  161. n810_get_jack, n810_set_jack),
  162. };
  163. static int n810_aic33_init(struct snd_soc_codec *codec)
  164. {
  165. int i, err;
  166. /* Not connected */
  167. snd_soc_dapm_set_endpoint(codec, "MONO_LOUT", 0);
  168. snd_soc_dapm_set_endpoint(codec, "HPLCOM", 0);
  169. snd_soc_dapm_set_endpoint(codec, "HPRCOM", 0);
  170. /* Add N810 specific controls */
  171. for (i = 0; i < ARRAY_SIZE(aic33_n810_controls); i++) {
  172. err = snd_ctl_add(codec->card,
  173. snd_soc_cnew(&aic33_n810_controls[i], codec, NULL));
  174. if (err < 0)
  175. return err;
  176. }
  177. /* Add N810 specific widgets */
  178. for (i = 0; i < ARRAY_SIZE(aic33_dapm_widgets); i++)
  179. snd_soc_dapm_new_control(codec, &aic33_dapm_widgets[i]);
  180. /* Set up N810 specific audio path audio_map */
  181. for (i = 0; i < ARRAY_SIZE(audio_map); i++)
  182. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  183. audio_map[i][1], audio_map[i][2]);
  184. snd_soc_dapm_sync_endpoints(codec);
  185. return 0;
  186. }
  187. /* Digital audio interface glue - connects codec <--> CPU */
  188. static struct snd_soc_dai_link n810_dai = {
  189. .name = "TLV320AIC33",
  190. .stream_name = "AIC33",
  191. .cpu_dai = &omap_mcbsp_dai[0],
  192. .codec_dai = &aic3x_dai,
  193. .init = n810_aic33_init,
  194. .ops = &n810_ops,
  195. };
  196. /* Audio machine driver */
  197. static struct snd_soc_machine snd_soc_machine_n810 = {
  198. .name = "N810",
  199. .dai_link = &n810_dai,
  200. .num_links = 1,
  201. };
  202. /* Audio private data */
  203. static struct aic3x_setup_data n810_aic33_setup = {
  204. .i2c_address = 0x18,
  205. };
  206. /* Audio subsystem */
  207. static struct snd_soc_device n810_snd_devdata = {
  208. .machine = &snd_soc_machine_n810,
  209. .platform = &omap_soc_platform,
  210. .codec_dev = &soc_codec_dev_aic3x,
  211. .codec_data = &n810_aic33_setup,
  212. };
  213. static struct platform_device *n810_snd_device;
  214. static int __init n810_soc_init(void)
  215. {
  216. int err;
  217. struct device *dev;
  218. if (!machine_is_nokia_n810())
  219. return -ENODEV;
  220. n810_snd_device = platform_device_alloc("soc-audio", -1);
  221. if (!n810_snd_device)
  222. return -ENOMEM;
  223. platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
  224. n810_snd_devdata.dev = &n810_snd_device->dev;
  225. *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
  226. err = platform_device_add(n810_snd_device);
  227. if (err)
  228. goto err1;
  229. dev = &n810_snd_device->dev;
  230. sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
  231. if (IS_ERR(sys_clkout2_src)) {
  232. dev_err(dev, "Could not get sys_clkout2_src clock\n");
  233. return -ENODEV;
  234. }
  235. sys_clkout2 = clk_get(dev, "sys_clkout2");
  236. if (IS_ERR(sys_clkout2)) {
  237. dev_err(dev, "Could not get sys_clkout2\n");
  238. goto err1;
  239. }
  240. /*
  241. * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
  242. * 96 MHz as its parent in order to get 12 MHz
  243. */
  244. func96m_clk = clk_get(dev, "func_96m_ck");
  245. if (IS_ERR(func96m_clk)) {
  246. dev_err(dev, "Could not get func 96M clock\n");
  247. goto err2;
  248. }
  249. clk_set_parent(sys_clkout2_src, func96m_clk);
  250. clk_set_rate(sys_clkout2, 12000000);
  251. if (omap_request_gpio(RX44_HEADSET_AMP_GPIO) < 0)
  252. BUG();
  253. if (omap_request_gpio(RX44_SPEAKER_AMP_GPIO) < 0)
  254. BUG();
  255. omap_set_gpio_direction(RX44_HEADSET_AMP_GPIO, 0);
  256. omap_set_gpio_direction(RX44_SPEAKER_AMP_GPIO, 0);
  257. return 0;
  258. err2:
  259. clk_put(sys_clkout2);
  260. platform_device_del(n810_snd_device);
  261. err1:
  262. platform_device_put(n810_snd_device);
  263. return err;
  264. }
  265. static void __exit n810_soc_exit(void)
  266. {
  267. platform_device_unregister(n810_snd_device);
  268. }
  269. module_init(n810_soc_init);
  270. module_exit(n810_soc_exit);
  271. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
  272. MODULE_DESCRIPTION("ALSA SoC Nokia N810");
  273. MODULE_LICENSE("GPL");