Thomas Gleixner | 0376148 | 2019-05-28 09:57:24 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Free Electrons |
| 4 | * |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 5 | * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> |
| 6 | * |
| 7 | * Allwinner A31 APB0 clock gates driver |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/clk-provider.h> |
Paul Gortmaker | 439a36d | 2016-07-04 17:12:18 -0400 | [diff] [blame] | 11 | #include <linux/init.h> |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 12 | #include <linux/of.h> |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 13 | #include <linux/of_device.h> |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
| 15 | |
| 16 | #define SUN6I_APB0_GATES_MAX_SIZE 32 |
| 17 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 18 | struct gates_data { |
| 19 | DECLARE_BITMAP(mask, SUN6I_APB0_GATES_MAX_SIZE); |
| 20 | }; |
| 21 | |
| 22 | static const struct gates_data sun6i_a31_apb0_gates __initconst = { |
| 23 | .mask = {0x7F}, |
| 24 | }; |
| 25 | |
Chen-Yu Tsai | 6c1d66f | 2014-07-09 15:54:35 +0800 | [diff] [blame] | 26 | static const struct gates_data sun8i_a23_apb0_gates __initconst = { |
| 27 | .mask = {0x5D}, |
| 28 | }; |
| 29 | |
Emilio López | 381c1cc | 2014-07-28 00:49:43 -0300 | [diff] [blame] | 30 | static const struct of_device_id sun6i_a31_apb0_gates_clk_dt_ids[] = { |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 31 | { .compatible = "allwinner,sun6i-a31-apb0-gates-clk", .data = &sun6i_a31_apb0_gates }, |
Chen-Yu Tsai | 6c1d66f | 2014-07-09 15:54:35 +0800 | [diff] [blame] | 32 | { .compatible = "allwinner,sun8i-a23-apb0-gates-clk", .data = &sun8i_a23_apb0_gates }, |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 33 | { /* sentinel */ } |
| 34 | }; |
| 35 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 36 | static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev) |
| 37 | { |
| 38 | struct device_node *np = pdev->dev.of_node; |
| 39 | struct clk_onecell_data *clk_data; |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 40 | const struct gates_data *data; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 41 | const char *clk_parent; |
| 42 | const char *clk_name; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 43 | void __iomem *reg; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 44 | int ngates; |
| 45 | int i; |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 46 | int j = 0; |
| 47 | |
| 48 | if (!np) |
| 49 | return -ENODEV; |
| 50 | |
Corentin Labbe | 5d22a61 | 2019-11-10 16:35:20 +0000 | [diff] [blame] | 51 | data = of_device_get_match_data(&pdev->dev); |
| 52 | if (!data) |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 53 | return -ENODEV; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 54 | |
Cai Huoqing | ac57ffb | 2021-09-07 16:53:04 +0800 | [diff] [blame] | 55 | reg = devm_platform_ioremap_resource(pdev, 0); |
Himangi Saraogi | c3dcac8 | 2014-06-28 22:53:55 +0530 | [diff] [blame] | 56 | if (IS_ERR(reg)) |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 57 | return PTR_ERR(reg); |
| 58 | |
| 59 | clk_parent = of_clk_get_parent_name(np, 0); |
| 60 | if (!clk_parent) |
| 61 | return -EINVAL; |
| 62 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 63 | clk_data = devm_kzalloc(&pdev->dev, sizeof(struct clk_onecell_data), |
| 64 | GFP_KERNEL); |
| 65 | if (!clk_data) |
| 66 | return -ENOMEM; |
| 67 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 68 | /* Worst-case size approximation and memory allocation */ |
| 69 | ngates = find_last_bit(data->mask, SUN6I_APB0_GATES_MAX_SIZE); |
| 70 | clk_data->clks = devm_kcalloc(&pdev->dev, (ngates + 1), |
| 71 | sizeof(struct clk *), GFP_KERNEL); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 72 | if (!clk_data->clks) |
| 73 | return -ENOMEM; |
| 74 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 75 | for_each_set_bit(i, data->mask, SUN6I_APB0_GATES_MAX_SIZE) { |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 76 | of_property_read_string_index(np, "clock-output-names", |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 77 | j, &clk_name); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 78 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 79 | clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name, |
| 80 | clk_parent, 0, reg, i, |
| 81 | 0, NULL); |
| 82 | WARN_ON(IS_ERR(clk_data->clks[i])); |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 83 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 84 | j++; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Chen-Yu Tsai | b72efd0 | 2014-07-09 15:54:34 +0800 | [diff] [blame] | 87 | clk_data->clk_num = ngates + 1; |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 88 | |
| 89 | return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); |
| 90 | } |
| 91 | |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 92 | static struct platform_driver sun6i_a31_apb0_gates_clk_driver = { |
| 93 | .driver = { |
| 94 | .name = "sun6i-a31-apb0-gates-clk", |
Boris BREZILLON | c8a76ca | 2014-05-15 10:55:11 +0200 | [diff] [blame] | 95 | .of_match_table = sun6i_a31_apb0_gates_clk_dt_ids, |
| 96 | }, |
| 97 | .probe = sun6i_a31_apb0_gates_clk_probe, |
| 98 | }; |
Paul Gortmaker | 439a36d | 2016-07-04 17:12:18 -0400 | [diff] [blame] | 99 | builtin_platform_driver(sun6i_a31_apb0_gates_clk_driver); |