2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/of_address.h>
20 #define SMD_SOURCE_MAX 2
22 #define SMD_DIV_SHIFT 8
23 #define SMD_MAX_DIV 0xf
25 struct at91sam9x5_clk_smd
{
30 #define to_at91sam9x5_clk_smd(hw) \
31 container_of(hw, struct at91sam9x5_clk_smd, hw)
33 static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw
*hw
,
34 unsigned long parent_rate
)
38 struct at91sam9x5_clk_smd
*smd
= to_at91sam9x5_clk_smd(hw
);
39 struct at91_pmc
*pmc
= smd
->pmc
;
41 tmp
= pmc_read(pmc
, AT91_PMC_SMD
);
42 smddiv
= (tmp
& AT91_PMC_SMD_DIV
) >> SMD_DIV_SHIFT
;
43 return parent_rate
/ (smddiv
+ 1);
46 static long at91sam9x5_clk_smd_round_rate(struct clk_hw
*hw
, unsigned long rate
,
47 unsigned long *parent_rate
)
50 unsigned long bestrate
;
53 if (rate
>= *parent_rate
)
56 div
= *parent_rate
/ rate
;
57 if (div
> SMD_MAX_DIV
)
58 return *parent_rate
/ (SMD_MAX_DIV
+ 1);
60 bestrate
= *parent_rate
/ div
;
61 tmp
= *parent_rate
/ (div
+ 1);
62 if (bestrate
- rate
> rate
- tmp
)
68 static int at91sam9x5_clk_smd_set_parent(struct clk_hw
*hw
, u8 index
)
71 struct at91sam9x5_clk_smd
*smd
= to_at91sam9x5_clk_smd(hw
);
72 struct at91_pmc
*pmc
= smd
->pmc
;
76 tmp
= pmc_read(pmc
, AT91_PMC_SMD
) & ~AT91_PMC_SMDS
;
79 pmc_write(pmc
, AT91_PMC_SMD
, tmp
);
83 static u8
at91sam9x5_clk_smd_get_parent(struct clk_hw
*hw
)
85 struct at91sam9x5_clk_smd
*smd
= to_at91sam9x5_clk_smd(hw
);
86 struct at91_pmc
*pmc
= smd
->pmc
;
88 return pmc_read(pmc
, AT91_PMC_SMD
) & AT91_PMC_SMDS
;
91 static int at91sam9x5_clk_smd_set_rate(struct clk_hw
*hw
, unsigned long rate
,
92 unsigned long parent_rate
)
95 struct at91sam9x5_clk_smd
*smd
= to_at91sam9x5_clk_smd(hw
);
96 struct at91_pmc
*pmc
= smd
->pmc
;
97 unsigned long div
= parent_rate
/ rate
;
99 if (parent_rate
% rate
|| div
< 1 || div
> (SMD_MAX_DIV
+ 1))
101 tmp
= pmc_read(pmc
, AT91_PMC_SMD
) & ~AT91_PMC_SMD_DIV
;
102 tmp
|= (div
- 1) << SMD_DIV_SHIFT
;
103 pmc_write(pmc
, AT91_PMC_SMD
, tmp
);
108 static const struct clk_ops at91sam9x5_smd_ops
= {
109 .recalc_rate
= at91sam9x5_clk_smd_recalc_rate
,
110 .round_rate
= at91sam9x5_clk_smd_round_rate
,
111 .get_parent
= at91sam9x5_clk_smd_get_parent
,
112 .set_parent
= at91sam9x5_clk_smd_set_parent
,
113 .set_rate
= at91sam9x5_clk_smd_set_rate
,
116 static struct clk
* __init
117 at91sam9x5_clk_register_smd(struct at91_pmc
*pmc
, const char *name
,
118 const char **parent_names
, u8 num_parents
)
120 struct at91sam9x5_clk_smd
*smd
;
121 struct clk
*clk
= NULL
;
122 struct clk_init_data init
;
124 smd
= kzalloc(sizeof(*smd
), GFP_KERNEL
);
126 return ERR_PTR(-ENOMEM
);
129 init
.ops
= &at91sam9x5_smd_ops
;
130 init
.parent_names
= parent_names
;
131 init
.num_parents
= num_parents
;
132 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
;
134 smd
->hw
.init
= &init
;
137 clk
= clk_register(NULL
, &smd
->hw
);
144 void __init
of_at91sam9x5_clk_smd_setup(struct device_node
*np
,
145 struct at91_pmc
*pmc
)
150 const char *parent_names
[SMD_SOURCE_MAX
];
151 const char *name
= np
->name
;
153 num_parents
= of_count_phandle_with_args(np
, "clocks", "#clock-cells");
154 if (num_parents
<= 0 || num_parents
> SMD_SOURCE_MAX
)
157 for (i
= 0; i
< num_parents
; i
++) {
158 parent_names
[i
] = of_clk_get_parent_name(np
, i
);
159 if (!parent_names
[i
])
163 of_property_read_string(np
, "clock-output-names", &name
);
165 clk
= at91sam9x5_clk_register_smd(pmc
, name
, parent_names
,
170 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);