9
9
10
10
#include <zephyr/drivers/gpio.h>
11
11
#include <zephyr/drivers/gpio/gpio_utils.h>
12
- #include <zephyr/drivers/i2c .h>
12
+ #include <zephyr/drivers/mfd/npm1300 .h>
13
13
#include <zephyr/dt-bindings/gpio/nordic-npm1300-gpio.h>
14
14
#include <zephyr/kernel.h>
15
15
#include <zephyr/sys/util_macro.h>
16
- #include <zephyr/toolchain.h>
17
- #include <zephyr/sys/byteorder.h>
18
16
19
17
/* nPM1300 GPIO base address */
20
18
#define NPM_GPIO_BASE 0x06U
44
42
45
43
struct gpio_npm1300_config {
46
44
struct gpio_driver_config common ;
47
- struct i2c_dt_spec bus ;
45
+ const struct device * mfd ;
48
46
};
49
47
50
48
struct gpio_npm1300_data {
51
49
struct gpio_driver_data common ;
52
50
};
53
51
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
-
71
52
static int gpio_npm1300_port_get_raw (const struct device * dev , uint32_t * value )
72
53
{
54
+ const struct gpio_npm1300_config * config = dev -> config ;
73
55
int ret ;
74
56
uint8_t data ;
75
57
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 );
77
59
78
60
if (ret < 0 ) {
79
61
return ret ;
@@ -87,16 +69,19 @@ static int gpio_npm1300_port_get_raw(const struct device *dev, uint32_t *value)
87
69
static int gpio_npm1300_port_set_masked_raw (const struct device * dev , gpio_port_pins_t mask ,
88
70
gpio_port_value_t value )
89
71
{
72
+ const struct gpio_npm1300_config * config = dev -> config ;
90
73
int ret = 0 ;
91
74
92
75
for (size_t idx = 0 ; idx < NPM1300_GPIO_PINS ; idx ++ ) {
93
76
if ((mask & BIT (idx )) != 0U ) {
94
77
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 );
97
81
} 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 );
100
85
}
101
86
if (ret != 0U ) {
102
87
return ret ;
@@ -120,6 +105,7 @@ static int gpio_npm1300_port_clear_bits_raw(const struct device *dev, gpio_port_
120
105
static inline int gpio_npm1300_configure (const struct device * dev , gpio_pin_t pin ,
121
106
gpio_flags_t flags )
122
107
{
108
+ const struct gpio_npm1300_config * config = dev -> config ;
123
109
int ret = 0 ;
124
110
125
111
if (k_is_in_isr ()) {
@@ -132,48 +118,49 @@ static inline int gpio_npm1300_configure(const struct device *dev, gpio_pin_t pi
132
118
133
119
/* Configure mode */
134
120
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 );
137
123
} 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 );
140
126
} 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 );
143
129
}
144
130
145
131
if (ret < 0 ) {
146
132
return ret ;
147
133
}
148
134
149
135
/* 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 ));
152
138
if (ret < 0 ) {
153
139
return ret ;
154
140
}
155
141
156
142
/* 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 ));
158
145
if (ret < 0 ) {
159
146
return ret ;
160
147
}
161
148
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 ));
164
151
if (ret < 0 ) {
165
152
return ret ;
166
153
}
167
154
168
155
/* 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 ));
171
158
if (ret < 0 ) {
172
159
return ret ;
173
160
}
174
161
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 ));
177
164
178
165
return ret ;
179
166
}
@@ -217,7 +204,7 @@ static int gpio_npm1300_init(const struct device *dev)
217
204
{
218
205
const struct gpio_npm1300_config * config = dev -> config ;
219
206
220
- if (!device_is_ready (config -> bus . bus )) {
207
+ if (!device_is_ready (config -> mfd )) {
221
208
return - ENODEV ;
222
209
}
223
210
@@ -230,9 +217,9 @@ static int gpio_npm1300_init(const struct device *dev)
230
217
{ \
231
218
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
232
219
}, \
233
- .bus = I2C_DT_SPEC_GET (DT_INST_PARENT(n))}; \
220
+ .mfd = DEVICE_DT_GET (DT_INST_PARENT(n))}; \
234
221
\
235
- static struct gpio_npm1300_data gpio_npm1300_data##n; \
222
+ static struct gpio_npm1300_data gpio_npm1300_data##n; \
236
223
\
237
224
DEVICE_DT_INST_DEFINE(n, &gpio_npm1300_init, NULL, &gpio_npm1300_data##n, \
238
225
&gpio_npm1300_config##n, POST_KERNEL, \
0 commit comments