Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * MOXA ART SoCs clock driver. |
| 3 | * |
| 4 | * Copyright (C) 2013 Jonas Jensen |
| 5 | * |
| 6 | * Jonas Jensen <jonas.jensen@gmail.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
| 9 | * License version 2. This program is licensed "as is" without any |
| 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
Stephen Boyd | 67bb540 | 2015-05-01 16:09:33 -0700 | [diff] [blame] | 13 | #include <linux/clk.h> |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 14 | #include <linux/clk-provider.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/of_address.h> |
| 17 | #include <linux/clkdev.h> |
| 18 | |
Stephen Boyd | 61ad23a | 2015-05-01 16:09:33 -0700 | [diff] [blame] | 19 | static void __init moxart_of_pll_clk_init(struct device_node *node) |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 20 | { |
Gustavo A. R. Silva | c8108cf | 2017-07-04 17:36:50 -0500 | [diff] [blame] | 21 | void __iomem *base; |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 22 | struct clk_hw *hw; |
| 23 | struct clk *ref_clk; |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 24 | unsigned int mul; |
| 25 | const char *name = node->name; |
| 26 | const char *parent_name; |
| 27 | |
| 28 | of_property_read_string(node, "clock-output-names", &name); |
| 29 | parent_name = of_clk_get_parent_name(node, 0); |
| 30 | |
| 31 | base = of_iomap(node, 0); |
| 32 | if (!base) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 33 | pr_err("%pOF: of_iomap failed\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
| 37 | mul = readl(base + 0x30) >> 3 & 0x3f; |
| 38 | iounmap(base); |
| 39 | |
| 40 | ref_clk = of_clk_get(node, 0); |
| 41 | if (IS_ERR(ref_clk)) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 42 | pr_err("%pOF: of_clk_get failed\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 46 | hw = clk_hw_register_fixed_factor(NULL, name, parent_name, 0, mul, 1); |
| 47 | if (IS_ERR(hw)) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 48 | pr_err("%pOF: failed to register clock\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 52 | clk_hw_register_clkdev(hw, NULL, name); |
| 53 | of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 54 | } |
| 55 | CLK_OF_DECLARE(moxart_pll_clock, "moxa,moxart-pll-clock", |
| 56 | moxart_of_pll_clk_init); |
| 57 | |
Stephen Boyd | 61ad23a | 2015-05-01 16:09:33 -0700 | [diff] [blame] | 58 | static void __init moxart_of_apb_clk_init(struct device_node *node) |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 59 | { |
Gustavo A. R. Silva | c8108cf | 2017-07-04 17:36:50 -0500 | [diff] [blame] | 60 | void __iomem *base; |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 61 | struct clk_hw *hw; |
| 62 | struct clk *pll_clk; |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 63 | unsigned int div, val; |
| 64 | unsigned int div_idx[] = { 2, 3, 4, 6, 8}; |
| 65 | const char *name = node->name; |
| 66 | const char *parent_name; |
| 67 | |
| 68 | of_property_read_string(node, "clock-output-names", &name); |
| 69 | parent_name = of_clk_get_parent_name(node, 0); |
| 70 | |
| 71 | base = of_iomap(node, 0); |
| 72 | if (!base) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 73 | pr_err("%pOF: of_iomap failed\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | val = readl(base + 0xc) >> 4 & 0x7; |
| 78 | iounmap(base); |
| 79 | |
| 80 | if (val > 4) |
| 81 | val = 0; |
| 82 | div = div_idx[val] * 2; |
| 83 | |
| 84 | pll_clk = of_clk_get(node, 0); |
| 85 | if (IS_ERR(pll_clk)) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 86 | pr_err("%pOF: of_clk_get failed\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 90 | hw = clk_hw_register_fixed_factor(NULL, name, parent_name, 0, 1, div); |
| 91 | if (IS_ERR(hw)) { |
Rob Herring | 1667393 | 2017-07-18 16:42:52 -0500 | [diff] [blame] | 92 | pr_err("%pOF: failed to register clock\n", node); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
Stephen Boyd | 5a962d2 | 2016-06-01 16:15:18 -0700 | [diff] [blame] | 96 | clk_hw_register_clkdev(hw, NULL, name); |
| 97 | of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw); |
Jonas Jensen | c7bb4fc | 2014-01-28 12:09:11 +0100 | [diff] [blame] | 98 | } |
| 99 | CLK_OF_DECLARE(moxart_apb_clock, "moxa,moxart-apb-clock", |
| 100 | moxart_of_apb_clk_init); |