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_device.h>
15 #include <linux/slab.h>
17 #define PLT_CLK_NAME_BASE "pmc_plt_clk"
19 #define PMC_CLK_CTL_OFFSET 0x60
20 #define PMC_CLK_CTL_SIZE 4
22 #define PMC_CLK_CTL_GATED_ON_D3 0x0
23 #define PMC_CLK_CTL_FORCE_ON 0x1
24 #define PMC_CLK_CTL_FORCE_OFF 0x2
25 #define PMC_CLK_CTL_RESERVED 0x3
26 #define PMC_MASK_CLK_CTL GENMASK(1, 0)
27 #define PMC_MASK_CLK_FREQ BIT(2)
28 #define PMC_CLK_FREQ_XTAL (0 << 2) /* 25 MHz */
29 #define PMC_CLK_FREQ_PLL (1 << 2) /* 19.2 MHz */
31 struct clk_plt_fixed
{
33 struct clk_lookup
*lookup
;
39 struct clk_lookup
*lookup
;
40 /* protect access to PMC registers */
44 #define to_clk_plt(_hw) container_of(_hw, struct clk_plt, hw)
47 struct clk_plt_fixed
**parents
;
49 struct clk_plt
*clks
[PMC_CLK_NUM
];
50 struct clk_lookup
*mclk_lookup
;
51 struct clk_lookup
*ether_clk_lookup
;
54 /* Return an index in parent table */
55 static inline int plt_reg_to_parent(int reg
)
57 switch (reg
& PMC_MASK_CLK_FREQ
) {
59 case PMC_CLK_FREQ_XTAL
:
61 case PMC_CLK_FREQ_PLL
:
66 /* Return clk index of parent */
67 static inline int plt_parent_to_reg(int index
)
72 return PMC_CLK_FREQ_XTAL
;
74 return PMC_CLK_FREQ_PLL
;
78 /* Abstract status in simpler enabled/disabled value */
79 static inline int plt_reg_to_enabled(int reg
)
81 switch (reg
& PMC_MASK_CLK_CTL
) {
82 case PMC_CLK_CTL_GATED_ON_D3
:
83 case PMC_CLK_CTL_FORCE_ON
:
84 return 1; /* enabled */
85 case PMC_CLK_CTL_FORCE_OFF
:
86 case PMC_CLK_CTL_RESERVED
:
88 return 0; /* disabled */
92 static void plt_clk_reg_update(struct clk_plt
*clk
, u32 mask
, u32 val
)
97 spin_lock_irqsave(&clk
->lock
, flags
);
99 tmp
= readl(clk
->reg
);
100 tmp
= (tmp
& ~mask
) | (val
& mask
);
101 writel(tmp
, clk
->reg
);
103 spin_unlock_irqrestore(&clk
->lock
, flags
);
106 static int plt_clk_set_parent(struct clk_hw
*hw
, u8 index
)
108 struct clk_plt
*clk
= to_clk_plt(hw
);
110 plt_clk_reg_update(clk
, PMC_MASK_CLK_FREQ
, plt_parent_to_reg(index
));
115 static u8
plt_clk_get_parent(struct clk_hw
*hw
)
117 struct clk_plt
*clk
= to_clk_plt(hw
);
120 value
= readl(clk
->reg
);
122 return plt_reg_to_parent(value
);
125 static int plt_clk_enable(struct clk_hw
*hw
)
127 struct clk_plt
*clk
= to_clk_plt(hw
);
129 plt_clk_reg_update(clk
, PMC_MASK_CLK_CTL
, PMC_CLK_CTL_FORCE_ON
);
134 static void plt_clk_disable(struct clk_hw
*hw
)
136 struct clk_plt
*clk
= to_clk_plt(hw
);
138 plt_clk_reg_update(clk
, PMC_MASK_CLK_CTL
, PMC_CLK_CTL_FORCE_OFF
);
141 static int plt_clk_is_enabled(struct clk_hw
*hw
)
143 struct clk_plt
*clk
= to_clk_plt(hw
);
146 value
= readl(clk
->reg
);
148 return plt_reg_to_enabled(value
);
151 static const struct clk_ops plt_clk_ops
= {
152 .enable
= plt_clk_enable
,
153 .disable
= plt_clk_disable
,
154 .is_enabled
= plt_clk_is_enabled
,
155 .get_parent
= plt_clk_get_parent
,
156 .set_parent
= plt_clk_set_parent
,
157 .determine_rate
= __clk_mux_determine_rate
,
160 static struct clk_plt
*plt_clk_register(struct platform_device
*pdev
, int id
,
161 const struct pmc_clk_data
*pmc_data
,
162 const char **parent_names
,
165 struct clk_plt
*pclk
;
166 struct clk_init_data init
;
169 pclk
= devm_kzalloc(&pdev
->dev
, sizeof(*pclk
), GFP_KERNEL
);
171 return ERR_PTR(-ENOMEM
);
173 init
.name
= kasprintf(GFP_KERNEL
, "%s_%d", PLT_CLK_NAME_BASE
, id
);
174 init
.ops
= &plt_clk_ops
;
176 init
.parent_names
= parent_names
;
177 init
.num_parents
= num_parents
;
179 pclk
->hw
.init
= &init
;
180 pclk
->reg
= pmc_data
->base
+ PMC_CLK_CTL_OFFSET
+ id
* PMC_CLK_CTL_SIZE
;
181 spin_lock_init(&pclk
->lock
);
184 * On some systems, the pmc_plt_clocks already enabled by the
185 * firmware are being marked as critical to avoid them being
186 * gated by the clock framework.
188 if (pmc_data
->critical
&& plt_clk_is_enabled(&pclk
->hw
))
189 init
.flags
|= CLK_IS_CRITICAL
;
191 ret
= devm_clk_hw_register(&pdev
->dev
, &pclk
->hw
);
197 pclk
->lookup
= clkdev_hw_create(&pclk
->hw
, init
.name
, NULL
);
199 pclk
= ERR_PTR(-ENOMEM
);
208 static void plt_clk_unregister(struct clk_plt
*pclk
)
210 clkdev_drop(pclk
->lookup
);
213 static struct clk_plt_fixed
*plt_clk_register_fixed_rate(struct platform_device
*pdev
,
215 const char *parent_name
,
216 unsigned long fixed_rate
)
218 struct clk_plt_fixed
*pclk
;
220 pclk
= devm_kzalloc(&pdev
->dev
, sizeof(*pclk
), GFP_KERNEL
);
222 return ERR_PTR(-ENOMEM
);
224 pclk
->clk
= clk_hw_register_fixed_rate(&pdev
->dev
, name
, parent_name
,
226 if (IS_ERR(pclk
->clk
))
227 return ERR_CAST(pclk
->clk
);
229 pclk
->lookup
= clkdev_hw_create(pclk
->clk
, name
, NULL
);
231 clk_hw_unregister_fixed_rate(pclk
->clk
);
232 return ERR_PTR(-ENOMEM
);
238 static void plt_clk_unregister_fixed_rate(struct clk_plt_fixed
*pclk
)
240 clkdev_drop(pclk
->lookup
);
241 clk_hw_unregister_fixed_rate(pclk
->clk
);
244 static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data
*data
,
248 plt_clk_unregister_fixed_rate(data
->parents
[i
]);
251 static void plt_clk_free_parent_names_loop(const char **parent_names
,
255 kfree_const(parent_names
[i
]);
259 static void plt_clk_unregister_loop(struct clk_plt_data
*data
,
263 plt_clk_unregister(data
->clks
[i
]);
266 static const char **plt_clk_register_parents(struct platform_device
*pdev
,
267 struct clk_plt_data
*data
,
268 const struct pmc_clk
*clks
)
270 const char **parent_names
;
276 while (clks
[nparents
].name
)
279 data
->parents
= devm_kcalloc(&pdev
->dev
, nparents
,
280 sizeof(*data
->parents
), GFP_KERNEL
);
282 return ERR_PTR(-ENOMEM
);
284 parent_names
= kcalloc(nparents
, sizeof(*parent_names
),
287 return ERR_PTR(-ENOMEM
);
289 for (i
= 0; i
< nparents
; i
++) {
291 plt_clk_register_fixed_rate(pdev
, clks
[i
].name
,
294 if (IS_ERR(data
->parents
[i
])) {
295 err
= PTR_ERR(data
->parents
[i
]);
298 parent_names
[i
] = kstrdup_const(clks
[i
].name
, GFP_KERNEL
);
301 data
->nparents
= nparents
;
305 plt_clk_unregister_fixed_rate_loop(data
, i
);
306 plt_clk_free_parent_names_loop(parent_names
, i
);
310 static void plt_clk_unregister_parents(struct clk_plt_data
*data
)
312 plt_clk_unregister_fixed_rate_loop(data
, data
->nparents
);
315 static int plt_clk_probe(struct platform_device
*pdev
)
317 const struct pmc_clk_data
*pmc_data
;
318 const char **parent_names
;
319 struct clk_plt_data
*data
;
323 pmc_data
= dev_get_platdata(&pdev
->dev
);
324 if (!pmc_data
|| !pmc_data
->clks
)
327 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
331 parent_names
= plt_clk_register_parents(pdev
, data
, pmc_data
->clks
);
332 if (IS_ERR(parent_names
))
333 return PTR_ERR(parent_names
);
335 for (i
= 0; i
< PMC_CLK_NUM
; i
++) {
336 data
->clks
[i
] = plt_clk_register(pdev
, i
, pmc_data
,
337 parent_names
, data
->nparents
);
338 if (IS_ERR(data
->clks
[i
])) {
339 err
= PTR_ERR(data
->clks
[i
]);
340 goto err_unreg_clk_plt
;
343 data
->mclk_lookup
= clkdev_hw_create(&data
->clks
[3]->hw
, "mclk", NULL
);
344 if (!data
->mclk_lookup
) {
346 goto err_unreg_clk_plt
;
349 data
->ether_clk_lookup
= clkdev_hw_create(&data
->clks
[4]->hw
,
351 if (!data
->ether_clk_lookup
) {
356 plt_clk_free_parent_names_loop(parent_names
, data
->nparents
);
358 platform_set_drvdata(pdev
, data
);
362 clkdev_drop(data
->mclk_lookup
);
364 plt_clk_unregister_loop(data
, i
);
365 plt_clk_unregister_parents(data
);
366 plt_clk_free_parent_names_loop(parent_names
, data
->nparents
);
370 static int plt_clk_remove(struct platform_device
*pdev
)
372 struct clk_plt_data
*data
;
374 data
= platform_get_drvdata(pdev
);
376 clkdev_drop(data
->ether_clk_lookup
);
377 clkdev_drop(data
->mclk_lookup
);
378 plt_clk_unregister_loop(data
, PMC_CLK_NUM
);
379 plt_clk_unregister_parents(data
);
383 static struct platform_driver plt_clk_driver
= {
385 .name
= "clk-pmc-atom",
387 .probe
= plt_clk_probe
,
388 .remove
= plt_clk_remove
,
390 builtin_platform_driver(plt_clk_driver
);