From patchwork Wed Aug 2 17:05:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Wang X-Patchwork-Id: 9877167 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DD5E2602BC for ; Wed, 2 Aug 2017 17:06:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C46A7287FE for ; Wed, 2 Aug 2017 17:06:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B91692880F; Wed, 2 Aug 2017 17:06:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3CC92287FE for ; Wed, 2 Aug 2017 17:06:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=NjKvhCs9156eV85SssOI8GWQjzi53wMwCwjwa8bX3as=; b=ZKjO21Akc3CfKD mgHOycVH1ngKGBBRxxU/VkHtMkra/XxL4g3MJfszGP/cbXUYhcSkXXOL8MPvc8mb35rCxaFo0Yrb3 9o7gfn/cld31Yojt7/vR2ejFeK+2LJWk2ePN9pm90L8QdIXdEz799dnZMicMm7O3wFm5s5bJfsJLd 0T7bmHLVgOc39/Jlajazf10CdnTJuKPhya1RZLxhXiGnUsjusZcCXBt1dlTHhN605VpdcTMXzo7Y6 W5j/DBQWEJ8sEpSvYiYwIPMleKyAepRGW96b05LCzG2IyQMQ0HaninZcJ3eeycLM1IDGrJ97Uo0MR /4oLjZ1WWwyVQL1NhwrA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dcx6T-0001MB-KU; Wed, 02 Aug 2017 17:06:37 +0000 Received: from [210.61.82.184] (helo=mailgw02.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dcx5r-0000Zg-4Z; Wed, 02 Aug 2017 17:06:02 +0000 Received: from mtkcas08.mediatek.inc [(172.21.101.126)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1662937974; Thu, 03 Aug 2017 01:05:30 +0800 Received: from mtkcas09.mediatek.inc (172.21.101.178) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Thu, 3 Aug 2017 01:05:29 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas09.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Thu, 3 Aug 2017 01:05:28 +0800 From: To: , , , , , , , , Subject: [PATCH 2/2] tty: serial: 8250: Add MediaTek BTIF controller on MT7622 and MT7623 SoC Date: Thu, 3 Aug 2017 01:05:23 +0800 Message-ID: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170802_100559_446689_3F7276C6 X-CRM114-Status: GOOD ( 23.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, Sean Wang , linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-serial@vger.kernel.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Sean Wang MediaTek BTIF controller is the serial interface similar to UART but it works only as the digital device which is mainly used to communicate with the connectivity module also called CONNSYS inside the SoC which could be mostly found on those MediaTek SoCs with Bluetooth feature. And the controller is made as being compatible with the 8250 register layout so it tends to be integrated with existing 8250 core driver and have no requirement for the modem configuration additionally such as the baud rate calculation and assignment. Signed-off-by: Sean Wang --- drivers/tty/serial/8250/8250_btif.c | 224 ++++++++++++++++++++++++++++++++++++ drivers/tty/serial/8250/Kconfig | 9 ++ drivers/tty/serial/8250/Makefile | 1 + 3 files changed, 234 insertions(+) create mode 100644 drivers/tty/serial/8250/8250_btif.c diff --git a/drivers/tty/serial/8250/8250_btif.c b/drivers/tty/serial/8250/8250_btif.c new file mode 100644 index 0000000..06433e9 --- /dev/null +++ b/drivers/tty/serial/8250/8250_btif.c @@ -0,0 +1,224 @@ +/* + * Driver for MediaTek BTIF Controller + * + * Copyright (C) 2017 Sean Wang + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "8250.h" + +#define MTK_BTIF_TRI_LVL 0x60 +#define BTIF_LOOP BIT(7) + +struct mtk_btif_data { + int line; + struct clk *main_clk; +}; + +static void +mtk_btif_do_pm(struct uart_port *port, unsigned int state, unsigned int old) +{ + if (!state) + pm_runtime_get_sync(port->dev); + + serial8250_do_pm(port, state, old); + + if (state) + pm_runtime_put_sync_suspend(port->dev); +} + +static int mtk_btif_probe_of(struct platform_device *pdev, struct uart_port *p, + struct mtk_btif_data *data) +{ + int err; + + data->main_clk = devm_clk_get(&pdev->dev, "main"); + if (IS_ERR(data->main_clk)) { + dev_warn(&pdev->dev, "Can't get main clock\n"); + return PTR_ERR(data->main_clk); + } + + err = clk_prepare_enable(data->main_clk); + if (err) { + dev_warn(&pdev->dev, "Can't prepare main_clk\n"); + clk_put(data->main_clk); + return err; + } + + p->uartclk = clk_get_rate(data->main_clk); + + return 0; +} + +static int mtk_btif_runtime_suspend(struct device *dev) +{ + struct mtk_btif_data *data = dev_get_drvdata(dev); + + clk_disable_unprepare(data->main_clk); + + return 0; +} + +static int mtk_btif_runtime_resume(struct device *dev) +{ + struct mtk_btif_data *data = dev_get_drvdata(dev); + int err; + + err = clk_prepare_enable(data->main_clk); + if (err) { + dev_warn(dev, "Can't enable main clock\n"); + return err; + } + + return 0; +} + +static int mtk_btif_probe(struct platform_device *pdev) +{ + struct uart_8250_port uart = {}; + struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); + struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); + struct mtk_btif_data *data; + int err; + u32 tmp; + + if (!regs || !irq) { + dev_err(&pdev->dev, "no registers/irq defined\n"); + return -EINVAL; + } + + uart.port.membase = devm_ioremap(&pdev->dev, regs->start, + resource_size(regs)); + if (!uart.port.membase) + return -ENOMEM; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + if (pdev->dev.of_node) { + err = mtk_btif_probe_of(pdev, &uart.port, data); + if (err) + return err; + } else { + return -ENODEV; + } + + spin_lock_init(&uart.port.lock); + uart.port.mapbase = regs->start; + uart.port.irq = irq->start; + uart.port.pm = mtk_btif_do_pm; + uart.port.type = PORT_8250; + uart.port.flags = UPF_FIXED_TYPE; + uart.port.dev = &pdev->dev; + uart.port.iotype = UPIO_MEM32; + uart.port.regshift = 2; + uart.port.private_data = data; + + platform_set_drvdata(pdev, data); + + pm_runtime_enable(&pdev->dev); + + if (!pm_runtime_enabled(&pdev->dev)) { + err = mtk_btif_runtime_resume(&pdev->dev); + if (err) + return err; + } + + if (of_property_read_bool(pdev->dev.of_node, "mediatek,loopback")) { + dev_info(&pdev->dev, "btif is entering loopback mode\n"); + tmp = readl(uart.port.membase + MTK_BTIF_TRI_LVL); + tmp |= BTIF_LOOP; + writel(tmp, uart.port.membase + MTK_BTIF_TRI_LVL); + } + + data->line = serial8250_register_8250_port(&uart); + if (data->line < 0) + return data->line; + + return 0; +} + +static int mtk_btif_remove(struct platform_device *pdev) +{ + struct mtk_btif_data *data = platform_get_drvdata(pdev); + + pm_runtime_get_sync(&pdev->dev); + + serial8250_unregister_port(data->line); + + pm_runtime_disable(&pdev->dev); + pm_runtime_put_noidle(&pdev->dev); + + if (!pm_runtime_status_suspended(&pdev->dev)) + mtk_btif_runtime_suspend(&pdev->dev); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int mtk_btif_suspend(struct device *dev) +{ + struct mtk_btif_data *data = dev_get_drvdata(dev); + + serial8250_suspend_port(data->line); + + return 0; +} + +static int mtk_btif_resume(struct device *dev) +{ + struct mtk_btif_data *data = dev_get_drvdata(dev); + + serial8250_resume_port(data->line); + + return 0; +} +#endif /* CONFIG_PM_SLEEP */ + +static const struct dev_pm_ops mtk_btif_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(mtk_btif_suspend, mtk_btif_resume) + SET_RUNTIME_PM_OPS(mtk_btif_runtime_suspend, mtk_btif_runtime_resume, + NULL) +}; + +static const struct of_device_id mtk_btif_of_match[] = { + { .compatible = "mediatek,mt7622-btif" }, + { .compatible = "mediatek,mt7623-btif" }, + { /* Sentinel */ } +}; +MODULE_DEVICE_TABLE(of, mtk_btif_of_match); + +static struct platform_driver mtk_btif_platform_driver = { + .driver = { + .name = "mediatek-btif", + .pm = &mtk_btif_pm_ops, + .of_match_table = mtk_btif_of_match, + }, + .probe = mtk_btif_probe, + .remove = mtk_btif_remove, +}; +module_platform_driver(mtk_btif_platform_driver); + +MODULE_AUTHOR("Sean Wang "); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("MediaTek BTIF controller driver"); diff --git a/drivers/tty/serial/8250/Kconfig b/drivers/tty/serial/8250/Kconfig index a1161ec..0e18cfc 100644 --- a/drivers/tty/serial/8250/Kconfig +++ b/drivers/tty/serial/8250/Kconfig @@ -403,6 +403,15 @@ config SERIAL_8250_MT6577 If you have a Mediatek based board and want to use the serial port, say Y to this option. If unsure, say N. +config SERIAL_8250_BTIF + tristate "MediaTek BTIF controller support" + depends on SERIAL_8250 && ARCH_MEDIATEK + help + Selecting this option will enable BTIF controller used by MediaTek + Bluetooth to communicate with internal hardware module inside the + SoC also being called CONNSYS which could be found on MediaTek SoCs + with Bluetooth feature, say Y to this option. If unsure, say N. + config SERIAL_8250_UNIPHIER tristate "Support for UniPhier on-chip UART" depends on SERIAL_8250 diff --git a/drivers/tty/serial/8250/Makefile b/drivers/tty/serial/8250/Makefile index a44a99a..2fc73ce 100644 --- a/drivers/tty/serial/8250/Makefile +++ b/drivers/tty/serial/8250/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_SERIAL_8250_EM) += 8250_em.o obj-$(CONFIG_SERIAL_8250_OMAP) += 8250_omap.o obj-$(CONFIG_SERIAL_8250_LPC18XX) += 8250_lpc18xx.o obj-$(CONFIG_SERIAL_8250_MT6577) += 8250_mtk.o +obj-$(CONFIG_SERIAL_8250_BTIF) += 8250_btif.o obj-$(CONFIG_SERIAL_8250_UNIPHIER) += 8250_uniphier.o obj-$(CONFIG_SERIAL_8250_INGENIC) += 8250_ingenic.o obj-$(CONFIG_SERIAL_8250_LPSS) += 8250_lpss.o