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 6U
44 
45 /**
46  * @defgroup mdf_interface_axp192 MFD AXP192 interface
47  *
48  * Pins of AXP192 support several different functions. The mfd interface offers
49  * an API to configure and control these different functions.
50  *
51  * The 6 GPIOS are mapped as follows:
52  *  [0]: GPIO0
53  *  [1]: GPIO1
54  *  [2]: GPIO2
55  *  [3]: GPIO3
56  *  [4]: GPIO4
57  *  [5]: EXTEN
58  *
59  * @ingroup mfd_interfaces
60  * @{
61  */
62 
63 /**
64  * @brief Request a GPIO pin to be configured to a specific function. GPIO0..5
65  * of AXP192 feature various functions (see @ref axp192_gpio_func for details).
66  * A GPIO can only be used by one driver instance. Subsequential calls on the
67  * same GPIO will overwrite according function.
68  *
69  * @param dev axp192 mfd device
70  * @param client_dev client device the gpio is used in
71  * @param gpio GPIO to be configured (0..5)
72  * @param func Function to be configured (see @ref axp192_gpio_func for details)
73  * @retval 0 on success
74  * @retval -EINVAL if an invalid GPIO number is passed
75  * @retval -ENOTSUP if the requested function is not supported by the given
76  * @retval -errno in case of any bus error
77  */
78 int mfd_axp192_gpio_func_ctrl(const struct device *dev, const struct device *client_dev,
79 			      uint8_t gpio, enum axp192_gpio_func func);
80 
81 /**
82  * @brief Read out current configuration of a specific GPIO pin.
83  *
84  * @param dev axp192 mfd device
85  * @param gpio GPIO to read configuration from
86  * @param func Pointer to store current function configuration in.
87  * @return 0 on success
88  * @retval -EINVAL if an invalid GPIO number is passed
89  * @retval -errno in case of any bus error
90  */
91 int mfd_axp192_gpio_func_get(const struct device *dev, uint8_t gpio, enum axp192_gpio_func *func);
92 
93 /**
94  * @brief Enable pull-down on specified GPIO pin. AXP192 only supports
95  * pull-down on GPIO3..5. Pull-ups are not supported.
96  *
97  * @param dev axp192 mfd device
98  * @param gpio GPIO to control pull-downs
99  * @param enable true to enable, false to disable pull-down
100  * @retval 0 on success
101  * @retval -EINVAL if an invalid argument is given (e.g. invalid GPIO number)
102  * @retval -ENOTSUP if pull-down is not supported by the givenn GPIO
103  * @retval -errno in case of any bus error
104  */
105 int mfd_axp192_gpio_pd_ctrl(const struct device *dev, uint8_t gpio, bool enable);
106 
107 /**
108  * @brief Read out the current pull-down configuration of a specific GPIO.
109  *
110  * @param dev axp192 mfd device
111  * @param gpio GPIO to control pull-downs
112  * @param enabled Pointer to current pull-down configuration (true: pull-down
113  * enabled/ false: pull-down disabled)
114  * @retval -EINVAL if an invalid argument is given (e.g. invalid GPIO number)
115  * @retval -ENOTSUP if pull-down is not supported by the givenn GPIO
116  * @retval -errno in case of any bus error
117  */
118 int mfd_axp192_gpio_pd_get(const struct device *dev, uint8_t gpio, bool *enabled);
119 
120 /**
121  * @brief Read GPIO port.
122  *
123  * @param dev axp192 mfd device
124  * @param value Pointer to port value
125  * @retval 0 on success
126  * @retval -errno in case of any bus error
127  */
128 int mfd_axp192_gpio_read_port(const struct device *dev, uint8_t *value);
129 
130 /**
131  * @brief Write GPIO port.
132  *
133  * @param dev axp192 mfd device
134  * @param value port value
135  * @param mask pin mask within the port
136  * @retval 0 on success
137  * @retval -errno in case of any bus error
138  */
139 int mfd_axp192_gpio_write_port(const struct device *dev, uint8_t value, uint8_t mask);
140 
141 /**
142  * @}
143  */
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif /* ZEPHYR_INCLUDE_DRIVERS_MFD_AXP192_H_ */
150