Describe an SPI Device and Its Chip Select in a Device Tree
Add an SPI peripheral to a device tree with reg as the chip select, spi-max-frequency and the mode flags, using an MCP3008 ADC as the worked example.

SPI has no addresses. Where I2C identifies a chip by an address on a shared pair of wires, SPI picks one with a dedicated select line per device, and a controller with two such lines can serve two chips. The device tree has to say which line each chip uses, how fast it may be clocked and in which of four timing modes it talks, and a mistake in any of the three gives a device that is found by the kernel and returns noise. This post describes an MCP3008, an 8 channel 10-bit ADC, on SPI for Linux 6.12, explains reg as a chip select number, the mode properties and the GPIO chip select option, and shows how to verify the reading.
The shape of an SPI node
The SPI controller is defined in the SoC's .dtsi and disabled. The board enables it and puts each peripheral inside as a child:
&spi0 {
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
adc@0 {
compatible = "microchip,mcp3008";
reg = <0>;
spi-max-frequency = <1000000>;
vref-supply = <®_3v3>;
};
};The pieces:
#address-cells = <1>and#size-cells = <0>set how the children'sregis read: one cell for the chip select number, none for a size.adc@0follows the naming rule: a generic role,adc, and the unit address equal toreg, here0.reg = <0>is the chip select number, an index into the controller's select lines. It is not a memory address. Chip select 0 is the controller's first line, and on boards wired for it, that is the pin namedCE0orCS0.spi-max-frequencyis the highest clock the device tolerates, in hertz. The controller driver picks a rate at or below it, rounded to what its divider offers. This property is required for every SPI child. The MCP3008's datasheet limits the clock depending on supply voltage, so check the figure for your rail: the value above is deliberately conservative.vref-supplyis a regulator reference the driver reads for the ADC's scale. It is a property of this binding, not of SPI generally, and the document atDocumentation/devicetree/bindings/iio/adc/microchip,mcp3008.yamllists the rest.
The mode properties
SPI has four modes, each a pairing of clock polarity (CPOL) and clock phase (CPHA). The device's datasheet states which it needs. The device tree encodes them as flags:
| Mode | CPOL | CPHA | Properties on the child node |
|---|---|---|---|
| 0 | 0 | 0 | none (the default) |
| 1 | 0 | 1 | spi-cpha |
| 2 | 1 | 0 | spi-cpol |
| 3 | 1 | 1 | spi-cpol and spi-cpha |
The MCP3008 works in modes 0 and 3, so the node above needs no flag. A sensor that needs mode 3 adds both:
sensor@1 {
compatible = "vendor,example-sensor";
reg = <1>;
spi-max-frequency = <5000000>;
spi-cpol;
spi-cpha;
};Other flags from the binding in Documentation/devicetree/bindings/spi/spi-peripheral-props.yaml: spi-cs-high for a device whose select is active high instead of low, spi-3wire for devices that share one data line, spi-lsb-first for the rare chip that sends the least significant bit first, and spi-rx-bus-width and spi-tx-bus-width for dual and quad SPI, where several data lines carry bits in parallel. A wrong mode shows up as readings that are shifted by a bit, constant or random, and the signature of wrong timing is that the data looks plausible but moves.
Chip selects as GPIOs
A controller has a limited number of native select lines, and some drivers apply them with fixed timing that a device may not like. Linux can use any GPIO as a chip select instead. The controller node lists them in cs-gpios, and the child's reg indexes into that list:
&spi0 {
status = "okay";
cs-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>,
<&gpio0 6 GPIO_ACTIVE_LOW>;
#address-cells = <1>;
#size-cells = <0>;
adc@0 {
compatible = "microchip,mcp3008";
reg = <0>;
spi-max-frequency = <1000000>;
};
adc@1 {
compatible = "microchip,mcp3008";
reg = <1>;
spi-max-frequency = <1000000>;
};
};GPIO_ACTIVE_LOW comes from <dt-bindings/gpio/gpio.h>, so this file needs the preprocessor and must be built by the kernel. Here reg = <0> means "the first entry of cs-gpios", GPIO 5, and reg = <1> the second. Each GPIO the controller uses as a select must be claimed by it, which the driver does by itself; you should not also declare it as a hog or give it to another driver.
On recent kernels the polarity in cs-gpios is honored the way the flags say, which was not always the case, so check the controller's binding for the release you target. A select that idles high when it should idle low makes every transfer fail in a way that looks like a dead chip.
Check it from userspace
After boot, the kernel registers an SPI device for each node and binds the driver by the compatible string. The device appears in sysfs, named spi<bus>.<chip select>:
ls /sys/bus/spi/devices/
ls /sys/bus/iio/devices/spi0.0
iio:device0spi0.0 is bus 0, chip select 0. The ADC driver registers an IIO device, iio:device0. Read a channel's raw value, with a known voltage on the input:
cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw
cat /sys/bus/iio/devices/iio:device0/in_voltage_scale512
3.222656250The first number is the 10-bit code: 512 of 1023 is half scale. The second is the millivolts per code for the supply, so 512 * 3.22265625 gives about 1650 mV on a 3.3 V reference. The reading you see depends on your input, so these are example values. If the raw value is always 0 or 1023, suspect the mode or the select line. If the file does not exist, the driver did not bind.
Why the device does not show up
The controller is disabled or has no driver. ls /sys/class/spi_master/ should list spi0. If it does not, the controller's status or its driver is the issue.
**reg and cs-gpios disagree.** A reg larger than the number of selects fails with a message from the SPI core such as chipselect 2 >= max 2, and the device is skipped. Count the entries.
The compatible string is not recognized. SPI devices are matched through the of: modalias, and many drivers register a second table of plain names in spi_device_id. The node's string must match a driver's table; look at the driver's of_match_table.
**spidev in the device tree.** A node with compatible = "spidev" is rejected since Linux 5.x because it describes the Linux interface instead of the hardware; the driver prints spidev listed directly in DT is not allowed. Use the real part's string and, if you need raw access, a vendor string that spidev lists in its match table.
When this does not apply: a SPI device that the kernel has no driver for can be driven from userspace through spidev only after you declare it with an accepted compatible string. If you only prototype, that is fine, but ship the real binding once a kernel driver exists.


