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/serdev.h>
20 #include <linux/slab.h>
21 #include <linux/wait.h>
23 #define SIRF_BOOT_DELAY 500
24 #define SIRF_ON_OFF_PULSE_TIME 100
25 #define SIRF_ACTIVATE_TIMEOUT 200
26 #define SIRF_HIBERNATE_TIMEOUT 200
29 struct gnss_device
*gdev
;
30 struct serdev_device
*serdev
;
32 struct regulator
*vcc
;
33 struct gpio_desc
*on_off
;
34 struct gpio_desc
*wakeup
;
37 wait_queue_head_t power_wait
;
40 static int sirf_open(struct gnss_device
*gdev
)
42 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
43 struct serdev_device
*serdev
= data
->serdev
;
46 ret
= serdev_device_open(serdev
);
50 serdev_device_set_baudrate(serdev
, data
->speed
);
51 serdev_device_set_flow_control(serdev
, false);
53 ret
= pm_runtime_get_sync(&serdev
->dev
);
55 dev_err(&gdev
->dev
, "failed to runtime resume: %d\n", ret
);
56 pm_runtime_put_noidle(&serdev
->dev
);
63 serdev_device_close(serdev
);
68 static void sirf_close(struct gnss_device
*gdev
)
70 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
71 struct serdev_device
*serdev
= data
->serdev
;
73 serdev_device_close(serdev
);
75 pm_runtime_put(&serdev
->dev
);
78 static int sirf_write_raw(struct gnss_device
*gdev
, const unsigned char *buf
,
81 struct sirf_data
*data
= gnss_get_drvdata(gdev
);
82 struct serdev_device
*serdev
= data
->serdev
;
85 /* write is only buffered synchronously */
86 ret
= serdev_device_write(serdev
, buf
, count
, 0);
90 /* FIXME: determine if interrupted? */
91 serdev_device_wait_until_sent(serdev
, 0);
96 static const struct gnss_operations sirf_gnss_ops
= {
99 .write_raw
= sirf_write_raw
,
102 static int sirf_receive_buf(struct serdev_device
*serdev
,
103 const unsigned char *buf
, size_t count
)
105 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
106 struct gnss_device
*gdev
= data
->gdev
;
108 return gnss_insert_raw(gdev
, buf
, count
);
111 static const struct serdev_device_ops sirf_serdev_ops
= {
112 .receive_buf
= sirf_receive_buf
,
113 .write_wakeup
= serdev_device_write_wakeup
,
116 static irqreturn_t
sirf_wakeup_handler(int irq
, void *dev_id
)
118 struct sirf_data
*data
= dev_id
;
119 struct device
*dev
= &data
->serdev
->dev
;
122 ret
= gpiod_get_value_cansleep(data
->wakeup
);
123 dev_dbg(dev
, "%s - wakeup = %d\n", __func__
, ret
);
127 data
->active
= !!ret
;
128 wake_up_interruptible(&data
->power_wait
);
133 static int sirf_wait_for_power_state(struct sirf_data
*data
, bool active
,
134 unsigned long timeout
)
138 ret
= wait_event_interruptible_timeout(data
->power_wait
,
139 data
->active
== active
, msecs_to_jiffies(timeout
));
144 dev_warn(&data
->serdev
->dev
, "timeout waiting for active state = %d\n",
152 static void sirf_pulse_on_off(struct sirf_data
*data
)
154 gpiod_set_value_cansleep(data
->on_off
, 1);
155 msleep(SIRF_ON_OFF_PULSE_TIME
);
156 gpiod_set_value_cansleep(data
->on_off
, 0);
159 static int sirf_set_active(struct sirf_data
*data
, bool active
)
161 unsigned long timeout
;
166 timeout
= SIRF_ACTIVATE_TIMEOUT
;
168 timeout
= SIRF_HIBERNATE_TIMEOUT
;
170 while (retries
-- > 0) {
171 sirf_pulse_on_off(data
);
172 ret
= sirf_wait_for_power_state(data
, active
, timeout
);
174 if (ret
== -ETIMEDOUT
)
189 static int sirf_runtime_suspend(struct device
*dev
)
191 struct sirf_data
*data
= dev_get_drvdata(dev
);
194 return regulator_disable(data
->vcc
);
196 return sirf_set_active(data
, false);
199 static int sirf_runtime_resume(struct device
*dev
)
201 struct sirf_data
*data
= dev_get_drvdata(dev
);
204 return regulator_enable(data
->vcc
);
206 return sirf_set_active(data
, true);
209 static int __maybe_unused
sirf_suspend(struct device
*dev
)
211 struct sirf_data
*data
= dev_get_drvdata(dev
);
214 if (!pm_runtime_suspended(dev
))
215 ret
= sirf_runtime_suspend(dev
);
218 disable_irq(data
->irq
);
223 static int __maybe_unused
sirf_resume(struct device
*dev
)
225 struct sirf_data
*data
= dev_get_drvdata(dev
);
229 enable_irq(data
->irq
);
231 if (!pm_runtime_suspended(dev
))
232 ret
= sirf_runtime_resume(dev
);
237 static const struct dev_pm_ops sirf_pm_ops
= {
238 SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend
, sirf_resume
)
239 SET_RUNTIME_PM_OPS(sirf_runtime_suspend
, sirf_runtime_resume
, NULL
)
242 static int sirf_parse_dt(struct serdev_device
*serdev
)
244 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
245 struct device_node
*node
= serdev
->dev
.of_node
;
248 of_property_read_u32(node
, "current-speed", &speed
);
255 static int sirf_probe(struct serdev_device
*serdev
)
257 struct device
*dev
= &serdev
->dev
;
258 struct gnss_device
*gdev
;
259 struct sirf_data
*data
;
262 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
266 gdev
= gnss_allocate_device(dev
);
270 gdev
->type
= GNSS_TYPE_SIRF
;
271 gdev
->ops
= &sirf_gnss_ops
;
272 gnss_set_drvdata(gdev
, data
);
274 data
->serdev
= serdev
;
277 init_waitqueue_head(&data
->power_wait
);
279 serdev_device_set_drvdata(serdev
, data
);
280 serdev_device_set_client_ops(serdev
, &sirf_serdev_ops
);
282 ret
= sirf_parse_dt(serdev
);
286 data
->vcc
= devm_regulator_get(dev
, "vcc");
287 if (IS_ERR(data
->vcc
)) {
288 ret
= PTR_ERR(data
->vcc
);
292 data
->on_off
= devm_gpiod_get_optional(dev
, "sirf,onoff",
294 if (IS_ERR(data
->on_off
))
298 data
->wakeup
= devm_gpiod_get_optional(dev
, "sirf,wakeup",
300 if (IS_ERR(data
->wakeup
))
304 * Configurations where WAKEUP has been left not connected,
305 * are currently not supported.
308 dev_err(dev
, "no wakeup gpio specified\n");
315 ret
= gpiod_to_irq(data
->wakeup
);
321 ret
= devm_request_threaded_irq(dev
, data
->irq
, NULL
,
323 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
330 ret
= regulator_enable(data
->vcc
);
334 /* Wait for chip to boot into hibernate mode */
335 msleep(SIRF_BOOT_DELAY
);
338 if (IS_ENABLED(CONFIG_PM
)) {
339 pm_runtime_set_suspended(dev
); /* clear runtime_error flag */
340 pm_runtime_enable(dev
);
342 ret
= sirf_runtime_resume(dev
);
344 goto err_disable_vcc
;
347 ret
= gnss_register_device(gdev
);
349 goto err_disable_rpm
;
354 if (IS_ENABLED(CONFIG_PM
))
355 pm_runtime_disable(dev
);
357 sirf_runtime_suspend(dev
);
360 regulator_disable(data
->vcc
);
362 gnss_put_device(data
->gdev
);
367 static void sirf_remove(struct serdev_device
*serdev
)
369 struct sirf_data
*data
= serdev_device_get_drvdata(serdev
);
371 gnss_deregister_device(data
->gdev
);
373 if (IS_ENABLED(CONFIG_PM
))
374 pm_runtime_disable(&serdev
->dev
);
376 sirf_runtime_suspend(&serdev
->dev
);
379 regulator_disable(data
->vcc
);
381 gnss_put_device(data
->gdev
);
385 static const struct of_device_id sirf_of_match
[] = {
386 { .compatible
= "fastrax,uc430" },
387 { .compatible
= "linx,r4" },
388 { .compatible
= "wi2wi,w2sg0008i" },
389 { .compatible
= "wi2wi,w2sg0084i" },
392 MODULE_DEVICE_TABLE(of
, sirf_of_match
);
395 static struct serdev_device_driver sirf_driver
= {
398 .of_match_table
= of_match_ptr(sirf_of_match
),
402 .remove
= sirf_remove
,
404 module_serdev_device_driver(sirf_driver
);
406 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
407 MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
408 MODULE_LICENSE("GPL v2");