linux/drivers/gnss/ubx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * u-blox 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/init.h>
  11#include <linux/kernel.h>
  12#include <linux/module.h>
  13#include <linux/of.h>
  14#include <linux/regulator/consumer.h>
  15#include <linux/serdev.h>
  16
  17#include "serial.h"
  18
  19struct ubx_data {
  20        struct regulator *v_bckp;
  21        struct regulator *vcc;
  22};
  23
  24static int ubx_set_active(struct gnss_serial *gserial)
  25{
  26        struct ubx_data *data = gnss_serial_get_drvdata(gserial);
  27        int ret;
  28
  29        ret = regulator_enable(data->vcc);
  30        if (ret)
  31                return ret;
  32
  33        return 0;
  34}
  35
  36static int ubx_set_standby(struct gnss_serial *gserial)
  37{
  38        struct ubx_data *data = gnss_serial_get_drvdata(gserial);
  39        int ret;
  40
  41        ret = regulator_disable(data->vcc);
  42        if (ret)
  43                return ret;
  44
  45        return 0;
  46}
  47
  48static int ubx_set_power(struct gnss_serial *gserial,
  49                                enum gnss_serial_pm_state state)
  50{
  51        switch (state) {
  52        case GNSS_SERIAL_ACTIVE:
  53                return ubx_set_active(gserial);
  54        case GNSS_SERIAL_OFF:
  55        case GNSS_SERIAL_STANDBY:
  56                return ubx_set_standby(gserial);
  57        }
  58
  59        return -EINVAL;
  60}
  61
  62static const struct gnss_serial_ops ubx_gserial_ops = {
  63        .set_power = ubx_set_power,
  64};
  65
  66static int ubx_probe(struct serdev_device *serdev)
  67{
  68        struct gnss_serial *gserial;
  69        struct ubx_data *data;
  70        int ret;
  71
  72        gserial = gnss_serial_allocate(serdev, sizeof(*data));
  73        if (IS_ERR(gserial)) {
  74                ret = PTR_ERR(gserial);
  75                return ret;
  76        }
  77
  78        gserial->ops = &ubx_gserial_ops;
  79
  80        gserial->gdev->type = GNSS_TYPE_UBX;
  81
  82        data = gnss_serial_get_drvdata(gserial);
  83
  84        data->vcc = devm_regulator_get(&serdev->dev, "vcc");
  85        if (IS_ERR(data->vcc)) {
  86                ret = PTR_ERR(data->vcc);
  87                goto err_free_gserial;
  88        }
  89
  90        data->v_bckp = devm_regulator_get_optional(&serdev->dev, "v-bckp");
  91        if (IS_ERR(data->v_bckp)) {
  92                ret = PTR_ERR(data->v_bckp);
  93                if (ret == -ENODEV)
  94                        data->v_bckp = NULL;
  95                else
  96                        goto err_free_gserial;
  97        }
  98
  99        if (data->v_bckp) {
 100                ret = regulator_enable(data->v_bckp);
 101                if (ret)
 102                        goto err_free_gserial;
 103        }
 104
 105        ret = gnss_serial_register(gserial);
 106        if (ret)
 107                goto err_disable_v_bckp;
 108
 109        return 0;
 110
 111err_disable_v_bckp:
 112        if (data->v_bckp)
 113                regulator_disable(data->v_bckp);
 114err_free_gserial:
 115        gnss_serial_free(gserial);
 116
 117        return ret;
 118}
 119
 120static void ubx_remove(struct serdev_device *serdev)
 121{
 122        struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
 123        struct ubx_data *data = gnss_serial_get_drvdata(gserial);
 124
 125        gnss_serial_deregister(gserial);
 126        if (data->v_bckp)
 127                regulator_disable(data->v_bckp);
 128        gnss_serial_free(gserial);
 129};
 130
 131#ifdef CONFIG_OF
 132static const struct of_device_id ubx_of_match[] = {
 133        { .compatible = "u-blox,neo-6m" },
 134        { .compatible = "u-blox,neo-8" },
 135        { .compatible = "u-blox,neo-m8" },
 136        {},
 137};
 138MODULE_DEVICE_TABLE(of, ubx_of_match);
 139#endif
 140
 141static struct serdev_device_driver ubx_driver = {
 142        .driver = {
 143                .name           = "gnss-ubx",
 144                .of_match_table = of_match_ptr(ubx_of_match),
 145                .pm             = &gnss_serial_pm_ops,
 146        },
 147        .probe  = ubx_probe,
 148        .remove = ubx_remove,
 149};
 150module_serdev_device_driver(ubx_driver);
 151
 152MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
 153MODULE_DESCRIPTION("u-blox GNSS receiver driver");
 154MODULE_LICENSE("GPL v2");
 155