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