diff --git a/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml b/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml index 6366261e5799d6..b27edd46162987 100644 --- a/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml +++ b/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml @@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/input/touchscreen/apple,p105-z2-multitouch.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Apple P105 Z2 N1 touchscreen +title: Apple Z2 N1 touchscreen maintainers: - Alexandre Figueiredo <17xande@gmail.com> @@ -15,7 +15,9 @@ allOf: properties: compatible: - const: apple,p105-z2-multitouch + enum: + - apple,n42-z2-multitouch + - apple,p105-z2-multitouch reg: maxItems: 1 @@ -43,7 +45,7 @@ properties: firmware-name: maxItems: 1 - description: mtprops file containing the preconstructed HBPP DATA packet + description: HBPP DATA packet on N42 or an mtprops file containing it on P105 generation: $ref: /schemas/types.yaml#/definitions/uint32 @@ -62,6 +64,21 @@ properties: $ref: /schemas/types.yaml#/definitions/uint32 description: Frequency-locked loop multiplier used to calibrate the digitizer + apple,ref-clk-div-val: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Reference clock divider for the digitizer calibration + default: 32 + + apple,z2-prox-cal-blob: + $ref: /schemas/types.yaml#/definitions/uint8-array + maxItems: 128 + description: Per-device proximity calibration supplied by the bootloader + + apple,z2-cal-blob: + $ref: /schemas/types.yaml#/definitions/uint8-array + maxItems: 1536 + description: Per-device panel calibration supplied by the bootloader + apple,scan-start-report: $ref: /schemas/types.yaml#/definitions/uint8-array minItems: 4 diff --git a/Documentation/devicetree/bindings/mtd/apple,h2fmi.yaml b/Documentation/devicetree/bindings/mtd/apple,h2fmi.yaml index a60445d3b5f61b..1afa825403754e 100644 --- a/Documentation/devicetree/bindings/mtd/apple,h2fmi.yaml +++ b/Documentation/devicetree/bindings/mtd/apple,h2fmi.yaml @@ -10,13 +10,16 @@ maintainers: - Shayne Legault <8741557+shaynelegault@users.noreply.github.com> description: - The H2FMI controller drives the Hynix PPN packages used as boot storage on + The H2FMI controller drives the PPN packages used as boot storage on Apple S5L8940X-family devices. Each of its two buses has separate FMI, FMC - and ECC register windows. + and ECC register windows. The S5L8950X (A6) controller is driven + read-only and must not fall back to apple,s5l8940x-fmi. properties: compatible: - const: apple,s5l8940x-fmi + enum: + - apple,s5l8940x-fmi + - apple,s5l8950x-fmi reg: minItems: 6 @@ -26,11 +29,28 @@ properties: minItems: 2 maxItems: 2 + clocks: + description: + PMGR power-state gates the controller needs (on S5L8950X FMI0, + FMI0-BCH, FMI1, FMI1-BCH and FMI-DLL), switched on before any + register access. + minItems: 1 + maxItems: 5 + required: - compatible - reg - interrupts +allOf: + - if: + properties: + compatible: + const: apple,s5l8950x-fmi + then: + required: + - clocks + additionalProperties: false examples: diff --git a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi index 0d8858f3ece075..e7c76d227d43b2 100644 --- a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi +++ b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi @@ -13,6 +13,55 @@ / { aliases { serial0 = &uart6; + bluetooth0 = &bluetooth; + touchscreen0 = &touchscreen; + wifi0 = &wifi; + }; + + /* + * Physical buttons and mute switch (ADT /buttons): active-low on SoC + * GPIOs 0..4, identical layout to P105 (iPad mini 1). + * Codes: KEY_HOMEPAGE 172, KEY_POWER 116, KEY_VOLUMEUP 115, + * KEY_VOLUMEDOWN 114, EV_SW 5, SW_MUTE_DEVICE 14. + */ + gpio-keys { + compatible = "gpio-keys"; + autorepeat; + + home { + label = "Home"; + gpios = <&gpio 0 1>; + linux,code = <172>; + debounce-interval = <20>; + }; + + hold { + label = "Hold"; + gpios = <&gpio 1 1>; + linux,code = <116>; + debounce-interval = <20>; + }; + + volume-up { + label = "Volume Up"; + gpios = <&gpio 2 1>; + linux,code = <115>; + debounce-interval = <20>; + }; + + volume-down { + label = "Volume Down"; + gpios = <&gpio 3 1>; + linux,code = <114>; + debounce-interval = <20>; + }; + + ringer-switch { + label = "Ringer Switch"; + gpios = <&gpio 4 1>; + linux,code = <113>; /* KEY_MUTE, held while on silent */ + debounce-interval = <50>; + }; }; }; @@ -28,3 +77,384 @@ &uart6 { status = "okay"; }; + +/* + * SanDisk 1Xnm64GeX2 PPN 1.5.5, 2 buses x 2 CAUs x 1056 blocks (12H321 + * ADT flash-controller0/disk), driven read-only. The boot chain lives in + * the same NAND (boot-from-nand 1), so nothing may write it blindly. + */ +&fmi0_ps { + status = "okay"; +}; + +&fmi0_bch_ps { + status = "okay"; +}; + +&fmi1_ps { + status = "okay"; +}; + +&fmi1_bch_ps { + status = "okay"; +}; + +&fmi_dll_ps { + status = "okay"; +}; + +&nand { + status = "okay"; +}; + +&gpio { + /* + * UART3 (Bluetooth): ADT function-tx = 0x0400 (pin 32), function-rts = + * 0x0402 (pin 34), so TX=32, RX=33, RTS=34, CTS=35. + */ + uart3_pins: uart3-pins { + pins = "gpio32", "gpio33", "gpio34", "gpio35"; + function = "periph"; + }; +}; + +/* + * The PMGR gates of the consumers below (s5l8950x.dtsi keeps every gate + * disabled until a board has a consumer for it). Each consumer holds its + * gate from probe: I2C, SPI and HDQ enable their clock at probe, the + * LM3534 backlight enables the DWI's, and the UART3 serial port its own + * while the Bluetooth line discipline has it open. + */ +&i2c0_ps { + status = "okay"; +}; + +&i2c1_ps { + status = "okay"; +}; + +&spi1_ps { + status = "okay"; +}; + +&uart3_ps { + status = "okay"; +}; + +&uart5_ps { + status = "okay"; +}; + +/* + * UART3: BCM4334 Bluetooth half (ADT uart3/bluetooth, "bluetooth,n88"). + * function-power_enable = PMU GPIO 8 (0x68 + 8 = 0x70) + * function-bt_wake = SoC GPIO 7 (0x0007) + * Patchram: "BCM4334B1 37.4 MHz Centennial Murata" -> brcm/BCM4334B0.hcd. + * local-bd-address is filled at boot by m1n1 via the bluetooth0 alias. + */ +&uart3 { + pinctrl-names = "default"; + pinctrl-0 = <&uart3_pins>; + status = "okay"; + + bluetooth: bluetooth { + compatible = "brcm,bcm4334-bt"; + shutdown-gpios = <&pmu 8 0>; + device-wakeup-gpios = <&gpio 7 0>; + max-speed = <1500000>; + local-bd-address = [00 00 00 00 00 00]; + }; +}; + +/* + * Attach Wi-Fi WL_REG_ON (PMU GPIO 10 = reg 0x72) to the shared S5L8950X + * USB/HSIC PHY defined in s5l8950x.dtsi. + */ +&otgphy { + hsic-supply = <&wlan_reg_on>; +}; + +&soc { + /* + * DWI power-state switch (PMGR device-clocks index 0x4d -> 0xf101134). + * SPI1/UART3/UART5/I2C* power-state gates come from s5l8950x.dtsi. + */ + dwi_ps: dwi-ps@f101134 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101134 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "dwi_ps"; + }; + + /* + * Touch 32768 Hz reference clock: PWM channel 2 (live ADT + * /arm-io/pwm/grape-clk, reg = 2, default-hz = 32768; multi-touch + * function-clock_enable = Cmwp). PMGR gate is index 0x49 = 0xf101124. + * + * Note: the vibrator (live ADT /arm-io/pwm/vibrator, PWM ch 1, + * default-hz = 32000, enable = SoC GPIO 95 = 0x0b07) shares this + * 0x33500000 MMIO window and is deferred until PWM is split into a + * shared provider. + */ + touchclk: touchclk@3500000 { + compatible = "apple,s5l8940x-pwm-clk"; + #clock-cells = <0>; + reg = <0x3500000 0x1000>, <0xf101124 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "touchclk"; + clock-frequency = <32768>; + apple,pwm-channel = <2>; + }; + + /* + * Digital Wire Interface (live ADT /arm-io/dwi@F700000, reg 0xf700000, + * version = 1): carries 11-bit brightness commands to the external TI + * LM3534 backlight. iBoot leaves it set up (+0x00 = 1, +0x04 = 0x78) + * with its last level in +0x14. 0xf107000 lies past the PMGR's range: + * no level ever reached the LM3534, and once dark the panel never lit + * again. + */ + dwi: dwi@f700000 { + compatible = "apple,s5l8950x-dwi", "apple,s5l8940x-dwi"; + reg = <0xf700000 0x1000>; + clocks = <&dwi_ps>; + apple,dwi-version = <1>; + }; + + /* + * I2C0: Dialog D1972 PMU (0x74), TI LM3534 backlight (0x63), plus + * audio/gyro/tristar (deferred). Confirmed from live 12H321 ADT. + */ + i2c0: i2c@3200000 { + compatible = "apple,s5l8940x-i2c"; + reg = <0x3200000 0x1000>; + clock-frequency = <100000>; + interrupts = <18>; + clocks = <&i2c0_ps>; + clock-names = "i2c"; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + pmu: pmu@74 { + compatible = "apple,pmu-d1972"; + reg = <0x74>; + system-power-controller; + gpio-controller; + #gpio-cells = <2>; + #address-cells = <1>; + #size-cells = <0>; + + /* + * Live ADT multi-touch function-power_ana = 0x020c (LDO 12): + * AppleD1972PMU LDO table (0x810ece34) gives en_reg = 0x22, + * en_mask = 0x04. Whether the scan also needs LDO 15 (0x22 + * bit 0x40), as on the iPad mini 1, is not known yet. + */ + touch_ana: touch_ana@22 { + compatible = "apple,pmu-pwrsw"; + reg = <0x22>; + mask = <0x04>; + enable = <0x04>; + }; + + /* + * Live ADT multi-touch function-power_ldo = 0x0213 (LDO 19): + * AppleD1972PMU LDO table gives en_reg = 0x23, en_mask = 0x40. + */ + touch_ldo: touch_ldo@23 { + compatible = "apple,pmu-pwrsw"; + reg = <0x23>; + mask = <0x40>; + enable = <0x40>; + }; + + /* + * BCM4334 WL_REG_ON: live ADT wlan function-device_reset is + * PMU GPIO 10 (0x68 + 10 = 0x72), bit 1 is output level. + */ + wlan_reg_on: wlan_reg_on@72 { + compatible = "apple,pmu-pwrsw"; + reg = <0x72>; + mask = <0x02>; + enable = <0x02>; + }; + + /* + * D1972 USB input current limit (register 0x3f, 10 mA/LSB + * from 70 mA; VBUS online = 0x0a bit 6; sag = 0x03). + * Log-only until apple_pmu_charger.d1972_armed=1. + */ + charger@3f { + compatible = "apple,pmu-d1972-charger"; + reg = <0x3f>; + }; + }; + + /* + * External TI LM3534 display backlight (live ADT "backlight,lm3534", + * using-LM3534 = 1): enable via I2C reg 0x10 bit 0, 11-bit + * brightness via DWI block. + */ + backlight: lm3534@63 { + compatible = "apple,lm3534-backlight"; + reg = <0x63>; + apple,dwi = <&dwi>; + }; + }; + + /* + * I2C1: motion sensors (LIS3DSH accelerometer @ 0x1d, AK8963 compass @ 0x0c). + */ + i2c1: i2c@3300000 { + compatible = "apple,s5l8940x-i2c"; + reg = <0x3300000 0x1000>; + clock-frequency = <100000>; + interrupts = <19>; + clocks = <&i2c1_ps>; + clock-names = "i2c"; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + accelerometer@1d { + compatible = "st,lis3dsh-accel"; + reg = <0x1d>; + }; + + compass@c { + compatible = "asahi-kasei,ak8963"; + reg = <0x0c>; + }; + }; + + /* + * SPI1: Gen1 Z2 multi-touch controller (live ADT multi-touch,n41; + * AppleMultitouchN1SPI C1F57,1, Family 0x57, 25x10, 5000x8900, N42.mtprops). + * GPIOs (port << 8 | pin): + * function-enable_cs = 0x0601 -> SoC GPIO 49 + * function-reset = 0x1c07 -> SoC GPIO 231 + * ATTN interrupt = 0xc9 -> SoC GPIO 201 (active-low) + */ + spi1: spi@2100000 { + compatible = "apple,s5l8940x-spi"; + reg = <0x2100000 0x1000>; + interrupts = <29>; + clocks = <&spi1_ps>; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + /* + * The kernel boots the digitizer, as iOS 8.4.1's + * AppleMultitouchN1SPI does. firmware-name is the "Constructed + * Firmware" of the C1F57,1 entry in the firmware's N42.mtprops. + * The panel and prox calibration come from the boot loader + * (ADT multi-touch-calibration and prox-calibration, copied + * through the touchscreen0 alias). The clock values are those + * of the "AppleMultitouchSPI - C1,N41" personality: fll-mval + * 6099, ref-clk-div-val 2, clk32-clock-enable-val 1. + */ + touchscreen: touch@0 { + compatible = "apple,n42-z2-multitouch"; + reg = <0>; + spi-max-frequency = <5000000>; + generation = <1>; + firmware-name = "apple/z2-n42.bin"; + apple,fll-mval = <0x17d3>; + apple,ref-clk-div-val = <2>; + apple,firmware-execute-address = <0x10003400>; + apple,calibration-address = <0x10009000>; + apple,scan-start-report = [9b 0b 0b 02]; + hv-supply = <&touch_ana>; + core-supply = <&touch_ldo>; + reset-gpios = <&gpio 231 0>; + cs-gpios = <&gpio 49 0>; + irq-gpios = <&gpio 201 1>; + touchscreen-inverted-y; + clocks = <&touchclk>; + }; + }; + + /* + * UART5 as 1-wire HDQ for the TI bq27540 gas gauge (live ADT + * uart5/gas-gauge, pin 44 = 0x0504, PMGR gate uart5_ps @ 0xf10110c). + * Delete the disabled UART5 serial node from s5l8950x.dtsi so there + * is no duplicate unit-address at @2a00000. + */ + /delete-node/ serial@2a00000; + + /* + * s5l8950x: the driver runs Apple's own HDQ transaction here (iBEC + * and AppleHDQGasGaugeControl): 0xC0 for a 0, an echo check, 0xF9 + * threshold, hi-lo-hi 16-bit reads. + */ + battery: hdq@2a00000 { + compatible = "apple,s5l8950x-hdq-battery", "apple,s5l8940x-hdq-battery"; + reg = <0x2a00000 0x1000>; + clocks = <&uart5_ps>; + status = "okay"; + }; + + /* + * EHCI host for BCM4334 B3 Murata Wi-Fi (M=HEIN m=6.9 V=m, 0a5c:bd1a, + * centennial.trx + heineken-m-ms.txt) on HSIC port 4. + * Port 2 (cellular baseband) is intentionally masked out. + */ + ehci: usb@6400000 { + compatible = "apple,s5l8950x-ehci", "apple,s5l8940x-ehci", + "generic-ehci"; + reg = <0x6400000 0x10000>; + interrupts = <12>; + phys = <&otgphy 1>; + phy-names = "usb"; + apple,hsic-ports = <0x8>; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + /* + * brcm,bcm4329-fmac lets brcmfmac read local-mac-address from + * this node; m1n1 fills it from the ADT's mac-address-wifi0 + * through the wifi0 alias (otherwise the NVRAM placeholder + * 02:00:00:00:00:00 is the MAC). + */ + wifi: wifi@4 { + compatible = "usba5c,bd1a", "brcm,bcm4329-fmac"; + reg = <4>; + /* Live ADT wlan: HOST_READY 0x0606 = 54, DEVICE_READY 0x0701 = 57 */ + apple,host-ready-gpios = <&gpio 54 0>; + apple,device-ready-gpios = <&gpio 57 0>; + }; + }; + + /* + * Display pipeline (live AppleMobileCLCD: 640x1136, stride 2560, BGRA / + * a8r8g8b8, PixelClock 102.6 MHz; lcd,pinot reset GPIO 202, LDO 13). + * First-stage display uses framebuffer0 (simple-framebuffer) in + * s5l8950x.dtsi; CLCD and MIPI-DSIM stay disabled until KMS bring-up. + */ + clcd: display-controller@a104000 { + compatible = "apple,s5l8950x-clcd", "apple,s5l8940x-clcd"; + reg = <0xa104000 0x1000>, <0x9200000 0x1000>; + status = "disabled"; + }; + + dsim: dsi@9500000 { + compatible = "apple,s5l8950x-mipi-dsim", "apple,s5l8940x-mipi-dsim", + "samsung,mipi-dsim-1"; + reg = <0x9500000 0x1000>; + interrupts = <41>; + #address-cells = <1>; + #size-cells = <0>; + apple,lanes = <4>; + status = "disabled"; + + panel@0 { + compatible = "apple,lcd-pinot"; + reg = <0>; + status = "disabled"; + }; + }; +}; diff --git a/arch/arm/boot/dts/apple/s5l8950x.dtsi b/arch/arm/boot/dts/apple/s5l8950x.dtsi index 84d98ff6de92ff..30ea2cbef07d5e 100644 --- a/arch/arm/boot/dts/apple/s5l8950x.dtsi +++ b/arch/arm/boot/dts/apple/s5l8950x.dtsi @@ -217,6 +217,59 @@ status = "disabled"; }; + /* + * H2FMI gates: ADT flash-controller0 clock-gates <0x14 0x16 0x15 + * 0x17 0x18>, which the pmgr device-clocks table maps to + * power-state indices 0x32..0x36 ("FMI0", "FMI0-BCH", "FMI1", + * "FMI1-BCH", "FMI-DLL"). Disabled with the flash controller: + * only a board that enables the controller enables these, so + * an unclaimed gate is never switched off by clk_disable_unused. + */ + fmi0_ps: fmi0-ps@f1010c8 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010c8 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "fmi0_ps"; + status = "disabled"; + }; + + fmi0_bch_ps: fmi0-bch-ps@f1010cc { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010cc 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "fmi0_bch_ps"; + status = "disabled"; + }; + + fmi1_ps: fmi1-ps@f1010d0 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010d0 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "fmi1_ps"; + status = "disabled"; + }; + + fmi1_bch_ps: fmi1-bch-ps@f1010d4 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010d4 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "fmi1_bch_ps"; + status = "disabled"; + }; + + fmi_dll_ps: fmi-dll-ps@f1010d8 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf1010d8 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "fmi_dll_ps"; + status = "disabled"; + }; + uart1_ps: uart1-ps@f1010fc { compatible = "apple,s5l8940x-pmgr-clk-gate"; #clock-cells = <0>; @@ -380,6 +433,33 @@ status = "disabled"; }; + /* + * NAND: H2FMI driving two PPN packages, CE0 of each bus (ADT + * ce-bitmap 0x101, channels ce-map 0 and 8). ADT + * /arm-io/flash-controller0 ("fmi,s5l8950x", "fmi,s5l8920x"): + * FMI, FMC, ECC for bus 0, then bus 1, IRQs 33 and 34. The + * ADT's seventh window, the FMI DLL at 0x1000000 (0x10 bytes), + * is not used by the driver and not mapped. + * + * Read-only: "apple,s5l8950x-fmi" has no program or erase path + * and never resets the packages to SDR. Never add apple,s5l8940x-fmi as + * a fallback; a driver without the S5L8950X entry would bind it + * writable. + */ + nand: flash-controller@1200000 { + compatible = "apple,s5l8950x-fmi"; + reg = <0x1200000 0x1000>, /* FMI0 */ + <0x1240000 0x1000>, /* FMC0 */ + <0x1280000 0x1000>, /* ECC0 */ + <0x1300000 0x1000>, /* FMI1 */ + <0x1340000 0x1000>, /* FMC1 */ + <0x1380000 0x1000>; /* ECC1 */ + interrupts = <33>, <34>; + clocks = <&fmi0_ps>, <&fmi0_bch_ps>, <&fmi1_ps>, + <&fmi1_bch_ps>, <&fmi_dll_ps>; + status = "disabled"; + }; + /* * USB OTG PHY (ADT /arm-io/otgphyctrl + /arm-io/usb-complex). * Uses S5L8950X PMGR gates (+0x1084/+0x1088/+0x109c) and diff --git a/arch/arm/configs/orchard_applearmv7_defconfig b/arch/arm/configs/orchard_applearmv7_defconfig index 8bb1bdafb0cf49..55d68b10e74f18 100644 --- a/arch/arm/configs/orchard_applearmv7_defconfig +++ b/arch/arm/configs/orchard_applearmv7_defconfig @@ -522,6 +522,7 @@ CONFIG_BACKLIGHT_CLASS_DEVICE=y CONFIG_BACKLIGHT_PWM=y CONFIG_BACKLIGHT_AS3711=y CONFIG_BACKLIGHT_APPLE_PMU_WLED=y +CONFIG_BACKLIGHT_APPLE_LM3534=y CONFIG_BACKLIGHT_GPIO=y CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION=y CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y diff --git a/drivers/input/touchscreen/apple-z2.c b/drivers/input/touchscreen/apple-z2.c index 1e5bd28bafde71..b060b0895ef84f 100644 --- a/drivers/input/touchscreen/apple-z2.c +++ b/drivers/input/touchscreen/apple-z2.c @@ -91,6 +91,8 @@ struct hx_touch_data { struct delayed_work lost_work; unsigned lost_frame; const char *firmware_name; + bool raw_packet; + u32 ref_clk_div; u32 execute_addr; u32 cal_addr; u32 fll_mval; @@ -819,7 +821,7 @@ static int hx_touch_hbpp_upload(struct hx_touch_data *hxt, const u8 *packet, } static int hx_touch_hbpp_cal_data(struct hx_touch_data *hxt, - const struct firmware *cal) + const struct firmware *cal, u32 addr) { const u8 ack[] = { 0x1a, 0xa1 }; u8 *pkt = hxt->tx_data; @@ -832,7 +834,7 @@ static int hx_touch_hbpp_cal_data(struct hx_touch_data *hxt, padded = ALIGN(len, 4); put_unaligned_be32(0x18e13001, pkt); put_unaligned_be16(padded / 4, pkt + 4); - hx_touch_hbpp_put32(pkt + 6, hxt->cal_addr + off); + hx_touch_hbpp_put32(pkt + 6, addr + off); put_unaligned_be16(hx_touch_sum16(pkt + 4, 6), pkt + 10); sum = 0; @@ -926,7 +928,7 @@ static int hx_touch_n1_calibrate(struct hx_touch_data *hxt) u32 addr, value; } regs[] = { { 0x10003060, hxt->fll_mval }, /* FLL M value */ - { 0x1000305c, 0x20 }, /* reference clock divider */ + { 0x1000305c, hxt->ref_clk_div }, /* reference clock divider */ { 0x10003058, 6 }, { 0x10003000, 3 }, /* constant calibration */ { 0x10003518, 1 }, /* clk32 enable */ @@ -1076,6 +1078,32 @@ static int hx_touch_n1_start_scan(struct hx_touch_data *hxt) return 0; } +/* The bootloader can supply per-unit calibration directly in the DT. */ +static int hx_touch_n42_cal(struct hx_touch_data *hxt, const char *prop, + const char *name, u32 addr, size_t limit) +{ + struct device *dev = &hxt->spi->dev; + const struct firmware *file = NULL; + struct firmware blob = {}; + int len, ret; + + blob.data = of_get_property(dev->of_node, prop, &len); + if (blob.data && len > 0 && memchr_inv(blob.data, 0, len)) { + blob.size = len; + } else { + ret = firmware_request_nowarn(&file, name, dev); + if (ret) + return ret == -ENOENT ? 0 : ret; + blob = *file; + } + if (!blob.size || blob.size > limit) + ret = -EINVAL; + else + ret = hx_touch_hbpp_cal_data(hxt, &blob, addr); + release_firmware(file); + return ret; +} + static int hx_touch_n1_boot(struct hx_touch_data *hxt, const u8 *packet, size_t len, const struct firmware *cal) { @@ -1107,9 +1135,17 @@ static int hx_touch_n1_boot(struct hx_touch_data *hxt, const u8 *packet, ret = hx_touch_hbpp_upload(hxt, packet, len); if (ret) return ret; - /* P105 has no prox-calibration in its ADT. */ - if (cal) { - ret = hx_touch_hbpp_cal_data(hxt, cal); + if (hxt->raw_packet) { + ret = hx_touch_n42_cal(hxt, "apple,z2-prox-cal-blob", + "apple/z2-prox-cal.bin", 0x10009600, 128); + if (ret) + return ret; + ret = hx_touch_n42_cal(hxt, "apple,z2-cal-blob", "apple/z2-cal.bin", + hxt->cal_addr, 0x600); + if (ret) + return ret; + } else if (cal) { + ret = hx_touch_hbpp_cal_data(hxt, cal, hxt->cal_addr); if (ret) return ret; } @@ -1133,6 +1169,25 @@ static void hx_touch_boot_work(struct work_struct *work) ret = request_firmware(&fw, hxt->firmware_name, dev); if (ret) return; + if (hxt->raw_packet) { + ret = -EINVAL; + if (fw->size >= 20 && fw->size <= SZ_128K && + !memcmp(fw->data, "\x18\xe1\x30\x01", 4)) { + ret = hx_touch_power_on(hxt); + if (!ret) { + ret = hx_touch_n1_boot(hxt, fw->data, fw->size, NULL); + if (ret) + hx_touch_power_off(hxt); + } + } + release_firmware(fw); + if (ret) { + dev_err(dev, "touch controller boot failed: %pe\n", ERR_PTR(ret)); + return; + } + packet = NULL; + goto ready; + } len = hx_touch_decode_mtprops(fw, &packet); release_firmware(fw); if (len < 0) { @@ -1162,6 +1217,7 @@ static void hx_touch_boot_work(struct work_struct *work) goto release_cal; } + ready: mutex_lock(&hxt->mutex); hxt->ready = 1; hx_touch_read_all_reports(hxt); @@ -1245,6 +1301,9 @@ static int hx_touch_spi_probe(struct spi_device *spi) if (device_get_match_data(&spi->dev)) { struct device_node *np = spi->dev.of_node; + hxt->raw_packet = of_device_is_compatible(np, "apple,n42-z2-multitouch"); + hxt->ref_clk_div = 0x20; + of_property_read_u32(np, "apple,ref-clk-div-val", &hxt->ref_clk_div); if (of_property_read_string(np, "firmware-name", &hxt->firmware_name) || of_property_read_u32(np, "apple,firmware-execute-address", &hxt->execute_addr) || of_property_read_u32(np, "apple,calibration-address", &hxt->cal_addr) || @@ -1379,6 +1438,7 @@ static void hx_touch_spi_remove(struct spi_device *spi) static const bool hx_touch_n1 = true; static const struct of_device_id hx_touch_of_match[] = { + { .compatible = "apple,n42-z2-multitouch", .data = &hx_touch_n1 }, { .compatible = "apple,p105-z2-multitouch", .data = &hx_touch_n1 }, { .compatible = "apple,z2-multitouch" }, { }, @@ -1386,6 +1446,7 @@ static const struct of_device_id hx_touch_of_match[] = { MODULE_DEVICE_TABLE(of, hx_touch_of_match); static const struct spi_device_id hx_touch_spi_ids[] = { + { "n42-z2-multitouch" }, { "p105-z2-multitouch" }, { "z2-multitouch" }, { } diff --git a/drivers/mfd/apple-pmu-i2c.c b/drivers/mfd/apple-pmu-i2c.c index aa69d2e3626281..f9c3c9a36d535e 100644 --- a/drivers/mfd/apple-pmu-i2c.c +++ b/drivers/mfd/apple-pmu-i2c.c @@ -20,9 +20,74 @@ #include #include +struct apple_pmu_off_info { + u8 pre_zero_reg; /* written 0 first (WLED/boost disable) */ + u8 events_reg; /* read nevents bytes to clear pending events */ + u8 masks_reg; /* write nevents off_masks bytes */ + u8 nevents; + const u8 *off_masks; + u8 alarm_ctrl_reg; /* written 0x01 (no RTC wake alarm) */ + u8 zero_reg1; /* written 0 */ + u8 zero_reg2; /* optional second register written 0 */ + u8 boot_flags_reg; /* cleared with mask 0xd0 for iBoot */ + u8 standby_reg; /* ORed with 0x01 in atomic power-off */ +}; + struct apple_pmu_i2c_info { unsigned reg_bits, val_bits; unsigned gpio_cfg; /* GPIO n's configuration register: gpio_cfg + n */ + unsigned ngpio; + const struct apple_pmu_off_info *off; +}; + +#define APPLE_PMU_GPIO_WAKE 0x02 + +static const u8 d1946_off_masks[9] = { + 0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, +}; + +static const struct apple_pmu_off_info d1946_off_info = { + .pre_zero_reg = 0xd2, + .events_reg = 0x01, + .masks_reg = 0x12, + .nevents = ARRAY_SIZE(d1946_off_masks), + .off_masks = d1946_off_masks, + .alarm_ctrl_reg = 0x5b, + .zero_reg1 = 0xa1, + .boot_flags_reg = 0xaf, + .standby_reg = 0x1b, +}; + +/* + * D1972 (iPhone 5, S5L8950X, ADT "pmu,d1972"): 8-bit register addressing, + * 12 GPIOs at 0x68 + n (AppleD1972PMU vtable +0x3c4, 14G61 0x810e7678), and + * the same shutdown sequence as D1946 with shifted registers + * (AppleD1972PMU::_shutdownGated, 14G61 0x810e7a80): + * + * 0x7d = 0 pre-shutdown disable + * read 0x01..0x09 clear pending events + * 0x12..0x1a = mask table wake masks (0xae is at byte 5, not byte 6) + * 0x62 = 0x01 no RTC wake alarm (RTC counter is 0x64..0x67) + * 0xa2 = 0, 0xa4 = 0 + * 0xb0 &= ~0xd0 boot flags for iBoot + * 0x68..0x73: bit 1 off clear wake bit on all 12 input GPIOs (>= 0x60) + * 0x1b |= 1 PMU standby + */ +static const u8 d1972_off_masks[9] = { + 0x88, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, 0xff, +}; + +static const struct apple_pmu_off_info d1972_off_info = { + .pre_zero_reg = 0x7d, + .events_reg = 0x01, + .masks_reg = 0x12, + .nevents = ARRAY_SIZE(d1972_off_masks), + .off_masks = d1972_off_masks, + .alarm_ctrl_reg = 0x62, + .zero_reg1 = 0xa2, + .zero_reg2 = 0xa4, + .boot_flags_reg = 0xb0, + .standby_reg = 0x1b, }; static const struct apple_pmu_i2c_info apple_pmu_i2c_d2333_info = { .reg_bits = 16, .val_bits = 8 }; @@ -46,7 +111,12 @@ static const struct apple_pmu_i2c_info apple_pmu_i2c_chestnut_info = { .reg_bits * _setLDO's bounds check of 0x16. */ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1946_info = { - .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x61, + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x61, .ngpio = 18, + .off = &d1946_off_info, +}; +static const struct apple_pmu_i2c_info apple_pmu_i2c_d1972_info = { + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x68, .ngpio = 12, + .off = &d1972_off_info, }; /* * The iPad 3's D1974 (ADT "pmu,d1974") takes the same 8-bit addressing and @@ -56,75 +126,51 @@ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1946_info = { * D1946's power-off is not checked against this chip. */ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1974_info = { - .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x64, + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x64, .ngpio = 18, }; + /* - * Power-off, the way AppleD1946PMU::_shutdownGated does it (kernelcache - * 12H321, 0x80ecec80; vtable slot 0x3a4). In order: - * - * 0xd2 = 0 the backlight's strings off - * read 0x01..0x09 the event registers, which clears them, so - * a stale event does not wake it at once - * 0x12..0x1a = mask table the events allowed to wake it: iOS's table - * 0x5b = 0x01 no RTC wake alarm (0x41 with one) - * 0xa1 = 0 - * 0xaf &= ~0xd0 flags for iBoot; a restart sets 0x90 instead - * 0x61..0x72: bit 1 off wake off on every PMU GPIO configured as an - * input (config >= 0x60) -- "cannot turn off GPIO - * wakes" is iOS's message when this fails - * 0x1b |= 1 "pmu go stdby": the PMU drops every rail - * - * Read on this iPad under Linux before any of it: the masks all 0x00, 0x5b - * 0x03, 0xaf 0x40, 0x1b 0x00. iOS's other branch, an external shutdown - * function, needs function-external_shutdown in the ADT, which P105's PMU - * does not have. Hold starts the iPad again, into iOS. - * - * All of it but the last write runs in the power-off prepare stage, where - * regmap may still sleep; the last one runs with interrupts off, straight - * through the I2C adapter's atomic transfer. + * Power-off, the way AppleD1946PMU::_shutdownGated (12H321, 0x80ecec80) and + * AppleD1972PMU::_shutdownGated (14G61, 0x810e7a80) do it. All of it but the + * last write runs in the power-off prepare stage, where regmap may still sleep; + * the last one runs with interrupts off, straight through the I2C adapter's + * atomic transfer. */ -#define D1946_EVENTS 0x01 -#define D1946_EVENT_MASKS 0x12 -#define D1946_NEVENTS 9 -#define D1946_STANDBY 0x1b -#define D1946_ALARM_CTRL 0x5b -#define D1946_GPIO_CFG 0x61 -#define D1946_NGPIO 18 -#define D1946_GPIO_WAKE 0x02 -#define D1946_REG_A1 0xa1 -#define D1946_BOOT_FLAGS 0xaf -#define D1946_WLED_ENABLE 0xd2 - -static const u8 d1946_off_masks[D1946_NEVENTS] = { - 0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, -}; - -static int apple_pmu_d1946_power_off_prepare(struct sys_off_data *data) +static int apple_pmu_power_off_prepare(struct sys_off_data *data) { - struct regmap *map = ((struct apple_pmu_i2c *)data->cb_data)->regmap; + struct apple_pmu_i2c *pmu = data->cb_data; + const struct apple_pmu_i2c_info *info = pmu->info; + const struct apple_pmu_off_info *off = info->off; + struct regmap *map = pmu->regmap; unsigned int i, v; - regmap_write(map, D1946_WLED_ENABLE, 0); - for (i = 0; i < D1946_NEVENTS; i++) - regmap_read(map, D1946_EVENTS + i, &v); - for (i = 0; i < D1946_NEVENTS; i++) - regmap_write(map, D1946_EVENT_MASKS + i, d1946_off_masks[i]); - regmap_write(map, D1946_ALARM_CTRL, 0x01); - regmap_write(map, D1946_REG_A1, 0); - regmap_update_bits(map, D1946_BOOT_FLAGS, 0xd0, 0); - for (i = 0; i < D1946_NGPIO; i++) - if (!regmap_read(map, D1946_GPIO_CFG + i, &v) && v >= 0x60 && (v & D1946_GPIO_WAKE)) - regmap_write(map, D1946_GPIO_CFG + i, v & ~D1946_GPIO_WAKE); + if (off->pre_zero_reg) + regmap_write(map, off->pre_zero_reg, 0); + for (i = 0; i < off->nevents; i++) + regmap_read(map, off->events_reg + i, &v); + for (i = 0; i < off->nevents; i++) + regmap_write(map, off->masks_reg + i, off->off_masks[i]); + regmap_write(map, off->alarm_ctrl_reg, 0x01); + if (off->zero_reg1) + regmap_write(map, off->zero_reg1, 0); + if (off->zero_reg2) + regmap_write(map, off->zero_reg2, 0); + regmap_update_bits(map, off->boot_flags_reg, 0xd0, 0); + for (i = 0; i < info->ngpio; i++) + if (!regmap_read(map, info->gpio_cfg + i, &v) && v >= 0x60 && (v & APPLE_PMU_GPIO_WAKE)) + regmap_write(map, info->gpio_cfg + i, v & ~APPLE_PMU_GPIO_WAKE); return NOTIFY_DONE; } -static int apple_pmu_d1946_power_off(struct sys_off_data *data) +static int apple_pmu_power_off(struct sys_off_data *data) { - struct i2c_client *client = to_i2c_client(((struct apple_pmu_i2c *)data->cb_data)->dev); - int v = i2c_smbus_read_byte_data(client, D1946_STANDBY); + struct apple_pmu_i2c *pmu = data->cb_data; + const struct apple_pmu_off_info *off = pmu->info->off; + struct i2c_client *client = to_i2c_client(pmu->dev); + int v = i2c_smbus_read_byte_data(client, off->standby_reg); if (v >= 0) - i2c_smbus_write_byte_data(client, D1946_STANDBY, v | 0x01); + i2c_smbus_write_byte_data(client, off->standby_reg, v | 0x01); mdelay(1000); pr_emerg("apple-pmu-i2c: the PMU did not power off (standby %d)\n", v); return NOTIFY_DONE; @@ -224,7 +270,8 @@ static int apple_pmu_gpio_direction_input(struct gpio_chip *gc, unsigned int n) return apple_pmu_gpio_get_direction(gc, n) == GPIO_LINE_DIRECTION_IN ? 0 : -EPERM; } -static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_base) +static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_base, + unsigned int ngpio) { struct apple_pmu_gpio *g = devm_kzalloc(pmu->dev, sizeof(*g), GFP_KERNEL); struct gpio_chip *gc; @@ -238,7 +285,7 @@ static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_b gc->parent = pmu->dev; gc->owner = THIS_MODULE; gc->base = -1; - gc->ngpio = D1946_NGPIO; + gc->ngpio = ngpio; gc->can_sleep = true; gc->get_direction = apple_pmu_gpio_get_direction; gc->direction_input = apple_pmu_gpio_direction_input; @@ -250,6 +297,7 @@ static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_b static const struct of_device_id apple_pmu_i2c_of_match[] = { { .compatible = "apple,pmu-d1974", .data = &apple_pmu_i2c_d1974_info }, + { .compatible = "apple,pmu-d1972", .data = &apple_pmu_i2c_d1972_info }, { .compatible = "apple,pmu-d1946", .data = &apple_pmu_i2c_d1946_info }, { .compatible = "apple,pmu-d2333", .data = &apple_pmu_i2c_d2333_info }, { .compatible = "apple,pmu-chestnut", .data = &apple_pmu_i2c_chestnut_info }, @@ -276,6 +324,7 @@ static int apple_pmu_i2c_probe(struct i2c_client *i2c) info = of_id->data; pmu->dev = &i2c->dev; + pmu->info = info; i2c_set_clientdata(i2c, pmu); pmu->config.reg_bits = info->reg_bits; @@ -321,20 +370,20 @@ static int apple_pmu_i2c_probe(struct i2c_client *i2c) } } - if (info == &apple_pmu_i2c_d1946_info && of_device_is_system_power_controller(node)) { + if (info->off && of_device_is_system_power_controller(node)) { ret = devm_register_sys_off_handler(&i2c->dev, SYS_OFF_MODE_POWER_OFF_PREPARE, SYS_OFF_PRIO_DEFAULT, - apple_pmu_d1946_power_off_prepare, pmu); + apple_pmu_power_off_prepare, pmu); if (!ret) ret = devm_register_sys_off_handler(&i2c->dev, SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, - apple_pmu_d1946_power_off, pmu); + apple_pmu_power_off, pmu); if (ret) dev_warn(&i2c->dev, "no power-off: %d\n", ret); } - if (info->gpio_cfg && of_property_read_bool(node, "gpio-controller")) { - ret = apple_pmu_gpio_register(pmu, info->gpio_cfg); + if (info->gpio_cfg && info->ngpio && of_property_read_bool(node, "gpio-controller")) { + ret = apple_pmu_gpio_register(pmu, info->gpio_cfg, info->ngpio); if (ret) dev_warn(&i2c->dev, "no GPIOs: %d\n", ret); } diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c index d4877223288dbe..7fc0b9af70e832 100644 --- a/drivers/mtd/apple-sftl-core.c +++ b/drivers/mtd/apple-sftl-core.c @@ -63,6 +63,7 @@ enum sftl_block_role { * lanes in order, four logical pages per physical page. */ struct sftl_geometry { + bool n42_install; /* N42 16 GB installation layout */ u16 buses, caus, lanes; u16 blocks; /* physical blocks per CAU */ u16 pages; /* MLC pages per block */ @@ -86,6 +87,7 @@ struct sftl_geometry { * the device: it is read-only, while a 64 GB * checkpoint would persist a wrong acceptance. */ + bool equal_sequences; /* replay a record that repeats the previous sequence */ }; /* 64 GB Hynix PPN with 13G36 (iOS 9.3.5) on the media. */ @@ -140,10 +142,33 @@ static const struct sftl_geometry sftl_32gb = { .degrade_remnants = true, .lpp = 2, .reused_names = true, .moved_epochs = true, }; +/* + * iPhone 5 (N42) 16 GB SanDisk PPN, 12H321 on the media: the P105 16 GB + * layout (same 12H321 FTL) with 1056 instead of 1064 blocks + * per CAU. Read-only. Only buses, caus, blocks and pages are measured + * (live ADT flash-controller0/disk). subblocks, vfl_first, bootstrap and + * lpages are NOT measured: they assume the P105 tail (two VFL blocks at + * the top, seven blocks between them and the last superblock) moves down + * with the block count and that the exported size is the same 16 GB. The + * loader checks each against the media (VFL header +12/+14, member list, + * context cursor) and refuses on a mismatch, logging the media's value. + */ +static const struct sftl_geometry sftl_16gb_n42 = { + .n42_install = true, + .lpp = 4, + .buses = 2, .caus = 2, .lanes = 4, .blocks = 1056, .pages = 256, + .subblocks = 1046, .vfl_pages = 128, .vfl_first = 1054, + .bootstrap = { 1055, 1054 }, .nr_bootstrap = 2, + .map_batch = 16, + .disk_pages = 3906250, .lpages = 3906266, .summary_sequences = true, + .equal_sequences = true, +}; + static const struct sftl_geometry *const sftl_geometries[] = { &sftl_64gb, &sftl_16gb, &sftl_32gb, + &sftl_16gb_n42, }; struct sftl_run { @@ -211,6 +236,7 @@ struct apple_sftl { u8 stage_oob[APPLE_PPN_OOB_SIZE], move_oob[APPLE_PPN_OOB_SIZE]; struct mutex lock; /* serializes reads and the shared page buffer */ bool failed; + u32 nr_equal; /* journal records that repeated the previous sequence */ }; static u32 sftl_stride(const struct apple_sftl *sftl) @@ -555,8 +581,13 @@ static int sftl_vfl_members(struct apple_sftl *sftl) members[i].lane = row[2]; if (members[i].block < sftl->geo->vfl_first || members[i].block >= sftl->geo->blocks || - members[i].lane >= sftl->geo->lanes || row[3]) + members[i].lane >= sftl->geo->lanes || row[3]) { + dev_dbg(sftl->ppn->dev, + "VFL member %u: block %u lane %u (%02x), profile expects blocks %u..%u\n", + i, members[i].block, members[i].lane, row[3], + sftl->geo->vfl_first, sftl->geo->blocks - 1); return -EBADMSG; + } for (j = 0; j < i; j++) if (members[j].block == members[i].block && members[j].lane == members[i].lane) return -EBADMSG; @@ -707,8 +738,14 @@ static int sftl_vfl_load_refs(struct apple_sftl *sftl, struct sftl_vfl_scan *sca */ if (get_unaligned_le16(scan->sample + 10) != 16 || get_unaligned_le16(scan->sample + 12) != sftl->geo->subblocks || - get_unaligned_le16(scan->sample + 14) != sftl->geo->subblocks) + get_unaligned_le16(scan->sample + 14) != sftl->geo->subblocks) { + dev_dbg(sftl->ppn->dev, + "VFL header +10 %u, FTL superblocks %u, VFL superblocks %u; profile expects 16, %u\n", + get_unaligned_le16(scan->sample + 10), + get_unaligned_le16(scan->sample + 12), + get_unaligned_le16(scan->sample + 14), sftl->geo->subblocks); return -EOPNOTSUPP; + } for (lane = 0; lane < sftl->geo->lanes; lane++) { const struct sftl_vfl_slot *table; u16 ref = get_unaligned_le16(scan->sample + 0x200 + 32 * lane); @@ -1087,6 +1124,9 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec */ if (cursor != sftl->geo->lpages || !map_records || (native && map_records % sftl->geo->map_batch)) { + dev_dbg(sftl->ppn->dev, + "context covers %u logical pages in %u records (native %d); profile expects %u in batches of %u\n", + cursor, map_records, native, sftl->geo->lpages, sftl->geo->map_batch); if (short_range) return -EBADMSG; /* A partial program or a native aggregate that lost its tail does @@ -1717,6 +1757,45 @@ static bool sftl_context_gap(const struct apple_sftl *sftl, const struct sftl_re (journal->items[low + count - 1].stamp & SFTL_SEQUENCE) == next - 1; } +/* The first sequence of the block that holds a record: the order iOS + * replays blocks in. + */ +static u64 sftl_record_head(const struct apple_sftl *sftl, const u8 *heads, + const struct sftl_record *record) +{ + return sftl_sequence(heads + record->address / sftl_stride(sftl) * SFTL_META); +} + +static bool sftl_record_after(const struct apple_sftl *sftl, const u8 *heads, + const struct sftl_record *a, const struct sftl_record *b) +{ + u64 head_a = sftl_record_head(sftl, heads, a), head_b = sftl_record_head(sftl, heads, b); + + return head_a > head_b || (head_a == head_b && a->address > b->address); +} + +/* + * Records that share a sequence are put in the order iOS replays them: + * by the first sequence of their block, then by position in it, so the + * later block's record is applied last. sort() is not stable; the runs + * are short, so an insertion pass over the sorted array is enough. + */ +static void sftl_order_equal(const struct apple_sftl *sftl, const u8 *heads, + struct sftl_records *journal) +{ + u32 i, j; + + for (i = 1; i < journal->count; i++) { + struct sftl_record record = journal->items[i]; + u64 sequence = record.stamp & SFTL_SEQUENCE; + + for (j = i; j && (journal->items[j - 1].stamp & SFTL_SEQUENCE) == sequence && + sftl_record_after(sftl, heads, &journal->items[j - 1], &record); j--) + journal->items[j] = journal->items[j - 1]; + journal->items[j] = record; + } +} + static int sftl_journal(struct apple_sftl *sftl, const u8 *heads, struct sftl_records *journal) { u64 floor = sftl->checkpoint_sequence, expected = floor + 1; @@ -1826,14 +1905,20 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads, struct sftl_re } } sort(journal->items, journal->count, sizeof(*journal->items), sftl_record_compare, NULL); + if (sftl->geo->equal_sequences) + sftl_order_equal(sftl, heads, journal); for (i = 0; i < journal->count; i++) { const struct sftl_record *record = &journal->items[i]; + const struct sftl_record *prev = i ? &journal->items[i - 1] : NULL; u64 sequence = record->stamp & SFTL_SEQUENCE; ret = -EBADMSG; if (sequence == expected + sftl->lpp && sftl_take_torn(sftl, expected - 1)) expected = sequence; - if (sequence != expected++) + if (sftl->geo->equal_sequences && prev && + sequence == (prev->stamp & SFTL_SEQUENCE)) + sftl->nr_equal++; + else if (sequence != expected++) goto out; /* Context and summary chunks, and padding, map nothing. */ if (sftl_kind(record) == 0x1f || sftl_kind(record) == 0x1c || @@ -3298,6 +3383,7 @@ EXPORT_SYMBOL_GPL(apple_sftl_flush); void apple_sftl_get_info(struct apple_sftl *sftl, struct apple_sftl_info *info) { info->read_only = sftl->vfl_degraded || sftl->context_degraded || !sftl->geo->writer; + info->n42_install = sftl->geo->n42_install; info->logical_pages = sftl->geo->lpages; info->disk_pages = sftl->geo->disk_pages; info->nr_runs = sftl->nr_runs; diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index a266329fe5a417..9ce7420714cb0e 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -3519,10 +3519,16 @@ static void sftl_test_gc_preserves_last_sequences(struct kunit *test) #define T16_NEXT 6U /* first page follows the new block's summary */ #define T16_NEXT_LBA 3000U #define T16_REMNANT 7U /* a context block of an older format */ +#define T16_OPEN 6U /* open after the new block, all lanes */ +#define T16_OPEN_LBA 3000U +/* The same layout on the iPhone 5: 1056 blocks, 1046 superblocks. */ +#define N42_BLOCKS 1056U +#define N42_SUBBLOCKS 1046U struct t16_fixture { struct apple_ppn provider; struct apple_ppn_ops ops; + u16 blocks, subblocks; /* per CAU; the VFL is in the top two blocks */ u16 vfl_block; /* bank holding the VFL; the other is erased */ u16 range_records; /* range records in the context */ bool summary_own_span; /* the new block's span counts its summary */ @@ -3551,6 +3557,7 @@ struct t16_fixture { bool trim_bad_count; /* the first TRIM above the checkpoint claims too many ranges */ bool trim_last; /* omit ordinary records after the TRIM slots */ bool closed_trim; /* one TRIM after the data run in the new closed block */ + bool open_repeat; /* an open block starts on the summary's last sequence */ }; /* Fixed records before the ranges: the native base, statistics and tables. */ @@ -3569,15 +3576,15 @@ static unsigned int t16_lanes(u32 block) return block == T16_NEW ? 3 : T16_LANES; } -static u16 t16_usage(unsigned int lane, u32 block) +static u16 t16_usage(const struct t16_fixture *f, unsigned int lane, u32 block) { if (!block) return 0xb002; if (block == T16_NEW && lane == 2) return 0xbffe; - if (block >= 1062) + if (block >= f->blocks - 2U) return 0xbff1; - if (block >= T16_SUBBLOCKS) + if (block >= f->subblocks) return 0xb003; return 0; } @@ -3593,7 +3600,7 @@ static void t16_meta(u8 *meta, u8 kind, u8 flags, u64 sequence, u32 key, u32 hig static int t16_vfl(struct t16_fixture *f, unsigned int lane, u32 block, unsigned int page, u8 *data, u8 *oob, u8 *status) { - u16 group = block == 1063 ? 0 : 1; + u16 group = block == f->blocks - 1U ? 0 : 1; unsigned int i; if (block != f->vfl_block || page > T16_LANES + 1) @@ -3609,10 +3616,10 @@ static int t16_vfl(struct t16_fixture *f, unsigned int lane, u32 block, put_unaligned_le32(U32_MAX, data); put_unaligned_le32(T16_DISK, data + 4); put_unaligned_le32(0x100000, data + 8); - put_unaligned_le16(T16_SUBBLOCKS, data + 12); - put_unaligned_le16(T16_SUBBLOCKS, data + 14); + put_unaligned_le16(f->subblocks, data + 12); + put_unaligned_le16(f->subblocks, data + 14); for (i = 0; i < 8; i++) { - put_unaligned_le16(i < 4 ? 1063 : 1062, data + 0x100 + 4 * i); + put_unaligned_le16(f->blocks - (i < 4 ? 1 : 2), data + 0x100 + 4 * i); data[0x100 + 4 * i + 2] = i % 4; } for (i = 0; i < T16_LANES; i++) @@ -3620,9 +3627,9 @@ static int t16_vfl(struct t16_fixture *f, unsigned int lane, u32 block, return 0; } oob[1] = page - 1; - for (i = 0; i < T16_BLOCKS; i++) - put_unaligned_le16(t16_usage(page - 1, i), data + 2 * i); - memset(data + 2 * T16_BLOCKS, 0xff, APPLE_PPN_PAGE_SIZE - 2 * T16_BLOCKS); + for (i = 0; i < f->blocks; i++) + put_unaligned_le16(t16_usage(f, page - 1, i), data + 2 * i); + memset(data + 2 * f->blocks, 0xff, APPLE_PPN_PAGE_SIZE - 2 * f->blocks); return 0; } @@ -3644,8 +3651,8 @@ static void t16_context_record(struct t16_fixture *f, u32 index, u8 *payload, u8 continue; subtype = groups[i].subtype; start = (index - first) * groups[i].entries; - entries = start >= T16_SUBBLOCKS ? 0 : - min_t(u32, groups[i].entries, T16_SUBBLOCKS - start); + entries = start >= f->subblocks ? 0 : + min_t(u32, groups[i].entries, f->subblocks - start); break; } t16_meta(meta, 0x1f, subtype, T16_CONTEXT_BASE + index, 0, U32_MAX); @@ -3655,7 +3662,7 @@ static void t16_context_record(struct t16_fixture *f, u32 index, u8 *payload, u8 put_unaligned_le32(1, payload); put_unaligned_le32(T16_CONTEXT, payload + 4); } else if (index == 3 && !f->legacy) { - put_unaligned_le32(T16_SUBBLOCKS, payload + 4096 - 4); + put_unaligned_le32(f->subblocks, payload + 4096 - 4); } else if (subtype == 3) { for (i = 0; i < entries; i++) { u32 block = start + i; @@ -3887,6 +3894,17 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 put_unaligned_le32(T16_NEXT_LBA + slot, payload); t16_meta(meta, 1, 0, sequence, T16_NEXT_LBA + slot, sequence >> 16); } + /* + * The open block's first record repeats the last sequence the new + * block's summary counted; the next three continue from there. + */ + if (block == T16_OPEN && f->open_repeat && slot < 4) { + u64 sequence = t16_last(f) + slot; + + memset(payload, 'r', 4096); + put_unaligned_le32(T16_OPEN_LBA + slot, payload); + t16_meta(meta, 1, 0, sequence, T16_OPEN_LBA + slot, sequence >> 16); + } } static int t16_read_page(void *priv, unsigned int bus, unsigned int cau, @@ -3896,7 +3914,7 @@ static int t16_read_page(void *priv, unsigned int bus, unsigned int cau, struct t16_fixture *f = priv; unsigned int lane = cau * 2 + bus, width, index, chunk, i; - if (bus >= 2 || cau >= 2 || block >= T16_BLOCKS || + if (bus >= 2 || cau >= 2 || block >= f->blocks || page >= (context ? T16_PAGES / 2 : T16_PAGES)) return -EINVAL; memset(data, 0xff, APPLE_PPN_PAGE_SIZE); @@ -3904,12 +3922,12 @@ static int t16_read_page(void *priv, unsigned int bus, unsigned int cau, *status = 0x49; if (context) return t16_vfl(f, lane, block, page, data, oob, status); - if (block >= T16_SUBBLOCKS || t16_usage(lane, block)) + if (block >= f->subblocks || t16_usage(f, lane, block)) return 0; width = t16_lanes(block); /* Lanes of the superblock in order, the retired lane skipped. */ for (index = 0, i = 0; i < lane; i++) - if (!t16_usage(i, block)) + if (!t16_usage(f, i, block)) index++; for (chunk = 0; chunk < 4; chunk++) t16_slot(f, block, (page * width + index) * 4 + chunk, @@ -3930,6 +3948,8 @@ static struct t16_fixture *t16_fixture(struct kunit *test) .ops = &f->ops, .priv = f, .buses = 2, .caus = 2, .blocks = T16_BLOCKS, .pages = T16_PAGES, }; + f->blocks = T16_BLOCKS; + f->subblocks = T16_SUBBLOCKS; f->vfl_block = 1063; f->range_records = 32; f->new_block = true; @@ -4379,6 +4399,47 @@ static void sftl_test_16gb_context_rules(struct kunit *test) apple_sftl_free(sftl); } +/* + * On the iPhone 5 profile a record that repeats the previous sequence is + * replayed in block order and the journal goes on; the P105 16 GB profile + * still refuses the same medium. + */ +static void sftl_test_n42_repeated_sequence(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u64 last; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->legacy = true; + last = t16_last(f); + f->open_repeat = true; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); + + f->provider.blocks = N42_BLOCKS; + f->blocks = N42_BLOCKS; + f->subblocks = N42_SUBBLOCKS; + f->vfl_block = N42_BLOCKS - 1; + f->open_repeat = false; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, last); + apple_sftl_free(sftl); + + f->open_repeat = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_TRUE(test, info.read_only); + KUNIT_EXPECT_EQ(test, info.journal_sequence, last + 3); + t16_expect_page(test, sftl, T16_OPEN_LBA, 'r', T16_OPEN_LBA); + t16_expect_page(test, sftl, T16_OPEN_LBA + 3, 'r', T16_OPEN_LBA + 3); + t16_expect_page(test, sftl, T16_NEW_LBA + 3, 'n', T16_NEW_LBA + 3); + t16_expect_page(test, sftl, 0, 'o', 0); + apple_sftl_free(sftl); +} + static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_rotate_mounted), KUNIT_CASE(sftl_test_vfl_rotation_cuts), @@ -4454,6 +4515,7 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_16gb_trim_flag1), KUNIT_CASE(sftl_test_16gb_closed_trim_flag1), KUNIT_CASE(sftl_test_16gb_hidden_tail), + KUNIT_CASE(sftl_test_n42_repeated_sequence), {} }; diff --git a/drivers/mtd/apple-sftl.h b/drivers/mtd/apple-sftl.h index 6c16a6ae6fbdb8..205e7ce060def8 100644 --- a/drivers/mtd/apple-sftl.h +++ b/drivers/mtd/apple-sftl.h @@ -53,6 +53,7 @@ void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, const struct apple_sftl_checkpoint_ops *ops, void *priv); struct apple_sftl_info { + bool n42_install; bool read_only; u32 logical_pages; u32 disk_pages; diff --git a/drivers/mtd/devices/Kconfig b/drivers/mtd/devices/Kconfig index f4be1b409392be..9ed5f748179254 100644 --- a/drivers/mtd/devices/Kconfig +++ b/drivers/mtd/devices/Kconfig @@ -223,6 +223,10 @@ config MTD_APPLE_H2FMI reported geometry validates; the erase extent follows from that geometry. + On the A6 (S5L8950X, iPhone 5) the driver is read-only: no program, + erase or mutation-status command can be sent, nothing is offered + for writing, and the packages are never reset to SDR. + If unsure, say N. config MTD_APPLE_H2FMI_P105_CAPTURE diff --git a/drivers/mtd/devices/mtd-apple-h2fmi-test.c b/drivers/mtd/devices/mtd-apple-h2fmi-test.c index a8434b08c10af0..83be1db07ede1b 100644 --- a/drivers/mtd/devices/mtd-apple-h2fmi-test.c +++ b/drivers/mtd/devices/mtd-apple-h2fmi-test.c @@ -13,6 +13,11 @@ static const u32 ppn_64gb[] = { 8, 3, 2088, 12, 128, 64, 7, 1, 2, 16448, }; +/* iPhone 5 (N42) SanDisk package, from the live 12H321 ADT. */ +static const u32 ppn_n42[] = { + 2, 4, 1056, 11, 256, 128, 8, 1, 2, 16448, +}; + static void h2fmi_test_params(u8 *params, const u32 *fields) { int i; @@ -42,6 +47,34 @@ static void h2fmi_geometry_test(struct kunit *test) 70061654016ULL); } +/* The N42 profile parses, addresses like the P105 16 GB one and stays read-only. */ +static void h2fmi_n42_geometry_test(struct kunit *test) +{ + u8 params[H2FMI_PARAMS_SIZE]; + struct h2fmi_geometry g; + u32 row; + + h2fmi_test_params(params, ppn_n42); + KUNIT_ASSERT_EQ(test, h2fmi_parse_geometry(params, sizeof(params), &g), 0); + KUNIT_EXPECT_EQ(test, g.caus, 2U); + KUNIT_EXPECT_EQ(test, g.blocks, 1056U); + KUNIT_EXPECT_EQ(test, g.slc_pages, 128U); + KUNIT_EXPECT_FALSE(test, h2fmi_geometry_writable(&g)); + KUNIT_EXPECT_EQ(test, h2fmi_bad_block_bytes(&g), 132U); + KUNIT_ASSERT_EQ(test, h2fmi_page_row(&g, 1, 1055, 255, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0xc1fffU); + KUNIT_EXPECT_EQ(test, h2fmi_page_row(&g, 0, 1056, 0, &row), -EINVAL); + KUNIT_ASSERT_EQ(test, h2fmi_slc_row(&g, 1, 1055, 127, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0x8c1f7fU); + + h2fmi_test_params(params, ppn_16gb); + KUNIT_ASSERT_EQ(test, h2fmi_parse_geometry(params, sizeof(params), &g), 0); + KUNIT_EXPECT_TRUE(test, h2fmi_geometry_writable(&g)); + h2fmi_test_params(params, ppn_64gb); + KUNIT_ASSERT_EQ(test, h2fmi_parse_geometry(params, sizeof(params), &g), 0); + KUNIT_EXPECT_TRUE(test, h2fmi_geometry_writable(&g)); +} + static void h2fmi_invalid_geometry_test(struct kunit *test) { u8 params[H2FMI_PARAMS_SIZE]; @@ -290,6 +323,7 @@ static void h2fmi_sdr_facts_test(struct kunit *test) static struct kunit_case h2fmi_test_cases[] = { KUNIT_CASE(h2fmi_raw_write_ownership_test), KUNIT_CASE(h2fmi_geometry_test), + KUNIT_CASE(h2fmi_n42_geometry_test), KUNIT_CASE(h2fmi_invalid_geometry_test), KUNIT_CASE(h2fmi_row_test), KUNIT_CASE(h2fmi_erase_row_test), diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.c b/drivers/mtd/devices/mtd-apple-h2fmi.c index 1a4fd736c76621..04b4820c149af2 100644 --- a/drivers/mtd/devices/mtd-apple-h2fmi.c +++ b/drivers/mtd/devices/mtd-apple-h2fmi.c @@ -30,6 +30,8 @@ * Logical-address translation and allocation belong to the FTL above. */ +#include +#include #include #include #include @@ -43,12 +45,38 @@ #include #include #include +#include #include +#include #include #include #include "mtd-apple-h2fmi.h" +/* + * Per-SoC policy. read_only: no program, erase or mutation-status command + * is ever sent, no MTD is writable and SFTL gets no program or erase + * callback. no_sdr_reset: the packages are never reset to SDR (NAND + * reset 0xff, set feature 0xef), neither at probe nor to recover from a + * failed transaction. The driver only + * runs on the DDR interface iBoot leaves. + */ +struct h2fmi_soc { + bool read_only; + bool no_sdr_reset; + bool clean_by_status; /* status 49 alone means erased (read-only only) */ +}; + +/* A5 (P105): the measured platform, writes and SDR recovery as before. */ +static const struct h2fmi_soc h2fmi_s5l8940x = { }; + +/* A6 (iPhone 5, N42): read-only bring-up. */ +static const struct h2fmi_soc h2fmi_s5l8950x = { + .read_only = true, + .no_sdr_reset = true, + .clean_by_status = true, +}; + #define H2FMI_BUSES 2 #define H2FMI_PAGE_MASK (H2FMI_PAGE_DATA - 1) @@ -176,8 +204,14 @@ struct h2fmi { struct platform_device *sftl; struct apple_ppn_ops ppn_ops; struct apple_ppn_access access; + struct clk_bulk_data *clks; + int nclks; bool reset_needed; /* a transaction failed; reset before the next */ unsigned int resets; + bool read_only; /* no program/erase command ever reaches a package */ + bool sdr_allowed; /* the packages may be reset to SDR */ + bool clean_by_status; /* a 49 page is erased whatever its data phase held */ + unsigned int clean_logged; }; /* @@ -208,6 +242,20 @@ static int h2fmi_cmd_write(struct h2fmi *fmi, struct h2fmi_bus *b, u8 op, u8 op2 { int i; + /* + * Read-only: program (8A, 17), its status (79) and erase (6A D7) + * never leave the controller. Without SDR permission neither does + * the reset (FF) or set feature (EF, E7) the SDR bring-up needs. + */ + if (fmi->read_only && (op == 0x8a || op == 0x17 || op == 0x79 || op == 0x6a)) { + dev_err(fmi->dev, "read-only: refusing NAND opcodes %02x %02x\n", op, op2); + return -EROFS; + } + if (!fmi->sdr_allowed && (op == 0xff || op == 0xef || op == 0xe7)) { + dev_err(fmi->dev, "no SDR reset on this platform: refusing NAND opcodes %02x %02x\n", + op, op2); + return -EPERM; + } for (i = 0; i < ARRAY_SIZE(h2fmi_ops); i++) if (h2fmi_ops[i][0] == op && h2fmi_ops[i][1] == op2) { writel(op | op2 << 8, b->fmc + FMC_CMD); @@ -712,6 +760,13 @@ static int h2fmi_recover(struct h2fmi *fmi) mutex_lock_nested(&fmi->bus[1].lock, SINGLE_DEPTH_NESTING); if (fmi->bus[0].failed || fmi->bus[1].failed) { ret = -EIO; + } else if (fmi->reset_needed && !fmi->sdr_allowed) { + /* Recovery is a reset to SDR, which this platform does not do. */ + for (i = 0; i < H2FMI_BUSES; i++) + fmi->bus[i].failed = true; + dev_err(fmi->dev, "PPN transaction failed and no reset is allowed, stopping\n"); + fmi->reset_needed = false; + ret = -EIO; } else if (fmi->reset_needed) { ret = fmi->resets++ < H2FMI_MAX_RESETS ? h2fmi_sdr_bringup(fmi, &fmi->bus[0].geometry) : -EIO; @@ -841,9 +896,14 @@ static int h2fmi_read_geometry(struct h2fmi *fmi, struct h2fmi_bus *b) b->page, H2FMI_PARAMS_SIZE, &status); if (!ret && status != PPN_ST_GOOD) ret = -EIO; - if (!ret) + if (!ret) { ret = h2fmi_parse_geometry(b->page, H2FMI_PARAMS_SIZE, &b->geometry); + /* Keep what the package said, so a new profile can be added. */ + if (ret) + dev_err(fmi->dev, "unsupported PPN parameters: %*ph\n", + 64, b->page); + } mutex_unlock(&b->lock); return ret; } @@ -1012,6 +1072,8 @@ static int h2fmi_program_page(struct h2fmi *fmi, struct h2fmi_bus *b, u8 status, *expected; int ret; + if (fmi->read_only) + return -EROFS; if (!data || !oob) return -EINVAL; ret = h2fmi_page_row(&b->geometry, cau, block, page, &row); @@ -1099,6 +1161,40 @@ static int h2fmi_mtd_write_oob(struct mtd_info *mtd, loff_t to, return ret; } +/* + * A page the package reports clean (49) whose data phase was not all FF. + * On the A5 an erased page always read back FF, and the driver insists on + * it, since erase and program verification rely on it. The iPhone 5's + * SanDisk package does not: its first boot read 49 with other bytes on a + * block the VFL lists. iBEC (N42 12H321, single-page PPN read at + * 0xbff09810, status switch at 0xbff09914) never looks at the data of a 49 + * page: 40 is good, 42 good with a refresh note, 43..45 an error, 49 + * "clean". Where clean_by_status is set (read-only S5L8950X only), do the + * same: the page is erased and its data and metadata read as FF. The first + * few such pages are logged with what the data phase really held. + * Caller holds b->lock. + */ +static bool h2fmi_clean_by_status(struct h2fmi *fmi, struct h2fmi_bus *b, u32 row) +{ + unsigned int i, ff = 0, zero = 0; + + if (!fmi->clean_by_status) + return false; + if (fmi->clean_logged < 8) { + fmi->clean_logged++; + for (i = 0; i < H2FMI_PAGE_RAW; i++) { + ff += b->page[i] == 0xff; + zero += !b->page[i]; + } + dev_dbg(fmi->dev, + "row %08x status 49 with data: %u FF, %u 00 of %u bytes, data %16ph meta %16ph; taken as erased (%u/8)\n", + row, ff, zero, H2FMI_PAGE_RAW, b->page, b->page + H2FMI_CHUNK_LO, + fmi->clean_logged); + } + memset(b->page, 0xff, H2FMI_PAGE_RAW); + return true; +} + static int h2fmi_read_page_at(struct mtd_info *mtd, loff_t from, u8 *data, u8 *oob, u8 *stp) { @@ -1128,7 +1224,8 @@ static int h2fmi_read_page_at(struct mtd_info *mtd, loff_t from, b->page, H2FMI_PAGE_RAW, &st); if (!ret && h2fmi_page_status(st) >= 0) { if (st == PPN_ST_ERASED && - memchr_inv(b->page, 0xff, H2FMI_PAGE_RAW)) + memchr_inv(b->page, 0xff, H2FMI_PAGE_RAW) && + !h2fmi_clean_by_status(fmi, b, row)) ret = -EBADMSG; else h2fmi_split_page(b->page, data, oob); @@ -1168,7 +1265,8 @@ static int h2fmi_ppn_read(void *priv, unsigned int bus, unsigned int cau, ret = h2fmi_read_transaction(fmi, b, 0x0a, 0x37, row, 4, b->page, H2FMI_PAGE_RAW, status); if (!ret && h2fmi_page_status(*status) >= 0) { - if (*status == PPN_ST_ERASED && memchr_inv(b->page, 0xff, H2FMI_PAGE_RAW)) + if (*status == PPN_ST_ERASED && memchr_inv(b->page, 0xff, H2FMI_PAGE_RAW) && + !h2fmi_clean_by_status(fmi, b, row)) ret = -EIO; else h2fmi_split_page(b->page, data, oob); @@ -1255,6 +1353,8 @@ static int h2fmi_ppn_erase(void *priv, unsigned int bus, unsigned int cau, u8 status; int ret; + if (fmi->read_only) + return -EROFS; if (bus >= H2FMI_BUSES) return -EINVAL; b = &fmi->bus[bus]; @@ -1302,9 +1402,13 @@ static int h2fmi_register_sftl(struct h2fmi *fmi) /* The recovery supports the measured 64 GB and 16 GB profiles. */ if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL) || !((g->caus == 8 && g->blocks == 2088 && g->pages == 128 && g->slc_pages == 64) || - (g->caus == 2 && g->blocks == 1064 && g->pages == 256 && g->slc_pages == 128))) + (g->caus == 2 && g->blocks == 1064 && g->pages == 256 && g->slc_pages == 128) || + (g->caus == 2 && g->blocks == 1056 && g->pages == 256 && g->slc_pages == 128))) return 0; fmi->ppn_ops.read_page = h2fmi_ppn_read; + /* A read-only provider has neither program nor erase: SFTL stays read-only. */ + if (fmi->read_only) + goto publish; fmi->ppn_ops.program_page = h2fmi_ppn_program; /* * Erase follows the discovered geometry. SFTL verifies every erased @@ -1316,6 +1420,7 @@ static int h2fmi_register_sftl(struct h2fmi *fmi) dev_info(fmi->dev, "PPN geometry %u CAUs x %u blocks x %u pages has no valid erase extent; reclamation stays off\n", g->caus, g->blocks, g->pages); +publish: fmi->sftl = platform_device_register_data(fmi->dev, "apple-sftl", PLATFORM_DEVID_AUTO, &provider, sizeof(provider)); @@ -1479,8 +1584,9 @@ static int h2fmi_setup_mtd(struct h2fmi *fmi, int idx) mtd->type = MTD_NANDFLASH; /* Page metadata is MTD OOB. Pages are programmed whole, data and OOB - * together; there is no erase. */ - mtd->flags = MTD_WRITEABLE | MTD_NO_ERASE; + * together; there is no erase. A read-only instance has no write + * method at all, so the MTD core answers every write with an error. */ + mtd->flags = fmi->read_only ? MTD_NO_ERASE : MTD_WRITEABLE | MTD_NO_ERASE; mtd->erasesize = g->pages * H2FMI_PAGE_DATA; mtd->size = (u64)g->blocks * mtd->erasesize; mtd->writesize = H2FMI_PAGE_DATA; @@ -1498,26 +1604,72 @@ static int h2fmi_setup_mtd(struct h2fmi *fmi, int idx) /* _read_oob only: add_mtd_device() rejects a driver that has both. */ mtd->_read_oob = h2fmi_mtd_read_oob; - mtd->_write_oob = h2fmi_mtd_write_oob; + if (!fmi->read_only) + mtd->_write_oob = h2fmi_mtd_write_oob; mtd->_block_isbad = h2fmi_mtd_block_isbad; return mtd_device_register(mtd, NULL, 0); } +/* + * The PMGR gates listed in the node's clocks (S5L8950X: FMI0, FMI0-BCH, + * FMI1, FMI1-BCH, FMI-DLL) are switched on before the first register + * access: on the A6 a read of a gated block stalls the bus without an + * abort. Each gate's state as iBoot left it is logged. A node without + * clocks (P105) is unchanged: nothing is switched. + * + * They are never switched off again, not after a failed probe and not on + * unbind: the ADT lists the same ids as power gates, so switching them off + * would drop the DDR and DLL state iBoot trained, which only a reboot + * restores, and would turn any later access into a bus stall. The enable + * count is kept, so clk_disable_unused leaves them alone too. + */ +static int h2fmi_clocks(struct h2fmi *fmi) +{ + int i, ret; + + fmi->nclks = devm_clk_bulk_get_all(fmi->dev, &fmi->clks); + if (fmi->nclks < 0) + return dev_err_probe(fmi->dev, fmi->nclks, "PMGR gates\n"); + if (!fmi->nclks) + return 0; + for (i = 0; i < fmi->nclks; i++) + dev_dbg(fmi->dev, "PMGR gate %s was %s\n", + __clk_get_name(fmi->clks[i].clk), + __clk_is_enabled(fmi->clks[i].clk) ? "on" : "off"); + ret = clk_bulk_prepare_enable(fmi->nclks, fmi->clks); + if (ret) + return dev_err_probe(fmi->dev, ret, "enabling the PMGR gates\n"); + return 0; +} + static int h2fmi_probe(struct platform_device *pdev) { + const struct h2fmi_soc *soc; struct h2fmi *fmi; int i, ret; BUILD_BUG_ON(H2FMI_PAGE_DATA != (1 << H2FMI_PAGE_SHIFT)); BUILD_BUG_ON(H2FMI_PAGE_RAW != 16448); + soc = of_device_get_match_data(&pdev->dev); + if (!soc) + return -ENODEV; fmi = devm_kzalloc(&pdev->dev, sizeof(*fmi), GFP_KERNEL); if (!fmi) return -ENOMEM; fmi->dev = &pdev->dev; + /* Both guards hold from the first command on. */ + fmi->read_only = soc->read_only; + fmi->sdr_allowed = !soc->no_sdr_reset; + /* Only a driver that can never program may take the status alone. */ + fmi->clean_by_status = soc->clean_by_status && fmi->read_only; apple_ppn_access_init(&fmi->access); + ret = h2fmi_clocks(fmi); + if (ret) + return ret; + /* Six regions, in the ADT's order: FMI, FMC, ECC for bus 0, then bus 1. */ for (i = 0; i < H2FMI_BUSES; i++) { struct h2fmi_bus *b = &fmi->bus[i]; @@ -1538,8 +1690,9 @@ static int h2fmi_probe(struct platform_device *pdev) return -ENOMEM; /* iBoot read the NAND to boot and leaves the block clocked and - * in DDR. Nothing here turns the clocks on. If the interface - * is not in DDR, the packages are reset and run in SDR. */ + * in DDR. Only gates named in the node are switched on (see + * h2fmi_clocks()). If the interface is not in DDR, the packages + * are reset and run in SDR where the platform allows it. */ dev_info(&pdev->dev, "bus %d: FMC_ON=0x%08x IF_CTRL=0x%08x\n", i, readl(b->fmc + FMC_ON), readl(b->fmc + FMC_IF_CTRL)); } @@ -1547,6 +1700,9 @@ static int h2fmi_probe(struct platform_device *pdev) /* Without iBoot's DDR state, bring both packages up in SDR first. */ if (readl(fmi->bus[0].fmc + FMC_ON) != FMC_ON_DDR || readl(fmi->bus[1].fmc + FMC_ON) != FMC_ON_DDR) { + if (!fmi->sdr_allowed) + return dev_err_probe(&pdev->dev, -ENODEV, + "interface not in DDR and no SDR reset allowed\n"); mutex_lock(&fmi->bus[0].lock); mutex_lock_nested(&fmi->bus[1].lock, SINGLE_DEPTH_NESTING); ret = h2fmi_sdr_bringup(fmi, NULL); @@ -1567,6 +1723,8 @@ static int h2fmi_probe(struct platform_device *pdev) return dev_err_probe(&pdev->dev, -EINVAL, "PPN package geometries disagree\n"); } + if (!h2fmi_geometry_writable(&fmi->bus[0].geometry)) + fmi->read_only = true; fmi->ndies = H2FMI_BUSES * fmi->bus[0].geometry.caus; fmi->mtd = devm_kcalloc(&pdev->dev, fmi->ndies, sizeof(*fmi->mtd), GFP_KERNEL); fmi->die = devm_kcalloc(&pdev->dev, fmi->ndies, sizeof(*fmi->die), GFP_KERNEL); @@ -1592,9 +1750,10 @@ static int h2fmi_probe(struct platform_device *pdev) } platform_set_drvdata(pdev, fmi); - dev_info(&pdev->dev, "%u CAUs, %u blocks of %u KiB each\n", + dev_info(&pdev->dev, "%u CAUs, %u blocks of %u KiB each%s\n", fmi->ndies, fmi->bus[0].geometry.blocks, - fmi->mtd[0].erasesize / 1024); + fmi->mtd[0].erasesize / 1024, + fmi->read_only ? ", read-only" : ""); return 0; } @@ -1610,7 +1769,9 @@ static void h2fmi_remove(struct platform_device *pdev) } static const struct of_device_id h2fmi_of_match[] = { - { .compatible = "apple,s5l8940x-fmi" }, + { .compatible = "apple,s5l8940x-fmi", .data = &h2fmi_s5l8940x }, + /* No fallback to s5l8940x-fmi in the DT: an older driver would bind it writable. */ + { .compatible = "apple,s5l8950x-fmi", .data = &h2fmi_s5l8950x }, { } }; MODULE_DEVICE_TABLE(of, h2fmi_of_match); diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.h b/drivers/mtd/devices/mtd-apple-h2fmi.h index 0639a2b0d95ac0..515a08ea19b76c 100644 --- a/drivers/mtd/devices/mtd-apple-h2fmi.h +++ b/drivers/mtd/devices/mtd-apple-h2fmi.h @@ -73,9 +73,34 @@ static inline int h2fmi_parse_geometry(const u8 *params, size_t len, *geometry = g; return 0; } + /* + * iPhone 5 (N42) SanDisk 1Xnm64GeX2, PPN 1.5.5: the P105 16 GB + * address layout with 1056 instead of 1064 blocks per CAU (live + * 12H321 ADT flash-controller0/disk: caus 2, cau-bits 4, + * blocks-per-cau 1056, block-bits 11, page-address-bits 8, 256 MLC + * and 128 SLC pages, page-size 16448). Read-only: see + * h2fmi_geometry_writable(). + */ + if (g.caus == 2 && g.cau_bits == 4 && g.blocks == 1056 && + g.block_bits == 11 && g.pages == 256 && g.slc_pages == 128 && + g.page_bits == 8) { + *geometry = g; + return 0; + } return -EOPNOTSUPP; } +/* + * Program and erase are offered only on the two P105 profiles they were + * measured on. Any other accepted profile is read-only whatever the + * platform compatible says. + */ +static inline bool h2fmi_geometry_writable(const struct h2fmi_geometry *g) +{ + return (g->caus == 2 && g->blocks == 1064) || + (g->caus == 8 && g->blocks == 2088); +} + static inline int h2fmi_page_row(const struct h2fmi_geometry *g, unsigned int cau, unsigned int block, unsigned int page, u32 *row) diff --git a/drivers/power/supply/apple-hdq-battery.c b/drivers/power/supply/apple-hdq-battery.c index 785bc0072f11a1..3ae2a1080c7efc 100644 --- a/drivers/power/supply/apple-hdq-battery.c +++ b/drivers/power/supply/apple-hdq-battery.c @@ -19,11 +19,28 @@ * Only reads (command bit 7 clear) are ever sent: register numbers are the * bq27541 family's standard commands, 16 bits as two 8-bit reads. * README ("Battery gauge") has the summary. + * + * The iPhone 5 (S5L8950X, N42) uses the transaction exactly as Apple writes + * it, in its iBEC (12H321, 0xbff143b4 / 0xbff14554 / 0xbff14248) and in iOS + * (AppleHDQGasGaugeControl 109.30.4: _sendBreak, _hdqRead8, hdqRead16): + * + * - break: UCON bit 4 set, 200 us, cleared by hand, 40 us, RX drained; + * - a 0 is sent as 0xC0 (start + six low bits, ~121 us), a 1 as 0xFE; + * - the 8 echoed frames are read back one by one and must equal what was + * sent, then 8 more are the gauge's: a frame of 0xF9 or more is a 1. + * Frames with a receive error (UERSTAT) are dropped, as iBEC does; + * - one 8-bit read is tried up to 4 times, 70 us apart (iOS); + * - a 16-bit value is read hi, lo, hi and taken only when both hi reads + * agree, up to 10 times (iBEC and iOS alike). + * + * That path is selected by "apple,s5l8950x-hdq-battery"; the A5 boards keep + * the original one unchanged. */ #include #include #include #include +#include #include #include #include @@ -36,6 +53,7 @@ #define UFCON 0x08 #define UTRSTAT 0x10 #define UTRSTAT_RXRDY BIT(0) /* Apple's UFSTAT layout differs from Samsung's */ +#define UERSTAT 0x14 /* error of the frame at the RX FIFO head */ #define UTXH 0x20 #define URXH 0x24 #define UBRDIV 0x28 @@ -60,6 +78,39 @@ #define BQ_SOC 0x2c /* % */ #define BQ_DCAP 0x3c /* mAh */ +struct hdq_variant { + bool apple_txn; /* iBEC/iOS transaction (S5L8950X) */ +}; + +static const struct hdq_variant hdq_s5l8940x = { .apple_txn = false }; +static const struct hdq_variant hdq_s5l8950x = { .apple_txn = true }; + +/* Apple transaction (S5L8950X path) */ +#define HDQ_TX1 0xFE +#define HDQ_TX0 0xC0 +#define HDQ_RX1_MIN 0xF9 +#define HDQ_BREAK_US 200 +#define HDQ_BREAK_REC_US 40 +#define HDQ_READ8_TRIES 4 +#define HDQ_READ8_GAP_US 70 +#define HDQ_READ16_TRIES 10 +#define HDQ_TXN_DEADLINE_US 10000 /* 16 frames are ~4 ms */ +#define HDQ_TRACE_LEN 24 /* 16 frames + dropped error frames */ +#define HDQ_STALE_OK (30 * HZ) /* last good values stay served */ + +/* the last failed (or rejected) 8-bit transaction, for the log */ +struct hdq_trace { + u8 reg; + u8 n; + const char *why; + u8 rx[HDQ_TRACE_LEN]; + u8 err[HDQ_TRACE_LEN]; +}; + +struct hdq_regs { + int soc, volt, ai, temp, rm, fcc, flags, cyct, dcap; +}; + struct hdq_battery { struct device *dev; void __iomem *base; @@ -68,6 +119,12 @@ struct hdq_battery { unsigned long stamp; /* jiffies of the last refresh */ int soc, volt, ai, temp, rm, fcc, flags, cyct, dcap; bool valid; + /* S5L8950X path only */ + const struct hdq_variant *var; + unsigned long fail_stamp; /* jiffies of the last failed refresh */ + int fail_ret; + unsigned int fails, retries; + struct hdq_trace trace; }; static void hdq_drain(struct hdq_battery *b) @@ -120,6 +177,235 @@ static int hdq_read16(struct hdq_battery *b, u8 reg) return -EIO; } +/* --- S5L8950X: Apple's transaction ------------------------------------- */ + +static void hdq_apple_drain(struct hdq_battery *b) +{ + int i; + + for (i = 0; i < 512 && (readl(b->base + UTRSTAT) & UTRSTAT_RXRDY); i++) { + readl(b->base + UERSTAT); + readl(b->base + URXH); + } +} + +/* iBEC 0xbff14554, iOS _sendBreak: the break bit is held and cleared by hand */ +static void hdq_apple_break(struct hdq_battery *b) +{ + writel(readl(b->base + UCON) | UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_US); + writel(readl(b->base + UCON) & ~UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_REC_US); + hdq_apple_drain(b); +} + +/* one frame and its UERSTAT (read first, as iBEC does), or -ETIMEDOUT */ +static int hdq_apple_rx(struct hdq_battery *b, ktime_t deadline, u8 *c, u8 *err) +{ + for (;;) { + if (readl(b->base + UTRSTAT) & UTRSTAT_RXRDY) { + *err = readl(b->base + UERSTAT) & 0xff; + *c = readl(b->base + URXH) & 0xff; + return 0; + } + if (ktime_after(ktime_get(), deadline)) + return -ETIMEDOUT; + /* the 16-deep FIFO holds a whole transaction */ + usleep_range(100, 200); + } +} + +/* one transaction: break, 8 command bits, their echo, the gauge's 8 bits */ +static int hdq_apple_txn8(struct hdq_battery *b, u8 reg, u8 *val) +{ + struct hdq_trace *t = &b->trace; + ktime_t deadline; + u8 tx[8], c, e; + int i, got = 0, v = 0; + + t->reg = reg; + t->n = 0; + t->why = NULL; + for (i = 0; i < 8; i++) /* LSB first; bit 7 clear: read */ + tx[i] = ((reg >> i) & 1) ? HDQ_TX1 : HDQ_TX0; + + hdq_apple_break(b); + for (i = 0; i < 8; i++) + writel(tx[i], b->base + UTXH); + + deadline = ktime_add_us(ktime_get(), HDQ_TXN_DEADLINE_US); + while (got < 16) { + if (t->n >= HDQ_TRACE_LEN) { + t->why = "too many error frames"; + return -EIO; + } + if (hdq_apple_rx(b, deadline, &c, &e)) { + t->why = got < 8 ? "echo timeout" : "no answer"; + return -EIO; + } + t->rx[t->n] = c; + t->err[t->n] = e; + t->n++; + if (e) + continue; /* dropped, as iBEC's uart_getc does */ + if (got < 8 && c != tx[got]) { + t->why = "echo mismatch"; + return -EIO; + } + if (got >= 8 && c >= HDQ_RX1_MIN) + v |= 1 << (got - 8); + got++; + } + *val = v; + return 0; +} + +/* iOS _hdqRead8 around it: up to 4 tries, 70 us apart */ +static int hdq_apple_read8(struct hdq_battery *b, u8 reg) +{ + u8 v; + int i; + + for (i = 0; i < HDQ_READ8_TRIES; i++) { + if (i) { + b->retries++; + udelay(HDQ_READ8_GAP_US); + } + if (!hdq_apple_txn8(b, reg, &v)) + return v; + } + return -EIO; +} + +/* iBEC 0xbff14248, iOS hdqRead16: hi, lo, hi again until both hi agree */ +static int hdq_apple_read16(struct hdq_battery *b, u8 reg) +{ + int hi, lo, hi2, i; + + for (i = 0; i < HDQ_READ16_TRIES; i++) { + if (i) { + b->retries++; + udelay(10); + } + hi = hdq_apple_read8(b, reg + 1); + if (hi < 0) + return hi; + lo = hdq_apple_read8(b, reg); + if (lo < 0) + return lo; + hi2 = hdq_apple_read8(b, reg + 1); + if (hi2 < 0) + return hi2; + if (hi == hi2) + return lo | hi << 8; + } + b->trace.reg = reg; + b->trace.why = "high byte unstable (hi lo hi)"; + b->trace.n = 3; + b->trace.rx[0] = hi; + b->trace.rx[1] = lo; + b->trace.rx[2] = hi2; + memset(b->trace.err, 0, 3); + return -EIO; +} + +/* a value no bq27540 reports means the wire lied, whatever the echo said */ +static const char *hdq_apple_implausible(const struct hdq_regs *r) +{ + if (r->soc > 100) + return "soc"; + if (r->volt < 2500 || r->volt > 5000) + return "volt"; + if (r->temp < 2731 - 400 || r->temp > 2731 + 900) /* -40..+90 C */ + return "temp"; + if (r->rm > 0x7fff || r->fcc > 0x7fff) + return "charge"; + if (r->dcap < 100 || r->dcap > 0x7fff) + return "dcap"; + if (r->cyct > 10000) + return "cycles"; + return NULL; +} + +/* + * Everything at once into temporaries, checked, then committed together. + * After a failure the bus is left alone for 5 s, and the last good values + * are served for up to 30 s, so one bad transaction reaches no consumer. + */ +static int hdq_apple_refresh(struct hdq_battery *b) +{ + struct hdq_regs r; + struct { u8 reg; int *dst; } m[] = { + { BQ_SOC, &r.soc }, { BQ_VOLT, &r.volt }, { BQ_AI, &r.ai }, + { BQ_TEMP, &r.temp }, { BQ_RM, &r.rm }, { BQ_FCC, &r.fcc }, + { BQ_FLAGS, &r.flags }, { BQ_CYCT, &r.cyct }, { BQ_DCAP, &r.dcap }, + }; + const struct hdq_trace *t = &b->trace; + const char *bad; + int i, v; + + if (b->valid && time_before(jiffies, b->stamp + 5 * HZ)) + return 0; + if (b->fail_ret && time_before(jiffies, b->fail_stamp + 5 * HZ)) + goto stale; + + for (i = 0; i < ARRAY_SIZE(m); i++) { + v = hdq_apple_read16(b, m[i].reg); + if (v < 0) { + b->fails++; + dev_warn_ratelimited(b->dev, + "refresh failed: reg %#04x (byte %#04x) %s; rx %u: %*ph; uerstat: %*ph; fails %u retries %u\n", + m[i].reg, t->reg, t->why ?: "?", t->n, t->n, t->rx, + t->n, t->err, b->fails, b->retries); + b->fail_ret = v; + goto failed; + } + *m[i].dst = v; + } + + bad = hdq_apple_implausible(&r); + if (bad) { + b->fails++; + dev_warn_ratelimited(b->dev, + "refresh rejected (%s): soc %04x volt %04x ai %04x temp %04x rm %04x fcc %04x flags %04x cyc %04x dcap %04x; fails %u retries %u\n", + bad, r.soc, r.volt, r.ai, r.temp, r.rm, r.fcc, r.flags, + r.cyct, r.dcap, b->fails, b->retries); + b->fail_ret = -EIO; + goto failed; + } + + b->soc = r.soc; + b->volt = r.volt; + b->ai = (s16)r.ai; + b->temp = r.temp; + b->rm = r.rm; + b->fcc = r.fcc; + b->flags = r.flags; + b->cyct = r.cyct; + b->dcap = r.dcap; + b->stamp = jiffies; + b->valid = true; + b->fail_ret = 0; + return 0; + +failed: + b->fail_stamp = jiffies; +stale: + if (b->valid && time_before(jiffies, b->stamp + HDQ_STALE_OK)) + return 0; + return b->fail_ret; +} + +/* UART5 as the boot chain left it, and whether the break bit clears itself */ +static void hdq_apple_probe_log(struct hdq_battery *b) +{ + u32 ulcon = readl(b->base + ULCON), ucon = readl(b->base + UCON); + u32 ufcon = readl(b->base + UFCON), ubrdiv = readl(b->base + UBRDIV); + + dev_dbg(b->dev, "UART5 as found: ULCON %#x UCON %#x UFCON %#x UBRDIV %#x\n", + ulcon, ucon, ufcon, ubrdiv); +} + /* everything at once, at most every 5 s: a 16-bit read is ~10 ms of bus */ static int hdq_refresh(struct hdq_battery *b) { @@ -130,6 +416,8 @@ static int hdq_refresh(struct hdq_battery *b) }; int i, v; + if (b->var->apple_txn) + return hdq_apple_refresh(b); if (b->valid && time_before(jiffies, b->stamp + 5 * HZ)) return 0; for (i = 0; i < ARRAY_SIZE(r); i++) { @@ -236,6 +524,9 @@ static int hdq_probe(struct platform_device *pdev) if (!b) return -ENOMEM; b->dev = &pdev->dev; + b->var = of_device_get_match_data(&pdev->dev); + if (!b->var) + b->var = &hdq_s5l8940x; mutex_init(&b->lock); b->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(b->base)) @@ -246,17 +537,38 @@ static int hdq_probe(struct platform_device *pdev) if (IS_ERR(clk)) return dev_err_probe(&pdev->dev, PTR_ERR(clk), "UART5 power\n"); + if (b->var->apple_txn) + hdq_apple_probe_log(b); + writel(HDQ_ULCON, b->base + ULCON); writel(HDQ_UFCON, b->base + UFCON); writel(HDQ_UBRDIV, b->base + UBRDIV); writel(0, b->base + UFRACVAL); writel(HDQ_UCON, b->base + UCON); + if (b->var->apple_txn) { + /* does this UART end a break by itself? Apple never relies on it */ + writel(HDQ_UCON | UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_US); + dev_dbg(&pdev->dev, "break bit %s after %d us\n", + readl(b->base + UCON) & UCON_SBREAK ? "still set" : "cleared by the UART", + HDQ_BREAK_US); + writel(HDQ_UCON, b->base + UCON); + udelay(HDQ_BREAK_REC_US); + hdq_apple_drain(b); + } + mutex_lock(&b->lock); soc = hdq_refresh(b) ? -1 : b->soc; mutex_unlock(&b->lock); if (soc < 0) dev_warn(&pdev->dev, "the gauge does not answer yet\n"); + else if (b->var->apple_txn) + dev_info(&pdev->dev, "bq27540: %d%%, %d mV, %d mA, %s%d.%d C, %d/%d mAh (design %d), %d cycles, flags %#06x, retries %u\n", + b->soc, b->volt, b->ai, b->temp < 2731 ? "-" : "", + abs(b->temp - 2731) / 10, abs(b->temp - 2731) % 10, + b->rm, b->fcc, b->dcap, + b->cyct, b->flags, b->retries); else dev_info(&pdev->dev, "bq27540: %d%%, %d mV, %d mA, %d/%d mAh, %d cycles\n", b->soc, b->volt, b->ai, b->rm, b->fcc, b->cyct); @@ -268,7 +580,8 @@ static int hdq_probe(struct platform_device *pdev) } static const struct of_device_id hdq_of_match[] = { - { .compatible = "apple,s5l8940x-hdq-battery" }, + { .compatible = "apple,s5l8950x-hdq-battery", .data = &hdq_s5l8950x }, + { .compatible = "apple,s5l8940x-hdq-battery", .data = &hdq_s5l8940x }, { } }; MODULE_DEVICE_TABLE(of, hdq_of_match); @@ -282,5 +595,5 @@ static struct platform_driver hdq_driver = { }; module_platform_driver(hdq_driver); -MODULE_DESCRIPTION("bq27540 gas gauge on HDQ through UART5 (iPad mini 1)"); +MODULE_DESCRIPTION("bq27540 gas gauge on HDQ through UART5 (A5 iPads, iPhone 5)"); MODULE_LICENSE("GPL"); diff --git a/drivers/power/supply/apple-pmu-charger.c b/drivers/power/supply/apple-pmu-charger.c index cf265c512f3bc5..a2e89c1b1fc510 100644 --- a/drivers/power/supply/apple-pmu-charger.c +++ b/drivers/power/supply/apple-pmu-charger.c @@ -40,6 +40,11 @@ * - Above 45 C on the battery gauge it drops to 100 mA, and stays there * until below 40 C. Missing/failed temperature also selects this fallback. * iBEC's charge table ends at 45 C. This is derating, not charge inhibition. + * On the D1972 a reading outside -20..+70 C counts as missing, and it + * takes three cool readings in a row to leave the fallback. + * + * The temperature is the battery gauge's ("battery", POWER_SUPPLY_PROP_TEMP), + * on every chip; this driver reads no PMU ADC channel. */ #include #include @@ -58,11 +63,8 @@ #define PMU_STATUS0_VBUS BIT(6) #define PMU_INSUSP0 0x1b /* bit 2: input suspend */ #define PMU_INSUSP1 0x1c /* bit 7: input suspend */ -#define PMU_ILIM 0x3d /* the only register written */ #define PMU_ICHG 0x3f /* charge current setting */ -#define ILIM_MIN_CODE 0x02 /* < 100 mA; also the PMU's own sag value */ -#define ILIM_STEP 8 /* codes per ramp step, 100 mA */ #define ILIM_STEP_MS 10 #define ILIM_HARD_MAX_MA 2100 #define ILIM_DEFAULT_MA 1000 @@ -71,9 +73,106 @@ #define HOT_DECI_C 450 #define COOL_DECI_C 400 +/* + * temp_guard (D1972 / iPhone 5): the battery temperature is the gauge's, over + * HDQ. A reading outside -20..+70 C is no reading at all, and leaving the + * 100 mA fallback takes COOL_SAMPLES cool readings in a row, so neither a + * corrupt word nor a single good one between failures moves the limit up. + */ +#define GUARD_MIN_DECI_C (-200) +#define GUARD_MAX_DECI_C 700 +#define COOL_SAMPLES 3 + +struct pmu_chg_chip { + const char *name; + u8 status_reg; + u8 vbus_mask; + u8 ilim_reg; + u8 min_code; /* < 100 mA; also the PMU's own VBUS-sag drop value */ + u8 ramp_step; /* codes per ramp step (~100 mA) */ + unsigned int max_ma; + u8 (*ma_to_code)(unsigned int ma); + unsigned int (*code_to_ma)(u8 code); + bool temp_guard; +}; + +/* D1946: iBEC's encoding, 0x9ff0c618..0x9ff0c662 (register 0x3d) */ +static u8 d1946_ma_to_code(unsigned int ma) +{ + unsigned int code; + + ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); + if (ma < 100) + return 0x02; + code = (8 * ma - 600) / 100; + if (code >= 0x73) + return code - 4; + if (code > 0x6e) + return 0x6e; + if (code >= 0x4b) + return code - 2; + if (code > 0x48) + return 0x48; + return code; +} + +static unsigned int d1946_code_to_ma(u8 code) +{ + return code <= 0x02 ? 100 : (code * 100 + 600) / 8; +} + +/* + * D1972 (iPhone 5, S5L8950X): register 0x3f. + * AppleD1972PMUPowerSource (14G61 0x810e9bf4..0x810e9c2f): linear 10 mA/LSB + * from 70 mA (code = (mA - 70) / 10; < 70 mA suspends, > 2619 mA clamps to + * 0xff). On VBUS sag the PMU drops 0x3f back to 0x03 (100 mA, checked at + * 0x810e99e6). Online is status register 0x0a bit 6 (0x810e9ec8), as on D1946. + */ +static u8 d1972_ma_to_code(unsigned int ma) +{ + ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); + if (ma < 100) + return 0x03; + return (ma - 70) / 10; +} + +static unsigned int d1972_code_to_ma(u8 code) +{ + return code <= 0x03 ? 100 : 70 + code * 10; +} + +static const struct pmu_chg_chip pmu_chg_d1946 = { + .name = "d1946", + .status_reg = PMU_STATUS0, + .vbus_mask = PMU_STATUS0_VBUS, + .ilim_reg = 0x3d, + .min_code = 0x02, + .ramp_step = 8, + .max_ma = ILIM_HARD_MAX_MA, + .ma_to_code = d1946_ma_to_code, + .code_to_ma = d1946_code_to_ma, +}; + +static const struct pmu_chg_chip pmu_chg_d1972 = { + .name = "d1972", + .status_reg = PMU_STATUS0, + .vbus_mask = PMU_STATUS0_VBUS, + .ilim_reg = 0x3f, + .min_code = 0x03, + .ramp_step = 10, + .max_ma = ILIM_HARD_MAX_MA, + .ma_to_code = d1972_ma_to_code, + .code_to_ma = d1972_code_to_ma, + .temp_guard = true, +}; + +static bool d1972_armed; +module_param(d1972_armed, bool, 0644); +MODULE_PARM_DESC(d1972_armed, "enable D1972 charger writes (default off)"); + static bool armed = true; module_param(armed, bool, 0644); -MODULE_PARM_DESC(armed, "write 0x3d (default on; off: log only)"); +MODULE_PARM_DESC(armed, "enable D1946 charger writes (default on)"); static int max_input_ma = ILIM_DEFAULT_MA; module_param(max_input_ma, int, 0644); @@ -82,52 +181,33 @@ MODULE_PARM_DESC(max_input_ma, "ceiling for input_current_limit, 100..2100 mA (d struct pmu_chg { struct device *dev; struct regmap *map; + const struct pmu_chg_chip *chip; struct power_supply *psy; struct delayed_work work; struct mutex lock; /* everything below */ unsigned int req_ma; /* asked for: default or input_current_limit */ - u8 probe_code; /* 0x3d as iBEC left it */ + u8 probe_code; /* ILIM register as iBoot left it */ u8 last_written; /* 0 = nothing written while online */ u8 ceiling; /* after a sag, until unplugged; 0xff = none */ bool online; bool was_armed; bool hot; + u8 cool_samples; /* temp_guard: cool readings in a row */ u8 logged_code; /* last "would set" logged, to say it once */ }; -/* iBEC's encoding, 0x9ff0c618..0x9ff0c662 */ -static u8 pmu_chg_ma_to_code(unsigned int ma) -{ - unsigned int code; - - ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); - if (ma < 100) - return ILIM_MIN_CODE; - code = (8 * ma - 600) / 100; - if (code >= 0x73) - return code - 4; - if (code > 0x6e) - return 0x6e; - if (code >= 0x4b) - return code - 2; - if (code > 0x48) - return 0x48; - return code; -} - -/* The nominal current of a code, for the log only. */ -static unsigned int pmu_chg_code_to_ma(u8 code) +static bool pmu_chg_armed(struct pmu_chg *c) { - return code <= ILIM_MIN_CODE ? 100 : (code * 100 + 600) / 8; + return c->chip == &pmu_chg_d1972 ? READ_ONCE(d1972_armed) : READ_ONCE(armed); } static int pmu_chg_write(struct pmu_chg *c, unsigned int reg, u8 val) { int ret; - if (reg != PMU_ILIM) { + if (reg != c->chip->ilim_reg) { dev_err(c->dev, "refusing PMU write 0x%02x = 0x%02x: only 0x%02x is ours\n", - reg, val, PMU_ILIM); + reg, val, c->chip->ilim_reg); return -EPERM; } ret = regmap_write(c->map, reg, val); @@ -153,8 +233,10 @@ static void pmu_chg_log_vbus(struct pmu_chg *c, u8 ilim, bool is_armed) int ret; u8 val; - dev_info(c->dev, "VBUS on: 0x3d = 0x%02x, %s\n", ilim, + dev_info(c->dev, "VBUS on: 0x%02x = 0x%02x, %s\n", c->chip->ilim_reg, ilim, is_armed ? "armed" : "disarmed"); + if (c->chip != &pmu_chg_d1946) + return; for (i = 0; i < ARRAY_SIZE(regs); i++) { ret = pmu_chg_read(c, regs[i], &val); if (ret) @@ -167,42 +249,90 @@ static void pmu_chg_log_vbus(struct pmu_chg *c, u8 ilim, bool is_armed) static void pmu_chg_sagged(struct pmu_chg *c, u8 at) { - c->ceiling = max_t(int, ILIM_MIN_CODE, at - ILIM_STEP); + c->ceiling = max_t(int, c->chip->min_code, at - c->chip->ramp_step); dev_warn(c->dev, "VBUS sagged at 0x%02x: limiting USB input current to %u mA until unplugged\n", - at, pmu_chg_code_to_ma(c->ceiling)); + at, c->chip->code_to_ma(c->ceiling)); } -/* Ramp 0x3d from cur up to target, 8 codes every 10 ms, watching for a sag. */ +/* Ramp ILIM from cur up to target, ramp_step codes every 10 ms, watching for a sag. */ static void pmu_chg_ramp(struct pmu_chg *c, u8 cur, u8 target) { while (cur < target) { - u8 next = min_t(int, cur + ILIM_STEP, target), rb; + u8 next = min_t(int, cur + c->chip->ramp_step, target), rb; - if (pmu_chg_write(c, PMU_ILIM, next)) + if (pmu_chg_write(c, c->chip->ilim_reg, next)) return; msleep(ILIM_STEP_MS); - if (pmu_chg_read(c, PMU_ILIM, &rb)) + if (pmu_chg_read(c, c->chip->ilim_reg, &rb)) return; - if (rb == ILIM_MIN_CODE && next > ILIM_MIN_CODE) { + if (rb == c->chip->min_code && next > c->chip->min_code) { pmu_chg_sagged(c, next); if (c->ceiling < next) - pmu_chg_write(c, PMU_ILIM, c->ceiling); + pmu_chg_write(c, c->chip->ilim_reg, c->ceiling); return; } if (rb != next) { - dev_warn(c->dev, "0x3d reads 0x%02x after writing 0x%02x; stopping the ramp\n", - rb, next); + dev_warn(c->dev, "0x%02x reads 0x%02x after writing 0x%02x; stopping the ramp\n", + c->chip->ilim_reg, rb, next); return; } cur = rb; } } +static bool pmu_chg_battery_hot_guarded(struct pmu_chg *c) +{ + union power_supply_propval t = {}; + struct power_supply *bat = power_supply_get_by_name("battery"); + int ret = -ENODEV; + + if (bat) { + ret = power_supply_get_property(bat, POWER_SUPPLY_PROP_TEMP, &t); + power_supply_put(bat); + if (!ret && (t.intval < GUARD_MIN_DECI_C || t.intval > GUARD_MAX_DECI_C)) + ret = -ERANGE; + } + + if (ret) { + /* no reading: the 100 mA fallback, and the cool count starts over */ + if (!c->hot) + dev_warn(c->dev, "battery temperature unavailable (%d%s): input limited to 100 mA\n", + ret == -ERANGE ? t.intval : ret, + ret == -ERANGE ? " deci-C, out of range" : ""); + c->hot = true; + c->cool_samples = 0; + return true; + } + + if (t.intval > HOT_DECI_C) { + if (!c->hot) + dev_warn(c->dev, "battery at %s%d.%d C: input limited to 100 mA\n", + t.intval < 0 ? "-" : "", abs(t.intval) / 10, abs(t.intval) % 10); + c->hot = true; + c->cool_samples = 0; + } else if (c->hot) { + if (t.intval < COOL_DECI_C && ++c->cool_samples >= COOL_SAMPLES) { + c->hot = false; + c->cool_samples = 0; + dev_info(c->dev, "battery back to %s%d.%d C (%d readings in a row)\n", + t.intval < 0 ? "-" : "", abs(t.intval) / 10, + abs(t.intval) % 10, COOL_SAMPLES); + } else if (t.intval >= COOL_DECI_C) { + c->cool_samples = 0; + } + } + return c->hot; +} + static bool pmu_chg_battery_hot(struct pmu_chg *c) { union power_supply_propval t; - struct power_supply *bat = power_supply_get_by_name("battery"); + struct power_supply *bat; + if (c->chip->temp_guard) + return pmu_chg_battery_hot_guarded(c); + + bat = power_supply_get_by_name("battery"); if (!bat) { c->hot = true; return true; @@ -230,35 +360,37 @@ static bool pmu_chg_battery_hot(struct pmu_chg *c) static u8 pmu_chg_target(struct pmu_chg *c) { unsigned int ma = min_t(unsigned int, c->req_ma, - clamp(READ_ONCE(max_input_ma), 100, ILIM_HARD_MAX_MA)); - u8 code = pmu_chg_ma_to_code(ma); + clamp(READ_ONCE(max_input_ma), 100, (int)c->chip->max_ma)); + u8 code = c->chip->ma_to_code(ma); if (c->ceiling != 0xff) code = min(code, c->ceiling); if (pmu_chg_battery_hot(c)) - code = ILIM_MIN_CODE; + code = c->chip->min_code; return code; } static void pmu_chg_work(struct work_struct *w) { struct pmu_chg *c = container_of(to_delayed_work(w), struct pmu_chg, work); - bool is_armed = READ_ONCE(armed), online, changed = false; + bool is_armed = pmu_chg_armed(c), online, changed = false; u8 st, cur, target; mutex_lock(&c->lock); if (c->was_armed && !is_armed) { - if (pmu_chg_write(c, PMU_ILIM, c->probe_code)) + if (pmu_chg_write(c, c->chip->ilim_reg, c->probe_code)) goto out; /* Leave restoration pending for the next poll. */ - dev_info(c->dev, "disarmed: 0x3d back to 0x%02x\n", c->probe_code); + dev_info(c->dev, "disarmed: 0x%02x back to 0x%02x\n", + c->chip->ilim_reg, c->probe_code); c->last_written = 0; c->logged_code = 0; c->was_armed = false; goto out; } - if (pmu_chg_read(c, PMU_STATUS0, &st) || pmu_chg_read(c, PMU_ILIM, &cur)) + if (pmu_chg_read(c, c->chip->status_reg, &st) || + pmu_chg_read(c, c->chip->ilim_reg, &cur)) goto out; - online = st & PMU_STATUS0_VBUS; + online = !!(st & c->chip->vbus_mask); changed = online != c->online; c->online = online; if (changed) { @@ -276,8 +408,9 @@ static void pmu_chg_work(struct work_struct *w) target = pmu_chg_target(c); if (!is_armed) { if (cur != target && target != c->logged_code) { - dev_info(c->dev, "disarmed: would set 0x3d 0x%02x -> 0x%02x (%u mA)\n", - cur, target, pmu_chg_code_to_ma(target)); + dev_info(c->dev, "disarmed: would set 0x%02x 0x%02x -> 0x%02x (%u mA)\n", + c->chip->ilim_reg, cur, target, + c->chip->code_to_ma(target)); c->logged_code = target; } goto out; @@ -285,14 +418,14 @@ static void pmu_chg_work(struct work_struct *w) c->was_armed = true; /* steady state: the PMU dropped what we set, so the source sagged */ - if (cur == ILIM_MIN_CODE && c->last_written > ILIM_MIN_CODE) { + if (cur == c->chip->min_code && c->last_written > c->chip->min_code) { pmu_chg_sagged(c, c->last_written); target = min(target, c->ceiling); } if (cur < target) pmu_chg_ramp(c, cur, target); else if (cur > target) - pmu_chg_write(c, PMU_ILIM, target); + pmu_chg_write(c, c->chip->ilim_reg, target); out: mutex_unlock(&c->lock); if (changed) @@ -325,7 +458,7 @@ static int pmu_chg_set(struct power_supply *psy, enum power_supply_property psp, if (psp != POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT) return -EINVAL; - if (ma < 100 || ma > clamp(READ_ONCE(max_input_ma), 100, ILIM_HARD_MAX_MA)) + if (ma < 100 || ma > clamp(READ_ONCE(max_input_ma), 100, (int)c->chip->max_ma)) return -ERANGE; mutex_lock(&c->lock); c->req_ma = ma; @@ -360,25 +493,31 @@ static const struct power_supply_desc pmu_chg_desc = { static int pmu_chg_probe(struct platform_device *pdev) { struct apple_pmu_i2c *pmu = dev_get_drvdata(pdev->dev.parent); + const struct pmu_chg_chip *chip = of_device_get_match_data(&pdev->dev); struct power_supply_config cfg = {}; struct pmu_chg *c; u8 st; int ret; + if (!chip) + return -ENODEV; + c = devm_kzalloc(&pdev->dev, sizeof(*c), GFP_KERNEL); if (!c) return -ENOMEM; c->dev = &pdev->dev; c->map = pmu->regmap; + c->chip = chip; c->req_ma = ILIM_DEFAULT_MA; c->ceiling = 0xff; mutex_init(&c->lock); - if (pmu_chg_read(c, PMU_ILIM, &c->probe_code) || pmu_chg_read(c, PMU_STATUS0, &st)) + if (pmu_chg_read(c, chip->ilim_reg, &c->probe_code) || + pmu_chg_read(c, chip->status_reg, &st)) return -EIO; - c->online = st & PMU_STATUS0_VBUS; + c->online = !!(st & chip->vbus_mask); if (c->online) - pmu_chg_log_vbus(c, c->probe_code, READ_ONCE(armed)); + pmu_chg_log_vbus(c, c->probe_code, pmu_chg_armed(c)); cfg.drv_data = c; cfg.of_node = pdev->dev.of_node; @@ -393,14 +532,16 @@ static int pmu_chg_probe(struct platform_device *pdev) if (ret) return ret; schedule_delayed_work(&c->work, msecs_to_jiffies(POLL_MS)); - dev_info(&pdev->dev, "0x3d = 0x%02x (%u mA) at probe, VBUS %s, %s\n", - c->probe_code, pmu_chg_code_to_ma(c->probe_code), - c->online ? "on" : "off", armed ? "armed" : "disarmed (log only)"); + dev_info(&pdev->dev, "%s: 0x%02x = 0x%02x (%u mA) at probe, VBUS %s, %s\n", + chip->name, chip->ilim_reg, c->probe_code, + chip->code_to_ma(c->probe_code), + c->online ? "on" : "off", pmu_chg_armed(c) ? "armed" : "disarmed (log only)"); return 0; } static const struct of_device_id pmu_chg_of_match[] = { - { .compatible = "apple,pmu-d1946-charger" }, + { .compatible = "apple,pmu-d1946-charger", .data = &pmu_chg_d1946 }, + { .compatible = "apple,pmu-d1972-charger", .data = &pmu_chg_d1972 }, { } }; MODULE_DEVICE_TABLE(of, pmu_chg_of_match); @@ -414,5 +555,5 @@ static struct platform_driver pmu_chg_driver = { }; module_platform_driver(pmu_chg_driver); -MODULE_DESCRIPTION("D1946 PMU USB input current limit (iPad mini 1)"); +MODULE_DESCRIPTION("Apple Dialog PMU (D1946/D1972) USB input current limit"); MODULE_LICENSE("GPL"); diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig index 361b50ea226ee7..b5ddc2c7ba3196 100644 --- a/drivers/video/backlight/Kconfig +++ b/drivers/video/backlight/Kconfig @@ -458,6 +458,15 @@ config BACKLIGHT_APPLE_PMU_WLED The iPad mini 1's backlight, driven by the white-LED strings of its D1946 PMU, as /sys/class/backlight/apple-pmu-wled (0..2047). +config BACKLIGHT_APPLE_LM3534 + tristate "Apple TI LM3534 + DWI backlight (iPhone 5 / N42)" + depends on I2C && OF + select REGMAP_I2C + help + Backlight driver for Apple S5L8950X devices (iPhone 5) using an + external TI LM3534 LED driver over I2C0 for enable control and the + SoC DWI block for 11-bit brightness updates (0..2047). + config BACKLIGHT_GPIO tristate "Generic GPIO based Backlight Driver" depends on GPIOLIB || COMPILE_TEST diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile index 050be5fcab7df9..9547c361a9bd07 100644 --- a/drivers/video/backlight/Makefile +++ b/drivers/video/backlight/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_BACKLIGHT_DA903X) += da903x_bl.o obj-$(CONFIG_BACKLIGHT_DA9052) += da9052_bl.o obj-$(CONFIG_BACKLIGHT_EP93XX) += ep93xx_bl.o obj-$(CONFIG_BACKLIGHT_APPLE_PMU_WLED) += apple-pmu-wled.o +obj-$(CONFIG_BACKLIGHT_APPLE_LM3534) += apple-lm3534.o obj-$(CONFIG_BACKLIGHT_GPIO) += gpio_backlight.o obj-$(CONFIG_BACKLIGHT_HP680) += hp680_bl.o obj-$(CONFIG_BACKLIGHT_HP700) += jornada720_bl.o diff --git a/drivers/video/backlight/apple-lm3534.c b/drivers/video/backlight/apple-lm3534.c new file mode 100644 index 00000000000000..df11961ac0f7cd --- /dev/null +++ b/drivers/video/backlight/apple-lm3534.c @@ -0,0 +1,245 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * TI LM3534 backlight driver with Apple S5L DWI brightness updates (iPhone 5 / N42). + * + * On N42 (S5L8950X), the Dialog D1972 PMU has no white-LED strings; the display + * backlight is driven by an external TI LM3534 on I2C0 (0x63, ADT "backlight,lm3534", + * using-LM3534 = 1) paired with the SoC's DWI block (0x3f700000, version = 1): + * + * - Enable/disable (AppleLMBacklight, 14G61 0x80e0c914): I2C register 0x10 + * bit 0 switches the output; enabling is followed by a 600 us delay. + * Other bits of 0x10 (boost/OVP/DWI mode set up by iBoot) are preserved. + * - Brightness (AppleS5L8940XDWI::sendWLED, 14G61 0x812d2860): 11-bit level + * 0..2047 encoded for DWI version 1 as (0xa0000000 | (level & 0x7ff)), + * written to DWI+0x14 after polling DWI+0x10 bit 0 clear, then triggered + * by writing 0x71 to DWI+0x10. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LM3534_REG_ENABLE 0x10 +#define LM3534_ENABLE_BIT BIT(0) +#define LM3534_ENABLE_DELAY_US 600 + +#define LM3534_MAX_BRIGHTNESS 2047 +#define LM3534_DEFAULT_BRIGHTNESS 1024 + +#define DWI_SYNC_CTRL 0x10 +#define DWI_SYNC_DATA 0x14 +#define DWI_CTRL_BUSY BIT(0) +#define DWI_CTRL_TRIGGER 0x71 + +struct apple_lm3534 { + struct device *dev; + struct regmap *regmap; + void __iomem *dwi_base; + struct clk *dwi_clk; + u32 dwi_version; + bool enabled; +}; + +static const struct regmap_config apple_lm3534_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0xff, +}; + +static u32 apple_dwi_encode_wled(u32 level, u32 version) +{ + level &= 0x7ff; + if (version == 0) + return 0x20000000u | ((level & 0x3u) << 9) | (level >> 2); + return 0xa0000000u | level; +} + +static int apple_lm3534_dwi_send(struct apple_lm3534 *bl, u32 level) +{ + u32 val, word; + int ret; + + if (!bl->dwi_base) + return 0; + + ret = readl_poll_timeout(bl->dwi_base + DWI_SYNC_CTRL, val, + !(val & DWI_CTRL_BUSY), 10, 10000); + if (ret) { + dev_warn_ratelimited(bl->dev, "DWI busy timeout (ctrl=0x%08x)\n", val); + return ret; + } + + word = apple_dwi_encode_wled(level, bl->dwi_version); + writel(word, bl->dwi_base + DWI_SYNC_DATA); + writel(DWI_CTRL_TRIGGER, bl->dwi_base + DWI_SYNC_CTRL); + return 0; +} + +static int apple_lm3534_set_enable(struct apple_lm3534 *bl, bool enable) +{ + int ret; + + if (bl->enabled == enable) + return 0; + + ret = regmap_update_bits(bl->regmap, LM3534_REG_ENABLE, + LM3534_ENABLE_BIT, + enable ? LM3534_ENABLE_BIT : 0); + if (ret) + return ret; + + if (enable) + usleep_range(LM3534_ENABLE_DELAY_US, LM3534_ENABLE_DELAY_US + 200); + + bl->enabled = enable; + return 0; +} + +static int apple_lm3534_update_status(struct backlight_device *bd) +{ + struct apple_lm3534 *bl = bl_get_data(bd); + int level = backlight_get_brightness(bd); + int ret; + + if (!level) + return apple_lm3534_set_enable(bl, false); + + ret = apple_lm3534_set_enable(bl, true); + if (ret) + return ret; + + return apple_lm3534_dwi_send(bl, level); +} + +static const struct backlight_ops apple_lm3534_ops = { + .options = BL_CORE_SUSPENDRESUME, + .update_status = apple_lm3534_update_status, +}; + +static void apple_lm3534_dwi_clk_cleanup(void *clk) +{ + clk_disable_unprepare(clk); + clk_put(clk); +} + +static int apple_lm3534_attach_dwi(struct apple_lm3534 *bl) +{ + struct device_node *dwi_np; + struct resource res; + int ret; + + dwi_np = of_parse_phandle(bl->dev->of_node, "apple,dwi", 0); + if (!dwi_np) { + dev_warn(bl->dev, "no apple,dwi phandle: only on/off control available\n"); + return 0; + } + + bl->dwi_version = 1; + of_property_read_u32(dwi_np, "apple,dwi-version", &bl->dwi_version); + + ret = of_address_to_resource(dwi_np, 0, &res); + if (ret) { + of_node_put(dwi_np); + return ret; + } + + bl->dwi_base = devm_ioremap_resource(bl->dev, &res); + if (IS_ERR(bl->dwi_base)) { + of_node_put(dwi_np); + return PTR_ERR(bl->dwi_base); + } + + bl->dwi_clk = of_clk_get(dwi_np, 0); + of_node_put(dwi_np); + if (IS_ERR(bl->dwi_clk)) { + ret = PTR_ERR(bl->dwi_clk); + if (ret == -ENOENT) { + bl->dwi_clk = NULL; + return 0; + } + return dev_err_probe(bl->dev, ret, "failed to get DWI clock\n"); + } + + ret = clk_prepare_enable(bl->dwi_clk); + if (ret) { + clk_put(bl->dwi_clk); + bl->dwi_clk = NULL; + return ret; + } + + return devm_add_action_or_reset(bl->dev, apple_lm3534_dwi_clk_cleanup, + bl->dwi_clk); +} + +static int apple_lm3534_probe(struct i2c_client *i2c) +{ + struct backlight_properties props = {}; + struct backlight_device *bd; + struct apple_lm3534 *bl; + unsigned int reg_en = 0; + u32 def_level = LM3534_DEFAULT_BRIGHTNESS; + int ret; + + bl = devm_kzalloc(&i2c->dev, sizeof(*bl), GFP_KERNEL); + if (!bl) + return -ENOMEM; + + bl->dev = &i2c->dev; + bl->regmap = devm_regmap_init_i2c(i2c, &apple_lm3534_regmap_config); + if (IS_ERR(bl->regmap)) + return PTR_ERR(bl->regmap); + + ret = apple_lm3534_attach_dwi(bl); + if (ret) + return ret; + + ret = regmap_read(bl->regmap, LM3534_REG_ENABLE, ®_en); + if (ret) + return dev_err_probe(&i2c->dev, ret, "failed to read LM3534 enable reg\n"); + + bl->enabled = !!(reg_en & LM3534_ENABLE_BIT); + of_property_read_u32(i2c->dev.of_node, "default-brightness-level", &def_level); + if (def_level > LM3534_MAX_BRIGHTNESS) + def_level = LM3534_MAX_BRIGHTNESS; + + props.type = BACKLIGHT_RAW; + props.scale = BACKLIGHT_SCALE_NON_LINEAR; + props.max_brightness = LM3534_MAX_BRIGHTNESS; + props.brightness = bl->enabled ? def_level : 0; + props.power = bl->enabled ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF; + + bd = devm_backlight_device_register(&i2c->dev, "apple-lm3534", &i2c->dev, bl, + &apple_lm3534_ops, &props); + if (IS_ERR(bd)) + return PTR_ERR(bd); + + i2c_set_clientdata(i2c, bd); + dev_info(&i2c->dev, "LM3534 reg 0x10=0x%02x (%s), DWI v%u %s\n", + reg_en, bl->enabled ? "on" : "off", bl->dwi_version, + bl->dwi_base ? "attached" : "absent"); + return 0; +} + +static const struct of_device_id apple_lm3534_of_match[] = { + { .compatible = "apple,lm3534-backlight" }, + { } +}; +MODULE_DEVICE_TABLE(of, apple_lm3534_of_match); + +static struct i2c_driver apple_lm3534_driver = { + .driver = { + .name = "apple-lm3534", + .of_match_table = apple_lm3534_of_match, + }, + .probe = apple_lm3534_probe, +}; +module_i2c_driver(apple_lm3534_driver); + +MODULE_DESCRIPTION("Apple TI LM3534 + DWI display backlight driver"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/apple-pmu-i2c.h b/include/linux/apple-pmu-i2c.h index 8d227b3d5ee543..6390967c13e65d 100644 --- a/include/linux/apple-pmu-i2c.h +++ b/include/linux/apple-pmu-i2c.h @@ -10,10 +10,13 @@ #include #include +struct apple_pmu_i2c_info; + struct apple_pmu_i2c { struct device *dev; struct regmap *regmap; struct regmap_config config; + const struct apple_pmu_i2c_info *info; }; #endif