Skip to content

Commit 8c73db6

Browse files
gmarullcarlescufi
authored andcommitted
drivers: regulator: npm1100: add initial support
While nPM1100 is to be operated in fixed configuration for some applications, it has some degree of configuration via GPIOs. For example, mode (auto/PWM) can be configured via MODE pin. VBUS current can also be adjusted using ISET pin, even though there is no API yet to limit the PMIC input current. This patch adds a new regulator class driver for nPM1100 PMIC, so that it can be used with the standard regulator API when needed. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent cc3faf2 commit 8c73db6

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

drivers/regulator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ zephyr_library()
66
zephyr_library_sources(regulator_common.c)
77
zephyr_library_sources_ifdef(CONFIG_REGULATOR_FAKE regulator_fake.c)
88
zephyr_library_sources_ifdef(CONFIG_REGULATOR_FIXED regulator_fixed.c)
9+
zephyr_library_sources_ifdef(CONFIG_REGULATOR_NPM1100 regulator_npm1100.c)
910
zephyr_library_sources_ifdef(CONFIG_REGULATOR_NPM6001 regulator_npm6001.c)
1011
zephyr_library_sources_ifdef(CONFIG_REGULATOR_PCA9420 regulator_pca9420.c)
1112
zephyr_library_sources_ifdef(CONFIG_REGULATOR_SHELL regulator_shell.c)

drivers/regulator/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ source "subsys/logging/Kconfig.template.log_config"
2323

2424
source "drivers/regulator/Kconfig.fake"
2525
source "drivers/regulator/Kconfig.fixed"
26+
source "drivers/regulator/Kconfig.npm1100"
2627
source "drivers/regulator/Kconfig.npm6001"
2728
source "drivers/regulator/Kconfig.pca9420"
2829

drivers/regulator/Kconfig.npm1100

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2023 Nordic Semiconductor ASA
2+
# SPDX -License-Identifier: Apache-2.0
3+
4+
config REGULATOR_NPM1100
5+
bool "nPM1100 PMIC regulator driver"
6+
default y
7+
depends on DT_HAS_NORDIC_NPM1100_ENABLED
8+
help
9+
Enable the Nordic nPM1100 PMIC regulator driver
10+
11+
config REGULATOR_NPM1100_INIT_PRIORITY
12+
int "nPM1100 regulator driver init priority"
13+
default KERNEL_INIT_PRIORITY_DEVICE
14+
depends on REGULATOR_NPM1100
15+
help
16+
Init priority for the Nordic nPM1100 regulator driver.

drivers/regulator/regulator_npm1100.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#define DT_DRV_COMPAT nordic_npm1100
7+
8+
#include <errno.h>
9+
10+
#include <zephyr/drivers/gpio.h>
11+
#include <zephyr/drivers/regulator.h>
12+
#include <zephyr/dt-bindings/regulator/npm1100.h>
13+
14+
struct regulator_npm1100_pconfig {
15+
struct gpio_dt_spec iset;
16+
};
17+
18+
struct regulator_npm1100_config {
19+
struct regulator_common_config common;
20+
struct gpio_dt_spec mode;
21+
};
22+
23+
struct regulator_npm1100_data {
24+
struct regulator_common_data data;
25+
};
26+
27+
static int regulator_npm1100_set_mode(const struct device *dev,
28+
regulator_mode_t mode)
29+
{
30+
const struct regulator_npm1100_config *config = dev->config;
31+
32+
if ((config->mode.port == NULL) || (mode > NPM1100_MODE_PWM)) {
33+
return -ENOTSUP;
34+
}
35+
36+
return gpio_pin_set_dt(&config->mode,
37+
mode == NPM1100_MODE_AUTO ? 0 : 1);
38+
}
39+
40+
static int regulator_npm1100_get_mode(const struct device *dev,
41+
regulator_mode_t *mode)
42+
{
43+
const struct regulator_npm1100_config *config = dev->config;
44+
int ret;
45+
46+
if (config->mode.port == NULL) {
47+
return -ENOTSUP;
48+
}
49+
50+
ret = gpio_pin_get_dt(&config->mode);
51+
if (ret < 0) {
52+
return ret;
53+
}
54+
55+
*mode = (ret == 0) ? NPM1100_MODE_AUTO : NPM1100_MODE_PWM;
56+
57+
return 0;
58+
}
59+
60+
static int regulator_npm1100_init(const struct device *dev)
61+
{
62+
const struct regulator_npm1100_config *config = dev->config;
63+
int ret;
64+
65+
if (config->mode.port != NULL) {
66+
if (!gpio_is_ready_dt(&config->mode)) {
67+
return -ENODEV;
68+
}
69+
70+
ret = gpio_pin_configure_dt(&config->mode,
71+
GPIO_INPUT | GPIO_OUTPUT_INACTIVE);
72+
if (ret < 0) {
73+
return ret;
74+
}
75+
}
76+
77+
regulator_common_data_init(dev);
78+
79+
return regulator_common_init(dev, true);
80+
}
81+
82+
static int regulator_npm1100_common_init(const struct device *dev)
83+
{
84+
const struct regulator_npm1100_pconfig *config = dev->config;
85+
86+
if (config->iset.port != NULL) {
87+
int ret;
88+
89+
if (!gpio_is_ready_dt(&config->iset)) {
90+
return -ENODEV;
91+
}
92+
93+
ret = gpio_pin_configure_dt(&config->iset,
94+
GPIO_OUTPUT_INACTIVE);
95+
if (ret < 0) {
96+
return ret;
97+
}
98+
}
99+
100+
return 0;
101+
}
102+
103+
static const struct regulator_driver_api api = {
104+
.set_mode = regulator_npm1100_set_mode,
105+
.get_mode = regulator_npm1100_get_mode,
106+
};
107+
108+
#define REGULATOR_NPM1100_DEFINE_BUCK(node_id, id) \
109+
static struct regulator_npm1100_data data_##id; \
110+
\
111+
static const struct regulator_npm1100_config config_##id = { \
112+
.common = REGULATOR_DT_COMMON_CONFIG_INIT(node_id), \
113+
.mode = GPIO_DT_SPEC_GET_OR(node_id, nordic_mode_gpios, {}), \
114+
}; \
115+
\
116+
DEVICE_DT_DEFINE(node_id, regulator_npm1100_init, NULL, &data_##id, \
117+
&config_##id, POST_KERNEL, \
118+
CONFIG_REGULATOR_NPM1100_INIT_PRIORITY, &api);
119+
120+
#define REGULATOR_NPM1100_DEFINE_BUCK_COND(inst) \
121+
COND_CODE_1(DT_NODE_EXISTS(DT_INST_CHILD(inst, buck)), \
122+
(REGULATOR_NPM1100_DEFINE_BUCK(DT_INST_CHILD(inst, buck), \
123+
buck##inst)), \
124+
())
125+
126+
#define REGULATOR_NPM1100_DEFINE_ALL(inst) \
127+
static const struct regulator_npm1100_pconfig config_##inst = { \
128+
.iset = GPIO_DT_SPEC_INST_GET_OR(inst, nordic_iset_gpios, {}), \
129+
}; \
130+
\
131+
DEVICE_DT_INST_DEFINE(inst, regulator_npm1100_common_init, NULL, NULL, \
132+
&config_##inst, POST_KERNEL, \
133+
CONFIG_REGULATOR_NPM1100_INIT_PRIORITY, NULL); \
134+
\
135+
REGULATOR_NPM1100_DEFINE_BUCK_COND(inst)
136+
137+
DT_INST_FOREACH_STATUS_OKAY(REGULATOR_NPM1100_DEFINE_ALL)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c), 2023 Nordic Semiconductor ASA
2+
# SPDX -License-Identifier: Apache-2.0
3+
4+
description: |
5+
Nordic nPM1100 PMIC
6+
7+
The PMIC has one buck converter. It needs to be defined as a child node,
8+
strictly following the BUCK node name. For example:
9+
10+
pmic {
11+
compatible = "nordic,npm1100";
12+
13+
BUCK {
14+
/* all properties for BUCK */
15+
};
16+
};
17+
18+
Note that only mode can be controlled (via GPIO pin MODE).
19+
20+
compatible: "nordic,npm1100"
21+
22+
include: base.yaml
23+
24+
properties:
25+
nordic,iset-gpios:
26+
type: phandle-array
27+
description: |
28+
ISET control pin.
29+
30+
child-binding:
31+
properties:
32+
nordic,mode-gpios:
33+
type: phandle-array
34+
description: |
35+
MODE control pin.
36+
37+
include:
38+
- name: regulator.yaml
39+
property-allowlist:
40+
- regulator-allowed-modes
41+
- regulator-initial-mode
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_REGULATOR_NPM1100_H_
8+
#define ZEPHYR_INCLUDE_DT_BINDINGS_REGULATOR_NPM1100_H_
9+
10+
/**
11+
* @defgroup regulator_npm1100 NPM1100 Devicetree helpers.
12+
* @ingroup regulator_interface
13+
* @{
14+
*/
15+
16+
/**
17+
* @name NPM1100 Regulator modes
18+
* @{
19+
*/
20+
/** Automatic mode */
21+
#define NPM1100_MODE_AUTO 0
22+
/** PWM mode */
23+
#define NPM1100_MODE_PWM 1
24+
/** @} */
25+
26+
/** @} */
27+
28+
#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_REGULATOR_NPM1100_H_*/

0 commit comments

Comments
 (0)