1 /*
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2023 Linumiz
5 */
6
7 #include "mc3419.h"
8 #include <zephyr/logging/log.h>
9
10 LOG_MODULE_DECLARE(MC3419, CONFIG_SENSOR_LOG_LEVEL);
11
mc3419_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pin_mask)12 static void mc3419_gpio_callback(const struct device *dev,
13 struct gpio_callback *cb,
14 uint32_t pin_mask)
15 {
16 struct mc3419_driver_data *data = CONTAINER_OF(cb,
17 struct mc3419_driver_data, gpio_cb);
18
19 const struct mc3419_config *cfg = data->gpio_dev->config;
20
21 if ((pin_mask & BIT(cfg->int_gpio.pin)) == 0U) {
22 return;
23 }
24
25 #if defined(CONFIG_MC3419_TRIGGER_OWN_THREAD)
26 k_sem_give(&data->trig_sem);
27 #else
28 k_work_submit(&data->work);
29 #endif
30 }
31
mc3419_process_int(const struct device * dev)32 static void mc3419_process_int(const struct device *dev)
33 {
34 int ret = 0;
35 const struct mc3419_config *cfg = dev->config;
36 const struct mc3419_driver_data *data = dev->data;
37 uint8_t int_source = 0;
38
39 ret = i2c_reg_read_byte_dt(&cfg->i2c, MC3419_REG_INT_STATUS, &int_source);
40 if (ret < 0) {
41 goto exit;
42 }
43
44 if (int_source & MC3419_DATA_READY_MASK) {
45 if (data->handler[MC3419_TRIG_DATA_READY]) {
46 data->handler[MC3419_TRIG_DATA_READY](dev,
47 data->trigger[MC3419_TRIG_DATA_READY]);
48 }
49 }
50
51 if (int_source & MC3419_ANY_MOTION_MASK) {
52 if (data->handler[MC3419_TRIG_ANY_MOTION]) {
53 data->handler[MC3419_TRIG_ANY_MOTION](dev,
54 data->trigger[MC3419_TRIG_ANY_MOTION]);
55 }
56 }
57 exit:
58 ret = i2c_reg_write_byte_dt(&cfg->i2c, MC3419_REG_INT_STATUS,
59 MC3419_INT_CLEAR);
60 if (ret < 0) {
61 LOG_ERR("Failed to clear interrupt (%d)", ret);
62 }
63 }
64
65 #if defined(CONFIG_MC3419_TRIGGER_OWN_THREAD)
mc3419_thread(struct mc3419_driver_data * data)66 static void mc3419_thread(struct mc3419_driver_data *data)
67 {
68 while (1) {
69 k_sem_take(&data->trig_sem, K_FOREVER);
70 mc3419_process_int(data->gpio_dev);
71 }
72 }
73 #else
mc3419_work_cb(struct k_work * work)74 static void mc3419_work_cb(struct k_work *work)
75 {
76 struct mc3419_driver_data *data = CONTAINER_OF(work,
77 struct mc3419_driver_data, work);
78
79 mc3419_process_int(data->gpio_dev);
80 }
81 #endif
82
mc3419_configure_trigger(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)83 int mc3419_configure_trigger(const struct device *dev,
84 const struct sensor_trigger *trig,
85 sensor_trigger_handler_t handler)
86 {
87 int ret = 0;
88 uint8_t buf = 0;
89 const struct mc3419_config *cfg = dev->config;
90 struct mc3419_driver_data *data = dev->data;
91
92 if (!(trig->type & SENSOR_TRIG_DATA_READY) &&
93 !(trig->type & SENSOR_TRIG_MOTION)) {
94 LOG_ERR("Unsupported sensor trigger");
95 return -ENOTSUP;
96 }
97
98 if (trig->type & SENSOR_TRIG_DATA_READY) {
99 data->handler[MC3419_TRIG_DATA_READY] = handler;
100 data->trigger[MC3419_TRIG_DATA_READY] = trig;
101 buf |= MC3419_DATA_READY_MASK;
102 }
103
104 if (trig->type & SENSOR_TRIG_MOTION) {
105 uint8_t int_mask = MC3419_ANY_MOTION_MASK;
106
107 buf |= MC3419_ANY_MOTION_MASK;
108 data->handler[MC3419_TRIG_ANY_MOTION] = handler;
109 data->trigger[MC3419_TRIG_ANY_MOTION] = trig;
110
111 ret = i2c_reg_update_byte_dt(&cfg->i2c, MC3419_REG_MOTION_CTRL,
112 int_mask, handler ? int_mask : 0);
113 if (ret < 0) {
114 LOG_ERR("Failed to configure motion interrupt (%d)", ret);
115 return ret;
116 }
117 }
118
119 ret = i2c_reg_update_byte_dt(&cfg->i2c, MC3419_REG_INT_CTRL,
120 buf, buf);
121 if (ret < 0) {
122 LOG_ERR("Failed to configure interrupt (%d)", ret);
123 return ret;
124 }
125
126 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_FALLING);
127
128 return ret;
129 }
130
mc3419_trigger_init(const struct device * dev)131 int mc3419_trigger_init(const struct device *dev)
132 {
133 int ret = 0;
134 struct mc3419_driver_data *data = dev->data;
135 const struct mc3419_config *cfg = dev->config;
136
137 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
138 LOG_ERR("GPIO port %s not ready", cfg->int_gpio.port->name);
139 return -ENODEV;
140 }
141
142 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
143 if (ret < 0) {
144 LOG_ERR("Failed to configure interrupt gpio");
145 return ret;
146 }
147
148 data->gpio_dev = dev;
149
150 #if defined(CONFIG_MC3419_TRIGGER_OWN_THREAD)
151 k_sem_init(&data->trig_sem, 0, 1);
152 k_thread_create(&data->thread, data->thread_stack,
153 CONFIG_MC3419_THREAD_STACK_SIZE,
154 (k_thread_entry_t)mc3419_thread, data, NULL,
155 NULL, K_PRIO_COOP(CONFIG_MC3419_THREAD_PRIORITY), 0,
156 K_NO_WAIT);
157 #else
158 k_work_init(&data->work, mc3419_work_cb);
159 #endif
160 gpio_init_callback(&data->gpio_cb, mc3419_gpio_callback,
161 BIT(cfg->int_gpio.pin));
162 ret = gpio_add_callback(cfg->int_gpio.port, &data->gpio_cb);
163 if (ret < 0) {
164 LOG_ERR("Failed to set int callback");
165 return ret;
166 }
167
168 if (cfg->int_cfg) {
169 ret = i2c_reg_write_byte_dt(&cfg->i2c, MC3419_REG_COMM_CTRL,
170 MC3419_INT_ROUTE);
171 if (ret < 0) {
172 LOG_ERR("Failed to route the interrupt to INT2 pin (%d)", ret);
173 return ret;
174 }
175 }
176
177 return 0;
178 }
179