2 * SCI Clock driver for keystone based devices
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Tero Kristo <t-kristo@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 #include <linux/clk-provider.h>
17 #include <linux/err.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/soc/ti/ti_sci_protocol.h>
25 #include <linux/bsearch.h>
26 #include <linux/list_sort.h>
28 #define SCI_CLK_SSC_ENABLE BIT(0)
29 #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1)
30 #define SCI_CLK_INPUT_TERMINATION BIT(2)
33 * struct sci_clk_provider - TI SCI clock provider representation
34 * @sci: Handle to the System Control Interface protocol handler
35 * @ops: Pointer to the SCI ops to be used by the clocks
36 * @dev: Device pointer for the clock provider
37 * @clocks: Clocks array for this device
38 * @num_clocks: Total number of clocks for this provider
40 struct sci_clk_provider {
41 const struct ti_sci_handle *sci;
42 const struct ti_sci_clk_ops *ops;
44 struct sci_clk **clocks;
49 * struct sci_clk - TI SCI clock representation
50 * @hw: Hardware clock cookie for common clock framework
51 * @dev_id: Device index
52 * @clk_id: Clock index
53 * @num_parents: Number of parents for this clock
54 * @provider: Master clock provider
55 * @flags: Flags for the clock
56 * @node: Link for handling clocks probed via DT
63 struct sci_clk_provider *provider;
65 struct list_head node;
68 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
71 * sci_clk_prepare - Prepare (enable) a TI SCI clock
72 * @hw: clock to prepare
74 * Prepares a clock to be actively used. Returns the SCI protocol status.
76 static int sci_clk_prepare(struct clk_hw *hw)
78 struct sci_clk *clk = to_sci_clk(hw);
79 bool enable_ssc = clk->flags & SCI_CLK_SSC_ENABLE;
80 bool allow_freq_change = clk->flags & SCI_CLK_ALLOW_FREQ_CHANGE;
81 bool input_termination = clk->flags & SCI_CLK_INPUT_TERMINATION;
83 return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id,
84 clk->clk_id, enable_ssc,
90 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
91 * @hw: clock to unprepare
93 * Un-prepares a clock from active state.
95 static void sci_clk_unprepare(struct clk_hw *hw)
97 struct sci_clk *clk = to_sci_clk(hw);
100 ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id,
103 dev_err(clk->provider->dev,
104 "unprepare failed for dev=%d, clk=%d, ret=%d\n",
105 clk->dev_id, clk->clk_id, ret);
109 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
110 * @hw: clock to check status for
112 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
113 * value if clock is enabled, zero otherwise.
115 static int sci_clk_is_prepared(struct clk_hw *hw)
117 struct sci_clk *clk = to_sci_clk(hw);
118 bool req_state, current_state;
121 ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id,
122 clk->clk_id, &req_state,
125 dev_err(clk->provider->dev,
126 "is_prepared failed for dev=%d, clk=%d, ret=%d\n",
127 clk->dev_id, clk->clk_id, ret);
135 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock
136 * @hw: clock to get rate for
137 * @parent_rate: parent rate provided by common clock framework, not used
139 * Gets the current clock rate of a TI SCI clock. Returns the current
140 * clock rate, or zero in failure.
142 static unsigned long sci_clk_recalc_rate(struct clk_hw *hw,
143 unsigned long parent_rate)
145 struct sci_clk *clk = to_sci_clk(hw);
149 ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id,
152 dev_err(clk->provider->dev,
153 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n",
154 clk->dev_id, clk->clk_id, ret);
162 * sci_clk_determine_rate - Determines a clock rate a clock can be set to
163 * @hw: clock to change rate for
164 * @req: requested rate configuration for the clock
166 * Determines a suitable clock rate and parent for a TI SCI clock.
167 * The parent handling is un-used, as generally the parent clock rates
168 * are not known by the kernel; instead these are internally handled
169 * by the firmware. Returns 0 on success, negative error value on failure.
171 static int sci_clk_determine_rate(struct clk_hw *hw,
172 struct clk_rate_request *req)
174 struct sci_clk *clk = to_sci_clk(hw);
178 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci,
186 dev_err(clk->provider->dev,
187 "determine-rate failed for dev=%d, clk=%d, ret=%d\n",
188 clk->dev_id, clk->clk_id, ret);
192 req->rate = new_rate;
198 * sci_clk_set_rate - Set rate for a TI SCI clock
199 * @hw: clock to change rate for
200 * @rate: target rate for the clock
201 * @parent_rate: rate of the clock parent, not used for TI SCI clocks
203 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
206 static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate,
207 unsigned long parent_rate)
209 struct sci_clk *clk = to_sci_clk(hw);
211 return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id,
212 clk->clk_id, rate, rate, rate);
216 * sci_clk_get_parent - Get the current parent of a TI SCI clock
217 * @hw: clock to get parent for
219 * Returns the index of the currently selected parent for a TI SCI clock.
221 static u8 sci_clk_get_parent(struct clk_hw *hw)
223 struct sci_clk *clk = to_sci_clk(hw);
227 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
228 clk->clk_id, &parent_id);
230 dev_err(clk->provider->dev,
231 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
232 clk->dev_id, clk->clk_id, ret);
236 return parent_id - clk->clk_id - 1;
240 * sci_clk_set_parent - Set the parent of a TI SCI clock
241 * @hw: clock to set parent for
242 * @index: new parent index for the clock
244 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
246 static int sci_clk_set_parent(struct clk_hw *hw, u8 index)
248 struct sci_clk *clk = to_sci_clk(hw);
250 return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
252 index + 1 + clk->clk_id);
255 static const struct clk_ops sci_clk_ops = {
256 .prepare = sci_clk_prepare,
257 .unprepare = sci_clk_unprepare,
258 .is_prepared = sci_clk_is_prepared,
259 .recalc_rate = sci_clk_recalc_rate,
260 .determine_rate = sci_clk_determine_rate,
261 .set_rate = sci_clk_set_rate,
262 .get_parent = sci_clk_get_parent,
263 .set_parent = sci_clk_set_parent,
267 * _sci_clk_get - Gets a handle for an SCI clock
268 * @provider: Handle to SCI clock provider
269 * @sci_clk: Handle to the SCI clock to populate
271 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
272 * entry and registers it with the common clock framework. Called from
273 * the common clock framework, when a corresponding of_clk_get call is
274 * executed, or recursively from itself when parsing parent clocks.
275 * Returns 0 on success, negative error code on failure.
277 static int _sci_clk_build(struct sci_clk_provider *provider,
278 struct sci_clk *sci_clk)
280 struct clk_init_data init = { NULL };
282 char **parent_names = NULL;
286 name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id,
292 * From kernel point of view, we only care about a clocks parents,
293 * if it has more than 1 possible parent. In this case, it is going
294 * to have mux functionality. Otherwise it is going to act as a root
297 if (sci_clk->num_parents < 2)
298 sci_clk->num_parents = 0;
300 if (sci_clk->num_parents) {
301 parent_names = kcalloc(sci_clk->num_parents, sizeof(char *),
309 for (i = 0; i < sci_clk->num_parents; i++) {
312 parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d",
314 sci_clk->clk_id + 1 + i);
319 parent_names[i] = parent_name;
321 init.parent_names = (void *)parent_names;
324 init.ops = &sci_clk_ops;
325 init.num_parents = sci_clk->num_parents;
326 sci_clk->hw.init = &init;
328 ret = devm_clk_hw_register(provider->dev, &sci_clk->hw);
330 dev_err(provider->dev, "failed clk register with %d\n", ret);
334 for (i = 0; i < sci_clk->num_parents; i++)
335 kfree(parent_names[i]);
345 static int _cmp_sci_clk(const void *a, const void *b)
347 const struct sci_clk *ca = a;
348 const struct sci_clk *cb = *(struct sci_clk **)b;
350 if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id)
352 if (ca->dev_id > cb->dev_id ||
353 (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id))
359 * sci_clk_get - Xlate function for getting clock handles
360 * @clkspec: device tree clock specifier
361 * @data: pointer to the clock provider
363 * Xlate function for retrieving clock TI SCI hw clock handles based on
364 * device tree clock specifier. Called from the common clock framework,
365 * when a corresponding of_clk_get call is executed. Returns a pointer
366 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
368 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data)
370 struct sci_clk_provider *provider = data;
371 struct sci_clk **clk;
374 if (clkspec->args_count != 2)
375 return ERR_PTR(-EINVAL);
377 key.dev_id = clkspec->args[0];
378 key.clk_id = clkspec->args[1];
380 clk = bsearch(&key, provider->clocks, provider->num_clocks,
381 sizeof(clk), _cmp_sci_clk);
384 return ERR_PTR(-ENODEV);
389 static int ti_sci_init_clocks(struct sci_clk_provider *p)
394 for (i = 0; i < p->num_clocks; i++) {
395 ret = _sci_clk_build(p, p->clocks[i]);
403 static const struct of_device_id ti_sci_clk_of_match[] = {
404 { .compatible = "ti,k2g-sci-clk" },
407 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
409 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
410 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
414 struct sci_clk **clks = NULL;
415 struct sci_clk **tmp_clks;
416 struct sci_clk *sci_clk;
422 struct device *dev = provider->dev;
425 ret = provider->ops->get_num_parents(provider->sci, dev_id,
426 clk_id, &num_parents);
447 if (num_clks == max_clks) {
448 tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
451 memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
453 devm_kfree(dev, clks);
458 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL);
461 sci_clk->dev_id = dev_id;
462 sci_clk->clk_id = clk_id;
463 sci_clk->provider = provider;
464 sci_clk->num_parents = num_parents;
466 clks[num_clks] = sci_clk;
472 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
474 if (!provider->clocks)
477 memcpy(provider->clocks, clks, num_clks * sizeof(sci_clk));
479 provider->num_clocks = num_clks;
481 devm_kfree(dev, clks);
488 static int _cmp_sci_clk_list(void *priv, struct list_head *a,
491 struct sci_clk *ca = container_of(a, struct sci_clk, node);
492 struct sci_clk *cb = container_of(b, struct sci_clk, node);
494 return _cmp_sci_clk(ca, &cb);
497 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider)
499 struct device *dev = provider->dev;
500 struct device_node *np = NULL;
503 struct of_phandle_args args;
504 struct list_head clks;
505 struct sci_clk *sci_clk, *prev;
509 const char * const clk_names[] = {
510 "clocks", "assigned-clocks", "assigned-clock-parents", NULL
512 const char * const *clk_name;
514 INIT_LIST_HEAD(&clks);
516 clk_name = clk_names;
519 np = of_find_node_with_property(np, *clk_name);
525 if (!of_device_is_available(np))
531 ret = of_parse_phandle_with_args(np, *clk_name,
532 "#clock-cells", index,
537 if (args.args_count == 2 && args.np == dev->of_node) {
538 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk),
543 sci_clk->dev_id = args.args[0];
544 sci_clk->clk_id = args.args[1];
545 sci_clk->provider = provider;
546 provider->ops->get_num_parents(provider->sci,
549 &sci_clk->num_parents);
550 list_add_tail(&sci_clk->node, &clks);
554 num_parents = sci_clk->num_parents;
555 if (num_parents == 1)
558 clk_id = args.args[1] + 1;
560 while (num_parents--) {
561 sci_clk = devm_kzalloc(dev,
566 sci_clk->dev_id = args.args[0];
567 sci_clk->clk_id = clk_id++;
568 sci_clk->provider = provider;
569 list_add_tail(&sci_clk->node, &clks);
579 list_sort(NULL, &clks, _cmp_sci_clk_list);
581 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
583 if (!provider->clocks)
589 list_for_each_entry(sci_clk, &clks, node) {
590 if (prev && prev->dev_id == sci_clk->dev_id &&
591 prev->clk_id == sci_clk->clk_id)
594 provider->clocks[num_clks++] = sci_clk;
598 provider->num_clocks = num_clks;
605 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
606 * @pdev: platform device pointer to be probed
608 * Probes the TI SCI clock device. Allocates a new clock provider
609 * and registers this to the common clock framework. Also applies
610 * any required flags to the identified clocks via clock lists
611 * supplied from DT. Returns 0 for success, negative error value
614 static int ti_sci_clk_probe(struct platform_device *pdev)
616 struct device *dev = &pdev->dev;
617 struct device_node *np = dev->of_node;
618 struct sci_clk_provider *provider;
619 const struct ti_sci_handle *handle;
622 handle = devm_ti_sci_get_handle(dev);
624 return PTR_ERR(handle);
626 provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
630 provider->sci = handle;
631 provider->ops = &handle->ops.clk_ops;
634 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
635 ret = ti_sci_scan_clocks_from_fw(provider);
637 dev_err(dev, "scan clocks from FW failed: %d\n", ret);
641 ret = ti_sci_scan_clocks_from_dt(provider);
643 dev_err(dev, "scan clocks from DT failed: %d\n", ret);
648 ret = ti_sci_init_clocks(provider);
650 pr_err("ti-sci-init-clocks failed.\n");
654 return of_clk_add_hw_provider(np, sci_clk_get, provider);
658 * ti_sci_clk_remove - Remove TI SCI clock device
659 * @pdev: platform device pointer for the device to be removed
661 * Removes the TI SCI device. Unregisters the clock provider registered
662 * via common clock framework. Any memory allocated for the device will
663 * be free'd silently via the devm framework. Returns 0 always.
665 static int ti_sci_clk_remove(struct platform_device *pdev)
667 of_clk_del_provider(pdev->dev.of_node);
672 static struct platform_driver ti_sci_clk_driver = {
673 .probe = ti_sci_clk_probe,
674 .remove = ti_sci_clk_remove,
676 .name = "ti-sci-clk",
677 .of_match_table = of_match_ptr(ti_sci_clk_of_match),
680 module_platform_driver(ti_sci_clk_driver);
682 MODULE_LICENSE("GPL v2");
683 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
684 MODULE_AUTHOR("Tero Kristo");
685 MODULE_ALIAS("platform:ti-sci-clk");