blob: 9d792d2865a05de4700e22a8e5408ef16a99355a [file] [log] [blame]
Tomasz Bursztyka714dd972015-11-21 21:16:01 -05001/* clock_control.h - public clock controller driver API */
2
3/*
4 * Copyright (c) 2015 Intel Corporation
5 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08006 * SPDX-License-Identifier: Apache-2.0
Tomasz Bursztyka714dd972015-11-21 21:16:01 -05007 */
8
Flavio Ceolin67ca1762018-09-14 10:43:44 -07009#ifndef ZEPHYR_INCLUDE_CLOCK_CONTROL_H_
10#define ZEPHYR_INCLUDE_CLOCK_CONTROL_H_
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050011
Kumar Gala78908162017-04-19 10:32:08 -050012#include <zephyr/types.h>
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050013#include <stddef.h>
14#include <device.h>
Maciek Borzecki14482f02016-03-05 15:08:51 +010015#include <misc/__assert.h>
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050016
17#ifdef __cplusplus
18extern "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 */
31typedef void *clock_control_subsys_t;
32
33typedef int (*clock_control)(struct device *dev, clock_control_subsys_t sys);
34
Maciek Borzecki14482f02016-03-05 15:08:51 +010035typedef int (*clock_control_get)(struct device *dev,
36 clock_control_subsys_t sys,
Kumar Galacc334c72017-04-21 10:55:34 -050037 u32_t *rate);
Maciek Borzecki14482f02016-03-05 15:08:51 +010038
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050039struct clock_control_driver_api {
Maciek Borzecki14482f02016-03-05 15:08:51 +010040 clock_control on;
41 clock_control off;
42 clock_control_get get_rate;
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050043};
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 */
51static inline int clock_control_on(struct device *dev,
52 clock_control_subsys_t sys)
53{
Marcus Shawcroft4b93e142016-10-22 09:59:20 +010054 const struct clock_control_driver_api *api = dev->driver_api;
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050055
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050056 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 */
65static inline int clock_control_off(struct device *dev,
66 clock_control_subsys_t sys)
67{
Marcus Shawcroft4b93e142016-10-22 09:59:20 +010068 const struct clock_control_driver_api *api = dev->driver_api;
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050069
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050070 return api->off(dev, sys);
71}
72
Maciek Borzecki14482f02016-03-05 15:08:51 +010073/**
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 */
80static inline int clock_control_get_rate(struct device *dev,
81 clock_control_subsys_t sys,
Kumar Galacc334c72017-04-21 10:55:34 -050082 u32_t *rate)
Maciek Borzecki14482f02016-03-05 15:08:51 +010083{
Marcus Shawcroft4b93e142016-10-22 09:59:20 +010084 const struct clock_control_driver_api *api = dev->driver_api;
Maciek Borzecki14482f02016-03-05 15:08:51 +010085
Flavio Ceolind8837c62018-09-18 12:40:54 -070086 __ASSERT(api->get_rate != NULL, "%s not implemented for device %s",
Maciek Borzecki14482f02016-03-05 15:08:51 +010087 __func__, dev->config->name);
88
89 return api->get_rate(dev, sys, rate);
90}
91
Tomasz Bursztyka714dd972015-11-21 21:16:01 -050092#ifdef __cplusplus
93}
94#endif
95
Flavio Ceolin67ca1762018-09-14 10:43:44 -070096#endif /* ZEPHYR_INCLUDE_CLOCK_CONTROL_H_ */