From patchwork Fri Sep 17 15:13:43 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: 12502551 X-Patchwork-Delegate: kuba@kernel.org 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,URIBL_BLOCKED, 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 35386C433F5 for ; Fri, 17 Sep 2021 15:16:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F13960FA0 for ; Fri, 17 Sep 2021 15:16:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344423AbhIQPR6 (ORCPT ); Fri, 17 Sep 2021 11:17:58 -0400 Received: from mail-bn7nam10on2051.outbound.protection.outlook.com ([40.107.92.51]:15403 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S245070AbhIQPQ6 (ORCPT ); Fri, 17 Sep 2021 11:16:58 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nmVm5ShgAJfaNEMYKwhAyN5scmdnZQAa3MgeUaiF3Av0de9BpVhy0gXn43y22lfa/omLsJc+tk6wcOvag2hV2m0bXIbbz4nbG88Pc4DxvSeoeJf7Zpx/6ROOeFCJAKTPvgjev7Ij9T4gOl3AZxXINFan8P2E8pTcuSgRztAZOzlLAIByC2Wu3JYObTwp/Oq89EBlvZr29cT2KdbYOyIGhZMfk885hRaRQ6lRKdwdBbgNZWgh3iJ/XBTUtVGIN99p4lZGyt1HvMtawDtQ2kF3ExB8Ylo/wEXn1vp0Fbd8hMkkuTOPH4TqJR2ReUOpsmdgvKoF64wnP67XPtLtgYt/GA== 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=D5L4puuXU2pclo+gcplLRDQYqNNx6aeJ3DkplojLCJr4eyAE7PKXWfLsB5i7oTt4U7kEpCSZ1yv91Tf0vwl8I5QKJcgnlC/nbnrp0AmGDCrbsKUngPMeRyifRiPZyXd1t0l/TBL4a4LzmbDzd8/SlkYL4q1JZ33WL1dwM1tou6tm9KckTIXxJAB3mduRjxqiSToz8ewk35YrTDKCXy07uAMfFZIlv2rKeKil4ZV+XxQ2bmYYsyYMFIIigp7fmGDExZbKSQVcK0E1pW6GAgCysDS/q+YshPUObZt/x1ItZHZ6tz0NJkbQCVYzBarSQsuWtYIqLqjstXcSjOR628yp/w== 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=S6c33euPo7/7P665te0EbLGRP/23x9YiLwwmIJf/Kc4+slktsqStnf/+P0iSBVBf/iCvUBnD8Amx9TBaF/WA2NCxQZCKqoLrVcn6pJEvSFdm17Fc8pBnsiLP3Gy487LWD7qL1IW+Oz+bpw85mM70vK27tLBl5xLp+S8i3X4xcQY= 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 SA0PR11MB4574.namprd11.prod.outlook.com (2603:10b6:806:71::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Fri, 17 Sep 2021 15:14:36 +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.4500.017; Fri, 17 Sep 2021 15:14:36 +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 v6 07/24] wfx: add bus_spi.c Date: Fri, 17 Sep 2021 17:13:43 +0200 Message-Id: <20210917151401.2274772-8-Jerome.Pouiller@silabs.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210917151401.2274772-1-Jerome.Pouiller@silabs.com> References: <20210917151401.2274772-1-Jerome.Pouiller@silabs.com> X-ClientProxiedBy: SA9PR03CA0029.namprd03.prod.outlook.com (2603:10b6:806:20::34) 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 SA9PR03CA0029.namprd03.prod.outlook.com (2603:10b6:806:20::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend Transport; Fri, 17 Sep 2021 15:14:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2718d408-1c3d-454e-c478-08d979eddc35 X-MS-TrafficTypeDiagnostic: SA0PR11MB4574: 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: kaKXFfzQXh2FqUkLD71CtkzNmgraamh22xwlxzf5wawk+jB5qc7+QtelIpESBixi0vzFXMH4x+6XD/u54hLA9hnCdQ9PLvg+rVY7tUklJR2Uu1kQeTMHEX4gGCCnK44/JrVqMQUcP3wduUE5SkJxyWK7HxBlOG3ameucndLQ0fs/sWLmduW6kOPwGNe6O+j357w1NTKlb/GBCKGEXduDQBh/f3qJvYlWdLoX1crIpOR+1om9G8NnPDynRyG9UXFI04Nwv5KW2rsFeJ+tcSH6V0F+A37QfS93Ctlq2gDfeDvnSpB9yCIzqph7BmKz0i2M7/6ux8vcpHbL7IiGKAixQDdVp5vqzANB8g2I9SZDu48BHOKC4hZyEzXfyPcXvKVuWXODnjSU5IqZfDvIY1Qxi0NHoOQr2SZv8G+/yP6GVrKhbTQHd5DA0kV7OuEfx+A4XlLaTIh0uWwESgkwtex2NoStAwpFjuEoXPz7xcYuYFtrBs6AaYTGKwOoagQqYUHFcVyP0Bg61xBu470xv695OIkHz9In1J7nuDXvqIlo2jP5oiAwAILdafiHBiOfwuIjJfhFRhwtzE6BkgWfavHxeaiAUoHWt7PkLHHP8cQc99PCJFx4815G9GxtjBdtTiL0VWN9gJupmn16tKr41xR0pg== 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:(136003)(346002)(376002)(39850400004)(396003)(366004)(52116002)(6666004)(38100700002)(8936002)(7696005)(478600001)(6486002)(186003)(7416002)(86362001)(5660300002)(54906003)(66556008)(66476007)(4326008)(1076003)(66946007)(2616005)(107886003)(6916009)(316002)(36756003)(8676002)(66574015)(2906002)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Y4d57Lw3M7H3SwQafi5gf9omYisa?= =?utf-8?q?WP4AaILGRDO09s+EXZTCI3NKJqLrseIcAK0ooaCrgZie/VxaxdLBzcf4j+lOaWNVK?= =?utf-8?q?VRqX0QZp+5EN4GViVfet3SsRQ5S3qw6bJgU/urnmR3fQNAZ+D1EuclYDERuNbBj38?= =?utf-8?q?hwsZOUdhyy0nCJ8Pwu1creJcDynBbGnl3TNx2yt81/IO86McCRgQZvm1ykEpe9YN1?= =?utf-8?q?HiTMWqufzTufJNZLInbq6FmoyWucf14Eb425kPfqvt9jDgskJF3jW9sZumDnHPXQK?= =?utf-8?q?TV8TEN6rTVU4RXDp9MtjThajzsbol2FWJwVosTqiHQrr1m4gqbEOSQ7TvzhPNtPhM?= =?utf-8?q?1e36PqsGdf5StboyUuNRbY8ruorx0xXKpeRe9/I/UJd2/q8/HlSnkRW16joU2POeS?= =?utf-8?q?G6Wgq9jnBC40mPaNZdpTl9w8RY1+vDCKYsZIkT4/lyiQ2muJ1deM8TKrdW8Sia7kO?= =?utf-8?q?5xAZaGJ6crqgrWkbFkXT/NQ09efvT2XkvICcuhmwP28m/sEpJjyLBG1grjBXksDBO?= =?utf-8?q?5HlksxBXfUkck++ea4+Orp0p9+hAnEAmr7Kiy/0D8qQuHuMx7Oy5TL4++TVqQUFLA?= =?utf-8?q?4UrihJSKUe01+Hqk8K2xJj8bVsUoHBSf37qCuaxP+TzfdRwA9dj1Z5RJgRQoRMzDW?= =?utf-8?q?oVU7YEhV/9dPXIEe4Hw31179Ew5sWa80k7nzKtTd98NtSpSJOnra+mOYUDH1hcMRP?= =?utf-8?q?TKPopgzlsNRH+yA1gSGiMVk2kq5XGoXsZu2ASW88JdVmEOhD2i9IQraL1CUKWLMPl?= =?utf-8?q?PL18Y84PbXzHvG0SKwe3YBBsOqFB/lS6gPbl08FvTJptvGLmSPYSPsdeo4weiG+Q+?= =?utf-8?q?7CtsH+70kSbh6IaMTPAcuYTh6L/dym4sAj/wc75XTAne1BdsBV8/D43UcguZXrORj?= =?utf-8?q?ZUlRmE//vYD2BGBeSYutc3lRPB4Fm4nW6ue0jVF2p+y2F3qUFXaHTA/crvkQApxbG?= =?utf-8?q?GzZoDAx2iIbLHFlu5SPn+KV+FEefBP79K55EVVjSoKyX+xnSmrOHIpYHNfmEfvots?= =?utf-8?q?tkz6Nhp50fauDM/8VWwurPzeq8NbcBWWM+DHw5M5Robb9X3pxdVE+4LacZYcOl1Bg?= =?utf-8?q?FVKp99EMJ1Umu+ZR1VOHSP6BS91EInZfteIsGV1gPO+lWsGeDG45hnv7BzSkVYLp5?= =?utf-8?q?B5IGuptRQaAIlfTjQ4/iFCjj2fjovJEWMlaZr2Mg6kHDGh5UZTj+322Pge17nSwMc?= =?utf-8?q?Zlz15WCAGUKDSknveRRUTftPvMi95TT6/HaaTmt7fCI0ULPWqXMmN5+VRBAWMpQP6?= =?utf-8?q?EASmk6izxUKbb+DS+q+e00F2LOTJ7uH0X3+eHuJOAviXvraCxmHufS9bACwcPvZFg?= =?utf-8?q?ywPeYWkZv93j2GQ?= X-OriginatorOrg: silabs.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2718d408-1c3d-454e-c478-08d979eddc35 X-MS-Exchange-CrossTenant-AuthSource: SN6PR11MB2718.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Sep 2021 15:14:36.8271 (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: U2Acsdr2GM4DdOa35JFq7Dfn7AX+mk1NM/4tZj8feBNUJA8nRNo5wDfIUCvu60gGwaAkJYcUjBP3c3biph/qVQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4574 Precedence: bulk List-ID: X-Mailing-List: netdev@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, +};