summary refs log tree commit diff
path: root/modules/hardware/ads7846.nix
blob: 4d7582ada1f03a790d92c6229f15330157403c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{ ... }:

{
  # calibration for touchscreen
  services =
    let
      calibrationPoints = "3900 200 200 3900";
      calibrationMatrix = "-1 0 1 0 1 0";
    in
    {
      libinput.enable = true;

      # xorg configuration
      xserver.inputClassSections = [
        ''
          Identifier	"ads7846 calibration"
          MatchProduct	"ADS7846 Touchscreen"
          Option	"Calibration"	"${calibrationPoints}"
          Option	"SwapAxes"	"0"
          Driver  "libinput"
        ''
      ];

      # wayland configuration
      udev.extraRules = ''
        ATTRS{name}=="ADS7846 Touchscreen", ENV{LIBINPUT_CALIBRATION_MATRIX}="${calibrationMatrix}"
      '';
    };

  hardware.deviceTree =
    let
      cs = 1;
      penirq = 25;
      penirq_pull = 2;
      speed = 50000;
      pmin = 0;
      pmax = 255;
      xohms = 150;
      xmin = 200;
      xmax = 3900;
      ymin = 200;
      ymax = 3900;
    in
    {
      overlays = [
        {
          name = "ads7846-overlay";
          # taken from raspberry-pi kernel tree:
          # https://github.com/raspberrypi/linux/blob/rpi-6.6.y/arch/arm/boot/dts/overlays/ads7846-overlay.dts
          dtsText = ''
            /*
            * Generic Device Tree overlay for the ADS7846 touch controller
            *
            */

            /dts-v1/;
            /plugin/;

            / {
                    compatible = "brcm,bcm2711";

                    fragment@0 {
                            target = <&spi0>;
                            __overlay__ {
                                    status = "okay";
                            };
                    };

                    fragment@1 {
                            target = <&spidev0>;
                            __overlay__ {
                                    status = "disabled";
                            };
                    };

                    fragment@2 {
                            target = <&spidev1>;
                            __overlay__ {
                                    status = "disabled";
                            };
                    };

                    fragment@3 {
                            target = <&gpio>;
                            __overlay__ {
                                    ads7846_pins: ads7846_pins {
                                            brcm,pins = <${toString penirq}>; /* mel: previously illegal value of 255 */
                                            brcm,function = <0>; /* in */
                                            brcm,pull = <${toString penirq_pull}>; /* none */
                                    };
                            };
                    };

                    fragment@4 {
                            target = <&spi0>;
                            __overlay__ {
                                    /* needed to avoid dtc warning */
                                    #address-cells = <1>;
                                    #size-cells = <0>;

                                    ads7846: ads7846@1 {
                                            compatible = "ti,ads7846";
                                            reg = <${toString cs}>;
                                            pinctrl-names = "default";
                                            pinctrl-0 = <&ads7846_pins>;

                                            spi-max-frequency = <${toString speed}>;
                                            interrupts = <${toString penirq} 2>; /* high-to-low edge triggered */
                                            interrupt-parent = <&gpio>;
                                            pendown-gpio = <&gpio ${toString penirq} 1>;

                                            /* driver defaults */
                                            ti,x-min = /bits/ 16 <${toString xmin}>;
                                            ti,y-min = /bits/ 16 <${toString ymin}>;
                                            ti,x-max = /bits/ 16 <${toString xmax}>;
                                            ti,y-max = /bits/ 16 <${toString ymax}>;
                                            ti,pressure-min = /bits/ 16 <${toString pmin}>;
                                            ti,pressure-max = /bits/ 16 <${toString pmax}>;
                                            ti,x-plate-ohms = /bits/ 16 <${toString xohms}>;
                                    };
                            };
                    };
                    __overrides__ {
                            cs =     <&ads7846>,"reg:0";
                            speed =  <&ads7846>,"spi-max-frequency:0";
                            penirq = <&ads7846_pins>,"brcm,pins:0", /* REQUIRED */
                                    <&ads7846>,"interrupts:0",
                                    <&ads7846>,"pendown-gpio:4";
                            penirq_pull = <&ads7846_pins>,"brcm,pull:0";
                            swapxy = <&ads7846>,"ti,swap-xy?";
                            xmin =   <&ads7846>,"ti,x-min;0";
                            ymin =   <&ads7846>,"ti,y-min;0";
                            xmax =   <&ads7846>,"ti,x-max;0";
                            ymax =   <&ads7846>,"ti,y-max;0";
                            pmin =   <&ads7846>,"ti,pressure-min;0";
                            pmax =   <&ads7846>,"ti,pressure-max;0";
                            xohms =  <&ads7846>,"ti,x-plate-ohms;0";
                    };
            };
          '';
        }
      ];
    };
}