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 119f5b64b7SGuennadi Liakhovetski #include <linux/device.h> 129f5b64b7SGuennadi Liakhovetski #include <linux/firmware.h> 139f5b64b7SGuennadi Liakhovetski #include <linux/module.h> 149f5b64b7SGuennadi Liakhovetski 15cbfa5184SGuennadi Liakhovetski #include <asm/clkdev.h> 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 #include <sound/soc-dapm.h> 249f5b64b7SGuennadi Liakhovetski 259f5b64b7SGuennadi Liakhovetski #include "../codecs/wm8978.h" 269f5b64b7SGuennadi Liakhovetski #include "siu.h" 279f5b64b7SGuennadi Liakhovetski 289f5b64b7SGuennadi Liakhovetski /* Default 8000Hz sampling frequency */ 299f5b64b7SGuennadi Liakhovetski static unsigned long codec_freq = 8000 * 512; 309f5b64b7SGuennadi Liakhovetski 319f5b64b7SGuennadi Liakhovetski static unsigned int use_count; 329f5b64b7SGuennadi Liakhovetski 339f5b64b7SGuennadi Liakhovetski /* External clock, sourced from the codec at the SIUMCKB pin */ 349f5b64b7SGuennadi Liakhovetski static unsigned long siumckb_recalc(struct clk *clk) 359f5b64b7SGuennadi Liakhovetski { 369f5b64b7SGuennadi Liakhovetski return codec_freq; 379f5b64b7SGuennadi Liakhovetski } 389f5b64b7SGuennadi Liakhovetski 399f5b64b7SGuennadi Liakhovetski static struct clk_ops siumckb_clk_ops = { 409f5b64b7SGuennadi Liakhovetski .recalc = siumckb_recalc, 419f5b64b7SGuennadi Liakhovetski }; 429f5b64b7SGuennadi Liakhovetski 439f5b64b7SGuennadi Liakhovetski static struct clk siumckb_clk = { 449f5b64b7SGuennadi Liakhovetski .ops = &siumckb_clk_ops, 459f5b64b7SGuennadi Liakhovetski .rate = 0, /* initialised at run-time */ 469f5b64b7SGuennadi Liakhovetski }; 479f5b64b7SGuennadi Liakhovetski 48cbfa5184SGuennadi Liakhovetski static struct clk_lookup *siumckb_lookup; 49cbfa5184SGuennadi Liakhovetski 509f5b64b7SGuennadi Liakhovetski static int migor_hw_params(struct snd_pcm_substream *substream, 519f5b64b7SGuennadi Liakhovetski struct snd_pcm_hw_params *params) 529f5b64b7SGuennadi Liakhovetski { 539f5b64b7SGuennadi Liakhovetski struct snd_soc_pcm_runtime *rtd = substream->private_data; 54f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 559f5b64b7SGuennadi Liakhovetski int ret; 569f5b64b7SGuennadi Liakhovetski unsigned int rate = params_rate(params); 579f5b64b7SGuennadi Liakhovetski 589f5b64b7SGuennadi Liakhovetski ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000, 599f5b64b7SGuennadi Liakhovetski SND_SOC_CLOCK_IN); 609f5b64b7SGuennadi Liakhovetski if (ret < 0) 619f5b64b7SGuennadi Liakhovetski return ret; 629f5b64b7SGuennadi Liakhovetski 639f5b64b7SGuennadi Liakhovetski ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512); 649f5b64b7SGuennadi Liakhovetski if (ret < 0) 659f5b64b7SGuennadi Liakhovetski return ret; 669f5b64b7SGuennadi Liakhovetski 679f5b64b7SGuennadi Liakhovetski ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF | 689f5b64b7SGuennadi Liakhovetski SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); 699f5b64b7SGuennadi Liakhovetski if (ret < 0) 709f5b64b7SGuennadi Liakhovetski return ret; 719f5b64b7SGuennadi Liakhovetski 72f0fba2adSLiam Girdwood ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF | 739f5b64b7SGuennadi Liakhovetski SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); 749f5b64b7SGuennadi Liakhovetski if (ret < 0) 759f5b64b7SGuennadi Liakhovetski return ret; 769f5b64b7SGuennadi Liakhovetski 779f5b64b7SGuennadi Liakhovetski codec_freq = rate * 512; 789f5b64b7SGuennadi Liakhovetski /* 799f5b64b7SGuennadi Liakhovetski * This propagates the parent frequency change to children and 809f5b64b7SGuennadi Liakhovetski * recalculates the frequency table 819f5b64b7SGuennadi Liakhovetski */ 829f5b64b7SGuennadi Liakhovetski clk_set_rate(&siumckb_clk, codec_freq); 839f5b64b7SGuennadi Liakhovetski dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq); 849f5b64b7SGuennadi Liakhovetski 85f0fba2adSLiam Girdwood ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT, 869f5b64b7SGuennadi Liakhovetski codec_freq / 2, SND_SOC_CLOCK_IN); 879f5b64b7SGuennadi Liakhovetski 889f5b64b7SGuennadi Liakhovetski if (!ret) 899f5b64b7SGuennadi Liakhovetski use_count++; 909f5b64b7SGuennadi Liakhovetski 919f5b64b7SGuennadi Liakhovetski return ret; 929f5b64b7SGuennadi Liakhovetski } 939f5b64b7SGuennadi Liakhovetski 949f5b64b7SGuennadi Liakhovetski static int migor_hw_free(struct snd_pcm_substream *substream) 959f5b64b7SGuennadi Liakhovetski { 969f5b64b7SGuennadi Liakhovetski struct snd_soc_pcm_runtime *rtd = substream->private_data; 97f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai; 989f5b64b7SGuennadi Liakhovetski 999f5b64b7SGuennadi Liakhovetski if (use_count) { 1009f5b64b7SGuennadi Liakhovetski use_count--; 1019f5b64b7SGuennadi Liakhovetski 1029f5b64b7SGuennadi Liakhovetski if (!use_count) 1039f5b64b7SGuennadi Liakhovetski snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0, 1049f5b64b7SGuennadi Liakhovetski SND_SOC_CLOCK_IN); 1059f5b64b7SGuennadi Liakhovetski } else { 1069f5b64b7SGuennadi Liakhovetski dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n"); 1079f5b64b7SGuennadi Liakhovetski } 1089f5b64b7SGuennadi Liakhovetski 1099f5b64b7SGuennadi Liakhovetski return 0; 1109f5b64b7SGuennadi Liakhovetski } 1119f5b64b7SGuennadi Liakhovetski 1129f5b64b7SGuennadi Liakhovetski static struct snd_soc_ops migor_dai_ops = { 1139f5b64b7SGuennadi Liakhovetski .hw_params = migor_hw_params, 1149f5b64b7SGuennadi Liakhovetski .hw_free = migor_hw_free, 1159f5b64b7SGuennadi Liakhovetski }; 1169f5b64b7SGuennadi Liakhovetski 1179f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_widget migor_dapm_widgets[] = { 1189f5b64b7SGuennadi Liakhovetski SND_SOC_DAPM_HP("Headphone", NULL), 1199f5b64b7SGuennadi Liakhovetski SND_SOC_DAPM_MIC("Onboard Microphone", NULL), 1209f5b64b7SGuennadi Liakhovetski SND_SOC_DAPM_MIC("External Microphone", NULL), 1219f5b64b7SGuennadi Liakhovetski }; 1229f5b64b7SGuennadi Liakhovetski 1239f5b64b7SGuennadi Liakhovetski static const struct snd_soc_dapm_route audio_map[] = { 1249f5b64b7SGuennadi Liakhovetski /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */ 1259f5b64b7SGuennadi Liakhovetski { "Headphone", NULL, "OUT4 VMID" }, 1269f5b64b7SGuennadi Liakhovetski { "OUT4 VMID", NULL, "LHP" }, 1279f5b64b7SGuennadi Liakhovetski { "OUT4 VMID", NULL, "RHP" }, 1289f5b64b7SGuennadi Liakhovetski 1299f5b64b7SGuennadi Liakhovetski /* On-board microphone */ 1309f5b64b7SGuennadi Liakhovetski { "RMICN", NULL, "Mic Bias" }, 1319f5b64b7SGuennadi Liakhovetski { "RMICP", NULL, "Mic Bias" }, 1329f5b64b7SGuennadi Liakhovetski { "Mic Bias", NULL, "Onboard Microphone" }, 1339f5b64b7SGuennadi Liakhovetski 1349f5b64b7SGuennadi Liakhovetski /* External microphone */ 1359f5b64b7SGuennadi Liakhovetski { "LMICN", NULL, "Mic Bias" }, 1369f5b64b7SGuennadi Liakhovetski { "LMICP", NULL, "Mic Bias" }, 1379f5b64b7SGuennadi Liakhovetski { "Mic Bias", NULL, "External Microphone" }, 1389f5b64b7SGuennadi Liakhovetski }; 1399f5b64b7SGuennadi Liakhovetski 140f0fba2adSLiam Girdwood static int migor_dai_init(struct snd_soc_pcm_runtime *rtd) 1419f5b64b7SGuennadi Liakhovetski { 142f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 143f0fba2adSLiam Girdwood 1449f5b64b7SGuennadi Liakhovetski snd_soc_dapm_new_controls(codec, migor_dapm_widgets, 1459f5b64b7SGuennadi Liakhovetski ARRAY_SIZE(migor_dapm_widgets)); 1469f5b64b7SGuennadi Liakhovetski 1479f5b64b7SGuennadi Liakhovetski snd_soc_dapm_add_routes(codec, 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", 1679f5b64b7SGuennadi Liakhovetski .dai_link = &migor_dai, 1689f5b64b7SGuennadi Liakhovetski .num_links = 1, 1699f5b64b7SGuennadi Liakhovetski }; 1709f5b64b7SGuennadi Liakhovetski 1719f5b64b7SGuennadi Liakhovetski static struct platform_device *migor_snd_device; 1729f5b64b7SGuennadi Liakhovetski 1739f5b64b7SGuennadi Liakhovetski static int __init migor_init(void) 1749f5b64b7SGuennadi Liakhovetski { 1759f5b64b7SGuennadi Liakhovetski int ret; 1769f5b64b7SGuennadi Liakhovetski 1779f5b64b7SGuennadi Liakhovetski ret = clk_register(&siumckb_clk); 1789f5b64b7SGuennadi Liakhovetski if (ret < 0) 1799f5b64b7SGuennadi Liakhovetski return ret; 1809f5b64b7SGuennadi Liakhovetski 181cbfa5184SGuennadi Liakhovetski siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL); 182cbfa5184SGuennadi Liakhovetski if (!siumckb_lookup) { 183cbfa5184SGuennadi Liakhovetski ret = -ENOMEM; 184cbfa5184SGuennadi Liakhovetski goto eclkdevalloc; 185cbfa5184SGuennadi Liakhovetski } 186cbfa5184SGuennadi Liakhovetski clkdev_add(siumckb_lookup); 187cbfa5184SGuennadi Liakhovetski 1889f5b64b7SGuennadi Liakhovetski /* Port number used on this machine: port B */ 1899f5b64b7SGuennadi Liakhovetski migor_snd_device = platform_device_alloc("soc-audio", 1); 1909f5b64b7SGuennadi Liakhovetski if (!migor_snd_device) { 1919f5b64b7SGuennadi Liakhovetski ret = -ENOMEM; 1929f5b64b7SGuennadi Liakhovetski goto epdevalloc; 1939f5b64b7SGuennadi Liakhovetski } 1949f5b64b7SGuennadi Liakhovetski 195f0fba2adSLiam Girdwood platform_set_drvdata(migor_snd_device, &snd_soc_migor); 1969f5b64b7SGuennadi Liakhovetski 1979f5b64b7SGuennadi Liakhovetski ret = platform_device_add(migor_snd_device); 1989f5b64b7SGuennadi Liakhovetski if (ret) 1999f5b64b7SGuennadi Liakhovetski goto epdevadd; 2009f5b64b7SGuennadi Liakhovetski 2019f5b64b7SGuennadi Liakhovetski return 0; 2029f5b64b7SGuennadi Liakhovetski 2039f5b64b7SGuennadi Liakhovetski epdevadd: 2049f5b64b7SGuennadi Liakhovetski platform_device_put(migor_snd_device); 2059f5b64b7SGuennadi Liakhovetski epdevalloc: 206cbfa5184SGuennadi Liakhovetski clkdev_drop(siumckb_lookup); 207cbfa5184SGuennadi Liakhovetski eclkdevalloc: 2089f5b64b7SGuennadi Liakhovetski clk_unregister(&siumckb_clk); 2099f5b64b7SGuennadi Liakhovetski return ret; 2109f5b64b7SGuennadi Liakhovetski } 2119f5b64b7SGuennadi Liakhovetski 2129f5b64b7SGuennadi Liakhovetski static void __exit migor_exit(void) 2139f5b64b7SGuennadi Liakhovetski { 214cbfa5184SGuennadi Liakhovetski clkdev_drop(siumckb_lookup); 2159f5b64b7SGuennadi Liakhovetski clk_unregister(&siumckb_clk); 2169f5b64b7SGuennadi Liakhovetski platform_device_unregister(migor_snd_device); 2179f5b64b7SGuennadi Liakhovetski } 2189f5b64b7SGuennadi Liakhovetski 2199f5b64b7SGuennadi Liakhovetski module_init(migor_init); 2209f5b64b7SGuennadi Liakhovetski module_exit(migor_exit); 2219f5b64b7SGuennadi Liakhovetski 2229f5b64b7SGuennadi Liakhovetski MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); 2239f5b64b7SGuennadi Liakhovetski MODULE_DESCRIPTION("ALSA SoC Migor"); 2249f5b64b7SGuennadi Liakhovetski MODULE_LICENSE("GPL v2"); 225