clk-slow.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * drivers/clk/at91/clk-slow.c
  3. *
  4. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/clk/at91_pmc.h>
  15. #include <linux/of.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/regmap.h>
  18. #include "pmc.h"
  19. struct clk_sam9260_slow {
  20. struct clk_hw hw;
  21. struct regmap *regmap;
  22. };
  23. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  24. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  25. {
  26. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  27. unsigned int status;
  28. regmap_read(slowck->regmap, AT91_PMC_SR, &status);
  29. return status & AT91_PMC_OSCSEL ? 1 : 0;
  30. }
  31. static const struct clk_ops sam9260_slow_ops = {
  32. .get_parent = clk_sam9260_slow_get_parent,
  33. };
  34. static struct clk_hw * __init
  35. at91_clk_register_sam9260_slow(struct regmap *regmap,
  36. const char *name,
  37. const char **parent_names,
  38. int num_parents)
  39. {
  40. struct clk_sam9260_slow *slowck;
  41. struct clk_hw *hw;
  42. struct clk_init_data init;
  43. int ret;
  44. if (!name)
  45. return ERR_PTR(-EINVAL);
  46. if (!parent_names || !num_parents)
  47. return ERR_PTR(-EINVAL);
  48. slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
  49. if (!slowck)
  50. return ERR_PTR(-ENOMEM);
  51. init.name = name;
  52. init.ops = &sam9260_slow_ops;
  53. init.parent_names = parent_names;
  54. init.num_parents = num_parents;
  55. init.flags = 0;
  56. slowck->hw.init = &init;
  57. slowck->regmap = regmap;
  58. hw = &slowck->hw;
  59. ret = clk_hw_register(NULL, &slowck->hw);
  60. if (ret) {
  61. kfree(slowck);
  62. hw = ERR_PTR(ret);
  63. }
  64. return hw;
  65. }
  66. static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  67. {
  68. struct clk_hw *hw;
  69. const char *parent_names[2];
  70. unsigned int num_parents;
  71. const char *name = np->name;
  72. struct regmap *regmap;
  73. num_parents = of_clk_get_parent_count(np);
  74. if (num_parents != 2)
  75. return;
  76. of_clk_parent_fill(np, parent_names, num_parents);
  77. regmap = syscon_node_to_regmap(of_get_parent(np));
  78. if (IS_ERR(regmap))
  79. return;
  80. of_property_read_string(np, "clock-output-names", &name);
  81. hw = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  82. num_parents);
  83. if (IS_ERR(hw))
  84. return;
  85. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  86. }
  87. CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  88. of_at91sam9260_clk_slow_setup);