1616#include <logging/log.h>
1717LOG_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
3970static 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 ;
0 commit comments