Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments System Control Interface (TI SCI) clock driver |
| 4 | * |
| 5 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * Andreas Dannenberg <dannenberg@ti.com> |
| 7 | * |
| 8 | * Loosely based on Linux kernel sci-clk.c... |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <clk-uclass.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 15 | #include <linux/err.h> |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 16 | #include <linux/soc/ti/ti_sci_protocol.h> |
Keerthy | 280d7a8 | 2019-10-24 15:00:47 +0530 | [diff] [blame] | 17 | #include <k3-avs.h> |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * struct ti_sci_clk_data - clock controller information structure |
| 21 | * @sci: TI SCI handle used for communication with system controller |
| 22 | */ |
| 23 | struct ti_sci_clk_data { |
| 24 | const struct ti_sci_handle *sci; |
| 25 | }; |
| 26 | |
| 27 | static int ti_sci_clk_probe(struct udevice *dev) |
| 28 | { |
| 29 | struct ti_sci_clk_data *data = dev_get_priv(dev); |
| 30 | |
| 31 | debug("%s(dev=%p)\n", __func__, dev); |
| 32 | |
| 33 | if (!data) |
| 34 | return -ENOMEM; |
| 35 | |
| 36 | /* Store handle for communication with the system controller */ |
| 37 | data->sci = ti_sci_get_handle(dev); |
| 38 | if (IS_ERR(data->sci)) |
| 39 | return PTR_ERR(data->sci); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int ti_sci_clk_of_xlate(struct clk *clk, |
| 45 | struct ofnode_phandle_args *args) |
| 46 | { |
| 47 | debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count); |
| 48 | |
| 49 | if (args->args_count != 2) { |
| 50 | debug("Invalid args_count: %d\n", args->args_count); |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * On TI SCI-based devices, the clock provider id field is used as a |
| 56 | * device ID, and the data field is used as the associated sub-ID. |
| 57 | */ |
| 58 | clk->id = args->args[0]; |
| 59 | clk->data = args->args[1]; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int ti_sci_clk_request(struct clk *clk) |
| 65 | { |
| 66 | debug("%s(clk=%p)\n", __func__, clk); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int ti_sci_clk_free(struct clk *clk) |
| 71 | { |
| 72 | debug("%s(clk=%p)\n", __func__, clk); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static ulong ti_sci_clk_get_rate(struct clk *clk) |
| 77 | { |
| 78 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 79 | const struct ti_sci_handle *sci = data->sci; |
| 80 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 81 | u64 current_freq; |
| 82 | int ret; |
| 83 | |
| 84 | debug("%s(clk=%p)\n", __func__, clk); |
| 85 | |
| 86 | ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq); |
| 87 | if (ret) { |
| 88 | dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | debug("%s(current_freq=%llu)\n", __func__, current_freq); |
| 93 | |
| 94 | return current_freq; |
| 95 | } |
| 96 | |
| 97 | static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate) |
| 98 | { |
| 99 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 100 | const struct ti_sci_handle *sci = data->sci; |
| 101 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 102 | int ret; |
| 103 | |
| 104 | debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate); |
| 105 | |
Keerthy | 280d7a8 | 2019-10-24 15:00:47 +0530 | [diff] [blame] | 106 | #ifdef CONFIG_K3_AVS0 |
| 107 | k3_avs_notify_freq(clk->id, clk->data, rate); |
| 108 | #endif |
| 109 | |
Lokesh Vutla | 74fc6b8 | 2020-01-17 11:57:30 +0530 | [diff] [blame] | 110 | ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX); |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 111 | if (ret) |
| 112 | dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent) |
| 118 | { |
| 119 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 120 | const struct ti_sci_handle *sci = data->sci; |
| 121 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 122 | u8 num_parents; |
| 123 | u8 parent_cid; |
| 124 | int ret; |
| 125 | |
| 126 | debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent); |
| 127 | |
| 128 | /* Make sure the clock parent is valid for a given device ID */ |
| 129 | if (clk->id != parent->id) |
| 130 | return -EINVAL; |
| 131 | |
| 132 | /* Make sure clock has parents that can be set */ |
| 133 | ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents); |
| 134 | if (ret) { |
| 135 | dev_err(clk->dev, "%s: get_num_parents failed (%d)\n", |
| 136 | __func__, ret); |
| 137 | return ret; |
| 138 | } |
| 139 | if (num_parents < 2) { |
| 140 | dev_err(clk->dev, "%s: clock has no settable parents!\n", |
| 141 | __func__); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | /* Make sure parent clock ID is valid */ |
| 146 | parent_cid = parent->data - clk->data - 1; |
| 147 | if (parent_cid >= num_parents) { |
| 148 | dev_err(clk->dev, "%s: invalid parent clock!\n", __func__); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | /* Ready to proceed to configure the new clock parent */ |
| 153 | ret = cops->set_parent(sci, clk->id, clk->data, parent->data); |
| 154 | if (ret) |
| 155 | dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__, |
| 156 | ret); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int ti_sci_clk_enable(struct clk *clk) |
| 162 | { |
| 163 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 164 | const struct ti_sci_handle *sci = data->sci; |
| 165 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 166 | int ret; |
| 167 | |
| 168 | debug("%s(clk=%p)\n", __func__, clk); |
| 169 | |
| 170 | /* |
| 171 | * Allow the System Controller to automatically manage the state of |
| 172 | * this clock. If the device is enabled, then the clock is enabled. |
| 173 | */ |
| 174 | ret = cops->put_clock(sci, clk->id, clk->data); |
| 175 | if (ret) |
| 176 | dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int ti_sci_clk_disable(struct clk *clk) |
| 182 | { |
| 183 | struct ti_sci_clk_data *data = dev_get_priv(clk->dev); |
| 184 | const struct ti_sci_handle *sci = data->sci; |
| 185 | const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops; |
| 186 | int ret; |
| 187 | |
| 188 | debug("%s(clk=%p)\n", __func__, clk); |
| 189 | |
| 190 | /* Unconditionally disable clock, regardless of state of the device */ |
| 191 | ret = cops->idle_clock(sci, clk->id, clk->data); |
| 192 | if (ret) |
| 193 | dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__, |
| 194 | ret); |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static const struct udevice_id ti_sci_clk_of_match[] = { |
| 200 | { .compatible = "ti,k2g-sci-clk" }, |
| 201 | { /* sentinel */ }, |
| 202 | }; |
| 203 | |
| 204 | static struct clk_ops ti_sci_clk_ops = { |
| 205 | .of_xlate = ti_sci_clk_of_xlate, |
| 206 | .request = ti_sci_clk_request, |
Simon Glass | 2cdd3f4 | 2020-02-03 07:35:54 -0700 | [diff] [blame] | 207 | .rfree = ti_sci_clk_free, |
Andreas Dannenberg | 1530e35 | 2018-08-27 15:57:43 +0530 | [diff] [blame] | 208 | .get_rate = ti_sci_clk_get_rate, |
| 209 | .set_rate = ti_sci_clk_set_rate, |
| 210 | .set_parent = ti_sci_clk_set_parent, |
| 211 | .enable = ti_sci_clk_enable, |
| 212 | .disable = ti_sci_clk_disable, |
| 213 | }; |
| 214 | |
| 215 | U_BOOT_DRIVER(ti_sci_clk) = { |
| 216 | .name = "ti-sci-clk", |
| 217 | .id = UCLASS_CLK, |
| 218 | .of_match = ti_sci_clk_of_match, |
| 219 | .probe = ti_sci_clk_probe, |
| 220 | .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data), |
| 221 | .ops = &ti_sci_clk_ops, |
| 222 | }; |