n810.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <asm/mach-types.h>
  30. #include <mach/hardware.h>
  31. #include <linux/gpio.h>
  32. #include <plat/mcbsp.h>
  33. #include "omap-mcbsp.h"
  34. #include "omap-pcm.h"
  35. #define N810_HEADSET_AMP_GPIO 10
  36. #define N810_SPEAKER_AMP_GPIO 101
  37. enum {
  38. N810_JACK_DISABLED,
  39. N810_JACK_HP,
  40. N810_JACK_HS,
  41. N810_JACK_MIC,
  42. };
  43. static struct clk *sys_clkout2;
  44. static struct clk *sys_clkout2_src;
  45. static struct clk *func96m_clk;
  46. static int n810_spk_func;
  47. static int n810_jack_func;
  48. static int n810_dmic_func;
  49. static void n810_ext_control(struct snd_soc_codec *codec)
  50. {
  51. struct snd_soc_dapm_context *dapm = &codec->dapm;
  52. int hp = 0, line1l = 0;
  53. switch (n810_jack_func) {
  54. case N810_JACK_HS:
  55. line1l = 1;
  56. case N810_JACK_HP:
  57. hp = 1;
  58. break;
  59. case N810_JACK_MIC:
  60. line1l = 1;
  61. break;
  62. }
  63. if (n810_spk_func)
  64. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  65. else
  66. snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  67. if (hp)
  68. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  69. else
  70. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  71. if (line1l)
  72. snd_soc_dapm_enable_pin(dapm, "LINE1L");
  73. else
  74. snd_soc_dapm_disable_pin(dapm, "LINE1L");
  75. if (n810_dmic_func)
  76. snd_soc_dapm_enable_pin(dapm, "DMic");
  77. else
  78. snd_soc_dapm_disable_pin(dapm, "DMic");
  79. snd_soc_dapm_sync(dapm);
  80. }
  81. static int n810_startup(struct snd_pcm_substream *substream)
  82. {
  83. struct snd_pcm_runtime *runtime = substream->runtime;
  84. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  85. struct snd_soc_codec *codec = rtd->codec;
  86. snd_pcm_hw_constraint_minmax(runtime,
  87. SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
  88. n810_ext_control(codec);
  89. return clk_enable(sys_clkout2);
  90. }
  91. static void n810_shutdown(struct snd_pcm_substream *substream)
  92. {
  93. clk_disable(sys_clkout2);
  94. }
  95. static int n810_hw_params(struct snd_pcm_substream *substream,
  96. struct snd_pcm_hw_params *params)
  97. {
  98. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  99. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  101. int err;
  102. /* Set codec DAI configuration */
  103. err = snd_soc_dai_set_fmt(codec_dai,
  104. SND_SOC_DAIFMT_I2S |
  105. SND_SOC_DAIFMT_NB_NF |
  106. SND_SOC_DAIFMT_CBM_CFM);
  107. if (err < 0)
  108. return err;
  109. /* Set cpu DAI configuration */
  110. err = snd_soc_dai_set_fmt(cpu_dai,
  111. SND_SOC_DAIFMT_I2S |
  112. SND_SOC_DAIFMT_NB_NF |
  113. SND_SOC_DAIFMT_CBM_CFM);
  114. if (err < 0)
  115. return err;
  116. /* Set the codec system clock for DAC and ADC */
  117. err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
  118. SND_SOC_CLOCK_IN);
  119. return err;
  120. }
  121. static struct snd_soc_ops n810_ops = {
  122. .startup = n810_startup,
  123. .hw_params = n810_hw_params,
  124. .shutdown = n810_shutdown,
  125. };
  126. static int n810_get_spk(struct snd_kcontrol *kcontrol,
  127. struct snd_ctl_elem_value *ucontrol)
  128. {
  129. ucontrol->value.integer.value[0] = n810_spk_func;
  130. return 0;
  131. }
  132. static int n810_set_spk(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  136. if (n810_spk_func == ucontrol->value.integer.value[0])
  137. return 0;
  138. n810_spk_func = ucontrol->value.integer.value[0];
  139. n810_ext_control(codec);
  140. return 1;
  141. }
  142. static int n810_get_jack(struct snd_kcontrol *kcontrol,
  143. struct snd_ctl_elem_value *ucontrol)
  144. {
  145. ucontrol->value.integer.value[0] = n810_jack_func;
  146. return 0;
  147. }
  148. static int n810_set_jack(struct snd_kcontrol *kcontrol,
  149. struct snd_ctl_elem_value *ucontrol)
  150. {
  151. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  152. if (n810_jack_func == ucontrol->value.integer.value[0])
  153. return 0;
  154. n810_jack_func = ucontrol->value.integer.value[0];
  155. n810_ext_control(codec);
  156. return 1;
  157. }
  158. static int n810_get_input(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol)
  160. {
  161. ucontrol->value.integer.value[0] = n810_dmic_func;
  162. return 0;
  163. }
  164. static int n810_set_input(struct snd_kcontrol *kcontrol,
  165. struct snd_ctl_elem_value *ucontrol)
  166. {
  167. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  168. if (n810_dmic_func == ucontrol->value.integer.value[0])
  169. return 0;
  170. n810_dmic_func = ucontrol->value.integer.value[0];
  171. n810_ext_control(codec);
  172. return 1;
  173. }
  174. static int n810_spk_event(struct snd_soc_dapm_widget *w,
  175. struct snd_kcontrol *k, int event)
  176. {
  177. if (SND_SOC_DAPM_EVENT_ON(event))
  178. gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
  179. else
  180. gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
  181. return 0;
  182. }
  183. static int n810_jack_event(struct snd_soc_dapm_widget *w,
  184. struct snd_kcontrol *k, int event)
  185. {
  186. if (SND_SOC_DAPM_EVENT_ON(event))
  187. gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
  188. else
  189. gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
  190. return 0;
  191. }
  192. static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
  193. SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
  194. SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
  195. SND_SOC_DAPM_MIC("DMic", NULL),
  196. };
  197. static const struct snd_soc_dapm_route audio_map[] = {
  198. {"Headphone Jack", NULL, "HPLOUT"},
  199. {"Headphone Jack", NULL, "HPROUT"},
  200. {"Ext Spk", NULL, "LLOUT"},
  201. {"Ext Spk", NULL, "RLOUT"},
  202. {"DMic Rate 64", NULL, "Mic Bias 2V"},
  203. {"Mic Bias 2V", NULL, "DMic"},
  204. };
  205. static const char *spk_function[] = {"Off", "On"};
  206. static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
  207. static const char *input_function[] = {"ADC", "Digital Mic"};
  208. static const struct soc_enum n810_enum[] = {
  209. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  210. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  211. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  212. };
  213. static const struct snd_kcontrol_new aic33_n810_controls[] = {
  214. SOC_ENUM_EXT("Speaker Function", n810_enum[0],
  215. n810_get_spk, n810_set_spk),
  216. SOC_ENUM_EXT("Jack Function", n810_enum[1],
  217. n810_get_jack, n810_set_jack),
  218. SOC_ENUM_EXT("Input Select", n810_enum[2],
  219. n810_get_input, n810_set_input),
  220. };
  221. static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
  222. {
  223. struct snd_soc_codec *codec = rtd->codec;
  224. struct snd_soc_dapm_context *dapm = &codec->dapm;
  225. int err;
  226. /* Not connected */
  227. snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
  228. snd_soc_dapm_nc_pin(dapm, "HPLCOM");
  229. snd_soc_dapm_nc_pin(dapm, "HPRCOM");
  230. snd_soc_dapm_nc_pin(dapm, "MIC3L");
  231. snd_soc_dapm_nc_pin(dapm, "MIC3R");
  232. snd_soc_dapm_nc_pin(dapm, "LINE1R");
  233. snd_soc_dapm_nc_pin(dapm, "LINE2L");
  234. snd_soc_dapm_nc_pin(dapm, "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(dapm, aic33_dapm_widgets,
  242. ARRAY_SIZE(aic33_dapm_widgets));
  243. /* Set up N810 specific audio path audio_map */
  244. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  245. snd_soc_dapm_sync(dapm);
  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");