Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 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> |
Johan Hovold | 466720d | 2018-11-14 09:37:54 +0100 | [diff] [blame] | 19 | #include <linux/sched.h> |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 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 | struct sirf_data { |
| 30 | struct gnss_device *gdev; |
| 31 | struct serdev_device *serdev; |
| 32 | speed_t speed; |
| 33 | struct regulator *vcc; |
| 34 | struct gpio_desc *on_off; |
| 35 | struct gpio_desc *wakeup; |
| 36 | int irq; |
| 37 | bool active; |
| 38 | wait_queue_head_t power_wait; |
| 39 | }; |
| 40 | |
| 41 | static int sirf_open(struct gnss_device *gdev) |
| 42 | { |
| 43 | struct sirf_data *data = gnss_get_drvdata(gdev); |
| 44 | struct serdev_device *serdev = data->serdev; |
| 45 | int ret; |
| 46 | |
| 47 | ret = serdev_device_open(serdev); |
| 48 | if (ret) |
| 49 | return ret; |
| 50 | |
| 51 | serdev_device_set_baudrate(serdev, data->speed); |
| 52 | serdev_device_set_flow_control(serdev, false); |
| 53 | |
| 54 | ret = pm_runtime_get_sync(&serdev->dev); |
| 55 | if (ret < 0) { |
| 56 | dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret); |
| 57 | pm_runtime_put_noidle(&serdev->dev); |
| 58 | goto err_close; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | |
| 63 | err_close: |
| 64 | serdev_device_close(serdev); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | static void sirf_close(struct gnss_device *gdev) |
| 70 | { |
| 71 | struct sirf_data *data = gnss_get_drvdata(gdev); |
| 72 | struct serdev_device *serdev = data->serdev; |
| 73 | |
| 74 | serdev_device_close(serdev); |
| 75 | |
| 76 | pm_runtime_put(&serdev->dev); |
| 77 | } |
| 78 | |
| 79 | static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf, |
| 80 | size_t count) |
| 81 | { |
| 82 | struct sirf_data *data = gnss_get_drvdata(gdev); |
| 83 | struct serdev_device *serdev = data->serdev; |
| 84 | int ret; |
| 85 | |
| 86 | /* write is only buffered synchronously */ |
Johan Hovold | 466720d | 2018-11-14 09:37:54 +0100 | [diff] [blame] | 87 | ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT); |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 88 | if (ret < 0) |
| 89 | return ret; |
| 90 | |
| 91 | /* FIXME: determine if interrupted? */ |
| 92 | serdev_device_wait_until_sent(serdev, 0); |
| 93 | |
| 94 | return count; |
| 95 | } |
| 96 | |
| 97 | static const struct gnss_operations sirf_gnss_ops = { |
| 98 | .open = sirf_open, |
| 99 | .close = sirf_close, |
| 100 | .write_raw = sirf_write_raw, |
| 101 | }; |
| 102 | |
| 103 | static int sirf_receive_buf(struct serdev_device *serdev, |
| 104 | const unsigned char *buf, size_t count) |
| 105 | { |
| 106 | struct sirf_data *data = serdev_device_get_drvdata(serdev); |
| 107 | struct gnss_device *gdev = data->gdev; |
| 108 | |
| 109 | return gnss_insert_raw(gdev, buf, count); |
| 110 | } |
| 111 | |
| 112 | static const struct serdev_device_ops sirf_serdev_ops = { |
| 113 | .receive_buf = sirf_receive_buf, |
| 114 | .write_wakeup = serdev_device_write_wakeup, |
| 115 | }; |
| 116 | |
| 117 | static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id) |
| 118 | { |
| 119 | struct sirf_data *data = dev_id; |
| 120 | struct device *dev = &data->serdev->dev; |
| 121 | int ret; |
| 122 | |
| 123 | ret = gpiod_get_value_cansleep(data->wakeup); |
| 124 | dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret); |
| 125 | if (ret < 0) |
| 126 | goto out; |
| 127 | |
| 128 | data->active = !!ret; |
| 129 | wake_up_interruptible(&data->power_wait); |
| 130 | out: |
| 131 | return IRQ_HANDLED; |
| 132 | } |
| 133 | |
| 134 | static int sirf_wait_for_power_state(struct sirf_data *data, bool active, |
| 135 | unsigned long timeout) |
| 136 | { |
| 137 | int ret; |
| 138 | |
| 139 | ret = wait_event_interruptible_timeout(data->power_wait, |
| 140 | data->active == active, msecs_to_jiffies(timeout)); |
| 141 | if (ret < 0) |
| 142 | return ret; |
| 143 | |
| 144 | if (ret == 0) { |
| 145 | dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n", |
| 146 | active); |
| 147 | return -ETIMEDOUT; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static void sirf_pulse_on_off(struct sirf_data *data) |
| 154 | { |
| 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); |
| 158 | } |
| 159 | |
| 160 | static int sirf_set_active(struct sirf_data *data, bool active) |
| 161 | { |
| 162 | unsigned long timeout; |
| 163 | int retries = 3; |
| 164 | int ret; |
| 165 | |
| 166 | if (active) |
| 167 | timeout = SIRF_ACTIVATE_TIMEOUT; |
| 168 | else |
| 169 | timeout = SIRF_HIBERNATE_TIMEOUT; |
| 170 | |
Johan Hovold | 3c1773b | 2018-12-05 11:21:49 +0100 | [diff] [blame] | 171 | do { |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 172 | sirf_pulse_on_off(data); |
| 173 | ret = sirf_wait_for_power_state(data, active, timeout); |
| 174 | if (ret < 0) { |
| 175 | if (ret == -ETIMEDOUT) |
| 176 | continue; |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | break; |
Johan Hovold | 3c1773b | 2018-12-05 11:21:49 +0100 | [diff] [blame] | 182 | } while (retries--); |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 183 | |
Johan Hovold | 3c1773b | 2018-12-05 11:21:49 +0100 | [diff] [blame] | 184 | if (retries < 0) |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 185 | return -ETIMEDOUT; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int sirf_runtime_suspend(struct device *dev) |
| 191 | { |
| 192 | struct sirf_data *data = dev_get_drvdata(dev); |
| 193 | |
| 194 | if (!data->on_off) |
| 195 | return regulator_disable(data->vcc); |
| 196 | |
| 197 | return sirf_set_active(data, false); |
| 198 | } |
| 199 | |
| 200 | static int sirf_runtime_resume(struct device *dev) |
| 201 | { |
| 202 | struct sirf_data *data = dev_get_drvdata(dev); |
| 203 | |
| 204 | if (!data->on_off) |
| 205 | return regulator_enable(data->vcc); |
| 206 | |
| 207 | return sirf_set_active(data, true); |
| 208 | } |
| 209 | |
| 210 | static int __maybe_unused sirf_suspend(struct device *dev) |
| 211 | { |
| 212 | struct sirf_data *data = dev_get_drvdata(dev); |
| 213 | int ret = 0; |
| 214 | |
| 215 | if (!pm_runtime_suspended(dev)) |
| 216 | ret = sirf_runtime_suspend(dev); |
| 217 | |
| 218 | if (data->wakeup) |
| 219 | disable_irq(data->irq); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | static int __maybe_unused sirf_resume(struct device *dev) |
| 225 | { |
| 226 | struct sirf_data *data = dev_get_drvdata(dev); |
| 227 | int ret = 0; |
| 228 | |
| 229 | if (data->wakeup) |
| 230 | enable_irq(data->irq); |
| 231 | |
| 232 | if (!pm_runtime_suspended(dev)) |
| 233 | ret = sirf_runtime_resume(dev); |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 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) |
| 241 | }; |
| 242 | |
| 243 | static int sirf_parse_dt(struct serdev_device *serdev) |
| 244 | { |
| 245 | struct sirf_data *data = serdev_device_get_drvdata(serdev); |
| 246 | struct device_node *node = serdev->dev.of_node; |
| 247 | u32 speed = 9600; |
| 248 | |
| 249 | of_property_read_u32(node, "current-speed", &speed); |
| 250 | |
| 251 | data->speed = speed; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int sirf_probe(struct serdev_device *serdev) |
| 257 | { |
| 258 | struct device *dev = &serdev->dev; |
| 259 | struct gnss_device *gdev; |
| 260 | struct sirf_data *data; |
| 261 | int ret; |
| 262 | |
| 263 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 264 | if (!data) |
| 265 | return -ENOMEM; |
| 266 | |
| 267 | gdev = gnss_allocate_device(dev); |
| 268 | if (!gdev) |
| 269 | return -ENOMEM; |
| 270 | |
Johan Hovold | 10f1466 | 2018-06-01 10:22:59 +0200 | [diff] [blame] | 271 | gdev->type = GNSS_TYPE_SIRF; |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 272 | gdev->ops = &sirf_gnss_ops; |
| 273 | gnss_set_drvdata(gdev, data); |
| 274 | |
| 275 | data->serdev = serdev; |
| 276 | data->gdev = gdev; |
| 277 | |
| 278 | init_waitqueue_head(&data->power_wait); |
| 279 | |
| 280 | serdev_device_set_drvdata(serdev, data); |
| 281 | serdev_device_set_client_ops(serdev, &sirf_serdev_ops); |
| 282 | |
| 283 | ret = sirf_parse_dt(serdev); |
| 284 | if (ret) |
| 285 | goto err_put_device; |
| 286 | |
| 287 | data->vcc = devm_regulator_get(dev, "vcc"); |
| 288 | if (IS_ERR(data->vcc)) { |
| 289 | ret = PTR_ERR(data->vcc); |
| 290 | goto err_put_device; |
| 291 | } |
| 292 | |
| 293 | data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff", |
| 294 | GPIOD_OUT_LOW); |
| 295 | if (IS_ERR(data->on_off)) |
| 296 | goto err_put_device; |
| 297 | |
| 298 | if (data->on_off) { |
| 299 | data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup", |
| 300 | GPIOD_IN); |
| 301 | if (IS_ERR(data->wakeup)) |
| 302 | goto err_put_device; |
| 303 | |
| 304 | /* |
| 305 | * Configurations where WAKEUP has been left not connected, |
| 306 | * are currently not supported. |
| 307 | */ |
| 308 | if (!data->wakeup) { |
| 309 | dev_err(dev, "no wakeup gpio specified\n"); |
| 310 | ret = -ENODEV; |
| 311 | goto err_put_device; |
| 312 | } |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 313 | |
| 314 | ret = regulator_enable(data->vcc); |
| 315 | if (ret) |
| 316 | goto err_put_device; |
| 317 | |
| 318 | /* Wait for chip to boot into hibernate mode. */ |
| 319 | msleep(SIRF_BOOT_DELAY); |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if (data->wakeup) { |
| 323 | ret = gpiod_to_irq(data->wakeup); |
| 324 | if (ret < 0) |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 325 | goto err_disable_vcc; |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 326 | data->irq = ret; |
| 327 | |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 328 | ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler, |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 329 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 330 | "wakeup", data); |
| 331 | if (ret) |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 332 | goto err_disable_vcc; |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | if (IS_ENABLED(CONFIG_PM)) { |
| 336 | pm_runtime_set_suspended(dev); /* clear runtime_error flag */ |
| 337 | pm_runtime_enable(dev); |
| 338 | } else { |
| 339 | ret = sirf_runtime_resume(dev); |
| 340 | if (ret < 0) |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 341 | goto err_free_irq; |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | ret = gnss_register_device(gdev); |
| 345 | if (ret) |
| 346 | goto err_disable_rpm; |
| 347 | |
| 348 | return 0; |
| 349 | |
| 350 | err_disable_rpm: |
| 351 | if (IS_ENABLED(CONFIG_PM)) |
| 352 | pm_runtime_disable(dev); |
| 353 | else |
| 354 | sirf_runtime_suspend(dev); |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 355 | err_free_irq: |
| 356 | if (data->wakeup) |
| 357 | free_irq(data->irq, data); |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 358 | err_disable_vcc: |
| 359 | if (data->on_off) |
| 360 | regulator_disable(data->vcc); |
| 361 | err_put_device: |
| 362 | gnss_put_device(data->gdev); |
| 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | static void sirf_remove(struct serdev_device *serdev) |
| 368 | { |
| 369 | struct sirf_data *data = serdev_device_get_drvdata(serdev); |
| 370 | |
| 371 | gnss_deregister_device(data->gdev); |
| 372 | |
| 373 | if (IS_ENABLED(CONFIG_PM)) |
| 374 | pm_runtime_disable(&serdev->dev); |
| 375 | else |
| 376 | sirf_runtime_suspend(&serdev->dev); |
| 377 | |
Johan Hovold | 09675c2 | 2019-01-22 18:22:53 +0100 | [diff] [blame] | 378 | if (data->wakeup) |
| 379 | free_irq(data->irq, data); |
| 380 | |
Johan Hovold | d2efbbd | 2018-06-01 10:22:58 +0200 | [diff] [blame] | 381 | if (data->on_off) |
| 382 | regulator_disable(data->vcc); |
| 383 | |
| 384 | gnss_put_device(data->gdev); |
| 385 | }; |
| 386 | |
| 387 | #ifdef CONFIG_OF |
| 388 | static const struct of_device_id sirf_of_match[] = { |
| 389 | { .compatible = "fastrax,uc430" }, |
| 390 | { .compatible = "linx,r4" }, |
| 391 | { .compatible = "wi2wi,w2sg0008i" }, |
| 392 | { .compatible = "wi2wi,w2sg0084i" }, |
| 393 | {}, |
| 394 | }; |
| 395 | MODULE_DEVICE_TABLE(of, sirf_of_match); |
| 396 | #endif |
| 397 | |
| 398 | static struct serdev_device_driver sirf_driver = { |
| 399 | .driver = { |
| 400 | .name = "gnss-sirf", |
| 401 | .of_match_table = of_match_ptr(sirf_of_match), |
| 402 | .pm = &sirf_pm_ops, |
| 403 | }, |
| 404 | .probe = sirf_probe, |
| 405 | .remove = sirf_remove, |
| 406 | }; |
| 407 | module_serdev_device_driver(sirf_driver); |
| 408 | |
| 409 | MODULE_AUTHOR("Johan Hovold <johan@kernel.org>"); |
| 410 | MODULE_DESCRIPTION("SiRFstar GNSS receiver driver"); |
| 411 | MODULE_LICENSE("GPL v2"); |