1 /*
2 * SCI Clock driver for keystone based devices
3 *
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Tero Kristo <t-kristo@ti.com>
6 *
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.
10 *
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.
15 */
16 #include <linux/clk-provider.h>
17 #include <linux/err.h>
18 #include <linux/io.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>
27
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)
31
32 /**
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
39 */
40 struct sci_clk_provider {
41 const struct ti_sci_handle *sci;
42 const struct ti_sci_clk_ops *ops;
43 struct device *dev;
44 struct sci_clk **clocks;
45 int num_clocks;
46 };
47
48 /**
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 */
58 struct sci_clk {
59 struct clk_hw hw;
60 u16 dev_id;
61 u32 clk_id;
62 u32 num_parents;
63 struct sci_clk_provider *provider;
64 u8 flags;
65 struct list_head node;
66 };
67
68 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
69
70 /**
71 * sci_clk_prepare - Prepare (enable) a TI SCI clock
72 * @hw: clock to prepare
73 *
74 * Prepares a clock to be actively used. Returns the SCI protocol status.
75 */
sci_clk_prepare(struct clk_hw * hw)76 static int sci_clk_prepare(struct clk_hw *hw)
77 {
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;
82
83 return clk->provider->ops->get_clock(clk->provider->sci, clk->dev_id,
84 clk->clk_id, enable_ssc,
85 allow_freq_change,
86 input_termination);
87 }
88
89 /**
90 * sci_clk_unprepare - Un-prepares (disables) a TI SCI clock
91 * @hw: clock to unprepare
92 *
93 * Un-prepares a clock from active state.
94 */
sci_clk_unprepare(struct clk_hw * hw)95 static void sci_clk_unprepare(struct clk_hw *hw)
96 {
97 struct sci_clk *clk = to_sci_clk(hw);
98 int ret;
99
100 ret = clk->provider->ops->put_clock(clk->provider->sci, clk->dev_id,
101 clk->clk_id);
102 if (ret)
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);
106 }
107
108 /**
109 * sci_clk_is_prepared - Check if a TI SCI clock is prepared or not
110 * @hw: clock to check status for
111 *
112 * Checks if a clock is prepared (enabled) in hardware. Returns non-zero
113 * value if clock is enabled, zero otherwise.
114 */
sci_clk_is_prepared(struct clk_hw * hw)115 static int sci_clk_is_prepared(struct clk_hw *hw)
116 {
117 struct sci_clk *clk = to_sci_clk(hw);
118 bool req_state, current_state;
119 int ret;
120
121 ret = clk->provider->ops->is_on(clk->provider->sci, clk->dev_id,
122 clk->clk_id, &req_state,
123 ¤t_state);
124 if (ret) {
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);
128 return 0;
129 }
130
131 return req_state;
132 }
133
134 /**
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
138 *
139 * Gets the current clock rate of a TI SCI clock. Returns the current
140 * clock rate, or zero in failure.
141 */
sci_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)142 static unsigned long sci_clk_recalc_rate(struct clk_hw *hw,
143 unsigned long parent_rate)
144 {
145 struct sci_clk *clk = to_sci_clk(hw);
146 u64 freq;
147 int ret;
148
149 ret = clk->provider->ops->get_freq(clk->provider->sci, clk->dev_id,
150 clk->clk_id, &freq);
151 if (ret) {
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);
155 return 0;
156 }
157
158 return freq;
159 }
160
161 /**
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
165 *
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.
170 */
sci_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)171 static int sci_clk_determine_rate(struct clk_hw *hw,
172 struct clk_rate_request *req)
173 {
174 struct sci_clk *clk = to_sci_clk(hw);
175 int ret;
176 u64 new_rate;
177
178 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci,
179 clk->dev_id,
180 clk->clk_id,
181 req->min_rate,
182 req->rate,
183 req->max_rate,
184 &new_rate);
185 if (ret) {
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);
189 return ret;
190 }
191
192 req->rate = new_rate;
193
194 return 0;
195 }
196
197 /**
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
202 *
203 * Sets a clock frequency for a TI SCI clock. Returns the TI SCI
204 * protocol status.
205 */
sci_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)206 static int sci_clk_set_rate(struct clk_hw *hw, unsigned long rate,
207 unsigned long parent_rate)
208 {
209 struct sci_clk *clk = to_sci_clk(hw);
210
211 return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id,
212 clk->clk_id, rate, rate, rate);
213 }
214
215 /**
216 * sci_clk_get_parent - Get the current parent of a TI SCI clock
217 * @hw: clock to get parent for
218 *
219 * Returns the index of the currently selected parent for a TI SCI clock.
220 */
sci_clk_get_parent(struct clk_hw * hw)221 static u8 sci_clk_get_parent(struct clk_hw *hw)
222 {
223 struct sci_clk *clk = to_sci_clk(hw);
224 u32 parent_id = 0;
225 int ret;
226
227 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
228 clk->clk_id, (void *)&parent_id);
229 if (ret) {
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);
233 return 0;
234 }
235
236 parent_id = parent_id - clk->clk_id - 1;
237
238 return (u8)parent_id;
239 }
240
241 /**
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
245 *
246 * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
247 */
sci_clk_set_parent(struct clk_hw * hw,u8 index)248 static int sci_clk_set_parent(struct clk_hw *hw, u8 index)
249 {
250 struct sci_clk *clk = to_sci_clk(hw);
251
252 return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
253 clk->clk_id,
254 index + 1 + clk->clk_id);
255 }
256
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,
266 };
267
268 /**
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
272 *
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.
278 */
_sci_clk_build(struct sci_clk_provider * provider,struct sci_clk * sci_clk)279 static int _sci_clk_build(struct sci_clk_provider *provider,
280 struct sci_clk *sci_clk)
281 {
282 struct clk_init_data init = { NULL };
283 char *name = NULL;
284 char **parent_names = NULL;
285 int i;
286 int ret = 0;
287
288 name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id,
289 sci_clk->clk_id);
290 if (!name)
291 return -ENOMEM;
292
293 init.name = name;
294
295 /*
296 * From kernel point of view, we only care about a clocks parents,
297 * if it has more than 1 possible parent. In this case, it is going
298 * to have mux functionality. Otherwise it is going to act as a root
299 * clock.
300 */
301 if (sci_clk->num_parents < 2)
302 sci_clk->num_parents = 0;
303
304 if (sci_clk->num_parents) {
305 parent_names = kcalloc(sci_clk->num_parents, sizeof(char *),
306 GFP_KERNEL);
307
308 if (!parent_names) {
309 ret = -ENOMEM;
310 goto err;
311 }
312
313 for (i = 0; i < sci_clk->num_parents; i++) {
314 char *parent_name;
315
316 parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d",
317 sci_clk->dev_id,
318 sci_clk->clk_id + 1 + i);
319 if (!parent_name) {
320 ret = -ENOMEM;
321 goto err;
322 }
323 parent_names[i] = parent_name;
324 }
325 init.parent_names = (void *)parent_names;
326 }
327
328 init.ops = &sci_clk_ops;
329 init.num_parents = sci_clk->num_parents;
330 sci_clk->hw.init = &init;
331
332 ret = devm_clk_hw_register(provider->dev, &sci_clk->hw);
333 if (ret)
334 dev_err(provider->dev, "failed clk register with %d\n", ret);
335
336 err:
337 if (parent_names) {
338 for (i = 0; i < sci_clk->num_parents; i++)
339 kfree(parent_names[i]);
340
341 kfree(parent_names);
342 }
343
344 kfree(name);
345
346 return ret;
347 }
348
_cmp_sci_clk(const void * a,const void * b)349 static int _cmp_sci_clk(const void *a, const void *b)
350 {
351 const struct sci_clk *ca = a;
352 const struct sci_clk *cb = *(struct sci_clk **)b;
353
354 if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id)
355 return 0;
356 if (ca->dev_id > cb->dev_id ||
357 (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id))
358 return 1;
359 return -1;
360 }
361
362 /**
363 * sci_clk_get - Xlate function for getting clock handles
364 * @clkspec: device tree clock specifier
365 * @data: pointer to the clock provider
366 *
367 * Xlate function for retrieving clock TI SCI hw clock handles based on
368 * device tree clock specifier. Called from the common clock framework,
369 * when a corresponding of_clk_get call is executed. Returns a pointer
370 * to the TI SCI hw clock struct, or ERR_PTR value in failure.
371 */
sci_clk_get(struct of_phandle_args * clkspec,void * data)372 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data)
373 {
374 struct sci_clk_provider *provider = data;
375 struct sci_clk **clk;
376 struct sci_clk key;
377
378 if (clkspec->args_count != 2)
379 return ERR_PTR(-EINVAL);
380
381 key.dev_id = clkspec->args[0];
382 key.clk_id = clkspec->args[1];
383
384 clk = bsearch(&key, provider->clocks, provider->num_clocks,
385 sizeof(clk), _cmp_sci_clk);
386
387 if (!clk)
388 return ERR_PTR(-ENODEV);
389
390 return &(*clk)->hw;
391 }
392
ti_sci_init_clocks(struct sci_clk_provider * p)393 static int ti_sci_init_clocks(struct sci_clk_provider *p)
394 {
395 int i;
396 int ret;
397
398 for (i = 0; i < p->num_clocks; i++) {
399 ret = _sci_clk_build(p, p->clocks[i]);
400 if (ret)
401 return ret;
402 }
403
404 return 0;
405 }
406
407 static const struct of_device_id ti_sci_clk_of_match[] = {
408 { .compatible = "ti,k2g-sci-clk" },
409 { /* Sentinel */ },
410 };
411 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
412
413 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
ti_sci_scan_clocks_from_fw(struct sci_clk_provider * provider)414 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
415 {
416 int ret;
417 int num_clks = 0;
418 struct sci_clk **clks = NULL;
419 struct sci_clk **tmp_clks;
420 struct sci_clk *sci_clk;
421 int max_clks = 0;
422 int clk_id = 0;
423 int dev_id = 0;
424 u32 num_parents = 0;
425 int gap_size = 0;
426 struct device *dev = provider->dev;
427
428 while (1) {
429 ret = provider->ops->get_num_parents(provider->sci, dev_id,
430 clk_id,
431 (void *)&num_parents);
432 if (ret) {
433 gap_size++;
434 if (!clk_id) {
435 if (gap_size >= 5)
436 break;
437 dev_id++;
438 } else {
439 if (gap_size >= 2) {
440 dev_id++;
441 clk_id = 0;
442 gap_size = 0;
443 } else {
444 clk_id++;
445 }
446 }
447 continue;
448 }
449
450 gap_size = 0;
451
452 if (num_clks == max_clks) {
453 tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
454 sizeof(sci_clk),
455 GFP_KERNEL);
456 memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
457 if (max_clks)
458 devm_kfree(dev, clks);
459 max_clks += 64;
460 clks = tmp_clks;
461 }
462
463 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL);
464 if (!sci_clk)
465 return -ENOMEM;
466 sci_clk->dev_id = dev_id;
467 sci_clk->clk_id = clk_id;
468 sci_clk->provider = provider;
469 sci_clk->num_parents = num_parents;
470
471 clks[num_clks] = sci_clk;
472
473 clk_id++;
474 num_clks++;
475 }
476
477 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
478 GFP_KERNEL);
479 if (!provider->clocks)
480 return -ENOMEM;
481
482 memcpy(provider->clocks, clks, num_clks * sizeof(sci_clk));
483
484 provider->num_clocks = num_clks;
485
486 devm_kfree(dev, clks);
487
488 return 0;
489 }
490
491 #else
492
_cmp_sci_clk_list(void * priv,struct list_head * a,struct list_head * b)493 static int _cmp_sci_clk_list(void *priv, struct list_head *a,
494 struct list_head *b)
495 {
496 struct sci_clk *ca = container_of(a, struct sci_clk, node);
497 struct sci_clk *cb = container_of(b, struct sci_clk, node);
498
499 return _cmp_sci_clk(ca, &cb);
500 }
501
ti_sci_scan_clocks_from_dt(struct sci_clk_provider * provider)502 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider)
503 {
504 struct device *dev = provider->dev;
505 struct device_node *np = NULL;
506 int ret;
507 int index;
508 struct of_phandle_args args;
509 struct list_head clks;
510 struct sci_clk *sci_clk, *prev;
511 int num_clks = 0;
512 int num_parents;
513 int clk_id;
514 const char * const clk_names[] = {
515 "clocks", "assigned-clocks", "assigned-clock-parents", NULL
516 };
517 const char * const *clk_name;
518
519 INIT_LIST_HEAD(&clks);
520
521 clk_name = clk_names;
522
523 while (*clk_name) {
524 np = of_find_node_with_property(np, *clk_name);
525 if (!np) {
526 clk_name++;
527 continue;
528 }
529
530 if (!of_device_is_available(np))
531 continue;
532
533 index = 0;
534
535 do {
536 ret = of_parse_phandle_with_args(np, *clk_name,
537 "#clock-cells", index,
538 &args);
539 if (ret)
540 break;
541
542 if (args.args_count == 2 && args.np == dev->of_node) {
543 sci_clk = devm_kzalloc(dev, sizeof(*sci_clk),
544 GFP_KERNEL);
545 if (!sci_clk)
546 return -ENOMEM;
547
548 sci_clk->dev_id = args.args[0];
549 sci_clk->clk_id = args.args[1];
550 sci_clk->provider = provider;
551 provider->ops->get_num_parents(provider->sci,
552 sci_clk->dev_id,
553 sci_clk->clk_id,
554 (void *)&sci_clk->num_parents);
555 list_add_tail(&sci_clk->node, &clks);
556
557 num_clks++;
558
559 num_parents = sci_clk->num_parents;
560 if (num_parents == 1)
561 num_parents = 0;
562
563 /*
564 * Linux kernel has inherent limitation
565 * of 255 clock parents at the moment.
566 * Right now, it is not expected that
567 * any mux clock from sci-clk driver
568 * would exceed that limit either, but
569 * the ABI basically provides that
570 * possibility. Print out a warning if
571 * this happens for any clock.
572 */
573 if (num_parents >= 255) {
574 dev_warn(dev, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n",
575 sci_clk->dev_id,
576 sci_clk->clk_id, num_parents);
577 num_parents = 255;
578 }
579
580 clk_id = args.args[1] + 1;
581
582 while (num_parents--) {
583 sci_clk = devm_kzalloc(dev,
584 sizeof(*sci_clk),
585 GFP_KERNEL);
586 if (!sci_clk)
587 return -ENOMEM;
588 sci_clk->dev_id = args.args[0];
589 sci_clk->clk_id = clk_id++;
590 sci_clk->provider = provider;
591 list_add_tail(&sci_clk->node, &clks);
592
593 num_clks++;
594 }
595 }
596
597 index++;
598 } while (args.np);
599 }
600
601 list_sort(NULL, &clks, _cmp_sci_clk_list);
602
603 provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
604 GFP_KERNEL);
605 if (!provider->clocks)
606 return -ENOMEM;
607
608 num_clks = 0;
609 prev = NULL;
610
611 list_for_each_entry(sci_clk, &clks, node) {
612 if (prev && prev->dev_id == sci_clk->dev_id &&
613 prev->clk_id == sci_clk->clk_id)
614 continue;
615
616 provider->clocks[num_clks++] = sci_clk;
617 prev = sci_clk;
618 }
619
620 provider->num_clocks = num_clks;
621
622 return 0;
623 }
624 #endif
625
626 /**
627 * ti_sci_clk_probe - Probe function for the TI SCI clock driver
628 * @pdev: platform device pointer to be probed
629 *
630 * Probes the TI SCI clock device. Allocates a new clock provider
631 * and registers this to the common clock framework. Also applies
632 * any required flags to the identified clocks via clock lists
633 * supplied from DT. Returns 0 for success, negative error value
634 * for failure.
635 */
ti_sci_clk_probe(struct platform_device * pdev)636 static int ti_sci_clk_probe(struct platform_device *pdev)
637 {
638 struct device *dev = &pdev->dev;
639 struct device_node *np = dev->of_node;
640 struct sci_clk_provider *provider;
641 const struct ti_sci_handle *handle;
642 int ret;
643
644 handle = devm_ti_sci_get_handle(dev);
645 if (IS_ERR(handle))
646 return PTR_ERR(handle);
647
648 provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
649 if (!provider)
650 return -ENOMEM;
651
652 provider->sci = handle;
653 provider->ops = &handle->ops.clk_ops;
654 provider->dev = dev;
655
656 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
657 ret = ti_sci_scan_clocks_from_fw(provider);
658 if (ret) {
659 dev_err(dev, "scan clocks from FW failed: %d\n", ret);
660 return ret;
661 }
662 #else
663 ret = ti_sci_scan_clocks_from_dt(provider);
664 if (ret) {
665 dev_err(dev, "scan clocks from DT failed: %d\n", ret);
666 return ret;
667 }
668 #endif
669
670 ret = ti_sci_init_clocks(provider);
671 if (ret) {
672 pr_err("ti-sci-init-clocks failed.\n");
673 return ret;
674 }
675
676 return of_clk_add_hw_provider(np, sci_clk_get, provider);
677 }
678
679 /**
680 * ti_sci_clk_remove - Remove TI SCI clock device
681 * @pdev: platform device pointer for the device to be removed
682 *
683 * Removes the TI SCI device. Unregisters the clock provider registered
684 * via common clock framework. Any memory allocated for the device will
685 * be free'd silently via the devm framework. Returns 0 always.
686 */
ti_sci_clk_remove(struct platform_device * pdev)687 static int ti_sci_clk_remove(struct platform_device *pdev)
688 {
689 of_clk_del_provider(pdev->dev.of_node);
690
691 return 0;
692 }
693
694 static struct platform_driver ti_sci_clk_driver = {
695 .probe = ti_sci_clk_probe,
696 .remove = ti_sci_clk_remove,
697 .driver = {
698 .name = "ti-sci-clk",
699 .of_match_table = of_match_ptr(ti_sci_clk_of_match),
700 },
701 };
702 module_platform_driver(ti_sci_clk_driver);
703
704 MODULE_LICENSE("GPL v2");
705 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
706 MODULE_AUTHOR("Tero Kristo");
707 MODULE_ALIAS("platform:ti-sci-clk");
708