rx51.c 12 KB

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