1/*
2 * Tobermory audio support
3 *
4 * Copyright 2011 Wolfson Microelectronics
5 *
6 * This program is free software; you can redistribute  it and/or modify it
7 * under  the terms of  the GNU General  Public License as published by the
8 * Free Software Foundation;  either version 2 of the  License, or (at your
9 * option) any later version.
10 */
11
12#include <sound/soc.h>
13#include <sound/soc-dapm.h>
14#include <sound/jack.h>
15#include <linux/gpio.h>
16#include <linux/module.h>
17
18#include "../codecs/wm8962.h"
19
20static int sample_rate = 44100;
21
22static int tobermory_set_bias_level(struct snd_soc_card *card,
23					  struct snd_soc_dapm_context *dapm,
24					  enum snd_soc_bias_level level)
25{
26	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
27	int ret;
28
29	if (dapm->dev != codec_dai->dev)
30		return 0;
31
32	switch (level) {
33	case SND_SOC_BIAS_PREPARE:
34		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
35			ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
36						  WM8962_FLL_MCLK, 32768,
37						  sample_rate * 512);
38			if (ret < 0)
39				pr_err("Failed to start FLL: %d\n", ret);
40
41			ret = snd_soc_dai_set_sysclk(codec_dai,
42						     WM8962_SYSCLK_FLL,
43						     sample_rate * 512,
44						     SND_SOC_CLOCK_IN);
45			if (ret < 0) {
46				pr_err("Failed to set SYSCLK: %d\n", ret);
47				snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
48						    0, 0, 0);
49				return ret;
50			}
51		}
52		break;
53
54	default:
55		break;
56	}
57
58	return 0;
59}
60
61static int tobermory_set_bias_level_post(struct snd_soc_card *card,
62					       struct snd_soc_dapm_context *dapm,
63					       enum snd_soc_bias_level level)
64{
65	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
66	int ret;
67
68	if (dapm->dev != codec_dai->dev)
69		return 0;
70
71	switch (level) {
72	case SND_SOC_BIAS_STANDBY:
73		ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
74					     32768, SND_SOC_CLOCK_IN);
75		if (ret < 0) {
76			pr_err("Failed to switch away from FLL: %d\n", ret);
77			return ret;
78		}
79
80		ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
81					  0, 0, 0);
82		if (ret < 0) {
83			pr_err("Failed to stop FLL: %d\n", ret);
84			return ret;
85		}
86		break;
87
88	default:
89		break;
90	}
91
92	dapm->bias_level = level;
93
94	return 0;
95}
96
97static int tobermory_hw_params(struct snd_pcm_substream *substream,
98			      struct snd_pcm_hw_params *params)
99{
100	sample_rate = params_rate(params);
101
102	return 0;
103}
104
105static struct snd_soc_ops tobermory_ops = {
106	.hw_params = tobermory_hw_params,
107};
108
109static struct snd_soc_dai_link tobermory_dai[] = {
110	{
111		.name = "CPU",
112		.stream_name = "CPU",
113		.cpu_dai_name = "samsung-i2s.0",
114		.codec_dai_name = "wm8962",
115		.platform_name = "samsung-i2s.0",
116		.codec_name = "wm8962.1-001a",
117		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
118				| SND_SOC_DAIFMT_CBM_CFM,
119		.ops = &tobermory_ops,
120	},
121};
122
123static const struct snd_kcontrol_new controls[] = {
124	SOC_DAPM_PIN_SWITCH("Main Speaker"),
125	SOC_DAPM_PIN_SWITCH("DMIC"),
126};
127
128static struct snd_soc_dapm_widget widgets[] = {
129	SND_SOC_DAPM_HP("Headphone", NULL),
130	SND_SOC_DAPM_MIC("Headset Mic", NULL),
131
132	SND_SOC_DAPM_MIC("DMIC", NULL),
133	SND_SOC_DAPM_MIC("AMIC", NULL),
134
135	SND_SOC_DAPM_SPK("Main Speaker", NULL),
136};
137
138static struct snd_soc_dapm_route audio_paths[] = {
139	{ "Headphone", NULL, "HPOUTL" },
140	{ "Headphone", NULL, "HPOUTR" },
141
142	{ "Main Speaker", NULL, "SPKOUTL" },
143	{ "Main Speaker", NULL, "SPKOUTR" },
144
145	{ "Headset Mic", NULL, "MICBIAS" },
146	{ "IN4L", NULL, "Headset Mic" },
147	{ "IN4R", NULL, "Headset Mic" },
148
149	{ "AMIC", NULL, "MICBIAS" },
150	{ "IN1L", NULL, "AMIC" },
151	{ "IN1R", NULL, "AMIC" },
152
153	{ "DMIC", NULL, "MICBIAS" },
154	{ "DMICDAT", NULL, "DMIC" },
155};
156
157static struct snd_soc_jack tobermory_headset;
158
159/* Headset jack detection DAPM pins */
160static struct snd_soc_jack_pin tobermory_headset_pins[] = {
161	{
162		.pin = "Headset Mic",
163		.mask = SND_JACK_MICROPHONE,
164	},
165	{
166		.pin = "Headphone",
167		.mask = SND_JACK_MICROPHONE,
168	},
169};
170
171static int tobermory_late_probe(struct snd_soc_card *card)
172{
173	struct snd_soc_codec *codec = card->rtd[0].codec;
174	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
175	int ret;
176
177	ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
178				     32768, SND_SOC_CLOCK_IN);
179	if (ret < 0)
180		return ret;
181
182	ret = snd_soc_card_jack_new(card, "Headset", SND_JACK_HEADSET |
183				    SND_JACK_BTN_0, &tobermory_headset,
184				    tobermory_headset_pins,
185				    ARRAY_SIZE(tobermory_headset_pins));
186	if (ret)
187		return ret;
188
189	wm8962_mic_detect(codec, &tobermory_headset);
190
191	return 0;
192}
193
194static struct snd_soc_card tobermory = {
195	.name = "Tobermory",
196	.owner = THIS_MODULE,
197	.dai_link = tobermory_dai,
198	.num_links = ARRAY_SIZE(tobermory_dai),
199
200	.set_bias_level = tobermory_set_bias_level,
201	.set_bias_level_post = tobermory_set_bias_level_post,
202
203	.controls = controls,
204	.num_controls = ARRAY_SIZE(controls),
205	.dapm_widgets = widgets,
206	.num_dapm_widgets = ARRAY_SIZE(widgets),
207	.dapm_routes = audio_paths,
208	.num_dapm_routes = ARRAY_SIZE(audio_paths),
209	.fully_routed = true,
210
211	.late_probe = tobermory_late_probe,
212};
213
214static int tobermory_probe(struct platform_device *pdev)
215{
216	struct snd_soc_card *card = &tobermory;
217	int ret;
218
219	card->dev = &pdev->dev;
220
221	ret = devm_snd_soc_register_card(&pdev->dev, card);
222	if (ret)
223		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
224			ret);
225
226	return ret;
227}
228
229static struct platform_driver tobermory_driver = {
230	.driver = {
231		.name = "tobermory",
232		.pm = &snd_soc_pm_ops,
233	},
234	.probe = tobermory_probe,
235};
236
237module_platform_driver(tobermory_driver);
238
239MODULE_DESCRIPTION("Tobermory audio support");
240MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
241MODULE_LICENSE("GPL");
242MODULE_ALIAS("platform:tobermory");
243