1*036d4b05SVishwaroop A.. SPDX-License-Identifier: GPL-2.0 2*036d4b05SVishwaroop A 3*036d4b05SVishwaroop A============================== 4*036d4b05SVishwaroop AHow to instantiate SPI devices 5*036d4b05SVishwaroop A============================== 6*036d4b05SVishwaroop A 7*036d4b05SVishwaroop ASPI devices are normally declared statically via device-tree, ACPI, or 8*036d4b05SVishwaroop Aboard files. When the SPI controller is registered, these devices are 9*036d4b05SVishwaroop Ainstantiated automatically by the SPI core. This is the preferred method 10*036d4b05SVishwaroop Afor any device with a proper kernel driver. 11*036d4b05SVishwaroop A 12*036d4b05SVishwaroop AInstantiate from user-space 13*036d4b05SVishwaroop A--------------------------- 14*036d4b05SVishwaroop A 15*036d4b05SVishwaroop AIn certain cases a SPI device cannot be declared statically: 16*036d4b05SVishwaroop A 17*036d4b05SVishwaroop A* The ``spidev`` driver, which provides raw userspace access to SPI 18*036d4b05SVishwaroop A buses, explicitly rejects the bare ``"spidev"`` compatible string in 19*036d4b05SVishwaroop A device-tree because spidev is a Linux implementation detail, not a 20*036d4b05SVishwaroop A hardware description. Vendor-specific compatible strings for spidev 21*036d4b05SVishwaroop A (e.g. ``"vendor,board-spidev"``) are also generally not accepted 22*036d4b05SVishwaroop A upstream. Device-tree overlays do not help here either, since the 23*036d4b05SVishwaroop A spidev driver performs the same compatible check regardless of how 24*036d4b05SVishwaroop A the DT node was loaded. 25*036d4b05SVishwaroop A 26*036d4b05SVishwaroop A* You are developing or testing a SPI device on a development board 27*036d4b05SVishwaroop A where the SPI bus is exposed on expansion headers, and the connected 28*036d4b05SVishwaroop A device may change frequently. 29*036d4b05SVishwaroop A 30*036d4b05SVishwaroop AFor these cases, a sysfs interface is provided on each SPI host controller 31*036d4b05SVishwaroop A(similar to the I2C ``new_device``/``delete_device`` interface described 32*036d4b05SVishwaroop Ain Documentation/i2c/instantiating-devices.rst). Two write-only 33*036d4b05SVishwaroop Aattribute files are created in every SPI host controller directory: 34*036d4b05SVishwaroop A``new_device`` and ``delete_device``. 35*036d4b05SVishwaroop A 36*036d4b05SVishwaroop AFile ``new_device`` takes 2 to 4 parameters: the name of the SPI 37*036d4b05SVishwaroop Adevice (a string), the chip select number, and optionally 38*036d4b05SVishwaroop A``max_speed_hz`` and ``mode``:: 39*036d4b05SVishwaroop A 40*036d4b05SVishwaroop A <modalias> <chip_select> [<max_speed_hz> [<mode>]] 41*036d4b05SVishwaroop A 42*036d4b05SVishwaroop AThe modalias is set both as the device's ``modalias`` field and as its 43*036d4b05SVishwaroop A``driver_override``. This ensures that the device binds to the named 44*036d4b05SVishwaroop Adriver directly, bypassing the normal bus matching logic (OF, ACPI, 45*036d4b05SVishwaroop Aand ``id_table``). This is necessary because drivers like ``spidev`` 46*036d4b05SVishwaroop Adeliberately exclude generic names from their ``id_table``. 47*036d4b05SVishwaroop A 48*036d4b05SVishwaroop AIf ``max_speed_hz`` is omitted or 0, ``spi_setup()`` clamps it to 49*036d4b05SVishwaroop Athe controller's maximum speed. If ``mode`` is omitted, SPI mode 0 50*036d4b05SVishwaroop A(CPOL=0, CPHA=0) is used. 51*036d4b05SVishwaroop A 52*036d4b05SVishwaroop AFile ``delete_device`` takes a single parameter: the chip select 53*036d4b05SVishwaroop Anumber. As no two devices can share a chip select on a given SPI bus, 54*036d4b05SVishwaroop Athe chip select is sufficient to uniquely identify the device. 55*036d4b05SVishwaroop A 56*036d4b05SVishwaroop AExamples:: 57*036d4b05SVishwaroop A 58*036d4b05SVishwaroop A # Create a spidev device on SPI bus 0, chip select 0 59*036d4b05SVishwaroop A echo spidev 0 > /sys/class/spi_master/spi0/new_device 60*036d4b05SVishwaroop A 61*036d4b05SVishwaroop A # Create with explicit clock rate and SPI mode 62*036d4b05SVishwaroop A echo spidev 0 10000000 3 > /sys/class/spi_master/spi0/new_device 63*036d4b05SVishwaroop A 64*036d4b05SVishwaroop A # Remove the device 65*036d4b05SVishwaroop A echo 0 > /sys/class/spi_master/spi0/delete_device 66*036d4b05SVishwaroop A 67*036d4b05SVishwaroop AThe attributes are added after the host controller and its firmware-described 68*036d4b05SVishwaroop Adevices have been registered. Their addition emits a ``change`` uevent, 69*036d4b05SVishwaroop Aallowing a udev rule to write to ``new_device`` when the interface is ready. 70*036d4b05SVishwaroop A 71*036d4b05SVishwaroop ALimitations 72*036d4b05SVishwaroop A^^^^^^^^^^^ 73*036d4b05SVishwaroop A 74*036d4b05SVishwaroop ADevices created through this interface have the following limitations 75*036d4b05SVishwaroop Acompared to devices declared via device-tree: 76*036d4b05SVishwaroop A 77*036d4b05SVishwaroop A* No interrupt (IRQ) support. 78*036d4b05SVishwaroop A* No additional properties such as ``spi-max-frequency`` DT bindings 79*036d4b05SVishwaroop A or controller-specific configuration. 80*036d4b05SVishwaroop A* No platform data or software nodes. 81*036d4b05SVishwaroop A 82*036d4b05SVishwaroop AFor ``spidev`` usage these limitations are not relevant, since spidev 83*036d4b05SVishwaroop Aprovides a raw byte-level interface that does not require any of these 84*036d4b05SVishwaroop Afeatures. 85*036d4b05SVishwaroop A 86*036d4b05SVishwaroop AOnly devices created via ``new_device`` can be removed through 87*036d4b05SVishwaroop A``delete_device``. Devices declared via device-tree, ACPI, or board 88*036d4b05SVishwaroop Afiles are not affected by this interface. 89