rx51.c 13 KB

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