blob: 82f582344fe7e1dfce0e39af7c69289c5c4a3a3d [file] [log] [blame]
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +01001/*
2 * ALSA SoC driver for Migo-R
3 *
4 * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Guennadi Liakhovetskif4fd58d2011-01-11 08:34:52 +010011#include <linux/clkdev.h>
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010012#include <linux/device.h>
13#include <linux/firmware.h>
14#include <linux/module.h>
15
16#include <asm/clock.h>
17
18#include <cpu/sh7722.h>
19
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/soc.h>
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010023
24#include "../codecs/wm8978.h"
25#include "siu.h"
26
27/* Default 8000Hz sampling frequency */
28static unsigned long codec_freq = 8000 * 512;
29
30static unsigned int use_count;
31
32/* External clock, sourced from the codec at the SIUMCKB pin */
33static unsigned long siumckb_recalc(struct clk *clk)
34{
35 return codec_freq;
36}
37
Guennadi Liakhovetskic8587192012-05-09 12:57:05 +020038static struct sh_clk_ops siumckb_clk_ops = {
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010039 .recalc = siumckb_recalc,
40};
41
42static struct clk siumckb_clk = {
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010043 .ops = &siumckb_clk_ops,
44 .rate = 0, /* initialised at run-time */
45};
46
Guennadi Liakhovetskicbfa5182010-09-17 12:30:11 +020047static struct clk_lookup *siumckb_lookup;
48
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010049static int migor_hw_params(struct snd_pcm_substream *substream,
50 struct snd_pcm_hw_params *params)
51{
52 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000053 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010054 int ret;
55 unsigned int rate = params_rate(params);
56
57 ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000,
58 SND_SOC_CLOCK_IN);
59 if (ret < 0)
60 return ret;
61
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010062 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
63 if (ret < 0)
64 return ret;
65
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010066 codec_freq = rate * 512;
67 /*
68 * This propagates the parent frequency change to children and
69 * recalculates the frequency table
70 */
71 clk_set_rate(&siumckb_clk, codec_freq);
72 dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
73
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000074 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010075 codec_freq / 2, SND_SOC_CLOCK_IN);
76
77 if (!ret)
78 use_count++;
79
80 return ret;
81}
82
83static int migor_hw_free(struct snd_pcm_substream *substream)
84{
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000086 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +010087
88 if (use_count) {
89 use_count--;
90
91 if (!use_count)
92 snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
93 SND_SOC_CLOCK_IN);
94 } else {
95 dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
96 }
97
98 return 0;
99}
100
101static struct snd_soc_ops migor_dai_ops = {
102 .hw_params = migor_hw_params,
103 .hw_free = migor_hw_free,
104};
105
106static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
107 SND_SOC_DAPM_HP("Headphone", NULL),
108 SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
109 SND_SOC_DAPM_MIC("External Microphone", NULL),
110};
111
112static const struct snd_soc_dapm_route audio_map[] = {
113 /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
114 { "Headphone", NULL, "OUT4 VMID" },
115 { "OUT4 VMID", NULL, "LHP" },
116 { "OUT4 VMID", NULL, "RHP" },
117
118 /* On-board microphone */
119 { "RMICN", NULL, "Mic Bias" },
120 { "RMICP", NULL, "Mic Bias" },
121 { "Mic Bias", NULL, "Onboard Microphone" },
122
123 /* External microphone */
124 { "LMICN", NULL, "Mic Bias" },
125 { "LMICP", NULL, "Mic Bias" },
126 { "Mic Bias", NULL, "External Microphone" },
127};
128
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100129/* migor digital audio interface glue - connects codec <--> CPU */
130static struct snd_soc_dai_link migor_dai = {
131 .name = "wm8978",
132 .stream_name = "WM8978",
Kuninori Morimoto0a09dfa2013-03-22 00:54:50 -0700133 .cpu_dai_name = "siu-pcm-audio",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000134 .codec_dai_name = "wm8978-hifi",
135 .platform_name = "siu-pcm-audio",
Mark Brown5250a502010-09-19 16:07:06 +0100136 .codec_name = "wm8978.0-001a",
Lars-Peter Clausen9cb2a252015-01-01 17:16:30 +0100137 .dai_fmt = SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_I2S |
138 SND_SOC_DAIFMT_CBS_CFS,
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100139 .ops = &migor_dai_ops,
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100140};
141
142/* migor audio machine driver */
143static struct snd_soc_card snd_soc_migor = {
144 .name = "Migo-R",
Axel Lin4a7042e2011-12-23 14:53:32 +0800145 .owner = THIS_MODULE,
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100146 .dai_link = &migor_dai,
147 .num_links = 1,
Lars-Peter Clausen23a735c2014-03-03 08:14:14 +0100148
149 .dapm_widgets = migor_dapm_widgets,
150 .num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets),
151 .dapm_routes = audio_map,
152 .num_dapm_routes = ARRAY_SIZE(audio_map),
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100153};
154
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100155static struct platform_device *migor_snd_device;
156
157static int __init migor_init(void)
158{
159 int ret;
160
161 ret = clk_register(&siumckb_clk);
162 if (ret < 0)
163 return ret;
164
Guennadi Liakhovetskicbfa5182010-09-17 12:30:11 +0200165 siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL);
166 if (!siumckb_lookup) {
167 ret = -ENOMEM;
168 goto eclkdevalloc;
169 }
170 clkdev_add(siumckb_lookup);
171
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100172 /* Port number used on this machine: port B */
173 migor_snd_device = platform_device_alloc("soc-audio", 1);
174 if (!migor_snd_device) {
175 ret = -ENOMEM;
176 goto epdevalloc;
177 }
178
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000179 platform_set_drvdata(migor_snd_device, &snd_soc_migor);
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100180
181 ret = platform_device_add(migor_snd_device);
182 if (ret)
183 goto epdevadd;
184
185 return 0;
186
187epdevadd:
188 platform_device_put(migor_snd_device);
189epdevalloc:
Guennadi Liakhovetskicbfa5182010-09-17 12:30:11 +0200190 clkdev_drop(siumckb_lookup);
191eclkdevalloc:
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100192 clk_unregister(&siumckb_clk);
193 return ret;
194}
195
196static void __exit migor_exit(void)
197{
Guennadi Liakhovetskicbfa5182010-09-17 12:30:11 +0200198 clkdev_drop(siumckb_lookup);
Guennadi Liakhovetski9f5b64b2010-01-27 12:15:00 +0100199 clk_unregister(&siumckb_clk);
200 platform_device_unregister(migor_snd_device);
201}
202
203module_init(migor_init);
204module_exit(migor_exit);
205
206MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
207MODULE_DESCRIPTION("ALSA SoC Migor");
208MODULE_LICENSE("GPL v2");