1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 Chen-Yu Tsai
5 * Chen-Yu Tsai <wens@csie.org>
8 #include <linux/clk-provider.h>
11 #include <linux/of_address.h>
12 #include <linux/reset-controller.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
16 static DEFINE_SPINLOCK(ve_lock
);
18 #define SUN4I_VE_ENABLE 31
19 #define SUN4I_VE_DIVIDER_SHIFT 16
20 #define SUN4I_VE_DIVIDER_WIDTH 3
21 #define SUN4I_VE_RESET 0
24 * sunxi_ve_reset... - reset bit in ve clk registers handling
27 struct ve_reset_data
{
30 struct reset_controller_dev rcdev
;
33 static int sunxi_ve_reset_assert(struct reset_controller_dev
*rcdev
,
36 struct ve_reset_data
*data
= container_of(rcdev
,
42 spin_lock_irqsave(data
->lock
, flags
);
44 reg
= readl(data
->reg
);
45 writel(reg
& ~BIT(SUN4I_VE_RESET
), data
->reg
);
47 spin_unlock_irqrestore(data
->lock
, flags
);
52 static int sunxi_ve_reset_deassert(struct reset_controller_dev
*rcdev
,
55 struct ve_reset_data
*data
= container_of(rcdev
,
61 spin_lock_irqsave(data
->lock
, flags
);
63 reg
= readl(data
->reg
);
64 writel(reg
| BIT(SUN4I_VE_RESET
), data
->reg
);
66 spin_unlock_irqrestore(data
->lock
, flags
);
71 static int sunxi_ve_of_xlate(struct reset_controller_dev
*rcdev
,
72 const struct of_phandle_args
*reset_spec
)
74 if (WARN_ON(reset_spec
->args_count
!= 0))
80 static const struct reset_control_ops sunxi_ve_reset_ops
= {
81 .assert = sunxi_ve_reset_assert
,
82 .deassert
= sunxi_ve_reset_deassert
,
85 static void __init
sun4i_ve_clk_setup(struct device_node
*node
)
88 struct clk_divider
*div
;
89 struct clk_gate
*gate
;
90 struct ve_reset_data
*reset_data
;
92 const char *clk_name
= node
->name
;
96 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
100 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
104 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
108 of_property_read_string(node
, "clock-output-names", &clk_name
);
109 parent
= of_clk_get_parent_name(node
, 0);
112 gate
->bit_idx
= SUN4I_VE_ENABLE
;
113 gate
->lock
= &ve_lock
;
116 div
->shift
= SUN4I_VE_DIVIDER_SHIFT
;
117 div
->width
= SUN4I_VE_DIVIDER_WIDTH
;
118 div
->lock
= &ve_lock
;
120 clk
= clk_register_composite(NULL
, clk_name
, &parent
, 1,
122 &div
->hw
, &clk_divider_ops
,
123 &gate
->hw
, &clk_gate_ops
,
124 CLK_SET_RATE_PARENT
);
128 err
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
130 goto err_unregister_clk
;
132 reset_data
= kzalloc(sizeof(*reset_data
), GFP_KERNEL
);
134 goto err_del_provider
;
136 reset_data
->reg
= reg
;
137 reset_data
->lock
= &ve_lock
;
138 reset_data
->rcdev
.nr_resets
= 1;
139 reset_data
->rcdev
.ops
= &sunxi_ve_reset_ops
;
140 reset_data
->rcdev
.of_node
= node
;
141 reset_data
->rcdev
.of_xlate
= sunxi_ve_of_xlate
;
142 reset_data
->rcdev
.of_reset_n_cells
= 0;
143 err
= reset_controller_register(&reset_data
->rcdev
);
152 of_clk_del_provider(node
);
162 CLK_OF_DECLARE(sun4i_ve
, "allwinner,sun4i-a10-ve-clk",