xref: /openbmc/linux/drivers/gnss/sirf.c (revision 176f011b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SiRFstar GNSS receiver driver
4  *
5  * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
6  */
7 
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>
23 
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
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
37 
38 struct sirf_data {
39 	struct gnss_device *gdev;
40 	struct serdev_device *serdev;
41 	speed_t	speed;
42 	struct regulator *vcc;
43 	struct gpio_desc *on_off;
44 	struct gpio_desc *wakeup;
45 	int irq;
46 	bool active;
47 
48 	struct mutex gdev_mutex;
49 	bool open;
50 
51 	struct mutex serdev_mutex;
52 	int serdev_count;
53 
54 	wait_queue_head_t power_wait;
55 };
56 
57 static int sirf_serdev_open(struct sirf_data *data)
58 {
59 	int ret = 0;
60 
61 	mutex_lock(&data->serdev_mutex);
62 	if (++data->serdev_count == 1) {
63 		ret = serdev_device_open(data->serdev);
64 		if (ret) {
65 			data->serdev_count--;
66 			goto out_unlock;
67 		}
68 
69 		serdev_device_set_baudrate(data->serdev, data->speed);
70 		serdev_device_set_flow_control(data->serdev, false);
71 	}
72 
73 out_unlock:
74 	mutex_unlock(&data->serdev_mutex);
75 
76 	return ret;
77 }
78 
79 static void sirf_serdev_close(struct sirf_data *data)
80 {
81 	mutex_lock(&data->serdev_mutex);
82 	if (--data->serdev_count == 0)
83 		serdev_device_close(data->serdev);
84 	mutex_unlock(&data->serdev_mutex);
85 }
86 
87 static int sirf_open(struct gnss_device *gdev)
88 {
89 	struct sirf_data *data = gnss_get_drvdata(gdev);
90 	struct serdev_device *serdev = data->serdev;
91 	int ret;
92 
93 	mutex_lock(&data->gdev_mutex);
94 	data->open = true;
95 	mutex_unlock(&data->gdev_mutex);
96 
97 	ret = sirf_serdev_open(data);
98 	if (ret) {
99 		mutex_lock(&data->gdev_mutex);
100 		data->open = false;
101 		mutex_unlock(&data->gdev_mutex);
102 		return ret;
103 	}
104 
105 	ret = pm_runtime_get_sync(&serdev->dev);
106 	if (ret < 0) {
107 		dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
108 		pm_runtime_put_noidle(&serdev->dev);
109 		goto err_close;
110 	}
111 
112 	return 0;
113 
114 err_close:
115 	sirf_serdev_close(data);
116 
117 	mutex_lock(&data->gdev_mutex);
118 	data->open = false;
119 	mutex_unlock(&data->gdev_mutex);
120 
121 	return ret;
122 }
123 
124 static void sirf_close(struct gnss_device *gdev)
125 {
126 	struct sirf_data *data = gnss_get_drvdata(gdev);
127 	struct serdev_device *serdev = data->serdev;
128 
129 	sirf_serdev_close(data);
130 
131 	pm_runtime_put(&serdev->dev);
132 
133 	mutex_lock(&data->gdev_mutex);
134 	data->open = false;
135 	mutex_unlock(&data->gdev_mutex);
136 }
137 
138 static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
139 				size_t count)
140 {
141 	struct sirf_data *data = gnss_get_drvdata(gdev);
142 	struct serdev_device *serdev = data->serdev;
143 	int ret;
144 
145 	/* write is only buffered synchronously */
146 	ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
147 	if (ret < 0 || ret < count)
148 		return ret;
149 
150 	/* FIXME: determine if interrupted? */
151 	serdev_device_wait_until_sent(serdev, 0);
152 
153 	return count;
154 }
155 
156 static const struct gnss_operations sirf_gnss_ops = {
157 	.open		= sirf_open,
158 	.close		= sirf_close,
159 	.write_raw	= sirf_write_raw,
160 };
161 
162 static int sirf_receive_buf(struct serdev_device *serdev,
163 				const unsigned char *buf, size_t count)
164 {
165 	struct sirf_data *data = serdev_device_get_drvdata(serdev);
166 	struct gnss_device *gdev = data->gdev;
167 	int ret = 0;
168 
169 	if (!data->wakeup && !data->active) {
170 		data->active = true;
171 		wake_up_interruptible(&data->power_wait);
172 	}
173 
174 	mutex_lock(&data->gdev_mutex);
175 	if (data->open)
176 		ret = gnss_insert_raw(gdev, buf, count);
177 	mutex_unlock(&data->gdev_mutex);
178 
179 	return ret;
180 }
181 
182 static const struct serdev_device_ops sirf_serdev_ops = {
183 	.receive_buf	= sirf_receive_buf,
184 	.write_wakeup	= serdev_device_write_wakeup,
185 };
186 
187 static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id)
188 {
189 	struct sirf_data *data = dev_id;
190 	struct device *dev = &data->serdev->dev;
191 	int ret;
192 
193 	ret = gpiod_get_value_cansleep(data->wakeup);
194 	dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
195 	if (ret < 0)
196 		goto out;
197 
198 	data->active = ret;
199 	wake_up_interruptible(&data->power_wait);
200 out:
201 	return IRQ_HANDLED;
202 }
203 
204 static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
205 						bool active,
206 						unsigned long timeout)
207 {
208 	int ret;
209 
210 	/* Wait for state change (including any shutdown messages). */
211 	msleep(timeout);
212 
213 	/* Wait for data reception or timeout. */
214 	data->active = false;
215 	ret = wait_event_interruptible_timeout(data->power_wait,
216 			data->active, msecs_to_jiffies(SIRF_REPORT_CYCLE));
217 	if (ret < 0)
218 		return ret;
219 
220 	if (ret > 0 && !active)
221 		return -ETIMEDOUT;
222 
223 	if (ret == 0 && active)
224 		return -ETIMEDOUT;
225 
226 	return 0;
227 }
228 
229 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
230 					unsigned long timeout)
231 {
232 	int ret;
233 
234 	if (!data->wakeup)
235 		return sirf_wait_for_power_state_nowakeup(data, active, timeout);
236 
237 	ret = wait_event_interruptible_timeout(data->power_wait,
238 			data->active == active, msecs_to_jiffies(timeout));
239 	if (ret < 0)
240 		return ret;
241 
242 	if (ret == 0) {
243 		dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
244 				active);
245 		return -ETIMEDOUT;
246 	}
247 
248 	return 0;
249 }
250 
251 static void sirf_pulse_on_off(struct sirf_data *data)
252 {
253 	gpiod_set_value_cansleep(data->on_off, 1);
254 	msleep(SIRF_ON_OFF_PULSE_TIME);
255 	gpiod_set_value_cansleep(data->on_off, 0);
256 }
257 
258 static int sirf_set_active(struct sirf_data *data, bool active)
259 {
260 	unsigned long timeout;
261 	int retries = 3;
262 	int ret;
263 
264 	if (active)
265 		timeout = SIRF_ACTIVATE_TIMEOUT;
266 	else
267 		timeout = SIRF_HIBERNATE_TIMEOUT;
268 
269 	if (!data->wakeup) {
270 		ret = sirf_serdev_open(data);
271 		if (ret)
272 			return ret;
273 	}
274 
275 	do {
276 		sirf_pulse_on_off(data);
277 		ret = sirf_wait_for_power_state(data, active, timeout);
278 	} while (ret == -ETIMEDOUT && retries--);
279 
280 	if (!data->wakeup)
281 		sirf_serdev_close(data);
282 
283 	if (ret)
284 		return ret;
285 
286 	return 0;
287 }
288 
289 static int sirf_runtime_suspend(struct device *dev)
290 {
291 	struct sirf_data *data = dev_get_drvdata(dev);
292 
293 	if (!data->on_off)
294 		return regulator_disable(data->vcc);
295 
296 	return sirf_set_active(data, false);
297 }
298 
299 static int sirf_runtime_resume(struct device *dev)
300 {
301 	struct sirf_data *data = dev_get_drvdata(dev);
302 
303 	if (!data->on_off)
304 		return regulator_enable(data->vcc);
305 
306 	return sirf_set_active(data, true);
307 }
308 
309 static int __maybe_unused sirf_suspend(struct device *dev)
310 {
311 	struct sirf_data *data = dev_get_drvdata(dev);
312 	int ret = 0;
313 
314 	if (!pm_runtime_suspended(dev))
315 		ret = sirf_runtime_suspend(dev);
316 
317 	if (data->wakeup)
318 		disable_irq(data->irq);
319 
320 	return ret;
321 }
322 
323 static int __maybe_unused sirf_resume(struct device *dev)
324 {
325 	struct sirf_data *data = dev_get_drvdata(dev);
326 	int ret = 0;
327 
328 	if (data->wakeup)
329 		enable_irq(data->irq);
330 
331 	if (!pm_runtime_suspended(dev))
332 		ret = sirf_runtime_resume(dev);
333 
334 	return ret;
335 }
336 
337 static const struct dev_pm_ops sirf_pm_ops = {
338 	SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend, sirf_resume)
339 	SET_RUNTIME_PM_OPS(sirf_runtime_suspend, sirf_runtime_resume, NULL)
340 };
341 
342 static int sirf_parse_dt(struct serdev_device *serdev)
343 {
344 	struct sirf_data *data = serdev_device_get_drvdata(serdev);
345 	struct device_node *node = serdev->dev.of_node;
346 	u32 speed = 9600;
347 
348 	of_property_read_u32(node, "current-speed", &speed);
349 
350 	data->speed = speed;
351 
352 	return 0;
353 }
354 
355 static int sirf_probe(struct serdev_device *serdev)
356 {
357 	struct device *dev = &serdev->dev;
358 	struct gnss_device *gdev;
359 	struct sirf_data *data;
360 	int ret;
361 
362 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
363 	if (!data)
364 		return -ENOMEM;
365 
366 	gdev = gnss_allocate_device(dev);
367 	if (!gdev)
368 		return -ENOMEM;
369 
370 	gdev->type = GNSS_TYPE_SIRF;
371 	gdev->ops = &sirf_gnss_ops;
372 	gnss_set_drvdata(gdev, data);
373 
374 	data->serdev = serdev;
375 	data->gdev = gdev;
376 
377 	mutex_init(&data->gdev_mutex);
378 	mutex_init(&data->serdev_mutex);
379 	init_waitqueue_head(&data->power_wait);
380 
381 	serdev_device_set_drvdata(serdev, data);
382 	serdev_device_set_client_ops(serdev, &sirf_serdev_ops);
383 
384 	ret = sirf_parse_dt(serdev);
385 	if (ret)
386 		goto err_put_device;
387 
388 	data->vcc = devm_regulator_get(dev, "vcc");
389 	if (IS_ERR(data->vcc)) {
390 		ret = PTR_ERR(data->vcc);
391 		goto err_put_device;
392 	}
393 
394 	data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
395 			GPIOD_OUT_LOW);
396 	if (IS_ERR(data->on_off))
397 		goto err_put_device;
398 
399 	if (data->on_off) {
400 		data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
401 				GPIOD_IN);
402 		if (IS_ERR(data->wakeup))
403 			goto err_put_device;
404 
405 		ret = regulator_enable(data->vcc);
406 		if (ret)
407 			goto err_put_device;
408 
409 		/* Wait for chip to boot into hibernate mode. */
410 		msleep(SIRF_BOOT_DELAY);
411 	}
412 
413 	if (data->wakeup) {
414 		ret = gpiod_get_value_cansleep(data->wakeup);
415 		if (ret < 0)
416 			goto err_disable_vcc;
417 		data->active = ret;
418 
419 		ret = gpiod_to_irq(data->wakeup);
420 		if (ret < 0)
421 			goto err_disable_vcc;
422 		data->irq = ret;
423 
424 		ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
425 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
426 				"wakeup", data);
427 		if (ret)
428 			goto err_disable_vcc;
429 	}
430 
431 	if (data->on_off) {
432 		if (!data->wakeup) {
433 			data->active = false;
434 
435 			ret = sirf_serdev_open(data);
436 			if (ret)
437 				goto err_disable_vcc;
438 
439 			msleep(SIRF_REPORT_CYCLE);
440 			sirf_serdev_close(data);
441 		}
442 
443 		/* Force hibernate mode if already active. */
444 		if (data->active) {
445 			ret = sirf_set_active(data, false);
446 			if (ret) {
447 				dev_err(dev, "failed to set hibernate mode: %d\n",
448 						ret);
449 				goto err_free_irq;
450 			}
451 		}
452 	}
453 
454 	if (IS_ENABLED(CONFIG_PM)) {
455 		pm_runtime_set_suspended(dev);	/* clear runtime_error flag */
456 		pm_runtime_enable(dev);
457 	} else {
458 		ret = sirf_runtime_resume(dev);
459 		if (ret < 0)
460 			goto err_free_irq;
461 	}
462 
463 	ret = gnss_register_device(gdev);
464 	if (ret)
465 		goto err_disable_rpm;
466 
467 	return 0;
468 
469 err_disable_rpm:
470 	if (IS_ENABLED(CONFIG_PM))
471 		pm_runtime_disable(dev);
472 	else
473 		sirf_runtime_suspend(dev);
474 err_free_irq:
475 	if (data->wakeup)
476 		free_irq(data->irq, data);
477 err_disable_vcc:
478 	if (data->on_off)
479 		regulator_disable(data->vcc);
480 err_put_device:
481 	gnss_put_device(data->gdev);
482 
483 	return ret;
484 }
485 
486 static void sirf_remove(struct serdev_device *serdev)
487 {
488 	struct sirf_data *data = serdev_device_get_drvdata(serdev);
489 
490 	gnss_deregister_device(data->gdev);
491 
492 	if (IS_ENABLED(CONFIG_PM))
493 		pm_runtime_disable(&serdev->dev);
494 	else
495 		sirf_runtime_suspend(&serdev->dev);
496 
497 	if (data->wakeup)
498 		free_irq(data->irq, data);
499 
500 	if (data->on_off)
501 		regulator_disable(data->vcc);
502 
503 	gnss_put_device(data->gdev);
504 };
505 
506 #ifdef CONFIG_OF
507 static const struct of_device_id sirf_of_match[] = {
508 	{ .compatible = "fastrax,uc430" },
509 	{ .compatible = "linx,r4" },
510 	{ .compatible = "wi2wi,w2sg0004" },
511 	{ .compatible = "wi2wi,w2sg0008i" },
512 	{ .compatible = "wi2wi,w2sg0084i" },
513 	{},
514 };
515 MODULE_DEVICE_TABLE(of, sirf_of_match);
516 #endif
517 
518 static struct serdev_device_driver sirf_driver = {
519 	.driver	= {
520 		.name		= "gnss-sirf",
521 		.of_match_table	= of_match_ptr(sirf_of_match),
522 		.pm		= &sirf_pm_ops,
523 	},
524 	.probe	= sirf_probe,
525 	.remove	= sirf_remove,
526 };
527 module_serdev_device_driver(sirf_driver);
528 
529 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
530 MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
531 MODULE_LICENSE("GPL v2");
532