2019-04-24 19:24:16 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.hardware.deviceTree;
|
2020-02-05 15:35:34 +01:00
|
|
|
|
|
|
|
overlayType = types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Name of this overlay
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dtsFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
description = ''
|
|
|
|
Path to .dts overlay file, overlay is applied to
|
|
|
|
each .dtb file matching "compatible" of the overlay.
|
|
|
|
'';
|
|
|
|
default = null;
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression "./dts/overlays.dts";
|
2020-02-05 15:35:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
dtsText = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Literal DTS contents, overlay is applied to
|
|
|
|
each .dtb file matching "compatible" of the overlay.
|
|
|
|
'';
|
2021-10-03 18:06:03 +02:00
|
|
|
example = ''
|
2020-02-05 15:35:34 +01:00
|
|
|
/dts-v1/;
|
|
|
|
/plugin/;
|
|
|
|
/ {
|
|
|
|
compatible = "raspberrypi";
|
|
|
|
fragment@0 {
|
|
|
|
target-path = "/soc";
|
|
|
|
__overlay__ {
|
|
|
|
pps {
|
|
|
|
compatible = "pps-gpio";
|
|
|
|
status = "okay";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dtboFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Path to .dtbo compiled overlay file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# this requires kernel package
|
|
|
|
dtbsWithSymbols = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "dtbs-with-symbols";
|
|
|
|
inherit (cfg.kernelPackage) src nativeBuildInputs depsBuildBuild;
|
|
|
|
patches = map (patch: patch.patch) cfg.kernelPackage.kernelPatches;
|
|
|
|
buildPhase = ''
|
|
|
|
patchShebangs scripts/*
|
|
|
|
substituteInPlace scripts/Makefile.lib \
|
|
|
|
--replace 'DTC_FLAGS += $(DTC_FLAGS_$(basetarget))' 'DTC_FLAGS += $(DTC_FLAGS_$(basetarget)) -@'
|
2021-01-23 02:33:55 +01:00
|
|
|
make ${pkgs.stdenv.hostPlatform.linux-kernel.baseConfig} ARCH="${pkgs.stdenv.hostPlatform.linuxArch}"
|
|
|
|
make dtbs ARCH="${pkgs.stdenv.hostPlatform.linuxArch}"
|
2020-02-05 15:35:34 +01:00
|
|
|
'';
|
|
|
|
installPhase = ''
|
2021-01-23 02:33:55 +01:00
|
|
|
make dtbs_install INSTALL_DTBS_PATH=$out/dtbs ARCH="${pkgs.stdenv.hostPlatform.linuxArch}"
|
2020-02-05 15:35:34 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
filterDTBs = src: if isNull cfg.filter
|
|
|
|
then "${src}/dtbs"
|
|
|
|
else
|
|
|
|
pkgs.runCommand "dtbs-filtered" {} ''
|
|
|
|
mkdir -p $out
|
|
|
|
cd ${src}/dtbs
|
|
|
|
find . -type f -name '${cfg.filter}' -print0 \
|
|
|
|
| xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Compile single Device Tree overlay source
|
|
|
|
# file (.dts) into its compiled variant (.dtbo)
|
|
|
|
compileDTS = name: f: pkgs.callPackage({ dtc }: pkgs.stdenv.mkDerivation {
|
|
|
|
name = "${name}-dtbo";
|
|
|
|
|
|
|
|
nativeBuildInputs = [ dtc ];
|
|
|
|
|
|
|
|
buildCommand = ''
|
|
|
|
dtc -I dts ${f} -O dtb -@ -o $out
|
|
|
|
'';
|
|
|
|
}) {};
|
|
|
|
|
|
|
|
# Fill in `dtboFile` for each overlay if not set already.
|
|
|
|
# Existence of one of these is guarded by assertion below
|
|
|
|
withDTBOs = xs: flip map xs (o: o // { dtboFile =
|
|
|
|
if isNull o.dtboFile then
|
|
|
|
if !isNull o.dtsFile then compileDTS o.name o.dtsFile
|
|
|
|
else compileDTS o.name (pkgs.writeText "dts" o.dtsText)
|
|
|
|
else o.dtboFile; } );
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "hardware" "deviceTree" "base" ] "Use hardware.deviceTree.kernelPackage instead")
|
|
|
|
];
|
|
|
|
|
2019-04-24 19:24:16 +02:00
|
|
|
options = {
|
|
|
|
hardware.deviceTree = {
|
|
|
|
enable = mkOption {
|
2021-01-23 02:33:55 +01:00
|
|
|
default = pkgs.stdenv.hostPlatform.linux-kernel.DTB or false;
|
2019-04-24 19:24:16 +02:00
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Build device tree files. These are used to describe the
|
|
|
|
non-discoverable hardware of a system.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-05 15:35:34 +01:00
|
|
|
kernelPackage = mkOption {
|
|
|
|
default = config.boot.kernelPackages.kernel;
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = literalExpression "config.boot.kernelPackages.kernel";
|
|
|
|
example = literalExpression "pkgs.linux_latest";
|
2019-05-09 00:25:22 +02:00
|
|
|
type = types.path;
|
2019-04-24 19:24:16 +02:00
|
|
|
description = ''
|
2020-02-05 15:35:34 +01:00
|
|
|
Kernel package containing the base device-tree (.dtb) to boot. Uses
|
2019-04-24 19:24:16 +02:00
|
|
|
device trees bundled with the Linux kernel by default.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-21 10:33:09 +02:00
|
|
|
name = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "some-dtb.dtb";
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = ''
|
|
|
|
The name of an explicit dtb to be loaded, relative to the dtb base.
|
|
|
|
Useful in extlinux scenarios if the bootloader doesn't pick the
|
|
|
|
right .dtb file from FDTDIR.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-05 15:35:34 +01:00
|
|
|
filter = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "*rpi*.dtb";
|
|
|
|
description = ''
|
|
|
|
Only include .dtb files matching glob expression.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-24 19:24:16 +02:00
|
|
|
overlays = mkOption {
|
|
|
|
default = [];
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2020-02-05 15:35:34 +01:00
|
|
|
[
|
|
|
|
{ name = "pps"; dtsFile = ./dts/pps.dts; }
|
|
|
|
{ name = "spi";
|
|
|
|
dtsText = "...";
|
|
|
|
}
|
|
|
|
{ name = "precompiled"; dtboFile = ./dtbos/example.dtbo; }
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
type = types.listOf (types.coercedTo types.path (path: {
|
|
|
|
name = baseNameOf path;
|
|
|
|
dtboFile = path;
|
|
|
|
}) overlayType);
|
2019-04-24 19:24:16 +02:00
|
|
|
description = ''
|
2020-02-05 15:35:34 +01:00
|
|
|
List of overlays to apply to base device-tree (.dtb) files.
|
2019-04-24 19:24:16 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.path;
|
2019-05-09 00:25:22 +02:00
|
|
|
internal = true;
|
2019-04-24 19:24:16 +02:00
|
|
|
description = ''
|
2020-02-05 15:35:34 +01:00
|
|
|
A path containing the result of applying `overlays` to `kernelPackage`.
|
2019-04-24 19:24:16 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (cfg.enable) {
|
2020-02-05 15:35:34 +01:00
|
|
|
|
|
|
|
assertions = let
|
|
|
|
invalidOverlay = o: isNull o.dtsFile && isNull o.dtsText && isNull o.dtboFile;
|
|
|
|
in lib.singleton {
|
|
|
|
assertion = lib.all (o: !invalidOverlay o) cfg.overlays;
|
|
|
|
message = ''
|
|
|
|
deviceTree overlay needs one of dtsFile, dtsText or dtboFile set.
|
|
|
|
Offending overlay(s):
|
|
|
|
${toString (map (o: o.name) (builtins.filter invalidOverlay cfg.overlays))}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-24 19:24:16 +02:00
|
|
|
hardware.deviceTree.package = if (cfg.overlays != [])
|
2020-02-05 15:35:34 +01:00
|
|
|
then pkgs.deviceTree.applyOverlays (filterDTBs dtbsWithSymbols) (withDTBOs cfg.overlays)
|
|
|
|
else (filterDTBs cfg.kernelPackage);
|
2019-04-24 19:24:16 +02:00
|
|
|
};
|
|
|
|
}
|