Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 4 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <clk.h> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 11 | #include <clk-uclass.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 13 | #include <dt-structs.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 15 | |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 16 | static inline const struct clk_ops *clk_dev_ops(struct udevice *dev) |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 17 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 18 | return (const struct clk_ops *)dev->driver->ops; |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 19 | } |
| 20 | |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 21 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 22 | # if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 23 | int clk_get_by_index_platdata(struct udevice *dev, int index, |
Simon Glass | 0d15463 | 2017-08-29 14:15:56 -0600 | [diff] [blame] | 24 | struct phandle_1_arg *cells, struct clk *clk) |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 25 | { |
| 26 | int ret; |
| 27 | |
| 28 | if (index != 0) |
| 29 | return -ENOSYS; |
| 30 | ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev); |
| 31 | if (ret) |
| 32 | return ret; |
Simon Glass | bc79617 | 2017-08-29 14:15:58 -0600 | [diff] [blame] | 33 | clk->id = cells[0].arg[0]; |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | # else |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 38 | static int clk_of_xlate_default(struct clk *clk, |
Simon Glass | a4e0ef5 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 39 | struct ofnode_phandle_args *args) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 40 | { |
| 41 | debug("%s(clk=%p)\n", __func__, clk); |
| 42 | |
| 43 | if (args->args_count > 1) { |
| 44 | debug("Invaild args_count: %d\n", args->args_count); |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | |
| 48 | if (args->args_count) |
| 49 | clk->id = args->args[0]; |
| 50 | else |
| 51 | clk->id = 0; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame^] | 56 | static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name, |
| 57 | int index, struct clk *clk) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 58 | { |
| 59 | int ret; |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 60 | struct ofnode_phandle_args args; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 61 | struct udevice *dev_clk; |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 62 | const struct clk_ops *ops; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 63 | |
| 64 | debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk); |
| 65 | |
| 66 | assert(clk); |
Patrice Chotard | 82a8a66 | 2017-07-18 11:57:07 +0200 | [diff] [blame] | 67 | clk->dev = NULL; |
| 68 | |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame^] | 69 | ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0, |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 70 | index, &args); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 71 | if (ret) { |
| 72 | debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n", |
| 73 | __func__, ret); |
| 74 | return ret; |
| 75 | } |
| 76 | |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 77 | ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 78 | if (ret) { |
| 79 | debug("%s: uclass_get_device_by_of_offset failed: err=%d\n", |
| 80 | __func__, ret); |
| 81 | return ret; |
| 82 | } |
Wenyou Yang | 3f56b13 | 2016-09-27 11:00:28 +0800 | [diff] [blame] | 83 | |
| 84 | clk->dev = dev_clk; |
| 85 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 86 | ops = clk_dev_ops(dev_clk); |
| 87 | |
| 88 | if (ops->of_xlate) |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 89 | ret = ops->of_xlate(clk, &args); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 90 | else |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 91 | ret = clk_of_xlate_default(clk, &args); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 92 | if (ret) { |
| 93 | debug("of_xlate() failed: %d\n", ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | return clk_request(dev_clk, clk); |
| 98 | } |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame^] | 99 | |
| 100 | int clk_get_by_index(struct udevice *dev, int index, struct clk *clk) |
| 101 | { |
| 102 | return clk_get_by_indexed_prop(dev, "clocks", index, clk); |
| 103 | } |
Michal Simek | 9e0758b | 2016-07-14 13:11:37 +0200 | [diff] [blame] | 104 | # endif /* OF_PLATDATA */ |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 105 | |
| 106 | int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk) |
| 107 | { |
| 108 | int index; |
| 109 | |
| 110 | debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk); |
Patrice Chotard | 82a8a66 | 2017-07-18 11:57:07 +0200 | [diff] [blame] | 111 | clk->dev = NULL; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 112 | |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 113 | index = dev_read_stringlist_search(dev, "clock-names", name); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 114 | if (index < 0) { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 115 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 116 | return index; |
| 117 | } |
| 118 | |
| 119 | return clk_get_by_index(dev, index, clk); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 120 | } |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 121 | |
| 122 | int clk_release_all(struct clk *clk, int count) |
| 123 | { |
| 124 | int i, ret; |
| 125 | |
| 126 | for (i = 0; i < count; i++) { |
| 127 | debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]); |
| 128 | |
| 129 | /* check if clock has been previously requested */ |
| 130 | if (!clk[i].dev) |
| 131 | continue; |
| 132 | |
| 133 | ret = clk_disable(&clk[i]); |
| 134 | if (ret && ret != -ENOSYS) |
| 135 | return ret; |
| 136 | |
| 137 | ret = clk_free(&clk[i]); |
| 138 | if (ret && ret != -ENOSYS) |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 145 | #endif /* OF_CONTROL */ |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 146 | |
| 147 | int clk_request(struct udevice *dev, struct clk *clk) |
| 148 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 149 | const struct clk_ops *ops = clk_dev_ops(dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 150 | |
| 151 | debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk); |
| 152 | |
| 153 | clk->dev = dev; |
| 154 | |
| 155 | if (!ops->request) |
| 156 | return 0; |
| 157 | |
| 158 | return ops->request(clk); |
| 159 | } |
| 160 | |
| 161 | int clk_free(struct clk *clk) |
| 162 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 163 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 164 | |
| 165 | debug("%s(clk=%p)\n", __func__, clk); |
| 166 | |
| 167 | if (!ops->free) |
| 168 | return 0; |
| 169 | |
| 170 | return ops->free(clk); |
| 171 | } |
| 172 | |
| 173 | ulong clk_get_rate(struct clk *clk) |
| 174 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 175 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 176 | |
| 177 | debug("%s(clk=%p)\n", __func__, clk); |
| 178 | |
| 179 | if (!ops->get_rate) |
| 180 | return -ENOSYS; |
| 181 | |
| 182 | return ops->get_rate(clk); |
| 183 | } |
| 184 | |
| 185 | ulong clk_set_rate(struct clk *clk, ulong rate) |
| 186 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 187 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 188 | |
| 189 | debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate); |
| 190 | |
| 191 | if (!ops->set_rate) |
| 192 | return -ENOSYS; |
| 193 | |
| 194 | return ops->set_rate(clk, rate); |
| 195 | } |
| 196 | |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 197 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 198 | { |
| 199 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
| 200 | |
| 201 | debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent); |
| 202 | |
| 203 | if (!ops->set_parent) |
| 204 | return -ENOSYS; |
| 205 | |
| 206 | return ops->set_parent(clk, parent); |
| 207 | } |
| 208 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 209 | int clk_enable(struct clk *clk) |
| 210 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 211 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 212 | |
| 213 | debug("%s(clk=%p)\n", __func__, clk); |
| 214 | |
| 215 | if (!ops->enable) |
| 216 | return -ENOSYS; |
| 217 | |
| 218 | return ops->enable(clk); |
| 219 | } |
| 220 | |
| 221 | int clk_disable(struct clk *clk) |
| 222 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 223 | const struct clk_ops *ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 224 | |
| 225 | debug("%s(clk=%p)\n", __func__, clk); |
| 226 | |
| 227 | if (!ops->disable) |
| 228 | return -ENOSYS; |
| 229 | |
| 230 | return ops->disable(clk); |
| 231 | } |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 232 | |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 233 | UCLASS_DRIVER(clk) = { |
| 234 | .id = UCLASS_CLK, |
| 235 | .name = "clk", |
| 236 | }; |