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_codec *codec)
  51. {
  52. struct snd_soc_dapm_context *dapm = &codec->dapm;
  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(dapm, "Ext Spk");
  66. else
  67. snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  68. if (hp)
  69. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  70. else
  71. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  72. if (line1l)
  73. snd_soc_dapm_enable_pin(dapm, "LINE1L");
  74. else
  75. snd_soc_dapm_disable_pin(dapm, "LINE1L");
  76. if (n810_dmic_func)
  77. snd_soc_dapm_enable_pin(dapm, "DMic");
  78. else
  79. snd_soc_dapm_disable_pin(dapm, "DMic");
  80. snd_soc_dapm_sync(dapm);
  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. int err;
  102. /* Set the codec system clock for DAC and ADC */
  103. err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
  104. SND_SOC_CLOCK_IN);
  105. return err;
  106. }
  107. static struct snd_soc_ops n810_ops = {
  108. .startup = n810_startup,
  109. .hw_params = n810_hw_params,
  110. .shutdown = n810_shutdown,
  111. };
  112. static int n810_get_spk(struct snd_kcontrol *kcontrol,
  113. struct snd_ctl_elem_value *ucontrol)
  114. {
  115. ucontrol->value.integer.value[0] = n810_spk_func;
  116. return 0;
  117. }
  118. static int n810_set_spk(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_value *ucontrol)
  120. {
  121. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  122. if (n810_spk_func == ucontrol->value.integer.value[0])
  123. return 0;
  124. n810_spk_func = ucontrol->value.integer.value[0];
  125. n810_ext_control(codec);
  126. return 1;
  127. }
  128. static int n810_get_jack(struct snd_kcontrol *kcontrol,
  129. struct snd_ctl_elem_value *ucontrol)
  130. {
  131. ucontrol->value.integer.value[0] = n810_jack_func;
  132. return 0;
  133. }
  134. static int n810_set_jack(struct snd_kcontrol *kcontrol,
  135. struct snd_ctl_elem_value *ucontrol)
  136. {
  137. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  138. if (n810_jack_func == ucontrol->value.integer.value[0])
  139. return 0;
  140. n810_jack_func = ucontrol->value.integer.value[0];
  141. n810_ext_control(codec);
  142. return 1;
  143. }
  144. static int n810_get_input(struct snd_kcontrol *kcontrol,
  145. struct snd_ctl_elem_value *ucontrol)
  146. {
  147. ucontrol->value.integer.value[0] = n810_dmic_func;
  148. return 0;
  149. }
  150. static int n810_set_input(struct snd_kcontrol *kcontrol,
  151. struct snd_ctl_elem_value *ucontrol)
  152. {
  153. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  154. if (n810_dmic_func == ucontrol->value.integer.value[0])
  155. return 0;
  156. n810_dmic_func = ucontrol->value.integer.value[0];
  157. n810_ext_control(codec);
  158. return 1;
  159. }
  160. static int n810_spk_event(struct snd_soc_dapm_widget *w,
  161. struct snd_kcontrol *k, int event)
  162. {
  163. if (SND_SOC_DAPM_EVENT_ON(event))
  164. gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
  165. else
  166. gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
  167. return 0;
  168. }
  169. static int n810_jack_event(struct snd_soc_dapm_widget *w,
  170. struct snd_kcontrol *k, int event)
  171. {
  172. if (SND_SOC_DAPM_EVENT_ON(event))
  173. gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
  174. else
  175. gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
  176. return 0;
  177. }
  178. static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
  179. SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
  180. SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
  181. SND_SOC_DAPM_MIC("DMic", NULL),
  182. };
  183. static const struct snd_soc_dapm_route audio_map[] = {
  184. {"Headphone Jack", NULL, "HPLOUT"},
  185. {"Headphone Jack", NULL, "HPROUT"},
  186. {"Ext Spk", NULL, "LLOUT"},
  187. {"Ext Spk", NULL, "RLOUT"},
  188. {"DMic Rate 64", NULL, "Mic Bias 2V"},
  189. {"Mic Bias 2V", NULL, "DMic"},
  190. };
  191. static const char *spk_function[] = {"Off", "On"};
  192. static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
  193. static const char *input_function[] = {"ADC", "Digital Mic"};
  194. static const struct soc_enum n810_enum[] = {
  195. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  196. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  197. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  198. };
  199. static const struct snd_kcontrol_new aic33_n810_controls[] = {
  200. SOC_ENUM_EXT("Speaker Function", n810_enum[0],
  201. n810_get_spk, n810_set_spk),
  202. SOC_ENUM_EXT("Jack Function", n810_enum[1],
  203. n810_get_jack, n810_set_jack),
  204. SOC_ENUM_EXT("Input Select", n810_enum[2],
  205. n810_get_input, n810_set_input),
  206. };
  207. static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
  208. {
  209. struct snd_soc_codec *codec = rtd->codec;
  210. struct snd_soc_dapm_context *dapm = &codec->dapm;
  211. /* Not connected */
  212. snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
  213. snd_soc_dapm_nc_pin(dapm, "HPLCOM");
  214. snd_soc_dapm_nc_pin(dapm, "HPRCOM");
  215. snd_soc_dapm_nc_pin(dapm, "MIC3L");
  216. snd_soc_dapm_nc_pin(dapm, "MIC3R");
  217. snd_soc_dapm_nc_pin(dapm, "LINE1R");
  218. snd_soc_dapm_nc_pin(dapm, "LINE2L");
  219. snd_soc_dapm_nc_pin(dapm, "LINE2R");
  220. return 0;
  221. }
  222. /* Digital audio interface glue - connects codec <--> CPU */
  223. static struct snd_soc_dai_link n810_dai = {
  224. .name = "TLV320AIC33",
  225. .stream_name = "AIC33",
  226. .cpu_dai_name = "omap-mcbsp-dai.1",
  227. .platform_name = "omap-pcm-audio",
  228. .codec_name = "tlv320aic3x-codec.2-0018",
  229. .codec_dai_name = "tlv320aic3x-hifi",
  230. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  231. SND_SOC_DAIFMT_CBM_CFM,
  232. .init = n810_aic33_init,
  233. .ops = &n810_ops,
  234. };
  235. /* Audio machine driver */
  236. static struct snd_soc_card snd_soc_n810 = {
  237. .name = "N810",
  238. .owner = THIS_MODULE,
  239. .dai_link = &n810_dai,
  240. .num_links = 1,
  241. .controls = aic33_n810_controls,
  242. .num_controls = ARRAY_SIZE(aic33_n810_controls),
  243. .dapm_widgets = aic33_dapm_widgets,
  244. .num_dapm_widgets = ARRAY_SIZE(aic33_dapm_widgets),
  245. .dapm_routes = audio_map,
  246. .num_dapm_routes = ARRAY_SIZE(audio_map),
  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, &snd_soc_n810);
  259. err = platform_device_add(n810_snd_device);
  260. if (err)
  261. goto err1;
  262. dev = &n810_snd_device->dev;
  263. sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
  264. if (IS_ERR(sys_clkout2_src)) {
  265. dev_err(dev, "Could not get sys_clkout2_src clock\n");
  266. err = PTR_ERR(sys_clkout2_src);
  267. goto err2;
  268. }
  269. sys_clkout2 = clk_get(dev, "sys_clkout2");
  270. if (IS_ERR(sys_clkout2)) {
  271. dev_err(dev, "Could not get sys_clkout2\n");
  272. err = PTR_ERR(sys_clkout2);
  273. goto err3;
  274. }
  275. /*
  276. * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
  277. * 96 MHz as its parent in order to get 12 MHz
  278. */
  279. func96m_clk = clk_get(dev, "func_96m_ck");
  280. if (IS_ERR(func96m_clk)) {
  281. dev_err(dev, "Could not get func 96M clock\n");
  282. err = PTR_ERR(func96m_clk);
  283. goto err4;
  284. }
  285. clk_set_parent(sys_clkout2_src, func96m_clk);
  286. clk_set_rate(sys_clkout2, 12000000);
  287. BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
  288. (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
  289. gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
  290. gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
  291. return 0;
  292. err4:
  293. clk_put(sys_clkout2);
  294. err3:
  295. clk_put(sys_clkout2_src);
  296. err2:
  297. platform_device_del(n810_snd_device);
  298. err1:
  299. platform_device_put(n810_snd_device);
  300. return err;
  301. }
  302. static void __exit n810_soc_exit(void)
  303. {
  304. gpio_free(N810_SPEAKER_AMP_GPIO);
  305. gpio_free(N810_HEADSET_AMP_GPIO);
  306. clk_put(sys_clkout2_src);
  307. clk_put(sys_clkout2);
  308. clk_put(func96m_clk);
  309. platform_device_unregister(n810_snd_device);
  310. }
  311. module_init(n810_soc_init);
  312. module_exit(n810_soc_exit);
  313. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  314. MODULE_DESCRIPTION("ALSA SoC Nokia N810");
  315. MODULE_LICENSE("GPL");