xref: /openbmc/linux/drivers/clk/keystone/sci-clk.c (revision 8e48b33f)
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 	u8 clk_id;
62 	u8 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  */
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  */
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  */
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 					&current_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  */
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  */
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  */
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  */
221 static u8 sci_clk_get_parent(struct clk_hw *hw)
222 {
223 	struct sci_clk *clk = to_sci_clk(hw);
224 	u8 parent_id;
225 	int ret;
226 
227 	ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
228 					     clk->clk_id, &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 	return parent_id - clk->clk_id - 1;
237 }
238 
239 /**
240  * sci_clk_set_parent - Set the parent of a TI SCI clock
241  * @hw: clock to set parent for
242  * @index: new parent index for the clock
243  *
244  * Sets the parent of a TI SCI clock. Return TI SCI protocol status.
245  */
246 static int sci_clk_set_parent(struct clk_hw *hw, u8 index)
247 {
248 	struct sci_clk *clk = to_sci_clk(hw);
249 
250 	return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
251 					      clk->clk_id,
252 					      index + 1 + clk->clk_id);
253 }
254 
255 static const struct clk_ops sci_clk_ops = {
256 	.prepare = sci_clk_prepare,
257 	.unprepare = sci_clk_unprepare,
258 	.is_prepared = sci_clk_is_prepared,
259 	.recalc_rate = sci_clk_recalc_rate,
260 	.determine_rate = sci_clk_determine_rate,
261 	.set_rate = sci_clk_set_rate,
262 	.get_parent = sci_clk_get_parent,
263 	.set_parent = sci_clk_set_parent,
264 };
265 
266 /**
267  * _sci_clk_get - Gets a handle for an SCI clock
268  * @provider: Handle to SCI clock provider
269  * @sci_clk: Handle to the SCI clock to populate
270  *
271  * Gets a handle to an existing TI SCI hw clock, or builds a new clock
272  * entry and registers it with the common clock framework. Called from
273  * the common clock framework, when a corresponding of_clk_get call is
274  * executed, or recursively from itself when parsing parent clocks.
275  * Returns 0 on success, negative error code on failure.
276  */
277 static int _sci_clk_build(struct sci_clk_provider *provider,
278 			  struct sci_clk *sci_clk)
279 {
280 	struct clk_init_data init = { NULL };
281 	char *name = NULL;
282 	char **parent_names = NULL;
283 	int i;
284 	int ret = 0;
285 
286 	name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id,
287 			 sci_clk->clk_id);
288 
289 	init.name = name;
290 
291 	/*
292 	 * From kernel point of view, we only care about a clocks parents,
293 	 * if it has more than 1 possible parent. In this case, it is going
294 	 * to have mux functionality. Otherwise it is going to act as a root
295 	 * clock.
296 	 */
297 	if (sci_clk->num_parents < 2)
298 		sci_clk->num_parents = 0;
299 
300 	if (sci_clk->num_parents) {
301 		parent_names = kcalloc(sci_clk->num_parents, sizeof(char *),
302 				       GFP_KERNEL);
303 
304 		if (!parent_names) {
305 			ret = -ENOMEM;
306 			goto err;
307 		}
308 
309 		for (i = 0; i < sci_clk->num_parents; i++) {
310 			char *parent_name;
311 
312 			parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d",
313 						sci_clk->dev_id,
314 						sci_clk->clk_id + 1 + i);
315 			if (!parent_name) {
316 				ret = -ENOMEM;
317 				goto err;
318 			}
319 			parent_names[i] = parent_name;
320 		}
321 		init.parent_names = (void *)parent_names;
322 	}
323 
324 	init.ops = &sci_clk_ops;
325 	init.num_parents = sci_clk->num_parents;
326 	sci_clk->hw.init = &init;
327 
328 	ret = devm_clk_hw_register(provider->dev, &sci_clk->hw);
329 	if (ret)
330 		dev_err(provider->dev, "failed clk register with %d\n", ret);
331 
332 err:
333 	if (parent_names) {
334 		for (i = 0; i < sci_clk->num_parents; i++)
335 			kfree(parent_names[i]);
336 
337 		kfree(parent_names);
338 	}
339 
340 	kfree(name);
341 
342 	return ret;
343 }
344 
345 static int _cmp_sci_clk(const void *a, const void *b)
346 {
347 	const struct sci_clk *ca = a;
348 	const struct sci_clk *cb = *(struct sci_clk **)b;
349 
350 	if (ca->dev_id == cb->dev_id && ca->clk_id == cb->clk_id)
351 		return 0;
352 	if (ca->dev_id > cb->dev_id ||
353 	    (ca->dev_id == cb->dev_id && ca->clk_id > cb->clk_id))
354 		return 1;
355 	return -1;
356 }
357 
358 /**
359  * sci_clk_get - Xlate function for getting clock handles
360  * @clkspec: device tree clock specifier
361  * @data: pointer to the clock provider
362  *
363  * Xlate function for retrieving clock TI SCI hw clock handles based on
364  * device tree clock specifier. Called from the common clock framework,
365  * when a corresponding of_clk_get call is executed. Returns a pointer
366  * to the TI SCI hw clock struct, or ERR_PTR value in failure.
367  */
368 static struct clk_hw *sci_clk_get(struct of_phandle_args *clkspec, void *data)
369 {
370 	struct sci_clk_provider *provider = data;
371 	struct sci_clk **clk;
372 	struct sci_clk key;
373 
374 	if (clkspec->args_count != 2)
375 		return ERR_PTR(-EINVAL);
376 
377 	key.dev_id = clkspec->args[0];
378 	key.clk_id = clkspec->args[1];
379 
380 	clk = bsearch(&key, provider->clocks, provider->num_clocks,
381 		      sizeof(clk), _cmp_sci_clk);
382 
383 	if (!clk)
384 		return ERR_PTR(-ENODEV);
385 
386 	return &(*clk)->hw;
387 }
388 
389 static int ti_sci_init_clocks(struct sci_clk_provider *p)
390 {
391 	int i;
392 	int ret;
393 
394 	for (i = 0; i < p->num_clocks; i++) {
395 		ret = _sci_clk_build(p, p->clocks[i]);
396 		if (ret)
397 			return ret;
398 	}
399 
400 	return 0;
401 }
402 
403 static const struct of_device_id ti_sci_clk_of_match[] = {
404 	{ .compatible = "ti,k2g-sci-clk" },
405 	{ /* Sentinel */ },
406 };
407 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
408 
409 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
410 static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
411 {
412 	int ret;
413 	int num_clks = 0;
414 	struct sci_clk **clks = NULL;
415 	struct sci_clk **tmp_clks;
416 	struct sci_clk *sci_clk;
417 	int max_clks = 0;
418 	int clk_id = 0;
419 	int dev_id = 0;
420 	u8 num_parents;
421 	int gap_size = 0;
422 	struct device *dev = provider->dev;
423 
424 	while (1) {
425 		ret = provider->ops->get_num_parents(provider->sci, dev_id,
426 						     clk_id, &num_parents);
427 		if (ret) {
428 			gap_size++;
429 			if (!clk_id) {
430 				if (gap_size >= 5)
431 					break;
432 				dev_id++;
433 			} else {
434 				if (gap_size >= 2) {
435 					dev_id++;
436 					clk_id = 0;
437 					gap_size = 0;
438 				} else {
439 					clk_id++;
440 				}
441 			}
442 			continue;
443 		}
444 
445 		gap_size = 0;
446 
447 		if (num_clks == max_clks) {
448 			tmp_clks = devm_kmalloc_array(dev, max_clks + 64,
449 						      sizeof(sci_clk),
450 						      GFP_KERNEL);
451 			memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
452 			if (max_clks)
453 				devm_kfree(dev, clks);
454 			max_clks += 64;
455 			clks = tmp_clks;
456 		}
457 
458 		sci_clk = devm_kzalloc(dev, sizeof(*sci_clk), GFP_KERNEL);
459 		if (!sci_clk)
460 			return -ENOMEM;
461 		sci_clk->dev_id = dev_id;
462 		sci_clk->clk_id = clk_id;
463 		sci_clk->provider = provider;
464 		sci_clk->num_parents = num_parents;
465 
466 		clks[num_clks] = sci_clk;
467 
468 		clk_id++;
469 		num_clks++;
470 	}
471 
472 	provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
473 					      GFP_KERNEL);
474 	if (!provider->clocks)
475 		return -ENOMEM;
476 
477 	memcpy(provider->clocks, clks, num_clks * sizeof(sci_clk));
478 
479 	provider->num_clocks = num_clks;
480 
481 	devm_kfree(dev, clks);
482 
483 	return 0;
484 }
485 
486 #else
487 
488 static int _cmp_sci_clk_list(void *priv, struct list_head *a,
489 			     struct list_head *b)
490 {
491 	struct sci_clk *ca = container_of(a, struct sci_clk, node);
492 	struct sci_clk *cb = container_of(b, struct sci_clk, node);
493 
494 	return _cmp_sci_clk(ca, &cb);
495 }
496 
497 static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider)
498 {
499 	struct device *dev = provider->dev;
500 	struct device_node *np = NULL;
501 	int ret;
502 	int index;
503 	struct of_phandle_args args;
504 	struct list_head clks;
505 	struct sci_clk *sci_clk, *prev;
506 	int num_clks = 0;
507 	int num_parents;
508 	int clk_id;
509 	const char * const clk_names[] = {
510 		"clocks", "assigned-clocks", "assigned-clock-parents", NULL
511 	};
512 	const char * const *clk_name;
513 
514 	INIT_LIST_HEAD(&clks);
515 
516 	clk_name = clk_names;
517 
518 	while (*clk_name) {
519 		np = of_find_node_with_property(np, *clk_name);
520 		if (!np) {
521 			clk_name++;
522 			break;
523 		}
524 
525 		if (!of_device_is_available(np))
526 			continue;
527 
528 		index = 0;
529 
530 		do {
531 			ret = of_parse_phandle_with_args(np, *clk_name,
532 							 "#clock-cells", index,
533 							 &args);
534 			if (ret)
535 				break;
536 
537 			if (args.args_count == 2 && args.np == dev->of_node) {
538 				sci_clk = devm_kzalloc(dev, sizeof(*sci_clk),
539 						       GFP_KERNEL);
540 				if (!sci_clk)
541 					return -ENOMEM;
542 
543 				sci_clk->dev_id = args.args[0];
544 				sci_clk->clk_id = args.args[1];
545 				sci_clk->provider = provider;
546 				provider->ops->get_num_parents(provider->sci,
547 							       sci_clk->dev_id,
548 							       sci_clk->clk_id,
549 							       &sci_clk->num_parents);
550 				list_add_tail(&sci_clk->node, &clks);
551 
552 				num_clks++;
553 
554 				num_parents = sci_clk->num_parents;
555 				if (num_parents == 1)
556 					num_parents = 0;
557 
558 				clk_id = args.args[1] + 1;
559 
560 				while (num_parents--) {
561 					sci_clk = devm_kzalloc(dev,
562 							       sizeof(*sci_clk),
563 							       GFP_KERNEL);
564 					if (!sci_clk)
565 						return -ENOMEM;
566 					sci_clk->dev_id = args.args[0];
567 					sci_clk->clk_id = clk_id++;
568 					sci_clk->provider = provider;
569 					list_add_tail(&sci_clk->node, &clks);
570 
571 					num_clks++;
572 				}
573 			}
574 
575 			index++;
576 		} while (args.np);
577 	}
578 
579 	list_sort(NULL, &clks, _cmp_sci_clk_list);
580 
581 	provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
582 					      GFP_KERNEL);
583 	if (!provider->clocks)
584 		return -ENOMEM;
585 
586 	num_clks = 0;
587 	prev = NULL;
588 
589 	list_for_each_entry(sci_clk, &clks, node) {
590 		if (prev && prev->dev_id == sci_clk->dev_id &&
591 		    prev->clk_id == sci_clk->clk_id)
592 			continue;
593 
594 		provider->clocks[num_clks++] = sci_clk;
595 		prev = sci_clk;
596 	}
597 
598 	provider->num_clocks = num_clks;
599 
600 	return 0;
601 }
602 #endif
603 
604 /**
605  * ti_sci_clk_probe - Probe function for the TI SCI clock driver
606  * @pdev: platform device pointer to be probed
607  *
608  * Probes the TI SCI clock device. Allocates a new clock provider
609  * and registers this to the common clock framework. Also applies
610  * any required flags to the identified clocks via clock lists
611  * supplied from DT. Returns 0 for success, negative error value
612  * for failure.
613  */
614 static int ti_sci_clk_probe(struct platform_device *pdev)
615 {
616 	struct device *dev = &pdev->dev;
617 	struct device_node *np = dev->of_node;
618 	struct sci_clk_provider *provider;
619 	const struct ti_sci_handle *handle;
620 	int ret;
621 
622 	handle = devm_ti_sci_get_handle(dev);
623 	if (IS_ERR(handle))
624 		return PTR_ERR(handle);
625 
626 	provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
627 	if (!provider)
628 		return -ENOMEM;
629 
630 	provider->sci = handle;
631 	provider->ops = &handle->ops.clk_ops;
632 	provider->dev = dev;
633 
634 #ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
635 	ret = ti_sci_scan_clocks_from_fw(provider);
636 	if (ret) {
637 		dev_err(dev, "scan clocks from FW failed: %d\n", ret);
638 		return ret;
639 	}
640 #else
641 	ret = ti_sci_scan_clocks_from_dt(provider);
642 	if (ret) {
643 		dev_err(dev, "scan clocks from DT failed: %d\n", ret);
644 		return ret;
645 	}
646 #endif
647 
648 	ret = ti_sci_init_clocks(provider);
649 	if (ret) {
650 		pr_err("ti-sci-init-clocks failed.\n");
651 		return ret;
652 	}
653 
654 	return of_clk_add_hw_provider(np, sci_clk_get, provider);
655 }
656 
657 /**
658  * ti_sci_clk_remove - Remove TI SCI clock device
659  * @pdev: platform device pointer for the device to be removed
660  *
661  * Removes the TI SCI device. Unregisters the clock provider registered
662  * via common clock framework. Any memory allocated for the device will
663  * be free'd silently via the devm framework. Returns 0 always.
664  */
665 static int ti_sci_clk_remove(struct platform_device *pdev)
666 {
667 	of_clk_del_provider(pdev->dev.of_node);
668 
669 	return 0;
670 }
671 
672 static struct platform_driver ti_sci_clk_driver = {
673 	.probe = ti_sci_clk_probe,
674 	.remove = ti_sci_clk_remove,
675 	.driver = {
676 		.name = "ti-sci-clk",
677 		.of_match_table = of_match_ptr(ti_sci_clk_of_match),
678 	},
679 };
680 module_platform_driver(ti_sci_clk_driver);
681 
682 MODULE_LICENSE("GPL v2");
683 MODULE_DESCRIPTION("TI System Control Interface(SCI) Clock driver");
684 MODULE_AUTHOR("Tero Kristo");
685 MODULE_ALIAS("platform:ti-sci-clk");
686