1 /*
2  * Copyright (c) 2023 Martin Kiepfer
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_MFD_AXP192_H_
8 #define ZEPHYR_INCLUDE_DRIVERS_MFD_AXP192_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <zephyr/device.h>
14 #include <zephyr/drivers/gpio.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * @brief GPIO function type. Only one function can be configured per GPIO.
22  */
23 enum axp192_gpio_func {
24 	AXP192_GPIO_FUNC_INPUT = BIT(0),
25 	AXP192_GPIO_FUNC_OUTPUT_OD = BIT(1),
26 	AXP192_GPIO_FUNC_OUTPUT_LOW = BIT(2),
27 	AXP192_GPIO_FUNC_LDO = BIT(3),
28 	AXP192_GPIO_FUNC_ADC = BIT(4),
29 	AXP192_GPIO_FUNC_PWM = BIT(5),
30 	AXP192_GPIO_FUNC_FLOAT = BIT(6),
31 	AXP192_GPIO_FUNC_CHARGE_CTL = BIT(7),
32 	AXP192_GPIO_FUNC_INVALID
33 };
34 
35 /**
36  * @brief Check if a given GPIO function value is valid.
37  */
38 #define AXP192_GPIO_FUNC_VALID(func) (func < AXP192_GPIO_FUNC_INVALID)
39 
40 /**
41  * @brief Maximum number of GPIOs supported by AXP192 PMIC.
42  */
43 #define AXP192_GPIO_MAX_NUM 5U
44 
45 /**
46  * @defgroup mdf_interface_axp192 MFD AXP192 interface
47  * @ingroup mfd_interfaces
48  * @{
49  */
50 
51 /**
52  * @brief Request a GPIO pin to be configured to a specific function. GPIO0..4
53  * of AXP192 feature various functions (see @ref axp192_gpio_func for details).
54  * A GPIO can only be used by one driver instance. Subsequential calls on the
55  * same GPIO will overwrite according function.
56  *
57  * @param dev axp192 mfd device
58  * @param client_dev client device the gpio is used in
59  * @param gpio GPIO to be configured (0..4)
60  * @param func Function to be configured (see @ref axp192_gpio_func for details)
61  * @retval 0 on success
62  * @retval -EINVAL if an invalid GPIO number is passed
63  * @retval -ENOTSUP if the requested function is not supported by the given
64  * @retval -errno in case of any bus error
65  */
66 int mfd_axp192_gpio_func_ctrl(const struct device *dev, const struct device *client_dev,
67 			      uint8_t gpio, enum axp192_gpio_func func);
68 
69 /**
70  * @brief Read out current configuration of a specific GPIO pin.
71  *
72  * @param dev axp192 mfd device
73  * @param gpio GPIO to read configuration from
74  * @param func Pointer to store current function configuration in.
75  * @return 0 on success
76  * @retval -EINVAL if an invalid GPIO number is passed
77  * @retval -errno in case of any bus error
78  */
79 int mfd_axp192_gpio_func_get(const struct device *dev, uint8_t gpio, enum axp192_gpio_func *func);
80 
81 /**
82  * @brief Enable pull-down on specified GPIO pin. AXP192 only supports
83  * pull-down on GPIO3..4. Pull-ups are not supprted.
84  *
85  * @param dev axp192 mfd device
86  * @param gpio GPIO to control pull-downs
87  * @param enable true to enable, false to disable pull-down
88  * @retval 0 on success
89  * @retval -EINVAL if an invalid argument is given (e.g. invalid GPIO number)
90  * @retval -ENOTSUP if pull-down is not supported by the givenn GPIO
91  * @retval -errno in case of any bus error
92  */
93 int mfd_axp192_gpio_pd_ctrl(const struct device *dev, uint8_t gpio, bool enable);
94 
95 /**
96  * @brief Read out the current pull-down configuration of a specific GPIO.
97  *
98  * @param dev axp192 mfd device
99  * @param gpio GPIO to control pull-downs
100  * @param enabled Pointer to current pull-down configuration (true: pull-down
101  * enabled/ false: pull-down disabled)
102  * @retval -EINVAL if an invalid argument is given (e.g. invalid GPIO number)
103  * @retval -ENOTSUP if pull-down is not supported by the givenn GPIO
104  * @retval -errno in case of any bus error
105  */
106 int mfd_axp192_gpio_pd_get(const struct device *dev, uint8_t gpio, bool *enabled);
107 
108 /**
109  * @brief Read GPIO port.
110  *
111  * @param dev axp192 mfd device
112  * @param value Pointer to port value
113  * @retval 0 on success
114  * @retval -errno in case of any bus error
115  */
116 int mfd_axp192_gpio_read_port(const struct device *dev, uint8_t *value);
117 
118 /**
119  * @brief Write GPIO port.
120  *
121  * @param dev axp192 mfd device
122  * @param value port value
123  * @param mask pin mask within the port
124  * @retval 0 on success
125  * @retval -errno in case of any bus error
126  */
127 int mfd_axp192_gpio_write_port(const struct device *dev, uint8_t value, uint8_t mask);
128 
129 /**
130  * @}
131  */
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* ZEPHYR_INCLUDE_DRIVERS_MFD_AXP192_H_ */
138