From patchwork Sun Jul 1 11:08:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andreas_F=C3=A4rber?= X-Patchwork-Id: 10498661 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 5B7C3602D6 for ; Sun, 1 Jul 2018 12:15:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 48ACA28AB8 for ; Sun, 1 Jul 2018 12:15:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3AFAE28AF8; Sun, 1 Jul 2018 12:15:40 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.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 1FB6A28AB8 for ; Sun, 1 Jul 2018 12:15:39 +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=9QXVcJy3U76J6H/R3y/s5zSsVZoEQaMLNwoE5RnCEMU=; b=t/TAIWAuQ2EQ2y fvZm1+gOOSm2evkTk4OwbkroWBcYwO8OUweSNijQAb+U2FyRmKreBaqosbrjaylggvYgwdPZDJ1rI ILy5EJLHWyWZApdoWnVFFeSEspRnCYgVV2m6ZWajlpFqD1732lwWHmC5Rpy4rSQpktyde4rlr8Kqi TX5TX+G4GbgjmAoGAV1hb3F47e9vyLkZjkaoQiyqUb9tG0rz//Q9eiNf+yrsmNeoUPOaFZwh90Y4d I0riRf4yf9/HTJQOEVb3gsk7So2EIzGimKQ4tpdpRTbpxPzUG4KPJplfDPJBEwv3wCK4xovTCH7UN 3wY8a813OrCzlLlAE4QA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fZbGQ-0003Rv-KL; Sun, 01 Jul 2018 12:15:34 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fZb6a-0003Y6-M5 for linux-arm-kernel@bombadil.infradead.org; Sun, 01 Jul 2018 12:05:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=811dXG3ep/C87uS6miuCKqZ2X0nHlbnq3jNqB+S7urY=; b=OqvMS/0yOEtGNAyAptZo8dhJmO T5hh8mWQ6nltbN1gdO5TXr4CPRkApn5R+x/gfiIufXpjTv569RMuTkIaaqfwxK3m3/iH9beoZToE2 tmjXXFWYIXVsjebLpULB12eKL4D87mYn5ecediza8+gQBL/Q5whaNkR+or6D+ziE9iRxvpuHM9Tfz jOGjtkfI7rVbVsVulsMWomWpo9RYI0wJTgA+kg1d1MxYDehZwFUshveGZDINgQ2Abduc9EFgEJe+l Mu1ub9Z9TwMDVBQixZg1Jzueb6/BIfYLo9q818EAz9ghAfFsjq7Q9+/QJlVonXjBVf2pGAh/pX70G 7I4dme/g==; Received: from mx2.suse.de ([195.135.220.15] helo=mx1.suse.de) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fZaEn-0003dO-9p for linux-arm-kernel@lists.infradead.org; Sun, 01 Jul 2018 11:09:51 +0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 74E4DAFBD; Sun, 1 Jul 2018 11:09:04 +0000 (UTC) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: netdev@vger.kernel.org Subject: [RFC net-next 15/15] net: lora: Add Semtech SX1301 Date: Sun, 1 Jul 2018 13:08:04 +0200 Message-Id: <20180701110804.32415-16-afaerber@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180701110804.32415-1-afaerber@suse.de> References: <20180701110804.32415-1-afaerber@suse.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180701_070949_556277_B4699FB0 X-CRM114-Status: GOOD ( 23.78 ) 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: Steve deRosier , Matthias Brugger , Jiri Pirko , Ben Whitten , Marcel Holtmann , Dollar Chen , linux-kernel@vger.kernel.org, =?UTF-8?q?Michael=20R=C3=B6der?= , Janus Piwek , linux-spi@vger.kernel.org, Mark Brown , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Jian-Hong Pan , Ken Yu , "David S . Miller" , 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 The Semtech SX1301 was the first multi-channel LoRa "concentrator". It uses a SPI interface to the host as well as a dual SPI interface to its radios. These two have been implemented as spi_controller, so that the Device Tree can specify whether the respective module uses two SX1257, two SX1255 or a combination of these or some unforeseen chipset. This implementation is the most recent - initialization is not yet complete, it will need to load firmware into the two on-chip MCUs. Unfortunately there is no full datasheet with register descriptions, only a BSD-licensed userspace HAL implementation using spidev devices. Therefore some register names are unknown. Cc: Ben Whitten Cc: Steve deRosier Cc: Mark Brown Cc: Michael Röder Cc: Ken Yu (禹凯) Cc: linux-spi@vger.kernel.org Signed-off-by: Andreas Färber --- drivers/net/lora/Kconfig | 7 + drivers/net/lora/Makefile | 3 + drivers/net/lora/sx1301.c | 446 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 456 insertions(+) create mode 100644 drivers/net/lora/sx1301.c diff --git a/drivers/net/lora/Kconfig b/drivers/net/lora/Kconfig index 68c7480d7812..950450e353b4 100644 --- a/drivers/net/lora/Kconfig +++ b/drivers/net/lora/Kconfig @@ -45,6 +45,13 @@ config LORA_SX1276 help Semtech SX1272/1276/1278 +config LORA_SX1301 + tristate "Semtech SX1301 SPI driver" + default y + depends on SPI + help + Semtech SX1301 + config LORA_USI tristate "USI WM-SG-SM-42 driver" default y diff --git a/drivers/net/lora/Makefile b/drivers/net/lora/Makefile index 44c578bde7d5..1cc1e3aa189b 100644 --- a/drivers/net/lora/Makefile +++ b/drivers/net/lora/Makefile @@ -22,6 +22,9 @@ lora-sx1257-y := sx1257.o obj-$(CONFIG_LORA_SX1276) += lora-sx1276.o lora-sx1276-y := sx1276.o +obj-$(CONFIG_LORA_SX1301) += lora-sx1301.o +lora-sx1301-y := sx1301.o + obj-$(CONFIG_LORA_USI) += lora-usi.o lora-usi-y := usi.o diff --git a/drivers/net/lora/sx1301.c b/drivers/net/lora/sx1301.c new file mode 100644 index 000000000000..5c936c1116d1 --- /dev/null +++ b/drivers/net/lora/sx1301.c @@ -0,0 +1,446 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Semtech SX1301 LoRa concentrator + * + * Copyright (c) 2018 Andreas Färber + * + * Based on SX1301 HAL code: + * Copyright (c) 2013 Semtech-Cycleo + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define REG_PAGE_RESET 0 +#define REG_VERSION 1 +#define REG_2_SPI_RADIO_A_DATA 33 +#define REG_2_SPI_RADIO_A_DATA_READBACK 34 +#define REG_2_SPI_RADIO_A_ADDR 35 +#define REG_2_SPI_RADIO_A_CS 37 +#define REG_2_SPI_RADIO_B_DATA 38 +#define REG_2_SPI_RADIO_B_DATA_READBACK 39 +#define REG_2_SPI_RADIO_B_ADDR 40 +#define REG_2_SPI_RADIO_B_CS 42 + +#define REG_PAGE_RESET_SOFT_RESET BIT(7) + +#define REG_16_GLOBAL_EN BIT(3) + +#define REG_17_CLK32M_EN BIT(0) + +#define REG_2_43_RADIO_A_EN BIT(0) +#define REG_2_43_RADIO_B_EN BIT(1) +#define REG_2_43_RADIO_RST BIT(2) + +struct spi_sx1301 { + struct spi_device *parent; + u8 page; + u8 regs; +}; + +struct sx1301_priv { + struct lora_priv lora; + struct gpio_desc *rst_gpio; + u8 cur_page; + struct spi_controller *radio_a_ctrl, *radio_b_ctrl; +}; + +static int sx1301_read(struct spi_device *spi, u8 reg, u8 *val) +{ + u8 addr = reg & 0x7f; + return spi_write_then_read(spi, &addr, 1, val, 1); +} + +static int sx1301_write(struct spi_device *spi, u8 reg, u8 val) +{ + u8 buf[2]; + + buf[0] = reg | BIT(7); + buf[1] = val; + return spi_write(spi, buf, 2); +} + +static int sx1301_page_switch(struct spi_device *spi, u8 page) +{ + struct sx1301_priv *priv = spi_get_drvdata(spi); + int ret; + + if (priv->cur_page == page) + return 0; + + dev_dbg(&spi->dev, "switching to page %u\n", (unsigned)page); + ret = sx1301_write(spi, REG_PAGE_RESET, page & 0x3); + if (ret) { + dev_err(&spi->dev, "switching to page %u failed\n", (unsigned)page); + return ret; + } + + priv->cur_page = page; + + return 0; +} + +static int sx1301_soft_reset(struct spi_device *spi) +{ + return sx1301_write(spi, REG_PAGE_RESET, REG_PAGE_RESET_SOFT_RESET); +} + +#define REG_RADIO_X_DATA 0 +#define REG_RADIO_X_DATA_READBACK 1 +#define REG_RADIO_X_ADDR 2 +#define REG_RADIO_X_CS 4 + +static int sx1301_radio_set_cs(struct spi_controller *ctrl, bool enable) +{ + struct spi_sx1301 *ssx = spi_controller_get_devdata(ctrl); + u8 cs; + int ret; + + dev_dbg(&ctrl->dev, "setting CS to %s\n", enable ? "1" : "0"); + + ret = sx1301_page_switch(ssx->parent, ssx->page); + if (ret) { + dev_warn(&ctrl->dev, "failed to switch page for CS (%d)\n", ret); + return ret; + } + + ret = sx1301_read(ssx->parent, ssx->regs + REG_RADIO_X_CS, &cs); + if (ret) { + dev_warn(&ctrl->dev, "failed to read CS (%d)\n", ret); + cs = 0; + } + + if (enable) + cs |= BIT(0); + else + cs &= ~BIT(0); + + ret = sx1301_write(ssx->parent, ssx->regs + REG_RADIO_X_CS, cs); + if (ret) + dev_warn(&ctrl->dev, "failed to write CS (%d)\n", ret); + + return 0; +} + +static void sx1301_radio_spi_set_cs(struct spi_device *spi, bool enable) +{ + int ret; + + dev_dbg(&spi->dev, "setting SPI CS to %s\n", enable ? "1" : "0"); + + if (enable) + return; + + ret = sx1301_radio_set_cs(spi->controller, enable); + if (ret) + dev_warn(&spi->dev, "failed to write CS (%d)\n", ret); +} + +static int sx1301_radio_spi_transfer_one(struct spi_controller *ctrl, + struct spi_device *spi, struct spi_transfer *xfr) +{ + struct spi_sx1301 *ssx = spi_controller_get_devdata(ctrl); + const u8 *tx_buf = xfr->tx_buf; + u8 *rx_buf = xfr->rx_buf; + int ret; + + if (xfr->len == 0 || xfr->len > 3) + return -EINVAL; + + dev_dbg(&spi->dev, "transferring one (%u)\n", xfr->len); + + ret = sx1301_page_switch(ssx->parent, ssx->page); + if (ret) { + dev_err(&spi->dev, "failed to switch page for transfer (%d)\n", ret); + return ret; + } + + if (tx_buf) { + ret = sx1301_write(ssx->parent, ssx->regs + REG_RADIO_X_ADDR, tx_buf ? tx_buf[0] : 0); + if (ret) { + dev_err(&spi->dev, "SPI radio address write failed\n"); + return ret; + } + + ret = sx1301_write(ssx->parent, ssx->regs + REG_RADIO_X_DATA, (tx_buf && xfr->len >= 2) ? tx_buf[1] : 0); + if (ret) { + dev_err(&spi->dev, "SPI radio data write failed\n"); + return ret; + } + + ret = sx1301_radio_set_cs(ctrl, true); + if (ret) { + dev_err(&spi->dev, "SPI radio CS set failed\n"); + return ret; + } + + ret = sx1301_radio_set_cs(ctrl, false); + if (ret) { + dev_err(&spi->dev, "SPI radio CS unset failed\n"); + return ret; + } + } + + if (rx_buf) { + ret = sx1301_read(ssx->parent, ssx->regs + REG_RADIO_X_DATA_READBACK, &rx_buf[xfr->len - 1]); + if (ret) { + dev_err(&spi->dev, "SPI radio data read failed\n"); + return ret; + } + } + + return 0; +} + +static void sx1301_radio_setup(struct spi_controller *ctrl) +{ + ctrl->mode_bits = SPI_CS_HIGH | SPI_NO_CS; + ctrl->bits_per_word_mask = SPI_BPW_MASK(8); + ctrl->num_chipselect = 1; + ctrl->set_cs = sx1301_radio_spi_set_cs; + ctrl->transfer_one = sx1301_radio_spi_transfer_one; +} + +static int sx1301_probe(struct spi_device *spi) +{ + struct net_device *netdev; + struct sx1301_priv *priv; + struct spi_sx1301 *radio; + struct gpio_desc *rst; + int ret; + u8 val; + + rst = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW); + if (IS_ERR(rst)) + return PTR_ERR(rst); + + gpiod_set_value_cansleep(rst, 1); + msleep(100); + gpiod_set_value_cansleep(rst, 0); + msleep(100); + + spi->bits_per_word = 8; + spi_setup(spi); + + ret = sx1301_read(spi, REG_VERSION, &val); + if (ret) { + dev_err(&spi->dev, "version read failed\n"); + goto err_version; + } + + if (val != 103) { + dev_err(&spi->dev, "unexpected version: %u\n", val); + ret = -ENXIO; + goto err_version; + } + + netdev = alloc_loradev(sizeof(*priv)); + if (!netdev) { + ret = -ENOMEM; + goto err_alloc_loradev; + } + + priv = netdev_priv(netdev); + priv->rst_gpio = rst; + priv->cur_page = 0xff; + + spi_set_drvdata(spi, netdev); + SET_NETDEV_DEV(netdev, &spi->dev); + + ret = sx1301_write(spi, REG_PAGE_RESET, 0); + if (ret) { + dev_err(&spi->dev, "page/reset write failed\n"); + return ret; + } + + ret = sx1301_soft_reset(spi); + if (ret) { + dev_err(&spi->dev, "soft reset failed\n"); + return ret; + } + + ret = sx1301_read(spi, 16, &val); + if (ret) { + dev_err(&spi->dev, "16 read failed\n"); + return ret; + } + + val &= ~REG_16_GLOBAL_EN; + + ret = sx1301_write(spi, 16, val); + if (ret) { + dev_err(&spi->dev, "16 write failed\n"); + return ret; + } + + ret = sx1301_read(spi, 17, &val); + if (ret) { + dev_err(&spi->dev, "17 read failed\n"); + return ret; + } + + val &= ~REG_17_CLK32M_EN; + + ret = sx1301_write(spi, 17, val); + if (ret) { + dev_err(&spi->dev, "17 write failed\n"); + return ret; + } + + ret = sx1301_page_switch(spi, 2); + if (ret) { + dev_err(&spi->dev, "page 2 switch failed\n"); + return ret; + } + + ret = sx1301_read(spi, 43, &val); + if (ret) { + dev_err(&spi->dev, "2|43 read failed\n"); + return ret; + } + + val |= REG_2_43_RADIO_B_EN | REG_2_43_RADIO_A_EN; + + ret = sx1301_write(spi, 43, val); + if (ret) { + dev_err(&spi->dev, "2|43 write failed\n"); + return ret; + } + + msleep(500); + + ret = sx1301_read(spi, 43, &val); + if (ret) { + dev_err(&spi->dev, "2|43 read failed\n"); + return ret; + } + + val |= REG_2_43_RADIO_RST; + + ret = sx1301_write(spi, 43, val); + if (ret) { + dev_err(&spi->dev, "2|43 write failed\n"); + return ret; + } + + msleep(5); + + ret = sx1301_read(spi, 43, &val); + if (ret) { + dev_err(&spi->dev, "2|43 read failed\n"); + return ret; + } + + val &= ~REG_2_43_RADIO_RST; + + ret = sx1301_write(spi, 43, val); + if (ret) { + dev_err(&spi->dev, "2|43 write failed\n"); + return ret; + } + + /* radio A */ + + priv->radio_a_ctrl = spi_alloc_master(&spi->dev, sizeof(*radio)); + if (!priv->radio_a_ctrl) { + ret = -ENOMEM; + goto err_radio_a_alloc; + } + + sx1301_radio_setup(priv->radio_a_ctrl); + priv->radio_a_ctrl->dev.of_node = of_get_child_by_name(spi->dev.of_node, "radio-a"); + + radio = spi_controller_get_devdata(priv->radio_a_ctrl); + radio->page = 2; + radio->regs = REG_2_SPI_RADIO_A_DATA; + radio->parent = spi; + + dev_info(&spi->dev, "registering radio A SPI\n"); + + ret = devm_spi_register_controller(&spi->dev, priv->radio_a_ctrl); + if (ret) { + dev_err(&spi->dev, "radio A SPI register failed\n"); + goto err_radio_a_register; + } + + /* radio B */ + + priv->radio_b_ctrl = spi_alloc_master(&spi->dev, sizeof(*radio)); + if (!priv->radio_b_ctrl) { + ret = -ENOMEM; + goto err_radio_b_alloc; + } + + sx1301_radio_setup(priv->radio_b_ctrl); + priv->radio_b_ctrl->dev.of_node = of_get_child_by_name(spi->dev.of_node, "radio-b"); + + radio = spi_controller_get_devdata(priv->radio_b_ctrl); + radio->page = 2; + radio->regs = REG_2_SPI_RADIO_B_DATA; + radio->parent = spi; + + dev_info(&spi->dev, "registering radio B SPI\n"); + + ret = devm_spi_register_controller(&spi->dev, priv->radio_b_ctrl); + if (ret) { + dev_err(&spi->dev, "radio B SPI register failed\n"); + goto err_radio_b_register; + } + + dev_info(&spi->dev, "SX1301 module probed\n"); + + return 0; +err_radio_b_register: + spi_controller_put(priv->radio_b_ctrl); +err_radio_b_alloc: +err_radio_a_register: + spi_controller_put(priv->radio_a_ctrl); +err_radio_a_alloc: + free_loradev(netdev); +err_alloc_loradev: +err_version: + return ret; +} + +static int sx1301_remove(struct spi_device *spi) +{ + struct net_device *netdev = spi_get_drvdata(spi); + + //unregister_loradev(netdev); + free_loradev(netdev); + + dev_info(&spi->dev, "SX1301 module removed\n"); + + return 0; +} + +#ifdef CONFIG_OF +static const struct of_device_id sx1301_dt_ids[] = { + { .compatible = "semtech,sx1301" }, + {} +}; +MODULE_DEVICE_TABLE(of, sx1301_dt_ids); +#endif + +static struct spi_driver sx1301_spi_driver = { + .driver = { + .name = "sx1301", + .of_match_table = of_match_ptr(sx1301_dt_ids), + }, + .probe = sx1301_probe, + .remove = sx1301_remove, +}; + +module_spi_driver(sx1301_spi_driver); + +MODULE_DESCRIPTION("SX1301 SPI driver"); +MODULE_AUTHOR("Andreas Färber "); +MODULE_LICENSE("GPL");