-
Notifications
You must be signed in to change notification settings - Fork 1
/
mx4-polled.c
221 lines (183 loc) · 6.18 KB
/
mx4-polled.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
+++ +++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++ +++
+++ COPYRIGHT (c) HostMobility AB +++
+++ +++
+++ The copyright to the computer Program(s) herein is the +++
+++ property of HostMobility, Sweden. The program(s) may be +++
+++ used and or copied only with the written permission of +++
+++ HostMobility, or in accordance with the terms and +++
+++ conditions stipulated in the agreement contract under +++
+++ which the program(s) have been supplied +++
+++ +++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++ +++
*/
#include "mx4-core.h"
#include <linux/irq.h>
/* This is offset of the first input of gpio base */
/* starting with {PROT_TYPE_DIGITAL_IN_1, 27, "digital-in-1 / sc", GPIOF_IN}, location in gpio.c */
#define INPUT_EVENT_OFFSET 27
static inline unsigned long ms_to_jiffies_raw(unsigned long ms)
{
return (ms * ((unsigned long)HZ)) / 1000;
}
static inline unsigned long ms_to_jiffies(unsigned long ms)
{
unsigned long val = ms_to_jiffies_raw (ms);
return (val == 0) ? 1 : val;
}
static inline unsigned long jiffies_to_ms(unsigned long jiffies)
{
return (jiffies * 1000) / HZ;
}
/* is to be called with the mutex locked */
static int is_value_polling_active(struct mx4_spi_device* mx4)
{
return delayed_work_pending(&mx4->polled.polled_work);
}
static void notify_periodic_polled_value_changes (
struct spi_device *spi,
struct kobject* obj,
u32 previous_value,
u32 current_value)
{
unsigned i = 0;
struct device *dev = &spi->dev;
struct mx4_spi_device* mx4 = dev_get_drvdata(dev);
struct mx4_irq *irq = &mx4->irq;
struct gpio_chip *chip = &mx4->chip;
u32 rising, falling, rising_mask, falling_mask;
rising_mask = (irq->irq_rise >> INPUT_EVENT_OFFSET);
falling_mask = (irq->irq_fall >> INPUT_EVENT_OFFSET);
rising = (current_value >> 16) & 0xffff;
falling = (current_value & 0xffff);
rising = rising & rising_mask;
falling = falling & falling_mask;
dev_dbg(dev, "input event change: rising: 0x%x, falling 0x%x,"
"rising mask: 0x%x, falling mask: 0x%x\n",
rising, falling, rising_mask, falling_mask);
for (; i < mx4_polled_inputs_count; ++i) {
int irq_nr = chip->to_irq(chip, i + INPUT_EVENT_OFFSET);
/* rising edge */
if (MX4_READ_INT_BIT (rising, i)) {
dev_dbg(dev, "rising event sent to gpio: %d\n",
(chip->base + i + INPUT_EVENT_OFFSET));
handle_nested_irq(irq_nr);
}
/* falling edge */
if (MX4_READ_INT_BIT (falling, i)) {
dev_dbg(dev, "falling event sent to gpio: %d\n",
(chip->base + i + INPUT_EVENT_OFFSET));
handle_nested_irq(irq_nr);
}
}
}
static void cancel_event_work_que(struct mx4_spi_device* mx4)
{
if (mx4->event_rdy_irq) {
cancel_work_sync(&mx4->polled.event_work);
flush_workqueue(mx4->polled.event_wq);
}
}
int mx4_event_stop(struct mx4_spi_device* mx4)
{
cancel_event_work_que(mx4);
return 0;
}
/* is to be called with the mutex locked */
int mx4_polled_stop(struct mx4_spi_device* mx4)
{
struct device *dev = &mx4->spi->dev;
if (is_value_polling_active(mx4)) {
/* Abort all on-going communication immediately otherwise our stop
* off work-queue could fail due to long timeout in spi protocol.
*/
mx4->spi_response_sync.has_data = 0;
wake_up_interruptible(&mx4->spi_response_sync.queue);
cancel_delayed_work_sync(&mx4->polled.polled_work);
flush_workqueue(mx4->polled.polled_wq);
dev_dbg(dev, "canceling spi periodical value polling\n");
return 1;
} else {
dev_dbg(dev, "canceling spi periodical value polling: "
"nothing to cancel\n");
return 0;
}
}
/* is to be called with a locked mutex */
int mx4_polled_start(struct mx4_spi_device* mx4)
{
int val = 0;
struct device *dev = &mx4->spi->dev;
if (ms_to_jiffies_raw(mx4->polled.period_ms) == 0) {
dev_warn(dev, "the polling period was tried to set to (%lu ms), "
"which is below the minimum jiffie resolution in this machine: "
"setting value to 1 jiffie = %lu ms", mx4->polled.period_ms,
jiffies_to_ms (1));
}
if (!is_value_polling_active(mx4)) {
dev_dbg(dev, "adding delayed work -> spi value polling\n");
val = queue_delayed_work (mx4->polled.polled_wq, &mx4->polled.polled_work,
ms_to_jiffies (mx4->polled.period_ms));
}
dev_dbg(dev, "adding delayed work -> result: %d\n", val);
return val;
}
static int mx4_updated_event_inputs(struct mx4_spi_device *mx4)
{
ssize_t op_result;
struct device *dev = &mx4->spi->dev;
mx4_spi_data_field_t current_value = 0;
op_result = mx4_spi_read_value(mx4->spi, ¤t_value,
PROT_TYPE_EVENT_INPUTS);
if (op_result != SUCCESSFULL_MX4_RW) {
dev_err(dev, "failed to update event inputs: %d", op_result);
return -1;
}
if (current_value != mx4->polled.last_input_value) {
notify_periodic_polled_value_changes(
mx4->spi,
&mx4->spi->dev.kobj,
mx4->polled.last_input_value,
current_value
);
mx4->polled.last_input_value = current_value;
}
return 0;
}
void mx4_event_work_callback(struct work_struct *work)
{
struct mx4_spi_device* mx4 = container_of(work, struct mx4_spi_device,
polled.event_work);
mutex_lock (&mx4->lock);
mx4_updated_event_inputs(mx4);
mutex_unlock(&mx4->lock);
}
void mx4_ping_co_cpu(struct mx4_spi_device* mx4)
{
struct device *dev = &mx4->spi->dev;
ssize_t op_result;
mx4_spi_data_field_t current_value = 0;
op_result = mx4_spi_read_value(mx4->spi, ¤t_value,
PROT_TYPE_POLLED);
if (op_result != SUCCESSFULL_MX4_RW) {
dev_err(dev, "Failed to ping co-cpu");
}
}
void mx4_polled_work_callback(struct work_struct* work)
{
struct mx4_spi_device* mx4 = container_of(work, struct mx4_spi_device,
polled.polled_work.work);
mutex_lock (&mx4->lock);
/* If there is no event_rdy signal then the update of event inputs
acts as an ping to co-cpu to keep the watchdog happy
*/
if (!mx4->event_rdy_present)
mx4_updated_event_inputs(mx4);
else
mx4_ping_co_cpu(mx4);
mx4_polled_start(mx4);
mutex_unlock(&mx4->lock);
}