blob: b899a3bc8f42356cce6e9c6f1629bf0402f5706d [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>
Daniel Mack49af5742009-11-27 13:47:10 +010025
26#include <asm/mach-types.h>
27
Daniel Mack49af5742009-11-27 13:47:10 +010028#include "pxa-ssp.h"
29
30#define GPIO_SPDIF_RESET (38)
31#define GPIO_MCLK_RESET (111)
32#define GPIO_CODEC_RESET (120)
33
34static struct i2c_client *max9486_client;
35static struct i2c_board_info max9486_hwmon_info = {
36 I2C_BOARD_INFO("max9485", 0x63),
37};
38
39#define MAX9485_MCLK_FREQ_112896 0x22
Daniel Macka4212962010-01-15 17:36:49 +010040#define MAX9485_MCLK_FREQ_122880 0x23
41#define MAX9485_MCLK_FREQ_225792 0x32
42#define MAX9485_MCLK_FREQ_245760 0x33
Daniel Mack49af5742009-11-27 13:47:10 +010043
44static void set_max9485_clk(char clk)
45{
46 i2c_master_send(max9486_client, &clk, 1);
47}
48
49static void raumfeld_enable_audio(bool en)
50{
51 if (en) {
52 gpio_set_value(GPIO_MCLK_RESET, 1);
53
54 /* wait some time to let the clocks become stable */
55 msleep(100);
56
57 gpio_set_value(GPIO_SPDIF_RESET, 1);
58 gpio_set_value(GPIO_CODEC_RESET, 1);
59 } else {
60 gpio_set_value(GPIO_MCLK_RESET, 0);
61 gpio_set_value(GPIO_SPDIF_RESET, 0);
62 gpio_set_value(GPIO_CODEC_RESET, 0);
63 }
64}
65
66/* CS4270 */
67static int raumfeld_cs4270_startup(struct snd_pcm_substream *substream)
68{
69 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000070 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Daniel Mack49af5742009-11-27 13:47:10 +010071
Daniel Macka4212962010-01-15 17:36:49 +010072 /* set freq to 0 to enable all possible codec sample rates */
73 return snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
74}
Daniel Mack49af5742009-11-27 13:47:10 +010075
Daniel Macka4212962010-01-15 17:36:49 +010076static void raumfeld_cs4270_shutdown(struct snd_pcm_substream *substream)
77{
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000079 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Daniel Macka4212962010-01-15 17:36:49 +010080
81 /* set freq to 0 to enable all possible codec sample rates */
82 snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
Daniel Mack49af5742009-11-27 13:47:10 +010083}
84
85static int raumfeld_cs4270_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
87{
88 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 struct snd_soc_dai *codec_dai = rtd->codec_dai;
90 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Daniel Mack49af5742009-11-27 13:47:10 +010091 unsigned int fmt, clk = 0;
92 int ret = 0;
93
94 switch (params_rate(params)) {
Daniel Mack49af5742009-11-27 13:47:10 +010095 case 44100:
Daniel Mack49af5742009-11-27 13:47:10 +010096 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
97 clk = 11289600;
98 break;
Daniel Macka4212962010-01-15 17:36:49 +010099 case 48000:
100 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
101 clk = 12288000;
102 break;
103 case 88200:
104 set_max9485_clk(MAX9485_MCLK_FREQ_225792);
105 clk = 22579200;
106 break;
107 case 96000:
108 set_max9485_clk(MAX9485_MCLK_FREQ_245760);
109 clk = 24576000;
110 break;
111 default:
112 return -EINVAL;
Daniel Mack49af5742009-11-27 13:47:10 +0100113 }
114
115 fmt = SND_SOC_DAIFMT_I2S |
116 SND_SOC_DAIFMT_NB_NF |
117 SND_SOC_DAIFMT_CBS_CFS;
118
119 /* setup the CODEC DAI */
120 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
121 if (ret < 0)
122 return ret;
123
124 ret = snd_soc_dai_set_sysclk(codec_dai, 0, clk, 0);
125 if (ret < 0)
126 return ret;
127
128 /* setup the CPU DAI */
Daniel Macka649d1f2009-11-30 14:06:37 +0100129 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
Daniel Mack49af5742009-11-27 13:47:10 +0100130 if (ret < 0)
131 return ret;
132
133 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
134 if (ret < 0)
135 return ret;
136
137 ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
138 if (ret < 0)
139 return ret;
140
Daniel Macka4212962010-01-15 17:36:49 +0100141 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
Daniel Mack49af5742009-11-27 13:47:10 +0100142 if (ret < 0)
143 return ret;
144
145 return 0;
146}
147
148static struct snd_soc_ops raumfeld_cs4270_ops = {
149 .startup = raumfeld_cs4270_startup,
Daniel Macka4212962010-01-15 17:36:49 +0100150 .shutdown = raumfeld_cs4270_shutdown,
Daniel Mack49af5742009-11-27 13:47:10 +0100151 .hw_params = raumfeld_cs4270_hw_params,
152};
153
Daniel Mack477a6692011-05-24 14:10:32 +0200154static int raumfeld_analog_suspend(struct snd_soc_card *card)
Daniel Mack49af5742009-11-27 13:47:10 +0100155{
156 raumfeld_enable_audio(false);
157 return 0;
158}
159
Daniel Mack477a6692011-05-24 14:10:32 +0200160static int raumfeld_analog_resume(struct snd_soc_card *card)
Daniel Mack49af5742009-11-27 13:47:10 +0100161{
162 raumfeld_enable_audio(true);
163 return 0;
164}
165
Daniel Mack49af5742009-11-27 13:47:10 +0100166/* AK4104 */
167
168static int raumfeld_ak4104_hw_params(struct snd_pcm_substream *substream,
169 struct snd_pcm_hw_params *params)
170{
171 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172 struct snd_soc_dai *codec_dai = rtd->codec_dai;
173 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Daniel Mack49af5742009-11-27 13:47:10 +0100174 int fmt, ret = 0, clk = 0;
175
176 switch (params_rate(params)) {
Daniel Mack49af5742009-11-27 13:47:10 +0100177 case 44100:
Daniel Mack49af5742009-11-27 13:47:10 +0100178 set_max9485_clk(MAX9485_MCLK_FREQ_112896);
179 clk = 11289600;
180 break;
Daniel Macka4212962010-01-15 17:36:49 +0100181 case 48000:
182 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
183 clk = 12288000;
184 break;
185 case 88200:
186 set_max9485_clk(MAX9485_MCLK_FREQ_225792);
187 clk = 22579200;
188 break;
189 case 96000:
190 set_max9485_clk(MAX9485_MCLK_FREQ_245760);
191 clk = 24576000;
192 break;
193 default:
194 return -EINVAL;
Daniel Mack49af5742009-11-27 13:47:10 +0100195 }
196
197 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF;
198
199 /* setup the CODEC DAI */
200 ret = snd_soc_dai_set_fmt(codec_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
201 if (ret < 0)
202 return ret;
203
204 /* setup the CPU DAI */
Daniel Macka649d1f2009-11-30 14:06:37 +0100205 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
Daniel Mack49af5742009-11-27 13:47:10 +0100206 if (ret < 0)
207 return ret;
208
209 ret = snd_soc_dai_set_fmt(cpu_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
210 if (ret < 0)
211 return ret;
212
213 ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
214 if (ret < 0)
215 return ret;
216
Daniel Macka4212962010-01-15 17:36:49 +0100217 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
Daniel Mack49af5742009-11-27 13:47:10 +0100218 if (ret < 0)
219 return ret;
220
221 return 0;
222}
223
224static struct snd_soc_ops raumfeld_ak4104_ops = {
225 .hw_params = raumfeld_ak4104_hw_params,
226};
227
Daniel Mack477a6692011-05-24 14:10:32 +0200228#define DAI_LINK_CS4270 \
229{ \
230 .name = "CS4270", \
231 .stream_name = "CS4270", \
232 .cpu_dai_name = "pxa-ssp-dai.0", \
233 .platform_name = "pxa-pcm-audio", \
234 .codec_dai_name = "cs4270-hifi", \
235 .codec_name = "cs4270-codec.0-0048", \
236 .ops = &raumfeld_cs4270_ops, \
237}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000238
Daniel Mack477a6692011-05-24 14:10:32 +0200239#define DAI_LINK_AK4104 \
240{ \
241 .name = "ak4104", \
242 .stream_name = "Playback", \
243 .cpu_dai_name = "pxa-ssp-dai.1", \
244 .codec_dai_name = "ak4104-hifi", \
245 .platform_name = "pxa-pcm-audio", \
246 .ops = &raumfeld_ak4104_ops, \
247 .codec_name = "spi0.0", \
248}
249
250static struct snd_soc_dai_link snd_soc_raumfeld_connector_dai[] =
251{
252 DAI_LINK_CS4270,
253 DAI_LINK_AK4104,
254};
255
256static struct snd_soc_dai_link snd_soc_raumfeld_speaker_dai[] =
257{
258 DAI_LINK_CS4270,
259};
260
261static struct snd_soc_card snd_soc_raumfeld_connector = {
262 .name = "Raumfeld Connector",
263 .dai_link = snd_soc_raumfeld_connector_dai,
264 .num_links = ARRAY_SIZE(snd_soc_raumfeld_connector_dai),
265 .suspend_post = raumfeld_analog_suspend,
266 .resume_pre = raumfeld_analog_resume,
267};
268
269static struct snd_soc_card snd_soc_raumfeld_speaker = {
270 .name = "Raumfeld Speaker",
271 .dai_link = snd_soc_raumfeld_speaker_dai,
272 .num_links = ARRAY_SIZE(snd_soc_raumfeld_speaker_dai),
273 .suspend_post = raumfeld_analog_suspend,
274 .resume_pre = raumfeld_analog_resume,
Daniel Mack49af5742009-11-27 13:47:10 +0100275};
276
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000277static struct platform_device *raumfeld_audio_device;
Daniel Mack49af5742009-11-27 13:47:10 +0100278
279static int __init raumfeld_audio_init(void)
280{
281 int ret;
282
283 if (!machine_is_raumfeld_speaker() &&
284 !machine_is_raumfeld_connector())
285 return 0;
286
287 max9486_client = i2c_new_device(i2c_get_adapter(0),
288 &max9486_hwmon_info);
289
290 if (!max9486_client)
291 return -ENOMEM;
292
293 set_max9485_clk(MAX9485_MCLK_FREQ_122880);
294
Daniel Mack477a6692011-05-24 14:10:32 +0200295 /* Register analog device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000296 raumfeld_audio_device = platform_device_alloc("soc-audio", 0);
297 if (!raumfeld_audio_device)
Daniel Mack49af5742009-11-27 13:47:10 +0100298 return -ENOMEM;
299
Daniel Mack49af5742009-11-27 13:47:10 +0100300 if (machine_is_raumfeld_speaker())
Daniel Mack477a6692011-05-24 14:10:32 +0200301 platform_set_drvdata(raumfeld_audio_device,
302 &snd_soc_raumfeld_speaker);
303
304 if (machine_is_raumfeld_connector())
305 platform_set_drvdata(raumfeld_audio_device,
306 &snd_soc_raumfeld_connector);
307
308 ret = platform_device_add(raumfeld_audio_device);
Axel Lin4ed0d012011-09-02 21:47:41 +0800309 if (ret < 0) {
310 platform_device_put(raumfeld_audio_device);
Daniel Mack49af5742009-11-27 13:47:10 +0100311 return ret;
Axel Lin4ed0d012011-09-02 21:47:41 +0800312 }
Daniel Mack49af5742009-11-27 13:47:10 +0100313
Daniel Mack49af5742009-11-27 13:47:10 +0100314 raumfeld_enable_audio(true);
Daniel Mack477a6692011-05-24 14:10:32 +0200315 return 0;
Daniel Mack49af5742009-11-27 13:47:10 +0100316}
317
318static void __exit raumfeld_audio_exit(void)
319{
320 raumfeld_enable_audio(false);
321
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000322 platform_device_unregister(raumfeld_audio_device);
Daniel Mack49af5742009-11-27 13:47:10 +0100323
324 i2c_unregister_device(max9486_client);
325
326 gpio_free(GPIO_MCLK_RESET);
327 gpio_free(GPIO_CODEC_RESET);
328 gpio_free(GPIO_SPDIF_RESET);
329}
330
331module_init(raumfeld_audio_init);
332module_exit(raumfeld_audio_exit);
333
334/* Module information */
335MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
336MODULE_DESCRIPTION("Raumfeld audio SoC");
337MODULE_LICENSE("GPL");