1 // SPDX-License-Identifier: GPL-2.0
3 * SiRFstar GNSS receiver driver
5 * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
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>
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
39 struct gnss_device *gdev;
40 struct serdev_device *serdev;
42 struct regulator *vcc;
43 struct regulator *lna;
44 struct gpio_desc *on_off;
45 struct gpio_desc *wakeup;
49 struct mutex gdev_mutex;
52 struct mutex serdev_mutex;
55 wait_queue_head_t power_wait;
58 static int sirf_serdev_open(struct sirf_data *data)
62 mutex_lock(&data->serdev_mutex);
63 if (++data->serdev_count == 1) {
64 ret = serdev_device_open(data->serdev);
70 serdev_device_set_baudrate(data->serdev, data->speed);
71 serdev_device_set_flow_control(data->serdev, false);
75 mutex_unlock(&data->serdev_mutex);
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;
94 mutex_lock(&data->gdev_mutex);
96 mutex_unlock(&data->gdev_mutex);
98 ret = sirf_serdev_open(data);
100 mutex_lock(&data->gdev_mutex);
102 mutex_unlock(&data->gdev_mutex);
106 ret = pm_runtime_get_sync(&serdev->dev);
108 dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
109 pm_runtime_put_noidle(&serdev->dev);
116 sirf_serdev_close(data);
118 mutex_lock(&data->gdev_mutex);
120 mutex_unlock(&data->gdev_mutex);
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);
136 mutex_unlock(&data->gdev_mutex);
139 static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
142 struct sirf_data *data = gnss_get_drvdata(gdev);
143 struct serdev_device *serdev = data->serdev;
146 /* write is only buffered synchronously */
147 ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
148 if (ret < 0 || ret < count)
151 /* FIXME: determine if interrupted? */
152 serdev_device_wait_until_sent(serdev, 0);
157 static const struct gnss_operations sirf_gnss_ops = {
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;
170 if (!data->wakeup && !data->active) {
172 wake_up_interruptible(&data->power_wait);
175 mutex_lock(&data->gdev_mutex);
177 ret = gnss_insert_raw(gdev, buf, count);
178 mutex_unlock(&data->gdev_mutex);
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;
194 ret = gpiod_get_value_cansleep(data->wakeup);
195 dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
200 wake_up_interruptible(&data->power_wait);
205 static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
207 unsigned long timeout)
211 /* Wait for state change (including any shutdown messages). */
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));
221 if (ret > 0 && !active)
224 if (ret == 0 && active)
230 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
231 unsigned long timeout)
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));
244 dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
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;
266 timeout = SIRF_ACTIVATE_TIMEOUT;
268 timeout = SIRF_HIBERNATE_TIMEOUT;
271 ret = sirf_serdev_open(data);
277 sirf_pulse_on_off(data);
278 ret = sirf_wait_for_power_state(data, active, timeout);
279 } while (ret == -ETIMEDOUT && retries--);
282 sirf_serdev_close(data);
290 static int sirf_runtime_suspend(struct device *dev)
292 struct sirf_data *data = dev_get_drvdata(dev);
297 ret = sirf_set_active(data, false);
299 ret = regulator_disable(data->vcc);
304 ret = regulator_disable(data->lna);
312 ret2 = sirf_set_active(data, true);
314 ret2 = regulator_enable(data->vcc);
318 "failed to reenable power on failed suspend: %d\n",
324 static int sirf_runtime_resume(struct device *dev)
326 struct sirf_data *data = dev_get_drvdata(dev);
329 ret = regulator_enable(data->lna);
334 ret = sirf_set_active(data, true);
336 ret = regulator_enable(data->vcc);
339 goto err_disable_lna;
344 regulator_disable(data->lna);
349 static int __maybe_unused sirf_suspend(struct device *dev)
351 struct sirf_data *data = dev_get_drvdata(dev);
354 if (!pm_runtime_suspended(dev))
355 ret = sirf_runtime_suspend(dev);
358 disable_irq(data->irq);
363 static int __maybe_unused sirf_resume(struct device *dev)
365 struct sirf_data *data = dev_get_drvdata(dev);
369 enable_irq(data->irq);
371 if (!pm_runtime_suspended(dev))
372 ret = sirf_runtime_resume(dev);
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;
388 of_property_read_u32(node, "current-speed", &speed);
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;
402 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
406 gdev = gnss_allocate_device(dev);
410 gdev->type = GNSS_TYPE_SIRF;
411 gdev->ops = &sirf_gnss_ops;
412 gnss_set_drvdata(gdev, data);
414 data->serdev = serdev;
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);
428 data->vcc = devm_regulator_get(dev, "vcc");
429 if (IS_ERR(data->vcc)) {
430 ret = PTR_ERR(data->vcc);
434 data->lna = devm_regulator_get(dev, "lna");
435 if (IS_ERR(data->lna)) {
436 ret = PTR_ERR(data->lna);
440 data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
442 if (IS_ERR(data->on_off)) {
443 ret = PTR_ERR(data->on_off);
448 data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
450 if (IS_ERR(data->wakeup)) {
451 ret = PTR_ERR(data->wakeup);
455 ret = regulator_enable(data->vcc);
459 /* Wait for chip to boot into hibernate mode. */
460 msleep(SIRF_BOOT_DELAY);
464 ret = gpiod_get_value_cansleep(data->wakeup);
466 goto err_disable_vcc;
469 ret = gpiod_to_irq(data->wakeup);
471 goto err_disable_vcc;
474 ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
475 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
478 goto err_disable_vcc;
483 data->active = false;
485 ret = sirf_serdev_open(data);
487 goto err_disable_vcc;
489 msleep(SIRF_REPORT_CYCLE);
490 sirf_serdev_close(data);
493 /* Force hibernate mode if already active. */
495 ret = sirf_set_active(data, false);
497 dev_err(dev, "failed to set hibernate mode: %d\n",
504 if (IS_ENABLED(CONFIG_PM)) {
505 pm_runtime_set_suspended(dev); /* clear runtime_error flag */
506 pm_runtime_enable(dev);
508 ret = sirf_runtime_resume(dev);
513 ret = gnss_register_device(gdev);
515 goto err_disable_rpm;
520 if (IS_ENABLED(CONFIG_PM))
521 pm_runtime_disable(dev);
523 sirf_runtime_suspend(dev);
526 free_irq(data->irq, data);
529 regulator_disable(data->vcc);
531 gnss_put_device(data->gdev);
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);
545 sirf_runtime_suspend(&serdev->dev);
548 free_irq(data->irq, data);
551 regulator_disable(data->vcc);
553 gnss_put_device(data->gdev);
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);
568 static struct serdev_device_driver sirf_driver = {
571 .of_match_table = of_match_ptr(sirf_of_match),
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");