rockchip_max98090.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
  4. *
  5. * Copyright (c) 2014, ROCKCHIP CORPORATION. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/gpio.h>
  12. #include <linux/of_gpio.h>
  13. #include <sound/core.h>
  14. #include <sound/jack.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include "rockchip_i2s.h"
  19. #include "../codecs/ts3a227e.h"
  20. #define DRV_NAME "rockchip-snd-max98090"
  21. static struct snd_soc_jack headset_jack;
  22. /* Headset jack detection DAPM pins */
  23. static struct snd_soc_jack_pin headset_jack_pins[] = {
  24. {
  25. .pin = "Headphone",
  26. .mask = SND_JACK_HEADPHONE,
  27. },
  28. {
  29. .pin = "Headset Mic",
  30. .mask = SND_JACK_MICROPHONE,
  31. },
  32. };
  33. #define RK_MAX98090_WIDGETS \
  34. SND_SOC_DAPM_HP("Headphone", NULL), \
  35. SND_SOC_DAPM_MIC("Headset Mic", NULL), \
  36. SND_SOC_DAPM_MIC("Int Mic", NULL), \
  37. SND_SOC_DAPM_SPK("Speaker", NULL)
  38. #define RK_HDMI_WIDGETS \
  39. SND_SOC_DAPM_LINE("HDMI", NULL)
  40. static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets[] = {
  41. RK_MAX98090_WIDGETS,
  42. };
  43. static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets[] = {
  44. RK_HDMI_WIDGETS,
  45. };
  46. static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets[] = {
  47. RK_MAX98090_WIDGETS,
  48. RK_HDMI_WIDGETS,
  49. };
  50. #define RK_MAX98090_AUDIO_MAP \
  51. {"IN34", NULL, "Headset Mic"}, \
  52. {"Headset Mic", NULL, "MICBIAS"}, \
  53. {"DMICL", NULL, "Int Mic"}, \
  54. {"Headphone", NULL, "HPL"}, \
  55. {"Headphone", NULL, "HPR"}, \
  56. {"Speaker", NULL, "SPKL"}, \
  57. {"Speaker", NULL, "SPKR"}
  58. #define RK_HDMI_AUDIO_MAP \
  59. {"HDMI", NULL, "TX"}
  60. static const struct snd_soc_dapm_route rk_max98090_audio_map[] = {
  61. RK_MAX98090_AUDIO_MAP,
  62. };
  63. static const struct snd_soc_dapm_route rk_hdmi_audio_map[] = {
  64. RK_HDMI_AUDIO_MAP,
  65. };
  66. static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map[] = {
  67. RK_MAX98090_AUDIO_MAP,
  68. RK_HDMI_AUDIO_MAP,
  69. };
  70. #define RK_MAX98090_CONTROLS \
  71. SOC_DAPM_PIN_SWITCH("Headphone"), \
  72. SOC_DAPM_PIN_SWITCH("Headset Mic"), \
  73. SOC_DAPM_PIN_SWITCH("Int Mic"), \
  74. SOC_DAPM_PIN_SWITCH("Speaker")
  75. #define RK_HDMI_CONTROLS \
  76. SOC_DAPM_PIN_SWITCH("HDMI")
  77. static const struct snd_kcontrol_new rk_max98090_controls[] = {
  78. RK_MAX98090_CONTROLS,
  79. };
  80. static const struct snd_kcontrol_new rk_hdmi_controls[] = {
  81. RK_HDMI_CONTROLS,
  82. };
  83. static const struct snd_kcontrol_new rk_max98090_hdmi_controls[] = {
  84. RK_MAX98090_CONTROLS,
  85. RK_HDMI_CONTROLS,
  86. };
  87. static int rk_jack_event(struct notifier_block *nb, unsigned long event,
  88. void *data)
  89. {
  90. struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
  91. struct snd_soc_dapm_context *dapm = &jack->card->dapm;
  92. if (event & SND_JACK_MICROPHONE) {
  93. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  94. snd_soc_dapm_force_enable_pin(dapm, "SHDN");
  95. } else {
  96. snd_soc_dapm_disable_pin(dapm, "MICBIAS");
  97. snd_soc_dapm_disable_pin(dapm, "SHDN");
  98. }
  99. snd_soc_dapm_sync(dapm);
  100. return 0;
  101. }
  102. static struct notifier_block rk_jack_nb = {
  103. .notifier_call = rk_jack_event,
  104. };
  105. static int rk_init(struct snd_soc_pcm_runtime *runtime)
  106. {
  107. /*
  108. * The jack has already been created in the rk_98090_headset_init()
  109. * function.
  110. */
  111. snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb);
  112. return 0;
  113. }
  114. static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params)
  116. {
  117. int ret = 0;
  118. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  119. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  120. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  121. int mclk;
  122. switch (params_rate(params)) {
  123. case 8000:
  124. case 16000:
  125. case 24000:
  126. case 32000:
  127. case 48000:
  128. case 64000:
  129. case 96000:
  130. mclk = 12288000;
  131. break;
  132. case 11025:
  133. case 22050:
  134. case 44100:
  135. case 88200:
  136. mclk = 11289600;
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  142. SND_SOC_CLOCK_OUT);
  143. if (ret) {
  144. dev_err(cpu_dai->dev, "Can't set cpu dai clock %d\n", ret);
  145. return ret;
  146. }
  147. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  148. SND_SOC_CLOCK_IN);
  149. /* HDMI codec dai does not need to set sysclk. */
  150. if (!strcmp(rtd->dai_link->name, "HDMI"))
  151. return 0;
  152. if (ret) {
  153. dev_err(codec_dai->dev, "Can't set codec dai clock %d\n", ret);
  154. return ret;
  155. }
  156. return ret;
  157. }
  158. static int rk_aif1_startup(struct snd_pcm_substream *substream)
  159. {
  160. /*
  161. * Set period size to 240 because pl330 has issue
  162. * dealing with larger period in stress testing.
  163. */
  164. return snd_pcm_hw_constraint_minmax(substream->runtime,
  165. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 240, 240);
  166. }
  167. static const struct snd_soc_ops rk_aif1_ops = {
  168. .hw_params = rk_aif1_hw_params,
  169. .startup = rk_aif1_startup,
  170. };
  171. SND_SOC_DAILINK_DEFS(analog,
  172. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  173. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
  174. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  175. SND_SOC_DAILINK_DEFS(hdmi,
  176. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  177. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
  178. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  179. enum {
  180. DAILINK_MAX98090,
  181. DAILINK_HDMI,
  182. };
  183. static struct snd_soc_jack rk_hdmi_jack;
  184. static int rk_hdmi_init(struct snd_soc_pcm_runtime *runtime)
  185. {
  186. struct snd_soc_card *card = runtime->card;
  187. struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
  188. int ret;
  189. /* enable jack detection */
  190. ret = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
  191. &rk_hdmi_jack, NULL, 0);
  192. if (ret) {
  193. dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
  194. return ret;
  195. }
  196. return snd_soc_component_set_jack(component, &rk_hdmi_jack, NULL);
  197. }
  198. /* max98090 dai_link */
  199. static struct snd_soc_dai_link rk_max98090_dailinks[] = {
  200. {
  201. .name = "max98090",
  202. .stream_name = "Analog",
  203. .init = rk_init,
  204. .ops = &rk_aif1_ops,
  205. /* set max98090 as slave */
  206. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  207. SND_SOC_DAIFMT_CBS_CFS,
  208. SND_SOC_DAILINK_REG(analog),
  209. },
  210. };
  211. /* HDMI codec dai_link */
  212. static struct snd_soc_dai_link rk_hdmi_dailinks[] = {
  213. {
  214. .name = "HDMI",
  215. .stream_name = "HDMI",
  216. .init = rk_hdmi_init,
  217. .ops = &rk_aif1_ops,
  218. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  219. SND_SOC_DAIFMT_CBS_CFS,
  220. SND_SOC_DAILINK_REG(hdmi),
  221. }
  222. };
  223. /* max98090 and HDMI codec dai_link */
  224. static struct snd_soc_dai_link rk_max98090_hdmi_dailinks[] = {
  225. [DAILINK_MAX98090] = {
  226. .name = "max98090",
  227. .stream_name = "Analog",
  228. .init = rk_init,
  229. .ops = &rk_aif1_ops,
  230. /* set max98090 as slave */
  231. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  232. SND_SOC_DAIFMT_CBS_CFS,
  233. SND_SOC_DAILINK_REG(analog),
  234. },
  235. [DAILINK_HDMI] = {
  236. .name = "HDMI",
  237. .stream_name = "HDMI",
  238. .init = rk_hdmi_init,
  239. .ops = &rk_aif1_ops,
  240. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  241. SND_SOC_DAIFMT_CBS_CFS,
  242. SND_SOC_DAILINK_REG(hdmi),
  243. }
  244. };
  245. static int rk_98090_headset_init(struct snd_soc_component *component);
  246. static struct snd_soc_aux_dev rk_98090_headset_dev = {
  247. .dlc = COMP_EMPTY(),
  248. .init = rk_98090_headset_init,
  249. };
  250. static struct snd_soc_card rockchip_max98090_card = {
  251. .name = "ROCKCHIP-I2S",
  252. .owner = THIS_MODULE,
  253. .dai_link = rk_max98090_dailinks,
  254. .num_links = ARRAY_SIZE(rk_max98090_dailinks),
  255. .aux_dev = &rk_98090_headset_dev,
  256. .num_aux_devs = 1,
  257. .dapm_widgets = rk_max98090_dapm_widgets,
  258. .num_dapm_widgets = ARRAY_SIZE(rk_max98090_dapm_widgets),
  259. .dapm_routes = rk_max98090_audio_map,
  260. .num_dapm_routes = ARRAY_SIZE(rk_max98090_audio_map),
  261. .controls = rk_max98090_controls,
  262. .num_controls = ARRAY_SIZE(rk_max98090_controls),
  263. };
  264. static struct snd_soc_card rockchip_hdmi_card = {
  265. .name = "ROCKCHIP-HDMI",
  266. .owner = THIS_MODULE,
  267. .dai_link = rk_hdmi_dailinks,
  268. .num_links = ARRAY_SIZE(rk_hdmi_dailinks),
  269. .dapm_widgets = rk_hdmi_dapm_widgets,
  270. .num_dapm_widgets = ARRAY_SIZE(rk_hdmi_dapm_widgets),
  271. .dapm_routes = rk_hdmi_audio_map,
  272. .num_dapm_routes = ARRAY_SIZE(rk_hdmi_audio_map),
  273. .controls = rk_hdmi_controls,
  274. .num_controls = ARRAY_SIZE(rk_hdmi_controls),
  275. };
  276. static struct snd_soc_card rockchip_max98090_hdmi_card = {
  277. .name = "ROCKCHIP-MAX98090-HDMI",
  278. .owner = THIS_MODULE,
  279. .dai_link = rk_max98090_hdmi_dailinks,
  280. .num_links = ARRAY_SIZE(rk_max98090_hdmi_dailinks),
  281. .aux_dev = &rk_98090_headset_dev,
  282. .num_aux_devs = 1,
  283. .dapm_widgets = rk_max98090_hdmi_dapm_widgets,
  284. .num_dapm_widgets = ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets),
  285. .dapm_routes = rk_max98090_hdmi_audio_map,
  286. .num_dapm_routes = ARRAY_SIZE(rk_max98090_hdmi_audio_map),
  287. .controls = rk_max98090_hdmi_controls,
  288. .num_controls = ARRAY_SIZE(rk_max98090_hdmi_controls),
  289. };
  290. static int rk_98090_headset_init(struct snd_soc_component *component)
  291. {
  292. int ret;
  293. /* Enable Headset and 4 Buttons Jack detection */
  294. ret = snd_soc_card_jack_new(component->card, "Headset Jack",
  295. SND_JACK_HEADSET |
  296. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  297. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  298. &headset_jack,
  299. headset_jack_pins,
  300. ARRAY_SIZE(headset_jack_pins));
  301. if (ret)
  302. return ret;
  303. ret = ts3a227e_enable_jack_detect(component, &headset_jack);
  304. return ret;
  305. }
  306. static int rk_parse_headset_from_of(struct device *dev, struct device_node *np)
  307. {
  308. rk_98090_headset_dev.dlc.of_node = of_parse_phandle(
  309. np, "rockchip,headset-codec", 0);
  310. if (!rk_98090_headset_dev.dlc.of_node) {
  311. dev_err(dev,
  312. "Property 'rockchip,headset-codec' missing/invalid\n");
  313. return -EINVAL;
  314. }
  315. return 0;
  316. }
  317. static int snd_rk_mc_probe(struct platform_device *pdev)
  318. {
  319. int ret = 0;
  320. struct snd_soc_card *card;
  321. struct device *dev = &pdev->dev;
  322. struct device_node *np = pdev->dev.of_node;
  323. struct device_node *np_cpu;
  324. struct device_node *np_audio, *np_hdmi;
  325. /* Parse DTS for I2S controller. */
  326. np_cpu = of_parse_phandle(np, "rockchip,i2s-controller", 0);
  327. if (!np_cpu) {
  328. dev_err(&pdev->dev,
  329. "Property 'rockchip,i2s-controller missing or invalid\n");
  330. return -EINVAL;
  331. }
  332. /*
  333. * Find the card to use based on the presences of audio codec
  334. * and hdmi codec in device property. Set their of_node accordingly.
  335. */
  336. np_audio = of_parse_phandle(np, "rockchip,audio-codec", 0);
  337. np_hdmi = of_parse_phandle(np, "rockchip,hdmi-codec", 0);
  338. if (np_audio && np_hdmi) {
  339. card = &rockchip_max98090_hdmi_card;
  340. card->dai_link[DAILINK_MAX98090].codecs->of_node = np_audio;
  341. card->dai_link[DAILINK_HDMI].codecs->of_node = np_hdmi;
  342. card->dai_link[DAILINK_MAX98090].cpus->of_node = np_cpu;
  343. card->dai_link[DAILINK_MAX98090].platforms->of_node = np_cpu;
  344. card->dai_link[DAILINK_HDMI].cpus->of_node = np_cpu;
  345. card->dai_link[DAILINK_HDMI].platforms->of_node = np_cpu;
  346. } else if (np_audio) {
  347. card = &rockchip_max98090_card;
  348. card->dai_link[0].codecs->of_node = np_audio;
  349. card->dai_link[0].cpus->of_node = np_cpu;
  350. card->dai_link[0].platforms->of_node = np_cpu;
  351. } else if (np_hdmi) {
  352. card = &rockchip_hdmi_card;
  353. card->dai_link[0].codecs->of_node = np_hdmi;
  354. card->dai_link[0].cpus->of_node = np_cpu;
  355. card->dai_link[0].platforms->of_node = np_cpu;
  356. } else {
  357. dev_err(dev, "At least one of codecs should be specified\n");
  358. return -EINVAL;
  359. }
  360. card->dev = dev;
  361. /* Parse headset detection codec. */
  362. if (np_audio) {
  363. ret = rk_parse_headset_from_of(dev, np);
  364. if (ret)
  365. return ret;
  366. }
  367. /* Parse card name. */
  368. ret = snd_soc_of_parse_card_name(card, "rockchip,model");
  369. if (ret) {
  370. dev_err(&pdev->dev,
  371. "Soc parse card name failed %d\n", ret);
  372. return ret;
  373. }
  374. /* register the soc card */
  375. ret = devm_snd_soc_register_card(&pdev->dev, card);
  376. if (ret) {
  377. dev_err(&pdev->dev,
  378. "Soc register card failed %d\n", ret);
  379. return ret;
  380. }
  381. return ret;
  382. }
  383. static const struct of_device_id rockchip_max98090_of_match[] = {
  384. { .compatible = "rockchip,rockchip-audio-max98090", },
  385. {},
  386. };
  387. MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match);
  388. static struct platform_driver snd_rk_mc_driver = {
  389. .probe = snd_rk_mc_probe,
  390. .driver = {
  391. .name = DRV_NAME,
  392. .pm = &snd_soc_pm_ops,
  393. .of_match_table = rockchip_max98090_of_match,
  394. },
  395. };
  396. module_platform_driver(snd_rk_mc_driver);
  397. MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
  398. MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
  399. MODULE_LICENSE("GPL v2");
  400. MODULE_ALIAS("platform:" DRV_NAME);