Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gnss / sirf.c
blob2ecb1d3e8eeb3bb049073f189d37c90b30d4b221
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SiRFstar GNSS receiver driver
5 * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
6 */
8 #include <linux/errno.h>
9 #include <linux/gnss.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/pm.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/sched.h>
20 #include <linux/serdev.h>
21 #include <linux/slab.h>
22 #include <linux/wait.h>
24 #define SIRF_BOOT_DELAY 500
25 #define SIRF_ON_OFF_PULSE_TIME 100
26 #define SIRF_ACTIVATE_TIMEOUT 200
27 #define SIRF_HIBERNATE_TIMEOUT 200
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.
36 #define SIRF_REPORT_CYCLE 2000
38 struct sirf_data {
39 struct gnss_device *gdev;
40 struct serdev_device *serdev;
41 speed_t speed;
42 struct regulator *vcc;
43 struct regulator *lna;
44 struct gpio_desc *on_off;
45 struct gpio_desc *wakeup;
46 int irq;
47 bool active;
49 struct mutex gdev_mutex;
50 bool open;
52 struct mutex serdev_mutex;
53 int serdev_count;
55 wait_queue_head_t power_wait;
58 static int sirf_serdev_open(struct sirf_data *data)
60 int ret = 0;
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;
70 serdev_device_set_baudrate(data->serdev, data->speed);
71 serdev_device_set_flow_control(data->serdev, false);
74 out_unlock:
75 mutex_unlock(&data->serdev_mutex);
77 return ret;
80 static void sirf_serdev_close(struct sirf_data *data)
82 mutex_lock(&data->serdev_mutex);
83 if (--data->serdev_count == 0)
84 serdev_device_close(data->serdev);
85 mutex_unlock(&data->serdev_mutex);
88 static int sirf_open(struct gnss_device *gdev)
90 struct sirf_data *data = gnss_get_drvdata(gdev);
91 struct serdev_device *serdev = data->serdev;
92 int ret;
94 mutex_lock(&data->gdev_mutex);
95 data->open = true;
96 mutex_unlock(&data->gdev_mutex);
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;
106 ret = pm_runtime_get_sync(&serdev->dev);
107 if (ret < 0) {
108 dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
109 pm_runtime_put_noidle(&serdev->dev);
110 goto err_close;
113 return 0;
115 err_close:
116 sirf_serdev_close(data);
118 mutex_lock(&data->gdev_mutex);
119 data->open = false;
120 mutex_unlock(&data->gdev_mutex);
122 return ret;
125 static void sirf_close(struct gnss_device *gdev)
127 struct sirf_data *data = gnss_get_drvdata(gdev);
128 struct serdev_device *serdev = data->serdev;
130 sirf_serdev_close(data);
132 pm_runtime_put(&serdev->dev);
134 mutex_lock(&data->gdev_mutex);
135 data->open = false;
136 mutex_unlock(&data->gdev_mutex);
139 static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
140 size_t count)
142 struct sirf_data *data = gnss_get_drvdata(gdev);
143 struct serdev_device *serdev = data->serdev;
144 int ret;
146 /* write is only buffered synchronously */
147 ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
148 if (ret < 0 || ret < count)
149 return ret;
151 /* FIXME: determine if interrupted? */
152 serdev_device_wait_until_sent(serdev, 0);
154 return count;
157 static const struct gnss_operations sirf_gnss_ops = {
158 .open = sirf_open,
159 .close = sirf_close,
160 .write_raw = sirf_write_raw,
163 static int sirf_receive_buf(struct serdev_device *serdev,
164 const unsigned char *buf, size_t count)
166 struct sirf_data *data = serdev_device_get_drvdata(serdev);
167 struct gnss_device *gdev = data->gdev;
168 int ret = 0;
170 if (!data->wakeup && !data->active) {
171 data->active = true;
172 wake_up_interruptible(&data->power_wait);
175 mutex_lock(&data->gdev_mutex);
176 if (data->open)
177 ret = gnss_insert_raw(gdev, buf, count);
178 mutex_unlock(&data->gdev_mutex);
180 return ret;
183 static const struct serdev_device_ops sirf_serdev_ops = {
184 .receive_buf = sirf_receive_buf,
185 .write_wakeup = serdev_device_write_wakeup,
188 static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id)
190 struct sirf_data *data = dev_id;
191 struct device *dev = &data->serdev->dev;
192 int ret;
194 ret = gpiod_get_value_cansleep(data->wakeup);
195 dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
196 if (ret < 0)
197 goto out;
199 data->active = ret;
200 wake_up_interruptible(&data->power_wait);
201 out:
202 return IRQ_HANDLED;
205 static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
206 bool active,
207 unsigned long timeout)
209 int ret;
211 /* Wait for state change (including any shutdown messages). */
212 msleep(timeout);
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;
221 if (ret > 0 && !active)
222 return -ETIMEDOUT;
224 if (ret == 0 && active)
225 return -ETIMEDOUT;
227 return 0;
230 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
231 unsigned long timeout)
233 int ret;
235 if (!data->wakeup)
236 return sirf_wait_for_power_state_nowakeup(data, active, timeout);
238 ret = wait_event_interruptible_timeout(data->power_wait,
239 data->active == active, msecs_to_jiffies(timeout));
240 if (ret < 0)
241 return ret;
243 if (ret == 0) {
244 dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
245 active);
246 return -ETIMEDOUT;
249 return 0;
252 static void sirf_pulse_on_off(struct sirf_data *data)
254 gpiod_set_value_cansleep(data->on_off, 1);
255 msleep(SIRF_ON_OFF_PULSE_TIME);
256 gpiod_set_value_cansleep(data->on_off, 0);
259 static int sirf_set_active(struct sirf_data *data, bool active)
261 unsigned long timeout;
262 int retries = 3;
263 int ret;
265 if (active)
266 timeout = SIRF_ACTIVATE_TIMEOUT;
267 else
268 timeout = SIRF_HIBERNATE_TIMEOUT;
270 if (!data->wakeup) {
271 ret = sirf_serdev_open(data);
272 if (ret)
273 return ret;
276 do {
277 sirf_pulse_on_off(data);
278 ret = sirf_wait_for_power_state(data, active, timeout);
279 } while (ret == -ETIMEDOUT && retries--);
281 if (!data->wakeup)
282 sirf_serdev_close(data);
284 if (ret)
285 return ret;
287 return 0;
290 static int sirf_runtime_suspend(struct device *dev)
292 struct sirf_data *data = dev_get_drvdata(dev);
293 int ret2;
294 int ret;
296 if (data->on_off)
297 ret = sirf_set_active(data, false);
298 else
299 ret = regulator_disable(data->vcc);
301 if (ret)
302 return ret;
304 ret = regulator_disable(data->lna);
305 if (ret)
306 goto err_reenable;
308 return 0;
310 err_reenable:
311 if (data->on_off)
312 ret2 = sirf_set_active(data, true);
313 else
314 ret2 = regulator_enable(data->vcc);
316 if (ret2)
317 dev_err(dev,
318 "failed to reenable power on failed suspend: %d\n",
319 ret2);
321 return ret;
324 static int sirf_runtime_resume(struct device *dev)
326 struct sirf_data *data = dev_get_drvdata(dev);
327 int ret;
329 ret = regulator_enable(data->lna);
330 if (ret)
331 return ret;
333 if (data->on_off)
334 ret = sirf_set_active(data, true);
335 else
336 ret = regulator_enable(data->vcc);
338 if (ret)
339 goto err_disable_lna;
341 return 0;
343 err_disable_lna:
344 regulator_disable(data->lna);
346 return ret;
349 static int __maybe_unused sirf_suspend(struct device *dev)
351 struct sirf_data *data = dev_get_drvdata(dev);
352 int ret = 0;
354 if (!pm_runtime_suspended(dev))
355 ret = sirf_runtime_suspend(dev);
357 if (data->wakeup)
358 disable_irq(data->irq);
360 return ret;
363 static int __maybe_unused sirf_resume(struct device *dev)
365 struct sirf_data *data = dev_get_drvdata(dev);
366 int ret = 0;
368 if (data->wakeup)
369 enable_irq(data->irq);
371 if (!pm_runtime_suspended(dev))
372 ret = sirf_runtime_resume(dev);
374 return ret;
377 static const struct dev_pm_ops sirf_pm_ops = {
378 SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend, sirf_resume)
379 SET_RUNTIME_PM_OPS(sirf_runtime_suspend, sirf_runtime_resume, NULL)
382 static int sirf_parse_dt(struct serdev_device *serdev)
384 struct sirf_data *data = serdev_device_get_drvdata(serdev);
385 struct device_node *node = serdev->dev.of_node;
386 u32 speed = 9600;
388 of_property_read_u32(node, "current-speed", &speed);
390 data->speed = speed;
392 return 0;
395 static int sirf_probe(struct serdev_device *serdev)
397 struct device *dev = &serdev->dev;
398 struct gnss_device *gdev;
399 struct sirf_data *data;
400 int ret;
402 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
403 if (!data)
404 return -ENOMEM;
406 gdev = gnss_allocate_device(dev);
407 if (!gdev)
408 return -ENOMEM;
410 gdev->type = GNSS_TYPE_SIRF;
411 gdev->ops = &sirf_gnss_ops;
412 gnss_set_drvdata(gdev, data);
414 data->serdev = serdev;
415 data->gdev = gdev;
417 mutex_init(&data->gdev_mutex);
418 mutex_init(&data->serdev_mutex);
419 init_waitqueue_head(&data->power_wait);
421 serdev_device_set_drvdata(serdev, data);
422 serdev_device_set_client_ops(serdev, &sirf_serdev_ops);
424 ret = sirf_parse_dt(serdev);
425 if (ret)
426 goto err_put_device;
428 data->vcc = devm_regulator_get(dev, "vcc");
429 if (IS_ERR(data->vcc)) {
430 ret = PTR_ERR(data->vcc);
431 goto err_put_device;
434 data->lna = devm_regulator_get(dev, "lna");
435 if (IS_ERR(data->lna)) {
436 ret = PTR_ERR(data->lna);
437 goto err_put_device;
440 data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
441 GPIOD_OUT_LOW);
442 if (IS_ERR(data->on_off)) {
443 ret = PTR_ERR(data->on_off);
444 goto err_put_device;
447 if (data->on_off) {
448 data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
449 GPIOD_IN);
450 if (IS_ERR(data->wakeup)) {
451 ret = PTR_ERR(data->wakeup);
452 goto err_put_device;
455 ret = regulator_enable(data->vcc);
456 if (ret)
457 goto err_put_device;
459 /* Wait for chip to boot into hibernate mode. */
460 msleep(SIRF_BOOT_DELAY);
463 if (data->wakeup) {
464 ret = gpiod_get_value_cansleep(data->wakeup);
465 if (ret < 0)
466 goto err_disable_vcc;
467 data->active = ret;
469 ret = gpiod_to_irq(data->wakeup);
470 if (ret < 0)
471 goto err_disable_vcc;
472 data->irq = ret;
474 ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
475 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
476 "wakeup", data);
477 if (ret)
478 goto err_disable_vcc;
481 if (data->on_off) {
482 if (!data->wakeup) {
483 data->active = false;
485 ret = sirf_serdev_open(data);
486 if (ret)
487 goto err_disable_vcc;
489 msleep(SIRF_REPORT_CYCLE);
490 sirf_serdev_close(data);
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;
504 if (IS_ENABLED(CONFIG_PM)) {
505 pm_runtime_set_suspended(dev); /* clear runtime_error flag */
506 pm_runtime_enable(dev);
507 } else {
508 ret = sirf_runtime_resume(dev);
509 if (ret < 0)
510 goto err_free_irq;
513 ret = gnss_register_device(gdev);
514 if (ret)
515 goto err_disable_rpm;
517 return 0;
519 err_disable_rpm:
520 if (IS_ENABLED(CONFIG_PM))
521 pm_runtime_disable(dev);
522 else
523 sirf_runtime_suspend(dev);
524 err_free_irq:
525 if (data->wakeup)
526 free_irq(data->irq, data);
527 err_disable_vcc:
528 if (data->on_off)
529 regulator_disable(data->vcc);
530 err_put_device:
531 gnss_put_device(data->gdev);
533 return ret;
536 static void sirf_remove(struct serdev_device *serdev)
538 struct sirf_data *data = serdev_device_get_drvdata(serdev);
540 gnss_deregister_device(data->gdev);
542 if (IS_ENABLED(CONFIG_PM))
543 pm_runtime_disable(&serdev->dev);
544 else
545 sirf_runtime_suspend(&serdev->dev);
547 if (data->wakeup)
548 free_irq(data->irq, data);
550 if (data->on_off)
551 regulator_disable(data->vcc);
553 gnss_put_device(data->gdev);
556 #ifdef CONFIG_OF
557 static const struct of_device_id sirf_of_match[] = {
558 { .compatible = "fastrax,uc430" },
559 { .compatible = "linx,r4" },
560 { .compatible = "wi2wi,w2sg0004" },
561 { .compatible = "wi2wi,w2sg0008i" },
562 { .compatible = "wi2wi,w2sg0084i" },
565 MODULE_DEVICE_TABLE(of, sirf_of_match);
566 #endif
568 static struct serdev_device_driver sirf_driver = {
569 .driver = {
570 .name = "gnss-sirf",
571 .of_match_table = of_match_ptr(sirf_of_match),
572 .pm = &sirf_pm_ops,
574 .probe = sirf_probe,
575 .remove = sirf_remove,
577 module_serdev_device_driver(sirf_driver);
579 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
580 MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
581 MODULE_LICENSE("GPL v2");