Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / qcom / lpass-gfm-sm8250.c
blobd366c7c2abc778d83d9df02e601043308ec11a1c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * LPASS Audio CC and Always ON CC Glitch Free Mux clock driver
5 * Copyright (c) 2020 Linaro Ltd.
6 * Author: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/clk-provider.h>
12 #include <linux/io.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/pm_clock.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_device.h>
20 #include <dt-bindings/clock/qcom,sm8250-lpass-audiocc.h>
21 #include <dt-bindings/clock/qcom,sm8250-lpass-aoncc.h>
23 struct lpass_gfm {
24 struct device *dev;
25 void __iomem *base;
28 struct clk_gfm {
29 unsigned int mux_reg;
30 unsigned int mux_mask;
31 struct clk_hw hw;
32 struct lpass_gfm *priv;
33 void __iomem *gfm_mux;
36 #define GFM_MASK BIT(1)
37 #define to_clk_gfm(_hw) container_of(_hw, struct clk_gfm, hw)
39 static u8 clk_gfm_get_parent(struct clk_hw *hw)
41 struct clk_gfm *clk = to_clk_gfm(hw);
43 return readl(clk->gfm_mux) & GFM_MASK;
46 static int clk_gfm_set_parent(struct clk_hw *hw, u8 index)
48 struct clk_gfm *clk = to_clk_gfm(hw);
49 unsigned int val;
51 val = readl(clk->gfm_mux);
53 if (index)
54 val |= GFM_MASK;
55 else
56 val &= ~GFM_MASK;
58 writel(val, clk->gfm_mux);
60 return 0;
63 static const struct clk_ops clk_gfm_ops = {
64 .get_parent = clk_gfm_get_parent,
65 .set_parent = clk_gfm_set_parent,
66 .determine_rate = __clk_mux_determine_rate,
69 static struct clk_gfm lpass_gfm_va_mclk = {
70 .mux_reg = 0x20000,
71 .mux_mask = BIT(0),
72 .hw.init = &(struct clk_init_data) {
73 .name = "VA_MCLK",
74 .ops = &clk_gfm_ops,
75 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
76 .num_parents = 2,
77 .parent_data = (const struct clk_parent_data[]){
79 .index = 0,
80 .fw_name = "LPASS_CLK_ID_TX_CORE_MCLK",
81 }, {
82 .index = 1,
83 .fw_name = "LPASS_CLK_ID_VA_CORE_MCLK",
89 static struct clk_gfm lpass_gfm_tx_npl = {
90 .mux_reg = 0x20000,
91 .mux_mask = BIT(0),
92 .hw.init = &(struct clk_init_data) {
93 .name = "TX_NPL",
94 .ops = &clk_gfm_ops,
95 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
96 .parent_data = (const struct clk_parent_data[]){
98 .index = 0,
99 .fw_name = "LPASS_CLK_ID_TX_CORE_NPL_MCLK",
100 }, {
101 .index = 1,
102 .fw_name = "LPASS_CLK_ID_VA_CORE_2X_MCLK",
105 .num_parents = 2,
109 static struct clk_gfm lpass_gfm_wsa_mclk = {
110 .mux_reg = 0x220d8,
111 .mux_mask = BIT(0),
112 .hw.init = &(struct clk_init_data) {
113 .name = "WSA_MCLK",
114 .ops = &clk_gfm_ops,
115 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
116 .parent_data = (const struct clk_parent_data[]){
118 .index = 0,
119 .fw_name = "LPASS_CLK_ID_TX_CORE_MCLK",
120 }, {
121 .index = 1,
122 .fw_name = "LPASS_CLK_ID_WSA_CORE_MCLK",
125 .num_parents = 2,
129 static struct clk_gfm lpass_gfm_wsa_npl = {
130 .mux_reg = 0x220d8,
131 .mux_mask = BIT(0),
132 .hw.init = &(struct clk_init_data) {
133 .name = "WSA_NPL",
134 .ops = &clk_gfm_ops,
135 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
136 .parent_data = (const struct clk_parent_data[]){
138 .index = 0,
139 .fw_name = "LPASS_CLK_ID_TX_CORE_NPL_MCLK",
140 }, {
141 .index = 1,
142 .fw_name = "LPASS_CLK_ID_WSA_CORE_NPL_MCLK",
145 .num_parents = 2,
149 static struct clk_gfm lpass_gfm_rx_mclk_mclk2 = {
150 .mux_reg = 0x240d8,
151 .mux_mask = BIT(0),
152 .hw.init = &(struct clk_init_data) {
153 .name = "RX_MCLK_MCLK2",
154 .ops = &clk_gfm_ops,
155 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
156 .parent_data = (const struct clk_parent_data[]){
158 .index = 0,
159 .fw_name = "LPASS_CLK_ID_TX_CORE_MCLK",
160 }, {
161 .index = 1,
162 .fw_name = "LPASS_CLK_ID_RX_CORE_MCLK",
165 .num_parents = 2,
169 static struct clk_gfm lpass_gfm_rx_npl = {
170 .mux_reg = 0x240d8,
171 .mux_mask = BIT(0),
172 .hw.init = &(struct clk_init_data) {
173 .name = "RX_NPL",
174 .ops = &clk_gfm_ops,
175 .flags = CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
176 .parent_data = (const struct clk_parent_data[]){
178 .index = 0,
179 .fw_name = "LPASS_CLK_ID_TX_CORE_NPL_MCLK",
180 }, {
181 .index = 1,
182 .fw_name = "LPASS_CLK_ID_RX_CORE_NPL_MCLK",
185 .num_parents = 2,
189 static struct clk_gfm *aoncc_gfm_clks[] = {
190 [LPASS_CDC_VA_MCLK] = &lpass_gfm_va_mclk,
191 [LPASS_CDC_TX_NPL] = &lpass_gfm_tx_npl,
194 static struct clk_hw_onecell_data aoncc_hw_onecell_data = {
195 .hws = {
196 [LPASS_CDC_VA_MCLK] = &lpass_gfm_va_mclk.hw,
197 [LPASS_CDC_TX_NPL] = &lpass_gfm_tx_npl.hw,
199 .num = ARRAY_SIZE(aoncc_gfm_clks),
202 static struct clk_gfm *audiocc_gfm_clks[] = {
203 [LPASS_CDC_WSA_NPL] = &lpass_gfm_wsa_npl,
204 [LPASS_CDC_WSA_MCLK] = &lpass_gfm_wsa_mclk,
205 [LPASS_CDC_RX_NPL] = &lpass_gfm_rx_npl,
206 [LPASS_CDC_RX_MCLK_MCLK2] = &lpass_gfm_rx_mclk_mclk2,
209 static struct clk_hw_onecell_data audiocc_hw_onecell_data = {
210 .hws = {
211 [LPASS_CDC_WSA_NPL] = &lpass_gfm_wsa_npl.hw,
212 [LPASS_CDC_WSA_MCLK] = &lpass_gfm_wsa_mclk.hw,
213 [LPASS_CDC_RX_NPL] = &lpass_gfm_rx_npl.hw,
214 [LPASS_CDC_RX_MCLK_MCLK2] = &lpass_gfm_rx_mclk_mclk2.hw,
216 .num = ARRAY_SIZE(audiocc_gfm_clks),
219 struct lpass_gfm_data {
220 struct clk_hw_onecell_data *onecell_data;
221 struct clk_gfm **gfm_clks;
224 static struct lpass_gfm_data audiocc_data = {
225 .onecell_data = &audiocc_hw_onecell_data,
226 .gfm_clks = audiocc_gfm_clks,
229 static struct lpass_gfm_data aoncc_data = {
230 .onecell_data = &aoncc_hw_onecell_data,
231 .gfm_clks = aoncc_gfm_clks,
234 static int lpass_gfm_clk_driver_probe(struct platform_device *pdev)
236 const struct lpass_gfm_data *data;
237 struct device *dev = &pdev->dev;
238 struct clk_gfm *gfm;
239 struct lpass_gfm *cc;
240 int err, i;
242 data = of_device_get_match_data(dev);
243 if (!data)
244 return -EINVAL;
246 cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL);
247 if (!cc)
248 return -ENOMEM;
250 cc->base = devm_platform_ioremap_resource(pdev, 0);
251 if (IS_ERR(cc->base))
252 return PTR_ERR(cc->base);
254 pm_runtime_enable(dev);
255 err = pm_clk_create(dev);
256 if (err)
257 goto pm_clk_err;
259 err = of_pm_clk_add_clks(dev);
260 if (err < 0) {
261 dev_dbg(dev, "Failed to get lpass core voting clocks\n");
262 goto clk_reg_err;
265 for (i = 0; i < data->onecell_data->num; i++) {
266 if (!data->gfm_clks[i])
267 continue;
269 gfm = data->gfm_clks[i];
270 gfm->priv = cc;
271 gfm->gfm_mux = cc->base;
272 gfm->gfm_mux = gfm->gfm_mux + data->gfm_clks[i]->mux_reg;
274 err = devm_clk_hw_register(dev, &data->gfm_clks[i]->hw);
275 if (err)
276 goto clk_reg_err;
280 err = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
281 data->onecell_data);
282 if (err)
283 goto clk_reg_err;
285 return 0;
287 clk_reg_err:
288 pm_clk_destroy(dev);
289 pm_clk_err:
290 pm_runtime_disable(dev);
291 return err;
294 static const struct of_device_id lpass_gfm_clk_match_table[] = {
296 .compatible = "qcom,sm8250-lpass-aoncc",
297 .data = &aoncc_data,
300 .compatible = "qcom,sm8250-lpass-audiocc",
301 .data = &audiocc_data,
305 MODULE_DEVICE_TABLE(of, lpass_gfm_clk_match_table);
307 static const struct dev_pm_ops lpass_gfm_pm_ops = {
308 SET_RUNTIME_PM_OPS(pm_clk_suspend, pm_clk_resume, NULL)
311 static struct platform_driver lpass_gfm_clk_driver = {
312 .probe = lpass_gfm_clk_driver_probe,
313 .driver = {
314 .name = "lpass-gfm-clk",
315 .of_match_table = lpass_gfm_clk_match_table,
316 .pm = &lpass_gfm_pm_ops,
319 module_platform_driver(lpass_gfm_clk_driver);
320 MODULE_LICENSE("GPL v2");