Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * SiRFstar GNSS receiver driver
0004  *
0005  * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
0006  */
0007 
0008 #include <linux/errno.h>
0009 #include <linux/gnss.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/init.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/pm.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/regulator/consumer.h>
0019 #include <linux/sched.h>
0020 #include <linux/serdev.h>
0021 #include <linux/slab.h>
0022 #include <linux/wait.h>
0023 
0024 #define SIRF_BOOT_DELAY         500
0025 #define SIRF_ON_OFF_PULSE_TIME      100
0026 #define SIRF_ACTIVATE_TIMEOUT       200
0027 #define SIRF_HIBERNATE_TIMEOUT      200
0028 /*
0029  * If no data arrives for this time, we assume that the chip is off.
0030  * REVISIT: The report cycle is configurable and can be several minutes long,
0031  * so this will only work reliably if the report cycle is set to a reasonable
0032  * low value. Also power saving settings (like send data only on movement)
0033  * might things work even worse.
0034  * Workaround might be to parse shutdown or bootup messages.
0035  */
0036 #define SIRF_REPORT_CYCLE   2000
0037 
0038 struct sirf_data {
0039     struct gnss_device *gdev;
0040     struct serdev_device *serdev;
0041     speed_t speed;
0042     struct regulator *vcc;
0043     struct regulator *lna;
0044     struct gpio_desc *on_off;
0045     struct gpio_desc *wakeup;
0046     int irq;
0047     bool active;
0048 
0049     struct mutex gdev_mutex;
0050     bool open;
0051 
0052     struct mutex serdev_mutex;
0053     int serdev_count;
0054 
0055     wait_queue_head_t power_wait;
0056 };
0057 
0058 static int sirf_serdev_open(struct sirf_data *data)
0059 {
0060     int ret = 0;
0061 
0062     mutex_lock(&data->serdev_mutex);
0063     if (++data->serdev_count == 1) {
0064         ret = serdev_device_open(data->serdev);
0065         if (ret) {
0066             data->serdev_count--;
0067             goto out_unlock;
0068         }
0069 
0070         serdev_device_set_baudrate(data->serdev, data->speed);
0071         serdev_device_set_flow_control(data->serdev, false);
0072     }
0073 
0074 out_unlock:
0075     mutex_unlock(&data->serdev_mutex);
0076 
0077     return ret;
0078 }
0079 
0080 static void sirf_serdev_close(struct sirf_data *data)
0081 {
0082     mutex_lock(&data->serdev_mutex);
0083     if (--data->serdev_count == 0)
0084         serdev_device_close(data->serdev);
0085     mutex_unlock(&data->serdev_mutex);
0086 }
0087 
0088 static int sirf_open(struct gnss_device *gdev)
0089 {
0090     struct sirf_data *data = gnss_get_drvdata(gdev);
0091     struct serdev_device *serdev = data->serdev;
0092     int ret;
0093 
0094     mutex_lock(&data->gdev_mutex);
0095     data->open = true;
0096     mutex_unlock(&data->gdev_mutex);
0097 
0098     ret = sirf_serdev_open(data);
0099     if (ret) {
0100         mutex_lock(&data->gdev_mutex);
0101         data->open = false;
0102         mutex_unlock(&data->gdev_mutex);
0103         return ret;
0104     }
0105 
0106     ret = pm_runtime_get_sync(&serdev->dev);
0107     if (ret < 0) {
0108         dev_err(&gdev->dev, "failed to runtime resume: %d\n", ret);
0109         pm_runtime_put_noidle(&serdev->dev);
0110         goto err_close;
0111     }
0112 
0113     return 0;
0114 
0115 err_close:
0116     sirf_serdev_close(data);
0117 
0118     mutex_lock(&data->gdev_mutex);
0119     data->open = false;
0120     mutex_unlock(&data->gdev_mutex);
0121 
0122     return ret;
0123 }
0124 
0125 static void sirf_close(struct gnss_device *gdev)
0126 {
0127     struct sirf_data *data = gnss_get_drvdata(gdev);
0128     struct serdev_device *serdev = data->serdev;
0129 
0130     sirf_serdev_close(data);
0131 
0132     pm_runtime_put(&serdev->dev);
0133 
0134     mutex_lock(&data->gdev_mutex);
0135     data->open = false;
0136     mutex_unlock(&data->gdev_mutex);
0137 }
0138 
0139 static int sirf_write_raw(struct gnss_device *gdev, const unsigned char *buf,
0140                 size_t count)
0141 {
0142     struct sirf_data *data = gnss_get_drvdata(gdev);
0143     struct serdev_device *serdev = data->serdev;
0144     int ret;
0145 
0146     /* write is only buffered synchronously */
0147     ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
0148     if (ret < 0 || ret < count)
0149         return ret;
0150 
0151     /* FIXME: determine if interrupted? */
0152     serdev_device_wait_until_sent(serdev, 0);
0153 
0154     return count;
0155 }
0156 
0157 static const struct gnss_operations sirf_gnss_ops = {
0158     .open       = sirf_open,
0159     .close      = sirf_close,
0160     .write_raw  = sirf_write_raw,
0161 };
0162 
0163 static int sirf_receive_buf(struct serdev_device *serdev,
0164                 const unsigned char *buf, size_t count)
0165 {
0166     struct sirf_data *data = serdev_device_get_drvdata(serdev);
0167     struct gnss_device *gdev = data->gdev;
0168     int ret = 0;
0169 
0170     if (!data->wakeup && !data->active) {
0171         data->active = true;
0172         wake_up_interruptible(&data->power_wait);
0173     }
0174 
0175     mutex_lock(&data->gdev_mutex);
0176     if (data->open)
0177         ret = gnss_insert_raw(gdev, buf, count);
0178     mutex_unlock(&data->gdev_mutex);
0179 
0180     return ret;
0181 }
0182 
0183 static const struct serdev_device_ops sirf_serdev_ops = {
0184     .receive_buf    = sirf_receive_buf,
0185     .write_wakeup   = serdev_device_write_wakeup,
0186 };
0187 
0188 static irqreturn_t sirf_wakeup_handler(int irq, void *dev_id)
0189 {
0190     struct sirf_data *data = dev_id;
0191     struct device *dev = &data->serdev->dev;
0192     int ret;
0193 
0194     ret = gpiod_get_value_cansleep(data->wakeup);
0195     dev_dbg(dev, "%s - wakeup = %d\n", __func__, ret);
0196     if (ret < 0)
0197         goto out;
0198 
0199     data->active = ret;
0200     wake_up_interruptible(&data->power_wait);
0201 out:
0202     return IRQ_HANDLED;
0203 }
0204 
0205 static int sirf_wait_for_power_state_nowakeup(struct sirf_data *data,
0206                         bool active,
0207                         unsigned long timeout)
0208 {
0209     int ret;
0210 
0211     /* Wait for state change (including any shutdown messages). */
0212     msleep(timeout);
0213 
0214     /* Wait for data reception or timeout. */
0215     data->active = false;
0216     ret = wait_event_interruptible_timeout(data->power_wait,
0217             data->active, msecs_to_jiffies(SIRF_REPORT_CYCLE));
0218     if (ret < 0)
0219         return ret;
0220 
0221     if (ret > 0 && !active)
0222         return -ETIMEDOUT;
0223 
0224     if (ret == 0 && active)
0225         return -ETIMEDOUT;
0226 
0227     return 0;
0228 }
0229 
0230 static int sirf_wait_for_power_state(struct sirf_data *data, bool active,
0231                     unsigned long timeout)
0232 {
0233     int ret;
0234 
0235     if (!data->wakeup)
0236         return sirf_wait_for_power_state_nowakeup(data, active, timeout);
0237 
0238     ret = wait_event_interruptible_timeout(data->power_wait,
0239             data->active == active, msecs_to_jiffies(timeout));
0240     if (ret < 0)
0241         return ret;
0242 
0243     if (ret == 0) {
0244         dev_warn(&data->serdev->dev, "timeout waiting for active state = %d\n",
0245                 active);
0246         return -ETIMEDOUT;
0247     }
0248 
0249     return 0;
0250 }
0251 
0252 static void sirf_pulse_on_off(struct sirf_data *data)
0253 {
0254     gpiod_set_value_cansleep(data->on_off, 1);
0255     msleep(SIRF_ON_OFF_PULSE_TIME);
0256     gpiod_set_value_cansleep(data->on_off, 0);
0257 }
0258 
0259 static int sirf_set_active(struct sirf_data *data, bool active)
0260 {
0261     unsigned long timeout;
0262     int retries = 3;
0263     int ret;
0264 
0265     if (active)
0266         timeout = SIRF_ACTIVATE_TIMEOUT;
0267     else
0268         timeout = SIRF_HIBERNATE_TIMEOUT;
0269 
0270     if (!data->wakeup) {
0271         ret = sirf_serdev_open(data);
0272         if (ret)
0273             return ret;
0274     }
0275 
0276     do {
0277         sirf_pulse_on_off(data);
0278         ret = sirf_wait_for_power_state(data, active, timeout);
0279     } while (ret == -ETIMEDOUT && retries--);
0280 
0281     if (!data->wakeup)
0282         sirf_serdev_close(data);
0283 
0284     if (ret)
0285         return ret;
0286 
0287     return 0;
0288 }
0289 
0290 static int sirf_runtime_suspend(struct device *dev)
0291 {
0292     struct sirf_data *data = dev_get_drvdata(dev);
0293     int ret2;
0294     int ret;
0295 
0296     if (data->on_off)
0297         ret = sirf_set_active(data, false);
0298     else
0299         ret = regulator_disable(data->vcc);
0300 
0301     if (ret)
0302         return ret;
0303 
0304     ret = regulator_disable(data->lna);
0305     if (ret)
0306         goto err_reenable;
0307 
0308     return 0;
0309 
0310 err_reenable:
0311     if (data->on_off)
0312         ret2 = sirf_set_active(data, true);
0313     else
0314         ret2 = regulator_enable(data->vcc);
0315 
0316     if (ret2)
0317         dev_err(dev,
0318             "failed to reenable power on failed suspend: %d\n",
0319             ret2);
0320 
0321     return ret;
0322 }
0323 
0324 static int sirf_runtime_resume(struct device *dev)
0325 {
0326     struct sirf_data *data = dev_get_drvdata(dev);
0327     int ret;
0328 
0329     ret = regulator_enable(data->lna);
0330     if (ret)
0331         return ret;
0332 
0333     if (data->on_off)
0334         ret = sirf_set_active(data, true);
0335     else
0336         ret = regulator_enable(data->vcc);
0337 
0338     if (ret)
0339         goto err_disable_lna;
0340 
0341     return 0;
0342 
0343 err_disable_lna:
0344     regulator_disable(data->lna);
0345 
0346     return ret;
0347 }
0348 
0349 static int __maybe_unused sirf_suspend(struct device *dev)
0350 {
0351     struct sirf_data *data = dev_get_drvdata(dev);
0352     int ret = 0;
0353 
0354     if (!pm_runtime_suspended(dev))
0355         ret = sirf_runtime_suspend(dev);
0356 
0357     if (data->wakeup)
0358         disable_irq(data->irq);
0359 
0360     return ret;
0361 }
0362 
0363 static int __maybe_unused sirf_resume(struct device *dev)
0364 {
0365     struct sirf_data *data = dev_get_drvdata(dev);
0366     int ret = 0;
0367 
0368     if (data->wakeup)
0369         enable_irq(data->irq);
0370 
0371     if (!pm_runtime_suspended(dev))
0372         ret = sirf_runtime_resume(dev);
0373 
0374     return ret;
0375 }
0376 
0377 static const struct dev_pm_ops sirf_pm_ops = {
0378     SET_SYSTEM_SLEEP_PM_OPS(sirf_suspend, sirf_resume)
0379     SET_RUNTIME_PM_OPS(sirf_runtime_suspend, sirf_runtime_resume, NULL)
0380 };
0381 
0382 static int sirf_parse_dt(struct serdev_device *serdev)
0383 {
0384     struct sirf_data *data = serdev_device_get_drvdata(serdev);
0385     struct device_node *node = serdev->dev.of_node;
0386     u32 speed = 9600;
0387 
0388     of_property_read_u32(node, "current-speed", &speed);
0389 
0390     data->speed = speed;
0391 
0392     return 0;
0393 }
0394 
0395 static int sirf_probe(struct serdev_device *serdev)
0396 {
0397     struct device *dev = &serdev->dev;
0398     struct gnss_device *gdev;
0399     struct sirf_data *data;
0400     int ret;
0401 
0402     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0403     if (!data)
0404         return -ENOMEM;
0405 
0406     gdev = gnss_allocate_device(dev);
0407     if (!gdev)
0408         return -ENOMEM;
0409 
0410     gdev->type = GNSS_TYPE_SIRF;
0411     gdev->ops = &sirf_gnss_ops;
0412     gnss_set_drvdata(gdev, data);
0413 
0414     data->serdev = serdev;
0415     data->gdev = gdev;
0416 
0417     mutex_init(&data->gdev_mutex);
0418     mutex_init(&data->serdev_mutex);
0419     init_waitqueue_head(&data->power_wait);
0420 
0421     serdev_device_set_drvdata(serdev, data);
0422     serdev_device_set_client_ops(serdev, &sirf_serdev_ops);
0423 
0424     ret = sirf_parse_dt(serdev);
0425     if (ret)
0426         goto err_put_device;
0427 
0428     data->vcc = devm_regulator_get(dev, "vcc");
0429     if (IS_ERR(data->vcc)) {
0430         ret = PTR_ERR(data->vcc);
0431         goto err_put_device;
0432     }
0433 
0434     data->lna = devm_regulator_get(dev, "lna");
0435     if (IS_ERR(data->lna)) {
0436         ret = PTR_ERR(data->lna);
0437         goto err_put_device;
0438     }
0439 
0440     data->on_off = devm_gpiod_get_optional(dev, "sirf,onoff",
0441             GPIOD_OUT_LOW);
0442     if (IS_ERR(data->on_off)) {
0443         ret = PTR_ERR(data->on_off);
0444         goto err_put_device;
0445     }
0446 
0447     if (data->on_off) {
0448         data->wakeup = devm_gpiod_get_optional(dev, "sirf,wakeup",
0449                 GPIOD_IN);
0450         if (IS_ERR(data->wakeup)) {
0451             ret = PTR_ERR(data->wakeup);
0452             goto err_put_device;
0453         }
0454 
0455         ret = regulator_enable(data->vcc);
0456         if (ret)
0457             goto err_put_device;
0458 
0459         /* Wait for chip to boot into hibernate mode. */
0460         msleep(SIRF_BOOT_DELAY);
0461     }
0462 
0463     if (data->wakeup) {
0464         ret = gpiod_get_value_cansleep(data->wakeup);
0465         if (ret < 0)
0466             goto err_disable_vcc;
0467         data->active = ret;
0468 
0469         ret = gpiod_to_irq(data->wakeup);
0470         if (ret < 0)
0471             goto err_disable_vcc;
0472         data->irq = ret;
0473 
0474         ret = request_threaded_irq(data->irq, NULL, sirf_wakeup_handler,
0475                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0476                 "wakeup", data);
0477         if (ret)
0478             goto err_disable_vcc;
0479     }
0480 
0481     if (data->on_off) {
0482         if (!data->wakeup) {
0483             data->active = false;
0484 
0485             ret = sirf_serdev_open(data);
0486             if (ret)
0487                 goto err_disable_vcc;
0488 
0489             msleep(SIRF_REPORT_CYCLE);
0490             sirf_serdev_close(data);
0491         }
0492 
0493         /* Force hibernate mode if already active. */
0494         if (data->active) {
0495             ret = sirf_set_active(data, false);
0496             if (ret) {
0497                 dev_err(dev, "failed to set hibernate mode: %d\n",
0498                         ret);
0499                 goto err_free_irq;
0500             }
0501         }
0502     }
0503 
0504     if (IS_ENABLED(CONFIG_PM)) {
0505         pm_runtime_set_suspended(dev);  /* clear runtime_error flag */
0506         pm_runtime_enable(dev);
0507     } else {
0508         ret = sirf_runtime_resume(dev);
0509         if (ret < 0)
0510             goto err_free_irq;
0511     }
0512 
0513     ret = gnss_register_device(gdev);
0514     if (ret)
0515         goto err_disable_rpm;
0516 
0517     return 0;
0518 
0519 err_disable_rpm:
0520     if (IS_ENABLED(CONFIG_PM))
0521         pm_runtime_disable(dev);
0522     else
0523         sirf_runtime_suspend(dev);
0524 err_free_irq:
0525     if (data->wakeup)
0526         free_irq(data->irq, data);
0527 err_disable_vcc:
0528     if (data->on_off)
0529         regulator_disable(data->vcc);
0530 err_put_device:
0531     gnss_put_device(data->gdev);
0532 
0533     return ret;
0534 }
0535 
0536 static void sirf_remove(struct serdev_device *serdev)
0537 {
0538     struct sirf_data *data = serdev_device_get_drvdata(serdev);
0539 
0540     gnss_deregister_device(data->gdev);
0541 
0542     if (IS_ENABLED(CONFIG_PM))
0543         pm_runtime_disable(&serdev->dev);
0544     else
0545         sirf_runtime_suspend(&serdev->dev);
0546 
0547     if (data->wakeup)
0548         free_irq(data->irq, data);
0549 
0550     if (data->on_off)
0551         regulator_disable(data->vcc);
0552 
0553     gnss_put_device(data->gdev);
0554 }
0555 
0556 #ifdef CONFIG_OF
0557 static const struct of_device_id sirf_of_match[] = {
0558     { .compatible = "fastrax,uc430" },
0559     { .compatible = "linx,r4" },
0560     { .compatible = "wi2wi,w2sg0004" },
0561     { .compatible = "wi2wi,w2sg0008i" },
0562     { .compatible = "wi2wi,w2sg0084i" },
0563     {},
0564 };
0565 MODULE_DEVICE_TABLE(of, sirf_of_match);
0566 #endif
0567 
0568 static struct serdev_device_driver sirf_driver = {
0569     .driver = {
0570         .name       = "gnss-sirf",
0571         .of_match_table = of_match_ptr(sirf_of_match),
0572         .pm     = &sirf_pm_ops,
0573     },
0574     .probe  = sirf_probe,
0575     .remove = sirf_remove,
0576 };
0577 module_serdev_device_driver(sirf_driver);
0578 
0579 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
0580 MODULE_DESCRIPTION("SiRFstar GNSS receiver driver");
0581 MODULE_LICENSE("GPL v2");