n810.c 9.4 KB

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