root/drivers/gnss/sirf.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sirf_serdev_open
  2. sirf_serdev_close
  3. sirf_open
  4. sirf_close
  5. sirf_write_raw
  6. sirf_receive_buf
  7. sirf_wakeup_handler
  8. sirf_wait_for_power_state_nowakeup
  9. sirf_wait_for_power_state
  10. sirf_pulse_on_off
  11. sirf_set_active
  12. sirf_runtime_suspend
  13. sirf_runtime_resume
  14. sirf_suspend
  15. sirf_resume
  16. sirf_parse_dt
  17. sirf_probe
  18. sirf_remove

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

/* [<][>][^][v][top][bottom][index][help] */