clk.h 6.1 KB

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