blob: a5a3461b66c551976c7667f202ee0f24d7199215 [file] [log] [blame]
Lukasz Majewski1d7993d2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 */
6
Patrick Delaunay560e1e02021-11-19 15:12:07 +01007#define LOG_CATEGORY UCLASS_CLK
8
Lukasz Majewski1d7993d2019-06-24 15:50:45 +02009#include <common.h>
Patrick Delaunay572c4462021-11-19 15:12:06 +010010#include <clk.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020011#include <clk-uclass.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020013#include <dm/device.h>
14#include <dm/uclass.h>
15#include <dm/lists.h>
16#include <dm/device-internal.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020017
18int clk_register(struct clk *clk, const char *drv_name,
19 const char *name, const char *parent_name)
20{
21 struct udevice *parent;
22 struct driver *drv;
23 int ret;
24
25 ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
Peng Fan5dcac2b2019-10-22 03:31:08 +000026 if (ret) {
Patrick Delaunay560e1e02021-11-19 15:12:07 +010027 log_err("%s: failed to get %s device (parent of %s)\n",
28 __func__, parent_name, name);
Dario Binacchi7b0830d2020-05-02 17:38:11 +020029 } else {
Patrick Delaunay560e1e02021-11-19 15:12:07 +010030 log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
31 parent->name, parent);
Peng Fan5dcac2b2019-10-22 03:31:08 +000032 }
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020033
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020034 drv = lists_driver_lookup_name(drv_name);
35 if (!drv) {
Patrick Delaunay560e1e02021-11-19 15:12:07 +010036 log_err("%s: %s is not a valid driver name\n",
37 __func__, drv_name);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020038 return -ENOENT;
39 }
40
Simon Glassa2703ce2020-11-28 17:50:03 -070041 ret = device_bind(parent, drv, name, NULL, ofnode_null(), &clk->dev);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020042 if (ret) {
Patrick Delaunay560e1e02021-11-19 15:12:07 +010043 log_err("%s: CLK: %s driver bind error [%d]!\n", __func__, name,
44 ret);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020045 return ret;
46 }
47
Peng Fane6849e22019-08-21 13:35:03 +000048 clk->enable_count = 0;
Simon Glass0fd3d912020-12-22 19:30:28 -070049
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020050 /* Store back pointer to clk from udevice */
Simon Glass0fd3d912020-12-22 19:30:28 -070051 /* FIXME: This is not allowed...should be allocated by driver model */
52 dev_set_uclass_priv(clk->dev, clk);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020053
54 return 0;
55}
56
57ulong clk_generic_get_rate(struct clk *clk)
58{
59 return clk_get_parent_rate(clk);
60}
61
62const char *clk_hw_get_name(const struct clk *hw)
63{
Claudiu Bezneab04da9f2020-09-07 17:46:32 +030064 assert(hw);
65 assert(hw->dev);
66
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020067 return hw->dev->name;
68}
Peng Fan24576122019-07-31 07:01:23 +000069
70bool clk_dev_binded(struct clk *clk)
71{
Simon Glass73466df2020-12-19 10:40:10 -070072 if (clk->dev && (dev_get_flags(clk->dev) & DM_FLAG_BOUND))
Peng Fan24576122019-07-31 07:01:23 +000073 return true;
74
75 return false;
76}
Sean Anderson3a11b5a2022-03-20 16:34:45 -040077
78/* Helper functions for clock ops */
79
80ulong ccf_clk_get_rate(struct clk *clk)
81{
82 struct clk *c;
83 int err = clk_get_by_id(clk->id, &c);
84
85 if (err)
86 return err;
87 return clk_get_rate(c);
88}
89
90ulong ccf_clk_set_rate(struct clk *clk, unsigned long rate)
91{
92 struct clk *c;
93 int err = clk_get_by_id(clk->id, &c);
94
95 if (err)
96 return err;
97 return clk_set_rate(c, rate);
98}
99
100int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
101{
102 struct clk *c, *p;
103 int err = clk_get_by_id(clk->id, &c);
104
105 if (err)
106 return err;
107
108 err = clk_get_by_id(parent->id, &p);
109 if (err)
110 return err;
111
112 return clk_set_parent(c, p);
113}
114
115static int ccf_clk_endisable(struct clk *clk, bool enable)
116{
117 struct clk *c;
118 int err = clk_get_by_id(clk->id, &c);
119
120 if (err)
121 return err;
122 return enable ? clk_enable(c) : clk_disable(c);
123}
124
125int ccf_clk_enable(struct clk *clk)
126{
127 return ccf_clk_endisable(clk, true);
128}
129
130int ccf_clk_disable(struct clk *clk)
131{
132 return ccf_clk_endisable(clk, false);
133}
134
135const struct clk_ops ccf_clk_ops = {
136 .set_rate = ccf_clk_set_rate,
137 .get_rate = ccf_clk_get_rate,
138 .set_parent = ccf_clk_set_parent,
139 .enable = ccf_clk_enable,
140 .disable = ccf_clk_disable,
141};