xref: /openbmc/linux/sound/soc/sh/migor.c (revision 9cb2a25c)
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 	codec_freq = rate * 512;
679f5b64b7SGuennadi Liakhovetski 	/*
689f5b64b7SGuennadi Liakhovetski 	 * This propagates the parent frequency change to children and
699f5b64b7SGuennadi Liakhovetski 	 * recalculates the frequency table
709f5b64b7SGuennadi Liakhovetski 	 */
719f5b64b7SGuennadi Liakhovetski 	clk_set_rate(&siumckb_clk, codec_freq);
729f5b64b7SGuennadi Liakhovetski 	dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
739f5b64b7SGuennadi Liakhovetski 
74f0fba2adSLiam Girdwood 	ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
759f5b64b7SGuennadi Liakhovetski 				     codec_freq / 2, SND_SOC_CLOCK_IN);
769f5b64b7SGuennadi Liakhovetski 
779f5b64b7SGuennadi Liakhovetski 	if (!ret)
789f5b64b7SGuennadi Liakhovetski 		use_count++;
799f5b64b7SGuennadi Liakhovetski 
809f5b64b7SGuennadi Liakhovetski 	return ret;
819f5b64b7SGuennadi Liakhovetski }
829f5b64b7SGuennadi Liakhovetski 
839f5b64b7SGuennadi Liakhovetski static int migor_hw_free(struct snd_pcm_substream *substream)
849f5b64b7SGuennadi Liakhovetski {
859f5b64b7SGuennadi Liakhovetski 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
86f0fba2adSLiam Girdwood 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
879f5b64b7SGuennadi Liakhovetski 
889f5b64b7SGuennadi Liakhovetski 	if (use_count) {
899f5b64b7SGuennadi Liakhovetski 		use_count--;
909f5b64b7SGuennadi Liakhovetski 
919f5b64b7SGuennadi Liakhovetski 		if (!use_count)
929f5b64b7SGuennadi Liakhovetski 			snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
939f5b64b7SGuennadi Liakhovetski 					       SND_SOC_CLOCK_IN);
949f5b64b7SGuennadi Liakhovetski 	} else {
959f5b64b7SGuennadi Liakhovetski 		dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
969f5b64b7SGuennadi Liakhovetski 	}
979f5b64b7SGuennadi Liakhovetski 
989f5b64b7SGuennadi Liakhovetski 	return 0;
999f5b64b7SGuennadi Liakhovetski }
1009f5b64b7SGuennadi Liakhovetski 
1019f5b64b7SGuennadi Liakhovetski static struct snd_soc_ops migor_dai_ops = {
1029f5b64b7SGuennadi Liakhovetski 	.hw_params = migor_hw_params,
1039f5b64b7SGuennadi Liakhovetski 	.hw_free = migor_hw_free,
1049f5b64b7SGuennadi Liakhovetski };
1059f5b64b7SGuennadi Liakhovetski 
1069f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
1079f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_HP("Headphone", NULL),
1089f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
1099f5b64b7SGuennadi Liakhovetski 	SND_SOC_DAPM_MIC("External Microphone", NULL),
1109f5b64b7SGuennadi Liakhovetski };
1119f5b64b7SGuennadi Liakhovetski 
1129f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_route audio_map[] = {
1139f5b64b7SGuennadi Liakhovetski 	/* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
1149f5b64b7SGuennadi Liakhovetski 	{ "Headphone", NULL,  "OUT4 VMID" },
1159f5b64b7SGuennadi Liakhovetski 	{ "OUT4 VMID", NULL,  "LHP" },
1169f5b64b7SGuennadi Liakhovetski 	{ "OUT4 VMID", NULL,  "RHP" },
1179f5b64b7SGuennadi Liakhovetski 
1189f5b64b7SGuennadi Liakhovetski 	/* On-board microphone */
1199f5b64b7SGuennadi Liakhovetski 	{ "RMICN", NULL, "Mic Bias" },
1209f5b64b7SGuennadi Liakhovetski 	{ "RMICP", NULL, "Mic Bias" },
1219f5b64b7SGuennadi Liakhovetski 	{ "Mic Bias", NULL, "Onboard Microphone" },
1229f5b64b7SGuennadi Liakhovetski 
1239f5b64b7SGuennadi Liakhovetski 	/* External microphone */
1249f5b64b7SGuennadi Liakhovetski 	{ "LMICN", NULL, "Mic Bias" },
1259f5b64b7SGuennadi Liakhovetski 	{ "LMICP", NULL, "Mic Bias" },
1269f5b64b7SGuennadi Liakhovetski 	{ "Mic Bias", NULL, "External Microphone" },
1279f5b64b7SGuennadi Liakhovetski };
1289f5b64b7SGuennadi Liakhovetski 
1299f5b64b7SGuennadi Liakhovetski /* migor digital audio interface glue - connects codec <--> CPU */
1309f5b64b7SGuennadi Liakhovetski static struct snd_soc_dai_link migor_dai = {
1319f5b64b7SGuennadi Liakhovetski 	.name = "wm8978",
1329f5b64b7SGuennadi Liakhovetski 	.stream_name = "WM8978",
1330a09dfa0SKuninori Morimoto 	.cpu_dai_name = "siu-pcm-audio",
134f0fba2adSLiam Girdwood 	.codec_dai_name = "wm8978-hifi",
135f0fba2adSLiam Girdwood 	.platform_name = "siu-pcm-audio",
1365250a503SMark Brown 	.codec_name = "wm8978.0-001a",
1379cb2a25cSLars-Peter Clausen 	.dai_fmt = SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_I2S |
1389cb2a25cSLars-Peter Clausen 		   SND_SOC_DAIFMT_CBS_CFS,
1399f5b64b7SGuennadi Liakhovetski 	.ops = &migor_dai_ops,
1409f5b64b7SGuennadi Liakhovetski };
1419f5b64b7SGuennadi Liakhovetski 
1429f5b64b7SGuennadi Liakhovetski /* migor audio machine driver */
1439f5b64b7SGuennadi Liakhovetski static struct snd_soc_card snd_soc_migor = {
1449f5b64b7SGuennadi Liakhovetski 	.name = "Migo-R",
1454a7042e5SAxel Lin 	.owner = THIS_MODULE,
1469f5b64b7SGuennadi Liakhovetski 	.dai_link = &migor_dai,
1479f5b64b7SGuennadi Liakhovetski 	.num_links = 1,
14823a735ccSLars-Peter Clausen 
14923a735ccSLars-Peter Clausen 	.dapm_widgets = migor_dapm_widgets,
15023a735ccSLars-Peter Clausen 	.num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets),
15123a735ccSLars-Peter Clausen 	.dapm_routes = audio_map,
15223a735ccSLars-Peter Clausen 	.num_dapm_routes = ARRAY_SIZE(audio_map),
1539f5b64b7SGuennadi Liakhovetski };
1549f5b64b7SGuennadi Liakhovetski 
1559f5b64b7SGuennadi Liakhovetski static struct platform_device *migor_snd_device;
1569f5b64b7SGuennadi Liakhovetski 
1579f5b64b7SGuennadi Liakhovetski static int __init migor_init(void)
1589f5b64b7SGuennadi Liakhovetski {
1599f5b64b7SGuennadi Liakhovetski 	int ret;
1609f5b64b7SGuennadi Liakhovetski 
1619f5b64b7SGuennadi Liakhovetski 	ret = clk_register(&siumckb_clk);
1629f5b64b7SGuennadi Liakhovetski 	if (ret < 0)
1639f5b64b7SGuennadi Liakhovetski 		return ret;
1649f5b64b7SGuennadi Liakhovetski 
165cbfa5184SGuennadi Liakhovetski 	siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL);
166cbfa5184SGuennadi Liakhovetski 	if (!siumckb_lookup) {
167cbfa5184SGuennadi Liakhovetski 		ret = -ENOMEM;
168cbfa5184SGuennadi Liakhovetski 		goto eclkdevalloc;
169cbfa5184SGuennadi Liakhovetski 	}
170cbfa5184SGuennadi Liakhovetski 	clkdev_add(siumckb_lookup);
171cbfa5184SGuennadi Liakhovetski 
1729f5b64b7SGuennadi Liakhovetski 	/* Port number used on this machine: port B */
1739f5b64b7SGuennadi Liakhovetski 	migor_snd_device = platform_device_alloc("soc-audio", 1);
1749f5b64b7SGuennadi Liakhovetski 	if (!migor_snd_device) {
1759f5b64b7SGuennadi Liakhovetski 		ret = -ENOMEM;
1769f5b64b7SGuennadi Liakhovetski 		goto epdevalloc;
1779f5b64b7SGuennadi Liakhovetski 	}
1789f5b64b7SGuennadi Liakhovetski 
179f0fba2adSLiam Girdwood 	platform_set_drvdata(migor_snd_device, &snd_soc_migor);
1809f5b64b7SGuennadi Liakhovetski 
1819f5b64b7SGuennadi Liakhovetski 	ret = platform_device_add(migor_snd_device);
1829f5b64b7SGuennadi Liakhovetski 	if (ret)
1839f5b64b7SGuennadi Liakhovetski 		goto epdevadd;
1849f5b64b7SGuennadi Liakhovetski 
1859f5b64b7SGuennadi Liakhovetski 	return 0;
1869f5b64b7SGuennadi Liakhovetski 
1879f5b64b7SGuennadi Liakhovetski epdevadd:
1889f5b64b7SGuennadi Liakhovetski 	platform_device_put(migor_snd_device);
1899f5b64b7SGuennadi Liakhovetski epdevalloc:
190cbfa5184SGuennadi Liakhovetski 	clkdev_drop(siumckb_lookup);
191cbfa5184SGuennadi Liakhovetski eclkdevalloc:
1929f5b64b7SGuennadi Liakhovetski 	clk_unregister(&siumckb_clk);
1939f5b64b7SGuennadi Liakhovetski 	return ret;
1949f5b64b7SGuennadi Liakhovetski }
1959f5b64b7SGuennadi Liakhovetski 
1969f5b64b7SGuennadi Liakhovetski static void __exit migor_exit(void)
1979f5b64b7SGuennadi Liakhovetski {
198cbfa5184SGuennadi Liakhovetski 	clkdev_drop(siumckb_lookup);
1999f5b64b7SGuennadi Liakhovetski 	clk_unregister(&siumckb_clk);
2009f5b64b7SGuennadi Liakhovetski 	platform_device_unregister(migor_snd_device);
2019f5b64b7SGuennadi Liakhovetski }
2029f5b64b7SGuennadi Liakhovetski 
2039f5b64b7SGuennadi Liakhovetski module_init(migor_init);
2049f5b64b7SGuennadi Liakhovetski module_exit(migor_exit);
2059f5b64b7SGuennadi Liakhovetski 
2069f5b64b7SGuennadi Liakhovetski MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
2079f5b64b7SGuennadi Liakhovetski MODULE_DESCRIPTION("ALSA SoC Migor");
2089f5b64b7SGuennadi Liakhovetski MODULE_LICENSE("GPL v2");
209