n810.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <mach/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->socdev->card->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->dai->codec_dai;
  101. struct snd_soc_dai *cpu_dai = rtd->dai->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_codec *codec)
  223. {
  224. int err;
  225. /* Not connected */
  226. snd_soc_dapm_nc_pin(codec, "MONO_LOUT");
  227. snd_soc_dapm_nc_pin(codec, "HPLCOM");
  228. snd_soc_dapm_nc_pin(codec, "HPRCOM");
  229. snd_soc_dapm_nc_pin(codec, "MIC3L");
  230. snd_soc_dapm_nc_pin(codec, "MIC3R");
  231. snd_soc_dapm_nc_pin(codec, "LINE1R");
  232. snd_soc_dapm_nc_pin(codec, "LINE2L");
  233. snd_soc_dapm_nc_pin(codec, "LINE2R");
  234. /* Add N810 specific controls */
  235. err = snd_soc_add_controls(codec, aic33_n810_controls,
  236. ARRAY_SIZE(aic33_n810_controls));
  237. if (err < 0)
  238. return err;
  239. /* Add N810 specific widgets */
  240. snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
  241. ARRAY_SIZE(aic33_dapm_widgets));
  242. /* Set up N810 specific audio path audio_map */
  243. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  244. snd_soc_dapm_sync(codec);
  245. return 0;
  246. }
  247. /* Digital audio interface glue - connects codec <--> CPU */
  248. static struct snd_soc_dai_link n810_dai = {
  249. .name = "TLV320AIC33",
  250. .stream_name = "AIC33",
  251. .cpu_dai = &omap_mcbsp_dai[0],
  252. .codec_dai = &aic3x_dai,
  253. .init = n810_aic33_init,
  254. .ops = &n810_ops,
  255. };
  256. /* Audio machine driver */
  257. static struct snd_soc_card snd_soc_n810 = {
  258. .name = "N810",
  259. .platform = &omap_soc_platform,
  260. .dai_link = &n810_dai,
  261. .num_links = 1,
  262. };
  263. /* Audio private data */
  264. static struct aic3x_setup_data n810_aic33_setup = {
  265. .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
  266. .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
  267. };
  268. /* Audio subsystem */
  269. static struct snd_soc_device n810_snd_devdata = {
  270. .card = &snd_soc_n810,
  271. .codec_dev = &soc_codec_dev_aic3x,
  272. .codec_data = &n810_aic33_setup,
  273. };
  274. static struct platform_device *n810_snd_device;
  275. /* temporary i2c device creation until this can be moved into the machine
  276. * support file.
  277. */
  278. static struct i2c_board_info i2c_device[] = {
  279. { I2C_BOARD_INFO("tlv320aic3x", 0x1b), }
  280. };
  281. static int __init n810_soc_init(void)
  282. {
  283. int err;
  284. struct device *dev;
  285. if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
  286. return -ENODEV;
  287. i2c_register_board_info(1, i2c_device, ARRAY_SIZE(i2c_device));
  288. n810_snd_device = platform_device_alloc("soc-audio", -1);
  289. if (!n810_snd_device)
  290. return -ENOMEM;
  291. platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
  292. n810_snd_devdata.dev = &n810_snd_device->dev;
  293. *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
  294. err = platform_device_add(n810_snd_device);
  295. if (err)
  296. goto err1;
  297. dev = &n810_snd_device->dev;
  298. sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
  299. if (IS_ERR(sys_clkout2_src)) {
  300. dev_err(dev, "Could not get sys_clkout2_src clock\n");
  301. err = PTR_ERR(sys_clkout2_src);
  302. goto err2;
  303. }
  304. sys_clkout2 = clk_get(dev, "sys_clkout2");
  305. if (IS_ERR(sys_clkout2)) {
  306. dev_err(dev, "Could not get sys_clkout2\n");
  307. err = PTR_ERR(sys_clkout2);
  308. goto err3;
  309. }
  310. /*
  311. * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
  312. * 96 MHz as its parent in order to get 12 MHz
  313. */
  314. func96m_clk = clk_get(dev, "func_96m_ck");
  315. if (IS_ERR(func96m_clk)) {
  316. dev_err(dev, "Could not get func 96M clock\n");
  317. err = PTR_ERR(func96m_clk);
  318. goto err4;
  319. }
  320. clk_set_parent(sys_clkout2_src, func96m_clk);
  321. clk_set_rate(sys_clkout2, 12000000);
  322. BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
  323. (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
  324. gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
  325. gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
  326. return 0;
  327. err4:
  328. clk_put(sys_clkout2);
  329. err3:
  330. clk_put(sys_clkout2_src);
  331. err2:
  332. platform_device_del(n810_snd_device);
  333. err1:
  334. platform_device_put(n810_snd_device);
  335. return err;
  336. }
  337. static void __exit n810_soc_exit(void)
  338. {
  339. gpio_free(N810_SPEAKER_AMP_GPIO);
  340. gpio_free(N810_HEADSET_AMP_GPIO);
  341. clk_put(sys_clkout2_src);
  342. clk_put(sys_clkout2);
  343. clk_put(func96m_clk);
  344. platform_device_unregister(n810_snd_device);
  345. }
  346. module_init(n810_soc_init);
  347. module_exit(n810_soc_exit);
  348. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  349. MODULE_DESCRIPTION("ALSA SoC Nokia N810");
  350. MODULE_LICENSE("GPL");