dm: clk: Add support for decoding clocks from the device tree
[oweals/u-boot.git] / drivers / clk / clk-uclass.c
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <dm/lists.h>
13 #include <dm/root.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 ulong clk_get_rate(struct udevice *dev)
18 {
19         struct clk_ops *ops = clk_get_ops(dev);
20
21         if (!ops->get_rate)
22                 return -ENOSYS;
23
24         return ops->get_rate(dev);
25 }
26
27 ulong clk_set_rate(struct udevice *dev, ulong rate)
28 {
29         struct clk_ops *ops = clk_get_ops(dev);
30
31         if (!ops->set_rate)
32                 return -ENOSYS;
33
34         return ops->set_rate(dev, rate);
35 }
36
37 int clk_enable(struct udevice *dev, int periph)
38 {
39         struct clk_ops *ops = clk_get_ops(dev);
40
41         if (!ops->enable)
42                 return -ENOSYS;
43
44         return ops->enable(dev, periph);
45 }
46
47 ulong clk_get_periph_rate(struct udevice *dev, int periph)
48 {
49         struct clk_ops *ops = clk_get_ops(dev);
50
51         if (!ops->get_periph_rate)
52                 return -ENOSYS;
53
54         return ops->get_periph_rate(dev, periph);
55 }
56
57 ulong clk_set_periph_rate(struct udevice *dev, int periph, ulong rate)
58 {
59         struct clk_ops *ops = clk_get_ops(dev);
60
61         if (!ops->set_periph_rate)
62                 return -ENOSYS;
63
64         return ops->set_periph_rate(dev, periph, rate);
65 }
66
67 #if CONFIG_IS_ENABLED(OF_CONTROL)
68 int clk_get_by_index(struct udevice *dev, int index, struct udevice **clk_devp)
69 {
70         struct fdtdec_phandle_args args;
71         int ret;
72
73         assert(*clk_devp);
74         ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
75                                              "clocks", "#clock-cells", 0, index,
76                                              &args);
77         if (ret) {
78                 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
79                       __func__, ret);
80                 return ret;
81         }
82
83         ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, clk_devp);
84         if (ret) {
85                 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
86                       __func__, ret);
87                 return ret;
88         }
89         return args.args_count > 0 ? args.args[0] : 0;
90 }
91 #endif
92
93 UCLASS_DRIVER(clk) = {
94         .id             = UCLASS_CLK,
95         .name           = "clk",
96 };