Merge tag 'drm-next-2018-11-02' of git://anongit.freedesktop.org/drm/drm
[linux/fpc-iii.git] / drivers / gnss / sirf.c
blob79cb98950013bbb60f4ff4126cd562453441045a
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/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
28 struct sirf_data {
29 struct gnss_device *gdev;
30 struct serdev_device *serdev;
31 speed_t speed;
32 struct regulator *vcc;
33 struct gpio_desc *on_off;
34 struct gpio_desc *wakeup;
35 int irq;
36 bool active;
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;
44 int ret;
46 ret = serdev_device_open(serdev);
47 if (ret)
48 return ret;
50 serdev_device_set_baudrate(serdev, data->speed);
51 serdev_device_set_flow_control(serdev, false);
53 ret = pm_runtime_get_sync(&serdev->dev);
54 if (ret < 0) {
55 dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
56 pm_runtime_put_noidle(&serdev->dev);
57 goto err_close;
60 return 0;
62 err_close:
63 serdev_device_close(serdev);
65 return ret;
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,
79 size_t count)
81 struct sirf_data *data = gnss_get_drvdata(gdev);
82 struct serdev_device *serdev = data->serdev;
83 int ret;
85 /* write is only buffered synchronously */
86 ret = serdev_device_write(serdev, buf, count, 0);
87 if (ret < 0)
88 return ret;
90 /* FIXME: determine if interrupted? */
91 serdev_device_wait_until_sent(serdev, 0);
93 return count;
96 static const struct gnss_operations sirf_gnss_ops = {
97 .open = sirf_open,
98 .close = sirf_close,
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;
120 int ret;
122 ret = gpiod_get_value_cansleep(data->wakeup);
123 dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
124 if (ret < 0)
125 goto out;
127 data->active = !!ret;
128 wake_up_interruptible(&data->power_wait);
129 out:
130 return IRQ_HANDLED;
133 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
134 unsigned long timeout)
136 int ret;
138 ret = wait_event_interruptible_timeout(data->power_wait,
139 data->active == active, msecs_to_jiffies(timeout));
140 if (ret < 0)
141 return ret;
143 if (ret == 0) {
144 dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
145 active);
146 return -ETIMEDOUT;
149 return 0;
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;
162 int retries = 3;
163 int ret;
165 if (active)
166 timeout = SIRF_ACTIVATE_TIMEOUT;
167 else
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);
173 if (ret < 0) {
174 if (ret == -ETIMEDOUT)
175 continue;
177 return ret;
180 break;
183 if (retries == 0)
184 return -ETIMEDOUT;
186 return 0;
189 static int sirf_runtime_suspend(struct device *dev)
191 struct sirf_data *data = dev_get_drvdata(dev);
193 if (!data->on_off)
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);
203 if (!data->on_off)
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);
212 int ret = 0;
214 if (!pm_runtime_suspended(dev))
215 ret = sirf_runtime_suspend(dev);
217 if (data->wakeup)
218 disable_irq(data->irq);
220 return ret;
223 static int __maybe_unused sirf_resume(struct device *dev)
225 struct sirf_data *data = dev_get_drvdata(dev);
226 int ret = 0;
228 if (data->wakeup)
229 enable_irq(data->irq);
231 if (!pm_runtime_suspended(dev))
232 ret = sirf_runtime_resume(dev);
234 return ret;
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;
246 u32 speed = 9600;
248 of_property_read_u32(node, "current-speed", &speed);
250 data->speed = speed;
252 return 0;
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;
260 int ret;
262 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
263 if (!data)
264 return -ENOMEM;
266 gdev = gnss_allocate_device(dev);
267 if (!gdev)
268 return -ENOMEM;
270 gdev->type = GNSS_TYPE_SIRF;
271 gdev->ops = &sirf_gnss_ops;
272 gnss_set_drvdata(gdev, data);
274 data->serdev = serdev;
275 data->gdev = gdev;
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);
283 if (ret)
284 goto err_put_device;
286 data->vcc = devm_regulator_get(dev, "vcc");
287 if (IS_ERR(data->vcc)) {
288 ret = PTR_ERR(data->vcc);
289 goto err_put_device;
292 data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
293 GPIOD_OUT_LOW);
294 if (IS_ERR(data->on_off))
295 goto err_put_device;
297 if (data->on_off) {
298 data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
299 GPIOD_IN);
300 if (IS_ERR(data->wakeup))
301 goto err_put_device;
304 * Configurations where WAKEUP has been left not connected,
305 * are currently not supported.
307 if (!data->wakeup) {
308 dev_err(dev, "no wakeup gpio specified\n");
309 ret = -ENODEV;
310 goto err_put_device;
314 if (data->wakeup) {
315 ret = gpiod_to_irq(data->wakeup);
316 if (ret < 0)
317 goto err_put_device;
319 data->irq = ret;
321 ret = devm_request_threaded_irq(dev, data->irq, NULL,
322 sirf_wakeup_handler,
323 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
324 "wakeup", data);
325 if (ret)
326 goto err_put_device;
329 if (data->on_off) {
330 ret = regulator_enable(data->vcc);
331 if (ret)
332 goto err_put_device;
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);
341 } else {
342 ret = sirf_runtime_resume(dev);
343 if (ret < 0)
344 goto err_disable_vcc;
347 ret = gnss_register_device(gdev);
348 if (ret)
349 goto err_disable_rpm;
351 return 0;
353 err_disable_rpm:
354 if (IS_ENABLED(CONFIG_PM))
355 pm_runtime_disable(dev);
356 else
357 sirf_runtime_suspend(dev);
358 err_disable_vcc:
359 if (data->on_off)
360 regulator_disable(data->vcc);
361 err_put_device:
362 gnss_put_device(data->gdev);
364 return ret;
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);
375 else
376 sirf_runtime_suspend(&serdev->dev);
378 if (data->on_off)
379 regulator_disable(data->vcc);
381 gnss_put_device(data->gdev);
384 #ifdef CONFIG_OF
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);
393 #endif
395 static struct serdev_device_driver sirf_driver = {
396 .driver = {
397 .name = "gnss-sirf",
398 .of_match_table = of_match_ptr(sirf_of_match),
399 .pm = &sirf_pm_ops,
401 .probe = sirf_probe,
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");