blob: 23d042aabb4f7724d2cf1602b8a2b1f4028a8c41 [file] [log] [blame]
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +02001/*
2 * Copyright (C) 2014 Free Electrons
3 *
4 * License Terms: GNU General Public License v2
5 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
6 *
7 * Allwinner A31 APB0 clock gates driver
8 *
9 */
10
11#include <linux/clk-provider.h>
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080012#include <linux/clkdev.h>
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020013#include <linux/module.h>
14#include <linux/of.h>
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080015#include <linux/of_device.h>
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020016#include <linux/platform_device.h>
17
18#define SUN6I_APB0_GATES_MAX_SIZE 32
19
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080020struct gates_data {
21 DECLARE_BITMAP(mask, SUN6I_APB0_GATES_MAX_SIZE);
22};
23
24static const struct gates_data sun6i_a31_apb0_gates __initconst = {
25 .mask = {0x7F},
26};
27
Chen-Yu Tsai6c1d66f2014-07-09 15:54:35 +080028static const struct gates_data sun8i_a23_apb0_gates __initconst = {
29 .mask = {0x5D},
30};
31
Emilio López381c1cc2014-07-28 00:49:43 -030032static const struct of_device_id sun6i_a31_apb0_gates_clk_dt_ids[] = {
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080033 { .compatible = "allwinner,sun6i-a31-apb0-gates-clk", .data = &sun6i_a31_apb0_gates },
Chen-Yu Tsai6c1d66f2014-07-09 15:54:35 +080034 { .compatible = "allwinner,sun8i-a23-apb0-gates-clk", .data = &sun8i_a23_apb0_gates },
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080035 { /* sentinel */ }
36};
Luis de Bethencourtf01745f2015-08-28 14:43:33 +020037MODULE_DEVICE_TABLE(of, sun6i_a31_apb0_gates_clk_dt_ids);
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080038
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020039static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev)
40{
41 struct device_node *np = pdev->dev.of_node;
42 struct clk_onecell_data *clk_data;
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080043 const struct of_device_id *device;
44 const struct gates_data *data;
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020045 const char *clk_parent;
46 const char *clk_name;
47 struct resource *r;
48 void __iomem *reg;
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020049 int ngates;
50 int i;
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080051 int j = 0;
52
53 if (!np)
54 return -ENODEV;
55
56 device = of_match_device(sun6i_a31_apb0_gates_clk_dt_ids, &pdev->dev);
57 if (!device)
58 return -ENODEV;
59 data = device->data;
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020060
61 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
62 reg = devm_ioremap_resource(&pdev->dev, r);
Himangi Saraogic3dcac82014-06-28 22:53:55 +053063 if (IS_ERR(reg))
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020064 return PTR_ERR(reg);
65
66 clk_parent = of_clk_get_parent_name(np, 0);
67 if (!clk_parent)
68 return -EINVAL;
69
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020070 clk_data = devm_kzalloc(&pdev->dev, sizeof(struct clk_onecell_data),
71 GFP_KERNEL);
72 if (!clk_data)
73 return -ENOMEM;
74
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080075 /* Worst-case size approximation and memory allocation */
76 ngates = find_last_bit(data->mask, SUN6I_APB0_GATES_MAX_SIZE);
77 clk_data->clks = devm_kcalloc(&pdev->dev, (ngates + 1),
78 sizeof(struct clk *), GFP_KERNEL);
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020079 if (!clk_data->clks)
80 return -ENOMEM;
81
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080082 for_each_set_bit(i, data->mask, SUN6I_APB0_GATES_MAX_SIZE) {
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020083 of_property_read_string_index(np, "clock-output-names",
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080084 j, &clk_name);
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020085
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080086 clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name,
87 clk_parent, 0, reg, i,
88 0, NULL);
89 WARN_ON(IS_ERR(clk_data->clks[i]));
90 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020091
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080092 j++;
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020093 }
94
Chen-Yu Tsaib72efd02014-07-09 15:54:34 +080095 clk_data->clk_num = ngates + 1;
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +020096
97 return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
98}
99
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +0200100static struct platform_driver sun6i_a31_apb0_gates_clk_driver = {
101 .driver = {
102 .name = "sun6i-a31-apb0-gates-clk",
Boris BREZILLONc8a76ca2014-05-15 10:55:11 +0200103 .of_match_table = sun6i_a31_apb0_gates_clk_dt_ids,
104 },
105 .probe = sun6i_a31_apb0_gates_clk_probe,
106};
107module_platform_driver(sun6i_a31_apb0_gates_clk_driver);
108
109MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>");
110MODULE_DESCRIPTION("Allwinner A31 APB0 gate clocks driver");
111MODULE_LICENSE("GPL v2");