common: Drop log.h from common header
[pandora-u-boot.git] / drivers / clk / sunxi / clk_sunxi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Amarula Solutions.
4  * Author: Jagan Teki <jagan@amarulasolutions.com>
5  */
6
7 #include <common.h>
8 #include <clk-uclass.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <reset.h>
13 #include <asm/io.h>
14 #include <asm/arch/ccu.h>
15 #include <linux/log2.h>
16
17 static const struct ccu_clk_gate *priv_to_gate(struct ccu_priv *priv,
18                                                unsigned long id)
19 {
20         return &priv->desc->gates[id];
21 }
22
23 static int sunxi_set_gate(struct clk *clk, bool on)
24 {
25         struct ccu_priv *priv = dev_get_priv(clk->dev);
26         const struct ccu_clk_gate *gate = priv_to_gate(priv, clk->id);
27         u32 reg;
28
29         if (!(gate->flags & CCU_CLK_F_IS_VALID)) {
30                 printf("%s: (CLK#%ld) unhandled\n", __func__, clk->id);
31                 return 0;
32         }
33
34         debug("%s: (CLK#%ld) off#0x%x, BIT(%d)\n", __func__,
35               clk->id, gate->off, ilog2(gate->bit));
36
37         reg = readl(priv->base + gate->off);
38         if (on)
39                 reg |= gate->bit;
40         else
41                 reg &= ~gate->bit;
42
43         writel(reg, priv->base + gate->off);
44
45         return 0;
46 }
47
48 static int sunxi_clk_enable(struct clk *clk)
49 {
50         return sunxi_set_gate(clk, true);
51 }
52
53 static int sunxi_clk_disable(struct clk *clk)
54 {
55         return sunxi_set_gate(clk, false);
56 }
57
58 struct clk_ops sunxi_clk_ops = {
59         .enable = sunxi_clk_enable,
60         .disable = sunxi_clk_disable,
61 };
62
63 int sunxi_clk_probe(struct udevice *dev)
64 {
65         struct ccu_priv *priv = dev_get_priv(dev);
66         struct clk_bulk clk_bulk;
67         struct reset_ctl_bulk rst_bulk;
68         int ret;
69
70         priv->base = dev_read_addr_ptr(dev);
71         if (!priv->base)
72                 return -ENOMEM;
73
74         priv->desc = (const struct ccu_desc *)dev_get_driver_data(dev);
75         if (!priv->desc)
76                 return -EINVAL;
77
78         ret = clk_get_bulk(dev, &clk_bulk);
79         if (!ret)
80                 clk_enable_bulk(&clk_bulk);
81
82         ret = reset_get_bulk(dev, &rst_bulk);
83         if (!ret)
84                 reset_deassert_bulk(&rst_bulk);
85
86         return 0;
87 }