clk-sun6i-ar100.c 2.2 KB

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