aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-03-18 00:51:52 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-03-18 00:51:52 -0400
commit10fdfee7f7fd8d4a6a8455ac4c9fbbc51d79b9f7 (patch)
treeace3300ec40d2a253c59a70ee37eb76fa53c859f
parent0f49fc95b86fc77b867d643e2d38bc9f28035ed4 (diff)
parent4d2508a55990c9b0fc1afb2c2cc5636712fd30f7 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
Pull input updates from Dmitry Torokhov: "The most notable item is addition of support for Synaptics RMI4 protocol which is native protocol for all current Synaptics devices (touchscreens, touchpads). In later releases we'll switch devices using HID and PS/2 protocol emulation to RMI4. You will also get: - BYD PS/2 touchpad protocol support for psmouse - MELFAS MIP4 Touchscreen driver - rotary encoder was moved away from legacy platform data and to generic device properties API, devm_* API, and can now handle encoders using more than 2 GPIOs - Cypress touchpad driver was switched to devm_* API and device properties - other assorted driver fixes" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input: (40 commits) ARM: pxa/raumfeld: use PROPERTY_ENTRY_INTEGER to define props Input: synaptics-rmi4 - using logical instead of bitwise AND Input: powermate - fix oops with malicious USB descriptors Input: snvs_pwrkey - fix returned value check of syscon_regmap_lookup_by_phandle() MAINTAINERS: add devicetree bindings to Input Drivers section Input: synaptics-rmi4 - add device tree support to the SPI transport driver Input: synaptics-rmi4 - add SPI transport driver Input: synaptics-rmi4 - add support for F30 Input: synaptics-rmi4 - add support for F12 Input: synaptics-rmi4 - add device tree support for 2d sensors and F11 Input: synaptics-rmi4 - add support for 2D sensors and F11 Input: synaptics-rmi4 - add device tree support for RMI4 I2C devices Input: synaptics-rmi4 - add I2C transport driver Input: synaptics-rmi4 - add support for Synaptics RMI4 devices Input: ad7879 - add device tree support Input: ad7879 - fix default x/y axis assignment Input: ad7879 - move header to platform_data directory Input: ts4800 - add hardware dependency Input: cyapa - fix for losing events during device power transitions Input: sh_keysc - remove dependency on SUPERH ...
-rw-r--r--Documentation/devicetree/bindings/goldfish/events.txt17
-rw-r--r--Documentation/devicetree/bindings/input/rmi4/rmi_2d_sensor.txt56
-rw-r--r--Documentation/devicetree/bindings/input/rmi4/rmi_f01.txt39
-rw-r--r--Documentation/devicetree/bindings/input/rmi4/rmi_i2c.txt53
-rw-r--r--Documentation/devicetree/bindings/input/rmi4/rmi_spi.txt57
-rw-r--r--Documentation/devicetree/bindings/input/rotary-encoder.txt2
-rw-r--r--Documentation/devicetree/bindings/input/touchscreen/ad7879.txt53
-rw-r--r--Documentation/devicetree/bindings/input/touchscreen/cyttsp.txt95
-rw-r--r--Documentation/devicetree/bindings/input/touchscreen/touchscreen.txt2
-rw-r--r--Documentation/devicetree/bindings/spi/spi-bus.txt2
-rw-r--r--Documentation/devicetree/bindings/vendor-prefixes.txt1
-rw-r--r--MAINTAINERS1
-rw-r--r--arch/arm/mach-pxa/raumfeld.c43
-rw-r--r--arch/blackfin/mach-bf527/boards/ezbrd.c2
-rw-r--r--arch/blackfin/mach-bf527/boards/ezkit.c2
-rw-r--r--arch/blackfin/mach-bf527/boards/tll6527m.c2
-rw-r--r--arch/blackfin/mach-bf537/boards/stamp.c2
-rw-r--r--arch/blackfin/mach-bf538/boards/ezkit.c2
-rw-r--r--drivers/input/Kconfig2
-rw-r--r--drivers/input/Makefile2
-rw-r--r--drivers/input/keyboard/Kconfig2
-rw-r--r--drivers/input/keyboard/goldfish_events.c17
-rw-r--r--drivers/input/keyboard/snvs_pwrkey.c8
-rw-r--r--drivers/input/keyboard/spear-keyboard.c6
-rw-r--r--drivers/input/misc/powermate.c3
-rw-r--r--drivers/input/misc/rotary_encoder.c403
-rw-r--r--drivers/input/mouse/Kconfig10
-rw-r--r--drivers/input/mouse/Makefile1
-rw-r--r--drivers/input/mouse/byd.c337
-rw-r--r--drivers/input/mouse/byd.h18
-rw-r--r--drivers/input/mouse/cyapa.c22
-rw-r--r--drivers/input/mouse/cyapa.h14
-rw-r--r--drivers/input/mouse/cyapa_gen3.c108
-rw-r--r--drivers/input/mouse/cyapa_gen5.c99
-rw-r--r--drivers/input/mouse/cyapa_gen6.c4
-rw-r--r--drivers/input/mouse/psmouse-base.c14
-rw-r--r--drivers/input/mouse/psmouse.h1
-rw-r--r--drivers/input/rmi4/Kconfig63
-rw-r--r--drivers/input/rmi4/Makefile13
-rw-r--r--drivers/input/rmi4/rmi_2d_sensor.c329
-rw-r--r--drivers/input/rmi4/rmi_2d_sensor.h87
-rw-r--r--drivers/input/rmi4/rmi_bus.c419
-rw-r--r--drivers/input/rmi4/rmi_bus.h182
-rw-r--r--drivers/input/rmi4/rmi_driver.c1055
-rw-r--r--drivers/input/rmi4/rmi_driver.h105
-rw-r--r--drivers/input/rmi4/rmi_f01.c624
-rw-r--r--drivers/input/rmi4/rmi_f11.c1317
-rw-r--r--drivers/input/rmi4/rmi_f12.c457
-rw-r--r--drivers/input/rmi4/rmi_f30.c407
-rw-r--r--drivers/input/rmi4/rmi_i2c.c397
-rw-r--r--drivers/input/rmi4/rmi_spi.c589
-rw-r--r--drivers/input/touchscreen/Kconfig17
-rw-r--r--drivers/input/touchscreen/Makefile1
-rw-r--r--drivers/input/touchscreen/ad7879-i2c.c10
-rw-r--r--drivers/input/touchscreen/ad7879-spi.c10
-rw-r--r--drivers/input/touchscreen/ad7879.c160
-rw-r--r--drivers/input/touchscreen/cyttsp_core.c194
-rw-r--r--drivers/input/touchscreen/cyttsp_core.h10
-rw-r--r--drivers/input/touchscreen/cyttsp_i2c.c10
-rw-r--r--drivers/input/touchscreen/cyttsp_spi.c10
-rw-r--r--drivers/input/touchscreen/melfas_mip4.c1517
-rw-r--r--drivers/input/touchscreen/stmpe-ts.c31
-rw-r--r--include/linux/input/cyttsp.h15
-rw-r--r--include/linux/platform_data/ad7879.h (renamed from include/linux/spi/ad7879.h)2
-rw-r--r--include/linux/rmi.h359
-rw-r--r--include/linux/rotary_encoder.h17
-rw-r--r--include/uapi/linux/input.h1
67 files changed, 9387 insertions, 523 deletions
diff --git a/Documentation/devicetree/bindings/goldfish/events.txt b/Documentation/devicetree/bindings/goldfish/events.txt
new file mode 100644
index 000000000000..5babf46317a4
--- /dev/null
+++ b/Documentation/devicetree/bindings/goldfish/events.txt
@@ -0,0 +1,17 @@
1Android Goldfish Events Keypad
2
3Android goldfish events keypad device generated by android emulator.
4
5Required properties:
6
7- compatible : should contain "google,goldfish-events-keypad" to match emulator
8- reg : <registers mapping>
9- interrupts : <interrupt mapping>
10
11Example:
12
13 goldfish-events@9040000 {
14 compatible = "google,goldfish-events-keypad";
15 reg = <0x9040000 0x1000>;
16 interrupts = <0x5>;
17 };
diff --git a/Documentation/devicetree/bindings/input/rmi4/rmi_2d_sensor.txt b/Documentation/devicetree/bindings/input/rmi4/rmi_2d_sensor.txt
new file mode 100644
index 000000000000..f2c30c8b725d
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/rmi4/rmi_2d_sensor.txt
@@ -0,0 +1,56 @@
1Synaptics RMI4 2D Sensor Device Binding
2
3The Synaptics RMI4 core is able to support RMI4 devices using different
4transports and different functions. This file describes the device tree
5bindings for devices which contain 2D sensors using Function 11 or
6Function 12. Complete documentation for transports and other functions
7can be found in:
8Documentation/devicetree/bindings/input/rmi4.
9
10RMI4 Function 11 and Function 12 are for 2D touch position sensing.
11Additional documentation for F11 can be found at:
12http://www.synaptics.com/sites/default/files/511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
13
14Optional Touch Properties:
15Description in Documentation/devicetree/bindings/input/touch
16- touchscreen-inverted-x
17- touchscreen-inverted-y
18- touchscreen-swapped-x-y
19- touchscreen-x-mm
20- touchscreen-y-mm
21
22Optional Properties:
23- syna,clip-x-low: Sets a minimum value for X.
24- syna,clip-y-low: Sets a minimum value for Y.
25- syna,clip-x-high: Sets a maximum value for X.
26- syna,clip-y-high: Sets a maximum value for Y.
27- syna,offset-x: Add an offset to X.
28- syna,offset-y: Add an offset to Y.
29- syna,delta-x-threshold: Set the minimum distance on the X axis required
30 to generate an interrupt in reduced reporting
31 mode.
32- syna,delta-y-threshold: Set the minimum distance on the Y axis required
33 to generate an interrupt in reduced reporting
34 mode.
35- syna,sensor-type: Set the sensor type. 1 for touchscreen 2 for touchpad.
36- syna,disable-report-mask: Mask for disabling posiiton reporting. Used to
37 disable reporing absolute position data.
38- syna,rezero-wait-ms: Time in miliseconds to wait after issuing a rezero
39 command.
40
41
42Example of a RMI4 I2C device with F11:
43Example:
44 &i2c1 {
45 rmi4-i2c-dev@2c {
46 compatible = "syna,rmi4-i2c";
47
48 ...
49
50 rmi4-f11@11 {
51 reg = <0x11>;
52 touchscreen-inverted-y;
53 syna,sensor-type = <2>;
54 };
55 };
56 };
diff --git a/Documentation/devicetree/bindings/input/rmi4/rmi_f01.txt b/Documentation/devicetree/bindings/input/rmi4/rmi_f01.txt
new file mode 100644
index 000000000000..079cad2b6843
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/rmi4/rmi_f01.txt
@@ -0,0 +1,39 @@
1Synaptics RMI4 F01 Device Binding
2
3The Synaptics RMI4 core is able to support RMI4 devices using different
4transports and different functions. This file describes the device tree
5bindings for devices which contain Function 1. Complete documentation
6for transports and other functions can be found in:
7Documentation/devicetree/bindings/input/rmi4.
8
9Additional documentation for F01 can be found at:
10http://www.synaptics.com/sites/default/files/511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
11
12Optional Properties:
13- syna,nosleep-mode: If set the device will run at full power without sleeping.
14 nosleep has 3 modes, 0 will not change the default
15 setting, 1 will disable nosleep (allow sleeping),
16 and 2 will enable nosleep (disabling sleep).
17- syna,wakeup-threshold: Defines the amplitude of the disturbance to the
18 background capacitance that will cause the
19 device to wake from dozing.
20- syna,doze-holdoff-ms: The delay to wait after the last finger lift and the
21 first doze cycle.
22- syna,doze-interval-ms: The time period that the device sleeps between finger
23 activity.
24
25
26Example of a RMI4 I2C device with F01:
27 Example:
28 &i2c1 {
29 rmi4-i2c-dev@2c {
30 compatible = "syna,rmi4-i2c";
31
32 ...
33
34 rmi4-f01@1 {
35 reg = <0x1>;
36 syna,nosleep-mode = <1>;
37 };
38 };
39 };
diff --git a/Documentation/devicetree/bindings/input/rmi4/rmi_i2c.txt b/Documentation/devicetree/bindings/input/rmi4/rmi_i2c.txt
new file mode 100644
index 000000000000..95fa715c6046
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/rmi4/rmi_i2c.txt
@@ -0,0 +1,53 @@
1Synaptics RMI4 I2C Device Binding
2
3The Synaptics RMI4 core is able to support RMI4 devices using different
4transports and different functions. This file describes the device tree
5bindings for devices using the I2C transport driver. Complete documentation
6for other transports and functions can be found in
7Documentation/devicetree/bindings/input/rmi4.
8
9Required Properties:
10- compatible: syna,rmi4-i2c
11- reg: I2C address
12- #address-cells: Set to 1 to indicate that the function child nodes
13 consist of only on uint32 value.
14- #size-cells: Set to 0 to indicate that the function child nodes do not
15 have a size property.
16
17Optional Properties:
18- interrupts: interrupt which the rmi device is connected to.
19- interrupt-parent: The interrupt controller.
20See Documentation/devicetree/bindings/interrupt-controller/interrupts.txt
21
22- syna,reset-delay-ms: The number of milliseconds to wait after resetting the
23 device.
24
25Function Parameters:
26Parameters specific to RMI functions are contained in child nodes of the rmi device
27 node. Documentation for the parameters of each function can be found in:
28Documentation/devicetree/bindings/input/rmi4/rmi_f*.txt.
29
30
31
32Example:
33 &i2c1 {
34 rmi4-i2c-dev@2c {
35 compatible = "syna,rmi4-i2c";
36 reg = <0x2c>;
37 #address-cells = <1>;
38 #size-cells = <0>;
39 interrupt-parent = <&gpio>;
40 interrupts = <4 2>;
41
42 rmi4-f01@1 {
43 reg = <0x1>;
44 syna,nosleep-mode = <1>;
45 };
46
47 rmi4-f11@11 {
48 reg = <0x11>;
49 touchscreen-inverted-y;
50 syna,sensor-type = <2>;
51 };
52 };
53 };
diff --git a/Documentation/devicetree/bindings/input/rmi4/rmi_spi.txt b/Documentation/devicetree/bindings/input/rmi4/rmi_spi.txt
new file mode 100644
index 000000000000..a4ca7828f21d
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/rmi4/rmi_spi.txt
@@ -0,0 +1,57 @@
1Synaptics RMI4 SPI Device Binding
2
3The Synaptics RMI4 core is able to support RMI4 devices using different
4transports and different functions. This file describes the device tree
5bindings for devices using the SPI transport driver. Complete documentation
6for other transports and functions can be found in
7Documentation/devicetree/bindings/input/rmi4.
8
9Required Properties:
10- compatible: syna,rmi4-spi
11- reg: Chip select address for the device
12- #address-cells: Set to 1 to indicate that the function child nodes
13 consist of only on uint32 value.
14- #size-cells: Set to 0 to indicate that the function child nodes do not
15 have a size property.
16
17Optional Properties:
18- interrupts: interrupt which the rmi device is connected to.
19- interrupt-parent: The interrupt controller.
20See Documentation/devicetree/bindings/interrupt-controller/interrupts.txt
21
22- spi-rx-delay-us: microsecond delay after a read transfer.
23- spi-tx-delay-us: microsecond delay after a write transfer.
24
25Function Parameters:
26Parameters specific to RMI functions are contained in child nodes of the rmi device
27 node. Documentation for the parameters of each function can be found in:
28Documentation/devicetree/bindings/input/rmi4/rmi_f*.txt.
29
30
31
32Example:
33 spi@7000d800 {
34 rmi4-spi-dev@0 {
35 compatible = "syna,rmi4-spi";
36 reg = <0x0>;
37 #address-cells = <1>;
38 #size-cells = <0>;
39 spi-max-frequency = <4000000>;
40 spi-cpha;
41 spi-cpol;
42 interrupt-parent = <&gpio>;
43 interrupts = <TEGRA_GPIO(K, 2) 0x2>;
44 spi-rx-delay-us = <30>;
45
46 rmi4-f01@1 {
47 reg = <0x1>;
48 syna,nosleep-mode = <1>;
49 };
50
51 rmi4-f11@11 {
52 reg = <0x11>;
53 touchscreen-inverted-y;
54 syna,sensor-type = <2>;
55 };
56 };
57 };
diff --git a/Documentation/devicetree/bindings/input/rotary-encoder.txt b/Documentation/devicetree/bindings/input/rotary-encoder.txt
index de99cbbbf6da..6c9f0c8a846c 100644
--- a/Documentation/devicetree/bindings/input/rotary-encoder.txt
+++ b/Documentation/devicetree/bindings/input/rotary-encoder.txt
@@ -1,7 +1,7 @@
1Rotary encoder DT bindings 1Rotary encoder DT bindings
2 2
3Required properties: 3Required properties:
4- gpios: a spec for two GPIOs to be used 4- gpios: a spec for at least two GPIOs to be used, most significant first
5 5
6Optional properties: 6Optional properties:
7- linux,axis: the input subsystem axis to map to this rotary encoder. 7- linux,axis: the input subsystem axis to map to this rotary encoder.
diff --git a/Documentation/devicetree/bindings/input/touchscreen/ad7879.txt b/Documentation/devicetree/bindings/input/touchscreen/ad7879.txt
new file mode 100644
index 000000000000..e3f22d23fc8f
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/touchscreen/ad7879.txt
@@ -0,0 +1,53 @@
1* Analog Devices AD7879(-1)/AD7889(-1) touchscreen interface (SPI/I2C)
2
3Required properties:
4- compatible : for SPI slave, use "adi,ad7879"
5 for I2C slave, use "adi,ad7879-1"
6- reg : SPI chipselect/I2C slave address
7 See spi-bus.txt for more SPI slave properties
8- interrupt-parent : the phandle for the interrupt controller
9- interrupts : touch controller interrupt
10- touchscreen-max-pressure : maximum reported pressure
11- adi,resistance-plate-x : total resistance of X-plate (for pressure
12 calculation)
13Optional properties:
14- touchscreen-swapped-x-y : X and Y axis are swapped (boolean)
15- adi,first-conversion-delay : 0-12: In 128us steps (starting with 128us)
16 13 : 2.560ms
17 14 : 3.584ms
18 15 : 4.096ms
19 This property has to be a '/bits/ 8' value
20- adi,acquisition-time : 0: 2us
21 1: 4us
22 2: 8us
23 3: 16us
24 This property has to be a '/bits/ 8' value
25- adi,median-filter-size : 0: disabled
26 1: 4 measurements
27 2: 8 measurements
28 3: 16 measurements
29 This property has to be a '/bits/ 8' value
30- adi,averaging : 0: 2 middle values (1 if median disabled)
31 1: 4 middle values
32 2: 8 middle values
33 3: 16 values
34 This property has to be a '/bits/ 8' value
35- adi,conversion-interval: : 0 : convert one time only
36 1-255: 515us + val * 35us (up to 9.440ms)
37 This property has to be a '/bits/ 8' value
38
39Example:
40
41 ad7879@2c {
42 compatible = "adi,ad7879-1";
43 reg = <0x2c>;
44 interrupt-parent = <&gpio1>;
45 interrupts = <13 IRQ_TYPE_EDGE_FALLING>;
46 touchscreen-max-pressure = <4096>;
47 adi,resistance-plate-x = <120>;
48 adi,first-conversion-delay = /bits/ 8 <3>;
49 adi,acquisition-time = /bits/ 8 <1>;
50 adi,median-filter-size = /bits/ 8 <2>;
51 adi,averaging = /bits/ 8 <1>;
52 adi,conversion-interval = /bits/ 8 <255>;
53 };
diff --git a/Documentation/devicetree/bindings/input/touchscreen/cyttsp.txt b/Documentation/devicetree/bindings/input/touchscreen/cyttsp.txt
new file mode 100644
index 000000000000..b75d4cfd2c36
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/touchscreen/cyttsp.txt
@@ -0,0 +1,95 @@
1* Cypress cyttsp touchscreen controller
2
3Required properties:
4 - compatible : must be "cypress,cyttsp-i2c" or "cypress,cyttsp-spi"
5 - reg : Device I2C address or SPI chip select number
6 - spi-max-frequency : Maximum SPI clocking speed of the device (for cyttsp-spi)
7 - interrupt-parent : the phandle for the gpio controller
8 (see interrupt binding[0]).
9 - interrupts : (gpio) interrupt to which the chip is connected
10 (see interrupt binding[0]).
11 - bootloader-key : the 8-byte bootloader key that is required to switch
12 the chip from bootloader mode (default mode) to
13 application mode.
14 This property has to be specified as an array of 8
15 '/bits/ 8' values.
16
17Optional properties:
18 - reset-gpios : the reset gpio the chip is connected to
19 (see GPIO binding[1] for more details).
20 - touchscreen-size-x : horizontal resolution of touchscreen (in pixels)
21 - touchscreen-size-y : vertical resolution of touchscreen (in pixels)
22 - touchscreen-fuzz-x : horizontal noise value of the absolute input device
23 (in pixels)
24 - touchscreen-fuzz-y : vertical noise value of the absolute input device
25 (in pixels)
26 - active-distance : the distance in pixels beyond which a touch must move
27 before movement is detected and reported by the device.
28 Valid values: 0-15.
29 - active-interval-ms : the minimum period in ms between consecutive
30 scanning/processing cycles when the chip is in active mode.
31 Valid values: 0-255.
32 - lowpower-interval-ms : the minimum period in ms between consecutive
33 scanning/processing cycles when the chip is in low-power mode.
34 Valid values: 0-2550
35 - touch-timeout-ms : minimum time in ms spent in the active power state while no
36 touches are detected before entering low-power mode.
37 Valid values: 0-2550
38 - use-handshake : enable register-based handshake (boolean). This should
39 only be used if the chip is configured to use 'blocking
40 communication with timeout' (in this case the device
41 generates an interrupt at the end of every
42 scanning/processing cycle).
43
44[0]: Documentation/devicetree/bindings/interrupt-controller/interrupts.txt
45[1]: Documentation/devicetree/bindings/gpio/gpio.txt
46
47Example:
48 &i2c1 {
49 /* ... */
50 cyttsp@a {
51 compatible = "cypress,cyttsp-i2c";
52 reg = <0xa>;
53 interrupt-parent = <&gpio0>;
54 interrupts = <28 0>;
55 reset-gpios = <&gpio3 4 GPIO_ACTIVE_LOW>;
56
57 touchscreen-size-x = <800>;
58 touchscreen-size-y = <480>;
59 touchscreen-fuzz-x = <4>;
60 touchscreen-fuzz-y = <7>;
61
62 bootloader-key = /bits/ 8 <0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08>;
63 active-distance = <8>;
64 active-interval-ms = <0>;
65 lowpower-interval-ms = <200>;
66 touch-timeout-ms = <100>;
67 };
68
69 /* ... */
70 };
71
72 &mcspi1 {
73 /* ... */
74 cyttsp@0 {
75 compatible = "cypress,cyttsp-spi";
76 spi-max-frequency = <6000000>;
77 reg = <0>;
78 interrupt-parent = <&gpio0>;
79 interrupts = <28 0>;
80 reset-gpios = <&gpio3 4 GPIO_ACTIVE_LOW>;
81
82 touchscreen-size-x = <800>;
83 touchscreen-size-y = <480>;
84 touchscreen-fuzz-x = <4>;
85 touchscreen-fuzz-y = <7>;
86
87 bootloader-key = /bits/ 8 <0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08>;
88 active-distance = <8>;
89 active-interval-ms = <0>;
90 lowpower-interval-ms = <200>;
91 touch-timeout-ms = <100>;
92 };
93
94 /* ... */
95 };
diff --git a/Documentation/devicetree/bindings/input/touchscreen/touchscreen.txt b/Documentation/devicetree/bindings/input/touchscreen/touchscreen.txt
index ac23caf518ad..bccaa4e73045 100644
--- a/Documentation/devicetree/bindings/input/touchscreen/touchscreen.txt
+++ b/Documentation/devicetree/bindings/input/touchscreen/touchscreen.txt
@@ -18,6 +18,8 @@ Optional properties for Touchscreens:
18 - touchscreen-inverted-y : Y axis is inverted (boolean) 18 - touchscreen-inverted-y : Y axis is inverted (boolean)
19 - touchscreen-swapped-x-y : X and Y axis are swapped (boolean) 19 - touchscreen-swapped-x-y : X and Y axis are swapped (boolean)
20 Swapping is done after inverting the axis 20 Swapping is done after inverting the axis
21 - touchscreen-x-mm : horizontal length in mm of the touchscreen
22 - touchscreen-y-mm : vertical length in mm of the touchscreen
21 23
22Deprecated properties for Touchscreens: 24Deprecated properties for Touchscreens:
23 - x-size : deprecated name for touchscreen-size-x 25 - x-size : deprecated name for touchscreen-size-x
diff --git a/Documentation/devicetree/bindings/spi/spi-bus.txt b/Documentation/devicetree/bindings/spi/spi-bus.txt
index bbaa857dd68f..42d595425dfb 100644
--- a/Documentation/devicetree/bindings/spi/spi-bus.txt
+++ b/Documentation/devicetree/bindings/spi/spi-bus.txt
@@ -61,6 +61,8 @@ contain the following properties.
61 used for MOSI. Defaults to 1 if not present. 61 used for MOSI. Defaults to 1 if not present.
62- spi-rx-bus-width - (optional) The bus width(number of data wires) that 62- spi-rx-bus-width - (optional) The bus width(number of data wires) that
63 used for MISO. Defaults to 1 if not present. 63 used for MISO. Defaults to 1 if not present.
64- spi-rx-delay-us - (optional) Microsecond delay after a read transfer.
65- spi-tx-delay-us - (optional) Microsecond delay after a write transfer.
64 66
65Some SPI controllers and devices support Dual and Quad SPI transfer mode. 67Some SPI controllers and devices support Dual and Quad SPI transfer mode.
66It allows data in the SPI system to be transferred in 2 wires(DUAL) or 4 wires(QUAD). 68It allows data in the SPI system to be transferred in 2 wires(DUAL) or 4 wires(QUAD).
diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 83700da22b79..bd33a268fd1b 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -229,6 +229,7 @@ st STMicroelectronics
229startek Startek 229startek Startek
230ste ST-Ericsson 230ste ST-Ericsson
231stericsson ST-Ericsson 231stericsson ST-Ericsson
232syna Synaptics Inc.
232synology Synology, Inc. 233synology Synology, Inc.
233SUNW Sun Microsystems, Inc 234SUNW Sun Microsystems, Inc
234tbs TBS Technologies 235tbs TBS Technologies
diff --git a/MAINTAINERS b/MAINTAINERS
index 94ea42b76b30..145289e3eb7a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5581,6 +5581,7 @@ F: drivers/input/
5581F: include/linux/input.h 5581F: include/linux/input.h
5582F: include/uapi/linux/input.h 5582F: include/uapi/linux/input.h
5583F: include/linux/input/ 5583F: include/linux/input/
5584F: Documentation/devicetree/bindings/input/
5584 5585
5585INPUT MULTITOUCH (MT) PROTOCOL 5586INPUT MULTITOUCH (MT) PROTOCOL
5586M: Henrik Rydberg <rydberg@bitmath.org> 5587M: Henrik Rydberg <rydberg@bitmath.org>
diff --git a/arch/arm/mach-pxa/raumfeld.c b/arch/arm/mach-pxa/raumfeld.c
index 8347d87a713d..5a941bd3dbed 100644
--- a/arch/arm/mach-pxa/raumfeld.c
+++ b/arch/arm/mach-pxa/raumfeld.c
@@ -18,12 +18,13 @@
18 18
19#include <linux/init.h> 19#include <linux/init.h>
20#include <linux/kernel.h> 20#include <linux/kernel.h>
21#include <linux/property.h>
21#include <linux/platform_device.h> 22#include <linux/platform_device.h>
22#include <linux/interrupt.h> 23#include <linux/interrupt.h>
23#include <linux/gpio.h> 24#include <linux/gpio.h>
25#include <linux/gpio/machine.h>
24#include <linux/smsc911x.h> 26#include <linux/smsc911x.h>
25#include <linux/input.h> 27#include <linux/input.h>
26#include <linux/rotary_encoder.h>
27#include <linux/gpio_keys.h> 28#include <linux/gpio_keys.h>
28#include <linux/input/eeti_ts.h> 29#include <linux/input/eeti_ts.h>
29#include <linux/leds.h> 30#include <linux/leds.h>
@@ -366,22 +367,31 @@ static struct pxaohci_platform_data raumfeld_ohci_info = {
366 * Rotary encoder input device 367 * Rotary encoder input device
367 */ 368 */
368 369
369static struct rotary_encoder_platform_data raumfeld_rotary_encoder_info = { 370static struct gpiod_lookup_table raumfeld_rotary_gpios_table = {
370 .steps = 24, 371 .dev_id = "rotary-encoder.0",
371 .axis = REL_X, 372 .table = {
372 .relative_axis = 1, 373 GPIO_LOOKUP_IDX("gpio-0",
373 .gpio_a = GPIO_VOLENC_A, 374 GPIO_VOLENC_A, NULL, 0, GPIO_ACTIVE_LOW),
374 .gpio_b = GPIO_VOLENC_B, 375 GPIO_LOOKUP_IDX("gpio-0",
375 .inverted_a = 1, 376 GPIO_VOLENC_B, NULL, 1, GPIO_ACTIVE_HIGH),
376 .inverted_b = 0, 377 { },
378 },
379};
380
381static struct property_entry raumfeld_rotary_properties[] = {
382 PROPERTY_ENTRY_INTEGER("rotary-encoder,steps-per-period", u32, 24),
383 PROPERTY_ENTRY_INTEGER("linux,axis", u32, REL_X),
384 PROPERTY_ENTRY_INTEGER("rotary-encoder,relative_axis", u32, 1),
385 { },
386};
387
388static struct property_set raumfeld_rotary_property_set = {
389 .properties = raumfeld_rotary_properties,
377}; 390};
378 391
379static struct platform_device rotary_encoder_device = { 392static struct platform_device rotary_encoder_device = {
380 .name = "rotary-encoder", 393 .name = "rotary-encoder",
381 .id = 0, 394 .id = 0,
382 .dev = {
383 .platform_data = &raumfeld_rotary_encoder_info,
384 }
385}; 395};
386 396
387/** 397/**
@@ -1051,7 +1061,12 @@ static void __init __maybe_unused raumfeld_controller_init(void)
1051 int ret; 1061 int ret;
1052 1062
1053 pxa3xx_mfp_config(ARRAY_AND_SIZE(raumfeld_controller_pin_config)); 1063 pxa3xx_mfp_config(ARRAY_AND_SIZE(raumfeld_controller_pin_config));
1064
1065 gpiod_add_lookup_table(&raumfeld_rotary_gpios_table);
1066 device_add_property_set(&rotary_encoder_device.dev,
1067 &raumfeld_rotary_property_set);
1054 platform_device_register(&rotary_encoder_device); 1068 platform_device_register(&rotary_encoder_device);
1069
1055 spi_register_board_info(ARRAY_AND_SIZE(controller_spi_devices)); 1070 spi_register_board_info(ARRAY_AND_SIZE(controller_spi_devices));
1056 i2c_register_board_info(0, &raumfeld_controller_i2c_board_info, 1); 1071 i2c_register_board_info(0, &raumfeld_controller_i2c_board_info, 1);
1057 1072
@@ -1086,6 +1101,10 @@ static void __init __maybe_unused raumfeld_speaker_init(void)
1086 i2c_register_board_info(0, &raumfeld_connector_i2c_board_info, 1); 1101 i2c_register_board_info(0, &raumfeld_connector_i2c_board_info, 1);
1087 1102
1088 platform_device_register(&smc91x_device); 1103 platform_device_register(&smc91x_device);
1104
1105 gpiod_add_lookup_table(&raumfeld_rotary_gpios_table);
1106 device_add_property_set(&rotary_encoder_device.dev,
1107 &raumfeld_rotary_property_set);
1089 platform_device_register(&rotary_encoder_device); 1108 platform_device_register(&rotary_encoder_device);
1090 1109
1091 raumfeld_audio_init(); 1110 raumfeld_audio_init();
diff --git a/arch/blackfin/mach-bf527/boards/ezbrd.c b/arch/blackfin/mach-bf527/boards/ezbrd.c
index a3a572352769..80bcfd1d023e 100644
--- a/arch/blackfin/mach-bf527/boards/ezbrd.c
+++ b/arch/blackfin/mach-bf527/boards/ezbrd.c
@@ -279,7 +279,7 @@ static const struct ad7877_platform_data bfin_ad7877_ts_info = {
279#endif 279#endif
280 280
281#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879) 281#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879)
282#include <linux/spi/ad7879.h> 282#include <linux/platform_data/ad7879.h>
283static const struct ad7879_platform_data bfin_ad7879_ts_info = { 283static const struct ad7879_platform_data bfin_ad7879_ts_info = {
284 .model = 7879, /* Model = AD7879 */ 284 .model = 7879, /* Model = AD7879 */
285 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */ 285 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */
diff --git a/arch/blackfin/mach-bf527/boards/ezkit.c b/arch/blackfin/mach-bf527/boards/ezkit.c
index d4219e8e5ab8..571edfd2ecf3 100644
--- a/arch/blackfin/mach-bf527/boards/ezkit.c
+++ b/arch/blackfin/mach-bf527/boards/ezkit.c
@@ -477,7 +477,7 @@ static const struct ad7877_platform_data bfin_ad7877_ts_info = {
477#endif 477#endif
478 478
479#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879) 479#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879)
480#include <linux/spi/ad7879.h> 480#include <linux/platform_data/ad7879.h>
481static const struct ad7879_platform_data bfin_ad7879_ts_info = { 481static const struct ad7879_platform_data bfin_ad7879_ts_info = {
482 .model = 7879, /* Model = AD7879 */ 482 .model = 7879, /* Model = AD7879 */
483 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */ 483 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */
diff --git a/arch/blackfin/mach-bf527/boards/tll6527m.c b/arch/blackfin/mach-bf527/boards/tll6527m.c
index a0f5856a5ff8..c1acce4c2e45 100644
--- a/arch/blackfin/mach-bf527/boards/tll6527m.c
+++ b/arch/blackfin/mach-bf527/boards/tll6527m.c
@@ -29,7 +29,7 @@
29#include <asm/dpmc.h> 29#include <asm/dpmc.h>
30 30
31#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879) 31#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879)
32#include <linux/spi/ad7879.h> 32#include <linux/platform_data/ad7879.h>
33#define LCD_BACKLIGHT_GPIO 0x40 33#define LCD_BACKLIGHT_GPIO 0x40
34/* TLL6527M uses TLL7UIQ35 / ADI LCD EZ Extender. AD7879 AUX GPIO is used for 34/* TLL6527M uses TLL7UIQ35 / ADI LCD EZ Extender. AD7879 AUX GPIO is used for
35 * LCD Backlight Enable 35 * LCD Backlight Enable
diff --git a/arch/blackfin/mach-bf537/boards/stamp.c b/arch/blackfin/mach-bf537/boards/stamp.c
index c181543a399a..eaec7b4832a2 100644
--- a/arch/blackfin/mach-bf537/boards/stamp.c
+++ b/arch/blackfin/mach-bf537/boards/stamp.c
@@ -776,7 +776,7 @@ static const struct ad7877_platform_data bfin_ad7877_ts_info = {
776#endif 776#endif
777 777
778#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879) 778#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879)
779#include <linux/spi/ad7879.h> 779#include <linux/platform_data/ad7879.h>
780static const struct ad7879_platform_data bfin_ad7879_ts_info = { 780static const struct ad7879_platform_data bfin_ad7879_ts_info = {
781 .model = 7879, /* Model = AD7879 */ 781 .model = 7879, /* Model = AD7879 */
782 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */ 782 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */
diff --git a/arch/blackfin/mach-bf538/boards/ezkit.c b/arch/blackfin/mach-bf538/boards/ezkit.c
index c423af36fa91..1b6a52ad8a0e 100644
--- a/arch/blackfin/mach-bf538/boards/ezkit.c
+++ b/arch/blackfin/mach-bf538/boards/ezkit.c
@@ -521,7 +521,7 @@ static struct bfin5xx_spi_chip spi_flash_chip_info = {
521#endif /* CONFIG_SPI_BFIN5XX */ 521#endif /* CONFIG_SPI_BFIN5XX */
522 522
523#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879) 523#if IS_ENABLED(CONFIG_TOUCHSCREEN_AD7879)
524#include <linux/spi/ad7879.h> 524#include <linux/platform_data/ad7879.h>
525static const struct ad7879_platform_data bfin_ad7879_ts_info = { 525static const struct ad7879_platform_data bfin_ad7879_ts_info = {
526 .model = 7879, /* Model = AD7879 */ 526 .model = 7879, /* Model = AD7879 */
527 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */ 527 .x_plate_ohms = 620, /* 620 Ohm from the touch datasheet */
diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index a35532ec00e4..6261874c07c9 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -201,6 +201,8 @@ source "drivers/input/touchscreen/Kconfig"
201 201
202source "drivers/input/misc/Kconfig" 202source "drivers/input/misc/Kconfig"
203 203
204source "drivers/input/rmi4/Kconfig"
205
204endif 206endif
205 207
206menu "Hardware I/O ports" 208menu "Hardware I/O ports"
diff --git a/drivers/input/Makefile b/drivers/input/Makefile
index 0c9302ca9954..595820bbabe9 100644
--- a/drivers/input/Makefile
+++ b/drivers/input/Makefile
@@ -26,3 +26,5 @@ obj-$(CONFIG_INPUT_TOUCHSCREEN) += touchscreen/
26obj-$(CONFIG_INPUT_MISC) += misc/ 26obj-$(CONFIG_INPUT_MISC) += misc/
27 27
28obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o 28obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o
29
30obj-$(CONFIG_RMI4_CORE) += rmi4/
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index ddd8148d51d7..509608c95994 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -560,7 +560,7 @@ config KEYBOARD_SUNKBD
560 560
561config KEYBOARD_SH_KEYSC 561config KEYBOARD_SH_KEYSC
562 tristate "SuperH KEYSC keypad support" 562 tristate "SuperH KEYSC keypad support"
563 depends on SUPERH || ARCH_SHMOBILE || COMPILE_TEST 563 depends on ARCH_SHMOBILE || COMPILE_TEST
564 help 564 help
565 Say Y here if you want to use a keypad attached to the KEYSC block 565 Say Y here if you want to use a keypad attached to the KEYSC block
566 on SuperH processors such as sh7722 and sh7343. 566 on SuperH processors such as sh7722 and sh7343.
diff --git a/drivers/input/keyboard/goldfish_events.c b/drivers/input/keyboard/goldfish_events.c
index 907e4e278fce..f6e643b589b6 100644
--- a/drivers/input/keyboard/goldfish_events.c
+++ b/drivers/input/keyboard/goldfish_events.c
@@ -22,6 +22,7 @@
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/irq.h> 23#include <linux/irq.h>
24#include <linux/io.h> 24#include <linux/io.h>
25#include <linux/acpi.h>
25 26
26enum { 27enum {
27 REG_READ = 0x00, 28 REG_READ = 0x00,
@@ -178,10 +179,26 @@ static int events_probe(struct platform_device *pdev)
178 return 0; 179 return 0;
179} 180}
180 181
182static const struct of_device_id goldfish_events_of_match[] = {
183 { .compatible = "google,goldfish-events-keypad", },
184 {},
185};
186MODULE_DEVICE_TABLE(of, goldfish_events_of_match);
187
188#ifdef CONFIG_ACPI
189static const struct acpi_device_id goldfish_events_acpi_match[] = {
190 { "GFSH0002", 0 },
191 { },
192};
193MODULE_DEVICE_TABLE(acpi, goldfish_events_acpi_match);
194#endif
195
181static struct platform_driver events_driver = { 196static struct platform_driver events_driver = {
182 .probe = events_probe, 197 .probe = events_probe,
183 .driver = { 198 .driver = {
184 .name = "goldfish_events", 199 .name = "goldfish_events",
200 .of_match_table = goldfish_events_of_match,
201 .acpi_match_table = ACPI_PTR(goldfish_events_acpi_match),
185 }, 202 },
186}; 203};
187 204
diff --git a/drivers/input/keyboard/snvs_pwrkey.c b/drivers/input/keyboard/snvs_pwrkey.c
index 9adf13a5864a..24a9f599082f 100644
--- a/drivers/input/keyboard/snvs_pwrkey.c
+++ b/drivers/input/keyboard/snvs_pwrkey.c
@@ -111,9 +111,9 @@ static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
111 return -ENOMEM; 111 return -ENOMEM;
112 112
113 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap"); 113 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
114 if (!pdata->snvs) { 114 if (IS_ERR(pdata->snvs)) {
115 dev_err(&pdev->dev, "Can't get snvs syscon\n"); 115 dev_err(&pdev->dev, "Can't get snvs syscon\n");
116 return -ENODEV; 116 return PTR_ERR(pdata->snvs);
117 } 117 }
118 118
119 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) { 119 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
@@ -180,7 +180,7 @@ static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
180 return 0; 180 return 0;
181} 181}
182 182
183static int imx_snvs_pwrkey_suspend(struct device *dev) 183static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev)
184{ 184{
185 struct platform_device *pdev = to_platform_device(dev); 185 struct platform_device *pdev = to_platform_device(dev);
186 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 186 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
@@ -191,7 +191,7 @@ static int imx_snvs_pwrkey_suspend(struct device *dev)
191 return 0; 191 return 0;
192} 192}
193 193
194static int imx_snvs_pwrkey_resume(struct device *dev) 194static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev)
195{ 195{
196 struct platform_device *pdev = to_platform_device(dev); 196 struct platform_device *pdev = to_platform_device(dev);
197 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 197 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
diff --git a/drivers/input/keyboard/spear-keyboard.c b/drivers/input/keyboard/spear-keyboard.c
index 623d451767e3..8083eaa0524a 100644
--- a/drivers/input/keyboard/spear-keyboard.c
+++ b/drivers/input/keyboard/spear-keyboard.c
@@ -288,8 +288,7 @@ static int spear_kbd_remove(struct platform_device *pdev)
288 return 0; 288 return 0;
289} 289}
290 290
291#ifdef CONFIG_PM 291static int __maybe_unused spear_kbd_suspend(struct device *dev)
292static int spear_kbd_suspend(struct device *dev)
293{ 292{
294 struct platform_device *pdev = to_platform_device(dev); 293 struct platform_device *pdev = to_platform_device(dev);
295 struct spear_kbd *kbd = platform_get_drvdata(pdev); 294 struct spear_kbd *kbd = platform_get_drvdata(pdev);
@@ -342,7 +341,7 @@ static int spear_kbd_suspend(struct device *dev)
342 return 0; 341 return 0;
343} 342}
344 343
345static int spear_kbd_resume(struct device *dev) 344static int __maybe_unused spear_kbd_resume(struct device *dev)
346{ 345{
347 struct platform_device *pdev = to_platform_device(dev); 346 struct platform_device *pdev = to_platform_device(dev);
348 struct spear_kbd *kbd = platform_get_drvdata(pdev); 347 struct spear_kbd *kbd = platform_get_drvdata(pdev);
@@ -368,7 +367,6 @@ static int spear_kbd_resume(struct device *dev)
368 367
369 return 0; 368 return 0;
370} 369}
371#endif
372 370
373static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops, spear_kbd_suspend, spear_kbd_resume); 371static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops, spear_kbd_suspend, spear_kbd_resume);
374 372
diff --git a/drivers/input/misc/powermate.c b/drivers/input/misc/powermate.c
index 63b539d3daba..84909a12ff36 100644
--- a/drivers/input/misc/powermate.c
+++ b/drivers/input/misc/powermate.c
@@ -307,6 +307,9 @@ static int powermate_probe(struct usb_interface *intf, const struct usb_device_i
307 int error = -ENOMEM; 307 int error = -ENOMEM;
308 308
309 interface = intf->cur_altsetting; 309 interface = intf->cur_altsetting;
310 if (interface->desc.bNumEndpoints < 1)
311 return -EINVAL;
312
310 endpoint = &interface->endpoint[0].desc; 313 endpoint = &interface->endpoint[0].desc;
311 if (!usb_endpoint_is_int_in(endpoint)) 314 if (!usb_endpoint_is_int_in(endpoint))
312 return -EIO; 315 return -EIO;
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index 8aee71986430..96c486de49e0 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -20,70 +20,78 @@
20#include <linux/input.h> 20#include <linux/input.h>
21#include <linux/device.h> 21#include <linux/device.h>
22#include <linux/platform_device.h> 22#include <linux/platform_device.h>
23#include <linux/gpio.h> 23#include <linux/gpio/consumer.h>
24#include <linux/rotary_encoder.h>
25#include <linux/slab.h> 24#include <linux/slab.h>
26#include <linux/of.h> 25#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/of_gpio.h>
29#include <linux/pm.h> 26#include <linux/pm.h>
27#include <linux/property.h>
30 28
31#define DRV_NAME "rotary-encoder" 29#define DRV_NAME "rotary-encoder"
32 30
33struct rotary_encoder { 31struct rotary_encoder {
34 struct input_dev *input; 32 struct input_dev *input;
35 const struct rotary_encoder_platform_data *pdata;
36 33
37 unsigned int axis; 34 struct mutex access_mutex;
35
36 u32 steps;
37 u32 axis;
38 bool relative_axis;
39 bool rollover;
40
38 unsigned int pos; 41 unsigned int pos;
39 42
40 unsigned int irq_a; 43 struct gpio_descs *gpios;
41 unsigned int irq_b; 44
45 unsigned int *irq;
42 46
43 bool armed; 47 bool armed;
44 unsigned char dir; /* 0 - clockwise, 1 - CCW */ 48 signed char dir; /* 1 - clockwise, -1 - CCW */
45 49
46 char last_stable; 50 unsigned last_stable;
47}; 51};
48 52
49static int rotary_encoder_get_state(const struct rotary_encoder_platform_data *pdata) 53static unsigned rotary_encoder_get_state(struct rotary_encoder *encoder)
50{ 54{
51 int a = !!gpio_get_value(pdata->gpio_a); 55 int i;
52 int b = !!gpio_get_value(pdata->gpio_b); 56 unsigned ret = 0;
53 57
54 a ^= pdata->inverted_a; 58 for (i = 0; i < encoder->gpios->ndescs; ++i) {
55 b ^= pdata->inverted_b; 59 int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
60 /* convert from gray encoding to normal */
61 if (ret & 1)
62 val = !val;
56 63
57 return ((a << 1) | b); 64 ret = ret << 1 | val;
65 }
66
67 return ret & 3;
58} 68}
59 69
60static void rotary_encoder_report_event(struct rotary_encoder *encoder) 70static void rotary_encoder_report_event(struct rotary_encoder *encoder)
61{ 71{
62 const struct rotary_encoder_platform_data *pdata = encoder->pdata; 72 if (encoder->relative_axis) {
63
64 if (pdata->relative_axis) {
65 input_report_rel(encoder->input, 73 input_report_rel(encoder->input,
66 pdata->axis, encoder->dir ? -1 : 1); 74 encoder->axis, encoder->dir);
67 } else { 75 } else {
68 unsigned int pos = encoder->pos; 76 unsigned int pos = encoder->pos;
69 77
70 if (encoder->dir) { 78 if (encoder->dir < 0) {
71 /* turning counter-clockwise */ 79 /* turning counter-clockwise */
72 if (pdata->rollover) 80 if (encoder->rollover)
73 pos += pdata->steps; 81 pos += encoder->steps;
74 if (pos) 82 if (pos)
75 pos--; 83 pos--;
76 } else { 84 } else {
77 /* turning clockwise */ 85 /* turning clockwise */
78 if (pdata->rollover || pos < pdata->steps) 86 if (encoder->rollover || pos < encoder->steps)
79 pos++; 87 pos++;
80 } 88 }
81 89
82 if (pdata->rollover) 90 if (encoder->rollover)
83 pos %= pdata->steps; 91 pos %= encoder->steps;
84 92
85 encoder->pos = pos; 93 encoder->pos = pos;
86 input_report_abs(encoder->input, pdata->axis, encoder->pos); 94 input_report_abs(encoder->input, encoder->axis, encoder->pos);
87 } 95 }
88 96
89 input_sync(encoder->input); 97 input_sync(encoder->input);
@@ -92,9 +100,11 @@ static void rotary_encoder_report_event(struct rotary_encoder *encoder)
92static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) 100static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
93{ 101{
94 struct rotary_encoder *encoder = dev_id; 102 struct rotary_encoder *encoder = dev_id;
95 int state; 103 unsigned state;
96 104
97 state = rotary_encoder_get_state(encoder->pdata); 105 mutex_lock(&encoder->access_mutex);
106
107 state = rotary_encoder_get_state(encoder);
98 108
99 switch (state) { 109 switch (state) {
100 case 0x0: 110 case 0x0:
@@ -105,334 +115,227 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
105 break; 115 break;
106 116
107 case 0x1: 117 case 0x1:
108 case 0x2: 118 case 0x3:
109 if (encoder->armed) 119 if (encoder->armed)
110 encoder->dir = state - 1; 120 encoder->dir = 2 - state;
111 break; 121 break;
112 122
113 case 0x3: 123 case 0x2:
114 encoder->armed = true; 124 encoder->armed = true;
115 break; 125 break;
116 } 126 }
117 127
128 mutex_unlock(&encoder->access_mutex);
129
118 return IRQ_HANDLED; 130 return IRQ_HANDLED;
119} 131}
120 132
121static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id) 133static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
122{ 134{
123 struct rotary_encoder *encoder = dev_id; 135 struct rotary_encoder *encoder = dev_id;
124 int state; 136 unsigned int state;
125 137
126 state = rotary_encoder_get_state(encoder->pdata); 138 mutex_lock(&encoder->access_mutex);
127 139
128 switch (state) { 140 state = rotary_encoder_get_state(encoder);
129 case 0x00: 141
130 case 0x03: 142 if (state & 1) {
143 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1;
144 } else {
131 if (state != encoder->last_stable) { 145 if (state != encoder->last_stable) {
132 rotary_encoder_report_event(encoder); 146 rotary_encoder_report_event(encoder);
133 encoder->last_stable = state; 147 encoder->last_stable = state;
134 } 148 }
135 break;
136
137 case 0x01:
138 case 0x02:
139 encoder->dir = (encoder->last_stable + state) & 0x01;
140 break;
141 } 149 }
142 150
151 mutex_unlock(&encoder->access_mutex);
152
143 return IRQ_HANDLED; 153 return IRQ_HANDLED;
144} 154}
145 155
146static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id) 156static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
147{ 157{
148 struct rotary_encoder *encoder = dev_id; 158 struct rotary_encoder *encoder = dev_id;
149 unsigned char sum; 159 unsigned int state;
150 int state;
151
152 state = rotary_encoder_get_state(encoder->pdata);
153
154 /*
155 * We encode the previous and the current state using a byte.
156 * The previous state in the MSB nibble, the current state in the LSB
157 * nibble. Then use a table to decide the direction of the turn.
158 */
159 sum = (encoder->last_stable << 4) + state;
160 switch (sum) {
161 case 0x31:
162 case 0x10:
163 case 0x02:
164 case 0x23:
165 encoder->dir = 0; /* clockwise */
166 break;
167 160
168 case 0x13: 161 mutex_lock(&encoder->access_mutex);
169 case 0x01:
170 case 0x20:
171 case 0x32:
172 encoder->dir = 1; /* counter-clockwise */
173 break;
174 162
175 default: 163 state = rotary_encoder_get_state(encoder);
176 /* 164
177 * Ignore all other values. This covers the case when the 165 if ((encoder->last_stable + 1) % 4 == state)
178 * state didn't change (a spurious interrupt) and the 166 encoder->dir = 1;
179 * cases where the state changed by two steps, making it 167 else if (encoder->last_stable == (state + 1) % 4)
180 * impossible to tell the direction. 168 encoder->dir = -1;
181 * 169 else
182 * In either case, don't report any event and save the
183 * state for later.
184 */
185 goto out; 170 goto out;
186 }
187 171
188 rotary_encoder_report_event(encoder); 172 rotary_encoder_report_event(encoder);
189 173
190out: 174out:
191 encoder->last_stable = state; 175 encoder->last_stable = state;
176 mutex_unlock(&encoder->access_mutex);
177
192 return IRQ_HANDLED; 178 return IRQ_HANDLED;
193} 179}
194 180
195#ifdef CONFIG_OF 181static int rotary_encoder_probe(struct platform_device *pdev)
196static const struct of_device_id rotary_encoder_of_match[] = {
197 { .compatible = "rotary-encoder", },
198 { },
199};
200MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
201
202static struct rotary_encoder_platform_data *rotary_encoder_parse_dt(struct device *dev)
203{ 182{
204 const struct of_device_id *of_id = 183 struct device *dev = &pdev->dev;
205 of_match_device(rotary_encoder_of_match, dev); 184 struct rotary_encoder *encoder;
206 struct device_node *np = dev->of_node; 185 struct input_dev *input;
207 struct rotary_encoder_platform_data *pdata; 186 irq_handler_t handler;
208 enum of_gpio_flags flags; 187 u32 steps_per_period;
209 int error; 188 unsigned int i;
210 189 int err;
211 if (!of_id || !np)
212 return NULL;
213
214 pdata = kzalloc(sizeof(struct rotary_encoder_platform_data),
215 GFP_KERNEL);
216 if (!pdata)
217 return ERR_PTR(-ENOMEM);
218
219 of_property_read_u32(np, "rotary-encoder,steps", &pdata->steps);
220 of_property_read_u32(np, "linux,axis", &pdata->axis);
221 190
222 pdata->gpio_a = of_get_gpio_flags(np, 0, &flags); 191 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
223 pdata->inverted_a = flags & OF_GPIO_ACTIVE_LOW; 192 if (!encoder)
193 return -ENOMEM;
224 194
225 pdata->gpio_b = of_get_gpio_flags(np, 1, &flags); 195 mutex_init(&encoder->access_mutex);
226 pdata->inverted_b = flags & OF_GPIO_ACTIVE_LOW;
227 196
228 pdata->relative_axis = 197 device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
229 of_property_read_bool(np, "rotary-encoder,relative-axis");
230 pdata->rollover = of_property_read_bool(np, "rotary-encoder,rollover");
231 198
232 error = of_property_read_u32(np, "rotary-encoder,steps-per-period", 199 err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
233 &pdata->steps_per_period); 200 &steps_per_period);
234 if (error) { 201 if (err) {
235 /* 202 /*
236 * The 'half-period' property has been deprecated, you must use 203 * The 'half-period' property has been deprecated, you must
237 * 'steps-per-period' and set an appropriate value, but we still 204 * use 'steps-per-period' and set an appropriate value, but
238 * need to parse it to maintain compatibility. 205 * we still need to parse it to maintain compatibility. If
206 * neither property is present we fall back to the one step
207 * per period behavior.
239 */ 208 */
240 if (of_property_read_bool(np, "rotary-encoder,half-period")) { 209 steps_per_period = device_property_read_bool(dev,
241 pdata->steps_per_period = 2; 210 "rotary-encoder,half-period") ? 2 : 1;
242 } else {
243 /* Fallback to one step per period behavior */
244 pdata->steps_per_period = 1;
245 }
246 } 211 }
247 212
248 pdata->wakeup_source = of_property_read_bool(np, "wakeup-source"); 213 encoder->rollover =
214 device_property_read_bool(dev, "rotary-encoder,rollover");
249 215
250 return pdata; 216 device_property_read_u32(dev, "linux,axis", &encoder->axis);
251} 217 encoder->relative_axis =
252#else 218 device_property_read_bool(dev, "rotary-encoder,relative-axis");
253static inline struct rotary_encoder_platform_data *
254rotary_encoder_parse_dt(struct device *dev)
255{
256 return NULL;
257}
258#endif
259
260static int rotary_encoder_probe(struct platform_device *pdev)
261{
262 struct device *dev = &pdev->dev;
263 const struct rotary_encoder_platform_data *pdata = dev_get_platdata(dev);
264 struct rotary_encoder *encoder;
265 struct input_dev *input;
266 irq_handler_t handler;
267 int err;
268
269 if (!pdata) {
270 pdata = rotary_encoder_parse_dt(dev);
271 if (IS_ERR(pdata))
272 return PTR_ERR(pdata);
273 219
274 if (!pdata) { 220 encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
275 dev_err(dev, "missing platform data\n"); 221 if (IS_ERR(encoder->gpios)) {
276 return -EINVAL; 222 dev_err(dev, "unable to get gpios\n");
277 } 223 return PTR_ERR(encoder->gpios);
278 } 224 }
279 225 if (encoder->gpios->ndescs < 2) {
280 encoder = kzalloc(sizeof(struct rotary_encoder), GFP_KERNEL); 226 dev_err(dev, "not enough gpios found\n");
281 input = input_allocate_device(); 227 return -EINVAL;
282 if (!encoder || !input) {
283 err = -ENOMEM;
284 goto exit_free_mem;
285 } 228 }
286 229
230 input = devm_input_allocate_device(dev);
231 if (!input)
232 return -ENOMEM;
233
287 encoder->input = input; 234 encoder->input = input;
288 encoder->pdata = pdata;
289 235
290 input->name = pdev->name; 236 input->name = pdev->name;
291 input->id.bustype = BUS_HOST; 237 input->id.bustype = BUS_HOST;
292 input->dev.parent = dev; 238 input->dev.parent = dev;
293 239
294 if (pdata->relative_axis) { 240 if (encoder->relative_axis)
295 input->evbit[0] = BIT_MASK(EV_REL); 241 input_set_capability(input, EV_REL, encoder->axis);
296 input->relbit[0] = BIT_MASK(pdata->axis); 242 else
297 } else { 243 input_set_abs_params(input,
298 input->evbit[0] = BIT_MASK(EV_ABS); 244 encoder->axis, 0, encoder->steps, 0, 1);
299 input_set_abs_params(encoder->input,
300 pdata->axis, 0, pdata->steps, 0, 1);
301 }
302
303 /* request the GPIOs */
304 err = gpio_request_one(pdata->gpio_a, GPIOF_IN, dev_name(dev));
305 if (err) {
306 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
307 goto exit_free_mem;
308 }
309
310 err = gpio_request_one(pdata->gpio_b, GPIOF_IN, dev_name(dev));
311 if (err) {
312 dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
313 goto exit_free_gpio_a;
314 }
315
316 encoder->irq_a = gpio_to_irq(pdata->gpio_a);
317 encoder->irq_b = gpio_to_irq(pdata->gpio_b);
318 245
319 switch (pdata->steps_per_period) { 246 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
320 case 4: 247 case 4:
321 handler = &rotary_encoder_quarter_period_irq; 248 handler = &rotary_encoder_quarter_period_irq;
322 encoder->last_stable = rotary_encoder_get_state(pdata); 249 encoder->last_stable = rotary_encoder_get_state(encoder);
323 break; 250 break;
324 case 2: 251 case 2:
325 handler = &rotary_encoder_half_period_irq; 252 handler = &rotary_encoder_half_period_irq;
326 encoder->last_stable = rotary_encoder_get_state(pdata); 253 encoder->last_stable = rotary_encoder_get_state(encoder);
327 break; 254 break;
328 case 1: 255 case 1:
329 handler = &rotary_encoder_irq; 256 handler = &rotary_encoder_irq;
330 break; 257 break;
331 default: 258 default:
332 dev_err(dev, "'%d' is not a valid steps-per-period value\n", 259 dev_err(dev, "'%d' is not a valid steps-per-period value\n",
333 pdata->steps_per_period); 260 steps_per_period);
334 err = -EINVAL; 261 return -EINVAL;
335 goto exit_free_gpio_b;
336 } 262 }
337 263
338 err = request_irq(encoder->irq_a, handler, 264 encoder->irq =
339 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 265 devm_kzalloc(dev,
340 DRV_NAME, encoder); 266 sizeof(*encoder->irq) * encoder->gpios->ndescs,
341 if (err) { 267 GFP_KERNEL);
342 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a); 268 if (!encoder->irq)
343 goto exit_free_gpio_b; 269 return -ENOMEM;
344 } 270
345 271 for (i = 0; i < encoder->gpios->ndescs; ++i) {
346 err = request_irq(encoder->irq_b, handler, 272 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
347 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 273
348 DRV_NAME, encoder); 274 err = devm_request_threaded_irq(dev, encoder->irq[i],
349 if (err) { 275 NULL, handler,
350 dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b); 276 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
351 goto exit_free_irq_a; 277 IRQF_ONESHOT,
278 DRV_NAME, encoder);
279 if (err) {
280 dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
281 encoder->irq[i], i);
282 return err;
283 }
352 } 284 }
353 285
354 err = input_register_device(input); 286 err = input_register_device(input);
355 if (err) { 287 if (err) {
356 dev_err(dev, "failed to register input device\n"); 288 dev_err(dev, "failed to register input device\n");
357 goto exit_free_irq_b; 289 return err;
358 } 290 }
359 291
360 device_init_wakeup(&pdev->dev, pdata->wakeup_source); 292 device_init_wakeup(dev,
293 device_property_read_bool(dev, "wakeup-source"));
361 294
362 platform_set_drvdata(pdev, encoder); 295 platform_set_drvdata(pdev, encoder);
363 296
364 return 0; 297 return 0;
365
366exit_free_irq_b:
367 free_irq(encoder->irq_b, encoder);
368exit_free_irq_a:
369 free_irq(encoder->irq_a, encoder);
370exit_free_gpio_b:
371 gpio_free(pdata->gpio_b);
372exit_free_gpio_a:
373 gpio_free(pdata->gpio_a);
374exit_free_mem:
375 input_free_device(input);
376 kfree(encoder);
377 if (!dev_get_platdata(&pdev->dev))
378 kfree(pdata);
379
380 return err;
381} 298}
382 299
383static int rotary_encoder_remove(struct platform_device *pdev) 300static int __maybe_unused rotary_encoder_suspend(struct device *dev)
384{
385 struct rotary_encoder *encoder = platform_get_drvdata(pdev);
386 const struct rotary_encoder_platform_data *pdata = encoder->pdata;
387
388 device_init_wakeup(&pdev->dev, false);
389
390 free_irq(encoder->irq_a, encoder);
391 free_irq(encoder->irq_b, encoder);
392 gpio_free(pdata->gpio_a);
393 gpio_free(pdata->gpio_b);
394
395 input_unregister_device(encoder->input);
396 kfree(encoder);
397
398 if (!dev_get_platdata(&pdev->dev))
399 kfree(pdata);
400
401 return 0;
402}
403
404#ifdef CONFIG_PM_SLEEP
405static int rotary_encoder_suspend(struct device *dev)
406{ 301{
407 struct rotary_encoder *encoder = dev_get_drvdata(dev); 302 struct rotary_encoder *encoder = dev_get_drvdata(dev);
303 unsigned int i;
408 304
409 if (device_may_wakeup(dev)) { 305 if (device_may_wakeup(dev)) {
410 enable_irq_wake(encoder->irq_a); 306 for (i = 0; i < encoder->gpios->ndescs; ++i)
411 enable_irq_wake(encoder->irq_b); 307 enable_irq_wake(encoder->irq[i]);
412 } 308 }
413 309
414 return 0; 310 return 0;
415} 311}
416 312
417static int rotary_encoder_resume(struct device *dev) 313static int __maybe_unused rotary_encoder_resume(struct device *dev)
418{ 314{
419 struct rotary_encoder *encoder = dev_get_drvdata(dev); 315 struct rotary_encoder *encoder = dev_get_drvdata(dev);
316 unsigned int i;
420 317
421 if (device_may_wakeup(dev)) { 318 if (device_may_wakeup(dev)) {
422 disable_irq_wake(encoder->irq_a); 319 for (i = 0; i < encoder->gpios->ndescs; ++i)
423 disable_irq_wake(encoder->irq_b); 320 disable_irq_wake(encoder->irq[i]);
424 } 321 }
425 322
426 return 0; 323 return 0;
427} 324}
428#endif
429 325
430static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops, 326static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
431 rotary_encoder_suspend, rotary_encoder_resume); 327 rotary_encoder_suspend, rotary_encoder_resume);
328
329#ifdef CONFIG_OF
330static const struct of_device_id rotary_encoder_of_match[] = {
331 { .compatible = "rotary-encoder", },
332 { },
333};
334MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
335#endif
432 336
433static struct platform_driver rotary_encoder_driver = { 337static struct platform_driver rotary_encoder_driver = {
434 .probe = rotary_encoder_probe, 338 .probe = rotary_encoder_probe,
435 .remove = rotary_encoder_remove,
436 .driver = { 339 .driver = {
437 .name = DRV_NAME, 340 .name = DRV_NAME,
438 .pm = &rotary_encoder_pm_ops, 341 .pm = &rotary_encoder_pm_ops,
diff --git a/drivers/input/mouse/Kconfig b/drivers/input/mouse/Kconfig
index 17f97e5e11e7..096abb4ad5cd 100644
--- a/drivers/input/mouse/Kconfig
+++ b/drivers/input/mouse/Kconfig
@@ -48,6 +48,16 @@ config MOUSE_PS2_ALPS
48 48
49 If unsure, say Y. 49 If unsure, say Y.
50 50
51config MOUSE_PS2_BYD
52 bool "BYD PS/2 mouse protocol extension" if EXPERT
53 default y
54 depends on MOUSE_PS2
55 help
56 Say Y here if you have a BYD PS/2 touchpad connected to
57 your system.
58
59 If unsure, say Y.
60
51config MOUSE_PS2_LOGIPS2PP 61config MOUSE_PS2_LOGIPS2PP
52 bool "Logitech PS/2++ mouse protocol extension" if EXPERT 62 bool "Logitech PS/2++ mouse protocol extension" if EXPERT
53 default y 63 default y
diff --git a/drivers/input/mouse/Makefile b/drivers/input/mouse/Makefile
index ee6a6e9563d4..6168b134937b 100644
--- a/drivers/input/mouse/Makefile
+++ b/drivers/input/mouse/Makefile
@@ -28,6 +28,7 @@ cyapatp-objs := cyapa.o cyapa_gen3.o cyapa_gen5.o cyapa_gen6.o
28psmouse-objs := psmouse-base.o synaptics.o focaltech.o 28psmouse-objs := psmouse-base.o synaptics.o focaltech.o
29 29
30psmouse-$(CONFIG_MOUSE_PS2_ALPS) += alps.o 30psmouse-$(CONFIG_MOUSE_PS2_ALPS) += alps.o
31psmouse-$(CONFIG_MOUSE_PS2_BYD) += byd.o
31psmouse-$(CONFIG_MOUSE_PS2_ELANTECH) += elantech.o 32psmouse-$(CONFIG_MOUSE_PS2_ELANTECH) += elantech.o
32psmouse-$(CONFIG_MOUSE_PS2_OLPC) += hgpk.o 33psmouse-$(CONFIG_MOUSE_PS2_OLPC) += hgpk.o
33psmouse-$(CONFIG_MOUSE_PS2_LOGIPS2PP) += logips2pp.o 34psmouse-$(CONFIG_MOUSE_PS2_LOGIPS2PP) += logips2pp.o
diff --git a/drivers/input/mouse/byd.c b/drivers/input/mouse/byd.c
new file mode 100644
index 000000000000..9425e0f6c5ce
--- /dev/null
+++ b/drivers/input/mouse/byd.c
@@ -0,0 +1,337 @@
1/*
2 * BYD TouchPad PS/2 mouse driver
3 *
4 * Copyright (C) 2015 Chris Diamand <chris@diamand.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/delay.h>
12#include <linux/input.h>
13#include <linux/libps2.h>
14#include <linux/serio.h>
15
16#include "psmouse.h"
17#include "byd.h"
18
19#define PS2_Y_OVERFLOW BIT_MASK(7)
20#define PS2_X_OVERFLOW BIT_MASK(6)
21#define PS2_Y_SIGN BIT_MASK(5)
22#define PS2_X_SIGN BIT_MASK(4)
23#define PS2_ALWAYS_1 BIT_MASK(3)
24#define PS2_MIDDLE BIT_MASK(2)
25#define PS2_RIGHT BIT_MASK(1)
26#define PS2_LEFT BIT_MASK(0)
27
28/*
29 * The touchpad reports gestures in the last byte of each packet. It can take
30 * any of the following values:
31 */
32
33/* One-finger scrolling in one of the edge scroll zones. */
34#define BYD_SCROLLUP 0xCA
35#define BYD_SCROLLDOWN 0x36
36#define BYD_SCROLLLEFT 0xCB
37#define BYD_SCROLLRIGHT 0x35
38/* Two-finger scrolling. */
39#define BYD_2DOWN 0x2B
40#define BYD_2UP 0xD5
41#define BYD_2LEFT 0xD6
42#define BYD_2RIGHT 0x2A
43/* Pinching in or out. */
44#define BYD_ZOOMOUT 0xD8
45#define BYD_ZOOMIN 0x28
46/* Three-finger swipe. */
47#define BYD_3UP 0xD3
48#define BYD_3DOWN 0x2D
49#define BYD_3LEFT 0xD4
50#define BYD_3RIGHT 0x2C
51/* Four-finger swipe. */
52#define BYD_4UP 0xCD
53#define BYD_4DOWN 0x33
54
55int byd_detect(struct psmouse *psmouse, bool set_properties)
56{
57 struct ps2dev *ps2dev = &psmouse->ps2dev;
58 unsigned char param[4];
59
60 param[0] = 0x03;
61 param[1] = 0x00;
62 param[2] = 0x00;
63 param[3] = 0x00;
64
65 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
66 return -1;
67 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
68 return -1;
69 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
70 return -1;
71 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
72 return -1;
73 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
74 return -1;
75
76 if (param[1] != 0x03 || param[2] != 0x64)
77 return -ENODEV;
78
79 psmouse_dbg(psmouse, "BYD touchpad detected\n");
80
81 if (set_properties) {
82 psmouse->vendor = "BYD";
83 psmouse->name = "TouchPad";
84 }
85
86 return 0;
87}
88
89static psmouse_ret_t byd_process_byte(struct psmouse *psmouse)
90{
91 struct input_dev *dev = psmouse->dev;
92 u8 *pkt = psmouse->packet;
93
94 if (psmouse->pktcnt > 0 && !(pkt[0] & PS2_ALWAYS_1)) {
95 psmouse_warn(psmouse, "Always_1 bit not 1. pkt[0] = %02x\n",
96 pkt[0]);
97 return PSMOUSE_BAD_DATA;
98 }
99
100 if (psmouse->pktcnt < psmouse->pktsize)
101 return PSMOUSE_GOOD_DATA;
102
103 /* Otherwise, a full packet has been received */
104 switch (pkt[3]) {
105 case 0: {
106 /* Standard packet */
107 /* Sign-extend if a sign bit is set. */
108 unsigned int signx = pkt[0] & PS2_X_SIGN ? ~0xFF : 0;
109 unsigned int signy = pkt[0] & PS2_Y_SIGN ? ~0xFF : 0;
110 int dx = signx | (int) pkt[1];
111 int dy = signy | (int) pkt[2];
112
113 input_report_rel(psmouse->dev, REL_X, dx);
114 input_report_rel(psmouse->dev, REL_Y, -dy);
115
116 input_report_key(psmouse->dev, BTN_LEFT, pkt[0] & PS2_LEFT);
117 input_report_key(psmouse->dev, BTN_RIGHT, pkt[0] & PS2_RIGHT);
118 input_report_key(psmouse->dev, BTN_MIDDLE, pkt[0] & PS2_MIDDLE);
119 break;
120 }
121
122 case BYD_SCROLLDOWN:
123 case BYD_2DOWN:
124 input_report_rel(dev, REL_WHEEL, -1);
125 break;
126
127 case BYD_SCROLLUP:
128 case BYD_2UP:
129 input_report_rel(dev, REL_WHEEL, 1);
130 break;
131
132 case BYD_SCROLLLEFT:
133 case BYD_2LEFT:
134 input_report_rel(dev, REL_HWHEEL, -1);
135 break;
136
137 case BYD_SCROLLRIGHT:
138 case BYD_2RIGHT:
139 input_report_rel(dev, REL_HWHEEL, 1);
140 break;
141
142 case BYD_ZOOMOUT:
143 case BYD_ZOOMIN:
144 case BYD_3UP:
145 case BYD_3DOWN:
146 case BYD_3LEFT:
147 case BYD_3RIGHT:
148 case BYD_4UP:
149 case BYD_4DOWN:
150 break;
151
152 default:
153 psmouse_warn(psmouse,
154 "Unrecognized Z: pkt = %02x %02x %02x %02x\n",
155 psmouse->packet[0], psmouse->packet[1],
156 psmouse->packet[2], psmouse->packet[3]);
157 return PSMOUSE_BAD_DATA;
158 }
159
160 input_sync(dev);
161
162 return PSMOUSE_FULL_PACKET;
163}
164
165/* Send a sequence of bytes, where each is ACKed before the next is sent. */
166static int byd_send_sequence(struct psmouse *psmouse, const u8 *seq, size_t len)
167{
168 unsigned int i;
169
170 for (i = 0; i < len; ++i) {
171 if (ps2_command(&psmouse->ps2dev, NULL, seq[i]))
172 return -1;
173 }
174 return 0;
175}
176
177/* Keep scrolling after fingers are removed. */
178#define SCROLL_INERTIAL 0x01
179#define SCROLL_NO_INERTIAL 0x02
180
181/* Clicking can be done by tapping or pressing. */
182#define CLICK_BOTH 0x01
183/* Clicking can only be done by pressing. */
184#define CLICK_PRESS_ONLY 0x02
185
186static int byd_enable(struct psmouse *psmouse)
187{
188 const u8 seq1[] = { 0xE2, 0x00, 0xE0, 0x02, 0xE0 };
189 const u8 seq2[] = {
190 0xD3, 0x01,
191 0xD0, 0x00,
192 0xD0, 0x04,
193 /* Whether clicking is done by tapping or pressing. */
194 0xD4, CLICK_PRESS_ONLY,
195 0xD5, 0x01,
196 0xD7, 0x03,
197 /* Vertical and horizontal one-finger scroll zone inertia. */
198 0xD8, SCROLL_INERTIAL,
199 0xDA, 0x05,
200 0xDB, 0x02,
201 0xE4, 0x05,
202 0xD6, 0x01,
203 0xDE, 0x04,
204 0xE3, 0x01,
205 0xCF, 0x00,
206 0xD2, 0x03,
207 /* Vertical and horizontal two-finger scrolling inertia. */
208 0xE5, SCROLL_INERTIAL,
209 0xD9, 0x02,
210 0xD9, 0x07,
211 0xDC, 0x03,
212 0xDD, 0x03,
213 0xDF, 0x03,
214 0xE1, 0x03,
215 0xD1, 0x00,
216 0xCE, 0x00,
217 0xCC, 0x00,
218 0xE0, 0x00,
219 0xE2, 0x01
220 };
221 u8 param[4];
222
223 if (byd_send_sequence(psmouse, seq1, ARRAY_SIZE(seq1)))
224 return -1;
225
226 /* Send a 0x01 command, which should return 4 bytes. */
227 if (ps2_command(&psmouse->ps2dev, param, 0x0401))
228 return -1;
229
230 if (byd_send_sequence(psmouse, seq2, ARRAY_SIZE(seq2)))
231 return -1;
232
233 return 0;
234}
235
236/*
237 * Send the set of PS/2 commands required to make it identify as an
238 * intellimouse with 4-byte instead of 3-byte packets.
239 */
240static int byd_send_intellimouse_sequence(struct psmouse *psmouse)
241{
242 struct ps2dev *ps2dev = &psmouse->ps2dev;
243 u8 param[4];
244 int i;
245 const struct {
246 u16 command;
247 u8 arg;
248 } seq[] = {
249 { PSMOUSE_CMD_RESET_BAT, 0 },
250 { PSMOUSE_CMD_RESET_BAT, 0 },
251 { PSMOUSE_CMD_GETID, 0 },
252 { PSMOUSE_CMD_SETSCALE11, 0 },
253 { PSMOUSE_CMD_SETSCALE11, 0 },
254 { PSMOUSE_CMD_SETSCALE11, 0 },
255 { PSMOUSE_CMD_GETINFO, 0 },
256 { PSMOUSE_CMD_SETRES, 0x03 },
257 { PSMOUSE_CMD_SETRATE, 0xC8 },
258 { PSMOUSE_CMD_SETRATE, 0x64 },
259 { PSMOUSE_CMD_SETRATE, 0x50 },
260 { PSMOUSE_CMD_GETID, 0 },
261 { PSMOUSE_CMD_SETRATE, 0xC8 },
262 { PSMOUSE_CMD_SETRATE, 0xC8 },
263 { PSMOUSE_CMD_SETRATE, 0x50 },
264 { PSMOUSE_CMD_GETID, 0 },
265 { PSMOUSE_CMD_SETRATE, 0x64 },
266 { PSMOUSE_CMD_SETRES, 0x03 },
267 { PSMOUSE_CMD_ENABLE, 0 }
268 };
269
270 memset(param, 0, sizeof(param));
271 for (i = 0; i < ARRAY_SIZE(seq); ++i) {
272 param[0] = seq[i].arg;
273 if (ps2_command(ps2dev, param, seq[i].command))
274 return -1;
275 }
276
277 return 0;
278}
279
280static int byd_reset_touchpad(struct psmouse *psmouse)
281{
282 if (byd_send_intellimouse_sequence(psmouse))
283 return -EIO;
284
285 if (byd_enable(psmouse))
286 return -EIO;
287
288 return 0;
289}
290
291static int byd_reconnect(struct psmouse *psmouse)
292{
293 int retry = 0, error = 0;
294
295 psmouse_dbg(psmouse, "Reconnect\n");
296 do {
297 psmouse_reset(psmouse);
298 if (retry)
299 ssleep(1);
300 error = byd_detect(psmouse, 0);
301 } while (error && ++retry < 3);
302
303 if (error)
304 return error;
305
306 psmouse_dbg(psmouse, "Reconnected after %d attempts\n", retry);
307
308 error = byd_reset_touchpad(psmouse);
309 if (error) {
310 psmouse_err(psmouse, "Unable to initialize device\n");
311 return error;
312 }
313
314 return 0;
315}
316
317int byd_init(struct psmouse *psmouse)
318{
319 struct input_dev *dev = psmouse->dev;
320
321 if (psmouse_reset(psmouse))
322 return -EIO;
323
324 if (byd_reset_touchpad(psmouse))
325 return -EIO;
326
327 psmouse->reconnect = byd_reconnect;
328 psmouse->protocol_handler = byd_process_byte;
329 psmouse->pktsize = 4;
330 psmouse->resync_time = 0;
331
332 __set_bit(BTN_MIDDLE, dev->keybit);
333 __set_bit(REL_WHEEL, dev->relbit);
334 __set_bit(REL_HWHEEL, dev->relbit);
335
336 return 0;
337}
diff --git a/drivers/input/mouse/byd.h b/drivers/input/mouse/byd.h
new file mode 100644
index 000000000000..d6c120cf36cd
--- /dev/null
+++ b/drivers/input/mouse/byd.h
@@ -0,0 +1,18 @@
1#ifndef _BYD_H
2#define _BYD_H
3
4#ifdef CONFIG_MOUSE_PS2_BYD
5int byd_detect(struct psmouse *psmouse, bool set_properties);
6int byd_init(struct psmouse *psmouse);
7#else
8static inline int byd_detect(struct psmouse *psmouse, bool set_properties)
9{
10 return -ENOSYS;
11}
12static inline int byd_init(struct psmouse *psmouse)
13{
14 return -ENOSYS;
15}
16#endif /* CONFIG_MOUSE_PS2_BYD */
17
18#endif /* _BYD_H */
diff --git a/drivers/input/mouse/cyapa.c b/drivers/input/mouse/cyapa.c
index eb76b61418f3..dc2394292088 100644
--- a/drivers/input/mouse/cyapa.c
+++ b/drivers/input/mouse/cyapa.c
@@ -383,7 +383,7 @@ static int cyapa_open(struct input_dev *input)
383 * when in operational mode. 383 * when in operational mode.
384 */ 384 */
385 error = cyapa->ops->set_power_mode(cyapa, 385 error = cyapa->ops->set_power_mode(cyapa,
386 PWR_MODE_FULL_ACTIVE, 0, false); 386 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
387 if (error) { 387 if (error) {
388 dev_warn(dev, "set active power failed: %d\n", error); 388 dev_warn(dev, "set active power failed: %d\n", error);
389 goto out; 389 goto out;
@@ -424,7 +424,8 @@ static void cyapa_close(struct input_dev *input)
424 pm_runtime_set_suspended(dev); 424 pm_runtime_set_suspended(dev);
425 425
426 if (cyapa->operational) 426 if (cyapa->operational)
427 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0, false); 427 cyapa->ops->set_power_mode(cyapa,
428 PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
428 429
429 mutex_unlock(&cyapa->state_sync_lock); 430 mutex_unlock(&cyapa->state_sync_lock);
430} 431}
@@ -534,7 +535,7 @@ static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
534 */ 535 */
535 if (!input || cyapa->operational) 536 if (!input || cyapa->operational)
536 cyapa->ops->set_power_mode(cyapa, 537 cyapa->ops->set_power_mode(cyapa,
537 PWR_MODE_FULL_ACTIVE, 0, false); 538 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
538 /* Gen3 always using polling mode for command. */ 539 /* Gen3 always using polling mode for command. */
539 if (cyapa->gen >= CYAPA_GEN5) 540 if (cyapa->gen >= CYAPA_GEN5)
540 enable_irq(cyapa->client->irq); 541 enable_irq(cyapa->client->irq);
@@ -550,7 +551,7 @@ static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
550 disable_irq(cyapa->client->irq); 551 disable_irq(cyapa->client->irq);
551 if (!input || cyapa->operational) 552 if (!input || cyapa->operational)
552 cyapa->ops->set_power_mode(cyapa, 553 cyapa->ops->set_power_mode(cyapa,
553 PWR_MODE_OFF, 0, false); 554 PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
554 } 555 }
555} 556}
556 557
@@ -617,7 +618,8 @@ static int cyapa_initialize(struct cyapa *cyapa)
617 618
618 /* Power down the device until we need it. */ 619 /* Power down the device until we need it. */
619 if (cyapa->operational) 620 if (cyapa->operational)
620 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0, false); 621 cyapa->ops->set_power_mode(cyapa,
622 PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
621 623
622 return 0; 624 return 0;
623} 625}
@@ -634,7 +636,7 @@ static int cyapa_reinitialize(struct cyapa *cyapa)
634 /* Avoid command failures when TP was in OFF state. */ 636 /* Avoid command failures when TP was in OFF state. */
635 if (cyapa->operational) 637 if (cyapa->operational)
636 cyapa->ops->set_power_mode(cyapa, 638 cyapa->ops->set_power_mode(cyapa,
637 PWR_MODE_FULL_ACTIVE, 0, false); 639 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
638 640
639 error = cyapa_detect(cyapa); 641 error = cyapa_detect(cyapa);
640 if (error) 642 if (error)
@@ -654,7 +656,7 @@ out:
654 /* Reset to power OFF state to save power when no user open. */ 656 /* Reset to power OFF state to save power when no user open. */
655 if (cyapa->operational) 657 if (cyapa->operational)
656 cyapa->ops->set_power_mode(cyapa, 658 cyapa->ops->set_power_mode(cyapa,
657 PWR_MODE_OFF, 0, false); 659 PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
658 } else if (!error && cyapa->operational) { 660 } else if (!error && cyapa->operational) {
659 /* 661 /*
660 * Make sure only enable runtime PM when device is 662 * Make sure only enable runtime PM when device is
@@ -1392,7 +1394,7 @@ static int __maybe_unused cyapa_suspend(struct device *dev)
1392 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode 1394 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
1393 : PWR_MODE_OFF; 1395 : PWR_MODE_OFF;
1394 error = cyapa->ops->set_power_mode(cyapa, power_mode, 1396 error = cyapa->ops->set_power_mode(cyapa, power_mode,
1395 cyapa->suspend_sleep_time, true); 1397 cyapa->suspend_sleep_time, CYAPA_PM_SUSPEND);
1396 if (error) 1398 if (error)
1397 dev_err(dev, "suspend set power mode failed: %d\n", 1399 dev_err(dev, "suspend set power mode failed: %d\n",
1398 error); 1400 error);
@@ -1447,7 +1449,7 @@ static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
1447 error = cyapa->ops->set_power_mode(cyapa, 1449 error = cyapa->ops->set_power_mode(cyapa,
1448 cyapa->runtime_suspend_power_mode, 1450 cyapa->runtime_suspend_power_mode,
1449 cyapa->runtime_suspend_sleep_time, 1451 cyapa->runtime_suspend_sleep_time,
1450 false); 1452 CYAPA_PM_RUNTIME_SUSPEND);
1451 if (error) 1453 if (error)
1452 dev_warn(dev, "runtime suspend failed: %d\n", error); 1454 dev_warn(dev, "runtime suspend failed: %d\n", error);
1453 1455
@@ -1460,7 +1462,7 @@ static int __maybe_unused cyapa_runtime_resume(struct device *dev)
1460 int error; 1462 int error;
1461 1463
1462 error = cyapa->ops->set_power_mode(cyapa, 1464 error = cyapa->ops->set_power_mode(cyapa,
1463 PWR_MODE_FULL_ACTIVE, 0, false); 1465 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_RUNTIME_RESUME);
1464 if (error) 1466 if (error)
1465 dev_warn(dev, "runtime resume failed: %d\n", error); 1467 dev_warn(dev, "runtime resume failed: %d\n", error);
1466 1468
diff --git a/drivers/input/mouse/cyapa.h b/drivers/input/mouse/cyapa.h
index b812bba8cdd7..ce951fe4516a 100644
--- a/drivers/input/mouse/cyapa.h
+++ b/drivers/input/mouse/cyapa.h
@@ -250,6 +250,15 @@ struct cyapa;
250 250
251typedef bool (*cb_sort)(struct cyapa *, u8 *, int); 251typedef bool (*cb_sort)(struct cyapa *, u8 *, int);
252 252
253enum cyapa_pm_stage {
254 CYAPA_PM_DEACTIVE,
255 CYAPA_PM_ACTIVE,
256 CYAPA_PM_SUSPEND,
257 CYAPA_PM_RESUME,
258 CYAPA_PM_RUNTIME_SUSPEND,
259 CYAPA_PM_RUNTIME_RESUME,
260};
261
253struct cyapa_dev_ops { 262struct cyapa_dev_ops {
254 int (*check_fw)(struct cyapa *, const struct firmware *); 263 int (*check_fw)(struct cyapa *, const struct firmware *);
255 int (*bl_enter)(struct cyapa *); 264 int (*bl_enter)(struct cyapa *);
@@ -273,7 +282,7 @@ struct cyapa_dev_ops {
273 int (*sort_empty_output_data)(struct cyapa *, 282 int (*sort_empty_output_data)(struct cyapa *,
274 u8 *, int *, cb_sort); 283 u8 *, int *, cb_sort);
275 284
276 int (*set_power_mode)(struct cyapa *, u8, u16, bool); 285 int (*set_power_mode)(struct cyapa *, u8, u16, enum cyapa_pm_stage);
277 286
278 int (*set_proximity)(struct cyapa *, bool); 287 int (*set_proximity)(struct cyapa *, bool);
279}; 288};
@@ -289,6 +298,9 @@ struct cyapa_pip_cmd_states {
289 u8 *resp_data; 298 u8 *resp_data;
290 int *resp_len; 299 int *resp_len;
291 300
301 enum cyapa_pm_stage pm_stage;
302 struct mutex pm_stage_lock;
303
292 u8 irq_cmd_buf[CYAPA_REG_MAP_SIZE]; 304 u8 irq_cmd_buf[CYAPA_REG_MAP_SIZE];
293 u8 empty_buf[CYAPA_REG_MAP_SIZE]; 305 u8 empty_buf[CYAPA_REG_MAP_SIZE];
294}; 306};
diff --git a/drivers/input/mouse/cyapa_gen3.c b/drivers/input/mouse/cyapa_gen3.c
index 1a9d12ae7538..f9600753eca5 100644
--- a/drivers/input/mouse/cyapa_gen3.c
+++ b/drivers/input/mouse/cyapa_gen3.c
@@ -269,6 +269,7 @@ static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
269 { CYAPA_SMBUS_MIN_BASELINE, 1 }, /* CYAPA_CMD_MIN_BASELINE */ 269 { CYAPA_SMBUS_MIN_BASELINE, 1 }, /* CYAPA_CMD_MIN_BASELINE */
270}; 270};
271 271
272static int cyapa_gen3_try_poll_handler(struct cyapa *cyapa);
272 273
273/* 274/*
274 * cyapa_smbus_read_block - perform smbus block read command 275 * cyapa_smbus_read_block - perform smbus block read command
@@ -950,12 +951,14 @@ static u16 cyapa_get_wait_time_for_pwr_cmd(u8 pwr_mode)
950 * Device power mode can only be set when device is in operational mode. 951 * Device power mode can only be set when device is in operational mode.
951 */ 952 */
952static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode, 953static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
953 u16 always_unused, bool is_suspend_unused) 954 u16 always_unused, enum cyapa_pm_stage pm_stage)
954{ 955{
955 int ret; 956 struct input_dev *input = cyapa->input;
956 u8 power; 957 u8 power;
957 int tries; 958 int tries;
958 u16 sleep_time; 959 int sleep_time;
960 int interval;
961 int ret;
959 962
960 if (cyapa->state != CYAPA_STATE_OP) 963 if (cyapa->state != CYAPA_STATE_OP)
961 return 0; 964 return 0;
@@ -977,7 +980,7 @@ static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
977 if ((ret & PWR_MODE_MASK) == power_mode) 980 if ((ret & PWR_MODE_MASK) == power_mode)
978 return 0; 981 return 0;
979 982
980 sleep_time = cyapa_get_wait_time_for_pwr_cmd(ret & PWR_MODE_MASK); 983 sleep_time = (int)cyapa_get_wait_time_for_pwr_cmd(ret & PWR_MODE_MASK);
981 power = ret; 984 power = ret;
982 power &= ~PWR_MODE_MASK; 985 power &= ~PWR_MODE_MASK;
983 power |= power_mode & PWR_MODE_MASK; 986 power |= power_mode & PWR_MODE_MASK;
@@ -995,7 +998,23 @@ static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
995 * doing so before issuing the next command may result in errors 998 * doing so before issuing the next command may result in errors
996 * depending on the command's content. 999 * depending on the command's content.
997 */ 1000 */
998 msleep(sleep_time); 1001 if (cyapa->operational && input && input->users &&
1002 (pm_stage == CYAPA_PM_RUNTIME_SUSPEND ||
1003 pm_stage == CYAPA_PM_RUNTIME_RESUME)) {
1004 /* Try to polling in 120Hz, read may fail, just ignore it. */
1005 interval = 1000 / 120;
1006 while (sleep_time > 0) {
1007 if (sleep_time > interval)
1008 msleep(interval);
1009 else
1010 msleep(sleep_time);
1011 sleep_time -= interval;
1012 cyapa_gen3_try_poll_handler(cyapa);
1013 }
1014 } else {
1015 msleep(sleep_time);
1016 }
1017
999 return ret; 1018 return ret;
1000} 1019}
1001 1020
@@ -1112,7 +1131,7 @@ static int cyapa_gen3_do_operational_check(struct cyapa *cyapa)
1112 * may cause problems, so we set the power mode first here. 1131 * may cause problems, so we set the power mode first here.
1113 */ 1132 */
1114 error = cyapa_gen3_set_power_mode(cyapa, 1133 error = cyapa_gen3_set_power_mode(cyapa,
1115 PWR_MODE_FULL_ACTIVE, 0, false); 1134 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
1116 if (error) 1135 if (error)
1117 dev_err(dev, "%s: set full power mode failed: %d\n", 1136 dev_err(dev, "%s: set full power mode failed: %d\n",
1118 __func__, error); 1137 __func__, error);
@@ -1168,32 +1187,16 @@ static bool cyapa_gen3_irq_cmd_handler(struct cyapa *cyapa)
1168 return false; 1187 return false;
1169} 1188}
1170 1189
1171static int cyapa_gen3_irq_handler(struct cyapa *cyapa) 1190static int cyapa_gen3_event_process(struct cyapa *cyapa,
1191 struct cyapa_reg_data *data)
1172{ 1192{
1173 struct input_dev *input = cyapa->input; 1193 struct input_dev *input = cyapa->input;
1174 struct device *dev = &cyapa->client->dev;
1175 struct cyapa_reg_data data;
1176 int num_fingers; 1194 int num_fingers;
1177 int ret;
1178 int i; 1195 int i;
1179 1196
1180 ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data); 1197 num_fingers = (data->finger_btn >> 4) & 0x0f;
1181 if (ret != sizeof(data)) {
1182 dev_err(dev, "failed to read report data, (%d)\n", ret);
1183 return -EINVAL;
1184 }
1185
1186 if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
1187 (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
1188 (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
1189 dev_err(dev, "invalid device state bytes, %02x %02x\n",
1190 data.device_status, data.finger_btn);
1191 return -EINVAL;
1192 }
1193
1194 num_fingers = (data.finger_btn >> 4) & 0x0f;
1195 for (i = 0; i < num_fingers; i++) { 1198 for (i = 0; i < num_fingers; i++) {
1196 const struct cyapa_touch *touch = &data.touches[i]; 1199 const struct cyapa_touch *touch = &data->touches[i];
1197 /* Note: touch->id range is 1 to 15; slots are 0 to 14. */ 1200 /* Note: touch->id range is 1 to 15; slots are 0 to 14. */
1198 int slot = touch->id - 1; 1201 int slot = touch->id - 1;
1199 1202
@@ -1210,18 +1213,65 @@ static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
1210 1213
1211 if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK) 1214 if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
1212 input_report_key(input, BTN_LEFT, 1215 input_report_key(input, BTN_LEFT,
1213 !!(data.finger_btn & OP_DATA_LEFT_BTN)); 1216 !!(data->finger_btn & OP_DATA_LEFT_BTN));
1214 if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK) 1217 if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
1215 input_report_key(input, BTN_MIDDLE, 1218 input_report_key(input, BTN_MIDDLE,
1216 !!(data.finger_btn & OP_DATA_MIDDLE_BTN)); 1219 !!(data->finger_btn & OP_DATA_MIDDLE_BTN));
1217 if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK) 1220 if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
1218 input_report_key(input, BTN_RIGHT, 1221 input_report_key(input, BTN_RIGHT,
1219 !!(data.finger_btn & OP_DATA_RIGHT_BTN)); 1222 !!(data->finger_btn & OP_DATA_RIGHT_BTN));
1220 input_sync(input); 1223 input_sync(input);
1221 1224
1222 return 0; 1225 return 0;
1223} 1226}
1224 1227
1228static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
1229{
1230 struct device *dev = &cyapa->client->dev;
1231 struct cyapa_reg_data data;
1232 int ret;
1233
1234 ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
1235 if (ret != sizeof(data)) {
1236 dev_err(dev, "failed to read report data, (%d)\n", ret);
1237 return -EINVAL;
1238 }
1239
1240 if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
1241 (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
1242 (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
1243 dev_err(dev, "invalid device state bytes: %02x %02x\n",
1244 data.device_status, data.finger_btn);
1245 return -EINVAL;
1246 }
1247
1248 return cyapa_gen3_event_process(cyapa, &data);
1249}
1250
1251/*
1252 * This function will be called in the cyapa_gen3_set_power_mode function,
1253 * and it's known that it may failed in some situation after the set power
1254 * mode command was sent. So this function is aimed to avoid the knwon
1255 * and unwanted output I2C and data parse error messages.
1256 */
1257static int cyapa_gen3_try_poll_handler(struct cyapa *cyapa)
1258{
1259 struct cyapa_reg_data data;
1260 int ret;
1261
1262 ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
1263 if (ret != sizeof(data))
1264 return -EINVAL;
1265
1266 if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
1267 (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
1268 (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID)
1269 return -EINVAL;
1270
1271 return cyapa_gen3_event_process(cyapa, &data);
1272
1273}
1274
1225static int cyapa_gen3_initialize(struct cyapa *cyapa) { return 0; } 1275static int cyapa_gen3_initialize(struct cyapa *cyapa) { return 0; }
1226static int cyapa_gen3_bl_initiate(struct cyapa *cyapa, 1276static int cyapa_gen3_bl_initiate(struct cyapa *cyapa,
1227 const struct firmware *fw) { return 0; } 1277 const struct firmware *fw) { return 0; }
diff --git a/drivers/input/mouse/cyapa_gen5.c b/drivers/input/mouse/cyapa_gen5.c
index 118ba977181e..5775d40b3d53 100644
--- a/drivers/input/mouse/cyapa_gen5.c
+++ b/drivers/input/mouse/cyapa_gen5.c
@@ -342,6 +342,9 @@ u8 pip_bl_read_app_info[] = { 0x04, 0x00, 0x0b, 0x00, 0x40, 0x00,
342static u8 cyapa_pip_bl_cmd_key[] = { 0xa5, 0x01, 0x02, 0x03, 342static u8 cyapa_pip_bl_cmd_key[] = { 0xa5, 0x01, 0x02, 0x03,
343 0xff, 0xfe, 0xfd, 0x5a }; 343 0xff, 0xfe, 0xfd, 0x5a };
344 344
345static int cyapa_pip_event_process(struct cyapa *cyapa,
346 struct cyapa_pip_report_data *report_data);
347
345int cyapa_pip_cmd_state_initialize(struct cyapa *cyapa) 348int cyapa_pip_cmd_state_initialize(struct cyapa *cyapa)
346{ 349{
347 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip; 350 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
@@ -350,6 +353,9 @@ int cyapa_pip_cmd_state_initialize(struct cyapa *cyapa)
350 atomic_set(&pip->cmd_issued, 0); 353 atomic_set(&pip->cmd_issued, 0);
351 mutex_init(&pip->cmd_lock); 354 mutex_init(&pip->cmd_lock);
352 355
356 mutex_init(&pip->pm_stage_lock);
357 pip->pm_stage = CYAPA_PM_DEACTIVE;
358
353 pip->resp_sort_func = NULL; 359 pip->resp_sort_func = NULL;
354 pip->in_progress_cmd = PIP_INVALID_CMD; 360 pip->in_progress_cmd = PIP_INVALID_CMD;
355 pip->resp_data = NULL; 361 pip->resp_data = NULL;
@@ -397,6 +403,38 @@ ssize_t cyapa_i2c_pip_write(struct cyapa *cyapa, u8 *buf, size_t size)
397 return 0; 403 return 0;
398} 404}
399 405
406static void cyapa_set_pip_pm_state(struct cyapa *cyapa,
407 enum cyapa_pm_stage pm_stage)
408{
409 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
410
411 mutex_lock(&pip->pm_stage_lock);
412 pip->pm_stage = pm_stage;
413 mutex_unlock(&pip->pm_stage_lock);
414}
415
416static void cyapa_reset_pip_pm_state(struct cyapa *cyapa)
417{
418 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
419
420 /* Indicates the pip->pm_stage is not valid. */
421 mutex_lock(&pip->pm_stage_lock);
422 pip->pm_stage = CYAPA_PM_DEACTIVE;
423 mutex_unlock(&pip->pm_stage_lock);
424}
425
426static enum cyapa_pm_stage cyapa_get_pip_pm_state(struct cyapa *cyapa)
427{
428 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
429 enum cyapa_pm_stage pm_stage;
430
431 mutex_lock(&pip->pm_stage_lock);
432 pm_stage = pip->pm_stage;
433 mutex_unlock(&pip->pm_stage_lock);
434
435 return pm_stage;
436}
437
400/** 438/**
401 * This function is aimed to dump all not read data in Gen5 trackpad 439 * This function is aimed to dump all not read data in Gen5 trackpad
402 * before send any command, otherwise, the interrupt line will be blocked. 440 * before send any command, otherwise, the interrupt line will be blocked.
@@ -404,7 +442,9 @@ ssize_t cyapa_i2c_pip_write(struct cyapa *cyapa, u8 *buf, size_t size)
404int cyapa_empty_pip_output_data(struct cyapa *cyapa, 442int cyapa_empty_pip_output_data(struct cyapa *cyapa,
405 u8 *buf, int *len, cb_sort func) 443 u8 *buf, int *len, cb_sort func)
406{ 444{
445 struct input_dev *input = cyapa->input;
407 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip; 446 struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
447 enum cyapa_pm_stage pm_stage = cyapa_get_pip_pm_state(cyapa);
408 int length; 448 int length;
409 int report_count; 449 int report_count;
410 int empty_count; 450 int empty_count;
@@ -478,6 +518,12 @@ int cyapa_empty_pip_output_data(struct cyapa *cyapa,
478 *len = length; 518 *len = length;
479 /* Response found, success. */ 519 /* Response found, success. */
480 return 0; 520 return 0;
521 } else if (cyapa->operational && input && input->users &&
522 (pm_stage == CYAPA_PM_RUNTIME_RESUME ||
523 pm_stage == CYAPA_PM_RUNTIME_SUSPEND)) {
524 /* Parse the data and report it if it's valid. */
525 cyapa_pip_event_process(cyapa,
526 (struct cyapa_pip_report_data *)pip->empty_buf);
481 } 527 }
482 528
483 error = -EINVAL; 529 error = -EINVAL;
@@ -1566,15 +1612,17 @@ int cyapa_pip_deep_sleep(struct cyapa *cyapa, u8 state)
1566} 1612}
1567 1613
1568static int cyapa_gen5_set_power_mode(struct cyapa *cyapa, 1614static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1569 u8 power_mode, u16 sleep_time, bool is_suspend) 1615 u8 power_mode, u16 sleep_time, enum cyapa_pm_stage pm_stage)
1570{ 1616{
1571 struct device *dev = &cyapa->client->dev; 1617 struct device *dev = &cyapa->client->dev;
1572 u8 power_state; 1618 u8 power_state;
1573 int error; 1619 int error = 0;
1574 1620
1575 if (cyapa->state != CYAPA_STATE_GEN5_APP) 1621 if (cyapa->state != CYAPA_STATE_GEN5_APP)
1576 return 0; 1622 return 0;
1577 1623
1624 cyapa_set_pip_pm_state(cyapa, pm_stage);
1625
1578 if (PIP_DEV_GET_PWR_STATE(cyapa) == UNINIT_PWR_MODE) { 1626 if (PIP_DEV_GET_PWR_STATE(cyapa) == UNINIT_PWR_MODE) {
1579 /* 1627 /*
1580 * Assume TP in deep sleep mode when driver is loaded, 1628 * Assume TP in deep sleep mode when driver is loaded,
@@ -1597,7 +1645,7 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1597 power_mode == PWR_MODE_BTN_ONLY || 1645 power_mode == PWR_MODE_BTN_ONLY ||
1598 PIP_DEV_GET_SLEEP_TIME(cyapa) == sleep_time) { 1646 PIP_DEV_GET_SLEEP_TIME(cyapa) == sleep_time) {
1599 /* Has in correct power mode state, early return. */ 1647 /* Has in correct power mode state, early return. */
1600 return 0; 1648 goto out;
1601 } 1649 }
1602 } 1650 }
1603 1651
@@ -1605,11 +1653,11 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1605 error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_OFF); 1653 error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_OFF);
1606 if (error) { 1654 if (error) {
1607 dev_err(dev, "enter deep sleep fail: %d\n", error); 1655 dev_err(dev, "enter deep sleep fail: %d\n", error);
1608 return error; 1656 goto out;
1609 } 1657 }
1610 1658
1611 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF); 1659 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF);
1612 return 0; 1660 goto out;
1613 } 1661 }
1614 1662
1615 /* 1663 /*
@@ -1621,7 +1669,7 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1621 error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_ON); 1669 error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_ON);
1622 if (error) { 1670 if (error) {
1623 dev_err(dev, "deep sleep wake fail: %d\n", error); 1671 dev_err(dev, "deep sleep wake fail: %d\n", error);
1624 return error; 1672 goto out;
1625 } 1673 }
1626 } 1674 }
1627 1675
@@ -1630,7 +1678,7 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1630 GEN5_POWER_STATE_ACTIVE); 1678 GEN5_POWER_STATE_ACTIVE);
1631 if (error) { 1679 if (error) {
1632 dev_err(dev, "change to active fail: %d\n", error); 1680 dev_err(dev, "change to active fail: %d\n", error);
1633 return error; 1681 goto out;
1634 } 1682 }
1635 1683
1636 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_FULL_ACTIVE); 1684 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_FULL_ACTIVE);
@@ -1639,7 +1687,7 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1639 GEN5_POWER_STATE_BTN_ONLY); 1687 GEN5_POWER_STATE_BTN_ONLY);
1640 if (error) { 1688 if (error) {
1641 dev_err(dev, "fail to button only mode: %d\n", error); 1689 dev_err(dev, "fail to button only mode: %d\n", error);
1642 return error; 1690 goto out;
1643 } 1691 }
1644 1692
1645 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_BTN_ONLY); 1693 PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_BTN_ONLY);
@@ -1664,7 +1712,7 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1664 if (error) { 1712 if (error) {
1665 dev_err(dev, "set power state to 0x%02x failed: %d\n", 1713 dev_err(dev, "set power state to 0x%02x failed: %d\n",
1666 power_state, error); 1714 power_state, error);
1667 return error; 1715 goto out;
1668 } 1716 }
1669 1717
1670 /* 1718 /*
@@ -1677,14 +1725,16 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
1677 * is suspending which may cause interrupt line unable to be 1725 * is suspending which may cause interrupt line unable to be
1678 * asserted again. 1726 * asserted again.
1679 */ 1727 */
1680 if (is_suspend) 1728 if (pm_stage == CYAPA_PM_SUSPEND)
1681 cyapa_gen5_disable_pip_report(cyapa); 1729 cyapa_gen5_disable_pip_report(cyapa);
1682 1730
1683 PIP_DEV_SET_PWR_STATE(cyapa, 1731 PIP_DEV_SET_PWR_STATE(cyapa,
1684 cyapa_sleep_time_to_pwr_cmd(sleep_time)); 1732 cyapa_sleep_time_to_pwr_cmd(sleep_time));
1685 } 1733 }
1686 1734
1687 return 0; 1735out:
1736 cyapa_reset_pip_pm_state(cyapa);
1737 return error;
1688} 1738}
1689 1739
1690int cyapa_pip_resume_scanning(struct cyapa *cyapa) 1740int cyapa_pip_resume_scanning(struct cyapa *cyapa)
@@ -2513,7 +2563,7 @@ static int cyapa_gen5_do_operational_check(struct cyapa *cyapa)
2513 * the device state is required. 2563 * the device state is required.
2514 */ 2564 */
2515 error = cyapa_gen5_set_power_mode(cyapa, 2565 error = cyapa_gen5_set_power_mode(cyapa,
2516 PWR_MODE_FULL_ACTIVE, 0, false); 2566 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
2517 if (error) 2567 if (error)
2518 dev_warn(dev, "%s: failed to set power active mode.\n", 2568 dev_warn(dev, "%s: failed to set power active mode.\n",
2519 __func__); 2569 __func__);
@@ -2715,7 +2765,6 @@ int cyapa_pip_irq_handler(struct cyapa *cyapa)
2715 struct device *dev = &cyapa->client->dev; 2765 struct device *dev = &cyapa->client->dev;
2716 struct cyapa_pip_report_data report_data; 2766 struct cyapa_pip_report_data report_data;
2717 unsigned int report_len; 2767 unsigned int report_len;
2718 u8 report_id;
2719 int ret; 2768 int ret;
2720 2769
2721 if (!cyapa_is_pip_app_mode(cyapa)) { 2770 if (!cyapa_is_pip_app_mode(cyapa)) {
@@ -2752,7 +2801,23 @@ int cyapa_pip_irq_handler(struct cyapa *cyapa)
2752 return -EINVAL; 2801 return -EINVAL;
2753 } 2802 }
2754 2803
2755 report_id = report_data.report_head[PIP_RESP_REPORT_ID_OFFSET]; 2804 return cyapa_pip_event_process(cyapa, &report_data);
2805}
2806
2807static int cyapa_pip_event_process(struct cyapa *cyapa,
2808 struct cyapa_pip_report_data *report_data)
2809{
2810 struct device *dev = &cyapa->client->dev;
2811 unsigned int report_len;
2812 u8 report_id;
2813
2814 report_len = get_unaligned_le16(
2815 &report_data->report_head[PIP_RESP_LENGTH_OFFSET]);
2816 /* Idle, no data for report. */
2817 if (report_len == PIP_RESP_LENGTH_SIZE)
2818 return 0;
2819
2820 report_id = report_data->report_head[PIP_RESP_REPORT_ID_OFFSET];
2756 if (report_id == PIP_WAKEUP_EVENT_REPORT_ID && 2821 if (report_id == PIP_WAKEUP_EVENT_REPORT_ID &&
2757 report_len == PIP_WAKEUP_EVENT_SIZE) { 2822 report_len == PIP_WAKEUP_EVENT_SIZE) {
2758 /* 2823 /*
@@ -2805,11 +2870,11 @@ int cyapa_pip_irq_handler(struct cyapa *cyapa)
2805 } 2870 }
2806 2871
2807 if (report_id == PIP_TOUCH_REPORT_ID) 2872 if (report_id == PIP_TOUCH_REPORT_ID)
2808 cyapa_pip_report_touches(cyapa, &report_data); 2873 cyapa_pip_report_touches(cyapa, report_data);
2809 else if (report_id == PIP_PROXIMITY_REPORT_ID) 2874 else if (report_id == PIP_PROXIMITY_REPORT_ID)
2810 cyapa_pip_report_proximity(cyapa, &report_data); 2875 cyapa_pip_report_proximity(cyapa, report_data);
2811 else 2876 else
2812 cyapa_pip_report_buttons(cyapa, &report_data); 2877 cyapa_pip_report_buttons(cyapa, report_data);
2813 2878
2814 return 0; 2879 return 0;
2815} 2880}
diff --git a/drivers/input/mouse/cyapa_gen6.c b/drivers/input/mouse/cyapa_gen6.c
index e4eb048d1bf6..016397850b1b 100644
--- a/drivers/input/mouse/cyapa_gen6.c
+++ b/drivers/input/mouse/cyapa_gen6.c
@@ -425,7 +425,7 @@ static int cyapa_gen6_deep_sleep(struct cyapa *cyapa, u8 state)
425} 425}
426 426
427static int cyapa_gen6_set_power_mode(struct cyapa *cyapa, 427static int cyapa_gen6_set_power_mode(struct cyapa *cyapa,
428 u8 power_mode, u16 sleep_time, bool is_suspend) 428 u8 power_mode, u16 sleep_time, enum cyapa_pm_stage pm_stage)
429{ 429{
430 struct device *dev = &cyapa->client->dev; 430 struct device *dev = &cyapa->client->dev;
431 struct gen6_interval_setting *interval_setting = 431 struct gen6_interval_setting *interval_setting =
@@ -689,7 +689,7 @@ static int cyapa_gen6_operational_check(struct cyapa *cyapa)
689 * the device state is required. 689 * the device state is required.
690 */ 690 */
691 error = cyapa_gen6_set_power_mode(cyapa, 691 error = cyapa_gen6_set_power_mode(cyapa,
692 PWR_MODE_FULL_ACTIVE, 0, false); 692 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
693 if (error) 693 if (error)
694 dev_warn(dev, "%s: failed to set power active mode.\n", 694 dev_warn(dev, "%s: failed to set power active mode.\n",
695 __func__); 695 __func__);
diff --git a/drivers/input/mouse/psmouse-base.c b/drivers/input/mouse/psmouse-base.c
index b9e4ee34c132..39d1becd35c9 100644
--- a/drivers/input/mouse/psmouse-base.c
+++ b/drivers/input/mouse/psmouse-base.c
@@ -37,6 +37,7 @@
37#include "cypress_ps2.h" 37#include "cypress_ps2.h"
38#include "focaltech.h" 38#include "focaltech.h"
39#include "vmmouse.h" 39#include "vmmouse.h"
40#include "byd.h"
40 41
41#define DRIVER_DESC "PS/2 mouse driver" 42#define DRIVER_DESC "PS/2 mouse driver"
42 43
@@ -842,6 +843,15 @@ static const struct psmouse_protocol psmouse_protocols[] = {
842 .init = vmmouse_init, 843 .init = vmmouse_init,
843 }, 844 },
844#endif 845#endif
846#ifdef CONFIG_MOUSE_PS2_BYD
847 {
848 .type = PSMOUSE_BYD,
849 .name = "BydPS/2",
850 .alias = "byd",
851 .detect = byd_detect,
852 .init = byd_init,
853 },
854#endif
845 { 855 {
846 .type = PSMOUSE_AUTO, 856 .type = PSMOUSE_AUTO,
847 .name = "auto", 857 .name = "auto",
@@ -1105,6 +1115,10 @@ static int psmouse_extensions(struct psmouse *psmouse,
1105 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2, 1115 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
1106 &max_proto, set_properties, true)) 1116 &max_proto, set_properties, true))
1107 return PSMOUSE_TOUCHKIT_PS2; 1117 return PSMOUSE_TOUCHKIT_PS2;
1118
1119 if (psmouse_try_protocol(psmouse, PSMOUSE_BYD,
1120 &max_proto, set_properties, true))
1121 return PSMOUSE_BYD;
1108 } 1122 }
1109 1123
1110 /* 1124 /*
diff --git a/drivers/input/mouse/psmouse.h b/drivers/input/mouse/psmouse.h
index ad5a5a1ea872..e0ca6cda3d16 100644
--- a/drivers/input/mouse/psmouse.h
+++ b/drivers/input/mouse/psmouse.h
@@ -104,6 +104,7 @@ enum psmouse_type {
104 PSMOUSE_CYPRESS, 104 PSMOUSE_CYPRESS,
105 PSMOUSE_FOCALTECH, 105 PSMOUSE_FOCALTECH,
106 PSMOUSE_VMMOUSE, 106 PSMOUSE_VMMOUSE,
107 PSMOUSE_BYD,
107 PSMOUSE_AUTO /* This one should always be last */ 108 PSMOUSE_AUTO /* This one should always be last */
108}; 109};
109 110
diff --git a/drivers/input/rmi4/Kconfig b/drivers/input/rmi4/Kconfig
new file mode 100644
index 000000000000..f73df2495fed
--- /dev/null
+++ b/drivers/input/rmi4/Kconfig
@@ -0,0 +1,63 @@
1#
2# RMI4 configuration
3#
4config RMI4_CORE
5 tristate "Synaptics RMI4 bus support"
6 help
7 Say Y here if you want to support the Synaptics RMI4 bus. This is
8 required for all RMI4 device support.
9
10 If unsure, say Y.
11
12config RMI4_I2C
13 tristate "RMI4 I2C Support"
14 depends on RMI4_CORE && I2C
15 help
16 Say Y here if you want to support RMI4 devices connected to an I2C
17 bus.
18
19 If unsure, say Y.
20
21config RMI4_SPI
22 tristate "RMI4 SPI Support"
23 depends on RMI4_CORE && SPI
24 help
25 Say Y here if you want to support RMI4 devices connected to a SPI
26 bus.
27
28 If unsure, say N.
29
30config RMI4_2D_SENSOR
31 bool
32 depends on RMI4_CORE
33
34config RMI4_F11
35 bool "RMI4 Function 11 (2D pointing)"
36 select RMI4_2D_SENSOR
37 depends on RMI4_CORE
38 help
39 Say Y here if you want to add support for RMI4 function 11.
40
41 Function 11 provides 2D multifinger pointing for touchscreens and
42 touchpads. For sensors that support relative pointing, F11 also
43 provides mouse input.
44
45config RMI4_F12
46 bool "RMI4 Function 12 (2D pointing)"
47 select RMI4_2D_SENSOR
48 depends on RMI4_CORE
49 help
50 Say Y here if you want to add support for RMI4 function 12.
51
52 Function 12 provides 2D multifinger pointing for touchscreens and
53 touchpads. For sensors that support relative pointing, F12 also
54 provides mouse input.
55
56config RMI4_F30
57 bool "RMI4 Function 30 (GPIO LED)"
58 depends on RMI4_CORE
59 help
60 Say Y here if you want to add support for RMI4 function 30.
61
62 Function 30 provides GPIO and LED support for RMI4 devices. This
63 includes support for buttons on TouchPads and ClickPads.
diff --git a/drivers/input/rmi4/Makefile b/drivers/input/rmi4/Makefile
new file mode 100644
index 000000000000..95c00a783992
--- /dev/null
+++ b/drivers/input/rmi4/Makefile
@@ -0,0 +1,13 @@
1obj-$(CONFIG_RMI4_CORE) += rmi_core.o
2rmi_core-y := rmi_bus.o rmi_driver.o rmi_f01.o
3
4rmi_core-$(CONFIG_RMI4_2D_SENSOR) += rmi_2d_sensor.o
5
6# Function drivers
7rmi_core-$(CONFIG_RMI4_F11) += rmi_f11.o
8rmi_core-$(CONFIG_RMI4_F12) += rmi_f12.o
9rmi_core-$(CONFIG_RMI4_F30) += rmi_f30.o
10
11# Transports
12obj-$(CONFIG_RMI4_I2C) += rmi_i2c.o
13obj-$(CONFIG_RMI4_SPI) += rmi_spi.o
diff --git a/drivers/input/rmi4/rmi_2d_sensor.c b/drivers/input/rmi4/rmi_2d_sensor.c
new file mode 100644
index 000000000000..e97bd7fabccc
--- /dev/null
+++ b/drivers/input/rmi4/rmi_2d_sensor.c
@@ -0,0 +1,329 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/of.h>
13#include <linux/input.h>
14#include <linux/input/mt.h>
15#include <linux/rmi.h>
16#include "rmi_driver.h"
17#include "rmi_2d_sensor.h"
18
19#define RMI_2D_REL_POS_MIN -128
20#define RMI_2D_REL_POS_MAX 127
21
22/* maximum ABS_MT_POSITION displacement (in mm) */
23#define DMAX 10
24
25void rmi_2d_sensor_abs_process(struct rmi_2d_sensor *sensor,
26 struct rmi_2d_sensor_abs_object *obj,
27 int slot)
28{
29 struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
30
31 /* we keep the previous values if the finger is released */
32 if (obj->type == RMI_2D_OBJECT_NONE)
33 return;
34
35 if (axis_align->swap_axes)
36 swap(obj->x, obj->y);
37
38 if (axis_align->flip_x)
39 obj->x = sensor->max_x - obj->x;
40
41 if (axis_align->flip_y)
42 obj->y = sensor->max_y - obj->y;
43
44 /*
45 * Here checking if X offset or y offset are specified is
46 * redundant. We just add the offsets or clip the values.
47 *
48 * Note: offsets need to be applied before clipping occurs,
49 * or we could get funny values that are outside of
50 * clipping boundaries.
51 */
52 obj->x += axis_align->offset_x;
53 obj->y += axis_align->offset_y;
54
55 obj->x = max(axis_align->clip_x_low, obj->x);
56 obj->y = max(axis_align->clip_y_low, obj->y);
57
58 if (axis_align->clip_x_high)
59 obj->x = min(sensor->max_x, obj->x);
60
61 if (axis_align->clip_y_high)
62 obj->y = min(sensor->max_y, obj->y);
63
64 sensor->tracking_pos[slot].x = obj->x;
65 sensor->tracking_pos[slot].y = obj->y;
66}
67EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_process);
68
69void rmi_2d_sensor_abs_report(struct rmi_2d_sensor *sensor,
70 struct rmi_2d_sensor_abs_object *obj,
71 int slot)
72{
73 struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
74 struct input_dev *input = sensor->input;
75 int wide, major, minor;
76
77 if (sensor->kernel_tracking)
78 input_mt_slot(input, sensor->tracking_slots[slot]);
79 else
80 input_mt_slot(input, slot);
81
82 input_mt_report_slot_state(input, obj->mt_tool,
83 obj->type != RMI_2D_OBJECT_NONE);
84
85 if (obj->type != RMI_2D_OBJECT_NONE) {
86 obj->x = sensor->tracking_pos[slot].x;
87 obj->y = sensor->tracking_pos[slot].y;
88
89 if (axis_align->swap_axes)
90 swap(obj->wx, obj->wy);
91
92 wide = (obj->wx > obj->wy);
93 major = max(obj->wx, obj->wy);
94 minor = min(obj->wx, obj->wy);
95
96 if (obj->type == RMI_2D_OBJECT_STYLUS) {
97 major = max(1, major);
98 minor = max(1, minor);
99 }
100
101 input_event(sensor->input, EV_ABS, ABS_MT_POSITION_X, obj->x);
102 input_event(sensor->input, EV_ABS, ABS_MT_POSITION_Y, obj->y);
103 input_event(sensor->input, EV_ABS, ABS_MT_ORIENTATION, wide);
104 input_event(sensor->input, EV_ABS, ABS_MT_PRESSURE, obj->z);
105 input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
106 input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
107
108 rmi_dbg(RMI_DEBUG_2D_SENSOR, &sensor->input->dev,
109 "%s: obj[%d]: type: 0x%02x X: %d Y: %d Z: %d WX: %d WY: %d\n",
110 __func__, slot, obj->type, obj->x, obj->y, obj->z,
111 obj->wx, obj->wy);
112 }
113}
114EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_report);
115
116void rmi_2d_sensor_rel_report(struct rmi_2d_sensor *sensor, int x, int y)
117{
118 struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
119
120 x = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)x));
121 y = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)y));
122
123 if (axis_align->swap_axes)
124 swap(x, y);
125
126 if (axis_align->flip_x)
127 x = min(RMI_2D_REL_POS_MAX, -x);
128
129 if (axis_align->flip_y)
130 y = min(RMI_2D_REL_POS_MAX, -y);
131
132 if (x || y) {
133 input_report_rel(sensor->input, REL_X, x);
134 input_report_rel(sensor->input, REL_Y, y);
135 }
136}
137EXPORT_SYMBOL_GPL(rmi_2d_sensor_rel_report);
138
139static void rmi_2d_sensor_set_input_params(struct rmi_2d_sensor *sensor)
140{
141 struct input_dev *input = sensor->input;
142 int res_x;
143 int res_y;
144 int input_flags = 0;
145
146 if (sensor->report_abs) {
147 if (sensor->axis_align.swap_axes)
148 swap(sensor->max_x, sensor->max_y);
149
150 sensor->min_x = sensor->axis_align.clip_x_low;
151 if (sensor->axis_align.clip_x_high)
152 sensor->max_x = min(sensor->max_x,
153 sensor->axis_align.clip_x_high);
154
155 sensor->min_y = sensor->axis_align.clip_y_low;
156 if (sensor->axis_align.clip_y_high)
157 sensor->max_y = min(sensor->max_y,
158 sensor->axis_align.clip_y_high);
159
160 set_bit(EV_ABS, input->evbit);
161 input_set_abs_params(input, ABS_MT_POSITION_X, 0, sensor->max_x,
162 0, 0);
163 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, sensor->max_y,
164 0, 0);
165
166 if (sensor->x_mm && sensor->y_mm) {
167 res_x = (sensor->max_x - sensor->min_x) / sensor->x_mm;
168 res_y = (sensor->max_y - sensor->min_y) / sensor->y_mm;
169
170 input_abs_set_res(input, ABS_X, res_x);
171 input_abs_set_res(input, ABS_Y, res_y);
172
173 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
174 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
175
176 if (!sensor->dmax)
177 sensor->dmax = DMAX * res_x;
178 }
179
180 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
181 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
182 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
183 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
184
185 if (sensor->sensor_type == rmi_sensor_touchpad)
186 input_flags = INPUT_MT_POINTER;
187 else
188 input_flags = INPUT_MT_DIRECT;
189
190 if (sensor->kernel_tracking)
191 input_flags |= INPUT_MT_TRACK;
192
193 input_mt_init_slots(input, sensor->nbr_fingers, input_flags);
194 }
195
196 if (sensor->report_rel) {
197 set_bit(EV_REL, input->evbit);
198 set_bit(REL_X, input->relbit);
199 set_bit(REL_Y, input->relbit);
200 }
201
202 if (sensor->topbuttonpad)
203 set_bit(INPUT_PROP_TOPBUTTONPAD, input->propbit);
204}
205EXPORT_SYMBOL_GPL(rmi_2d_sensor_set_input_params);
206
207int rmi_2d_sensor_configure_input(struct rmi_function *fn,
208 struct rmi_2d_sensor *sensor)
209{
210 struct rmi_device *rmi_dev = fn->rmi_dev;
211 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
212
213 if (!drv_data->input)
214 return -ENODEV;
215
216 sensor->input = drv_data->input;
217 rmi_2d_sensor_set_input_params(sensor);
218
219 return 0;
220}
221EXPORT_SYMBOL_GPL(rmi_2d_sensor_configure_input);
222
223#ifdef CONFIG_OF
224int rmi_2d_sensor_of_probe(struct device *dev,
225 struct rmi_2d_sensor_platform_data *pdata)
226{
227 int retval;
228 u32 val;
229
230 pdata->axis_align.swap_axes = of_property_read_bool(dev->of_node,
231 "touchscreen-swapped-x-y");
232
233 pdata->axis_align.flip_x = of_property_read_bool(dev->of_node,
234 "touchscreen-inverted-x");
235
236 pdata->axis_align.flip_y = of_property_read_bool(dev->of_node,
237 "touchscreen-inverted-y");
238
239 retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-low", 1);
240 if (retval)
241 return retval;
242
243 pdata->axis_align.clip_x_low = val;
244
245 retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-low", 1);
246 if (retval)
247 return retval;
248
249 pdata->axis_align.clip_y_low = val;
250
251 retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-high", 1);
252 if (retval)
253 return retval;
254
255 pdata->axis_align.clip_x_high = val;
256
257 retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-high", 1);
258 if (retval)
259 return retval;
260
261 pdata->axis_align.clip_y_high = val;
262
263 retval = rmi_of_property_read_u32(dev, &val, "syna,offset-x", 1);
264 if (retval)
265 return retval;
266
267 pdata->axis_align.offset_x = val;
268
269 retval = rmi_of_property_read_u32(dev, &val, "syna,offset-y", 1);
270 if (retval)
271 return retval;
272
273 pdata->axis_align.offset_y = val;
274
275 retval = rmi_of_property_read_u32(dev, &val, "syna,delta-x-threshold",
276 1);
277 if (retval)
278 return retval;
279
280 pdata->axis_align.delta_x_threshold = val;
281
282 retval = rmi_of_property_read_u32(dev, &val, "syna,delta-y-threshold",
283 1);
284 if (retval)
285 return retval;
286
287 pdata->axis_align.delta_y_threshold = val;
288
289 retval = rmi_of_property_read_u32(dev, (u32 *)&pdata->sensor_type,
290 "syna,sensor-type", 1);
291 if (retval)
292 return retval;
293
294 retval = rmi_of_property_read_u32(dev, &val, "touchscreen-x-mm", 1);
295 if (retval)
296 return retval;
297
298 pdata->x_mm = val;
299
300 retval = rmi_of_property_read_u32(dev, &val, "touchscreen-y-mm", 1);
301 if (retval)
302 return retval;
303
304 pdata->y_mm = val;
305
306 retval = rmi_of_property_read_u32(dev, &val,
307 "syna,disable-report-mask", 1);
308 if (retval)
309 return retval;
310
311 pdata->disable_report_mask = val;
312
313 retval = rmi_of_property_read_u32(dev, &val, "syna,rezero-wait-ms",
314 1);
315 if (retval)
316 return retval;
317
318 pdata->rezero_wait = val;
319
320 return 0;
321}
322#else
323inline int rmi_2d_sensor_of_probe(struct device *dev,
324 struct rmi_2d_sensor_platform_data *pdata)
325{
326 return -ENODEV;
327}
328#endif
329EXPORT_SYMBOL_GPL(rmi_2d_sensor_of_probe);
diff --git a/drivers/input/rmi4/rmi_2d_sensor.h b/drivers/input/rmi4/rmi_2d_sensor.h
new file mode 100644
index 000000000000..77fcdfef003c
--- /dev/null
+++ b/drivers/input/rmi4/rmi_2d_sensor.h
@@ -0,0 +1,87 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#ifndef _RMI_2D_SENSOR_H
11#define _RMI_2D_SENSOR_H
12
13enum rmi_2d_sensor_object_type {
14 RMI_2D_OBJECT_NONE,
15 RMI_2D_OBJECT_FINGER,
16 RMI_2D_OBJECT_STYLUS,
17 RMI_2D_OBJECT_PALM,
18 RMI_2D_OBJECT_UNCLASSIFIED,
19};
20
21struct rmi_2d_sensor_abs_object {
22 enum rmi_2d_sensor_object_type type;
23 int mt_tool;
24 u16 x;
25 u16 y;
26 u8 z;
27 u8 wx;
28 u8 wy;
29};
30
31/**
32 * @axis_align - controls parameters that are useful in system prototyping
33 * and bring up.
34 * @max_x - The maximum X coordinate that will be reported by this sensor.
35 * @max_y - The maximum Y coordinate that will be reported by this sensor.
36 * @nbr_fingers - How many fingers can this sensor report?
37 * @data_pkt - buffer for data reported by this sensor.
38 * @pkt_size - number of bytes in that buffer.
39 * @attn_size - Size of the HID attention report (only contains abs data).
40 * position when two fingers are on the device. When this is true, we
41 * assume we have one of those sensors and report events appropriately.
42 * @sensor_type - indicates whether we're touchscreen or touchpad.
43 * @input - input device for absolute pointing stream
44 * @input_phys - buffer for the absolute phys name for this sensor.
45 */
46struct rmi_2d_sensor {
47 struct rmi_2d_axis_alignment axis_align;
48 struct input_mt_pos *tracking_pos;
49 int *tracking_slots;
50 bool kernel_tracking;
51 struct rmi_2d_sensor_abs_object *objs;
52 int dmax;
53 u16 min_x;
54 u16 max_x;
55 u16 min_y;
56 u16 max_y;
57 u8 nbr_fingers;
58 u8 *data_pkt;
59 int pkt_size;
60 int attn_size;
61 bool topbuttonpad;
62 enum rmi_sensor_type sensor_type;
63 struct input_dev *input;
64 struct rmi_function *fn;
65 char input_phys[32];
66 u8 report_abs;
67 u8 report_rel;
68 u8 x_mm;
69 u8 y_mm;
70};
71
72int rmi_2d_sensor_of_probe(struct device *dev,
73 struct rmi_2d_sensor_platform_data *pdata);
74
75void rmi_2d_sensor_abs_process(struct rmi_2d_sensor *sensor,
76 struct rmi_2d_sensor_abs_object *obj,
77 int slot);
78
79void rmi_2d_sensor_abs_report(struct rmi_2d_sensor *sensor,
80 struct rmi_2d_sensor_abs_object *obj,
81 int slot);
82
83void rmi_2d_sensor_rel_report(struct rmi_2d_sensor *sensor, int x, int y);
84
85int rmi_2d_sensor_configure_input(struct rmi_function *fn,
86 struct rmi_2d_sensor *sensor);
87#endif /* _RMI_2D_SENSOR_H */
diff --git a/drivers/input/rmi4/rmi_bus.c b/drivers/input/rmi4/rmi_bus.c
new file mode 100644
index 000000000000..b368b0515c5a
--- /dev/null
+++ b/drivers/input/rmi4/rmi_bus.c
@@ -0,0 +1,419 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/kconfig.h>
13#include <linux/list.h>
14#include <linux/pm.h>
15#include <linux/rmi.h>
16#include <linux/slab.h>
17#include <linux/types.h>
18#include <linux/of.h>
19#include "rmi_bus.h"
20#include "rmi_driver.h"
21
22static int debug_flags;
23module_param(debug_flags, int, 0644);
24MODULE_PARM_DESC(debug_flags, "control debugging information");
25
26void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
27{
28 struct va_format vaf;
29 va_list args;
30
31 if (flags & debug_flags) {
32 va_start(args, fmt);
33
34 vaf.fmt = fmt;
35 vaf.va = &args;
36
37 dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
38
39 va_end(args);
40 }
41}
42EXPORT_SYMBOL_GPL(rmi_dbg);
43
44/*
45 * RMI Physical devices
46 *
47 * Physical RMI device consists of several functions serving particular
48 * purpose. For example F11 is a 2D touch sensor while F01 is a generic
49 * function present in every RMI device.
50 */
51
52static void rmi_release_device(struct device *dev)
53{
54 struct rmi_device *rmi_dev = to_rmi_device(dev);
55
56 kfree(rmi_dev);
57}
58
59static struct device_type rmi_device_type = {
60 .name = "rmi4_sensor",
61 .release = rmi_release_device,
62};
63
64bool rmi_is_physical_device(struct device *dev)
65{
66 return dev->type == &rmi_device_type;
67}
68
69/**
70 * rmi_register_transport_device - register a transport device connection
71 * on the RMI bus. Transport drivers provide communication from the devices
72 * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
73 *
74 * @xport: the transport device to register
75 */
76int rmi_register_transport_device(struct rmi_transport_dev *xport)
77{
78 static atomic_t transport_device_count = ATOMIC_INIT(0);
79 struct rmi_device *rmi_dev;
80 int error;
81
82 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
83 if (!rmi_dev)
84 return -ENOMEM;
85
86 device_initialize(&rmi_dev->dev);
87
88 rmi_dev->xport = xport;
89 rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
90
91 dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
92
93 rmi_dev->dev.bus = &rmi_bus_type;
94 rmi_dev->dev.type = &rmi_device_type;
95
96 xport->rmi_dev = rmi_dev;
97
98 error = device_add(&rmi_dev->dev);
99 if (error)
100 goto err_put_device;
101
102 rmi_dbg(RMI_DEBUG_CORE, xport->dev,
103 "%s: Registered %s as %s.\n", __func__,
104 dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
105
106 return 0;
107
108err_put_device:
109 put_device(&rmi_dev->dev);
110 return error;
111}
112EXPORT_SYMBOL_GPL(rmi_register_transport_device);
113
114/**
115 * rmi_unregister_transport_device - unregister a transport device connection
116 * @xport: the transport driver to unregister
117 *
118 */
119void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
120{
121 struct rmi_device *rmi_dev = xport->rmi_dev;
122
123 device_del(&rmi_dev->dev);
124 put_device(&rmi_dev->dev);
125}
126EXPORT_SYMBOL(rmi_unregister_transport_device);
127
128
129/* Function specific stuff */
130
131static void rmi_release_function(struct device *dev)
132{
133 struct rmi_function *fn = to_rmi_function(dev);
134
135 kfree(fn);
136}
137
138static struct device_type rmi_function_type = {
139 .name = "rmi4_function",
140 .release = rmi_release_function,
141};
142
143bool rmi_is_function_device(struct device *dev)
144{
145 return dev->type == &rmi_function_type;
146}
147
148static int rmi_function_match(struct device *dev, struct device_driver *drv)
149{
150 struct rmi_function_handler *handler = to_rmi_function_handler(drv);
151 struct rmi_function *fn = to_rmi_function(dev);
152
153 return fn->fd.function_number == handler->func;
154}
155
156#ifdef CONFIG_OF
157static void rmi_function_of_probe(struct rmi_function *fn)
158{
159 char of_name[9];
160
161 snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
162 fn->fd.function_number);
163 fn->dev.of_node = of_find_node_by_name(
164 fn->rmi_dev->xport->dev->of_node, of_name);
165}
166#else
167static inline void rmi_function_of_probe(struct rmi_function *fn)
168{}
169#endif
170
171static int rmi_function_probe(struct device *dev)
172{
173 struct rmi_function *fn = to_rmi_function(dev);
174 struct rmi_function_handler *handler =
175 to_rmi_function_handler(dev->driver);
176 int error;
177
178 rmi_function_of_probe(fn);
179
180 if (handler->probe) {
181 error = handler->probe(fn);
182 return error;
183 }
184
185 return 0;
186}
187
188static int rmi_function_remove(struct device *dev)
189{
190 struct rmi_function *fn = to_rmi_function(dev);
191 struct rmi_function_handler *handler =
192 to_rmi_function_handler(dev->driver);
193
194 if (handler->remove)
195 handler->remove(fn);
196
197 return 0;
198}
199
200int rmi_register_function(struct rmi_function *fn)
201{
202 struct rmi_device *rmi_dev = fn->rmi_dev;
203 int error;
204
205 device_initialize(&fn->dev);
206
207 dev_set_name(&fn->dev, "%s.fn%02x",
208 dev_name(&rmi_dev->dev), fn->fd.function_number);
209
210 fn->dev.parent = &rmi_dev->dev;
211 fn->dev.type = &rmi_function_type;
212 fn->dev.bus = &rmi_bus_type;
213
214 error = device_add(&fn->dev);
215 if (error) {
216 dev_err(&rmi_dev->dev,
217 "Failed device_register function device %s\n",
218 dev_name(&fn->dev));
219 goto err_put_device;
220 }
221
222 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
223 fn->fd.function_number);
224
225 return 0;
226
227err_put_device:
228 put_device(&fn->dev);
229 return error;
230}
231
232void rmi_unregister_function(struct rmi_function *fn)
233{
234 device_del(&fn->dev);
235
236 if (fn->dev.of_node)
237 of_node_put(fn->dev.of_node);
238
239 put_device(&fn->dev);
240}
241
242/**
243 * rmi_register_function_handler - register a handler for an RMI function
244 * @handler: RMI handler that should be registered.
245 * @module: pointer to module that implements the handler
246 * @mod_name: name of the module implementing the handler
247 *
248 * This function performs additional setup of RMI function handler and
249 * registers it with the RMI core so that it can be bound to
250 * RMI function devices.
251 */
252int __rmi_register_function_handler(struct rmi_function_handler *handler,
253 struct module *owner,
254 const char *mod_name)
255{
256 struct device_driver *driver = &handler->driver;
257 int error;
258
259 driver->bus = &rmi_bus_type;
260 driver->owner = owner;
261 driver->mod_name = mod_name;
262 driver->probe = rmi_function_probe;
263 driver->remove = rmi_function_remove;
264
265 error = driver_register(&handler->driver);
266 if (error) {
267 pr_err("driver_register() failed for %s, error: %d\n",
268 handler->driver.name, error);
269 return error;
270 }
271
272 return 0;
273}
274EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
275
276/**
277 * rmi_unregister_function_handler - unregister given RMI function handler
278 * @handler: RMI handler that should be unregistered.
279 *
280 * This function unregisters given function handler from RMI core which
281 * causes it to be unbound from the function devices.
282 */
283void rmi_unregister_function_handler(struct rmi_function_handler *handler)
284{
285 driver_unregister(&handler->driver);
286}
287EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
288
289/* Bus specific stuff */
290
291static int rmi_bus_match(struct device *dev, struct device_driver *drv)
292{
293 bool physical = rmi_is_physical_device(dev);
294
295 /* First see if types are not compatible */
296 if (physical != rmi_is_physical_driver(drv))
297 return 0;
298
299 return physical || rmi_function_match(dev, drv);
300}
301
302struct bus_type rmi_bus_type = {
303 .match = rmi_bus_match,
304 .name = "rmi4",
305};
306
307static struct rmi_function_handler *fn_handlers[] = {
308 &rmi_f01_handler,
309#ifdef CONFIG_RMI4_F11
310 &rmi_f11_handler,
311#endif
312#ifdef CONFIG_RMI4_F12
313 &rmi_f12_handler,
314#endif
315#ifdef CONFIG_RMI4_F30
316 &rmi_f30_handler,
317#endif
318};
319
320static void __rmi_unregister_function_handlers(int start_idx)
321{
322 int i;
323
324 for (i = start_idx; i >= 0; i--)
325 rmi_unregister_function_handler(fn_handlers[i]);
326}
327
328static void rmi_unregister_function_handlers(void)
329{
330 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
331}
332
333static int rmi_register_function_handlers(void)
334{
335 int ret;
336 int i;
337
338 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
339 ret = rmi_register_function_handler(fn_handlers[i]);
340 if (ret) {
341 pr_err("%s: error registering the RMI F%02x handler: %d\n",
342 __func__, fn_handlers[i]->func, ret);
343 goto err_unregister_function_handlers;
344 }
345 }
346
347 return 0;
348
349err_unregister_function_handlers:
350 __rmi_unregister_function_handlers(i - 1);
351 return ret;
352}
353
354int rmi_of_property_read_u32(struct device *dev, u32 *result,
355 const char *prop, bool optional)
356{
357 int retval;
358 u32 val = 0;
359
360 retval = of_property_read_u32(dev->of_node, prop, &val);
361 if (retval && (!optional && retval == -EINVAL)) {
362 dev_err(dev, "Failed to get %s value: %d\n",
363 prop, retval);
364 return retval;
365 }
366 *result = val;
367
368 return 0;
369}
370EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
371
372static int __init rmi_bus_init(void)
373{
374 int error;
375
376 error = bus_register(&rmi_bus_type);
377 if (error) {
378 pr_err("%s: error registering the RMI bus: %d\n",
379 __func__, error);
380 return error;
381 }
382
383 error = rmi_register_function_handlers();
384 if (error)
385 goto err_unregister_bus;
386
387 error = rmi_register_physical_driver();
388 if (error) {
389 pr_err("%s: error registering the RMI physical driver: %d\n",
390 __func__, error);
391 goto err_unregister_bus;
392 }
393
394 return 0;
395
396err_unregister_bus:
397 bus_unregister(&rmi_bus_type);
398 return error;
399}
400module_init(rmi_bus_init);
401
402static void __exit rmi_bus_exit(void)
403{
404 /*
405 * We should only ever get here if all drivers are unloaded, so
406 * all we have to do at this point is unregister ourselves.
407 */
408
409 rmi_unregister_physical_driver();
410 rmi_unregister_function_handlers();
411 bus_unregister(&rmi_bus_type);
412}
413module_exit(rmi_bus_exit);
414
415MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
416MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
417MODULE_DESCRIPTION("RMI bus");
418MODULE_LICENSE("GPL");
419MODULE_VERSION(RMI_DRIVER_VERSION);
diff --git a/drivers/input/rmi4/rmi_bus.h b/drivers/input/rmi4/rmi_bus.h
new file mode 100644
index 000000000000..899579830536
--- /dev/null
+++ b/drivers/input/rmi4/rmi_bus.h
@@ -0,0 +1,182 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#ifndef _RMI_BUS_H
11#define _RMI_BUS_H
12
13#include <linux/rmi.h>
14
15struct rmi_device;
16
17/**
18 * struct rmi_function - represents the implementation of an RMI4
19 * function for a particular device (basically, a driver for that RMI4 function)
20 *
21 * @fd: The function descriptor of the RMI function
22 * @rmi_dev: Pointer to the RMI device associated with this function container
23 * @dev: The device associated with this particular function.
24 *
25 * @num_of_irqs: The number of irqs needed by this function
26 * @irq_pos: The position in the irq bitfield this function holds
27 * @irq_mask: For convenience, can be used to mask IRQ bits off during ATTN
28 * interrupt handling.
29 *
30 * @node: entry in device's list of functions
31 */
32struct rmi_function {
33 struct rmi_function_descriptor fd;
34 struct rmi_device *rmi_dev;
35 struct device dev;
36 struct list_head node;
37
38 unsigned int num_of_irqs;
39 unsigned int irq_pos;
40 unsigned long irq_mask[];
41};
42
43#define to_rmi_function(d) container_of(d, struct rmi_function, dev)
44
45bool rmi_is_function_device(struct device *dev);
46
47int __must_check rmi_register_function(struct rmi_function *);
48void rmi_unregister_function(struct rmi_function *);
49
50/**
51 * struct rmi_function_handler - driver routines for a particular RMI function.
52 *
53 * @func: The RMI function number
54 * @reset: Called when a reset of the touch sensor is detected. The routine
55 * should perform any out-of-the-ordinary reset handling that might be
56 * necessary. Restoring of touch sensor configuration registers should be
57 * handled in the config() callback, below.
58 * @config: Called when the function container is first initialized, and
59 * after a reset is detected. This routine should write any necessary
60 * configuration settings to the device.
61 * @attention: Called when the IRQ(s) for the function are set by the touch
62 * sensor.
63 * @suspend: Should perform any required operations to suspend the particular
64 * function.
65 * @resume: Should perform any required operations to resume the particular
66 * function.
67 *
68 * All callbacks are expected to return 0 on success, error code on failure.
69 */
70struct rmi_function_handler {
71 struct device_driver driver;
72
73 u8 func;
74
75 int (*probe)(struct rmi_function *fn);
76 void (*remove)(struct rmi_function *fn);
77 int (*config)(struct rmi_function *fn);
78 int (*reset)(struct rmi_function *fn);
79 int (*attention)(struct rmi_function *fn, unsigned long *irq_bits);
80 int (*suspend)(struct rmi_function *fn);
81 int (*resume)(struct rmi_function *fn);
82};
83
84#define to_rmi_function_handler(d) \
85 container_of(d, struct rmi_function_handler, driver)
86
87int __must_check __rmi_register_function_handler(struct rmi_function_handler *,
88 struct module *, const char *);
89#define rmi_register_function_handler(handler) \
90 __rmi_register_function_handler(handler, THIS_MODULE, KBUILD_MODNAME)
91
92void rmi_unregister_function_handler(struct rmi_function_handler *);
93
94#define to_rmi_driver(d) \
95 container_of(d, struct rmi_driver, driver)
96
97#define to_rmi_device(d) container_of(d, struct rmi_device, dev)
98
99static inline struct rmi_device_platform_data *
100rmi_get_platform_data(struct rmi_device *d)
101{
102 return &d->xport->pdata;
103}
104
105bool rmi_is_physical_device(struct device *dev);
106
107/**
108 * rmi_read - read a single byte
109 * @d: Pointer to an RMI device
110 * @addr: The address to read from
111 * @buf: The read buffer
112 *
113 * Reads a single byte of data using the underlying transport protocol
114 * into memory pointed by @buf. It returns 0 on success or a negative
115 * error code.
116 */
117static inline int rmi_read(struct rmi_device *d, u16 addr, u8 *buf)
118{
119 return d->xport->ops->read_block(d->xport, addr, buf, 1);
120}
121
122/**
123 * rmi_read_block - read a block of bytes
124 * @d: Pointer to an RMI device
125 * @addr: The start address to read from
126 * @buf: The read buffer
127 * @len: Length of the read buffer
128 *
129 * Reads a block of byte data using the underlying transport protocol
130 * into memory pointed by @buf. It returns 0 on success or a negative
131 * error code.
132 */
133static inline int rmi_read_block(struct rmi_device *d, u16 addr,
134 void *buf, size_t len)
135{
136 return d->xport->ops->read_block(d->xport, addr, buf, len);
137}
138
139/**
140 * rmi_write - write a single byte
141 * @d: Pointer to an RMI device
142 * @addr: The address to write to
143 * @data: The data to write
144 *
145 * Writes a single byte using the underlying transport protocol. It
146 * returns zero on success or a negative error code.
147 */
148static inline int rmi_write(struct rmi_device *d, u16 addr, u8 data)
149{
150 return d->xport->ops->write_block(d->xport, addr, &data, 1);
151}
152
153/**
154 * rmi_write_block - write a block of bytes
155 * @d: Pointer to an RMI device
156 * @addr: The start address to write to
157 * @buf: The write buffer
158 * @len: Length of the write buffer
159 *
160 * Writes a block of byte data from buf using the underlaying transport
161 * protocol. It returns the amount of bytes written or a negative error code.
162 */
163static inline int rmi_write_block(struct rmi_device *d, u16 addr,
164 const void *buf, size_t len)
165{
166 return d->xport->ops->write_block(d->xport, addr, buf, len);
167}
168
169int rmi_for_each_dev(void *data, int (*func)(struct device *dev, void *data));
170
171extern struct bus_type rmi_bus_type;
172
173int rmi_of_property_read_u32(struct device *dev, u32 *result,
174 const char *prop, bool optional);
175
176#define RMI_DEBUG_CORE BIT(0)
177#define RMI_DEBUG_XPORT BIT(1)
178#define RMI_DEBUG_FN BIT(2)
179#define RMI_DEBUG_2D_SENSOR BIT(3)
180
181void rmi_dbg(int flags, struct device *dev, const char *fmt, ...);
182#endif
diff --git a/drivers/input/rmi4/rmi_driver.c b/drivers/input/rmi4/rmi_driver.c
new file mode 100644
index 000000000000..da38f0ad80ed
--- /dev/null
+++ b/drivers/input/rmi4/rmi_driver.c
@@ -0,0 +1,1055 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This driver provides the core support for a single RMI4-based device.
6 *
7 * The RMI4 specification can be found here (URL split for line length):
8 *
9 * http://www.synaptics.com/sites/default/files/
10 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 */
16
17#include <linux/bitmap.h>
18#include <linux/delay.h>
19#include <linux/fs.h>
20#include <linux/kconfig.h>
21#include <linux/pm.h>
22#include <linux/slab.h>
23#include <linux/of.h>
24#include <uapi/linux/input.h>
25#include <linux/rmi.h>
26#include "rmi_bus.h"
27#include "rmi_driver.h"
28
29#define HAS_NONSTANDARD_PDT_MASK 0x40
30#define RMI4_MAX_PAGE 0xff
31#define RMI4_PAGE_SIZE 0x100
32#define RMI4_PAGE_MASK 0xFF00
33
34#define RMI_DEVICE_RESET_CMD 0x01
35#define DEFAULT_RESET_DELAY_MS 100
36
37static void rmi_free_function_list(struct rmi_device *rmi_dev)
38{
39 struct rmi_function *fn, *tmp;
40 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
41
42 data->f01_container = NULL;
43
44 /* Doing it in the reverse order so F01 will be removed last */
45 list_for_each_entry_safe_reverse(fn, tmp,
46 &data->function_list, node) {
47 list_del(&fn->node);
48 rmi_unregister_function(fn);
49 }
50}
51
52static int reset_one_function(struct rmi_function *fn)
53{
54 struct rmi_function_handler *fh;
55 int retval = 0;
56
57 if (!fn || !fn->dev.driver)
58 return 0;
59
60 fh = to_rmi_function_handler(fn->dev.driver);
61 if (fh->reset) {
62 retval = fh->reset(fn);
63 if (retval < 0)
64 dev_err(&fn->dev, "Reset failed with code %d.\n",
65 retval);
66 }
67
68 return retval;
69}
70
71static int configure_one_function(struct rmi_function *fn)
72{
73 struct rmi_function_handler *fh;
74 int retval = 0;
75
76 if (!fn || !fn->dev.driver)
77 return 0;
78
79 fh = to_rmi_function_handler(fn->dev.driver);
80 if (fh->config) {
81 retval = fh->config(fn);
82 if (retval < 0)
83 dev_err(&fn->dev, "Config failed with code %d.\n",
84 retval);
85 }
86
87 return retval;
88}
89
90static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
91{
92 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
93 struct rmi_function *entry;
94 int retval;
95
96 list_for_each_entry(entry, &data->function_list, node) {
97 retval = reset_one_function(entry);
98 if (retval < 0)
99 return retval;
100 }
101
102 return 0;
103}
104
105static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
106{
107 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
108 struct rmi_function *entry;
109 int retval;
110
111 list_for_each_entry(entry, &data->function_list, node) {
112 retval = configure_one_function(entry);
113 if (retval < 0)
114 return retval;
115 }
116
117 return 0;
118}
119
120static void process_one_interrupt(struct rmi_driver_data *data,
121 struct rmi_function *fn)
122{
123 struct rmi_function_handler *fh;
124
125 if (!fn || !fn->dev.driver)
126 return;
127
128 fh = to_rmi_function_handler(fn->dev.driver);
129 if (fn->irq_mask && fh->attention) {
130 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
131 data->irq_count);
132 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
133 fh->attention(fn, data->fn_irq_bits);
134 }
135}
136
137int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
138{
139 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
140 struct device *dev = &rmi_dev->dev;
141 struct rmi_function *entry;
142 int error;
143
144 if (!data)
145 return 0;
146
147 if (!rmi_dev->xport->attn_data) {
148 error = rmi_read_block(rmi_dev,
149 data->f01_container->fd.data_base_addr + 1,
150 data->irq_status, data->num_of_irq_regs);
151 if (error < 0) {
152 dev_err(dev, "Failed to read irqs, code=%d\n", error);
153 return error;
154 }
155 }
156
157 mutex_lock(&data->irq_mutex);
158 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
159 data->irq_count);
160 /*
161 * At this point, irq_status has all bits that are set in the
162 * interrupt status register and are enabled.
163 */
164 mutex_unlock(&data->irq_mutex);
165
166 /*
167 * It would be nice to be able to use irq_chip to handle these
168 * nested IRQs. Unfortunately, most of the current customers for
169 * this driver are using older kernels (3.0.x) that don't support
170 * the features required for that. Once they've shifted to more
171 * recent kernels (say, 3.3 and higher), this should be switched to
172 * use irq_chip.
173 */
174 list_for_each_entry(entry, &data->function_list, node)
175 if (entry->irq_mask)
176 process_one_interrupt(data, entry);
177
178 if (data->input)
179 input_sync(data->input);
180
181 return 0;
182}
183EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
184
185static int suspend_one_function(struct rmi_function *fn)
186{
187 struct rmi_function_handler *fh;
188 int retval = 0;
189
190 if (!fn || !fn->dev.driver)
191 return 0;
192
193 fh = to_rmi_function_handler(fn->dev.driver);
194 if (fh->suspend) {
195 retval = fh->suspend(fn);
196 if (retval < 0)
197 dev_err(&fn->dev, "Suspend failed with code %d.\n",
198 retval);
199 }
200
201 return retval;
202}
203
204static int rmi_suspend_functions(struct rmi_device *rmi_dev)
205{
206 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
207 struct rmi_function *entry;
208 int retval;
209
210 list_for_each_entry(entry, &data->function_list, node) {
211 retval = suspend_one_function(entry);
212 if (retval < 0)
213 return retval;
214 }
215
216 return 0;
217}
218
219static int resume_one_function(struct rmi_function *fn)
220{
221 struct rmi_function_handler *fh;
222 int retval = 0;
223
224 if (!fn || !fn->dev.driver)
225 return 0;
226
227 fh = to_rmi_function_handler(fn->dev.driver);
228 if (fh->resume) {
229 retval = fh->resume(fn);
230 if (retval < 0)
231 dev_err(&fn->dev, "Resume failed with code %d.\n",
232 retval);
233 }
234
235 return retval;
236}
237
238static int rmi_resume_functions(struct rmi_device *rmi_dev)
239{
240 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
241 struct rmi_function *entry;
242 int retval;
243
244 list_for_each_entry(entry, &data->function_list, node) {
245 retval = resume_one_function(entry);
246 if (retval < 0)
247 return retval;
248 }
249
250 return 0;
251}
252
253static int enable_sensor(struct rmi_device *rmi_dev)
254{
255 int retval = 0;
256
257 retval = rmi_driver_process_config_requests(rmi_dev);
258 if (retval < 0)
259 return retval;
260
261 return rmi_process_interrupt_requests(rmi_dev);
262}
263
264/**
265 * rmi_driver_set_input_params - set input device id and other data.
266 *
267 * @rmi_dev: Pointer to an RMI device
268 * @input: Pointer to input device
269 *
270 */
271static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
272 struct input_dev *input)
273{
274 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
275 input->id.vendor = SYNAPTICS_VENDOR_ID;
276 input->id.bustype = BUS_RMI;
277 return 0;
278}
279
280static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
281 struct input_dev *input)
282{
283 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
284 char *device_name = rmi_f01_get_product_ID(data->f01_container);
285 char *name;
286
287 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
288 "Synaptics %s", device_name);
289 if (!name)
290 return;
291
292 input->name = name;
293}
294
295static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
296 unsigned long *mask)
297{
298 int error = 0;
299 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
300 struct device *dev = &rmi_dev->dev;
301
302 mutex_lock(&data->irq_mutex);
303 bitmap_or(data->new_irq_mask,
304 data->current_irq_mask, mask, data->irq_count);
305
306 error = rmi_write_block(rmi_dev,
307 data->f01_container->fd.control_base_addr + 1,
308 data->new_irq_mask, data->num_of_irq_regs);
309 if (error < 0) {
310 dev_err(dev, "%s: Failed to change enabled interrupts!",
311 __func__);
312 goto error_unlock;
313 }
314 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
315 data->num_of_irq_regs);
316
317error_unlock:
318 mutex_unlock(&data->irq_mutex);
319 return error;
320}
321
322static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
323 unsigned long *mask)
324{
325 int error = 0;
326 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
327 struct device *dev = &rmi_dev->dev;
328
329 mutex_lock(&data->irq_mutex);
330 bitmap_andnot(data->new_irq_mask,
331 data->current_irq_mask, mask, data->irq_count);
332
333 error = rmi_write_block(rmi_dev,
334 data->f01_container->fd.control_base_addr + 1,
335 data->new_irq_mask, data->num_of_irq_regs);
336 if (error < 0) {
337 dev_err(dev, "%s: Failed to change enabled interrupts!",
338 __func__);
339 goto error_unlock;
340 }
341 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
342 data->num_of_irq_regs);
343
344error_unlock:
345 mutex_unlock(&data->irq_mutex);
346 return error;
347}
348
349static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
350{
351 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
352 int error;
353
354 /*
355 * Can get called before the driver is fully ready to deal with
356 * this situation.
357 */
358 if (!data || !data->f01_container) {
359 dev_warn(&rmi_dev->dev,
360 "Not ready to handle reset yet!\n");
361 return 0;
362 }
363
364 error = rmi_read_block(rmi_dev,
365 data->f01_container->fd.control_base_addr + 1,
366 data->current_irq_mask, data->num_of_irq_regs);
367 if (error < 0) {
368 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
369 __func__);
370 return error;
371 }
372
373 error = rmi_driver_process_reset_requests(rmi_dev);
374 if (error < 0)
375 return error;
376
377 error = rmi_driver_process_config_requests(rmi_dev);
378 if (error < 0)
379 return error;
380
381 return 0;
382}
383
384int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
385 u16 pdt_address)
386{
387 u8 buf[RMI_PDT_ENTRY_SIZE];
388 int error;
389
390 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
391 if (error) {
392 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
393 pdt_address, error);
394 return error;
395 }
396
397 entry->page_start = pdt_address & RMI4_PAGE_MASK;
398 entry->query_base_addr = buf[0];
399 entry->command_base_addr = buf[1];
400 entry->control_base_addr = buf[2];
401 entry->data_base_addr = buf[3];
402 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
403 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
404 entry->function_number = buf[5];
405
406 return 0;
407}
408EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
409
410static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
411 struct rmi_function_descriptor *fd)
412{
413 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
414 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
415 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
416 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
417 fd->function_number = pdt->function_number;
418 fd->interrupt_source_count = pdt->interrupt_source_count;
419 fd->function_version = pdt->function_version;
420}
421
422#define RMI_SCAN_CONTINUE 0
423#define RMI_SCAN_DONE 1
424
425static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
426 int page,
427 void *ctx,
428 int (*callback)(struct rmi_device *rmi_dev,
429 void *ctx,
430 const struct pdt_entry *entry))
431{
432 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
433 struct pdt_entry pdt_entry;
434 u16 page_start = RMI4_PAGE_SIZE * page;
435 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
436 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
437 u16 addr;
438 int error;
439 int retval;
440
441 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
442 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
443 if (error)
444 return error;
445
446 if (RMI4_END_OF_PDT(pdt_entry.function_number))
447 break;
448
449 retval = callback(rmi_dev, ctx, &pdt_entry);
450 if (retval != RMI_SCAN_CONTINUE)
451 return retval;
452 }
453
454 return (data->f01_bootloader_mode || addr == pdt_start) ?
455 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
456}
457
458static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
459 int (*callback)(struct rmi_device *rmi_dev,
460 void *ctx,
461 const struct pdt_entry *entry))
462{
463 int page;
464 int retval = RMI_SCAN_DONE;
465
466 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
467 retval = rmi_scan_pdt_page(rmi_dev, page, ctx, callback);
468 if (retval != RMI_SCAN_CONTINUE)
469 break;
470 }
471
472 return retval < 0 ? retval : 0;
473}
474
475int rmi_read_register_desc(struct rmi_device *d, u16 addr,
476 struct rmi_register_descriptor *rdesc)
477{
478 int ret;
479 u8 size_presence_reg;
480 u8 buf[35];
481 int presense_offset = 1;
482 u8 *struct_buf;
483 int reg;
484 int offset = 0;
485 int map_offset = 0;
486 int i;
487 int b;
488
489 /*
490 * The first register of the register descriptor is the size of
491 * the register descriptor's presense register.
492 */
493 ret = rmi_read(d, addr, &size_presence_reg);
494 if (ret)
495 return ret;
496 ++addr;
497
498 if (size_presence_reg < 0 || size_presence_reg > 35)
499 return -EIO;
500
501 memset(buf, 0, sizeof(buf));
502
503 /*
504 * The presence register contains the size of the register structure
505 * and a bitmap which identified which packet registers are present
506 * for this particular register type (ie query, control, or data).
507 */
508 ret = rmi_read_block(d, addr, buf, size_presence_reg);
509 if (ret)
510 return ret;
511 ++addr;
512
513 if (buf[0] == 0) {
514 presense_offset = 3;
515 rdesc->struct_size = buf[1] | (buf[2] << 8);
516 } else {
517 rdesc->struct_size = buf[0];
518 }
519
520 for (i = presense_offset; i < size_presence_reg; i++) {
521 for (b = 0; b < 8; b++) {
522 if (buf[i] & (0x1 << b))
523 bitmap_set(rdesc->presense_map, map_offset, 1);
524 ++map_offset;
525 }
526 }
527
528 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
529 RMI_REG_DESC_PRESENSE_BITS);
530
531 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
532 sizeof(struct rmi_register_desc_item),
533 GFP_KERNEL);
534 if (!rdesc->registers)
535 return -ENOMEM;
536
537 /*
538 * Allocate a temporary buffer to hold the register structure.
539 * I'm not using devm_kzalloc here since it will not be retained
540 * after exiting this function
541 */
542 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
543 if (!struct_buf)
544 return -ENOMEM;
545
546 /*
547 * The register structure contains information about every packet
548 * register of this type. This includes the size of the packet
549 * register and a bitmap of all subpackets contained in the packet
550 * register.
551 */
552 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
553 if (ret)
554 goto free_struct_buff;
555
556 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
557 map_offset = 0;
558 for (i = 0; i < rdesc->num_registers; i++) {
559 struct rmi_register_desc_item *item = &rdesc->registers[i];
560 int reg_size = struct_buf[offset];
561
562 ++offset;
563 if (reg_size == 0) {
564 reg_size = struct_buf[offset] |
565 (struct_buf[offset + 1] << 8);
566 offset += 2;
567 }
568
569 if (reg_size == 0) {
570 reg_size = struct_buf[offset] |
571 (struct_buf[offset + 1] << 8) |
572 (struct_buf[offset + 2] << 16) |
573 (struct_buf[offset + 3] << 24);
574 offset += 4;
575 }
576
577 item->reg = reg;
578 item->reg_size = reg_size;
579
580 do {
581 for (b = 0; b < 7; b++) {
582 if (struct_buf[offset] & (0x1 << b))
583 bitmap_set(item->subpacket_map,
584 map_offset, 1);
585 ++map_offset;
586 }
587 } while (struct_buf[offset++] & 0x80);
588
589 item->num_subpackets = bitmap_weight(item->subpacket_map,
590 RMI_REG_DESC_SUBPACKET_BITS);
591
592 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
593 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
594 item->reg, item->reg_size, item->num_subpackets);
595
596 reg = find_next_bit(rdesc->presense_map,
597 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
598 }
599
600free_struct_buff:
601 kfree(struct_buf);
602 return ret;
603}
604EXPORT_SYMBOL_GPL(rmi_read_register_desc);
605
606const struct rmi_register_desc_item *rmi_get_register_desc_item(
607 struct rmi_register_descriptor *rdesc, u16 reg)
608{
609 const struct rmi_register_desc_item *item;
610 int i;
611
612 for (i = 0; i < rdesc->num_registers; i++) {
613 item = &rdesc->registers[i];
614 if (item->reg == reg)
615 return item;
616 }
617
618 return NULL;
619}
620EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
621
622size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
623{
624 const struct rmi_register_desc_item *item;
625 int i;
626 size_t size = 0;
627
628 for (i = 0; i < rdesc->num_registers; i++) {
629 item = &rdesc->registers[i];
630 size += item->reg_size;
631 }
632 return size;
633}
634EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
635
636/* Compute the register offset relative to the base address */
637int rmi_register_desc_calc_reg_offset(
638 struct rmi_register_descriptor *rdesc, u16 reg)
639{
640 const struct rmi_register_desc_item *item;
641 int offset = 0;
642 int i;
643
644 for (i = 0; i < rdesc->num_registers; i++) {
645 item = &rdesc->registers[i];
646 if (item->reg == reg)
647 return offset;
648 ++offset;
649 }
650 return -1;
651}
652EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
653
654bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
655 u8 subpacket)
656{
657 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
658 subpacket) == subpacket;
659}
660
661/* Indicates that flash programming is enabled (bootloader mode). */
662#define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
663
664/*
665 * Given the PDT entry for F01, read the device status register to determine
666 * if we're stuck in bootloader mode or not.
667 *
668 */
669static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
670 const struct pdt_entry *pdt)
671{
672 int error;
673 u8 device_status;
674
675 error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
676 &device_status);
677 if (error) {
678 dev_err(&rmi_dev->dev,
679 "Failed to read device status: %d.\n", error);
680 return error;
681 }
682
683 return RMI_F01_STATUS_BOOTLOADER(device_status);
684}
685
686static int rmi_count_irqs(struct rmi_device *rmi_dev,
687 void *ctx, const struct pdt_entry *pdt)
688{
689 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
690 int *irq_count = ctx;
691
692 *irq_count += pdt->interrupt_source_count;
693 if (pdt->function_number == 0x01) {
694 data->f01_bootloader_mode =
695 rmi_check_bootloader_mode(rmi_dev, pdt);
696 if (data->f01_bootloader_mode)
697 dev_warn(&rmi_dev->dev,
698 "WARNING: RMI4 device is in bootloader mode!\n");
699 }
700
701 return RMI_SCAN_CONTINUE;
702}
703
704static int rmi_initial_reset(struct rmi_device *rmi_dev,
705 void *ctx, const struct pdt_entry *pdt)
706{
707 int error;
708
709 if (pdt->function_number == 0x01) {
710 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
711 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
712 const struct rmi_device_platform_data *pdata =
713 rmi_get_platform_data(rmi_dev);
714
715 if (rmi_dev->xport->ops->reset) {
716 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
717 cmd_addr);
718 if (error)
719 return error;
720
721 return RMI_SCAN_DONE;
722 }
723
724 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
725 if (error) {
726 dev_err(&rmi_dev->dev,
727 "Initial reset failed. Code = %d.\n", error);
728 return error;
729 }
730
731 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
732
733 return RMI_SCAN_DONE;
734 }
735
736 /* F01 should always be on page 0. If we don't find it there, fail. */
737 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
738}
739
740static int rmi_create_function(struct rmi_device *rmi_dev,
741 void *ctx, const struct pdt_entry *pdt)
742{
743 struct device *dev = &rmi_dev->dev;
744 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
745 int *current_irq_count = ctx;
746 struct rmi_function *fn;
747 int i;
748 int error;
749
750 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
751 pdt->function_number);
752
753 fn = kzalloc(sizeof(struct rmi_function) +
754 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
755 GFP_KERNEL);
756 if (!fn) {
757 dev_err(dev, "Failed to allocate memory for F%02X\n",
758 pdt->function_number);
759 return -ENOMEM;
760 }
761
762 INIT_LIST_HEAD(&fn->node);
763 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
764
765 fn->rmi_dev = rmi_dev;
766
767 fn->num_of_irqs = pdt->interrupt_source_count;
768 fn->irq_pos = *current_irq_count;
769 *current_irq_count += fn->num_of_irqs;
770
771 for (i = 0; i < fn->num_of_irqs; i++)
772 set_bit(fn->irq_pos + i, fn->irq_mask);
773
774 error = rmi_register_function(fn);
775 if (error)
776 goto err_put_fn;
777
778 if (pdt->function_number == 0x01)
779 data->f01_container = fn;
780
781 list_add_tail(&fn->node, &data->function_list);
782
783 return RMI_SCAN_CONTINUE;
784
785err_put_fn:
786 put_device(&fn->dev);
787 return error;
788}
789
790int rmi_driver_suspend(struct rmi_device *rmi_dev)
791{
792 int retval = 0;
793
794 retval = rmi_suspend_functions(rmi_dev);
795 if (retval)
796 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
797 retval);
798
799 return retval;
800}
801EXPORT_SYMBOL_GPL(rmi_driver_suspend);
802
803int rmi_driver_resume(struct rmi_device *rmi_dev)
804{
805 int retval;
806
807 retval = rmi_resume_functions(rmi_dev);
808 if (retval)
809 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
810 retval);
811
812 return retval;
813}
814EXPORT_SYMBOL_GPL(rmi_driver_resume);
815
816static int rmi_driver_remove(struct device *dev)
817{
818 struct rmi_device *rmi_dev = to_rmi_device(dev);
819
820 rmi_free_function_list(rmi_dev);
821
822 return 0;
823}
824
825#ifdef CONFIG_OF
826static int rmi_driver_of_probe(struct device *dev,
827 struct rmi_device_platform_data *pdata)
828{
829 int retval;
830
831 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
832 "syna,reset-delay-ms", 1);
833 if (retval)
834 return retval;
835
836 return 0;
837}
838#else
839static inline int rmi_driver_of_probe(struct device *dev,
840 struct rmi_device_platform_data *pdata)
841{
842 return -ENODEV;
843}
844#endif
845
846static int rmi_driver_probe(struct device *dev)
847{
848 struct rmi_driver *rmi_driver;
849 struct rmi_driver_data *data;
850 struct rmi_device_platform_data *pdata;
851 struct rmi_device *rmi_dev;
852 size_t size;
853 void *irq_memory;
854 int irq_count;
855 int retval;
856
857 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
858 __func__);
859
860 if (!rmi_is_physical_device(dev)) {
861 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
862 return -ENODEV;
863 }
864
865 rmi_dev = to_rmi_device(dev);
866 rmi_driver = to_rmi_driver(dev->driver);
867 rmi_dev->driver = rmi_driver;
868
869 pdata = rmi_get_platform_data(rmi_dev);
870
871 if (rmi_dev->xport->dev->of_node) {
872 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
873 if (retval)
874 return retval;
875 }
876
877 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
878 if (!data)
879 return -ENOMEM;
880
881 INIT_LIST_HEAD(&data->function_list);
882 data->rmi_dev = rmi_dev;
883 dev_set_drvdata(&rmi_dev->dev, data);
884
885 /*
886 * Right before a warm boot, the sensor might be in some unusual state,
887 * such as F54 diagnostics, or F34 bootloader mode after a firmware
888 * or configuration update. In order to clear the sensor to a known
889 * state and/or apply any updates, we issue a initial reset to clear any
890 * previous settings and force it into normal operation.
891 *
892 * We have to do this before actually building the PDT because
893 * the reflash updates (if any) might cause various registers to move
894 * around.
895 *
896 * For a number of reasons, this initial reset may fail to return
897 * within the specified time, but we'll still be able to bring up the
898 * driver normally after that failure. This occurs most commonly in
899 * a cold boot situation (where then firmware takes longer to come up
900 * than from a warm boot) and the reset_delay_ms in the platform data
901 * has been set too short to accommodate that. Since the sensor will
902 * eventually come up and be usable, we don't want to just fail here
903 * and leave the customer's device unusable. So we warn them, and
904 * continue processing.
905 */
906 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
907 if (retval < 0)
908 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
909
910 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
911 if (retval < 0) {
912 /*
913 * we'll print out a warning and continue since
914 * failure to get the PDT properties is not a cause to fail
915 */
916 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
917 PDT_PROPERTIES_LOCATION, retval);
918 }
919
920 /*
921 * We need to count the IRQs and allocate their storage before scanning
922 * the PDT and creating the function entries, because adding a new
923 * function can trigger events that result in the IRQ related storage
924 * being accessed.
925 */
926 rmi_dbg(RMI_DEBUG_CORE, dev, "Counting IRQs.\n");
927 irq_count = 0;
928 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
929 if (retval < 0) {
930 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
931 goto err;
932 }
933 data->irq_count = irq_count;
934 data->num_of_irq_regs = (data->irq_count + 7) / 8;
935
936 mutex_init(&data->irq_mutex);
937
938 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
939 irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
940 if (!irq_memory) {
941 dev_err(dev, "Failed to allocate memory for irq masks.\n");
942 goto err;
943 }
944
945 data->irq_status = irq_memory + size * 0;
946 data->fn_irq_bits = irq_memory + size * 1;
947 data->current_irq_mask = irq_memory + size * 2;
948 data->new_irq_mask = irq_memory + size * 3;
949
950 if (rmi_dev->xport->input) {
951 /*
952 * The transport driver already has an input device.
953 * In some cases it is preferable to reuse the transport
954 * devices input device instead of creating a new one here.
955 * One example is some HID touchpads report "pass-through"
956 * button events are not reported by rmi registers.
957 */
958 data->input = rmi_dev->xport->input;
959 } else {
960 data->input = devm_input_allocate_device(dev);
961 if (!data->input) {
962 dev_err(dev, "%s: Failed to allocate input device.\n",
963 __func__);
964 retval = -ENOMEM;
965 goto err_destroy_functions;
966 }
967 rmi_driver_set_input_params(rmi_dev, data->input);
968 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
969 "%s/input0", dev_name(dev));
970 }
971
972 irq_count = 0;
973 rmi_dbg(RMI_DEBUG_CORE, dev, "Creating functions.");
974 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
975 if (retval < 0) {
976 dev_err(dev, "Function creation failed with code %d.\n",
977 retval);
978 goto err_destroy_functions;
979 }
980
981 if (!data->f01_container) {
982 dev_err(dev, "Missing F01 container!\n");
983 retval = -EINVAL;
984 goto err_destroy_functions;
985 }
986
987 retval = rmi_read_block(rmi_dev,
988 data->f01_container->fd.control_base_addr + 1,
989 data->current_irq_mask, data->num_of_irq_regs);
990 if (retval < 0) {
991 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
992 __func__);
993 goto err_destroy_functions;
994 }
995
996 if (data->input) {
997 rmi_driver_set_input_name(rmi_dev, data->input);
998 if (!rmi_dev->xport->input) {
999 if (input_register_device(data->input)) {
1000 dev_err(dev, "%s: Failed to register input device.\n",
1001 __func__);
1002 goto err_destroy_functions;
1003 }
1004 }
1005 }
1006
1007 if (data->f01_container->dev.driver)
1008 /* Driver already bound, so enable ATTN now. */
1009 return enable_sensor(rmi_dev);
1010
1011 return 0;
1012
1013err_destroy_functions:
1014 rmi_free_function_list(rmi_dev);
1015err:
1016 return retval < 0 ? retval : 0;
1017}
1018
1019static struct rmi_driver rmi_physical_driver = {
1020 .driver = {
1021 .owner = THIS_MODULE,
1022 .name = "rmi4_physical",
1023 .bus = &rmi_bus_type,
1024 .probe = rmi_driver_probe,
1025 .remove = rmi_driver_remove,
1026 },
1027 .reset_handler = rmi_driver_reset_handler,
1028 .clear_irq_bits = rmi_driver_clear_irq_bits,
1029 .set_irq_bits = rmi_driver_set_irq_bits,
1030 .set_input_params = rmi_driver_set_input_params,
1031};
1032
1033bool rmi_is_physical_driver(struct device_driver *drv)
1034{
1035 return drv == &rmi_physical_driver.driver;
1036}
1037
1038int __init rmi_register_physical_driver(void)
1039{
1040 int error;
1041
1042 error = driver_register(&rmi_physical_driver.driver);
1043 if (error) {
1044 pr_err("%s: driver register failed, code=%d.\n", __func__,
1045 error);
1046 return error;
1047 }
1048
1049 return 0;
1050}
1051
1052void __exit rmi_unregister_physical_driver(void)
1053{
1054 driver_unregister(&rmi_physical_driver.driver);
1055}
diff --git a/drivers/input/rmi4/rmi_driver.h b/drivers/input/rmi4/rmi_driver.h
new file mode 100644
index 000000000000..6e140fa3cce1
--- /dev/null
+++ b/drivers/input/rmi4/rmi_driver.h
@@ -0,0 +1,105 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#ifndef _RMI_DRIVER_H
11#define _RMI_DRIVER_H
12
13#include <linux/ctype.h>
14#include <linux/hrtimer.h>
15#include <linux/ktime.h>
16#include <linux/input.h>
17#include "rmi_bus.h"
18
19#define RMI_DRIVER_VERSION "2.0"
20
21#define SYNAPTICS_INPUT_DEVICE_NAME "Synaptics RMI4 Touch Sensor"
22#define SYNAPTICS_VENDOR_ID 0x06cb
23
24#define GROUP(_attrs) { \
25 .attrs = _attrs, \
26}
27
28#define PDT_PROPERTIES_LOCATION 0x00EF
29#define BSR_LOCATION 0x00FE
30
31#define RMI_PDT_PROPS_HAS_BSR 0x02
32
33#define NAME_BUFFER_SIZE 256
34
35#define RMI_PDT_ENTRY_SIZE 6
36#define RMI_PDT_FUNCTION_VERSION_MASK 0x60
37#define RMI_PDT_INT_SOURCE_COUNT_MASK 0x07
38
39#define PDT_START_SCAN_LOCATION 0x00e9
40#define PDT_END_SCAN_LOCATION 0x0005
41#define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
42
43struct pdt_entry {
44 u16 page_start;
45 u8 query_base_addr;
46 u8 command_base_addr;
47 u8 control_base_addr;
48 u8 data_base_addr;
49 u8 interrupt_source_count;
50 u8 function_version;
51 u8 function_number;
52};
53
54int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
55 u16 pdt_address);
56
57#define RMI_REG_DESC_PRESENSE_BITS (32 * BITS_PER_BYTE)
58#define RMI_REG_DESC_SUBPACKET_BITS (37 * BITS_PER_BYTE)
59
60/* describes a single packet register */
61struct rmi_register_desc_item {
62 u16 reg;
63 unsigned long reg_size;
64 u8 num_subpackets;
65 unsigned long subpacket_map[BITS_TO_LONGS(
66 RMI_REG_DESC_SUBPACKET_BITS)];
67};
68
69/*
70 * describes the packet registers for a particular type
71 * (ie query, control, data)
72 */
73struct rmi_register_descriptor {
74 unsigned long struct_size;
75 unsigned long presense_map[BITS_TO_LONGS(RMI_REG_DESC_PRESENSE_BITS)];
76 u8 num_registers;
77 struct rmi_register_desc_item *registers;
78};
79
80int rmi_read_register_desc(struct rmi_device *d, u16 addr,
81 struct rmi_register_descriptor *rdesc);
82const struct rmi_register_desc_item *rmi_get_register_desc_item(
83 struct rmi_register_descriptor *rdesc, u16 reg);
84
85/*
86 * Calculate the total size of all of the registers described in the
87 * descriptor.
88 */
89size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc);
90int rmi_register_desc_calc_reg_offset(
91 struct rmi_register_descriptor *rdesc, u16 reg);
92bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
93 u8 subpacket);
94
95bool rmi_is_physical_driver(struct device_driver *);
96int rmi_register_physical_driver(void);
97void rmi_unregister_physical_driver(void);
98
99char *rmi_f01_get_product_ID(struct rmi_function *fn);
100
101extern struct rmi_function_handler rmi_f01_handler;
102extern struct rmi_function_handler rmi_f11_handler;
103extern struct rmi_function_handler rmi_f12_handler;
104extern struct rmi_function_handler rmi_f30_handler;
105#endif
diff --git a/drivers/input/rmi4/rmi_f01.c b/drivers/input/rmi4/rmi_f01.c
new file mode 100644
index 000000000000..eb362bc71a4c
--- /dev/null
+++ b/drivers/input/rmi4/rmi_f01.c
@@ -0,0 +1,624 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/kconfig.h>
12#include <linux/rmi.h>
13#include <linux/slab.h>
14#include <linux/uaccess.h>
15#include <linux/of.h>
16#include "rmi_driver.h"
17
18#define RMI_PRODUCT_ID_LENGTH 10
19#define RMI_PRODUCT_INFO_LENGTH 2
20
21#define RMI_DATE_CODE_LENGTH 3
22
23#define PRODUCT_ID_OFFSET 0x10
24#define PRODUCT_INFO_OFFSET 0x1E
25
26
27/* Force a firmware reset of the sensor */
28#define RMI_F01_CMD_DEVICE_RESET 1
29
30/* Various F01_RMI_QueryX bits */
31
32#define RMI_F01_QRY1_CUSTOM_MAP BIT(0)
33#define RMI_F01_QRY1_NON_COMPLIANT BIT(1)
34#define RMI_F01_QRY1_HAS_LTS BIT(2)
35#define RMI_F01_QRY1_HAS_SENSOR_ID BIT(3)
36#define RMI_F01_QRY1_HAS_CHARGER_INP BIT(4)
37#define RMI_F01_QRY1_HAS_ADJ_DOZE BIT(5)
38#define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF BIT(6)
39#define RMI_F01_QRY1_HAS_QUERY42 BIT(7)
40
41#define RMI_F01_QRY5_YEAR_MASK 0x1f
42#define RMI_F01_QRY6_MONTH_MASK 0x0f
43#define RMI_F01_QRY7_DAY_MASK 0x1f
44
45#define RMI_F01_QRY2_PRODINFO_MASK 0x7f
46
47#define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
48
49struct f01_basic_properties {
50 u8 manufacturer_id;
51 bool has_lts;
52 bool has_adjustable_doze;
53 bool has_adjustable_doze_holdoff;
54 char dom[11]; /* YYYY/MM/DD + '\0' */
55 u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
56 u16 productinfo;
57 u32 firmware_id;
58};
59
60/* F01 device status bits */
61
62/* Most recent device status event */
63#define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
64/* The device has lost its configuration for some reason. */
65#define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
66
67/* Control register bits */
68
69/*
70 * Sleep mode controls power management on the device and affects all
71 * functions of the device.
72 */
73#define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
74
75#define RMI_SLEEP_MODE_NORMAL 0x00
76#define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
77#define RMI_SLEEP_MODE_RESERVED0 0x02
78#define RMI_SLEEP_MODE_RESERVED1 0x03
79
80/*
81 * This bit disables whatever sleep mode may be selected by the sleep_mode
82 * field and forces the device to run at full power without sleeping.
83 */
84#define RMI_F01_CRTL0_NOSLEEP_BIT BIT(2)
85
86/*
87 * When this bit is set, the touch controller employs a noise-filtering
88 * algorithm designed for use with a connected battery charger.
89 */
90#define RMI_F01_CRTL0_CHARGER_BIT BIT(5)
91
92/*
93 * Sets the report rate for the device. The effect of this setting is
94 * highly product dependent. Check the spec sheet for your particular
95 * touch sensor.
96 */
97#define RMI_F01_CRTL0_REPORTRATE_BIT BIT(6)
98
99/*
100 * Written by the host as an indicator that the device has been
101 * successfully configured.
102 */
103#define RMI_F01_CRTL0_CONFIGURED_BIT BIT(7)
104
105/**
106 * @ctrl0 - see the bit definitions above.
107 * @doze_interval - controls the interval between checks for finger presence
108 * when the touch sensor is in doze mode, in units of 10ms.
109 * @wakeup_threshold - controls the capacitance threshold at which the touch
110 * sensor will decide to wake up from that low power state.
111 * @doze_holdoff - controls how long the touch sensor waits after the last
112 * finger lifts before entering the doze state, in units of 100ms.
113 */
114struct f01_device_control {
115 u8 ctrl0;
116 u8 doze_interval;
117 u8 wakeup_threshold;
118 u8 doze_holdoff;
119};
120
121struct f01_data {
122 struct f01_basic_properties properties;
123 struct f01_device_control device_control;
124
125 u16 doze_interval_addr;
126 u16 wakeup_threshold_addr;
127 u16 doze_holdoff_addr;
128
129 bool suspended;
130 bool old_nosleep;
131
132 unsigned int num_of_irq_regs;
133};
134
135static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
136 u16 query_base_addr,
137 struct f01_basic_properties *props)
138{
139 u8 queries[RMI_F01_BASIC_QUERY_LEN];
140 int ret;
141 int query_offset = query_base_addr;
142 bool has_ds4_queries = false;
143 bool has_query42 = false;
144 bool has_sensor_id = false;
145 bool has_package_id_query = false;
146 bool has_build_id_query = false;
147 u16 prod_info_addr;
148 u8 ds4_query_len;
149
150 ret = rmi_read_block(rmi_dev, query_offset,
151 queries, RMI_F01_BASIC_QUERY_LEN);
152 if (ret) {
153 dev_err(&rmi_dev->dev,
154 "Failed to read device query registers: %d\n", ret);
155 return ret;
156 }
157
158 prod_info_addr = query_offset + 17;
159 query_offset += RMI_F01_BASIC_QUERY_LEN;
160
161 /* Now parse what we got */
162 props->manufacturer_id = queries[0];
163
164 props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
165 props->has_adjustable_doze =
166 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
167 props->has_adjustable_doze_holdoff =
168 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
169 has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
170 has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
171
172 snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
173 queries[5] & RMI_F01_QRY5_YEAR_MASK,
174 queries[6] & RMI_F01_QRY6_MONTH_MASK,
175 queries[7] & RMI_F01_QRY7_DAY_MASK);
176
177 memcpy(props->product_id, &queries[11],
178 RMI_PRODUCT_ID_LENGTH);
179 props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
180
181 props->productinfo =
182 ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
183 (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
184
185 if (has_sensor_id)
186 query_offset++;
187
188 if (has_query42) {
189 ret = rmi_read(rmi_dev, query_offset, queries);
190 if (ret) {
191 dev_err(&rmi_dev->dev,
192 "Failed to read query 42 register: %d\n", ret);
193 return ret;
194 }
195
196 has_ds4_queries = !!(queries[0] & BIT(0));
197 query_offset++;
198 }
199
200 if (has_ds4_queries) {
201 ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
202 if (ret) {
203 dev_err(&rmi_dev->dev,
204 "Failed to read DS4 queries length: %d\n", ret);
205 return ret;
206 }
207 query_offset++;
208
209 if (ds4_query_len > 0) {
210 ret = rmi_read(rmi_dev, query_offset, queries);
211 if (ret) {
212 dev_err(&rmi_dev->dev,
213 "Failed to read DS4 queries: %d\n",
214 ret);
215 return ret;
216 }
217
218 has_package_id_query = !!(queries[0] & BIT(0));
219 has_build_id_query = !!(queries[0] & BIT(1));
220 }
221
222 if (has_package_id_query)
223 prod_info_addr++;
224
225 if (has_build_id_query) {
226 ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
227 3);
228 if (ret) {
229 dev_err(&rmi_dev->dev,
230 "Failed to read product info: %d\n",
231 ret);
232 return ret;
233 }
234
235 props->firmware_id = queries[1] << 8 | queries[0];
236 props->firmware_id += queries[2] * 65536;
237 }
238 }
239
240 return 0;
241}
242
243char *rmi_f01_get_product_ID(struct rmi_function *fn)
244{
245 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
246
247 return f01->properties.product_id;
248}
249
250#ifdef CONFIG_OF
251static int rmi_f01_of_probe(struct device *dev,
252 struct rmi_device_platform_data *pdata)
253{
254 int retval;
255 u32 val;
256
257 retval = rmi_of_property_read_u32(dev,
258 (u32 *)&pdata->power_management.nosleep,
259 "syna,nosleep-mode", 1);
260 if (retval)
261 return retval;
262
263 retval = rmi_of_property_read_u32(dev, &val,
264 "syna,wakeup-threshold", 1);
265 if (retval)
266 return retval;
267
268 pdata->power_management.wakeup_threshold = val;
269
270 retval = rmi_of_property_read_u32(dev, &val,
271 "syna,doze-holdoff-ms", 1);
272 if (retval)
273 return retval;
274
275 pdata->power_management.doze_holdoff = val * 100;
276
277 retval = rmi_of_property_read_u32(dev, &val,
278 "syna,doze-interval-ms", 1);
279 if (retval)
280 return retval;
281
282 pdata->power_management.doze_interval = val / 10;
283
284 return 0;
285}
286#else
287static inline int rmi_f01_of_probe(struct device *dev,
288 struct rmi_device_platform_data *pdata)
289{
290 return -ENODEV;
291}
292#endif
293
294static int rmi_f01_probe(struct rmi_function *fn)
295{
296 struct rmi_device *rmi_dev = fn->rmi_dev;
297 struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
298 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
299 struct f01_data *f01;
300 int error;
301 u16 ctrl_base_addr = fn->fd.control_base_addr;
302 u8 device_status;
303 u8 temp;
304
305 if (fn->dev.of_node) {
306 error = rmi_f01_of_probe(&fn->dev, pdata);
307 if (error)
308 return error;
309 }
310
311 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
312 if (!f01)
313 return -ENOMEM;
314
315 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
316
317 /*
318 * Set the configured bit and (optionally) other important stuff
319 * in the device control register.
320 */
321
322 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
323 &f01->device_control.ctrl0);
324 if (error) {
325 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
326 return error;
327 }
328
329 switch (pdata->power_management.nosleep) {
330 case RMI_F01_NOSLEEP_DEFAULT:
331 break;
332 case RMI_F01_NOSLEEP_OFF:
333 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
334 break;
335 case RMI_F01_NOSLEEP_ON:
336 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
337 break;
338 }
339
340 /*
341 * Sleep mode might be set as a hangover from a system crash or
342 * reboot without power cycle. If so, clear it so the sensor
343 * is certain to function.
344 */
345 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
346 RMI_SLEEP_MODE_NORMAL) {
347 dev_warn(&fn->dev,
348 "WARNING: Non-zero sleep mode found. Clearing...\n");
349 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
350 }
351
352 f01->device_control.ctrl0 |= RMI_F01_CRTL0_CONFIGURED_BIT;
353
354 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
355 f01->device_control.ctrl0);
356 if (error) {
357 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
358 return error;
359 }
360
361 /* Dummy read in order to clear irqs */
362 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
363 if (error < 0) {
364 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
365 return error;
366 }
367
368 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
369 &f01->properties);
370 if (error < 0) {
371 dev_err(&fn->dev, "Failed to read F01 properties.\n");
372 return error;
373 }
374
375 dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
376 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
377 f01->properties.product_id, f01->properties.firmware_id);
378
379 /* Advance to interrupt control registers, then skip over them. */
380 ctrl_base_addr++;
381 ctrl_base_addr += f01->num_of_irq_regs;
382
383 /* read control register */
384 if (f01->properties.has_adjustable_doze) {
385 f01->doze_interval_addr = ctrl_base_addr;
386 ctrl_base_addr++;
387
388 if (pdata->power_management.doze_interval) {
389 f01->device_control.doze_interval =
390 pdata->power_management.doze_interval;
391 error = rmi_write(rmi_dev, f01->doze_interval_addr,
392 f01->device_control.doze_interval);
393 if (error) {
394 dev_err(&fn->dev,
395 "Failed to configure F01 doze interval register: %d\n",
396 error);
397 return error;
398 }
399 } else {
400 error = rmi_read(rmi_dev, f01->doze_interval_addr,
401 &f01->device_control.doze_interval);
402 if (error) {
403 dev_err(&fn->dev,
404 "Failed to read F01 doze interval register: %d\n",
405 error);
406 return error;
407 }
408 }
409
410 f01->wakeup_threshold_addr = ctrl_base_addr;
411 ctrl_base_addr++;
412
413 if (pdata->power_management.wakeup_threshold) {
414 f01->device_control.wakeup_threshold =
415 pdata->power_management.wakeup_threshold;
416 error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
417 f01->device_control.wakeup_threshold);
418 if (error) {
419 dev_err(&fn->dev,
420 "Failed to configure F01 wakeup threshold register: %d\n",
421 error);
422 return error;
423 }
424 } else {
425 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
426 &f01->device_control.wakeup_threshold);
427 if (error < 0) {
428 dev_err(&fn->dev,
429 "Failed to read F01 wakeup threshold register: %d\n",
430 error);
431 return error;
432 }
433 }
434 }
435
436 if (f01->properties.has_lts)
437 ctrl_base_addr++;
438
439 if (f01->properties.has_adjustable_doze_holdoff) {
440 f01->doze_holdoff_addr = ctrl_base_addr;
441 ctrl_base_addr++;
442
443 if (pdata->power_management.doze_holdoff) {
444 f01->device_control.doze_holdoff =
445 pdata->power_management.doze_holdoff;
446 error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
447 f01->device_control.doze_holdoff);
448 if (error) {
449 dev_err(&fn->dev,
450 "Failed to configure F01 doze holdoff register: %d\n",
451 error);
452 return error;
453 }
454 } else {
455 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
456 &f01->device_control.doze_holdoff);
457 if (error) {
458 dev_err(&fn->dev,
459 "Failed to read F01 doze holdoff register: %d\n",
460 error);
461 return error;
462 }
463 }
464 }
465
466 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
467 if (error < 0) {
468 dev_err(&fn->dev,
469 "Failed to read device status: %d\n", error);
470 return error;
471 }
472
473 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
474 dev_err(&fn->dev,
475 "Device was reset during configuration process, status: %#02x!\n",
476 RMI_F01_STATUS_CODE(device_status));
477 return -EINVAL;
478 }
479
480 dev_set_drvdata(&fn->dev, f01);
481
482 return 0;
483}
484
485static int rmi_f01_config(struct rmi_function *fn)
486{
487 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
488 int error;
489
490 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
491 f01->device_control.ctrl0);
492 if (error) {
493 dev_err(&fn->dev,
494 "Failed to write device_control register: %d\n", error);
495 return error;
496 }
497
498 if (f01->properties.has_adjustable_doze) {
499 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
500 f01->device_control.doze_interval);
501 if (error) {
502 dev_err(&fn->dev,
503 "Failed to write doze interval: %d\n", error);
504 return error;
505 }
506
507 error = rmi_write_block(fn->rmi_dev,
508 f01->wakeup_threshold_addr,
509 &f01->device_control.wakeup_threshold,
510 sizeof(u8));
511 if (error) {
512 dev_err(&fn->dev,
513 "Failed to write wakeup threshold: %d\n",
514 error);
515 return error;
516 }
517 }
518
519 if (f01->properties.has_adjustable_doze_holdoff) {
520 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
521 f01->device_control.doze_holdoff);
522 if (error) {
523 dev_err(&fn->dev,
524 "Failed to write doze holdoff: %d\n", error);
525 return error;
526 }
527 }
528
529 return 0;
530}
531
532static int rmi_f01_suspend(struct rmi_function *fn)
533{
534 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
535 int error;
536
537 f01->old_nosleep =
538 f01->device_control.ctrl0 & RMI_F01_CRTL0_NOSLEEP_BIT;
539 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
540
541 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
542 if (device_may_wakeup(fn->rmi_dev->xport->dev))
543 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
544 else
545 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
546
547 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
548 f01->device_control.ctrl0);
549 if (error) {
550 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
551 if (f01->old_nosleep)
552 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
553 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
554 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
555 return error;
556 }
557
558 return 0;
559}
560
561static int rmi_f01_resume(struct rmi_function *fn)
562{
563 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
564 int error;
565
566 if (f01->old_nosleep)
567 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
568
569 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
570 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
571
572 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
573 f01->device_control.ctrl0);
574 if (error) {
575 dev_err(&fn->dev,
576 "Failed to restore normal operation: %d.\n", error);
577 return error;
578 }
579
580 return 0;
581}
582
583static int rmi_f01_attention(struct rmi_function *fn,
584 unsigned long *irq_bits)
585{
586 struct rmi_device *rmi_dev = fn->rmi_dev;
587 int error;
588 u8 device_status;
589
590 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
591 if (error) {
592 dev_err(&fn->dev,
593 "Failed to read device status: %d.\n", error);
594 return error;
595 }
596
597 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
598 dev_warn(&fn->dev, "Device reset detected.\n");
599 error = rmi_dev->driver->reset_handler(rmi_dev);
600 if (error) {
601 dev_err(&fn->dev, "Device reset failed: %d\n", error);
602 return error;
603 }
604 }
605
606 return 0;
607}
608
609struct rmi_function_handler rmi_f01_handler = {
610 .driver = {
611 .name = "rmi4_f01",
612 /*
613 * Do not allow user unbinding F01 as it is critical
614 * function.
615 */
616 .suppress_bind_attrs = true,
617 },
618 .func = 0x01,
619 .probe = rmi_f01_probe,
620 .config = rmi_f01_config,
621 .attention = rmi_f01_attention,
622 .suspend = rmi_f01_suspend,
623 .resume = rmi_f01_resume,
624};
diff --git a/drivers/input/rmi4/rmi_f11.c b/drivers/input/rmi4/rmi_f11.c
new file mode 100644
index 000000000000..ec8a10d53288
--- /dev/null
+++ b/drivers/input/rmi4/rmi_f11.c
@@ -0,0 +1,1317 @@
1/*
2 * Copyright (c) 2011-2015 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/input.h>
14#include <linux/input/mt.h>
15#include <linux/kconfig.h>
16#include <linux/rmi.h>
17#include <linux/slab.h>
18#include <linux/of.h>
19#include "rmi_driver.h"
20#include "rmi_2d_sensor.h"
21
22#define F11_MAX_NUM_OF_FINGERS 10
23#define F11_MAX_NUM_OF_TOUCH_SHAPES 16
24
25#define FINGER_STATE_MASK 0x03
26
27#define F11_CTRL_SENSOR_MAX_X_POS_OFFSET 6
28#define F11_CTRL_SENSOR_MAX_Y_POS_OFFSET 8
29
30#define DEFAULT_XY_MAX 9999
31#define DEFAULT_MAX_ABS_MT_PRESSURE 255
32#define DEFAULT_MAX_ABS_MT_TOUCH 15
33#define DEFAULT_MAX_ABS_MT_ORIENTATION 1
34#define DEFAULT_MIN_ABS_MT_TRACKING_ID 1
35#define DEFAULT_MAX_ABS_MT_TRACKING_ID 10
36
37/** A note about RMI4 F11 register structure.
38 *
39 * The properties for
40 * a given sensor are described by its query registers. The number of query
41 * registers and the layout of their contents are described by the F11 device
42 * queries as well as the sensor query information.
43 *
44 * Similarly, each sensor has control registers that govern its behavior. The
45 * size and layout of the control registers for a given sensor can be determined
46 * by parsing that sensors query registers.
47 *
48 * And in a likewise fashion, each sensor has data registers where it reports
49 * its touch data and other interesting stuff. The size and layout of a
50 * sensors data registers must be determined by parsing its query registers.
51 *
52 * The short story is that we need to read and parse a lot of query
53 * registers in order to determine the attributes of a sensor. Then
54 * we need to use that data to compute the size of the control and data
55 * registers for sensor.
56 *
57 * The end result is that we have a number of structs that aren't used to
58 * directly generate the input events, but their size, location and contents
59 * are critical to determining where the data we are interested in lives.
60 *
61 * At this time, the driver does not yet comprehend all possible F11
62 * configuration options, but it should be sufficient to cover 99% of RMI4 F11
63 * devices currently in the field.
64 */
65
66/* maximum ABS_MT_POSITION displacement (in mm) */
67#define DMAX 10
68
69/**
70 * @rezero - writing this to the F11 command register will cause the sensor to
71 * calibrate to the current capacitive state.
72 */
73#define RMI_F11_REZERO 0x01
74
75#define RMI_F11_HAS_QUERY9 (1 << 3)
76#define RMI_F11_HAS_QUERY11 (1 << 4)
77#define RMI_F11_HAS_QUERY12 (1 << 5)
78#define RMI_F11_HAS_QUERY27 (1 << 6)
79#define RMI_F11_HAS_QUERY28 (1 << 7)
80
81/** Defs for Query 1 */
82
83#define RMI_F11_NR_FINGERS_MASK 0x07
84#define RMI_F11_HAS_REL (1 << 3)
85#define RMI_F11_HAS_ABS (1 << 4)
86#define RMI_F11_HAS_GESTURES (1 << 5)
87#define RMI_F11_HAS_SENSITIVITY_ADJ (1 << 6)
88#define RMI_F11_CONFIGURABLE (1 << 7)
89
90/** Defs for Query 2, 3, and 4. */
91#define RMI_F11_NR_ELECTRODES_MASK 0x7F
92
93/** Defs for Query 5 */
94
95#define RMI_F11_ABS_DATA_SIZE_MASK 0x03
96#define RMI_F11_HAS_ANCHORED_FINGER (1 << 2)
97#define RMI_F11_HAS_ADJ_HYST (1 << 3)
98#define RMI_F11_HAS_DRIBBLE (1 << 4)
99#define RMI_F11_HAS_BENDING_CORRECTION (1 << 5)
100#define RMI_F11_HAS_LARGE_OBJECT_SUPPRESSION (1 << 6)
101#define RMI_F11_HAS_JITTER_FILTER (1 << 7)
102
103/** Defs for Query 7 */
104#define RMI_F11_HAS_SINGLE_TAP (1 << 0)
105#define RMI_F11_HAS_TAP_AND_HOLD (1 << 1)
106#define RMI_F11_HAS_DOUBLE_TAP (1 << 2)
107#define RMI_F11_HAS_EARLY_TAP (1 << 3)
108#define RMI_F11_HAS_FLICK (1 << 4)
109#define RMI_F11_HAS_PRESS (1 << 5)
110#define RMI_F11_HAS_PINCH (1 << 6)
111#define RMI_F11_HAS_CHIRAL (1 << 7)
112
113/** Defs for Query 8 */
114#define RMI_F11_HAS_PALM_DET (1 << 0)
115#define RMI_F11_HAS_ROTATE (1 << 1)
116#define RMI_F11_HAS_TOUCH_SHAPES (1 << 2)
117#define RMI_F11_HAS_SCROLL_ZONES (1 << 3)
118#define RMI_F11_HAS_INDIVIDUAL_SCROLL_ZONES (1 << 4)
119#define RMI_F11_HAS_MF_SCROLL (1 << 5)
120#define RMI_F11_HAS_MF_EDGE_MOTION (1 << 6)
121#define RMI_F11_HAS_MF_SCROLL_INERTIA (1 << 7)
122
123/** Defs for Query 9. */
124#define RMI_F11_HAS_PEN (1 << 0)
125#define RMI_F11_HAS_PROXIMITY (1 << 1)
126#define RMI_F11_HAS_PALM_DET_SENSITIVITY (1 << 2)
127#define RMI_F11_HAS_SUPPRESS_ON_PALM_DETECT (1 << 3)
128#define RMI_F11_HAS_TWO_PEN_THRESHOLDS (1 << 4)
129#define RMI_F11_HAS_CONTACT_GEOMETRY (1 << 5)
130#define RMI_F11_HAS_PEN_HOVER_DISCRIMINATION (1 << 6)
131#define RMI_F11_HAS_PEN_FILTERS (1 << 7)
132
133/** Defs for Query 10. */
134#define RMI_F11_NR_TOUCH_SHAPES_MASK 0x1F
135
136/** Defs for Query 11 */
137
138#define RMI_F11_HAS_Z_TUNING (1 << 0)
139#define RMI_F11_HAS_ALGORITHM_SELECTION (1 << 1)
140#define RMI_F11_HAS_W_TUNING (1 << 2)
141#define RMI_F11_HAS_PITCH_INFO (1 << 3)
142#define RMI_F11_HAS_FINGER_SIZE (1 << 4)
143#define RMI_F11_HAS_SEGMENTATION_AGGRESSIVENESS (1 << 5)
144#define RMI_F11_HAS_XY_CLIP (1 << 6)
145#define RMI_F11_HAS_DRUMMING_FILTER (1 << 7)
146
147/** Defs for Query 12. */
148
149#define RMI_F11_HAS_GAPLESS_FINGER (1 << 0)
150#define RMI_F11_HAS_GAPLESS_FINGER_TUNING (1 << 1)
151#define RMI_F11_HAS_8BIT_W (1 << 2)
152#define RMI_F11_HAS_ADJUSTABLE_MAPPING (1 << 3)
153#define RMI_F11_HAS_INFO2 (1 << 4)
154#define RMI_F11_HAS_PHYSICAL_PROPS (1 << 5)
155#define RMI_F11_HAS_FINGER_LIMIT (1 << 6)
156#define RMI_F11_HAS_LINEAR_COEFF (1 << 7)
157
158/** Defs for Query 13. */
159
160#define RMI_F11_JITTER_WINDOW_MASK 0x1F
161#define RMI_F11_JITTER_FILTER_MASK 0x60
162#define RMI_F11_JITTER_FILTER_SHIFT 5
163
164/** Defs for Query 14. */
165#define RMI_F11_LIGHT_CONTROL_MASK 0x03
166#define RMI_F11_IS_CLEAR (1 << 2)
167#define RMI_F11_CLICKPAD_PROPS_MASK 0x18
168#define RMI_F11_CLICKPAD_PROPS_SHIFT 3
169#define RMI_F11_MOUSE_BUTTONS_MASK 0x60
170#define RMI_F11_MOUSE_BUTTONS_SHIFT 5
171#define RMI_F11_HAS_ADVANCED_GESTURES (1 << 7)
172
173#define RMI_F11_QUERY_SIZE 4
174#define RMI_F11_QUERY_GESTURE_SIZE 2
175
176#define F11_LIGHT_CTL_NONE 0x00
177#define F11_LUXPAD 0x01
178#define F11_DUAL_MODE 0x02
179
180#define F11_NOT_CLICKPAD 0x00
181#define F11_HINGED_CLICKPAD 0x01
182#define F11_UNIFORM_CLICKPAD 0x02
183
184/**
185 * Query registers 1 through 4 are always present.
186 *
187 * @nr_fingers - describes the maximum number of fingers the 2-D sensor
188 * supports.
189 * @has_rel - the sensor supports relative motion reporting.
190 * @has_abs - the sensor supports absolute poition reporting.
191 * @has_gestures - the sensor supports gesture reporting.
192 * @has_sensitivity_adjust - the sensor supports a global sensitivity
193 * adjustment.
194 * @configurable - the sensor supports various configuration options.
195 * @num_of_x_electrodes - the maximum number of electrodes the 2-D sensor
196 * supports on the X axis.
197 * @num_of_y_electrodes - the maximum number of electrodes the 2-D sensor
198 * supports on the Y axis.
199 * @max_electrodes - the total number of X and Y electrodes that may be
200 * configured.
201 *
202 * Query 5 is present if the has_abs bit is set.
203 *
204 * @abs_data_size - describes the format of data reported by the absolute
205 * data source. Only one format (the kind used here) is supported at this
206 * time.
207 * @has_anchored_finger - then the sensor supports the high-precision second
208 * finger tracking provided by the manual tracking and motion sensitivity
209 * options.
210 * @has_adjust_hyst - the difference between the finger release threshold and
211 * the touch threshold.
212 * @has_dribble - the sensor supports the generation of dribble interrupts,
213 * which may be enabled or disabled with the dribble control bit.
214 * @has_bending_correction - Bending related data registers 28 and 36, and
215 * control register 52..57 are present.
216 * @has_large_object_suppression - control register 58 and data register 28
217 * exist.
218 * @has_jitter_filter - query 13 and control 73..76 exist.
219 *
220 * Gesture information queries 7 and 8 are present if has_gestures bit is set.
221 *
222 * @has_single_tap - a basic single-tap gesture is supported.
223 * @has_tap_n_hold - tap-and-hold gesture is supported.
224 * @has_double_tap - double-tap gesture is supported.
225 * @has_early_tap - early tap is supported and reported as soon as the finger
226 * lifts for any tap event that could be interpreted as either a single tap
227 * or as the first tap of a double-tap or tap-and-hold gesture.
228 * @has_flick - flick detection is supported.
229 * @has_press - press gesture reporting is supported.
230 * @has_pinch - pinch gesture detection is supported.
231 * @has_palm_det - the 2-D sensor notifies the host whenever a large conductive
232 * object such as a palm or a cheek touches the 2-D sensor.
233 * @has_rotate - rotation gesture detection is supported.
234 * @has_touch_shapes - TouchShapes are supported. A TouchShape is a fixed
235 * rectangular area on the sensor that behaves like a capacitive button.
236 * @has_scroll_zones - scrolling areas near the sensor edges are supported.
237 * @has_individual_scroll_zones - if 1, then 4 scroll zones are supported;
238 * if 0, then only two are supported.
239 * @has_mf_scroll - the multifinger_scrolling bit will be set when
240 * more than one finger is involved in a scrolling action.
241 *
242 * Convenience for checking bytes in the gesture info registers. This is done
243 * often enough that we put it here to declutter the conditionals
244 *
245 * @query7_nonzero - true if none of the query 7 bits are set
246 * @query8_nonzero - true if none of the query 8 bits are set
247 *
248 * Query 9 is present if the has_query9 is set.
249 *
250 * @has_pen - detection of a stylus is supported and registers F11_2D_Ctrl20
251 * and F11_2D_Ctrl21 exist.
252 * @has_proximity - detection of fingers near the sensor is supported and
253 * registers F11_2D_Ctrl22 through F11_2D_Ctrl26 exist.
254 * @has_palm_det_sensitivity - the sensor supports the palm detect sensitivity
255 * feature and register F11_2D_Ctrl27 exists.
256 * @has_two_pen_thresholds - is has_pen is also set, then F11_2D_Ctrl35 exists.
257 * @has_contact_geometry - the sensor supports the use of contact geometry to
258 * map absolute X and Y target positions and registers F11_2D_Data18
259 * through F11_2D_Data27 exist.
260 *
261 * Touch shape info (query 10) is present if has_touch_shapes is set.
262 *
263 * @nr_touch_shapes - the total number of touch shapes supported.
264 *
265 * Query 11 is present if the has_query11 bit is set in query 0.
266 *
267 * @has_z_tuning - if set, the sensor supports Z tuning and registers
268 * F11_2D_Ctrl29 through F11_2D_Ctrl33 exist.
269 * @has_algorithm_selection - controls choice of noise suppression algorithm
270 * @has_w_tuning - the sensor supports Wx and Wy scaling and registers
271 * F11_2D_Ctrl36 through F11_2D_Ctrl39 exist.
272 * @has_pitch_info - the X and Y pitches of the sensor electrodes can be
273 * configured and registers F11_2D_Ctrl40 and F11_2D_Ctrl41 exist.
274 * @has_finger_size - the default finger width settings for the
275 * sensor can be configured and registers F11_2D_Ctrl42 through F11_2D_Ctrl44
276 * exist.
277 * @has_segmentation_aggressiveness - the sensor’s ability to distinguish
278 * multiple objects close together can be configured and register F11_2D_Ctrl45
279 * exists.
280 * @has_XY_clip - the inactive outside borders of the sensor can be
281 * configured and registers F11_2D_Ctrl46 through F11_2D_Ctrl49 exist.
282 * @has_drumming_filter - the sensor can be configured to distinguish
283 * between a fast flick and a quick drumming movement and registers
284 * F11_2D_Ctrl50 and F11_2D_Ctrl51 exist.
285 *
286 * Query 12 is present if hasQuery12 bit is set.
287 *
288 * @has_gapless_finger - control registers relating to gapless finger are
289 * present.
290 * @has_gapless_finger_tuning - additional control and data registers relating
291 * to gapless finger are present.
292 * @has_8bit_w - larger W value reporting is supported.
293 * @has_adjustable_mapping - TBD
294 * @has_info2 - the general info query14 is present
295 * @has_physical_props - additional queries describing the physical properties
296 * of the sensor are present.
297 * @has_finger_limit - indicates that F11 Ctrl 80 exists.
298 * @has_linear_coeff - indicates that F11 Ctrl 81 exists.
299 *
300 * Query 13 is present if Query 5's has_jitter_filter bit is set.
301 * @jitter_window_size - used by Design Studio 4.
302 * @jitter_filter_type - used by Design Studio 4.
303 *
304 * Query 14 is present if query 12's has_general_info2 flag is set.
305 *
306 * @light_control - Indicates what light/led control features are present, if
307 * any.
308 * @is_clear - if set, this is a clear sensor (indicating direct pointing
309 * application), otherwise it's opaque (indicating indirect pointing).
310 * @clickpad_props - specifies if this is a clickpad, and if so what sort of
311 * mechanism it uses
312 * @mouse_buttons - specifies the number of mouse buttons present (if any).
313 * @has_advanced_gestures - advanced driver gestures are supported.
314 */
315struct f11_2d_sensor_queries {
316 /* query1 */
317 u8 nr_fingers;
318 bool has_rel;
319 bool has_abs;
320 bool has_gestures;
321 bool has_sensitivity_adjust;
322 bool configurable;
323
324 /* query2 */
325 u8 nr_x_electrodes;
326
327 /* query3 */
328 u8 nr_y_electrodes;
329
330 /* query4 */
331 u8 max_electrodes;
332
333 /* query5 */
334 u8 abs_data_size;
335 bool has_anchored_finger;
336 bool has_adj_hyst;
337 bool has_dribble;
338 bool has_bending_correction;
339 bool has_large_object_suppression;
340 bool has_jitter_filter;
341
342 u8 f11_2d_query6;
343
344 /* query 7 */
345 bool has_single_tap;
346 bool has_tap_n_hold;
347 bool has_double_tap;
348 bool has_early_tap;
349 bool has_flick;
350 bool has_press;
351 bool has_pinch;
352 bool has_chiral;
353
354 bool query7_nonzero;
355
356 /* query 8 */
357 bool has_palm_det;
358 bool has_rotate;
359 bool has_touch_shapes;
360 bool has_scroll_zones;
361 bool has_individual_scroll_zones;
362 bool has_mf_scroll;
363 bool has_mf_edge_motion;
364 bool has_mf_scroll_inertia;
365
366 bool query8_nonzero;
367
368 /* Query 9 */
369 bool has_pen;
370 bool has_proximity;
371 bool has_palm_det_sensitivity;
372 bool has_suppress_on_palm_detect;
373 bool has_two_pen_thresholds;
374 bool has_contact_geometry;
375 bool has_pen_hover_discrimination;
376 bool has_pen_filters;
377
378 /* Query 10 */
379 u8 nr_touch_shapes;
380
381 /* Query 11. */
382 bool has_z_tuning;
383 bool has_algorithm_selection;
384 bool has_w_tuning;
385 bool has_pitch_info;
386 bool has_finger_size;
387 bool has_segmentation_aggressiveness;
388 bool has_XY_clip;
389 bool has_drumming_filter;
390
391 /* Query 12 */
392 bool has_gapless_finger;
393 bool has_gapless_finger_tuning;
394 bool has_8bit_w;
395 bool has_adjustable_mapping;
396 bool has_info2;
397 bool has_physical_props;
398 bool has_finger_limit;
399 bool has_linear_coeff_2;
400
401 /* Query 13 */
402 u8 jitter_window_size;
403 u8 jitter_filter_type;
404
405 /* Query 14 */
406 u8 light_control;
407 bool is_clear;
408 u8 clickpad_props;
409 u8 mouse_buttons;
410 bool has_advanced_gestures;
411
412 /* Query 15 - 18 */
413 u16 x_sensor_size_mm;
414 u16 y_sensor_size_mm;
415};
416
417/* Defs for Ctrl0. */
418#define RMI_F11_REPORT_MODE_MASK 0x07
419#define RMI_F11_ABS_POS_FILT (1 << 3)
420#define RMI_F11_REL_POS_FILT (1 << 4)
421#define RMI_F11_REL_BALLISTICS (1 << 5)
422#define RMI_F11_DRIBBLE (1 << 6)
423#define RMI_F11_REPORT_BEYOND_CLIP (1 << 7)
424
425/* Defs for Ctrl1. */
426#define RMI_F11_PALM_DETECT_THRESH_MASK 0x0F
427#define RMI_F11_MOTION_SENSITIVITY_MASK 0x30
428#define RMI_F11_MANUAL_TRACKING (1 << 6)
429#define RMI_F11_MANUAL_TRACKED_FINGER (1 << 7)
430
431#define RMI_F11_DELTA_X_THRESHOLD 2
432#define RMI_F11_DELTA_Y_THRESHOLD 3
433
434#define RMI_F11_CTRL_REG_COUNT 12
435
436struct f11_2d_ctrl {
437 u8 ctrl0_11[RMI_F11_CTRL_REG_COUNT];
438 u16 ctrl0_11_address;
439};
440
441#define RMI_F11_ABS_BYTES 5
442#define RMI_F11_REL_BYTES 2
443
444/* Defs for Data 8 */
445
446#define RMI_F11_SINGLE_TAP (1 << 0)
447#define RMI_F11_TAP_AND_HOLD (1 << 1)
448#define RMI_F11_DOUBLE_TAP (1 << 2)
449#define RMI_F11_EARLY_TAP (1 << 3)
450#define RMI_F11_FLICK (1 << 4)
451#define RMI_F11_PRESS (1 << 5)
452#define RMI_F11_PINCH (1 << 6)
453
454/* Defs for Data 9 */
455
456#define RMI_F11_PALM_DETECT (1 << 0)
457#define RMI_F11_ROTATE (1 << 1)
458#define RMI_F11_SHAPE (1 << 2)
459#define RMI_F11_SCROLLZONE (1 << 3)
460#define RMI_F11_GESTURE_FINGER_COUNT_MASK 0x70
461
462/** Handy pointers into our data buffer.
463 *
464 * @f_state - start of finger state registers.
465 * @abs_pos - start of absolute position registers (if present).
466 * @rel_pos - start of relative data registers (if present).
467 * @gest_1 - gesture flags (if present).
468 * @gest_2 - gesture flags & finger count (if present).
469 * @pinch - pinch motion register (if present).
470 * @flick - flick distance X & Y, flick time (if present).
471 * @rotate - rotate motion and finger separation.
472 * @multi_scroll - chiral deltas for X and Y (if present).
473 * @scroll_zones - scroll deltas for 4 regions (if present).
474 */
475struct f11_2d_data {
476 u8 *f_state;
477 u8 *abs_pos;
478 s8 *rel_pos;
479 u8 *gest_1;
480 u8 *gest_2;
481 s8 *pinch;
482 u8 *flick;
483 u8 *rotate;
484 u8 *shapes;
485 s8 *multi_scroll;
486 s8 *scroll_zones;
487};
488
489/** Data pertaining to F11 in general. For per-sensor data, see struct
490 * f11_2d_sensor.
491 *
492 * @dev_query - F11 device specific query registers.
493 * @dev_controls - F11 device specific control registers.
494 * @dev_controls_mutex - lock for the control registers.
495 * @rezero_wait_ms - if nonzero, upon resume we will wait this many
496 * milliseconds before rezeroing the sensor(s). This is useful in systems with
497 * poor electrical behavior on resume, where the initial calibration of the
498 * sensor(s) coming out of sleep state may be bogus.
499 * @sensors - per sensor data structures.
500 */
501struct f11_data {
502 bool has_query9;
503 bool has_query11;
504 bool has_query12;
505 bool has_query27;
506 bool has_query28;
507 bool has_acm;
508 struct f11_2d_ctrl dev_controls;
509 struct mutex dev_controls_mutex;
510 u16 rezero_wait_ms;
511 struct rmi_2d_sensor sensor;
512 struct f11_2d_sensor_queries sens_query;
513 struct f11_2d_data data;
514 struct rmi_2d_sensor_platform_data sensor_pdata;
515 unsigned long *abs_mask;
516 unsigned long *rel_mask;
517 unsigned long *result_bits;
518};
519
520enum f11_finger_state {
521 F11_NO_FINGER = 0x00,
522 F11_PRESENT = 0x01,
523 F11_INACCURATE = 0x02,
524 F11_RESERVED = 0x03
525};
526
527static void rmi_f11_rel_pos_report(struct f11_data *f11, u8 n_finger)
528{
529 struct rmi_2d_sensor *sensor = &f11->sensor;
530 struct f11_2d_data *data = &f11->data;
531 s8 x, y;
532
533 x = data->rel_pos[n_finger * 2];
534 y = data->rel_pos[n_finger * 2 + 1];
535
536 rmi_2d_sensor_rel_report(sensor, x, y);
537}
538
539static void rmi_f11_abs_pos_process(struct f11_data *f11,
540 struct rmi_2d_sensor *sensor,
541 struct rmi_2d_sensor_abs_object *obj,
542 enum f11_finger_state finger_state,
543 u8 n_finger)
544{
545 struct f11_2d_data *data = &f11->data;
546 u8 *pos_data = &data->abs_pos[n_finger * RMI_F11_ABS_BYTES];
547 int tool_type = MT_TOOL_FINGER;
548
549 switch (finger_state) {
550 case F11_PRESENT:
551 obj->type = RMI_2D_OBJECT_FINGER;
552 break;
553 default:
554 obj->type = RMI_2D_OBJECT_NONE;
555 }
556
557 obj->mt_tool = tool_type;
558 obj->x = (pos_data[0] << 4) | (pos_data[2] & 0x0F);
559 obj->y = (pos_data[1] << 4) | (pos_data[2] >> 4);
560 obj->z = pos_data[4];
561 obj->wx = pos_data[3] & 0x0f;
562 obj->wy = pos_data[3] >> 4;
563
564 rmi_2d_sensor_abs_process(sensor, obj, n_finger);
565}
566
567static inline u8 rmi_f11_parse_finger_state(const u8 *f_state, u8 n_finger)
568{
569 return (f_state[n_finger / 4] >> (2 * (n_finger % 4))) &
570 FINGER_STATE_MASK;
571}
572
573static void rmi_f11_finger_handler(struct f11_data *f11,
574 struct rmi_2d_sensor *sensor,
575 unsigned long *irq_bits, int num_irq_regs)
576{
577 const u8 *f_state = f11->data.f_state;
578 u8 finger_state;
579 u8 i;
580
581 int abs_bits = bitmap_and(f11->result_bits, irq_bits, f11->abs_mask,
582 num_irq_regs * 8);
583 int rel_bits = bitmap_and(f11->result_bits, irq_bits, f11->rel_mask,
584 num_irq_regs * 8);
585
586 for (i = 0; i < sensor->nbr_fingers; i++) {
587 /* Possible of having 4 fingers per f_statet register */
588 finger_state = rmi_f11_parse_finger_state(f_state, i);
589 if (finger_state == F11_RESERVED) {
590 pr_err("Invalid finger state[%d]: 0x%02x", i,
591 finger_state);
592 continue;
593 }
594
595 if (abs_bits)
596 rmi_f11_abs_pos_process(f11, sensor, &sensor->objs[i],
597 finger_state, i);
598
599 if (rel_bits)
600 rmi_f11_rel_pos_report(f11, i);
601 }
602
603 if (abs_bits) {
604 /*
605 * the absolute part is made in 2 parts to allow the kernel
606 * tracking to take place.
607 */
608 if (sensor->kernel_tracking)
609 input_mt_assign_slots(sensor->input,
610 sensor->tracking_slots,
611 sensor->tracking_pos,
612 sensor->nbr_fingers,
613 sensor->dmax);
614
615 for (i = 0; i < sensor->nbr_fingers; i++) {
616 finger_state = rmi_f11_parse_finger_state(f_state, i);
617 if (finger_state == F11_RESERVED)
618 /* no need to send twice the error */
619 continue;
620
621 rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
622 }
623
624 input_mt_sync_frame(sensor->input);
625 }
626}
627
628static int f11_2d_construct_data(struct f11_data *f11)
629{
630 struct rmi_2d_sensor *sensor = &f11->sensor;
631 struct f11_2d_sensor_queries *query = &f11->sens_query;
632 struct f11_2d_data *data = &f11->data;
633 int i;
634
635 sensor->nbr_fingers = (query->nr_fingers == 5 ? 10 :
636 query->nr_fingers + 1);
637
638 sensor->pkt_size = DIV_ROUND_UP(sensor->nbr_fingers, 4);
639
640 if (query->has_abs) {
641 sensor->pkt_size += (sensor->nbr_fingers * 5);
642 sensor->attn_size = sensor->pkt_size;
643 }
644
645 if (query->has_rel)
646 sensor->pkt_size += (sensor->nbr_fingers * 2);
647
648 /* Check if F11_2D_Query7 is non-zero */
649 if (query->query7_nonzero)
650 sensor->pkt_size += sizeof(u8);
651
652 /* Check if F11_2D_Query7 or F11_2D_Query8 is non-zero */
653 if (query->query7_nonzero || query->query8_nonzero)
654 sensor->pkt_size += sizeof(u8);
655
656 if (query->has_pinch || query->has_flick || query->has_rotate) {
657 sensor->pkt_size += 3;
658 if (!query->has_flick)
659 sensor->pkt_size--;
660 if (!query->has_rotate)
661 sensor->pkt_size--;
662 }
663
664 if (query->has_touch_shapes)
665 sensor->pkt_size +=
666 DIV_ROUND_UP(query->nr_touch_shapes + 1, 8);
667
668 sensor->data_pkt = devm_kzalloc(&sensor->fn->dev, sensor->pkt_size,
669 GFP_KERNEL);
670 if (!sensor->data_pkt)
671 return -ENOMEM;
672
673 data->f_state = sensor->data_pkt;
674 i = DIV_ROUND_UP(sensor->nbr_fingers, 4);
675
676 if (query->has_abs) {
677 data->abs_pos = &sensor->data_pkt[i];
678 i += (sensor->nbr_fingers * RMI_F11_ABS_BYTES);
679 }
680
681 if (query->has_rel) {
682 data->rel_pos = &sensor->data_pkt[i];
683 i += (sensor->nbr_fingers * RMI_F11_REL_BYTES);
684 }
685
686 if (query->query7_nonzero) {
687 data->gest_1 = &sensor->data_pkt[i];
688 i++;
689 }
690
691 if (query->query7_nonzero || query->query8_nonzero) {
692 data->gest_2 = &sensor->data_pkt[i];
693 i++;
694 }
695
696 if (query->has_pinch) {
697 data->pinch = &sensor->data_pkt[i];
698 i++;
699 }
700
701 if (query->has_flick) {
702 if (query->has_pinch) {
703 data->flick = data->pinch;
704 i += 2;
705 } else {
706 data->flick = &sensor->data_pkt[i];
707 i += 3;
708 }
709 }
710
711 if (query->has_rotate) {
712 if (query->has_flick) {
713 data->rotate = data->flick + 1;
714 } else {
715 data->rotate = &sensor->data_pkt[i];
716 i += 2;
717 }
718 }
719
720 if (query->has_touch_shapes)
721 data->shapes = &sensor->data_pkt[i];
722
723 return 0;
724}
725
726static int f11_read_control_regs(struct rmi_function *fn,
727 struct f11_2d_ctrl *ctrl, u16 ctrl_base_addr) {
728 struct rmi_device *rmi_dev = fn->rmi_dev;
729 int error = 0;
730
731 ctrl->ctrl0_11_address = ctrl_base_addr;
732 error = rmi_read_block(rmi_dev, ctrl_base_addr, ctrl->ctrl0_11,
733 RMI_F11_CTRL_REG_COUNT);
734 if (error < 0) {
735 dev_err(&fn->dev, "Failed to read ctrl0, code: %d.\n", error);
736 return error;
737 }
738
739 return 0;
740}
741
742static int f11_write_control_regs(struct rmi_function *fn,
743 struct f11_2d_sensor_queries *query,
744 struct f11_2d_ctrl *ctrl,
745 u16 ctrl_base_addr)
746{
747 struct rmi_device *rmi_dev = fn->rmi_dev;
748 int error;
749
750 error = rmi_write_block(rmi_dev, ctrl_base_addr, ctrl->ctrl0_11,
751 RMI_F11_CTRL_REG_COUNT);
752 if (error < 0)
753 return error;
754
755 return 0;
756}
757
758static int rmi_f11_get_query_parameters(struct rmi_device *rmi_dev,
759 struct f11_data *f11,
760 struct f11_2d_sensor_queries *sensor_query,
761 u16 query_base_addr)
762{
763 int query_size;
764 int rc;
765 u8 query_buf[RMI_F11_QUERY_SIZE];
766 bool has_query36 = false;
767
768 rc = rmi_read_block(rmi_dev, query_base_addr, query_buf,
769 RMI_F11_QUERY_SIZE);
770 if (rc < 0)
771 return rc;
772
773 sensor_query->nr_fingers = query_buf[0] & RMI_F11_NR_FINGERS_MASK;
774 sensor_query->has_rel = !!(query_buf[0] & RMI_F11_HAS_REL);
775 sensor_query->has_abs = !!(query_buf[0] & RMI_F11_HAS_ABS);
776 sensor_query->has_gestures = !!(query_buf[0] & RMI_F11_HAS_GESTURES);
777 sensor_query->has_sensitivity_adjust =
778 !!(query_buf[0] & RMI_F11_HAS_SENSITIVITY_ADJ);
779 sensor_query->configurable = !!(query_buf[0] & RMI_F11_CONFIGURABLE);
780
781 sensor_query->nr_x_electrodes =
782 query_buf[1] & RMI_F11_NR_ELECTRODES_MASK;
783 sensor_query->nr_y_electrodes =
784 query_buf[2] & RMI_F11_NR_ELECTRODES_MASK;
785 sensor_query->max_electrodes =
786 query_buf[3] & RMI_F11_NR_ELECTRODES_MASK;
787
788 query_size = RMI_F11_QUERY_SIZE;
789
790 if (sensor_query->has_abs) {
791 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
792 if (rc < 0)
793 return rc;
794
795 sensor_query->abs_data_size =
796 query_buf[0] & RMI_F11_ABS_DATA_SIZE_MASK;
797 sensor_query->has_anchored_finger =
798 !!(query_buf[0] & RMI_F11_HAS_ANCHORED_FINGER);
799 sensor_query->has_adj_hyst =
800 !!(query_buf[0] & RMI_F11_HAS_ADJ_HYST);
801 sensor_query->has_dribble =
802 !!(query_buf[0] & RMI_F11_HAS_DRIBBLE);
803 sensor_query->has_bending_correction =
804 !!(query_buf[0] & RMI_F11_HAS_BENDING_CORRECTION);
805 sensor_query->has_large_object_suppression =
806 !!(query_buf[0] & RMI_F11_HAS_LARGE_OBJECT_SUPPRESSION);
807 sensor_query->has_jitter_filter =
808 !!(query_buf[0] & RMI_F11_HAS_JITTER_FILTER);
809 query_size++;
810 }
811
812 if (sensor_query->has_rel) {
813 rc = rmi_read(rmi_dev, query_base_addr + query_size,
814 &sensor_query->f11_2d_query6);
815 if (rc < 0)
816 return rc;
817 query_size++;
818 }
819
820 if (sensor_query->has_gestures) {
821 rc = rmi_read_block(rmi_dev, query_base_addr + query_size,
822 query_buf, RMI_F11_QUERY_GESTURE_SIZE);
823 if (rc < 0)
824 return rc;
825
826 sensor_query->has_single_tap =
827 !!(query_buf[0] & RMI_F11_HAS_SINGLE_TAP);
828 sensor_query->has_tap_n_hold =
829 !!(query_buf[0] & RMI_F11_HAS_TAP_AND_HOLD);
830 sensor_query->has_double_tap =
831 !!(query_buf[0] & RMI_F11_HAS_DOUBLE_TAP);
832 sensor_query->has_early_tap =
833 !!(query_buf[0] & RMI_F11_HAS_EARLY_TAP);
834 sensor_query->has_flick =
835 !!(query_buf[0] & RMI_F11_HAS_FLICK);
836 sensor_query->has_press =
837 !!(query_buf[0] & RMI_F11_HAS_PRESS);
838 sensor_query->has_pinch =
839 !!(query_buf[0] & RMI_F11_HAS_PINCH);
840 sensor_query->has_chiral =
841 !!(query_buf[0] & RMI_F11_HAS_CHIRAL);
842
843 /* query 8 */
844 sensor_query->has_palm_det =
845 !!(query_buf[1] & RMI_F11_HAS_PALM_DET);
846 sensor_query->has_rotate =
847 !!(query_buf[1] & RMI_F11_HAS_ROTATE);
848 sensor_query->has_touch_shapes =
849 !!(query_buf[1] & RMI_F11_HAS_TOUCH_SHAPES);
850 sensor_query->has_scroll_zones =
851 !!(query_buf[1] & RMI_F11_HAS_SCROLL_ZONES);
852 sensor_query->has_individual_scroll_zones =
853 !!(query_buf[1] & RMI_F11_HAS_INDIVIDUAL_SCROLL_ZONES);
854 sensor_query->has_mf_scroll =
855 !!(query_buf[1] & RMI_F11_HAS_MF_SCROLL);
856 sensor_query->has_mf_edge_motion =
857 !!(query_buf[1] & RMI_F11_HAS_MF_EDGE_MOTION);
858 sensor_query->has_mf_scroll_inertia =
859 !!(query_buf[1] & RMI_F11_HAS_MF_SCROLL_INERTIA);
860
861 sensor_query->query7_nonzero = !!(query_buf[0]);
862 sensor_query->query8_nonzero = !!(query_buf[1]);
863
864 query_size += 2;
865 }
866
867 if (f11->has_query9) {
868 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
869 if (rc < 0)
870 return rc;
871
872 sensor_query->has_pen =
873 !!(query_buf[0] & RMI_F11_HAS_PEN);
874 sensor_query->has_proximity =
875 !!(query_buf[0] & RMI_F11_HAS_PROXIMITY);
876 sensor_query->has_palm_det_sensitivity =
877 !!(query_buf[0] & RMI_F11_HAS_PALM_DET_SENSITIVITY);
878 sensor_query->has_suppress_on_palm_detect =
879 !!(query_buf[0] & RMI_F11_HAS_SUPPRESS_ON_PALM_DETECT);
880 sensor_query->has_two_pen_thresholds =
881 !!(query_buf[0] & RMI_F11_HAS_TWO_PEN_THRESHOLDS);
882 sensor_query->has_contact_geometry =
883 !!(query_buf[0] & RMI_F11_HAS_CONTACT_GEOMETRY);
884 sensor_query->has_pen_hover_discrimination =
885 !!(query_buf[0] & RMI_F11_HAS_PEN_HOVER_DISCRIMINATION);
886 sensor_query->has_pen_filters =
887 !!(query_buf[0] & RMI_F11_HAS_PEN_FILTERS);
888
889 query_size++;
890 }
891
892 if (sensor_query->has_touch_shapes) {
893 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
894 if (rc < 0)
895 return rc;
896
897 sensor_query->nr_touch_shapes = query_buf[0] &
898 RMI_F11_NR_TOUCH_SHAPES_MASK;
899
900 query_size++;
901 }
902
903 if (f11->has_query11) {
904 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
905 if (rc < 0)
906 return rc;
907
908 sensor_query->has_z_tuning =
909 !!(query_buf[0] & RMI_F11_HAS_Z_TUNING);
910 sensor_query->has_algorithm_selection =
911 !!(query_buf[0] & RMI_F11_HAS_ALGORITHM_SELECTION);
912 sensor_query->has_w_tuning =
913 !!(query_buf[0] & RMI_F11_HAS_W_TUNING);
914 sensor_query->has_pitch_info =
915 !!(query_buf[0] & RMI_F11_HAS_PITCH_INFO);
916 sensor_query->has_finger_size =
917 !!(query_buf[0] & RMI_F11_HAS_FINGER_SIZE);
918 sensor_query->has_segmentation_aggressiveness =
919 !!(query_buf[0] &
920 RMI_F11_HAS_SEGMENTATION_AGGRESSIVENESS);
921 sensor_query->has_XY_clip =
922 !!(query_buf[0] & RMI_F11_HAS_XY_CLIP);
923 sensor_query->has_drumming_filter =
924 !!(query_buf[0] & RMI_F11_HAS_DRUMMING_FILTER);
925
926 query_size++;
927 }
928
929 if (f11->has_query12) {
930 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
931 if (rc < 0)
932 return rc;
933
934 sensor_query->has_gapless_finger =
935 !!(query_buf[0] & RMI_F11_HAS_GAPLESS_FINGER);
936 sensor_query->has_gapless_finger_tuning =
937 !!(query_buf[0] & RMI_F11_HAS_GAPLESS_FINGER_TUNING);
938 sensor_query->has_8bit_w =
939 !!(query_buf[0] & RMI_F11_HAS_8BIT_W);
940 sensor_query->has_adjustable_mapping =
941 !!(query_buf[0] & RMI_F11_HAS_ADJUSTABLE_MAPPING);
942 sensor_query->has_info2 =
943 !!(query_buf[0] & RMI_F11_HAS_INFO2);
944 sensor_query->has_physical_props =
945 !!(query_buf[0] & RMI_F11_HAS_PHYSICAL_PROPS);
946 sensor_query->has_finger_limit =
947 !!(query_buf[0] & RMI_F11_HAS_FINGER_LIMIT);
948 sensor_query->has_linear_coeff_2 =
949 !!(query_buf[0] & RMI_F11_HAS_LINEAR_COEFF);
950
951 query_size++;
952 }
953
954 if (sensor_query->has_jitter_filter) {
955 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
956 if (rc < 0)
957 return rc;
958
959 sensor_query->jitter_window_size = query_buf[0] &
960 RMI_F11_JITTER_WINDOW_MASK;
961 sensor_query->jitter_filter_type = (query_buf[0] &
962 RMI_F11_JITTER_FILTER_MASK) >>
963 RMI_F11_JITTER_FILTER_SHIFT;
964
965 query_size++;
966 }
967
968 if (sensor_query->has_info2) {
969 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
970 if (rc < 0)
971 return rc;
972
973 sensor_query->light_control =
974 query_buf[0] & RMI_F11_LIGHT_CONTROL_MASK;
975 sensor_query->is_clear =
976 !!(query_buf[0] & RMI_F11_IS_CLEAR);
977 sensor_query->clickpad_props =
978 (query_buf[0] & RMI_F11_CLICKPAD_PROPS_MASK) >>
979 RMI_F11_CLICKPAD_PROPS_SHIFT;
980 sensor_query->mouse_buttons =
981 (query_buf[0] & RMI_F11_MOUSE_BUTTONS_MASK) >>
982 RMI_F11_MOUSE_BUTTONS_SHIFT;
983 sensor_query->has_advanced_gestures =
984 !!(query_buf[0] & RMI_F11_HAS_ADVANCED_GESTURES);
985
986 query_size++;
987 }
988
989 if (sensor_query->has_physical_props) {
990 rc = rmi_read_block(rmi_dev, query_base_addr
991 + query_size, query_buf, 4);
992 if (rc < 0)
993 return rc;
994
995 sensor_query->x_sensor_size_mm =
996 (query_buf[0] | (query_buf[1] << 8)) / 10;
997 sensor_query->y_sensor_size_mm =
998 (query_buf[2] | (query_buf[3] << 8)) / 10;
999
1000 /*
1001 * query 15 - 18 contain the size of the sensor
1002 * and query 19 - 26 contain bezel dimensions
1003 */
1004 query_size += 12;
1005 }
1006
1007 if (f11->has_query27)
1008 ++query_size;
1009
1010 if (f11->has_query28) {
1011 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1012 query_buf);
1013 if (rc < 0)
1014 return rc;
1015
1016 has_query36 = !!(query_buf[0] & BIT(6));
1017 }
1018
1019 if (has_query36) {
1020 query_size += 2;
1021 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1022 query_buf);
1023 if (rc < 0)
1024 return rc;
1025
1026 if (!!(query_buf[0] & BIT(5)))
1027 f11->has_acm = true;
1028 }
1029
1030 return query_size;
1031}
1032
1033static int rmi_f11_initialize(struct rmi_function *fn)
1034{
1035 struct rmi_device *rmi_dev = fn->rmi_dev;
1036 struct f11_data *f11;
1037 struct f11_2d_ctrl *ctrl;
1038 u8 query_offset;
1039 u16 query_base_addr;
1040 u16 control_base_addr;
1041 u16 max_x_pos, max_y_pos;
1042 int rc;
1043 const struct rmi_device_platform_data *pdata =
1044 rmi_get_platform_data(rmi_dev);
1045 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
1046 struct rmi_2d_sensor *sensor;
1047 u8 buf;
1048 int mask_size;
1049
1050 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Initializing F11 values.\n");
1051
1052 mask_size = BITS_TO_LONGS(drvdata->irq_count) * sizeof(unsigned long);
1053
1054 /*
1055 ** init instance data, fill in values and create any sysfs files
1056 */
1057 f11 = devm_kzalloc(&fn->dev, sizeof(struct f11_data) + mask_size * 3,
1058 GFP_KERNEL);
1059 if (!f11)
1060 return -ENOMEM;
1061
1062 if (fn->dev.of_node) {
1063 rc = rmi_2d_sensor_of_probe(&fn->dev, &f11->sensor_pdata);
1064 if (rc)
1065 return rc;
1066 } else if (pdata->sensor_pdata) {
1067 f11->sensor_pdata = *pdata->sensor_pdata;
1068 }
1069
1070 f11->rezero_wait_ms = f11->sensor_pdata.rezero_wait;
1071
1072 f11->abs_mask = (unsigned long *)((char *)f11
1073 + sizeof(struct f11_data));
1074 f11->rel_mask = (unsigned long *)((char *)f11
1075 + sizeof(struct f11_data) + mask_size);
1076 f11->result_bits = (unsigned long *)((char *)f11
1077 + sizeof(struct f11_data) + mask_size * 2);
1078
1079 set_bit(fn->irq_pos, f11->abs_mask);
1080 set_bit(fn->irq_pos + 1, f11->rel_mask);
1081
1082 query_base_addr = fn->fd.query_base_addr;
1083 control_base_addr = fn->fd.control_base_addr;
1084
1085 rc = rmi_read(rmi_dev, query_base_addr, &buf);
1086 if (rc < 0)
1087 return rc;
1088
1089 f11->has_query9 = !!(buf & RMI_F11_HAS_QUERY9);
1090 f11->has_query11 = !!(buf & RMI_F11_HAS_QUERY11);
1091 f11->has_query12 = !!(buf & RMI_F11_HAS_QUERY12);
1092 f11->has_query27 = !!(buf & RMI_F11_HAS_QUERY27);
1093 f11->has_query28 = !!(buf & RMI_F11_HAS_QUERY28);
1094
1095 query_offset = (query_base_addr + 1);
1096 sensor = &f11->sensor;
1097 sensor->fn = fn;
1098
1099 rc = rmi_f11_get_query_parameters(rmi_dev, f11,
1100 &f11->sens_query, query_offset);
1101 if (rc < 0)
1102 return rc;
1103 query_offset += rc;
1104
1105 rc = f11_read_control_regs(fn, &f11->dev_controls,
1106 control_base_addr);
1107 if (rc < 0) {
1108 dev_err(&fn->dev,
1109 "Failed to read F11 control params.\n");
1110 return rc;
1111 }
1112
1113 if (f11->sens_query.has_info2) {
1114 if (f11->sens_query.is_clear)
1115 f11->sensor.sensor_type = rmi_sensor_touchscreen;
1116 else
1117 f11->sensor.sensor_type = rmi_sensor_touchpad;
1118 }
1119
1120 sensor->report_abs = f11->sens_query.has_abs;
1121
1122 sensor->axis_align =
1123 f11->sensor_pdata.axis_align;
1124
1125 sensor->topbuttonpad = f11->sensor_pdata.topbuttonpad;
1126 sensor->kernel_tracking = f11->sensor_pdata.kernel_tracking;
1127 sensor->dmax = f11->sensor_pdata.dmax;
1128
1129 if (f11->sens_query.has_physical_props) {
1130 sensor->x_mm = f11->sens_query.x_sensor_size_mm;
1131 sensor->y_mm = f11->sens_query.y_sensor_size_mm;
1132 } else {
1133 sensor->x_mm = f11->sensor_pdata.x_mm;
1134 sensor->y_mm = f11->sensor_pdata.y_mm;
1135 }
1136
1137 if (sensor->sensor_type == rmi_sensor_default)
1138 sensor->sensor_type =
1139 f11->sensor_pdata.sensor_type;
1140
1141 sensor->report_abs = sensor->report_abs
1142 && !(f11->sensor_pdata.disable_report_mask
1143 & RMI_F11_DISABLE_ABS_REPORT);
1144
1145 if (!sensor->report_abs)
1146 /*
1147 * If device doesn't have abs or if it has been disables
1148 * fallback to reporting rel data.
1149 */
1150 sensor->report_rel = f11->sens_query.has_rel;
1151
1152 rc = rmi_read_block(rmi_dev,
1153 control_base_addr + F11_CTRL_SENSOR_MAX_X_POS_OFFSET,
1154 (u8 *)&max_x_pos, sizeof(max_x_pos));
1155 if (rc < 0)
1156 return rc;
1157
1158 rc = rmi_read_block(rmi_dev,
1159 control_base_addr + F11_CTRL_SENSOR_MAX_Y_POS_OFFSET,
1160 (u8 *)&max_y_pos, sizeof(max_y_pos));
1161 if (rc < 0)
1162 return rc;
1163
1164 sensor->max_x = max_x_pos;
1165 sensor->max_y = max_y_pos;
1166
1167 rc = f11_2d_construct_data(f11);
1168 if (rc < 0)
1169 return rc;
1170
1171 if (f11->has_acm)
1172 f11->sensor.attn_size += f11->sensor.nbr_fingers * 2;
1173
1174 /* allocate the in-kernel tracking buffers */
1175 sensor->tracking_pos = devm_kzalloc(&fn->dev,
1176 sizeof(struct input_mt_pos) * sensor->nbr_fingers,
1177 GFP_KERNEL);
1178 sensor->tracking_slots = devm_kzalloc(&fn->dev,
1179 sizeof(int) * sensor->nbr_fingers, GFP_KERNEL);
1180 sensor->objs = devm_kzalloc(&fn->dev,
1181 sizeof(struct rmi_2d_sensor_abs_object)
1182 * sensor->nbr_fingers, GFP_KERNEL);
1183 if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
1184 return -ENOMEM;
1185
1186 ctrl = &f11->dev_controls;
1187 if (sensor->axis_align.delta_x_threshold)
1188 ctrl->ctrl0_11[RMI_F11_DELTA_X_THRESHOLD] =
1189 sensor->axis_align.delta_x_threshold;
1190
1191 if (sensor->axis_align.delta_y_threshold)
1192 ctrl->ctrl0_11[RMI_F11_DELTA_Y_THRESHOLD] =
1193 sensor->axis_align.delta_y_threshold;
1194
1195 if (f11->sens_query.has_dribble)
1196 ctrl->ctrl0_11[0] = ctrl->ctrl0_11[0] & ~BIT(6);
1197
1198 if (f11->sens_query.has_palm_det)
1199 ctrl->ctrl0_11[11] = ctrl->ctrl0_11[11] & ~BIT(0);
1200
1201 rc = f11_write_control_regs(fn, &f11->sens_query,
1202 &f11->dev_controls, fn->fd.query_base_addr);
1203 if (rc)
1204 dev_warn(&fn->dev, "Failed to write control registers\n");
1205
1206 mutex_init(&f11->dev_controls_mutex);
1207
1208 dev_set_drvdata(&fn->dev, f11);
1209
1210 return 0;
1211}
1212
1213static int rmi_f11_config(struct rmi_function *fn)
1214{
1215 struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1216 struct rmi_driver *drv = fn->rmi_dev->driver;
1217 struct rmi_2d_sensor *sensor = &f11->sensor;
1218 int rc;
1219
1220 if (!sensor->report_abs)
1221 drv->clear_irq_bits(fn->rmi_dev, f11->abs_mask);
1222 else
1223 drv->set_irq_bits(fn->rmi_dev, f11->abs_mask);
1224
1225 if (!sensor->report_rel)
1226 drv->clear_irq_bits(fn->rmi_dev, f11->rel_mask);
1227 else
1228 drv->set_irq_bits(fn->rmi_dev, f11->rel_mask);
1229
1230 rc = f11_write_control_regs(fn, &f11->sens_query,
1231 &f11->dev_controls, fn->fd.query_base_addr);
1232 if (rc < 0)
1233 return rc;
1234
1235 return 0;
1236}
1237
1238static int rmi_f11_attention(struct rmi_function *fn, unsigned long *irq_bits)
1239{
1240 struct rmi_device *rmi_dev = fn->rmi_dev;
1241 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
1242 struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1243 u16 data_base_addr = fn->fd.data_base_addr;
1244 u16 data_base_addr_offset = 0;
1245 int error;
1246
1247 if (rmi_dev->xport->attn_data) {
1248 memcpy(f11->sensor.data_pkt, rmi_dev->xport->attn_data,
1249 f11->sensor.attn_size);
1250 rmi_dev->xport->attn_data += f11->sensor.attn_size;
1251 rmi_dev->xport->attn_size -= f11->sensor.attn_size;
1252 } else {
1253 error = rmi_read_block(rmi_dev,
1254 data_base_addr + data_base_addr_offset,
1255 f11->sensor.data_pkt,
1256 f11->sensor.pkt_size);
1257 if (error < 0)
1258 return error;
1259 }
1260
1261 rmi_f11_finger_handler(f11, &f11->sensor, irq_bits,
1262 drvdata->num_of_irq_regs);
1263 data_base_addr_offset += f11->sensor.pkt_size;
1264
1265 return 0;
1266}
1267
1268static int rmi_f11_resume(struct rmi_function *fn)
1269{
1270 struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1271 int error;
1272
1273 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Resuming...\n");
1274 if (!f11->rezero_wait_ms)
1275 return 0;
1276
1277 mdelay(f11->rezero_wait_ms);
1278
1279 error = rmi_write(fn->rmi_dev, fn->fd.command_base_addr,
1280 RMI_F11_REZERO);
1281 if (error) {
1282 dev_err(&fn->dev,
1283 "%s: failed to issue rezero command, error = %d.",
1284 __func__, error);
1285 return error;
1286 }
1287
1288 return 0;
1289}
1290
1291static int rmi_f11_probe(struct rmi_function *fn)
1292{
1293 int error;
1294 struct f11_data *f11;
1295
1296 error = rmi_f11_initialize(fn);
1297 if (error)
1298 return error;
1299
1300 f11 = dev_get_drvdata(&fn->dev);
1301 error = rmi_2d_sensor_configure_input(fn, &f11->sensor);
1302 if (error)
1303 return error;
1304
1305 return 0;
1306}
1307
1308struct rmi_function_handler rmi_f11_handler = {
1309 .driver = {
1310 .name = "rmi4_f11",
1311 },
1312 .func = 0x11,
1313 .probe = rmi_f11_probe,
1314 .config = rmi_f11_config,
1315 .attention = rmi_f11_attention,
1316 .resume = rmi_f11_resume,
1317};
diff --git a/drivers/input/rmi4/rmi_f12.c b/drivers/input/rmi4/rmi_f12.c
new file mode 100644
index 000000000000..8dd3fb5e1f94
--- /dev/null
+++ b/drivers/input/rmi4/rmi_f12.c
@@ -0,0 +1,457 @@
1/*
2 * Copyright (c) 2012-2016 Synaptics Incorporated
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
8#include <linux/input.h>
9#include <linux/input/mt.h>
10#include <linux/rmi.h>
11#include "rmi_driver.h"
12#include "rmi_2d_sensor.h"
13
14enum rmi_f12_object_type {
15 RMI_F12_OBJECT_NONE = 0x00,
16 RMI_F12_OBJECT_FINGER = 0x01,
17 RMI_F12_OBJECT_STYLUS = 0x02,
18 RMI_F12_OBJECT_PALM = 0x03,
19 RMI_F12_OBJECT_UNCLASSIFIED = 0x04,
20 RMI_F12_OBJECT_GLOVED_FINGER = 0x06,
21 RMI_F12_OBJECT_NARROW_OBJECT = 0x07,
22 RMI_F12_OBJECT_HAND_EDGE = 0x08,
23 RMI_F12_OBJECT_COVER = 0x0A,
24 RMI_F12_OBJECT_STYLUS_2 = 0x0B,
25 RMI_F12_OBJECT_ERASER = 0x0C,
26 RMI_F12_OBJECT_SMALL_OBJECT = 0x0D,
27};
28
29struct f12_data {
30 struct rmi_function *fn;
31 struct rmi_2d_sensor sensor;
32 struct rmi_2d_sensor_platform_data sensor_pdata;
33
34 u16 data_addr;
35
36 struct rmi_register_descriptor query_reg_desc;
37 struct rmi_register_descriptor control_reg_desc;
38 struct rmi_register_descriptor data_reg_desc;
39
40 /* F12 Data1 describes sensed objects */
41 const struct rmi_register_desc_item *data1;
42 u16 data1_offset;
43
44 /* F12 Data5 describes finger ACM */
45 const struct rmi_register_desc_item *data5;
46 u16 data5_offset;
47
48 /* F12 Data5 describes Pen */
49 const struct rmi_register_desc_item *data6;
50 u16 data6_offset;
51
52
53 /* F12 Data9 reports relative data */
54 const struct rmi_register_desc_item *data9;
55 u16 data9_offset;
56
57 const struct rmi_register_desc_item *data15;
58 u16 data15_offset;
59};
60
61static int rmi_f12_read_sensor_tuning(struct f12_data *f12)
62{
63 const struct rmi_register_desc_item *item;
64 struct rmi_2d_sensor *sensor = &f12->sensor;
65 struct rmi_function *fn = sensor->fn;
66 struct rmi_device *rmi_dev = fn->rmi_dev;
67 int ret;
68 int offset;
69 u8 buf[14];
70 int pitch_x = 0;
71 int pitch_y = 0;
72 int clip_x_low = 0;
73 int clip_x_high = 0;
74 int clip_y_low = 0;
75 int clip_y_high = 0;
76 int rx_receivers = 0;
77 int tx_receivers = 0;
78 int sensor_flags = 0;
79
80 item = rmi_get_register_desc_item(&f12->control_reg_desc, 8);
81 if (!item) {
82 dev_err(&fn->dev,
83 "F12 does not have the sensor tuning control register\n");
84 return -ENODEV;
85 }
86
87 offset = rmi_register_desc_calc_reg_offset(&f12->control_reg_desc, 8);
88
89 if (item->reg_size > 14) {
90 dev_err(&fn->dev, "F12 control8 should be 14 bytes, not: %ld\n",
91 item->reg_size);
92 return -ENODEV;
93 }
94
95 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr + offset, buf,
96 item->reg_size);
97 if (ret)
98 return ret;
99
100 offset = 0;
101 if (rmi_register_desc_has_subpacket(item, 0)) {
102 sensor->max_x = (buf[offset + 1] << 8) | buf[offset];
103 sensor->max_y = (buf[offset + 3] << 8) | buf[offset + 2];
104 offset += 4;
105 }
106
107 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: max_x: %d max_y: %d\n", __func__,
108 sensor->max_x, sensor->max_y);
109
110 if (rmi_register_desc_has_subpacket(item, 1)) {
111 pitch_x = (buf[offset + 1] << 8) | buf[offset];
112 pitch_y = (buf[offset + 3] << 8) | buf[offset + 2];
113 offset += 4;
114 }
115
116 if (rmi_register_desc_has_subpacket(item, 2)) {
117 sensor->axis_align.clip_x_low = buf[offset];
118 sensor->axis_align.clip_x_high = sensor->max_x
119 - buf[offset + 1];
120 sensor->axis_align.clip_y_low = buf[offset + 2];
121 sensor->axis_align.clip_y_high = sensor->max_y
122 - buf[offset + 3];
123 offset += 4;
124 }
125
126 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x low: %d x high: %d y low: %d y high: %d\n",
127 __func__, clip_x_low, clip_x_high, clip_y_low, clip_y_high);
128
129 if (rmi_register_desc_has_subpacket(item, 3)) {
130 rx_receivers = buf[offset];
131 tx_receivers = buf[offset + 1];
132 offset += 2;
133 }
134
135 if (rmi_register_desc_has_subpacket(item, 4)) {
136 sensor_flags = buf[offset];
137 offset += 1;
138 }
139
140 sensor->x_mm = (pitch_x * rx_receivers) >> 12;
141 sensor->y_mm = (pitch_y * tx_receivers) >> 12;
142
143 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x_mm: %d y_mm: %d\n", __func__,
144 sensor->x_mm, sensor->y_mm);
145
146 return 0;
147}
148
149static void rmi_f12_process_objects(struct f12_data *f12, u8 *data1)
150{
151 int i;
152 struct rmi_2d_sensor *sensor = &f12->sensor;
153
154 for (i = 0; i < f12->data1->num_subpackets; i++) {
155 struct rmi_2d_sensor_abs_object *obj = &sensor->objs[i];
156
157 obj->type = RMI_2D_OBJECT_NONE;
158 obj->mt_tool = MT_TOOL_FINGER;
159
160 switch (data1[0]) {
161 case RMI_F12_OBJECT_FINGER:
162 obj->type = RMI_2D_OBJECT_FINGER;
163 break;
164 case RMI_F12_OBJECT_STYLUS:
165 obj->type = RMI_2D_OBJECT_STYLUS;
166 obj->mt_tool = MT_TOOL_PEN;
167 break;
168 case RMI_F12_OBJECT_PALM:
169 obj->type = RMI_2D_OBJECT_PALM;
170 obj->mt_tool = MT_TOOL_PALM;
171 break;
172 case RMI_F12_OBJECT_UNCLASSIFIED:
173 obj->type = RMI_2D_OBJECT_UNCLASSIFIED;
174 break;
175 }
176
177 obj->x = (data1[2] << 8) | data1[1];
178 obj->y = (data1[4] << 8) | data1[3];
179 obj->z = data1[5];
180 obj->wx = data1[6];
181 obj->wy = data1[7];
182
183 rmi_2d_sensor_abs_process(sensor, obj, i);
184
185 data1 += 8;
186 }
187
188 if (sensor->kernel_tracking)
189 input_mt_assign_slots(sensor->input,
190 sensor->tracking_slots,
191 sensor->tracking_pos,
192 sensor->nbr_fingers,
193 sensor->dmax);
194
195 for (i = 0; i < sensor->nbr_fingers; i++)
196 rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
197}
198
199static int rmi_f12_attention(struct rmi_function *fn,
200 unsigned long *irq_nr_regs)
201{
202 int retval;
203 struct rmi_device *rmi_dev = fn->rmi_dev;
204 struct f12_data *f12 = dev_get_drvdata(&fn->dev);
205 struct rmi_2d_sensor *sensor = &f12->sensor;
206
207 if (rmi_dev->xport->attn_data) {
208 memcpy(sensor->data_pkt, rmi_dev->xport->attn_data,
209 sensor->attn_size);
210 rmi_dev->xport->attn_data += sensor->attn_size;
211 rmi_dev->xport->attn_size -= sensor->attn_size;
212 } else {
213 retval = rmi_read_block(rmi_dev, f12->data_addr,
214 sensor->data_pkt, sensor->pkt_size);
215 if (retval < 0) {
216 dev_err(&fn->dev, "Failed to read object data. Code: %d.\n",
217 retval);
218 return retval;
219 }
220 }
221
222 if (f12->data1)
223 rmi_f12_process_objects(f12,
224 &sensor->data_pkt[f12->data1_offset]);
225
226 input_mt_sync_frame(sensor->input);
227
228 return 0;
229}
230
231static int rmi_f12_config(struct rmi_function *fn)
232{
233 struct rmi_driver *drv = fn->rmi_dev->driver;
234
235 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
236
237 return 0;
238}
239
240static int rmi_f12_probe(struct rmi_function *fn)
241{
242 struct f12_data *f12;
243 int ret;
244 struct rmi_device *rmi_dev = fn->rmi_dev;
245 char buf;
246 u16 query_addr = fn->fd.query_base_addr;
247 const struct rmi_register_desc_item *item;
248 struct rmi_2d_sensor *sensor;
249 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
250 struct rmi_transport_dev *xport = rmi_dev->xport;
251 u16 data_offset = 0;
252
253 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s\n", __func__);
254
255 ret = rmi_read(fn->rmi_dev, query_addr, &buf);
256 if (ret < 0) {
257 dev_err(&fn->dev, "Failed to read general info register: %d\n",
258 ret);
259 return -ENODEV;
260 }
261 ++query_addr;
262
263 if (!(buf & 0x1)) {
264 dev_err(&fn->dev,
265 "Behavior of F12 without register descriptors is undefined.\n");
266 return -ENODEV;
267 }
268
269 f12 = devm_kzalloc(&fn->dev, sizeof(struct f12_data), GFP_KERNEL);
270 if (!f12)
271 return -ENOMEM;
272
273 if (fn->dev.of_node) {
274 ret = rmi_2d_sensor_of_probe(&fn->dev, &f12->sensor_pdata);
275 if (ret)
276 return ret;
277 } else if (pdata->sensor_pdata) {
278 f12->sensor_pdata = *pdata->sensor_pdata;
279 }
280
281 ret = rmi_read_register_desc(rmi_dev, query_addr,
282 &f12->query_reg_desc);
283 if (ret) {
284 dev_err(&fn->dev,
285 "Failed to read the Query Register Descriptor: %d\n",
286 ret);
287 return ret;
288 }
289 query_addr += 3;
290
291 ret = rmi_read_register_desc(rmi_dev, query_addr,
292 &f12->control_reg_desc);
293 if (ret) {
294 dev_err(&fn->dev,
295 "Failed to read the Control Register Descriptor: %d\n",
296 ret);
297 return ret;
298 }
299 query_addr += 3;
300
301 ret = rmi_read_register_desc(rmi_dev, query_addr,
302 &f12->data_reg_desc);
303 if (ret) {
304 dev_err(&fn->dev,
305 "Failed to read the Data Register Descriptor: %d\n",
306 ret);
307 return ret;
308 }
309 query_addr += 3;
310
311 sensor = &f12->sensor;
312 sensor->fn = fn;
313 f12->data_addr = fn->fd.data_base_addr;
314 sensor->pkt_size = rmi_register_desc_calc_size(&f12->data_reg_desc);
315
316 sensor->axis_align =
317 f12->sensor_pdata.axis_align;
318
319 sensor->x_mm = f12->sensor_pdata.x_mm;
320 sensor->y_mm = f12->sensor_pdata.y_mm;
321
322 if (sensor->sensor_type == rmi_sensor_default)
323 sensor->sensor_type =
324 f12->sensor_pdata.sensor_type;
325
326 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: data packet size: %d\n", __func__,
327 sensor->pkt_size);
328 sensor->data_pkt = devm_kzalloc(&fn->dev, sensor->pkt_size, GFP_KERNEL);
329 if (!sensor->data_pkt)
330 return -ENOMEM;
331
332 dev_set_drvdata(&fn->dev, f12);
333
334 ret = rmi_f12_read_sensor_tuning(f12);
335 if (ret)
336 return ret;
337
338 /*
339 * Figure out what data is contained in the data registers. HID devices
340 * may have registers defined, but their data is not reported in the
341 * HID attention report. Registers which are not reported in the HID
342 * attention report check to see if the device is receiving data from
343 * HID attention reports.
344 */
345 item = rmi_get_register_desc_item(&f12->data_reg_desc, 0);
346 if (item && !xport->attn_data)
347 data_offset += item->reg_size;
348
349 item = rmi_get_register_desc_item(&f12->data_reg_desc, 1);
350 if (item) {
351 f12->data1 = item;
352 f12->data1_offset = data_offset;
353 data_offset += item->reg_size;
354 sensor->nbr_fingers = item->num_subpackets;
355 sensor->report_abs = 1;
356 sensor->attn_size += item->reg_size;
357 }
358
359 item = rmi_get_register_desc_item(&f12->data_reg_desc, 2);
360 if (item && !xport->attn_data)
361 data_offset += item->reg_size;
362
363 item = rmi_get_register_desc_item(&f12->data_reg_desc, 3);
364 if (item && !xport->attn_data)
365 data_offset += item->reg_size;
366
367 item = rmi_get_register_desc_item(&f12->data_reg_desc, 4);
368 if (item && !xport->attn_data)
369 data_offset += item->reg_size;
370
371 item = rmi_get_register_desc_item(&f12->data_reg_desc, 5);
372 if (item) {
373 f12->data5 = item;
374 f12->data5_offset = data_offset;
375 data_offset += item->reg_size;
376 sensor->attn_size += item->reg_size;
377 }
378
379 item = rmi_get_register_desc_item(&f12->data_reg_desc, 6);
380 if (item && !xport->attn_data) {
381 f12->data6 = item;
382 f12->data6_offset = data_offset;
383 data_offset += item->reg_size;
384 }
385
386 item = rmi_get_register_desc_item(&f12->data_reg_desc, 7);
387 if (item && !xport->attn_data)
388 data_offset += item->reg_size;
389
390 item = rmi_get_register_desc_item(&f12->data_reg_desc, 8);
391 if (item && !xport->attn_data)
392 data_offset += item->reg_size;
393
394 item = rmi_get_register_desc_item(&f12->data_reg_desc, 9);
395 if (item && !xport->attn_data) {
396 f12->data9 = item;
397 f12->data9_offset = data_offset;
398 data_offset += item->reg_size;
399 if (!sensor->report_abs)
400 sensor->report_rel = 1;
401 }
402
403 item = rmi_get_register_desc_item(&f12->data_reg_desc, 10);
404 if (item && !xport->attn_data)
405 data_offset += item->reg_size;
406
407 item = rmi_get_register_desc_item(&f12->data_reg_desc, 11);
408 if (item && !xport->attn_data)
409 data_offset += item->reg_size;
410
411 item = rmi_get_register_desc_item(&f12->data_reg_desc, 12);
412 if (item && !xport->attn_data)
413 data_offset += item->reg_size;
414
415 item = rmi_get_register_desc_item(&f12->data_reg_desc, 13);
416 if (item && !xport->attn_data)
417 data_offset += item->reg_size;
418
419 item = rmi_get_register_desc_item(&f12->data_reg_desc, 14);
420 if (item && !xport->attn_data)
421 data_offset += item->reg_size;
422
423 item = rmi_get_register_desc_item(&f12->data_reg_desc, 15);
424 if (item && !xport->attn_data) {
425 f12->data15 = item;
426 f12->data15_offset = data_offset;
427 data_offset += item->reg_size;
428 }
429
430 /* allocate the in-kernel tracking buffers */
431 sensor->tracking_pos = devm_kzalloc(&fn->dev,
432 sizeof(struct input_mt_pos) * sensor->nbr_fingers,
433 GFP_KERNEL);
434 sensor->tracking_slots = devm_kzalloc(&fn->dev,
435 sizeof(int) * sensor->nbr_fingers, GFP_KERNEL);
436 sensor->objs = devm_kzalloc(&fn->dev,
437 sizeof(struct rmi_2d_sensor_abs_object)
438 * sensor->nbr_fingers, GFP_KERNEL);
439 if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
440 return -ENOMEM;
441
442 ret = rmi_2d_sensor_configure_input(fn, sensor);
443 if (ret)
444 return ret;
445
446 return 0;
447}
448
449struct rmi_function_handler rmi_f12_handler = {
450 .driver = {
451 .name = "rmi4_f12",
452 },
453 .func = 0x12,
454 .probe = rmi_f12_probe,
455 .config = rmi_f12_config,
456 .attention = rmi_f12_attention,
457};
diff --git a/drivers/input/rmi4/rmi_f30.c b/drivers/input/rmi4/rmi_f30.c
new file mode 100644
index 000000000000..760aff1bc420
--- /dev/null
+++ b/drivers/input/rmi4/rmi_f30.c
@@ -0,0 +1,407 @@
1/*
2 * Copyright (c) 2012-2016 Synaptics Incorporated
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/rmi.h>
11#include <linux/input.h>
12#include <linux/slab.h>
13#include "rmi_driver.h"
14
15#define RMI_F30_QUERY_SIZE 2
16
17/* Defs for Query 0 */
18#define RMI_F30_EXTENDED_PATTERNS 0x01
19#define RMI_F30_HAS_MAPPABLE_BUTTONS (1 << 1)
20#define RMI_F30_HAS_LED (1 << 2)
21#define RMI_F30_HAS_GPIO (1 << 3)
22#define RMI_F30_HAS_HAPTIC (1 << 4)
23#define RMI_F30_HAS_GPIO_DRV_CTL (1 << 5)
24#define RMI_F30_HAS_MECH_MOUSE_BTNS (1 << 6)
25
26/* Defs for Query 1 */
27#define RMI_F30_GPIO_LED_COUNT 0x1F
28
29/* Defs for Control Registers */
30#define RMI_F30_CTRL_1_GPIO_DEBOUNCE 0x01
31#define RMI_F30_CTRL_1_HALT (1 << 4)
32#define RMI_F30_CTRL_1_HALTED (1 << 5)
33#define RMI_F30_CTRL_10_NUM_MECH_MOUSE_BTNS 0x03
34
35struct rmi_f30_ctrl_data {
36 int address;
37 int length;
38 u8 *regs;
39};
40
41#define RMI_F30_CTRL_MAX_REGS 32
42#define RMI_F30_CTRL_MAX_BYTES ((RMI_F30_CTRL_MAX_REGS + 7) >> 3)
43#define RMI_F30_CTRL_MAX_REG_BLOCKS 11
44
45#define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \
46 + 1 \
47 + RMI_F30_CTRL_MAX_BYTES \
48 + RMI_F30_CTRL_MAX_BYTES \
49 + RMI_F30_CTRL_MAX_BYTES \
50 + 6 \
51 + RMI_F30_CTRL_MAX_REGS \
52 + RMI_F30_CTRL_MAX_REGS \
53 + RMI_F30_CTRL_MAX_BYTES \
54 + 1 \
55 + 1)
56
57struct f30_data {
58 /* Query Data */
59 bool has_extended_pattern;
60 bool has_mappable_buttons;
61 bool has_led;
62 bool has_gpio;
63 bool has_haptic;
64 bool has_gpio_driver_control;
65 bool has_mech_mouse_btns;
66 u8 gpioled_count;
67
68 u8 register_count;
69
70 /* Control Register Data */
71 struct rmi_f30_ctrl_data ctrl[RMI_F30_CTRL_MAX_REG_BLOCKS];
72 u8 ctrl_regs[RMI_F30_CTRL_REGS_MAX_SIZE];
73 u32 ctrl_regs_size;
74
75 u8 data_regs[RMI_F30_CTRL_MAX_BYTES];
76 u16 *gpioled_key_map;
77
78 struct input_dev *input;
79};
80
81static int rmi_f30_read_control_parameters(struct rmi_function *fn,
82 struct f30_data *f30)
83{
84 struct rmi_device *rmi_dev = fn->rmi_dev;
85 int error = 0;
86
87 error = rmi_read_block(rmi_dev, fn->fd.control_base_addr,
88 f30->ctrl_regs, f30->ctrl_regs_size);
89 if (error) {
90 dev_err(&rmi_dev->dev, "%s : Could not read control registers at 0x%x error (%d)\n",
91 __func__, fn->fd.control_base_addr, error);
92 return error;
93 }
94
95 return 0;
96}
97
98static int rmi_f30_attention(struct rmi_function *fn, unsigned long *irq_bits)
99{
100 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
101 struct rmi_device *rmi_dev = fn->rmi_dev;
102 int retval;
103 int gpiled = 0;
104 int value = 0;
105 int i;
106 int reg_num;
107
108 if (!f30->input)
109 return 0;
110
111 /* Read the gpi led data. */
112 if (rmi_dev->xport->attn_data) {
113 memcpy(f30->data_regs, rmi_dev->xport->attn_data,
114 f30->register_count);
115 rmi_dev->xport->attn_data += f30->register_count;
116 rmi_dev->xport->attn_size -= f30->register_count;
117 } else {
118 retval = rmi_read_block(rmi_dev, fn->fd.data_base_addr,
119 f30->data_regs, f30->register_count);
120
121 if (retval) {
122 dev_err(&fn->dev, "%s: Failed to read F30 data registers.\n",
123 __func__);
124 return retval;
125 }
126 }
127
128 for (reg_num = 0; reg_num < f30->register_count; ++reg_num) {
129 for (i = 0; gpiled < f30->gpioled_count && i < 8; ++i,
130 ++gpiled) {
131 if (f30->gpioled_key_map[gpiled] != 0) {
132 /* buttons have pull up resistors */
133 value = (((f30->data_regs[reg_num] >> i) & 0x01)
134 == 0);
135
136 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
137 "%s: call input report key (0x%04x) value (0x%02x)",
138 __func__,
139 f30->gpioled_key_map[gpiled], value);
140 input_report_key(f30->input,
141 f30->gpioled_key_map[gpiled],
142 value);
143 }
144
145 }
146 }
147
148 return 0;
149}
150
151static int rmi_f30_register_device(struct rmi_function *fn)
152{
153 int i;
154 struct rmi_device *rmi_dev = fn->rmi_dev;
155 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
156 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
157 struct input_dev *input_dev;
158 int button_count = 0;
159
160 input_dev = drv_data->input;
161 if (!input_dev) {
162 dev_info(&fn->dev, "F30: no input device found, ignoring.\n");
163 return -EINVAL;
164 }
165
166 f30->input = input_dev;
167
168 set_bit(EV_KEY, input_dev->evbit);
169
170 input_dev->keycode = f30->gpioled_key_map;
171 input_dev->keycodesize = sizeof(u16);
172 input_dev->keycodemax = f30->gpioled_count;
173
174 for (i = 0; i < f30->gpioled_count; i++) {
175 if (f30->gpioled_key_map[i] != 0) {
176 input_set_capability(input_dev, EV_KEY,
177 f30->gpioled_key_map[i]);
178 button_count++;
179 }
180 }
181
182 if (button_count == 1)
183 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
184 return 0;
185}
186
187static int rmi_f30_config(struct rmi_function *fn)
188{
189 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
190 struct rmi_driver *drv = fn->rmi_dev->driver;
191 const struct rmi_device_platform_data *pdata =
192 rmi_get_platform_data(fn->rmi_dev);
193 int error;
194
195 if (pdata->f30_data && pdata->f30_data->disable) {
196 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
197 } else {
198 /* Write Control Register values back to device */
199 error = rmi_write_block(fn->rmi_dev, fn->fd.control_base_addr,
200 f30->ctrl_regs, f30->ctrl_regs_size);
201 if (error) {
202 dev_err(&fn->rmi_dev->dev,
203 "%s : Could not write control registers at 0x%x error (%d)\n",
204 __func__, fn->fd.control_base_addr, error);
205 return error;
206 }
207
208 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
209 }
210 return 0;
211}
212
213static inline void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data *ctrl,
214 int *ctrl_addr, int len, u8 **reg)
215{
216 ctrl->address = *ctrl_addr;
217 ctrl->length = len;
218 ctrl->regs = *reg;
219 *ctrl_addr += len;
220 *reg += len;
221}
222
223static inline bool rmi_f30_is_valid_button(int button,
224 struct rmi_f30_ctrl_data *ctrl)
225{
226 int byte_position = button >> 3;
227 int bit_position = button & 0x07;
228
229 /*
230 * ctrl2 -> dir == 0 -> input mode
231 * ctrl3 -> data == 1 -> actual button
232 */
233 return !(ctrl[2].regs[byte_position] & BIT(bit_position)) &&
234 (ctrl[3].regs[byte_position] & BIT(bit_position));
235}
236
237static inline int rmi_f30_initialize(struct rmi_function *fn)
238{
239 struct f30_data *f30;
240 struct rmi_device *rmi_dev = fn->rmi_dev;
241 const struct rmi_device_platform_data *pdata;
242 int retval = 0;
243 int control_address;
244 int i;
245 int button;
246 u8 buf[RMI_F30_QUERY_SIZE];
247 u8 *ctrl_reg;
248 u8 *map_memory;
249
250 f30 = devm_kzalloc(&fn->dev, sizeof(struct f30_data),
251 GFP_KERNEL);
252 if (!f30)
253 return -ENOMEM;
254
255 dev_set_drvdata(&fn->dev, f30);
256
257 retval = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr, buf,
258 RMI_F30_QUERY_SIZE);
259
260 if (retval) {
261 dev_err(&fn->dev, "Failed to read query register.\n");
262 return retval;
263 }
264
265 f30->has_extended_pattern = buf[0] & RMI_F30_EXTENDED_PATTERNS;
266 f30->has_mappable_buttons = buf[0] & RMI_F30_HAS_MAPPABLE_BUTTONS;
267 f30->has_led = buf[0] & RMI_F30_HAS_LED;
268 f30->has_gpio = buf[0] & RMI_F30_HAS_GPIO;
269 f30->has_haptic = buf[0] & RMI_F30_HAS_HAPTIC;
270 f30->has_gpio_driver_control = buf[0] & RMI_F30_HAS_GPIO_DRV_CTL;
271 f30->has_mech_mouse_btns = buf[0] & RMI_F30_HAS_MECH_MOUSE_BTNS;
272 f30->gpioled_count = buf[1] & RMI_F30_GPIO_LED_COUNT;
273
274 f30->register_count = (f30->gpioled_count + 7) >> 3;
275
276 control_address = fn->fd.control_base_addr;
277 ctrl_reg = f30->ctrl_regs;
278
279 if (f30->has_gpio && f30->has_led)
280 rmi_f30_set_ctrl_data(&f30->ctrl[0], &control_address,
281 f30->register_count, &ctrl_reg);
282
283 rmi_f30_set_ctrl_data(&f30->ctrl[1], &control_address, sizeof(u8),
284 &ctrl_reg);
285
286 if (f30->has_gpio) {
287 rmi_f30_set_ctrl_data(&f30->ctrl[2], &control_address,
288 f30->register_count, &ctrl_reg);
289
290 rmi_f30_set_ctrl_data(&f30->ctrl[3], &control_address,
291 f30->register_count, &ctrl_reg);
292 }
293
294 if (f30->has_led) {
295 int ctrl5_len;
296
297 rmi_f30_set_ctrl_data(&f30->ctrl[4], &control_address,
298 f30->register_count, &ctrl_reg);
299
300 if (f30->has_extended_pattern)
301 ctrl5_len = 6;
302 else
303 ctrl5_len = 2;
304
305 rmi_f30_set_ctrl_data(&f30->ctrl[5], &control_address,
306 ctrl5_len, &ctrl_reg);
307 }
308
309 if (f30->has_led || f30->has_gpio_driver_control) {
310 /* control 6 uses a byte per gpio/led */
311 rmi_f30_set_ctrl_data(&f30->ctrl[6], &control_address,
312 f30->gpioled_count, &ctrl_reg);
313 }
314
315 if (f30->has_mappable_buttons) {
316 /* control 7 uses a byte per gpio/led */
317 rmi_f30_set_ctrl_data(&f30->ctrl[7], &control_address,
318 f30->gpioled_count, &ctrl_reg);
319 }
320
321 if (f30->has_haptic) {
322 rmi_f30_set_ctrl_data(&f30->ctrl[8], &control_address,
323 f30->register_count, &ctrl_reg);
324
325 rmi_f30_set_ctrl_data(&f30->ctrl[9], &control_address,
326 sizeof(u8), &ctrl_reg);
327 }
328
329 if (f30->has_mech_mouse_btns)
330 rmi_f30_set_ctrl_data(&f30->ctrl[10], &control_address,
331 sizeof(u8), &ctrl_reg);
332
333 f30->ctrl_regs_size = ctrl_reg - f30->ctrl_regs
334 ?: RMI_F30_CTRL_REGS_MAX_SIZE;
335
336 retval = rmi_f30_read_control_parameters(fn, f30);
337 if (retval < 0) {
338 dev_err(&fn->dev,
339 "Failed to initialize F19 control params.\n");
340 return retval;
341 }
342
343 map_memory = devm_kzalloc(&fn->dev,
344 (f30->gpioled_count * (sizeof(u16))),
345 GFP_KERNEL);
346 if (!map_memory) {
347 dev_err(&fn->dev, "Failed to allocate gpioled map memory.\n");
348 return -ENOMEM;
349 }
350
351 f30->gpioled_key_map = (u16 *)map_memory;
352
353 pdata = rmi_get_platform_data(rmi_dev);
354 if (pdata && f30->has_gpio) {
355 button = BTN_LEFT;
356 for (i = 0; i < f30->gpioled_count; i++) {
357 if (rmi_f30_is_valid_button(i, f30->ctrl)) {
358 f30->gpioled_key_map[i] = button++;
359
360 /*
361 * buttonpad might be given by
362 * f30->has_mech_mouse_btns, but I am
363 * not sure, so use only the pdata info
364 */
365 if (pdata->f30_data &&
366 pdata->f30_data->buttonpad)
367 break;
368 }
369 }
370 }
371
372 return 0;
373}
374
375static int rmi_f30_probe(struct rmi_function *fn)
376{
377 int rc;
378 const struct rmi_device_platform_data *pdata =
379 rmi_get_platform_data(fn->rmi_dev);
380
381 if (pdata->f30_data && pdata->f30_data->disable)
382 return 0;
383
384 rc = rmi_f30_initialize(fn);
385 if (rc < 0)
386 goto error_exit;
387
388 rc = rmi_f30_register_device(fn);
389 if (rc < 0)
390 goto error_exit;
391
392 return 0;
393
394error_exit:
395 return rc;
396
397}
398
399struct rmi_function_handler rmi_f30_handler = {
400 .driver = {
401 .name = "rmi4_f30",
402 },
403 .func = 0x30,
404 .probe = rmi_f30_probe,
405 .config = rmi_f30_config,
406 .attention = rmi_f30_attention,
407};
diff --git a/drivers/input/rmi4/rmi_i2c.c b/drivers/input/rmi4/rmi_i2c.c
new file mode 100644
index 000000000000..a96a326b53bd
--- /dev/null
+++ b/drivers/input/rmi4/rmi_i2c.c
@@ -0,0 +1,397 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/i2c.h>
11#include <linux/rmi.h>
12#include <linux/irq.h>
13#include <linux/of.h>
14#include "rmi_driver.h"
15
16#define BUFFER_SIZE_INCREMENT 32
17
18/**
19 * struct rmi_i2c_xport - stores information for i2c communication
20 *
21 * @xport: The transport interface structure
22 *
23 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
24 * @page: Keeps track of the current virtual page
25 *
26 * @tx_buf: Buffer used for transmitting data to the sensor over i2c.
27 * @tx_buf_size: Size of the buffer
28 */
29struct rmi_i2c_xport {
30 struct rmi_transport_dev xport;
31 struct i2c_client *client;
32
33 struct mutex page_mutex;
34 int page;
35
36 int irq;
37
38 u8 *tx_buf;
39 size_t tx_buf_size;
40};
41
42#define RMI_PAGE_SELECT_REGISTER 0xff
43#define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
44
45/*
46 * rmi_set_page - Set RMI page
47 * @xport: The pointer to the rmi_transport_dev struct
48 * @page: The new page address.
49 *
50 * RMI devices have 16-bit addressing, but some of the transport
51 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
52 * a page address at 0xff of every page so we can reliable page addresses
53 * every 256 registers.
54 *
55 * The page_mutex lock must be held when this function is entered.
56 *
57 * Returns zero on success, non-zero on failure.
58 */
59static int rmi_set_page(struct rmi_i2c_xport *rmi_i2c, u8 page)
60{
61 struct i2c_client *client = rmi_i2c->client;
62 u8 txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page};
63 int retval;
64
65 retval = i2c_master_send(client, txbuf, sizeof(txbuf));
66 if (retval != sizeof(txbuf)) {
67 dev_err(&client->dev,
68 "%s: set page failed: %d.", __func__, retval);
69 return (retval < 0) ? retval : -EIO;
70 }
71
72 rmi_i2c->page = page;
73 return 0;
74}
75
76static int rmi_i2c_write_block(struct rmi_transport_dev *xport, u16 addr,
77 const void *buf, size_t len)
78{
79 struct rmi_i2c_xport *rmi_i2c =
80 container_of(xport, struct rmi_i2c_xport, xport);
81 struct i2c_client *client = rmi_i2c->client;
82 size_t tx_size = len + 1;
83 int retval;
84
85 mutex_lock(&rmi_i2c->page_mutex);
86
87 if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) {
88 if (rmi_i2c->tx_buf)
89 devm_kfree(&client->dev, rmi_i2c->tx_buf);
90 rmi_i2c->tx_buf_size = tx_size + BUFFER_SIZE_INCREMENT;
91 rmi_i2c->tx_buf = devm_kzalloc(&client->dev,
92 rmi_i2c->tx_buf_size,
93 GFP_KERNEL);
94 if (!rmi_i2c->tx_buf) {
95 rmi_i2c->tx_buf_size = 0;
96 retval = -ENOMEM;
97 goto exit;
98 }
99 }
100
101 rmi_i2c->tx_buf[0] = addr & 0xff;
102 memcpy(rmi_i2c->tx_buf + 1, buf, len);
103
104 if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
105 retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
106 if (retval)
107 goto exit;
108 }
109
110 retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size);
111 if (retval == tx_size)
112 retval = 0;
113 else if (retval >= 0)
114 retval = -EIO;
115
116exit:
117 rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
118 "write %zd bytes at %#06x: %d (%*ph)\n",
119 len, addr, retval, (int)len, buf);
120
121 mutex_unlock(&rmi_i2c->page_mutex);
122 return retval;
123}
124
125static int rmi_i2c_read_block(struct rmi_transport_dev *xport, u16 addr,
126 void *buf, size_t len)
127{
128 struct rmi_i2c_xport *rmi_i2c =
129 container_of(xport, struct rmi_i2c_xport, xport);
130 struct i2c_client *client = rmi_i2c->client;
131 u8 addr_offset = addr & 0xff;
132 int retval;
133 struct i2c_msg msgs[] = {
134 {
135 .addr = client->addr,
136 .len = sizeof(addr_offset),
137 .buf = &addr_offset,
138 },
139 {
140 .addr = client->addr,
141 .flags = I2C_M_RD,
142 .len = len,
143 .buf = buf,
144 },
145 };
146
147 mutex_lock(&rmi_i2c->page_mutex);
148
149 if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
150 retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
151 if (retval)
152 goto exit;
153 }
154
155 retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
156 if (retval == ARRAY_SIZE(msgs))
157 retval = 0; /* success */
158 else if (retval >= 0)
159 retval = -EIO;
160
161exit:
162 rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
163 "read %zd bytes at %#06x: %d (%*ph)\n",
164 len, addr, retval, (int)len, buf);
165
166 mutex_unlock(&rmi_i2c->page_mutex);
167 return retval;
168}
169
170static const struct rmi_transport_ops rmi_i2c_ops = {
171 .write_block = rmi_i2c_write_block,
172 .read_block = rmi_i2c_read_block,
173};
174
175static irqreturn_t rmi_i2c_irq(int irq, void *dev_id)
176{
177 struct rmi_i2c_xport *rmi_i2c = dev_id;
178 struct rmi_device *rmi_dev = rmi_i2c->xport.rmi_dev;
179 int ret;
180
181 ret = rmi_process_interrupt_requests(rmi_dev);
182 if (ret)
183 rmi_dbg(RMI_DEBUG_XPORT, &rmi_dev->dev,
184 "Failed to process interrupt request: %d\n", ret);
185
186 return IRQ_HANDLED;
187}
188
189static int rmi_i2c_init_irq(struct i2c_client *client)
190{
191 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
192 int irq_flags = irqd_get_trigger_type(irq_get_irq_data(rmi_i2c->irq));
193 int ret;
194
195 if (!irq_flags)
196 irq_flags = IRQF_TRIGGER_LOW;
197
198 ret = devm_request_threaded_irq(&client->dev, rmi_i2c->irq, NULL,
199 rmi_i2c_irq, irq_flags | IRQF_ONESHOT, client->name,
200 rmi_i2c);
201 if (ret < 0) {
202 dev_warn(&client->dev, "Failed to register interrupt %d\n",
203 rmi_i2c->irq);
204
205 return ret;
206 }
207
208 return 0;
209}
210
211#ifdef CONFIG_OF
212static const struct of_device_id rmi_i2c_of_match[] = {
213 { .compatible = "syna,rmi4-i2c" },
214 {},
215};
216MODULE_DEVICE_TABLE(of, rmi_i2c_of_match);
217#endif
218
219static int rmi_i2c_probe(struct i2c_client *client,
220 const struct i2c_device_id *id)
221{
222 struct rmi_device_platform_data *pdata;
223 struct rmi_device_platform_data *client_pdata =
224 dev_get_platdata(&client->dev);
225 struct rmi_i2c_xport *rmi_i2c;
226 int retval;
227
228 rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport),
229 GFP_KERNEL);
230 if (!rmi_i2c)
231 return -ENOMEM;
232
233 pdata = &rmi_i2c->xport.pdata;
234
235 if (!client->dev.of_node && client_pdata)
236 *pdata = *client_pdata;
237
238 if (client->irq > 0)
239 rmi_i2c->irq = client->irq;
240
241 rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s.\n",
242 dev_name(&client->dev));
243 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
244 dev_err(&client->dev,
245 "adapter does not support required functionality.\n");
246 return -ENODEV;
247 }
248
249 rmi_i2c->client = client;
250 mutex_init(&rmi_i2c->page_mutex);
251
252 rmi_i2c->xport.dev = &client->dev;
253 rmi_i2c->xport.proto_name = "i2c";
254 rmi_i2c->xport.ops = &rmi_i2c_ops;
255
256 i2c_set_clientdata(client, rmi_i2c);
257
258 /*
259 * Setting the page to zero will (a) make sure the PSR is in a
260 * known state, and (b) make sure we can talk to the device.
261 */
262 retval = rmi_set_page(rmi_i2c, 0);
263 if (retval) {
264 dev_err(&client->dev, "Failed to set page select to 0.\n");
265 return retval;
266 }
267
268 retval = rmi_register_transport_device(&rmi_i2c->xport);
269 if (retval) {
270 dev_err(&client->dev, "Failed to register transport driver at 0x%.2X.\n",
271 client->addr);
272 return retval;
273 }
274
275 retval = rmi_i2c_init_irq(client);
276 if (retval < 0)
277 return retval;
278
279 dev_info(&client->dev, "registered rmi i2c driver at %#04x.\n",
280 client->addr);
281 return 0;
282}
283
284static int rmi_i2c_remove(struct i2c_client *client)
285{
286 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
287
288 rmi_unregister_transport_device(&rmi_i2c->xport);
289
290 return 0;
291}
292
293#ifdef CONFIG_PM_SLEEP
294static int rmi_i2c_suspend(struct device *dev)
295{
296 struct i2c_client *client = to_i2c_client(dev);
297 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
298 int ret;
299
300 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev);
301 if (ret)
302 dev_warn(dev, "Failed to resume device: %d\n", ret);
303
304 disable_irq(rmi_i2c->irq);
305 if (device_may_wakeup(&client->dev)) {
306 ret = enable_irq_wake(rmi_i2c->irq);
307 if (!ret)
308 dev_warn(dev, "Failed to enable irq for wake: %d\n",
309 ret);
310 }
311 return ret;
312}
313
314static int rmi_i2c_resume(struct device *dev)
315{
316 struct i2c_client *client = to_i2c_client(dev);
317 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
318 int ret;
319
320 enable_irq(rmi_i2c->irq);
321 if (device_may_wakeup(&client->dev)) {
322 ret = disable_irq_wake(rmi_i2c->irq);
323 if (!ret)
324 dev_warn(dev, "Failed to disable irq for wake: %d\n",
325 ret);
326 }
327
328 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev);
329 if (ret)
330 dev_warn(dev, "Failed to resume device: %d\n", ret);
331
332 return ret;
333}
334#endif
335
336#ifdef CONFIG_PM
337static int rmi_i2c_runtime_suspend(struct device *dev)
338{
339 struct i2c_client *client = to_i2c_client(dev);
340 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
341 int ret;
342
343 ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev);
344 if (ret)
345 dev_warn(dev, "Failed to resume device: %d\n", ret);
346
347 disable_irq(rmi_i2c->irq);
348
349 return 0;
350}
351
352static int rmi_i2c_runtime_resume(struct device *dev)
353{
354 struct i2c_client *client = to_i2c_client(dev);
355 struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
356 int ret;
357
358 enable_irq(rmi_i2c->irq);
359
360 ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev);
361 if (ret)
362 dev_warn(dev, "Failed to resume device: %d\n", ret);
363
364 return 0;
365}
366#endif
367
368static const struct dev_pm_ops rmi_i2c_pm = {
369 SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend, rmi_i2c_resume)
370 SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend, rmi_i2c_runtime_resume,
371 NULL)
372};
373
374static const struct i2c_device_id rmi_id[] = {
375 { "rmi4_i2c", 0 },
376 { }
377};
378MODULE_DEVICE_TABLE(i2c, rmi_id);
379
380static struct i2c_driver rmi_i2c_driver = {
381 .driver = {
382 .name = "rmi4_i2c",
383 .pm = &rmi_i2c_pm,
384 .of_match_table = of_match_ptr(rmi_i2c_of_match),
385 },
386 .id_table = rmi_id,
387 .probe = rmi_i2c_probe,
388 .remove = rmi_i2c_remove,
389};
390
391module_i2c_driver(rmi_i2c_driver);
392
393MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
394MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
395MODULE_DESCRIPTION("RMI I2C driver");
396MODULE_LICENSE("GPL");
397MODULE_VERSION(RMI_DRIVER_VERSION);
diff --git a/drivers/input/rmi4/rmi_spi.c b/drivers/input/rmi4/rmi_spi.c
new file mode 100644
index 000000000000..55bd1b34970c
--- /dev/null
+++ b/drivers/input/rmi4/rmi_spi.c
@@ -0,0 +1,589 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/rmi.h>
13#include <linux/slab.h>
14#include <linux/spi/spi.h>
15#include <linux/irq.h>
16#include <linux/of.h>
17#include "rmi_driver.h"
18
19#define RMI_SPI_DEFAULT_XFER_BUF_SIZE 64
20
21#define RMI_PAGE_SELECT_REGISTER 0x00FF
22#define RMI_SPI_PAGE(addr) (((addr) >> 8) & 0x80)
23#define RMI_SPI_XFER_SIZE_LIMIT 255
24
25#define BUFFER_SIZE_INCREMENT 32
26
27enum rmi_spi_op {
28 RMI_SPI_WRITE = 0,
29 RMI_SPI_READ,
30 RMI_SPI_V2_READ_UNIFIED,
31 RMI_SPI_V2_READ_SPLIT,
32 RMI_SPI_V2_WRITE,
33};
34
35struct rmi_spi_cmd {
36 enum rmi_spi_op op;
37 u16 addr;
38};
39
40struct rmi_spi_xport {
41 struct rmi_transport_dev xport;
42 struct spi_device *spi;
43
44 struct mutex page_mutex;
45 int page;
46
47 int irq;
48
49 u8 *rx_buf;
50 u8 *tx_buf;
51 int xfer_buf_size;
52
53 struct spi_transfer *rx_xfers;
54 struct spi_transfer *tx_xfers;
55 int rx_xfer_count;
56 int tx_xfer_count;
57};
58
59static int rmi_spi_manage_pools(struct rmi_spi_xport *rmi_spi, int len)
60{
61 struct spi_device *spi = rmi_spi->spi;
62 int buf_size = rmi_spi->xfer_buf_size
63 ? rmi_spi->xfer_buf_size : RMI_SPI_DEFAULT_XFER_BUF_SIZE;
64 struct spi_transfer *xfer_buf;
65 void *buf;
66 void *tmp;
67
68 while (buf_size < len)
69 buf_size *= 2;
70
71 if (buf_size > RMI_SPI_XFER_SIZE_LIMIT)
72 buf_size = RMI_SPI_XFER_SIZE_LIMIT;
73
74 tmp = rmi_spi->rx_buf;
75 buf = devm_kzalloc(&spi->dev, buf_size * 2,
76 GFP_KERNEL | GFP_DMA);
77 if (!buf)
78 return -ENOMEM;
79
80 rmi_spi->rx_buf = buf;
81 rmi_spi->tx_buf = &rmi_spi->rx_buf[buf_size];
82 rmi_spi->xfer_buf_size = buf_size;
83
84 if (tmp)
85 devm_kfree(&spi->dev, tmp);
86
87 if (rmi_spi->xport.pdata.spi_data.read_delay_us)
88 rmi_spi->rx_xfer_count = buf_size;
89 else
90 rmi_spi->rx_xfer_count = 1;
91
92 if (rmi_spi->xport.pdata.spi_data.write_delay_us)
93 rmi_spi->tx_xfer_count = buf_size;
94 else
95 rmi_spi->tx_xfer_count = 1;
96
97 /*
98 * Allocate a pool of spi_transfer buffers for devices which need
99 * per byte delays.
100 */
101 tmp = rmi_spi->rx_xfers;
102 xfer_buf = devm_kzalloc(&spi->dev,
103 (rmi_spi->rx_xfer_count + rmi_spi->tx_xfer_count)
104 * sizeof(struct spi_transfer), GFP_KERNEL);
105 if (!xfer_buf)
106 return -ENOMEM;
107
108 rmi_spi->rx_xfers = xfer_buf;
109 rmi_spi->tx_xfers = &xfer_buf[rmi_spi->rx_xfer_count];
110
111 if (tmp)
112 devm_kfree(&spi->dev, tmp);
113
114 return 0;
115}
116
117static int rmi_spi_xfer(struct rmi_spi_xport *rmi_spi,
118 const struct rmi_spi_cmd *cmd, const u8 *tx_buf,
119 int tx_len, u8 *rx_buf, int rx_len)
120{
121 struct spi_device *spi = rmi_spi->spi;
122 struct rmi_device_platform_data_spi *spi_data =
123 &rmi_spi->xport.pdata.spi_data;
124 struct spi_message msg;
125 struct spi_transfer *xfer;
126 int ret = 0;
127 int len;
128 int cmd_len = 0;
129 int total_tx_len;
130 int i;
131 u16 addr = cmd->addr;
132
133 spi_message_init(&msg);
134
135 switch (cmd->op) {
136 case RMI_SPI_WRITE:
137 case RMI_SPI_READ:
138 cmd_len += 2;
139 break;
140 case RMI_SPI_V2_READ_UNIFIED:
141 case RMI_SPI_V2_READ_SPLIT:
142 case RMI_SPI_V2_WRITE:
143 cmd_len += 4;
144 break;
145 }
146
147 total_tx_len = cmd_len + tx_len;
148 len = max(total_tx_len, rx_len);
149
150 if (len > RMI_SPI_XFER_SIZE_LIMIT)
151 return -EINVAL;
152
153 if (rmi_spi->xfer_buf_size < len)
154 rmi_spi_manage_pools(rmi_spi, len);
155
156 if (addr == 0)
157 /*
158 * SPI needs an address. Use 0x7FF if we want to keep
159 * reading from the last position of the register pointer.
160 */
161 addr = 0x7FF;
162
163 switch (cmd->op) {
164 case RMI_SPI_WRITE:
165 rmi_spi->tx_buf[0] = (addr >> 8);
166 rmi_spi->tx_buf[1] = addr & 0xFF;
167 break;
168 case RMI_SPI_READ:
169 rmi_spi->tx_buf[0] = (addr >> 8) | 0x80;
170 rmi_spi->tx_buf[1] = addr & 0xFF;
171 break;
172 case RMI_SPI_V2_READ_UNIFIED:
173 break;
174 case RMI_SPI_V2_READ_SPLIT:
175 break;
176 case RMI_SPI_V2_WRITE:
177 rmi_spi->tx_buf[0] = 0x40;
178 rmi_spi->tx_buf[1] = (addr >> 8) & 0xFF;
179 rmi_spi->tx_buf[2] = addr & 0xFF;
180 rmi_spi->tx_buf[3] = tx_len;
181 break;
182 }
183
184 if (tx_buf)
185 memcpy(&rmi_spi->tx_buf[cmd_len], tx_buf, tx_len);
186
187 if (rmi_spi->tx_xfer_count > 1) {
188 for (i = 0; i < total_tx_len; i++) {
189 xfer = &rmi_spi->tx_xfers[i];
190 memset(xfer, 0, sizeof(struct spi_transfer));
191 xfer->tx_buf = &rmi_spi->tx_buf[i];
192 xfer->len = 1;
193 xfer->delay_usecs = spi_data->write_delay_us;
194 spi_message_add_tail(xfer, &msg);
195 }
196 } else {
197 xfer = rmi_spi->tx_xfers;
198 memset(xfer, 0, sizeof(struct spi_transfer));
199 xfer->tx_buf = rmi_spi->tx_buf;
200 xfer->len = total_tx_len;
201 spi_message_add_tail(xfer, &msg);
202 }
203
204 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: cmd: %s tx_buf len: %d tx_buf: %*ph\n",
205 __func__, cmd->op == RMI_SPI_WRITE ? "WRITE" : "READ",
206 total_tx_len, total_tx_len, rmi_spi->tx_buf);
207
208 if (rx_buf) {
209 if (rmi_spi->rx_xfer_count > 1) {
210 for (i = 0; i < rx_len; i++) {
211 xfer = &rmi_spi->rx_xfers[i];
212 memset(xfer, 0, sizeof(struct spi_transfer));
213 xfer->rx_buf = &rmi_spi->rx_buf[i];
214 xfer->len = 1;
215 xfer->delay_usecs = spi_data->read_delay_us;
216 spi_message_add_tail(xfer, &msg);
217 }
218 } else {
219 xfer = rmi_spi->rx_xfers;
220 memset(xfer, 0, sizeof(struct spi_transfer));
221 xfer->rx_buf = rmi_spi->rx_buf;
222 xfer->len = rx_len;
223 spi_message_add_tail(xfer, &msg);
224 }
225 }
226
227 ret = spi_sync(spi, &msg);
228 if (ret < 0) {
229 dev_err(&spi->dev, "spi xfer failed: %d\n", ret);
230 return ret;
231 }
232
233 if (rx_buf) {
234 memcpy(rx_buf, rmi_spi->rx_buf, rx_len);
235 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: (%d) %*ph\n",
236 __func__, rx_len, rx_len, rx_buf);
237 }
238
239 return 0;
240}
241
242/*
243 * rmi_set_page - Set RMI page
244 * @xport: The pointer to the rmi_transport_dev struct
245 * @page: The new page address.
246 *
247 * RMI devices have 16-bit addressing, but some of the transport
248 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
249 * a page address at 0xff of every page so we can reliable page addresses
250 * every 256 registers.
251 *
252 * The page_mutex lock must be held when this function is entered.
253 *
254 * Returns zero on success, non-zero on failure.
255 */
256static int rmi_set_page(struct rmi_spi_xport *rmi_spi, u8 page)
257{
258 struct rmi_spi_cmd cmd;
259 int ret;
260
261 cmd.op = RMI_SPI_WRITE;
262 cmd.addr = RMI_PAGE_SELECT_REGISTER;
263
264 ret = rmi_spi_xfer(rmi_spi, &cmd, &page, 1, NULL, 0);
265
266 if (ret)
267 rmi_spi->page = page;
268
269 return ret;
270}
271
272static int rmi_spi_write_block(struct rmi_transport_dev *xport, u16 addr,
273 const void *buf, size_t len)
274{
275 struct rmi_spi_xport *rmi_spi =
276 container_of(xport, struct rmi_spi_xport, xport);
277 struct rmi_spi_cmd cmd;
278 int ret;
279
280 mutex_lock(&rmi_spi->page_mutex);
281
282 if (RMI_SPI_PAGE(addr) != rmi_spi->page) {
283 ret = rmi_set_page(rmi_spi, RMI_SPI_PAGE(addr));
284 if (ret)
285 goto exit;
286 }
287
288 cmd.op = RMI_SPI_WRITE;
289 cmd.addr = addr;
290
291 ret = rmi_spi_xfer(rmi_spi, &cmd, buf, len, NULL, 0);
292
293exit:
294 mutex_unlock(&rmi_spi->page_mutex);
295 return ret;
296}
297
298static int rmi_spi_read_block(struct rmi_transport_dev *xport, u16 addr,
299 void *buf, size_t len)
300{
301 struct rmi_spi_xport *rmi_spi =
302 container_of(xport, struct rmi_spi_xport, xport);
303 struct rmi_spi_cmd cmd;
304 int ret;
305
306 mutex_lock(&rmi_spi->page_mutex);
307
308 if (RMI_SPI_PAGE(addr) != rmi_spi->page) {
309 ret = rmi_set_page(rmi_spi, RMI_SPI_PAGE(addr));
310 if (ret)
311 goto exit;
312 }
313
314 cmd.op = RMI_SPI_READ;
315 cmd.addr = addr;
316
317 ret = rmi_spi_xfer(rmi_spi, &cmd, NULL, 0, buf, len);
318
319exit:
320 mutex_unlock(&rmi_spi->page_mutex);
321 return ret;
322}
323
324static const struct rmi_transport_ops rmi_spi_ops = {
325 .write_block = rmi_spi_write_block,
326 .read_block = rmi_spi_read_block,
327};
328
329static irqreturn_t rmi_spi_irq(int irq, void *dev_id)
330{
331 struct rmi_spi_xport *rmi_spi = dev_id;
332 struct rmi_device *rmi_dev = rmi_spi->xport.rmi_dev;
333 int ret;
334
335 ret = rmi_process_interrupt_requests(rmi_dev);
336 if (ret)
337 rmi_dbg(RMI_DEBUG_XPORT, &rmi_dev->dev,
338 "Failed to process interrupt request: %d\n", ret);
339
340 return IRQ_HANDLED;
341}
342
343static int rmi_spi_init_irq(struct spi_device *spi)
344{
345 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
346 int irq_flags = irqd_get_trigger_type(irq_get_irq_data(rmi_spi->irq));
347 int ret;
348
349 if (!irq_flags)
350 irq_flags = IRQF_TRIGGER_LOW;
351
352 ret = devm_request_threaded_irq(&spi->dev, rmi_spi->irq, NULL,
353 rmi_spi_irq, irq_flags | IRQF_ONESHOT,
354 dev_name(&spi->dev), rmi_spi);
355 if (ret < 0) {
356 dev_warn(&spi->dev, "Failed to register interrupt %d\n",
357 rmi_spi->irq);
358 return ret;
359 }
360
361 return 0;
362}
363
364#ifdef CONFIG_OF
365static int rmi_spi_of_probe(struct spi_device *spi,
366 struct rmi_device_platform_data *pdata)
367{
368 struct device *dev = &spi->dev;
369 int retval;
370
371 retval = rmi_of_property_read_u32(dev,
372 &pdata->spi_data.read_delay_us,
373 "spi-rx-delay-us", 1);
374 if (retval)
375 return retval;
376
377 retval = rmi_of_property_read_u32(dev,
378 &pdata->spi_data.write_delay_us,
379 "spi-tx-delay-us", 1);
380 if (retval)
381 return retval;
382
383 return 0;
384}
385
386static const struct of_device_id rmi_spi_of_match[] = {
387 { .compatible = "syna,rmi4-spi" },
388 {},
389};
390MODULE_DEVICE_TABLE(of, rmi_spi_of_match);
391#else
392static inline int rmi_spi_of_probe(struct spi_device *spi,
393 struct rmi_device_platform_data *pdata)
394{
395 return -ENODEV;
396}
397#endif
398
399static int rmi_spi_probe(struct spi_device *spi)
400{
401 struct rmi_spi_xport *rmi_spi;
402 struct rmi_device_platform_data *pdata;
403 struct rmi_device_platform_data *spi_pdata = spi->dev.platform_data;
404 int retval;
405
406 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX)
407 return -EINVAL;
408
409 rmi_spi = devm_kzalloc(&spi->dev, sizeof(struct rmi_spi_xport),
410 GFP_KERNEL);
411 if (!rmi_spi)
412 return -ENOMEM;
413
414 pdata = &rmi_spi->xport.pdata;
415
416 if (spi->dev.of_node) {
417 retval = rmi_spi_of_probe(spi, pdata);
418 if (retval)
419 return retval;
420 } else if (spi_pdata) {
421 *pdata = *spi_pdata;
422 }
423
424 if (pdata->spi_data.bits_per_word)
425 spi->bits_per_word = pdata->spi_data.bits_per_word;
426
427 if (pdata->spi_data.mode)
428 spi->mode = pdata->spi_data.mode;
429
430 retval = spi_setup(spi);
431 if (retval < 0) {
432 dev_err(&spi->dev, "spi_setup failed!\n");
433 return retval;
434 }
435
436 if (spi->irq > 0)
437 rmi_spi->irq = spi->irq;
438
439 rmi_spi->spi = spi;
440 mutex_init(&rmi_spi->page_mutex);
441
442 rmi_spi->xport.dev = &spi->dev;
443 rmi_spi->xport.proto_name = "spi";
444 rmi_spi->xport.ops = &rmi_spi_ops;
445
446 spi_set_drvdata(spi, rmi_spi);
447
448 retval = rmi_spi_manage_pools(rmi_spi, RMI_SPI_DEFAULT_XFER_BUF_SIZE);
449 if (retval)
450 return retval;
451
452 /*
453 * Setting the page to zero will (a) make sure the PSR is in a
454 * known state, and (b) make sure we can talk to the device.
455 */
456 retval = rmi_set_page(rmi_spi, 0);
457 if (retval) {
458 dev_err(&spi->dev, "Failed to set page select to 0.\n");
459 return retval;
460 }
461
462 retval = rmi_register_transport_device(&rmi_spi->xport);
463 if (retval) {
464 dev_err(&spi->dev, "failed to register transport.\n");
465 return retval;
466 }
467
468 retval = rmi_spi_init_irq(spi);
469 if (retval < 0)
470 return retval;
471
472 dev_info(&spi->dev, "registered RMI SPI driver\n");
473 return 0;
474}
475
476static int rmi_spi_remove(struct spi_device *spi)
477{
478 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
479
480 rmi_unregister_transport_device(&rmi_spi->xport);
481
482 return 0;
483}
484
485#ifdef CONFIG_PM_SLEEP
486static int rmi_spi_suspend(struct device *dev)
487{
488 struct spi_device *spi = to_spi_device(dev);
489 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
490 int ret;
491
492 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev);
493 if (ret)
494 dev_warn(dev, "Failed to resume device: %d\n", ret);
495
496 disable_irq(rmi_spi->irq);
497 if (device_may_wakeup(&spi->dev)) {
498 ret = enable_irq_wake(rmi_spi->irq);
499 if (!ret)
500 dev_warn(dev, "Failed to enable irq for wake: %d\n",
501 ret);
502 }
503 return ret;
504}
505
506static int rmi_spi_resume(struct device *dev)
507{
508 struct spi_device *spi = to_spi_device(dev);
509 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
510 int ret;
511
512 enable_irq(rmi_spi->irq);
513 if (device_may_wakeup(&spi->dev)) {
514 ret = disable_irq_wake(rmi_spi->irq);
515 if (!ret)
516 dev_warn(dev, "Failed to disable irq for wake: %d\n",
517 ret);
518 }
519
520 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev);
521 if (ret)
522 dev_warn(dev, "Failed to resume device: %d\n", ret);
523
524 return ret;
525}
526#endif
527
528#ifdef CONFIG_PM
529static int rmi_spi_runtime_suspend(struct device *dev)
530{
531 struct spi_device *spi = to_spi_device(dev);
532 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
533 int ret;
534
535 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev);
536 if (ret)
537 dev_warn(dev, "Failed to resume device: %d\n", ret);
538
539 disable_irq(rmi_spi->irq);
540
541 return 0;
542}
543
544static int rmi_spi_runtime_resume(struct device *dev)
545{
546 struct spi_device *spi = to_spi_device(dev);
547 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
548 int ret;
549
550 enable_irq(rmi_spi->irq);
551
552 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev);
553 if (ret)
554 dev_warn(dev, "Failed to resume device: %d\n", ret);
555
556 return 0;
557}
558#endif
559
560static const struct dev_pm_ops rmi_spi_pm = {
561 SET_SYSTEM_SLEEP_PM_OPS(rmi_spi_suspend, rmi_spi_resume)
562 SET_RUNTIME_PM_OPS(rmi_spi_runtime_suspend, rmi_spi_runtime_resume,
563 NULL)
564};
565
566static const struct spi_device_id rmi_id[] = {
567 { "rmi4_spi", 0 },
568 { }
569};
570MODULE_DEVICE_TABLE(spi, rmi_id);
571
572static struct spi_driver rmi_spi_driver = {
573 .driver = {
574 .name = "rmi4_spi",
575 .pm = &rmi_spi_pm,
576 .of_match_table = of_match_ptr(rmi_spi_of_match),
577 },
578 .id_table = rmi_id,
579 .probe = rmi_spi_probe,
580 .remove = rmi_spi_remove,
581};
582
583module_spi_driver(rmi_spi_driver);
584
585MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
586MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
587MODULE_DESCRIPTION("RMI SPI driver");
588MODULE_LICENSE("GPL");
589MODULE_VERSION(RMI_DRIVER_VERSION);
diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 66c62641b59a..1f99e7f30e27 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -334,7 +334,7 @@ config TOUCHSCREEN_FUJITSU
334config TOUCHSCREEN_GOODIX 334config TOUCHSCREEN_GOODIX
335 tristate "Goodix I2C touchscreen" 335 tristate "Goodix I2C touchscreen"
336 depends on I2C 336 depends on I2C
337 depends on GPIOLIB 337 depends on GPIOLIB || COMPILE_TEST
338 help 338 help
339 Say Y here if you have the Goodix touchscreen (such as one 339 Say Y here if you have the Goodix touchscreen (such as one
340 installed in Onda v975w tablets) connected to your 340 installed in Onda v975w tablets) connected to your
@@ -491,6 +491,17 @@ config TOUCHSCREEN_MMS114
491 To compile this driver as a module, choose M here: the 491 To compile this driver as a module, choose M here: the
492 module will be called mms114. 492 module will be called mms114.
493 493
494config TOUCHSCREEN_MELFAS_MIP4
495 tristate "MELFAS MIP4 Touchscreen"
496 depends on I2C
497 help
498 Say Y here if you have a MELFAS MIP4 Touchscreen device.
499
500 If unsure, say N.
501
502 To compile this driver as a module, choose M here:
503 the module will be called melfas_mip4.
504
494config TOUCHSCREEN_MTOUCH 505config TOUCHSCREEN_MTOUCH
495 tristate "MicroTouch serial touchscreens" 506 tristate "MicroTouch serial touchscreens"
496 select SERIO 507 select SERIO
@@ -941,6 +952,7 @@ config TOUCHSCREEN_TOUCHIT213
941config TOUCHSCREEN_TS4800 952config TOUCHSCREEN_TS4800
942 tristate "TS-4800 touchscreen" 953 tristate "TS-4800 touchscreen"
943 depends on HAS_IOMEM && OF 954 depends on HAS_IOMEM && OF
955 depends on SOC_IMX51 || COMPILE_TEST
944 select MFD_SYSCON 956 select MFD_SYSCON
945 select INPUT_POLLDEV 957 select INPUT_POLLDEV
946 help 958 help
@@ -1112,7 +1124,8 @@ config TOUCHSCREEN_ZFORCE
1112 1124
1113config TOUCHSCREEN_COLIBRI_VF50 1125config TOUCHSCREEN_COLIBRI_VF50
1114 tristate "Toradex Colibri on board touchscreen driver" 1126 tristate "Toradex Colibri on board touchscreen driver"
1115 depends on GPIOLIB && IIO && VF610_ADC 1127 depends on IIO && VF610_ADC
1128 depends on GPIOLIB || COMPILE_TEST
1116 help 1129 help
1117 Say Y here if you have a Colibri VF50 and plan to use 1130 Say Y here if you have a Colibri VF50 and plan to use
1118 the on-board provided 4-wire touchscreen driver. 1131 the on-board provided 4-wire touchscreen driver.
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index 968ff12e3132..4b518c76e0d7 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -48,6 +48,7 @@ obj-$(CONFIG_TOUCHSCREEN_LPC32XX) += lpc32xx_ts.o
48obj-$(CONFIG_TOUCHSCREEN_MAX11801) += max11801_ts.o 48obj-$(CONFIG_TOUCHSCREEN_MAX11801) += max11801_ts.o
49obj-$(CONFIG_TOUCHSCREEN_MC13783) += mc13783_ts.o 49obj-$(CONFIG_TOUCHSCREEN_MC13783) += mc13783_ts.o
50obj-$(CONFIG_TOUCHSCREEN_MCS5000) += mcs5000_ts.o 50obj-$(CONFIG_TOUCHSCREEN_MCS5000) += mcs5000_ts.o
51obj-$(CONFIG_TOUCHSCREEN_MELFAS_MIP4) += melfas_mip4.o
51obj-$(CONFIG_TOUCHSCREEN_MIGOR) += migor_ts.o 52obj-$(CONFIG_TOUCHSCREEN_MIGOR) += migor_ts.o
52obj-$(CONFIG_TOUCHSCREEN_MMS114) += mms114.o 53obj-$(CONFIG_TOUCHSCREEN_MMS114) += mms114.o
53obj-$(CONFIG_TOUCHSCREEN_MTOUCH) += mtouch.o 54obj-$(CONFIG_TOUCHSCREEN_MTOUCH) += mtouch.o
diff --git a/drivers/input/touchscreen/ad7879-i2c.c b/drivers/input/touchscreen/ad7879-i2c.c
index d66962c5b1c2..58f72e0246ab 100644
--- a/drivers/input/touchscreen/ad7879-i2c.c
+++ b/drivers/input/touchscreen/ad7879-i2c.c
@@ -10,6 +10,7 @@
10#include <linux/i2c.h> 10#include <linux/i2c.h>
11#include <linux/module.h> 11#include <linux/module.h>
12#include <linux/types.h> 12#include <linux/types.h>
13#include <linux/of.h>
13#include <linux/pm.h> 14#include <linux/pm.h>
14 15
15#include "ad7879.h" 16#include "ad7879.h"
@@ -91,10 +92,19 @@ static const struct i2c_device_id ad7879_id[] = {
91}; 92};
92MODULE_DEVICE_TABLE(i2c, ad7879_id); 93MODULE_DEVICE_TABLE(i2c, ad7879_id);
93 94
95#ifdef CONFIG_OF
96static const struct of_device_id ad7879_i2c_dt_ids[] = {
97 { .compatible = "adi,ad7879-1", },
98 { }
99};
100MODULE_DEVICE_TABLE(of, ad7879_i2c_dt_ids);
101#endif
102
94static struct i2c_driver ad7879_i2c_driver = { 103static struct i2c_driver ad7879_i2c_driver = {
95 .driver = { 104 .driver = {
96 .name = "ad7879", 105 .name = "ad7879",
97 .pm = &ad7879_pm_ops, 106 .pm = &ad7879_pm_ops,
107 .of_match_table = of_match_ptr(ad7879_i2c_dt_ids),
98 }, 108 },
99 .probe = ad7879_i2c_probe, 109 .probe = ad7879_i2c_probe,
100 .remove = ad7879_i2c_remove, 110 .remove = ad7879_i2c_remove,
diff --git a/drivers/input/touchscreen/ad7879-spi.c b/drivers/input/touchscreen/ad7879-spi.c
index 48033c2689ab..d42b6b9af191 100644
--- a/drivers/input/touchscreen/ad7879-spi.c
+++ b/drivers/input/touchscreen/ad7879-spi.c
@@ -10,6 +10,7 @@
10#include <linux/pm.h> 10#include <linux/pm.h>
11#include <linux/spi/spi.h> 11#include <linux/spi/spi.h>
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/of.h>
13 14
14#include "ad7879.h" 15#include "ad7879.h"
15 16
@@ -146,10 +147,19 @@ static int ad7879_spi_remove(struct spi_device *spi)
146 return 0; 147 return 0;
147} 148}
148 149
150#ifdef CONFIG_OF
151static const struct of_device_id ad7879_spi_dt_ids[] = {
152 { .compatible = "adi,ad7879", },
153 { }
154};
155MODULE_DEVICE_TABLE(of, ad7879_spi_dt_ids);
156#endif
157
149static struct spi_driver ad7879_spi_driver = { 158static struct spi_driver ad7879_spi_driver = {
150 .driver = { 159 .driver = {
151 .name = "ad7879", 160 .name = "ad7879",
152 .pm = &ad7879_pm_ops, 161 .pm = &ad7879_pm_ops,
162 .of_match_table = of_match_ptr(ad7879_spi_dt_ids),
153 }, 163 },
154 .probe = ad7879_spi_probe, 164 .probe = ad7879_spi_probe,
155 .remove = ad7879_spi_remove, 165 .remove = ad7879_spi_remove,
diff --git a/drivers/input/touchscreen/ad7879.c b/drivers/input/touchscreen/ad7879.c
index 16b5cc2196f2..69d299d5dd00 100644
--- a/drivers/input/touchscreen/ad7879.c
+++ b/drivers/input/touchscreen/ad7879.c
@@ -31,7 +31,8 @@
31#include <linux/i2c.h> 31#include <linux/i2c.h>
32#include <linux/gpio.h> 32#include <linux/gpio.h>
33 33
34#include <linux/spi/ad7879.h> 34#include <linux/input/touchscreen.h>
35#include <linux/platform_data/ad7879.h>
35#include <linux/module.h> 36#include <linux/module.h>
36#include "ad7879.h" 37#include "ad7879.h"
37 38
@@ -94,8 +95,8 @@
94#define AD7879_TEMP_BIT (1<<1) 95#define AD7879_TEMP_BIT (1<<1)
95 96
96enum { 97enum {
97 AD7879_SEQ_XPOS = 0, 98 AD7879_SEQ_YPOS = 0,
98 AD7879_SEQ_YPOS = 1, 99 AD7879_SEQ_XPOS = 1,
99 AD7879_SEQ_Z1 = 2, 100 AD7879_SEQ_Z1 = 2,
100 AD7879_SEQ_Z2 = 3, 101 AD7879_SEQ_Z2 = 3,
101 AD7879_NR_SENSE = 4, 102 AD7879_NR_SENSE = 4,
@@ -126,7 +127,6 @@ struct ad7879 {
126 u8 pen_down_acc_interval; 127 u8 pen_down_acc_interval;
127 u8 median; 128 u8 median;
128 u16 x_plate_ohms; 129 u16 x_plate_ohms;
129 u16 pressure_max;
130 u16 cmd_crtl1; 130 u16 cmd_crtl1;
131 u16 cmd_crtl2; 131 u16 cmd_crtl2;
132 u16 cmd_crtl3; 132 u16 cmd_crtl3;
@@ -170,10 +170,10 @@ static int ad7879_report(struct ad7879 *ts)
170 * filter. The combination of these two techniques provides a robust 170 * filter. The combination of these two techniques provides a robust
171 * solution, discarding the spurious noise in the signal and keeping 171 * solution, discarding the spurious noise in the signal and keeping
172 * only the data of interest. The size of both filters is 172 * only the data of interest. The size of both filters is
173 * programmable. (dev.platform_data, see linux/spi/ad7879.h) Other 173 * programmable. (dev.platform_data, see linux/platform_data/ad7879.h)
174 * user-programmable conversion controls include variable acquisition 174 * Other user-programmable conversion controls include variable
175 * time, and first conversion delay. Up to 16 averages can be taken 175 * acquisition time, and first conversion delay. Up to 16 averages can
176 * per conversion. 176 * be taken per conversion.
177 */ 177 */
178 178
179 if (likely(x && z1)) { 179 if (likely(x && z1)) {
@@ -186,7 +186,7 @@ static int ad7879_report(struct ad7879 *ts)
186 * Sample found inconsistent, pressure is beyond 186 * Sample found inconsistent, pressure is beyond
187 * the maximum. Don't report it to user space. 187 * the maximum. Don't report it to user space.
188 */ 188 */
189 if (Rt > ts->pressure_max) 189 if (Rt > input_abs_get_max(input_dev, ABS_PRESSURE))
190 return -EINVAL; 190 return -EINVAL;
191 191
192 /* 192 /*
@@ -469,7 +469,7 @@ static void ad7879_gpio_remove(struct ad7879 *ts)
469{ 469{
470 const struct ad7879_platform_data *pdata = dev_get_platdata(ts->dev); 470 const struct ad7879_platform_data *pdata = dev_get_platdata(ts->dev);
471 471
472 if (pdata->gpio_export) 472 if (pdata && pdata->gpio_export)
473 gpiochip_remove(&ts->gc); 473 gpiochip_remove(&ts->gc);
474 474
475} 475}
@@ -485,6 +485,32 @@ static inline void ad7879_gpio_remove(struct ad7879 *ts)
485} 485}
486#endif 486#endif
487 487
488static int ad7879_parse_dt(struct device *dev, struct ad7879 *ts)
489{
490 int err;
491 u32 tmp;
492
493 err = device_property_read_u32(dev, "adi,resistance-plate-x", &tmp);
494 if (err) {
495 dev_err(dev, "failed to get resistance-plate-x property\n");
496 return err;
497 }
498 ts->x_plate_ohms = (u16)tmp;
499
500 device_property_read_u8(dev, "adi,first-conversion-delay",
501 &ts->first_conversion_delay);
502 device_property_read_u8(dev, "adi,acquisition-time",
503 &ts->acquisition_time);
504 device_property_read_u8(dev, "adi,median-filter-size", &ts->median);
505 device_property_read_u8(dev, "adi,averaging", &ts->averaging);
506 device_property_read_u8(dev, "adi,conversion-interval",
507 &ts->pen_down_acc_interval);
508
509 ts->swap_xy = device_property_read_bool(dev, "touchscreen-swapped-x-y");
510
511 return 0;
512}
513
488struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq, 514struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
489 const struct ad7879_bus_ops *bops) 515 const struct ad7879_bus_ops *bops)
490{ 516{
@@ -495,41 +521,44 @@ struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
495 u16 revid; 521 u16 revid;
496 522
497 if (!irq) { 523 if (!irq) {
498 dev_err(dev, "no IRQ?\n"); 524 dev_err(dev, "No IRQ specified\n");
499 err = -EINVAL; 525 return ERR_PTR(-EINVAL);
500 goto err_out;
501 } 526 }
502 527
503 if (!pdata) { 528 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
504 dev_err(dev, "no platform data?\n"); 529 if (!ts)
505 err = -EINVAL; 530 return ERR_PTR(-ENOMEM);
506 goto err_out; 531
532 if (pdata) {
533 /* Platform data use swapped axis (backward compatibility) */
534 ts->swap_xy = !pdata->swap_xy;
535
536 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
537
538 ts->first_conversion_delay = pdata->first_conversion_delay;
539 ts->acquisition_time = pdata->acquisition_time;
540 ts->averaging = pdata->averaging;
541 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
542 ts->median = pdata->median;
543 } else if (dev->of_node) {
544 ad7879_parse_dt(dev, ts);
545 } else {
546 dev_err(dev, "No platform data\n");
547 return ERR_PTR(-EINVAL);
507 } 548 }
508 549
509 ts = kzalloc(sizeof(*ts), GFP_KERNEL); 550 input_dev = devm_input_allocate_device(dev);
510 input_dev = input_allocate_device(); 551 if (!input_dev) {
511 if (!ts || !input_dev) { 552 dev_err(dev, "Failed to allocate input device\n");
512 err = -ENOMEM; 553 return ERR_PTR(-ENOMEM);
513 goto err_free_mem;
514 } 554 }
515 555
516 ts->bops = bops; 556 ts->bops = bops;
517 ts->dev = dev; 557 ts->dev = dev;
518 ts->input = input_dev; 558 ts->input = input_dev;
519 ts->irq = irq; 559 ts->irq = irq;
520 ts->swap_xy = pdata->swap_xy;
521 560
522 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts); 561 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
523
524 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
525 ts->pressure_max = pdata->pressure_max ? : ~0;
526
527 ts->first_conversion_delay = pdata->first_conversion_delay;
528 ts->acquisition_time = pdata->acquisition_time;
529 ts->averaging = pdata->averaging;
530 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
531 ts->median = pdata->median;
532
533 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 562 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
534 563
535 input_dev->name = "AD7879 Touchscreen"; 564 input_dev->name = "AD7879 Touchscreen";
@@ -550,21 +579,33 @@ struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
550 __set_bit(EV_KEY, input_dev->evbit); 579 __set_bit(EV_KEY, input_dev->evbit);
551 __set_bit(BTN_TOUCH, input_dev->keybit); 580 __set_bit(BTN_TOUCH, input_dev->keybit);
552 581
553 input_set_abs_params(input_dev, ABS_X, 582 if (pdata) {
554 pdata->x_min ? : 0, 583 input_set_abs_params(input_dev, ABS_X,
555 pdata->x_max ? : MAX_12BIT, 584 pdata->x_min ? : 0,
556 0, 0); 585 pdata->x_max ? : MAX_12BIT,
557 input_set_abs_params(input_dev, ABS_Y, 586 0, 0);
558 pdata->y_min ? : 0, 587 input_set_abs_params(input_dev, ABS_Y,
559 pdata->y_max ? : MAX_12BIT, 588 pdata->y_min ? : 0,
560 0, 0); 589 pdata->y_max ? : MAX_12BIT,
561 input_set_abs_params(input_dev, ABS_PRESSURE, 590 0, 0);
562 pdata->pressure_min, pdata->pressure_max, 0, 0); 591 input_set_abs_params(input_dev, ABS_PRESSURE,
592 pdata->pressure_min,
593 pdata->pressure_max ? : ~0,
594 0, 0);
595 } else {
596 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
597 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
598 touchscreen_parse_properties(input_dev, false);
599 if (!input_abs_get_max(input_dev, ABS_PRESSURE)) {
600 dev_err(dev, "Touchscreen pressure is not specified\n");
601 return ERR_PTR(-EINVAL);
602 }
603 }
563 604
564 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET); 605 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET);
565 if (err < 0) { 606 if (err < 0) {
566 dev_err(dev, "Failed to write %s\n", input_dev->name); 607 dev_err(dev, "Failed to write %s\n", input_dev->name);
567 goto err_free_mem; 608 return ERR_PTR(err);
568 } 609 }
569 610
570 revid = ad7879_read(ts, AD7879_REG_REVID); 611 revid = ad7879_read(ts, AD7879_REG_REVID);
@@ -573,8 +614,7 @@ struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
573 if (input_dev->id.product != devid) { 614 if (input_dev->id.product != devid) {
574 dev_err(dev, "Failed to probe %s (%x vs %x)\n", 615 dev_err(dev, "Failed to probe %s (%x vs %x)\n",
575 input_dev->name, devid, revid); 616 input_dev->name, devid, revid);
576 err = -ENODEV; 617 return ERR_PTR(-ENODEV);
577 goto err_free_mem;
578 } 618 }
579 619
580 ts->cmd_crtl3 = AD7879_YPLUS_BIT | 620 ts->cmd_crtl3 = AD7879_YPLUS_BIT |
@@ -594,23 +634,25 @@ struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
594 AD7879_ACQ(ts->acquisition_time) | 634 AD7879_ACQ(ts->acquisition_time) |
595 AD7879_TMR(ts->pen_down_acc_interval); 635 AD7879_TMR(ts->pen_down_acc_interval);
596 636
597 err = request_threaded_irq(ts->irq, NULL, ad7879_irq, 637 err = devm_request_threaded_irq(dev, ts->irq, NULL, ad7879_irq,
598 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 638 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
599 dev_name(dev), ts); 639 dev_name(dev), ts);
600 if (err) { 640 if (err) {
601 dev_err(dev, "irq %d busy?\n", ts->irq); 641 dev_err(dev, "Failed to request IRQ: %d\n", err);
602 goto err_free_mem; 642 return ERR_PTR(err);
603 } 643 }
604 644
605 __ad7879_disable(ts); 645 __ad7879_disable(ts);
606 646
607 err = sysfs_create_group(&dev->kobj, &ad7879_attr_group); 647 err = sysfs_create_group(&dev->kobj, &ad7879_attr_group);
608 if (err) 648 if (err)
609 goto err_free_irq; 649 goto err_out;
610 650
611 err = ad7879_gpio_add(ts, pdata); 651 if (pdata) {
612 if (err) 652 err = ad7879_gpio_add(ts, pdata);
613 goto err_remove_attr; 653 if (err)
654 goto err_remove_attr;
655 }
614 656
615 err = input_register_device(input_dev); 657 err = input_register_device(input_dev);
616 if (err) 658 if (err)
@@ -622,11 +664,6 @@ err_remove_gpio:
622 ad7879_gpio_remove(ts); 664 ad7879_gpio_remove(ts);
623err_remove_attr: 665err_remove_attr:
624 sysfs_remove_group(&dev->kobj, &ad7879_attr_group); 666 sysfs_remove_group(&dev->kobj, &ad7879_attr_group);
625err_free_irq:
626 free_irq(ts->irq, ts);
627err_free_mem:
628 input_free_device(input_dev);
629 kfree(ts);
630err_out: 667err_out:
631 return ERR_PTR(err); 668 return ERR_PTR(err);
632} 669}
@@ -636,9 +673,6 @@ void ad7879_remove(struct ad7879 *ts)
636{ 673{
637 ad7879_gpio_remove(ts); 674 ad7879_gpio_remove(ts);
638 sysfs_remove_group(&ts->dev->kobj, &ad7879_attr_group); 675 sysfs_remove_group(&ts->dev->kobj, &ad7879_attr_group);
639 free_irq(ts->irq, ts);
640 input_unregister_device(ts->input);
641 kfree(ts);
642} 676}
643EXPORT_SYMBOL(ad7879_remove); 677EXPORT_SYMBOL(ad7879_remove);
644 678
diff --git a/drivers/input/touchscreen/cyttsp_core.c b/drivers/input/touchscreen/cyttsp_core.c
index 5b74e8b84e79..91cda8f8119d 100644
--- a/drivers/input/touchscreen/cyttsp_core.c
+++ b/drivers/input/touchscreen/cyttsp_core.c
@@ -30,9 +30,12 @@
30#include <linux/delay.h> 30#include <linux/delay.h>
31#include <linux/input.h> 31#include <linux/input.h>
32#include <linux/input/mt.h> 32#include <linux/input/mt.h>
33#include <linux/input/touchscreen.h>
33#include <linux/gpio.h> 34#include <linux/gpio.h>
34#include <linux/interrupt.h> 35#include <linux/interrupt.h>
35#include <linux/slab.h> 36#include <linux/slab.h>
37#include <linux/property.h>
38#include <linux/gpio/consumer.h>
36 39
37#include "cyttsp_core.h" 40#include "cyttsp_core.h"
38 41
@@ -57,6 +60,7 @@
57#define CY_DELAY_DFLT 20 /* ms */ 60#define CY_DELAY_DFLT 20 /* ms */
58#define CY_DELAY_MAX 500 61#define CY_DELAY_MAX 500
59#define CY_ACT_DIST_DFLT 0xF8 62#define CY_ACT_DIST_DFLT 0xF8
63#define CY_ACT_DIST_MASK 0x0F
60#define CY_HNDSHK_BIT 0x80 64#define CY_HNDSHK_BIT 0x80
61/* device mode bits */ 65/* device mode bits */
62#define CY_OPERATE_MODE 0x00 66#define CY_OPERATE_MODE 0x00
@@ -120,7 +124,7 @@ static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
120 124
121static int cyttsp_handshake(struct cyttsp *ts) 125static int cyttsp_handshake(struct cyttsp *ts)
122{ 126{
123 if (ts->pdata->use_hndshk) 127 if (ts->use_hndshk)
124 return ttsp_send_command(ts, 128 return ttsp_send_command(ts,
125 ts->xy_data.hst_mode ^ CY_HNDSHK_BIT); 129 ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
126 130
@@ -142,9 +146,9 @@ static int cyttsp_exit_bl_mode(struct cyttsp *ts)
142 u8 bl_cmd[sizeof(bl_command)]; 146 u8 bl_cmd[sizeof(bl_command)];
143 147
144 memcpy(bl_cmd, bl_command, sizeof(bl_command)); 148 memcpy(bl_cmd, bl_command, sizeof(bl_command));
145 if (ts->pdata->bl_keys) 149 if (ts->bl_keys)
146 memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS], 150 memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
147 ts->pdata->bl_keys, CY_NUM_BL_KEYS); 151 ts->bl_keys, CY_NUM_BL_KEYS);
148 152
149 error = ttsp_write_block_data(ts, CY_REG_BASE, 153 error = ttsp_write_block_data(ts, CY_REG_BASE,
150 sizeof(bl_cmd), bl_cmd); 154 sizeof(bl_cmd), bl_cmd);
@@ -217,14 +221,14 @@ static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
217{ 221{
218 int retval = 0; 222 int retval = 0;
219 223
220 if (ts->pdata->act_intrvl != CY_ACT_INTRVL_DFLT || 224 if (ts->act_intrvl != CY_ACT_INTRVL_DFLT ||
221 ts->pdata->tch_tmout != CY_TCH_TMOUT_DFLT || 225 ts->tch_tmout != CY_TCH_TMOUT_DFLT ||
222 ts->pdata->lp_intrvl != CY_LP_INTRVL_DFLT) { 226 ts->lp_intrvl != CY_LP_INTRVL_DFLT) {
223 227
224 u8 intrvl_ray[] = { 228 u8 intrvl_ray[] = {
225 ts->pdata->act_intrvl, 229 ts->act_intrvl,
226 ts->pdata->tch_tmout, 230 ts->tch_tmout,
227 ts->pdata->lp_intrvl 231 ts->lp_intrvl
228 }; 232 };
229 233
230 /* set intrvl registers */ 234 /* set intrvl registers */
@@ -236,6 +240,16 @@ static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
236 return retval; 240 return retval;
237} 241}
238 242
243static void cyttsp_hard_reset(struct cyttsp *ts)
244{
245 if (ts->reset_gpio) {
246 gpiod_set_value_cansleep(ts->reset_gpio, 1);
247 msleep(CY_DELAY_DFLT);
248 gpiod_set_value_cansleep(ts->reset_gpio, 0);
249 msleep(CY_DELAY_DFLT);
250 }
251}
252
239static int cyttsp_soft_reset(struct cyttsp *ts) 253static int cyttsp_soft_reset(struct cyttsp *ts)
240{ 254{
241 unsigned long timeout; 255 unsigned long timeout;
@@ -263,7 +277,7 @@ out:
263 277
264static int cyttsp_act_dist_setup(struct cyttsp *ts) 278static int cyttsp_act_dist_setup(struct cyttsp *ts)
265{ 279{
266 u8 act_dist_setup = ts->pdata->act_dist; 280 u8 act_dist_setup = ts->act_dist;
267 281
268 /* Init gesture; active distance setup */ 282 /* Init gesture; active distance setup */
269 return ttsp_write_block_data(ts, CY_REG_ACT_DIST, 283 return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
@@ -528,45 +542,110 @@ static void cyttsp_close(struct input_dev *dev)
528 cyttsp_disable(ts); 542 cyttsp_disable(ts);
529} 543}
530 544
545static int cyttsp_parse_properties(struct cyttsp *ts)
546{
547 struct device *dev = ts->dev;
548 u32 dt_value;
549 int ret;
550
551 ts->bl_keys = devm_kzalloc(dev, CY_NUM_BL_KEYS, GFP_KERNEL);
552 if (!ts->bl_keys)
553 return -ENOMEM;
554
555 /* Set some default values */
556 ts->use_hndshk = false;
557 ts->act_dist = CY_ACT_DIST_DFLT;
558 ts->act_intrvl = CY_ACT_INTRVL_DFLT;
559 ts->tch_tmout = CY_TCH_TMOUT_DFLT;
560 ts->lp_intrvl = CY_LP_INTRVL_DFLT;
561
562 ret = device_property_read_u8_array(dev, "bootloader-key",
563 ts->bl_keys, CY_NUM_BL_KEYS);
564 if (ret) {
565 dev_err(dev,
566 "bootloader-key property could not be retrieved\n");
567 return ret;
568 }
569
570 ts->use_hndshk = device_property_present(dev, "use-handshake");
571
572 if (!device_property_read_u32(dev, "active-distance", &dt_value)) {
573 if (dt_value > 15) {
574 dev_err(dev, "active-distance (%u) must be [0-15]\n",
575 dt_value);
576 return -EINVAL;
577 }
578 ts->act_dist &= ~CY_ACT_DIST_MASK;
579 ts->act_dist |= dt_value;
580 }
581
582 if (!device_property_read_u32(dev, "active-interval-ms", &dt_value)) {
583 if (dt_value > 255) {
584 dev_err(dev, "active-interval-ms (%u) must be [0-255]\n",
585 dt_value);
586 return -EINVAL;
587 }
588 ts->act_intrvl = dt_value;
589 }
590
591 if (!device_property_read_u32(dev, "lowpower-interval-ms", &dt_value)) {
592 if (dt_value > 2550) {
593 dev_err(dev, "lowpower-interval-ms (%u) must be [0-2550]\n",
594 dt_value);
595 return -EINVAL;
596 }
597 /* Register value is expressed in 0.01s / bit */
598 ts->lp_intrvl = dt_value / 10;
599 }
600
601 if (!device_property_read_u32(dev, "touch-timeout-ms", &dt_value)) {
602 if (dt_value > 2550) {
603 dev_err(dev, "touch-timeout-ms (%u) must be [0-2550]\n",
604 dt_value);
605 return -EINVAL;
606 }
607 /* Register value is expressed in 0.01s / bit */
608 ts->tch_tmout = dt_value / 10;
609 }
610
611 return 0;
612}
613
531struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops, 614struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
532 struct device *dev, int irq, size_t xfer_buf_size) 615 struct device *dev, int irq, size_t xfer_buf_size)
533{ 616{
534 const struct cyttsp_platform_data *pdata = dev_get_platdata(dev);
535 struct cyttsp *ts; 617 struct cyttsp *ts;
536 struct input_dev *input_dev; 618 struct input_dev *input_dev;
537 int error; 619 int error;
538 620
539 if (!pdata || !pdata->name || irq <= 0) { 621 ts = devm_kzalloc(dev, sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
540 error = -EINVAL; 622 if (!ts)
541 goto err_out; 623 return ERR_PTR(-ENOMEM);
542 }
543 624
544 ts = kzalloc(sizeof(*ts) + xfer_buf_size, GFP_KERNEL); 625 input_dev = devm_input_allocate_device(dev);
545 input_dev = input_allocate_device(); 626 if (!input_dev)
546 if (!ts || !input_dev) { 627 return ERR_PTR(-ENOMEM);
547 error = -ENOMEM;
548 goto err_free_mem;
549 }
550 628
551 ts->dev = dev; 629 ts->dev = dev;
552 ts->input = input_dev; 630 ts->input = input_dev;
553 ts->pdata = dev_get_platdata(dev);
554 ts->bus_ops = bus_ops; 631 ts->bus_ops = bus_ops;
555 ts->irq = irq; 632 ts->irq = irq;
556 633
634 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
635 if (IS_ERR(ts->reset_gpio)) {
636 error = PTR_ERR(ts->reset_gpio);
637 dev_err(dev, "Failed to request reset gpio, error %d\n", error);
638 return ERR_PTR(error);
639 }
640
641 error = cyttsp_parse_properties(ts);
642 if (error)
643 return ERR_PTR(error);
644
557 init_completion(&ts->bl_ready); 645 init_completion(&ts->bl_ready);
558 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 646 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
559 647
560 if (pdata->init) { 648 input_dev->name = "Cypress TTSP TouchScreen";
561 error = pdata->init();
562 if (error) {
563 dev_err(ts->dev, "platform init failed, err: %d\n",
564 error);
565 goto err_free_mem;
566 }
567 }
568
569 input_dev->name = pdata->name;
570 input_dev->phys = ts->phys; 649 input_dev->phys = ts->phys;
571 input_dev->id.bustype = bus_ops->bustype; 650 input_dev->id.bustype = bus_ops->bustype;
572 input_dev->dev.parent = ts->dev; 651 input_dev->dev.parent = ts->dev;
@@ -576,63 +655,44 @@ struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
576 655
577 input_set_drvdata(input_dev, ts); 656 input_set_drvdata(input_dev, ts);
578 657
579 __set_bit(EV_ABS, input_dev->evbit); 658 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
580 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 659 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
581 0, pdata->maxx, 0, 0); 660 touchscreen_parse_properties(input_dev, true);
582 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
583 0, pdata->maxy, 0, 0);
584 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
585 0, CY_MAXZ, 0, 0);
586 661
587 input_mt_init_slots(input_dev, CY_MAX_ID, 0); 662 error = input_mt_init_slots(input_dev, CY_MAX_ID, 0);
663 if (error) {
664 dev_err(dev, "Unable to init MT slots.\n");
665 return ERR_PTR(error);
666 }
588 667
589 error = request_threaded_irq(ts->irq, NULL, cyttsp_irq, 668 error = devm_request_threaded_irq(dev, ts->irq, NULL, cyttsp_irq,
590 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 669 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
591 pdata->name, ts); 670 "cyttsp", ts);
592 if (error) { 671 if (error) {
593 dev_err(ts->dev, "failed to request IRQ %d, err: %d\n", 672 dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
594 ts->irq, error); 673 ts->irq, error);
595 goto err_platform_exit; 674 return ERR_PTR(error);
596 } 675 }
597 676
598 disable_irq(ts->irq); 677 disable_irq(ts->irq);
599 678
679 cyttsp_hard_reset(ts);
680
600 error = cyttsp_power_on(ts); 681 error = cyttsp_power_on(ts);
601 if (error) 682 if (error)
602 goto err_free_irq; 683 return ERR_PTR(error);
603 684
604 error = input_register_device(input_dev); 685 error = input_register_device(input_dev);
605 if (error) { 686 if (error) {
606 dev_err(ts->dev, "failed to register input device: %d\n", 687 dev_err(ts->dev, "failed to register input device: %d\n",
607 error); 688 error);
608 goto err_free_irq; 689 return ERR_PTR(error);
609 } 690 }
610 691
611 return ts; 692 return ts;
612
613err_free_irq:
614 free_irq(ts->irq, ts);
615err_platform_exit:
616 if (pdata->exit)
617 pdata->exit();
618err_free_mem:
619 input_free_device(input_dev);
620 kfree(ts);
621err_out:
622 return ERR_PTR(error);
623} 693}
624EXPORT_SYMBOL_GPL(cyttsp_probe); 694EXPORT_SYMBOL_GPL(cyttsp_probe);
625 695
626void cyttsp_remove(struct cyttsp *ts)
627{
628 free_irq(ts->irq, ts);
629 input_unregister_device(ts->input);
630 if (ts->pdata->exit)
631 ts->pdata->exit();
632 kfree(ts);
633}
634EXPORT_SYMBOL_GPL(cyttsp_remove);
635
636MODULE_LICENSE("GPL"); 696MODULE_LICENSE("GPL");
637MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core"); 697MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
638MODULE_AUTHOR("Cypress"); 698MODULE_AUTHOR("Cypress");
diff --git a/drivers/input/touchscreen/cyttsp_core.h b/drivers/input/touchscreen/cyttsp_core.h
index 07074110a902..7835e2bacf5a 100644
--- a/drivers/input/touchscreen/cyttsp_core.h
+++ b/drivers/input/touchscreen/cyttsp_core.h
@@ -129,7 +129,6 @@ struct cyttsp {
129 int irq; 129 int irq;
130 struct input_dev *input; 130 struct input_dev *input;
131 char phys[32]; 131 char phys[32];
132 const struct cyttsp_platform_data *pdata;
133 const struct cyttsp_bus_ops *bus_ops; 132 const struct cyttsp_bus_ops *bus_ops;
134 struct cyttsp_bootloader_data bl_data; 133 struct cyttsp_bootloader_data bl_data;
135 struct cyttsp_sysinfo_data sysinfo_data; 134 struct cyttsp_sysinfo_data sysinfo_data;
@@ -138,12 +137,19 @@ struct cyttsp {
138 enum cyttsp_state state; 137 enum cyttsp_state state;
139 bool suspended; 138 bool suspended;
140 139
140 struct gpio_desc *reset_gpio;
141 bool use_hndshk;
142 u8 act_dist;
143 u8 act_intrvl;
144 u8 tch_tmout;
145 u8 lp_intrvl;
146 u8 *bl_keys;
147
141 u8 xfer_buf[] ____cacheline_aligned; 148 u8 xfer_buf[] ____cacheline_aligned;
142}; 149};
143 150
144struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops, 151struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
145 struct device *dev, int irq, size_t xfer_buf_size); 152 struct device *dev, int irq, size_t xfer_buf_size);
146void cyttsp_remove(struct cyttsp *ts);
147 153
148int cyttsp_i2c_write_block_data(struct device *dev, u8 *xfer_buf, u16 addr, 154int cyttsp_i2c_write_block_data(struct device *dev, u8 *xfer_buf, u16 addr,
149 u8 length, const void *values); 155 u8 length, const void *values);
diff --git a/drivers/input/touchscreen/cyttsp_i2c.c b/drivers/input/touchscreen/cyttsp_i2c.c
index eee51b3f2e3f..1edfdba96ede 100644
--- a/drivers/input/touchscreen/cyttsp_i2c.c
+++ b/drivers/input/touchscreen/cyttsp_i2c.c
@@ -56,15 +56,6 @@ static int cyttsp_i2c_probe(struct i2c_client *client,
56 return 0; 56 return 0;
57} 57}
58 58
59static int cyttsp_i2c_remove(struct i2c_client *client)
60{
61 struct cyttsp *ts = i2c_get_clientdata(client);
62
63 cyttsp_remove(ts);
64
65 return 0;
66}
67
68static const struct i2c_device_id cyttsp_i2c_id[] = { 59static const struct i2c_device_id cyttsp_i2c_id[] = {
69 { CY_I2C_NAME, 0 }, 60 { CY_I2C_NAME, 0 },
70 { } 61 { }
@@ -77,7 +68,6 @@ static struct i2c_driver cyttsp_i2c_driver = {
77 .pm = &cyttsp_pm_ops, 68 .pm = &cyttsp_pm_ops,
78 }, 69 },
79 .probe = cyttsp_i2c_probe, 70 .probe = cyttsp_i2c_probe,
80 .remove = cyttsp_i2c_remove,
81 .id_table = cyttsp_i2c_id, 71 .id_table = cyttsp_i2c_id,
82}; 72};
83 73
diff --git a/drivers/input/touchscreen/cyttsp_spi.c b/drivers/input/touchscreen/cyttsp_spi.c
index bbeeb2488b57..3c9d18b1b6ef 100644
--- a/drivers/input/touchscreen/cyttsp_spi.c
+++ b/drivers/input/touchscreen/cyttsp_spi.c
@@ -170,22 +170,12 @@ static int cyttsp_spi_probe(struct spi_device *spi)
170 return 0; 170 return 0;
171} 171}
172 172
173static int cyttsp_spi_remove(struct spi_device *spi)
174{
175 struct cyttsp *ts = spi_get_drvdata(spi);
176
177 cyttsp_remove(ts);
178
179 return 0;
180}
181
182static struct spi_driver cyttsp_spi_driver = { 173static struct spi_driver cyttsp_spi_driver = {
183 .driver = { 174 .driver = {
184 .name = CY_SPI_NAME, 175 .name = CY_SPI_NAME,
185 .pm = &cyttsp_pm_ops, 176 .pm = &cyttsp_pm_ops,
186 }, 177 },
187 .probe = cyttsp_spi_probe, 178 .probe = cyttsp_spi_probe,
188 .remove = cyttsp_spi_remove,
189}; 179};
190 180
191module_spi_driver(cyttsp_spi_driver); 181module_spi_driver(cyttsp_spi_driver);
diff --git a/drivers/input/touchscreen/melfas_mip4.c b/drivers/input/touchscreen/melfas_mip4.c
new file mode 100644
index 000000000000..892729734c51
--- /dev/null
+++ b/drivers/input/touchscreen/melfas_mip4.c
@@ -0,0 +1,1517 @@
1/*
2 * MELFAS MIP4 Touchscreen
3 *
4 * Copyright (C) 2016 MELFAS Inc.
5 *
6 * Author : Sangwon Jee <jeesw@melfas.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/acpi.h>
20#include <linux/delay.h>
21#include <linux/firmware.h>
22#include <linux/gpio/consumer.h>
23#include <linux/i2c.h>
24#include <linux/input.h>
25#include <linux/input/mt.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/slab.h>
30#include <asm/unaligned.h>
31
32#define MIP4_DEVICE_NAME "mip4_ts"
33
34/*****************************************************************
35 * Protocol
36 * Version : MIP 4.0 Rev 4.6
37 *****************************************************************/
38
39/* Address */
40#define MIP4_R0_BOOT 0x00
41#define MIP4_R1_BOOT_MODE 0x01
42#define MIP4_R1_BOOT_BUF_ADDR 0x10
43#define MIP4_R1_BOOT_STATUS 0x20
44#define MIP4_R1_BOOT_CMD 0x30
45#define MIP4_R1_BOOT_TARGET_ADDR 0x40
46#define MIP4_R1_BOOT_SIZE 0x44
47
48#define MIP4_R0_INFO 0x01
49#define MIP4_R1_INFO_PRODUCT_NAME 0x00
50#define MIP4_R1_INFO_RESOLUTION_X 0x10
51#define MIP4_R1_INFO_RESOLUTION_Y 0x12
52#define MIP4_R1_INFO_NODE_NUM_X 0x14
53#define MIP4_R1_INFO_NODE_NUM_Y 0x15
54#define MIP4_R1_INFO_KEY_NUM 0x16
55#define MIP4_R1_INFO_PRESSURE_NUM 0x17
56#define MIP4_R1_INFO_LENGTH_X 0x18
57#define MIP4_R1_INFO_LENGTH_Y 0x1A
58#define MIP4_R1_INFO_PPM_X 0x1C
59#define MIP4_R1_INFO_PPM_Y 0x1D
60#define MIP4_R1_INFO_VERSION_BOOT 0x20
61#define MIP4_R1_INFO_VERSION_CORE 0x22
62#define MIP4_R1_INFO_VERSION_APP 0x24
63#define MIP4_R1_INFO_VERSION_PARAM 0x26
64#define MIP4_R1_INFO_SECT_BOOT_START 0x30
65#define MIP4_R1_INFO_SECT_BOOT_END 0x31
66#define MIP4_R1_INFO_SECT_CORE_START 0x32
67#define MIP4_R1_INFO_SECT_CORE_END 0x33
68#define MIP4_R1_INFO_SECT_APP_START 0x34
69#define MIP4_R1_INFO_SECT_APP_END 0x35
70#define MIP4_R1_INFO_SECT_PARAM_START 0x36
71#define MIP4_R1_INFO_SECT_PARAM_END 0x37
72#define MIP4_R1_INFO_BUILD_DATE 0x40
73#define MIP4_R1_INFO_BUILD_TIME 0x44
74#define MIP4_R1_INFO_CHECKSUM_PRECALC 0x48
75#define MIP4_R1_INFO_CHECKSUM_REALTIME 0x4A
76#define MIP4_R1_INFO_PROTOCOL_NAME 0x50
77#define MIP4_R1_INFO_PROTOCOL_VERSION 0x58
78#define MIP4_R1_INFO_IC_ID 0x70
79#define MIP4_R1_INFO_IC_NAME 0x71
80#define MIP4_R1_INFO_IC_VENDOR_ID 0x75
81#define MIP4_R1_INFO_IC_HW_CATEGORY 0x77
82#define MIP4_R1_INFO_CONTACT_THD_SCR 0x78
83#define MIP4_R1_INFO_CONTACT_THD_KEY 0x7A
84
85#define MIP4_R0_EVENT 0x02
86#define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00
87#define MIP4_R1_EVENT_FORMAT 0x04
88#define MIP4_R1_EVENT_SIZE 0x06
89#define MIP4_R1_EVENT_PACKET_INFO 0x10
90#define MIP4_R1_EVENT_PACKET_DATA 0x11
91
92#define MIP4_R0_CTRL 0x06
93#define MIP4_R1_CTRL_READY_STATUS 0x00
94#define MIP4_R1_CTRL_EVENT_READY 0x01
95#define MIP4_R1_CTRL_MODE 0x10
96#define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11
97#define MIP4_R1_CTRL_RECALIBRATE 0x12
98#define MIP4_R1_CTRL_POWER_STATE 0x13
99#define MIP4_R1_CTRL_GESTURE_TYPE 0x14
100#define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18
101#define MIP4_R1_CTRL_CHARGER_MODE 0x19
102#define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A
103#define MIP4_R1_CTRL_WINDOW_MODE 0x1B
104#define MIP4_R1_CTRL_PALM_REJECTION 0x1C
105#define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D
106#define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E
107#define MIP4_R1_CTRL_I2C_ON_LPM 0x1F
108#define MIP4_R1_CTRL_GESTURE_DEBUG 0x20
109#define MIP4_R1_CTRL_PALM_EVENT 0x22
110#define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23
111
112/* Value */
113#define MIP4_BOOT_MODE_BOOT 0x01
114#define MIP4_BOOT_MODE_APP 0x02
115
116#define MIP4_BOOT_STATUS_BUSY 0x05
117#define MIP4_BOOT_STATUS_ERROR 0x0E
118#define MIP4_BOOT_STATUS_DONE 0xA0
119
120#define MIP4_BOOT_CMD_MASS_ERASE 0x15
121#define MIP4_BOOT_CMD_PROGRAM 0x54
122#define MIP4_BOOT_CMD_ERASE 0x8F
123#define MIP4_BOOT_CMD_WRITE 0xA5
124#define MIP4_BOOT_CMD_READ 0xC2
125
126#define MIP4_EVENT_INPUT_TYPE_KEY 0
127#define MIP4_EVENT_INPUT_TYPE_SCREEN 1
128#define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2
129
130#define I2C_RETRY_COUNT 3 /* 2~ */
131
132#define MIP4_BUF_SIZE 128
133#define MIP4_MAX_FINGERS 10
134#define MIP4_MAX_KEYS 4
135
136#define MIP4_TOUCH_MAJOR_MIN 0
137#define MIP4_TOUCH_MAJOR_MAX 255
138#define MIP4_TOUCH_MINOR_MIN 0
139#define MIP4_TOUCH_MINOR_MAX 255
140#define MIP4_PRESSURE_MIN 0
141#define MIP4_PRESSURE_MAX 255
142
143#define MIP4_FW_NAME "melfas_mip4.fw"
144#define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */
145
146struct mip4_fw_version {
147 u16 boot;
148 u16 core;
149 u16 app;
150 u16 param;
151};
152
153struct mip4_ts {
154 struct i2c_client *client;
155 struct input_dev *input;
156 struct gpio_desc *gpio_ce;
157
158 char phys[32];
159 char product_name[16];
160
161 unsigned int max_x;
162 unsigned int max_y;
163 u8 node_x;
164 u8 node_y;
165 u8 node_key;
166 unsigned int ppm_x;
167 unsigned int ppm_y;
168
169 struct mip4_fw_version fw_version;
170
171 unsigned int event_size;
172 unsigned int event_format;
173
174 unsigned int key_num;
175 unsigned short key_code[MIP4_MAX_KEYS];
176
177 bool wake_irq_enabled;
178
179 u8 buf[MIP4_BUF_SIZE];
180};
181
182static int mip4_i2c_xfer(struct mip4_ts *ts,
183 char *write_buf, unsigned int write_len,
184 char *read_buf, unsigned int read_len)
185{
186 struct i2c_msg msg[] = {
187 {
188 .addr = ts->client->addr,
189 .flags = 0,
190 .buf = write_buf,
191 .len = write_len,
192 }, {
193 .addr = ts->client->addr,
194 .flags = I2C_M_RD,
195 .buf = read_buf,
196 .len = read_len,
197 },
198 };
199 int retry = I2C_RETRY_COUNT;
200 int res;
201 int error;
202
203 do {
204 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
205 if (res == ARRAY_SIZE(msg))
206 return 0;
207
208 error = res < 0 ? res : -EIO;
209 dev_err(&ts->client->dev,
210 "%s - i2c_transfer failed: %d (%d)\n",
211 __func__, error, res);
212 } while (--retry);
213
214 return error;
215}
216
217static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
218{
219 v->boot = get_unaligned_le16(buf + 0);
220 v->core = get_unaligned_le16(buf + 2);
221 v->app = get_unaligned_le16(buf + 4);
222 v->param = get_unaligned_le16(buf + 6);
223}
224
225/*
226 * Read chip firmware version
227 */
228static int mip4_get_fw_version(struct mip4_ts *ts)
229{
230 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
231 u8 buf[sizeof(ts->fw_version)];
232 int error;
233
234 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
235 if (error) {
236 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
237 return error;
238 }
239
240 mip4_parse_fw_version(buf, &ts->fw_version);
241
242 return 0;
243}
244
245/*
246 * Fetch device characteristics
247 */
248static int mip4_query_device(struct mip4_ts *ts)
249{
250 int error;
251 u8 cmd[2];
252 u8 buf[14];
253
254 /* Product name */
255 cmd[0] = MIP4_R0_INFO;
256 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
257 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
258 ts->product_name, sizeof(ts->product_name));
259 if (error)
260 dev_warn(&ts->client->dev,
261 "Failed to retrieve product name: %d\n", error);
262 else
263 dev_dbg(&ts->client->dev, "product name: %.*s\n",
264 (int)sizeof(ts->product_name), ts->product_name);
265
266 /* Firmware version */
267 error = mip4_get_fw_version(ts);
268 if (error)
269 dev_warn(&ts->client->dev,
270 "Failed to retrieve FW version: %d\n", error);
271 else
272 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
273 ts->fw_version.boot, ts->fw_version.core,
274 ts->fw_version.app, ts->fw_version.param);
275
276 /* Resolution */
277 cmd[0] = MIP4_R0_INFO;
278 cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
279 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
280 if (error) {
281 dev_warn(&ts->client->dev,
282 "Failed to retrieve touchscreen parameters: %d\n",
283 error);
284 } else {
285 ts->max_x = get_unaligned_le16(&buf[0]);
286 ts->max_y = get_unaligned_le16(&buf[2]);
287 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
288 ts->max_x, ts->max_y);
289
290 ts->node_x = buf[4];
291 ts->node_y = buf[5];
292 ts->node_key = buf[6];
293 dev_dbg(&ts->client->dev,
294 "node_x: %d, node_y: %d, node_key: %d\n",
295 ts->node_x, ts->node_y, ts->node_key);
296
297 ts->ppm_x = buf[12];
298 ts->ppm_y = buf[13];
299 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
300 ts->ppm_x, ts->ppm_y);
301
302 /* Key ts */
303 if (ts->node_key > 0)
304 ts->key_num = ts->node_key;
305 }
306
307 /* Protocol */
308 cmd[0] = MIP4_R0_EVENT;
309 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
310 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
311 if (error) {
312 dev_warn(&ts->client->dev,
313 "Failed to retrieve device type: %d\n", error);
314 ts->event_format = 0xff;
315 } else {
316 ts->event_format = get_unaligned_le16(&buf[4]);
317 ts->event_size = buf[6];
318 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
319 ts->event_format, ts->event_size);
320
321 if (ts->event_format == 2 || ts->event_format > 3)
322 dev_warn(&ts->client->dev,
323 "Unknown event format %d\n", ts->event_format);
324 }
325
326 return 0;
327}
328
329static int mip4_power_on(struct mip4_ts *ts)
330{
331 if (ts->gpio_ce) {
332 gpiod_set_value_cansleep(ts->gpio_ce, 1);
333
334 /* Booting delay : 200~300ms */
335 usleep_range(200 * 1000, 300 * 1000);
336 }
337
338 return 0;
339}
340
341static void mip4_power_off(struct mip4_ts *ts)
342{
343 if (ts->gpio_ce)
344 gpiod_set_value_cansleep(ts->gpio_ce, 0);
345}
346
347/*
348 * Clear touch input event status
349 */
350static void mip4_clear_input(struct mip4_ts *ts)
351{
352 int i;
353
354 /* Screen */
355 for (i = 0; i < MIP4_MAX_FINGERS; i++) {
356 input_mt_slot(ts->input, i);
357 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
358 }
359
360 /* Keys */
361 for (i = 0; i < ts->key_num; i++)
362 input_report_key(ts->input, ts->key_code[i], 0);
363
364 input_sync(ts->input);
365}
366
367static int mip4_enable(struct mip4_ts *ts)
368{
369 int error;
370
371 error = mip4_power_on(ts);
372 if (error)
373 return error;
374
375 enable_irq(ts->client->irq);
376
377 return 0;
378}
379
380static void mip4_disable(struct mip4_ts *ts)
381{
382 disable_irq(ts->client->irq);
383
384 mip4_power_off(ts);
385
386 mip4_clear_input(ts);
387}
388
389/*****************************************************************
390 * Input handling
391 *****************************************************************/
392
393static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
394{
395 u8 key;
396 bool down;
397
398 switch (ts->event_format) {
399 case 0:
400 case 1:
401 key = packet[0] & 0x0F;
402 down = packet[0] & 0x80;
403 break;
404
405 case 3:
406 default:
407 key = packet[0] & 0x0F;
408 down = packet[1] & 0x01;
409 break;
410 }
411
412 /* Report key event */
413 if (key >= 1 && key <= ts->key_num) {
414 unsigned short keycode = ts->key_code[key - 1];
415
416 dev_dbg(&ts->client->dev,
417 "Key - ID: %d, keycode: %d, state: %d\n",
418 key, keycode, down);
419
420 input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
421 input_report_key(ts->input, keycode, down);
422
423 } else {
424 dev_err(&ts->client->dev, "Unknown key: %d\n", key);
425 }
426}
427
428static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
429{
430 int id;
431 bool hover;
432 bool palm;
433 bool state;
434 u16 x, y;
435 u8 pressure_stage = 0;
436 u8 pressure;
437 u8 size;
438 u8 touch_major;
439 u8 touch_minor;
440
441 switch (ts->event_format) {
442 case 0:
443 case 1:
444 /* Touch only */
445 state = packet[0] & BIT(7);
446 hover = packet[0] & BIT(5);
447 palm = packet[0] & BIT(4);
448 id = (packet[0] & 0x0F) - 1;
449 x = ((packet[1] & 0x0F) << 8) | packet[2];
450 y = (((packet[1] >> 4) & 0x0F) << 8) |
451 packet[3];
452 pressure = packet[4];
453 size = packet[5];
454 if (ts->event_format == 0) {
455 touch_major = packet[5];
456 touch_minor = packet[5];
457 } else {
458 touch_major = packet[6];
459 touch_minor = packet[7];
460 }
461 break;
462
463 case 3:
464 default:
465 /* Touch + Force(Pressure) */
466 id = (packet[0] & 0x0F) - 1;
467 hover = packet[1] & BIT(2);
468 palm = packet[1] & BIT(1);
469 state = packet[1] & BIT(0);
470 x = ((packet[2] & 0x0F) << 8) | packet[3];
471 y = (((packet[2] >> 4) & 0x0F) << 8) |
472 packet[4];
473 size = packet[6];
474 pressure_stage = (packet[7] & 0xF0) >> 4;
475 pressure = ((packet[7] & 0x0F) << 8) |
476 packet[8];
477 touch_major = packet[9];
478 touch_minor = packet[10];
479 break;
480 }
481
482 dev_dbg(&ts->client->dev,
483 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
484 id, state, x, y, pressure);
485
486 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
487 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
488 } else if (state) {
489 /* Press or Move event */
490 input_mt_slot(ts->input, id);
491 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
492 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
493 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
494 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
495 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
496 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
497 } else {
498 /* Release event */
499 input_mt_slot(ts->input, id);
500 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
501 }
502
503 input_mt_sync_frame(ts->input);
504}
505
506static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
507{
508 u8 type;
509
510 switch (ts->event_format) {
511 case 0:
512 case 1:
513 type = (packet[0] & 0x40) >> 6;
514 break;
515
516 case 3:
517 type = (packet[0] & 0xF0) >> 4;
518 break;
519
520 default:
521 /* Should not happen unless we have corrupted firmware */
522 return -EINVAL;
523 }
524
525 dev_dbg(&ts->client->dev, "Type: %d\n", type);
526
527 /* Report input event */
528 switch (type) {
529 case MIP4_EVENT_INPUT_TYPE_KEY:
530 mip4_report_keys(ts, packet);
531 break;
532
533 case MIP4_EVENT_INPUT_TYPE_SCREEN:
534 mip4_report_touch(ts, packet);
535 break;
536
537 default:
538 dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
539 break;
540 }
541
542 return 0;
543}
544
545static irqreturn_t mip4_interrupt(int irq, void *dev_id)
546{
547 struct mip4_ts *ts = dev_id;
548 struct i2c_client *client = ts->client;
549 unsigned int i;
550 int error;
551 u8 cmd[2];
552 u8 size;
553 bool alert;
554
555 /* Read packet info */
556 cmd[0] = MIP4_R0_EVENT;
557 cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
558 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
559 if (error) {
560 dev_err(&client->dev,
561 "Failed to read packet info: %d\n", error);
562 goto out;
563 }
564
565 size = ts->buf[0] & 0x7F;
566 alert = ts->buf[0] & BIT(7);
567 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
568
569 /* Check size */
570 if (!size) {
571 dev_err(&client->dev, "Empty packet\n");
572 goto out;
573 }
574
575 /* Read packet data */
576 cmd[0] = MIP4_R0_EVENT;
577 cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
578 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
579 if (error) {
580 dev_err(&client->dev,
581 "Failed to read packet data: %d\n", error);
582 goto out;
583 }
584
585 if (alert) {
586 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
587 } else {
588 for (i = 0; i < size; i += ts->event_size) {
589 error = mip4_handle_packet(ts, &ts->buf[i]);
590 if (error)
591 break;
592 }
593
594 input_sync(ts->input);
595 }
596
597out:
598 return IRQ_HANDLED;
599}
600
601static int mip4_input_open(struct input_dev *dev)
602{
603 struct mip4_ts *ts = input_get_drvdata(dev);
604
605 return mip4_enable(ts);
606}
607
608static void mip4_input_close(struct input_dev *dev)
609{
610 struct mip4_ts *ts = input_get_drvdata(dev);
611
612 mip4_disable(ts);
613}
614
615/*****************************************************************
616 * Firmware update
617 *****************************************************************/
618
619/* Firmware Info */
620#define MIP4_BL_PAGE_SIZE 512 /* 512 */
621#define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
622
623/*
624 * Firmware binary tail info
625 */
626
627struct mip4_bin_tail {
628 u8 tail_mark[4];
629 u8 chip_name[4];
630
631 __le32 bin_start_addr;
632 __le32 bin_length;
633
634 __le16 ver_boot;
635 __le16 ver_core;
636 __le16 ver_app;
637 __le16 ver_param;
638
639 u8 boot_start;
640 u8 boot_end;
641 u8 core_start;
642 u8 core_end;
643 u8 app_start;
644 u8 app_end;
645 u8 param_start;
646 u8 param_end;
647
648 u8 checksum_type;
649 u8 hw_category;
650
651 __le16 param_id;
652 __le32 param_length;
653 __le32 build_date;
654 __le32 build_time;
655
656 __le32 reserved1;
657 __le32 reserved2;
658 __le16 reserved3;
659 __le16 tail_size;
660 __le32 crc;
661} __packed;
662
663#define MIP4_BIN_TAIL_MARK "MBT\001"
664#define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
665
666/*
667* Bootloader - Read status
668*/
669static int mip4_bl_read_status(struct mip4_ts *ts)
670{
671 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
672 u8 result;
673 struct i2c_msg msg[] = {
674 {
675 .addr = ts->client->addr,
676 .flags = 0,
677 .buf = cmd,
678 .len = sizeof(cmd),
679 }, {
680 .addr = ts->client->addr,
681 .flags = I2C_M_RD,
682 .buf = &result,
683 .len = sizeof(result),
684 },
685 };
686 int ret;
687 int error;
688 int retry = 1000;
689
690 do {
691 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
692 if (ret != ARRAY_SIZE(msg)) {
693 error = ret < 0 ? ret : -EIO;
694 dev_err(&ts->client->dev,
695 "Failed to read bootloader status: %d\n",
696 error);
697 return error;
698 }
699
700 switch (result) {
701 case MIP4_BOOT_STATUS_DONE:
702 dev_dbg(&ts->client->dev, "%s - done\n", __func__);
703 return 0;
704
705 case MIP4_BOOT_STATUS_ERROR:
706 dev_err(&ts->client->dev, "Bootloader failure\n");
707 return -EIO;
708
709 case MIP4_BOOT_STATUS_BUSY:
710 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
711 error = -EBUSY;
712 break;
713
714 default:
715 dev_err(&ts->client->dev,
716 "Unexpected bootloader status: %#02x\n",
717 result);
718 error = -EINVAL;
719 break;
720 }
721
722 usleep_range(1000, 2000);
723 } while (--retry);
724
725 return error;
726}
727
728/*
729* Bootloader - Change mode
730*/
731static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
732{
733 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
734 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
735 u8 result;
736 struct i2c_msg msg[] = {
737 {
738 .addr = ts->client->addr,
739 .flags = 0,
740 .buf = mode_read_cmd,
741 .len = sizeof(mode_read_cmd),
742 }, {
743 .addr = ts->client->addr,
744 .flags = I2C_M_RD,
745 .buf = &result,
746 .len = sizeof(result),
747 },
748 };
749 int retry = 10;
750 int ret;
751 int error;
752
753 do {
754 /* Send mode change command */
755 ret = i2c_master_send(ts->client,
756 mode_chg_cmd, sizeof(mode_chg_cmd));
757 if (ret != sizeof(mode_chg_cmd)) {
758 error = ret < 0 ? ret : -EIO;
759 dev_err(&ts->client->dev,
760 "Failed to send %d mode change: %d (%d)\n",
761 mode, error, ret);
762 return error;
763 }
764
765 dev_dbg(&ts->client->dev,
766 "Sent mode change request (mode: %d)\n", mode);
767
768 /* Wait */
769 msleep(1000);
770
771 /* Verify target mode */
772 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
773 if (ret != ARRAY_SIZE(msg)) {
774 error = ret < 0 ? ret : -EIO;
775 dev_err(&ts->client->dev,
776 "Failed to read device mode: %d\n", error);
777 return error;
778 }
779
780 dev_dbg(&ts->client->dev,
781 "Current device mode: %d, want: %d\n", result, mode);
782
783 if (result == mode)
784 return 0;
785
786 } while (--retry);
787
788 return -EIO;
789}
790
791/*
792 * Bootloader - Start bootloader mode
793 */
794static int mip4_bl_enter(struct mip4_ts *ts)
795{
796 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
797}
798
799/*
800 * Bootloader - Exit bootloader mode
801 */
802static int mip4_bl_exit(struct mip4_ts *ts)
803{
804 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
805}
806
807static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
808{
809 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
810 u8 result[sizeof(u16)];
811 struct i2c_msg msg[] = {
812 {
813 .addr = ts->client->addr,
814 .flags = 0,
815 .buf = cmd,
816 .len = sizeof(cmd),
817 }, {
818 .addr = ts->client->addr,
819 .flags = I2C_M_RD,
820 .buf = result,
821 .len = sizeof(result),
822 },
823 };
824 int ret;
825 int error;
826
827 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
828 if (ret != ARRAY_SIZE(msg)) {
829 error = ret < 0 ? ret : -EIO;
830 dev_err(&ts->client->dev,
831 "Failed to retrieve bootloader buffer address: %d\n",
832 error);
833 return error;
834 }
835
836 *buf_addr = get_unaligned_le16(result);
837 dev_dbg(&ts->client->dev,
838 "Bootloader buffer address %#04x\n", *buf_addr);
839
840 return 0;
841}
842
843static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
844 const u8 *data, int length, u16 buf_addr)
845{
846 u8 cmd[6];
847 u8 *data_buf;
848 u16 buf_offset;
849 int ret;
850 int error;
851
852 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
853 offset, length);
854
855 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
856 dev_err(&ts->client->dev,
857 "Invalid page length: %d\n", length);
858 return -EINVAL;
859 }
860
861 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
862 if (!data_buf)
863 return -ENOMEM;
864
865 /* Addr */
866 cmd[0] = MIP4_R0_BOOT;
867 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
868 put_unaligned_le32(offset, &cmd[2]);
869 ret = i2c_master_send(ts->client, cmd, 6);
870 if (ret != 6) {
871 error = ret < 0 ? ret : -EIO;
872 dev_err(&ts->client->dev,
873 "Failed to send write page address: %d\n", error);
874 goto out;
875 }
876
877 /* Size */
878 cmd[0] = MIP4_R0_BOOT;
879 cmd[1] = MIP4_R1_BOOT_SIZE;
880 put_unaligned_le32(length, &cmd[2]);
881 ret = i2c_master_send(ts->client, cmd, 6);
882 if (ret != 6) {
883 error = ret < 0 ? ret : -EIO;
884 dev_err(&ts->client->dev,
885 "Failed to send write page size: %d\n", error);
886 goto out;
887 }
888
889 /* Data */
890 for (buf_offset = 0;
891 buf_offset < length;
892 buf_offset += MIP4_BL_PACKET_SIZE) {
893 dev_dbg(&ts->client->dev,
894 "writing chunk at %#04x (size %d)\n",
895 buf_offset, MIP4_BL_PACKET_SIZE);
896 put_unaligned_be16(buf_addr + buf_offset, data_buf);
897 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
898 ret = i2c_master_send(ts->client,
899 data_buf, 2 + MIP4_BL_PACKET_SIZE);
900 if (ret != 2 + MIP4_BL_PACKET_SIZE) {
901 error = ret < 0 ? ret : -EIO;
902 dev_err(&ts->client->dev,
903 "Failed to read chunk at %#04x (size %d): %d\n",
904 buf_offset, MIP4_BL_PACKET_SIZE, error);
905 goto out;
906 }
907 }
908
909 /* Command */
910 cmd[0] = MIP4_R0_BOOT;
911 cmd[1] = MIP4_R1_BOOT_CMD;
912 cmd[2] = MIP4_BOOT_CMD_PROGRAM;
913 ret = i2c_master_send(ts->client, cmd, 3);
914 if (ret != 3) {
915 error = ret < 0 ? ret : -EIO;
916 dev_err(&ts->client->dev,
917 "Failed to send 'write' command: %d\n", error);
918 goto out;
919 }
920
921 /* Status */
922 error = mip4_bl_read_status(ts);
923
924out:
925 kfree(data_buf);
926 return error ? error : 0;
927}
928
929static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
930 const u8 *data, int length, int buf_addr)
931{
932 u8 cmd[8];
933 u8 *read_buf;
934 int buf_offset;
935 struct i2c_msg msg[] = {
936 {
937 .addr = ts->client->addr,
938 .flags = 0,
939 .buf = cmd,
940 .len = 2,
941 }, {
942 .addr = ts->client->addr,
943 .flags = I2C_M_RD,
944 .len = MIP4_BL_PACKET_SIZE,
945 },
946 };
947 int ret;
948 int error;
949
950 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
951 offset, length);
952
953 /* Addr */
954 cmd[0] = MIP4_R0_BOOT;
955 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
956 put_unaligned_le32(offset, &cmd[2]);
957 ret = i2c_master_send(ts->client, cmd, 6);
958 if (ret != 6) {
959 error = ret < 0 ? ret : -EIO;
960 dev_err(&ts->client->dev,
961 "Failed to send read page address: %d\n", error);
962 return error;
963 }
964
965 /* Size */
966 cmd[0] = MIP4_R0_BOOT;
967 cmd[1] = MIP4_R1_BOOT_SIZE;
968 put_unaligned_le32(length, &cmd[2]);
969 ret = i2c_master_send(ts->client, cmd, 6);
970 if (ret != 6) {
971 error = ret < 0 ? ret : -EIO;
972 dev_err(&ts->client->dev,
973 "Failed to send read page size: %d\n", error);
974 return error;
975 }
976
977 /* Command */
978 cmd[0] = MIP4_R0_BOOT;
979 cmd[1] = MIP4_R1_BOOT_CMD;
980 cmd[2] = MIP4_BOOT_CMD_READ;
981 ret = i2c_master_send(ts->client, cmd, 3);
982 if (ret != 3) {
983 error = ret < 0 ? ret : -EIO;
984 dev_err(&ts->client->dev,
985 "Failed to send 'read' command: %d\n", error);
986 return error;
987 }
988
989 /* Status */
990 error = mip4_bl_read_status(ts);
991 if (error)
992 return error;
993
994 /* Read */
995 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
996 if (!read_buf)
997 return -ENOMEM;
998
999 for (buf_offset = 0;
1000 buf_offset < length;
1001 buf_offset += MIP4_BL_PACKET_SIZE) {
1002 dev_dbg(&ts->client->dev,
1003 "reading chunk at %#04x (size %d)\n",
1004 buf_offset, MIP4_BL_PACKET_SIZE);
1005 put_unaligned_be16(buf_addr + buf_offset, cmd);
1006 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1007 if (ret != ARRAY_SIZE(msg)) {
1008 error = ret < 0 ? ret : -EIO;
1009 dev_err(&ts->client->dev,
1010 "Failed to read chunk at %#04x (size %d): %d\n",
1011 buf_offset, MIP4_BL_PACKET_SIZE, error);
1012 break;
1013 }
1014
1015 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1016 dev_err(&ts->client->dev,
1017 "Failed to validate chunk at %#04x (size %d)\n",
1018 buf_offset, MIP4_BL_PACKET_SIZE);
1019#if MIP4_FW_UPDATE_DEBUG
1020 print_hex_dump(KERN_DEBUG,
1021 MIP4_DEVICE_NAME " F/W File: ",
1022 DUMP_PREFIX_OFFSET, 16, 1,
1023 data + offset, MIP4_BL_PACKET_SIZE,
1024 false);
1025 print_hex_dump(KERN_DEBUG,
1026 MIP4_DEVICE_NAME " F/W Chip: ",
1027 DUMP_PREFIX_OFFSET, 16, 1,
1028 read_buf, MIP4_BL_PAGE_SIZE, false);
1029#endif
1030 error = -EINVAL;
1031 break;
1032 }
1033 }
1034
1035 kfree(read_buf);
1036 return error ? error : 0;
1037}
1038
1039/*
1040 * Flash chip firmware
1041 */
1042static int mip4_flash_fw(struct mip4_ts *ts,
1043 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1044{
1045 struct i2c_client *client = ts->client;
1046 int offset;
1047 u16 buf_addr;
1048 int error, error2;
1049
1050 /* Enter bootloader mode */
1051 dev_dbg(&client->dev, "Entering bootloader mode\n");
1052
1053 error = mip4_bl_enter(ts);
1054 if (error) {
1055 dev_err(&client->dev,
1056 "Failed to enter bootloader mode: %d\n",
1057 error);
1058 return error;
1059 }
1060
1061 /* Read info */
1062 error = mip4_bl_get_address(ts, &buf_addr);
1063 if (error)
1064 goto exit_bl;
1065
1066 /* Program & Verify */
1067 dev_dbg(&client->dev,
1068 "Program & Verify, page size: %d, packet size: %d\n",
1069 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1070
1071 for (offset = fw_offset;
1072 offset < fw_offset + fw_size;
1073 offset += MIP4_BL_PAGE_SIZE) {
1074 /* Program */
1075 error = mip4_bl_program_page(ts, offset, fw_data + offset,
1076 MIP4_BL_PAGE_SIZE, buf_addr);
1077 if (error)
1078 break;
1079
1080 /* Verify */
1081 error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1082 MIP4_BL_PAGE_SIZE, buf_addr);
1083 if (error)
1084 break;
1085 }
1086
1087exit_bl:
1088 /* Exit bootloader mode */
1089 dev_dbg(&client->dev, "Exiting bootloader mode\n");
1090
1091 error2 = mip4_bl_exit(ts);
1092 if (error2) {
1093 dev_err(&client->dev,
1094 "Failed to exit bootloader mode: %d\n", error2);
1095 if (!error)
1096 error = error2;
1097 }
1098
1099 /* Reset chip */
1100 mip4_power_off(ts);
1101 mip4_power_on(ts);
1102
1103 mip4_query_device(ts);
1104
1105 /* Refresh device parameters */
1106 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1107 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1108 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1109 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1110 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1111 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1112 input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1113 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1114
1115 return error ? error : 0;
1116}
1117
1118static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1119 u32 *fw_offset_start, u32 *fw_size,
1120 const struct mip4_bin_tail **pfw_info)
1121{
1122 const struct mip4_bin_tail *fw_info;
1123 struct mip4_fw_version fw_version;
1124 u16 tail_size;
1125
1126 if (fw->size < MIP4_BIN_TAIL_SIZE) {
1127 dev_err(&ts->client->dev,
1128 "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1129 MIP4_BIN_TAIL_SIZE, fw->size);
1130 return -EINVAL;
1131 }
1132
1133 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1134
1135#if MIP4_FW_UPDATE_DEBUG
1136 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1137 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1138#endif
1139
1140 tail_size = get_unaligned_le16(&fw_info->tail_size);
1141 if (tail_size != MIP4_BIN_TAIL_SIZE) {
1142 dev_err(&ts->client->dev,
1143 "wrong tail size: %d (expected %zd)\n",
1144 tail_size, MIP4_BIN_TAIL_SIZE);
1145 return -EINVAL;
1146 }
1147
1148 /* Check bin format */
1149 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1150 sizeof(fw_info->tail_mark))) {
1151 dev_err(&ts->client->dev,
1152 "unable to locate tail marker (%*ph vs %*ph)\n",
1153 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1154 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1155 return -EINVAL;
1156 }
1157
1158 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1159 *fw_size = get_unaligned_le32(&fw_info->bin_length);
1160
1161 dev_dbg(&ts->client->dev,
1162 "F/W Data offset: %#08x, size: %d\n",
1163 *fw_offset_start, *fw_size);
1164
1165 if (*fw_size % MIP4_BL_PAGE_SIZE) {
1166 dev_err(&ts->client->dev,
1167 "encoded fw length %d is not multiple of pages (%d)\n",
1168 *fw_size, MIP4_BL_PAGE_SIZE);
1169 return -EINVAL;
1170 }
1171
1172 if (fw->size != *fw_offset_start + *fw_size) {
1173 dev_err(&ts->client->dev,
1174 "Wrong firmware size, expected %d bytes, got %zd\n",
1175 *fw_offset_start + *fw_size, fw->size);
1176 return -EINVAL;
1177 }
1178
1179 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1180
1181 dev_dbg(&ts->client->dev,
1182 "F/W file version %04X %04X %04X %04X\n",
1183 fw_version.boot, fw_version.core,
1184 fw_version.app, fw_version.param);
1185
1186 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1187 ts->fw_version.boot, ts->fw_version.core,
1188 ts->fw_version.app, ts->fw_version.param);
1189
1190 /* Check F/W type */
1191 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1192 fw_version.core == 0xEEEE &&
1193 fw_version.app == 0xEEEE &&
1194 fw_version.param == 0xEEEE) {
1195 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1196 } else if (fw_version.boot == 0xEEEE &&
1197 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1198 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1199 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1200 dev_dbg(&ts->client->dev, "F/W type: Main\n");
1201 } else {
1202 dev_err(&ts->client->dev, "Wrong firmware type\n");
1203 return -EINVAL;
1204 }
1205
1206 return 0;
1207}
1208
1209static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1210{
1211 const struct mip4_bin_tail *fw_info;
1212 u32 fw_start_offset;
1213 u32 fw_size;
1214 int retires = 3;
1215 int error;
1216
1217 error = mip4_parse_firmware(ts, fw,
1218 &fw_start_offset, &fw_size, &fw_info);
1219 if (error)
1220 return error;
1221
1222 if (ts->input->users) {
1223 disable_irq(ts->client->irq);
1224 } else {
1225 error = mip4_power_on(ts);
1226 if (error)
1227 return error;
1228 }
1229
1230 /* Update firmware */
1231 do {
1232 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1233 if (!error)
1234 break;
1235 } while (--retires);
1236
1237 if (error)
1238 dev_err(&ts->client->dev,
1239 "Failed to flash firmware: %d\n", error);
1240
1241 /* Enable IRQ */
1242 if (ts->input->users)
1243 enable_irq(ts->client->irq);
1244 else
1245 mip4_power_off(ts);
1246
1247 return error ? error : 0;
1248}
1249
1250static ssize_t mip4_sysfs_fw_update(struct device *dev,
1251 struct device_attribute *attr,
1252 const char *buf, size_t count)
1253{
1254 struct i2c_client *client = to_i2c_client(dev);
1255 struct mip4_ts *ts = i2c_get_clientdata(client);
1256 const struct firmware *fw;
1257 int error;
1258
1259 error = request_firmware(&fw, MIP4_FW_NAME, dev);
1260 if (error) {
1261 dev_err(&ts->client->dev,
1262 "Failed to retrieve firmware %s: %d\n",
1263 MIP4_FW_NAME, error);
1264 return error;
1265 }
1266
1267 /*
1268 * Take input mutex to prevent racing with itself and also with
1269 * userspace opening and closing the device and also suspend/resume
1270 * transitions.
1271 */
1272 mutex_lock(&ts->input->mutex);
1273
1274 error = mip4_execute_fw_update(ts, fw);
1275
1276 mutex_unlock(&ts->input->mutex);
1277
1278 release_firmware(fw);
1279
1280 if (error) {
1281 dev_err(&ts->client->dev,
1282 "Firmware update failed: %d\n", error);
1283 return error;
1284 }
1285
1286 return count;
1287}
1288
1289static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1290
1291static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1292 struct device_attribute *attr,
1293 char *buf)
1294{
1295 struct i2c_client *client = to_i2c_client(dev);
1296 struct mip4_ts *ts = i2c_get_clientdata(client);
1297 size_t count;
1298
1299 /* Take lock to prevent racing with firmware update */
1300 mutex_lock(&ts->input->mutex);
1301
1302 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1303 ts->fw_version.boot, ts->fw_version.core,
1304 ts->fw_version.app, ts->fw_version.param);
1305
1306 mutex_unlock(&ts->input->mutex);
1307
1308 return count;
1309}
1310
1311static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1312
1313static struct attribute *mip4_attrs[] = {
1314 &dev_attr_fw_version.attr,
1315 &dev_attr_update_fw.attr,
1316 NULL,
1317};
1318
1319static const struct attribute_group mip4_attr_group = {
1320 .attrs = mip4_attrs,
1321};
1322
1323static void mip4_sysfs_remove(void *_data)
1324{
1325 struct mip4_ts *ts = _data;
1326
1327 sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
1328}
1329
1330static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1331{
1332 struct mip4_ts *ts;
1333 struct input_dev *input;
1334 int error;
1335
1336 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1337 dev_err(&client->dev, "Not supported I2C adapter\n");
1338 return -ENXIO;
1339 }
1340
1341 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1342 if (!ts)
1343 return -ENOMEM;
1344
1345 input = devm_input_allocate_device(&client->dev);
1346 if (!input)
1347 return -ENOMEM;
1348
1349 ts->client = client;
1350 ts->input = input;
1351
1352 snprintf(ts->phys, sizeof(ts->phys),
1353 "%s/input0", dev_name(&client->dev));
1354
1355 ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1356 "ce", GPIOD_OUT_LOW);
1357 if (IS_ERR(ts->gpio_ce)) {
1358 error = PTR_ERR(ts->gpio_ce);
1359 if (error != EPROBE_DEFER)
1360 dev_err(&client->dev,
1361 "Failed to get gpio: %d\n", error);
1362 return error;
1363 }
1364
1365 error = mip4_power_on(ts);
1366 if (error)
1367 return error;
1368 error = mip4_query_device(ts);
1369 mip4_power_off(ts);
1370 if (error)
1371 return error;
1372
1373 input->name = "MELFAS MIP4 Touchscreen";
1374 input->phys = ts->phys;
1375
1376 input->id.bustype = BUS_I2C;
1377 input->id.vendor = 0x13c5;
1378
1379 input->open = mip4_input_open;
1380 input->close = mip4_input_close;
1381
1382 input_set_drvdata(input, ts);
1383
1384 input->keycode = ts->key_code;
1385 input->keycodesize = sizeof(*ts->key_code);
1386 input->keycodemax = ts->key_num;
1387
1388 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1389 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1390 input_set_abs_params(input, ABS_MT_PRESSURE,
1391 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1392 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1393 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1394 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1395 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1396 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1397 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1398
1399 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1400 if (error)
1401 return error;
1402
1403 i2c_set_clientdata(client, ts);
1404
1405 error = devm_request_threaded_irq(&client->dev, client->irq,
1406 NULL, mip4_interrupt,
1407 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1408 if (error) {
1409 dev_err(&client->dev,
1410 "Failed to request interrupt %d: %d\n",
1411 client->irq, error);
1412 return error;
1413 }
1414
1415 disable_irq(client->irq);
1416
1417 error = input_register_device(input);
1418 if (error) {
1419 dev_err(&client->dev,
1420 "Failed to register input device: %d\n", error);
1421 return error;
1422 }
1423
1424 error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
1425 if (error) {
1426 dev_err(&client->dev,
1427 "Failed to create sysfs attribute group: %d\n", error);
1428 return error;
1429 }
1430
1431 error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
1432 if (error) {
1433 mip4_sysfs_remove(ts);
1434 dev_err(&client->dev,
1435 "Failed to install sysfs remoce action: %d\n", error);
1436 return error;
1437 }
1438
1439 return 0;
1440}
1441
1442static int __maybe_unused mip4_suspend(struct device *dev)
1443{
1444 struct i2c_client *client = to_i2c_client(dev);
1445 struct mip4_ts *ts = i2c_get_clientdata(client);
1446 struct input_dev *input = ts->input;
1447
1448 mutex_lock(&input->mutex);
1449
1450 if (device_may_wakeup(dev))
1451 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1452 else if (input->users)
1453 mip4_disable(ts);
1454
1455 mutex_unlock(&input->mutex);
1456
1457 return 0;
1458}
1459
1460static int __maybe_unused mip4_resume(struct device *dev)
1461{
1462 struct i2c_client *client = to_i2c_client(dev);
1463 struct mip4_ts *ts = i2c_get_clientdata(client);
1464 struct input_dev *input = ts->input;
1465
1466 mutex_lock(&input->mutex);
1467
1468 if (ts->wake_irq_enabled)
1469 disable_irq_wake(client->irq);
1470 else if (input->users)
1471 mip4_enable(ts);
1472
1473 mutex_unlock(&input->mutex);
1474
1475 return 0;
1476}
1477
1478static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1479
1480#ifdef CONFIG_OF
1481static const struct of_device_id mip4_of_match[] = {
1482 { .compatible = "melfas,"MIP4_DEVICE_NAME, },
1483 { },
1484};
1485MODULE_DEVICE_TABLE(of, mip4_of_match);
1486#endif
1487
1488#ifdef CONFIG_ACPI
1489static const struct acpi_device_id mip4_acpi_match[] = {
1490 { "MLFS0000", 0},
1491 { },
1492};
1493MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1494#endif
1495
1496static const struct i2c_device_id mip4_i2c_ids[] = {
1497 { MIP4_DEVICE_NAME, 0 },
1498 { },
1499};
1500MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1501
1502static struct i2c_driver mip4_driver = {
1503 .id_table = mip4_i2c_ids,
1504 .probe = mip4_probe,
1505 .driver = {
1506 .name = MIP4_DEVICE_NAME,
1507 .of_match_table = of_match_ptr(mip4_of_match),
1508 .acpi_match_table = ACPI_PTR(mip4_acpi_match),
1509 .pm = &mip4_pm_ops,
1510 },
1511};
1512module_i2c_driver(mip4_driver);
1513
1514MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1515MODULE_VERSION("2016.03.03");
1516MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1517MODULE_LICENSE("GPL");
diff --git a/drivers/input/touchscreen/stmpe-ts.c b/drivers/input/touchscreen/stmpe-ts.c
index e414d43e5159..2a78e27b4495 100644
--- a/drivers/input/touchscreen/stmpe-ts.c
+++ b/drivers/input/touchscreen/stmpe-ts.c
@@ -63,6 +63,37 @@
63#define STMPE_TS_NAME "stmpe-ts" 63#define STMPE_TS_NAME "stmpe-ts"
64#define XY_MASK 0xfff 64#define XY_MASK 0xfff
65 65
66/**
67 * struct stmpe_touch - stmpe811 touch screen controller state
68 * @stmpe: pointer back to STMPE MFD container
69 * @idev: registered input device
70 * @work: a work item used to scan the device
71 * @dev: a pointer back to the MFD cell struct device*
72 * @sample_time: ADC converstion time in number of clock.
73 * (0 -> 36 clocks, 1 -> 44 clocks, 2 -> 56 clocks, 3 -> 64 clocks,
74 * 4 -> 80 clocks, 5 -> 96 clocks, 6 -> 144 clocks),
75 * recommended is 4.
76 * @mod_12b: ADC Bit mode (0 -> 10bit ADC, 1 -> 12bit ADC)
77 * @ref_sel: ADC reference source
78 * (0 -> internal reference, 1 -> external reference)
79 * @adc_freq: ADC Clock speed
80 * (0 -> 1.625 MHz, 1 -> 3.25 MHz, 2 || 3 -> 6.5 MHz)
81 * @ave_ctrl: Sample average control
82 * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
83 * @touch_det_delay: Touch detect interrupt delay
84 * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
85 * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
86 * recommended is 3
87 * @settling: Panel driver settling time
88 * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
89 * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
90 * recommended is 2
91 * @fraction_z: Length of the fractional part in z
92 * (fraction_z ([0..7]) = Count of the fractional part)
93 * recommended is 7
94 * @i_drive: current limit value of the touchscreen drivers
95 * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
96 */
66struct stmpe_touch { 97struct stmpe_touch {
67 struct stmpe *stmpe; 98 struct stmpe *stmpe;
68 struct input_dev *idev; 99 struct input_dev *idev;
diff --git a/include/linux/input/cyttsp.h b/include/linux/input/cyttsp.h
index 5af7c66f1fca..586c8c95dcb0 100644
--- a/include/linux/input/cyttsp.h
+++ b/include/linux/input/cyttsp.h
@@ -40,19 +40,4 @@
40/* Active distance in pixels for a gesture to be reported */ 40/* Active distance in pixels for a gesture to be reported */
41#define CY_ACT_DIST_DFLT 0xF8 /* pixels */ 41#define CY_ACT_DIST_DFLT 0xF8 /* pixels */
42 42
43struct cyttsp_platform_data {
44 u32 maxx;
45 u32 maxy;
46 bool use_hndshk;
47 u8 act_dist; /* Active distance */
48 u8 act_intrvl; /* Active refresh interval; ms */
49 u8 tch_tmout; /* Active touch timeout; ms */
50 u8 lp_intrvl; /* Low power refresh interval; ms */
51 int (*init)(void);
52 void (*exit)(void);
53 char *name;
54 s16 irq_gpio;
55 u8 *bl_keys;
56};
57
58#endif /* _CYTTSP_H_ */ 43#endif /* _CYTTSP_H_ */
diff --git a/include/linux/spi/ad7879.h b/include/linux/platform_data/ad7879.h
index 58368be0b4c0..69e2e1fd2bc8 100644
--- a/include/linux/spi/ad7879.h
+++ b/include/linux/platform_data/ad7879.h
@@ -1,4 +1,4 @@
1/* linux/spi/ad7879.h */ 1/* linux/platform_data/ad7879.h */
2 2
3/* Touchscreen characteristics vary between boards and models. The 3/* Touchscreen characteristics vary between boards and models. The
4 * platform_data for the device's "struct device" holds this information. 4 * platform_data for the device's "struct device" holds this information.
diff --git a/include/linux/rmi.h b/include/linux/rmi.h
new file mode 100644
index 000000000000..e0aca1476001
--- /dev/null
+++ b/include/linux/rmi.h
@@ -0,0 +1,359 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#ifndef _RMI_H
11#define _RMI_H
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/interrupt.h>
15#include <linux/input.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/types.h>
19
20#define NAME_BUFFER_SIZE 256
21
22/**
23 * struct rmi_2d_axis_alignment - target axis alignment
24 * @swap_axes: set to TRUE if desired to swap x- and y-axis
25 * @flip_x: set to TRUE if desired to flip direction on x-axis
26 * @flip_y: set to TRUE if desired to flip direction on y-axis
27 * @clip_x_low - reported X coordinates below this setting will be clipped to
28 * the specified value
29 * @clip_x_high - reported X coordinates above this setting will be clipped to
30 * the specified value
31 * @clip_y_low - reported Y coordinates below this setting will be clipped to
32 * the specified value
33 * @clip_y_high - reported Y coordinates above this setting will be clipped to
34 * the specified value
35 * @offset_x - this value will be added to all reported X coordinates
36 * @offset_y - this value will be added to all reported Y coordinates
37 * @rel_report_enabled - if set to true, the relative reporting will be
38 * automatically enabled for this sensor.
39 */
40struct rmi_2d_axis_alignment {
41 bool swap_axes;
42 bool flip_x;
43 bool flip_y;
44 u16 clip_x_low;
45 u16 clip_y_low;
46 u16 clip_x_high;
47 u16 clip_y_high;
48 u16 offset_x;
49 u16 offset_y;
50 u8 delta_x_threshold;
51 u8 delta_y_threshold;
52};
53
54/** This is used to override any hints an F11 2D sensor might have provided
55 * as to what type of sensor it is.
56 *
57 * @rmi_f11_sensor_default - do not override, determine from F11_2D_QUERY14 if
58 * available.
59 * @rmi_f11_sensor_touchscreen - treat the sensor as a touchscreen (direct
60 * pointing).
61 * @rmi_f11_sensor_touchpad - thread the sensor as a touchpad (indirect
62 * pointing).
63 */
64enum rmi_sensor_type {
65 rmi_sensor_default = 0,
66 rmi_sensor_touchscreen,
67 rmi_sensor_touchpad
68};
69
70#define RMI_F11_DISABLE_ABS_REPORT BIT(0)
71
72/**
73 * struct rmi_2d_sensor_data - overrides defaults for a 2D sensor.
74 * @axis_align - provides axis alignment overrides (see above).
75 * @sensor_type - Forces the driver to treat the sensor as an indirect
76 * pointing device (touchpad) rather than a direct pointing device
77 * (touchscreen). This is useful when F11_2D_QUERY14 register is not
78 * available.
79 * @disable_report_mask - Force data to not be reported even if it is supported
80 * by the firware.
81 * @topbuttonpad - Used with the "5 buttons touchpads" found on the Lenovo 40
82 * series
83 * @kernel_tracking - most moderns RMI f11 firmwares implement Multifinger
84 * Type B protocol. However, there are some corner cases where the user
85 * triggers some jumps by tapping with two fingers on the touchpad.
86 * Use this setting and dmax to filter out these jumps.
87 * Also, when using an old sensor using MF Type A behavior, set to true to
88 * report an actual MT protocol B.
89 * @dmax - the maximum distance (in sensor units) the kernel tracking allows two
90 * distincts fingers to be considered the same.
91 */
92struct rmi_2d_sensor_platform_data {
93 struct rmi_2d_axis_alignment axis_align;
94 enum rmi_sensor_type sensor_type;
95 int x_mm;
96 int y_mm;
97 int disable_report_mask;
98 u16 rezero_wait;
99 bool topbuttonpad;
100 bool kernel_tracking;
101 int dmax;
102};
103
104/**
105 * struct rmi_f30_data - overrides defaults for a single F30 GPIOs/LED chip.
106 * @buttonpad - the touchpad is a buttonpad, so enable only the first actual
107 * button that is found.
108 * @trackstick_buttons - Set when the function 30 is handling the physical
109 * buttons of the trackstick (as a PD/2 passthrough device.
110 * @disable - the touchpad incorrectly reports F30 and it should be ignored.
111 * This is a special case which is due to misconfigured firmware.
112 */
113struct rmi_f30_data {
114 bool buttonpad;
115 bool trackstick_buttons;
116 bool disable;
117};
118
119/**
120 * struct rmi_f01_power - override default power management settings.
121 *
122 */
123enum rmi_f01_nosleep {
124 RMI_F01_NOSLEEP_DEFAULT = 0,
125 RMI_F01_NOSLEEP_OFF = 1,
126 RMI_F01_NOSLEEP_ON = 2
127};
128
129/**
130 * struct rmi_f01_power_management -When non-zero, these values will be written
131 * to the touch sensor to override the default firmware settigns. For a
132 * detailed explanation of what each field does, see the corresponding
133 * documention in the RMI4 specification.
134 *
135 * @nosleep - specifies whether the device is permitted to sleep or doze (that
136 * is, enter a temporary low power state) when no fingers are touching the
137 * sensor.
138 * @wakeup_threshold - controls the capacitance threshold at which the touch
139 * sensor will decide to wake up from that low power state.
140 * @doze_holdoff - controls how long the touch sensor waits after the last
141 * finger lifts before entering the doze state, in units of 100ms.
142 * @doze_interval - controls the interval between checks for finger presence
143 * when the touch sensor is in doze mode, in units of 10ms.
144 */
145struct rmi_f01_power_management {
146 enum rmi_f01_nosleep nosleep;
147 u8 wakeup_threshold;
148 u8 doze_holdoff;
149 u8 doze_interval;
150};
151
152/**
153 * struct rmi_device_platform_data_spi - provides parameters used in SPI
154 * communications. All Synaptics SPI products support a standard SPI
155 * interface; some also support what is called SPI V2 mode, depending on
156 * firmware and/or ASIC limitations. In V2 mode, the touch sensor can
157 * support shorter delays during certain operations, and these are specified
158 * separately from the standard mode delays.
159 *
160 * @block_delay - for standard SPI transactions consisting of both a read and
161 * write operation, the delay (in microseconds) between the read and write
162 * operations.
163 * @split_read_block_delay_us - for V2 SPI transactions consisting of both a
164 * read and write operation, the delay (in microseconds) between the read and
165 * write operations.
166 * @read_delay_us - the delay between each byte of a read operation in normal
167 * SPI mode.
168 * @write_delay_us - the delay between each byte of a write operation in normal
169 * SPI mode.
170 * @split_read_byte_delay_us - the delay between each byte of a read operation
171 * in V2 mode.
172 * @pre_delay_us - the delay before the start of a SPI transaction. This is
173 * typically useful in conjunction with custom chip select assertions (see
174 * below).
175 * @post_delay_us - the delay after the completion of an SPI transaction. This
176 * is typically useful in conjunction with custom chip select assertions (see
177 * below).
178 * @cs_assert - For systems where the SPI subsystem does not control the CS/SSB
179 * line, or where such control is broken, you can provide a custom routine to
180 * handle a GPIO as CS/SSB. This routine will be called at the beginning and
181 * end of each SPI transaction. The RMI SPI implementation will wait
182 * pre_delay_us after this routine returns before starting the SPI transfer;
183 * and post_delay_us after completion of the SPI transfer(s) before calling it
184 * with assert==FALSE.
185 */
186struct rmi_device_platform_data_spi {
187 u32 block_delay_us;
188 u32 split_read_block_delay_us;
189 u32 read_delay_us;
190 u32 write_delay_us;
191 u32 split_read_byte_delay_us;
192 u32 pre_delay_us;
193 u32 post_delay_us;
194 u8 bits_per_word;
195 u16 mode;
196
197 void *cs_assert_data;
198 int (*cs_assert)(const void *cs_assert_data, const bool assert);
199};
200
201/**
202 * struct rmi_device_platform_data - system specific configuration info.
203 *
204 * @reset_delay_ms - after issuing a reset command to the touch sensor, the
205 * driver waits a few milliseconds to give the firmware a chance to
206 * to re-initialize. You can override the default wait period here.
207 */
208struct rmi_device_platform_data {
209 int reset_delay_ms;
210
211 struct rmi_device_platform_data_spi spi_data;
212
213 /* function handler pdata */
214 struct rmi_2d_sensor_platform_data *sensor_pdata;
215 struct rmi_f01_power_management power_management;
216 struct rmi_f30_data *f30_data;
217};
218
219/**
220 * struct rmi_function_descriptor - RMI function base addresses
221 *
222 * @query_base_addr: The RMI Query base address
223 * @command_base_addr: The RMI Command base address
224 * @control_base_addr: The RMI Control base address
225 * @data_base_addr: The RMI Data base address
226 * @interrupt_source_count: The number of irqs this RMI function needs
227 * @function_number: The RMI function number
228 *
229 * This struct is used when iterating the Page Description Table. The addresses
230 * are 16-bit values to include the current page address.
231 *
232 */
233struct rmi_function_descriptor {
234 u16 query_base_addr;
235 u16 command_base_addr;
236 u16 control_base_addr;
237 u16 data_base_addr;
238 u8 interrupt_source_count;
239 u8 function_number;
240 u8 function_version;
241};
242
243struct rmi_device;
244
245/**
246 * struct rmi_transport_dev - represent an RMI transport device
247 *
248 * @dev: Pointer to the communication device, e.g. i2c or spi
249 * @rmi_dev: Pointer to the RMI device
250 * @proto_name: name of the transport protocol (SPI, i2c, etc)
251 * @ops: pointer to transport operations implementation
252 *
253 * The RMI transport device implements the glue between different communication
254 * buses such as I2C and SPI.
255 *
256 */
257struct rmi_transport_dev {
258 struct device *dev;
259 struct rmi_device *rmi_dev;
260
261 const char *proto_name;
262 const struct rmi_transport_ops *ops;
263
264 struct rmi_device_platform_data pdata;
265
266 struct input_dev *input;
267
268 void *attn_data;
269 int attn_size;
270};
271
272/**
273 * struct rmi_transport_ops - defines transport protocol operations.
274 *
275 * @write_block: Writing a block of data to the specified address
276 * @read_block: Read a block of data from the specified address.
277 */
278struct rmi_transport_ops {
279 int (*write_block)(struct rmi_transport_dev *xport, u16 addr,
280 const void *buf, size_t len);
281 int (*read_block)(struct rmi_transport_dev *xport, u16 addr,
282 void *buf, size_t len);
283 int (*reset)(struct rmi_transport_dev *xport, u16 reset_addr);
284};
285
286/**
287 * struct rmi_driver - driver for an RMI4 sensor on the RMI bus.
288 *
289 * @driver: Device driver model driver
290 * @reset_handler: Called when a reset is detected.
291 * @clear_irq_bits: Clear the specified bits in the current interrupt mask.
292 * @set_irq_bist: Set the specified bits in the current interrupt mask.
293 * @store_productid: Callback for cache product id from function 01
294 * @data: Private data pointer
295 *
296 */
297struct rmi_driver {
298 struct device_driver driver;
299
300 int (*reset_handler)(struct rmi_device *rmi_dev);
301 int (*clear_irq_bits)(struct rmi_device *rmi_dev, unsigned long *mask);
302 int (*set_irq_bits)(struct rmi_device *rmi_dev, unsigned long *mask);
303 int (*store_productid)(struct rmi_device *rmi_dev);
304 int (*set_input_params)(struct rmi_device *rmi_dev,
305 struct input_dev *input);
306 void *data;
307};
308
309/**
310 * struct rmi_device - represents an RMI4 sensor device on the RMI bus.
311 *
312 * @dev: The device created for the RMI bus
313 * @number: Unique number for the device on the bus.
314 * @driver: Pointer to associated driver
315 * @xport: Pointer to the transport interface
316 *
317 */
318struct rmi_device {
319 struct device dev;
320 int number;
321
322 struct rmi_driver *driver;
323 struct rmi_transport_dev *xport;
324
325};
326
327struct rmi_driver_data {
328 struct list_head function_list;
329
330 struct rmi_device *rmi_dev;
331
332 struct rmi_function *f01_container;
333 bool f01_bootloader_mode;
334
335 u32 attn_count;
336 int num_of_irq_regs;
337 int irq_count;
338 unsigned long *irq_status;
339 unsigned long *fn_irq_bits;
340 unsigned long *current_irq_mask;
341 unsigned long *new_irq_mask;
342 struct mutex irq_mutex;
343 struct input_dev *input;
344
345 u8 pdt_props;
346 u8 bsr;
347
348 bool enabled;
349
350 void *data;
351};
352
353int rmi_register_transport_device(struct rmi_transport_dev *xport);
354void rmi_unregister_transport_device(struct rmi_transport_dev *xport);
355int rmi_process_interrupt_requests(struct rmi_device *rmi_dev);
356
357int rmi_driver_suspend(struct rmi_device *rmi_dev);
358int rmi_driver_resume(struct rmi_device *rmi_dev);
359#endif
diff --git a/include/linux/rotary_encoder.h b/include/linux/rotary_encoder.h
deleted file mode 100644
index fe3dc64e5aeb..000000000000
--- a/include/linux/rotary_encoder.h
+++ /dev/null
@@ -1,17 +0,0 @@
1#ifndef __ROTARY_ENCODER_H__
2#define __ROTARY_ENCODER_H__
3
4struct rotary_encoder_platform_data {
5 unsigned int steps;
6 unsigned int axis;
7 unsigned int gpio_a;
8 unsigned int gpio_b;
9 unsigned int inverted_a;
10 unsigned int inverted_b;
11 unsigned int steps_per_period;
12 bool relative_axis;
13 bool rollover;
14 bool wakeup_source;
15};
16
17#endif /* __ROTARY_ENCODER_H__ */
diff --git a/include/uapi/linux/input.h b/include/uapi/linux/input.h
index 2758687300b4..01113841190d 100644
--- a/include/uapi/linux/input.h
+++ b/include/uapi/linux/input.h
@@ -246,6 +246,7 @@ struct input_mask {
246#define BUS_GSC 0x1A 246#define BUS_GSC 0x1A
247#define BUS_ATARI 0x1B 247#define BUS_ATARI 0x1B
248#define BUS_SPI 0x1C 248#define BUS_SPI 0x1C
249#define BUS_RMI 0x1D
249 250
250/* 251/*
251 * MT_TOOL types 252 * MT_TOOL types