hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/gnss/sirf.c
....@@ -25,18 +25,65 @@
2525 #define SIRF_ON_OFF_PULSE_TIME 100
2626 #define SIRF_ACTIVATE_TIMEOUT 200
2727 #define SIRF_HIBERNATE_TIMEOUT 200
28
+/*
29
+ * If no data arrives for this time, we assume that the chip is off.
30
+ * REVISIT: The report cycle is configurable and can be several minutes long,
31
+ * so this will only work reliably if the report cycle is set to a reasonable
32
+ * low value. Also power saving settings (like send data only on movement)
33
+ * might things work even worse.
34
+ * Workaround might be to parse shutdown or bootup messages.
35
+ */
36
+#define SIRF_REPORT_CYCLE 2000
2837
2938 struct sirf_data {
3039 struct gnss_device *gdev;
3140 struct serdev_device *serdev;
3241 speed_t speed;
3342 struct regulator *vcc;
43
+ struct regulator *lna;
3444 struct gpio_desc *on_off;
3545 struct gpio_desc *wakeup;
3646 int irq;
3747 bool active;
48
+
49
+ struct mutex gdev_mutex;
50
+ bool open;
51
+
52
+ struct mutex serdev_mutex;
53
+ int serdev_count;
54
+
3855 wait_queue_head_t power_wait;
3956 };
57
+
58
+static int sirf_serdev_open(struct sirf_data *data)
59
+{
60
+ int ret = 0;
61
+
62
+ mutex_lock(&data->serdev_mutex);
63
+ if (++data->serdev_count == 1) {
64
+ ret = serdev_device_open(data->serdev);
65
+ if (ret) {
66
+ data->serdev_count--;
67
+ goto out_unlock;
68
+ }
69
+
70
+ serdev_device_set_baudrate(data->serdev, data->speed);
71
+ serdev_device_set_flow_control(data->serdev, false);
72
+ }
73
+
74
+out_unlock:
75
+ mutex_unlock(&data->serdev_mutex);
76
+
77
+ return ret;
78
+}
79
+
80
+static void sirf_serdev_close(struct sirf_data *data)
81
+{
82
+ mutex_lock(&data->serdev_mutex);
83
+ if (--data->serdev_count == 0)
84
+ serdev_device_close(data->serdev);
85
+ mutex_unlock(&data->serdev_mutex);
86
+}
4087
4188 static int sirf_open(struct gnss_device *gdev)
4289 {
....@@ -44,12 +91,17 @@
4491 struct serdev_device *serdev = data->serdev;
4592 int ret;
4693
47
- ret = serdev_device_open(serdev);
48
- if (ret)
49
- return ret;
94
+ mutex_lock(&data->gdev_mutex);
95
+ data->open = true;
96
+ mutex_unlock(&data->gdev_mutex);
5097
51
- serdev_device_set_baudrate(serdev, data->speed);
52
- serdev_device_set_flow_control(serdev, false);
98
+ ret = sirf_serdev_open(data);
99
+ if (ret) {
100
+ mutex_lock(&data->gdev_mutex);
101
+ data->open = false;
102
+ mutex_unlock(&data->gdev_mutex);
103
+ return ret;
104
+ }
53105
54106 ret = pm_runtime_get_sync(&serdev->dev);
55107 if (ret < 0) {
....@@ -61,7 +113,11 @@
61113 return 0;
62114
63115 err_close:
64
- serdev_device_close(serdev);
116
+ sirf_serdev_close(data);
117
+
118
+ mutex_lock(&data->gdev_mutex);
119
+ data->open = false;
120
+ mutex_unlock(&data->gdev_mutex);
65121
66122 return ret;
67123 }
....@@ -71,9 +127,13 @@
71127 struct sirf_data *data = gnss_get_drvdata(gdev);
72128 struct serdev_device *serdev = data->serdev;
73129
74
- serdev_device_close(serdev);
130
+ sirf_serdev_close(data);
75131
76132 pm_runtime_put(&serdev->dev);
133
+
134
+ mutex_lock(&data->gdev_mutex);
135
+ data->open = false;
136
+ mutex_unlock(&data->gdev_mutex);
77137 }
78138
79139 static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
....@@ -85,7 +145,7 @@
85145
86146 /* write is only buffered synchronously */
87147 ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
88
- if (ret < 0)
148
+ if (ret < 0 || ret < count)
89149 return ret;
90150
91151 /* FIXME: determine if interrupted? */
....@@ -105,8 +165,19 @@
105165 {
106166 struct sirf_data *data = serdev_device_get_drvdata(serdev);
107167 struct gnss_device *gdev = data->gdev;
168
+ int ret = 0;
108169
109
- return gnss_insert_raw(gdev, buf, count);
170
+ if (!data->wakeup && !data->active) {
171
+ data->active = true;
172
+ wake_up_interruptible(&data->power_wait);
173
+ }
174
+
175
+ mutex_lock(&data->gdev_mutex);
176
+ if (data->open)
177
+ ret = gnss_insert_raw(gdev, buf, count);
178
+ mutex_unlock(&data->gdev_mutex);
179
+
180
+ return ret;
110181 }
111182
112183 static const struct serdev_device_ops sirf_serdev_ops = {
....@@ -125,16 +196,44 @@
125196 if (ret < 0)
126197 goto out;
127198
128
- data->active = !!ret;
199
+ data->active = ret;
129200 wake_up_interruptible(&data->power_wait);
130201 out:
131202 return IRQ_HANDLED;
203
+}
204
+
205
+static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
206
+ bool active,
207
+ unsigned long timeout)
208
+{
209
+ int ret;
210
+
211
+ /* Wait for state change (including any shutdown messages). */
212
+ msleep(timeout);
213
+
214
+ /* Wait for data reception or timeout. */
215
+ data->active = false;
216
+ ret = wait_event_interruptible_timeout(data->power_wait,
217
+ data->active, msecs_to_jiffies(SIRF_REPORT_CYCLE));
218
+ if (ret < 0)
219
+ return ret;
220
+
221
+ if (ret > 0 && !active)
222
+ return -ETIMEDOUT;
223
+
224
+ if (ret == 0 && active)
225
+ return -ETIMEDOUT;
226
+
227
+ return 0;
132228 }
133229
134230 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
135231 unsigned long timeout)
136232 {
137233 int ret;
234
+
235
+ if (!data->wakeup)
236
+ return sirf_wait_for_power_state_nowakeup(data, active, timeout);
138237
139238 ret = wait_event_interruptible_timeout(data->power_wait,
140239 data->active == active, msecs_to_jiffies(timeout));
....@@ -168,21 +267,22 @@
168267 else
169268 timeout = SIRF_HIBERNATE_TIMEOUT;
170269
270
+ if (!data->wakeup) {
271
+ ret = sirf_serdev_open(data);
272
+ if (ret)
273
+ return ret;
274
+ }
275
+
171276 do {
172277 sirf_pulse_on_off(data);
173278 ret = sirf_wait_for_power_state(data, active, timeout);
174
- if (ret < 0) {
175
- if (ret == -ETIMEDOUT)
176
- continue;
279
+ } while (ret == -ETIMEDOUT && retries--);
177280
178
- return ret;
179
- }
281
+ if (!data->wakeup)
282
+ sirf_serdev_close(data);
180283
181
- break;
182
- } while (retries--);
183
-
184
- if (retries < 0)
185
- return -ETIMEDOUT;
284
+ if (ret)
285
+ return ret;
186286
187287 return 0;
188288 }
....@@ -190,21 +290,60 @@
190290 static int sirf_runtime_suspend(struct device *dev)
191291 {
192292 struct sirf_data *data = dev_get_drvdata(dev);
293
+ int ret2;
294
+ int ret;
193295
194
- if (!data->on_off)
195
- return regulator_disable(data->vcc);
296
+ if (data->on_off)
297
+ ret = sirf_set_active(data, false);
298
+ else
299
+ ret = regulator_disable(data->vcc);
196300
197
- return sirf_set_active(data, false);
301
+ if (ret)
302
+ return ret;
303
+
304
+ ret = regulator_disable(data->lna);
305
+ if (ret)
306
+ goto err_reenable;
307
+
308
+ return 0;
309
+
310
+err_reenable:
311
+ if (data->on_off)
312
+ ret2 = sirf_set_active(data, true);
313
+ else
314
+ ret2 = regulator_enable(data->vcc);
315
+
316
+ if (ret2)
317
+ dev_err(dev,
318
+ "failed to reenable power on failed suspend: %d\n",
319
+ ret2);
320
+
321
+ return ret;
198322 }
199323
200324 static int sirf_runtime_resume(struct device *dev)
201325 {
202326 struct sirf_data *data = dev_get_drvdata(dev);
327
+ int ret;
203328
204
- if (!data->on_off)
205
- return regulator_enable(data->vcc);
329
+ ret = regulator_enable(data->lna);
330
+ if (ret)
331
+ return ret;
206332
207
- return sirf_set_active(data, true);
333
+ if (data->on_off)
334
+ ret = sirf_set_active(data, true);
335
+ else
336
+ ret = regulator_enable(data->vcc);
337
+
338
+ if (ret)
339
+ goto err_disable_lna;
340
+
341
+ return 0;
342
+
343
+err_disable_lna:
344
+ regulator_disable(data->lna);
345
+
346
+ return ret;
208347 }
209348
210349 static int __maybe_unused sirf_suspend(struct device *dev)
....@@ -275,6 +414,8 @@
275414 data->serdev = serdev;
276415 data->gdev = gdev;
277416
417
+ mutex_init(&data->gdev_mutex);
418
+ mutex_init(&data->serdev_mutex);
278419 init_waitqueue_head(&data->power_wait);
279420
280421 serdev_device_set_drvdata(serdev, data);
....@@ -287,6 +428,12 @@
287428 data->vcc = devm_regulator_get(dev, "vcc");
288429 if (IS_ERR(data->vcc)) {
289430 ret = PTR_ERR(data->vcc);
431
+ goto err_put_device;
432
+ }
433
+
434
+ data->lna = devm_regulator_get(dev, "lna");
435
+ if (IS_ERR(data->lna)) {
436
+ ret = PTR_ERR(data->lna);
290437 goto err_put_device;
291438 }
292439
....@@ -305,16 +452,6 @@
305452 goto err_put_device;
306453 }
307454
308
- /*
309
- * Configurations where WAKEUP has been left not connected,
310
- * are currently not supported.
311
- */
312
- if (!data->wakeup) {
313
- dev_err(dev, "no wakeup gpio specified\n");
314
- ret = -ENODEV;
315
- goto err_put_device;
316
- }
317
-
318455 ret = regulator_enable(data->vcc);
319456 if (ret)
320457 goto err_put_device;
....@@ -324,6 +461,11 @@
324461 }
325462
326463 if (data->wakeup) {
464
+ ret = gpiod_get_value_cansleep(data->wakeup);
465
+ if (ret < 0)
466
+ goto err_disable_vcc;
467
+ data->active = ret;
468
+
327469 ret = gpiod_to_irq(data->wakeup);
328470 if (ret < 0)
329471 goto err_disable_vcc;
....@@ -334,6 +476,29 @@
334476 "wakeup", data);
335477 if (ret)
336478 goto err_disable_vcc;
479
+ }
480
+
481
+ if (data->on_off) {
482
+ if (!data->wakeup) {
483
+ data->active = false;
484
+
485
+ ret = sirf_serdev_open(data);
486
+ if (ret)
487
+ goto err_disable_vcc;
488
+
489
+ msleep(SIRF_REPORT_CYCLE);
490
+ sirf_serdev_close(data);
491
+ }
492
+
493
+ /* Force hibernate mode if already active. */
494
+ if (data->active) {
495
+ ret = sirf_set_active(data, false);
496
+ if (ret) {
497
+ dev_err(dev, "failed to set hibernate mode: %d\n",
498
+ ret);
499
+ goto err_free_irq;
500
+ }
501
+ }
337502 }
338503
339504 if (IS_ENABLED(CONFIG_PM)) {
....@@ -392,6 +557,7 @@
392557 static const struct of_device_id sirf_of_match[] = {
393558 { .compatible = "fastrax,uc430" },
394559 { .compatible = "linx,r4" },
560
+ { .compatible = "wi2wi,w2sg0004" },
395561 { .compatible = "wi2wi,w2sg0008i" },
396562 { .compatible = "wi2wi,w2sg0084i" },
397563 {},