Skip to content

Commit 68589ca

Browse files
aasinclaircarlescufi
authored andcommitted
drivers: gpio: npm1300: Use MFD register functions
Local register read/write functions have been removed and replaced with calls to the new MFD functions. Signed-off-by: Andy Sinclair <[email protected]>
1 parent b31f604 commit 68589ca

File tree

3 files changed

+35
-71
lines changed

3 files changed

+35
-71
lines changed

drivers/gpio/Kconfig.npm1300

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config GPIO_NPM1300
66
default y
77
depends on DT_HAS_NORDIC_NPM1300_GPIO_ENABLED
88
select I2C
9+
select MFD
910
help
1011
Enable the nPM1300 GPIO driver.
1112

drivers/gpio/gpio_npm1300.c

+31-44
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99

1010
#include <zephyr/drivers/gpio.h>
1111
#include <zephyr/drivers/gpio/gpio_utils.h>
12-
#include <zephyr/drivers/i2c.h>
12+
#include <zephyr/drivers/mfd/npm1300.h>
1313
#include <zephyr/dt-bindings/gpio/nordic-npm1300-gpio.h>
1414
#include <zephyr/kernel.h>
1515
#include <zephyr/sys/util_macro.h>
16-
#include <zephyr/toolchain.h>
17-
#include <zephyr/sys/byteorder.h>
1816

1917
/* nPM1300 GPIO base address */
2018
#define NPM_GPIO_BASE 0x06U
@@ -44,36 +42,20 @@
4442

4543
struct gpio_npm1300_config {
4644
struct gpio_driver_config common;
47-
struct i2c_dt_spec bus;
45+
const struct device *mfd;
4846
};
4947

5048
struct gpio_npm1300_data {
5149
struct gpio_driver_data common;
5250
};
5351

54-
/* Write single register to specified address */
55-
static int reg_write(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data)
56-
{
57-
const struct gpio_npm1300_config *config = dev->config;
58-
uint8_t buff[] = {base, offset, data};
59-
60-
return i2c_write_dt(&config->bus, buff, sizeof(buff));
61-
}
62-
63-
static int reg_read(const struct device *dev, uint8_t base, uint8_t offset, uint8_t *data)
64-
{
65-
const struct gpio_npm1300_config *config = dev->config;
66-
uint8_t buff[] = {base, offset};
67-
68-
return i2c_write_read_dt(&config->bus, buff, sizeof(buff), data, 1U);
69-
}
70-
7152
static int gpio_npm1300_port_get_raw(const struct device *dev, uint32_t *value)
7253
{
54+
const struct gpio_npm1300_config *config = dev->config;
7355
int ret;
7456
uint8_t data;
7557

76-
ret = reg_read(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_STATUS, &data);
58+
ret = mfd_npm1300_reg_read(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_STATUS, &data);
7759

7860
if (ret < 0) {
7961
return ret;
@@ -87,16 +69,19 @@ static int gpio_npm1300_port_get_raw(const struct device *dev, uint32_t *value)
8769
static int gpio_npm1300_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask,
8870
gpio_port_value_t value)
8971
{
72+
const struct gpio_npm1300_config *config = dev->config;
9073
int ret = 0;
9174

9275
for (size_t idx = 0; idx < NPM1300_GPIO_PINS; idx++) {
9376
if ((mask & BIT(idx)) != 0U) {
9477
if ((value & BIT(idx)) != 0U) {
95-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + idx,
96-
NPM1300_GPIO_GPOLOGIC1);
78+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE,
79+
NPM_GPIO_OFFSET_MODE + idx,
80+
NPM1300_GPIO_GPOLOGIC1);
9781
} else {
98-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + idx,
99-
NPM1300_GPIO_GPOLOGIC0);
82+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE,
83+
NPM_GPIO_OFFSET_MODE + idx,
84+
NPM1300_GPIO_GPOLOGIC0);
10085
}
10186
if (ret != 0U) {
10287
return ret;
@@ -120,6 +105,7 @@ static int gpio_npm1300_port_clear_bits_raw(const struct device *dev, gpio_port_
120105
static inline int gpio_npm1300_configure(const struct device *dev, gpio_pin_t pin,
121106
gpio_flags_t flags)
122107
{
108+
const struct gpio_npm1300_config *config = dev->config;
123109
int ret = 0;
124110

125111
if (k_is_in_isr()) {
@@ -132,48 +118,49 @@ static inline int gpio_npm1300_configure(const struct device *dev, gpio_pin_t pi
132118

133119
/* Configure mode */
134120
if ((flags & GPIO_INPUT) != 0U) {
135-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
136-
NPM1300_GPIO_GPIINPUT);
121+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
122+
NPM1300_GPIO_GPIINPUT);
137123
} else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) {
138-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
139-
NPM1300_GPIO_GPOLOGIC1);
124+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
125+
NPM1300_GPIO_GPOLOGIC1);
140126
} else if ((flags & GPIO_OUTPUT) != 0U) {
141-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
142-
NPM1300_GPIO_GPOLOGIC0);
127+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_MODE + pin,
128+
NPM1300_GPIO_GPOLOGIC0);
143129
}
144130

145131
if (ret < 0) {
146132
return ret;
147133
}
148134

149135
/* Configure open drain */
150-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_OPENDRAIN + pin,
151-
!!(flags & GPIO_SINGLE_ENDED));
136+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_OPENDRAIN + pin,
137+
!!(flags & GPIO_SINGLE_ENDED));
152138
if (ret < 0) {
153139
return ret;
154140
}
155141

156142
/* Configure pulls */
157-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_PULLUP + pin, !!(flags & GPIO_PULL_UP));
143+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_PULLUP + pin,
144+
!!(flags & GPIO_PULL_UP));
158145
if (ret < 0) {
159146
return ret;
160147
}
161148

162-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_PULLDOWN + pin,
163-
!!(flags & GPIO_PULL_DOWN));
149+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_PULLDOWN + pin,
150+
!!(flags & GPIO_PULL_DOWN));
164151
if (ret < 0) {
165152
return ret;
166153
}
167154

168155
/* Configure drive strength and debounce */
169-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_DRIVE + pin,
170-
!!(flags & NPM1300_GPIO_DRIVE_6MA));
156+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_DRIVE + pin,
157+
!!(flags & NPM1300_GPIO_DRIVE_6MA));
171158
if (ret < 0) {
172159
return ret;
173160
}
174161

175-
ret = reg_write(dev, NPM_GPIO_BASE, NPM_GPIO_OFFSET_DEBOUNCE + pin,
176-
!!(flags & NPM1300_GPIO_DEBOUNCE_ON));
162+
ret = mfd_npm1300_reg_write(config->mfd, NPM_GPIO_BASE, NPM_GPIO_OFFSET_DEBOUNCE + pin,
163+
!!(flags & NPM1300_GPIO_DEBOUNCE_ON));
177164

178165
return ret;
179166
}
@@ -217,7 +204,7 @@ static int gpio_npm1300_init(const struct device *dev)
217204
{
218205
const struct gpio_npm1300_config *config = dev->config;
219206

220-
if (!device_is_ready(config->bus.bus)) {
207+
if (!device_is_ready(config->mfd)) {
221208
return -ENODEV;
222209
}
223210

@@ -230,9 +217,9 @@ static int gpio_npm1300_init(const struct device *dev)
230217
{ \
231218
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
232219
}, \
233-
.bus = I2C_DT_SPEC_GET(DT_INST_PARENT(n))}; \
220+
.mfd = DEVICE_DT_GET(DT_INST_PARENT(n))}; \
234221
\
235-
static struct gpio_npm1300_data gpio_npm1300_data##n; \
222+
static struct gpio_npm1300_data gpio_npm1300_data##n; \
236223
\
237224
DEVICE_DT_INST_DEFINE(n, &gpio_npm1300_init, NULL, &gpio_npm1300_data##n, \
238225
&gpio_npm1300_config##n, POST_KERNEL, \

drivers/mfd/mfd_npm1300.c

+3-27
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,9 @@ int mfd_npm1300_reg_read_burst(const struct device *dev, uint8_t base, uint8_t o
3737
size_t len)
3838
{
3939
const struct mfd_npm1300_config *config = dev->config;
40-
struct mfd_npm1300_data *mfd_data = dev->data;
4140
uint8_t buff[] = {base, offset};
42-
int ret;
43-
44-
k_mutex_lock(&mfd_data->mutex, K_FOREVER);
45-
46-
ret = i2c_write_read_dt(&config->i2c, buff, sizeof(buff), data, len);
47-
48-
k_mutex_unlock(&mfd_data->mutex);
4941

50-
return ret;
42+
return i2c_write_read_dt(&config->i2c, buff, sizeof(buff), data, len);
5143
}
5244

5345
int mfd_npm1300_reg_read(const struct device *dev, uint8_t base, uint8_t offset, uint8_t *data)
@@ -58,34 +50,18 @@ int mfd_npm1300_reg_read(const struct device *dev, uint8_t base, uint8_t offset,
5850
int mfd_npm1300_reg_write(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data)
5951
{
6052
const struct mfd_npm1300_config *config = dev->config;
61-
struct mfd_npm1300_data *mfd_data = dev->data;
6253
uint8_t buff[] = {base, offset, data};
63-
int ret;
64-
65-
k_mutex_lock(&mfd_data->mutex, K_FOREVER);
6654

67-
ret = i2c_write_dt(&config->i2c, buff, sizeof(buff));
68-
69-
k_mutex_unlock(&mfd_data->mutex);
70-
71-
return ret;
55+
return i2c_write_dt(&config->i2c, buff, sizeof(buff));
7256
}
7357

7458
int mfd_npm1300_reg_write2(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data1,
7559
uint8_t data2)
7660
{
7761
const struct mfd_npm1300_config *config = dev->config;
78-
struct mfd_npm1300_data *mfd_data = dev->data;
7962
uint8_t buff[] = {base, offset, data1, data2};
80-
int ret;
8163

82-
k_mutex_lock(&mfd_data->mutex, K_FOREVER);
83-
84-
ret = i2c_write_dt(&config->i2c, buff, sizeof(buff));
85-
86-
k_mutex_unlock(&mfd_data->mutex);
87-
88-
return ret;
64+
return i2c_write_dt(&config->i2c, buff, sizeof(buff));
8965
}
9066

9167
int mfd_npm1300_reg_update(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data,

0 commit comments

Comments
 (0)