Received: by 10.192.165.148 with SMTP id m20csp4882789imm; Tue, 24 Apr 2018 09:55:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx48C6vhJWa1yWFm2li/77BNKbZ7RiyTCA4TiLqIg3vvkx9fiQX2Z+41mGrbgpxwPYw3zA6ko X-Received: by 10.99.142.66 with SMTP id k63mr20812099pge.278.1524588921930; Tue, 24 Apr 2018 09:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524588921; cv=none; d=google.com; s=arc-20160816; b=XexMbQUH0fW2vnpF6V02go5DtPISfd34pp+rAHZrjLXoUPHC6YHzYRgG7EyEfzkUux ZTux4glTBLRsyA96/FUrhrL4MCA85NcaFItS2VzFx58lerRRBLVUbmazhWyym+B7CP06 0Ta8+fHMRxbNKaE2YsVDixPxCe74a0cByi4Q6iYYwFHJcOngCldUiY7wXRiNwvB7/wXY JD0Wq384FpuNSgxObJSLVLt+Uk3sIAGXF6L/Kka4hQ/FCS8fA79UElmJxt+SJtMRIqGU zg7pdPwXl8TITrUyjf+yNKw66gVRMONXn8WCikzaUKHTaIrej93IJYMZjU0JKZU2zNQX iASQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=SI2uMEndzYAzkpxCKSHWnYGKyTVLw+kHWrwb8JC4rsA=; b=NQfHrLkNVNWjbPlMN94kerdK6PHIFI0QH+agpU3JkaG+q9LAawvgnPxDtm5PhT/712 vvskt7aVaj6deHiPJqGjp0nDK6SOwy1gsE8UzNNVE010h4E0x//k64dgPwRlDz3BuDt9 sVaCsttcRWfjee2/xkrA9H2sHKE6OvTwXRlUoolvf2y8xdSiptVnvGyk/rov1P1NNH7/ P6nzw97wgOjyJUQ8G9W07WT2aE5lJr/7JBK4U0vTRERZ/G2QA2n9fRV+7Gm4u03hIALH ST8D0WlPmZKqeXJ1VcxFt9CrRsO/X45diwKeu6CSIGlyGBGGFvMU7z8qUI3clusfDPOp Sahw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=oeUJuuZv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a1-v6si15175938plp.567.2018.04.24.09.55.07; Tue, 24 Apr 2018 09:55:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=oeUJuuZv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752582AbeDXQxN (ORCPT + 99 others); Tue, 24 Apr 2018 12:53:13 -0400 Received: from mail-lf0-f68.google.com ([209.85.215.68]:39745 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751097AbeDXQuq (ORCPT ); Tue, 24 Apr 2018 12:50:46 -0400 Received: by mail-lf0-f68.google.com with SMTP id p142-v6so21344247lfd.6; Tue, 24 Apr 2018 09:50:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=SI2uMEndzYAzkpxCKSHWnYGKyTVLw+kHWrwb8JC4rsA=; b=oeUJuuZv0yld9NMbWr6mhwFHLz0I5lRrmRDMptM8pi+2B0UUfS2ZFLt3z/dtMO+7hM cboTWcYYuAJ8XlsrKSM8MzYMqMZoVaQQxYxa9NMJ39VuJZNZi0Bf3+5X3I8pQOpf5aTl xwh2zlokPYM/sIQa3iNBZNHZ9T6woQA8HnoPzzXbVDhcwFAlnqg2j+MyU4+HKtBSDn6z erKNXr1ssMEn4BtYu1yWj9HPPAWd07FkcMcL424oWLFCEwHYAYa79sT1uwHw85JiisbA UZs24utYRMOOLIcsLVJuj+An2VKYEjEMmUxYx9MYLZjwonaXtM0fC329RDfANMnvUtMF x77A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=SI2uMEndzYAzkpxCKSHWnYGKyTVLw+kHWrwb8JC4rsA=; b=XV9ZOaf9Xvyh1kHJ1/qvlPLMuZpwbdV9IahIRQ1QGtfd6ayYvMNPFb+yRRpznSwjQW aUzkMigQJqiGIqtT8S5+kZtYB9osfyyGCMGs0YSJ1gxzv60MtiSCmC8d9/T+gHBe3v2E IRhBrVprGJ2ujcGpdyPKvjQ361+ae1b+2CUvZRYb6BSa82qqngaoW3bqqvexK8/K0k4V Zd9B+wp/q9aKXuiYr7RMezzIe1KSEQexK2dOFTTCyiddKniEU90AigPSTuo4Qdcvt+7u C1ZZ0A+NGl3/l3frxBJPoaY5xZh48aSm9po2i/FXipMyMQwkNc06Q9+P91qoB1kjcp0A ILgQ== X-Gm-Message-State: ALQs6tBw9rNWwVhMJryhRrLsuWzWV9LRz+L38RrxCLvVvqzzFdPjZkgo 2Rtv6CgIUfi6OSAE93A4e/w= X-Received: by 10.46.62.12 with SMTP id l12mr16784165lja.66.1524588644750; Tue, 24 Apr 2018 09:50:44 -0700 (PDT) Received: from xi.terra (c-8bb2e655.07-184-6d6c6d4.cust.bredbandsbolaget.se. [85.230.178.139]) by smtp.gmail.com with ESMTPSA id c64-v6sm3419843lfe.3.2018.04.24.09.50.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 09:50:43 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.90_1) (envelope-from ) id 1fB19J-0004Tl-RW; Tue, 24 Apr 2018 18:50:37 +0200 From: Johan Hovold To: Greg Kroah-Hartman , Rob Herring , Mark Rutland Cc: Andreas Kemnade , Arnd Bergmann , "H . Nikolaus Schaller" , Pavel Machek , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Johan Hovold Subject: [PATCH 3/7] gnss: add generic serial driver Date: Tue, 24 Apr 2018 18:34:54 +0200 Message-Id: <20180424163458.11947-4-johan@kernel.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424163458.11947-1-johan@kernel.org> References: <20180424163458.11947-1-johan@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a generic serial GNSS driver (library) which provides a common implementation for the gnss interface and power management (runtime and system suspend). This allows GNSS drivers for specific chip to be implemented by simply providing a set_power() callback to handle three states: ACTIVE, STANDBY and OFF. Signed-off-by: Johan Hovold --- drivers/gnss/Kconfig | 7 + drivers/gnss/Makefile | 3 + drivers/gnss/serial.c | 288 ++++++++++++++++++++++++++++++++++++++++++ drivers/gnss/serial.h | 47 +++++++ 4 files changed, 345 insertions(+) create mode 100644 drivers/gnss/serial.c create mode 100644 drivers/gnss/serial.h diff --git a/drivers/gnss/Kconfig b/drivers/gnss/Kconfig index 103fcc70992e..f8ee54f99a8d 100644 --- a/drivers/gnss/Kconfig +++ b/drivers/gnss/Kconfig @@ -9,3 +9,10 @@ menuconfig GNSS To compile this driver as a module, choose M here: the module will be called gnss. + +if GNSS + +config GNSS_SERIAL + tristate + +endif # GNSS diff --git a/drivers/gnss/Makefile b/drivers/gnss/Makefile index 1f7a7baab1d9..171aba71684d 100644 --- a/drivers/gnss/Makefile +++ b/drivers/gnss/Makefile @@ -5,3 +5,6 @@ obj-$(CONFIG_GNSS) += gnss.o gnss-y := core.o + +obj-$(CONFIG_GNSS_SERIAL) += gnss-serial.o +gnss-serial-y := serial.o diff --git a/drivers/gnss/serial.c b/drivers/gnss/serial.c new file mode 100644 index 000000000000..06a4b511f380 --- /dev/null +++ b/drivers/gnss/serial.c @@ -0,0 +1,288 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Generic serial GNSS receiver driver + * + * Copyright (C) 2018 Johan Hovold + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" + +static int gnss_serial_open(struct gnss_device *gdev) +{ + struct gnss_serial *gserial = gnss_get_drvdata(gdev); + struct serdev_device *serdev = gserial->serdev; + int ret; + + ret = serdev_device_open(serdev); + if (ret) + return ret; + + serdev_device_set_baudrate(serdev, gserial->speed); + serdev_device_set_flow_control(serdev, false); + + ret = pm_runtime_get_sync(&serdev->dev); + if (ret < 0) { + pm_runtime_put_noidle(&serdev->dev); + goto err_close; + } + + return 0; + +err_close: + serdev_device_close(serdev); + + return ret; +} + +static void gnss_serial_close(struct gnss_device *gdev) +{ + struct gnss_serial *gserial = gnss_get_drvdata(gdev); + struct serdev_device *serdev = gserial->serdev; + + serdev_device_close(serdev); + + pm_runtime_put(&serdev->dev); +} + +static int gnss_serial_write_raw(struct gnss_device *gdev, + const unsigned char *buf, size_t count) +{ + struct gnss_serial *gserial = gnss_get_drvdata(gdev); + struct serdev_device *serdev = gserial->serdev; + int ret; + + /* write is only buffered synchronously */ + ret = serdev_device_write(serdev, buf, count, 0); + if (ret < 0) + return ret; + + /* FIXME: determine if interrupted? */ + serdev_device_wait_until_sent(serdev, 0); + + return count; +} + +static const struct gnss_operations gnss_serial_gnss_ops = { + .open = gnss_serial_open, + .close = gnss_serial_close, + .write_raw = gnss_serial_write_raw, +}; + +static int gnss_serial_receive_buf(struct serdev_device *serdev, + const unsigned char *buf, size_t count) +{ + struct gnss_serial *gserial = serdev_device_get_drvdata(serdev); + struct gnss_device *gdev = gserial->gdev; + + return gnss_insert_raw(gdev, buf, count); +} + +static const struct serdev_device_ops gnss_serial_serdev_ops = { + .receive_buf = gnss_serial_receive_buf, + .write_wakeup = serdev_device_write_wakeup, +}; + +static int gnss_serial_set_power(struct gnss_serial *gserial, + enum gnss_serial_pm_state state) +{ + if (!gserial->ops || !gserial->ops->set_power) + return 0; + + return gserial->ops->set_power(gserial, state); +} + +/* + * FIXME: need to provide subdriver defaults or separate dt parsing from + * allocation. + */ +static int gnss_serial_parse_dt(struct serdev_device *serdev) +{ + struct gnss_serial *gserial = serdev_device_get_drvdata(serdev); + struct device_node *node = serdev->dev.of_node; + u32 speed = 4800; + + of_property_read_u32(node, "current-speed", &speed); + + gserial->speed = speed; + + return 0; +} + +struct gnss_serial * +gnss_serial_allocate(struct serdev_device *serdev, size_t data_size) +{ + struct gnss_serial *gserial; + struct gnss_device *gdev; + int ret; + + gserial = kzalloc(sizeof(*gserial) + data_size, GFP_KERNEL); + if (!gserial) + return ERR_PTR(-ENOMEM); + + gdev = gnss_allocate_device(&serdev->dev); + if (!gdev) { + ret = -ENOMEM; + goto err_free_gserial; + } + + gdev->ops = &gnss_serial_gnss_ops; + gnss_set_drvdata(gdev, gserial); + + gserial->serdev = serdev; + gserial->gdev = gdev; + + serdev_device_set_drvdata(serdev, gserial); + serdev_device_set_client_ops(serdev, &gnss_serial_serdev_ops); + + ret = gnss_serial_parse_dt(serdev); + if (ret) + goto err_put_device; + + return gserial; + +err_put_device: + gnss_put_device(gserial->gdev); +err_free_gserial: + kfree(gserial); + + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(gnss_serial_allocate); + +void gnss_serial_free(struct gnss_serial *gserial) +{ + gnss_put_device(gserial->gdev); + kfree(gserial); +}; +EXPORT_SYMBOL_GPL(gnss_serial_free); + +int gnss_serial_register(struct gnss_serial *gserial) +{ + struct serdev_device *serdev = gserial->serdev; + int ret; + + if (IS_ENABLED(CONFIG_PM)) { + pm_runtime_enable(&serdev->dev); + } else { + ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); + if (ret < 0) + return ret; + } + + ret = gnss_register_device(gserial->gdev); + if (ret) + goto err_disable_rpm; + + return 0; + +err_disable_rpm: + if (IS_ENABLED(CONFIG_PM)) + pm_runtime_disable(&serdev->dev); + else + gnss_serial_set_power(gserial, GNSS_SERIAL_OFF); + + return ret; +} +EXPORT_SYMBOL_GPL(gnss_serial_register); + +void gnss_serial_deregister(struct gnss_serial *gserial) +{ + struct serdev_device *serdev = gserial->serdev; + + gnss_deregister_device(gserial->gdev); + + if (IS_ENABLED(CONFIG_PM)) + pm_runtime_disable(&serdev->dev); + else + gnss_serial_set_power(gserial, GNSS_SERIAL_OFF); +} +EXPORT_SYMBOL_GPL(gnss_serial_deregister); + +#ifdef CONFIG_PM +static int gnss_serial_runtime_suspend(struct device *dev) +{ + struct gnss_serial *gserial = dev_get_drvdata(dev); + + dev_dbg(dev, "%s\n", __func__); + + return gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY); +} + +static int gnss_serial_runtime_resume(struct device *dev) +{ + struct gnss_serial *gserial = dev_get_drvdata(dev); + + dev_dbg(dev, "%s\n", __func__); + + return gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); +} +#endif /* CONFIG_PM */ + +static int gnss_serial_prepare(struct device *dev) +{ + dev_dbg(dev, "%s - pm_runtime_suspended = %d\n", __func__, + pm_runtime_suspended(dev)); + + if (pm_runtime_suspended(dev)) + return 1; + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int gnss_serial_suspend(struct device *dev) +{ + struct gnss_serial *gserial = dev_get_drvdata(dev); + int ret = 0; + + dev_dbg(dev, "%s - pm_runtime_suspended = %d\n", __func__, + pm_runtime_suspended(dev)); + + /* + * FIXME: serdev currently lacks support for managing the underlying + * device's wakeup settings. A workaround would be to close the serdev + * device here if it is open. + */ + + if (!pm_runtime_suspended(dev)) + ret = gnss_serial_set_power(gserial, GNSS_SERIAL_STANDBY); + + return ret; +} + +static int gnss_serial_resume(struct device *dev) +{ + struct gnss_serial *gserial = dev_get_drvdata(dev); + int ret = 0; + + dev_dbg(dev, "%s - pm_runtime_suspended = %d\n", __func__, + pm_runtime_suspended(dev)); + + if (!pm_runtime_suspended(dev)) + ret = gnss_serial_set_power(gserial, GNSS_SERIAL_ACTIVE); + + return ret; +} +#endif /* CONFIG_PM_SLEEP */ + +const struct dev_pm_ops gnss_serial_pm_ops = { + .prepare = gnss_serial_prepare, + SET_SYSTEM_SLEEP_PM_OPS(gnss_serial_suspend, gnss_serial_resume) + SET_RUNTIME_PM_OPS(gnss_serial_runtime_suspend, gnss_serial_runtime_resume, NULL) +}; +EXPORT_SYMBOL_GPL(gnss_serial_pm_ops); + +MODULE_AUTHOR("Johan Hovold "); +MODULE_DESCRIPTION("Generic serial GNSS receiver driver"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/gnss/serial.h b/drivers/gnss/serial.h new file mode 100644 index 000000000000..01e7573bc473 --- /dev/null +++ b/drivers/gnss/serial.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Generic serial GNSS receiver driver + * + * Copyright (C) 2018 Johan Hovold + */ + +#ifndef _LINUX_GNSS_SERIAL_H +#define _LINUX_GNSS_SERIAL_H + +#include +#include + +struct gnss_serial { + struct serdev_device *serdev; + struct gnss_device *gdev; + speed_t speed; + const struct gnss_serial_ops *ops; + unsigned long drvdata[0]; +}; + +enum gnss_serial_pm_state { + GNSS_SERIAL_OFF, + GNSS_SERIAL_ACTIVE, + GNSS_SERIAL_STANDBY, +}; + +struct gnss_serial_ops { + int (*set_power)(struct gnss_serial *gserial, + enum gnss_serial_pm_state state); +}; + +extern const struct dev_pm_ops gnss_serial_pm_ops; + +struct gnss_serial *gnss_serial_allocate(struct serdev_device *gserial, + size_t data_size); +void gnss_serial_free(struct gnss_serial *gserial); + +int gnss_serial_register(struct gnss_serial *gserial); +void gnss_serial_deregister(struct gnss_serial *gserial); + +static inline void *gnss_serial_get_drvdata(struct gnss_serial *gserial) +{ + return &gserial[1]; +} + +#endif /* _LINUX_GNSS_SERIAL_H */ -- 2.17.0