1 /*
2 * Copyright (c) 2024 ITE Corporation. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT ite_it8801_altctrl
8
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/mfd/mfd_ite_it8801.h>
11 #include <zephyr/dt-bindings/mfd/mfd_it8801_altctrl.h>
12
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(mfd_it8801_altctrl, LOG_LEVEL_ERR);
15
16 struct mfd_altfunc_config {
17 /* gpiocr register */
18 uint8_t reg_gpiocr;
19 };
20
mfd_it8801_configure_pins(const struct i2c_dt_spec * i2c_dev,const struct device * dev,uint8_t pin,uint8_t func)21 int mfd_it8801_configure_pins(const struct i2c_dt_spec *i2c_dev, const struct device *dev,
22 uint8_t pin, uint8_t func)
23 {
24 const struct mfd_altfunc_config *config = dev->config;
25 int ret;
26 uint8_t reg_gpiocr = config->reg_gpiocr + pin;
27 uint8_t alt_val;
28
29 switch (func) {
30 case IT8801_ALT_FUNC_1:
31 /* Func1: Default GPIO setting */
32 alt_val = IT8801_GPIOAFS_FUN1;
33 break;
34 case IT8801_ALT_FUNC_2:
35 /* Func2: KSO or PWM setting */
36 alt_val = IT8801_GPIOAFS_FUN2;
37 break;
38 case IT8801_ALT_FUNC_3:
39 /* Func3: PWM setting */
40 alt_val = IT8801_GPIOAFS_FUN3;
41 break;
42 case IT8801_ALT_DEFAULT:
43 alt_val = IT8801_GPIOAFS_FUN1;
44 break;
45 default:
46 LOG_ERR("This function is not supported.");
47 return -EINVAL;
48 }
49
50 /* Common settings for alternate function. */
51 ret = i2c_reg_update_byte_dt(i2c_dev, reg_gpiocr, GENMASK(7, 6), alt_val << 6);
52 if (ret != 0) {
53 LOG_ERR("Failed to update gpiocr (ret %d)", ret);
54 return ret;
55 }
56
57 return 0;
58 }
59
60 #define MFD_IT8801_ALTCTRL_INIT(inst) \
61 static const struct mfd_altfunc_config it8801_mfd_alt_cfg_##inst = { \
62 .reg_gpiocr = DT_INST_REG_ADDR(inst), \
63 }; \
64 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &it8801_mfd_alt_cfg_##inst, POST_KERNEL, \
65 CONFIG_MFD_INIT_PRIORITY, NULL);
66 DT_INST_FOREACH_STATUS_OKAY(MFD_IT8801_ALTCTRL_INIT)
67