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>
27 #define SCI_CLK_SSC_ENABLE BIT(0)
28 #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1)
29 #define SCI_CLK_INPUT_TERMINATION BIT(2)
32 * struct sci_clk_provider - TI SCI clock provider representation
33 * @sci: Handle to the System Control Interface protocol handler
34 * @ops: Pointer to the SCI ops to be used by the clocks
35 * @dev: Device pointer for the clock provider
36 * @clocks: Clocks array for this device
37 * @num_clocks: Total number of clocks for this provider
39 struct sci_clk_provider
{
40 const struct ti_sci_handle
*sci
;
41 const struct ti_sci_clk_ops
*ops
;
43 struct sci_clk
**clocks
;
48 * struct sci_clk - TI SCI clock representation
49 * @hw: Hardware clock cookie for common clock framework
50 * @dev_id: Device index
51 * @clk_id: Clock index
52 * @num_parents: Number of parents for this clock
53 * @provider: Master clock provider
54 * @flags: Flags for the clock
61 struct sci_clk_provider
*provider
;
65 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
68 * sci_clk_prepare - Prepare (enable) a TI SCI clock
69 * @hw: clock to prepare
71 * Prepares a clock to be actively used. Returns the SCI protocol status.
73 static int sci_clk_prepare(struct clk_hw
*hw
)
75 struct sci_clk
*clk
= to_sci_clk(hw
);
76 bool enable_ssc
= clk
->flags
& SCI_CLK_SSC_ENABLE
;
77 bool allow_freq_change
= clk
->flags
& SCI_CLK_ALLOW_FREQ_CHANGE
;
78 bool input_termination
= clk
->flags
& SCI_CLK_INPUT_TERMINATION
;
80 return clk
->provider
->ops
->get_clock(clk
->provider
->sci
, clk
->dev_id
,
81 clk
->clk_id
, enable_ssc
,
87 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
88 * @hw: clock to unprepare
90 * Un-prepares a clock from active state.
92 static void sci_clk_unprepare(struct clk_hw
*hw
)
94 struct sci_clk
*clk
= to_sci_clk(hw
);
97 ret
= clk
->provider
->ops
->put_clock(clk
->provider
->sci
, clk
->dev_id
,
100 dev_err(clk
->provider
->dev
,
101 "unprepare failed for dev=%d, clk=%d, ret=%d\n",
102 clk
->dev_id
, clk
->clk_id
, ret
);
106 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
107 * @hw: clock to check status for
109 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
110 * value if clock is enabled, zero otherwise.
112 static int sci_clk_is_prepared(struct clk_hw
*hw
)
114 struct sci_clk
*clk
= to_sci_clk(hw
);
115 bool req_state
, current_state
;
118 ret
= clk
->provider
->ops
->is_on(clk
->provider
->sci
, clk
->dev_id
,
119 clk
->clk_id
, &req_state
,
122 dev_err(clk
->provider
->dev
,
123 "is_prepared failed for dev=%d, clk=%d, ret=%d\n",
124 clk
->dev_id
, clk
->clk_id
, ret
);
132 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock
133 * @hw: clock to get rate for
134 * @parent_rate: parent rate provided by common clock framework, not used
136 * Gets the current clock rate of a TI SCI clock. Returns the current
137 * clock rate, or zero in failure.
139 static unsigned long sci_clk_recalc_rate(struct clk_hw
*hw
,
140 unsigned long parent_rate
)
142 struct sci_clk
*clk
= to_sci_clk(hw
);
146 ret
= clk
->provider
->ops
->get_freq(clk
->provider
->sci
, clk
->dev_id
,
149 dev_err(clk
->provider
->dev
,
150 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n",
151 clk
->dev_id
, clk
->clk_id
, ret
);
159 * sci_clk_determine_rate - Determines a clock rate a clock can be set to
160 * @hw: clock to change rate for
161 * @req: requested rate configuration for the clock
163 * Determines a suitable clock rate and parent for a TI SCI clock.
164 * The parent handling is un-used, as generally the parent clock rates
165 * are not known by the kernel; instead these are internally handled
166 * by the firmware. Returns 0 on success, negative error value on failure.
168 static int sci_clk_determine_rate(struct clk_hw
*hw
,
169 struct clk_rate_request
*req
)
171 struct sci_clk
*clk
= to_sci_clk(hw
);
175 ret
= clk
->provider
->ops
->get_best_match_freq(clk
->provider
->sci
,
183 dev_err(clk
->provider
->dev
,
184 "determine-rate failed for dev=%d, clk=%d, ret=%d\n",
185 clk
->dev_id
, clk
->clk_id
, ret
);
189 req
->rate
= new_rate
;
195 * sci_clk_set_rate - Set rate for a TI SCI clock
196 * @hw: clock to change rate for
197 * @rate: target rate for the clock
198 * @parent_rate: rate of the clock parent, not used for TI SCI clocks
200 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
203 static int sci_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
204 unsigned long parent_rate
)
206 struct sci_clk
*clk
= to_sci_clk(hw
);
208 return clk
->provider
->ops
->set_freq(clk
->provider
->sci
, clk
->dev_id
,
209 clk
->clk_id
, rate
, rate
, rate
);
213 * sci_clk_get_parent - Get the current parent of a TI SCI clock
214 * @hw: clock to get parent for
216 * Returns the index of the currently selected parent for a TI SCI clock.
218 static u8
sci_clk_get_parent(struct clk_hw
*hw
)
220 struct sci_clk
*clk
= to_sci_clk(hw
);
224 ret
= clk
->provider
->ops
->get_parent(clk
->provider
->sci
, clk
->dev_id
,
225 clk
->clk_id
, &parent_id
);
227 dev_err(clk
->provider
->dev
,
228 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
229 clk
->dev_id
, clk
->clk_id
, ret
);
233 return parent_id
- clk
->clk_id
- 1;
237 * sci_clk_set_parent - Set the parent of a TI SCI clock
238 * @hw: clock to set parent for
239 * @index: new parent index for the clock
241 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
243 static int sci_clk_set_parent(struct clk_hw
*hw
, u8 index
)
245 struct sci_clk
*clk
= to_sci_clk(hw
);
247 return clk
->provider
->ops
->set_parent(clk
->provider
->sci
, clk
->dev_id
,
249 index
+ 1 + clk
->clk_id
);
252 static const struct clk_ops sci_clk_ops
= {
253 .prepare
= sci_clk_prepare
,
254 .unprepare
= sci_clk_unprepare
,
255 .is_prepared
= sci_clk_is_prepared
,
256 .recalc_rate
= sci_clk_recalc_rate
,
257 .determine_rate
= sci_clk_determine_rate
,
258 .set_rate
= sci_clk_set_rate
,
259 .get_parent
= sci_clk_get_parent
,
260 .set_parent
= sci_clk_set_parent
,
264 * _sci_clk_get - Gets a handle for an SCI clock
265 * @provider: Handle to SCI clock provider
266 * @sci_clk: Handle to the SCI clock to populate
268 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
269 * entry and registers it with the common clock framework. Called from
270 * the common clock framework, when a corresponding of_clk_get call is
271 * executed, or recursively from itself when parsing parent clocks.
272 * Returns 0 on success, negative error code on failure.
274 static int _sci_clk_build(struct sci_clk_provider
*provider
,
275 struct sci_clk
*sci_clk
)
277 struct clk_init_data init
= { NULL
};
279 char **parent_names
= NULL
;
283 name
= kasprintf(GFP_KERNEL
, "%s:%d:%d", dev_name(provider
->dev
),
284 sci_clk
->dev_id
, sci_clk
->clk_id
);
289 * From kernel point of view, we only care about a clocks parents,
290 * if it has more than 1 possible parent. In this case, it is going
291 * to have mux functionality. Otherwise it is going to act as a root
294 if (sci_clk
->num_parents
< 2)
295 sci_clk
->num_parents
= 0;
297 if (sci_clk
->num_parents
) {
298 parent_names
= kcalloc(sci_clk
->num_parents
, sizeof(char *),
306 for (i
= 0; i
< sci_clk
->num_parents
; i
++) {
309 parent_name
= kasprintf(GFP_KERNEL
, "%s:%d:%d",
310 dev_name(provider
->dev
),
312 sci_clk
->clk_id
+ 1 + i
);
317 parent_names
[i
] = parent_name
;
319 init
.parent_names
= (void *)parent_names
;
322 init
.ops
= &sci_clk_ops
;
323 init
.num_parents
= sci_clk
->num_parents
;
324 sci_clk
->hw
.init
= &init
;
326 ret
= devm_clk_hw_register(provider
->dev
, &sci_clk
->hw
);
328 dev_err(provider
->dev
, "failed clk register with %d\n", ret
);
332 for (i
= 0; i
< sci_clk
->num_parents
; i
++)
333 kfree(parent_names
[i
]);
343 static int _cmp_sci_clk(const void *a
, const void *b
)
345 const struct sci_clk
*ca
= a
;
346 const struct sci_clk
*cb
= *(struct sci_clk
**)b
;
348 if (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
== cb
->clk_id
)
350 if (ca
->dev_id
> cb
->dev_id
||
351 (ca
->dev_id
== cb
->dev_id
&& ca
->clk_id
> cb
->clk_id
))
357 * sci_clk_get - Xlate function for getting clock handles
358 * @clkspec: device tree clock specifier
359 * @data: pointer to the clock provider
361 * Xlate function for retrieving clock TI SCI hw clock handles based on
362 * device tree clock specifier. Called from the common clock framework,
363 * when a corresponding of_clk_get call is executed. Returns a pointer
364 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
366 static struct clk_hw
*sci_clk_get(struct of_phandle_args
*clkspec
, void *data
)
368 struct sci_clk_provider
*provider
= data
;
369 struct sci_clk
**clk
;
372 if (clkspec
->args_count
!= 2)
373 return ERR_PTR(-EINVAL
);
375 key
.dev_id
= clkspec
->args
[0];
376 key
.clk_id
= clkspec
->args
[1];
378 clk
= bsearch(&key
, provider
->clocks
, provider
->num_clocks
,
379 sizeof(clk
), _cmp_sci_clk
);
382 return ERR_PTR(-ENODEV
);
387 static int ti_sci_init_clocks(struct sci_clk_provider
*p
)
392 for (i
= 0; i
< p
->num_clocks
; i
++) {
393 ret
= _sci_clk_build(p
, p
->clocks
[i
]);
401 static const struct of_device_id ti_sci_clk_of_match
[] = {
402 { .compatible
= "ti,k2g-sci-clk" },
405 MODULE_DEVICE_TABLE(of
, ti_sci_clk_of_match
);
408 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
409 * @pdev: platform device pointer to be probed
411 * Probes the TI SCI clock device. Allocates a new clock provider
412 * and registers this to the common clock framework. Also applies
413 * any required flags to the identified clocks via clock lists
414 * supplied from DT. Returns 0 for success, negative error value
417 static int ti_sci_clk_probe(struct platform_device
*pdev
)
419 struct device
*dev
= &pdev
->dev
;
420 struct device_node
*np
= dev
->of_node
;
421 struct sci_clk_provider
*provider
;
422 const struct ti_sci_handle
*handle
;
425 struct sci_clk
**clks
= NULL
;
426 struct sci_clk
**tmp_clks
;
427 struct sci_clk
*sci_clk
;
434 handle
= devm_ti_sci_get_handle(dev
);
436 return PTR_ERR(handle
);
438 provider
= devm_kzalloc(dev
, sizeof(*provider
), GFP_KERNEL
);
442 provider
->sci
= handle
;
443 provider
->ops
= &handle
->ops
.clk_ops
;
447 ret
= provider
->ops
->get_num_parents(provider
->sci
, dev_id
,
448 clk_id
, &num_parents
);
469 if (num_clks
== max_clks
) {
470 tmp_clks
= devm_kmalloc_array(dev
, max_clks
+ 64,
473 memcpy(tmp_clks
, clks
, max_clks
* sizeof(sci_clk
));
475 devm_kfree(dev
, clks
);
480 sci_clk
= devm_kzalloc(dev
, sizeof(*sci_clk
), GFP_KERNEL
);
483 sci_clk
->dev_id
= dev_id
;
484 sci_clk
->clk_id
= clk_id
;
485 sci_clk
->provider
= provider
;
486 sci_clk
->num_parents
= num_parents
;
488 clks
[num_clks
] = sci_clk
;
494 provider
->clocks
= devm_kmalloc_array(dev
, num_clks
, sizeof(sci_clk
),
496 if (!provider
->clocks
)
499 memcpy(provider
->clocks
, clks
, num_clks
* sizeof(sci_clk
));
501 provider
->num_clocks
= num_clks
;
503 devm_kfree(dev
, clks
);
505 ret
= ti_sci_init_clocks(provider
);
507 pr_err("ti-sci-init-clocks failed.\n");
511 return of_clk_add_hw_provider(np
, sci_clk_get
, provider
);
515 * ti_sci_clk_remove - Remove TI SCI clock device
516 * @pdev: platform device pointer for the device to be removed
518 * Removes the TI SCI device. Unregisters the clock provider registered
519 * via common clock framework. Any memory allocated for the device will
520 * be free'd silently via the devm framework. Returns 0 always.
522 static int ti_sci_clk_remove(struct platform_device
*pdev
)
524 of_clk_del_provider(pdev
->dev
.of_node
);
529 static struct platform_driver ti_sci_clk_driver
= {
530 .probe
= ti_sci_clk_probe
,
531 .remove
= ti_sci_clk_remove
,
533 .name
= "ti-sci-clk",
534 .of_match_table
= of_match_ptr(ti_sci_clk_of_match
),
537 module_platform_driver(ti_sci_clk_driver
);
539 MODULE_LICENSE("GPL v2");
540 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
541 MODULE_AUTHOR("Tero Kristo");
542 MODULE_ALIAS("platform:ti-sci-clk");