clk-inverter.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2015 Heiko Stuebner <heiko@sntech.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/io.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/kernel.h>
  19. #include "clk.h"
  20. struct rockchip_inv_clock {
  21. struct clk_hw hw;
  22. void __iomem *reg;
  23. int shift;
  24. int flags;
  25. spinlock_t *lock;
  26. };
  27. #define to_inv_clock(_hw) container_of(_hw, struct rockchip_inv_clock, hw)
  28. #define INVERTER_MASK 0x1
  29. static int rockchip_inv_get_phase(struct clk_hw *hw)
  30. {
  31. struct rockchip_inv_clock *inv_clock = to_inv_clock(hw);
  32. u32 val;
  33. val = readl(inv_clock->reg) >> inv_clock->shift;
  34. val &= INVERTER_MASK;
  35. return val ? 180 : 0;
  36. }
  37. static int rockchip_inv_set_phase(struct clk_hw *hw, int degrees)
  38. {
  39. struct rockchip_inv_clock *inv_clock = to_inv_clock(hw);
  40. u32 val;
  41. if (degrees % 180 == 0) {
  42. val = !!degrees;
  43. } else {
  44. pr_err("%s: unsupported phase %d for %s\n",
  45. __func__, degrees, clk_hw_get_name(hw));
  46. return -EINVAL;
  47. }
  48. if (inv_clock->flags & ROCKCHIP_INVERTER_HIWORD_MASK) {
  49. writel(HIWORD_UPDATE(val, INVERTER_MASK, inv_clock->shift),
  50. inv_clock->reg);
  51. } else {
  52. unsigned long flags;
  53. u32 reg;
  54. spin_lock_irqsave(inv_clock->lock, flags);
  55. reg = readl(inv_clock->reg);
  56. reg &= ~BIT(inv_clock->shift);
  57. reg |= val;
  58. writel(reg, inv_clock->reg);
  59. spin_unlock_irqrestore(inv_clock->lock, flags);
  60. }
  61. return 0;
  62. }
  63. static const struct clk_ops rockchip_inv_clk_ops = {
  64. .get_phase = rockchip_inv_get_phase,
  65. .set_phase = rockchip_inv_set_phase,
  66. };
  67. struct clk *rockchip_clk_register_inverter(const char *name,
  68. const char *const *parent_names, u8 num_parents,
  69. void __iomem *reg, int shift, int flags,
  70. spinlock_t *lock)
  71. {
  72. struct clk_init_data init;
  73. struct rockchip_inv_clock *inv_clock;
  74. struct clk *clk;
  75. inv_clock = kmalloc(sizeof(*inv_clock), GFP_KERNEL);
  76. if (!inv_clock)
  77. return NULL;
  78. init.name = name;
  79. init.num_parents = num_parents;
  80. init.flags = CLK_SET_RATE_PARENT;
  81. init.parent_names = parent_names;
  82. init.ops = &rockchip_inv_clk_ops;
  83. inv_clock->hw.init = &init;
  84. inv_clock->reg = reg;
  85. inv_clock->shift = shift;
  86. inv_clock->flags = flags;
  87. inv_clock->lock = lock;
  88. clk = clk_register(NULL, &inv_clock->hw);
  89. if (IS_ERR(clk))
  90. goto err_free;
  91. return clk;
  92. err_free:
  93. kfree(inv_clock);
  94. return NULL;
  95. }