xref: /linux/drivers/gnss/ubx.c (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
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/gpio/consumer.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/serdev.h>
17 
18 #include "serial.h"
19 
20 struct ubx_data {
21 	struct regulator *vcc;
22 };
23 
24 static 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 
36 static 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 
48 static 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 
62 static const struct gnss_serial_ops ubx_gserial_ops = {
63 	.set_power = ubx_set_power,
64 };
65 
66 static int ubx_probe(struct serdev_device *serdev)
67 {
68 	struct gnss_serial *gserial;
69 	struct gpio_desc *safeboot;
70 	struct gpio_desc *reset;
71 	struct ubx_data *data;
72 	int ret;
73 
74 	gserial = gnss_serial_allocate(serdev, sizeof(*data));
75 	if (IS_ERR(gserial)) {
76 		ret = PTR_ERR(gserial);
77 		return ret;
78 	}
79 
80 	gserial->ops = &ubx_gserial_ops;
81 
82 	gserial->gdev->type = GNSS_TYPE_UBX;
83 
84 	data = gnss_serial_get_drvdata(gserial);
85 
86 	data->vcc = devm_regulator_get(&serdev->dev, "vcc");
87 	if (IS_ERR(data->vcc)) {
88 		ret = PTR_ERR(data->vcc);
89 		goto err_free_gserial;
90 	}
91 
92 	ret = devm_regulator_get_enable_optional(&serdev->dev, "v-bckp");
93 	if (ret < 0 && ret != -ENODEV)
94 		goto err_free_gserial;
95 
96 	/* Deassert safeboot */
97 	safeboot = devm_gpiod_get_optional(&serdev->dev, "safeboot", GPIOD_OUT_LOW);
98 	if (IS_ERR(safeboot)) {
99 		ret = PTR_ERR(safeboot);
100 		goto err_free_gserial;
101 	}
102 
103 	/* Deassert reset */
104 	reset = devm_gpiod_get_optional(&serdev->dev, "reset", GPIOD_OUT_LOW);
105 	if (IS_ERR(reset)) {
106 		ret = PTR_ERR(reset);
107 		goto err_free_gserial;
108 	}
109 
110 	ret = gnss_serial_register(gserial);
111 	if (ret)
112 		goto err_free_gserial;
113 
114 	return 0;
115 
116 err_free_gserial:
117 	gnss_serial_free(gserial);
118 
119 	return ret;
120 }
121 
122 static void ubx_remove(struct serdev_device *serdev)
123 {
124 	struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
125 
126 	gnss_serial_deregister(gserial);
127 	gnss_serial_free(gserial);
128 }
129 
130 #ifdef CONFIG_OF
131 static const struct of_device_id ubx_of_match[] = {
132 	{ .compatible = "u-blox,neo-6m" },
133 	{ .compatible = "u-blox,neo-8" },
134 	{ .compatible = "u-blox,neo-m8" },
135 	{},
136 };
137 MODULE_DEVICE_TABLE(of, ubx_of_match);
138 #endif
139 
140 static struct serdev_device_driver ubx_driver = {
141 	.driver	= {
142 		.name		= "gnss-ubx",
143 		.of_match_table	= of_match_ptr(ubx_of_match),
144 		.pm		= &gnss_serial_pm_ops,
145 	},
146 	.probe	= ubx_probe,
147 	.remove	= ubx_remove,
148 };
149 module_serdev_device_driver(ubx_driver);
150 
151 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
152 MODULE_DESCRIPTION("u-blox GNSS receiver driver");
153 MODULE_LICENSE("GPL v2");
154