clk.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. * Copyright (c) 2016, NVIDIA CORPORATION.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef _CLK_H_
  9. #define _CLK_H_
  10. #include <linux/types.h>
  11. /**
  12. * A clock is a hardware signal that oscillates autonomously at a specific
  13. * frequency and duty cycle. Most hardware modules require one or more clock
  14. * signal to drive their operation. Clock signals are typically generated
  15. * externally to the HW module consuming them, by an entity this API calls a
  16. * clock provider. This API provides a standard means for drivers to enable and
  17. * disable clocks, and to set the rate at which they oscillate.
  18. *
  19. * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
  20. * often implement multiple separate clocks, since the hardware it manages
  21. * often has this capability. clock_uclass.h describes the interface which
  22. * clock providers must implement.
  23. *
  24. * Clock consumers/clients are the HW modules driven by the clock signals. This
  25. * header file describes the API used by drivers for those HW modules.
  26. */
  27. struct udevice;
  28. /**
  29. * struct clk - A handle to (allowing control of) a single clock.
  30. *
  31. * Clients provide storage for clock handles. The content of the structure is
  32. * managed solely by the clock API and clock drivers. A clock struct is
  33. * initialized by "get"ing the clock struct. The clock struct is passed to all
  34. * other clock APIs to identify which clock signal to operate upon.
  35. *
  36. * @dev: The device which implements the clock signal.
  37. * @id: The clock signal ID within the provider.
  38. *
  39. * Currently, the clock API assumes that a single integer ID is enough to
  40. * identify and configure any clock signal for any clock provider. If this
  41. * assumption becomes invalid in the future, the struct could be expanded to
  42. * either (a) add more fields to allow clock providers to store additional
  43. * information, or (b) replace the id field with an opaque pointer, which the
  44. * provider would dynamically allocated during its .of_xlate op, and process
  45. * during is .request op. This may require the addition of an extra op to clean
  46. * up the allocation.
  47. */
  48. struct clk {
  49. struct udevice *dev;
  50. /*
  51. * Written by of_xlate. We assume a single id is enough for now. In the
  52. * future, we might add more fields here.
  53. */
  54. unsigned long id;
  55. };
  56. #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
  57. struct phandle_2_cell;
  58. int clk_get_by_index_platdata(struct udevice *dev, int index,
  59. struct phandle_2_cell *cells, struct clk *clk);
  60. /**
  61. * clock_get_by_index - Get/request a clock by integer index.
  62. *
  63. * This looks up and requests a clock. The index is relative to the client
  64. * device; each device is assumed to have n clocks associated with it somehow,
  65. * and this function finds and requests one of them. The mapping of client
  66. * device clock indices to provider clocks may be via device-tree properties,
  67. * board-provided mapping tables, or some other mechanism.
  68. *
  69. * @dev: The client device.
  70. * @index: The index of the clock to request, within the client's list of
  71. * clocks.
  72. * @clock A pointer to a clock struct to initialize.
  73. * @return 0 if OK, or a negative error code.
  74. */
  75. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
  76. /**
  77. * clock_get_by_name - Get/request a clock by name.
  78. *
  79. * This looks up and requests a clock. The name is relative to the client
  80. * device; each device is assumed to have n clocks associated with it somehow,
  81. * and this function finds and requests one of them. The mapping of client
  82. * device clock names to provider clocks may be via device-tree properties,
  83. * board-provided mapping tables, or some other mechanism.
  84. *
  85. * @dev: The client device.
  86. * @name: The name of the clock to request, within the client's list of
  87. * clocks.
  88. * @clock: A pointer to a clock struct to initialize.
  89. * @return 0 if OK, or a negative error code.
  90. */
  91. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  92. #else
  93. static inline int clk_get_by_index(struct udevice *dev, int index,
  94. struct clk *clk)
  95. {
  96. return -ENOSYS;
  97. }
  98. static inline int clk_get_by_name(struct udevice *dev, const char *name,
  99. struct clk *clk)
  100. {
  101. return -ENOSYS;
  102. }
  103. #endif
  104. /**
  105. * clk_request - Request a clock by provider-specific ID.
  106. *
  107. * This requests a clock using a provider-specific ID. Generally, this function
  108. * should not be used, since clk_get_by_index/name() provide an interface that
  109. * better separates clients from intimate knowledge of clock providers.
  110. * However, this function may be useful in core SoC-specific code.
  111. *
  112. * @dev: The clock provider device.
  113. * @clock: A pointer to a clock struct to initialize. The caller must
  114. * have already initialized any field in this struct which the
  115. * clock provider uses to identify the clock.
  116. * @return 0 if OK, or a negative error code.
  117. */
  118. int clk_request(struct udevice *dev, struct clk *clk);
  119. /**
  120. * clock_free - Free a previously requested clock.
  121. *
  122. * @clock: A clock struct that was previously successfully requested by
  123. * clk_request/get_by_*().
  124. * @return 0 if OK, or a negative error code.
  125. */
  126. int clk_free(struct clk *clk);
  127. /**
  128. * clk_get_rate() - Get current clock rate.
  129. *
  130. * @clk: A clock struct that was previously successfully requested by
  131. * clk_request/get_by_*().
  132. * @return clock rate in Hz, or -ve error code.
  133. */
  134. ulong clk_get_rate(struct clk *clk);
  135. /**
  136. * clk_set_rate() - Set current clock rate.
  137. *
  138. * @clk: A clock struct that was previously successfully requested by
  139. * clk_request/get_by_*().
  140. * @rate: New clock rate in Hz.
  141. * @return new rate, or -ve error code.
  142. */
  143. ulong clk_set_rate(struct clk *clk, ulong rate);
  144. /**
  145. * clk_enable() - Enable (turn on) a clock.
  146. *
  147. * @clk: A clock struct that was previously successfully requested by
  148. * clk_request/get_by_*().
  149. * @return zero on success, or -ve error code.
  150. */
  151. int clk_enable(struct clk *clk);
  152. /**
  153. * clk_disable() - Disable (turn off) a clock.
  154. *
  155. * @clk: A clock struct that was previously successfully requested by
  156. * clk_request/get_by_*().
  157. * @return zero on success, or -ve error code.
  158. */
  159. int clk_disable(struct clk *clk);
  160. int soc_clk_dump(void);
  161. #endif