From patchwork Sun Jul 1 11:07:57 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: 10498635 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 CD56960284 for ; Sun, 1 Jul 2018 11:12:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD724286FD for ; Sun, 1 Jul 2018 11:12:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B14F428AFB; Sun, 1 Jul 2018 11:12:20 +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 17912286FD for ; Sun, 1 Jul 2018 11:12:20 +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=1tflAWTsif7nFst1fInS2uEh9n25xckdWN2IUPlwhKU=; b=DcSsWn5ySCKb7v wd+Vl5E9Bl5Ijd6hnFOb2SFOS1naIzQeBzX5iklSGmtw9A6IEKPmtPj1OGix45ZXsXJ5xbg7B4gWq zQGMes4l9KaJoGB56BuPv41d5zCJ4r+kZ2K/lflZtPmC3Cb1svTPL6aaYkw4NoesWgRdzVyDbZKQN 617C5MZ2r1J0KPAgwg7e1c0X7eU7BSjYUpd9aO+olBhtqWsakIqsY+IeLpsUGdzl5Q2c27iH8v9Lk TKWp1vMZte9dxs/N/NwGiv6jmQziA1fitheIxdg/19GQ2cDOqEDdqgXxxMqe4Qvc9S9gm8x0Fvht+ YUyKGCJ27iEX8jI1V8OA==; 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 1fZaH7-0007ha-8p; Sun, 01 Jul 2018 11:12:13 +0000 Received: from mx2.suse.de ([195.135.220.15] helo=mx1.suse.de) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fZaEP-0004XX-96 for linux-arm-kernel@lists.infradead.org; Sun, 01 Jul 2018 11:09:39 +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 77AE8AF53; Sun, 1 Jul 2018 11:08:59 +0000 (UTC) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: netdev@vger.kernel.org Subject: [RFC net-next 08/15] net: lora: sx1276: Add debugfs Date: Sun, 1 Jul 2018 13:07:57 +0200 Message-Id: <20180701110804.32415-9-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_040926_591685_0FD7A575 X-CRM114-Status: GOOD ( 12.34 ) 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: Matthias Brugger , Jiri Pirko , Marcel Holtmann , Dollar Chen , linux-kernel@vger.kernel.org, =?UTF-8?q?Michael=20R=C3=B6der?= , Janus Piwek , =?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 Allow some interactive inspection at runtime via debugfs. Signed-off-by: Andreas Färber --- drivers/net/lora/sx1276.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/drivers/net/lora/sx1276.c b/drivers/net/lora/sx1276.c index d6732111247a..1072019cfcc1 100644 --- a/drivers/net/lora/sx1276.c +++ b/drivers/net/lora/sx1276.c @@ -5,6 +5,7 @@ * Copyright (c) 2016-2018 Andreas Färber */ +#include #include #include #include @@ -61,6 +62,8 @@ struct sx1276_priv { struct workqueue_struct *wq; struct work_struct tx_work; + + struct dentry *debugfs; }; static int sx1276_read_single(struct spi_device *spi, u8 reg, u8 *val) @@ -416,6 +419,128 @@ static const struct net_device_ops sx1276_netdev_ops = { .ndo_start_xmit = sx1276_loradev_start_xmit, }; +static ssize_t sx1276_freq_read(struct file *file, char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct net_device *netdev = file->private_data; + struct sx1276_priv *priv = netdev_priv(netdev); + struct spi_device *spi = priv->spi; + ssize_t size; + char *buf; + int ret; + u8 msb, mid, lsb; + u32 freq_xosc; + unsigned long long frf; + + ret = of_property_read_u32(spi->dev.of_node, "clock-frequency", &freq_xosc); + if (ret) + return 0; + + mutex_lock(&priv->spi_lock); + + ret = sx1276_read_single(spi, REG_FRF_MSB, &msb); + if (!ret) + ret = sx1276_read_single(spi, REG_FRF_MID, &mid); + if (!ret) + ret = sx1276_read_single(spi, REG_FRF_LSB, &lsb); + + mutex_unlock(&priv->spi_lock); + + if (ret) + return 0; + + frf = freq_xosc; + frf *= ((ulong)msb << 16) | ((ulong)mid << 8) | lsb; + frf /= (1 << 19); + + buf = kasprintf(GFP_KERNEL, "%llu\n", frf); + if (!buf) + return 0; + + size = simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf)); + kfree(buf); + + return size; +} + +static const struct file_operations sx1276_freq_fops = { + .owner = THIS_MODULE, + .open = simple_open, + .read = sx1276_freq_read, +}; + +static ssize_t sx1276_state_read(struct file *file, char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct net_device *netdev = file->private_data; + struct sx1276_priv *priv = netdev_priv(netdev); + struct spi_device *spi = priv->spi; + ssize_t size; + char *buf; + int len = 0; + int ret; + u8 val; + bool lora_mode = true; + const int max_len = 4096; + + buf = kzalloc(max_len, GFP_KERNEL); + if (!buf) + return 0; + + mutex_lock(&priv->spi_lock); + + ret = sx1276_read_single(spi, REG_OPMODE, &val); + if (!ret) { + len += snprintf(buf + len, max_len - len, "RegOpMode = 0x%02x\n", val); + lora_mode = (val & REG_OPMODE_LONG_RANGE_MODE) != 0; + } + + ret = sx1276_read_single(spi, REG_FRF_MSB, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegFrMsb = 0x%02x\n", val); + ret = sx1276_read_single(spi, REG_FRF_MID, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegFrMid = 0x%02x\n", val); + ret = sx1276_read_single(spi, REG_FRF_LSB, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegFrLsb = 0x%02x\n", val); + + ret = sx1276_read_single(spi, REG_PA_CONFIG, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegPaConfig = 0x%02x\n", val); + + if (lora_mode) { + ret = sx1276_read_single(spi, LORA_REG_IRQ_FLAGS_MASK, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegIrqFlagsMask = 0x%02x\n", val); + + ret = sx1276_read_single(spi, LORA_REG_IRQ_FLAGS, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegIrqFlags = 0x%02x\n", val); + + ret = sx1276_read_single(spi, LORA_REG_SYNC_WORD, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegSyncWord = 0x%02x\n", val); + } + + ret = sx1276_read_single(spi, REG_PA_DAC, &val); + if (!ret) + len += snprintf(buf + len, max_len - len, "RegPaDac = 0x%02x\n", val); + + mutex_unlock(&priv->spi_lock); + + size = simple_read_from_buffer(user_buf, count, ppos, buf, len); + kfree(buf); + + return size; +} + +static const struct file_operations sx1276_state_fops = { + .owner = THIS_MODULE, + .open = simple_open, + .read = sx1276_state_read, +}; + static int sx1276_probe(struct spi_device *spi) { struct net_device *netdev; @@ -566,6 +691,10 @@ static int sx1276_probe(struct spi_device *spi) return ret; } + priv->debugfs = debugfs_create_dir(dev_name(&spi->dev), NULL); + debugfs_create_file("state", S_IRUGO, priv->debugfs, netdev, &sx1276_state_fops); + debugfs_create_file("frequency", S_IRUGO, priv->debugfs, netdev, &sx1276_freq_fops); + dev_info(&spi->dev, "SX1276 module probed (SX%d)", model); return 0; @@ -574,6 +703,9 @@ static int sx1276_probe(struct spi_device *spi) static int sx1276_remove(struct spi_device *spi) { struct net_device *netdev = spi_get_drvdata(spi); + struct sx1276_priv *priv = netdev_priv(netdev); + + debugfs_remove_recursive(priv->debugfs); unregister_loradev(netdev); free_loradev(netdev);