blob: be76d6c8f471bbe388f8e485cbb455d5abe1f960 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +03002/*
3 * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +03005 */
6
7#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +030010#include <dm/device.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060011#include <linux/printk.h>
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +030012
13#include "clk-lib.h"
14
15#define HZ_IN_MHZ 1000000
16#define ceil(x, y) ({ ulong __x = (x), __y = (y); (__x + __y - 1) / __y; })
17
18int soc_clk_ctl(const char *name, ulong *rate, enum clk_ctl_ops ctl)
19{
20 int ret;
21 ulong mhz_rate, priv_rate;
22 struct clk clk;
23
24 /* Dummy fmeas device, just to be able to use standard clk_* api */
25 struct udevice fmeas = {
26 .name = "clk-fmeas",
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +030027 };
Simon Glassa7ece582020-12-19 10:40:14 -070028 dev_set_ofnode(&fmeas, ofnode_path("/clk-fmeas"));
Eugeniy Paltsev24e026e2018-03-26 15:57:37 +030029
30 ret = clk_get_by_name(&fmeas, name, &clk);
31 if (ret) {
32 pr_err("clock '%s' not found, err=%d\n", name, ret);
33 return ret;
34 }
35
36 if (ctl & CLK_ON) {
37 ret = clk_enable(&clk);
38 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
39 return ret;
40 }
41
42 if ((ctl & CLK_SET) && rate) {
43 priv_rate = ctl & CLK_MHZ ? (*rate) * HZ_IN_MHZ : *rate;
44 ret = clk_set_rate(&clk, priv_rate);
45 if (ret)
46 return ret;
47 }
48
49 if (ctl & CLK_OFF) {
50 ret = clk_disable(&clk);
51 if (ret) {
52 pr_err("clock '%s' can't be disabled, err=%d\n", name, ret);
53 return ret;
54 }
55 }
56
57 priv_rate = clk_get_rate(&clk);
58
59 clk_free(&clk);
60
61 mhz_rate = ceil(priv_rate, HZ_IN_MHZ);
62
63 if (ctl & CLK_MHZ)
64 priv_rate = mhz_rate;
65
66 if ((ctl & CLK_GET) && rate)
67 *rate = priv_rate;
68
69 if ((ctl & CLK_PRINT) && (ctl & CLK_MHZ))
70 printf("HSDK: clock '%s' rate %lu MHz\n", name, priv_rate);
71 else if (ctl & CLK_PRINT)
72 printf("HSDK: clock '%s' rate %lu Hz\n", name, priv_rate);
73 else
74 debug("HSDK: clock '%s' rate %lu MHz\n", name, mhz_rate);
75
76 return 0;
77}