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
, (void *)&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 parent_id
= parent_id
- clk
->clk_id
- 1;
238 return (u8
)parent_id
;
242 * sci_clk_set_parent - Set the parent of a TI SCI clock
243 * @hw: clock to set parent for
244 * @index: new parent index for the clock
246 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
248 static int sci_clk_set_parent(struct clk_hw
*hw
, u8 index
)
250 struct sci_clk
*clk
= to_sci_clk(hw
);
252 return clk
->provider
->ops
->set_parent(clk
->provider
->sci
, clk
->dev_id
,
254 index
+ 1 + clk
->clk_id
);
257 static const struct clk_ops sci_clk_ops
= {
258 .prepare
= sci_clk_prepare
,
259 .unprepare
= sci_clk_unprepare
,
260 .is_prepared
= sci_clk_is_prepared
,
261 .recalc_rate
= sci_clk_recalc_rate
,
262 .determine_rate
= sci_clk_determine_rate
,
263 .set_rate
= sci_clk_set_rate
,
264 .get_parent
= sci_clk_get_parent
,
265 .set_parent
= sci_clk_set_parent
,
269 * _sci_clk_get - Gets a handle for an SCI clock
270 * @provider: Handle to SCI clock provider
271 * @sci_clk: Handle to the SCI clock to populate
273 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
274 * entry and registers it with the common clock framework. Called from
275 * the common clock framework, when a corresponding of_clk_get call is
276 * executed, or recursively from itself when parsing parent clocks.
277 * Returns 0 on success, negative error code on failure.
279 static int _sci_clk_build(struct sci_clk_provider
*provider
,
280 struct sci_clk
*sci_clk
)
282 struct clk_init_data init
= { NULL
};
284 char **parent_names
= NULL
;
288 name
= kasprintf(GFP_KERNEL
, "clk:%d:%d", sci_clk
->dev_id
,
294 * From kernel point of view, we only care about a clocks parents,
295 * if it has more than 1 possible parent. In this case, it is going
296 * to have mux functionality. Otherwise it is going to act as a root
299 if (sci_clk
->num_parents
< 2)
300 sci_clk
->num_parents
= 0;
302 if (sci_clk
->num_parents
) {
303 parent_names
= kcalloc(sci_clk
->num_parents
, sizeof(char *),
311 for (i
= 0; i
< sci_clk
->num_parents
; i
++) {
314 parent_name
= kasprintf(GFP_KERNEL
, "clk:%d:%d",
316 sci_clk
->clk_id
+ 1 + i
);
321 parent_names
[i
] = parent_name
;
323 init
.parent_names
= (void *)parent_names
;
326 init
.ops
= &sci_clk_ops
;
327 init
.num_parents
= sci_clk
->num_parents
;
328 sci_clk
->hw
.init
= &init
;
330 ret
= devm_clk_hw_register(provider
->dev
, &sci_clk
->hw
);
332 dev_err(provider
->dev
, "failed clk register with %d\n", ret
);
336 for (i
= 0; i
< sci_clk
->num_parents
; i
++)
337 kfree(parent_names
[i
]);
347 static int _cmp_sci_clk(const void *a
, const void *b
)
349 const struct sci_clk
*ca
= a
;
350 const struct sci_clk
*cb
= *(struct sci_clk
**)b
;
352 if (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
== cb
->clk_id
)
354 if (ca
->dev_id
> cb
->dev_id
||
355 (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
> cb
->clk_id
))
361 * sci_clk_get - Xlate function for getting clock handles
362 * @clkspec: device tree clock specifier
363 * @data: pointer to the clock provider
365 * Xlate function for retrieving clock TI SCI hw clock handles based on
366 * device tree clock specifier. Called from the common clock framework,
367 * when a corresponding of_clk_get call is executed. Returns a pointer
368 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
370 static struct clk_hw
*sci_clk_get(struct of_phandle_args
*clkspec
, void *data
)
372 struct sci_clk_provider
*provider
= data
;
373 struct sci_clk
**clk
;
376 if (clkspec
->args_count
!= 2)
377 return ERR_PTR(-EINVAL
);
379 key
.dev_id
= clkspec
->args
[0];
380 key
.clk_id
= clkspec
->args
[1];
382 clk
= bsearch(&key
, provider
->clocks
, provider
->num_clocks
,
383 sizeof(clk
), _cmp_sci_clk
);
386 return ERR_PTR(-ENODEV
);
391 static int ti_sci_init_clocks(struct sci_clk_provider
*p
)
396 for (i
= 0; i
< p
->num_clocks
; i
++) {
397 ret
= _sci_clk_build(p
, p
->clocks
[i
]);
405 static const struct of_device_id ti_sci_clk_of_match
[] = {
406 { .compatible
= "ti,k2g-sci-clk" },
409 MODULE_DEVICE_TABLE(of
, ti_sci_clk_of_match
);
411 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
412 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider
*provider
)
416 struct sci_clk
**clks
= NULL
;
417 struct sci_clk
**tmp_clks
;
418 struct sci_clk
*sci_clk
;
424 struct device
*dev
= provider
->dev
;
427 ret
= provider
->ops
->get_num_parents(provider
->sci
, dev_id
,
429 (void *)&num_parents
);
450 if (num_clks
== max_clks
) {
451 tmp_clks
= devm_kmalloc_array(dev
, max_clks
+ 64,
454 memcpy(tmp_clks
, clks
, max_clks
* sizeof(sci_clk
));
456 devm_kfree(dev
, clks
);
461 sci_clk
= devm_kzalloc(dev
, sizeof(*sci_clk
), GFP_KERNEL
);
464 sci_clk
->dev_id
= dev_id
;
465 sci_clk
->clk_id
= clk_id
;
466 sci_clk
->provider
= provider
;
467 sci_clk
->num_parents
= num_parents
;
469 clks
[num_clks
] = sci_clk
;
475 provider
->clocks
= devm_kmalloc_array(dev
, num_clks
, sizeof(sci_clk
),
477 if (!provider
->clocks
)
480 memcpy(provider
->clocks
, clks
, num_clks
* sizeof(sci_clk
));
482 provider
->num_clocks
= num_clks
;
484 devm_kfree(dev
, clks
);
491 static int _cmp_sci_clk_list(void *priv
, struct list_head
*a
,
494 struct sci_clk
*ca
= container_of(a
, struct sci_clk
, node
);
495 struct sci_clk
*cb
= container_of(b
, struct sci_clk
, node
);
497 return _cmp_sci_clk(ca
, &cb
);
500 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider
*provider
)
502 struct device
*dev
= provider
->dev
;
503 struct device_node
*np
= NULL
;
506 struct of_phandle_args args
;
507 struct list_head clks
;
508 struct sci_clk
*sci_clk
, *prev
;
512 const char * const clk_names
[] = {
513 "clocks", "assigned-clocks", "assigned-clock-parents", NULL
515 const char * const *clk_name
;
517 INIT_LIST_HEAD(&clks
);
519 clk_name
= clk_names
;
522 np
= of_find_node_with_property(np
, *clk_name
);
528 if (!of_device_is_available(np
))
534 ret
= of_parse_phandle_with_args(np
, *clk_name
,
535 "#clock-cells", index
,
540 if (args
.args_count
== 2 && args
.np
== dev
->of_node
) {
541 sci_clk
= devm_kzalloc(dev
, sizeof(*sci_clk
),
546 sci_clk
->dev_id
= args
.args
[0];
547 sci_clk
->clk_id
= args
.args
[1];
548 sci_clk
->provider
= provider
;
549 provider
->ops
->get_num_parents(provider
->sci
,
552 (void *)&sci_clk
->num_parents
);
553 list_add_tail(&sci_clk
->node
, &clks
);
557 num_parents
= sci_clk
->num_parents
;
558 if (num_parents
== 1)
562 * Linux kernel has inherent limitation
563 * of 255 clock parents at the moment.
564 * Right now, it is not expected that
565 * any mux clock from sci-clk driver
566 * would exceed that limit either, but
567 * the ABI basically provides that
568 * possibility. Print out a warning if
569 * this happens for any clock.
571 if (num_parents
>= 255) {
572 dev_warn(dev
, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n",
574 sci_clk
->clk_id
, num_parents
);
578 clk_id
= args
.args
[1] + 1;
580 while (num_parents
--) {
581 sci_clk
= devm_kzalloc(dev
,
586 sci_clk
->dev_id
= args
.args
[0];
587 sci_clk
->clk_id
= clk_id
++;
588 sci_clk
->provider
= provider
;
589 list_add_tail(&sci_clk
->node
, &clks
);
599 list_sort(NULL
, &clks
, _cmp_sci_clk_list
);
601 provider
->clocks
= devm_kmalloc_array(dev
, num_clks
, sizeof(sci_clk
),
603 if (!provider
->clocks
)
609 list_for_each_entry(sci_clk
, &clks
, node
) {
610 if (prev
&& prev
->dev_id
== sci_clk
->dev_id
&&
611 prev
->clk_id
== sci_clk
->clk_id
)
614 provider
->clocks
[num_clks
++] = sci_clk
;
618 provider
->num_clocks
= num_clks
;
625 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
626 * @pdev: platform device pointer to be probed
628 * Probes the TI SCI clock device. Allocates a new clock provider
629 * and registers this to the common clock framework. Also applies
630 * any required flags to the identified clocks via clock lists
631 * supplied from DT. Returns 0 for success, negative error value
634 static int ti_sci_clk_probe(struct platform_device
*pdev
)
636 struct device
*dev
= &pdev
->dev
;
637 struct device_node
*np
= dev
->of_node
;
638 struct sci_clk_provider
*provider
;
639 const struct ti_sci_handle
*handle
;
642 handle
= devm_ti_sci_get_handle(dev
);
644 return PTR_ERR(handle
);
646 provider
= devm_kzalloc(dev
, sizeof(*provider
), GFP_KERNEL
);
650 provider
->sci
= handle
;
651 provider
->ops
= &handle
->ops
.clk_ops
;
654 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
655 ret
= ti_sci_scan_clocks_from_fw(provider
);
657 dev_err(dev
, "scan clocks from FW failed: %d\n", ret
);
661 ret
= ti_sci_scan_clocks_from_dt(provider
);
663 dev_err(dev
, "scan clocks from DT failed: %d\n", ret
);
668 ret
= ti_sci_init_clocks(provider
);
670 pr_err("ti-sci-init-clocks failed.\n");
674 return of_clk_add_hw_provider(np
, sci_clk_get
, provider
);
678 * ti_sci_clk_remove - Remove TI SCI clock device
679 * @pdev: platform device pointer for the device to be removed
681 * Removes the TI SCI device. Unregisters the clock provider registered
682 * via common clock framework. Any memory allocated for the device will
683 * be free'd silently via the devm framework. Returns 0 always.
685 static int ti_sci_clk_remove(struct platform_device
*pdev
)
687 of_clk_del_provider(pdev
->dev
.of_node
);
692 static struct platform_driver ti_sci_clk_driver
= {
693 .probe
= ti_sci_clk_probe
,
694 .remove
= ti_sci_clk_remove
,
696 .name
= "ti-sci-clk",
697 .of_match_table
= of_match_ptr(ti_sci_clk_of_match
),
700 module_platform_driver(ti_sci_clk_driver
);
702 MODULE_LICENSE("GPL v2");
703 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
704 MODULE_AUTHOR("Tero Kristo");
705 MODULE_ALIAS("platform:ti-sci-clk");