rx51.c 13 KB

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