Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * u-blox 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/init.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/regulator/consumer.h>
0015 #include <linux/serdev.h>
0016 
0017 #include "serial.h"
0018 
0019 struct ubx_data {
0020     struct regulator *v_bckp;
0021     struct regulator *vcc;
0022 };
0023 
0024 static int ubx_set_active(struct gnss_serial *gserial)
0025 {
0026     struct ubx_data *data = gnss_serial_get_drvdata(gserial);
0027     int ret;
0028 
0029     ret = regulator_enable(data->vcc);
0030     if (ret)
0031         return ret;
0032 
0033     return 0;
0034 }
0035 
0036 static int ubx_set_standby(struct gnss_serial *gserial)
0037 {
0038     struct ubx_data *data = gnss_serial_get_drvdata(gserial);
0039     int ret;
0040 
0041     ret = regulator_disable(data->vcc);
0042     if (ret)
0043         return ret;
0044 
0045     return 0;
0046 }
0047 
0048 static int ubx_set_power(struct gnss_serial *gserial,
0049                 enum gnss_serial_pm_state state)
0050 {
0051     switch (state) {
0052     case GNSS_SERIAL_ACTIVE:
0053         return ubx_set_active(gserial);
0054     case GNSS_SERIAL_OFF:
0055     case GNSS_SERIAL_STANDBY:
0056         return ubx_set_standby(gserial);
0057     }
0058 
0059     return -EINVAL;
0060 }
0061 
0062 static const struct gnss_serial_ops ubx_gserial_ops = {
0063     .set_power = ubx_set_power,
0064 };
0065 
0066 static int ubx_probe(struct serdev_device *serdev)
0067 {
0068     struct gnss_serial *gserial;
0069     struct ubx_data *data;
0070     int ret;
0071 
0072     gserial = gnss_serial_allocate(serdev, sizeof(*data));
0073     if (IS_ERR(gserial)) {
0074         ret = PTR_ERR(gserial);
0075         return ret;
0076     }
0077 
0078     gserial->ops = &ubx_gserial_ops;
0079 
0080     gserial->gdev->type = GNSS_TYPE_UBX;
0081 
0082     data = gnss_serial_get_drvdata(gserial);
0083 
0084     data->vcc = devm_regulator_get(&serdev->dev, "vcc");
0085     if (IS_ERR(data->vcc)) {
0086         ret = PTR_ERR(data->vcc);
0087         goto err_free_gserial;
0088     }
0089 
0090     data->v_bckp = devm_regulator_get_optional(&serdev->dev, "v-bckp");
0091     if (IS_ERR(data->v_bckp)) {
0092         ret = PTR_ERR(data->v_bckp);
0093         if (ret == -ENODEV)
0094             data->v_bckp = NULL;
0095         else
0096             goto err_free_gserial;
0097     }
0098 
0099     if (data->v_bckp) {
0100         ret = regulator_enable(data->v_bckp);
0101         if (ret)
0102             goto err_free_gserial;
0103     }
0104 
0105     ret = gnss_serial_register(gserial);
0106     if (ret)
0107         goto err_disable_v_bckp;
0108 
0109     return 0;
0110 
0111 err_disable_v_bckp:
0112     if (data->v_bckp)
0113         regulator_disable(data->v_bckp);
0114 err_free_gserial:
0115     gnss_serial_free(gserial);
0116 
0117     return ret;
0118 }
0119 
0120 static void ubx_remove(struct serdev_device *serdev)
0121 {
0122     struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
0123     struct ubx_data *data = gnss_serial_get_drvdata(gserial);
0124 
0125     gnss_serial_deregister(gserial);
0126     if (data->v_bckp)
0127         regulator_disable(data->v_bckp);
0128     gnss_serial_free(gserial);
0129 }
0130 
0131 #ifdef CONFIG_OF
0132 static const struct of_device_id ubx_of_match[] = {
0133     { .compatible = "u-blox,neo-6m" },
0134     { .compatible = "u-blox,neo-8" },
0135     { .compatible = "u-blox,neo-m8" },
0136     {},
0137 };
0138 MODULE_DEVICE_TABLE(of, ubx_of_match);
0139 #endif
0140 
0141 static struct serdev_device_driver ubx_driver = {
0142     .driver = {
0143         .name       = "gnss-ubx",
0144         .of_match_table = of_match_ptr(ubx_of_match),
0145         .pm     = &gnss_serial_pm_ops,
0146     },
0147     .probe  = ubx_probe,
0148     .remove = ubx_remove,
0149 };
0150 module_serdev_device_driver(ubx_driver);
0151 
0152 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
0153 MODULE_DESCRIPTION("u-blox GNSS receiver driver");
0154 MODULE_LICENSE("GPL v2");