ap806-system-controller.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Marvell Armada AP806 System Controller
  3. *
  4. * Copyright (C) 2016 Marvell
  5. *
  6. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #define pr_fmt(fmt) "ap806-system-controller: " fmt
  13. #include <linux/clk-provider.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #define AP806_SAR_REG 0x400
  21. #define AP806_SAR_CLKFREQ_MODE_MASK 0x1f
  22. #define AP806_CLK_NUM 4
  23. static struct clk *ap806_clks[AP806_CLK_NUM];
  24. static struct clk_onecell_data ap806_clk_data = {
  25. .clks = ap806_clks,
  26. .clk_num = AP806_CLK_NUM,
  27. };
  28. static int ap806_syscon_clk_probe(struct platform_device *pdev)
  29. {
  30. unsigned int freq_mode, cpuclk_freq;
  31. const char *name, *fixedclk_name;
  32. struct device_node *np = pdev->dev.of_node;
  33. struct regmap *regmap;
  34. u32 reg;
  35. int ret;
  36. regmap = syscon_node_to_regmap(np);
  37. if (IS_ERR(regmap)) {
  38. dev_err(&pdev->dev, "cannot get regmap\n");
  39. return PTR_ERR(regmap);
  40. }
  41. ret = regmap_read(regmap, AP806_SAR_REG, &reg);
  42. if (ret) {
  43. dev_err(&pdev->dev, "cannot read from regmap\n");
  44. return ret;
  45. }
  46. freq_mode = reg & AP806_SAR_CLKFREQ_MODE_MASK;
  47. switch (freq_mode) {
  48. case 0x0 ... 0x5:
  49. cpuclk_freq = 2000;
  50. break;
  51. case 0x6 ... 0xB:
  52. cpuclk_freq = 1800;
  53. break;
  54. case 0xC ... 0x11:
  55. cpuclk_freq = 1600;
  56. break;
  57. case 0x12 ... 0x16:
  58. cpuclk_freq = 1400;
  59. break;
  60. case 0x17 ... 0x19:
  61. cpuclk_freq = 1300;
  62. break;
  63. default:
  64. dev_err(&pdev->dev, "invalid SAR value\n");
  65. return -EINVAL;
  66. }
  67. /* Convert to hertz */
  68. cpuclk_freq *= 1000 * 1000;
  69. /* CPU clocks depend on the Sample At Reset configuration */
  70. of_property_read_string_index(np, "clock-output-names",
  71. 0, &name);
  72. ap806_clks[0] = clk_register_fixed_rate(&pdev->dev, name, NULL,
  73. 0, cpuclk_freq);
  74. if (IS_ERR(ap806_clks[0])) {
  75. ret = PTR_ERR(ap806_clks[0]);
  76. goto fail0;
  77. }
  78. of_property_read_string_index(np, "clock-output-names",
  79. 1, &name);
  80. ap806_clks[1] = clk_register_fixed_rate(&pdev->dev, name, NULL, 0,
  81. cpuclk_freq);
  82. if (IS_ERR(ap806_clks[1])) {
  83. ret = PTR_ERR(ap806_clks[1]);
  84. goto fail1;
  85. }
  86. /* Fixed clock is always 1200 Mhz */
  87. of_property_read_string_index(np, "clock-output-names",
  88. 2, &fixedclk_name);
  89. ap806_clks[2] = clk_register_fixed_rate(&pdev->dev, fixedclk_name, NULL,
  90. 0, 1200 * 1000 * 1000);
  91. if (IS_ERR(ap806_clks[2])) {
  92. ret = PTR_ERR(ap806_clks[2]);
  93. goto fail2;
  94. }
  95. /* MSS Clock is fixed clock divided by 6 */
  96. of_property_read_string_index(np, "clock-output-names",
  97. 3, &name);
  98. ap806_clks[3] = clk_register_fixed_factor(NULL, name, fixedclk_name,
  99. 0, 1, 6);
  100. if (IS_ERR(ap806_clks[3])) {
  101. ret = PTR_ERR(ap806_clks[3]);
  102. goto fail3;
  103. }
  104. ret = of_clk_add_provider(np, of_clk_src_onecell_get, &ap806_clk_data);
  105. if (ret)
  106. goto fail_clk_add;
  107. return 0;
  108. fail_clk_add:
  109. clk_unregister_fixed_factor(ap806_clks[3]);
  110. fail3:
  111. clk_unregister_fixed_rate(ap806_clks[2]);
  112. fail2:
  113. clk_unregister_fixed_rate(ap806_clks[1]);
  114. fail1:
  115. clk_unregister_fixed_rate(ap806_clks[0]);
  116. fail0:
  117. return ret;
  118. }
  119. static int ap806_syscon_clk_remove(struct platform_device *pdev)
  120. {
  121. of_clk_del_provider(pdev->dev.of_node);
  122. clk_unregister_fixed_factor(ap806_clks[3]);
  123. clk_unregister_fixed_rate(ap806_clks[2]);
  124. clk_unregister_fixed_rate(ap806_clks[1]);
  125. clk_unregister_fixed_rate(ap806_clks[0]);
  126. return 0;
  127. }
  128. static const struct of_device_id ap806_syscon_of_match[] = {
  129. { .compatible = "marvell,ap806-system-controller", },
  130. { }
  131. };
  132. MODULE_DEVICE_TABLE(of, armada8k_pcie_of_match);
  133. static struct platform_driver ap806_syscon_driver = {
  134. .probe = ap806_syscon_clk_probe,
  135. .remove = ap806_syscon_clk_remove,
  136. .driver = {
  137. .name = "marvell-ap806-system-controller",
  138. .of_match_table = ap806_syscon_of_match,
  139. },
  140. };
  141. module_platform_driver(ap806_syscon_driver);
  142. MODULE_DESCRIPTION("Marvell AP806 System Controller driver");
  143. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  144. MODULE_LICENSE("GPL");