n810.c 10.0 KB

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