blob: ef795dfe99200c6074a661ae0d67b21969397e1a [file] [log] [blame]
Banajit Goswamide8271c2017-01-18 00:28:59 -08001/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/err.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/clk.h>
19#include <linux/clk/msm-clk-provider.h>
20#include <linux/clk/msm-clk.h>
21#include <linux/platform_device.h>
22#include <linux/gpio.h>
23#include <linux/of_gpio.h>
24#include <dt-bindings/clock/audio-ext-clk.h>
25#include <sound/q6afe-v2.h>
26
27struct pinctrl_info {
28 struct pinctrl *pinctrl;
29 struct pinctrl_state *sleep;
30 struct pinctrl_state *active;
31};
32
33struct audio_ext_ap_clk {
34 bool enabled;
35 int gpio;
36 struct clk c;
37};
38
39struct audio_ext_pmi_clk {
40 int gpio;
41 struct clk c;
42};
43
44struct audio_ext_ap_clk2 {
45 bool enabled;
46 struct pinctrl_info pnctrl_info;
47 struct clk c;
48};
49
50static struct afe_clk_set clk2_config = {
51 Q6AFE_LPASS_CLK_CONFIG_API_VERSION,
52 Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR,
53 Q6AFE_LPASS_IBIT_CLK_11_P2896_MHZ,
54 Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO,
55 Q6AFE_LPASS_CLK_ROOT_DEFAULT,
56 0,
57};
58
59static inline struct audio_ext_ap_clk *to_audio_ap_clk(struct clk *clk)
60{
61 return container_of(clk, struct audio_ext_ap_clk, c);
62}
63
64static int audio_ext_clk_prepare(struct clk *clk)
65{
66 struct audio_ext_ap_clk *audio_clk = to_audio_ap_clk(clk);
67
68 pr_debug("%s: gpio: %d\n", __func__, audio_clk->gpio);
69 if (gpio_is_valid(audio_clk->gpio))
70 return gpio_direction_output(audio_clk->gpio, 1);
71 return 0;
72}
73
74static void audio_ext_clk_unprepare(struct clk *clk)
75{
76 struct audio_ext_ap_clk *audio_clk = to_audio_ap_clk(clk);
77
78 pr_debug("%s: gpio: %d\n", __func__, audio_clk->gpio);
79 if (gpio_is_valid(audio_clk->gpio))
80 gpio_direction_output(audio_clk->gpio, 0);
81}
82
83static inline struct audio_ext_ap_clk2 *to_audio_ap_clk2(struct clk *clk)
84{
85 return container_of(clk, struct audio_ext_ap_clk2, c);
86}
87
88static int audio_ext_clk2_prepare(struct clk *clk)
89{
90 struct audio_ext_ap_clk2 *audio_clk2 = to_audio_ap_clk2(clk);
91 struct pinctrl_info *pnctrl_info = &audio_clk2->pnctrl_info;
92 int ret;
93
94
95 if (!pnctrl_info->pinctrl || !pnctrl_info->active)
96 return 0;
97
98 ret = pinctrl_select_state(pnctrl_info->pinctrl,
99 pnctrl_info->active);
100 if (ret) {
101 pr_err("%s: active state select failed with %d\n",
102 __func__, ret);
103 return -EIO;
104 }
105
106 clk2_config.enable = 1;
107 ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk2_config);
108 if (ret < 0) {
109 pr_err("%s: failed to set clock, ret = %d\n", __func__, ret);
110 return -EINVAL;
111 }
112
113 return 0;
114}
115
116static void audio_ext_clk2_unprepare(struct clk *clk)
117{
118 struct audio_ext_ap_clk2 *audio_clk2 = to_audio_ap_clk2(clk);
119 struct pinctrl_info *pnctrl_info = &audio_clk2->pnctrl_info;
120 int ret;
121
122 if (!pnctrl_info->pinctrl || !pnctrl_info->sleep)
123 return;
124
125 ret = pinctrl_select_state(pnctrl_info->pinctrl,
126 pnctrl_info->sleep);
127 if (ret)
128 pr_err("%s: sleep state select failed with %d\n",
129 __func__, ret);
130
131 clk2_config.enable = 0;
132 ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk2_config);
133 if (ret < 0)
134 pr_err("%s: failed to reset clock, ret = %d\n", __func__, ret);
135}
136
137static const struct clk_ops audio_ext_ap_clk_ops = {
138 .prepare = audio_ext_clk_prepare,
139 .unprepare = audio_ext_clk_unprepare,
140};
141
142static const struct clk_ops audio_ext_ap_clk2_ops = {
143 .prepare = audio_ext_clk2_prepare,
144 .unprepare = audio_ext_clk2_unprepare,
145};
146
147static struct audio_ext_pmi_clk audio_pmi_clk = {
148 .gpio = -EINVAL,
149 .c = {
150 .dbg_name = "audio_ext_pmi_clk",
151 .ops = &clk_ops_dummy,
152 CLK_INIT(audio_pmi_clk.c),
153 },
154};
155
156static struct audio_ext_pmi_clk audio_pmi_lnbb_clk = {
157 .gpio = -EINVAL,
158 .c = {
159 .dbg_name = "audio_ext_pmi_lnbb_clk",
160 .ops = &clk_ops_dummy,
161 CLK_INIT(audio_pmi_lnbb_clk.c),
162 },
163};
164
165static struct audio_ext_ap_clk audio_ap_clk = {
166 .gpio = -EINVAL,
167 .c = {
168 .dbg_name = "audio_ext_ap_clk",
169 .ops = &audio_ext_ap_clk_ops,
170 CLK_INIT(audio_ap_clk.c),
171 },
172};
173
174static struct audio_ext_ap_clk2 audio_ap_clk2 = {
175 .c = {
176 .dbg_name = "audio_ext_ap_clk2",
177 .ops = &audio_ext_ap_clk2_ops,
178 CLK_INIT(audio_ap_clk2.c),
179 },
180};
181
182static struct clk_lookup audio_ref_clock[] = {
183 CLK_LIST(audio_ap_clk),
184 CLK_LIST(audio_pmi_clk),
185 CLK_LIST(audio_pmi_lnbb_clk),
186 CLK_LIST(audio_ap_clk2),
187};
188
189static int audio_get_pinctrl(struct platform_device *pdev)
190{
191 struct pinctrl_info *pnctrl_info;
192 struct pinctrl *pinctrl;
193 int ret;
194
195 pnctrl_info = &audio_ap_clk2.pnctrl_info;
196
197 if (pnctrl_info->pinctrl) {
198 dev_dbg(&pdev->dev, "%s: already requested before\n",
199 __func__);
200 return -EINVAL;
201 }
202
203 pinctrl = devm_pinctrl_get(&pdev->dev);
204 if (IS_ERR_OR_NULL(pinctrl)) {
205 dev_dbg(&pdev->dev, "%s: Unable to get pinctrl handle\n",
206 __func__);
207 return -EINVAL;
208 }
209 pnctrl_info->pinctrl = pinctrl;
210 /* get all state handles from Device Tree */
211 pnctrl_info->sleep = pinctrl_lookup_state(pinctrl, "sleep");
212 if (IS_ERR(pnctrl_info->sleep)) {
213 dev_err(&pdev->dev, "%s: could not get sleep pinstate\n",
214 __func__);
215 goto err;
216 }
217 pnctrl_info->active = pinctrl_lookup_state(pinctrl, "active");
218 if (IS_ERR(pnctrl_info->active)) {
219 dev_err(&pdev->dev, "%s: could not get active pinstate\n",
220 __func__);
221 goto err;
222 }
223 /* Reset the TLMM pins to a default state */
224 ret = pinctrl_select_state(pnctrl_info->pinctrl,
225 pnctrl_info->sleep);
226 if (ret) {
227 dev_err(&pdev->dev, "%s: Disable TLMM pins failed with %d\n",
228 __func__, ret);
229 goto err;
230 }
231 return 0;
232
233err:
234 devm_pinctrl_put(pnctrl_info->pinctrl);
235 return -EINVAL;
236}
237
238static int audio_ref_clk_probe(struct platform_device *pdev)
239{
240 int clk_gpio;
241 int ret;
242 struct clk *audio_clk;
243
244 clk_gpio = of_get_named_gpio(pdev->dev.of_node,
245 "qcom,audio-ref-clk-gpio", 0);
246 if (clk_gpio > 0) {
247 ret = gpio_request(clk_gpio, "EXT_CLK");
248 if (ret) {
249 dev_err(&pdev->dev,
250 "Request ext clk gpio failed %d, err:%d\n",
251 clk_gpio, ret);
252 goto err;
253 }
254 if (of_property_read_bool(pdev->dev.of_node,
255 "qcom,node_has_rpm_clock")) {
256 audio_clk = clk_get(&pdev->dev, NULL);
257 if (IS_ERR(audio_clk)) {
258 dev_err(&pdev->dev, "Failed to get RPM div clk\n");
259 ret = PTR_ERR(audio_clk);
260 goto err_gpio;
261 }
262 audio_pmi_clk.c.parent = audio_clk;
263 audio_pmi_clk.gpio = clk_gpio;
264 } else
265 audio_ap_clk.gpio = clk_gpio;
266
267 } else {
268 if (of_property_read_bool(pdev->dev.of_node,
269 "qcom,node_has_rpm_clock")) {
270 audio_clk = clk_get(&pdev->dev, NULL);
271 if (IS_ERR(audio_clk)) {
272 dev_err(&pdev->dev, "Failed to get lnbbclk2\n");
273 ret = PTR_ERR(audio_clk);
274 goto err;
275 }
276 audio_pmi_lnbb_clk.c.parent = audio_clk;
277 audio_pmi_lnbb_clk.gpio = -EINVAL;
278 }
279 }
280
281 ret = audio_get_pinctrl(pdev);
282 if (ret)
283 dev_dbg(&pdev->dev, "%s: Parsing pinctrl failed\n",
284 __func__);
285
286 ret = of_msm_clock_register(pdev->dev.of_node, audio_ref_clock,
287 ARRAY_SIZE(audio_ref_clock));
288 if (ret) {
289 dev_err(&pdev->dev, "%s: audio ref clock register failed\n",
290 __func__);
291 goto err_gpio;
292 }
293
294 return 0;
295
296err_gpio:
297 gpio_free(clk_gpio);
298
299err:
300 return ret;
301}
302
303static int audio_ref_clk_remove(struct platform_device *pdev)
304{
305 struct pinctrl_info *pnctrl_info = &audio_ap_clk2.pnctrl_info;
306
307 if (audio_pmi_clk.gpio > 0)
308 gpio_free(audio_pmi_clk.gpio);
309 else if (audio_ap_clk.gpio > 0)
310 gpio_free(audio_ap_clk.gpio);
311
312 if (pnctrl_info->pinctrl) {
313 devm_pinctrl_put(pnctrl_info->pinctrl);
314 pnctrl_info->pinctrl = NULL;
315 }
316
317 return 0;
318}
319
320static const struct of_device_id audio_ref_clk_match[] = {
321 {.compatible = "qcom,audio-ref-clk"},
322 {}
323};
324MODULE_DEVICE_TABLE(of, audio_ref_clk_match);
325
326static struct platform_driver audio_ref_clk_driver = {
327 .driver = {
328 .name = "audio-ref-clk",
329 .owner = THIS_MODULE,
330 .of_match_table = audio_ref_clk_match,
331 },
332 .probe = audio_ref_clk_probe,
333 .remove = audio_ref_clk_remove,
334};
335
336static int __init audio_ref_clk_platform_init(void)
337{
338 return platform_driver_register(&audio_ref_clk_driver);
339}
340module_init(audio_ref_clk_platform_init);
341
342static void __exit audio_ref_clk_platform_exit(void)
343{
344 platform_driver_unregister(&audio_ref_clk_driver);
345}
346module_exit(audio_ref_clk_platform_exit);
347
348MODULE_DESCRIPTION("Audio Ref Clock module platform driver");
349MODULE_LICENSE("GPL v2");