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_data - TI SCI clock data
34 * @num_clks: number of clocks for this device
42 * struct sci_clk_provider - TI SCI clock provider representation
43 * @sci: Handle to the System Control Interface protocol handler
44 * @ops: Pointer to the SCI ops to be used by the clocks
45 * @dev: Device pointer for the clock provider
46 * @clk_data: Clock data
47 * @clocks: Clocks array for this device
48 * @num_clocks: Total number of clocks for this provider
50 struct sci_clk_provider {
51 const struct ti_sci_handle *sci;
52 const struct ti_sci_clk_ops *ops;
54 const struct sci_clk_data *clk_data;
55 struct clk_hw **clocks;
60 * struct sci_clk - TI SCI clock representation
61 * @hw: Hardware clock cookie for common clock framework
62 * @dev_id: Device index
63 * @clk_id: Clock index
64 * @provider: Master clock provider
65 * @flags: Flags for the clock
71 struct sci_clk_provider *provider;
75 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
78 * sci_clk_prepare - Prepare (enable) a TI SCI clock
79 * @hw: clock to prepare
81 * Prepares a clock to be actively used. Returns the SCI protocol status.
83 static int sci_clk_prepare(struct clk_hw *hw)
85 struct sci_clk *clk = to_sci_clk(hw);
86 bool enable_ssc = clk->flags & SCI_CLK_SSC_ENABLE;
87 bool allow_freq_change = clk->flags & SCI_CLK_ALLOW_FREQ_CHANGE;
88 bool input_termination = clk->flags & SCI_CLK_INPUT_TERMINATION;
90 return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id,
91 clk->clk_id, enable_ssc,
97 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
98 * @hw: clock to unprepare
100 * Un-prepares a clock from active state.
102 static void sci_clk_unprepare(struct clk_hw *hw)
104 struct sci_clk *clk = to_sci_clk(hw);
107 ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id,
110 dev_err(clk->provider->dev,
111 "unprepare failed for dev=%d, clk=%d, ret=%d\n",
112 clk->dev_id, clk->clk_id, ret);
116 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
117 * @hw: clock to check status for
119 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
120 * value if clock is enabled, zero otherwise.
122 static int sci_clk_is_prepared(struct clk_hw *hw)
124 struct sci_clk *clk = to_sci_clk(hw);
125 bool req_state, current_state;
128 ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id,
129 clk->clk_id, &req_state,
132 dev_err(clk->provider->dev,
133 "is_prepared failed for dev=%d, clk=%d, ret=%d\n",
134 clk->dev_id, clk->clk_id, ret);
142 * sci_clk_recalc_rate - Get clock rate for a TI SCI clock
143 * @hw: clock to get rate for
144 * @parent_rate: parent rate provided by common clock framework, not used
146 * Gets the current clock rate of a TI SCI clock. Returns the current
147 * clock rate, or zero in failure.
149 static unsigned long sci_clk_recalc_rate(struct clk_hw *hw,
150 unsigned long parent_rate)
152 struct sci_clk *clk = to_sci_clk(hw);
156 ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id,
159 dev_err(clk->provider->dev,
160 "recalc-rate failed for dev=%d, clk=%d, ret=%d\n",
161 clk->dev_id, clk->clk_id, ret);
169 * sci_clk_determine_rate - Determines a clock rate a clock can be set to
170 * @hw: clock to change rate for
171 * @req: requested rate configuration for the clock
173 * Determines a suitable clock rate and parent for a TI SCI clock.
174 * The parent handling is un-used, as generally the parent clock rates
175 * are not known by the kernel; instead these are internally handled
176 * by the firmware. Returns 0 on success, negative error value on failure.
178 static int sci_clk_determine_rate(struct clk_hw *hw,
179 struct clk_rate_request *req)
181 struct sci_clk *clk = to_sci_clk(hw);
185 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci,
193 dev_err(clk->provider->dev,
194 "determine-rate failed for dev=%d, clk=%d, ret=%d\n",
195 clk->dev_id, clk->clk_id, ret);
199 req->rate = new_rate;
205 * sci_clk_set_rate - Set rate for a TI SCI clock
206 * @hw: clock to change rate for
207 * @rate: target rate for the clock
208 * @parent_rate: rate of the clock parent, not used for TI SCI clocks
210 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
213 static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate,
214 unsigned long parent_rate)
216 struct sci_clk *clk = to_sci_clk(hw);
218 return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id,
219 clk->clk_id, rate, rate, rate);
223 * sci_clk_get_parent - Get the current parent of a TI SCI clock
224 * @hw: clock to get parent for
226 * Returns the index of the currently selected parent for a TI SCI clock.
228 static u8 sci_clk_get_parent(struct clk_hw *hw)
230 struct sci_clk *clk = to_sci_clk(hw);
234 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
235 clk->clk_id, &parent_id);
237 dev_err(clk->provider->dev,
238 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
239 clk->dev_id, clk->clk_id, ret);
243 return parent_id - clk->clk_id - 1;
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);
257 return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
259 index + 1 + clk->clk_id);
262 static const struct clk_ops sci_clk_ops = {
263 .prepare = sci_clk_prepare,
264 .unprepare = sci_clk_unprepare,
265 .is_prepared = sci_clk_is_prepared,
266 .recalc_rate = sci_clk_recalc_rate,
267 .determine_rate = sci_clk_determine_rate,
268 .set_rate = sci_clk_set_rate,
269 .get_parent = sci_clk_get_parent,
270 .set_parent = sci_clk_set_parent,
274 * _sci_clk_get - Gets a handle for an SCI clock
275 * @provider: Handle to SCI clock provider
276 * @dev_id: device ID for the clock to register
277 * @clk_id: clock ID for the clock to register
279 * Gets a handle to an existing TI SCI hw clock, or builds a new clock
280 * entry and registers it with the common clock framework. Called from
281 * the common clock framework, when a corresponding of_clk_get call is
282 * executed, or recursively from itself when parsing parent clocks.
283 * Returns a pointer to the hw clock struct, or ERR_PTR value in failure.
285 static struct clk_hw *_sci_clk_build(struct sci_clk_provider *provider,
286 u16 dev_id, u8 clk_id)
288 struct clk_init_data init = { NULL };
289 struct sci_clk *sci_clk = NULL;
291 char **parent_names = NULL;
295 sci_clk = devm_kzalloc(provider->dev, sizeof(*sci_clk), GFP_KERNEL);
297 return ERR_PTR(-ENOMEM);
299 sci_clk->dev_id = dev_id;
300 sci_clk->clk_id = clk_id;
301 sci_clk->provider = provider;
303 ret = provider->ops->get_num_parents(provider->sci, dev_id,
309 name = kasprintf(GFP_KERNEL, "%s:%d:%d", dev_name(provider->dev),
310 sci_clk->dev_id, sci_clk->clk_id);
315 * From kernel point of view, we only care about a clocks parents,
316 * if it has more than 1 possible parent. In this case, it is going
317 * to have mux functionality. Otherwise it is going to act as a root
320 if (init.num_parents < 2)
321 init.num_parents = 0;
323 if (init.num_parents) {
324 parent_names = kcalloc(init.num_parents, sizeof(char *),
332 for (i = 0; i < init.num_parents; i++) {
335 parent_name = kasprintf(GFP_KERNEL, "%s:%d:%d",
336 dev_name(provider->dev),
338 sci_clk->clk_id + 1 + i);
343 parent_names[i] = parent_name;
345 init.parent_names = (void *)parent_names;
348 init.ops = &sci_clk_ops;
349 sci_clk->hw.init = &init;
351 ret = devm_clk_hw_register(provider->dev, &sci_clk->hw);
353 dev_err(provider->dev, "failed clk register with %d\n", ret);
357 for (i = 0; i < init.num_parents; i++)
358 kfree(parent_names[i]);
371 static int _cmp_sci_clk(const void *a, const void *b)
373 const struct sci_clk *ca = a;
374 const struct sci_clk *cb = *(struct sci_clk **)b;
376 if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id)
378 if (ca->dev_id > cb->dev_id ||
379 (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id))
385 * sci_clk_get - Xlate function for getting clock handles
386 * @clkspec: device tree clock specifier
387 * @data: pointer to the clock provider
389 * Xlate function for retrieving clock TI SCI hw clock handles based on
390 * device tree clock specifier. Called from the common clock framework,
391 * when a corresponding of_clk_get call is executed. Returns a pointer
392 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
394 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data)
396 struct sci_clk_provider *provider = data;
397 struct sci_clk **clk;
400 if (clkspec->args_count != 2)
401 return ERR_PTR(-EINVAL);
403 key.dev_id = clkspec->args[0];
404 key.clk_id = clkspec->args[1];
406 clk = bsearch(&key, provider->clocks, provider->num_clocks,
407 sizeof(clk), _cmp_sci_clk);
410 return ERR_PTR(-ENODEV);
415 static int ti_sci_init_clocks(struct sci_clk_provider *p)
417 const struct sci_clk_data *data = p->clk_data;
422 while (data->num_clks) {
423 num_clks += data->num_clks;
427 p->num_clocks = num_clks;
429 p->clocks = devm_kcalloc(p->dev, num_clks, sizeof(struct sci_clk),
438 while (data->num_clks) {
439 for (i = 0; i < data->num_clks; i++) {
440 hw = _sci_clk_build(p, data->dev, i);
442 p->clocks[num_clks++] = hw;
446 /* Skip any holes in the clock lists */
447 if (PTR_ERR(hw) == -ENODEV)
458 static const struct sci_clk_data k2g_clk_data[] = {
460 { .dev = 0x0, .num_clks = 4 },
463 { .dev = 0x1, .num_clks = 5 },
466 { .dev = 0x2, .num_clks = 2 },
469 { .dev = 0x3, .num_clks = 8 },
472 { .dev = 0x4, .num_clks = 8 },
475 { .dev = 0x5, .num_clks = 8 },
478 { .dev = 0x6, .num_clks = 8 },
481 { .dev = 0x8, .num_clks = 2 },
484 { .dev = 0x9, .num_clks = 2 },
487 { .dev = 0xa, .num_clks = 6 },
490 { .dev = 0xb, .num_clks = 3 },
493 { .dev = 0xc, .num_clks = 3 },
496 { .dev = 0xd, .num_clks = 1 },
499 { .dev = 0xe, .num_clks = 1 },
502 { .dev = 0x10, .num_clks = 1 },
505 { .dev = 0x11, .num_clks = 1 },
508 { .dev = 0x12, .num_clks = 1 },
511 { .dev = 0x13, .num_clks = 1 },
514 { .dev = 0x14, .num_clks = 6 },
517 { .dev = 0x15, .num_clks = 6 },
520 { .dev = 0x16, .num_clks = 7 },
523 { .dev = 0x17, .num_clks = 7 },
526 { .dev = 0x18, .num_clks = 14 },
529 { .dev = 0x19, .num_clks = 1 },
532 { .dev = 0x1b, .num_clks = 1 },
535 { .dev = 0x1c, .num_clks = 1 },
538 { .dev = 0x1d, .num_clks = 9 },
541 { .dev = 0x1e, .num_clks = 9 },
544 { .dev = 0x1f, .num_clks = 9 },
547 { .dev = 0x20, .num_clks = 9 },
550 { .dev = 0x21, .num_clks = 9 },
553 { .dev = 0x22, .num_clks = 9 },
556 { .dev = 0x23, .num_clks = 9 },
559 { .dev = 0x25, .num_clks = 1 },
562 { .dev = 0x26, .num_clks = 1 },
565 { .dev = 0x27, .num_clks = 1 },
568 { .dev = 0x29, .num_clks = 1 },
571 { .dev = 0x2b, .num_clks = 8 },
574 { .dev = 0x2c, .num_clks = 1 },
577 { .dev = 0x2d, .num_clks = 1 },
580 { .dev = 0x2e, .num_clks = 1 },
583 { .dev = 0x2f, .num_clks = 1 },
586 { .dev = 0x30, .num_clks = 1 },
589 { .dev = 0x31, .num_clks = 1 },
592 { .dev = 0x32, .num_clks = 1 },
595 { .dev = 0x33, .num_clks = 1 },
598 { .dev = 0x34, .num_clks = 1 },
601 { .dev = 0x35, .num_clks = 1 },
604 { .dev = 0x36, .num_clks = 1 },
607 { .dev = 0x37, .num_clks = 1 },
610 { .dev = 0x38, .num_clks = 1 },
613 { .dev = 0x39, .num_clks = 1 },
616 { .dev = 0x3a, .num_clks = 1 },
619 { .dev = 0x3b, .num_clks = 1 },
622 { .dev = 0x3c, .num_clks = 1 },
625 { .dev = 0x3f, .num_clks = 2 },
628 { .dev = 0x40, .num_clks = 1 },
631 { .dev = 0x41, .num_clks = 1 },
634 { .dev = 0x42, .num_clks = 1 },
637 { .dev = 0x43, .num_clks = 5 },
639 /* arm_64b_counter0 */
640 { .dev = 0x44, .num_clks = 2 },
643 { .dev = 0x45, .num_clks = 2 },
646 { .dev = 0x46, .num_clks = 2 },
649 { .dev = 0x47, .num_clks = 1 },
652 { .dev = 0x49, .num_clks = 1 },
655 { .dev = 0x4c, .num_clks = 36 },
658 { .dev = 0x4f, .num_clks = 2 },
662 static const struct of_device_id ti_sci_clk_of_match[] = {
663 { .compatible = "ti,k2g-sci-clk", .data = &k2g_clk_data },
666 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
669 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
670 * @pdev: platform device pointer to be probed
672 * Probes the TI SCI clock device. Allocates a new clock provider
673 * and registers this to the common clock framework. Also applies
674 * any required flags to the identified clocks via clock lists
675 * supplied from DT. Returns 0 for success, negative error value
678 static int ti_sci_clk_probe(struct platform_device *pdev)
680 struct device *dev = &pdev->dev;
681 struct device_node *np = dev->of_node;
682 struct sci_clk_provider *provider;
683 const struct ti_sci_handle *handle;
684 const struct sci_clk_data *data;
687 data = of_device_get_match_data(dev);
691 handle = devm_ti_sci_get_handle(dev);
693 return PTR_ERR(handle);
695 provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
699 provider->clk_data = data;
701 provider->sci = handle;
702 provider->ops = &handle->ops.clk_ops;
705 ret = ti_sci_init_clocks(provider);
707 pr_err("ti-sci-init-clocks failed.\n");
711 return of_clk_add_hw_provider(np, sci_clk_get, provider);
715 * ti_sci_clk_remove - Remove TI SCI clock device
716 * @pdev: platform device pointer for the device to be removed
718 * Removes the TI SCI device. Unregisters the clock provider registered
719 * via common clock framework. Any memory allocated for the device will
720 * be free'd silently via the devm framework. Returns 0 always.
722 static int ti_sci_clk_remove(struct platform_device *pdev)
724 of_clk_del_provider(pdev->dev.of_node);
729 static struct platform_driver ti_sci_clk_driver = {
730 .probe = ti_sci_clk_probe,
731 .remove = ti_sci_clk_remove,
733 .name = "ti-sci-clk",
734 .of_match_table = of_match_ptr(ti_sci_clk_of_match),
737 module_platform_driver(ti_sci_clk_driver);
739 MODULE_LICENSE("GPL v2");
740 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
741 MODULE_AUTHOR("Tero Kristo");
742 MODULE_ALIAS("platform:ti-sci-clk");