Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk-provider.h> |
| 12 | #include <linux/clkdev.h> |
| 13 | #include <linux/clk/at91_pmc.h> |
| 14 | #include <linux/of.h> |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 15 | #include <linux/mfd/syscon.h> |
| 16 | #include <linux/regmap.h> |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 17 | |
| 18 | #include "pmc.h" |
| 19 | |
| 20 | #define USB_SOURCE_MAX 2 |
| 21 | |
| 22 | #define SAM9X5_USB_DIV_SHIFT 8 |
| 23 | #define SAM9X5_USB_MAX_DIV 0xf |
| 24 | |
| 25 | #define RM9200_USB_DIV_SHIFT 28 |
| 26 | #define RM9200_USB_DIV_TAB_SIZE 4 |
| 27 | |
| 28 | struct at91sam9x5_clk_usb { |
| 29 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 30 | struct regmap *regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | #define to_at91sam9x5_clk_usb(hw) \ |
| 34 | container_of(hw, struct at91sam9x5_clk_usb, hw) |
| 35 | |
| 36 | struct at91rm9200_clk_usb { |
| 37 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 38 | struct regmap *regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 39 | u32 divisors[4]; |
| 40 | }; |
| 41 | |
| 42 | #define to_at91rm9200_clk_usb(hw) \ |
| 43 | container_of(hw, struct at91rm9200_clk_usb, hw) |
| 44 | |
| 45 | static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw, |
| 46 | unsigned long parent_rate) |
| 47 | { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 48 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 49 | unsigned int usbr; |
| 50 | u8 usbdiv; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 51 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 52 | regmap_read(usb->regmap, AT91_PMC_USB, &usbr); |
| 53 | usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT; |
Boris Brezillon | 69daf75 | 2014-11-17 14:16:56 +0100 | [diff] [blame] | 54 | |
| 55 | return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1)); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 58 | static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw, |
| 59 | struct clk_rate_request *req) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 60 | { |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 61 | struct clk_hw *parent; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 62 | long best_rate = -EINVAL; |
| 63 | unsigned long tmp_rate; |
| 64 | int best_diff = -1; |
| 65 | int tmp_diff; |
| 66 | int i; |
Boris Brezillon | 69daf75 | 2014-11-17 14:16:56 +0100 | [diff] [blame] | 67 | |
Stephen Boyd | 497295a | 2015-06-25 16:53:23 -0700 | [diff] [blame] | 68 | for (i = 0; i < clk_hw_get_num_parents(hw); i++) { |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 69 | int div; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 70 | |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 71 | parent = clk_hw_get_parent_by_index(hw, i); |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 72 | if (!parent) |
| 73 | continue; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 74 | |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 75 | for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) { |
| 76 | unsigned long tmp_parent_rate; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 77 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 78 | tmp_parent_rate = req->rate * div; |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 79 | tmp_parent_rate = clk_hw_round_rate(parent, |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 80 | tmp_parent_rate); |
| 81 | tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div); |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 82 | if (tmp_rate < req->rate) |
| 83 | tmp_diff = req->rate - tmp_rate; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 84 | else |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 85 | tmp_diff = tmp_rate - req->rate; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 86 | |
| 87 | if (best_diff < 0 || best_diff > tmp_diff) { |
| 88 | best_rate = tmp_rate; |
| 89 | best_diff = tmp_diff; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 90 | req->best_parent_rate = tmp_parent_rate; |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 91 | req->best_parent_hw = parent; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 94 | if (!best_diff || tmp_rate < req->rate) |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (!best_diff) |
| 99 | break; |
| 100 | } |
| 101 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 102 | if (best_rate < 0) |
| 103 | return best_rate; |
| 104 | |
| 105 | req->rate = best_rate; |
| 106 | return 0; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index) |
| 110 | { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 111 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 112 | |
| 113 | if (index > 1) |
| 114 | return -EINVAL; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 115 | |
| 116 | regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, |
| 117 | index ? AT91_PMC_USBS : 0); |
| 118 | |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw) |
| 123 | { |
| 124 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 125 | unsigned int usbr; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 126 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 127 | regmap_read(usb->regmap, AT91_PMC_USB, &usbr); |
| 128 | |
| 129 | return usbr & AT91_PMC_USBS; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate, |
| 133 | unsigned long parent_rate) |
| 134 | { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 135 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris Brezillon | 69daf75 | 2014-11-17 14:16:56 +0100 | [diff] [blame] | 136 | unsigned long div; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 137 | |
Boris Brezillon | 69daf75 | 2014-11-17 14:16:56 +0100 | [diff] [blame] | 138 | if (!rate) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | div = DIV_ROUND_CLOSEST(parent_rate, rate); |
| 142 | if (div > SAM9X5_USB_MAX_DIV + 1 || !div) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 145 | regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV, |
| 146 | (div - 1) << SAM9X5_USB_DIV_SHIFT); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static const struct clk_ops at91sam9x5_usb_ops = { |
| 152 | .recalc_rate = at91sam9x5_clk_usb_recalc_rate, |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 153 | .determine_rate = at91sam9x5_clk_usb_determine_rate, |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 154 | .get_parent = at91sam9x5_clk_usb_get_parent, |
| 155 | .set_parent = at91sam9x5_clk_usb_set_parent, |
| 156 | .set_rate = at91sam9x5_clk_usb_set_rate, |
| 157 | }; |
| 158 | |
| 159 | static int at91sam9n12_clk_usb_enable(struct clk_hw *hw) |
| 160 | { |
| 161 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 162 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 163 | regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, |
| 164 | AT91_PMC_USBS); |
| 165 | |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void at91sam9n12_clk_usb_disable(struct clk_hw *hw) |
| 170 | { |
| 171 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 172 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 173 | regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw) |
| 177 | { |
| 178 | struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 179 | unsigned int usbr; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 180 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 181 | regmap_read(usb->regmap, AT91_PMC_USB, &usbr); |
| 182 | |
| 183 | return usbr & AT91_PMC_USBS; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static const struct clk_ops at91sam9n12_usb_ops = { |
| 187 | .enable = at91sam9n12_clk_usb_enable, |
| 188 | .disable = at91sam9n12_clk_usb_disable, |
| 189 | .is_enabled = at91sam9n12_clk_usb_is_enabled, |
| 190 | .recalc_rate = at91sam9x5_clk_usb_recalc_rate, |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 191 | .determine_rate = at91sam9x5_clk_usb_determine_rate, |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 192 | .set_rate = at91sam9x5_clk_usb_set_rate, |
| 193 | }; |
| 194 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 195 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 196 | at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name, |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 197 | const char **parent_names, u8 num_parents) |
| 198 | { |
| 199 | struct at91sam9x5_clk_usb *usb; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 200 | struct clk_hw *hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 201 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 202 | int ret; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 203 | |
| 204 | usb = kzalloc(sizeof(*usb), GFP_KERNEL); |
| 205 | if (!usb) |
| 206 | return ERR_PTR(-ENOMEM); |
| 207 | |
| 208 | init.name = name; |
| 209 | init.ops = &at91sam9x5_usb_ops; |
| 210 | init.parent_names = parent_names; |
| 211 | init.num_parents = num_parents; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 212 | init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE | |
| 213 | CLK_SET_RATE_PARENT; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 214 | |
| 215 | usb->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 216 | usb->regmap = regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 217 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 218 | hw = &usb->hw; |
| 219 | ret = clk_hw_register(NULL, &usb->hw); |
| 220 | if (ret) { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 221 | kfree(usb); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 222 | hw = ERR_PTR(ret); |
| 223 | } |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 224 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 225 | return hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 228 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 229 | at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name, |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 230 | const char *parent_name) |
| 231 | { |
| 232 | struct at91sam9x5_clk_usb *usb; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 233 | struct clk_hw *hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 234 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 235 | int ret; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 236 | |
| 237 | usb = kzalloc(sizeof(*usb), GFP_KERNEL); |
| 238 | if (!usb) |
| 239 | return ERR_PTR(-ENOMEM); |
| 240 | |
| 241 | init.name = name; |
| 242 | init.ops = &at91sam9n12_usb_ops; |
| 243 | init.parent_names = &parent_name; |
| 244 | init.num_parents = 1; |
Boris Brezillon | 4591243 | 2015-03-29 03:45:33 +0200 | [diff] [blame] | 245 | init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 246 | |
| 247 | usb->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 248 | usb->regmap = regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 249 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 250 | hw = &usb->hw; |
| 251 | ret = clk_hw_register(NULL, &usb->hw); |
| 252 | if (ret) { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 253 | kfree(usb); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 254 | hw = ERR_PTR(ret); |
| 255 | } |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 256 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 257 | return hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw, |
| 261 | unsigned long parent_rate) |
| 262 | { |
| 263 | struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 264 | unsigned int pllbr; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 265 | u8 usbdiv; |
| 266 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 267 | regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr); |
| 268 | |
| 269 | usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 270 | if (usb->divisors[usbdiv]) |
| 271 | return parent_rate / usb->divisors[usbdiv]; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate, |
| 277 | unsigned long *parent_rate) |
| 278 | { |
| 279 | struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw); |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 280 | struct clk_hw *parent = clk_hw_get_parent(hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 281 | unsigned long bestrate = 0; |
| 282 | int bestdiff = -1; |
| 283 | unsigned long tmprate; |
| 284 | int tmpdiff; |
| 285 | int i = 0; |
| 286 | |
Boris BREZILLON | 13a6073 | 2014-09-02 09:50:17 +0200 | [diff] [blame] | 287 | for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) { |
| 288 | unsigned long tmp_parent_rate; |
| 289 | |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 290 | if (!usb->divisors[i]) |
| 291 | continue; |
Boris BREZILLON | 13a6073 | 2014-09-02 09:50:17 +0200 | [diff] [blame] | 292 | |
| 293 | tmp_parent_rate = rate * usb->divisors[i]; |
Stephen Boyd | d097933 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 294 | tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate); |
Boris Brezillon | ff553ea | 2014-11-14 19:54:49 +0100 | [diff] [blame] | 295 | tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 296 | if (tmprate < rate) |
| 297 | tmpdiff = rate - tmprate; |
| 298 | else |
| 299 | tmpdiff = tmprate - rate; |
| 300 | |
| 301 | if (bestdiff < 0 || bestdiff > tmpdiff) { |
| 302 | bestrate = tmprate; |
| 303 | bestdiff = tmpdiff; |
Boris BREZILLON | 13a6073 | 2014-09-02 09:50:17 +0200 | [diff] [blame] | 304 | *parent_rate = tmp_parent_rate; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (!bestdiff) |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | return bestrate; |
| 312 | } |
| 313 | |
| 314 | static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate, |
| 315 | unsigned long parent_rate) |
| 316 | { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 317 | int i; |
| 318 | struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw); |
Boris BREZILLON | 16eeaec | 2014-09-02 09:50:18 +0200 | [diff] [blame] | 319 | unsigned long div; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 320 | |
Boris Brezillon | ff553ea | 2014-11-14 19:54:49 +0100 | [diff] [blame] | 321 | if (!rate) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 322 | return -EINVAL; |
Boris BREZILLON | 16eeaec | 2014-09-02 09:50:18 +0200 | [diff] [blame] | 323 | |
Boris Brezillon | ff553ea | 2014-11-14 19:54:49 +0100 | [diff] [blame] | 324 | div = DIV_ROUND_CLOSEST(parent_rate, rate); |
Boris BREZILLON | 16eeaec | 2014-09-02 09:50:18 +0200 | [diff] [blame] | 325 | |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 326 | for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) { |
| 327 | if (usb->divisors[i] == div) { |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 328 | regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR, |
| 329 | AT91_PMC_USBDIV, |
| 330 | i << RM9200_USB_DIV_SHIFT); |
| 331 | |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | static const struct clk_ops at91rm9200_usb_ops = { |
| 340 | .recalc_rate = at91rm9200_clk_usb_recalc_rate, |
| 341 | .round_rate = at91rm9200_clk_usb_round_rate, |
| 342 | .set_rate = at91rm9200_clk_usb_set_rate, |
| 343 | }; |
| 344 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 345 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 346 | at91rm9200_clk_register_usb(struct regmap *regmap, const char *name, |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 347 | const char *parent_name, const u32 *divisors) |
| 348 | { |
| 349 | struct at91rm9200_clk_usb *usb; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 350 | struct clk_hw *hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 351 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 352 | int ret; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 353 | |
| 354 | usb = kzalloc(sizeof(*usb), GFP_KERNEL); |
| 355 | if (!usb) |
| 356 | return ERR_PTR(-ENOMEM); |
| 357 | |
| 358 | init.name = name; |
| 359 | init.ops = &at91rm9200_usb_ops; |
| 360 | init.parent_names = &parent_name; |
| 361 | init.num_parents = 1; |
Boris BREZILLON | 13a6073 | 2014-09-02 09:50:17 +0200 | [diff] [blame] | 362 | init.flags = CLK_SET_RATE_PARENT; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 363 | |
| 364 | usb->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 365 | usb->regmap = regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 366 | memcpy(usb->divisors, divisors, sizeof(usb->divisors)); |
| 367 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 368 | hw = &usb->hw; |
| 369 | ret = clk_hw_register(NULL, &usb->hw); |
| 370 | if (ret) { |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 371 | kfree(usb); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 372 | hw = ERR_PTR(ret); |
| 373 | } |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 374 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 375 | return hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 378 | static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 379 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 380 | struct clk_hw *hw; |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 381 | unsigned int num_parents; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 382 | const char *parent_names[USB_SOURCE_MAX]; |
| 383 | const char *name = np->name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 384 | struct regmap *regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 385 | |
Geert Uytterhoeven | 51a43be | 2015-05-29 11:25:45 +0200 | [diff] [blame] | 386 | num_parents = of_clk_get_parent_count(np); |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 387 | if (num_parents == 0 || num_parents > USB_SOURCE_MAX) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 388 | return; |
| 389 | |
Dinh Nguyen | f0557fb | 2015-07-06 22:59:01 -0500 | [diff] [blame] | 390 | of_clk_parent_fill(np, parent_names, num_parents); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 391 | |
| 392 | of_property_read_string(np, "clock-output-names", &name); |
| 393 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 394 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 395 | if (IS_ERR(regmap)) |
| 396 | return; |
| 397 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 398 | hw = at91sam9x5_clk_register_usb(regmap, name, parent_names, |
| 399 | num_parents); |
| 400 | if (IS_ERR(hw)) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 401 | return; |
| 402 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 403 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 404 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 405 | CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb", |
| 406 | of_at91sam9x5_clk_usb_setup); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 407 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 408 | static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 409 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 410 | struct clk_hw *hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 411 | const char *parent_name; |
| 412 | const char *name = np->name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 413 | struct regmap *regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 414 | |
| 415 | parent_name = of_clk_get_parent_name(np, 0); |
| 416 | if (!parent_name) |
| 417 | return; |
| 418 | |
| 419 | of_property_read_string(np, "clock-output-names", &name); |
| 420 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 421 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 422 | if (IS_ERR(regmap)) |
| 423 | return; |
| 424 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 425 | hw = at91sam9n12_clk_register_usb(regmap, name, parent_name); |
| 426 | if (IS_ERR(hw)) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 427 | return; |
| 428 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 429 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 430 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 431 | CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb", |
| 432 | of_at91sam9n12_clk_usb_setup); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 433 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 434 | static void __init of_at91rm9200_clk_usb_setup(struct device_node *np) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 435 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 436 | struct clk_hw *hw; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 437 | const char *parent_name; |
| 438 | const char *name = np->name; |
| 439 | u32 divisors[4] = {0, 0, 0, 0}; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 440 | struct regmap *regmap; |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 441 | |
| 442 | parent_name = of_clk_get_parent_name(np, 0); |
| 443 | if (!parent_name) |
| 444 | return; |
| 445 | |
| 446 | of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4); |
| 447 | if (!divisors[0]) |
| 448 | return; |
| 449 | |
| 450 | of_property_read_string(np, "clock-output-names", &name); |
| 451 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 452 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 453 | if (IS_ERR(regmap)) |
| 454 | return; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 455 | hw = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors); |
| 456 | if (IS_ERR(hw)) |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 457 | return; |
| 458 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 459 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | c84a61d | 2013-10-17 18:55:41 +0200 | [diff] [blame] | 460 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 461 | CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb", |
| 462 | of_at91rm9200_clk_usb_setup); |