]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/soc/sh/migor.c
866d78fb839852f89c5db9c00f38c978979d1ec5
[mv-sheeva.git] / sound / soc / sh / migor.c
1 /*
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
11 #include <linux/device.h>
12 #include <linux/firmware.h>
13 #include <linux/module.h>
14
15 #include <asm/clock.h>
16
17 #include <cpu/sh7722.h>
18
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
23
24 #include "../codecs/wm8978.h"
25 #include "siu.h"
26
27 /* Default 8000Hz sampling frequency */
28 static unsigned long codec_freq = 8000 * 512;
29
30 static unsigned int use_count;
31
32 /* External clock, sourced from the codec at the SIUMCKB pin */
33 static unsigned long siumckb_recalc(struct clk *clk)
34 {
35         return codec_freq;
36 }
37
38 static struct clk_ops siumckb_clk_ops = {
39         .recalc = siumckb_recalc,
40 };
41
42 static struct clk siumckb_clk = {
43         .name           = "siumckb_clk",
44         .id             = -1,
45         .ops            = &siumckb_clk_ops,
46         .rate           = 0, /* initialised at run-time */
47 };
48
49 static 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;
53         struct snd_soc_dai *codec_dai = rtd->codec_dai;
54         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
62         ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
63         if (ret < 0)
64                 return ret;
65
66         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF |
67                                   SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
68         if (ret < 0)
69                 return ret;
70
71         ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF |
72                                   SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
73         if (ret < 0)
74                 return ret;
75
76         codec_freq = rate * 512;
77         /*
78          * This propagates the parent frequency change to children and
79          * recalculates the frequency table
80          */
81         clk_set_rate(&siumckb_clk, codec_freq);
82         dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
83
84         ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
85                                      codec_freq / 2, SND_SOC_CLOCK_IN);
86
87         if (!ret)
88                 use_count++;
89
90         return ret;
91 }
92
93 static int migor_hw_free(struct snd_pcm_substream *substream)
94 {
95         struct snd_soc_pcm_runtime *rtd = substream->private_data;
96         struct snd_soc_dai *codec_dai = rtd->codec_dai;
97
98         if (use_count) {
99                 use_count--;
100
101                 if (!use_count)
102                         snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
103                                                SND_SOC_CLOCK_IN);
104         } else {
105                 dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
106         }
107
108         return 0;
109 }
110
111 static struct snd_soc_ops migor_dai_ops = {
112         .hw_params = migor_hw_params,
113         .hw_free = migor_hw_free,
114 };
115
116 static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
117         SND_SOC_DAPM_HP("Headphone", NULL),
118         SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
119         SND_SOC_DAPM_MIC("External Microphone", NULL),
120 };
121
122 static const struct snd_soc_dapm_route audio_map[] = {
123         /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
124         { "Headphone", NULL,  "OUT4 VMID" },
125         { "OUT4 VMID", NULL,  "LHP" },
126         { "OUT4 VMID", NULL,  "RHP" },
127
128         /* On-board microphone */
129         { "RMICN", NULL, "Mic Bias" },
130         { "RMICP", NULL, "Mic Bias" },
131         { "Mic Bias", NULL, "Onboard Microphone" },
132
133         /* External microphone */
134         { "LMICN", NULL, "Mic Bias" },
135         { "LMICP", NULL, "Mic Bias" },
136         { "Mic Bias", NULL, "External Microphone" },
137 };
138
139 static int migor_dai_init(struct snd_soc_pcm_runtime *rtd)
140 {
141         struct snd_soc_codec *codec = rtd->codec;
142
143         snd_soc_dapm_new_controls(codec, migor_dapm_widgets,
144                                   ARRAY_SIZE(migor_dapm_widgets));
145
146         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
147
148         return 0;
149 }
150
151 /* migor digital audio interface glue - connects codec <--> CPU */
152 static struct snd_soc_dai_link migor_dai = {
153         .name = "wm8978",
154         .stream_name = "WM8978",
155         .cpu_dai_name = "siu-i2s-dai",
156         .codec_dai_name = "wm8978-hifi",
157         .platform_name = "siu-pcm-audio",
158         .codec_name = "wm8978-codec.0-001a",
159         .ops = &migor_dai_ops,
160         .init = migor_dai_init,
161 };
162
163 /* migor audio machine driver */
164 static struct snd_soc_card snd_soc_migor = {
165         .name = "Migo-R",
166         .dai_link = &migor_dai,
167         .num_links = 1,
168 };
169
170 static struct platform_device *migor_snd_device;
171
172 static int __init migor_init(void)
173 {
174         int ret;
175
176         ret = clk_register(&siumckb_clk);
177         if (ret < 0)
178                 return ret;
179
180         /* Port number used on this machine: port B */
181         migor_snd_device = platform_device_alloc("soc-audio", 1);
182         if (!migor_snd_device) {
183                 ret = -ENOMEM;
184                 goto epdevalloc;
185         }
186
187         platform_set_drvdata(migor_snd_device, &snd_soc_migor);
188
189         ret = platform_device_add(migor_snd_device);
190         if (ret)
191                 goto epdevadd;
192
193         return 0;
194
195 epdevadd:
196         platform_device_put(migor_snd_device);
197 epdevalloc:
198         clk_unregister(&siumckb_clk);
199         return ret;
200 }
201
202 static void __exit migor_exit(void)
203 {
204         clk_unregister(&siumckb_clk);
205         platform_device_unregister(migor_snd_device);
206 }
207
208 module_init(migor_init);
209 module_exit(migor_exit);
210
211 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
212 MODULE_DESCRIPTION("ALSA SoC Migor");
213 MODULE_LICENSE("GPL v2");