Merge git://git.denx.de/u-boot-sunxi
[platform/kernel/u-boot.git] / drivers / clk / at91 / pmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Atmel Corporation
4  *               Wenyou.Yang <wenyou.yang@atmel.com>
5  */
6
7 #include <common.h>
8 #include <clk-uclass.h>
9 #include <dm.h>
10 #include <dm/lists.h>
11 #include <dm/util.h>
12 #include "pmc.h"
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 static const struct udevice_id at91_pmc_match[] = {
17         { .compatible = "atmel,at91rm9200-pmc" },
18         { .compatible = "atmel,at91sam9260-pmc" },
19         { .compatible = "atmel,at91sam9g45-pmc" },
20         { .compatible = "atmel,at91sam9n12-pmc" },
21         { .compatible = "atmel,at91sam9x5-pmc" },
22         { .compatible = "atmel,sama5d3-pmc" },
23         { .compatible = "atmel,sama5d2-pmc" },
24         {}
25 };
26
27 U_BOOT_DRIVER(at91_pmc) = {
28         .name = "at91-pmc",
29         .id = UCLASS_SIMPLE_BUS,
30         .of_match = at91_pmc_match,
31 };
32
33 /*---------------------------------------------------------*/
34
35 int at91_pmc_core_probe(struct udevice *dev)
36 {
37         struct pmc_platdata *plat = dev_get_platdata(dev);
38
39         dev = dev_get_parent(dev);
40
41         plat->reg_base = (struct at91_pmc *)devfdt_get_addr_ptr(dev);
42
43         return 0;
44 }
45
46 /**
47  * at91_clk_sub_device_bind() - for the at91 clock driver
48  * Recursively bind its children as clk devices.
49  *
50  * @return: 0 on success, or negative error code on failure
51  */
52 int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
53 {
54         const void *fdt = gd->fdt_blob;
55         int offset = dev_of_offset(dev);
56         bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
57         const char *name;
58         int ret;
59
60         for (offset = fdt_first_subnode(fdt, offset);
61              offset > 0;
62              offset = fdt_next_subnode(fdt, offset)) {
63                 if (pre_reloc_only &&
64                     !dm_fdt_pre_reloc(fdt, offset))
65                         continue;
66                 /*
67                  * If this node has "compatible" property, this is not
68                  * a clock sub-node, but a normal device. skip.
69                  */
70                 fdt_get_property(fdt, offset, "compatible", &ret);
71                 if (ret >= 0)
72                         continue;
73
74                 if (ret != -FDT_ERR_NOTFOUND)
75                         return ret;
76
77                 name = fdt_get_name(fdt, offset, NULL);
78                 if (!name)
79                         return -EINVAL;
80                 ret = device_bind_driver_to_node(dev, drv_name, name,
81                                         offset_to_ofnode(offset), NULL);
82                 if (ret)
83                         return ret;
84         }
85
86         return 0;
87 }
88
89 int at91_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
90 {
91         int periph;
92
93         if (args->args_count) {
94                 debug("Invalid args_count: %d\n", args->args_count);
95                 return -EINVAL;
96         }
97
98         periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
99                                  -1);
100         if (periph < 0)
101                 return -EINVAL;
102
103         clk->id = periph;
104
105         return 0;
106 }
107
108 int at91_clk_probe(struct udevice *dev)
109 {
110         struct udevice *dev_periph_container, *dev_pmc;
111         struct pmc_platdata *plat = dev_get_platdata(dev);
112
113         dev_periph_container = dev_get_parent(dev);
114         dev_pmc = dev_get_parent(dev_periph_container);
115
116         plat->reg_base = (struct at91_pmc *)devfdt_get_addr_ptr(dev_pmc);
117
118         return 0;
119 }