2 * SCI Clock driver for keystone based devices
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://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
57 * @cached_req: Cached requested freq for determine rate calls
58 * @cached_res: Cached result freq for determine rate calls
65 struct sci_clk_provider
*provider
;
67 struct list_head node
;
68 unsigned long cached_req
;
69 unsigned long cached_res
;
72 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
75 * sci_clk_prepare - Prepare (enable) a TI SCI clock
76 * @hw: clock to prepare
78 * Prepares a clock to be actively used. Returns the SCI protocol status.
80 static int sci_clk_prepare(struct clk_hw
*hw
)
82 struct sci_clk
*clk
= to_sci_clk(hw
);
83 bool enable_ssc
= clk
->flags
& SCI_CLK_SSC_ENABLE
;
84 bool allow_freq_change
= clk
->flags
& SCI_CLK_ALLOW_FREQ_CHANGE
;
85 bool input_termination
= clk
->flags
& SCI_CLK_INPUT_TERMINATION
;
87 return clk
->provider
->ops
->get_clock(clk
->provider
->sci
, clk
->dev_id
,
88 clk
->clk_id
, enable_ssc
,
94 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
95 * @hw: clock to unprepare
97 * Un-prepares a clock from active state.
99 static void sci_clk_unprepare(struct clk_hw
*hw
)
101 struct sci_clk
*clk
= to_sci_clk(hw
);
104 ret
= clk
->provider
->ops
->put_clock(clk
->provider
->sci
, clk
->dev_id
,
107 dev_err(clk
->provider
->dev
,
108 "unprepare failed for dev=%d, clk=%d, ret=%d\n",
109 clk
->dev_id
, clk
->clk_id
, ret
);
113 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
114 * @hw: clock to check status for
116 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
117 * value if clock is enabled, zero otherwise.
119 static int sci_clk_is_prepared(struct clk_hw
*hw
)
121 struct sci_clk
*clk
= to_sci_clk(hw
);
122 bool req_state
, current_state
;
125 ret
= clk
->provider
->ops
->is_on(clk
->provider
->sci
, clk
->dev_id
,
126 clk
->clk_id
, &req_state
,
129 dev_err(clk
->provider
->dev
,
130 "is_prepared failed for dev=%d, clk=%d, ret=%d\n",
131 clk
->dev_id
, clk
->clk_id
, ret
);
139 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock
140 * @hw: clock to get rate for
141 * @parent_rate: parent rate provided by common clock framework, not used
143 * Gets the current clock rate of a TI SCI clock. Returns the current
144 * clock rate, or zero in failure.
146 static unsigned long sci_clk_recalc_rate(struct clk_hw
*hw
,
147 unsigned long parent_rate
)
149 struct sci_clk
*clk
= to_sci_clk(hw
);
153 ret
= clk
->provider
->ops
->get_freq(clk
->provider
->sci
, clk
->dev_id
,
156 dev_err(clk
->provider
->dev
,
157 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n",
158 clk
->dev_id
, clk
->clk_id
, ret
);
166 * sci_clk_determine_rate - Determines a clock rate a clock can be set to
167 * @hw: clock to change rate for
168 * @req: requested rate configuration for the clock
170 * Determines a suitable clock rate and parent for a TI SCI clock.
171 * The parent handling is un-used, as generally the parent clock rates
172 * are not known by the kernel; instead these are internally handled
173 * by the firmware. Returns 0 on success, negative error value on failure.
175 static int sci_clk_determine_rate(struct clk_hw
*hw
,
176 struct clk_rate_request
*req
)
178 struct sci_clk
*clk
= to_sci_clk(hw
);
182 if (clk
->cached_req
&& clk
->cached_req
== req
->rate
) {
183 req
->rate
= clk
->cached_res
;
187 ret
= clk
->provider
->ops
->get_best_match_freq(clk
->provider
->sci
,
195 dev_err(clk
->provider
->dev
,
196 "determine-rate failed for dev=%d, clk=%d, ret=%d\n",
197 clk
->dev_id
, clk
->clk_id
, ret
);
201 clk
->cached_req
= req
->rate
;
202 clk
->cached_res
= new_rate
;
204 req
->rate
= new_rate
;
210 * sci_clk_set_rate - Set rate for a TI SCI clock
211 * @hw: clock to change rate for
212 * @rate: target rate for the clock
213 * @parent_rate: rate of the clock parent, not used for TI SCI clocks
215 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
218 static int sci_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
219 unsigned long parent_rate
)
221 struct sci_clk
*clk
= to_sci_clk(hw
);
223 return clk
->provider
->ops
->set_freq(clk
->provider
->sci
, clk
->dev_id
,
224 clk
->clk_id
, rate
/ 10 * 9, rate
,
229 * sci_clk_get_parent - Get the current parent of a TI SCI clock
230 * @hw: clock to get parent for
232 * Returns the index of the currently selected parent for a TI SCI clock.
234 static u8
sci_clk_get_parent(struct clk_hw
*hw
)
236 struct sci_clk
*clk
= to_sci_clk(hw
);
240 ret
= clk
->provider
->ops
->get_parent(clk
->provider
->sci
, clk
->dev_id
,
241 clk
->clk_id
, (void *)&parent_id
);
243 dev_err(clk
->provider
->dev
,
244 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
245 clk
->dev_id
, clk
->clk_id
, ret
);
249 parent_id
= parent_id
- clk
->clk_id
- 1;
251 return (u8
)parent_id
;
255 * sci_clk_set_parent - Set the parent of a TI SCI clock
256 * @hw: clock to set parent for
257 * @index: new parent index for the clock
259 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
261 static int sci_clk_set_parent(struct clk_hw
*hw
, u8 index
)
263 struct sci_clk
*clk
= to_sci_clk(hw
);
267 return clk
->provider
->ops
->set_parent(clk
->provider
->sci
, clk
->dev_id
,
269 index
+ 1 + clk
->clk_id
);
272 static const struct clk_ops sci_clk_ops
= {
273 .prepare
= sci_clk_prepare
,
274 .unprepare
= sci_clk_unprepare
,
275 .is_prepared
= sci_clk_is_prepared
,
276 .recalc_rate
= sci_clk_recalc_rate
,
277 .determine_rate
= sci_clk_determine_rate
,
278 .set_rate
= sci_clk_set_rate
,
279 .get_parent
= sci_clk_get_parent
,
280 .set_parent
= sci_clk_set_parent
,
284 * _sci_clk_get - Gets a handle for an SCI clock
285 * @provider: Handle to SCI clock provider
286 * @sci_clk: Handle to the SCI clock to populate
288 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
289 * entry and registers it with the common clock framework. Called from
290 * the common clock framework, when a corresponding of_clk_get call is
291 * executed, or recursively from itself when parsing parent clocks.
292 * Returns 0 on success, negative error code on failure.
294 static int _sci_clk_build(struct sci_clk_provider
*provider
,
295 struct sci_clk
*sci_clk
)
297 struct clk_init_data init
= { NULL
};
299 char **parent_names
= NULL
;
303 name
= kasprintf(GFP_KERNEL
, "clk:%d:%d", sci_clk
->dev_id
,
309 * From kernel point of view, we only care about a clocks parents,
310 * if it has more than 1 possible parent. In this case, it is going
311 * to have mux functionality. Otherwise it is going to act as a root
314 if (sci_clk
->num_parents
< 2)
315 sci_clk
->num_parents
= 0;
317 if (sci_clk
->num_parents
) {
318 parent_names
= kcalloc(sci_clk
->num_parents
, sizeof(char *),
326 for (i
= 0; i
< sci_clk
->num_parents
; i
++) {
329 parent_name
= kasprintf(GFP_KERNEL
, "clk:%d:%d",
331 sci_clk
->clk_id
+ 1 + i
);
336 parent_names
[i
] = parent_name
;
338 init
.parent_names
= (void *)parent_names
;
341 init
.ops
= &sci_clk_ops
;
342 init
.num_parents
= sci_clk
->num_parents
;
343 sci_clk
->hw
.init
= &init
;
345 ret
= devm_clk_hw_register(provider
->dev
, &sci_clk
->hw
);
347 dev_err(provider
->dev
, "failed clk register with %d\n", ret
);
351 for (i
= 0; i
< sci_clk
->num_parents
; i
++)
352 kfree(parent_names
[i
]);
362 static int _cmp_sci_clk(const void *a
, const void *b
)
364 const struct sci_clk
*ca
= a
;
365 const struct sci_clk
*cb
= *(struct sci_clk
**)b
;
367 if (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
== cb
->clk_id
)
369 if (ca
->dev_id
> cb
->dev_id
||
370 (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
> cb
->clk_id
))
376 * sci_clk_get - Xlate function for getting clock handles
377 * @clkspec: device tree clock specifier
378 * @data: pointer to the clock provider
380 * Xlate function for retrieving clock TI SCI hw clock handles based on
381 * device tree clock specifier. Called from the common clock framework,
382 * when a corresponding of_clk_get call is executed. Returns a pointer
383 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
385 static struct clk_hw
*sci_clk_get(struct of_phandle_args
*clkspec
, void *data
)
387 struct sci_clk_provider
*provider
= data
;
388 struct sci_clk
**clk
;
391 if (clkspec
->args_count
!= 2)
392 return ERR_PTR(-EINVAL
);
394 key
.dev_id
= clkspec
->args
[0];
395 key
.clk_id
= clkspec
->args
[1];
397 clk
= bsearch(&key
, provider
->clocks
, provider
->num_clocks
,
398 sizeof(clk
), _cmp_sci_clk
);
401 return ERR_PTR(-ENODEV
);
406 static int ti_sci_init_clocks(struct sci_clk_provider
*p
)
411 for (i
= 0; i
< p
->num_clocks
; i
++) {
412 ret
= _sci_clk_build(p
, p
->clocks
[i
]);
420 static const struct of_device_id ti_sci_clk_of_match
[] = {
421 { .compatible
= "ti,k2g-sci-clk" },
424 MODULE_DEVICE_TABLE(of
, ti_sci_clk_of_match
);
426 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
427 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider
*provider
)
431 struct sci_clk
**clks
= NULL
;
432 struct sci_clk
**tmp_clks
;
433 struct sci_clk
*sci_clk
;
439 struct device
*dev
= provider
->dev
;
442 ret
= provider
->ops
->get_num_parents(provider
->sci
, dev_id
,
444 (void *)&num_parents
);
465 if (num_clks
== max_clks
) {
466 tmp_clks
= devm_kmalloc_array(dev
, max_clks
+ 64,
469 memcpy(tmp_clks
, clks
, max_clks
* sizeof(sci_clk
));
471 devm_kfree(dev
, clks
);
476 sci_clk
= devm_kzalloc(dev
, sizeof(*sci_clk
), GFP_KERNEL
);
479 sci_clk
->dev_id
= dev_id
;
480 sci_clk
->clk_id
= clk_id
;
481 sci_clk
->provider
= provider
;
482 sci_clk
->num_parents
= num_parents
;
484 clks
[num_clks
] = sci_clk
;
490 provider
->clocks
= devm_kmalloc_array(dev
, num_clks
, sizeof(sci_clk
),
492 if (!provider
->clocks
)
495 memcpy(provider
->clocks
, clks
, num_clks
* sizeof(sci_clk
));
497 provider
->num_clocks
= num_clks
;
499 devm_kfree(dev
, clks
);
506 static int _cmp_sci_clk_list(void *priv
, struct list_head
*a
,
509 struct sci_clk
*ca
= container_of(a
, struct sci_clk
, node
);
510 struct sci_clk
*cb
= container_of(b
, struct sci_clk
, node
);
512 return _cmp_sci_clk(ca
, &cb
);
515 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider
*provider
)
517 struct device
*dev
= provider
->dev
;
518 struct device_node
*np
= NULL
;
521 struct of_phandle_args args
;
522 struct list_head clks
;
523 struct sci_clk
*sci_clk
, *prev
;
527 const char * const clk_names
[] = {
528 "clocks", "assigned-clocks", "assigned-clock-parents", NULL
530 const char * const *clk_name
;
532 INIT_LIST_HEAD(&clks
);
534 clk_name
= clk_names
;
537 np
= of_find_node_with_property(np
, *clk_name
);
543 if (!of_device_is_available(np
))
549 ret
= of_parse_phandle_with_args(np
, *clk_name
,
550 "#clock-cells", index
,
555 if (args
.args_count
== 2 && args
.np
== dev
->of_node
) {
556 sci_clk
= devm_kzalloc(dev
, sizeof(*sci_clk
),
561 sci_clk
->dev_id
= args
.args
[0];
562 sci_clk
->clk_id
= args
.args
[1];
563 sci_clk
->provider
= provider
;
564 provider
->ops
->get_num_parents(provider
->sci
,
567 (void *)&sci_clk
->num_parents
);
568 list_add_tail(&sci_clk
->node
, &clks
);
572 num_parents
= sci_clk
->num_parents
;
573 if (num_parents
== 1)
577 * Linux kernel has inherent limitation
578 * of 255 clock parents at the moment.
579 * Right now, it is not expected that
580 * any mux clock from sci-clk driver
581 * would exceed that limit either, but
582 * the ABI basically provides that
583 * possibility. Print out a warning if
584 * this happens for any clock.
586 if (num_parents
>= 255) {
587 dev_warn(dev
, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n",
589 sci_clk
->clk_id
, num_parents
);
593 clk_id
= args
.args
[1] + 1;
595 while (num_parents
--) {
596 sci_clk
= devm_kzalloc(dev
,
601 sci_clk
->dev_id
= args
.args
[0];
602 sci_clk
->clk_id
= clk_id
++;
603 sci_clk
->provider
= provider
;
604 list_add_tail(&sci_clk
->node
, &clks
);
614 list_sort(NULL
, &clks
, _cmp_sci_clk_list
);
616 provider
->clocks
= devm_kmalloc_array(dev
, num_clks
, sizeof(sci_clk
),
618 if (!provider
->clocks
)
624 list_for_each_entry(sci_clk
, &clks
, node
) {
625 if (prev
&& prev
->dev_id
== sci_clk
->dev_id
&&
626 prev
->clk_id
== sci_clk
->clk_id
)
629 provider
->clocks
[num_clks
++] = sci_clk
;
633 provider
->num_clocks
= num_clks
;
640 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
641 * @pdev: platform device pointer to be probed
643 * Probes the TI SCI clock device. Allocates a new clock provider
644 * and registers this to the common clock framework. Also applies
645 * any required flags to the identified clocks via clock lists
646 * supplied from DT. Returns 0 for success, negative error value
649 static int ti_sci_clk_probe(struct platform_device
*pdev
)
651 struct device
*dev
= &pdev
->dev
;
652 struct device_node
*np
= dev
->of_node
;
653 struct sci_clk_provider
*provider
;
654 const struct ti_sci_handle
*handle
;
657 handle
= devm_ti_sci_get_handle(dev
);
659 return PTR_ERR(handle
);
661 provider
= devm_kzalloc(dev
, sizeof(*provider
), GFP_KERNEL
);
665 provider
->sci
= handle
;
666 provider
->ops
= &handle
->ops
.clk_ops
;
669 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
670 ret
= ti_sci_scan_clocks_from_fw(provider
);
672 dev_err(dev
, "scan clocks from FW failed: %d\n", ret
);
676 ret
= ti_sci_scan_clocks_from_dt(provider
);
678 dev_err(dev
, "scan clocks from DT failed: %d\n", ret
);
683 ret
= ti_sci_init_clocks(provider
);
685 pr_err("ti-sci-init-clocks failed.\n");
689 return of_clk_add_hw_provider(np
, sci_clk_get
, provider
);
693 * ti_sci_clk_remove - Remove TI SCI clock device
694 * @pdev: platform device pointer for the device to be removed
696 * Removes the TI SCI device. Unregisters the clock provider registered
697 * via common clock framework. Any memory allocated for the device will
698 * be free'd silently via the devm framework. Returns 0 always.
700 static int ti_sci_clk_remove(struct platform_device
*pdev
)
702 of_clk_del_provider(pdev
->dev
.of_node
);
707 static struct platform_driver ti_sci_clk_driver
= {
708 .probe
= ti_sci_clk_probe
,
709 .remove
= ti_sci_clk_remove
,
711 .name
= "ti-sci-clk",
712 .of_match_table
= of_match_ptr(ti_sci_clk_of_match
),
715 module_platform_driver(ti_sci_clk_driver
);
717 MODULE_LICENSE("GPL v2");
718 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
719 MODULE_AUTHOR("Tero Kristo");
720 MODULE_ALIAS("platform:ti-sci-clk");