midas_wm1811.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Midas audio support
  4. //
  5. // Copyright (C) 2018 Simon Shields <[email protected]>
  6. // Copyright (C) 2020 Samsung Electronics Co., Ltd.
  7. #include <linux/clk.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/mfd/wm8994/registers.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <sound/jack.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include "i2s.h"
  19. #include "../codecs/wm8994.h"
  20. /*
  21. * The MCLK1 clock source is XCLKOUT with its mux set to the external fixed rate
  22. * oscillator (XXTI).
  23. */
  24. #define MCLK1_RATE 24000000U
  25. #define MCLK2_RATE 32768U
  26. #define DEFAULT_FLL1_RATE 11289600U
  27. struct midas_priv {
  28. struct regulator *reg_mic_bias;
  29. struct regulator *reg_submic_bias;
  30. struct gpio_desc *gpio_fm_sel;
  31. struct gpio_desc *gpio_lineout_sel;
  32. unsigned int fll1_rate;
  33. struct snd_soc_jack headset_jack;
  34. };
  35. static int midas_start_fll1(struct snd_soc_pcm_runtime *rtd, unsigned int rate)
  36. {
  37. struct snd_soc_card *card = rtd->card;
  38. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  39. struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
  40. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  41. int ret;
  42. if (!rate)
  43. rate = priv->fll1_rate;
  44. /*
  45. * If no new rate is requested, set FLL1 to a sane default for jack
  46. * detection.
  47. */
  48. if (!rate)
  49. rate = DEFAULT_FLL1_RATE;
  50. if (rate != priv->fll1_rate && priv->fll1_rate) {
  51. /* while reconfiguring, switch to MCLK2 for SYSCLK */
  52. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
  53. MCLK2_RATE, SND_SOC_CLOCK_IN);
  54. if (ret < 0) {
  55. dev_err(card->dev, "Unable to switch to MCLK2: %d\n", ret);
  56. return ret;
  57. }
  58. }
  59. ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
  60. MCLK1_RATE, rate);
  61. if (ret < 0) {
  62. dev_err(card->dev, "Failed to set FLL1 rate: %d\n", ret);
  63. return ret;
  64. }
  65. priv->fll1_rate = rate;
  66. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_FLL1,
  67. priv->fll1_rate, SND_SOC_CLOCK_IN);
  68. if (ret < 0) {
  69. dev_err(card->dev, "Failed to set SYSCLK source: %d\n", ret);
  70. return ret;
  71. }
  72. ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK, 0,
  73. SAMSUNG_I2S_OPCLK_PCLK);
  74. if (ret < 0) {
  75. dev_err(card->dev, "Failed to set OPCLK source: %d\n", ret);
  76. return ret;
  77. }
  78. return 0;
  79. }
  80. static int midas_stop_fll1(struct snd_soc_pcm_runtime *rtd)
  81. {
  82. struct snd_soc_card *card = rtd->card;
  83. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  84. struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
  85. int ret;
  86. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
  87. MCLK2_RATE, SND_SOC_CLOCK_IN);
  88. if (ret < 0) {
  89. dev_err(card->dev, "Unable to switch to MCLK2: %d\n", ret);
  90. return ret;
  91. }
  92. ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 0, 0, 0);
  93. if (ret < 0) {
  94. dev_err(card->dev, "Unable to stop FLL1: %d\n", ret);
  95. return ret;
  96. }
  97. priv->fll1_rate = 0;
  98. return 0;
  99. }
  100. static int midas_aif1_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. unsigned int pll_out;
  105. /* AIF1CLK should be at least 3MHz for "optimal performance" */
  106. if (params_rate(params) == 8000 || params_rate(params) == 11025)
  107. pll_out = params_rate(params) * 512;
  108. else
  109. pll_out = params_rate(params) * 256;
  110. return midas_start_fll1(rtd, pll_out);
  111. }
  112. static const struct snd_soc_ops midas_aif1_ops = {
  113. .hw_params = midas_aif1_hw_params,
  114. };
  115. /*
  116. * We only have a single external speaker, so mix stereo data
  117. * to a single mono stream.
  118. */
  119. static int midas_ext_spkmode(struct snd_soc_dapm_widget *w,
  120. struct snd_kcontrol *kcontrol, int event)
  121. {
  122. struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
  123. int ret = 0;
  124. switch (event) {
  125. case SND_SOC_DAPM_PRE_PMU:
  126. ret = snd_soc_component_update_bits(codec, WM8994_SPKOUT_MIXERS,
  127. WM8994_SPKMIXR_TO_SPKOUTL_MASK,
  128. WM8994_SPKMIXR_TO_SPKOUTL);
  129. break;
  130. case SND_SOC_DAPM_POST_PMD:
  131. ret = snd_soc_component_update_bits(codec, WM8994_SPKOUT_MIXERS,
  132. WM8994_SPKMIXR_TO_SPKOUTL_MASK,
  133. 0);
  134. break;
  135. }
  136. return ret;
  137. }
  138. static int midas_mic_bias(struct snd_soc_dapm_widget *w,
  139. struct snd_kcontrol *kcontrol, int event)
  140. {
  141. struct snd_soc_card *card = w->dapm->card;
  142. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  143. switch (event) {
  144. case SND_SOC_DAPM_PRE_PMU:
  145. return regulator_enable(priv->reg_mic_bias);
  146. case SND_SOC_DAPM_POST_PMD:
  147. return regulator_disable(priv->reg_mic_bias);
  148. }
  149. return 0;
  150. }
  151. static int midas_submic_bias(struct snd_soc_dapm_widget *w,
  152. struct snd_kcontrol *kcontrol, int event)
  153. {
  154. struct snd_soc_card *card = w->dapm->card;
  155. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  156. switch (event) {
  157. case SND_SOC_DAPM_PRE_PMU:
  158. return regulator_enable(priv->reg_submic_bias);
  159. case SND_SOC_DAPM_POST_PMD:
  160. return regulator_disable(priv->reg_submic_bias);
  161. }
  162. return 0;
  163. }
  164. static int midas_fm_set(struct snd_soc_dapm_widget *w,
  165. struct snd_kcontrol *kcontrol, int event)
  166. {
  167. struct snd_soc_card *card = w->dapm->card;
  168. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  169. if (!priv->gpio_fm_sel)
  170. return 0;
  171. switch (event) {
  172. case SND_SOC_DAPM_PRE_PMU:
  173. gpiod_set_value_cansleep(priv->gpio_fm_sel, 1);
  174. break;
  175. case SND_SOC_DAPM_POST_PMD:
  176. gpiod_set_value_cansleep(priv->gpio_fm_sel, 0);
  177. break;
  178. }
  179. return 0;
  180. }
  181. static int midas_line_set(struct snd_soc_dapm_widget *w,
  182. struct snd_kcontrol *kcontrol, int event)
  183. {
  184. struct snd_soc_card *card = w->dapm->card;
  185. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  186. if (!priv->gpio_lineout_sel)
  187. return 0;
  188. switch (event) {
  189. case SND_SOC_DAPM_PRE_PMU:
  190. gpiod_set_value_cansleep(priv->gpio_lineout_sel, 1);
  191. break;
  192. case SND_SOC_DAPM_POST_PMD:
  193. gpiod_set_value_cansleep(priv->gpio_lineout_sel, 0);
  194. break;
  195. }
  196. return 0;
  197. }
  198. static const struct snd_kcontrol_new midas_controls[] = {
  199. SOC_DAPM_PIN_SWITCH("HP"),
  200. SOC_DAPM_PIN_SWITCH("SPK"),
  201. SOC_DAPM_PIN_SWITCH("RCV"),
  202. SOC_DAPM_PIN_SWITCH("LINE"),
  203. SOC_DAPM_PIN_SWITCH("HDMI"),
  204. SOC_DAPM_PIN_SWITCH("Main Mic"),
  205. SOC_DAPM_PIN_SWITCH("Sub Mic"),
  206. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  207. SOC_DAPM_PIN_SWITCH("FM In"),
  208. };
  209. static const struct snd_soc_dapm_widget midas_dapm_widgets[] = {
  210. SND_SOC_DAPM_HP("HP", NULL),
  211. SND_SOC_DAPM_SPK("SPK", midas_ext_spkmode),
  212. SND_SOC_DAPM_SPK("RCV", NULL),
  213. /* FIXME: toggle MAX77693 on i9300/i9305 */
  214. SND_SOC_DAPM_LINE("LINE", midas_line_set),
  215. SND_SOC_DAPM_LINE("HDMI", NULL),
  216. SND_SOC_DAPM_LINE("FM In", midas_fm_set),
  217. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  218. SND_SOC_DAPM_MIC("Main Mic", midas_mic_bias),
  219. SND_SOC_DAPM_MIC("Sub Mic", midas_submic_bias),
  220. };
  221. static int midas_set_bias_level(struct snd_soc_card *card,
  222. struct snd_soc_dapm_context *dapm,
  223. enum snd_soc_bias_level level)
  224. {
  225. struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card,
  226. &card->dai_link[0]);
  227. struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
  228. if (dapm->dev != aif1_dai->dev)
  229. return 0;
  230. switch (level) {
  231. case SND_SOC_BIAS_STANDBY:
  232. return midas_stop_fll1(rtd);
  233. case SND_SOC_BIAS_PREPARE:
  234. return midas_start_fll1(rtd, 0);
  235. default:
  236. break;
  237. }
  238. return 0;
  239. }
  240. static int midas_late_probe(struct snd_soc_card *card)
  241. {
  242. struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card,
  243. &card->dai_link[0]);
  244. struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
  245. struct midas_priv *priv = snd_soc_card_get_drvdata(card);
  246. int ret;
  247. /* Use MCLK2 as SYSCLK for boot */
  248. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2, MCLK2_RATE,
  249. SND_SOC_CLOCK_IN);
  250. if (ret < 0) {
  251. dev_err(aif1_dai->dev, "Failed to switch to MCLK2: %d\n", ret);
  252. return ret;
  253. }
  254. ret = snd_soc_card_jack_new(card, "Headset",
  255. SND_JACK_HEADSET | SND_JACK_MECHANICAL |
  256. SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2 |
  257. SND_JACK_BTN_3 | SND_JACK_BTN_4 | SND_JACK_BTN_5,
  258. &priv->headset_jack);
  259. if (ret)
  260. return ret;
  261. wm8958_mic_detect(aif1_dai->component, &priv->headset_jack,
  262. NULL, NULL, NULL, NULL);
  263. return 0;
  264. }
  265. static struct snd_soc_dai_driver midas_ext_dai[] = {
  266. {
  267. .name = "Voice call",
  268. .playback = {
  269. .channels_min = 1,
  270. .channels_max = 2,
  271. .rate_min = 8000,
  272. .rate_max = 16000,
  273. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
  274. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  275. },
  276. .capture = {
  277. .channels_min = 1,
  278. .channels_max = 2,
  279. .rate_min = 8000,
  280. .rate_max = 16000,
  281. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
  282. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  283. },
  284. },
  285. {
  286. .name = "Bluetooth",
  287. .playback = {
  288. .channels_min = 1,
  289. .channels_max = 2,
  290. .rate_min = 8000,
  291. .rate_max = 16000,
  292. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
  293. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  294. },
  295. .capture = {
  296. .channels_min = 1,
  297. .channels_max = 2,
  298. .rate_min = 8000,
  299. .rate_max = 16000,
  300. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
  301. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  302. },
  303. },
  304. };
  305. static const struct snd_soc_component_driver midas_component = {
  306. .name = "midas-audio",
  307. };
  308. SND_SOC_DAILINK_DEFS(wm1811_hifi,
  309. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  310. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif1")),
  311. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  312. SND_SOC_DAILINK_DEFS(wm1811_voice,
  313. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  314. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif2")),
  315. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  316. SND_SOC_DAILINK_DEFS(wm1811_bt,
  317. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  318. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif3")),
  319. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  320. static struct snd_soc_dai_link midas_dai[] = {
  321. {
  322. .name = "WM8994 AIF1",
  323. .stream_name = "HiFi Primary",
  324. .ops = &midas_aif1_ops,
  325. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  326. SND_SOC_DAIFMT_CBM_CFM,
  327. SND_SOC_DAILINK_REG(wm1811_hifi),
  328. }, {
  329. .name = "WM1811 Voice",
  330. .stream_name = "Voice call",
  331. .ignore_suspend = 1,
  332. SND_SOC_DAILINK_REG(wm1811_voice),
  333. }, {
  334. .name = "WM1811 BT",
  335. .stream_name = "Bluetooth",
  336. .ignore_suspend = 1,
  337. SND_SOC_DAILINK_REG(wm1811_bt),
  338. },
  339. };
  340. static struct snd_soc_card midas_card = {
  341. .name = "Midas WM1811",
  342. .owner = THIS_MODULE,
  343. .dai_link = midas_dai,
  344. .num_links = ARRAY_SIZE(midas_dai),
  345. .controls = midas_controls,
  346. .num_controls = ARRAY_SIZE(midas_controls),
  347. .dapm_widgets = midas_dapm_widgets,
  348. .num_dapm_widgets = ARRAY_SIZE(midas_dapm_widgets),
  349. .set_bias_level = midas_set_bias_level,
  350. .late_probe = midas_late_probe,
  351. };
  352. static int midas_probe(struct platform_device *pdev)
  353. {
  354. struct device_node *cpu_dai_node = NULL, *codec_dai_node = NULL;
  355. struct device_node *cpu = NULL, *codec = NULL;
  356. struct snd_soc_card *card = &midas_card;
  357. struct device *dev = &pdev->dev;
  358. static struct snd_soc_dai_link *dai_link;
  359. struct midas_priv *priv;
  360. int ret, i;
  361. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  362. if (!priv)
  363. return -ENOMEM;
  364. snd_soc_card_set_drvdata(card, priv);
  365. card->dev = dev;
  366. priv->reg_mic_bias = devm_regulator_get(dev, "mic-bias");
  367. if (IS_ERR(priv->reg_mic_bias)) {
  368. dev_err(dev, "Failed to get mic bias regulator\n");
  369. return PTR_ERR(priv->reg_mic_bias);
  370. }
  371. priv->reg_submic_bias = devm_regulator_get(dev, "submic-bias");
  372. if (IS_ERR(priv->reg_submic_bias)) {
  373. dev_err(dev, "Failed to get submic bias regulator\n");
  374. return PTR_ERR(priv->reg_submic_bias);
  375. }
  376. priv->gpio_fm_sel = devm_gpiod_get_optional(dev, "fm-sel", GPIOD_OUT_HIGH);
  377. if (IS_ERR(priv->gpio_fm_sel)) {
  378. dev_err(dev, "Failed to get FM selection GPIO\n");
  379. return PTR_ERR(priv->gpio_fm_sel);
  380. }
  381. priv->gpio_lineout_sel = devm_gpiod_get_optional(dev, "lineout-sel",
  382. GPIOD_OUT_HIGH);
  383. if (IS_ERR(priv->gpio_lineout_sel)) {
  384. dev_err(dev, "Failed to get line out selection GPIO\n");
  385. return PTR_ERR(priv->gpio_lineout_sel);
  386. }
  387. ret = snd_soc_of_parse_card_name(card, "model");
  388. if (ret < 0) {
  389. dev_err(dev, "Card name is not specified\n");
  390. return ret;
  391. }
  392. ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
  393. if (ret < 0) {
  394. dev_err(dev, "Audio routing invalid/unspecified\n");
  395. return ret;
  396. }
  397. cpu = of_get_child_by_name(dev->of_node, "cpu");
  398. if (!cpu)
  399. return -EINVAL;
  400. codec = of_get_child_by_name(dev->of_node, "codec");
  401. if (!codec) {
  402. of_node_put(cpu);
  403. return -EINVAL;
  404. }
  405. cpu_dai_node = of_parse_phandle(cpu, "sound-dai", 0);
  406. of_node_put(cpu);
  407. if (!cpu_dai_node) {
  408. dev_err(dev, "parsing cpu/sound-dai failed\n");
  409. of_node_put(codec);
  410. return -EINVAL;
  411. }
  412. codec_dai_node = of_parse_phandle(codec, "sound-dai", 0);
  413. of_node_put(codec);
  414. if (!codec_dai_node) {
  415. dev_err(dev, "audio-codec property invalid/missing\n");
  416. ret = -EINVAL;
  417. goto put_cpu_dai_node;
  418. }
  419. for_each_card_prelinks(card, i, dai_link) {
  420. dai_link->codecs->of_node = codec_dai_node;
  421. dai_link->cpus->of_node = cpu_dai_node;
  422. dai_link->platforms->of_node = cpu_dai_node;
  423. }
  424. ret = devm_snd_soc_register_component(dev, &midas_component,
  425. midas_ext_dai, ARRAY_SIZE(midas_ext_dai));
  426. if (ret < 0) {
  427. dev_err(dev, "Failed to register component: %d\n", ret);
  428. goto put_codec_dai_node;
  429. }
  430. ret = devm_snd_soc_register_card(dev, card);
  431. if (ret < 0) {
  432. dev_err(dev, "Failed to register card: %d\n", ret);
  433. goto put_codec_dai_node;
  434. }
  435. return 0;
  436. put_codec_dai_node:
  437. of_node_put(codec_dai_node);
  438. put_cpu_dai_node:
  439. of_node_put(cpu_dai_node);
  440. return ret;
  441. }
  442. static const struct of_device_id midas_of_match[] = {
  443. { .compatible = "samsung,midas-audio" },
  444. { },
  445. };
  446. MODULE_DEVICE_TABLE(of, midas_of_match);
  447. static struct platform_driver midas_driver = {
  448. .driver = {
  449. .name = "midas-audio",
  450. .of_match_table = midas_of_match,
  451. .pm = &snd_soc_pm_ops,
  452. },
  453. .probe = midas_probe,
  454. };
  455. module_platform_driver(midas_driver);
  456. MODULE_AUTHOR("Simon Shields <[email protected]>");
  457. MODULE_DESCRIPTION("ASoC support for Midas");
  458. MODULE_LICENSE("GPL v2");