Skip to content

Commit 0fcc649

Browse files
committed
drivers: mfd: it8801_altctrl: Add alternate controller for MFD
IT8801 support GPIO alternate function switching. Some GPIO pins can be switched as KSO or PWM function. Signed-off-by: Tim Lin <[email protected]>
1 parent e7a7a17 commit 0fcc649

File tree

9 files changed

+233
-0
lines changed

9 files changed

+233
-0
lines changed

drivers/mfd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ zephyr_library_sources_ifdef(CONFIG_NXP_LP_FLEXCOMM mfd_nxp_lp_flexcomm.c)
1717
zephyr_library_sources_ifdef(CONFIG_MFD_BD8LB600FS mfd_bd8lb600fs.c)
1818
zephyr_library_sources_ifdef(CONFIG_MFD_TLE9104 mfd_tle9104.c)
1919
zephyr_library_sources_ifdef(CONFIG_MFD_ITE_IT8801 mfd_ite_it8801.c)
20+
zephyr_library_sources_ifdef(CONFIG_MFD_ITE_IT8801_ALTCTRL mfd_it8801_altctrl.c)

drivers/mfd/Kconfig.it8801

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ config MFD_ITE_IT8801
99
help
1010
Enable the ITE IT8801 ioexpander multi-function device driver.
1111
This ioexpander provides a GPIO/PWM/Keyboard function via I2C bus.
12+
13+
if MFD_ITE_IT8801
14+
15+
config MFD_ITE_IT8801_ALTCTRL
16+
bool "ITE IT8801 MFD alternate controller"
17+
default y
18+
19+
endif # MFD_ITE_IT8801

drivers/mfd/mfd_it8801_altctrl.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
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)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2024 ITE Corporation. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: ITE IT8801 GPIO controller node
5+
6+
compatible: "ite,it8801-altctrl"
7+
8+
include: base.yaml
9+
10+
altctrl-cells:
11+
- pin
12+
- alt_func
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2024 ITE Corporation. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: ITE IT8801 alternate controller node
5+
6+
compatible: "ite,it8801-mfd-map"
7+
8+
child-binding:
9+
description: Child node to present the mapping of IT8801 altternate function
10+
properties:
11+
altctrls:
12+
type: phandle-array
13+
required: true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2024 ITE Corporation. All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
it8801-mfd {
9+
#address-cells = <1>;
10+
#size-cells = <1>;
11+
12+
it8801_gpio0: gpiocr@0a {
13+
compatible = "ite,it8801-altctrl";
14+
reg = <0x0a 8>;
15+
#altctrl-cells = <2>;
16+
};
17+
18+
it8801_gpio1: gpiocr@12 {
19+
compatible = "ite,it8801-altctrl";
20+
reg = <0x12 6>;
21+
#altctrl-cells = <2>;
22+
};
23+
24+
it8801_gpio2: gpiocr@1a {
25+
compatible = "ite,it8801-altctrl";
26+
reg = <0x1a 4>;
27+
#altctrl-cells = <2>;
28+
};
29+
};
30+
};

dts/riscv/ite/it8801-mfd-map.dtsi

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024 ITE Corporation. All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ite/it8801-mfd-gpiocr.dtsi>
8+
#include <zephyr/dt-bindings/mfd/mfd_it8801_altctrl.h>
9+
10+
11+
/ {
12+
/* GPIO pin mapping to alternate function */
13+
it8801-mfd-map {
14+
compatible = "ite,it8801-mfd-map";
15+
16+
/* PWM alternate function */
17+
pwm1_gp12_default: pwm1_gp12_default {
18+
altctrls = <&it8801_gpio1 2 IT8801_ALT_FUNC_2>;
19+
};
20+
pwm2_gp13_default: pwm2_gp13_default {
21+
altctrls = <&it8801_gpio1 3 IT8801_ALT_FUNC_2>;
22+
};
23+
pwm3_gp14_default: pwm3_gp14_default {
24+
altctrls = <&it8801_gpio1 4 IT8801_ALT_FUNC_2>;
25+
};
26+
pwm4_gp15_default: pwm4_gp15_default {
27+
altctrls = <&it8801_gpio1 5 IT8801_ALT_FUNC_2>;
28+
};
29+
pwm7_gp20_default: pwm7_gp20_default {
30+
altctrls = <&it8801_gpio2 0 IT8801_ALT_FUNC_2>;
31+
};
32+
pwm8_gp23_default: pwm8_gp23_default {
33+
altctrls = <&it8801_gpio2 3 IT8801_ALT_FUNC_3>;
34+
};
35+
pwm9_gp22_default: pwm9_gp22_default {
36+
altctrls = <&it8801_gpio2 2 IT8801_ALT_FUNC_3>;
37+
};
38+
39+
/* Keyboard alternate function */
40+
kso18_gp01_default: kso18_gp01_default {
41+
altctrls = <&it8801_gpio0 1 IT8801_ALT_FUNC_2>;
42+
};
43+
kso19_gp00_default: kso19_gp00_default {
44+
altctrls = <&it8801_gpio0 0 IT8801_ALT_FUNC_2>;
45+
};
46+
kso20_gp23_default: kso20_gp23_default {
47+
altctrls = <&it8801_gpio2 3 IT8801_ALT_FUNC_2>;
48+
};
49+
kso21_gp22_default: kso21_gp22_default {
50+
altctrls = <&it8801_gpio2 2 IT8801_ALT_FUNC_2>;
51+
};
52+
kso22_gp21_default: kso22_gp21_default {
53+
altctrls = <&it8801_gpio2 1 IT8801_ALT_FUNC_2>;
54+
};
55+
};
56+
};

include/zephyr/drivers/mfd/mfd_ite_it8801.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ static const struct it8801_vendor_id_t it8801_id_verify[] = {
8787
#define IT8801_PWMMCR_MCR_BREATHING 2
8888
#define IT8801_PWMMCR_MCR_ON 3
8989

90+
/*
91+
* For IT8801 MFD alternate function controller
92+
*/
93+
#define IT8801_DT_INST_MFDCTRL(inst, idx) DT_INST_PHANDLE_BY_IDX(inst, mfdctrl, idx)
94+
95+
#define IT8801_DT_INST_MFDCTRL_LEN(inst) DT_INST_PROP_LEN_OR(inst, mfdctrl, 0)
96+
97+
#define IT8801_DEV_MFD(idx, inst) \
98+
DEVICE_DT_GET(DT_PHANDLE(IT8801_DT_INST_MFDCTRL(inst, idx), altctrls))
99+
#define IT8801_DEV_MFD_PIN(idx, inst) DT_PHA(IT8801_DT_INST_MFDCTRL(inst, idx), altctrls, pin)
100+
#define IT8801_DEV_MFD_FUNC(idx, inst) DT_PHA(IT8801_DT_INST_MFDCTRL(inst, idx), altctrls, alt_func)
101+
102+
#define IT8801_DT_MFD_ITEMS_FUNC(idx, inst) \
103+
{ \
104+
.gpiocr = IT8801_DEV_MFD(idx, inst), \
105+
.pin = IT8801_DEV_MFD_PIN(idx, inst), \
106+
.alt_func = IT8801_DEV_MFD_FUNC(idx, inst), \
107+
}
108+
109+
#define IT8801_DT_MFD_ITEMS_LIST(inst) \
110+
{LISTIFY(IT8801_DT_INST_MFDCTRL_LEN(inst), \
111+
IT8801_DT_MFD_ITEMS_FUNC, (,), \
112+
inst) }
113+
114+
/*
115+
* Configure alternate function pin
116+
*/
117+
int mfd_it8801_configure_pins(const struct i2c_dt_spec *i2c_dev, const struct device *dev,
118+
uint8_t pin, uint8_t func);
119+
90120
/* Define the IT8801 MFD interrupt callback function handler */
91121
typedef void (*it8801_callback_handler_t)(const struct device *dev);
92122

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2024 ITE Technology Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_MFD_IT8801_ALTCTRL_H_
7+
#define ZEPHYR_INCLUDE_DT_BINDINGS_MFD_IT8801_ALTCTRL_H_
8+
9+
/**
10+
* @brief PIN alternate function.
11+
*/
12+
#define IT8801_ALT_FUNC_1 0U
13+
#define IT8801_ALT_FUNC_2 1U
14+
#define IT8801_ALT_FUNC_3 2U
15+
#define IT8801_ALT_DEFAULT 3U
16+
17+
#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_MFD_IT8801_ALTCTRL_H_ */

0 commit comments

Comments
 (0)