blob: 2cda82bc5d2e4362e2a3941428ad1eaf9802934e [file] [log] [blame]
Daniel Mack49af5742009-11-27 13:47:10 +01001/*
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
Daniel Mack49af5742009-11-27 13:47:10 +010029#include "pxa-ssp.h"
30
31#define GPIO_SPDIF_RESET (38)
32#define GPIO_MCLK_RESET (111)
33#define GPIO_CODEC_RESET (120)
34
35static struct i2c_client *max9486_client;
36static struct i2c_board_info max9486_hwmon_info = {
37 I2C_BOARD_INFO("max9485", 0x63),
38};
39
40#define MAX9485_MCLK_FREQ_112896 0x22
Daniel Macka4212962010-01-15 17:36:49 +010041#define MAX9485_MCLK_FREQ_122880 0x23
42#define MAX9485_MCLK_FREQ_225792 0x32
43#define MAX9485_MCLK_FREQ_245760 0x33
Daniel Mack49af5742009-11-27 13:47:10 +010044
45static void set_max9485_clk(char clk)
46{
47 i2c_master_send(max9486_client, &clk, 1);
48}
49
50static 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 */
68static int raumfeld_cs4270_startup(struct snd_pcm_substream *substream)
69{
70 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000071 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Daniel Mack49af5742009-11-27 13:47:10 +010072
Daniel Macka4212962010-01-15 17:36:49 +010073 /* set freq to 0 to enable all possible codec sample rates */
74 return snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
75}
Daniel Mack49af5742009-11-27 13:47:10 +010076
Daniel Macka4212962010-01-15 17:36:49 +010077static void raumfeld_cs4270_shutdown(struct snd_pcm_substream *substream)
78{
79 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000080 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Daniel Macka4212962010-01-15 17:36:49 +010081
82 /* set freq to 0 to enable all possible codec sample rates */
83 snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
Daniel Mack49af5742009-11-27 13:47:10 +010084}
85
86static 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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000090 struct snd_soc_dai *codec_dai = rtd->codec_dai;
91 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Daniel Mack49af5742009-11-27 13:47:10 +010092 unsigned int fmt, clk = 0;
93 int ret = 0;
94
95 switch (params_rate(params)) {
Daniel Mack49af5742009-11-27 13:47:10 +010096 case 44100:
Daniel Mack49af5742009-11-27 13:47:10 +010097 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
98 clk = 11289600;
99 break;
Daniel Macka4212962010-01-15 17:36:49 +0100100 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;
Daniel Mack49af5742009-11-27 13:47:10 +0100114 }
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 */
Daniel Macka649d1f2009-11-30 14:06:37 +0100130 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
Daniel Mack49af5742009-11-27 13:47:10 +0100131 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
Daniel Macka4212962010-01-15 17:36:49 +0100142 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
Daniel Mack49af5742009-11-27 13:47:10 +0100143 if (ret < 0)
144 return ret;
145
146 return 0;
147}
148
149static struct snd_soc_ops raumfeld_cs4270_ops = {
150 .startup = raumfeld_cs4270_startup,
Daniel Macka4212962010-01-15 17:36:49 +0100151 .shutdown = raumfeld_cs4270_shutdown,
Daniel Mack49af5742009-11-27 13:47:10 +0100152 .hw_params = raumfeld_cs4270_hw_params,
153};
154
155static int raumfeld_line_suspend(struct platform_device *pdev, pm_message_t state)
156{
157 raumfeld_enable_audio(false);
158 return 0;
159}
160
161static int raumfeld_line_resume(struct platform_device *pdev)
162{
163 raumfeld_enable_audio(true);
164 return 0;
165}
166
Daniel Mack49af5742009-11-27 13:47:10 +0100167/* AK4104 */
168
169static 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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173 struct snd_soc_dai *codec_dai = rtd->codec_dai;
174 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Daniel Mack49af5742009-11-27 13:47:10 +0100175 int fmt, ret = 0, clk = 0;
176
177 switch (params_rate(params)) {
Daniel Mack49af5742009-11-27 13:47:10 +0100178 case 44100:
Daniel Mack49af5742009-11-27 13:47:10 +0100179 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
180 clk = 11289600;
181 break;
Daniel Macka4212962010-01-15 17:36:49 +0100182 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;
Daniel Mack49af5742009-11-27 13:47:10 +0100196 }
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 */
Daniel Macka649d1f2009-11-30 14:06:37 +0100206 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
Daniel Mack49af5742009-11-27 13:47:10 +0100207 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
Daniel Macka4212962010-01-15 17:36:49 +0100218 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
Daniel Mack49af5742009-11-27 13:47:10 +0100219 if (ret < 0)
220 return ret;
221
222 return 0;
223}
224
225static struct snd_soc_ops raumfeld_ak4104_ops = {
226 .hw_params = raumfeld_ak4104_hw_params,
227};
228
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000229static struct snd_soc_dai_link raumfeld_dai[] = {
230{
Daniel Mack49af5742009-11-27 13:47:10 +0100231 .name = "ak4104",
232 .stream_name = "Playback",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000233 .cpu_dai_name = "pxa-ssp-dai.1",
234 .codec_dai_name = "ak4104-hifi",
235 .platform_name = "pxa-pcm-audio",
Daniel Mack49af5742009-11-27 13:47:10 +0100236 .ops = &raumfeld_ak4104_ops,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000237 .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
249static 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),
Daniel Mack49af5742009-11-27 13:47:10 +0100255};
256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000257static struct platform_device *raumfeld_audio_device;
Daniel Mack49af5742009-11-27 13:47:10 +0100258
259static 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275 /* Register LINE and SPDIF */
276 raumfeld_audio_device = platform_device_alloc("soc-audio", 0);
277 if (!raumfeld_audio_device)
Daniel Mack49af5742009-11-27 13:47:10 +0100278 return -ENOMEM;
279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 platform_set_drvdata(raumfeld_audio_device,
281 &snd_soc_raumfeld);
282 ret = platform_device_add(raumfeld_audio_device);
Daniel Mack49af5742009-11-27 13:47:10 +0100283
284 /* no S/PDIF on Speakers */
285 if (machine_is_raumfeld_speaker())
286 return ret;
287
Daniel Mack49af5742009-11-27 13:47:10 +0100288 raumfeld_enable_audio(true);
289
290 return ret;
291}
292
293static void __exit raumfeld_audio_exit(void)
294{
295 raumfeld_enable_audio(false);
296
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000297 platform_device_unregister(raumfeld_audio_device);
Daniel Mack49af5742009-11-27 13:47:10 +0100298
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
306module_init(raumfeld_audio_init);
307module_exit(raumfeld_audio_exit);
308
309/* Module information */
310MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
311MODULE_DESCRIPTION("Raumfeld audio SoC");
312MODULE_LICENSE("GPL");