1 // SPDX-License-Identifier: GPL-2.0
3 * Generic serial GNSS receiver driver
5 * Copyright (C) 2018 Johan Hovold <johan@kernel.org>
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>
15 #include <linux/pm_runtime.h>
16 #include <linux/sched.h>
17 #include <linux/serdev.h>
18 #include <linux/slab.h>
22 static int gnss_serial_open(struct gnss_device *gdev)
24 struct gnss_serial *gserial = gnss_get_drvdata(gdev);
25 struct serdev_device *serdev = gserial->serdev;
28 ret = serdev_device_open(serdev);
32 serdev_device_set_baudrate(serdev, gserial->speed);
33 serdev_device_set_flow_control(serdev, false);
35 ret = pm_runtime_get_sync(&serdev->dev);
37 pm_runtime_put_noidle(&serdev->dev);
44 serdev_device_close(serdev);
49 static void gnss_serial_close(struct gnss_device *gdev)
51 struct gnss_serial *gserial = gnss_get_drvdata(gdev);
52 struct serdev_device *serdev = gserial->serdev;
54 serdev_device_close(serdev);
56 pm_runtime_put(&serdev->dev);
59 static int gnss_serial_write_raw(struct gnss_device *gdev,
60 const unsigned char *buf, size_t count)
62 struct gnss_serial *gserial = gnss_get_drvdata(gdev);
63 struct serdev_device *serdev = gserial->serdev;
66 /* write is only buffered synchronously */
67 ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT);
68 if (ret < 0 || ret < count)
71 /* FIXME: determine if interrupted? */
72 serdev_device_wait_until_sent(serdev, 0);
77 static const struct gnss_operations gnss_serial_gnss_ops = {
78 .open = gnss_serial_open,
79 .close = gnss_serial_close,
80 .write_raw = gnss_serial_write_raw,
83 static int gnss_serial_receive_buf(struct serdev_device *serdev,
84 const unsigned char *buf, size_t count)
86 struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
87 struct gnss_device *gdev = gserial->gdev;
89 return gnss_insert_raw(gdev, buf, count);
92 static const struct serdev_device_ops gnss_serial_serdev_ops = {
93 .receive_buf = gnss_serial_receive_buf,
94 .write_wakeup = serdev_device_write_wakeup,
97 static int gnss_serial_set_power(struct gnss_serial *gserial,
98 enum gnss_serial_pm_state state)
100 if (!gserial->ops || !gserial->ops->set_power)
103 return gserial->ops->set_power(gserial, state);
107 * FIXME: need to provide subdriver defaults or separate dt parsing from
110 static int gnss_serial_parse_dt(struct serdev_device *serdev)
112 struct gnss_serial *gserial = serdev_device_get_drvdata(serdev);
113 struct device_node *node = serdev->dev.of_node;
116 of_property_read_u32(node, "current-speed", &speed);
118 gserial->speed = speed;
123 struct gnss_serial *gnss_serial_allocate(struct serdev_device *serdev,
126 struct gnss_serial *gserial;
127 struct gnss_device *gdev;
130 gserial = kzalloc(sizeof(*gserial) + data_size, GFP_KERNEL);
132 return ERR_PTR(-ENOMEM);
134 gdev = gnss_allocate_device(&serdev->dev);
137 goto err_free_gserial;
140 gdev->ops = &gnss_serial_gnss_ops;
141 gnss_set_drvdata(gdev, gserial);
143 gserial->serdev = serdev;
144 gserial->gdev = gdev;
146 serdev_device_set_drvdata(serdev, gserial);
147 serdev_device_set_client_ops(serdev, &gnss_serial_serdev_ops);
149 ret = gnss_serial_parse_dt(serdev);
156 gnss_put_device(gserial->gdev);
162 EXPORT_SYMBOL_GPL(gnss_serial_allocate);
164 void gnss_serial_free(struct gnss_serial *gserial)
166 gnss_put_device(gserial->gdev);
169 EXPORT_SYMBOL_GPL(gnss_serial_free);
171 int gnss_serial_register(struct gnss_serial *gserial)
173 struct serdev_device *serdev = gserial->serdev;
176 if (IS_ENABLED(CONFIG_PM)) {
177 pm_runtime_enable(&serdev->dev);
179 ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE);
184 ret = gnss_register_device(gserial->gdev);
186 goto err_disable_rpm;
191 if (IS_ENABLED(CONFIG_PM))
192 pm_runtime_disable(&serdev->dev);
194 gnss_serial_set_power(gserial, GNSS_SERIAL_OFF);
198 EXPORT_SYMBOL_GPL(gnss_serial_register);
200 void gnss_serial_deregister(struct gnss_serial *gserial)
202 struct serdev_device *serdev = gserial->serdev;
204 gnss_deregister_device(gserial->gdev);
206 if (IS_ENABLED(CONFIG_PM))
207 pm_runtime_disable(&serdev->dev);
209 gnss_serial_set_power(gserial, GNSS_SERIAL_OFF);
211 EXPORT_SYMBOL_GPL(gnss_serial_deregister);
214 static int gnss_serial_runtime_suspend(struct device *dev)
216 struct gnss_serial *gserial = dev_get_drvdata(dev);
218 return gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY);
221 static int gnss_serial_runtime_resume(struct device *dev)
223 struct gnss_serial *gserial = dev_get_drvdata(dev);
225 return gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE);
227 #endif /* CONFIG_PM */
229 static int gnss_serial_prepare(struct device *dev)
231 if (pm_runtime_suspended(dev))
237 #ifdef CONFIG_PM_SLEEP
238 static int gnss_serial_suspend(struct device *dev)
240 struct gnss_serial *gserial = dev_get_drvdata(dev);
244 * FIXME: serdev currently lacks support for managing the underlying
245 * device's wakeup settings. A workaround would be to close the serdev
246 * device here if it is open.
249 if (!pm_runtime_suspended(dev))
250 ret = gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY);
255 static int gnss_serial_resume(struct device *dev)
257 struct gnss_serial *gserial = dev_get_drvdata(dev);
260 if (!pm_runtime_suspended(dev))
261 ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE);
265 #endif /* CONFIG_PM_SLEEP */
267 const struct dev_pm_ops gnss_serial_pm_ops = {
268 .prepare = gnss_serial_prepare,
269 SET_SYSTEM_SLEEP_PM_OPS(gnss_serial_suspend, gnss_serial_resume)
270 SET_RUNTIME_PM_OPS(gnss_serial_runtime_suspend, gnss_serial_runtime_resume, NULL)
272 EXPORT_SYMBOL_GPL(gnss_serial_pm_ops);
274 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");
275 MODULE_DESCRIPTION("Generic serial GNSS receiver driver");
276 MODULE_LICENSE("GPL v2");