1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Atom platform clocks driver for BayTrail and CherryTrail SoCs
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Irina Tirdea <irina.tirdea@intel.com>
9 #include <linux/clk-provider.h>
10 #include <linux/clkdev.h>
11 #include <linux/err.h>
13 #include <linux/platform_data/x86/clk-pmc-atom.h>
14 #include <linux/platform_data/x86/pmc_atom.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #define PLT_CLK_NAME_BASE "pmc_plt_clk"
20 struct clk_plt_fixed
{
22 struct clk_lookup
*lookup
;
28 struct clk_lookup
*lookup
;
29 /* protect access to PMC registers */
33 #define to_clk_plt(_hw) container_of(_hw, struct clk_plt, hw)
36 struct clk_plt_fixed
**parents
;
38 struct clk_plt
*clks
[PMC_CLK_NUM
];
39 struct clk_lookup
*mclk_lookup
;
40 struct clk_lookup
*ether_clk_lookup
;
43 /* Return an index in parent table */
44 static inline int plt_reg_to_parent(int reg
)
46 switch (reg
& PMC_MASK_CLK_FREQ
) {
48 case PMC_CLK_FREQ_XTAL
:
50 case PMC_CLK_FREQ_PLL
:
55 /* Return clk index of parent */
56 static inline int plt_parent_to_reg(int index
)
61 return PMC_CLK_FREQ_XTAL
;
63 return PMC_CLK_FREQ_PLL
;
67 /* Abstract status in simpler enabled/disabled value */
68 static inline int plt_reg_to_enabled(int reg
)
70 switch (reg
& PMC_MASK_CLK_CTL
) {
71 case PMC_CLK_CTL_GATED_ON_D3
:
72 case PMC_CLK_CTL_FORCE_ON
:
73 return 1; /* enabled */
74 case PMC_CLK_CTL_FORCE_OFF
:
75 case PMC_CLK_CTL_RESERVED
:
77 return 0; /* disabled */
81 static void plt_clk_reg_update(struct clk_plt
*clk
, u32 mask
, u32 val
)
86 spin_lock_irqsave(&clk
->lock
, flags
);
88 tmp
= readl(clk
->reg
);
89 tmp
= (tmp
& ~mask
) | (val
& mask
);
90 writel(tmp
, clk
->reg
);
92 spin_unlock_irqrestore(&clk
->lock
, flags
);
95 static int plt_clk_set_parent(struct clk_hw
*hw
, u8 index
)
97 struct clk_plt
*clk
= to_clk_plt(hw
);
99 plt_clk_reg_update(clk
, PMC_MASK_CLK_FREQ
, plt_parent_to_reg(index
));
104 static u8
plt_clk_get_parent(struct clk_hw
*hw
)
106 struct clk_plt
*clk
= to_clk_plt(hw
);
109 value
= readl(clk
->reg
);
111 return plt_reg_to_parent(value
);
114 static int plt_clk_enable(struct clk_hw
*hw
)
116 struct clk_plt
*clk
= to_clk_plt(hw
);
118 plt_clk_reg_update(clk
, PMC_MASK_CLK_CTL
, PMC_CLK_CTL_FORCE_ON
);
123 static void plt_clk_disable(struct clk_hw
*hw
)
125 struct clk_plt
*clk
= to_clk_plt(hw
);
127 plt_clk_reg_update(clk
, PMC_MASK_CLK_CTL
, PMC_CLK_CTL_FORCE_OFF
);
130 static int plt_clk_is_enabled(struct clk_hw
*hw
)
132 struct clk_plt
*clk
= to_clk_plt(hw
);
135 value
= readl(clk
->reg
);
137 return plt_reg_to_enabled(value
);
140 static const struct clk_ops plt_clk_ops
= {
141 .enable
= plt_clk_enable
,
142 .disable
= plt_clk_disable
,
143 .is_enabled
= plt_clk_is_enabled
,
144 .get_parent
= plt_clk_get_parent
,
145 .set_parent
= plt_clk_set_parent
,
146 .determine_rate
= __clk_mux_determine_rate
,
149 static struct clk_plt
*plt_clk_register(struct platform_device
*pdev
, int id
,
150 const struct pmc_clk_data
*pmc_data
,
151 const char **parent_names
,
154 struct clk_plt
*pclk
;
155 struct clk_init_data init
;
158 pclk
= devm_kzalloc(&pdev
->dev
, sizeof(*pclk
), GFP_KERNEL
);
160 return ERR_PTR(-ENOMEM
);
162 init
.name
= kasprintf(GFP_KERNEL
, "%s_%d", PLT_CLK_NAME_BASE
, id
);
163 init
.ops
= &plt_clk_ops
;
165 init
.parent_names
= parent_names
;
166 init
.num_parents
= num_parents
;
168 pclk
->hw
.init
= &init
;
169 pclk
->reg
= pmc_data
->base
+ PMC_CLK_CTL_OFFSET
+ id
* PMC_CLK_CTL_SIZE
;
170 spin_lock_init(&pclk
->lock
);
173 * On some systems, the pmc_plt_clocks already enabled by the
174 * firmware are being marked as critical to avoid them being
175 * gated by the clock framework.
177 if (pmc_data
->critical
&& plt_clk_is_enabled(&pclk
->hw
))
178 init
.flags
|= CLK_IS_CRITICAL
;
180 ret
= devm_clk_hw_register(&pdev
->dev
, &pclk
->hw
);
186 pclk
->lookup
= clkdev_hw_create(&pclk
->hw
, init
.name
, NULL
);
188 pclk
= ERR_PTR(-ENOMEM
);
197 static void plt_clk_unregister(struct clk_plt
*pclk
)
199 clkdev_drop(pclk
->lookup
);
202 static struct clk_plt_fixed
*plt_clk_register_fixed_rate(struct platform_device
*pdev
,
204 const char *parent_name
,
205 unsigned long fixed_rate
)
207 struct clk_plt_fixed
*pclk
;
209 pclk
= devm_kzalloc(&pdev
->dev
, sizeof(*pclk
), GFP_KERNEL
);
211 return ERR_PTR(-ENOMEM
);
213 pclk
->clk
= clk_hw_register_fixed_rate(&pdev
->dev
, name
, parent_name
,
215 if (IS_ERR(pclk
->clk
))
216 return ERR_CAST(pclk
->clk
);
218 pclk
->lookup
= clkdev_hw_create(pclk
->clk
, name
, NULL
);
220 clk_hw_unregister_fixed_rate(pclk
->clk
);
221 return ERR_PTR(-ENOMEM
);
227 static void plt_clk_unregister_fixed_rate(struct clk_plt_fixed
*pclk
)
229 clkdev_drop(pclk
->lookup
);
230 clk_hw_unregister_fixed_rate(pclk
->clk
);
233 static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data
*data
,
237 plt_clk_unregister_fixed_rate(data
->parents
[i
]);
240 static void plt_clk_free_parent_names_loop(const char **parent_names
,
244 kfree_const(parent_names
[i
]);
248 static void plt_clk_unregister_loop(struct clk_plt_data
*data
,
252 plt_clk_unregister(data
->clks
[i
]);
255 static const char **plt_clk_register_parents(struct platform_device
*pdev
,
256 struct clk_plt_data
*data
,
257 const struct pmc_clk
*clks
)
259 const char **parent_names
;
265 while (clks
[nparents
].name
)
268 data
->parents
= devm_kcalloc(&pdev
->dev
, nparents
,
269 sizeof(*data
->parents
), GFP_KERNEL
);
271 return ERR_PTR(-ENOMEM
);
273 parent_names
= kcalloc(nparents
, sizeof(*parent_names
),
276 return ERR_PTR(-ENOMEM
);
278 for (i
= 0; i
< nparents
; i
++) {
280 plt_clk_register_fixed_rate(pdev
, clks
[i
].name
,
283 if (IS_ERR(data
->parents
[i
])) {
284 err
= PTR_ERR(data
->parents
[i
]);
287 parent_names
[i
] = kstrdup_const(clks
[i
].name
, GFP_KERNEL
);
290 data
->nparents
= nparents
;
294 plt_clk_unregister_fixed_rate_loop(data
, i
);
295 plt_clk_free_parent_names_loop(parent_names
, i
);
299 static void plt_clk_unregister_parents(struct clk_plt_data
*data
)
301 plt_clk_unregister_fixed_rate_loop(data
, data
->nparents
);
304 static int plt_clk_probe(struct platform_device
*pdev
)
306 const struct pmc_clk_data
*pmc_data
;
307 const char **parent_names
;
308 struct clk_plt_data
*data
;
312 pmc_data
= dev_get_platdata(&pdev
->dev
);
313 if (!pmc_data
|| !pmc_data
->clks
)
316 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
320 parent_names
= plt_clk_register_parents(pdev
, data
, pmc_data
->clks
);
321 if (IS_ERR(parent_names
))
322 return PTR_ERR(parent_names
);
324 for (i
= 0; i
< PMC_CLK_NUM
; i
++) {
325 data
->clks
[i
] = plt_clk_register(pdev
, i
, pmc_data
,
326 parent_names
, data
->nparents
);
327 if (IS_ERR(data
->clks
[i
])) {
328 err
= PTR_ERR(data
->clks
[i
]);
329 goto err_unreg_clk_plt
;
332 data
->mclk_lookup
= clkdev_hw_create(&data
->clks
[3]->hw
, "mclk", NULL
);
333 if (!data
->mclk_lookup
) {
335 goto err_unreg_clk_plt
;
338 data
->ether_clk_lookup
= clkdev_hw_create(&data
->clks
[4]->hw
,
340 if (!data
->ether_clk_lookup
) {
345 plt_clk_free_parent_names_loop(parent_names
, data
->nparents
);
347 platform_set_drvdata(pdev
, data
);
351 clkdev_drop(data
->mclk_lookup
);
353 plt_clk_unregister_loop(data
, i
);
354 plt_clk_unregister_parents(data
);
355 plt_clk_free_parent_names_loop(parent_names
, data
->nparents
);
359 static void plt_clk_remove(struct platform_device
*pdev
)
361 struct clk_plt_data
*data
;
363 data
= platform_get_drvdata(pdev
);
365 clkdev_drop(data
->ether_clk_lookup
);
366 clkdev_drop(data
->mclk_lookup
);
367 plt_clk_unregister_loop(data
, PMC_CLK_NUM
);
368 plt_clk_unregister_parents(data
);
371 static struct platform_driver plt_clk_driver
= {
373 .name
= "clk-pmc-atom",
375 .probe
= plt_clk_probe
,
376 .remove
= plt_clk_remove
,
378 builtin_platform_driver(plt_clk_driver
);