Skip to content

Commit 60262f4

Browse files
pabigotMaureenHelm
authored andcommitted
sensor: adt7420: update for new GPIO API
Update sample overlays. Add GPIO flags to configuration state. Refactor to split out setup/handle/process phases. Switch to new API replacing callback dis/enable with interrupt dis/enable. Signed-off-by: Peter Bigot <[email protected]>
1 parent a7a7c62 commit 60262f4

File tree

6 files changed

+79
-36
lines changed

6 files changed

+79
-36
lines changed

drivers/sensor/adt7420/adt7420.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ static const struct adt7420_dev_config adt7420_config = {
219219
.i2c_port = DT_INST_0_ADI_ADT7420_BUS_NAME,
220220
.i2c_addr = DT_INST_0_ADI_ADT7420_BASE_ADDRESS,
221221
#ifdef CONFIG_ADT7420_TRIGGER
222-
.gpio_port = DT_INST_0_ADI_ADT7420_INT_GPIOS_CONTROLLER,
223-
.int_gpio = DT_INST_0_ADI_ADT7420_INT_GPIOS_PIN,
222+
.int_pin = DT_INST_0_ADI_ADT7420_INT_GPIOS_PIN,
223+
.int_flags = DT_INST_0_ADI_ADT7420_INT_GPIOS_FLAGS,
224+
.int_name = DT_INST_0_ADI_ADT7420_INT_GPIOS_CONTROLLER,
224225
#endif
225226
};
226227

drivers/sensor/adt7420/adt7420.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ struct adt7420_data {
6868
sensor_trigger_handler_t th_handler;
6969
struct sensor_trigger th_trigger;
7070

71+
struct device *dev;
72+
7173
#if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
7274
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_ADT7420_THREAD_STACK_SIZE);
7375
struct k_sem gpio_sem;
7476
struct k_thread thread;
7577
#elif defined(CONFIG_ADT7420_TRIGGER_GLOBAL_THREAD)
7678
struct k_work work;
77-
struct device *dev;
7879
#endif
7980
#endif /* CONFIG_ADT7420_TRIGGER */
8081

@@ -84,8 +85,9 @@ struct adt7420_dev_config {
8485
const char *i2c_port;
8586
u16_t i2c_addr;
8687
#ifdef CONFIG_ADT7420_TRIGGER
87-
const char *gpio_port;
88-
u8_t int_gpio;
88+
gpio_pin_t int_pin;
89+
gpio_flags_t int_flags;
90+
const char *int_name;
8991
#endif
9092
};
9193

drivers/sensor/adt7420/adt7420_trigger.c

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,33 @@
1616
#include <logging/log.h>
1717
LOG_MODULE_DECLARE(ADT7420, CONFIG_SENSOR_LOG_LEVEL);
1818

19-
static void adt7420_thread_cb(void *arg)
19+
static void setup_int(struct device *dev,
20+
bool enable)
21+
{
22+
struct adt7420_data *drv_data = dev->driver_data;
23+
const struct adt7420_dev_config *cfg = dev->config->config_info;
24+
gpio_flags_t flags = enable
25+
? GPIO_INT_EDGE_TO_ACTIVE
26+
: GPIO_INT_DISABLE;
27+
28+
gpio_pin_interrupt_configure(drv_data->gpio, cfg->int_pin, flags);
29+
}
30+
31+
static void handle_int(struct device *dev)
32+
{
33+
struct adt7420_data *drv_data = dev->driver_data;
34+
35+
setup_int(dev, false);
36+
37+
#if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
38+
k_sem_give(&drv_data->gpio_sem);
39+
#elif defined(CONFIG_ADT7420_TRIGGER_GLOBAL_THREAD)
40+
k_work_submit(&drv_data->work);
41+
#endif
42+
}
43+
44+
static void process_int(struct device *dev)
2045
{
21-
struct device *dev = arg;
2246
struct adt7420_data *drv_data = dev->driver_data;
2347
const struct adt7420_dev_config *cfg = dev->config->config_info;
2448
u8_t status;
@@ -33,23 +57,23 @@ static void adt7420_thread_cb(void *arg)
3357
drv_data->th_handler(dev, &drv_data->th_trigger);
3458
}
3559

36-
gpio_pin_enable_callback(drv_data->gpio, cfg->int_gpio);
60+
setup_int(dev, true);
61+
62+
/* Check for pin that asserted while we were offline */
63+
int pv = gpio_pin_get(drv_data->gpio, cfg->int_pin);
64+
65+
if (pv > 0) {
66+
handle_int(dev);
67+
}
3768
}
3869

3970
static void adt7420_gpio_callback(struct device *dev,
4071
struct gpio_callback *cb, u32_t pins)
4172
{
4273
struct adt7420_data *drv_data =
4374
CONTAINER_OF(cb, struct adt7420_data, gpio_cb);
44-
const struct adt7420_dev_config *cfg = dev->config->config_info;
45-
46-
gpio_pin_disable_callback(dev, cfg->int_gpio);
4775

48-
#if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
49-
k_sem_give(&drv_data->gpio_sem);
50-
#elif defined(CONFIG_ADT7420_TRIGGER_GLOBAL_THREAD)
51-
k_work_submit(&drv_data->work);
52-
#endif
76+
handle_int(drv_data->dev);
5377
}
5478

5579
#if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
@@ -62,7 +86,7 @@ static void adt7420_thread(int dev_ptr, int unused)
6286

6387
while (true) {
6488
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
65-
adt7420_thread_cb(dev);
89+
process_int(dev);
6690
}
6791
}
6892

@@ -71,7 +95,8 @@ static void adt7420_work_cb(struct k_work *work)
7195
{
7296
struct adt7420_data *drv_data =
7397
CONTAINER_OF(work, struct adt7420_data, work);
74-
adt7420_thread_cb(drv_data->dev);
98+
99+
process_int(drv_data->dev);
75100
}
76101
#endif
77102

@@ -82,17 +107,26 @@ int adt7420_trigger_set(struct device *dev,
82107
struct adt7420_data *drv_data = dev->driver_data;
83108
const struct adt7420_dev_config *cfg = dev->config->config_info;
84109

85-
gpio_pin_disable_callback(drv_data->gpio, cfg->int_gpio);
110+
setup_int(dev, false);
86111

87-
if (trig->type == SENSOR_TRIG_THRESHOLD) {
88-
drv_data->th_handler = handler;
89-
drv_data->th_trigger = *trig;
90-
} else {
112+
if (trig->type != SENSOR_TRIG_THRESHOLD) {
91113
LOG_ERR("Unsupported sensor trigger");
92114
return -ENOTSUP;
93115
}
116+
drv_data->th_handler = handler;
117+
118+
if (handler != NULL) {
119+
drv_data->th_trigger = *trig;
120+
121+
setup_int(dev, true);
94122

95-
gpio_pin_enable_callback(drv_data->gpio, cfg->int_gpio);
123+
/* Check whether already asserted */
124+
int pv = gpio_pin_get(drv_data->gpio, cfg->int_pin);
125+
126+
if (pv > 0) {
127+
handle_int(dev);
128+
}
129+
}
96130

97131
return 0;
98132
}
@@ -102,26 +136,30 @@ int adt7420_init_interrupt(struct device *dev)
102136
struct adt7420_data *drv_data = dev->driver_data;
103137
const struct adt7420_dev_config *cfg = dev->config->config_info;
104138

105-
drv_data->gpio = device_get_binding(cfg->gpio_port);
139+
drv_data->gpio = device_get_binding(cfg->int_name);
106140
if (drv_data->gpio == NULL) {
107141
LOG_DBG("Failed to get pointer to %s device!",
108-
cfg->gpio_port);
142+
cfg->int_name);
109143
return -EINVAL;
110144
}
111145

112-
gpio_pin_configure(drv_data->gpio, cfg->int_gpio,
113-
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
114-
GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE);
115-
116146
gpio_init_callback(&drv_data->gpio_cb,
117147
adt7420_gpio_callback,
118-
BIT(cfg->int_gpio));
148+
BIT(cfg->int_pin));
119149

120-
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
150+
int rc = gpio_pin_configure(drv_data->gpio, cfg->int_pin,
151+
GPIO_INPUT | cfg->int_flags);
152+
if (rc == 0) {
153+
gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb);
154+
}
155+
156+
if (rc != 0) {
121157
LOG_DBG("Failed to set gpio callback!");
122-
return -EIO;
158+
return rc;
123159
}
124160

161+
drv_data->dev = dev;
162+
125163
#if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
126164
k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
127165

@@ -132,7 +170,6 @@ int adt7420_init_interrupt(struct device *dev)
132170
0, K_NO_WAIT);
133171
#elif defined(CONFIG_ADT7420_TRIGGER_GLOBAL_THREAD)
134172
drv_data->work.handler = adt7420_work_cb;
135-
drv_data->dev = dev;
136173
#endif
137174

138175
return 0;

dts/bindings/sensor/adi,adt7420.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ properties:
1111
int-gpios:
1212
type: phandle-array
1313
required: false
14+
description: |
15+
The INT signal defaults to active low open drain, so requires a
16+
pull-up on the board or in the flags cell of this entry.

samples/sensor/adt7420/boards/frdm_k64f.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
compatible = "adi,adt7420";
1212
reg = <0x48>;
1313
label = "ADT7420";
14-
int-gpios = <&gpioc 16 0>;
14+
int-gpios = <&gpioc 16 GPIO_ACTIVE_LOW>;
1515
};
1616
};

samples/sensor/adt7420/boards/nrf52_pca10040.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
compatible = "adi,adt7420";
1212
reg = <0x48>;
1313
label = "ADT7420";
14-
int-gpios = <&gpio0 11 0>;
14+
int-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;
1515
};
1616
};

0 commit comments

Comments
 (0)