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
30 struct gnss_device
*gdev
;
31 struct serdev_device
*serdev
;
33 struct regulator
*vcc
;
34 struct gpio_desc
*on_off
;
35 struct gpio_desc
*wakeup
;
38 wait_queue_head_t power_wait
;
41 static int sirf_open(struct gnss_device
*gdev
)
43 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
44 struct serdev_device
*serdev
= data
->serdev
;
47 ret
= serdev_device_open(serdev
);
51 serdev_device_set_baudrate(serdev
, data
->speed
);
52 serdev_device_set_flow_control(serdev
, false);
54 ret
= pm_runtime_get_sync(&serdev
->dev
);
56 dev_err(&gdev
->dev
, "failed to runtime resume: %d\n", ret
);
57 pm_runtime_put_noidle(&serdev
->dev
);
64 serdev_device_close(serdev
);
69 static void sirf_close(struct gnss_device
*gdev
)
71 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
72 struct serdev_device
*serdev
= data
->serdev
;
74 serdev_device_close(serdev
);
76 pm_runtime_put(&serdev
->dev
);
79 static int sirf_write_raw(struct gnss_device
*gdev
, const unsigned char *buf
,
82 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
83 struct serdev_device
*serdev
= data
->serdev
;
86 /* write is only buffered synchronously */
87 ret
= serdev_device_write(serdev
, buf
, count
, MAX_SCHEDULE_TIMEOUT
);
91 /* FIXME: determine if interrupted? */
92 serdev_device_wait_until_sent(serdev
, 0);
97 static const struct gnss_operations sirf_gnss_ops
= {
100 .write_raw
= sirf_write_raw
,
103 static int sirf_receive_buf(struct serdev_device
*serdev
,
104 const unsigned char *buf
, size_t count
)
106 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
107 struct gnss_device
*gdev
= data
->gdev
;
109 return gnss_insert_raw(gdev
, buf
, count
);
112 static const struct serdev_device_ops sirf_serdev_ops
= {
113 .receive_buf
= sirf_receive_buf
,
114 .write_wakeup
= serdev_device_write_wakeup
,
117 static irqreturn_t
sirf_wakeup_handler(int irq
, void *dev_id
)
119 struct sirf_data
*data
= dev_id
;
120 struct device
*dev
= &data
->serdev
->dev
;
123 ret
= gpiod_get_value_cansleep(data
->wakeup
);
124 dev_dbg(dev
, "%s - wakeup = %d\n", __func__
, ret
);
128 data
->active
= !!ret
;
129 wake_up_interruptible(&data
->power_wait
);
134 static int sirf_wait_for_power_state(struct sirf_data
*data
, bool active
,
135 unsigned long timeout
)
139 ret
= wait_event_interruptible_timeout(data
->power_wait
,
140 data
->active
== active
, msecs_to_jiffies(timeout
));
145 dev_warn(&data
->serdev
->dev
, "timeout waiting for active state = %d\n",
153 static void sirf_pulse_on_off(struct sirf_data
*data
)
155 gpiod_set_value_cansleep(data
->on_off
, 1);
156 msleep(SIRF_ON_OFF_PULSE_TIME
);
157 gpiod_set_value_cansleep(data
->on_off
, 0);
160 static int sirf_set_active(struct sirf_data
*data
, bool active
)
162 unsigned long timeout
;
167 timeout
= SIRF_ACTIVATE_TIMEOUT
;
169 timeout
= SIRF_HIBERNATE_TIMEOUT
;
172 sirf_pulse_on_off(data
);
173 ret
= sirf_wait_for_power_state(data
, active
, timeout
);
175 if (ret
== -ETIMEDOUT
)
190 static int sirf_runtime_suspend(struct device
*dev
)
192 struct sirf_data
*data
= dev_get_drvdata(dev
);
195 return regulator_disable(data
->vcc
);
197 return sirf_set_active(data
, false);
200 static int sirf_runtime_resume(struct device
*dev
)
202 struct sirf_data
*data
= dev_get_drvdata(dev
);
205 return regulator_enable(data
->vcc
);
207 return sirf_set_active(data
, true);
210 static int __maybe_unused
sirf_suspend(struct device
*dev
)
212 struct sirf_data
*data
= dev_get_drvdata(dev
);
215 if (!pm_runtime_suspended(dev
))
216 ret
= sirf_runtime_suspend(dev
);
219 disable_irq(data
->irq
);
224 static int __maybe_unused
sirf_resume(struct device
*dev
)
226 struct sirf_data
*data
= dev_get_drvdata(dev
);
230 enable_irq(data
->irq
);
232 if (!pm_runtime_suspended(dev
))
233 ret
= sirf_runtime_resume(dev
);
238 static const struct dev_pm_ops sirf_pm_ops
= {
239 SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend
, sirf_resume
)
240 SET_RUNTIME_PM_OPS(sirf_runtime_suspend
, sirf_runtime_resume
, NULL
)
243 static int sirf_parse_dt(struct serdev_device
*serdev
)
245 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
246 struct device_node
*node
= serdev
->dev
.of_node
;
249 of_property_read_u32(node
, "current-speed", &speed
);
256 static int sirf_probe(struct serdev_device
*serdev
)
258 struct device
*dev
= &serdev
->dev
;
259 struct gnss_device
*gdev
;
260 struct sirf_data
*data
;
263 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
267 gdev
= gnss_allocate_device(dev
);
271 gdev
->type
= GNSS_TYPE_SIRF
;
272 gdev
->ops
= &sirf_gnss_ops
;
273 gnss_set_drvdata(gdev
, data
);
275 data
->serdev
= serdev
;
278 init_waitqueue_head(&data
->power_wait
);
280 serdev_device_set_drvdata(serdev
, data
);
281 serdev_device_set_client_ops(serdev
, &sirf_serdev_ops
);
283 ret
= sirf_parse_dt(serdev
);
287 data
->vcc
= devm_regulator_get(dev
, "vcc");
288 if (IS_ERR(data
->vcc
)) {
289 ret
= PTR_ERR(data
->vcc
);
293 data
->on_off
= devm_gpiod_get_optional(dev
, "sirf,onoff",
295 if (IS_ERR(data
->on_off
)) {
296 ret
= PTR_ERR(data
->on_off
);
301 data
->wakeup
= devm_gpiod_get_optional(dev
, "sirf,wakeup",
303 if (IS_ERR(data
->wakeup
)) {
304 ret
= PTR_ERR(data
->wakeup
);
309 * Configurations where WAKEUP has been left not connected,
310 * are currently not supported.
313 dev_err(dev
, "no wakeup gpio specified\n");
318 ret
= regulator_enable(data
->vcc
);
322 /* Wait for chip to boot into hibernate mode. */
323 msleep(SIRF_BOOT_DELAY
);
327 ret
= gpiod_to_irq(data
->wakeup
);
329 goto err_disable_vcc
;
332 ret
= request_threaded_irq(data
->irq
, NULL
, sirf_wakeup_handler
,
333 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
336 goto err_disable_vcc
;
339 if (IS_ENABLED(CONFIG_PM
)) {
340 pm_runtime_set_suspended(dev
); /* clear runtime_error flag */
341 pm_runtime_enable(dev
);
343 ret
= sirf_runtime_resume(dev
);
348 ret
= gnss_register_device(gdev
);
350 goto err_disable_rpm
;
355 if (IS_ENABLED(CONFIG_PM
))
356 pm_runtime_disable(dev
);
358 sirf_runtime_suspend(dev
);
361 free_irq(data
->irq
, data
);
364 regulator_disable(data
->vcc
);
366 gnss_put_device(data
->gdev
);
371 static void sirf_remove(struct serdev_device
*serdev
)
373 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
375 gnss_deregister_device(data
->gdev
);
377 if (IS_ENABLED(CONFIG_PM
))
378 pm_runtime_disable(&serdev
->dev
);
380 sirf_runtime_suspend(&serdev
->dev
);
383 free_irq(data
->irq
, data
);
386 regulator_disable(data
->vcc
);
388 gnss_put_device(data
->gdev
);
392 static const struct of_device_id sirf_of_match
[] = {
393 { .compatible
= "fastrax,uc430" },
394 { .compatible
= "linx,r4" },
395 { .compatible
= "wi2wi,w2sg0008i" },
396 { .compatible
= "wi2wi,w2sg0084i" },
399 MODULE_DEVICE_TABLE(of
, sirf_of_match
);
402 static struct serdev_device_driver sirf_driver
= {
405 .of_match_table
= of_match_ptr(sirf_of_match
),
409 .remove
= sirf_remove
,
411 module_serdev_device_driver(sirf_driver
);
413 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
414 MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
415 MODULE_LICENSE("GPL v2");