]>
Commit | Line | Data |
---|---|---|
8111dbec DM |
1 | /* |
2 | * Clock Driver for HiFiBerry DAC Pro | |
3 | * | |
4 | * Author: Stuart MacLean | |
5 | * Copyright 2015 | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * version 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <linux/clk-provider.h> | |
18 | #include <linux/clkdev.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/of.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/platform_device.h> | |
24 | ||
25 | /* Clock rate of CLK44EN attached to GPIO6 pin */ | |
26 | #define CLK_44EN_RATE 22579200UL | |
27 | /* Clock rate of CLK48EN attached to GPIO3 pin */ | |
28 | #define CLK_48EN_RATE 24576000UL | |
29 | ||
30 | /** | |
31 | * struct hifiberry_dacpro_clk - Common struct to the HiFiBerry DAC Pro | |
32 | * @hw: clk_hw for the common clk framework | |
33 | * @mode: 0 => CLK44EN, 1 => CLK48EN | |
34 | */ | |
35 | struct clk_hifiberry_hw { | |
36 | struct clk_hw hw; | |
37 | uint8_t mode; | |
38 | }; | |
39 | ||
40 | #define to_hifiberry_clk(_hw) container_of(_hw, struct clk_hifiberry_hw, hw) | |
41 | ||
42 | static const struct of_device_id clk_hifiberry_dacpro_dt_ids[] = { | |
43 | { .compatible = "hifiberry,dacpro-clk",}, | |
44 | { } | |
45 | }; | |
46 | MODULE_DEVICE_TABLE(of, clk_hifiberry_dacpro_dt_ids); | |
47 | ||
48 | static unsigned long clk_hifiberry_dacpro_recalc_rate(struct clk_hw *hw, | |
49 | unsigned long parent_rate) | |
50 | { | |
51 | return (to_hifiberry_clk(hw)->mode == 0) ? CLK_44EN_RATE : | |
52 | CLK_48EN_RATE; | |
53 | } | |
54 | ||
55 | static long clk_hifiberry_dacpro_round_rate(struct clk_hw *hw, | |
56 | unsigned long rate, unsigned long *parent_rate) | |
57 | { | |
58 | long actual_rate; | |
59 | ||
60 | if (rate <= CLK_44EN_RATE) { | |
61 | actual_rate = (long)CLK_44EN_RATE; | |
62 | } else if (rate >= CLK_48EN_RATE) { | |
63 | actual_rate = (long)CLK_48EN_RATE; | |
64 | } else { | |
65 | long diff44Rate = (long)(rate - CLK_44EN_RATE); | |
66 | long diff48Rate = (long)(CLK_48EN_RATE - rate); | |
67 | ||
68 | if (diff44Rate < diff48Rate) | |
69 | actual_rate = (long)CLK_44EN_RATE; | |
70 | else | |
71 | actual_rate = (long)CLK_48EN_RATE; | |
72 | } | |
73 | return actual_rate; | |
74 | } | |
75 | ||
76 | ||
77 | static int clk_hifiberry_dacpro_set_rate(struct clk_hw *hw, | |
78 | unsigned long rate, unsigned long parent_rate) | |
79 | { | |
80 | unsigned long actual_rate; | |
81 | struct clk_hifiberry_hw *clk = to_hifiberry_clk(hw); | |
82 | ||
83 | actual_rate = (unsigned long)clk_hifiberry_dacpro_round_rate(hw, rate, | |
84 | &parent_rate); | |
85 | clk->mode = (actual_rate == CLK_44EN_RATE) ? 0 : 1; | |
86 | return 0; | |
87 | } | |
88 | ||
89 | ||
90 | const struct clk_ops clk_hifiberry_dacpro_rate_ops = { | |
91 | .recalc_rate = clk_hifiberry_dacpro_recalc_rate, | |
92 | .round_rate = clk_hifiberry_dacpro_round_rate, | |
93 | .set_rate = clk_hifiberry_dacpro_set_rate, | |
94 | }; | |
95 | ||
96 | static int clk_hifiberry_dacpro_probe(struct platform_device *pdev) | |
97 | { | |
98 | int ret; | |
99 | struct clk_hifiberry_hw *proclk; | |
100 | struct clk *clk; | |
101 | struct device *dev; | |
102 | struct clk_init_data init; | |
103 | ||
104 | dev = &pdev->dev; | |
105 | ||
106 | proclk = kzalloc(sizeof(struct clk_hifiberry_hw), GFP_KERNEL); | |
107 | if (!proclk) | |
108 | return -ENOMEM; | |
109 | ||
110 | init.name = "clk-hifiberry-dacpro"; | |
111 | init.ops = &clk_hifiberry_dacpro_rate_ops; | |
112 | init.flags = CLK_IS_BASIC; | |
113 | init.parent_names = NULL; | |
114 | init.num_parents = 0; | |
115 | ||
116 | proclk->mode = 0; | |
117 | proclk->hw.init = &init; | |
118 | ||
119 | clk = devm_clk_register(dev, &proclk->hw); | |
120 | if (!IS_ERR(clk)) { | |
121 | ret = of_clk_add_provider(dev->of_node, of_clk_src_simple_get, | |
122 | clk); | |
123 | } else { | |
124 | dev_err(dev, "Fail to register clock driver\n"); | |
125 | kfree(proclk); | |
126 | ret = PTR_ERR(clk); | |
127 | } | |
128 | return ret; | |
129 | } | |
130 | ||
131 | static int clk_hifiberry_dacpro_remove(struct platform_device *pdev) | |
132 | { | |
133 | of_clk_del_provider(pdev->dev.of_node); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static struct platform_driver clk_hifiberry_dacpro_driver = { | |
138 | .probe = clk_hifiberry_dacpro_probe, | |
139 | .remove = clk_hifiberry_dacpro_remove, | |
140 | .driver = { | |
141 | .name = "clk-hifiberry-dacpro", | |
142 | .of_match_table = clk_hifiberry_dacpro_dt_ids, | |
143 | }, | |
144 | }; | |
145 | ||
146 | static int __init clk_hifiberry_dacpro_init(void) | |
147 | { | |
148 | return platform_driver_register(&clk_hifiberry_dacpro_driver); | |
149 | } | |
150 | core_initcall(clk_hifiberry_dacpro_init); | |
151 | ||
152 | static void __exit clk_hifiberry_dacpro_exit(void) | |
153 | { | |
154 | platform_driver_unregister(&clk_hifiberry_dacpro_driver); | |
155 | } | |
156 | module_exit(clk_hifiberry_dacpro_exit); | |
157 | ||
158 | MODULE_DESCRIPTION("HiFiBerry DAC Pro clock driver"); | |
159 | MODULE_LICENSE("GPL v2"); | |
160 | MODULE_ALIAS("platform:clk-hifiberry-dacpro"); |