From patchwork Thu Dec 21 20:03:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Roeschley X-Patchwork-Id: 10128349 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 B3956603B5 for ; Thu, 21 Dec 2017 20:03:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A660929E17 for ; Thu, 21 Dec 2017 20:03:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9AC8529E22; Thu, 21 Dec 2017 20:03:19 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5EB229E17 for ; Thu, 21 Dec 2017 20:03:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754579AbdLUUDS (ORCPT ); Thu, 21 Dec 2017 15:03:18 -0500 Received: from mx0a-00010702.pphosted.com ([148.163.156.75]:60704 "EHLO mx0b-00010702.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751866AbdLUUDR (ORCPT ); Thu, 21 Dec 2017 15:03:17 -0500 Received: from pps.filterd (m0098780.ppops.net [127.0.0.1]) by mx0a-00010702.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id vBLJtjsp018469; Thu, 21 Dec 2017 14:03:12 -0600 Received: from ni.com (skprod2.natinst.com [130.164.80.23]) by mx0a-00010702.pphosted.com with ESMTP id 2ew1vvcenx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 21 Dec 2017 14:03:11 -0600 Received: from us-aus-exhub1.ni.corp.natinst.com (us-aus-exhub1.ni.corp.natinst.com [130.164.68.41]) by us-aus-skprod2.natinst.com (8.16.0.21/8.16.0.21) with ESMTPS id vBLK3AiY011176 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Thu, 21 Dec 2017 14:03:10 -0600 Received: from us-aus-exch4.ni.corp.natinst.com (130.164.68.14) by us-aus-exhub1.ni.corp.natinst.com (130.164.68.41) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 21 Dec 2017 14:03:10 -0600 Received: from us-aus-exhub2.ni.corp.natinst.com (130.164.68.32) by us-aus-exch4.ni.corp.natinst.com (130.164.68.14) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 21 Dec 2017 14:03:09 -0600 Received: from senary.amer.corp.natinst.com (130.164.49.7) by us-aus-exhub2.ni.corp.natinst.com (130.164.68.32) with Microsoft SMTP Server id 15.0.1156.6 via Frontend Transport; Thu, 21 Dec 2017 14:03:09 -0600 From: Kyle Roeschley To: Mark Brown CC: , , "Geert Uytterhoeven" Subject: [PATCH] spi: Add a sysfs interface to instantiate devices Date: Thu, 21 Dec 2017 14:03:09 -0600 Message-ID: <20171221200309.17967-1-kyle.roeschley@ni.com> X-Mailer: git-send-email 2.15.1 MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-12-21_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=inbound_policy_notspam policy=inbound_policy score=30 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=30 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1712210268 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a sysfs interface to instantiate and delete SPI devices using the spidev driver. This can be used when developing a driver on a self-soldered board which doesn't yet have proper SPI device declaration at the platform level, and presumably for various debugging situations. Inspired by 99cd8e25875a ("i2c: Add a sysfs interface to instantiate devices"). Signed-off-by: Kyle Roeschley --- Documentation/spi/spi-summary | 14 ++++++++ drivers/spi/spi.c | 78 +++++++++++++++++++++++++++++++++++++++++++ include/linux/spi/spi.h | 3 ++ 3 files changed, 95 insertions(+) diff --git a/Documentation/spi/spi-summary b/Documentation/spi/spi-summary index 1721c1b570c3..51d9747c4426 100644 --- a/Documentation/spi/spi-summary +++ b/Documentation/spi/spi-summary @@ -339,6 +339,20 @@ up the spi bus master, and will likely need spi_new_device() to provide the board info based on the board that was hotplugged. Of course, you'd later call at least spi_unregister_device() when that board is removed. +Alternatively, a sysfs interface was added to let the user create devices which +using the spidev driver. This interface is made of 2 attribute files which are +created in every SPI master directory: new_device and delete_device. Both files +are write only and you must write the decimal SPI chip select number to them in +order to properly instantiate or delete a SPI device. As no two devices can be +attached to the same master with the same chip select line, the chip select +number is sufficient to uniquely identify the device to be deleted. + +Example: +# echo 1 > /sys/class/spi_master/spi0/new_device + +In general, this interface should only be used when in-kernel device +declaration can't be done. + When Linux includes support for MMC/SD/SDIO/DataFlash cards through SPI, those configurations will also be dynamic. Fortunately, such devices all support basic device identification probes, so they should hotplug normally. diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index b33a727a0158..648ccdf359f9 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -242,8 +242,85 @@ static const struct attribute_group spi_controller_statistics_group = { .attrs = spi_controller_statistics_attrs, }; +static ssize_t +new_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctlr = container_of(dev, struct spi_controller, + dev); + struct spi_device *spi; + struct spi_board_info bi = { + .modalias = "spidev", + .max_speed_hz = ctlr->max_speed_hz, + }; + + if (kstrtou16(buf, 0, &bi.chip_select) < 0) + return -EINVAL; + + spi = spi_new_device(ctlr, &bi); + if (!spi) { + dev_err(dev, "can't create new device\n"); + return -ENXIO; + } + + mutex_lock(&ctlr->bus_lock_mutex); + list_add_tail(&spi->userspace_device, &ctlr->userspace_devices); + mutex_unlock(&ctlr->bus_lock_mutex); + + dev_info(dev, "created spidev device %s\n", dev_name(&spi->dev)); + + 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 = container_of(dev, struct spi_controller, + dev); + struct spi_device *spi, *next; + int ret = -ENXIO; + u16 cs; + + if (kstrtou16(buf, 0, &cs) < 0) + return -EINVAL; + + mutex_lock(&ctlr->bus_lock_mutex); + list_for_each_entry_safe(spi, next, &ctlr->userspace_devices, + userspace_device) { + if (spi->chip_select != cs) + continue; + + dev_info(dev, "deleting spidev device %s\n", + dev_name(&spi->dev)); + list_del(&spi->userspace_device); + spi_unregister_device(spi); + ret = count; + break; + } + mutex_unlock(&ctlr->bus_lock_mutex); + + if (ret == -ENXIO) + dev_err(dev, "can't find spidev device %u in list\n", cs); + + return ret; +} +static DEVICE_ATTR_WO(delete_device); + +static struct attribute *spi_controller_userspace_attrs[] = { + &dev_attr_new_device.attr, + &dev_attr_delete_device.attr, + NULL, +}; + +static const struct attribute_group spi_controller_userspace_group = { + .attrs = spi_controller_userspace_attrs, +}; + static const struct attribute_group *spi_master_groups[] = { &spi_controller_statistics_group, + &spi_controller_userspace_group, NULL, }; @@ -2129,6 +2206,7 @@ int spi_register_controller(struct spi_controller *ctlr) return id; ctlr->bus_num = id; } + INIT_LIST_HEAD(&ctlr->userspace_devices); INIT_LIST_HEAD(&ctlr->queue); spin_lock_init(&ctlr->queue_lock); spin_lock_init(&ctlr->bus_lock_spinlock); diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index bc6bb325d1bf..f7255745326d 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -172,6 +172,8 @@ struct spi_device { /* the statistics */ struct spi_statistics statistics; + struct list_head userspace_device; + /* * likely need more hooks for more protocol options affecting how * the controller talks to each chip, like: @@ -410,6 +412,7 @@ struct spi_controller { struct device dev; struct list_head list; + struct list_head userspace_devices; /* other than negative (== assign one dynamically), bus_num is fully * board-specific. usually that simplifies to being SOC-specific.