n810.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * n810.c -- SoC audio for Nokia N810
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Jarkko Nikula <jarkko.nikula@bitmer.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 <linux/module.h>
  33. #include <plat/mcbsp.h>
  34. #include "omap-mcbsp.h"
  35. #include "omap-pcm.h"
  36. #define N810_HEADSET_AMP_GPIO 10
  37. #define N810_SPEAKER_AMP_GPIO 101
  38. enum {
  39. N810_JACK_DISABLED,
  40. N810_JACK_HP,
  41. N810_JACK_HS,
  42. N810_JACK_MIC,
  43. };
  44. static struct clk *sys_clkout2;
  45. static struct clk *sys_clkout2_src;
  46. static struct clk *func96m_clk;
  47. static int n810_spk_func;
  48. static int n810_jack_func;
  49. static int n810_dmic_func;
  50. static void n810_ext_control(struct snd_soc_dapm_context *dapm)
  51. {
  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->dapm);
  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. int err;
  101. /* Set the codec system clock for DAC and ADC */
  102. err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
  103. SND_SOC_CLOCK_IN);
  104. return err;
  105. }
  106. static struct snd_soc_ops n810_ops = {
  107. .startup = n810_startup,
  108. .hw_params = n810_hw_params,
  109. .shutdown = n810_shutdown,
  110. };
  111. static int n810_get_spk(struct snd_kcontrol *kcontrol,
  112. struct snd_ctl_elem_value *ucontrol)
  113. {
  114. ucontrol->value.integer.value[0] = n810_spk_func;
  115. return 0;
  116. }
  117. static int n810_set_spk(struct snd_kcontrol *kcontrol,
  118. struct snd_ctl_elem_value *ucontrol)
  119. {
  120. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  121. if (n810_spk_func == ucontrol->value.integer.value[0])
  122. return 0;
  123. n810_spk_func = ucontrol->value.integer.value[0];
  124. n810_ext_control(&card->dapm);
  125. return 1;
  126. }
  127. static int n810_get_jack(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_value *ucontrol)
  129. {
  130. ucontrol->value.integer.value[0] = n810_jack_func;
  131. return 0;
  132. }
  133. static int n810_set_jack(struct snd_kcontrol *kcontrol,
  134. struct snd_ctl_elem_value *ucontrol)
  135. {
  136. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  137. if (n810_jack_func == ucontrol->value.integer.value[0])
  138. return 0;
  139. n810_jack_func = ucontrol->value.integer.value[0];
  140. n810_ext_control(&card->dapm);
  141. return 1;
  142. }
  143. static int n810_get_input(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. ucontrol->value.integer.value[0] = n810_dmic_func;
  147. return 0;
  148. }
  149. static int n810_set_input(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  153. if (n810_dmic_func == ucontrol->value.integer.value[0])
  154. return 0;
  155. n810_dmic_func = ucontrol->value.integer.value[0];
  156. n810_ext_control(&card->dapm);
  157. return 1;
  158. }
  159. static int n810_spk_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_SPEAKER_AMP_GPIO, 1);
  164. else
  165. gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
  166. return 0;
  167. }
  168. static int n810_jack_event(struct snd_soc_dapm_widget *w,
  169. struct snd_kcontrol *k, int event)
  170. {
  171. if (SND_SOC_DAPM_EVENT_ON(event))
  172. gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
  173. else
  174. gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
  175. return 0;
  176. }
  177. static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
  178. SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
  179. SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
  180. SND_SOC_DAPM_MIC("DMic", NULL),
  181. };
  182. static const struct snd_soc_dapm_route audio_map[] = {
  183. {"Headphone Jack", NULL, "HPLOUT"},
  184. {"Headphone Jack", NULL, "HPROUT"},
  185. {"Ext Spk", NULL, "LLOUT"},
  186. {"Ext Spk", NULL, "RLOUT"},
  187. {"DMic Rate 64", NULL, "Mic Bias 2V"},
  188. {"Mic Bias 2V", NULL, "DMic"},
  189. };
  190. static const char *spk_function[] = {"Off", "On"};
  191. static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
  192. static const char *input_function[] = {"ADC", "Digital Mic"};
  193. static const struct soc_enum n810_enum[] = {
  194. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  195. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  196. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  197. };
  198. static const struct snd_kcontrol_new aic33_n810_controls[] = {
  199. SOC_ENUM_EXT("Speaker Function", n810_enum[0],
  200. n810_get_spk, n810_set_spk),
  201. SOC_ENUM_EXT("Jack Function", n810_enum[1],
  202. n810_get_jack, n810_set_jack),
  203. SOC_ENUM_EXT("Input Select", n810_enum[2],
  204. n810_get_input, n810_set_input),
  205. };
  206. static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
  207. {
  208. struct snd_soc_codec *codec = rtd->codec;
  209. struct snd_soc_dapm_context *dapm = &codec->dapm;
  210. /* Not connected */
  211. snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
  212. snd_soc_dapm_nc_pin(dapm, "HPLCOM");
  213. snd_soc_dapm_nc_pin(dapm, "HPRCOM");
  214. snd_soc_dapm_nc_pin(dapm, "MIC3L");
  215. snd_soc_dapm_nc_pin(dapm, "MIC3R");
  216. snd_soc_dapm_nc_pin(dapm, "LINE1R");
  217. snd_soc_dapm_nc_pin(dapm, "LINE2L");
  218. snd_soc_dapm_nc_pin(dapm, "LINE2R");
  219. return 0;
  220. }
  221. /* Digital audio interface glue - connects codec <--> CPU */
  222. static struct snd_soc_dai_link n810_dai = {
  223. .name = "TLV320AIC33",
  224. .stream_name = "AIC33",
  225. .cpu_dai_name = "omap-mcbsp.2",
  226. .platform_name = "omap-pcm-audio",
  227. .codec_name = "tlv320aic3x-codec.2-0018",
  228. .codec_dai_name = "tlv320aic3x-hifi",
  229. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  230. SND_SOC_DAIFMT_CBM_CFM,
  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. .owner = THIS_MODULE,
  238. .dai_link = &n810_dai,
  239. .num_links = 1,
  240. .controls = aic33_n810_controls,
  241. .num_controls = ARRAY_SIZE(aic33_n810_controls),
  242. .dapm_widgets = aic33_dapm_widgets,
  243. .num_dapm_widgets = ARRAY_SIZE(aic33_dapm_widgets),
  244. .dapm_routes = audio_map,
  245. .num_dapm_routes = ARRAY_SIZE(audio_map),
  246. };
  247. static struct platform_device *n810_snd_device;
  248. static int __init n810_soc_init(void)
  249. {
  250. int err;
  251. struct device *dev;
  252. if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
  253. return -ENODEV;
  254. n810_snd_device = platform_device_alloc("soc-audio", -1);
  255. if (!n810_snd_device)
  256. return -ENOMEM;
  257. platform_set_drvdata(n810_snd_device, &snd_soc_n810);
  258. err = platform_device_add(n810_snd_device);
  259. if (err)
  260. goto err1;
  261. dev = &n810_snd_device->dev;
  262. sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
  263. if (IS_ERR(sys_clkout2_src)) {
  264. dev_err(dev, "Could not get sys_clkout2_src clock\n");
  265. err = PTR_ERR(sys_clkout2_src);
  266. goto err2;
  267. }
  268. sys_clkout2 = clk_get(dev, "sys_clkout2");
  269. if (IS_ERR(sys_clkout2)) {
  270. dev_err(dev, "Could not get sys_clkout2\n");
  271. err = PTR_ERR(sys_clkout2);
  272. goto err3;
  273. }
  274. /*
  275. * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
  276. * 96 MHz as its parent in order to get 12 MHz
  277. */
  278. func96m_clk = clk_get(dev, "func_96m_ck");
  279. if (IS_ERR(func96m_clk)) {
  280. dev_err(dev, "Could not get func 96M clock\n");
  281. err = PTR_ERR(func96m_clk);
  282. goto err4;
  283. }
  284. clk_set_parent(sys_clkout2_src, func96m_clk);
  285. clk_set_rate(sys_clkout2, 12000000);
  286. BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
  287. (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
  288. gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
  289. gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
  290. return 0;
  291. err4:
  292. clk_put(sys_clkout2);
  293. err3:
  294. clk_put(sys_clkout2_src);
  295. err2:
  296. platform_device_del(n810_snd_device);
  297. err1:
  298. platform_device_put(n810_snd_device);
  299. return err;
  300. }
  301. static void __exit n810_soc_exit(void)
  302. {
  303. gpio_free(N810_SPEAKER_AMP_GPIO);
  304. gpio_free(N810_HEADSET_AMP_GPIO);
  305. clk_put(sys_clkout2_src);
  306. clk_put(sys_clkout2);
  307. clk_put(func96m_clk);
  308. platform_device_unregister(n810_snd_device);
  309. }
  310. module_init(n810_soc_init);
  311. module_exit(n810_soc_exit);
  312. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  313. MODULE_DESCRIPTION("ALSA SoC Nokia N810");
  314. MODULE_LICENSE("GPL");