Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 1 | /* clock_control.h - public clock controller driver API */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2015 Intel Corporation |
| 5 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 9 | #ifndef ZEPHYR_INCLUDE_CLOCK_CONTROL_H_ |
| 10 | #define ZEPHYR_INCLUDE_CLOCK_CONTROL_H_ |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 11 | |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 12 | #include <zephyr/types.h> |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <device.h> |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 15 | #include <misc/__assert.h> |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
| 21 | /* Clock control API */ |
| 22 | |
| 23 | /* Used to select all subsystem of a clock controller */ |
| 24 | #define CLOCK_CONTROL_SUBSYS_ALL NULL |
| 25 | |
| 26 | /** |
| 27 | * clock_control_subsys_t is a type to identify a clock controller sub-system. |
| 28 | * Such data pointed is opaque and relevant only to the clock controller |
| 29 | * driver instance being used. |
| 30 | */ |
| 31 | typedef void *clock_control_subsys_t; |
| 32 | |
| 33 | typedef int (*clock_control)(struct device *dev, clock_control_subsys_t sys); |
| 34 | |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 35 | typedef int (*clock_control_get)(struct device *dev, |
| 36 | clock_control_subsys_t sys, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 37 | u32_t *rate); |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 38 | |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 39 | struct clock_control_driver_api { |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 40 | clock_control on; |
| 41 | clock_control off; |
| 42 | clock_control_get get_rate; |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * @brief Enable the clock of a sub-system controlled by the device |
| 47 | * @param dev Pointer to the device structure for the clock controller driver |
| 48 | * instance |
| 49 | * @param sys A pointer to an opaque data representing the sub-system |
| 50 | */ |
| 51 | static inline int clock_control_on(struct device *dev, |
| 52 | clock_control_subsys_t sys) |
| 53 | { |
Marcus Shawcroft | 4b93e14 | 2016-10-22 09:59:20 +0100 | [diff] [blame] | 54 | const struct clock_control_driver_api *api = dev->driver_api; |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 55 | |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 56 | return api->on(dev, sys); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @brief Disable the clock of a sub-system controlled by the device |
| 61 | * @param dev Pointer to the device structure for the clock controller driver |
| 62 | * instance |
| 63 | * @param sys A pointer to an opaque data representing the sub-system |
| 64 | */ |
| 65 | static inline int clock_control_off(struct device *dev, |
| 66 | clock_control_subsys_t sys) |
| 67 | { |
Marcus Shawcroft | 4b93e14 | 2016-10-22 09:59:20 +0100 | [diff] [blame] | 68 | const struct clock_control_driver_api *api = dev->driver_api; |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 69 | |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 70 | return api->off(dev, sys); |
| 71 | } |
| 72 | |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 73 | /** |
| 74 | * @brief Obtain the clock rate of given sub-system |
| 75 | * @param dev Pointer to the device structure for the clock controller driver |
| 76 | * instance |
| 77 | * @param sys A pointer to an opaque data representing the sub-system |
| 78 | * @param[out] rate Subsystem clock rate |
| 79 | */ |
| 80 | static inline int clock_control_get_rate(struct device *dev, |
| 81 | clock_control_subsys_t sys, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 82 | u32_t *rate) |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 83 | { |
Marcus Shawcroft | 4b93e14 | 2016-10-22 09:59:20 +0100 | [diff] [blame] | 84 | const struct clock_control_driver_api *api = dev->driver_api; |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 85 | |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 86 | __ASSERT(api->get_rate != NULL, "%s not implemented for device %s", |
Maciek Borzecki | 14482f0 | 2016-03-05 15:08:51 +0100 | [diff] [blame] | 87 | __func__, dev->config->name); |
| 88 | |
| 89 | return api->get_rate(dev, sys, rate); |
| 90 | } |
| 91 | |
Tomasz Bursztyka | 714dd97 | 2015-11-21 21:16:01 -0500 | [diff] [blame] | 92 | #ifdef __cplusplus |
| 93 | } |
| 94 | #endif |
| 95 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 96 | #endif /* ZEPHYR_INCLUDE_CLOCK_CONTROL_H_ */ |