2 * Copyright 2015 Heiko Stuebner <heiko@sntech.de>
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/slab.h>
16 #include <linux/clk-provider.h>
18 #include <linux/spinlock.h>
19 #include <linux/kernel.h>
22 struct rockchip_inv_clock
{
30 #define to_inv_clock(_hw) container_of(_hw, struct rockchip_inv_clock, hw)
32 #define INVERTER_MASK 0x1
34 static int rockchip_inv_get_phase(struct clk_hw
*hw
)
36 struct rockchip_inv_clock
*inv_clock
= to_inv_clock(hw
);
39 val
= readl(inv_clock
->reg
) >> inv_clock
->shift
;
44 static int rockchip_inv_set_phase(struct clk_hw
*hw
, int degrees
)
46 struct rockchip_inv_clock
*inv_clock
= to_inv_clock(hw
);
49 if (degrees
% 180 == 0) {
52 pr_err("%s: unsupported phase %d for %s\n",
53 __func__
, degrees
, clk_hw_get_name(hw
));
57 if (inv_clock
->flags
& ROCKCHIP_INVERTER_HIWORD_MASK
) {
58 writel(HIWORD_UPDATE(val
, INVERTER_MASK
, inv_clock
->shift
),
64 spin_lock_irqsave(inv_clock
->lock
, flags
);
66 reg
= readl(inv_clock
->reg
);
67 reg
&= ~BIT(inv_clock
->shift
);
69 writel(reg
, inv_clock
->reg
);
71 spin_unlock_irqrestore(inv_clock
->lock
, flags
);
77 static const struct clk_ops rockchip_inv_clk_ops
= {
78 .get_phase
= rockchip_inv_get_phase
,
79 .set_phase
= rockchip_inv_set_phase
,
82 struct clk
*rockchip_clk_register_inverter(const char *name
,
83 const char *const *parent_names
, u8 num_parents
,
84 void __iomem
*reg
, int shift
, int flags
,
87 struct clk_init_data init
;
88 struct rockchip_inv_clock
*inv_clock
;
91 inv_clock
= kmalloc(sizeof(*inv_clock
), GFP_KERNEL
);
93 return ERR_PTR(-ENOMEM
);
96 init
.num_parents
= num_parents
;
97 init
.flags
= CLK_SET_RATE_PARENT
;
98 init
.parent_names
= parent_names
;
99 init
.ops
= &rockchip_inv_clk_ops
;
101 inv_clock
->hw
.init
= &init
;
102 inv_clock
->reg
= reg
;
103 inv_clock
->shift
= shift
;
104 inv_clock
->flags
= flags
;
105 inv_clock
->lock
= lock
;
107 clk
= clk_register(NULL
, &inv_clock
->hw
);