Merge branch 'x86/numa' into x86/urgent
[deliverable/linux.git] / sound / soc / pxa / raumfeld.c
1 /*
2 * raumfeld_audio.c -- SoC audio for Raumfeld audio devices
3 *
4 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
5 *
6 * based on code from:
7 *
8 * Wolfson Microelectronics PLC.
9 * Openedhand Ltd.
10 * Liam Girdwood <lrg@slimlogic.co.uk>
11 * Richard Purdie <richard@openedhand.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <sound/pcm.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26
27 #include <asm/mach-types.h>
28
29 #include "pxa-ssp.h"
30
31 #define GPIO_SPDIF_RESET (38)
32 #define GPIO_MCLK_RESET (111)
33 #define GPIO_CODEC_RESET (120)
34
35 static struct i2c_client *max9486_client;
36 static struct i2c_board_info max9486_hwmon_info = {
37 I2C_BOARD_INFO("max9485", 0x63),
38 };
39
40 #define MAX9485_MCLK_FREQ_112896 0x22
41 #define MAX9485_MCLK_FREQ_122880 0x23
42 #define MAX9485_MCLK_FREQ_225792 0x32
43 #define MAX9485_MCLK_FREQ_245760 0x33
44
45 static void set_max9485_clk(char clk)
46 {
47 i2c_master_send(max9486_client, &clk, 1);
48 }
49
50 static void raumfeld_enable_audio(bool en)
51 {
52 if (en) {
53 gpio_set_value(GPIO_MCLK_RESET, 1);
54
55 /* wait some time to let the clocks become stable */
56 msleep(100);
57
58 gpio_set_value(GPIO_SPDIF_RESET, 1);
59 gpio_set_value(GPIO_CODEC_RESET, 1);
60 } else {
61 gpio_set_value(GPIO_MCLK_RESET, 0);
62 gpio_set_value(GPIO_SPDIF_RESET, 0);
63 gpio_set_value(GPIO_CODEC_RESET, 0);
64 }
65 }
66
67 /* CS4270 */
68 static int raumfeld_cs4270_startup(struct snd_pcm_substream *substream)
69 {
70 struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 struct snd_soc_dai *codec_dai = rtd->codec_dai;
72
73 /* set freq to 0 to enable all possible codec sample rates */
74 return snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
75 }
76
77 static void raumfeld_cs4270_shutdown(struct snd_pcm_substream *substream)
78 {
79 struct snd_soc_pcm_runtime *rtd = substream->private_data;
80 struct snd_soc_dai *codec_dai = rtd->codec_dai;
81
82 /* set freq to 0 to enable all possible codec sample rates */
83 snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
84 }
85
86 static int raumfeld_cs4270_hw_params(struct snd_pcm_substream *substream,
87 struct snd_pcm_hw_params *params)
88 {
89 struct snd_soc_pcm_runtime *rtd = substream->private_data;
90 struct snd_soc_dai *codec_dai = rtd->codec_dai;
91 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
92 unsigned int fmt, clk = 0;
93 int ret = 0;
94
95 switch (params_rate(params)) {
96 case 44100:
97 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
98 clk = 11289600;
99 break;
100 case 48000:
101 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
102 clk = 12288000;
103 break;
104 case 88200:
105 set_max9485_clk(MAX9485_MCLK_FREQ_225792);
106 clk = 22579200;
107 break;
108 case 96000:
109 set_max9485_clk(MAX9485_MCLK_FREQ_245760);
110 clk = 24576000;
111 break;
112 default:
113 return -EINVAL;
114 }
115
116 fmt = SND_SOC_DAIFMT_I2S |
117 SND_SOC_DAIFMT_NB_NF |
118 SND_SOC_DAIFMT_CBS_CFS;
119
120 /* setup the CODEC DAI */
121 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
122 if (ret < 0)
123 return ret;
124
125 ret = snd_soc_dai_set_sysclk(codec_dai, 0, clk, 0);
126 if (ret < 0)
127 return ret;
128
129 /* setup the CPU DAI */
130 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
131 if (ret < 0)
132 return ret;
133
134 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
135 if (ret < 0)
136 return ret;
137
138 ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
139 if (ret < 0)
140 return ret;
141
142 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
143 if (ret < 0)
144 return ret;
145
146 return 0;
147 }
148
149 static struct snd_soc_ops raumfeld_cs4270_ops = {
150 .startup = raumfeld_cs4270_startup,
151 .shutdown = raumfeld_cs4270_shutdown,
152 .hw_params = raumfeld_cs4270_hw_params,
153 };
154
155 static int raumfeld_line_suspend(struct platform_device *pdev, pm_message_t state)
156 {
157 raumfeld_enable_audio(false);
158 return 0;
159 }
160
161 static int raumfeld_line_resume(struct platform_device *pdev)
162 {
163 raumfeld_enable_audio(true);
164 return 0;
165 }
166
167 /* AK4104 */
168
169 static int raumfeld_ak4104_hw_params(struct snd_pcm_substream *substream,
170 struct snd_pcm_hw_params *params)
171 {
172 struct snd_soc_pcm_runtime *rtd = substream->private_data;
173 struct snd_soc_dai *codec_dai = rtd->codec_dai;
174 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
175 int fmt, ret = 0, clk = 0;
176
177 switch (params_rate(params)) {
178 case 44100:
179 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
180 clk = 11289600;
181 break;
182 case 48000:
183 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
184 clk = 12288000;
185 break;
186 case 88200:
187 set_max9485_clk(MAX9485_MCLK_FREQ_225792);
188 clk = 22579200;
189 break;
190 case 96000:
191 set_max9485_clk(MAX9485_MCLK_FREQ_245760);
192 clk = 24576000;
193 break;
194 default:
195 return -EINVAL;
196 }
197
198 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF;
199
200 /* setup the CODEC DAI */
201 ret = snd_soc_dai_set_fmt(codec_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
202 if (ret < 0)
203 return ret;
204
205 /* setup the CPU DAI */
206 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
207 if (ret < 0)
208 return ret;
209
210 ret = snd_soc_dai_set_fmt(cpu_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
211 if (ret < 0)
212 return ret;
213
214 ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
215 if (ret < 0)
216 return ret;
217
218 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
219 if (ret < 0)
220 return ret;
221
222 return 0;
223 }
224
225 static struct snd_soc_ops raumfeld_ak4104_ops = {
226 .hw_params = raumfeld_ak4104_hw_params,
227 };
228
229 static struct snd_soc_dai_link raumfeld_dai[] = {
230 {
231 .name = "ak4104",
232 .stream_name = "Playback",
233 .cpu_dai_name = "pxa-ssp-dai.1",
234 .codec_dai_name = "ak4104-hifi",
235 .platform_name = "pxa-pcm-audio",
236 .ops = &raumfeld_ak4104_ops,
237 .codec_name = "ak4104-codec.0",
238 },
239 {
240 .name = "CS4270",
241 .stream_name = "CS4270",
242 .cpu_dai_name = "pxa-ssp-dai.0",
243 .platform_name = "pxa-pcm-audio",
244 .codec_dai_name = "cs4270-hifi",
245 .codec_name = "cs4270-codec.0-0048",
246 .ops = &raumfeld_cs4270_ops,
247 },};
248
249 static struct snd_soc_card snd_soc_raumfeld = {
250 .name = "Raumfeld",
251 .dai_link = raumfeld_dai,
252 .suspend_post = raumfeld_line_suspend,
253 .resume_pre = raumfeld_line_resume,
254 .num_links = ARRAY_SIZE(raumfeld_dai),
255 };
256
257 static struct platform_device *raumfeld_audio_device;
258
259 static int __init raumfeld_audio_init(void)
260 {
261 int ret;
262
263 if (!machine_is_raumfeld_speaker() &&
264 !machine_is_raumfeld_connector())
265 return 0;
266
267 max9486_client = i2c_new_device(i2c_get_adapter(0),
268 &max9486_hwmon_info);
269
270 if (!max9486_client)
271 return -ENOMEM;
272
273 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
274
275 /* Register LINE and SPDIF */
276 raumfeld_audio_device = platform_device_alloc("soc-audio", 0);
277 if (!raumfeld_audio_device)
278 return -ENOMEM;
279
280 platform_set_drvdata(raumfeld_audio_device,
281 &snd_soc_raumfeld);
282 ret = platform_device_add(raumfeld_audio_device);
283
284 /* no S/PDIF on Speakers */
285 if (machine_is_raumfeld_speaker())
286 return ret;
287
288 raumfeld_enable_audio(true);
289
290 return ret;
291 }
292
293 static void __exit raumfeld_audio_exit(void)
294 {
295 raumfeld_enable_audio(false);
296
297 platform_device_unregister(raumfeld_audio_device);
298
299 i2c_unregister_device(max9486_client);
300
301 gpio_free(GPIO_MCLK_RESET);
302 gpio_free(GPIO_CODEC_RESET);
303 gpio_free(GPIO_SPDIF_RESET);
304 }
305
306 module_init(raumfeld_audio_init);
307 module_exit(raumfeld_audio_exit);
308
309 /* Module information */
310 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
311 MODULE_DESCRIPTION("Raumfeld audio SoC");
312 MODULE_LICENSE("GPL");
This page took 0.044066 seconds and 5 git commands to generate.