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 <zephyr/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  * @brief Structure for specifying the configuration of a DAC channel.
30  */
31 struct dac_channel_cfg {
32 	/** Channel identifier of the DAC that should be configured. */
33 	uint8_t channel_id;
34 	/** Desired resolution of the DAC (depends on device capabilities). */
35 	uint8_t resolution;
36 	/** Enable output buffer for this channel.
37 	 * This is relevant for instance if the output is directly connected to the load,
38 	 * without an amplifierin between. The actual details on this are hardware dependent.
39 	 */
40 	bool buffered;
41 };
42 
43 /**
44  * @cond INTERNAL_HIDDEN
45  *
46  * For internal use only, skip these in public documentation.
47  */
48 
49 /*
50  * Type definition of DAC API function for configuring a channel.
51  * See dac_channel_setup() for argument descriptions.
52  */
53 typedef int (*dac_api_channel_setup)(const struct device *dev,
54 				     const struct dac_channel_cfg *channel_cfg);
55 
56 /*
57  * Type definition of DAC API function for setting a write request.
58  * See dac_write_value() for argument descriptions.
59  */
60 typedef int (*dac_api_write_value)(const struct device *dev,
61 				    uint8_t channel, uint32_t value);
62 
63 /*
64  * DAC driver API
65  *
66  * This is the mandatory API any DAC driver needs to expose.
67  */
68 __subsystem struct dac_driver_api {
69 	dac_api_channel_setup channel_setup;
70 	dac_api_write_value   write_value;
71 };
72 
73 /**
74  * @endcond
75  */
76 
77 /**
78  * @brief Configure a DAC channel.
79  *
80  * It is required to call this function and configure each channel before it is
81  * selected for a write request.
82  *
83  * @param dev          Pointer to the device structure for the driver instance.
84  * @param channel_cfg  Channel configuration.
85  *
86  * @retval 0         On success.
87  * @retval -EINVAL   If a parameter with an invalid value has been provided.
88  * @retval -ENOTSUP  If the requested resolution is not supported.
89  */
90 __syscall int dac_channel_setup(const struct device *dev,
91 				const struct dac_channel_cfg *channel_cfg);
92 
z_impl_dac_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)93 static inline int z_impl_dac_channel_setup(const struct device *dev,
94 					   const struct dac_channel_cfg *channel_cfg)
95 {
96 	const struct dac_driver_api *api =
97 				(const struct dac_driver_api *)dev->api;
98 
99 	return api->channel_setup(dev, channel_cfg);
100 }
101 
102 /**
103  * @brief Write a single value to a DAC channel
104  *
105  * @param dev         Pointer to the device structure for the driver instance.
106  * @param channel     Number of the channel to be used.
107  * @param value       Data to be written to DAC output registers.
108  *
109  * @retval 0        On success.
110  * @retval -EINVAL  If a parameter with an invalid value has been provided.
111  */
112 __syscall int dac_write_value(const struct device *dev, uint8_t channel,
113 			      uint32_t value);
114 
z_impl_dac_write_value(const struct device * dev,uint8_t channel,uint32_t value)115 static inline int z_impl_dac_write_value(const struct device *dev,
116 						uint8_t channel, uint32_t value)
117 {
118 	const struct dac_driver_api *api =
119 				(const struct dac_driver_api *)dev->api;
120 
121 	return api->write_value(dev, channel, value);
122 }
123 
124 /**
125  * @}
126  */
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #include <syscalls/dac.h>
133 
134 #endif  /* ZEPHYR_INCLUDE_DRIVERS_DAC_H_ */
135