18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Generic serial GNSS receiver driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Johan Hovold <johan@kernel.org> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/errno.h> 98c2ecf20Sopenharmony_ci#include <linux/gnss.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/of.h> 148c2ecf20Sopenharmony_ci#include <linux/pm.h> 158c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 168c2ecf20Sopenharmony_ci#include <linux/sched.h> 178c2ecf20Sopenharmony_ci#include <linux/serdev.h> 188c2ecf20Sopenharmony_ci#include <linux/slab.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include "serial.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic int gnss_serial_open(struct gnss_device *gdev) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci struct gnss_serial *gserial = gnss_get_drvdata(gdev); 258c2ecf20Sopenharmony_ci struct serdev_device *serdev = gserial->serdev; 268c2ecf20Sopenharmony_ci int ret; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci ret = serdev_device_open(serdev); 298c2ecf20Sopenharmony_ci if (ret) 308c2ecf20Sopenharmony_ci return ret; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci serdev_device_set_baudrate(serdev, gserial->speed); 338c2ecf20Sopenharmony_ci serdev_device_set_flow_control(serdev, false); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci ret = pm_runtime_get_sync(&serdev->dev); 368c2ecf20Sopenharmony_ci if (ret < 0) { 378c2ecf20Sopenharmony_ci pm_runtime_put_noidle(&serdev->dev); 388c2ecf20Sopenharmony_ci goto err_close; 398c2ecf20Sopenharmony_ci } 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci return 0; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cierr_close: 448c2ecf20Sopenharmony_ci serdev_device_close(serdev); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci return ret; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic void gnss_serial_close(struct gnss_device *gdev) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci struct gnss_serial *gserial = gnss_get_drvdata(gdev); 528c2ecf20Sopenharmony_ci struct serdev_device *serdev = gserial->serdev; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci serdev_device_close(serdev); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci pm_runtime_put(&serdev->dev); 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic int gnss_serial_write_raw(struct gnss_device *gdev, 608c2ecf20Sopenharmony_ci const unsigned char *buf, size_t count) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct gnss_serial *gserial = gnss_get_drvdata(gdev); 638c2ecf20Sopenharmony_ci struct serdev_device *serdev = gserial->serdev; 648c2ecf20Sopenharmony_ci int ret; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci /* write is only buffered synchronously */ 678c2ecf20Sopenharmony_ci ret = serdev_device_write(serdev, buf, count, MAX_SCHEDULE_TIMEOUT); 688c2ecf20Sopenharmony_ci if (ret < 0 || ret < count) 698c2ecf20Sopenharmony_ci return ret; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* FIXME: determine if interrupted? */ 728c2ecf20Sopenharmony_ci serdev_device_wait_until_sent(serdev, 0); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return count; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic const struct gnss_operations gnss_serial_gnss_ops = { 788c2ecf20Sopenharmony_ci .open = gnss_serial_open, 798c2ecf20Sopenharmony_ci .close = gnss_serial_close, 808c2ecf20Sopenharmony_ci .write_raw = gnss_serial_write_raw, 818c2ecf20Sopenharmony_ci}; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic int gnss_serial_receive_buf(struct serdev_device *serdev, 848c2ecf20Sopenharmony_ci const unsigned char *buf, size_t count) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct gnss_serial *gserial = serdev_device_get_drvdata(serdev); 878c2ecf20Sopenharmony_ci struct gnss_device *gdev = gserial->gdev; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return gnss_insert_raw(gdev, buf, count); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic const struct serdev_device_ops gnss_serial_serdev_ops = { 938c2ecf20Sopenharmony_ci .receive_buf = gnss_serial_receive_buf, 948c2ecf20Sopenharmony_ci .write_wakeup = serdev_device_write_wakeup, 958c2ecf20Sopenharmony_ci}; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic int gnss_serial_set_power(struct gnss_serial *gserial, 988c2ecf20Sopenharmony_ci enum gnss_serial_pm_state state) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci if (!gserial->ops || !gserial->ops->set_power) 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return gserial->ops->set_power(gserial, state); 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* 1078c2ecf20Sopenharmony_ci * FIXME: need to provide subdriver defaults or separate dt parsing from 1088c2ecf20Sopenharmony_ci * allocation. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_cistatic int gnss_serial_parse_dt(struct serdev_device *serdev) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct gnss_serial *gserial = serdev_device_get_drvdata(serdev); 1138c2ecf20Sopenharmony_ci struct device_node *node = serdev->dev.of_node; 1148c2ecf20Sopenharmony_ci u32 speed = 4800; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci of_property_read_u32(node, "current-speed", &speed); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci gserial->speed = speed; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistruct gnss_serial *gnss_serial_allocate(struct serdev_device *serdev, 1248c2ecf20Sopenharmony_ci size_t data_size) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct gnss_serial *gserial; 1278c2ecf20Sopenharmony_ci struct gnss_device *gdev; 1288c2ecf20Sopenharmony_ci int ret; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci gserial = kzalloc(sizeof(*gserial) + data_size, GFP_KERNEL); 1318c2ecf20Sopenharmony_ci if (!gserial) 1328c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci gdev = gnss_allocate_device(&serdev->dev); 1358c2ecf20Sopenharmony_ci if (!gdev) { 1368c2ecf20Sopenharmony_ci ret = -ENOMEM; 1378c2ecf20Sopenharmony_ci goto err_free_gserial; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci gdev->ops = &gnss_serial_gnss_ops; 1418c2ecf20Sopenharmony_ci gnss_set_drvdata(gdev, gserial); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci gserial->serdev = serdev; 1448c2ecf20Sopenharmony_ci gserial->gdev = gdev; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci serdev_device_set_drvdata(serdev, gserial); 1478c2ecf20Sopenharmony_ci serdev_device_set_client_ops(serdev, &gnss_serial_serdev_ops); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci ret = gnss_serial_parse_dt(serdev); 1508c2ecf20Sopenharmony_ci if (ret) 1518c2ecf20Sopenharmony_ci goto err_put_device; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci return gserial; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cierr_put_device: 1568c2ecf20Sopenharmony_ci gnss_put_device(gserial->gdev); 1578c2ecf20Sopenharmony_cierr_free_gserial: 1588c2ecf20Sopenharmony_ci kfree(gserial); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci return ERR_PTR(ret); 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gnss_serial_allocate); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_civoid gnss_serial_free(struct gnss_serial *gserial) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci gnss_put_device(gserial->gdev); 1678c2ecf20Sopenharmony_ci kfree(gserial); 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gnss_serial_free); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ciint gnss_serial_register(struct gnss_serial *gserial) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci struct serdev_device *serdev = gserial->serdev; 1748c2ecf20Sopenharmony_ci int ret; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PM)) { 1778c2ecf20Sopenharmony_ci pm_runtime_enable(&serdev->dev); 1788c2ecf20Sopenharmony_ci } else { 1798c2ecf20Sopenharmony_ci ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); 1808c2ecf20Sopenharmony_ci if (ret < 0) 1818c2ecf20Sopenharmony_ci return ret; 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci ret = gnss_register_device(gserial->gdev); 1858c2ecf20Sopenharmony_ci if (ret) 1868c2ecf20Sopenharmony_ci goto err_disable_rpm; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci return 0; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cierr_disable_rpm: 1918c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PM)) 1928c2ecf20Sopenharmony_ci pm_runtime_disable(&serdev->dev); 1938c2ecf20Sopenharmony_ci else 1948c2ecf20Sopenharmony_ci gnss_serial_set_power(gserial, GNSS_SERIAL_OFF); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci return ret; 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gnss_serial_register); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_civoid gnss_serial_deregister(struct gnss_serial *gserial) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct serdev_device *serdev = gserial->serdev; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci gnss_deregister_device(gserial->gdev); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PM)) 2078c2ecf20Sopenharmony_ci pm_runtime_disable(&serdev->dev); 2088c2ecf20Sopenharmony_ci else 2098c2ecf20Sopenharmony_ci gnss_serial_set_power(gserial, GNSS_SERIAL_OFF); 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gnss_serial_deregister); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 2148c2ecf20Sopenharmony_cistatic int gnss_serial_runtime_suspend(struct device *dev) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci struct gnss_serial *gserial = dev_get_drvdata(dev); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci return gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic int gnss_serial_runtime_resume(struct device *dev) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct gnss_serial *gserial = dev_get_drvdata(dev); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */ 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cistatic int gnss_serial_prepare(struct device *dev) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci if (pm_runtime_suspended(dev)) 2328c2ecf20Sopenharmony_ci return 1; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci return 0; 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 2388c2ecf20Sopenharmony_cistatic int gnss_serial_suspend(struct device *dev) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci struct gnss_serial *gserial = dev_get_drvdata(dev); 2418c2ecf20Sopenharmony_ci int ret = 0; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci /* 2448c2ecf20Sopenharmony_ci * FIXME: serdev currently lacks support for managing the underlying 2458c2ecf20Sopenharmony_ci * device's wakeup settings. A workaround would be to close the serdev 2468c2ecf20Sopenharmony_ci * device here if it is open. 2478c2ecf20Sopenharmony_ci */ 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (!pm_runtime_suspended(dev)) 2508c2ecf20Sopenharmony_ci ret = gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci return ret; 2538c2ecf20Sopenharmony_ci} 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cistatic int gnss_serial_resume(struct device *dev) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci struct gnss_serial *gserial = dev_get_drvdata(dev); 2588c2ecf20Sopenharmony_ci int ret = 0; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (!pm_runtime_suspended(dev)) 2618c2ecf20Sopenharmony_ci ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return ret; 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */ 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ciconst struct dev_pm_ops gnss_serial_pm_ops = { 2688c2ecf20Sopenharmony_ci .prepare = gnss_serial_prepare, 2698c2ecf20Sopenharmony_ci SET_SYSTEM_SLEEP_PM_OPS(gnss_serial_suspend, gnss_serial_resume) 2708c2ecf20Sopenharmony_ci SET_RUNTIME_PM_OPS(gnss_serial_runtime_suspend, gnss_serial_runtime_resume, NULL) 2718c2ecf20Sopenharmony_ci}; 2728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gnss_serial_pm_ops); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Johan Hovold <johan@kernel.org>"); 2758c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic serial GNSS receiver driver"); 2768c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 277