Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 1 | // 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 Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_CLK |
| 8 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 9 | #include <common.h> |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 10 | #include <clk.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 11 | #include <clk-uclass.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 13 | #include <dm/device.h> |
| 14 | #include <dm/uclass.h> |
| 15 | #include <dm/lists.h> |
| 16 | #include <dm/device-internal.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 17 | |
| 18 | int 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 Fan | c179a17 | 2019-10-22 03:31:08 +0000 | [diff] [blame] | 26 | if (ret) { |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 27 | log_err("%s: failed to get %s device (parent of %s)\n", |
| 28 | __func__, parent_name, name); |
Dario Binacchi | 8e312c5 | 2020-05-02 17:38:11 +0200 | [diff] [blame] | 29 | } else { |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 30 | log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name, |
| 31 | parent->name, parent); |
Peng Fan | c179a17 | 2019-10-22 03:31:08 +0000 | [diff] [blame] | 32 | } |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 33 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 34 | drv = lists_driver_lookup_name(drv_name); |
| 35 | if (!drv) { |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 36 | log_err("%s: %s is not a valid driver name\n", |
| 37 | __func__, drv_name); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 38 | return -ENOENT; |
| 39 | } |
| 40 | |
Simon Glass | 6996c66 | 2020-11-28 17:50:03 -0700 | [diff] [blame] | 41 | ret = device_bind(parent, drv, name, NULL, ofnode_null(), &clk->dev); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 42 | if (ret) { |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 43 | log_err("%s: CLK: %s driver bind error [%d]!\n", __func__, name, |
| 44 | ret); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 45 | return ret; |
| 46 | } |
| 47 | |
Peng Fan | 30a6ebc | 2019-08-21 13:35:03 +0000 | [diff] [blame] | 48 | clk->enable_count = 0; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 49 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 50 | /* Store back pointer to clk from udevice */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 51 | /* FIXME: This is not allowed...should be allocated by driver model */ |
| 52 | dev_set_uclass_priv(clk->dev, clk); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | ulong clk_generic_get_rate(struct clk *clk) |
| 58 | { |
| 59 | return clk_get_parent_rate(clk); |
| 60 | } |
| 61 | |
| 62 | const char *clk_hw_get_name(const struct clk *hw) |
| 63 | { |
Claudiu Beznea | d3e49d0 | 2020-09-07 17:46:32 +0300 | [diff] [blame] | 64 | assert(hw); |
| 65 | assert(hw->dev); |
| 66 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 67 | return hw->dev->name; |
| 68 | } |
Peng Fan | 1d0a50a | 2019-07-31 07:01:23 +0000 | [diff] [blame] | 69 | |
| 70 | bool clk_dev_binded(struct clk *clk) |
| 71 | { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 72 | if (clk->dev && (dev_get_flags(clk->dev) & DM_FLAG_BOUND)) |
Peng Fan | 1d0a50a | 2019-07-31 07:01:23 +0000 | [diff] [blame] | 73 | return true; |
| 74 | |
| 75 | return false; |
| 76 | } |