From patchwork Mon Sep 20 16:11:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?SsOpcsO0bWUgUG91aWxsZXI=?= X-Patchwork-Id: 12505837 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9268AC433EF for ; Mon, 20 Sep 2021 16:12:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7959B61184 for ; Mon, 20 Sep 2021 16:12:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242654AbhITQOE (ORCPT ); Mon, 20 Sep 2021 12:14:04 -0400 Received: from mail-bn7nam10on2055.outbound.protection.outlook.com ([40.107.92.55]:31296 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S242661AbhITQNu (ORCPT ); Mon, 20 Sep 2021 12:13:50 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XP7/cJrcHZecIdgKC9bZ4DIe8OS40PSz0j07ZlgTzdPrFMUJEg9G2B0OYmxGOSZT+sb1VJTZWHQiyFxBlRO20z9kkKpFBWP+srBOWZFOSAsqDvgbFqG6Q3AYU/COCGN88PrVHV86fP8KfLkE4Ikl+eKTKUkPeJ7Wp1ImOY2xm1lblEGQODpDAHRh8fG9xWaA3GP7IKnDmaMFLeRPgjorJDbbTiipFpgHIUb5OTl79uL6oHPsOh0qfMOoNDZ1TIzOUCbVJJFMk4FWiVTrIBnTn8okwSKmXgQCFilQtG0nFZ9S0/uIEOnNe97oU6I+5gSeT0hF/wzughz7+lfRYM5dXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=agdAC3VUVrwvu6B1Coi2+jA/vJB6/tC0zCloVaIaojU=; b=M14Wp+hHRf/C6o1Voulz4iiKtl6OUc6aMYrkWHDuah7TBMb7wW/njzc5+7Uh1pTv5r2E+xhk5YlWzw0Wn5fdHG6/6PGEYSecyM2kJgpqcqinhik5GOMIO/tg6PhpxndyQW/eBnclB2p1xLCJ5COj4vyHoZGxiejMEjjwL3n94VVVlI7VsipeZ0RCHwASyuS10B2a7jJrnaJiuBTVY5uD1eWC/H7ITvUt+Ap9xovsxLqakYcZ91t2RjWYK+jBPtQLwZ0tIr7ODNPgNnhhakFUiZrSQ7w6+sL6szhIne/LGLUDQrx3uLzjiu4jrDOYYZmzb4HTYHTQ2b4dWwIA7JuDJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=silabs.com; dmarc=pass action=none header.from=silabs.com; dkim=pass header.d=silabs.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=silabs.onmicrosoft.com; s=selector2-silabs-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=agdAC3VUVrwvu6B1Coi2+jA/vJB6/tC0zCloVaIaojU=; b=QwP2Vvx8tPpRt10pQuN0WiU0EPz5Tpqlm6yKPhS0BYzMPKvIxCwU87yfim+1RmGsZryF3NLav8H4FhN6NGXWOTx2YRaeHXsBXPhGXpsrW173GnvBdLjer4mAwf1KuaTKjzuOOxDdbZKf13qor5Sy5QDj1OaWX3ZGLcuKhqE7VHQ= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=silabs.com; Received: from SN6PR11MB2718.namprd11.prod.outlook.com (2603:10b6:805:63::18) by SA2PR11MB5002.namprd11.prod.outlook.com (2603:10b6:806:fb::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.16; Mon, 20 Sep 2021 16:12:10 +0000 Received: from SN6PR11MB2718.namprd11.prod.outlook.com ([fe80::7050:a0a:415:2ccd]) by SN6PR11MB2718.namprd11.prod.outlook.com ([fe80::7050:a0a:415:2ccd%7]) with mapi id 15.20.4523.018; Mon, 20 Sep 2021 16:12:10 +0000 From: Jerome Pouiller To: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, Kalle Valo Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman , "David S . Miller" , devicetree@vger.kernel.org, Rob Herring , linux-mmc@vger.kernel.org, =?utf-8?q?Pali?= =?utf-8?q?_Roh=C3=A1r?= , Ulf Hansson , =?utf-8?b?SsOpcsO0bWUgUG91aWxsZXI=?= Subject: [PATCH v7 07/24] wfx: add bus_spi.c Date: Mon, 20 Sep 2021 18:11:19 +0200 Message-Id: <20210920161136.2398632-8-Jerome.Pouiller@silabs.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920161136.2398632-1-Jerome.Pouiller@silabs.com> References: <20210920161136.2398632-1-Jerome.Pouiller@silabs.com> X-ClientProxiedBy: SA0PR11CA0064.namprd11.prod.outlook.com (2603:10b6:806:d2::9) To SN6PR11MB2718.namprd11.prod.outlook.com (2603:10b6:805:63::18) MIME-Version: 1.0 Received: from pc-42.silabs.com (2a01:e34:ecb5:66a0:9876:e1d7:65be:d294) by SA0PR11CA0064.namprd11.prod.outlook.com (2603:10b6:806:d2::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend Transport; Mon, 20 Sep 2021 16:12:08 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 64cba47c-47f5-4dad-7680-08d97c516685 X-MS-TrafficTypeDiagnostic: SA2PR11MB5002: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2733; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0t5vd3oCG1NrPdHf97NukwSvn4nLuR4YzXUG3B8aw63F8mRhBnWa/s9ny+daQiRBar6I72l3GFGQSGhWsUvAIbT206jJbzBxz9NZ0n6jcUROwGbYf9kkuxTiJ5HgW8Qzi7/aZF5t853xXwAUxLX20DcB7ODpKlNmjz+TZqChTCp8yd6V7V6iXsL/bVF0Z3EuGzOrkvZh5Aqg2ojwD00D7G4zH5ohfH8rQkOyh2U5hQAlrvyPURqitLyP9aUz5VmmbCOX7oAYT9FaYEG9KopB+1tP12YByBuVYkED/8Ao1Vnj8QVMGdvFO1VMSMH4TW8lKtUO3A868Nqt4ozJEB0pj9YtYAG7j7odAjUGgzAYiw84je17XKSRU6atLVJA/zp156vzQ8hzillpRQPpNVx2wAmNLW+FEqh+T4htuBncWzyYDPg5TP0agL09JnbVifk4gLElMF6fyfGyjQpRycwMkcqLQnPFW1u2FUb3GeK7oeFVh8zyQB2ZGg2A9WrjfRcvTsiJm61tFhVnokMpzspZXwjX/tFfjlVqX5ouB4SHh9+jOLS6TU6HhqI/mmRLPy9OSNa00zrblgmhQQspXfpo+dXjDmYGySnWYjZOTaJPwC52q+aQUFkJLISBqEA4alMDc3B4M7YUMc5E8Kt/80zr5Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR11MB2718.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66476007)(36756003)(2616005)(66556008)(107886003)(186003)(54906003)(5660300002)(6486002)(66574015)(66946007)(52116002)(38100700002)(83380400001)(7696005)(6916009)(2906002)(1076003)(4326008)(8936002)(316002)(8676002)(508600001)(86362001)(6666004)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7kCobPDItjEN7DDCBeF6TF23+HXVwK5qhWUSfhdSHqs/WnsT8Cl4xXoLxWPpEz3OLLbaVdikoyyCFRKO+e7vicwBgnpxQB1rGJrkL01T/JyhZZZvIRR4QiyL0f2yp4u5cCLE4++MU7bRQxJAHlpg8Fk9lYlOyOuf8BmOg+sY1n1+1R9Un4bEXBEzCK+J1LiHd6OyQrPV4yJVvpEzCqviFHKpoKyW0Cs2xXn04IDRoV2EoLlNWKibX4mYAWg3dCmf9pRRInUhWalZSseYFJzckze2DCPGhPmZ/sDSIKrz0nLv+4mvmlQWze4SoaOc3UqueP7b4pQTov1pT7f1vG5e3rgIqTILm+KRazq1cQGcRutVUOoL39AzBsVSk4pNtWtBnXzU/7wKcQ59G/Kbshl9rNrcB+epomeS3qkb1KajEE4qyrNQY4zlyvAnKTY9WV/DityXEp+p+MkOj9clvis+Xlh3f0jDNuKNoYPsoArVLZV3KbfBAXB9MbSJLweDb5hrJHwWMFpVttUycymCBuL7pqJsWA9ldtmsAUFuz2rzc9AUzdLnj+UceCJVTfHQPx5PkES0u/9t7CQo1eWhyrgLdaOQcyMF+Pf39Z5tHMp5fMo+JZ1FHsifyl6on19TPT9EekqC2u5FFy/PWCgKvWXdcXjdVOEoDTkIyM9iVwzo2epy8N1s3xgdnYdeDoRdj+20QQucZm2/y3MWr7ISkhi0KNZ8d4UGplmZR+DCMWRIsElGr2q9LGgkOJD4DqLeJE9PMMZGgkHDwa6/5vuM+0sr94SoNnLFp1ID33vq7O+k59d7hK88GoE423dztXum+9kL X-OriginatorOrg: silabs.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64cba47c-47f5-4dad-7680-08d97c516685 X-MS-Exchange-CrossTenant-AuthSource: SN6PR11MB2718.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Sep 2021 16:12:10.6352 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 54dbd822-5231-4b20-944d-6f4abcd541fb X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: x6mv1mbzsjjV7dCUz9dpS8aUE1jIZY+1f2owwz9Co3s6YhXcSZoE6VBdWNbpJzoYHx7rgBKFtdMaD9KHP/iF7A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR11MB5002 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Jérôme Pouiller Signed-off-by: Jérôme Pouiller --- drivers/net/wireless/silabs/wfx/bus_spi.c | 271 ++++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 drivers/net/wireless/silabs/wfx/bus_spi.c diff --git a/drivers/net/wireless/silabs/wfx/bus_spi.c b/drivers/net/wireless/silabs/wfx/bus_spi.c new file mode 100644 index 000000000000..55ffcd7c42e2 --- /dev/null +++ b/drivers/net/wireless/silabs/wfx/bus_spi.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * SPI interface. + * + * Copyright (c) 2017-2020, Silicon Laboratories, Inc. + * Copyright (c) 2011, Sagrad Inc. + * Copyright (c) 2010, ST-Ericsson + */ +#include +#include +#include +#include +#include +#include +#include + +#include "bus.h" +#include "wfx.h" +#include "hwio.h" +#include "main.h" +#include "bh.h" + +#define SET_WRITE 0x7FFF /* usage: and operation */ +#define SET_READ 0x8000 /* usage: or operation */ + +#define WFX_RESET_INVERTED 1 + +static const struct wfx_platform_data wfx_spi_pdata = { + .file_fw = "wfm_wf200", + .file_pds = "wf200.pds", + .use_rising_clk = true, +}; + +struct wfx_spi_priv { + struct spi_device *func; + struct wfx_dev *core; + struct gpio_desc *gpio_reset; + bool need_swab; +}; + +/* The chip reads 16bits of data at time and place them directly into (little + * endian) CPU register. So, the chip expects bytes order to be "B1 B0 B3 B2" + * (while LE is "B0 B1 B2 B3" and BE is "B3 B2 B1 B0") + * + * A little endian host with bits_per_word == 16 should do the right job + * natively. The code below to support big endian host and commonly used SPI + * 8bits. + */ +static int wfx_spi_copy_from_io(void *priv, unsigned int addr, + void *dst, size_t count) +{ + struct wfx_spi_priv *bus = priv; + u16 regaddr = (addr << 12) | (count / 2) | SET_READ; + struct spi_message m; + struct spi_transfer t_addr = { + .tx_buf = ®addr, + .len = sizeof(regaddr), + }; + struct spi_transfer t_msg = { + .rx_buf = dst, + .len = count, + }; + u16 *dst16 = dst; + int ret, i; + + WARN(count % 2, "buffer size must be a multiple of 2"); + + cpu_to_le16s(®addr); + if (bus->need_swab) + swab16s(®addr); + + spi_message_init(&m); + spi_message_add_tail(&t_addr, &m); + spi_message_add_tail(&t_msg, &m); + ret = spi_sync(bus->func, &m); + + if (bus->need_swab && addr == WFX_REG_CONFIG) + for (i = 0; i < count / 2; i++) + swab16s(&dst16[i]); + return ret; +} + +static int wfx_spi_copy_to_io(void *priv, unsigned int addr, + const void *src, size_t count) +{ + struct wfx_spi_priv *bus = priv; + u16 regaddr = (addr << 12) | (count / 2); + /* FIXME: use a bounce buffer */ + u16 *src16 = (void *)src; + int ret, i; + struct spi_message m; + struct spi_transfer t_addr = { + .tx_buf = ®addr, + .len = sizeof(regaddr), + }; + struct spi_transfer t_msg = { + .tx_buf = src, + .len = count, + }; + + WARN(count % 2, "buffer size must be a multiple of 2"); + WARN(regaddr & SET_READ, "bad addr or size overflow"); + + cpu_to_le16s(®addr); + + /* Register address and CONFIG content always use 16bit big endian + * ("BADC" order) + */ + if (bus->need_swab) + swab16s(®addr); + if (bus->need_swab && addr == WFX_REG_CONFIG) + for (i = 0; i < count / 2; i++) + swab16s(&src16[i]); + + spi_message_init(&m); + spi_message_add_tail(&t_addr, &m); + spi_message_add_tail(&t_msg, &m); + ret = spi_sync(bus->func, &m); + + if (bus->need_swab && addr == WFX_REG_CONFIG) + for (i = 0; i < count / 2; i++) + swab16s(&src16[i]); + return ret; +} + +static void wfx_spi_lock(void *priv) +{ +} + +static void wfx_spi_unlock(void *priv) +{ +} + +static irqreturn_t wfx_spi_irq_handler(int irq, void *priv) +{ + struct wfx_spi_priv *bus = priv; + + wfx_bh_request_rx(bus->core); + return IRQ_HANDLED; +} + +static int wfx_spi_irq_subscribe(void *priv) +{ + struct wfx_spi_priv *bus = priv; + u32 flags; + + flags = irq_get_trigger_type(bus->func->irq); + if (!flags) + flags = IRQF_TRIGGER_HIGH; + flags |= IRQF_ONESHOT; + return devm_request_threaded_irq(&bus->func->dev, bus->func->irq, NULL, + wfx_spi_irq_handler, IRQF_ONESHOT, + "wfx", bus); +} + +static int wfx_spi_irq_unsubscribe(void *priv) +{ + struct wfx_spi_priv *bus = priv; + + devm_free_irq(&bus->func->dev, bus->func->irq, bus); + return 0; +} + +static size_t wfx_spi_align_size(void *priv, size_t size) +{ + /* Most of SPI controllers avoid DMA if buffer size is not 32bit aligned + */ + return ALIGN(size, 4); +} + +static const struct hwbus_ops wfx_spi_hwbus_ops = { + .copy_from_io = wfx_spi_copy_from_io, + .copy_to_io = wfx_spi_copy_to_io, + .irq_subscribe = wfx_spi_irq_subscribe, + .irq_unsubscribe = wfx_spi_irq_unsubscribe, + .lock = wfx_spi_lock, + .unlock = wfx_spi_unlock, + .align_size = wfx_spi_align_size, +}; + +static int wfx_spi_probe(struct spi_device *func) +{ + struct wfx_spi_priv *bus; + int ret; + + if (!func->bits_per_word) + func->bits_per_word = 16; + ret = spi_setup(func); + if (ret) + return ret; + /* Trace below is also displayed by spi_setup() if compiled with DEBUG */ + dev_dbg(&func->dev, "SPI params: CS=%d, mode=%d bits/word=%d speed=%d\n", + func->chip_select, func->mode, func->bits_per_word, + func->max_speed_hz); + if (func->bits_per_word != 16 && func->bits_per_word != 8) + dev_warn(&func->dev, "unusual bits/word value: %d\n", + func->bits_per_word); + if (func->max_speed_hz > 50000000) + dev_warn(&func->dev, "%dHz is a very high speed\n", + func->max_speed_hz); + + bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL); + if (!bus) + return -ENOMEM; + bus->func = func; + if (func->bits_per_word == 8 || IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) + bus->need_swab = true; + spi_set_drvdata(func, bus); + + bus->gpio_reset = devm_gpiod_get_optional(&func->dev, "reset", + GPIOD_OUT_LOW); + if (IS_ERR(bus->gpio_reset)) + return PTR_ERR(bus->gpio_reset); + if (!bus->gpio_reset) { + dev_warn(&func->dev, + "gpio reset is not defined, trying to load firmware anyway\n"); + } else { + gpiod_set_consumer_name(bus->gpio_reset, "wfx reset"); + if (spi_get_device_id(func)->driver_data & WFX_RESET_INVERTED) + gpiod_toggle_active_low(bus->gpio_reset); + gpiod_set_value_cansleep(bus->gpio_reset, 1); + usleep_range(100, 150); + gpiod_set_value_cansleep(bus->gpio_reset, 0); + usleep_range(2000, 2500); + } + + bus->core = wfx_init_common(&func->dev, &wfx_spi_pdata, + &wfx_spi_hwbus_ops, bus); + if (!bus->core) + return -EIO; + + return wfx_probe(bus->core); +} + +static int wfx_spi_remove(struct spi_device *func) +{ + struct wfx_spi_priv *bus = spi_get_drvdata(func); + + wfx_release(bus->core); + return 0; +} + +/* For dynamic driver binding, kernel does not use OF to match driver. It only + * use modalias and modalias is a copy of 'compatible' DT node with vendor + * stripped. + */ +static const struct spi_device_id wfx_spi_id[] = { + { "wfx-spi", WFX_RESET_INVERTED }, + { "wf200", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(spi, wfx_spi_id); + +#ifdef CONFIG_OF +static const struct of_device_id wfx_spi_of_match[] = { + { .compatible = "silabs,wfx-spi", .data = (void *)WFX_RESET_INVERTED }, + { .compatible = "silabs,wf200" }, + { }, +}; +MODULE_DEVICE_TABLE(of, wfx_spi_of_match); +#endif + +struct spi_driver wfx_spi_driver = { + .driver = { + .name = "wfx-spi", + .of_match_table = of_match_ptr(wfx_spi_of_match), + }, + .id_table = wfx_spi_id, + .probe = wfx_spi_probe, + .remove = wfx_spi_remove, +};