Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 1 | /* |
| 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 Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 12 | #include <linux/clkdev.h> |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 15 | #include <linux/of_device.h> |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | |
| 18 | #define SUN6I_APB0_GATES_MAX_SIZE 32 |
| 19 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 20 | struct gates_data { |
| 21 | DECLARE_BITMAP(mask, SUN6I_APB0_GATES_MAX_SIZE); |
| 22 | }; |
| 23 | |
| 24 | static const struct gates_data sun6i_a31_apb0_gates __initconst = { |
| 25 | .mask = {0x7F}, |
| 26 | }; |
| 27 | |
| 28 | const struct of_device_id sun6i_a31_apb0_gates_clk_dt_ids[] = { |
| 29 | { .compatible = "allwinner,sun6i-a31-apb0-gates-clk", .data = &sun6i_a31_apb0_gates }, |
| 30 | { /* sentinel */ } |
| 31 | }; |
| 32 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 33 | static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev) |
| 34 | { |
| 35 | struct device_node *np = pdev->dev.of_node; |
| 36 | struct clk_onecell_data *clk_data; |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 37 | const struct of_device_id *device; |
| 38 | const struct gates_data *data; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 39 | const char *clk_parent; |
| 40 | const char *clk_name; |
| 41 | struct resource *r; |
| 42 | void __iomem *reg; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 43 | int ngates; |
| 44 | int i; |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 45 | int j = 0; |
| 46 | |
| 47 | if (!np) |
| 48 | return -ENODEV; |
| 49 | |
| 50 | device = of_match_device(sun6i_a31_apb0_gates_clk_dt_ids, &pdev->dev); |
| 51 | if (!device) |
| 52 | return -ENODEV; |
| 53 | data = device->data; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 54 | |
| 55 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 56 | reg = devm_ioremap_resource(&pdev->dev, r); |
| 57 | if (!reg) |
| 58 | return PTR_ERR(reg); |
| 59 | |
| 60 | clk_parent = of_clk_get_parent_name(np, 0); |
| 61 | if (!clk_parent) |
| 62 | return -EINVAL; |
| 63 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 64 | clk_data = devm_kzalloc(&pdev->dev, sizeof(struct clk_onecell_data), |
| 65 | GFP_KERNEL); |
| 66 | if (!clk_data) |
| 67 | return -ENOMEM; |
| 68 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 69 | /* Worst-case size approximation and memory allocation */ |
| 70 | ngates = find_last_bit(data->mask, SUN6I_APB0_GATES_MAX_SIZE); |
| 71 | clk_data->clks = devm_kcalloc(&pdev->dev, (ngates + 1), |
| 72 | sizeof(struct clk *), GFP_KERNEL); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 73 | if (!clk_data->clks) |
| 74 | return -ENOMEM; |
| 75 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 76 | for_each_set_bit(i, data->mask, SUN6I_APB0_GATES_MAX_SIZE) { |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 77 | of_property_read_string_index(np, "clock-output-names", |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 78 | j, &clk_name); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 79 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 80 | clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name, |
| 81 | clk_parent, 0, reg, i, |
| 82 | 0, NULL); |
| 83 | WARN_ON(IS_ERR(clk_data->clks[i])); |
| 84 | clk_register_clkdev(clk_data->clks[i], clk_name, NULL); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 85 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 86 | j++; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame^] | 89 | clk_data->clk_num = ngates + 1; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 90 | |
| 91 | return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); |
| 92 | } |
| 93 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 94 | static struct platform_driver sun6i_a31_apb0_gates_clk_driver = { |
| 95 | .driver = { |
| 96 | .name = "sun6i-a31-apb0-gates-clk", |
| 97 | .owner = THIS_MODULE, |
| 98 | .of_match_table = sun6i_a31_apb0_gates_clk_dt_ids, |
| 99 | }, |
| 100 | .probe = sun6i_a31_apb0_gates_clk_probe, |
| 101 | }; |
| 102 | module_platform_driver(sun6i_a31_apb0_gates_clk_driver); |
| 103 | |
| 104 | MODULE_AUTHOR("Boris BREZILLON <boris.brezillon@free-electrons.com>"); |
| 105 | MODULE_DESCRIPTION("Allwinner A31 APB0 gate clocks driver"); |
| 106 | MODULE_LICENSE("GPL v2"); |