1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4  */
5 
6 #include <linux/clk-provider.h>
7 #include <linux/slab.h>
8 #include <linux/err.h>
9 
10 #include "clk.h"
11 
12 static unsigned long clk_sync_source_recalc_rate(struct clk_hw *hw,
13 						 unsigned long parent_rate)
14 {
15 	struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
16 
17 	return sync->rate;
18 }
19 
20 static long clk_sync_source_round_rate(struct clk_hw *hw, unsigned long rate,
21 				       unsigned long *prate)
22 {
23 	struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
24 
25 	if (rate > sync->max_rate)
26 		return -EINVAL;
27 	else
28 		return rate;
29 }
30 
31 static int clk_sync_source_set_rate(struct clk_hw *hw, unsigned long rate,
32 				    unsigned long parent_rate)
33 {
34 	struct tegra_clk_sync_source *sync = to_clk_sync_source(hw);
35 
36 	sync->rate = rate;
37 	return 0;
38 }
39 
40 const struct clk_ops tegra_clk_sync_source_ops = {
41 	.round_rate = clk_sync_source_round_rate,
42 	.set_rate = clk_sync_source_set_rate,
43 	.recalc_rate = clk_sync_source_recalc_rate,
44 };
45 
46 struct clk *tegra_clk_register_sync_source(const char *name,
47 					   unsigned long max_rate)
48 {
49 	struct tegra_clk_sync_source *sync;
50 	struct clk_init_data init;
51 	struct clk *clk;
52 
53 	sync = kzalloc(sizeof(*sync), GFP_KERNEL);
54 	if (!sync) {
55 		pr_err("%s: could not allocate sync source clk\n", __func__);
56 		return ERR_PTR(-ENOMEM);
57 	}
58 
59 	sync->max_rate = max_rate;
60 
61 	init.ops = &tegra_clk_sync_source_ops;
62 	init.name = name;
63 	init.flags = 0;
64 	init.parent_names = NULL;
65 	init.num_parents = 0;
66 
67 	/* Data in .init is copied by clk_register(), so stack variable OK */
68 	sync->hw.init = &init;
69 
70 	clk = clk_register(NULL, &sync->hw);
71 	if (IS_ERR(clk))
72 		kfree(sync);
73 
74 	return clk;
75 }
76