rx51.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * rx51.c -- SoC audio for Nokia RX-51
  3. *
  4. * Copyright (C) 2008 - 2009 Nokia Corporation
  5. *
  6. * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
  7. * Eduardo Valentin <eduardo.valentin@nokia.com>
  8. * Jarkko Nikula <jarkko.nikula@bitmer.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/gpio.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/gpio/consumer.h>
  29. #include <linux/module.h>
  30. #include <sound/core.h>
  31. #include <sound/jack.h>
  32. #include <sound/pcm.h>
  33. #include <sound/soc.h>
  34. #include <linux/platform_data/asoc-ti-mcbsp.h>
  35. #include <asm/mach-types.h>
  36. #include "omap-mcbsp.h"
  37. enum {
  38. RX51_JACK_DISABLED,
  39. RX51_JACK_TVOUT, /* tv-out with stereo output */
  40. RX51_JACK_HP, /* headphone: stereo output, no mic */
  41. RX51_JACK_HS, /* headset: stereo output with mic */
  42. };
  43. struct rx51_audio_pdata {
  44. struct gpio_desc *tvout_selection_gpio;
  45. struct gpio_desc *jack_detection_gpio;
  46. struct gpio_desc *eci_sw_gpio;
  47. struct gpio_desc *speaker_amp_gpio;
  48. };
  49. static int rx51_spk_func;
  50. static int rx51_dmic_func;
  51. static int rx51_jack_func;
  52. static void rx51_ext_control(struct snd_soc_dapm_context *dapm)
  53. {
  54. struct snd_soc_card *card = dapm->card;
  55. struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
  56. int hp = 0, hs = 0, tvout = 0;
  57. switch (rx51_jack_func) {
  58. case RX51_JACK_TVOUT:
  59. tvout = 1;
  60. hp = 1;
  61. break;
  62. case RX51_JACK_HS:
  63. hs = 1;
  64. case RX51_JACK_HP:
  65. hp = 1;
  66. break;
  67. }
  68. snd_soc_dapm_mutex_lock(dapm);
  69. if (rx51_spk_func)
  70. snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
  71. else
  72. snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
  73. if (rx51_dmic_func)
  74. snd_soc_dapm_enable_pin_unlocked(dapm, "DMic");
  75. else
  76. snd_soc_dapm_disable_pin_unlocked(dapm, "DMic");
  77. if (hp)
  78. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  79. else
  80. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  81. if (hs)
  82. snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic");
  83. else
  84. snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic");
  85. gpiod_set_value(pdata->tvout_selection_gpio, tvout);
  86. snd_soc_dapm_sync_unlocked(dapm);
  87. snd_soc_dapm_mutex_unlock(dapm);
  88. }
  89. static int rx51_startup(struct snd_pcm_substream *substream)
  90. {
  91. struct snd_pcm_runtime *runtime = substream->runtime;
  92. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  93. struct snd_soc_card *card = rtd->card;
  94. snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  95. rx51_ext_control(&card->dapm);
  96. return 0;
  97. }
  98. static int rx51_hw_params(struct snd_pcm_substream *substream,
  99. struct snd_pcm_hw_params *params)
  100. {
  101. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  102. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  103. /* Set the codec system clock for DAC and ADC */
  104. return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
  105. SND_SOC_CLOCK_IN);
  106. }
  107. static struct snd_soc_ops rx51_ops = {
  108. .startup = rx51_startup,
  109. .hw_params = rx51_hw_params,
  110. };
  111. static int rx51_get_spk(struct snd_kcontrol *kcontrol,
  112. struct snd_ctl_elem_value *ucontrol)
  113. {
  114. ucontrol->value.enumerated.item[0] = rx51_spk_func;
  115. return 0;
  116. }
  117. static int rx51_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 (rx51_spk_func == ucontrol->value.enumerated.item[0])
  122. return 0;
  123. rx51_spk_func = ucontrol->value.enumerated.item[0];
  124. rx51_ext_control(&card->dapm);
  125. return 1;
  126. }
  127. static int rx51_spk_event(struct snd_soc_dapm_widget *w,
  128. struct snd_kcontrol *k, int event)
  129. {
  130. struct snd_soc_dapm_context *dapm = w->dapm;
  131. struct snd_soc_card *card = dapm->card;
  132. struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
  133. gpiod_set_raw_value_cansleep(pdata->speaker_amp_gpio,
  134. !!SND_SOC_DAPM_EVENT_ON(event));
  135. return 0;
  136. }
  137. static int rx51_get_input(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. ucontrol->value.enumerated.item[0] = rx51_dmic_func;
  141. return 0;
  142. }
  143. static int rx51_set_input(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  147. if (rx51_dmic_func == ucontrol->value.enumerated.item[0])
  148. return 0;
  149. rx51_dmic_func = ucontrol->value.enumerated.item[0];
  150. rx51_ext_control(&card->dapm);
  151. return 1;
  152. }
  153. static int rx51_get_jack(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. ucontrol->value.enumerated.item[0] = rx51_jack_func;
  157. return 0;
  158. }
  159. static int rx51_set_jack(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  163. if (rx51_jack_func == ucontrol->value.enumerated.item[0])
  164. return 0;
  165. rx51_jack_func = ucontrol->value.enumerated.item[0];
  166. rx51_ext_control(&card->dapm);
  167. return 1;
  168. }
  169. static struct snd_soc_jack rx51_av_jack;
  170. static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
  171. {
  172. .name = "avdet-gpio",
  173. .report = SND_JACK_HEADSET,
  174. .invert = 1,
  175. .debounce_time = 200,
  176. },
  177. };
  178. static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
  179. SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
  180. SND_SOC_DAPM_MIC("DMic", NULL),
  181. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  182. SND_SOC_DAPM_MIC("HS Mic", NULL),
  183. SND_SOC_DAPM_LINE("FM Transmitter", NULL),
  184. SND_SOC_DAPM_SPK("Earphone", NULL),
  185. };
  186. static const struct snd_soc_dapm_route audio_map[] = {
  187. {"Ext Spk", NULL, "HPLOUT"},
  188. {"Ext Spk", NULL, "HPROUT"},
  189. {"Ext Spk", NULL, "HPLCOM"},
  190. {"Ext Spk", NULL, "HPRCOM"},
  191. {"FM Transmitter", NULL, "LLOUT"},
  192. {"FM Transmitter", NULL, "RLOUT"},
  193. {"Headphone Jack", NULL, "TPA6130A2 HPLEFT"},
  194. {"Headphone Jack", NULL, "TPA6130A2 HPRIGHT"},
  195. {"TPA6130A2 LEFTIN", NULL, "LLOUT"},
  196. {"TPA6130A2 RIGHTIN", NULL, "RLOUT"},
  197. {"DMic Rate 64", NULL, "DMic"},
  198. {"DMic", NULL, "Mic Bias"},
  199. {"b LINE2R", NULL, "MONO_LOUT"},
  200. {"Earphone", NULL, "b HPLOUT"},
  201. {"LINE1L", NULL, "HS Mic"},
  202. {"HS Mic", NULL, "b Mic Bias"},
  203. };
  204. static const char * const spk_function[] = {"Off", "On"};
  205. static const char * const input_function[] = {"ADC", "Digital Mic"};
  206. static const char * const jack_function[] = {
  207. "Off", "TV-OUT", "Headphone", "Headset"
  208. };
  209. static const struct soc_enum rx51_enum[] = {
  210. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
  211. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
  212. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
  213. };
  214. static const struct snd_kcontrol_new aic34_rx51_controls[] = {
  215. SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
  216. rx51_get_spk, rx51_set_spk),
  217. SOC_ENUM_EXT("Input Select", rx51_enum[1],
  218. rx51_get_input, rx51_set_input),
  219. SOC_ENUM_EXT("Jack Function", rx51_enum[2],
  220. rx51_get_jack, rx51_set_jack),
  221. SOC_DAPM_PIN_SWITCH("FM Transmitter"),
  222. SOC_DAPM_PIN_SWITCH("Earphone"),
  223. };
  224. static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
  225. {
  226. struct snd_soc_card *card = rtd->card;
  227. struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
  228. int err;
  229. snd_soc_limit_volume(card, "TPA6130A2 Headphone Playback Volume", 42);
  230. err = omap_mcbsp_st_add_controls(rtd, 2);
  231. if (err < 0) {
  232. dev_err(card->dev, "Failed to add MCBSP controls\n");
  233. return err;
  234. }
  235. /* AV jack detection */
  236. err = snd_soc_card_jack_new(rtd->card, "AV Jack",
  237. SND_JACK_HEADSET | SND_JACK_VIDEOOUT,
  238. &rx51_av_jack, NULL, 0);
  239. if (err) {
  240. dev_err(card->dev, "Failed to add AV Jack\n");
  241. return err;
  242. }
  243. /* prepare gpio for snd_soc_jack_add_gpios */
  244. rx51_av_jack_gpios[0].gpio = desc_to_gpio(pdata->jack_detection_gpio);
  245. devm_gpiod_put(card->dev, pdata->jack_detection_gpio);
  246. err = snd_soc_jack_add_gpios(&rx51_av_jack,
  247. ARRAY_SIZE(rx51_av_jack_gpios),
  248. rx51_av_jack_gpios);
  249. if (err) {
  250. dev_err(card->dev, "Failed to add GPIOs\n");
  251. return err;
  252. }
  253. return err;
  254. }
  255. static int rx51_card_remove(struct snd_soc_card *card)
  256. {
  257. snd_soc_jack_free_gpios(&rx51_av_jack, ARRAY_SIZE(rx51_av_jack_gpios),
  258. rx51_av_jack_gpios);
  259. return 0;
  260. }
  261. /* Digital audio interface glue - connects codec <--> CPU */
  262. static struct snd_soc_dai_link rx51_dai[] = {
  263. {
  264. .name = "TLV320AIC34",
  265. .stream_name = "AIC34",
  266. .cpu_dai_name = "omap-mcbsp.2",
  267. .codec_dai_name = "tlv320aic3x-hifi",
  268. .platform_name = "omap-mcbsp.2",
  269. .codec_name = "tlv320aic3x-codec.2-0018",
  270. .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
  271. SND_SOC_DAIFMT_CBM_CFM,
  272. .init = rx51_aic34_init,
  273. .ops = &rx51_ops,
  274. },
  275. };
  276. static struct snd_soc_aux_dev rx51_aux_dev[] = {
  277. {
  278. .name = "TLV320AIC34b",
  279. .codec_name = "tlv320aic3x-codec.2-0019",
  280. },
  281. {
  282. .name = "TPA61320A2",
  283. .codec_name = "tpa6130a2.2-0060",
  284. },
  285. };
  286. static struct snd_soc_codec_conf rx51_codec_conf[] = {
  287. {
  288. .dev_name = "tlv320aic3x-codec.2-0019",
  289. .name_prefix = "b",
  290. },
  291. {
  292. .dev_name = "tpa6130a2.2-0060",
  293. .name_prefix = "TPA6130A2",
  294. },
  295. };
  296. /* Audio card */
  297. static struct snd_soc_card rx51_sound_card = {
  298. .name = "RX-51",
  299. .owner = THIS_MODULE,
  300. .remove = rx51_card_remove,
  301. .dai_link = rx51_dai,
  302. .num_links = ARRAY_SIZE(rx51_dai),
  303. .aux_dev = rx51_aux_dev,
  304. .num_aux_devs = ARRAY_SIZE(rx51_aux_dev),
  305. .codec_conf = rx51_codec_conf,
  306. .num_configs = ARRAY_SIZE(rx51_codec_conf),
  307. .fully_routed = true,
  308. .controls = aic34_rx51_controls,
  309. .num_controls = ARRAY_SIZE(aic34_rx51_controls),
  310. .dapm_widgets = aic34_dapm_widgets,
  311. .num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets),
  312. .dapm_routes = audio_map,
  313. .num_dapm_routes = ARRAY_SIZE(audio_map),
  314. };
  315. static int rx51_soc_probe(struct platform_device *pdev)
  316. {
  317. struct rx51_audio_pdata *pdata;
  318. struct device_node *np = pdev->dev.of_node;
  319. struct snd_soc_card *card = &rx51_sound_card;
  320. int err;
  321. if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
  322. return -ENODEV;
  323. card->dev = &pdev->dev;
  324. if (np) {
  325. struct device_node *dai_node;
  326. dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0);
  327. if (!dai_node) {
  328. dev_err(&pdev->dev, "McBSP node is not provided\n");
  329. return -EINVAL;
  330. }
  331. rx51_dai[0].cpu_dai_name = NULL;
  332. rx51_dai[0].platform_name = NULL;
  333. rx51_dai[0].cpu_of_node = dai_node;
  334. rx51_dai[0].platform_of_node = dai_node;
  335. dai_node = of_parse_phandle(np, "nokia,audio-codec", 0);
  336. if (!dai_node) {
  337. dev_err(&pdev->dev, "Codec node is not provided\n");
  338. return -EINVAL;
  339. }
  340. rx51_dai[0].codec_name = NULL;
  341. rx51_dai[0].codec_of_node = dai_node;
  342. dai_node = of_parse_phandle(np, "nokia,audio-codec", 1);
  343. if (!dai_node) {
  344. dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n");
  345. return -EINVAL;
  346. }
  347. rx51_aux_dev[0].codec_name = NULL;
  348. rx51_aux_dev[0].codec_of_node = dai_node;
  349. rx51_codec_conf[0].dev_name = NULL;
  350. rx51_codec_conf[0].of_node = dai_node;
  351. dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0);
  352. if (!dai_node) {
  353. dev_err(&pdev->dev, "Headphone amplifier node is not provided\n");
  354. return -EINVAL;
  355. }
  356. rx51_aux_dev[1].codec_name = NULL;
  357. rx51_aux_dev[1].codec_of_node = dai_node;
  358. rx51_codec_conf[1].dev_name = NULL;
  359. rx51_codec_conf[1].of_node = dai_node;
  360. }
  361. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  362. if (pdata == NULL) {
  363. dev_err(card->dev, "failed to create private data\n");
  364. return -ENOMEM;
  365. }
  366. snd_soc_card_set_drvdata(card, pdata);
  367. pdata->tvout_selection_gpio = devm_gpiod_get(card->dev,
  368. "tvout-selection",
  369. GPIOD_OUT_LOW);
  370. if (IS_ERR(pdata->tvout_selection_gpio)) {
  371. dev_err(card->dev, "could not get tvout selection gpio\n");
  372. return PTR_ERR(pdata->tvout_selection_gpio);
  373. }
  374. pdata->jack_detection_gpio = devm_gpiod_get(card->dev,
  375. "jack-detection",
  376. GPIOD_ASIS);
  377. if (IS_ERR(pdata->jack_detection_gpio)) {
  378. dev_err(card->dev, "could not get jack detection gpio\n");
  379. return PTR_ERR(pdata->jack_detection_gpio);
  380. }
  381. pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch",
  382. GPIOD_OUT_HIGH);
  383. if (IS_ERR(pdata->eci_sw_gpio)) {
  384. dev_err(card->dev, "could not get eci switch gpio\n");
  385. return PTR_ERR(pdata->eci_sw_gpio);
  386. }
  387. pdata->speaker_amp_gpio = devm_gpiod_get(card->dev,
  388. "speaker-amplifier",
  389. GPIOD_OUT_LOW);
  390. if (IS_ERR(pdata->speaker_amp_gpio)) {
  391. dev_err(card->dev, "could not get speaker enable gpio\n");
  392. return PTR_ERR(pdata->speaker_amp_gpio);
  393. }
  394. err = devm_snd_soc_register_card(card->dev, card);
  395. if (err) {
  396. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err);
  397. return err;
  398. }
  399. return 0;
  400. }
  401. #if defined(CONFIG_OF)
  402. static const struct of_device_id rx51_audio_of_match[] = {
  403. { .compatible = "nokia,n900-audio", },
  404. {},
  405. };
  406. MODULE_DEVICE_TABLE(of, rx51_audio_of_match);
  407. #endif
  408. static struct platform_driver rx51_soc_driver = {
  409. .driver = {
  410. .name = "rx51-audio",
  411. .of_match_table = of_match_ptr(rx51_audio_of_match),
  412. },
  413. .probe = rx51_soc_probe,
  414. };
  415. module_platform_driver(rx51_soc_driver);
  416. MODULE_AUTHOR("Nokia Corporation");
  417. MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS("platform:rx51-audio");