xref: /openbmc/linux/sound/soc/sh/migor.c (revision c8587193)
19f5b64b7SGuennadi Liakhovetski /*
29f5b64b7SGuennadi Liakhovetski  * ALSA SoC driver for Migo-R
39f5b64b7SGuennadi Liakhovetski  *
49f5b64b7SGuennadi Liakhovetski  * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
59f5b64b7SGuennadi Liakhovetski  *
69f5b64b7SGuennadi Liakhovetski  * This program is free software; you can redistribute it and/or modify
79f5b64b7SGuennadi Liakhovetski  * it under the terms of the GNU General Public License version 2 as
89f5b64b7SGuennadi Liakhovetski  * published by the Free Software Foundation.
99f5b64b7SGuennadi Liakhovetski  */
109f5b64b7SGuennadi Liakhovetski 
11f4fd58d6SGuennadi Liakhovetski #include <linux/clkdev.h>
129f5b64b7SGuennadi Liakhovetski #include <linux/device.h>
139f5b64b7SGuennadi Liakhovetski #include <linux/firmware.h>
149f5b64b7SGuennadi Liakhovetski #include <linux/module.h>
159f5b64b7SGuennadi Liakhovetski 
169f5b64b7SGuennadi Liakhovetski #include <asm/clock.h>
179f5b64b7SGuennadi Liakhovetski 
189f5b64b7SGuennadi Liakhovetski #include <cpu/sh7722.h>
199f5b64b7SGuennadi Liakhovetski 
209f5b64b7SGuennadi Liakhovetski #include <sound/core.h>
219f5b64b7SGuennadi Liakhovetski #include <sound/pcm.h>
229f5b64b7SGuennadi Liakhovetski #include <sound/soc.h>
239f5b64b7SGuennadi Liakhovetski 
249f5b64b7SGuennadi Liakhovetski #include "../codecs/wm8978.h"
259f5b64b7SGuennadi Liakhovetski #include "siu.h"
269f5b64b7SGuennadi Liakhovetski 
279f5b64b7SGuennadi Liakhovetski /* Default 8000Hz sampling frequency */
289f5b64b7SGuennadi Liakhovetski static unsigned long codec_freq = 8000 * 512;
299f5b64b7SGuennadi Liakhovetski 
309f5b64b7SGuennadi Liakhovetski static unsigned int use_count;
319f5b64b7SGuennadi Liakhovetski 
329f5b64b7SGuennadi Liakhovetski /* External clock, sourced from the codec at the SIUMCKB pin */
339f5b64b7SGuennadi Liakhovetski static unsigned long siumckb_recalc(struct clk *clk)
349f5b64b7SGuennadi Liakhovetski {
359f5b64b7SGuennadi Liakhovetski 	return codec_freq;
369f5b64b7SGuennadi Liakhovetski }
379f5b64b7SGuennadi Liakhovetski 
38c8587193SGuennadi Liakhovetski static struct sh_clk_ops siumckb_clk_ops = {
399f5b64b7SGuennadi Liakhovetski 	.recalc = siumckb_recalc,
409f5b64b7SGuennadi Liakhovetski };
419f5b64b7SGuennadi Liakhovetski 
429f5b64b7SGuennadi Liakhovetski static struct clk siumckb_clk = {
439f5b64b7SGuennadi Liakhovetski 	.ops		= &siumckb_clk_ops,
449f5b64b7SGuennadi Liakhovetski 	.rate		= 0, /* initialised at run-time */
459f5b64b7SGuennadi Liakhovetski };
469f5b64b7SGuennadi Liakhovetski 
47cbfa5184SGuennadi Liakhovetski static struct clk_lookup *siumckb_lookup;
48cbfa5184SGuennadi Liakhovetski 
499f5b64b7SGuennadi Liakhovetski static int migor_hw_params(struct snd_pcm_substream *substream,
509f5b64b7SGuennadi Liakhovetski 			   struct snd_pcm_hw_params *params)
519f5b64b7SGuennadi Liakhovetski {
529f5b64b7SGuennadi Liakhovetski 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
53f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
549f5b64b7SGuennadi Liakhovetski 	int ret;
559f5b64b7SGuennadi Liakhovetski 	unsigned int rate = params_rate(params);
569f5b64b7SGuennadi Liakhovetski 
579f5b64b7SGuennadi Liakhovetski 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000,
589f5b64b7SGuennadi Liakhovetski 				     SND_SOC_CLOCK_IN);
599f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
609f5b64b7SGuennadi Liakhovetski 		return ret;
619f5b64b7SGuennadi Liakhovetski 
629f5b64b7SGuennadi Liakhovetski 	ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
639f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
649f5b64b7SGuennadi Liakhovetski 		return ret;
659f5b64b7SGuennadi Liakhovetski 
669f5b64b7SGuennadi Liakhovetski 	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF |
679f5b64b7SGuennadi Liakhovetski 				  SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
689f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
699f5b64b7SGuennadi Liakhovetski 		return ret;
709f5b64b7SGuennadi Liakhovetski 
71f0fba2adSLiam Girdwood 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF |
729f5b64b7SGuennadi Liakhovetski 				  SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
739f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
749f5b64b7SGuennadi Liakhovetski 		return ret;
759f5b64b7SGuennadi Liakhovetski 
769f5b64b7SGuennadi Liakhovetski 	codec_freq = rate * 512;
779f5b64b7SGuennadi Liakhovetski 	/*
789f5b64b7SGuennadi Liakhovetski 	 * This propagates the parent frequency change to children and
799f5b64b7SGuennadi Liakhovetski 	 * recalculates the frequency table
809f5b64b7SGuennadi Liakhovetski 	 */
819f5b64b7SGuennadi Liakhovetski 	clk_set_rate(&siumckb_clk, codec_freq);
829f5b64b7SGuennadi Liakhovetski 	dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
839f5b64b7SGuennadi Liakhovetski 
84f0fba2adSLiam Girdwood 	ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
859f5b64b7SGuennadi Liakhovetski 				     codec_freq / 2, SND_SOC_CLOCK_IN);
869f5b64b7SGuennadi Liakhovetski 
879f5b64b7SGuennadi Liakhovetski 	if (!ret)
889f5b64b7SGuennadi Liakhovetski 		use_count++;
899f5b64b7SGuennadi Liakhovetski 
909f5b64b7SGuennadi Liakhovetski 	return ret;
919f5b64b7SGuennadi Liakhovetski }
929f5b64b7SGuennadi Liakhovetski 
939f5b64b7SGuennadi Liakhovetski static int migor_hw_free(struct snd_pcm_substream *substream)
949f5b64b7SGuennadi Liakhovetski {
959f5b64b7SGuennadi Liakhovetski 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
96f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
979f5b64b7SGuennadi Liakhovetski 
989f5b64b7SGuennadi Liakhovetski 	if (use_count) {
999f5b64b7SGuennadi Liakhovetski 		use_count--;
1009f5b64b7SGuennadi Liakhovetski 
1019f5b64b7SGuennadi Liakhovetski 		if (!use_count)
1029f5b64b7SGuennadi Liakhovetski 			snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
1039f5b64b7SGuennadi Liakhovetski 					       SND_SOC_CLOCK_IN);
1049f5b64b7SGuennadi Liakhovetski 	} else {
1059f5b64b7SGuennadi Liakhovetski 		dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
1069f5b64b7SGuennadi Liakhovetski 	}
1079f5b64b7SGuennadi Liakhovetski 
1089f5b64b7SGuennadi Liakhovetski 	return 0;
1099f5b64b7SGuennadi Liakhovetski }
1109f5b64b7SGuennadi Liakhovetski 
1119f5b64b7SGuennadi Liakhovetski static struct snd_soc_ops migor_dai_ops = {
1129f5b64b7SGuennadi Liakhovetski 	.hw_params = migor_hw_params,
1139f5b64b7SGuennadi Liakhovetski 	.hw_free = migor_hw_free,
1149f5b64b7SGuennadi Liakhovetski };
1159f5b64b7SGuennadi Liakhovetski 
1169f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
1179f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_HP("Headphone", NULL),
1189f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
1199f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_MIC("External Microphone", NULL),
1209f5b64b7SGuennadi Liakhovetski };
1219f5b64b7SGuennadi Liakhovetski 
1229f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_route audio_map[] = {
1239f5b64b7SGuennadi Liakhovetski 	/* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
1249f5b64b7SGuennadi Liakhovetski 	{ "Headphone", NULL,  "OUT4 VMID" },
1259f5b64b7SGuennadi Liakhovetski 	{ "OUT4 VMID", NULL,  "LHP" },
1269f5b64b7SGuennadi Liakhovetski 	{ "OUT4 VMID", NULL,  "RHP" },
1279f5b64b7SGuennadi Liakhovetski 
1289f5b64b7SGuennadi Liakhovetski 	/* On-board microphone */
1299f5b64b7SGuennadi Liakhovetski 	{ "RMICN", NULL, "Mic Bias" },
1309f5b64b7SGuennadi Liakhovetski 	{ "RMICP", NULL, "Mic Bias" },
1319f5b64b7SGuennadi Liakhovetski 	{ "Mic Bias", NULL, "Onboard Microphone" },
1329f5b64b7SGuennadi Liakhovetski 
1339f5b64b7SGuennadi Liakhovetski 	/* External microphone */
1349f5b64b7SGuennadi Liakhovetski 	{ "LMICN", NULL, "Mic Bias" },
1359f5b64b7SGuennadi Liakhovetski 	{ "LMICP", NULL, "Mic Bias" },
1369f5b64b7SGuennadi Liakhovetski 	{ "Mic Bias", NULL, "External Microphone" },
1379f5b64b7SGuennadi Liakhovetski };
1389f5b64b7SGuennadi Liakhovetski 
139f0fba2adSLiam Girdwood static int migor_dai_init(struct snd_soc_pcm_runtime *rtd)
1409f5b64b7SGuennadi Liakhovetski {
141f0fba2adSLiam Girdwood 	struct snd_soc_codec *codec = rtd->codec;
142ce6120ccSLiam Girdwood 	struct snd_soc_dapm_context *dapm = &codec->dapm;
143f0fba2adSLiam Girdwood 
144ce6120ccSLiam Girdwood 	snd_soc_dapm_new_controls(dapm, migor_dapm_widgets,
1459f5b64b7SGuennadi Liakhovetski 				  ARRAY_SIZE(migor_dapm_widgets));
1469f5b64b7SGuennadi Liakhovetski 
147ce6120ccSLiam Girdwood 	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
1489f5b64b7SGuennadi Liakhovetski 
1499f5b64b7SGuennadi Liakhovetski 	return 0;
1509f5b64b7SGuennadi Liakhovetski }
1519f5b64b7SGuennadi Liakhovetski 
1529f5b64b7SGuennadi Liakhovetski /* migor digital audio interface glue - connects codec <--> CPU */
1539f5b64b7SGuennadi Liakhovetski static struct snd_soc_dai_link migor_dai = {
1549f5b64b7SGuennadi Liakhovetski 	.name = "wm8978",
1559f5b64b7SGuennadi Liakhovetski 	.stream_name = "WM8978",
156f0fba2adSLiam Girdwood 	.cpu_dai_name = "siu-i2s-dai",
157f0fba2adSLiam Girdwood 	.codec_dai_name = "wm8978-hifi",
158f0fba2adSLiam Girdwood 	.platform_name = "siu-pcm-audio",
1595250a503SMark Brown 	.codec_name = "wm8978.0-001a",
1609f5b64b7SGuennadi Liakhovetski 	.ops = &migor_dai_ops,
1619f5b64b7SGuennadi Liakhovetski 	.init = migor_dai_init,
1629f5b64b7SGuennadi Liakhovetski };
1639f5b64b7SGuennadi Liakhovetski 
1649f5b64b7SGuennadi Liakhovetski /* migor audio machine driver */
1659f5b64b7SGuennadi Liakhovetski static struct snd_soc_card snd_soc_migor = {
1669f5b64b7SGuennadi Liakhovetski 	.name = "Migo-R",
1674a7042e5SAxel Lin 	.owner = THIS_MODULE,
1689f5b64b7SGuennadi Liakhovetski 	.dai_link = &migor_dai,
1699f5b64b7SGuennadi Liakhovetski 	.num_links = 1,
1709f5b64b7SGuennadi Liakhovetski };
1719f5b64b7SGuennadi Liakhovetski 
1729f5b64b7SGuennadi Liakhovetski static struct platform_device *migor_snd_device;
1739f5b64b7SGuennadi Liakhovetski 
1749f5b64b7SGuennadi Liakhovetski static int __init migor_init(void)
1759f5b64b7SGuennadi Liakhovetski {
1769f5b64b7SGuennadi Liakhovetski 	int ret;
1779f5b64b7SGuennadi Liakhovetski 
1789f5b64b7SGuennadi Liakhovetski 	ret = clk_register(&siumckb_clk);
1799f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
1809f5b64b7SGuennadi Liakhovetski 		return ret;
1819f5b64b7SGuennadi Liakhovetski 
182cbfa5184SGuennadi Liakhovetski 	siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL);
183cbfa5184SGuennadi Liakhovetski 	if (!siumckb_lookup) {
184cbfa5184SGuennadi Liakhovetski 		ret = -ENOMEM;
185cbfa5184SGuennadi Liakhovetski 		goto eclkdevalloc;
186cbfa5184SGuennadi Liakhovetski 	}
187cbfa5184SGuennadi Liakhovetski 	clkdev_add(siumckb_lookup);
188cbfa5184SGuennadi Liakhovetski 
1899f5b64b7SGuennadi Liakhovetski 	/* Port number used on this machine: port B */
1909f5b64b7SGuennadi Liakhovetski 	migor_snd_device = platform_device_alloc("soc-audio", 1);
1919f5b64b7SGuennadi Liakhovetski 	if (!migor_snd_device) {
1929f5b64b7SGuennadi Liakhovetski 		ret = -ENOMEM;
1939f5b64b7SGuennadi Liakhovetski 		goto epdevalloc;
1949f5b64b7SGuennadi Liakhovetski 	}
1959f5b64b7SGuennadi Liakhovetski 
196f0fba2adSLiam Girdwood 	platform_set_drvdata(migor_snd_device, &snd_soc_migor);
1979f5b64b7SGuennadi Liakhovetski 
1989f5b64b7SGuennadi Liakhovetski 	ret = platform_device_add(migor_snd_device);
1999f5b64b7SGuennadi Liakhovetski 	if (ret)
2009f5b64b7SGuennadi Liakhovetski 		goto epdevadd;
2019f5b64b7SGuennadi Liakhovetski 
2029f5b64b7SGuennadi Liakhovetski 	return 0;
2039f5b64b7SGuennadi Liakhovetski 
2049f5b64b7SGuennadi Liakhovetski epdevadd:
2059f5b64b7SGuennadi Liakhovetski 	platform_device_put(migor_snd_device);
2069f5b64b7SGuennadi Liakhovetski epdevalloc:
207cbfa5184SGuennadi Liakhovetski 	clkdev_drop(siumckb_lookup);
208cbfa5184SGuennadi Liakhovetski eclkdevalloc:
2099f5b64b7SGuennadi Liakhovetski 	clk_unregister(&siumckb_clk);
2109f5b64b7SGuennadi Liakhovetski 	return ret;
2119f5b64b7SGuennadi Liakhovetski }
2129f5b64b7SGuennadi Liakhovetski 
2139f5b64b7SGuennadi Liakhovetski static void __exit migor_exit(void)
2149f5b64b7SGuennadi Liakhovetski {
215cbfa5184SGuennadi Liakhovetski 	clkdev_drop(siumckb_lookup);
2169f5b64b7SGuennadi Liakhovetski 	clk_unregister(&siumckb_clk);
2179f5b64b7SGuennadi Liakhovetski 	platform_device_unregister(migor_snd_device);
2189f5b64b7SGuennadi Liakhovetski }
2199f5b64b7SGuennadi Liakhovetski 
2209f5b64b7SGuennadi Liakhovetski module_init(migor_init);
2219f5b64b7SGuennadi Liakhovetski module_exit(migor_exit);
2229f5b64b7SGuennadi Liakhovetski 
2239f5b64b7SGuennadi Liakhovetski MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
2249f5b64b7SGuennadi Liakhovetski MODULE_DESCRIPTION("ALSA SoC Migor");
2259f5b64b7SGuennadi Liakhovetski MODULE_LICENSE("GPL v2");
226