Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Broadcom |
| 3 | * Copyright (C) 2012 Stephen Warren |
| 4 | * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/clk-provider.h> |
| 20 | #include <linux/kernel.h> |
Paul Gortmaker | 80c6397 | 2016-07-04 17:12:15 -0400 | [diff] [blame] | 21 | #include <linux/init.h> |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 22 | #include <linux/of.h> |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 23 | #include <linux/of_device.h> |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/stringify.h> |
| 26 | #include <linux/regmap.h> |
| 27 | #include <linux/mfd/syscon.h> |
| 28 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 29 | #include <dt-bindings/clock/oxsemi,ox810se.h> |
Neil Armstrong | 6df4393 | 2016-10-05 17:07:51 +0200 | [diff] [blame] | 30 | #include <dt-bindings/clock/oxsemi,ox820.h> |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 31 | |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 32 | /* Standard regmap gate clocks */ |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 33 | struct clk_oxnas_gate { |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 34 | struct clk_hw hw; |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 35 | unsigned int bit; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 36 | struct regmap *regmap; |
| 37 | }; |
| 38 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 39 | struct oxnas_stdclk_data { |
| 40 | struct clk_hw_onecell_data *onecell_data; |
| 41 | struct clk_oxnas_gate **gates; |
| 42 | unsigned int ngates; |
| 43 | struct clk_oxnas_pll **plls; |
| 44 | unsigned int nplls; |
| 45 | }; |
| 46 | |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 47 | /* Regmap offsets */ |
| 48 | #define CLK_STAT_REGOFFSET 0x24 |
| 49 | #define CLK_SET_REGOFFSET 0x2c |
| 50 | #define CLK_CLR_REGOFFSET 0x30 |
| 51 | |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 52 | static inline struct clk_oxnas_gate *to_clk_oxnas_gate(struct clk_hw *hw) |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 53 | { |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 54 | return container_of(hw, struct clk_oxnas_gate, hw); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 57 | static int oxnas_clk_gate_is_enabled(struct clk_hw *hw) |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 58 | { |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 59 | struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 60 | int ret; |
| 61 | unsigned int val; |
| 62 | |
| 63 | ret = regmap_read(std->regmap, CLK_STAT_REGOFFSET, &val); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
| 67 | return val & BIT(std->bit); |
| 68 | } |
| 69 | |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 70 | static int oxnas_clk_gate_enable(struct clk_hw *hw) |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 71 | { |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 72 | struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 73 | |
| 74 | regmap_write(std->regmap, CLK_SET_REGOFFSET, BIT(std->bit)); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 79 | static void oxnas_clk_gate_disable(struct clk_hw *hw) |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 80 | { |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 81 | struct clk_oxnas_gate *std = to_clk_oxnas_gate(hw); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 82 | |
| 83 | regmap_write(std->regmap, CLK_CLR_REGOFFSET, BIT(std->bit)); |
| 84 | } |
| 85 | |
Neil Armstrong | 1a2cfd0 | 2016-10-05 17:07:49 +0200 | [diff] [blame] | 86 | static const struct clk_ops oxnas_clk_gate_ops = { |
| 87 | .enable = oxnas_clk_gate_enable, |
| 88 | .disable = oxnas_clk_gate_disable, |
| 89 | .is_enabled = oxnas_clk_gate_is_enabled, |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 92 | static const char *const osc_parents[] = { |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 93 | "oscillator", |
| 94 | }; |
| 95 | |
| 96 | static const char *const eth_parents[] = { |
| 97 | "gmacclk", |
| 98 | }; |
| 99 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 100 | #define OXNAS_GATE(_name, _bit, _parents) \ |
| 101 | struct clk_oxnas_gate _name = { \ |
| 102 | .bit = (_bit), \ |
| 103 | .hw.init = &(struct clk_init_data) { \ |
| 104 | .name = #_name, \ |
| 105 | .ops = &oxnas_clk_gate_ops, \ |
| 106 | .parent_names = _parents, \ |
| 107 | .num_parents = ARRAY_SIZE(_parents), \ |
| 108 | .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED), \ |
| 109 | }, \ |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 112 | static OXNAS_GATE(ox810se_leon, 0, osc_parents); |
| 113 | static OXNAS_GATE(ox810se_dma_sgdma, 1, osc_parents); |
| 114 | static OXNAS_GATE(ox810se_cipher, 2, osc_parents); |
| 115 | static OXNAS_GATE(ox810se_sata, 4, osc_parents); |
| 116 | static OXNAS_GATE(ox810se_audio, 5, osc_parents); |
| 117 | static OXNAS_GATE(ox810se_usbmph, 6, osc_parents); |
| 118 | static OXNAS_GATE(ox810se_etha, 7, eth_parents); |
| 119 | static OXNAS_GATE(ox810se_pciea, 8, osc_parents); |
| 120 | static OXNAS_GATE(ox810se_nand, 9, osc_parents); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 121 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 122 | static struct clk_oxnas_gate *ox810se_gates[] = { |
| 123 | &ox810se_leon, |
| 124 | &ox810se_dma_sgdma, |
| 125 | &ox810se_cipher, |
| 126 | &ox810se_sata, |
| 127 | &ox810se_audio, |
| 128 | &ox810se_usbmph, |
| 129 | &ox810se_etha, |
| 130 | &ox810se_pciea, |
| 131 | &ox810se_nand, |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Neil Armstrong | 6df4393 | 2016-10-05 17:07:51 +0200 | [diff] [blame] | 134 | static OXNAS_GATE(ox820_leon, 0, osc_parents); |
| 135 | static OXNAS_GATE(ox820_dma_sgdma, 1, osc_parents); |
| 136 | static OXNAS_GATE(ox820_cipher, 2, osc_parents); |
| 137 | static OXNAS_GATE(ox820_sd, 3, osc_parents); |
| 138 | static OXNAS_GATE(ox820_sata, 4, osc_parents); |
| 139 | static OXNAS_GATE(ox820_audio, 5, osc_parents); |
| 140 | static OXNAS_GATE(ox820_usbmph, 6, osc_parents); |
| 141 | static OXNAS_GATE(ox820_etha, 7, eth_parents); |
| 142 | static OXNAS_GATE(ox820_pciea, 8, osc_parents); |
| 143 | static OXNAS_GATE(ox820_nand, 9, osc_parents); |
| 144 | static OXNAS_GATE(ox820_ethb, 10, eth_parents); |
| 145 | static OXNAS_GATE(ox820_pcieb, 11, osc_parents); |
| 146 | static OXNAS_GATE(ox820_ref600, 12, osc_parents); |
| 147 | static OXNAS_GATE(ox820_usbdev, 13, osc_parents); |
| 148 | |
| 149 | static struct clk_oxnas_gate *ox820_gates[] = { |
| 150 | &ox820_leon, |
| 151 | &ox820_dma_sgdma, |
| 152 | &ox820_cipher, |
| 153 | &ox820_sd, |
| 154 | &ox820_sata, |
| 155 | &ox820_audio, |
| 156 | &ox820_usbmph, |
| 157 | &ox820_etha, |
| 158 | &ox820_pciea, |
| 159 | &ox820_nand, |
| 160 | &ox820_etha, |
| 161 | &ox820_pciea, |
| 162 | &ox820_ref600, |
| 163 | &ox820_usbdev, |
| 164 | }; |
| 165 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 166 | static struct clk_hw_onecell_data ox810se_hw_onecell_data = { |
| 167 | .hws = { |
| 168 | [CLK_810_LEON] = &ox810se_leon.hw, |
| 169 | [CLK_810_DMA_SGDMA] = &ox810se_dma_sgdma.hw, |
| 170 | [CLK_810_CIPHER] = &ox810se_cipher.hw, |
| 171 | [CLK_810_SATA] = &ox810se_sata.hw, |
| 172 | [CLK_810_AUDIO] = &ox810se_audio.hw, |
| 173 | [CLK_810_USBMPH] = &ox810se_usbmph.hw, |
| 174 | [CLK_810_ETHA] = &ox810se_etha.hw, |
| 175 | [CLK_810_PCIEA] = &ox810se_pciea.hw, |
| 176 | [CLK_810_NAND] = &ox810se_nand.hw, |
| 177 | }, |
| 178 | .num = ARRAY_SIZE(ox810se_gates), |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 179 | }; |
| 180 | |
Neil Armstrong | 6df4393 | 2016-10-05 17:07:51 +0200 | [diff] [blame] | 181 | static struct clk_hw_onecell_data ox820_hw_onecell_data = { |
| 182 | .hws = { |
| 183 | [CLK_820_LEON] = &ox820_leon.hw, |
| 184 | [CLK_820_DMA_SGDMA] = &ox820_dma_sgdma.hw, |
| 185 | [CLK_820_CIPHER] = &ox820_cipher.hw, |
| 186 | [CLK_820_SD] = &ox820_sd.hw, |
| 187 | [CLK_820_SATA] = &ox820_sata.hw, |
| 188 | [CLK_820_AUDIO] = &ox820_audio.hw, |
| 189 | [CLK_820_USBMPH] = &ox820_usbmph.hw, |
| 190 | [CLK_820_ETHA] = &ox820_etha.hw, |
| 191 | [CLK_820_PCIEA] = &ox820_pciea.hw, |
| 192 | [CLK_820_NAND] = &ox820_nand.hw, |
| 193 | [CLK_820_ETHB] = &ox820_ethb.hw, |
| 194 | [CLK_820_PCIEB] = &ox820_pcieb.hw, |
| 195 | [CLK_820_REF600] = &ox820_ref600.hw, |
| 196 | [CLK_820_USBDEV] = &ox820_usbdev.hw, |
| 197 | }, |
| 198 | .num = ARRAY_SIZE(ox820_gates), |
| 199 | }; |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 200 | |
| 201 | static struct oxnas_stdclk_data ox810se_stdclk_data = { |
| 202 | .onecell_data = &ox810se_hw_onecell_data, |
| 203 | .gates = ox810se_gates, |
| 204 | .ngates = ARRAY_SIZE(ox810se_gates), |
| 205 | }; |
| 206 | |
Neil Armstrong | 6df4393 | 2016-10-05 17:07:51 +0200 | [diff] [blame] | 207 | static struct oxnas_stdclk_data ox820_stdclk_data = { |
| 208 | .onecell_data = &ox820_hw_onecell_data, |
| 209 | .gates = ox820_gates, |
| 210 | .ngates = ARRAY_SIZE(ox820_gates), |
| 211 | }; |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 212 | |
| 213 | static const struct of_device_id oxnas_stdclk_dt_ids[] = { |
| 214 | { .compatible = "oxsemi,ox810se-stdclk", &ox810se_stdclk_data }, |
Neil Armstrong | 6df4393 | 2016-10-05 17:07:51 +0200 | [diff] [blame] | 215 | { .compatible = "oxsemi,ox820-stdclk", &ox820_stdclk_data }, |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 216 | { } |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static int oxnas_stdclk_probe(struct platform_device *pdev) |
| 220 | { |
| 221 | struct device_node *np = pdev->dev.of_node; |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 222 | const struct oxnas_stdclk_data *data; |
| 223 | const struct of_device_id *id; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 224 | struct regmap *regmap; |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 225 | int ret; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 226 | int i; |
| 227 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 228 | id = of_match_device(oxnas_stdclk_dt_ids, &pdev->dev); |
| 229 | if (!id) |
| 230 | return -ENODEV; |
| 231 | data = id->data; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 232 | |
| 233 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
Wei Yongjun | a5e9b85 | 2016-06-17 17:24:23 +0000 | [diff] [blame] | 234 | if (IS_ERR(regmap)) { |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 235 | dev_err(&pdev->dev, "failed to have parent regmap\n"); |
Wei Yongjun | a5e9b85 | 2016-06-17 17:24:23 +0000 | [diff] [blame] | 236 | return PTR_ERR(regmap); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 239 | for (i = 0 ; i < data->ngates ; ++i) |
| 240 | data->gates[i]->regmap = regmap; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 241 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 242 | for (i = 0; i < data->onecell_data->num; i++) { |
| 243 | if (!data->onecell_data->hws[i]) |
| 244 | continue; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 245 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 246 | ret = devm_clk_hw_register(&pdev->dev, |
| 247 | data->onecell_data->hws[i]); |
| 248 | if (ret) |
| 249 | return ret; |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Neil Armstrong | 5a9e54a | 2016-10-05 17:07:50 +0200 | [diff] [blame] | 252 | return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, |
| 253 | data->onecell_data); |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 256 | static struct platform_driver oxnas_stdclk_driver = { |
| 257 | .probe = oxnas_stdclk_probe, |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 258 | .driver = { |
| 259 | .name = "oxnas-stdclk", |
Paul Gortmaker | 80c6397 | 2016-07-04 17:12:15 -0400 | [diff] [blame] | 260 | .suppress_bind_attrs = true, |
Neil Armstrong | 0bbd72b | 2016-04-18 12:01:35 +0200 | [diff] [blame] | 261 | .of_match_table = oxnas_stdclk_dt_ids, |
| 262 | }, |
| 263 | }; |
Paul Gortmaker | 80c6397 | 2016-07-04 17:12:15 -0400 | [diff] [blame] | 264 | builtin_platform_driver(oxnas_stdclk_driver); |