2 * Copyright (C) 2014 Free Electrons
4 * License Terms: GNU General Public License v2
5 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
7 * Allwinner A31 APB0 clock gates driver
11 #include <linux/clk-provider.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
17 #define SUN6I_APB0_GATES_MAX_SIZE 32
20 DECLARE_BITMAP(mask
, SUN6I_APB0_GATES_MAX_SIZE
);
23 static const struct gates_data sun6i_a31_apb0_gates __initconst
= {
27 static const struct gates_data sun8i_a23_apb0_gates __initconst
= {
31 static const struct of_device_id sun6i_a31_apb0_gates_clk_dt_ids
[] = {
32 { .compatible
= "allwinner,sun6i-a31-apb0-gates-clk", .data
= &sun6i_a31_apb0_gates
},
33 { .compatible
= "allwinner,sun8i-a23-apb0-gates-clk", .data
= &sun8i_a23_apb0_gates
},
36 MODULE_DEVICE_TABLE(of
, sun6i_a31_apb0_gates_clk_dt_ids
);
38 static int sun6i_a31_apb0_gates_clk_probe(struct platform_device
*pdev
)
40 struct device_node
*np
= pdev
->dev
.of_node
;
41 struct clk_onecell_data
*clk_data
;
42 const struct of_device_id
*device
;
43 const struct gates_data
*data
;
44 const char *clk_parent
;
55 device
= of_match_device(sun6i_a31_apb0_gates_clk_dt_ids
, &pdev
->dev
);
60 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
61 reg
= devm_ioremap_resource(&pdev
->dev
, r
);
65 clk_parent
= of_clk_get_parent_name(np
, 0);
69 clk_data
= devm_kzalloc(&pdev
->dev
, sizeof(struct clk_onecell_data
),
74 /* Worst-case size approximation and memory allocation */
75 ngates
= find_last_bit(data
->mask
, SUN6I_APB0_GATES_MAX_SIZE
);
76 clk_data
->clks
= devm_kcalloc(&pdev
->dev
, (ngates
+ 1),
77 sizeof(struct clk
*), GFP_KERNEL
);
81 for_each_set_bit(i
, data
->mask
, SUN6I_APB0_GATES_MAX_SIZE
) {
82 of_property_read_string_index(np
, "clock-output-names",
85 clk_data
->clks
[i
] = clk_register_gate(&pdev
->dev
, clk_name
,
86 clk_parent
, 0, reg
, i
,
88 WARN_ON(IS_ERR(clk_data
->clks
[i
]));
93 clk_data
->clk_num
= ngates
+ 1;
95 return of_clk_add_provider(np
, of_clk_src_onecell_get
, clk_data
);
98 static struct platform_driver sun6i_a31_apb0_gates_clk_driver
= {
100 .name
= "sun6i-a31-apb0-gates-clk",
101 .of_match_table
= sun6i_a31_apb0_gates_clk_dt_ids
,
103 .probe
= sun6i_a31_apb0_gates_clk_probe
,
105 module_platform_driver(sun6i_a31_apb0_gates_clk_driver
);
107 MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>");
108 MODULE_DESCRIPTION("Allwinner A31 APB0 gate clocks driver");
109 MODULE_LICENSE("GPL v2");