1 /*
2  * Copyright (c) 2020 Libre Solar Technologies GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief DAC public API header file.
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_DAC_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_DAC_H_
14 
15 #include <device.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /**
22  * @brief DAC driver APIs
23  * @defgroup dac_interface DAC driver APIs
24  * @ingroup io_interfaces
25  * @{
26  */
27 
28 /**
29  * @struct dac_channel_cfg
30  * @brief Structure for specifying the configuration of a DAC channel.
31  *
32  * @param channel_id Channel identifier of the DAC that should be configured.
33  * @param resolution Desired resolution of the DAC (depends on device
34  *                   capabilities).
35  */
36 struct dac_channel_cfg {
37 	uint8_t channel_id;
38 	uint8_t resolution;
39 };
40 
41 /**
42  * @cond INTERNAL_HIDDEN
43  *
44  * For internal use only, skip these in public documentation.
45  */
46 
47 /*
48  * Type definition of DAC API function for configuring a channel.
49  * See dac_channel_setup() for argument descriptions.
50  */
51 typedef int (*dac_api_channel_setup)(const struct device *dev,
52 				     const struct dac_channel_cfg *channel_cfg);
53 
54 /*
55  * Type definition of DAC API function for setting a write request.
56  * See dac_write_value() for argument descriptions.
57  */
58 typedef int (*dac_api_write_value)(const struct device *dev,
59 				    uint8_t channel, uint32_t value);
60 
61 /*
62  * DAC driver API
63  *
64  * This is the mandatory API any DAC driver needs to expose.
65  */
66 __subsystem struct dac_driver_api {
67 	dac_api_channel_setup channel_setup;
68 	dac_api_write_value   write_value;
69 };
70 
71 /**
72  * @endcond
73  */
74 
75 /**
76  * @brief Configure a DAC channel.
77  *
78  * It is required to call this function and configure each channel before it is
79  * selected for a write request.
80  *
81  * @param dev          Pointer to the device structure for the driver instance.
82  * @param channel_cfg  Channel configuration.
83  *
84  * @retval 0         On success.
85  * @retval -EINVAL   If a parameter with an invalid value has been provided.
86  * @retval -ENOTSUP  If the requested resolution is not supported.
87  */
88 __syscall int dac_channel_setup(const struct device *dev,
89 				const struct dac_channel_cfg *channel_cfg);
90 
z_impl_dac_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)91 static inline int z_impl_dac_channel_setup(const struct device *dev,
92 					   const struct dac_channel_cfg *channel_cfg)
93 {
94 	const struct dac_driver_api *api =
95 				(const struct dac_driver_api *)dev->api;
96 
97 	return api->channel_setup(dev, channel_cfg);
98 }
99 
100 /**
101  * @brief Write a single value to a DAC channel
102  *
103  * @param dev         Pointer to the device structure for the driver instance.
104  * @param channel     Number of the channel to be used.
105  * @param value       Data to be written to DAC output registers.
106  *
107  * @retval 0        On success.
108  * @retval -EINVAL  If a parameter with an invalid value has been provided.
109  */
110 __syscall int dac_write_value(const struct device *dev, uint8_t channel,
111 			      uint32_t value);
112 
z_impl_dac_write_value(const struct device * dev,uint8_t channel,uint32_t value)113 static inline int z_impl_dac_write_value(const struct device *dev,
114 						uint8_t channel, uint32_t value)
115 {
116 	const struct dac_driver_api *api =
117 				(const struct dac_driver_api *)dev->api;
118 
119 	return api->write_value(dev, channel, value);
120 }
121 
122 /**
123  * @}
124  */
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #include <syscalls/dac.h>
131 
132 #endif  /* ZEPHYR_INCLUDE_DRIVERS_DAC_H_ */
133