clk-sun6i-apb0-gates.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 APB0 clock gates driver
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/init.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #define SUN6I_APB0_GATES_MAX_SIZE 32
  16. struct gates_data {
  17. DECLARE_BITMAP(mask, SUN6I_APB0_GATES_MAX_SIZE);
  18. };
  19. static const struct gates_data sun6i_a31_apb0_gates __initconst = {
  20. .mask = {0x7F},
  21. };
  22. static const struct gates_data sun8i_a23_apb0_gates __initconst = {
  23. .mask = {0x5D},
  24. };
  25. static const struct of_device_id sun6i_a31_apb0_gates_clk_dt_ids[] = {
  26. { .compatible = "allwinner,sun6i-a31-apb0-gates-clk", .data = &sun6i_a31_apb0_gates },
  27. { .compatible = "allwinner,sun8i-a23-apb0-gates-clk", .data = &sun8i_a23_apb0_gates },
  28. { /* sentinel */ }
  29. };
  30. static int sun6i_a31_apb0_gates_clk_probe(struct platform_device *pdev)
  31. {
  32. struct device_node *np = pdev->dev.of_node;
  33. struct clk_onecell_data *clk_data;
  34. const struct of_device_id *device;
  35. const struct gates_data *data;
  36. const char *clk_parent;
  37. const char *clk_name;
  38. struct resource *r;
  39. void __iomem *reg;
  40. int ngates;
  41. int i;
  42. int j = 0;
  43. if (!np)
  44. return -ENODEV;
  45. device = of_match_device(sun6i_a31_apb0_gates_clk_dt_ids, &pdev->dev);
  46. if (!device)
  47. return -ENODEV;
  48. data = device->data;
  49. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  50. reg = devm_ioremap_resource(&pdev->dev, r);
  51. if (IS_ERR(reg))
  52. return PTR_ERR(reg);
  53. clk_parent = of_clk_get_parent_name(np, 0);
  54. if (!clk_parent)
  55. return -EINVAL;
  56. clk_data = devm_kzalloc(&pdev->dev, sizeof(struct clk_onecell_data),
  57. GFP_KERNEL);
  58. if (!clk_data)
  59. return -ENOMEM;
  60. /* Worst-case size approximation and memory allocation */
  61. ngates = find_last_bit(data->mask, SUN6I_APB0_GATES_MAX_SIZE);
  62. clk_data->clks = devm_kcalloc(&pdev->dev, (ngates + 1),
  63. sizeof(struct clk *), GFP_KERNEL);
  64. if (!clk_data->clks)
  65. return -ENOMEM;
  66. for_each_set_bit(i, data->mask, SUN6I_APB0_GATES_MAX_SIZE) {
  67. of_property_read_string_index(np, "clock-output-names",
  68. j, &clk_name);
  69. clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name,
  70. clk_parent, 0, reg, i,
  71. 0, NULL);
  72. WARN_ON(IS_ERR(clk_data->clks[i]));
  73. j++;
  74. }
  75. clk_data->clk_num = ngates + 1;
  76. return of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  77. }
  78. static struct platform_driver sun6i_a31_apb0_gates_clk_driver = {
  79. .driver = {
  80. .name = "sun6i-a31-apb0-gates-clk",
  81. .of_match_table = sun6i_a31_apb0_gates_clk_dt_ids,
  82. },
  83. .probe = sun6i_a31_apb0_gates_clk_probe,
  84. };
  85. builtin_platform_driver(sun6i_a31_apb0_gates_clk_driver);