2 * Clock Driver for HiFiBerry DAC Pro
4 * Author: Stuart MacLean
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.
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.
17 #include <linux/clk-provider.h>
18 #include <linux/clkdev.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
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
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
35 struct clk_hifiberry_hw
{
40 #define to_hifiberry_clk(_hw) container_of(_hw, struct clk_hifiberry_hw, hw)
42 static const struct of_device_id clk_hifiberry_dacpro_dt_ids
[] = {
43 { .compatible
= "hifiberry,dacpro-clk",},
46 MODULE_DEVICE_TABLE(of
, clk_hifiberry_dacpro_dt_ids
);
48 static unsigned long clk_hifiberry_dacpro_recalc_rate(struct clk_hw
*hw
,
49 unsigned long parent_rate
)
51 return (to_hifiberry_clk(hw
)->mode
== 0) ? CLK_44EN_RATE
:
55 static long clk_hifiberry_dacpro_round_rate(struct clk_hw
*hw
,
56 unsigned long rate
, unsigned long *parent_rate
)
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
;
65 long diff44Rate
= (long)(rate
- CLK_44EN_RATE
);
66 long diff48Rate
= (long)(CLK_48EN_RATE
- rate
);
68 if (diff44Rate
< diff48Rate
)
69 actual_rate
= (long)CLK_44EN_RATE
;
71 actual_rate
= (long)CLK_48EN_RATE
;
77 static int clk_hifiberry_dacpro_set_rate(struct clk_hw
*hw
,
78 unsigned long rate
, unsigned long parent_rate
)
80 unsigned long actual_rate
;
81 struct clk_hifiberry_hw
*clk
= to_hifiberry_clk(hw
);
83 actual_rate
= (unsigned long)clk_hifiberry_dacpro_round_rate(hw
, rate
,
85 clk
->mode
= (actual_rate
== CLK_44EN_RATE
) ? 0 : 1;
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
,
96 static int clk_hifiberry_dacpro_probe(struct platform_device
*pdev
)
99 struct clk_hifiberry_hw
*proclk
;
102 struct clk_init_data init
;
106 proclk
= kzalloc(sizeof(struct clk_hifiberry_hw
), GFP_KERNEL
);
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;
117 proclk
->hw
.init
= &init
;
119 clk
= devm_clk_register(dev
, &proclk
->hw
);
121 ret
= of_clk_add_provider(dev
->of_node
, of_clk_src_simple_get
,
124 dev_err(dev
, "Fail to register clock driver\n");
131 static int clk_hifiberry_dacpro_remove(struct platform_device
*pdev
)
133 of_clk_del_provider(pdev
->dev
.of_node
);
137 static struct platform_driver clk_hifiberry_dacpro_driver
= {
138 .probe
= clk_hifiberry_dacpro_probe
,
139 .remove
= clk_hifiberry_dacpro_remove
,
141 .name
= "clk-hifiberry-dacpro",
142 .of_match_table
= clk_hifiberry_dacpro_dt_ids
,
146 static int __init
clk_hifiberry_dacpro_init(void)
148 return platform_driver_register(&clk_hifiberry_dacpro_driver
);
150 core_initcall(clk_hifiberry_dacpro_init
);
152 static void __exit
clk_hifiberry_dacpro_exit(void)
154 platform_driver_unregister(&clk_hifiberry_dacpro_driver
);
156 module_exit(clk_hifiberry_dacpro_exit
);
158 MODULE_DESCRIPTION("HiFiBerry DAC Pro clock driver");
159 MODULE_LICENSE("GPL v2");
160 MODULE_ALIAS("platform:clk-hifiberry-dacpro");