From nobody Tue Jun 16 12:41:43 2026 Received: from BN8PR05CU002.outbound.protection.outlook.com (mail-eastus2azon11011068.outbound.protection.outlook.com [52.101.57.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C05A63537D9; Sat, 25 Apr 2026 17:34:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.57.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777138488; cv=fail; b=P/qteW6hGR6ZX2qlSGR0fgQ6HYWiK9YVxYb6ONFaCcqNbt4tnbmVoUNsKk2IJvJSO72ICgzz5+O0tQfAkapEKhJKEoYyUA+UFzko5gp5Buvy2yjOOdj8l7s/75uBAF6yqgdfPwrWBv4+ONjeCQM2+WRexWAhrM08QS9F6sStDr8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777138488; c=relaxed/simple; bh=6xfaYM4bBX0+E4G59rdRhyA+Ff9dp5ZTS/Dn+QklwGc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gQ+Brbz1C9U2y3xC7RIKXKYmsBch+24j5FCPOjstd/pfOW4cpp2RdM/bEpSigo36IvxpkllNWq0iA9s/IODzvCQ2amd4wyCAENqWXLxqiTZs4VAiCX8ZYJ54ndjEu4vPLfmuyUsYuz2Mk1k961LtmAjx3xwBgVLFQei1RWJGMto= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=aq9lRPVd; arc=fail smtp.client-ip=52.101.57.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="aq9lRPVd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ecv3dlWwsKhkcA/gKFZvUzGSLi1EtfOi8kZAqYThmLBrsT4zPRbWeXwYPrJgdzo6LYK3B49UImaWuQN+C+JD1ayzIRktsJ80GBEBD2tKiWDxveZnxF0dkpPwJQaffaVQALB5HgwtZrLMDMUww/Wk7DoUlmezmJTYPr8jK7NhYg7SmCJXuCa8Fn8oU+aM/hXpxYJ1XxUoypvs8twHBV7HVVwvXnAGAqxWDdDYrEjTVGtA6C6asDW1MX9IBMQC2SyfNCWbHhcLkpOsw5JqY9HmsWIjUOmzG6dJQwTIGyHF9yQKlckoxaCiA3nZdLT5RB9fzMxc8Ah/5dGeHM00pyzF8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SUQWmc5MmWho0SmsC5h8r3ULWClDoJqhBY9aU1KMpYQ=; b=Yc+7rLH5aevLRZBXRSRL1cX/fVY+YuUDCyntw9QqwfAiRijwDGSxTLdkex029FV29zFI/PYzCQpNKxBGaff1TgGbBKoE8L1QnxGkgtMJzn9JRfznRSQ90nF32XpyQ3A5ajGCFjDpV8ez5iHqZz4Bp6lfd2rGYNJXVLVveB7hOQwBRfszAdrnTIEuoASjBZWopnz3JCxPTAL77H57rYVu1vhijE42P3q/tJZ9hL+nXYQIHEMPRD9CsJarndfd+e/N/pzUJ+RFFpZEQAdjYxAfAryXy0KfRcYTKWTHV+To7yqWZBNFAymmLWbClOQRya9vvJxa+fwd/4WQMNHOq5Et8Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SUQWmc5MmWho0SmsC5h8r3ULWClDoJqhBY9aU1KMpYQ=; b=aq9lRPVdaMqMS5/Rj7lZNulD/xsV878IOrNDnd9Rk3s4DtkzZOnBI0mCs8hBVBNBbv85LBMRFS+EXju/WcRUbBgo5K11ZzHHxDM1OawVVLptFFr8uf9YXJThfdDtx0vtaeGX2tPiPhzeq0orL5rcybEEoZAsZaYERwB46uS60K0XyEbmhFbj1DAT4cc/PyOtRseYUqz+/jQQeAwV//edRnWZIjYykRPYqVy9Q539VHJT5H330Fj6vcm77svhwW63izWQt6Ces//9OWbm+OpZXW5ea2zMF/aDxE2RctfKwcx0S11XPCsqm4wkBPBWnRjKCd6q3LIoB3dH/5a/zfm3xw== Received: from MW4PR04CA0287.namprd04.prod.outlook.com (2603:10b6:303:89::22) by DS2PR12MB9709.namprd12.prod.outlook.com (2603:10b6:8:276::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9870.13; Sat, 25 Apr 2026 17:34:38 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:303:89:cafe::dc) by MW4PR04CA0287.outlook.office365.com (2603:10b6:303:89::22) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9846.24 via Frontend Transport; Sat, 25 Apr 2026 17:34:38 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9846.18 via Frontend Transport; Sat, 25 Apr 2026 17:34:38 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 25 Apr 2026 10:34:27 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 25 Apr 2026 10:34:27 -0700 Received: from build-va-bionic-20251031.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 25 Apr 2026 10:34:26 -0700 From: Vishwaroop A To: Mark Brown CC: , , "Thierry Reding" , Jonathan Hunter , Francesco Dolcini , , Subject: [PATCH v2 1/2] spi: add new_device/delete_device sysfs interface Date: Sat, 25 Apr 2026 17:34:24 +0000 Message-ID: <20260425173425.622042-2-va@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260425173425.622042-1-va@nvidia.com> References: <20260420031417.291442-1-va@nvidia.com> <20260425173425.622042-1-va@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|DS2PR12MB9709:EE_ X-MS-Office365-Filtering-Correlation-Id: 98ec3cb3-a4d1-4829-6eb2-08dea2f0ecfd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|30052699003|1800799024|82310400026|36860700016|13003099007|22082099003|56012099003|18002099003; X-Microsoft-Antispam-Message-Info: kV0Ze7270ISoq9QkfmkXunaHKFfzrkmWNd0rjnPlqEY1dGDXyfnBPSAeqAJrXogSNFknUQq0OZkiESJHNMyyQhzQhFPFwg/ZizK4oar4cTdBQNUvzLO5SaHzPBJKzKZ+r3mCos4He+MDUqS/zCqQhiwDXrGE2rJM9NFdpFd6M27bO+lZInAJB13SmEOoiZQNoh5iE9XxIT+ilCcyBgwPa0y4eOlkAX1Wc4EBHtXgJ+22LJ3hMfb4dNkwCyboInUQzbGm9/nzwxtXy7Dy2Ux6YaxrFaOmTvBQH5j67RqQif5G6hAMq+9WBDlhRG8uiUAF4c781ZFNJk8SkkXe5nDnuP+ZpdQsTN1Zd6OfGUPAOptt73RaMxQul+7EQtbk9cPNQtqLYVn65I1ZJ2/2haJtZ9GePghAN2nyXsvUJkFfBYS9XJGtWdzsypbOakJzHMZO7NcG3vEfYASKz8o7SI9Cf6wTUOa/qZY04UP6f7VkkkAEfGt1pwzOiLh8y39y9EM8d2E9ZqZx4rtd0WItPSyqE4O53ScVXjacWVcrejRYBUQm69fZejoE8UIhf9cG/LLPkm+uJKjRotV0PjOzFGnk1SQrQaOqrlWA6arUzojvCJVCMU5SzDWfxOZoTeUi2Df5V50X6FU477dO6EyQYYdr7S5w41dKfxhVwGzuENoZSPtc5pN8lGPyZrq0JvRjFnsX2kC0aSgd6gjRB8AC/jGJBHGmRL2mzfDEAOJrIBhMbf/CPAcmWkEW3/BkWqSMpVpzpwTGoBTjCiTdsK1njpruyA== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(30052699003)(1800799024)(82310400026)(36860700016)(13003099007)(22082099003)(56012099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qb2zMQje/trKVHhsYWw0k9S7jgmpKTS1DAZ1wBW8LTvnpe3kwnWe21r5Unj0MijnHTgmvYRXyW5qsZQidXbL2A3nLzEEQ6TMRZ1k/HjgmItPtzIDGlvHIRsfhNMbkox8dNosSrptmXhsEVVidY2VppT2eA1ua844Q6LaapMnApgDQue0pBeg69xac6NWn1I9tl4mkMv1Quo6uc79DBsrbT1Toc7Xrn/Sv+TK4jMlZa+SqSRuCjGOBRR/STVaL0WfQx+HnEHE+PAJlbgjx0v3lxtgrsexU+Y8TevXrONjkncf4I+r4MR8EyeStGbRT0QtP65q50lSR2f5sj6rrtEY/diuD1vMJsJXcbtGQexP1ZIy5b/gmoSQpqjy/TUmIxwVJxfxlDtJJT5gOX8zH9obEy1joFrzSde7iEmpkZCUl9k72Ty7+TSVOvLl9wF0sIOi X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2026 17:34:38.4203 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 98ec3cb3-a4d1-4829-6eb2-08dea2f0ecfd X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS2PR12MB9709 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Development boards such as the Jetson AGX Orin expose SPI buses on expansion headers (e.g. the 40-pin header) so that users can connect and interact with SPI peripherals from userspace. The standard way to get /dev/spidevB.C character device nodes for this purpose is to register spi_device instances backed by the spidev driver. Today there is no viable way to do this on upstream kernels: - The spidev driver rejects the bare "spidev" compatible string in DT, since spidev is a Linux software interface and not a description of real hardware. - Vendor-specific compatible strings (e.g. "nvidia,tegra-spidev") have been rejected by DT maintainers for the same reason. The I2C subsystem solved an analogous problem by exposing new_device/delete_device sysfs attributes on each adapter. Add the same interface to SPI host controllers, so that userspace (e.g. a systemd unit at boot) can instantiate SPI devices at runtime without needing anything in device-tree. The new_device file accepts: [ []] where chip_select is required, while max_speed_hz and mode are optional and default to 0 if omitted. max_speed_hz =3D=3D 0 is clamped to the controller's maximum by spi_setup(); mode =3D=3D 0 selects SPI mode 0 (CPOL=3D0, CPHA=3D0). The modalias is used both as the device identifier and as a driver_override, so that the device binds to the named driver directly. This is necessary because some drivers like spidev deliberately exclude generic names from their id_table. Devices created this way are limited compared to those declared via DT or board files: - No IRQ is assigned (the device gets IRQ 0 / no interrupt). - No platform_data or device properties are attached. - No OF node is associated with the device. These limitations are acceptable for spidev, which only needs a registered spi_device to expose a character device to userspace. Only devices created via new_device can be removed through delete_device; DT and platform devices are unaffected. Link: https://lore.kernel.org/linux-tegra/909f0c92-d110-4253-903e-5c81e21e1= 2c9@nvidia.com/ Signed-off-by: Vishwaroop A --- drivers/spi/spi.c | 190 ++++++++++++++++++++++++++++++++++++++++ include/linux/spi/spi.h | 10 +++ 2 files changed, 200 insertions(+) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 7001f5dce8bd..916741690462 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -296,8 +296,178 @@ static const struct attribute_group spi_controller_st= atistics_group =3D { .attrs =3D spi_controller_statistics_attrs, }; =20 +static void spi_dev_set_name(struct spi_device *spi); +static int __spi_add_device(struct spi_device *spi, struct spi_device *par= ent); + +/* + * new_device_store - instantiate a new SPI device from userspace + * + * Takes parameters: [ []] + * + * Examples: + * echo spidev 0 > new_device + * echo spidev 0 10000000 > new_device + * echo spidev 0 10000000 3 > new_device + */ +static ssize_t +new_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctlr =3D container_of(dev, struct spi_controller, + dev); + struct spi_device *spi; + char modalias[SPI_NAME_SIZE]; + u16 chip_select; + u32 max_speed_hz =3D 0; + u32 mode =3D 0; + char *blank; + int n, res, status; + + blank =3D strchr(buf, ' '); + if (!blank) { + dev_err(dev, "%s: Missing parameters\n", "new_device"); + return -EINVAL; + } + + if (blank - buf > SPI_NAME_SIZE - 1) { + dev_err(dev, "%s: Invalid device name\n", "new_device"); + return -EINVAL; + } + + memset(modalias, 0, sizeof(modalias)); + memcpy(modalias, buf, blank - buf); + + /* + * sscanf fills only the fields it matches; unmatched optional + * fields (max_speed_hz, mode) stay zero from initialisation above. + * max_speed_hz =3D=3D 0 is clamped to the controller max by spi_setup(). + * mode =3D=3D 0 selects SPI mode 0 (CPOL=3D0, CPHA=3D0). + */ + res =3D sscanf(++blank, "%hu %u %u%n", + &chip_select, &max_speed_hz, &mode, &n); + if (res < 1) { + dev_err(dev, "%s: Can't parse chip select\n", "new_device"); + return -EINVAL; + } + + if (chip_select >=3D ctlr->num_chipselect) { + dev_err(dev, "%s: Chip select %u >=3D max %u\n", "new_device", + chip_select, ctlr->num_chipselect); + return -EINVAL; + } + + spi =3D spi_alloc_device(ctlr); + if (!spi) + return -ENOMEM; + + spi_set_chipselect(spi, 0, chip_select); + spi->max_speed_hz =3D max_speed_hz; + spi->mode =3D mode; + spi->cs_index_mask =3D BIT(0); + strscpy(spi->modalias, modalias, sizeof(spi->modalias)); + + /* + * Set driver_override so that the device binds to the driver + * named by modalias regardless of whether that driver's + * id_table contains a matching entry. This is needed because + * some drivers (e.g. spidev) deliberately omit generic names + * from their id_table. + */ + status =3D device_set_driver_override(&spi->dev, modalias); + if (status) { + spi_dev_put(spi); + return status; + } + + /* + * Use __spi_add_device() directly rather than spi_add_device() + * so we can hold add_lock across both the device registration + * and the list insertion. Without this, spi_unregister_controller() + * could run between the two operations: the device would be known + * to the core (so __unregister iterates over it) but not yet on + * the userspace_clients list, leading to a double-free or a + * dangling list entry. + */ + spi_dev_set_name(spi); + + mutex_lock(&ctlr->add_lock); + status =3D __spi_add_device(spi, NULL); + if (status) { + mutex_unlock(&ctlr->add_lock); + spi_dev_put(spi); + return status; + } + + mutex_lock(&ctlr->userspace_clients_lock); + list_add_tail(&spi->userspace_node, &ctlr->userspace_clients); + mutex_unlock(&ctlr->userspace_clients_lock); + mutex_unlock(&ctlr->add_lock); + + dev_info(dev, "%s: Instantiated device %s at CS%u\n", "new_device", + modalias, chip_select); + + return count; +} +static DEVICE_ATTR_WO(new_device); + +static ssize_t +delete_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctlr =3D container_of(dev, struct spi_controller, + dev); + struct spi_device *spi, *next; + unsigned short cs; + char end; + int res; + + res =3D sscanf(buf, "%hu%c", &cs, &end); + if (res < 1) { + dev_err(dev, "%s: Can't parse chip select\n", "delete_device"); + return -EINVAL; + } + if (res > 1 && end !=3D '\n') { + dev_err(dev, "%s: Extra parameters\n", "delete_device"); + return -EINVAL; + } + + res =3D -ENOENT; + mutex_lock(&ctlr->userspace_clients_lock); + list_for_each_entry_safe(spi, next, &ctlr->userspace_clients, + userspace_node) { + if (spi_get_chipselect(spi, 0) =3D=3D cs) { + dev_info(dev, "%s: Deleting device %s at CS%u\n", + "delete_device", spi->modalias, cs); + + list_del(&spi->userspace_node); + spi_unregister_device(spi); + res =3D count; + break; + } + } + mutex_unlock(&ctlr->userspace_clients_lock); + + if (res < 0) + dev_err(dev, "%s: Can't find device in list\n", + "delete_device"); + return res; +} +static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, 0200, NULL, + delete_device_store); + +static struct attribute *spi_controller_userspace_attrs[] =3D { + &dev_attr_new_device.attr, + &dev_attr_delete_device.attr, + NULL, +}; + +static const struct attribute_group spi_controller_userspace_group =3D { + .attrs =3D spi_controller_userspace_attrs, +}; + static const struct attribute_group *spi_controller_groups[] =3D { &spi_controller_statistics_group, + &spi_controller_userspace_group, NULL, }; =20 @@ -3256,6 +3426,8 @@ struct spi_controller *__spi_alloc_controller(struct = device *dev, mutex_init(&ctlr->bus_lock_mutex); mutex_init(&ctlr->io_mutex); mutex_init(&ctlr->add_lock); + mutex_init(&ctlr->userspace_clients_lock); + INIT_LIST_HEAD(&ctlr->userspace_clients); ctlr->bus_num =3D -1; ctlr->num_chipselect =3D 1; ctlr->num_data_lanes =3D 1; @@ -3633,6 +3805,24 @@ void spi_unregister_controller(struct spi_controller= *ctlr) if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) mutex_lock(&ctlr->add_lock); =20 + /* + * Remove devices created via the sysfs new_device interface. + * These must be explicitly removed before device_for_each_child() + * below because spi_unregister_device() does not remove devices + * from the userspace_clients list; freeing them without list_del() + * first would leave dangling pointers in that list. + */ + mutex_lock(&ctlr->userspace_clients_lock); + while (!list_empty(&ctlr->userspace_clients)) { + struct spi_device *spi; + + spi =3D list_first_entry(&ctlr->userspace_clients, + struct spi_device, userspace_node); + list_del(&spi->userspace_node); + spi_unregister_device(spi); + } + mutex_unlock(&ctlr->userspace_clients_lock); + device_for_each_child(&ctlr->dev, NULL, __unregister); =20 /* First make sure that this controller was ever added */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 7587b1c5d7ec..63c267ca9730 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -250,6 +250,9 @@ struct spi_device { u8 rx_lane_map[SPI_DEVICE_DATA_LANE_CNT_MAX]; u8 num_rx_lanes; =20 + /* Entry on controller's userspace_clients list */ + struct list_head userspace_node; + /* * Likely need more hooks for more protocol options affecting how * the controller talks to each chip, like: @@ -554,6 +557,9 @@ extern struct spi_device *devm_spi_new_ancillary_device= (struct spi_device *spi, * @defer_optimize_message: set to true if controller cannot pre-optimize = messages * and needs to defer the optimization step until the message is actually * being transferred + * @userspace_clients: list of SPI devices instantiated from userspace via + * the sysfs new_device interface + * @userspace_clients_lock: mutex protecting @userspace_clients * * Each SPI controller can communicate with one or more @spi_device * children. These make a small bus, sharing MOSI, MISO and SCK signals @@ -809,6 +815,10 @@ struct spi_controller { bool queue_empty; bool must_async; bool defer_optimize_message; + + /* List of SPI devices created via sysfs new_device interface */ + struct list_head userspace_clients; + struct mutex userspace_clients_lock; }; =20 static inline void *spi_controller_get_devdata(struct spi_controller *ctlr) --=20 2.17.1 From nobody Tue Jun 16 12:41:43 2026 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012021.outbound.protection.outlook.com [40.93.195.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2526E2AE78; Sat, 25 Apr 2026 17:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.195.21 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777138478; cv=fail; b=Nw1qTHm/D8rOj/uAtsHh8BDfAcCPceorxLcCvj47gTXyhOfOFXpSWbcpD4hSqnAf0DJLKCURNE+IrSru2QZuSslZMcOXvYGguCuyJABHSbvWhE5kwgg7xm3eTUqC64/K9vtqgVHLGOwhtcCdH2t384qosGkQl16BkUBC0ZllTV4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777138478; c=relaxed/simple; bh=Ou83dAw2WBD3/Hzx1mZsd7+BkNqm3pBdFfvn15o/wqs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iCD4xgw01omklFK0Y/+KWwXXVihEGWlTIGDEcNnG3EYv5DaAHG6MhC0L37Owtb6vRF0TiECTxyuDrTEECGMO1oydb1yiTGWdILVf2b4JIzxrdSNBk3BHIRRA9nhA8cgKjYUG7WusPNbVAeNincbE0djbbJ8/dYQkn5bANVgXIh0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=W8/tmXd6; arc=fail smtp.client-ip=40.93.195.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="W8/tmXd6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=aTm7n3b6xWWCFtGZ70/5DhwvM3xvVbg7iq0ORyBJjhAr0ibwNKE3uudTAYxRVnMzNWOsQEXPfTmCwjDMTCJ3wWudKgwv/OtWRT2v/Fg3SnZOqX9uYhM/dL4Zc2Lz0olNrn3FnMY/kv24CXJGz3Mj8Mi4utOi3nst+IdN32mx8GvEO45sf+MZXrS5SJI7eT56EAUYAEPdX4EksX0r2gHcxUFw9VR3GOwxJQ0HxgKY3nSH9gGl5pUqXoqiSvxdotaEO82UYYfCIPtD5GCgksDDtZZ+qiXHIyYhFDaRPK/MALXv6fIRs2qXWzLJpj8/30Dq5+o7J0HTQxeFB9ktmDWGZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sHzC2Nb1OFnYoQpsB5d+r63ZC5H9ijhPswIpPK5NHoI=; b=FU0qeMy6dVP9nAvz2BBBDVQzhTmqBjhTBTxf1MWKXTy/E/uWbfxbXnJtUJSZr5yX9qPG5FwkBn1e6BNjkaXMDIfQwtwMGDiNktMob5AsIMW2PzlLmm3ABYS9GSb0AoqUOM5/bFnpbrEz1d05BAOCsgpxgINFnZz35GBxRn99eGPKudEpkboPoD98uT3qNNyTw+g9Tmd58YXh8XT4NPX/jx2GPdaAjQYgcKHDbBkCWKiTigUVYBeHE7MegIZv/n+scXFUNO3D36OGWqTo3zJka/z6hLHd0Sa2912evKP7JcRNVzEMg5XWWtgkDQnhcrOcD01uFSVfcI3/mszRTYHBnA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=sHzC2Nb1OFnYoQpsB5d+r63ZC5H9ijhPswIpPK5NHoI=; b=W8/tmXd64GzGSUuW0PsvRq8vGJuQF5xKCdKK2l+yq7Op5bdVX5CzYF75A5Z+u1Mcvnr/mAYhCKony1NZ1yyOR2Pg549Y47DePPAuQeaImjmml0jAGbFfVuXElMINqz0JRSSt5tUZNsDOEoWbR3g4HcF3LrQR5VEHsrSSVM9wFu1FOVIncQ8RUSfegNcrGguHQ+kOlKmatQ/x7YE7cvXzzqbaXsK9JyJDiJ2WezPkZdej4+c6vlf1a7Y2ATictM5BBOp2z6lSrs76mGoQ8ZyxLgjjcLnBVaoVIa44m46UxgCSPyq7Rz0k9DmyTTPIcjeZLbPVFJ3yixx+dYwKAzUkbw== Received: from DSSP221CA0010.NAMP221.PROD.OUTLOOK.COM (2603:10b6:8:3d5::6) by SA5PPF634736581.namprd12.prod.outlook.com (2603:10b6:80f:fc04::8cd) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9818.21; Sat, 25 Apr 2026 17:34:32 +0000 Received: from DS3PEPF0000C37F.namprd04.prod.outlook.com (2603:10b6:8:3d5:cafe::9d) by DSSP221CA0010.outlook.office365.com (2603:10b6:8:3d5::6) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9846.24 via Frontend Transport; Sat, 25 Apr 2026 17:34:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF0000C37F.mail.protection.outlook.com (10.167.23.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9846.18 via Frontend Transport; Sat, 25 Apr 2026 17:34:32 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 25 Apr 2026 10:34:28 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 25 Apr 2026 10:34:28 -0700 Received: from build-va-bionic-20251031.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 25 Apr 2026 10:34:27 -0700 From: Vishwaroop A To: Mark Brown CC: , , "Thierry Reding" , Jonathan Hunter , Francesco Dolcini , , Subject: [PATCH v2 2/2] docs: spi: add documentation for userspace device instantiation Date: Sat, 25 Apr 2026 17:34:25 +0000 Message-ID: <20260425173425.622042-3-va@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260425173425.622042-1-va@nvidia.com> References: <20260420031417.291442-1-va@nvidia.com> <20260425173425.622042-1-va@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF0000C37F:EE_|SA5PPF634736581:EE_ X-MS-Office365-Filtering-Correlation-Id: fc48be23-5d98-4738-c00f-08dea2f0e93b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|1800799024|376014|30052699003|82310400026|22082099003|56012099003|18002099003; X-Microsoft-Antispam-Message-Info: KHmPdtaRMRb+JmhF0iUC9h+SWz1o+qm+s0HW/zZJ0+DKvMRjgEEEZ/1hC+ZX0vN0sos2dzOLF+1+WXjVoVjQENT29ojcW2awkAp6sy8KUnemLi4yOj1EgHPF5WSOQI3jPiwdgzcJTP4ECTGCSQSzGWIwJd/4KiwKGoZh/GNMe3QCAH88kSfHgwzCJEIjx2EXgE0B2GGqh2Lcw+lrrcu7gxT9rOc06yOunMOqLp5i6k1tIY9pTxWzvcInxkqhwrtBan2NYv96QwAWlebUsIJXujAR4CRZbhNN0LC5wi2lAJR2asfylVU0R8uYyDyoGQaMe6PwbBAh2Y9JW7UiAbXddIhmKZIiPj1g+BvZjBHIRYd+qgzbppNiQPt+11b4YoaRJGWFhVJ2mvJliJ9v49X8sfrUtrPrYyv4HLT3VE8UcVStX3Cwl2csmrx98wPuWNI+6zQFd9VVNWD3Z7MsRkLQyndwW6VXe1te2S2qHNYTgl8emyUvE68RoMFv5MYF1y/8jKMrdVHNie/s0bA0R5+tVf54AGeV3FTClhFCemW4v85o3LjOLq0l3I9mHMJWDb1sKn4ZbTU5yVStkT3sE1jZPtBiAfge3r7LxWR0AbvLeRHKXW9T2sXOSHGjsaAx1fhteEn4Uk4AjZKd2NwVxfbzRdJoQPOOkiFgMDiCG0m/gzoTPCJdIpm7uDrWXi5yfJ1UjTjxZBDrMbnBpDXXH3hy9ao6ZWUq7naJ6MWubiWvwb6CLyRBtd7eaEHxE4m+dlbHMSWvCdI2VCkkx93V3WWugw== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700016)(1800799024)(376014)(30052699003)(82310400026)(22082099003)(56012099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bg4kwrbUfCKjfZOGb9Y8uCEhkhJHFwVJCgC39KfS5muVv7hcp/A4StBoz+d4kFIDmsh46v37aXS2dpbvFkmoxtBrkYNsfMbQly9jzDG3njhLT0RVgzmo/vWzzT9iLIxS9AqBZaUC5u6VSfc9dfdu0e3B+5qlsvG8TmCpAzy5Qs8oyjJDuVtf7jppC2WxCbl58LD880b4mA10R2KyFlzOPQQxt/V33Yw0e9xlZybh3ti7//WRjf/3LuEcNyRob1Q1Cgbjh/FdFRLihEP+0Uak8VZ05Pkcv0Io1uCdLABT+Y+JRTGUs0JaV7CIqOiBKNGvztFP1w8izdjx8JGnpzcXKLAB3EqCaJThWDGbvvBekQc70c4EDD+WIbVKOVt//rgzeFIEXcTYQnyb1gKb6UnSqe40HwabufcQtbAj6tMGvit1RrSffcQ8K21n24ClPg+o X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2026 17:34:32.0521 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fc48be23-5d98-4738-c00f-08dea2f0e93b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF0000C37F.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA5PPF634736581 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document the new_device and delete_device sysfs attributes on SPI controllers: - Documentation/spi/instantiating-devices.rst: describes when and why this interface is needed, accepted parameters, usage examples, and limitations. - Documentation/ABI/testing/sysfs-class-spi-master: formal ABI entry for both attributes. Signed-off-by: Vishwaroop A --- .../ABI/testing/sysfs-class-spi-master | 34 +++++++ Documentation/spi/index.rst | 1 + Documentation/spi/instantiating-devices.rst | 88 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-class-spi-master create mode 100644 Documentation/spi/instantiating-devices.rst diff --git a/Documentation/ABI/testing/sysfs-class-spi-master b/Documentati= on/ABI/testing/sysfs-class-spi-master new file mode 100644 index 000000000000..b498be128bad --- /dev/null +++ b/Documentation/ABI/testing/sysfs-class-spi-master @@ -0,0 +1,34 @@ +What: /sys/class/spi_master/spiB/new_device +Date: April 2026 +KernelVersion: 7.2 +Contact: linux-spi@vger.kernel.org +Description: (WO) Instantiate a new SPI device on bus B, where B + is the bus number (0, 1, 2, ...). Takes parameters + in the format: + + [ []] + + where modalias is the driver name, chip_select is the + CS line number, and max_speed_hz and mode are optional. + + The device can later be removed with delete_device. + + Only devices created via this interface can be removed + with delete_device; platform and DT devices are not + affected. + + Example: + # echo spidev 0 > /sys/class/spi_master/spi0/new_device + # echo spidev 0 10000000 > /sys/class/spi_master/spi0/new_device + # echo spidev 0 10000000 3 > /sys/class/spi_master/spi0/new_device + +What: /sys/class/spi_master/spiB/delete_device +Date: April 2026 +KernelVersion: 7.2 +Contact: linux-spi@vger.kernel.org +Description: (WO) Remove a SPI device previously created via + new_device. Takes a single parameter: the chip select + number of the device to remove. + + Example: + # echo 0 > /sys/class/spi_master/spi0/delete_device diff --git a/Documentation/spi/index.rst b/Documentation/spi/index.rst index ac0c2233ce48..3f723e2c07da 100644 --- a/Documentation/spi/index.rst +++ b/Documentation/spi/index.rst @@ -8,6 +8,7 @@ Serial Peripheral Interface (SPI) :maxdepth: 1 =20 spi-summary + instantiating-devices spidev multiple-data-lanes butterfly diff --git a/Documentation/spi/instantiating-devices.rst b/Documentation/sp= i/instantiating-devices.rst new file mode 100644 index 000000000000..9ed08d94ae01 --- /dev/null +++ b/Documentation/spi/instantiating-devices.rst @@ -0,0 +1,88 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D +How to instantiate SPI devices +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D + +SPI devices are normally declared statically via device-tree, ACPI, or +board files. When the SPI controller is registered, these devices are +instantiated automatically by the SPI core. This is the preferred method +for any device with a proper kernel driver. + +Instantiate from user-space +--------------------------- + +In certain cases a SPI device cannot be declared statically: + +* The ``spidev`` driver, which provides raw userspace access to SPI + buses, explicitly rejects the bare ``"spidev"`` compatible string in + device-tree because spidev is a Linux implementation detail, not a + hardware description. Vendor-specific compatible strings for spidev + (e.g. ``"vendor,board-spidev"``) are also generally not accepted + upstream. Device-tree overlays do not help here either, since the + spidev driver performs the same compatible check regardless of how + the DT node was loaded. + +* You are developing or testing a SPI device on a development board + where the SPI bus is exposed on expansion headers, and the connected + device may change frequently. + +For these cases, a sysfs interface is provided on each SPI controller +(similar to the I2C ``new_device``/``delete_device`` interface described +in Documentation/i2c/instantiating-devices.rst). Two write-only +attribute files are created in every SPI controller directory: +``new_device`` and ``delete_device``. + +File ``new_device`` takes 2 to 4 parameters: the name of the SPI +device (a string), the chip select number, and optionally +``max_speed_hz`` and ``mode``:: + + [ []] + +The modalias is set both as the device's ``modalias`` field and as its +``driver_override``. This ensures that the device binds to the named +driver directly, bypassing the normal bus matching logic (OF, ACPI, +and ``id_table``). This is necessary because drivers like ``spidev`` +deliberately exclude generic names from their ``id_table``. + +If ``max_speed_hz`` is omitted or 0, ``spi_setup()`` clamps it to +the controller's maximum speed. If ``mode`` is omitted, SPI mode 0 +(CPOL=3D0, CPHA=3D0) is used. + +File ``delete_device`` takes a single parameter: the chip select +number. As no two devices can share a chip select on a given SPI bus, +the chip select is sufficient to uniquely identify the device. + +Examples:: + + # Create a spidev device on SPI bus 0, chip select 0 + echo spidev 0 > /sys/class/spi_master/spi0/new_device + + # Create with explicit clock rate and SPI mode + echo spidev 0 10000000 3 > /sys/class/spi_master/spi0/new_device + + # Remove the device + echo 0 > /sys/class/spi_master/spi0/delete_device + +On systems that need spidev access at boot, a systemd service or +udev rule can write to ``new_device`` after the SPI controller is +available. + +Limitations +^^^^^^^^^^^ + +Devices created through this interface have the following limitations +compared to devices declared via device-tree: + +* No interrupt (IRQ) support. +* No additional properties such as ``spi-max-frequency`` DT bindings + or controller-specific configuration. +* No platform data or software nodes. + +For ``spidev`` usage these limitations are not relevant, since spidev +provides a raw byte-level interface that does not require any of these +features. + +Only devices created via ``new_device`` can be removed through +``delete_device``. Devices declared via device-tree, ACPI, or board +files are not affected by this interface. --=20 2.17.1