Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: phy: samsung: Consolidate reference clock rate handling

This patch cleans up handling of reference clock rate in Samsung USB PHY
drivers. It is mostly a cosmetic change but improves error handling in
case of failing to get reference clock or invalid clock rate.

Signed-off-by: Tomasz Figa <t.figa@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

authored by

Tomasz Figa and committed by
Felipe Balbi
0aa823a2 87331b06

+86 -49
+67 -47
drivers/usb/phy/phy-samsung-usb.c
··· 162 162 } 163 163 EXPORT_SYMBOL_GPL(samsung_usbphy_set_type); 164 164 165 + int samsung_usbphy_rate_to_clksel_64xx(struct samsung_usbphy *sphy, 166 + unsigned long rate) 167 + { 168 + unsigned int clksel; 169 + 170 + switch (rate) { 171 + case 12 * MHZ: 172 + clksel = PHYCLK_CLKSEL_12M; 173 + break; 174 + case 24 * MHZ: 175 + clksel = PHYCLK_CLKSEL_24M; 176 + break; 177 + case 48 * MHZ: 178 + clksel = PHYCLK_CLKSEL_48M; 179 + break; 180 + default: 181 + dev_err(sphy->dev, 182 + "Invalid reference clock frequency: %lu\n", rate); 183 + return -EINVAL; 184 + } 185 + 186 + return clksel; 187 + } 188 + EXPORT_SYMBOL_GPL(samsung_usbphy_rate_to_clksel_64xx); 189 + 190 + int samsung_usbphy_rate_to_clksel_4x12(struct samsung_usbphy *sphy, 191 + unsigned long rate) 192 + { 193 + unsigned int clksel; 194 + 195 + switch (rate) { 196 + case 9600 * KHZ: 197 + clksel = FSEL_CLKSEL_9600K; 198 + break; 199 + case 10 * MHZ: 200 + clksel = FSEL_CLKSEL_10M; 201 + break; 202 + case 12 * MHZ: 203 + clksel = FSEL_CLKSEL_12M; 204 + break; 205 + case 19200 * KHZ: 206 + clksel = FSEL_CLKSEL_19200K; 207 + break; 208 + case 20 * MHZ: 209 + clksel = FSEL_CLKSEL_20M; 210 + break; 211 + case 24 * MHZ: 212 + clksel = FSEL_CLKSEL_24M; 213 + break; 214 + case 50 * MHZ: 215 + clksel = FSEL_CLKSEL_50M; 216 + break; 217 + default: 218 + dev_err(sphy->dev, 219 + "Invalid reference clock frequency: %lu\n", rate); 220 + return -EINVAL; 221 + } 222 + 223 + return clksel; 224 + } 225 + EXPORT_SYMBOL_GPL(samsung_usbphy_rate_to_clksel_4x12); 226 + 165 227 /* 166 228 * Returns reference clock frequency selection value 167 229 */ 168 230 int samsung_usbphy_get_refclk_freq(struct samsung_usbphy *sphy) 169 231 { 170 232 struct clk *ref_clk; 171 - int refclk_freq = 0; 233 + unsigned long rate; 234 + int refclk_freq; 172 235 173 236 /* 174 237 * In exynos5250 USB host and device PHY use ··· 246 183 return PTR_ERR(ref_clk); 247 184 } 248 185 249 - if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250) { 250 - /* set clock frequency for PLL */ 251 - switch (clk_get_rate(ref_clk)) { 252 - case 9600 * KHZ: 253 - refclk_freq = FSEL_CLKSEL_9600K; 254 - break; 255 - case 10 * MHZ: 256 - refclk_freq = FSEL_CLKSEL_10M; 257 - break; 258 - case 12 * MHZ: 259 - refclk_freq = FSEL_CLKSEL_12M; 260 - break; 261 - case 19200 * KHZ: 262 - refclk_freq = FSEL_CLKSEL_19200K; 263 - break; 264 - case 20 * MHZ: 265 - refclk_freq = FSEL_CLKSEL_20M; 266 - break; 267 - case 50 * MHZ: 268 - refclk_freq = FSEL_CLKSEL_50M; 269 - break; 270 - case 24 * MHZ: 271 - default: 272 - /* default reference clock */ 273 - refclk_freq = FSEL_CLKSEL_24M; 274 - break; 275 - } 276 - } else { 277 - switch (clk_get_rate(ref_clk)) { 278 - case 12 * MHZ: 279 - refclk_freq = PHYCLK_CLKSEL_12M; 280 - break; 281 - case 24 * MHZ: 282 - refclk_freq = PHYCLK_CLKSEL_24M; 283 - break; 284 - case 48 * MHZ: 285 - refclk_freq = PHYCLK_CLKSEL_48M; 286 - break; 287 - default: 288 - if (sphy->drv_data->cpu_type == TYPE_S3C64XX) 289 - refclk_freq = PHYCLK_CLKSEL_48M; 290 - else 291 - refclk_freq = PHYCLK_CLKSEL_24M; 292 - break; 293 - } 294 - } 186 + rate = clk_get_rate(ref_clk); 187 + refclk_freq = sphy->drv_data->rate_to_clksel(sphy, rate); 188 + 295 189 clk_put(ref_clk); 296 190 297 191 return refclk_freq;
+7
drivers/usb/phy/phy-samsung-usb.h
··· 244 244 TYPE_EXYNOS5250, 245 245 }; 246 246 247 + struct samsung_usbphy; 248 + 247 249 /* 248 250 * struct samsung_usbphy_drvdata - driver data for various SoC variants 249 251 * @cpu_type: machine identifier ··· 270 268 int hostphy_en_mask; 271 269 u32 devphy_reg_offset; 272 270 u32 hostphy_reg_offset; 271 + int (*rate_to_clksel)(struct samsung_usbphy *, unsigned long); 273 272 }; 274 273 275 274 /* ··· 328 325 extern int samsung_usbphy_set_type(struct usb_phy *phy, 329 326 enum samsung_usb_phy_type phy_type); 330 327 extern int samsung_usbphy_get_refclk_freq(struct samsung_usbphy *sphy); 328 + extern int samsung_usbphy_rate_to_clksel_64xx(struct samsung_usbphy *sphy, 329 + unsigned long rate); 330 + extern int samsung_usbphy_rate_to_clksel_4x12(struct samsung_usbphy *sphy, 331 + unsigned long rate);
+7 -1
drivers/usb/phy/phy-samsung-usb2.c
··· 408 408 sphy->phy.label = "samsung-usb2phy"; 409 409 sphy->phy.init = samsung_usb2phy_init; 410 410 sphy->phy.shutdown = samsung_usb2phy_shutdown; 411 - sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy); 411 + 412 + sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy); 413 + if (sphy->ref_clk_freq < 0) 414 + return -EINVAL; 412 415 413 416 sphy->phy.otg = otg; 414 417 sphy->phy.otg->phy = &sphy->phy; ··· 441 438 static const struct samsung_usbphy_drvdata usb2phy_s3c64xx = { 442 439 .cpu_type = TYPE_S3C64XX, 443 440 .devphy_en_mask = S3C64XX_USBPHY_ENABLE, 441 + .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx, 444 442 }; 445 443 446 444 static const struct samsung_usbphy_drvdata usb2phy_exynos4 = { 447 445 .cpu_type = TYPE_EXYNOS4210, 448 446 .devphy_en_mask = EXYNOS_USBPHY_ENABLE, 449 447 .hostphy_en_mask = EXYNOS_USBPHY_ENABLE, 448 + .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx, 450 449 }; 451 450 452 451 static struct samsung_usbphy_drvdata usb2phy_exynos5 = { 453 452 .cpu_type = TYPE_EXYNOS5250, 454 453 .hostphy_en_mask = EXYNOS_USBPHY_ENABLE, 455 454 .hostphy_reg_offset = EXYNOS_USBHOST_PHY_CTRL_OFFSET, 455 + .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12, 456 456 }; 457 457 458 458 #ifdef CONFIG_OF
+5 -1
drivers/usb/phy/phy-samsung-usb3.c
··· 274 274 sphy->phy.init = samsung_usb3phy_init; 275 275 sphy->phy.shutdown = samsung_usb3phy_shutdown; 276 276 sphy->drv_data = samsung_usbphy_get_driver_data(pdev); 277 - sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy); 277 + 278 + sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy); 279 + if (sphy->ref_clk_freq < 0) 280 + return -EINVAL; 278 281 279 282 spin_lock_init(&sphy->lock); 280 283 ··· 303 300 static struct samsung_usbphy_drvdata usb3phy_exynos5 = { 304 301 .cpu_type = TYPE_EXYNOS5250, 305 302 .devphy_en_mask = EXYNOS_USBPHY_ENABLE, 303 + .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12, 306 304 }; 307 305 308 306 #ifdef CONFIG_OF