1 /*
2  * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
3  *
4  * Copyright (c) 2014, ROCKCHIP CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/gpio.h>
24 #include <linux/of_gpio.h>
25 #include <sound/core.h>
26 #include <sound/jack.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 
31 #include "rockchip_i2s.h"
32 #include "../codecs/ts3a227e.h"
33 
34 #define DRV_NAME "rockchip-snd-max98090"
35 
36 static struct snd_soc_jack headset_jack;
37 
38 /* Headset jack detection DAPM pins */
39 static struct snd_soc_jack_pin headset_jack_pins[] = {
40 	{
41 		.pin = "Headphone",
42 		.mask = SND_JACK_HEADPHONE,
43 	},
44 	{
45 		.pin = "Headset Mic",
46 		.mask = SND_JACK_MICROPHONE,
47 	},
48 
49 };
50 
51 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
52 	SND_SOC_DAPM_HP("Headphone", NULL),
53 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
54 	SND_SOC_DAPM_MIC("Int Mic", NULL),
55 	SND_SOC_DAPM_SPK("Speaker", NULL),
56 };
57 
58 static const struct snd_soc_dapm_route rk_audio_map[] = {
59 	{"IN34", NULL, "Headset Mic"},
60 	{"IN34", NULL, "MICBIAS"},
61 	{"Headset Mic", NULL, "MICBIAS"},
62 	{"DMICL", NULL, "Int Mic"},
63 	{"Headphone", NULL, "HPL"},
64 	{"Headphone", NULL, "HPR"},
65 	{"Speaker", NULL, "SPKL"},
66 	{"Speaker", NULL, "SPKR"},
67 };
68 
69 static const struct snd_kcontrol_new rk_mc_controls[] = {
70 	SOC_DAPM_PIN_SWITCH("Headphone"),
71 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
72 	SOC_DAPM_PIN_SWITCH("Int Mic"),
73 	SOC_DAPM_PIN_SWITCH("Speaker"),
74 };
75 
76 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
77 			     struct snd_pcm_hw_params *params)
78 {
79 	int ret = 0;
80 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
81 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
82 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
83 	int mclk;
84 
85 	switch (params_rate(params)) {
86 	case 8000:
87 	case 16000:
88 	case 24000:
89 	case 32000:
90 	case 48000:
91 	case 64000:
92 	case 96000:
93 		mclk = 12288000;
94 		break;
95 	case 11025:
96 	case 22050:
97 	case 44100:
98 	case 88200:
99 		mclk = 11289600;
100 		break;
101 	default:
102 		return -EINVAL;
103 	}
104 
105 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
106 				     SND_SOC_CLOCK_OUT);
107 	if (ret < 0) {
108 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
109 		return ret;
110 	}
111 
112 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
113 				     SND_SOC_CLOCK_IN);
114 	if (ret < 0) {
115 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
116 		return ret;
117 	}
118 
119 	return ret;
120 }
121 
122 static const struct snd_soc_ops rk_aif1_ops = {
123 	.hw_params = rk_aif1_hw_params,
124 };
125 
126 static struct snd_soc_dai_link rk_dailink = {
127 	.name = "max98090",
128 	.stream_name = "Audio",
129 	.codec_dai_name = "HiFi",
130 	.ops = &rk_aif1_ops,
131 	/* set max98090 as slave */
132 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
133 		SND_SOC_DAIFMT_CBS_CFS,
134 };
135 
136 static int rk_98090_headset_init(struct snd_soc_component *component);
137 
138 static struct snd_soc_aux_dev rk_98090_headset_dev = {
139 	.name = "Headset Chip",
140 	.init = rk_98090_headset_init,
141 };
142 
143 static struct snd_soc_card snd_soc_card_rk = {
144 	.name = "ROCKCHIP-I2S",
145 	.owner = THIS_MODULE,
146 	.dai_link = &rk_dailink,
147 	.num_links = 1,
148 	.aux_dev = &rk_98090_headset_dev,
149 	.num_aux_devs = 1,
150 	.dapm_widgets = rk_dapm_widgets,
151 	.num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
152 	.dapm_routes = rk_audio_map,
153 	.num_dapm_routes = ARRAY_SIZE(rk_audio_map),
154 	.controls = rk_mc_controls,
155 	.num_controls = ARRAY_SIZE(rk_mc_controls),
156 };
157 
158 static int rk_98090_headset_init(struct snd_soc_component *component)
159 {
160 	int ret;
161 
162 	/* Enable Headset and 4 Buttons Jack detection */
163 	ret = snd_soc_card_jack_new(&snd_soc_card_rk, "Headset Jack",
164 				    SND_JACK_HEADSET |
165 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
166 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
167 				    &headset_jack,
168 				    headset_jack_pins,
169 				    ARRAY_SIZE(headset_jack_pins));
170 	if (ret)
171 		return ret;
172 
173 	ret = ts3a227e_enable_jack_detect(component, &headset_jack);
174 
175 	return ret;
176 }
177 
178 static int snd_rk_mc_probe(struct platform_device *pdev)
179 {
180 	int ret = 0;
181 	struct snd_soc_card *card = &snd_soc_card_rk;
182 	struct device_node *np = pdev->dev.of_node;
183 
184 	/* register the soc card */
185 	card->dev = &pdev->dev;
186 
187 	rk_dailink.codec_of_node = of_parse_phandle(np,
188 			"rockchip,audio-codec", 0);
189 	if (!rk_dailink.codec_of_node) {
190 		dev_err(&pdev->dev,
191 			"Property 'rockchip,audio-codec' missing or invalid\n");
192 		return -EINVAL;
193 	}
194 
195 	rk_dailink.cpu_of_node = of_parse_phandle(np,
196 			"rockchip,i2s-controller", 0);
197 	if (!rk_dailink.cpu_of_node) {
198 		dev_err(&pdev->dev,
199 			"Property 'rockchip,i2s-controller' missing or invalid\n");
200 		return -EINVAL;
201 	}
202 
203 	rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
204 
205 	rk_98090_headset_dev.codec_of_node = of_parse_phandle(np,
206 			"rockchip,headset-codec", 0);
207 	if (!rk_98090_headset_dev.codec_of_node) {
208 		dev_err(&pdev->dev,
209 			"Property 'rockchip,headset-codec' missing/invalid\n");
210 		return -EINVAL;
211 	}
212 
213 	ret = snd_soc_of_parse_card_name(card, "rockchip,model");
214 	if (ret) {
215 		dev_err(&pdev->dev,
216 			"Soc parse card name failed %d\n", ret);
217 		return ret;
218 	}
219 
220 	ret = devm_snd_soc_register_card(&pdev->dev, card);
221 	if (ret) {
222 		dev_err(&pdev->dev,
223 			"Soc register card failed %d\n", ret);
224 		return ret;
225 	}
226 
227 	return ret;
228 }
229 
230 static const struct of_device_id rockchip_max98090_of_match[] = {
231 	{ .compatible = "rockchip,rockchip-audio-max98090", },
232 	{},
233 };
234 
235 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match);
236 
237 static struct platform_driver snd_rk_mc_driver = {
238 	.probe = snd_rk_mc_probe,
239 	.driver = {
240 		.name = DRV_NAME,
241 		.pm = &snd_soc_pm_ops,
242 		.of_match_table = rockchip_max98090_of_match,
243 	},
244 };
245 
246 module_platform_driver(snd_rk_mc_driver);
247 
248 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
249 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:" DRV_NAME);
252