Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright (C) 2013 ARM Limited |
| 12 | */ |
| 13 | |
| 14 | #include <linux/amba/sp810.h> |
Stephen Boyd | 6d31e3b2 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/clk.h> |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 17 | #include <linux/clk-provider.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
| 21 | |
| 22 | #define to_clk_sp810_timerclken(_hw) \ |
| 23 | container_of(_hw, struct clk_sp810_timerclken, hw) |
| 24 | |
| 25 | struct clk_sp810; |
| 26 | |
| 27 | struct clk_sp810_timerclken { |
| 28 | struct clk_hw hw; |
| 29 | struct clk *clk; |
| 30 | struct clk_sp810 *sp810; |
| 31 | int channel; |
| 32 | }; |
| 33 | |
| 34 | struct clk_sp810 { |
| 35 | struct device_node *node; |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 36 | void __iomem *base; |
| 37 | spinlock_t lock; |
| 38 | struct clk_sp810_timerclken timerclken[4]; |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static u8 clk_sp810_timerclken_get_parent(struct clk_hw *hw) |
| 42 | { |
| 43 | struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw); |
| 44 | u32 val = readl(timerclken->sp810->base + SCCTRL); |
| 45 | |
| 46 | return !!(val & (1 << SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel))); |
| 47 | } |
| 48 | |
| 49 | static int clk_sp810_timerclken_set_parent(struct clk_hw *hw, u8 index) |
| 50 | { |
| 51 | struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw); |
| 52 | struct clk_sp810 *sp810 = timerclken->sp810; |
| 53 | u32 val, shift = SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel); |
| 54 | unsigned long flags = 0; |
| 55 | |
| 56 | if (WARN_ON(index > 1)) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | spin_lock_irqsave(&sp810->lock, flags); |
| 60 | |
| 61 | val = readl(sp810->base + SCCTRL); |
| 62 | val &= ~(1 << shift); |
| 63 | val |= index << shift; |
| 64 | writel(val, sp810->base + SCCTRL); |
| 65 | |
| 66 | spin_unlock_irqrestore(&sp810->lock, flags); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 71 | static const struct clk_ops clk_sp810_timerclken_ops = { |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 72 | .get_parent = clk_sp810_timerclken_get_parent, |
| 73 | .set_parent = clk_sp810_timerclken_set_parent, |
| 74 | }; |
| 75 | |
Sachin Kamat | 14b260c | 2013-10-08 16:47:44 +0530 | [diff] [blame] | 76 | static struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec, |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 77 | void *data) |
| 78 | { |
| 79 | struct clk_sp810 *sp810 = data; |
| 80 | |
Dan Carpenter | 3294bee | 2015-07-29 13:17:06 +0300 | [diff] [blame] | 81 | if (WARN_ON(clkspec->args_count != 1 || |
| 82 | clkspec->args[0] >= ARRAY_SIZE(sp810->timerclken))) |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 83 | return NULL; |
| 84 | |
| 85 | return sp810->timerclken[clkspec->args[0]].clk; |
| 86 | } |
| 87 | |
Stephen Boyd | 11bee5e | 2015-05-01 13:02:26 -0700 | [diff] [blame] | 88 | static void __init clk_sp810_of_setup(struct device_node *node) |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 89 | { |
| 90 | struct clk_sp810 *sp810 = kzalloc(sizeof(*sp810), GFP_KERNEL); |
| 91 | const char *parent_names[2]; |
Stephen Boyd | 62f4771 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 92 | int num = ARRAY_SIZE(parent_names); |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 93 | char name[12]; |
| 94 | struct clk_init_data init; |
| 95 | int i; |
Stephen Boyd | 62f4771 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 96 | bool deprecated; |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 97 | |
Sudip Mukherjee | 59fe66313 | 2015-11-20 14:22:28 +0530 | [diff] [blame^] | 98 | if (!sp810) |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 99 | return; |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 100 | |
Stephen Boyd | 62f4771 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 101 | if (of_clk_parent_fill(node, parent_names, num) != num) { |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 102 | pr_warn("Failed to obtain parent clocks for SP810!\n"); |
Sudip Mukherjee | 47c5ee3 | 2015-11-16 19:16:40 +0530 | [diff] [blame] | 103 | kfree(sp810); |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
| 107 | sp810->node = node; |
| 108 | sp810->base = of_iomap(node, 0); |
| 109 | spin_lock_init(&sp810->lock); |
| 110 | |
| 111 | init.name = name; |
| 112 | init.ops = &clk_sp810_timerclken_ops; |
| 113 | init.flags = CLK_IS_BASIC; |
| 114 | init.parent_names = parent_names; |
Stephen Boyd | 62f4771 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 115 | init.num_parents = num; |
| 116 | |
| 117 | deprecated = !of_find_property(node, "assigned-clock-parents", NULL); |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 118 | |
| 119 | for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) { |
| 120 | snprintf(name, ARRAY_SIZE(name), "timerclken%d", i); |
| 121 | |
| 122 | sp810->timerclken[i].sp810 = sp810; |
| 123 | sp810->timerclken[i].channel = i; |
| 124 | sp810->timerclken[i].hw.init = &init; |
| 125 | |
Stephen Boyd | 62f4771 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 126 | /* |
| 127 | * If DT isn't setting the parent, force it to be |
| 128 | * the 1 MHz clock without going through the framework. |
| 129 | * We do this before clk_register() so that it can determine |
| 130 | * the parent and setup the tree properly. |
| 131 | */ |
| 132 | if (deprecated) |
| 133 | init.ops->set_parent(&sp810->timerclken[i].hw, 1); |
| 134 | |
Pawel Moll | 6e973d2 | 2013-04-18 18:23:22 +0100 | [diff] [blame] | 135 | sp810->timerclken[i].clk = clk_register(NULL, |
| 136 | &sp810->timerclken[i].hw); |
| 137 | WARN_ON(IS_ERR(sp810->timerclken[i].clk)); |
| 138 | } |
| 139 | |
| 140 | of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810); |
| 141 | } |
| 142 | CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup); |