1/*
2 *  smdk_wm8580.c
3 *
4 *  Copyright (c) 2009 Samsung Electronics Co. Ltd
5 *  Author: Jaswinder Singh <jassisinghbrar@gmail.com>
6 *
7 *  This program is free software; you can redistribute  it and/or modify it
8 *  under  the terms of  the GNU General  Public License as published by the
9 *  Free Software Foundation;  either version 2 of the  License, or (at your
10 *  option) any later version.
11 */
12
13#include <linux/module.h>
14#include <sound/soc.h>
15#include <sound/pcm_params.h>
16
17#include <asm/mach-types.h>
18
19#include "../codecs/wm8580.h"
20#include "i2s.h"
21
22/*
23 * Default CFG switch settings to use this driver:
24 *
25 *   SMDK6410: Set CFG1 1-3 Off, CFG2 1-4 On
26 */
27
28/* SMDK has a 12MHZ crystal attached to WM8580 */
29#define SMDK_WM8580_FREQ 12000000
30
31static int smdk_hw_params(struct snd_pcm_substream *substream,
32	struct snd_pcm_hw_params *params)
33{
34	struct snd_soc_pcm_runtime *rtd = substream->private_data;
35	struct snd_soc_dai *codec_dai = rtd->codec_dai;
36	unsigned int pll_out;
37	int bfs, rfs, ret;
38
39	switch (params_width(params)) {
40	case 8:
41		bfs = 16;
42		break;
43	case 16:
44		bfs = 32;
45		break;
46	default:
47		return -EINVAL;
48	}
49
50	/* The Fvco for WM8580 PLLs must fall within [90,100]MHz.
51	 * This criterion can't be met if we request PLL output
52	 * as {8000x256, 64000x256, 11025x256}Hz.
53	 * As a wayout, we rather change rfs to a minimum value that
54	 * results in (params_rate(params) * rfs), and itself, acceptable
55	 * to both - the CODEC and the CPU.
56	 */
57	switch (params_rate(params)) {
58	case 16000:
59	case 22050:
60	case 32000:
61	case 44100:
62	case 48000:
63	case 88200:
64	case 96000:
65		rfs = 256;
66		break;
67	case 64000:
68		rfs = 384;
69		break;
70	case 8000:
71	case 11025:
72		rfs = 512;
73		break;
74	default:
75		return -EINVAL;
76	}
77	pll_out = params_rate(params) * rfs;
78
79	/* Set WM8580 to drive MCLK from its PLLA */
80	ret = snd_soc_dai_set_clkdiv(codec_dai, WM8580_MCLK,
81					WM8580_CLKSRC_PLLA);
82	if (ret < 0)
83		return ret;
84
85	ret = snd_soc_dai_set_pll(codec_dai, WM8580_PLLA, 0,
86					SMDK_WM8580_FREQ, pll_out);
87	if (ret < 0)
88		return ret;
89
90	ret = snd_soc_dai_set_sysclk(codec_dai, WM8580_CLKSRC_PLLA,
91				     pll_out, SND_SOC_CLOCK_IN);
92	if (ret < 0)
93		return ret;
94
95	return 0;
96}
97
98/*
99 * SMDK WM8580 DAI operations.
100 */
101static struct snd_soc_ops smdk_ops = {
102	.hw_params = smdk_hw_params,
103};
104
105/* SMDK Playback widgets */
106static const struct snd_soc_dapm_widget smdk_wm8580_dapm_widgets[] = {
107	SND_SOC_DAPM_HP("Front", NULL),
108	SND_SOC_DAPM_HP("Center+Sub", NULL),
109	SND_SOC_DAPM_HP("Rear", NULL),
110
111	SND_SOC_DAPM_MIC("MicIn", NULL),
112	SND_SOC_DAPM_LINE("LineIn", NULL),
113};
114
115/* SMDK-PAIFTX connections */
116static const struct snd_soc_dapm_route smdk_wm8580_audio_map[] = {
117	/* MicIn feeds AINL */
118	{"AINL", NULL, "MicIn"},
119
120	/* LineIn feeds AINL/R */
121	{"AINL", NULL, "LineIn"},
122	{"AINR", NULL, "LineIn"},
123
124	/* Front Left/Right are fed VOUT1L/R */
125	{"Front", NULL, "VOUT1L"},
126	{"Front", NULL, "VOUT1R"},
127
128	/* Center/Sub are fed VOUT2L/R */
129	{"Center+Sub", NULL, "VOUT2L"},
130	{"Center+Sub", NULL, "VOUT2R"},
131
132	/* Rear Left/Right are fed VOUT3L/R */
133	{"Rear", NULL, "VOUT3L"},
134	{"Rear", NULL, "VOUT3R"},
135};
136
137static int smdk_wm8580_init_paiftx(struct snd_soc_pcm_runtime *rtd)
138{
139	/* Enabling the microphone requires the fitting of a 0R
140	 * resistor to connect the line from the microphone jack.
141	 */
142	snd_soc_dapm_disable_pin(&rtd->card->dapm, "MicIn");
143
144	return 0;
145}
146
147enum {
148	PRI_PLAYBACK = 0,
149	PRI_CAPTURE,
150	SEC_PLAYBACK,
151};
152
153#define SMDK_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
154	SND_SOC_DAIFMT_CBM_CFM)
155
156static struct snd_soc_dai_link smdk_dai[] = {
157	[PRI_PLAYBACK] = { /* Primary Playback i/f */
158		.name = "WM8580 PAIF RX",
159		.stream_name = "Playback",
160		.cpu_dai_name = "samsung-i2s.0",
161		.codec_dai_name = "wm8580-hifi-playback",
162		.platform_name = "samsung-i2s.0",
163		.codec_name = "wm8580.0-001b",
164		.dai_fmt = SMDK_DAI_FMT,
165		.ops = &smdk_ops,
166	},
167	[PRI_CAPTURE] = { /* Primary Capture i/f */
168		.name = "WM8580 PAIF TX",
169		.stream_name = "Capture",
170		.cpu_dai_name = "samsung-i2s.0",
171		.codec_dai_name = "wm8580-hifi-capture",
172		.platform_name = "samsung-i2s.0",
173		.codec_name = "wm8580.0-001b",
174		.dai_fmt = SMDK_DAI_FMT,
175		.init = smdk_wm8580_init_paiftx,
176		.ops = &smdk_ops,
177	},
178	[SEC_PLAYBACK] = { /* Sec_Fifo Playback i/f */
179		.name = "Sec_FIFO TX",
180		.stream_name = "Playback",
181		.cpu_dai_name = "samsung-i2s-sec",
182		.codec_dai_name = "wm8580-hifi-playback",
183		.platform_name = "samsung-i2s-sec",
184		.codec_name = "wm8580.0-001b",
185		.dai_fmt = SMDK_DAI_FMT,
186		.ops = &smdk_ops,
187	},
188};
189
190static struct snd_soc_card smdk = {
191	.name = "SMDK-I2S",
192	.owner = THIS_MODULE,
193	.dai_link = smdk_dai,
194	.num_links = 2,
195
196	.dapm_widgets = smdk_wm8580_dapm_widgets,
197	.num_dapm_widgets = ARRAY_SIZE(smdk_wm8580_dapm_widgets),
198	.dapm_routes = smdk_wm8580_audio_map,
199	.num_dapm_routes = ARRAY_SIZE(smdk_wm8580_audio_map),
200};
201
202static struct platform_device *smdk_snd_device;
203
204static int __init smdk_audio_init(void)
205{
206	int ret;
207	char *str;
208
209	if (machine_is_smdkc100()
210			|| machine_is_smdkv210() || machine_is_smdkc110()) {
211		smdk.num_links = 3;
212	} else if (machine_is_smdk6410()) {
213		str = (char *)smdk_dai[PRI_PLAYBACK].cpu_dai_name;
214		str[strlen(str) - 1] = '2';
215		str = (char *)smdk_dai[PRI_CAPTURE].cpu_dai_name;
216		str[strlen(str) - 1] = '2';
217	}
218
219	smdk_snd_device = platform_device_alloc("soc-audio", -1);
220	if (!smdk_snd_device)
221		return -ENOMEM;
222
223	platform_set_drvdata(smdk_snd_device, &smdk);
224	ret = platform_device_add(smdk_snd_device);
225
226	if (ret)
227		platform_device_put(smdk_snd_device);
228
229	return ret;
230}
231module_init(smdk_audio_init);
232
233static void __exit smdk_audio_exit(void)
234{
235	platform_device_unregister(smdk_snd_device);
236}
237module_exit(smdk_audio_exit);
238
239MODULE_AUTHOR("Jaswinder Singh, jassisinghbrar@gmail.com");
240MODULE_DESCRIPTION("ALSA SoC SMDK WM8580");
241MODULE_LICENSE("GPL");
242