bytcr_wm5102.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bytcr_wm5102.c - ASoc Machine driver for Intel Baytrail platforms with a
  4. * Wolfson Microelectronics WM5102 codec
  5. *
  6. * Copyright (C) 2020 Hans de Goede <[email protected]>
  7. * Loosely based on bytcr_rt5640.c which is:
  8. * Copyright (C) 2014-2020 Intel Corp
  9. * Author: Subhransu S. Prusty <[email protected]>
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/clk.h>
  13. #include <linux/device.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/spi/spi.h>
  20. #include <sound/jack.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-acpi.h>
  25. #include "../../codecs/wm5102.h"
  26. #include "../atom/sst-atom-controls.h"
  27. #define MCLK_FREQ 25000000
  28. #define WM5102_MAX_SYSCLK_4K 49152000 /* max sysclk for 4K family */
  29. #define WM5102_MAX_SYSCLK_11025 45158400 /* max sysclk for 11.025K family */
  30. struct byt_wm5102_private {
  31. struct snd_soc_jack jack;
  32. struct clk *mclk;
  33. struct gpio_desc *spkvdd_en_gpio;
  34. };
  35. static int byt_wm5102_spkvdd_power_event(struct snd_soc_dapm_widget *w,
  36. struct snd_kcontrol *kcontrol, int event)
  37. {
  38. struct snd_soc_card *card = w->dapm->card;
  39. struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
  40. gpiod_set_value_cansleep(priv->spkvdd_en_gpio,
  41. !!SND_SOC_DAPM_EVENT_ON(event));
  42. return 0;
  43. }
  44. static int byt_wm5102_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, int rate)
  45. {
  46. struct snd_soc_component *codec_component = codec_dai->component;
  47. int sr_mult = ((rate % 4000) == 0) ?
  48. (WM5102_MAX_SYSCLK_4K / rate) :
  49. (WM5102_MAX_SYSCLK_11025 / rate);
  50. int ret;
  51. /* Reset FLL1 */
  52. snd_soc_dai_set_pll(codec_dai, WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
  53. snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
  54. /* Configure the FLL1 PLL before selecting it */
  55. ret = snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_CLK_SRC_MCLK1,
  56. MCLK_FREQ, rate * sr_mult);
  57. if (ret) {
  58. dev_err(codec_component->dev, "Error setting PLL: %d\n", ret);
  59. return ret;
  60. }
  61. ret = snd_soc_component_set_sysclk(codec_component, ARIZONA_CLK_SYSCLK,
  62. ARIZONA_CLK_SRC_FLL1, rate * sr_mult,
  63. SND_SOC_CLOCK_IN);
  64. if (ret) {
  65. dev_err(codec_component->dev, "Error setting SYSCLK: %d\n", ret);
  66. return ret;
  67. }
  68. ret = snd_soc_dai_set_sysclk(codec_dai, ARIZONA_CLK_SYSCLK,
  69. rate * 512, SND_SOC_CLOCK_IN);
  70. if (ret) {
  71. dev_err(codec_component->dev, "Error setting clock: %d\n", ret);
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  77. struct snd_kcontrol *k, int event)
  78. {
  79. struct snd_soc_dapm_context *dapm = w->dapm;
  80. struct snd_soc_card *card = dapm->card;
  81. struct snd_soc_dai *codec_dai;
  82. struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
  83. int ret;
  84. codec_dai = snd_soc_card_get_codec_dai(card, "wm5102-aif1");
  85. if (!codec_dai) {
  86. dev_err(card->dev, "Error codec DAI not found\n");
  87. return -EIO;
  88. }
  89. if (SND_SOC_DAPM_EVENT_ON(event)) {
  90. ret = clk_prepare_enable(priv->mclk);
  91. if (ret) {
  92. dev_err(card->dev, "Error enabling MCLK: %d\n", ret);
  93. return ret;
  94. }
  95. ret = byt_wm5102_prepare_and_enable_pll1(codec_dai, 48000);
  96. if (ret) {
  97. dev_err(card->dev, "Error setting codec sysclk: %d\n", ret);
  98. return ret;
  99. }
  100. } else {
  101. /*
  102. * The WM5102 has a separate 32KHz clock for jack-detect
  103. * so we can disable the PLL, followed by disabling the
  104. * platform clock which is the source-clock for the PLL.
  105. */
  106. snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
  107. clk_disable_unprepare(priv->mclk);
  108. }
  109. return 0;
  110. }
  111. static const struct snd_soc_dapm_widget byt_wm5102_widgets[] = {
  112. SND_SOC_DAPM_HP("Headphone", NULL),
  113. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  114. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  115. SND_SOC_DAPM_SPK("Speaker", NULL),
  116. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  117. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  118. SND_SOC_DAPM_POST_PMD),
  119. SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0,
  120. byt_wm5102_spkvdd_power_event,
  121. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  122. };
  123. static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = {
  124. {"Headphone", NULL, "Platform Clock"},
  125. {"Headset Mic", NULL, "Platform Clock"},
  126. {"Internal Mic", NULL, "Platform Clock"},
  127. {"Speaker", NULL, "Platform Clock"},
  128. {"Speaker", NULL, "SPKOUTLP"},
  129. {"Speaker", NULL, "SPKOUTLN"},
  130. {"Speaker", NULL, "SPKOUTRP"},
  131. {"Speaker", NULL, "SPKOUTRN"},
  132. {"Speaker", NULL, "Speaker VDD"},
  133. {"Headphone", NULL, "HPOUT1L"},
  134. {"Headphone", NULL, "HPOUT1R"},
  135. {"Internal Mic", NULL, "MICBIAS3"},
  136. {"IN3L", NULL, "Internal Mic"},
  137. /*
  138. * The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset
  139. * is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch.
  140. */
  141. {"Headset Mic", NULL, "MICBIAS1"},
  142. {"Headset Mic", NULL, "MICBIAS2"},
  143. {"IN1L", NULL, "Headset Mic"},
  144. {"AIF1 Playback", NULL, "ssp0 Tx"},
  145. {"ssp0 Tx", NULL, "modem_out"},
  146. {"modem_in", NULL, "ssp0 Rx"},
  147. {"ssp0 Rx", NULL, "AIF1 Capture"},
  148. };
  149. static const struct snd_kcontrol_new byt_wm5102_controls[] = {
  150. SOC_DAPM_PIN_SWITCH("Headphone"),
  151. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  152. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  153. SOC_DAPM_PIN_SWITCH("Speaker"),
  154. };
  155. static struct snd_soc_jack_pin byt_wm5102_pins[] = {
  156. {
  157. .pin = "Headphone",
  158. .mask = SND_JACK_HEADPHONE,
  159. },
  160. {
  161. .pin = "Headset Mic",
  162. .mask = SND_JACK_MICROPHONE,
  163. },
  164. };
  165. static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime)
  166. {
  167. struct snd_soc_card *card = runtime->card;
  168. struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
  169. struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
  170. int ret, jack_type;
  171. card->dapm.idle_bias_off = true;
  172. ret = snd_soc_add_card_controls(card, byt_wm5102_controls,
  173. ARRAY_SIZE(byt_wm5102_controls));
  174. if (ret) {
  175. dev_err(card->dev, "Error adding card controls: %d\n", ret);
  176. return ret;
  177. }
  178. /*
  179. * The firmware might enable the clock at boot (this information
  180. * may or may not be reflected in the enable clock register).
  181. * To change the rate we must disable the clock first to cover these
  182. * cases. Due to common clock framework restrictions that do not allow
  183. * to disable a clock that has not been enabled, we need to enable
  184. * the clock first.
  185. */
  186. ret = clk_prepare_enable(priv->mclk);
  187. if (!ret)
  188. clk_disable_unprepare(priv->mclk);
  189. ret = clk_set_rate(priv->mclk, MCLK_FREQ);
  190. if (ret) {
  191. dev_err(card->dev, "Error setting MCLK rate: %d\n", ret);
  192. return ret;
  193. }
  194. jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  195. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  196. ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type,
  197. &priv->jack, byt_wm5102_pins,
  198. ARRAY_SIZE(byt_wm5102_pins));
  199. if (ret) {
  200. dev_err(card->dev, "Error creating jack: %d\n", ret);
  201. return ret;
  202. }
  203. snd_soc_component_set_jack(component, &priv->jack, NULL);
  204. return 0;
  205. }
  206. static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  207. struct snd_pcm_hw_params *params)
  208. {
  209. struct snd_interval *rate = hw_param_interval(params,
  210. SNDRV_PCM_HW_PARAM_RATE);
  211. struct snd_interval *channels = hw_param_interval(params,
  212. SNDRV_PCM_HW_PARAM_CHANNELS);
  213. int ret;
  214. /* The DSP will covert the FE rate to 48k, stereo */
  215. rate->min = 48000;
  216. rate->max = 48000;
  217. channels->min = 2;
  218. channels->max = 2;
  219. /* set SSP0 to 16-bit */
  220. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  221. /*
  222. * Default mode for SSP configuration is TDM 4 slot, override config
  223. * with explicit setting to I2S 2ch 16-bit. The word length is set with
  224. * dai_set_tdm_slot() since there is no other API exposed
  225. */
  226. ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
  227. SND_SOC_DAIFMT_I2S |
  228. SND_SOC_DAIFMT_NB_NF |
  229. SND_SOC_DAIFMT_BP_FP);
  230. if (ret) {
  231. dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret);
  232. return ret;
  233. }
  234. ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 16);
  235. if (ret) {
  236. dev_err(rtd->dev, "Error setting I2S config: %d\n", ret);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream)
  242. {
  243. return snd_pcm_hw_constraint_single(substream->runtime,
  244. SNDRV_PCM_HW_PARAM_RATE, 48000);
  245. }
  246. static const struct snd_soc_ops byt_wm5102_aif1_ops = {
  247. .startup = byt_wm5102_aif1_startup,
  248. };
  249. SND_SOC_DAILINK_DEF(dummy,
  250. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  251. SND_SOC_DAILINK_DEF(media,
  252. DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
  253. SND_SOC_DAILINK_DEF(deepbuffer,
  254. DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
  255. SND_SOC_DAILINK_DEF(ssp0_port,
  256. DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
  257. SND_SOC_DAILINK_DEF(ssp0_codec,
  258. DAILINK_COMP_ARRAY(COMP_CODEC(
  259. /*
  260. * Note there is no need to overwrite the codec-name as is done in
  261. * other bytcr machine drivers, because the codec is a MFD child-dev.
  262. */
  263. "wm5102-codec",
  264. "wm5102-aif1")));
  265. SND_SOC_DAILINK_DEF(platform,
  266. DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
  267. static struct snd_soc_dai_link byt_wm5102_dais[] = {
  268. [MERR_DPCM_AUDIO] = {
  269. .name = "Baytrail Audio Port",
  270. .stream_name = "Baytrail Audio",
  271. .nonatomic = true,
  272. .dynamic = 1,
  273. .dpcm_playback = 1,
  274. .dpcm_capture = 1,
  275. .ops = &byt_wm5102_aif1_ops,
  276. SND_SOC_DAILINK_REG(media, dummy, platform),
  277. },
  278. [MERR_DPCM_DEEP_BUFFER] = {
  279. .name = "Deep-Buffer Audio Port",
  280. .stream_name = "Deep-Buffer Audio",
  281. .nonatomic = true,
  282. .dynamic = 1,
  283. .dpcm_playback = 1,
  284. .ops = &byt_wm5102_aif1_ops,
  285. SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
  286. },
  287. /* back ends */
  288. {
  289. /*
  290. * This must be named SSP2-Codec even though this machine driver
  291. * always uses SSP0. Most machine drivers support both and dynamically
  292. * update the dailink to point to SSP0 or SSP2, while keeping the name
  293. * as "SSP2-Codec". The SOF tplg files hardcode the "SSP2-Codec" even
  294. * in the byt-foo-ssp0.tplg versions because the other machine-drivers
  295. * use "SSP2-Codec" even when SSP0 is used.
  296. */
  297. .name = "SSP2-Codec",
  298. .id = 0,
  299. .no_pcm = 1,
  300. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  301. | SND_SOC_DAIFMT_CBC_CFC,
  302. .be_hw_params_fixup = byt_wm5102_codec_fixup,
  303. .dpcm_playback = 1,
  304. .dpcm_capture = 1,
  305. .init = byt_wm5102_init,
  306. SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform),
  307. },
  308. };
  309. /* use space before codec name to simplify card ID, and simplify driver name */
  310. #define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */
  311. #define SOF_DRIVER_NAME "SOF"
  312. #define CARD_NAME "bytcr-wm5102"
  313. #define DRIVER_NAME NULL /* card name will be used for driver name */
  314. /* SoC card */
  315. static struct snd_soc_card byt_wm5102_card = {
  316. .owner = THIS_MODULE,
  317. .dai_link = byt_wm5102_dais,
  318. .num_links = ARRAY_SIZE(byt_wm5102_dais),
  319. .dapm_widgets = byt_wm5102_widgets,
  320. .num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets),
  321. .dapm_routes = byt_wm5102_audio_map,
  322. .num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map),
  323. .fully_routed = true,
  324. };
  325. static int snd_byt_wm5102_mc_probe(struct platform_device *pdev)
  326. {
  327. char codec_name[SND_ACPI_I2C_ID_LEN];
  328. struct device *dev = &pdev->dev;
  329. struct byt_wm5102_private *priv;
  330. struct snd_soc_acpi_mach *mach;
  331. const char *platform_name;
  332. struct acpi_device *adev;
  333. struct device *codec_dev;
  334. bool sof_parent;
  335. int ret;
  336. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  337. if (!priv)
  338. return -ENOMEM;
  339. /* Get MCLK */
  340. priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
  341. if (IS_ERR(priv->mclk))
  342. return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n");
  343. /*
  344. * Get speaker VDD enable GPIO:
  345. * 1. Get codec-device-name
  346. * 2. Get codec-device
  347. * 3. Get GPIO from codec-device
  348. */
  349. mach = dev->platform_data;
  350. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  351. if (!adev) {
  352. dev_err(dev, "Error cannot find acpi-dev for codec\n");
  353. return -ENOENT;
  354. }
  355. snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev));
  356. codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name);
  357. acpi_dev_put(adev);
  358. if (!codec_dev)
  359. return -EPROBE_DEFER;
  360. /* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */
  361. priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW);
  362. put_device(codec_dev);
  363. if (IS_ERR(priv->spkvdd_en_gpio)) {
  364. ret = PTR_ERR(priv->spkvdd_en_gpio);
  365. /*
  366. * The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c,
  367. * so -ENOENT means that arizona-spi hasn't probed yet.
  368. */
  369. if (ret == -ENOENT)
  370. ret = -EPROBE_DEFER;
  371. return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n");
  372. }
  373. /* override platform name, if required */
  374. byt_wm5102_card.dev = dev;
  375. platform_name = mach->mach_params.platform;
  376. ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name);
  377. if (ret)
  378. goto out_put_gpio;
  379. /* set card and driver name and pm-ops */
  380. sof_parent = snd_soc_acpi_sof_parent(dev);
  381. if (sof_parent) {
  382. byt_wm5102_card.name = SOF_CARD_NAME;
  383. byt_wm5102_card.driver_name = SOF_DRIVER_NAME;
  384. dev->driver->pm = &snd_soc_pm_ops;
  385. } else {
  386. byt_wm5102_card.name = CARD_NAME;
  387. byt_wm5102_card.driver_name = DRIVER_NAME;
  388. }
  389. snd_soc_card_set_drvdata(&byt_wm5102_card, priv);
  390. ret = devm_snd_soc_register_card(dev, &byt_wm5102_card);
  391. if (ret) {
  392. dev_err_probe(dev, ret, "registering card\n");
  393. goto out_put_gpio;
  394. }
  395. platform_set_drvdata(pdev, &byt_wm5102_card);
  396. return 0;
  397. out_put_gpio:
  398. gpiod_put(priv->spkvdd_en_gpio);
  399. return ret;
  400. }
  401. static int snd_byt_wm5102_mc_remove(struct platform_device *pdev)
  402. {
  403. struct snd_soc_card *card = platform_get_drvdata(pdev);
  404. struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
  405. gpiod_put(priv->spkvdd_en_gpio);
  406. return 0;
  407. }
  408. static struct platform_driver snd_byt_wm5102_mc_driver = {
  409. .driver = {
  410. .name = "bytcr_wm5102",
  411. },
  412. .probe = snd_byt_wm5102_mc_probe,
  413. .remove = snd_byt_wm5102_mc_remove,
  414. };
  415. module_platform_driver(snd_byt_wm5102_mc_driver);
  416. MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver");
  417. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  418. MODULE_LICENSE("GPL v2");
  419. MODULE_ALIAS("platform:bytcr_wm5102");