Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH RFC 0/2] Input: Maxim MAX11936 touchscreen (NVIDIA Tegra Note 7)
@ 2026-11-29 18:54 Patrick Fischer via B4 Relay
  2026-11-29 18:54 ` [PATCH RFC 1/2] dt-bindings: input: touchscreen: Add Maxim MAX11936 Patrick Fischer via B4 Relay
  2026-11-29 18:54 ` [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver Patrick Fischer via B4 Relay
  0 siblings, 2 replies; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-29 18:54 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: Henrik Rydberg, Svyatoslav Ryhel, linux-input, devicetree,
	linux-tegra, linux-kernel, Patrick Fischer

The NVIDIA Tegra Note 7 is a tablet with a Tegra 4 (T114); it came in two
production board revisions, P1640 and P1988. Its touchscreen has a Maxim
MAX11936 controller on SPI. Four names belong to this setup; below each
is used in one role only:

- SmartTouch: Maxim's name for the controller family;
- DirectTouch: NVIDIA's name for touch processing on the SoC;
- touch_fusion: the closed vendor daemon on Android that did that
  processing;
- maxim_sti: NVIDIA's Android kernel driver that passed the frames to
  touch_fusion and its touches on to the input layer.

The controller does not compute touches. It delivers raw frames, 224 per
second, which touch_fusion turned into touches; without the daemon there
is no touch on this tablet. This RFC finds the touches in the kernel.
There is no daemon, no netlink and no firmware file: the controller runs
from its own flash, which the driver never writes, and the two tables it
needs before scanning are generated from a few constants. The start and
idle sequences were taken from recordings of touch_fusion running on
mainline; nothing was decompiled. Patch 2 has the details and the
numbers.

Questions:

1. The main question: is evaluating raw frames in the kernel acceptable
   for an input driver, or should this be a userspace program feeding
   uinput (a daemon again, but an open one)?

   The evaluation is about 500 lines of integer code (max11936-algo.c).
   While the controller scans, the interrupt thread takes 14 to 15 % of
   one CPU (measured). Of that, the evaluation is about 3 % (0.14 ms per
   frame, as timed by the driver); the rest is reading the frames over
   SPI, which a userspace variant needs as well. After 10 s without a
   contact the controller goes into its idle mode and the driver sees
   nothing until the next touch. Over 12 minutes with a few touches the
   interrupt thread averaged 1 % of one CPU.

   Part of the evaluation are a few rules: it tells pen from finger
   (MT_TOOL_PEN) by the size and strength of a contact in its first
   frames, keeps a contact that vanished for 3 frames, and takes two
   contacts closer than 48 pixels as one. Are such rules fine in an input
   driver?

   The question came up in 2014 for DirectTouch in "Touch processing on
   host CPU" [1]. Dmitry, you advised thinking long and hard before doing
   the processing in userspace [2]: it either goes back into the kernel
   through uinput with added latency, or needs device-specific drivers
   again in X, Wayland, Chrome and Android. Alan Cox suggested that basic
   processing might be better in the kernel, providing it can be simple
   enough, and that the raw data would otherwise belong in IIO [3]. As far
   as I can see, no driver came out of that thread.

2. The panel and tuning data (lines, drive code, thresholds, mapping to
   the screen, the pen rule) are match data of the compatible, not DT
   properties. The matrix geometry (40 x 24) is fixed for this tablet: I
   know of no other production device with this controller (the Nexus 9
   shipped with a Synaptics controller; Maxim parts appear only on its
   early prototypes). Is match data the right place? Patch 1 has the
   questions on the binding itself.

3. Slots come from input_mt_assign_slots(). A new contact is not reported
   before its tool is decided (up to 6 frames), so the core sees its slot
   as free; a small helper (max11936_slots_match()) moves such contacts
   back to the slot they had in the previous frame. Is that acceptable
   next to input_mt_assign_slots(), or is there a better way to hold back
   a contact for a few frames?

4. There is no debugfs in this version. For tuning another panel a dump
   of the last raw and decoded frame helped a lot. Would that rather be a
   V4L2 touch device, as atmel_mxt_ts and rmi4 F54 do for their raw data,
   or should it stay out of tree?

Testing: on a Tegra Note 7 (P1640) with avdd at 3.0 V, the driver built as
a module for v7.4-rc3 with the board's device tree; the series is based on
input next. Patch 2 lists what was tested. Not tested: system suspend, pen
and finger on the screen at the same time, the power-off after a fault,
and a long run (the longest stretch of continuous scanning was 70 s).
Svyatoslav Ryhel tested the out-of-tree predecessor of this driver on his
P1988 in early November: touch and pen worked.

avdd needs to be 3.0 V: at 2.85 V the controller flagged about 11 % of
the frames as bad (status bit 0x04) and the driver dropped them; at
3.0 V it was 24 of about 60000, all during pen strokes. NVIDIA's board
file uses 3.0 to 3.3 V, and Svyatoslav's board series [4] sets it to
3.0 V.

The device tree node is not part of this series. Svyatoslav posted the
board support for the Tegra Note 7 as a work-in-progress series [4],
which does not have the touchscreen node yet.

[1] https://lore.kernel.org/all/5440F282.8040306@itdev.co.uk/
[2] https://lore.kernel.org/all/20141017171756.GA22238@dtor-ws/
[3] https://lore.kernel.org/all/20141021132229.37a1197c@alan.etchedpixels.co.uk/
[4] https://lore.kernel.org/all/20261122201407.23742-1-clamor95@gmail.com/

Tool use (see Documentation/process/generated-content.rst): Claude Code
(Anthropic) wrote the code and the analysis of the recorded traffic,
and an independent review by another Claude Code session went over the
driver before this posting; I reviewed it, ran the tests on the device
and take responsibility for it.

---
Patrick Fischer (2):
      dt-bindings: input: touchscreen: Add Maxim MAX11936
      Input: add Maxim MAX11936 touchscreen driver

 .../bindings/input/touchscreen/maxim,max11936.yaml |   73 ++
 drivers/input/touchscreen/Kconfig                  |   14 +
 drivers/input/touchscreen/Makefile                 |    2 +
 drivers/input/touchscreen/max11936-algo.c          |  508 +++++++++
 drivers/input/touchscreen/max11936-core.c          | 1155 ++++++++++++++++++++
 drivers/input/touchscreen/max11936.h               |  141 +++
 6 files changed, 1893 insertions(+)
---
base-commit: 0d4a5266e498ac7c25511cb1fa40d5f3c0a600bc
change-id: 20261120-upstream-touch-max11936-1f707a7f1511

Best regards,
--  
Patrick Fischer <admin@tsv-stahl-riesa.de>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH RFC 1/2] dt-bindings: input: touchscreen: Add Maxim MAX11936
  2026-11-29 18:54 [PATCH RFC 0/2] Input: Maxim MAX11936 touchscreen (NVIDIA Tegra Note 7) Patrick Fischer via B4 Relay
@ 2026-11-29 18:54 ` Patrick Fischer via B4 Relay
  2026-11-29 18:54 ` [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver Patrick Fischer via B4 Relay
  1 sibling, 0 replies; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-29 18:54 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: Henrik Rydberg, Svyatoslav Ryhel, linux-input, devicetree,
	linux-tegra, linux-kernel, Patrick Fischer

From: Patrick Fischer <admin@tsv-stahl-riesa.de>

The MAX11936 is a capacitive touchscreen controller on SPI from Maxim's
SmartTouch family, found in the NVIDIA Tegra Note 7. It scans the panel,
delivers raw frames instead of touch coordinates and holds its interrupt
line low while a frame is ready.

There is no public datasheet. The part number comes from the version
string of the vendor's Android daemon ("Maxim 11936"). The panel geometry
(40 drive x 24 sense lines) and the tuning of the evaluation are fixed for
the one panel known with this controller, and nothing in them is a choice
of the board. The driver therefore keeps them as match data of the
compatible instead of DT properties.

Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
Questions on the binding:
- Is "maxim,max11936" acceptable as compatible without a datasheet or a
  product page?
- The supplies are named avdd and dvdd after their use on the board; I
  have no pin names. Fine?
- If another panel with this controller shows up, would it get its own
  compatible (e.g. a board compatible in front of "maxim,max11936")?

Tool use (see Documentation/process/generated-content.rst): Claude Code
wrote this binding; I reviewed it.
---
 .../bindings/input/touchscreen/maxim,max11936.yaml | 73 ++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/touchscreen/maxim,max11936.yaml b/Documentation/devicetree/bindings/input/touchscreen/maxim,max11936.yaml
new file mode 100644
index 000000000000..2cb7088950d8
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/touchscreen/maxim,max11936.yaml
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/touchscreen/maxim,max11936.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Maxim MAX11936 touchscreen controller
+
+maintainers:
+  - Patrick Fischer <admin@tsv-stahl-riesa.de>
+
+description:
+  Capacitive touchscreen controller on SPI from Maxim's SmartTouch family,
+  as used in the NVIDIA Tegra Note 7. The controller scans the panel and
+  delivers raw frames; it holds its interrupt line low while a frame is
+  ready.
+
+properties:
+  compatible:
+    const: maxim,max11936
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+    description:
+      Level triggered; the controller holds the line active until the frame
+      has been read.
+
+  reset-gpios:
+    maxItems: 1
+
+  avdd-supply:
+    description: Analog supply
+
+  dvdd-supply:
+    description: Digital supply
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - reset-gpios
+  - avdd-supply
+  - dvdd-supply
+
+allOf:
+  - $ref: touchscreen.yaml#
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        touchscreen@0 {
+            compatible = "maxim,max11936";
+            reg = <0>;
+            spi-max-frequency = <12000000>;
+            interrupt-parent = <&gpio>;
+            interrupts = <82 IRQ_TYPE_LEVEL_LOW>;
+            reset-gpios = <&gpio 84 GPIO_ACTIVE_LOW>;
+            avdd-supply = <&avdd_ts>;
+            dvdd-supply = <&dvdd_lcd>;
+        };
+    };

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver
  2026-11-29 18:54 [PATCH RFC 0/2] Input: Maxim MAX11936 touchscreen (NVIDIA Tegra Note 7) Patrick Fischer via B4 Relay
  2026-11-29 18:54 ` [PATCH RFC 1/2] dt-bindings: input: touchscreen: Add Maxim MAX11936 Patrick Fischer via B4 Relay
@ 2026-11-29 18:54 ` Patrick Fischer via B4 Relay
  2026-11-29 19:07   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Patrick Fischer via B4 Relay @ 2026-11-29 18:54 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley
  Cc: Henrik Rydberg, Svyatoslav Ryhel, linux-input, devicetree,
	linux-tegra, linux-kernel, Patrick Fischer

From: Patrick Fischer <admin@tsv-stahl-riesa.de>

The MAX11936 in the NVIDIA Tegra Note 7 does not report touches. It
drives all 40 drive lines of the panel at once with a code and delivers,
224 times a second, one row of 24 sense values per code step plus a
reference row. On Android, NVIDIA's kernel driver maxim_sti passed these
frames over netlink to the vendor daemon touch_fusion, which found the
touches and passed them back. This driver finds them in the kernel and
needs neither the daemon nor a firmware file.

The controller runs from the firmware in its own flash; the driver never
writes the flash and has no bootloader access. Before it scans, the
controller needs two tables. The one at 0x2040 holds the drive code: its
40 steps are the 40 rotations of one 40-bit sequence (0xb4bb173888). The
one at 0x2300 lists the drive and sense lines and a few fixed values.
Both are generated at probe time and match byte by byte what the daemon
wrote.

The start, idle and wake sequences, the register values and the frame
layout were observed while touch_fusion 1.2.0 ("fw v33.2") ran on a
mainline kernel against a module that logged the netlink and SPI traffic.
Nothing was decompiled. The format on the wire (command header, mailbox,
pages of at most 250 words) follows maxim_sti in NVIDIA's Android kernel
for the tablet (NVIDIA's linux-2.6 tree on nv-tegra.nvidia.com, branch
rel-tegranote-r8, read from the copy at
https://github.com/grate-driver/linux-2.6;
drivers/input/touchscreen/maxim_sti.c, GPL-2.0, copyright Maxim and
NVIDIA); no code was taken from it. Register names are ours; where the
meaning of a value is not known, the comments say so.

Below, an element is the crossing of one drive and one sense line, and
its signal is its decoded value. Per frame (max11936-algo.c):

  1. baseline of the raw frame, frozen while touched;
  2. difference to the baseline, decoded with the inverse of the code
     matrix (one row of 40 values, the matrix is circulant);
  3. regions above a threshold; palms and hands gripping an edge are
     dropped;
  4. local maxima with hysteresis;
  5. centroid around each maximum, mapped to the screen.

Integer arithmetic only, 0.14 ms per frame on the Cortex-A15 of the
Tegra 4 (median, as timed by the driver). Slots come from
input_mt_assign_slots().

Rules on top of that:

- Pen: a contact that stays small (at most 4 elements) and weak (signal
  below 300) for its first 6 frames (27 ms) is a pen and is reported only
  then; any other contact is a finger at once. A pen tip is small and
  weak, a finger is not.
- Lift-off: a contact that vanishes stays for 3 frames (13 ms) at its
  last position. A lifting finger crosses the threshold back and forth,
  which would otherwise end and start the contact several times.
- One finger, two maxima: two contacts closer than 48 pixels are one,
  the weaker goes. A flat finger, or one cut off by the edge, can show
  two maxima whose centroids end up a few pixels apart; touch_fusion
  never reported two contacts closer than 82 pixels.
- Bad frames: frames with status bit 0x04 are dropped and the previous
  contacts kept, since such a frame can decode to a line of false
  contacts.
- Jitter: the positions carry an input fuzz of 8, so the input core
  holds a resting contact within 4 pixels.

Interrupt: the controller holds its line low while a frame is ready. A
level-triggered threaded interrupt reads one frame per run and is run
again by the core while the line stays low. After 10 s without a contact
the controller is put into its idle mode, in which it scans slowly
against a reference frame and raises the line only on a touch.

Power: the controller is powered and set up at probe (reset, id, tables;
about 250 ms, the probe is asynchronous) and stays powered while the
driver is bound. close() only stops scanning, open() starts it again
with the sequence the daemon used to leave the idle mode. System suspend
powers it off and resume runs the whole start again, as NVIDIA's driver
did. After 200 unreadable frames in a row, or when entering or leaving
the idle mode fails, the driver powers the controller off and sets it up
again on the next open; it does not restart by itself.

Against touch_fusion on the same recorded frames (two sessions, 7568
reported contact positions): median distance 5.8 and 9.2 pixels, 2.3 %
and 0.4 % of its contacts missed, 1.7 % and 3.1 % additional ones.

While the controller scans, the interrupt thread takes 14 to 15 % of one
CPU, mostly reading the frames over SPI (the daemon and its kernel
thread together took 25 %). In the idle mode there are no interrupts
until a touch.

Tested on a Tegra Note 7 (P1640) with avdd at 3.0 V, the driver built as
a module for v7.4-rc3; the details are below the cut. Not tested: system
suspend, pen and finger on the screen at the same time, the power-off
after a fault, and a long run (the longest stretch of continuous scanning
was 70 s, the longest time the driver stayed loaded about 12 minutes,
mostly idle).

Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
---
Tested on 29 November 2026 (P1640, avdd 3.0 V, v7.4-rc3 with the board's
device tree, 208 to 223 frames/s measured):
- tapping at nine points, swiping, up to five fingers, palm and grip:
  all contacts reported as finger;
- pen strokes and taps: all 59 pen contacts reported as pen;
- fast taps and double taps: no tap lost, the shortest pause between the
  two taps of a double tap was 48 ms;
- display off and on, close and open, unloading and loading the module
  twice;
- idle mode: 20 wake-ups by a tap, first contact 9 ms after the wake-up
  (median), 10 minutes without a spurious wake-up; over 12 minutes with
  a few touches the interrupt thread averaged 1 % of one CPU;
- keeping the controller powered while closed: the battery current was
  not higher than with the controller off, within the 15 mA I can
  resolve on this board;
- PROVE_LOCKING, DEBUG_ATOMIC_SLEEP, DEBUG_OBJECTS and SLUB_DEBUG: no
  report. That run was without the Tegra EMC and DRM drivers, so with the
  display off: on this kernel the EMC probe produces a lockdep report of
  its own, which disables lockdep, and DRM does not come up without EMC;
- touches come out in the orientation the tablet is held (origin top
  left with the camera on top), matching the panel with
  rotation = <180>; no touchscreen-inverted-* needed.

Numbers behind the rules, from three recorded sessions with touch_fusion
and from the device:
- pen: 188 finger and 20 pen contacts in the recordings, all told apart
  right; fingers were reported 0.9 ms later on average, 86 % without
  delay;
- lift-off: the short gaps on lift-off went down from 44 to 9 and from
  156 to 31 in two sessions;
- 48 pixels: additional contacts against touch_fusion 2.1 -> 1.7 % and
  3.2 -> 3.1 %, gaps on lift-off 9 -> 1 and 31 -> 5; in a session on the
  device, 16 frames had two contacts for one finger, against 125 when
  the same frames are evaluated without the rule;
- status bit 0x04: with avdd at 3.0 V in 24 of 59510 frames, all during
  pen strokes; with avdd at 2.85 V in 1848 of 16284 frames (11 %),
  mostly without a touch.

Comments welcome on two choices: keeping the controller powered while
the device is closed (NVIDIA powered it off whenever the input device
was disabled), and not restarting it by itself after a fault.

Tool use (see Documentation/process/generated-content.rst): Claude Code
wrote this driver and the analysis of the recorded traffic, and found
the structure of the drive code; I reviewed it and ran the tests on the
device.
---
 drivers/input/touchscreen/Kconfig         |   14 +
 drivers/input/touchscreen/Makefile        |    2 +
 drivers/input/touchscreen/max11936-algo.c |  508 +++++++++++++
 drivers/input/touchscreen/max11936-core.c | 1155 +++++++++++++++++++++++++++++
 drivers/input/touchscreen/max11936.h      |  141 ++++
 5 files changed, 1820 insertions(+)

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 9b9ae8ac3f7f..03b501137a0d 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -644,6 +644,20 @@ config TOUCHSCREEN_MAX11801
 	  To compile this driver as a module, choose M here: the
 	  module will be called max11801_ts.
 
+config TOUCHSCREEN_MAX11936
+	tristate "Maxim MAX11936 touchscreen controller"
+	depends on SPI
+	help
+	  Say Y here if you have a touchscreen with the Maxim MAX11936
+	  controller (Maxim SmartTouch family), as in the NVIDIA Tegra
+	  Note 7. The driver finds the touches in the raw frames of the
+	  controller itself; no daemon and no firmware file are needed.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called max11936.
+
 config TOUCHSCREEN_MMS114
 	tristate "MELFAS MMS114 touchscreen"
 	depends on I2C
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index bfd9de83389d..b44f8888a939 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -60,6 +60,8 @@ obj-$(CONFIG_TOUCHSCREEN_INEXIO)	+= inexio.o
 obj-$(CONFIG_TOUCHSCREEN_IPROC)		+= bcm_iproc_tsc.o
 obj-$(CONFIG_TOUCHSCREEN_LPC32XX)	+= lpc32xx_ts.o
 obj-$(CONFIG_TOUCHSCREEN_MAX11801)	+= max11801_ts.o
+obj-$(CONFIG_TOUCHSCREEN_MAX11936)	+= max11936.o
+max11936-y				:= max11936-core.o max11936-algo.o
 obj-$(CONFIG_TOUCHSCREEN_MXS_LRADC)     += mxs-lradc-ts.o
 obj-$(CONFIG_TOUCHSCREEN_MX25)		+= fsl-imx25-tcq.o
 obj-$(CONFIG_TOUCHSCREEN_MC13783)	+= mc13783_ts.o
diff --git a/drivers/input/touchscreen/max11936-algo.c b/drivers/input/touchscreen/max11936-algo.c
new file mode 100644
index 000000000000..77adb224d8e2
--- /dev/null
+++ b/drivers/input/touchscreen/max11936-algo.c
@@ -0,0 +1,508 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Maxim MAX11936 touchscreen: contacts from the raw frames.
+ *
+ * The controller does not report touches. It drives all n_tx drive lines at
+ * once with a code (step k drives line s with sign BASE[(s + k) % n_tx]) and
+ * delivers one row of n_rx sense values per step, plus one reference row.
+ * An element is the crossing of one drive and one sense line; its signal is
+ * its decoded value. Per frame:
+ *  1. baseline of the raw frame (IIR in Q4), frozen while touched, taken
+ *     anew after a run of frames with a negative signal
+ *  2. difference to the baseline, decoded with the inverse of the code
+ *     matrix: S[s][c] = sum_k E[(s + k) % n_tx] * d[k][c] >> 16
+ *  3. regions above t_off; palms (too large) and hands gripping an edge are
+ *     dropped
+ *  4. local maxima (3x3): a new contact needs t_on, one near a current
+ *     contact t_off
+ *  5. centroid in a window of +-2 drive x +-1 sense lines above a quarter of
+ *     the peak, mapped to the screen
+ * Integer arithmetic only; everything sized by struct max11936_panel.
+ */
+
+#include <linux/errno.h>
+#include <linux/minmax.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include "max11936.h"
+
+#define BASE_SHIFT	6	/* IIR: B += (raw - B) >> BASE_SHIFT */
+#define NEG_THR		(-40)	/* negative signal ... */
+#define NEG_FRAMES	16	/* ... for this many frames: new baseline */
+#define DIFF_CLAMP	8191	/* keeps the decoding sums within s32 */
+#define WIN_S		2
+#define WIN_C		1
+#define FLOOR_SHIFT	2	/* centroid weight: S - peak / 4 */
+#define NEAR_PX		64	/* t_off suffices this close to a contact */
+
+/*
+ * Tegra Note 7 (P1640, P1988): 40 drive x 24 sense lines, drive code
+ * 0xb4bb173888. Its 40 x 40 code matrix is left-circulant, so is the
+ * inverse: one row of 40 values in Q16.
+ */
+static const s32 tegratab_code_inv[40] = {
+	1078, -2791, 2001, 1039, -2884, 1187, -2394, -2372, 1596, -2432,
+	967, 1108, 1217, -1378, -26, 938, -2477, -2440, -2428, -905,
+	-1264, 954, 1588, 669, -2574, -924, 1041, 1123, 1081, -2210,
+	-1838, -3500, 127, -2443, -2973, -2258, -398, -2301, -2363, -2910,
+};
+
+const struct max11936_panel max11936_tegratab = {
+	.n_tx = 40,
+	.n_rx = 24,
+	.code = 0xb4bb173888ULL,
+	.code_inv = tegratab_code_inv,
+	.t_on = 60,
+	.t_off = 35,
+	.palm_area = 30,
+	.palm_max = 40,
+	.palm_one = 32,
+	.grip_row = 5,
+	.grip_col = 9,
+	/*
+	 * Fitted to the positions touch_fusion reported; contacts on the
+	 * outermost lines map to up to 4 pixels outside the screen.
+	 */
+	.x0 = 203520,		/* x = 795.0 - 34.70 c */
+	.kx = 8883,
+	.y0 = 325069,		/* y = 1269.8 - 32.66 s */
+	.ky = 8361,
+	.x_max = 799,
+	.y_max = 1279,
+	.fuzz = 8,
+	.release = 3,
+	.min_sep = 48,
+	.pen_area = 4,
+	.pen_v_max = 300,
+	.pen_frames = 6,
+};
+
+#define SIG(a, s, c)	((a)->sig[(s) * (a)->p->n_rx + (c)])
+#define LABEL(a, s, c)	((a)->label[(s) * (a)->p->n_rx + (c)])
+
+/* Return: 0 if the panel data fit the work areas and make sense */
+int max11936_panel_check(const struct max11936_panel *p)
+{
+	if (p->n_tx < 3 || p->n_tx > MAX11936_TX_MAX ||
+	    p->n_rx < 3 || p->n_rx > MAX11936_RX_MAX ||
+	    p->t_off <= 0 || p->t_on < p->t_off)
+		return -EINVAL;
+
+	return 0;
+}
+
+/* start afresh: no baseline, no contacts */
+void max11936_algo_reset(struct max11936_algo *a, const struct max11936_panel *p)
+{
+	a->p = p;
+	a->base_valid = false;
+	a->neg = 0;
+}
+
+static void max11936_set_base(struct max11936_algo *a, const s16 *raw)
+{
+	int n = (a->p->n_tx + 1) * a->p->n_rx;
+
+	for (int k = 0; k < n; k++)
+		a->base[k] = raw[k] * 16;
+	a->base_valid = true;
+}
+
+static void max11936_decode(struct max11936_algo *a, const s16 *raw)
+{
+	const struct max11936_panel *p = a->p;
+	int nt = p->n_tx, nr = p->n_rx;
+
+	if (!a->base_valid)
+		max11936_set_base(a, raw);
+
+	for (int k = 0; k < nt * nr; k++)
+		a->diff[k] = clamp(raw[k] - (a->base[k] >> 4),
+				   -DIFF_CLAMP, DIFF_CLAMP);
+
+	/*
+	 * Row by row: every partial sum stays below n_tx * max|code_inv| *
+	 * DIFF_CLAMP, for the Tegra Note 7 40 * 3500 * 8191 < 2^31.
+	 */
+	for (int s = 0; s < nt; s++) {
+		s32 acc[MAX11936_RX_MAX] = { };
+
+		for (int k = 0; k < nt; k++) {
+			s32 e = p->code_inv[s + k < nt ? s + k : s + k - nt];
+			const s32 *d = a->diff + k * nr;
+
+			for (int c = 0; c < nr; c++)
+				acc[c] += e * d[c];
+		}
+		for (int c = 0; c < nr; c++)
+			SIG(a, s, c) = (acc[c] + 32768) >> 16;
+	}
+}
+
+static void max11936_update_base(struct max11936_algo *a, const s16 *raw,
+				 bool touched)
+{
+	int nt = a->p->n_tx, nr = a->p->n_rx;
+	bool neg = false;
+
+	if (touched) {
+		a->neg = 0;
+		return;
+	}
+
+	for (int k = 0; k < nt * nr; k++)
+		if (a->sig[k] < NEG_THR)
+			neg = true;
+	a->neg = neg ? a->neg + 1 : 0;
+	if (a->neg >= NEG_FRAMES) {
+		max11936_set_base(a, raw);
+		a->neg = 0;
+		return;
+	}
+
+	for (int k = 0; k < (nt + 1) * nr; k++)
+		a->base[k] += (raw[k] * 16 - a->base[k]) >> BASE_SHIFT;
+}
+
+/* regions above t_off (4-neighbourhood); returns their number */
+static int max11936_regions(struct max11936_algo *a)
+{
+	static const s8 ds[4] = { 1, -1, 0, 0 }, dc[4] = { 0, 0, 1, -1 };
+	int nt = a->p->n_tx, nr = a->p->n_rx, t_off = a->p->t_off;
+	int n = 0;
+
+	memset(a->label, 0, nt * nr * sizeof(*a->label));
+
+	for (int s = 0; s < nt; s++) {
+		for (int c = 0; c < nr; c++) {
+			int sp = 0, area = 0;
+
+			if (SIG(a, s, c) < t_off || LABEL(a, s, c))
+				continue;
+
+			LABEL(a, s, c) = ++n;
+			a->stack[sp++] = s * nr + c;
+			while (sp) {
+				int e = a->stack[--sp], u = e / nr, w = e % nr;
+
+				area++;
+				for (int i = 0; i < 4; i++) {
+					int uu = u + ds[i], ww = w + dc[i];
+
+					if (uu < 0 || uu >= nt || ww < 0 || ww >= nr ||
+					    LABEL(a, uu, ww) || SIG(a, uu, ww) < t_off)
+						continue;
+					LABEL(a, uu, ww) = n;
+					a->stack[sp++] = uu * nr + ww;
+				}
+			}
+			a->area[n] = area;
+		}
+	}
+
+	return n;
+}
+
+/* region lies along an edge like a gripping hand */
+static bool max11936_grip(struct max11936_algo *a, int g)
+{
+	int nt = a->p->n_tx, nr = a->p->n_rx;
+	int row0 = 0, row1 = 0, col0 = 0, col1 = 0;
+
+	for (int i = 0; i < nr; i++) {
+		row0 += LABEL(a, 0, i) == g;
+		row1 += LABEL(a, nt - 1, i) == g;
+	}
+	for (int i = 0; i < nt; i++) {
+		col0 += LABEL(a, i, 0) == g;
+		col1 += LABEL(a, i, nr - 1) == g;
+	}
+
+	return max(row0, row1) >= a->p->grip_row ||
+	       max(col0, col1) >= a->p->grip_col;
+}
+
+static bool max11936_is_peak(struct max11936_algo *a, int s, int c)
+{
+	int nt = a->p->n_tx, nr = a->p->n_rx;
+	s32 v = SIG(a, s, c);
+
+	for (int ds = -1; ds <= 1; ds++) {
+		for (int dc = -1; dc <= 1; dc++) {
+			int u = s + ds, w = c + dc;
+			s32 nb;
+
+			if ((!ds && !dc) || u < 0 || u >= nt || w < 0 || w >= nr)
+				continue;
+			nb = SIG(a, u, w);
+			/* on a tie the earlier element wins */
+			if (nb > v || (nb == v && (ds < 0 || (!ds && dc < 0))))
+				return false;
+		}
+	}
+
+	return true;
+}
+
+/* centroid around a peak, mapped to the screen */
+static void max11936_centroid(struct max11936_algo *a, int s, int c,
+			      struct max11936_peak *pk)
+{
+	const struct max11936_panel *p = a->p;
+	s32 floor = SIG(a, s, c) >> FLOOR_SHIFT;
+	s32 sw = 0, ss = 0, sc = 0, sq, cq;
+
+	for (int ds = -WIN_S; ds <= WIN_S; ds++) {
+		for (int dc = -WIN_C; dc <= WIN_C; dc++) {
+			int u = s + ds, w = c + dc;
+			s32 q;
+
+			if (u < 0 || u >= p->n_tx || w < 0 || w >= p->n_rx ||
+			    SIG(a, u, w) <= floor)
+				continue;
+			q = SIG(a, u, w) - floor;
+			sw += q;
+			ss += q * u;
+			sc += q * w;
+		}
+	}
+
+	/*
+	 * The peak itself is above floor (t_off > 0), so sw > 0. |S| stays
+	 * below n_tx * max|code_inv| * DIFF_CLAMP >> 16, for the Tegra Note 7
+	 * 8523, which keeps ss * 256 and sc * 256 below 1.3 * 10^9.
+	 */
+	sq = (ss * 256 + sw / 2) / sw;
+	cq = (sc * 256 + sw / 2) / sw;
+	pk->x = (p->x0 - ((p->kx * cq) >> 8)) >> 8;
+	pk->y = (p->y0 - ((p->ky * sq) >> 8)) >> 8;
+}
+
+static bool max11936_near(const struct max11936_peak *pk,
+			  const struct max11936_slot *slots, int nslots)
+{
+	for (int i = 0; i < nslots; i++) {
+		s32 dx = pk->x - slots[i].x, dy = pk->y - slots[i].y;
+
+		if (slots[i].active && dx * dx + dy * dy <= NEAR_PX * NEAR_PX)
+			return true;
+	}
+
+	return false;
+}
+
+/*
+ * A finger lying flat, or cut off by the edge of the panel, can show two
+ * maxima two lines apart. Their centroid windows overlap, so the two
+ * centroids end up a few pixels apart; keep the stronger one. @c is sorted
+ * strongest first.
+ */
+static int max11936_merge(const struct max11936_panel *p,
+			  struct max11936_contact *c, int n)
+{
+	s32 d2 = p->min_sep * p->min_sep;
+	int k = 0;
+
+	for (int i = 0; i < n; i++) {
+		int j;
+
+		for (j = 0; j < k; j++) {
+			s32 dx = c[i].x - c[j].x, dy = c[i].y - c[j].y;
+
+			if (dx * dx + dy * dy < d2)
+				break;
+		}
+		if (j == k)
+			c[k++] = c[i];
+	}
+
+	return k;
+}
+
+/**
+ * max11936_algo_frame() - contacts of one frame
+ * @a: state
+ * @raw: (n_tx + 1) x n_rx words as read from the chip
+ * @slots: current contacts, for the hysteresis
+ * @nslots: number of @slots
+ * @out: up to MAX11936_MAX_CONTACTS contacts, strongest first
+ *
+ * Return: number of contacts in @out
+ */
+int max11936_algo_frame(struct max11936_algo *a, const s16 *raw,
+			const struct max11936_slot *slots, int nslots,
+			struct max11936_contact *out)
+{
+	const struct max11936_panel *p = a->p;
+	int nt = p->n_tx, nr = p->n_rx;
+	int npk = 0, n = 0, regions;
+	bool touched = false;
+
+	max11936_decode(a, raw);
+	regions = max11936_regions(a);
+	memset(a->npeaks, 0, (regions + 1) * sizeof(*a->npeaks));
+
+	for (int s = 0; s < nt; s++) {
+		for (int c = 0; c < nr; c++) {
+			struct max11936_peak *pk = &a->peaks[npk];
+
+			if (SIG(a, s, c) < p->t_off)
+				continue;
+			touched = true;
+			if (npk >= MAX11936_MAX_PEAKS || !max11936_is_peak(a, s, c))
+				continue;
+
+			pk->v = SIG(a, s, c);
+			pk->region = LABEL(a, s, c);
+			pk->edge = !s || s == nt - 1 || !c || c == nr - 1;
+			max11936_centroid(a, s, c, pk);
+			a->npeaks[pk->region]++;
+			npk++;
+		}
+	}
+
+	for (int i = 0; i < npk; i++) {
+		const struct max11936_peak *pk = &a->peaks[i];
+		s32 area = a->area[pk->region], peaks = a->npeaks[pk->region];
+		int j;
+
+		/* palm, grip */
+		if (area > p->palm_max || area > p->palm_area * peaks ||
+		    (peaks == 1 && area > p->palm_one) ||
+		    max11936_grip(a, pk->region))
+			continue;
+		/* hysteresis */
+		if (pk->v < p->t_on && !max11936_near(pk, slots, nslots))
+			continue;
+
+		/* strongest first, stable for equal peaks; the weakest drop out */
+		if (n == MAX11936_MAX_CONTACTS && out[n - 1].v >= pk->v)
+			continue;
+		for (j = min(n, MAX11936_MAX_CONTACTS - 1); j > 0 && out[j - 1].v < pk->v; j--)
+			out[j] = out[j - 1];
+		out[j].x = pk->x;
+		out[j].y = pk->y;
+		out[j].v = pk->v;
+		/*
+		 * A pen tip covers few elements and stays weak. So does a finger
+		 * cut off by the edge of the panel.
+		 */
+		out[j].pen = area <= p->pen_area && pk->v < p->pen_v_max &&
+			     !pk->edge;
+		n = min(n + 1, MAX11936_MAX_CONTACTS);
+	}
+
+	n = max11936_merge(p, out, n);
+	max11936_update_base(a, raw, touched);
+	return n;
+}
+
+/**
+ * max11936_slots_match() - keep undecided contacts in their slots
+ * @slots: slot state
+ * @nslots: number of @slots
+ * @c: contacts of the frame
+ * @slot: slot of each contact from input_mt_assign_slots(), updated
+ * @n: number of contacts
+ * @dmax: largest move per frame
+ *
+ * A contact whose tool is not decided yet has not been reported, so
+ * input_mt_assign_slots() treats its slot as free and may match the contact
+ * to another free slot. Move such contacts back to the nearest undecided slot.
+ */
+void max11936_slots_match(const struct max11936_slot *slots, int nslots,
+			  const struct max11936_contact *c, int *slot, int n,
+			  s32 dmax)
+{
+	bool taken[MAX11936_MAX_CONTACTS] = { };
+
+	for (int i = 0; i < n; i++)
+		if (slot[i] >= 0 && slot[i] < nslots && slots[slot[i]].reported)
+			taken[slot[i]] = true;
+
+	for (int i = 0; i < n; i++) {
+		s32 best = dmax * dmax + 1;
+		int u = -1;
+
+		if (slot[i] < 0 || slot[i] >= nslots || slots[slot[i]].reported)
+			continue;
+		for (int k = 0; k < nslots; k++) {
+			s32 dx = c[i].x - slots[k].x, dy = c[i].y - slots[k].y;
+
+			if (!slots[k].active || slots[k].reported || taken[k] ||
+			    dx * dx + dy * dy >= best)
+				continue;
+			best = dx * dx + dy * dy;
+			u = k;
+		}
+		if (u < 0)
+			continue;
+		/* swap with a contact that got slot u */
+		for (int j = 0; j < n; j++)
+			if (slot[j] == u)
+				slot[j] = slot[i];
+		slot[i] = u;
+		taken[u] = true;
+	}
+}
+
+/**
+ * max11936_slots_update() - apply the contacts of a frame to the slots
+ * @p: panel data
+ * @slots: slot state
+ * @nslots: number of @slots
+ * @c: contacts of the frame
+ * @slot: slot of each contact (input_mt_assign_slots(), max11936_slots_match())
+ * @n: number of contacts
+ *
+ * A contact is a finger as soon as one frame shows a finger; one that looks
+ * like a pen in its first @p->pen_frames frames is a pen. Until then it is
+ * not reported, and it is dropped if it vanishes before: pen taps shorter
+ * than that are lost (the shortest one recorded lasted 11 frames). The tool
+ * stays for the life of the contact. A reported contact that vanishes stays
+ * for @p->release frames at its last position, which bridges the flicker of
+ * a finger whose signal crosses t_off while it is lifted.
+ */
+void max11936_slots_update(const struct max11936_panel *p,
+			   struct max11936_slot *slots, int nslots,
+			   const struct max11936_contact *c, const int *slot,
+			   int n)
+{
+	bool seen[MAX11936_MAX_CONTACTS] = { };
+
+	for (int i = 0; i < n; i++) {
+		struct max11936_slot *s;
+
+		if (slot[i] < 0 || slot[i] >= nslots || seen[slot[i]])
+			continue;
+		s = &slots[slot[i]];
+		seen[slot[i]] = true;
+		s->missed = 0;
+		s->x = c[i].x;
+		s->y = c[i].y;
+
+		if (!s->active) {
+			s->active = true;
+			s->reported = false;
+			s->frames = 0;
+		}
+		if (!s->reported) {
+			s->frames++;
+			if (!c[i].pen || s->frames >= p->pen_frames) {
+				s->pen = c[i].pen;
+				s->reported = true;
+			}
+		}
+	}
+
+	for (int i = 0; i < nslots; i++) {
+		struct max11936_slot *s = &slots[i];
+
+		if (!s->active || seen[i])
+			continue;
+		/* gone before the tool was decided: a blip or a lift-off tail */
+		if (!s->reported || ++s->missed > p->release)
+			*s = (struct max11936_slot){ };
+	}
+}
diff --git a/drivers/input/touchscreen/max11936-core.c b/drivers/input/touchscreen/max11936-core.c
new file mode 100644
index 000000000000..e1532df781a3
--- /dev/null
+++ b/drivers/input/touchscreen/max11936-core.c
@@ -0,0 +1,1155 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Maxim MAX11936 touchscreen controller (Maxim SmartTouch family), as used
+ * in the NVIDIA Tegra Note 7; NVIDIA calls the touch processing on the SoC
+ * "DirectTouch".
+ *
+ * The controller scans the panel and hands out raw frames; it does not find
+ * touches itself. On Android a daemon (touch_fusion) evaluated the frames and
+ * passed the touches back to the kernel over netlink. This driver does it in
+ * the kernel (max11936-algo.c) and needs neither the daemon nor a firmware
+ * file: the controller runs from the firmware in its own flash, and the two
+ * tables it needs before scanning are generated here from a few constants.
+ *
+ * The start, idle and wake sequences and the register values below were
+ * observed while the vendor daemon (touch_fusion 1.2.0, "fw v33.2") ran
+ * against a capturing kernel module on a Tegra Note 7. The format on the
+ * wire (command header, mailbox, pages) follows NVIDIA's maxim_sti driver
+ * in the Android kernel for the tablet (branch rel-tegranote-r8); no code
+ * was taken from it. Register names are ours, the meaning of some values
+ * is not known.
+ *
+ * Frames are read through the "legacy acceleration" mailbox at 0x0000, in
+ * pages of up to 250 words. The controller holds its interrupt line low
+ * while a frame is ready; a level-triggered threaded interrupt reads one
+ * frame per run and is run again by the core while the line stays low.
+ * After a while without a contact the controller is put into its idle mode:
+ * it scans slowly, compares against a reference frame and raises the line
+ * only on a touch.
+ *
+ * The controller is powered and set up at probe and stays powered while the
+ * driver is bound; open() and close() only start and stop scanning. System
+ * suspend powers it off, resume sets it up again, as NVIDIA's driver did.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitops.h>
+#include <linux/cache.h>
+#include <linux/cleanup.h>
+#include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/input/touchscreen.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/ktime.h>
+#include <linux/math.h>
+#include <linux/minmax.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+
+#include <asm/byteorder.h>
+
+#include "max11936.h"
+
+#define MAX11936_NAME		"max11936"
+
+/* registers */
+#define REG_MAILBOX		0x0000	/* method 2 requests, legacy frame pages */
+#define REG_LEGACY		0x0003	/* 0xbeef on, 0xdead off, answers 0xabcd */
+#define REG_DATA		0x0004	/* method 2 data */
+#define REG_STATUS		0x1381
+#define REG_MEAS_A		0x1387
+#define REG_MEAS_B		0x1388
+#define REG_ENABLE		0x1389
+#define REG_SCAN		0x138a
+#define REG_PERIOD		0x138b
+#define REG_MODE		0x138c
+#define REG_WAKE_A		0x138d
+#define REG_WAKE_B		0x138e
+#define REG_WAKE_LIM		0x138f	/* four words */
+#define REG_ID0			0x1393
+#define REG_ID1			0x1394
+#define REG_LEGACY_MODE		0x1397
+#define REG_DRIVE_CODE		0x2040
+#define REG_LINES		0x2300
+
+#define MB_REQUEST		0xfedc
+#define MB_DONE			0xabcd
+#define LEGACY_ON		0xbeef
+#define LEGACY_OFF		0xdead
+#define PAGE_DATA		0x8070
+#define PAGE_NOT_READY		0x6060
+#define PAGE_NOT_LEGACY		0xbabe
+
+#define STATUS_BAD_FRAME	0x0004	/* frame disturbed, see max11936_service() */
+
+#define MAX11936_BUF_SIZE	1024
+#define MAX11936_PAGE_WORDS	250
+/*
+ * A mailbox poll is one 4-byte transfer, some 10-20 us at 12 MHz; NVIDIA
+ * gave up after 200 of them.
+ */
+#define MAX11936_POLL_US	10000
+#define MAX11936_MAX_FAILS	200	/* unreadable frames in a row */
+#define MAX11936_SPURIOUS	50	/* idle interrupts without a wake-up */
+#define MAX11936_DMAX		160	/* largest move per frame, pixels */
+
+/*
+ * The controller needs a pause between two legacy pages: without it, it
+ * answers "not ready" in most frames; 50 us were enough in all tests,
+ * NVIDIA waited 310-335 us.
+ */
+#define MAX11936_PAGE_DELAY_US	100
+#define MAX11936_RESET_DELAY_MS	200
+#define MAX11936_STOP_DELAY_US	15000
+#define MAX11936_IDLE_MS	10000
+
+#define MAX11936_IRQ_ON		0	/* bit in max11936.flags */
+
+struct max11936_reg {
+	u16 reg;
+	u16 value;
+};
+
+/**
+ * struct max11936_data - per device data
+ * @panel: panel, tuning and mapping
+ * @rx_a: per sense line value in the line table
+ * @rx_b: second per sense line value
+ * @tail: last bytes of the line table
+ * @tail_len: length of @tail
+ * @buf0: address of the first frame buffer
+ * @buf1: address of the second frame buffer
+ * @buf_ref: address of the reference frame of the idle mode
+ * @wake_mask: status bits of a wake-up in idle mode
+ * @pre: registers before the tables
+ * @n_pre: number of @pre
+ * @post: registers after the tables
+ * @n_post: number of @post
+ * @idle_pre: registers to enter the idle mode, before the reference frame
+ * @n_idle_pre: number of @idle_pre
+ * @idle_post: registers to enter the idle mode, after the reference frame
+ * @n_idle_post: number of @idle_post
+ * @active: registers to leave the idle mode
+ * @n_active: number of @active
+ */
+struct max11936_data {
+	const struct max11936_panel *panel;
+	u8 rx_a, rx_b;
+	const u8 *tail;
+	unsigned int tail_len;
+	u16 buf0, buf1, buf_ref;
+	u16 wake_mask;
+	const struct max11936_reg *pre, *post, *idle_pre, *idle_post, *active;
+	unsigned int n_pre, n_post, n_idle_pre, n_idle_post, n_active;
+};
+
+/*
+ * Tegra Note 7. REG_PERIOD 0x03c1 gives 224 frames/s; without REG_MEAS_A/B
+ * the raw frame changes, without REG_LEGACY_MODE no frames come.
+ */
+static const struct max11936_reg tegratab_pre[] = {
+	{ REG_MEAS_A, 0x0b01 },
+	{ REG_MEAS_B, 0x0b69 },
+	{ REG_PERIOD, 0x03c1 },
+};
+
+static const struct max11936_reg tegratab_post[] = {
+	{ REG_MODE, 0x0040 },
+	{ REG_ENABLE, 0x0001 },
+	{ REG_LEGACY_MODE, 0x0001 },
+};
+
+/* wake limits, wake mode, period 0x208d (about 26 Hz), scanning on */
+static const struct max11936_reg tegratab_idle_pre[] = {
+	{ REG_WAKE_LIM, 0x005a },
+	{ REG_WAKE_LIM + 1, 0xffa6 },
+	{ REG_WAKE_LIM + 2, 0x7fff },
+	{ REG_WAKE_LIM + 3, 0x8000 },
+};
+
+static const struct max11936_reg tegratab_idle_post[] = {
+	{ REG_MODE, 0x0020 },
+	{ REG_WAKE_A, 0x00c5 },
+	{ REG_WAKE_B, 0x000a },
+	{ REG_PERIOD, 0x208d },
+	{ REG_SCAN, 0x0081 },
+};
+
+static const struct max11936_reg tegratab_active[] = {
+	{ REG_SCAN, 0x0000 },
+	{ REG_STATUS, 0xffff },
+	{ REG_MODE, 0x0040 },
+	{ REG_WAKE_A, 0x0000 },
+	{ REG_PERIOD, 0x03c1 },
+	{ REG_LEGACY_MODE, 0x0001 },
+};
+
+static const u8 tegratab_tail[] = {
+	0x00, 0xff, 0xff, 0xfe, 0xfe, 0xdc, 0xdc, 0x00, 0x55,
+	0x00, 0x01, 0x01, 0x1f, 0x00, 0x00, 0x20, 0x20,
+};
+
+static const struct max11936_data max11936_tegratab_data = {
+	.panel = &max11936_tegratab,
+	.rx_a = 15,
+	.rx_b = 4,
+	.tail = tegratab_tail,
+	.tail_len = sizeof(tegratab_tail),
+	.buf0 = 0x3000,
+	.buf1 = 0x33d8,
+	.buf_ref = 0x37b0,
+	.wake_mask = 0x01c0,
+	.pre = tegratab_pre,
+	.n_pre = ARRAY_SIZE(tegratab_pre),
+	.post = tegratab_post,
+	.n_post = ARRAY_SIZE(tegratab_post),
+	.idle_pre = tegratab_idle_pre,
+	.n_idle_pre = ARRAY_SIZE(tegratab_idle_pre),
+	.idle_post = tegratab_idle_post,
+	.n_idle_post = ARRAY_SIZE(tegratab_idle_post),
+	.active = tegratab_active,
+	.n_active = ARRAY_SIZE(tegratab_active),
+};
+
+struct max11936 {
+	struct spi_device *spi;
+	const struct max11936_data *data;
+	struct input_dev *input;
+	struct touchscreen_properties prop;
+	struct gpio_desc *reset;
+	struct regulator *avdd;
+	struct regulator *dvdd;
+
+	/*
+	 * Protects the controller (SPI, power, mode) and the evaluation state;
+	 * taken by the interrupt thread. Start and stop run under the input
+	 * device mutex. Stop disables the interrupt, waiting for a running
+	 * thread, before it takes the lock. MAX11936_IRQ_ON in @flags is set
+	 * while the interrupt is enabled; whoever clears it (stop, or the
+	 * thread giving up on a fault) disables the interrupt, so each enable
+	 * is matched by exactly one disable.
+	 */
+	struct mutex lock;
+	unsigned long flags;
+	bool running;
+	bool powered;
+	bool legacy;
+	bool idle;
+	unsigned int fails;
+	unsigned int spurious;
+	ktime_t last_touch;
+
+	u8 *drive_code;
+	u8 *lines;
+	unsigned int drive_code_len, lines_len;
+	unsigned int raw_words, frame_words;
+	u16 *frame;		/* raw frame, status copy, one more word */
+	__le16 *ref;		/* reference frame for the idle mode */
+	struct max11936_algo *algo;
+	struct max11936_slot slots[MAX11936_MAX_CONTACTS];
+	u8 req[8 + 2 * MAX11936_PAGE_WORDS];
+	u16 page[4 + MAX11936_PAGE_WORDS];	/* legacy page, method 2 data */
+
+	/* DMA safe, not shared with the fields above */
+	u8 tx[MAX11936_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
+	u8 rx[MAX11936_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
+};
+
+/*
+ * Drive code table: 256 bytes per 8 steps. In each block, drive line s has
+ * two bytes, two bits per step from the least significant end (01: +,
+ * 10: -). Step k drives line s with symbol (s + k) % n_tx of the code.
+ */
+static void max11936_gen_drive_code(const struct max11936_data *d, u8 *t)
+{
+	int n = d->panel->n_tx;
+
+	for (int k = 0; k < n; k++) {
+		for (int s = 0; s < n; s++) {
+			int i = (s + k) % n;
+			bool plus = (d->panel->code >> (n - 1 - i)) & 1;
+
+			t[256 * (k / 8) + 2 * s + (k % 8) / 4] |=
+				(plus ? 1 : 2) << (2 * (k % 4));
+		}
+	}
+}
+
+/*
+ * Line table: numbers of drive and sense lines, drive lines in order, sense
+ * lines reversed, two words per sense line, then fixed bytes.
+ */
+static void max11936_gen_lines(const struct max11936_data *d, u8 *t)
+{
+	int nt = d->panel->n_tx, nr = d->panel->n_rx, p = 0;
+
+	t[p++] = nt;
+	t[p++] = nr;
+	for (int i = 0; i < nt; i++)
+		t[p++] = i;
+	for (int i = 0; i < nr; i++)
+		t[p++] = nr - 1 - i;
+	for (int i = 0; i < nr; i++) {
+		t[p++] = d->rx_a;
+		t[p++] = 0;
+	}
+	for (int i = 0; i < nr; i++) {
+		t[p++] = d->rx_b;
+		t[p++] = 0;
+	}
+	memcpy(t + p, d->tail, d->tail_len);
+}
+
+/*
+ * One SPI transfer: address << 1 (bit 0 set for a read), then 16-bit words,
+ * all big endian. max11936_read() returns the words in CPU order. Data for
+ * max11936_write() are byte streams of little endian words (as the tables
+ * are laid out) and go out swapped word by word; an odd last byte goes out
+ * as it is.
+ */
+static int max11936_read(struct max11936 *ts, u16 addr, u16 *buf,
+			 unsigned int words)
+{
+	struct spi_transfer t = {
+		.tx_buf = ts->tx,
+		.rx_buf = ts->rx,
+		.len = 2 + 2 * words,
+	};
+	int error;
+
+	if (t.len > MAX11936_BUF_SIZE)
+		return -EINVAL;
+
+	memset(ts->tx, 0xff, t.len);
+	put_unaligned_be16(addr << 1 | 1, ts->tx);
+	error = spi_sync_transfer(ts->spi, &t, 1);
+	if (error)
+		return error;
+
+	for (unsigned int i = 0; i < words; i++)
+		buf[i] = get_unaligned_be16(ts->rx + 2 + 2 * i);
+
+	return 0;
+}
+
+static int max11936_write(struct max11936 *ts, u16 addr, const u8 *bytes,
+			  unsigned int len)
+{
+	unsigned int i;
+
+	if (2 + len > MAX11936_BUF_SIZE)
+		return -EINVAL;
+
+	put_unaligned_be16(addr << 1, ts->tx);
+	for (i = 0; i + 1 < len; i += 2)
+		put_unaligned_be16(get_unaligned_le16(bytes + i), ts->tx + 2 + i);
+	if (len & 1)
+		ts->tx[2 + i] = bytes[i];
+
+	return spi_write(ts->spi, ts->tx, 2 + len);
+}
+
+static int max11936_write_word(struct max11936 *ts, u16 addr, u16 value)
+{
+	__le16 v = cpu_to_le16(value);
+
+	return max11936_write(ts, addr, (const u8 *)&v, sizeof(v));
+}
+
+/* one word, or a negative error */
+static int max11936_read_word(struct max11936 *ts, u16 addr)
+{
+	u16 value;
+	int error;
+
+	error = max11936_read(ts, addr, &value, 1);
+	if (error)
+		return error;
+
+	return value;
+}
+
+/* reads the word at @addr until it is MB_DONE, without a pause */
+static int max11936_poll(struct max11936 *ts, u16 addr)
+{
+	int error, status;
+
+	error = read_poll_timeout(max11936_read_word, status,
+				  status < 0 || status == MB_DONE,
+				  0, MAX11936_POLL_US, false, ts, addr);
+	if (error)
+		return error;
+
+	return status < 0 ? status : 0;
+}
+
+static int max11936_legacy(struct max11936 *ts, bool on)
+{
+	int error;
+
+	error = max11936_write_word(ts, REG_LEGACY, on ? LEGACY_ON : LEGACY_OFF);
+	usleep_range(100, 120);
+	if (error || on)
+		return error;
+
+	return max11936_poll(ts, REG_LEGACY);
+}
+
+/*
+ * Method 2: request in the mailbox, wait for it, data at REG_DATA. @bytes
+ * are little endian words both ways.
+ */
+static int max11936_page(struct max11936 *ts, u16 addr, u8 *bytes,
+			 unsigned int len, bool write)
+{
+	unsigned int words = len / 2;
+	u8 *req = ts->req;
+	int error;
+
+	put_unaligned_le16(MB_REQUEST, req);
+	put_unaligned_le16(write ? addr << 1 : addr << 1 | 1, req + 2);
+	put_unaligned_le16(words, req + 4);
+
+	if (write) {
+		put_unaligned_le16(0, req + 6);
+		memcpy(req + 8, bytes, len);
+		error = max11936_write(ts, REG_MAILBOX, req, 8 + len);
+		if (error)
+			return error;
+
+		return max11936_poll(ts, REG_MAILBOX);
+	}
+
+	error = max11936_write(ts, REG_MAILBOX, req, 6);
+	if (error)
+		return error;
+
+	error = max11936_poll(ts, REG_MAILBOX);
+	if (error)
+		return error;
+
+	error = max11936_read(ts, REG_DATA, ts->page, words);
+	if (error)
+		return error;
+
+	for (unsigned int i = 0; i < words; i++)
+		put_unaligned_le16(ts->page[i], bytes + 2 * i);
+
+	return 0;
+}
+
+/*
+ * While legacy frames are on, every request is wrapped in off and on. As
+ * in NVIDIA's driver, a failed off is not an error of its own: the request
+ * then fails, or the controller has answered it anyway.
+ */
+static int max11936_access(struct max11936 *ts, u16 addr, u8 *bytes,
+			   unsigned int len, bool write)
+{
+	int error, legacy_error;
+
+	for (unsigned int off = 0; off < len; ) {
+		unsigned int n = min(len - off, 2 * MAX11936_PAGE_WORDS);
+
+		if (ts->legacy)
+			max11936_legacy(ts, false);
+
+		error = max11936_page(ts, addr + off / 2, bytes + off, n, write);
+		if (ts->legacy) {
+			legacy_error = max11936_legacy(ts, true);
+			if (!error)
+				error = legacy_error;
+		}
+		if (error)
+			return error;
+
+		off += n;
+	}
+
+	return 0;
+}
+
+static int max11936_write_reg(struct max11936 *ts, u16 reg, u16 value)
+{
+	__le16 v = cpu_to_le16(value);
+
+	return max11936_access(ts, reg, (u8 *)&v, sizeof(v), true);
+}
+
+static int max11936_write_regs(struct max11936 *ts,
+			       const struct max11936_reg *r, unsigned int n)
+{
+	int error;
+
+	for (unsigned int i = 0; i < n; i++) {
+		error = max11936_write_reg(ts, r[i].reg, r[i].value);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static int max11936_read_reg(struct max11936 *ts, u16 reg, u16 *value)
+{
+	__le16 v;
+	int error;
+
+	error = max11936_access(ts, reg, (u8 *)&v, sizeof(v), false);
+	if (error)
+		return error;
+
+	*value = le16_to_cpu(v);
+	return 0;
+}
+
+static int max11936_power_on(struct max11936 *ts)
+{
+	int error;
+
+	gpiod_set_value_cansleep(ts->reset, 1);
+	usleep_range(4000, 4800);
+	error = regulator_enable(ts->avdd);
+	if (error)
+		return error;
+	usleep_range(100, 300);
+	error = regulator_enable(ts->dvdd);
+	if (error) {
+		regulator_disable(ts->avdd);
+		return error;
+	}
+	usleep_range(300, 400);
+	gpiod_set_value_cansleep(ts->reset, 0);
+	msleep(MAX11936_RESET_DELAY_MS);
+	ts->powered = true;
+
+	/* the controller expects this first, meaning not known */
+	return max11936_write_reg(ts, REG_STATUS, 0x8000);
+}
+
+static void max11936_power_off(struct max11936 *ts)
+{
+	if (!ts->powered)
+		return;
+
+	gpiod_set_value_cansleep(ts->reset, 1);
+	usleep_range(100, 120);
+	regulator_disable(ts->dvdd);
+	usleep_range(3000, 3800);
+	regulator_disable(ts->avdd);
+	ts->powered = false;
+	ts->legacy = false;
+}
+
+static int max11936_read_id(struct max11936 *ts)
+{
+	u16 id[2];
+	int error;
+
+	error = max11936_read_reg(ts, REG_ID0, &id[0]);
+	if (error)
+		return error;
+
+	error = max11936_read_reg(ts, REG_ID1, &id[1]);
+	if (error)
+		return error;
+
+	/* the bus answers, but nothing drives it */
+	if ((!id[0] && !id[1]) || (id[0] == 0xffff && id[1] == 0xffff))
+		return -ENODEV;
+
+	dev_dbg(&ts->spi->dev, "controller id %04x %04x\n", id[0], id[1]);
+	return 0;
+}
+
+static int max11936_init_chip(struct max11936 *ts)
+{
+	const struct max11936_data *d = ts->data;
+	int error;
+
+	error = max11936_write_regs(ts, d->pre, d->n_pre);
+	if (error)
+		return error;
+
+	error = max11936_access(ts, REG_DRIVE_CODE, ts->drive_code,
+				ts->drive_code_len, true);
+	if (error)
+		return error;
+
+	error = max11936_access(ts, REG_LINES, ts->lines, ts->lines_len, true);
+	if (error)
+		return error;
+
+	error = max11936_write_regs(ts, d->post, d->n_post);
+	if (error)
+		return error;
+
+	error = max11936_legacy(ts, true);
+	if (error)
+		return error;
+
+	ts->legacy = true;
+	return 0;
+}
+
+static void max11936_stop_scan(struct max11936 *ts)
+{
+	if (!ts->powered)
+		return;
+
+	if (ts->legacy) {
+		max11936_legacy(ts, false);
+		ts->legacy = false;
+	}
+	max11936_write_reg(ts, REG_LEGACY_MODE, 0);
+	max11936_write_reg(ts, REG_SCAN, 0);
+	max11936_write_reg(ts, REG_STATUS, 0xffff);
+	usleep_range(MAX11936_STOP_DELAY_US, MAX11936_STOP_DELAY_US + 1000);
+	max11936_write_reg(ts, REG_STATUS, 0xffff);
+}
+
+/*
+ * Power, reset and the start sequence as the vendor daemon ran it at start
+ * and resume (id, tables, registers), then scanning off. About 250 ms.
+ * Under ts->lock.
+ */
+static int max11936_power_up(struct max11936 *ts)
+{
+	int error;
+
+	error = max11936_power_on(ts);
+	if (error)
+		goto err_power_off;
+
+	error = max11936_read_id(ts);
+	if (error)
+		goto err_power_off;
+
+	error = max11936_init_chip(ts);
+	if (error)
+		goto err_power_off;
+
+	max11936_stop_scan(ts);
+	return 0;
+
+err_power_off:
+	max11936_power_off(ts);
+	return error;
+}
+
+/* scanning on: the sequence the vendor daemon used to leave the idle mode */
+static int max11936_scan_on(struct max11936 *ts)
+{
+	const struct max11936_data *d = ts->data;
+	int error;
+
+	error = max11936_write_regs(ts, d->active, d->n_active);
+	if (error)
+		return error;
+
+	error = max11936_legacy(ts, true);
+	if (error)
+		return error;
+
+	ts->legacy = true;
+	return 0;
+}
+
+/*
+ * A legacy page: [0] PAGE_DATA (PAGE_NOT_READY: no frame), [1] chip address
+ * of the page, [2] words, [3] MB_REQUEST (PAGE_NOT_LEGACY: legacy mode got
+ * lost), then the data. The word after the raw frame is a copy of the
+ * status register.
+ */
+static int max11936_read_frame(struct max11936 *ts)
+{
+	u16 *page = ts->page;
+	unsigned int len = ts->frame_words, off = 0;
+	u16 base = 0;
+	int error;
+
+	while (off < len) {
+		unsigned int n = min(len - off, MAX11936_PAGE_WORDS);
+
+		error = max11936_read(ts, REG_MAILBOX, page, n + 4);
+		if (error)
+			return error;
+
+		if (page[3] == PAGE_NOT_LEGACY) {
+			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
+			max11936_legacy(ts, true);
+			return -EAGAIN;
+		}
+
+		usleep_range(MAX11936_PAGE_DELAY_US, MAX11936_PAGE_DELAY_US + 25);
+
+		if (page[0] != PAGE_DATA) {
+			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
+			return -EAGAIN;
+		}
+		if (!off && (page[1] == ts->data->buf0 || page[1] == ts->data->buf1))
+			base = page[1];
+		if (page[1] != base + off) {
+			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
+			return -EAGAIN;
+		}
+
+		memcpy(ts->frame + off, page + 4, 2 * n);
+		off += n;
+	}
+
+	return 0;
+}
+
+static void max11936_release_all(struct max11936 *ts)
+{
+	struct input_dev *input = ts->input;
+
+	for (int i = 0; i < MAX11936_MAX_CONTACTS; i++) {
+		if (ts->slots[i].active && ts->slots[i].reported) {
+			input_mt_slot(input, i);
+			input_mt_report_slot_inactive(input);
+		}
+	}
+	memset(ts->slots, 0, sizeof(ts->slots));
+	input_mt_sync_frame(input);
+	input_sync(input);
+}
+
+static void max11936_report(struct max11936 *ts,
+			    const struct max11936_contact *c, int n)
+{
+	struct input_mt_pos pos[MAX11936_MAX_CONTACTS];
+	int slot[MAX11936_MAX_CONTACTS];
+	struct input_dev *input = ts->input;
+	int error;
+
+	for (int i = 0; i < n; i++)
+		touchscreen_set_mt_pos(&pos[i], &ts->prop, c[i].x, c[i].y);
+
+	error = input_mt_assign_slots(input, slot, pos, n, MAX11936_DMAX);
+	if (error) {
+		dev_err_ratelimited(&ts->spi->dev, "slot assignment failed: %d\n",
+				    error);
+		return;
+	}
+	max11936_slots_match(ts->slots, MAX11936_MAX_CONTACTS, c, slot, n,
+			     MAX11936_DMAX);
+	max11936_slots_update(ts->data->panel, ts->slots, MAX11936_MAX_CONTACTS,
+			      c, slot, n);
+
+	for (int i = 0; i < MAX11936_MAX_CONTACTS; i++) {
+		const struct max11936_slot *s = &ts->slots[i];
+
+		if (!s->active || !s->reported)
+			continue;
+		input_mt_slot(input, i);
+		input_mt_report_slot_state(input, s->pen ? MT_TOOL_PEN :
+						      MT_TOOL_FINGER, true);
+		touchscreen_report_pos(input, &ts->prop, s->x, s->y, true);
+	}
+	input_mt_sync_frame(input);
+	input_sync(input);
+}
+
+static int max11936_enter_idle(struct max11936 *ts)
+{
+	const struct max11936_data *d = ts->data;
+	int error;
+
+	max11936_release_all(ts);
+	max11936_stop_scan(ts);
+
+	error = max11936_write_regs(ts, d->idle_pre, d->n_idle_pre);
+	if (error)
+		return error;
+
+	/* the last frame without a contact is the reference */
+	for (unsigned int i = 0; i < ts->raw_words; i++)
+		ts->ref[i] = cpu_to_le16(ts->frame[i]);
+	error = max11936_access(ts, d->buf_ref, (u8 *)ts->ref,
+				2 * ts->raw_words, true);
+	if (error)
+		return error;
+
+	error = max11936_write_regs(ts, d->idle_post, d->n_idle_post);
+	if (error)
+		return error;
+
+	ts->idle = true;
+	ts->spurious = 0;
+	return 0;
+}
+
+static int max11936_wake(struct max11936 *ts)
+{
+	const struct max11936_data *d = ts->data;
+	u16 status = 0;
+	int error;
+
+	error = max11936_read_reg(ts, REG_STATUS, &status);
+	if (!error && !(status & d->wake_mask) &&
+	    ++ts->spurious < MAX11936_SPURIOUS) {
+		usleep_range(1000, 2000);
+		return 0;
+	}
+
+	error = max11936_scan_on(ts);
+	if (error)
+		return error;
+
+	ts->idle = false;
+	ts->last_touch = ktime_get();
+	return 0;
+}
+
+/* gives up until the next open: stops and powers off the controller */
+static void max11936_fault(struct max11936 *ts, const char *why, int error)
+{
+	dev_err(&ts->spi->dev, "%s: %d, touchscreen off until reopened\n",
+		why, error);
+	/* a level interrupt would fire for ever while the line stays low */
+	if (test_and_clear_bit(MAX11936_IRQ_ON, &ts->flags))
+		disable_irq_nosync(ts->spi->irq);
+	max11936_release_all(ts);
+	max11936_stop_scan(ts);
+	max11936_power_off(ts);
+	ts->running = false;
+	ts->idle = false;
+}
+
+static void max11936_service(struct max11936 *ts)
+{
+	struct max11936_contact c[MAX11936_MAX_CONTACTS];
+	int error, n;
+
+	error = max11936_read_frame(ts);
+	if (error) {
+		if (++ts->fails >= MAX11936_MAX_FAILS)
+			max11936_fault(ts, "no frames", error);
+		else
+			usleep_range(1000, 2000);
+		return;
+	}
+	ts->fails = 0;
+
+	/*
+	 * Status bit 0x04 marks a disturbed frame. With avdd at 3.0 V it was
+	 * set in 24 of about 60000 frames, all during pen strokes; one of them
+	 * had the pen column smeared over all drive lines, which decodes to a
+	 * line of false contacts. With avdd at 2.85 V it was set in about 11 %
+	 * of the frames, mostly without a touch. Keep the contacts of the
+	 * previous frame.
+	 */
+	if (ts->frame[ts->raw_words] & STATUS_BAD_FRAME)
+		return;
+
+	n = max11936_algo_frame(ts->algo, (const s16 *)ts->frame, ts->slots,
+				MAX11936_MAX_CONTACTS, c);
+	max11936_report(ts, c, n);
+
+	if (n) {
+		ts->last_touch = ktime_get();
+		return;
+	}
+
+	if (ktime_ms_delta(ktime_get(), ts->last_touch) <= MAX11936_IDLE_MS)
+		return;
+
+	error = max11936_enter_idle(ts);
+	if (error)
+		max11936_fault(ts, "entering idle mode failed", error);
+}
+
+static irqreturn_t max11936_irq(int irq, void *dev_id)
+{
+	struct max11936 *ts = dev_id;
+	int error;
+
+	guard(mutex)(&ts->lock);
+
+	if (!ts->running)
+		return IRQ_HANDLED;
+
+	if (ts->idle) {
+		error = max11936_wake(ts);
+		if (error)
+			max11936_fault(ts, "leaving idle mode failed", error);
+	} else {
+		max11936_service(ts);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static void max11936_stop(struct max11936 *ts)
+{
+	if (test_and_clear_bit(MAX11936_IRQ_ON, &ts->flags))
+		disable_irq(ts->spi->irq);
+
+	guard(mutex)(&ts->lock);
+	if (ts->running) {
+		max11936_release_all(ts);
+		max11936_stop_scan(ts);
+	}
+	ts->running = false;
+	ts->idle = false;
+}
+
+/*
+ * Starts scanning; sets the controller up again first if it was powered
+ * off by suspend or after a fault. Under ts->lock.
+ */
+static int max11936_start_chip(struct max11936 *ts)
+{
+	int error;
+
+	if (!ts->powered) {
+		error = max11936_power_up(ts);
+		if (error)
+			return error;
+	}
+
+	error = max11936_scan_on(ts);
+	if (error) {
+		/* set up from scratch on the next start */
+		max11936_power_off(ts);
+		return error;
+	}
+
+	max11936_algo_reset(ts->algo, ts->data->panel);
+	memset(ts->slots, 0, sizeof(ts->slots));
+	ts->fails = 0;
+	ts->idle = false;
+	ts->last_touch = ktime_get();
+	ts->running = true;
+	return 0;
+}
+
+static int max11936_start(struct max11936 *ts)
+{
+	int error;
+
+	scoped_guard(mutex, &ts->lock) {
+		error = max11936_start_chip(ts);
+		if (error)
+			return error;
+	}
+
+	set_bit(MAX11936_IRQ_ON, &ts->flags);
+	enable_irq(ts->spi->irq);
+	return 0;
+}
+
+static int max11936_open(struct input_dev *input)
+{
+	struct max11936 *ts = input_get_drvdata(input);
+	int error;
+
+	error = max11936_start(ts);
+	if (error) {
+		dev_err(&ts->spi->dev, "failed to start: %d\n", error);
+		return error;
+	}
+
+	return 0;
+}
+
+static void max11936_close(struct input_dev *input)
+{
+	struct max11936 *ts = input_get_drvdata(input);
+
+	max11936_stop(ts);
+}
+
+static void max11936_power_down(void *data)
+{
+	struct max11936 *ts = data;
+
+	guard(mutex)(&ts->lock);
+	max11936_power_off(ts);
+}
+
+static int max11936_setup(struct max11936 *ts)
+{
+	struct device *dev = &ts->spi->dev;
+	int error;
+
+	scoped_guard(mutex, &ts->lock)
+		error = max11936_power_up(ts);
+	if (error)
+		return dev_err_probe(dev, error, "failed to set up the controller\n");
+
+	return devm_add_action_or_reset(dev, max11936_power_down, ts);
+}
+
+static int max11936_probe(struct spi_device *spi)
+{
+	struct device *dev = &spi->dev;
+	const struct max11936_data *d;
+	const struct max11936_panel *p;
+	struct input_dev *input;
+	struct max11936 *ts;
+	int error;
+
+	d = spi_get_device_match_data(spi);
+	if (!d)
+		return -ENODEV;
+	p = d->panel;
+
+	if (!spi->irq)
+		return dev_err_probe(dev, -EINVAL, "no interrupt\n");
+
+	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
+	if (!ts)
+		return -ENOMEM;
+
+	ts->spi = spi;
+	ts->data = d;
+	ts->raw_words = (p->n_tx + 1) * p->n_rx;
+	ts->frame_words = ts->raw_words + 2;
+	ts->drive_code_len = 256 * DIV_ROUND_UP(p->n_tx, 8);
+	ts->lines_len = 2 + p->n_tx + 5 * p->n_rx + d->tail_len;
+	spi_set_drvdata(spi, ts);
+
+	error = devm_mutex_init(dev, &ts->lock);
+	if (error)
+		return error;
+
+	ts->frame = devm_kcalloc(dev, ts->frame_words, sizeof(*ts->frame),
+				 GFP_KERNEL);
+	ts->drive_code = devm_kzalloc(dev, ts->drive_code_len, GFP_KERNEL);
+	ts->ref = devm_kcalloc(dev, ts->raw_words, sizeof(*ts->ref), GFP_KERNEL);
+	ts->lines = devm_kzalloc(dev, ts->lines_len, GFP_KERNEL);
+	ts->algo = devm_kzalloc(dev, sizeof(*ts->algo), GFP_KERNEL);
+	if (!ts->frame || !ts->ref || !ts->drive_code || !ts->lines || !ts->algo)
+		return -ENOMEM;
+
+	error = max11936_panel_check(p);
+	if (error)
+		return error;
+	max11936_algo_reset(ts->algo, p);
+	max11936_gen_drive_code(d, ts->drive_code);
+	max11936_gen_lines(d, ts->lines);
+
+	ts->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
+	if (IS_ERR(ts->reset))
+		return dev_err_probe(dev, PTR_ERR(ts->reset),
+				     "failed to get reset gpio\n");
+
+	ts->avdd = devm_regulator_get(dev, "avdd");
+	if (IS_ERR(ts->avdd))
+		return dev_err_probe(dev, PTR_ERR(ts->avdd),
+				     "failed to get avdd supply\n");
+
+	ts->dvdd = devm_regulator_get(dev, "dvdd");
+	if (IS_ERR(ts->dvdd))
+		return dev_err_probe(dev, PTR_ERR(ts->dvdd),
+				     "failed to get dvdd supply\n");
+
+	error = max11936_setup(ts);
+	if (error)
+		return error;
+
+	input = devm_input_allocate_device(dev);
+	if (!input)
+		return -ENOMEM;
+
+	ts->input = input;
+	input->name = "MAX11936 touchscreen";
+	input->id.bustype = BUS_SPI;
+	input->open = max11936_open;
+	input->close = max11936_close;
+	input_set_drvdata(input, ts);
+
+	input_set_abs_params(input, ABS_MT_POSITION_X, 0, p->x_max, p->fuzz, 0);
+	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, p->y_max, p->fuzz, 0);
+	input_set_abs_params(input, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
+	touchscreen_parse_properties(input, true, &ts->prop);
+
+	error = input_mt_init_slots(input, MAX11936_MAX_CONTACTS,
+				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
+				    INPUT_MT_TRACK);
+	if (error)
+		return error;
+
+	error = devm_request_threaded_irq(dev, spi->irq, NULL, max11936_irq,
+					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
+					  MAX11936_NAME, ts);
+	if (error)
+		return dev_err_probe(dev, error, "failed to request irq\n");
+
+	error = input_register_device(input);
+	if (error)
+		return dev_err_probe(dev, error, "failed to register input device\n");
+
+	return 0;
+}
+
+static int max11936_suspend(struct device *dev)
+{
+	struct max11936 *ts = dev_get_drvdata(dev);
+
+	guard(mutex)(&ts->input->mutex);
+	if (input_device_enabled(ts->input))
+		max11936_stop(ts);
+
+	guard(mutex)(&ts->lock);
+	max11936_power_off(ts);
+	return 0;
+}
+
+static int max11936_resume(struct device *dev)
+{
+	struct max11936 *ts = dev_get_drvdata(dev);
+
+	guard(mutex)(&ts->input->mutex);
+	if (input_device_enabled(ts->input))
+		return max11936_start(ts);
+
+	guard(mutex)(&ts->lock);
+	return max11936_power_up(ts);
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(max11936_pm, max11936_suspend, max11936_resume);
+
+static const struct of_device_id max11936_of_match[] = {
+	{ .compatible = "maxim,max11936", .data = &max11936_tegratab_data },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, max11936_of_match);
+
+static const struct spi_device_id max11936_spi_ids[] = {
+	{ "max11936", (kernel_ulong_t)&max11936_tegratab_data },
+	{ }
+};
+MODULE_DEVICE_TABLE(spi, max11936_spi_ids);
+
+static struct spi_driver max11936_driver = {
+	.driver = {
+		.name = MAX11936_NAME,
+		.of_match_table = max11936_of_match,
+		.pm = pm_sleep_ptr(&max11936_pm),
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+	},
+	.probe = max11936_probe,
+	.id_table = max11936_spi_ids,
+};
+module_spi_driver(max11936_driver);
+
+MODULE_AUTHOR("Patrick Fischer <admin@tsv-stahl-riesa.de>");
+MODULE_DESCRIPTION("Maxim MAX11936 touchscreen driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/input/touchscreen/max11936.h b/drivers/input/touchscreen/max11936.h
new file mode 100644
index 000000000000..f8f6f6e3b0e2
--- /dev/null
+++ b/drivers/input/touchscreen/max11936.h
@@ -0,0 +1,141 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Maxim MAX11936 touchscreen: evaluation of the raw frames
+ * (max11936-algo.c), shared with max11936-core.c.
+ */
+#ifndef _MAX11936_H
+#define _MAX11936_H
+
+#include <linux/types.h>
+
+/* Largest panel the work areas are sized for */
+#define MAX11936_TX_MAX		40
+#define MAX11936_RX_MAX		24
+#define MAX11936_MAX_CONTACTS	10
+#define MAX11936_MAX_PEAKS	(MAX11936_TX_MAX * MAX11936_RX_MAX / 4)
+
+/**
+ * struct max11936_panel - panel and tuning data of one device
+ * @n_tx: drive lines (coded steps), a frame has n_tx + 1 rows
+ * @n_rx: sense lines
+ * @code: drive code, n_tx bits; step k drives line s with symbol
+ *	(s + k) % n_tx, symbol i is bit n_tx - 1 - i (1: +, 0: -)
+ * @code_inv: inverse of the drive code matrix in Q16, one row of n_tx
+ *	values: D[s][k] = code_inv[(s + k) % n_tx]
+ * @t_on: signal (decoded value of an element) for a new contact
+ * @t_off: decoded signal for a contact to stay, and region boundary
+ * @palm_area: a region with more elements per peak than this is a palm
+ * @palm_max: ... or with more elements than this in total
+ * @palm_one: ... or with more than this and a single peak
+ * @grip_row: a region with this many elements on the first/last drive line
+ *	is a hand gripping the edge
+ * @grip_col: ... alike on the first/last sense line
+ * @x0: x = (x0 - (kx * c >> 8)) >> 8, c = sense position in 1/256 lines
+ * @kx: see @x0
+ * @y0: y = (y0 - (ky * s >> 8)) >> 8, s = drive position in 1/256 lines
+ * @ky: see @y0
+ * @x_max: largest x of the screen
+ * @y_max: largest y of the screen
+ * @fuzz: input fuzz of the position, against jitter
+ * @release: frames a contact is kept after it vanished (lift-off flicker)
+ * @min_sep: contacts closer than this many pixels are one (the weaker goes)
+ * @pen_area: a contact whose region has at most this many elements ...
+ * @pen_v_max: ... and whose peak stays below this looks like a pen
+ * @pen_frames: a contact that looks like a pen in this many frames is one
+ */
+struct max11936_panel {
+	u8 n_tx, n_rx;
+	u64 code;
+	const s32 *code_inv;
+	s32 t_on, t_off;
+	s32 palm_area, palm_max, palm_one;
+	s32 grip_row, grip_col;
+	s32 x0, kx, y0, ky;
+	s32 x_max, y_max;
+	s32 fuzz;
+	u8 release;
+	s32 min_sep;
+	s32 pen_area, pen_v_max;
+	u8 pen_frames;
+};
+
+/**
+ * struct max11936_contact - a contact found in one frame
+ * @x: screen position
+ * @y: screen position
+ * @v: peak of the decoded signal
+ * @pen: looks like a pen
+ */
+struct max11936_contact {
+	s32 x, y, v;
+	bool pen;
+};
+
+/**
+ * struct max11936_slot - state of one MT slot
+ * @active: slot holds a contact
+ * @reported: tool decided, contact is reported
+ * @pen: tool, fixed when decided
+ * @frames: frames seen while undecided
+ * @missed: frames the contact has not been seen
+ * @x: last position (before touchscreen transformation)
+ * @y: last position
+ */
+struct max11936_slot {
+	bool active;
+	bool reported;
+	bool pen;
+	u8 frames;
+	u8 missed;
+	s32 x, y;
+};
+
+struct max11936_peak {
+	s32 v, x, y, region;
+	bool edge;
+};
+
+/**
+ * struct max11936_algo - evaluation state and work areas
+ * @p: panel data
+ * @base: baseline of the raw frame, Q4
+ * @base_valid: @base holds a frame
+ * @neg: frames in a row with a negative signal
+ * @diff: raw frame minus baseline
+ * @sig: decoded signal, n_tx x n_rx
+ * @label: region of each element, 0 = none
+ * @stack: work area of the region search
+ * @area: elements per region
+ * @npeaks: peaks per region
+ * @peaks: all local maxima
+ */
+struct max11936_algo {
+	const struct max11936_panel *p;
+	s32 base[(MAX11936_TX_MAX + 1) * MAX11936_RX_MAX];
+	bool base_valid;
+	int neg;
+	s32 diff[MAX11936_TX_MAX * MAX11936_RX_MAX];
+	s32 sig[MAX11936_TX_MAX * MAX11936_RX_MAX];
+	s16 label[MAX11936_TX_MAX * MAX11936_RX_MAX];
+	s16 stack[MAX11936_TX_MAX * MAX11936_RX_MAX];
+	s32 area[MAX11936_TX_MAX * MAX11936_RX_MAX + 1];
+	s32 npeaks[MAX11936_TX_MAX * MAX11936_RX_MAX + 1];
+	struct max11936_peak peaks[MAX11936_MAX_PEAKS];
+};
+
+int max11936_panel_check(const struct max11936_panel *p);
+void max11936_algo_reset(struct max11936_algo *a, const struct max11936_panel *p);
+int max11936_algo_frame(struct max11936_algo *a, const s16 *raw,
+			const struct max11936_slot *slots, int nslots,
+			struct max11936_contact *out);
+void max11936_slots_match(const struct max11936_slot *slots, int nslots,
+			  const struct max11936_contact *c, int *slot, int n,
+			  s32 dmax);
+void max11936_slots_update(const struct max11936_panel *p,
+			   struct max11936_slot *slots, int nslots,
+			   const struct max11936_contact *c, const int *slot,
+			   int n);
+
+extern const struct max11936_panel max11936_tegratab;
+
+#endif

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver
  2026-11-29 18:54 ` [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver Patrick Fischer via B4 Relay
@ 2026-11-29 19:07   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-29 19:07 UTC (permalink / raw)
  To: Patrick Fischer; +Cc: linux-input, devicetree, robh, conor+dt, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] Input: max11936 - unmatched contacts inherit undecided slots
  in max11936_slots_match()
- [Medium] Input: max11936 - negative coordinates in max11936_centroid()
  not clamped
- [Low] Input: max11936 - page address 0 bypasses buffer check in
  max11936_read_frame()
--

commit 86f2cacf758e1982ade848c538a3a8dd41492144
Author: Patrick Fischer <admin@tsv-stahl-riesa.de>

Input: add Maxim MAX11936 touchscreen driver

This commit adds an input driver for the Maxim MAX11936 SPI touchscreen
controller used in the NVIDIA Tegra Note 7, decoding raw capacitance
frames into touch and pen contacts in the kernel.

> diff --git a/drivers/input/touchscreen/max11936-algo.c b/drivers/input/touchscreen/max11936-algo.c
> new file mode 100644
> index 0000000000000..77adb224d8e2f
> --- /dev/null
> +++ b/drivers/input/touchscreen/max11936-algo.c
[ ... ]
> +/* centroid around a peak, mapped to the screen */
> +static void max11936_centroid(struct max11936_algo *a, int s, int c,
> +			      struct max11936_peak *pk)
> +{
> +	const struct max11936_panel *p = a->p;
> +	s32 floor = SIG(a, s, c) >> FLOOR_SHIFT;
> +	s32 sw = 0, ss = 0, sc = 0, sq, cq;
> +
> +	for (int ds = -WIN_S; ds <= WIN_S; ds++) {
> +		for (int dc = -WIN_C; dc <= WIN_C; dc++) {
> +			int u = s + ds, w = c + dc;
> +			s32 q;
> +
> +			if (u < 0 || u >= p->n_tx || w < 0 || w >= p->n_rx ||
> +			    SIG(a, u, w) <= floor)
> +				continue;
> +			q = SIG(a, u, w) - floor;
> +			sw += q;
> +			ss += q * u;
> +			sc += q * w;
> +		}
> +	}
> +
> +	/*
> +	 * The peak itself is above floor (t_off > 0), so sw > 0. |S| stays
> +	 * below n_tx * max|code_inv| * DIFF_CLAMP >> 16, for the Tegra Note 7
> +	 * 8523, which keeps ss * 256 and sc * 256 below 1.3 * 10^9.
> +	 */
> +	sq = (ss * 256 + sw / 2) / sw;
> +	cq = (sc * 256 + sw / 2) / sw;
> +	pk->x = (p->x0 - ((p->kx * cq) >> 8)) >> 8;
> +	pk->y = (p->y0 - ((p->ky * sq) >> 8)) >> 8;
> +}

[Severity: Medium]
Should pk->x and pk->y be clamped to [0, p->x_max] and [0, p->y_max]?

With max11936_tegratab's mapping constants, peaks on the outermost sense
line (c = 23) or drive line (s = 39) produce coordinates down to -4.
max11936_report() passes those negative values to touchscreen_set_mt_pos()
and touchscreen_report_pos(), which take unsigned int coordinates and
report values outside the advertised axis ranges.

[ ... ]

> +void max11936_slots_match(const struct max11936_slot *slots, int nslots,
> +			  const struct max11936_contact *c, int *slot, int n,
> +			  s32 dmax)
> +{
> +	bool taken[MAX11936_MAX_CONTACTS] = { };
> +
> +	for (int i = 0; i < n; i++)
> +		if (slot[i] >= 0 && slot[i] < nslots && slots[slot[i]].reported)
> +			taken[slot[i]] = true;
> +
> +	for (int i = 0; i < n; i++) {
> +		s32 best = dmax * dmax + 1;
> +		int u = -1;
> +
> +		if (slot[i] < 0 || slot[i] >= nslots || slots[slot[i]].reported)
> +			continue;
> +		for (int k = 0; k < nslots; k++) {
> +			s32 dx = c[i].x - slots[k].x, dy = c[i].y - slots[k].y;
> +
> +			if (!slots[k].active || slots[k].reported || taken[k] ||
> +			    dx * dx + dy * dy >= best)
> +				continue;
> +			best = dx * dx + dy * dy;
> +			u = k;
> +		}
> +		if (u < 0)
> +			continue;
> +		/* swap with a contact that got slot u */
> +		for (int j = 0; j < n; j++)
> +			if (slot[j] == u)
> +				slot[j] = slot[i];
> +		slot[i] = u;
> +		taken[u] = true;
> +	}
> +}
> +
> +/**
> + * max11936_slots_update() - apply the contacts of a frame to the slots
> + * @p: panel data
> + * @slots: slot state
> + * @nslots: number of @slots
> + * @c: contacts of the frame
> + * @slot: slot of each contact (input_mt_assign_slots(), max11936_slots_match())
> + * @n: number of contacts
> + *
> + * A contact is a finger as soon as one frame shows a finger; one that looks
> + * like a pen in its first @p->pen_frames frames is a pen. Until then it is
> + * not reported, and it is dropped if it vanishes before: pen taps shorter
> + * than that are lost (the shortest one recorded lasted 11 frames). The tool
> + * stays for the life of the contact. A reported contact that vanishes stays
> + * for @p->release frames at its last position, which bridges the flicker of
> + * a finger whose signal crosses t_off while it is lifted.
> + */
> +void max11936_slots_update(const struct max11936_panel *p,
> +			   struct max11936_slot *slots, int nslots,
> +			   const struct max11936_contact *c, const int *slot,
> +			   int n)
> +{
> +	bool seen[MAX11936_MAX_CONTACTS] = { };
> +
> +	for (int i = 0; i < n; i++) {
> +		struct max11936_slot *s;
> +
> +		if (slot[i] < 0 || slot[i] >= nslots || seen[slot[i]])
> +			continue;
> +		s = &slots[slot[i]];
> +		seen[slot[i]] = true;
> +		s->missed = 0;
> +		s->x = c[i].x;
> +		s->y = c[i].y;
> +
> +		if (!s->active) {
> +			s->active = true;
> +			s->reported = false;
> +			s->frames = 0;
> +		}
> +		if (!s->reported) {
> +			s->frames++;
> +			if (!c[i].pen || s->frames >= p->pen_frames) {
> +				s->pen = c[i].pen;
> +				s->reported = true;
> +			}
> +		}
> +	}

[Severity: Medium]
Can an unmatched contact inherit an active undecided slot in
max11936_slots_match()?

Because unreported slots have no tracking ID in input->mt,
input_mt_assign_slots() treats them as free and can assign slot[i] = k for
a slot k where slots[k].active is true and slots[k].reported is false.  If
the previous undecided contact in slot k vanished and a new contact c[i]
appears farther than dmax away, max11936_slots_match() finds u < 0 (or
swaps another contact into slot[i]) and leaves slot[i] == k.
max11936_slots_update() then sees s->active already set, so it neither
expires the vanished contact nor resets s->frames to 0, and the new contact
inherits the vanished contact's frame count toward p->pen_frames.

Also, because c[] is ordered by signal strength, the greedy loop in
max11936_slots_match() lets a stronger new contact within dmax claim an
existing weaker pen contact's undecided slot before that pen contact is
matched.

> diff --git a/drivers/input/touchscreen/max11936-core.c b/drivers/input/touchscreen/max11936-core.c
> new file mode 100644
> index 0000000000000..e1532df781a3f
> --- /dev/null
> +++ b/drivers/input/touchscreen/max11936-core.c
[ ... ]
> +static int max11936_read_frame(struct max11936 *ts)
> +{
> +	u16 *page = ts->page;
> +	unsigned int len = ts->frame_words, off = 0;
> +	u16 base = 0;
> +	int error;
> +
> +	while (off < len) {
> +		unsigned int n = min(len - off, MAX11936_PAGE_WORDS);
> +
> +		error = max11936_read(ts, REG_MAILBOX, page, n + 4);
> +		if (error)
> +			return error;
> +
> +		if (page[3] == PAGE_NOT_LEGACY) {
> +			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
> +			max11936_legacy(ts, true);
> +			return -EAGAIN;
> +		}
> +
> +		usleep_range(MAX11936_PAGE_DELAY_US, MAX11936_PAGE_DELAY_US + 25);
> +
> +		if (page[0] != PAGE_DATA) {
> +			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
> +			return -EAGAIN;
> +		}
> +		if (!off && (page[1] == ts->data->buf0 || page[1] == ts->data->buf1))
> +			base = page[1];
> +		if (page[1] != base + off) {
> +			max11936_write_reg(ts, REG_LEGACY_MODE, 1);
> +			return -EAGAIN;
> +		}

[Severity: Low]
Does page[1] == 0 on the first page bypass the buffer address check here?
Because base is initialized to 0, page[1] == 0 at off == 0 satisfies
page[1] == base + off even though it matches neither buf0 nor buf1.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129-upstream-touch-max11936-v1-0-6f98b121b56b@tsv-stahl-riesa.de?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-29 19:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 18:54 [PATCH RFC 0/2] Input: Maxim MAX11936 touchscreen (NVIDIA Tegra Note 7) Patrick Fischer via B4 Relay
2026-11-29 18:54 ` [PATCH RFC 1/2] dt-bindings: input: touchscreen: Add Maxim MAX11936 Patrick Fischer via B4 Relay
2026-11-29 18:54 ` [PATCH RFC 2/2] Input: add Maxim MAX11936 touchscreen driver Patrick Fischer via B4 Relay
2026-11-29 19:07   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox