clk-sun6i-ar100.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 Free Electrons
  4. *
  5. * Author: Boris BREZILLON <[email protected]>
  6. *
  7. * Allwinner A31 AR100 clock driver
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/init.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/spinlock.h>
  15. #include "clk-factors.h"
  16. /*
  17. * sun6i_get_ar100_factors - Calculates factors p, m for AR100
  18. *
  19. * AR100 rate is calculated as follows
  20. * rate = (parent_rate >> p) / (m + 1);
  21. */
  22. static void sun6i_get_ar100_factors(struct factors_request *req)
  23. {
  24. unsigned long div;
  25. int shift;
  26. /* clock only divides */
  27. if (req->rate > req->parent_rate)
  28. req->rate = req->parent_rate;
  29. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  30. if (div < 32)
  31. shift = 0;
  32. else if (div >> 1 < 32)
  33. shift = 1;
  34. else if (div >> 2 < 32)
  35. shift = 2;
  36. else
  37. shift = 3;
  38. div >>= shift;
  39. if (div > 32)
  40. div = 32;
  41. req->rate = (req->parent_rate >> shift) / div;
  42. req->m = div - 1;
  43. req->p = shift;
  44. }
  45. static const struct clk_factors_config sun6i_ar100_config = {
  46. .mwidth = 5,
  47. .mshift = 8,
  48. .pwidth = 2,
  49. .pshift = 4,
  50. };
  51. static const struct factors_data sun6i_ar100_data = {
  52. .mux = 16,
  53. .muxmask = GENMASK(1, 0),
  54. .table = &sun6i_ar100_config,
  55. .getter = sun6i_get_ar100_factors,
  56. };
  57. static DEFINE_SPINLOCK(sun6i_ar100_lock);
  58. static int sun6i_a31_ar100_clk_probe(struct platform_device *pdev)
  59. {
  60. struct device_node *np = pdev->dev.of_node;
  61. void __iomem *reg;
  62. struct clk *clk;
  63. reg = devm_platform_ioremap_resource(pdev, 0);
  64. if (IS_ERR(reg))
  65. return PTR_ERR(reg);
  66. clk = sunxi_factors_register(np, &sun6i_ar100_data, &sun6i_ar100_lock,
  67. reg);
  68. if (!clk)
  69. return -ENOMEM;
  70. platform_set_drvdata(pdev, clk);
  71. return 0;
  72. }
  73. static const struct of_device_id sun6i_a31_ar100_clk_dt_ids[] = {
  74. { .compatible = "allwinner,sun6i-a31-ar100-clk" },
  75. { /* sentinel */ }
  76. };
  77. static struct platform_driver sun6i_a31_ar100_clk_driver = {
  78. .driver = {
  79. .name = "sun6i-a31-ar100-clk",
  80. .of_match_table = sun6i_a31_ar100_clk_dt_ids,
  81. .suppress_bind_attrs = true,
  82. },
  83. .probe = sun6i_a31_ar100_clk_probe,
  84. };
  85. builtin_platform_driver(sun6i_a31_ar100_clk_driver);