Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1176707imj; Sat, 16 Feb 2019 23:16:41 -0800 (PST) X-Google-Smtp-Source: AHgI3IbDuMTcvmLajVIuGUS7Z9filSqLjJKKwjb9AqdQJdMAfCqFOskqBfOl6dYhkhnXDTj/6OMF X-Received: by 2002:a63:e10:: with SMTP id d16mr13216232pgl.148.1550387801507; Sat, 16 Feb 2019 23:16:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550387801; cv=none; d=google.com; s=arc-20160816; b=A9XH8gwOI8utL5eT6qltk49d/ktE46Hhp3YrgBlzhW3pBM8c5RqvR6qVZzLmXjpy9D TU7W/+1rg60Zot7vNcU9X5D6JefYH12VqLN07knqDDvdRj5KD8TfZVyf7L1WDMayHdoP rNOtHegyBhRoRb59+RZvES26/KWvgOFgpyP7Xyg+fURaK638zJZbEo5dZEl/NurwshWP pbI4xOXjS70wRqVKkDDQBoeTr6/fgdicKFIE8z2ONXP6EdVdAPeE1onQWly64RZb8wjO h+iXAZUUZPlfTL93yTtTtMjXg5vbIuSKtMGrDtlZ/FhRQkUQ+RUodLtNLvdXYB2MQn96 Sa+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=ej3ldC2HNLoBFAGyYVx3I8ETyJoB90qa6HCx5Dplbh8=; b=HZ5glMi7YgaIY+PIn2pA6DAfENM+ZjXlxLSsYvQUeRDYCzSpMH3P6NknghdqHF9Dpi KLNBmmqZ5z07LaSm3moOw88wbnCVkpYojgmv1tyz7zV1+2F3qsZ8qM/UWhdQ2lQ2nLOr dKCo92MsldydBn4GjxWqZQjOydUXfNthx45hGBZHj1vxw7jqPWEbnnjt/JVQWprysIHh kv/iyn7mxfvYcOVSQ8gjpV/jtoNa8l/ZltgLhf6DQVot5XunV2dynGdQrp1chVy9S2si 0eIOWzEFiL4Wo0J6TJeoJQyM05224GXY6pGMdPhP3WxMsS4I2alPhBY37NM6QBWfjbI8 lHgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gHlDzH74; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 35si9437777ple.125.2019.02.16.23.16.25; Sat, 16 Feb 2019 23:16:41 -0800 (PST) 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=pass header.i=@gmail.com header.s=20161025 header.b=gHlDzH74; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732868AbfBPTiA (ORCPT + 99 others); Sat, 16 Feb 2019 14:38:00 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:40004 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732839AbfBPTiA (ORCPT ); Sat, 16 Feb 2019 14:38:00 -0500 Received: by mail-pg1-f195.google.com with SMTP id u9so2897027pgo.7; Sat, 16 Feb 2019 11:37:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ej3ldC2HNLoBFAGyYVx3I8ETyJoB90qa6HCx5Dplbh8=; b=gHlDzH74kHxYnqN2z759F0HWGdp/0qm48nKH6CapyULeD0aEFQh/Q0m7PUyC3/CCTw f69pZkFoqseTwaPuq25/4eg12YPvaGamlBpoySxWPf5L3Q91RRD00QsqwS9pSnhhW7IS SHavSRKNYzw+EFqzz552iZZApyirxZfErZdzei2rtpJ5NWNIKghsUhfjIw1djD3Ymdxe yjhg/S5SLqoxZ9dKgA9VEB6zAPCMlU+t4tAi8epK1+IwNTwlinwd5o9gsDJpOvqCehBD OcQB4VYp8PW152dOUjfKJ5qmbpEINbfNT0EUfcfA4XHfnR5PeKUUNeOnF+qUKdGNQowP jOSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ej3ldC2HNLoBFAGyYVx3I8ETyJoB90qa6HCx5Dplbh8=; b=o2jorPprApteGvkxbbFzt4XrrJ+nE1i89lc2DYcYX+YrJetb5wN3fGDqN5Im2JXAIO 7CB4gcPT0lktpmBumezftNUZUOhtO+RmPzpX9Ns5AwvjiDHY4myyQ3kCSMV0K8EW5r/v YJdExT4kc/Mu4a/nmJHfRAI4mQ4sXOhO9YmzUZPXM5Jas0VHXBLJiO61C4bq0n/NmWUK hkzsRMVA0i9Qhjs60nCcT/BOOEpF6DFwv0RhZ4qvS6xOy8Bz+txZYEUnZseBMjCrLxrc KYVL1I3P1o5T3fH0NW4malpZPQrERVpqip1Gfgxo3YarkevCmuBJMih8wr389JEbpioP yZKw== X-Gm-Message-State: AHQUAuYxKzX3C0ZQmbSVrBr8dooDw6RTq/4tUtm7O/s4zFRGPTreMpZX DvOy/UgVNAILbOIq5+g1eCw= X-Received: by 2002:a63:1723:: with SMTP id x35mr11094129pgl.364.1550345878851; Sat, 16 Feb 2019 11:37:58 -0800 (PST) Received: from localhost.localdomain ([43.224.245.179]) by smtp.gmail.com with ESMTPSA id k19sm9255725pfi.126.2019.02.16.11.37.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 16 Feb 2019 11:37:58 -0800 (PST) From: Xiang Xiao X-Google-Original-From: Xiang Xiao To: Ohad Ben Cohen , Bjorn Andersson , wendy.liang@xilinx.com, Arnaud POULIQUEN , Kumar Gala , linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Yanlin Zhu Subject: [PATCH] rpmsg: add clock API redirection driver Date: Sun, 17 Feb 2019 03:37:47 +0800 Message-Id: <1550345867-25762-1-git-send-email-xiaoxiang@xiaomi.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yanlin Zhu which could redirect clk API from remote to the kernel Signed-off-by: Yanlin Zhu --- drivers/rpmsg/Kconfig | 10 ++ drivers/rpmsg/Makefile | 1 + drivers/rpmsg/rpmsg_clk.c | 284 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 295 insertions(+) create mode 100644 drivers/rpmsg/rpmsg_clk.c diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig index 13ead55..ed04cb9 100644 --- a/drivers/rpmsg/Kconfig +++ b/drivers/rpmsg/Kconfig @@ -15,6 +15,16 @@ config RPMSG_CHAR in /dev. They make it possible for user-space programs to send and receive rpmsg packets. +config RPMSG_CLK + tristate "RPMSG clock API redirection" + depends on COMMON_CLK + depends on RPMSG + help + Say Y here to redirect clock API from the remote processor. + With this driver, the remote processor could: + 1.Reuse the clock driver in the kernel side, or + 2.Form a hybrid(kernel plus RTOS) clock tree. + config RPMSG_SYSLOG tristate "RPMSG syslog redirection" depends on RPMSG diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile index bfd22df..0d777b1 100644 --- a/drivers/rpmsg/Makefile +++ b/drivers/rpmsg/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_RPMSG) += rpmsg_core.o obj-$(CONFIG_RPMSG_CHAR) += rpmsg_char.o +obj-$(CONFIG_RPMSG_CLK) += rpmsg_clk.o obj-$(CONFIG_RPMSG_SYSLOG) += rpmsg_syslog.o obj-$(CONFIG_RPMSG_QCOM_GLINK_RPM) += qcom_glink_rpm.o obj-$(CONFIG_RPMSG_QCOM_GLINK_NATIVE) += qcom_glink_native.o diff --git a/drivers/rpmsg/rpmsg_clk.c b/drivers/rpmsg/rpmsg_clk.c new file mode 100644 index 0000000..0ec0241 --- /dev/null +++ b/drivers/rpmsg/rpmsg_clk.c @@ -0,0 +1,284 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2016 Pinecone Inc. + * + * redirect clk API from remote to the kernel. + */ + +#include +#include +#include +#include + +#define RPMSG_CLK_ENABLE 0 +#define RPMSG_CLK_DISABLE 1 +#define RPMSG_CLK_SETRATE 2 +#define RPMSG_CLK_SETPHASE 3 +#define RPMSG_CLK_GETPHASE 4 +#define RPMSG_CLK_GETRATE 5 +#define RPMSG_CLK_ROUNDRATE 6 +#define RPMSG_CLK_ISENABLED 7 + +struct rpmsg_clk_header { + u32 command; + u32 response; + s64 result; + u64 cookie; +} __packed; + +struct rpmsg_clk_enable { + struct rpmsg_clk_header header; + char name[0]; +} __packed; + +#define rpmsg_clk_disable rpmsg_clk_enable +#define rpmsg_clk_isenabled rpmsg_clk_enable + +struct rpmsg_clk_setrate { + struct rpmsg_clk_header header; + u64 rate; + char name[0]; +} __packed; + +#define rpmsg_clk_getrate rpmsg_clk_enable +#define rpmsg_clk_roundrate rpmsg_clk_setrate + +struct rpmsg_clk_setphase { + struct rpmsg_clk_header header; + u32 degrees; + char name[0]; +} __packed; + +#define rpmsg_clk_getphase rpmsg_clk_enable + +struct rpmsg_clk_res { + struct clk *clk; + atomic_t count; +}; + +static void rpmsg_clk_release(struct device *dev, void *res) +{ + struct rpmsg_clk_res *clkres = res; + int count = atomic_read(&clkres->count); + + while (count-- > 0) + clk_disable_unprepare(clkres->clk); + + clk_put(clkres->clk); +} + +static int rpmsg_clk_match(struct device *dev, void *res, void *data) +{ + struct rpmsg_clk_res *clkres = res; + + return !strcmp(__clk_get_name(clkres->clk), data); +} + +static struct rpmsg_clk_res * +rpmsg_clk_get_res(struct rpmsg_device *rpdev, const char *name) +{ + struct rpmsg_clk_res *clkres; + struct clk *clk; + + clkres = devres_find(&rpdev->dev, rpmsg_clk_release, + rpmsg_clk_match, (void *)name); + if (clkres) + return clkres; + + clkres = devres_alloc(rpmsg_clk_release, sizeof(*clkres), GFP_KERNEL); + if (!clkres) + return ERR_PTR(-ENOMEM); + + clk = clk_get(&rpdev->dev, name); + if (IS_ERR(clk)) { + devres_free(clkres); + return ERR_CAST(clk); + } + + clkres->clk = clk; + devres_add(&rpdev->dev, clkres); + + return clkres; +} + +static int rpmsg_clk_enable_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_enable *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) { + msg->header.result = clk_prepare_enable(clkres->clk); + if (msg->header.result >= 0) + atomic_inc(&clkres->count); + } else { + msg->header.result = PTR_ERR(clkres); + } + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_disable_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_disable *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) { + msg->header.result = 0; + if (atomic_dec_return(&clkres->count) >= 0) + clk_disable_unprepare(clkres->clk); + else + atomic_inc(&clkres->count); + } else { + msg->header.result = PTR_ERR(clkres); + } + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_getrate_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_getrate *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = clk_get_rate(clkres->clk); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_roundrate_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_roundrate *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = clk_round_rate(clkres->clk, msg->rate); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_setrate_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_setrate *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = clk_set_rate(clkres->clk, msg->rate); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_setphase_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_setphase *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = clk_set_phase(clkres->clk, msg->degrees); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_getphase_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_getphase *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = clk_get_phase(clkres->clk); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static int rpmsg_clk_isenabled_handler(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_isenabled *msg = data; + struct rpmsg_clk_res *clkres = rpmsg_clk_get_res(rpdev, msg->name); + + if (!IS_ERR(clkres)) + msg->header.result = __clk_is_enabled(clkres->clk); + else + msg->header.result = PTR_ERR(clkres); + + return rpmsg_send(rpdev->ept, msg, sizeof(*msg)); +} + +static const rpmsg_rx_cb_t rpmsg_clk_handler[] = { + [RPMSG_CLK_ENABLE] = rpmsg_clk_enable_handler, + [RPMSG_CLK_DISABLE] = rpmsg_clk_disable_handler, + [RPMSG_CLK_SETRATE] = rpmsg_clk_setrate_handler, + [RPMSG_CLK_SETPHASE] = rpmsg_clk_setphase_handler, + [RPMSG_CLK_GETPHASE] = rpmsg_clk_getphase_handler, + [RPMSG_CLK_GETRATE] = rpmsg_clk_getrate_handler, + [RPMSG_CLK_ROUNDRATE] = rpmsg_clk_roundrate_handler, + [RPMSG_CLK_ISENABLED] = rpmsg_clk_isenabled_handler, +}; + +static int rpmsg_clk_callback(struct rpmsg_device *rpdev, + void *data, int len, void *priv, u32 src) +{ + struct rpmsg_clk_header *hdr = data; + u32 cmd = hdr->command; + int ret = -EINVAL; + + if (cmd < ARRAY_SIZE(rpmsg_clk_handler)) { + hdr->response = 1; + ret = rpmsg_clk_handler[cmd](rpdev, data, len, priv, src); + } else { + dev_err(&rpdev->dev, "invalid command %u\n", cmd); + } + + return ret; +} + +static int rpmsg_clk_probe(struct rpmsg_device *rpdev) +{ + return 0; +} + +static void rpmsg_clk_remove(struct rpmsg_device *rpdev) +{ +} + +static const struct rpmsg_device_id rpmsg_clk_id_table[] = { + { .name = "rpmsg-clk" }, + { } +}; +MODULE_DEVICE_TABLE(rpmsg, rpmsg_clk_id_table); + +static struct rpmsg_driver rpmsg_clk_driver = { + .drv = { + .name = "rpmsg_clk", + .owner = THIS_MODULE, + }, + + .id_table = rpmsg_clk_id_table, + .probe = rpmsg_clk_probe, + .callback = rpmsg_clk_callback, + .remove = rpmsg_clk_remove, +}; + +module_rpmsg_driver(rpmsg_clk_driver); + +MODULE_ALIAS("rpmsg:rpmsg_clk"); +MODULE_AUTHOR("Yanlin Zhu "); +MODULE_DESCRIPTION("rpmsg clock API redirection driver"); +MODULE_LICENSE("GPL v2"); -- 2.7.4