1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCI Clock driver for keystone based devices
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
8 #include <linux/clk-provider.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/soc/ti/ti_sci_protocol.h>
17 #include <linux/bsearch.h>
18 #include <linux/list_sort.h>
20 #define SCI_CLK_SSC_ENABLE BIT(0)
21 #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1)
22 #define SCI_CLK_INPUT_TERMINATION BIT(2)
25 * struct sci_clk_provider - TI SCI clock provider representation
26 * @sci: Handle to the System Control Interface protocol handler
27 * @ops: Pointer to the SCI ops to be used by the clocks
28 * @dev: Device pointer for the clock provider
29 * @clocks: Clocks array for this device
30 * @num_clocks: Total number of clocks for this provider
32 struct sci_clk_provider {
33 const struct ti_sci_handle *sci;
34 const struct ti_sci_clk_ops *ops;
36 struct sci_clk **clocks;
41 * struct sci_clk - TI SCI clock representation
42 * @hw: Hardware clock cookie for common clock framework
43 * @dev_id: Device index
44 * @clk_id: Clock index
45 * @num_parents: Number of parents for this clock
46 * @provider: Master clock provider
47 * @flags: Flags for the clock
48 * @node: Link for handling clocks probed via DT
49 * @cached_req: Cached requested freq for determine rate calls
50 * @cached_res: Cached result freq for determine rate calls
57 struct sci_clk_provider *provider;
59 struct list_head node;
60 unsigned long cached_req;
61 unsigned long cached_res;
64 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
67 * sci_clk_prepare - Prepare (enable) a TI SCI clock
68 * @hw: clock to prepare
70 * Prepares a clock to be actively used. Returns the SCI protocol status.
72 static int sci_clk_prepare(struct clk_hw *hw)
74 struct sci_clk *clk = to_sci_clk(hw);
75 bool enable_ssc = clk->flags & SCI_CLK_SSC_ENABLE;
76 bool allow_freq_change = clk->flags & SCI_CLK_ALLOW_FREQ_CHANGE;
77 bool input_termination = clk->flags & SCI_CLK_INPUT_TERMINATION;
79 return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id,
80 clk->clk_id, enable_ssc,
86 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
87 * @hw: clock to unprepare
89 * Un-prepares a clock from active state.
91 static void sci_clk_unprepare(struct clk_hw *hw)
93 struct sci_clk *clk = to_sci_clk(hw);
96 ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id,
99 dev_err(clk->provider->dev,
100 "unprepare failed for dev=%d, clk=%d, ret=%d\n",
101 clk->dev_id, clk->clk_id, ret);
105 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
106 * @hw: clock to check status for
108 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
109 * value if clock is enabled, zero otherwise.
111 static int sci_clk_is_prepared(struct clk_hw *hw)
113 struct sci_clk *clk = to_sci_clk(hw);
114 bool req_state, current_state;
117 ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id,
118 clk->clk_id, &req_state,
121 dev_err(clk->provider->dev,
122 "is_prepared failed for dev=%d, clk=%d, ret=%d\n",
123 clk->dev_id, clk->clk_id, ret);
131 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock
132 * @hw: clock to get rate for
133 * @parent_rate: parent rate provided by common clock framework, not used
135 * Gets the current clock rate of a TI SCI clock. Returns the current
136 * clock rate, or zero in failure.
138 static unsigned long sci_clk_recalc_rate(struct clk_hw *hw,
139 unsigned long parent_rate)
141 struct sci_clk *clk = to_sci_clk(hw);
145 ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id,
148 dev_err(clk->provider->dev,
149 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n",
150 clk->dev_id, clk->clk_id, ret);
158 * sci_clk_determine_rate - Determines a clock rate a clock can be set to
159 * @hw: clock to change rate for
160 * @req: requested rate configuration for the clock
162 * Determines a suitable clock rate and parent for a TI SCI clock.
163 * The parent handling is un-used, as generally the parent clock rates
164 * are not known by the kernel; instead these are internally handled
165 * by the firmware. Returns 0 on success, negative error value on failure.
167 static int sci_clk_determine_rate(struct clk_hw *hw,
168 struct clk_rate_request *req)
170 struct sci_clk *clk = to_sci_clk(hw);
174 if (clk->cached_req && clk->cached_req == req->rate) {
175 req->rate = clk->cached_res;
179 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci,
187 dev_err(clk->provider->dev,
188 "determine-rate failed for dev=%d, clk=%d, ret=%d\n",
189 clk->dev_id, clk->clk_id, ret);
193 clk->cached_req = req->rate;
194 clk->cached_res = new_rate;
196 req->rate = new_rate;
202 * sci_clk_set_rate - Set rate for a TI SCI clock
203 * @hw: clock to change rate for
204 * @rate: target rate for the clock
205 * @parent_rate: rate of the clock parent, not used for TI SCI clocks
207 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
210 static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate,
211 unsigned long parent_rate)
213 struct sci_clk *clk = to_sci_clk(hw);
215 return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id,
216 clk->clk_id, rate / 10 * 9, rate,
221 * sci_clk_get_parent - Get the current parent of a TI SCI clock
222 * @hw: clock to get parent for
224 * Returns the index of the currently selected parent for a TI SCI clock.
226 static u8 sci_clk_get_parent(struct clk_hw *hw)
228 struct sci_clk *clk = to_sci_clk(hw);
232 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
233 clk->clk_id, (void *)&parent_id);
235 dev_err(clk->provider->dev,
236 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
237 clk->dev_id, clk->clk_id, ret);
241 parent_id = parent_id - clk->clk_id - 1;
243 return (u8)parent_id;
247 * sci_clk_set_parent - Set the parent of a TI SCI clock
248 * @hw: clock to set parent for
249 * @index: new parent index for the clock
251 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
253 static int sci_clk_set_parent(struct clk_hw *hw, u8 index)
255 struct sci_clk *clk = to_sci_clk(hw);
259 return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
261 index + 1 + clk->clk_id);
264 static const struct clk_ops sci_clk_ops = {
265 .prepare = sci_clk_prepare,
266 .unprepare = sci_clk_unprepare,
267 .is_prepared = sci_clk_is_prepared,
268 .recalc_rate = sci_clk_recalc_rate,
269 .determine_rate = sci_clk_determine_rate,
270 .set_rate = sci_clk_set_rate,
271 .get_parent = sci_clk_get_parent,
272 .set_parent = sci_clk_set_parent,
276 * _sci_clk_get - Gets a handle for an SCI clock
277 * @provider: Handle to SCI clock provider
278 * @sci_clk: Handle to the SCI clock to populate
280 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
281 * entry and registers it with the common clock framework. Called from
282 * the common clock framework, when a corresponding of_clk_get call is
283 * executed, or recursively from itself when parsing parent clocks.
284 * Returns 0 on success, negative error code on failure.
286 static int _sci_clk_build(struct sci_clk_provider *provider,
287 struct sci_clk *sci_clk)
289 struct clk_init_data init = { NULL };
291 char **parent_names = NULL;
295 name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id,
301 * From kernel point of view, we only care about a clocks parents,
302 * if it has more than 1 possible parent. In this case, it is going
303 * to have mux functionality. Otherwise it is going to act as a root
306 if (sci_clk->num_parents < 2)
307 sci_clk->num_parents = 0;
309 if (sci_clk->num_parents) {
310 parent_names = kcalloc(sci_clk->num_parents, sizeof(char *),
318 for (i = 0; i < sci_clk->num_parents; i++) {
321 parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d",
323 sci_clk->clk_id + 1 + i);
328 parent_names[i] = parent_name;
330 init.parent_names = (void *)parent_names;
333 init.ops = &sci_clk_ops;
334 init.num_parents = sci_clk->num_parents;
335 sci_clk->hw.init = &init;
337 ret = devm_clk_hw_register(provider->dev, &sci_clk->hw);
339 dev_err(provider->dev, "failed clk register with %d\n", ret);
343 for (i = 0; i < sci_clk->num_parents; i++)
344 kfree(parent_names[i]);
354 static int _cmp_sci_clk(const void *a, const void *b)
356 const struct sci_clk *ca = a;
357 const struct sci_clk *cb = *(struct sci_clk **)b;
359 if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id)
361 if (ca->dev_id > cb->dev_id ||
362 (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id))
368 * sci_clk_get - Xlate function for getting clock handles
369 * @clkspec: device tree clock specifier
370 * @data: pointer to the clock provider
372 * Xlate function for retrieving clock TI SCI hw clock handles based on
373 * device tree clock specifier. Called from the common clock framework,
374 * when a corresponding of_clk_get call is executed. Returns a pointer
375 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
377 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data)
379 struct sci_clk_provider *provider = data;
380 struct sci_clk **clk;
383 if (clkspec->args_count != 2)
384 return ERR_PTR(-EINVAL);
386 key.dev_id = clkspec->args[0];
387 key.clk_id = clkspec->args[1];
389 clk = bsearch(&key, provider->clocks, provider->num_clocks,
390 sizeof(clk), _cmp_sci_clk);
393 return ERR_PTR(-ENODEV);
398 static int ti_sci_init_clocks(struct sci_clk_provider *p)
403 for (i = 0; i < p->num_clocks; i++) {
404 ret = _sci_clk_build(p, p->clocks[i]);
412 static const struct of_device_id ti_sci_clk_of_match[] = {
413 { .compatible = "ti,k2g-sci-clk" },
416 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
418 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
419 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
423 struct sci_clk **clks = NULL;
424 struct sci_clk **tmp_clks;
425 struct sci_clk *sci_clk;
431 struct device *dev = provider->dev;
434 ret = provider->ops->get_num_parents(provider->sci, dev_id,
436 (void *)&num_parents);
457 if (num_clks == max_clks) {
458 tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
461 memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
463 devm_kfree(dev, clks);
468 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL);
471 sci_clk->dev_id = dev_id;
472 sci_clk->clk_id = clk_id;
473 sci_clk->provider = provider;
474 sci_clk->num_parents = num_parents;
476 clks[num_clks] = sci_clk;
482 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
484 if (!provider->clocks)
487 memcpy(provider->clocks, clks, num_clks * sizeof(sci_clk));
489 provider->num_clocks = num_clks;
491 devm_kfree(dev, clks);
498 static int _cmp_sci_clk_list(void *priv, const struct list_head *a,
499 const struct list_head *b)
501 struct sci_clk *ca = container_of(a, struct sci_clk, node);
502 struct sci_clk *cb = container_of(b, struct sci_clk, node);
504 return _cmp_sci_clk(ca, &cb);
507 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider)
509 struct device *dev = provider->dev;
510 struct device_node *np = NULL;
513 struct of_phandle_args args;
514 struct list_head clks;
515 struct sci_clk *sci_clk, *prev;
519 const char * const clk_names[] = {
520 "clocks", "assigned-clocks", "assigned-clock-parents", NULL
522 const char * const *clk_name;
524 INIT_LIST_HEAD(&clks);
526 clk_name = clk_names;
529 np = of_find_node_with_property(np, *clk_name);
535 if (!of_device_is_available(np))
541 ret = of_parse_phandle_with_args(np, *clk_name,
542 "#clock-cells", index,
547 if (args.args_count == 2 && args.np == dev->of_node) {
548 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk),
553 sci_clk->dev_id = args.args[0];
554 sci_clk->clk_id = args.args[1];
555 sci_clk->provider = provider;
556 provider->ops->get_num_parents(provider->sci,
559 (void *)&sci_clk->num_parents);
560 list_add_tail(&sci_clk->node, &clks);
564 num_parents = sci_clk->num_parents;
565 if (num_parents == 1)
569 * Linux kernel has inherent limitation
570 * of 255 clock parents at the moment.
571 * Right now, it is not expected that
572 * any mux clock from sci-clk driver
573 * would exceed that limit either, but
574 * the ABI basically provides that
575 * possibility. Print out a warning if
576 * this happens for any clock.
578 if (num_parents >= 255) {
579 dev_warn(dev, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n",
581 sci_clk->clk_id, num_parents);
585 clk_id = args.args[1] + 1;
587 while (num_parents--) {
588 sci_clk = devm_kzalloc(dev,
593 sci_clk->dev_id = args.args[0];
594 sci_clk->clk_id = clk_id++;
595 sci_clk->provider = provider;
596 list_add_tail(&sci_clk->node, &clks);
606 list_sort(NULL, &clks, _cmp_sci_clk_list);
608 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
610 if (!provider->clocks)
616 list_for_each_entry(sci_clk, &clks, node) {
617 if (prev && prev->dev_id == sci_clk->dev_id &&
618 prev->clk_id == sci_clk->clk_id)
621 provider->clocks[num_clks++] = sci_clk;
625 provider->num_clocks = num_clks;
632 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
633 * @pdev: platform device pointer to be probed
635 * Probes the TI SCI clock device. Allocates a new clock provider
636 * and registers this to the common clock framework. Also applies
637 * any required flags to the identified clocks via clock lists
638 * supplied from DT. Returns 0 for success, negative error value
641 static int ti_sci_clk_probe(struct platform_device *pdev)
643 struct device *dev = &pdev->dev;
644 struct device_node *np = dev->of_node;
645 struct sci_clk_provider *provider;
646 const struct ti_sci_handle *handle;
649 handle = devm_ti_sci_get_handle(dev);
651 return PTR_ERR(handle);
653 provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
657 provider->sci = handle;
658 provider->ops = &handle->ops.clk_ops;
661 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
662 ret = ti_sci_scan_clocks_from_fw(provider);
664 dev_err(dev, "scan clocks from FW failed: %d\n", ret);
668 ret = ti_sci_scan_clocks_from_dt(provider);
670 dev_err(dev, "scan clocks from DT failed: %d\n", ret);
675 ret = ti_sci_init_clocks(provider);
677 pr_err("ti-sci-init-clocks failed.\n");
681 return of_clk_add_hw_provider(np, sci_clk_get, provider);
685 * ti_sci_clk_remove - Remove TI SCI clock device
686 * @pdev: platform device pointer for the device to be removed
688 * Removes the TI SCI device. Unregisters the clock provider registered
689 * via common clock framework. Any memory allocated for the device will
690 * be free'd silently via the devm framework. Returns 0 always.
692 static int ti_sci_clk_remove(struct platform_device *pdev)
694 of_clk_del_provider(pdev->dev.of_node);
699 static struct platform_driver ti_sci_clk_driver = {
700 .probe = ti_sci_clk_probe,
701 .remove = ti_sci_clk_remove,
703 .name = "ti-sci-clk",
704 .of_match_table = of_match_ptr(ti_sci_clk_of_match),
707 module_platform_driver(ti_sci_clk_driver);
709 MODULE_LICENSE("GPL v2");
710 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
711 MODULE_AUTHOR("Tero Kristo");
712 MODULE_ALIAS("platform:ti-sci-clk");