n810.c 9.9 KB

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