clk-gate-exclusive.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2014 Freescale Semiconductor, Inc.
  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 version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/slab.h>
  12. #include "clk.h"
  13. /**
  14. * struct clk_gate_exclusive - i.MX specific gate clock which is mutually
  15. * exclusive with other gate clocks
  16. *
  17. * @gate: the parent class
  18. * @exclusive_mask: mask of gate bits which are mutually exclusive to this
  19. * gate clock
  20. *
  21. * The imx exclusive gate clock is a subclass of basic clk_gate
  22. * with an addtional mask to indicate which other gate bits in the same
  23. * register is mutually exclusive to this gate clock.
  24. */
  25. struct clk_gate_exclusive {
  26. struct clk_gate gate;
  27. u32 exclusive_mask;
  28. };
  29. static int clk_gate_exclusive_enable(struct clk_hw *hw)
  30. {
  31. struct clk_gate *gate = container_of(hw, struct clk_gate, hw);
  32. struct clk_gate_exclusive *exgate = container_of(gate,
  33. struct clk_gate_exclusive, gate);
  34. u32 val = readl(gate->reg);
  35. if (val & exgate->exclusive_mask)
  36. return -EBUSY;
  37. return clk_gate_ops.enable(hw);
  38. }
  39. static void clk_gate_exclusive_disable(struct clk_hw *hw)
  40. {
  41. clk_gate_ops.disable(hw);
  42. }
  43. static int clk_gate_exclusive_is_enabled(struct clk_hw *hw)
  44. {
  45. return clk_gate_ops.is_enabled(hw);
  46. }
  47. static const struct clk_ops clk_gate_exclusive_ops = {
  48. .enable = clk_gate_exclusive_enable,
  49. .disable = clk_gate_exclusive_disable,
  50. .is_enabled = clk_gate_exclusive_is_enabled,
  51. };
  52. struct clk *imx_clk_gate_exclusive(const char *name, const char *parent,
  53. void __iomem *reg, u8 shift, u32 exclusive_mask)
  54. {
  55. struct clk_gate_exclusive *exgate;
  56. struct clk_gate *gate;
  57. struct clk *clk;
  58. struct clk_init_data init;
  59. if (exclusive_mask == 0)
  60. return ERR_PTR(-EINVAL);
  61. exgate = kzalloc(sizeof(*exgate), GFP_KERNEL);
  62. if (!exgate)
  63. return ERR_PTR(-ENOMEM);
  64. gate = &exgate->gate;
  65. init.name = name;
  66. init.ops = &clk_gate_exclusive_ops;
  67. init.flags = CLK_SET_RATE_PARENT;
  68. init.parent_names = parent ? &parent : NULL;
  69. init.num_parents = parent ? 1 : 0;
  70. gate->reg = reg;
  71. gate->bit_idx = shift;
  72. gate->lock = &imx_ccm_lock;
  73. gate->hw.init = &init;
  74. exgate->exclusive_mask = exclusive_mask;
  75. clk = clk_register(NULL, &gate->hw);
  76. if (IS_ERR(clk))
  77. kfree(exgate);
  78. return clk;
  79. }