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