2019-04-24 17:24:16 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.hardware.deviceTree;
|
2020-02-05 14:35:34 +00:00
|
|
|
|
|
|
|
overlayType = types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Name of this overlay
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-08-19 19:34:32 +00:00
|
|
|
filter = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "*rpi*.dtb";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Only apply to .dtb files matching glob expression.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-05 14:35:34 +00:00
|
|
|
dtsFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Path to .dts overlay file, overlay is applied to
|
|
|
|
each .dtb file matching "compatible" of the overlay.
|
|
|
|
'';
|
|
|
|
default = null;
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression "./dts/overlays.dts";
|
2020-02-05 14:35:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dtsText = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Literal DTS contents, overlay is applied to
|
|
|
|
each .dtb file matching "compatible" of the overlay.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = ''
|
2020-02-05 14:35:34 +00:00
|
|
|
/dts-v1/;
|
|
|
|
/plugin/;
|
|
|
|
/ {
|
|
|
|
compatible = "raspberrypi";
|
2022-06-18 01:29:29 +00:00
|
|
|
};
|
|
|
|
&{/soc} {
|
|
|
|
pps {
|
|
|
|
compatible = "pps-gpio";
|
|
|
|
status = "okay";
|
2020-02-05 14:35:34 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dtboFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Path to .dtbo compiled overlay file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
|
2022-08-19 16:57:13 +00:00
|
|
|
filteredDTBs = filterDTBs cfg.kernelPackage;
|
|
|
|
|
2020-02-05 14:35:34 +00:00
|
|
|
# Compile single Device Tree overlay source
|
|
|
|
# file (.dts) into its compiled variant (.dtbo)
|
2022-06-18 01:44:01 +00:00
|
|
|
compileDTS = name: f: pkgs.callPackage({ stdenv, dtc }: stdenv.mkDerivation {
|
2020-02-05 14:35:34 +00:00
|
|
|
name = "${name}-dtbo";
|
|
|
|
|
|
|
|
nativeBuildInputs = [ dtc ];
|
|
|
|
|
|
|
|
buildCommand = ''
|
2022-06-18 01:44:01 +00:00
|
|
|
$CC -E -nostdinc -I${getDev cfg.kernelPackage}/lib/modules/${cfg.kernelPackage.modDirVersion}/source/scripts/dtc/include-prefixes -undef -D__DTS__ -x assembler-with-cpp ${f} | \
|
|
|
|
dtc -I dts -O dtb -@ -o $out
|
2020-02-05 14:35:34 +00:00
|
|
|
'';
|
|
|
|
}) {};
|
|
|
|
|
|
|
|
# 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 17:24:16 +00:00
|
|
|
options = {
|
|
|
|
hardware.deviceTree = {
|
|
|
|
enable = mkOption {
|
2021-01-23 01:33:55 +00:00
|
|
|
default = pkgs.stdenv.hostPlatform.linux-kernel.DTB or false;
|
2019-04-24 17:24:16 +00:00
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-04-24 17:24:16 +00:00
|
|
|
Build device tree files. These are used to describe the
|
|
|
|
non-discoverable hardware of a system.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-05 14:35:34 +00:00
|
|
|
kernelPackage = mkOption {
|
|
|
|
default = config.boot.kernelPackages.kernel;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "config.boot.kernelPackages.kernel";
|
|
|
|
example = literalExpression "pkgs.linux_latest";
|
2019-05-08 22:25:22 +00:00
|
|
|
type = types.path;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Kernel package containing the base device-tree (.dtb) to boot. Uses
|
2019-04-24 17:24:16 +00:00
|
|
|
device trees bundled with the Linux kernel by default.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-21 08:33:09 +00:00
|
|
|
name = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "some-dtb.dtb";
|
|
|
|
type = types.nullOr types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-06-21 08:33:09 +00:00
|
|
|
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 14:35:34 +00:00
|
|
|
filter = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "*rpi*.dtb";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
Only include .dtb files matching glob expression.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-24 17:24:16 +00:00
|
|
|
overlays = mkOption {
|
|
|
|
default = [];
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-02-05 14:35:34 +00: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;
|
2022-08-19 19:34:32 +00:00
|
|
|
filter = null;
|
2020-02-05 14:35:34 +00:00
|
|
|
dtboFile = path;
|
|
|
|
}) overlayType);
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
List of overlays to apply to base device-tree (.dtb) files.
|
2019-04-24 17:24:16 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.path;
|
2019-05-08 22:25:22 +00:00
|
|
|
internal = true;
|
2022-08-28 23:38:36 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-02-05 14:35:34 +00:00
|
|
|
A path containing the result of applying `overlays` to `kernelPackage`.
|
2019-04-24 17:24:16 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (cfg.enable) {
|
2020-02-05 14:35:34 +00: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 17:24:16 +00:00
|
|
|
hardware.deviceTree.package = if (cfg.overlays != [])
|
2022-08-19 16:57:13 +00:00
|
|
|
then pkgs.deviceTree.applyOverlays filteredDTBs (withDTBOs cfg.overlays)
|
|
|
|
else filteredDTBs;
|
2019-04-24 17:24:16 +00:00
|
|
|
};
|
|
|
|
}
|