n810.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <mach/hardware.h>
  31. #include <linux/gpio.h>
  32. #include <mach/mcbsp.h>
  33. #include "omap-mcbsp.h"
  34. #include "omap-pcm.h"
  35. #include "../codecs/tlv320aic3x.h"
  36. #define N810_HEADSET_AMP_GPIO 10
  37. #define N810_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 int n810_dmic_func;
  44. static void n810_ext_control(struct snd_soc_codec *codec)
  45. {
  46. if (n810_spk_func)
  47. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  48. else
  49. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  50. if (n810_jack_func)
  51. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  52. else
  53. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  54. if (n810_dmic_func)
  55. snd_soc_dapm_enable_pin(codec, "DMic");
  56. else
  57. snd_soc_dapm_disable_pin(codec, "DMic");
  58. snd_soc_dapm_sync(codec);
  59. }
  60. static int n810_startup(struct snd_pcm_substream *substream)
  61. {
  62. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  63. struct snd_soc_codec *codec = rtd->socdev->codec;
  64. n810_ext_control(codec);
  65. return clk_enable(sys_clkout2);
  66. }
  67. static void n810_shutdown(struct snd_pcm_substream *substream)
  68. {
  69. clk_disable(sys_clkout2);
  70. }
  71. static int n810_hw_params(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params)
  73. {
  74. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  75. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  76. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  77. int err;
  78. /* Set codec DAI configuration */
  79. err = snd_soc_dai_set_fmt(codec_dai,
  80. SND_SOC_DAIFMT_I2S |
  81. SND_SOC_DAIFMT_NB_NF |
  82. SND_SOC_DAIFMT_CBM_CFM);
  83. if (err < 0)
  84. return err;
  85. /* Set cpu DAI configuration */
  86. err = snd_soc_dai_set_fmt(cpu_dai,
  87. SND_SOC_DAIFMT_I2S |
  88. SND_SOC_DAIFMT_NB_NF |
  89. SND_SOC_DAIFMT_CBM_CFM);
  90. if (err < 0)
  91. return err;
  92. /* Set the codec system clock for DAC and ADC */
  93. err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
  94. SND_SOC_CLOCK_IN);
  95. return err;
  96. }
  97. static struct snd_soc_ops n810_ops = {
  98. .startup = n810_startup,
  99. .hw_params = n810_hw_params,
  100. .shutdown = n810_shutdown,
  101. };
  102. static int n810_get_spk(struct snd_kcontrol *kcontrol,
  103. struct snd_ctl_elem_value *ucontrol)
  104. {
  105. ucontrol->value.integer.value[0] = n810_spk_func;
  106. return 0;
  107. }
  108. static int n810_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 (n810_spk_func == ucontrol->value.integer.value[0])
  113. return 0;
  114. n810_spk_func = ucontrol->value.integer.value[0];
  115. n810_ext_control(codec);
  116. return 1;
  117. }
  118. static int n810_get_jack(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_value *ucontrol)
  120. {
  121. ucontrol->value.integer.value[0] = n810_jack_func;
  122. return 0;
  123. }
  124. static int n810_set_jack(struct snd_kcontrol *kcontrol,
  125. struct snd_ctl_elem_value *ucontrol)
  126. {
  127. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  128. if (n810_jack_func == ucontrol->value.integer.value[0])
  129. return 0;
  130. n810_jack_func = ucontrol->value.integer.value[0];
  131. n810_ext_control(codec);
  132. return 1;
  133. }
  134. static int n810_get_input(struct snd_kcontrol *kcontrol,
  135. struct snd_ctl_elem_value *ucontrol)
  136. {
  137. ucontrol->value.integer.value[0] = n810_dmic_func;
  138. return 0;
  139. }
  140. static int n810_set_input(struct snd_kcontrol *kcontrol,
  141. struct snd_ctl_elem_value *ucontrol)
  142. {
  143. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  144. if (n810_dmic_func == ucontrol->value.integer.value[0])
  145. return 0;
  146. n810_dmic_func = ucontrol->value.integer.value[0];
  147. n810_ext_control(codec);
  148. return 1;
  149. }
  150. static int n810_spk_event(struct snd_soc_dapm_widget *w,
  151. struct snd_kcontrol *k, int event)
  152. {
  153. if (SND_SOC_DAPM_EVENT_ON(event))
  154. gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
  155. else
  156. gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
  157. return 0;
  158. }
  159. static int n810_jack_event(struct snd_soc_dapm_widget *w,
  160. struct snd_kcontrol *k, int event)
  161. {
  162. if (SND_SOC_DAPM_EVENT_ON(event))
  163. gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
  164. else
  165. gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
  166. return 0;
  167. }
  168. static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
  169. SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
  170. SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
  171. SND_SOC_DAPM_MIC("DMic", NULL),
  172. };
  173. static const struct snd_soc_dapm_route audio_map[] = {
  174. {"Headphone Jack", NULL, "HPLOUT"},
  175. {"Headphone Jack", NULL, "HPROUT"},
  176. {"Ext Spk", NULL, "LLOUT"},
  177. {"Ext Spk", NULL, "RLOUT"},
  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 *jack_function[] = {"Off", "Headphone"};
  183. static const char *input_function[] = {"ADC", "Digital Mic"};
  184. static const struct soc_enum n810_enum[] = {
  185. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  186. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  187. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  188. };
  189. static const struct snd_kcontrol_new aic33_n810_controls[] = {
  190. SOC_ENUM_EXT("Speaker Function", n810_enum[0],
  191. n810_get_spk, n810_set_spk),
  192. SOC_ENUM_EXT("Jack Function", n810_enum[1],
  193. n810_get_jack, n810_set_jack),
  194. SOC_ENUM_EXT("Input Select", n810_enum[2],
  195. n810_get_input, n810_set_input),
  196. };
  197. static int n810_aic33_init(struct snd_soc_codec *codec)
  198. {
  199. int i, err;
  200. /* Not connected */
  201. snd_soc_dapm_nc_pin(codec, "MONO_LOUT");
  202. snd_soc_dapm_nc_pin(codec, "HPLCOM");
  203. snd_soc_dapm_nc_pin(codec, "HPRCOM");
  204. /* Add N810 specific controls */
  205. for (i = 0; i < ARRAY_SIZE(aic33_n810_controls); i++) {
  206. err = snd_ctl_add(codec->card,
  207. snd_soc_cnew(&aic33_n810_controls[i], codec, NULL));
  208. if (err < 0)
  209. return err;
  210. }
  211. /* Add N810 specific widgets */
  212. snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
  213. ARRAY_SIZE(aic33_dapm_widgets));
  214. /* Set up N810 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. return 0;
  218. }
  219. /* Digital audio interface glue - connects codec <--> CPU */
  220. static struct snd_soc_dai_link n810_dai = {
  221. .name = "TLV320AIC33",
  222. .stream_name = "AIC33",
  223. .cpu_dai = &omap_mcbsp_dai[0],
  224. .codec_dai = &aic3x_dai,
  225. .init = n810_aic33_init,
  226. .ops = &n810_ops,
  227. };
  228. /* Audio machine driver */
  229. static struct snd_soc_machine snd_soc_machine_n810 = {
  230. .name = "N810",
  231. .dai_link = &n810_dai,
  232. .num_links = 1,
  233. };
  234. /* Audio private data */
  235. static struct aic3x_setup_data n810_aic33_setup = {
  236. .i2c_bus = 2,
  237. .i2c_address = 0x18,
  238. .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
  239. .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
  240. };
  241. /* Audio subsystem */
  242. static struct snd_soc_device n810_snd_devdata = {
  243. .machine = &snd_soc_machine_n810,
  244. .platform = &omap_soc_platform,
  245. .codec_dev = &soc_codec_dev_aic3x,
  246. .codec_data = &n810_aic33_setup,
  247. };
  248. static struct platform_device *n810_snd_device;
  249. static int __init n810_soc_init(void)
  250. {
  251. int err;
  252. struct device *dev;
  253. if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
  254. return -ENODEV;
  255. n810_snd_device = platform_device_alloc("soc-audio", -1);
  256. if (!n810_snd_device)
  257. return -ENOMEM;
  258. platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
  259. n810_snd_devdata.dev = &n810_snd_device->dev;
  260. *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
  261. err = platform_device_add(n810_snd_device);
  262. if (err)
  263. goto err1;
  264. dev = &n810_snd_device->dev;
  265. sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
  266. if (IS_ERR(sys_clkout2_src)) {
  267. dev_err(dev, "Could not get sys_clkout2_src clock\n");
  268. err = PTR_ERR(sys_clkout2_src);
  269. goto err2;
  270. }
  271. sys_clkout2 = clk_get(dev, "sys_clkout2");
  272. if (IS_ERR(sys_clkout2)) {
  273. dev_err(dev, "Could not get sys_clkout2\n");
  274. err = PTR_ERR(sys_clkout2);
  275. goto err3;
  276. }
  277. /*
  278. * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
  279. * 96 MHz as its parent in order to get 12 MHz
  280. */
  281. func96m_clk = clk_get(dev, "func_96m_ck");
  282. if (IS_ERR(func96m_clk)) {
  283. dev_err(dev, "Could not get func 96M clock\n");
  284. err = PTR_ERR(func96m_clk);
  285. goto err4;
  286. }
  287. clk_set_parent(sys_clkout2_src, func96m_clk);
  288. clk_set_rate(sys_clkout2, 12000000);
  289. if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
  290. BUG();
  291. if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
  292. BUG();
  293. gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
  294. gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
  295. return 0;
  296. err4:
  297. clk_put(sys_clkout2);
  298. err3:
  299. clk_put(sys_clkout2_src);
  300. err2:
  301. platform_device_del(n810_snd_device);
  302. err1:
  303. platform_device_put(n810_snd_device);
  304. return err;
  305. }
  306. static void __exit n810_soc_exit(void)
  307. {
  308. gpio_free(N810_SPEAKER_AMP_GPIO);
  309. gpio_free(N810_HEADSET_AMP_GPIO);
  310. clk_put(sys_clkout2_src);
  311. clk_put(sys_clkout2);
  312. clk_put(func96m_clk);
  313. platform_device_unregister(n810_snd_device);
  314. }
  315. module_init(n810_soc_init);
  316. module_exit(n810_soc_exit);
  317. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
  318. MODULE_DESCRIPTION("ALSA SoC Nokia N810");
  319. MODULE_LICENSE("GPL");