diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8010-aop.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8010-aop.yaml index 9efb40bf1f0919..d2bdf3ec5dc456 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8010-aop.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8010-aop.yaml @@ -112,7 +112,9 @@ properties: type: boolean description: The device has a cellular modem. From the Apple device tree's AOP nub, - as iBoot passes it to the A12's firmware (boot argument "bbOn"). + as iBoot passes it to the A12's and the iPhone 7's (apple,d10-aop) + firmware (boot argument "bbOn"). The iPhone 7's firmware serves its + loudspeaker amplifiers only when it is set. apple,pmu-arc-reset: type: boolean @@ -204,6 +206,42 @@ properties: - "#clock-cells" additionalProperties: false + spuapp: + type: object + description: + The iPhone 7 firmware's loudspeaker service ("SPUApp"). It switches + the 12 MHz MCLK of the top speaker's amplifier and the Maggie FPGA + (clock 0) and the FPGA's enable (clock 1), and drives the bottom + speaker's and the Taptic Engine's amplifiers, which sit on the AOP's + own I2C bus. + properties: + compatible: + const: apple,t8010-aop-spuapp + + "#clock-cells": + const: 1 + + apple,btmspk-config: + $ref: /schemas/types.yaml#/definitions/uint8-array + minItems: 1 + maxItems: 64 + description: + The bottom speaker amplifier's configuration, the Apple device + tree's SPUApp spkamp-config_btmspk. + + apple,arc-config: + $ref: /schemas/types.yaml#/definitions/uint8-array + minItems: 1 + maxItems: 64 + description: + The Taptic Engine amplifier's configuration, the Apple device + tree's SPUApp spkamp-config_arc. + + required: + - compatible + - "#clock-cells" + additionalProperties: false + patternProperties: "^(accel|gyro|compass|pressure)$": type: object diff --git a/Documentation/devicetree/bindings/sound/apple,s8000-mca.yaml b/Documentation/devicetree/bindings/sound/apple,s8000-mca.yaml index 31c995854915b7..867bd364bfc34a 100644 --- a/Documentation/devicetree/bindings/sound/apple,s8000-mca.yaml +++ b/Documentation/devicetree/bindings/sound/apple,s8000-mca.yaml @@ -22,6 +22,9 @@ description: | iPhone 6s Plus. apple,s8000-mca-codec is the A9 MCA0 link to the iPhone 6s and 6s Plus headset codec, a clock consumer with four 16-bit TDM slots, and apple,t8010-mca-codec the same link to the iPhone 7's codec. + apple,t8010-mca-maggie is the iPhone 7's playback-only loudspeaker link + (TDM), a consumer of the clocks of its Maggie FPGA, with fifteen 16-bit + slots and ten idle bit clocks per frame. The MCAs are the same block; the compatible names the link setup iOS programs into it. The T7001 (A8X) MCA is the older "mca-version 1" register layout with the same fields: apple,t7001-mca is @@ -71,6 +74,7 @@ properties: - apple,t8010-mca - apple,t8010-mca-codec - apple,t8010-mca-i2s + - apple,t8010-mca-maggie - apple,t8011-mca clocks: diff --git a/Documentation/devicetree/bindings/sound/apple,t8010-maggie.yaml b/Documentation/devicetree/bindings/sound/apple,t8010-maggie.yaml new file mode 100644 index 00000000000000..8a63da41fcfa13 --- /dev/null +++ b/Documentation/devicetree/bindings/sound/apple,t8010-maggie.yaml @@ -0,0 +1,98 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/sound/apple,t8010-maggie.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple Maggie FPGA of the iPhone 7 + +maintainers: + - Paul Scalise + +description: | + A Lattice iCE5LP4K FPGA on SPI, between the iPhone 7's loudspeaker link + (MCA1) and its speaker amplifiers. It provides the link's bit and frame + clocks and passes each speaker's slot to its amplifier. It has no flash: + the driver loads Apple's bitstream through SPI. It runs while the + Always-On Processor provides its MCLK and its enable, and the AOP also + drives the bottom speaker's amplifier. + +allOf: + - $ref: dai-common.yaml# + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + const: apple,t8010-maggie + + reg: + maxItems: 1 + + reset-gpios: + maxItems: 1 + description: The FPGA's CRESET_B. + + cdone-gpios: + maxItems: 1 + description: The FPGA's CDONE, high once it is configured. + + clocks: + items: + - description: The FPGA's MCLK + - description: The FPGA's enable + + clock-names: + items: + - const: mclk + - const: enable + + apple,spuapp: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The AOP's SPUApp service, which drives the bottom speaker's amplifier. + + firmware-name: + maxItems: 1 + description: + The FPGA's bitstream, the payload of the restore image's + Firmware/Maggie/AppleMaggieFirmwareImage.im4p. + + '#sound-dai-cells': + const: 0 + +required: + - compatible + - reg + - reset-gpios + - cdone-gpios + - clocks + - clock-names + - apple,spuapp + - firmware-name + - '#sound-dai-cells' + +unevaluatedProperties: false + +examples: + - | + #include + + spi { + #address-cells = <1>; + #size-cells = <0>; + + fpga@1 { + compatible = "apple,t8010-maggie"; + reg = <1>; + spi-max-frequency = <12000000>; + spi-cpha; + spi-cpol; + reset-gpios = <&pinctrl_ap 22 GPIO_ACTIVE_LOW>; + cdone-gpios = <&pinctrl_ap 26 GPIO_ACTIVE_HIGH>; + clocks = <&aop_spuapp 0>, <&aop_spuapp 1>; + clock-names = "mclk", "enable"; + apple,spuapp = <&aop_spuapp>; + firmware-name = "apple/maggie-t8010.bin"; + #sound-dai-cells = <0>; + }; + }; diff --git a/Documentation/devicetree/bindings/sound/cirrus,cs35l26.yaml b/Documentation/devicetree/bindings/sound/cirrus,cs35l26.yaml new file mode 100644 index 00000000000000..68f38a09f2319f --- /dev/null +++ b/Documentation/devicetree/bindings/sound/cirrus,cs35l26.yaml @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/sound/cirrus,cs35l26.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Cirrus Logic CS35L26 speaker amplifier + +maintainers: + - Paul Scalise + +description: | + Boosted speaker amplifier with a PDM and a TDM input, on I2C. The iPhone + 7 uses one for its top speaker, which is also its earpiece: the PDM + input takes the headset codec's receiver output, and the TDM input the + top speaker's slot of the loudspeaker frame. The amplifier has no DAI of + its own; a sound card connects its PDMIN and TDMIN inputs and its SPK + output by name. + +allOf: + - $ref: dai-common.yaml# + +properties: + compatible: + const: cirrus,cs35l26 + + reg: + maxItems: 1 + + reset-gpios: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: mclk + +required: + - compatible + - reg + - reset-gpios + - clocks + - clock-names + +unevaluatedProperties: false + +examples: + - | + #include + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + amplifier@40 { + compatible = "cirrus,cs35l26"; + reg = <0x40>; + reset-gpios = <&pinctrl_ap 140 GPIO_ACTIVE_LOW>; + clocks = <&aop_spuapp 0>; + clock-names = "mclk"; + sound-name-prefix = "Top"; + }; + }; diff --git a/arch/arm64/boot/dts/apple/t8010-7.dtsi b/arch/arm64/boot/dts/apple/t8010-7.dtsi index ece508cd8f22c4..a6eb8fb6eedb50 100644 --- a/arch/arm64/boot/dts/apple/t8010-7.dtsi +++ b/arch/arm64/boot/dts/apple/t8010-7.dtsi @@ -220,7 +220,24 @@ <0x2 0x10000500 0x0 0x100>, <0x2 0x10e00000 0x0 0xd8000>; firmware-name = "apple/aop-t8010.bin", "apple/aop-t8010-ext.bin"; + /* the ADT's iop-aop-nub has-baseband; m1n1 adds its aop-target */ + apple,has-baseband; status = "okay"; + + /* + * The loudspeaker service: the MCLK and enable of the top amplifier + * and the Maggie FPGA, and the AOP's own amplifiers. Their + * configurations are the ADT SPUApp node's spkamp-config_btmspk and + * spkamp-config_arc. + */ + aop_spuapp: spuapp { + compatible = "apple,t8010-aop-spuapp"; + #clock-cells = <1>; + apple,btmspk-config = /bits/ 8 <0x00 0x01 0x04 0x02 0x0d 0x00 0x08 0xf0 0xe8 0x40 + 0x00 0x00 0x30 0x01 0x00 0x00 0x00 0x00 0x00 0x00>; + apple,arc-config = /bits/ 8 <0x06 0x07 0x08 0x02 0x0d 0x00 0x08 0xf0 0xe8 0x43 + 0x00 0x00 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00>; + }; }; /* No audio service in the iPhone 7 AOP: the microphones are on the codec. */ @@ -284,6 +301,19 @@ &i2c2 { status = "okay"; + /* + * ADT i2c2/audio-speaker-top: the top speaker's amplifier, which also + * drives the earpiece, reset AP GPIO 140. Its MCLK comes from the AOP. + */ + speaker_top: amplifier@40 { + compatible = "cirrus,cs35l26"; + reg = <0x40>; + reset-gpios = <&pinctrl_ap 140 GPIO_ACTIVE_LOW>; + clocks = <&aop_spuapp 0>; + clock-names = "mclk"; + sound-name-prefix = "Top"; + }; + light-sensor@29 { compatible = "apple,ct821", "amstaos,tsl2581"; reg = <0x29>; @@ -383,13 +413,62 @@ clock-names = "mclk"; #sound-dai-cells = <0>; }; + + /* + * ADT spi1/maggie-arc: the FPGA of the loudspeakers and the Taptic + * Engine, SPI mode 3, CRESET_B on AP GPIO 22 and CDONE on GPIO 26. It + * takes the codec's ASP data line (GPIO 9) as a peripheral pin. + */ + maggie: fpga@1 { + compatible = "apple,t8010-maggie"; + reg = <1>; + spi-max-frequency = <12000000>; + spi-cpha; + spi-cpol; + reset-gpios = <&pinctrl_ap 22 GPIO_ACTIVE_LOW>; + cdone-gpios = <&pinctrl_ap 26 GPIO_ACTIVE_HIGH>; + pinctrl-names = "default"; + pinctrl-0 = <&codec_asp_dout>; + clocks = <&aop_spuapp 0>, <&aop_spuapp 1>; + clock-names = "mclk", "enable"; + apple,spuapp = <&aop_spuapp>; + firmware-name = "apple/maggie-t8010.bin"; + #sound-dai-cells = <0>; + }; +}; + +&pinctrl_ap { + codec_asp_dout: codec-asp-dout-pins { + pinmux = ; + }; }; -/* The built-in microphones, through MCA0 and the codec. */ +/* ADT mca1 function-i2s_route aud1 -> pin2: the frame comes from Maggie. */ +&mca1 { + compatible = "apple,t8010-mca-maggie"; + apple,i2s-switch = <&i2s_switch 1 2>; + dmas = <&sio 0x2a>; + dma-names = "tx"; + status = "okay"; +}; + +/* + * The earpiece and the microphones on MCA0 and the codec, the loudspeakers + * on MCA1 and Maggie. The top speaker is also the earpiece: its amplifier + * takes the codec's PDM output or Maggie's frame. + */ / { sound { compatible = "simple-audio-card"; simple-audio-card,name = "iPhone 7"; + simple-audio-card,widgets = "Speaker", "Speaker", + "Speaker", "Earpiece"; + simple-audio-card,routing = "Top PDMIN", "PDMOUT", + "Top TDMIN", "TDMOUT", + "Earpiece", "Top SPK", + "Speaker", "Top SPK", + "Speaker", "BOTSPK"; + simple-audio-card,aux-devs = <&speaker_top>; #address-cells = <1>; #size-cells = <0>; @@ -407,6 +486,21 @@ sound-dai = <&cs42l71>; }; }; + + simple-audio-card,dai-link@1 { + reg = <1>; + format = "dsp_a"; + bitclock-master = <&maggie_dai>; + frame-master = <&maggie_dai>; + + cpu { + sound-dai = <&mca1>; + }; + + maggie_dai: codec { + sound-dai = <&maggie>; + }; + }; }; }; diff --git a/arch/arm64/boot/dts/apple/t8010.dtsi b/arch/arm64/boot/dts/apple/t8010.dtsi index 7e573b008077db..e5385fef47d793 100644 --- a/arch/arm64/boot/dts/apple/t8010.dtsi +++ b/arch/arm64/boot/dts/apple/t8010.dtsi @@ -204,6 +204,21 @@ status = "disabled"; }; + mca1: mca@20a0a4000 { + compatible = "apple,t8010-mca"; + reg = <0x2 0x0a0a4000 0x0 0x4000>, + <0x2 0x0a002004 0x0 0x4>; + reg-names = "mca", "mclk"; + interrupts = ; + power-domains = <&ps_mca1>; + dmas = <&sio 0x2a>, <&sio 0x2b>; + dma-names = "tx", "rx"; + #clock-cells = <0>; + clock-output-names = "mca1-mclk"; + #sound-dai-cells = <0>; + status = "disabled"; + }; + mca2: mca@20a0a8000 { compatible = "apple,t8010-mca"; reg = <0x2 0x0a0a8000 0x0 0x4000>, diff --git a/arch/arm64/configs/orchard_apple16k_defconfig b/arch/arm64/configs/orchard_apple16k_defconfig index 7a35170c983ba2..2f837f32d90622 100644 --- a/arch/arm64/configs/orchard_apple16k_defconfig +++ b/arch/arm64/configs/orchard_apple16k_defconfig @@ -345,7 +345,9 @@ CONFIG_SND_SOC_APPLE_MCA=m CONFIG_SND_SOC_APPLE_S8000_MCA=m CONFIG_SND_SOC_APPLE_T8010_PDM=m CONFIG_SND_SOC_APPLE_J120_SPEAKERS=m +CONFIG_SND_SOC_APPLE_MAGGIE=m CONFIG_SND_SOC_CS35L19=m +CONFIG_SND_SOC_CS35L26=m CONFIG_SND_SOC_CS35L27=m CONFIG_SND_SOC_CS42L75=m CONFIG_SND_SOC_CS42L71=m @@ -409,6 +411,7 @@ CONFIG_APPLE_MAILBOX=y CONFIG_APPLE_PMGR_MISC=y CONFIG_APPLE_RTKIT=y CONFIG_APPLE_AOP_T8010=m +CONFIG_APPLE_AOP_SPUAPP=m CONFIG_APPLE_MESA=y CONFIG_APPLE_PMP_T8015_V1=m CONFIG_APPLE_SIO=m diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 5104ce52adf618..ba691210df8566 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -50,6 +50,17 @@ config APPLE_AOP_T8010 Say 'y' or 'm' here if you have an A9- to A12-based iPad or iPhone. +config APPLE_AOP_SPUAPP + tristate "Apple iPhone 7 AOP loudspeaker service" + depends on APPLE_AOP_T8010 + depends on COMMON_CLK + help + The iPhone 7's Always-On Processor switches the clock and enable of + the loudspeaker path and drives two of its amplifiers. This driver + provides them to the speaker drivers. + + Say 'y' or 'm' here if you have an iPhone 7. + config APPLE_AOP_SMC tristate "Apple AOP-SMC link" depends on APPLE_AOP_T8010 && MFD_MACSMC diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index c5d88c10c6364d..66e66ec2d079e8 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -19,6 +19,9 @@ obj-$(CONFIG_APPLE_AOP_T8010) += apple-aop.o obj-$(CONFIG_APPLE_MESA) += apple-mesa.o apple-aop-y := aop.o +obj-$(CONFIG_APPLE_AOP_SPUAPP) += apple-aop-spuapp.o +apple-aop-spuapp-y := aop-spuapp.o + obj-$(CONFIG_APPLE_AOP_SMC) += apple-aop-smc.o apple-aop-smc-y := aop-smc.o diff --git a/drivers/soc/apple/aop-spuapp.c b/drivers/soc/apple/aop-spuapp.c new file mode 100644 index 00000000000000..5357b8099100f3 --- /dev/null +++ b/drivers/soc/apple/aop-spuapp.c @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple AOP "SPUApp" service: the iPhone 7's loudspeaker helpers + * + * On the iPhone 7 the Always-On Processor's firmware owns two lines of the + * loudspeaker path and two of its three amplifiers. Its SPUApp service + * takes them as properties: + * + * 0xd0 the 12 MHz MCLK of the top amplifier and the Maggie FPGA (u32 1, 0) + * 0xd1 the Maggie FPGA's enable (u32 1, 0) + * 0xd2 the power state of an amplifier the AOP drives: its name in 16 + * bytes, then the state (0 off, 1 standby, 2 play, 3 play with + * voltage and current sense) + * 0xd3 that amplifier's configuration: its name, a length byte and up to + * 64 bytes + * + * The AOP drives the bottom speaker's amplifier ("btmspk") and the Taptic + * Engine's ("arc"), CS35L25s on its own I2C bus. Each gets its 20-byte + * configuration from Apple's device tree (the SPUApp node's + * spkamp-config_btmspk and spkamp-config_arc) before its first state change. + * A state change answers 0xe00002d5 (busy) while the amplifier ramps, and + * reading the property back gives the result once it is done. + * + * The firmware stops (and the AOP has to be restarted) on an "off" for a + * line that is not on. The two lines are therefore clocks, whose counts the + * clock framework keeps. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SPUAPP_PROP_MCLK 0xd0 +#define SPUAPP_PROP_MAGGIE 0xd1 +#define SPUAPP_PROP_AMP_STATE 0xd2 +#define SPUAPP_PROP_AMP_CONFIG 0xd3 + +#define SPUAPP_GET_PROPERTY 0xa /* EPIC subtype, next to APPLE_AOP_SET_PROPERTY */ +#define SPUAPP_AMP_NAME_LEN 16 +#define SPUAPP_AMP_CONFIG_MAX 64 +#define SPUAPP_IORETURN_BUSY 0xe00002d5 +#define SPUAPP_STATE_POLLS 20 +#define SPUAPP_STATE_POLL_MS 10 +#define SPUAPP_MCLK_RATE 12000000 + +struct apple_spuapp_clk { + struct clk_hw hw; + struct apple_spuapp *sp; + u32 prop; + unsigned long rate; +}; + +struct apple_spuapp_amp { + const char *name; + const char *prop; /* its configuration in the device tree */ + u8 cfg[SPUAPP_AMP_CONFIG_MAX]; + int len; /* 0: none */ + bool configured; +}; + +struct apple_spuapp { + struct device *dev; + struct apple_aop_service service; + struct mutex amp_lock; /* the amplifiers' configuration, a state change */ + struct apple_spuapp_amp amps[2]; + struct apple_spuapp_clk clk[2]; + struct clk_hw_onecell_data *clk_data; +}; + +/* A property set: 0 and the service's IOReturn in @status, or an error */ +static int spuapp_set(struct apple_spuapp *sp, u32 prop, const void *val, size_t len, + u32 *status) +{ + u8 buf[8 + SPUAPP_AMP_NAME_LEN + 1 + SPUAPP_AMP_CONFIG_MAX]; + + if (len > sizeof(buf) - 8) + return -EINVAL; + put_unaligned_le32(0, buf); + put_unaligned_le32(prop, buf + 4); + memcpy(buf + 8, val, len); + return apple_aop_call_status(&sp->service, APPLE_AOP_SET_PROPERTY, buf, 8 + len, + NULL, 0, status); +} + +static int spuapp_get(struct apple_spuapp *sp, u32 prop, void *out, size_t len, u32 *status) +{ + u8 buf[8]; + + put_unaligned_le32(0, buf); + put_unaligned_le32(prop, buf + 4); + return apple_aop_call_status(&sp->service, SPUAPP_GET_PROPERTY, buf, sizeof(buf), + out, len, status); +} + +static struct apple_spuapp_clk *to_spuapp_clk(struct clk_hw *hw) +{ + return container_of(hw, struct apple_spuapp_clk, hw); +} + +static int spuapp_line(struct apple_spuapp_clk *c, bool on) +{ + __le32 val = cpu_to_le32(on); + u32 status = 0; + int ret; + + ret = spuapp_set(c->sp, c->prop, &val, sizeof(val), &status); + if (!ret && status) + ret = -EIO; + if (ret) + dev_err(c->sp->dev, "%s %s: %d (status %#x)\n", clk_hw_get_name(&c->hw), + on ? "on" : "off", ret, status); + return ret; +} + +static int spuapp_clk_prepare(struct clk_hw *hw) +{ + return spuapp_line(to_spuapp_clk(hw), true); +} + +static void spuapp_clk_unprepare(struct clk_hw *hw) +{ + spuapp_line(to_spuapp_clk(hw), false); +} + +static unsigned long spuapp_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) +{ + return to_spuapp_clk(hw)->rate; +} + +static const struct clk_ops spuapp_clk_ops = { + .prepare = spuapp_clk_prepare, + .unprepare = spuapp_clk_unprepare, + .recalc_rate = spuapp_clk_recalc_rate, +}; + +/* Under amp_lock */ +static int spuapp_amp_configure(struct apple_spuapp *sp, struct apple_spuapp_amp *amp) +{ + u8 val[SPUAPP_AMP_NAME_LEN + 1 + SPUAPP_AMP_CONFIG_MAX] = {}; + u32 status = 0; + int ret; + + strscpy(val, amp->name, SPUAPP_AMP_NAME_LEN); + val[SPUAPP_AMP_NAME_LEN] = amp->len; + memcpy(val + SPUAPP_AMP_NAME_LEN + 1, amp->cfg, amp->len); + ret = spuapp_set(sp, SPUAPP_PROP_AMP_CONFIG, val, SPUAPP_AMP_NAME_LEN + 1 + amp->len, + &status); + if (!ret && status) { + dev_err(sp->dev, "amplifier %s configuration: status %#x\n", amp->name, status); + ret = -EIO; + } + amp->configured = !ret; + return ret; +} + +int apple_spuapp_amp_state(struct apple_spuapp *sp, const char *amp, u8 state) +{ + u8 val[SPUAPP_AMP_NAME_LEN + 1] = {}, out[SPUAPP_AMP_NAME_LEN + 1]; + u32 status = 0; + unsigned int i; + int ret = 0; + + strscpy(val, amp, SPUAPP_AMP_NAME_LEN); + val[SPUAPP_AMP_NAME_LEN] = state; + + mutex_lock(&sp->amp_lock); + for (i = 0; i < ARRAY_SIZE(sp->amps); i++) + if (!strcmp(sp->amps[i].name, amp)) + break; + if (i == ARRAY_SIZE(sp->amps)) { + ret = -EINVAL; + } else if (state != APPLE_SPUAPP_AMP_OFF && sp->amps[i].len && + !sp->amps[i].configured) { + ret = spuapp_amp_configure(sp, &sp->amps[i]); + } + if (!ret) + ret = spuapp_set(sp, SPUAPP_PROP_AMP_STATE, val, sizeof(val), &status); + for (i = 0; !ret && status == SPUAPP_IORETURN_BUSY && i < SPUAPP_STATE_POLLS; i++) { + msleep(SPUAPP_STATE_POLL_MS); + ret = spuapp_get(sp, SPUAPP_PROP_AMP_STATE, out, sizeof(out), &status); + } + mutex_unlock(&sp->amp_lock); + if (ret) + return ret; + if (status) { + dev_err(sp->dev, "amplifier %s state %u: %s %#x\n", amp, state, + status == SPUAPP_IORETURN_BUSY ? "still busy," : "status", status); + return status == SPUAPP_IORETURN_BUSY ? -ETIMEDOUT : -EIO; + } + return 0; +} +EXPORT_SYMBOL_GPL(apple_spuapp_amp_state); + +static void spuapp_put(void *data) +{ + put_device(data); +} + +struct apple_spuapp *devm_apple_spuapp_get(struct device *dev) +{ + struct platform_device *pdev; + struct device_node *np; + struct apple_spuapp *sp; + int ret; + + np = of_parse_phandle(dev->of_node, "apple,spuapp", 0); + if (!np) + return ERR_PTR(-ENODEV); + /* The AOP registers the device once its firmware announced the service. */ + pdev = of_find_device_by_node(np); + of_node_put(np); + if (!pdev) + return ERR_PTR(-EPROBE_DEFER); + ret = devm_add_action_or_reset(dev, spuapp_put, &pdev->dev); + if (ret) + return ERR_PTR(ret); + + sp = platform_get_drvdata(pdev); + if (!sp) + return ERR_PTR(-EPROBE_DEFER); + /* Unbind the consumer before the service goes away. */ + if (!device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) + return ERR_PTR(-EINVAL); + return sp; +} +EXPORT_SYMBOL_GPL(devm_apple_spuapp_get); + +static int apple_spuapp_probe(struct platform_device *pdev) +{ + const struct apple_aop_service *service = dev_get_platdata(&pdev->dev); + static const struct { + const char *name; + u32 prop; + unsigned long rate; + } lines[] = { + [APPLE_SPUAPP_CLK_MCLK] = { "mclk", SPUAPP_PROP_MCLK, SPUAPP_MCLK_RATE }, + [APPLE_SPUAPP_CLK_MAGGIE] = { "maggie", SPUAPP_PROP_MAGGIE, 0 }, + }; + struct device *dev = &pdev->dev; + struct apple_spuapp *sp; + unsigned int i; + int ret; + + if (!service) + return -ENODEV; + + sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL); + if (!sp) + return -ENOMEM; + sp->dev = dev; + sp->service = *service; + mutex_init(&sp->amp_lock); + + sp->amps[0] = (struct apple_spuapp_amp){ .name = "btmspk", .prop = "apple,btmspk-config" }; + sp->amps[1] = (struct apple_spuapp_amp){ .name = "arc", .prop = "apple,arc-config" }; + for (i = 0; i < ARRAY_SIZE(sp->amps); i++) { + struct apple_spuapp_amp *amp = &sp->amps[i]; + + amp->len = of_property_count_u8_elems(dev->of_node, amp->prop); + if (amp->len == -EINVAL) { + amp->len = 0; /* no configuration */ + continue; + } + if (amp->len <= 0 || amp->len > SPUAPP_AMP_CONFIG_MAX) + return dev_err_probe(dev, -EINVAL, "%s\n", amp->prop); + of_property_read_u8_array(dev->of_node, amp->prop, amp->cfg, amp->len); + } + + sp->clk_data = devm_kzalloc(dev, struct_size(sp->clk_data, hws, ARRAY_SIZE(lines)), + GFP_KERNEL); + if (!sp->clk_data) + return -ENOMEM; + sp->clk_data->num = ARRAY_SIZE(lines); + + for (i = 0; i < ARRAY_SIZE(lines); i++) { + struct clk_init_data init = { .ops = &spuapp_clk_ops }; + struct apple_spuapp_clk *c = &sp->clk[i]; + + init.name = devm_kasprintf(dev, GFP_KERNEL, "%s-%s", dev_name(dev), + lines[i].name); + if (!init.name) + return -ENOMEM; + c->sp = sp; + c->prop = lines[i].prop; + c->rate = lines[i].rate; + c->hw.init = &init; + ret = devm_clk_hw_register(dev, &c->hw); + if (ret) + return dev_err_probe(dev, ret, "%s clock\n", lines[i].name); + sp->clk_data->hws[i] = &c->hw; + } + + platform_set_drvdata(pdev, sp); + return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, sp->clk_data); +} + +static const struct of_device_id apple_spuapp_of_match[] = { + { .compatible = "apple,t8010-aop-spuapp" }, + {} +}; +MODULE_DEVICE_TABLE(of, apple_spuapp_of_match); + +static struct platform_driver apple_spuapp_driver = { + .driver = { + .name = "apple-aop-spuapp", + .of_match_table = apple_spuapp_of_match, + }, + .probe = apple_spuapp_probe, +}; +module_platform_driver(apple_spuapp_driver); + +MODULE_DESCRIPTION("Apple AOP SPUApp service (iPhone 7 loudspeaker path)"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/soc/apple/aop.c b/drivers/soc/apple/aop.c index f8d1eacb7a0b60..79108c4976582e 100644 --- a/drivers/soc/apple/aop.c +++ b/drivers/soc/apple/aop.c @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include @@ -282,6 +282,8 @@ struct apple_aop_hw { bool timebase_bootarg; /* alig, bbOn, pARC: from the AOP nub, as iBoot passes them */ bool board_bootargs; + /* Only the bbOn boot argument, from "apple,has-baseband" */ + bool baseband_bootarg; /* CpAd, WrAd and nCal, as iBoot passes them to an ASCWrapV2 coprocessor */ bool ascv2_bootargs; /* Offset of the image header's "uuid" magic */ @@ -703,6 +705,11 @@ static int apple_aop_load(struct apple_aop *aop, u64 ecid) &baseband_le, sizeof(baseband_le)); apple_aop_add_bootarg(args, &count, APPLE_AOP_BOOTARG_PARC, &arc, sizeof(arc)); } + if (aop->hw->baseband_bootarg) { + baseband_le = cpu_to_le32(of_property_read_bool(np, "apple,has-baseband")); + apple_aop_add_bootarg(args, &count, APPLE_AOP_BOOTARG_BBON, + &baseband_le, sizeof(baseband_le)); + } if (aop->hw->ascv2_bootargs) { coproc_le = cpu_to_le64(aop->control_base - APPLE_AOP_ASCV2_RVBAR_OFFSET); wrapper_le = cpu_to_le64(aop->akf_base); @@ -815,11 +822,15 @@ static void apple_aop_register_service(struct work_struct *work) char name[32]; np = of_get_child_by_name(aop->dev->of_node, rw->service.name); - /* Device tree node names use '-' where a firmware may use '_'. */ - if (!np && strchr(rw->service.name, '_')) { - strscpy(name, rw->service.name, sizeof(name)); + /* + * Device tree node names are lowercase and use '-' where a firmware + * may use capitals or '_' (the iPhone 7's "SPUApp" is node "spuapp"). + */ + if (!np) { + string_lower(name, rw->service.name); strreplace(name, '_', '-'); - np = of_get_child_by_name(aop->dev->of_node, name); + if (strcmp(name, rw->service.name)) + np = of_get_child_by_name(aop->dev->of_node, name); } if (!np || !of_device_is_available(np)) goto out; @@ -1179,8 +1190,9 @@ static int apple_aop_send_epic(struct apple_aop_ep *ep, u32 channel, u16 subtype return apple_aop_send(ep, AFK_OPC_SEND, FIELD_PREP(AFK_MSG_WPTR, wptr)); } -int apple_aop_call(const struct apple_aop_service *service, u16 subtype, const void *data, - size_t len, void *reply, size_t reply_len) +int apple_aop_call_status(const struct apple_aop_service *service, u16 subtype, + const void *data, size_t len, void *reply, size_t reply_len, + u32 *status) { struct apple_aop_ep *ep = apple_aop_ep(service->aop, service->endpoint); struct apple_aop_call call = { @@ -1217,7 +1229,21 @@ int apple_aop_call(const struct apple_aop_service *service, u16 subtype, const v mutex_unlock(&ep->lock); } - return call.retcode > INT_MAX ? -EIO : call.retcode; + *status = call.retcode; + return 0; +} +EXPORT_SYMBOL_GPL(apple_aop_call_status); + +int apple_aop_call(const struct apple_aop_service *service, u16 subtype, const void *data, + size_t len, void *reply, size_t reply_len) +{ + u32 status; + int ret; + + ret = apple_aop_call_status(service, subtype, data, len, reply, reply_len, &status); + if (ret) + return ret; + return status > INT_MAX ? -EIO : status; } EXPORT_SYMBOL_GPL(apple_aop_call); @@ -1512,12 +1538,14 @@ static const struct apple_aop_hw apple_aop_t8011_hw = { /* * The iPhone 7 and 7 Plus share one firmware (t8010aop), which tells the - * boards apart by the AOP target, as iOS passes it (4 on the D101). + * boards apart by the AOP target, as iOS passes it (4 on the D101). It + * serves the loudspeaker amplifiers only when bbOn says there is a baseband. */ static const struct apple_aop_hw apple_aop_d10_hw = { .header = 0x20, .nonce = true, .target_bootarg = true, + .baseband_bootarg = true, }; /* diff --git a/drivers/soc/apple/apple-sio-core.c b/drivers/soc/apple/apple-sio-core.c index b8008bcdd252f1..19a3312b280b2f 100644 --- a/drivers/soc/apple/apple-sio-core.c +++ b/drivers/soc/apple/apple-sio-core.c @@ -1204,11 +1204,12 @@ static const struct apple_sio_hw apple_sio_s8000_hw = { /* * SPI3 touch TX, MCA2 speaker TX, MCA4 headphone TX and PDM microphone RX, - * each described by the J71b ADT, and MCA0 TX and RX, by the D10's. + * each described by the J71b ADT, and MCA0 TX and RX and MCA1 TX, by the + * D10's. */ static const struct apple_sio_dma_range apple_sio_t8010_dma_ranges[] = { { 0x1a, 0x1a }, - { 0x28, 0x29 }, + { 0x28, 0x2a }, { 0x2c, 0x2c }, { 0x30, 0x30 }, { 0x5b, 0x5b }, diff --git a/include/linux/soc/apple/aop-spuapp.h b/include/linux/soc/apple/aop-spuapp.h new file mode 100644 index 00000000000000..1f6a73a9c5a3e9 --- /dev/null +++ b/include/linux/soc/apple/aop-spuapp.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * Apple AOP "SPUApp" service of the iPhone 7: the amplifiers the AOP drives + */ +#ifndef _LINUX_SOC_APPLE_AOP_SPUAPP_H_ +#define _LINUX_SOC_APPLE_AOP_SPUAPP_H_ + +#include + +struct device; +struct apple_spuapp; + +/* The clocks of the service's node (#clock-cells = <1>) */ +#define APPLE_SPUAPP_CLK_MCLK 0 /* 12 MHz to the top amplifier and Maggie */ +#define APPLE_SPUAPP_CLK_MAGGIE 1 /* the Maggie FPGA's enable */ + +/* Power states of an amplifier the AOP drives */ +#define APPLE_SPUAPP_AMP_OFF 0 +#define APPLE_SPUAPP_AMP_STANDBY 1 +#define APPLE_SPUAPP_AMP_PLAY 2 +#define APPLE_SPUAPP_AMP_PLAY_SENSE 3 /* with voltage and current sense */ + +/* The service of the "apple,spuapp" phandle of @dev, for as long as @dev is bound */ +struct apple_spuapp *devm_apple_spuapp_get(struct device *dev); + +/* + * Take amplifier @amp ("btmspk", "arc") to APPLE_SPUAPP_AMP_* @state, after + * its configuration if it has not had it yet. Returns once the amplifier + * ramped. + */ +int apple_spuapp_amp_state(struct apple_spuapp *sp, const char *amp, u8 state); + +#endif /* _LINUX_SOC_APPLE_AOP_SPUAPP_H_ */ diff --git a/include/linux/soc/apple/aop.h b/include/linux/soc/apple/aop.h index b5188a0b10278d..a44f4c79272ce2 100644 --- a/include/linux/soc/apple/aop.h +++ b/include/linux/soc/apple/aop.h @@ -42,6 +42,15 @@ void apple_aop_unlisten(const struct apple_aop_service *service); int apple_aop_call(const struct apple_aop_service *service, u16 subtype, const void *data, size_t len, void *reply, size_t reply_len); +/* + * As apple_aop_call(), but returns 0 once the service replied, whatever its + * return code, which goes to @status (an IOReturn, such as 0xe00002d5 for a + * request the service accepted and still works on). + */ +int apple_aop_call_status(const struct apple_aop_service *service, u16 subtype, + const void *data, size_t len, void *reply, size_t reply_len, + u32 *status); + /* The EPIC notification subtype that sets a service property */ #define APPLE_AOP_SET_PROPERTY 0x4 diff --git a/sound/soc/apple/Kconfig b/sound/soc/apple/Kconfig index 59b0b831f6ad9f..302b25e2703039 100644 --- a/sound/soc/apple/Kconfig +++ b/sound/soc/apple/Kconfig @@ -35,6 +35,17 @@ config SND_SOC_APPLE_T8010_PDM controller of the Apple A10, which records the built-in microphones of the iPad 6 and iPad 7. +config SND_SOC_APPLE_MAGGIE + tristate "Apple Maggie FPGA (iPhone 7 loudspeakers)" + depends on ARCH_APPLE || COMPILE_TEST + depends on SPI && GPIOLIB && COMMON_CLK + depends on APPLE_AOP_SPUAPP + help + The FPGA behind the iPhone 7's loudspeakers, which clocks their + frame on MCA1. The driver loads its bitstream, provides the + frame while the loudspeakers play, and drives the bottom + speaker's amplifier through the Always-On Processor. + config SND_SOC_APPLE_T8020_SPEAKERS tristate "Apple A12 speaker card" depends on SND_SOC_APPLE_MCA && OF diff --git a/sound/soc/apple/Makefile b/sound/soc/apple/Makefile index 1096a329adf360..ea9d6a4ae94e3c 100644 --- a/sound/soc/apple/Makefile +++ b/sound/soc/apple/Makefile @@ -2,10 +2,12 @@ snd-soc-apple-mca-y := mca.o snd-soc-apple-s8000-mca-y := s8000-mca.o snd-soc-apple-t8010-pdm-y := t8010-pdm.o +snd-soc-apple-maggie-y := maggie.o obj-$(CONFIG_SND_SOC_APPLE_MCA) += snd-soc-apple-mca.o obj-$(CONFIG_SND_SOC_APPLE_S8000_MCA) += snd-soc-apple-s8000-mca.o obj-$(CONFIG_SND_SOC_APPLE_T8010_PDM) += snd-soc-apple-t8010-pdm.o +obj-$(CONFIG_SND_SOC_APPLE_MAGGIE) += snd-soc-apple-maggie.o obj-$(CONFIG_SND_SOC_APPLE_J120_SPEAKERS) += j120-speaker-amps.o snd-soc-apple-t8020-speakers-y := t8020-speakers.o obj-$(CONFIG_SND_SOC_APPLE_T8020_SPEAKERS) += snd-soc-apple-t8020-speakers.o diff --git a/sound/soc/apple/maggie.c b/sound/soc/apple/maggie.c new file mode 100644 index 00000000000000..c5c1968dcc349c --- /dev/null +++ b/sound/soc/apple/maggie.c @@ -0,0 +1,361 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple "Maggie" FPGA: the iPhone 7's loudspeaker frame + * + * The iPhone 7's loudspeakers and Taptic Engine sit behind a Lattice iCE5LP4K + * FPGA ("Maggie") on SPI1 chip select 1. It provides the TDM clocks of MCA1 + * (fifteen 16-bit slots and ten idle bit clocks per frame, 12 MHz at + * 48 kHz) and passes the bottom speaker's slot to its amplifier, a CS35L25 + * that the Always-On Processor drives, and the top speaker's slot to the + * CS35L26 on the AP's I2C bus. + * + * The FPGA has no flash. Its bitstream is the payload of the restore image's + * Firmware/Maggie/AppleMaggieFirmwareImage.im4p, and it is loaded the way + * iOS loads it: CRESET_B low, the chip select low with eight clocks, + * CRESET_B high, the image, then a CRC check, its read back, the re-sync and + * wake-up commands, and dummy clocks until CDONE goes high. It then runs + * while the AOP provides its MCLK and its enable. + * + * Its registers have 16-bit addresses and data. A write is {A_hi, A_lo, 0, + * D_hi, D_lo, ...}, with the address counting up for bursts of up to 128 + * words, and a read is {A_hi, A_lo, 0x80}, followed by the value. Everything + * below 0x7000 is write-only. The register port is slower than the + * configuration port: above 6 MHz a read comes back one to three bits late, + * so the registers are accessed at 4 MHz. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAGGIE_CRC 0x70ac +#define MAGGIE_REG_VERSION 0x7000 +#define MAGGIE_REG_ID 0x7002 +#define MAGGIE_ID 0x2e8b +#define MAGGIE_REG_HZ 4000000 +#define MAGGIE_LOAD_TRIES 5 +#define MAGGIE_FW_WAITS 60 /* seconds for the root file system */ + +struct maggie { + struct spi_device *spi; + struct device *dev; + struct gpio_desc *reset, *cdone; + struct clk *mclk, *enable; + struct apple_spuapp *spuapp; + const char *fw_name; + struct mutex lock; /* loading the FPGA */ + struct delayed_work load_work; + unsigned int load_waits; + bool loaded; +}; + +static int maggie_xfer(struct maggie *mg, const void *tx, void *rx, size_t len, bool keep_cs) +{ + struct spi_transfer t = { + .tx_buf = tx, .rx_buf = rx, .len = len, .cs_change = keep_cs, + }; + struct spi_message m; + + spi_message_init_with_transfers(&m, &t, 1); + return spi_sync_locked(mg->spi, &m); +} + +/* + * Apple's image ends with the CRC check and wake-up commands, 22 01 06 + * 00. The load sends them itself, with a read back of the CRC in between, + * so they stay out of the image and the CRC comes from them. An image + * without them is sent whole and checked against the known CRC. + */ +static size_t maggie_image_size(const struct firmware *fw, u16 *crc) +{ + const u8 *tail = fw->data + fw->size - 6; + + if (fw->size > 6 && tail[0] == 0x22 && tail[3] == 0x01 && tail[4] == 0x06 && + !tail[5]) { + *crc = get_unaligned_be16(tail + 1); + return fw->size - 6; + } + *crc = MAGGIE_CRC; + return fw->size; +} + +static int maggie_load_once(struct maggie *mg, const struct firmware *fw) +{ + static const u8 crc_rd[] = { 0x01, 0x0c, 0x00, 0xff, 0xff }; + static const u8 resync[] = { 0x7e, 0xaa, 0x99, 0x7e, 0x01, 0x0b }; + static const u8 wakeup[] = { 0x01, 0x06 }; + u8 *buf, *crc, rx[5] = {}; + bool done = false; + size_t size; + int ret, i; + u16 want; + u32 back; + + /* 15 zero bytes of dummy clocks, then the CRC check command */ + buf = kzalloc(16 + 3, GFP_KERNEL); + if (!buf) + return -ENOMEM; + size = maggie_image_size(fw, &want); + crc = buf + 16; + crc[0] = 0x22; + put_unaligned_be16(want, crc + 1); + spi_bus_lock(mg->spi->controller); + gpiod_set_value_cansleep(mg->reset, 1); /* CRESET_B low */ + usleep_range(1000, 1500); + ret = maggie_xfer(mg, buf, NULL, 1, true); /* CS low, 8 clocks */ + udelay(1); + gpiod_set_value_cansleep(mg->reset, 0); /* SPI configuration mode */ + usleep_range(1000, 1500); + ret = ret ?: maggie_xfer(mg, fw->data, NULL, size, true); + ret = ret ?: maggie_xfer(mg, crc, NULL, 3, true); + ret = ret ?: maggie_xfer(mg, crc_rd, rx, sizeof(crc_rd), true); + ret = ret ?: maggie_xfer(mg, resync, NULL, sizeof(resync), true); + ret = ret ?: maggie_xfer(mg, wakeup, NULL, sizeof(wakeup), true); + for (i = 0; !ret && i < 5 && !done; i++) { + ret = maggie_xfer(mg, buf, NULL, 15, true); /* 120 dummy clocks */ + done = gpiod_get_value_cansleep(mg->cdone) == 1; + if (!done) + usleep_range(10000, 11000); + } + maggie_xfer(mg, NULL, NULL, 0, false); /* CS high */ + spi_bus_unlock(mg->spi->controller); + kfree(buf); + if (ret) + return ret; + + back = ((rx[2] << 16 | rx[3] << 8 | rx[4]) >> 6) & 0xffff; + if (back != want) + dev_warn(mg->dev, "CRC read back %#x, expected %#x\n", back, want); + return done ? 0 : -EIO; +} + +static int maggie_read(struct maggie *mg, u16 addr, u16 *val) +{ + u8 tx[5] = { addr >> 8, addr & 0xff, 0x80 }, rx[5]; + struct spi_transfer t = { + .tx_buf = tx, .rx_buf = rx, .len = sizeof(tx), .speed_hz = MAGGIE_REG_HZ, + }; + int ret = spi_sync_transfer(mg->spi, &t, 1); + + if (!ret) + *val = rx[3] << 8 | rx[4]; + return ret; +} + +static void maggie_load_work(struct work_struct *work) +{ + struct maggie *mg = container_of(to_delayed_work(work), struct maggie, load_work); + const struct firmware *fw; + int ret, i; + + ret = request_firmware_direct(&fw, mg->fw_name, mg->dev); + if (ret == -ENOENT && ++mg->load_waits < MAGGIE_FW_WAITS) { + schedule_delayed_work(&mg->load_work, HZ); /* no root file system yet */ + return; + } + if (ret) { + dev_err(mg->dev, "firmware %s: %d\n", mg->fw_name, ret); + return; + } + + mutex_lock(&mg->lock); + for (i = 0; i < MAGGIE_LOAD_TRIES; i++) { + ret = maggie_load_once(mg, fw); + if (!ret) + break; + } + if (ret) { + dev_err(mg->dev, "configuration failed: %d\n", ret); + } else { + WRITE_ONCE(mg->loaded, true); + dev_info(mg->dev, "FPGA configured (%zu bytes)\n", fw->size); + } + mutex_unlock(&mg->lock); + release_firmware(fw); +} + +static int maggie_wait_loaded(struct maggie *mg) +{ + int i; + + for (i = 0; i < 100 && !READ_ONCE(mg->loaded); i++) + msleep(50); + return READ_ONCE(mg->loaded) ? 0 : -ENODEV; +} + +/* The AOP's MCLK and enable for the FPGA, and the frame with them */ +static int maggie_power(struct maggie *mg, bool on) +{ + int ret; + + if (!on) { + clk_disable_unprepare(mg->enable); + clk_disable_unprepare(mg->mclk); + return 0; + } + ret = clk_prepare_enable(mg->mclk); + if (ret) + return ret; + ret = clk_prepare_enable(mg->enable); + if (ret) + clk_disable_unprepare(mg->mclk); + return ret; +} + +static int maggie_frame_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kc, + int event) +{ + struct maggie *mg = snd_soc_component_get_drvdata(snd_soc_dapm_to_component(w->dapm)); + u16 id = 0, ver = 0; + int ret; + + if (!SND_SOC_DAPM_EVENT_ON(event)) + return maggie_power(mg, false); + + ret = maggie_wait_loaded(mg); + if (ret) + return ret; + ret = maggie_power(mg, true); + if (ret) + return ret; + /* the first read after the enable returns 0 */ + maggie_read(mg, MAGGIE_REG_VERSION, &ver); + if (!maggie_read(mg, MAGGIE_REG_ID, &id) && id != MAGGIE_ID) { + maggie_read(mg, MAGGIE_REG_VERSION, &ver); + dev_warn(mg->dev, "ID %#06x, version %#06x\n", id, ver); + } + return 0; +} + +/* The bottom speaker's amplifier, which the AOP drives */ +static int maggie_bottom_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kc, + int event) +{ + struct maggie *mg = snd_soc_component_get_drvdata(snd_soc_dapm_to_component(w->dapm)); + + return apple_spuapp_amp_state(mg->spuapp, "btmspk", + SND_SOC_DAPM_EVENT_ON(event) ? APPLE_SPUAPP_AMP_PLAY : + APPLE_SPUAPP_AMP_OFF); +} + +static const struct snd_soc_dapm_widget maggie_widgets[] = { + SND_SOC_DAPM_AIF_IN("TDM RX", "Speaker Playback", 0, SND_SOC_NOPM, 0, 0), + SND_SOC_DAPM_PGA_E("Frame", SND_SOC_NOPM, 0, 0, NULL, 0, maggie_frame_event, + SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), + SND_SOC_DAPM_OUT_DRV_E("Bottom Amp", SND_SOC_NOPM, 0, 0, NULL, 0, maggie_bottom_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + SND_SOC_DAPM_OUTPUT("BOTSPK"), + SND_SOC_DAPM_OUTPUT("TDMOUT"), +}; + +static const struct snd_soc_dapm_route maggie_routes[] = { + { "Frame", NULL, "TDM RX" }, + { "Bottom Amp", NULL, "Frame" }, + { "BOTSPK", NULL, "Bottom Amp" }, + { "TDMOUT", NULL, "Frame" }, +}; + +static const struct snd_soc_component_driver maggie_component = { + .name = "maggie", + .dapm_widgets = maggie_widgets, + .num_dapm_widgets = ARRAY_SIZE(maggie_widgets), + .dapm_routes = maggie_routes, + .num_dapm_routes = ARRAY_SIZE(maggie_routes), + .endianness = 1, +}; + +static struct snd_soc_dai_driver maggie_dai = { + .name = "maggie-tdm", + .playback = { + .stream_name = "Speaker Playback", + .channels_min = 2, + .channels_max = 2, + .rates = SNDRV_PCM_RATE_48000, + .formats = SNDRV_PCM_FMTBIT_S16_LE, + }, +}; + +static int maggie_probe(struct spi_device *spi) +{ + struct device *dev = &spi->dev; + struct maggie *mg; + int ret; + + mg = devm_kzalloc(dev, sizeof(*mg), GFP_KERNEL); + if (!mg) + return -ENOMEM; + mg->spi = spi; + mg->dev = dev; + mutex_init(&mg->lock); + INIT_DELAYED_WORK(&mg->load_work, maggie_load_work); + spi_set_drvdata(spi, mg); + + ret = of_property_read_string(dev->of_node, "firmware-name", &mg->fw_name); + if (ret) + return dev_err_probe(dev, ret, "firmware-name\n"); + mg->spuapp = devm_apple_spuapp_get(dev); + if (IS_ERR(mg->spuapp)) + return dev_err_probe(dev, PTR_ERR(mg->spuapp), "AOP SPUApp service\n"); + mg->mclk = devm_clk_get(dev, "mclk"); + if (IS_ERR(mg->mclk)) + return dev_err_probe(dev, PTR_ERR(mg->mclk), "MCLK\n"); + mg->enable = devm_clk_get(dev, "enable"); + if (IS_ERR(mg->enable)) + return dev_err_probe(dev, PTR_ERR(mg->enable), "enable\n"); + /* iOS holds the FPGA in reset (CRESET_B low) until it loads it */ + mg->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); + if (IS_ERR(mg->reset)) + return dev_err_probe(dev, PTR_ERR(mg->reset), "reset GPIO\n"); + mg->cdone = devm_gpiod_get(dev, "cdone", GPIOD_IN); + if (IS_ERR(mg->cdone)) + return dev_err_probe(dev, PTR_ERR(mg->cdone), "CDONE GPIO\n"); + + ret = devm_snd_soc_register_component(dev, &maggie_component, &maggie_dai, 1); + if (ret) + return ret; + schedule_delayed_work(&mg->load_work, 0); + return 0; +} + +static void maggie_remove(struct spi_device *spi) +{ + struct maggie *mg = spi_get_drvdata(spi); + + cancel_delayed_work_sync(&mg->load_work); +} + +static const struct of_device_id maggie_of_match[] = { + { .compatible = "apple,t8010-maggie" }, + {} +}; +MODULE_DEVICE_TABLE(of, maggie_of_match); + +static const struct spi_device_id maggie_spi_id[] = { + { "t8010-maggie" }, + {} +}; +MODULE_DEVICE_TABLE(spi, maggie_spi_id); + +static struct spi_driver maggie_driver = { + .driver = { + .name = "apple-maggie", + .of_match_table = maggie_of_match, + }, + .id_table = maggie_spi_id, + .probe = maggie_probe, + .remove = maggie_remove, +}; +module_spi_driver(maggie_driver); + +MODULE_DESCRIPTION("Apple Maggie FPGA (iPhone 7 loudspeakers)"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/sound/soc/apple/s8000-mca.c b/sound/soc/apple/s8000-mca.c index 57cbee8a45200f..d650114a77780e 100644 --- a/sound/soc/apple/s8000-mca.c +++ b/sound/soc/apple/s8000-mca.c @@ -1419,6 +1419,42 @@ static const struct s8000_mca_hw t8010_codec_mca_hw = { .sio_trigger = 0x180, }; +/* + * D10 (iPhone 7) MCA1 TX to the loudspeakers, from the ADT's + * /arm-io/mca1/virtual-mca1/virtual-mca1a/audio-speaker-top iisConfig: + * + * 00 12 00 00 0f 10 01 00 00 00 00 00 fa 00 01 00 + * 30 00 01 00 03 00 00 00 3e 00 00 00 02 05 10 10 + * + * clock consumer, format flags 0x12, 15 slots of 16 bits in a fixed 250-bit + * frame (ten idle bit clocks) with a 1-bit sync pulse, the bottom and top + * speakers in slots 0 and 1. The Maggie FPGA provides the clocks, and the + * SmartIO shim words are virtual-mca1a's dma-channels for endpoint 0x2a + * (FIFO 0x120, trigger 0x90). The sense slots 1 to 5 of RX are not used. + * + * As on N66, the output is capped at -12 dB until there is speaker + * protection. + */ +static const struct s8000_mca_hw t8010_maggie_mca_hw = { + .version = 5, + .regs = &mca_v4_regs, + .dai_format = SND_SOC_DAIFMT_DSP_A, + .channels = 2, + .slots = 15, + .frame_bits = 250, + .tx_slots = 0x3, + .tx_cfg = 0x133, + .rx_cfg = 0x137, + .formats = SNDRV_PCM_FMTBIT_S16_LE, + .sync_bits = 1, + .attenuation = 4, + .sio_xfer16 = 0x21, + .sio_xfer32 = 0x22, + .sio_timeout = 0, + .sio_fifo = 0x120, + .sio_trigger = 0x90, +}; + /* * N66 (iPhone 6s Plus) MCA2 TX to the CS35L19 speaker amplifier, from the * ADT's /arm-io/mca2/audio-speaker iisConfig (the phone's live ADT, iOS 15): @@ -1656,6 +1692,7 @@ static const struct of_device_id s8000_mca_of_match[] = { { .compatible = "apple,t8010-mca", .data = &t8010_mca_hw }, { .compatible = "apple,t8010-mca-codec", .data = &t8010_codec_mca_hw }, { .compatible = "apple,t8010-mca-i2s", .data = &t8010_i2s_mca_hw }, + { .compatible = "apple,t8010-mca-maggie", .data = &t8010_maggie_mca_hw }, { } }; MODULE_DEVICE_TABLE(of, s8000_mca_of_match); diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig index b06c513fdd50b4..5104f808c784e4 100644 --- a/sound/soc/codecs/Kconfig +++ b/sound/soc/codecs/Kconfig @@ -814,6 +814,13 @@ config SND_SOC_CS35L19 and, as the CS35L21, of the iPhone 6s and 6s Plus: a sibling of the CS35L33, with Apple's power-on sequence. +config SND_SOC_CS35L26 + tristate "Cirrus Logic CS35L26 speaker amplifier" + depends on I2C && GPIOLIB && COMMON_CLK + help + The boosted speaker amplifier of the iPhone 7's top speaker, which + is also its earpiece. + config SND_SOC_CS42L75 tristate "Cirrus Logic CS42L75 codec" depends on SPI_MASTER && OF && COMMON_CLK diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile index 08e16d013925d9..51aae5974dae6c 100644 --- a/sound/soc/codecs/Makefile +++ b/sound/soc/codecs/Makefile @@ -66,6 +66,7 @@ snd-soc-cs-amp-lib-y := cs-amp-lib.o snd-soc-cs-amp-lib-test-y := cs-amp-lib-test.o snd-soc-cs35l32-y := cs35l32.o snd-soc-cs35l19-y := cs35l19.o +snd-soc-cs35l26-y := cs35l26.o snd-soc-cs35l27-y := cs35l27.o snd-soc-cs42l75-y := cs42l75.o snd-soc-cs35l33-y := cs35l33.o @@ -516,6 +517,7 @@ obj-$(CONFIG_SND_SOC_CS_AMP_LIB) += snd-soc-cs-amp-lib.o obj-$(CONFIG_SND_SOC_CS_AMP_LIB_TEST) += snd-soc-cs-amp-lib-test.o obj-$(CONFIG_SND_SOC_CS35L32) += snd-soc-cs35l32.o obj-$(CONFIG_SND_SOC_CS35L19) += snd-soc-cs35l19.o +obj-$(CONFIG_SND_SOC_CS35L26) += snd-soc-cs35l26.o obj-$(CONFIG_SND_SOC_CS42L75) += snd-soc-cs42l75.o obj-$(CONFIG_SND_SOC_CS35L27) += snd-soc-cs35l27.o obj-$(CONFIG_SND_SOC_CS35L33) += snd-soc-cs35l33.o diff --git a/sound/soc/codecs/cs35l26.c b/sound/soc/codecs/cs35l26.c new file mode 100644 index 00000000000000..f9c41c566a64c8 --- /dev/null +++ b/sound/soc/codecs/cs35l26.c @@ -0,0 +1,338 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Cirrus Logic CS35L26 boosted speaker amplifier (iPhone 7) + * + * The iPhone 7's top speaker, which is also its earpiece, has a CS35L26 on + * the AP's I2C bus. There is no public datasheet. The register map has 8-bit + * addresses and data and resembles the CS35L35's, and the sequences below + * are the values iOS writes. The amplifier comes out of reset set up for the + * board's buses, so neither iOS nor this driver touch the clock and serial + * format registers. + * + * It has two inputs. The PDM input carries the CS42L71 codec's receiver + * output (the earpiece). The TDM input carries the top speaker's slot of + * the loudspeaker frame, which the Maggie FPGA clocks. Both need the 12 MHz + * MCLK, which the Always-On Processor switches. iOS keeps the amplifier in + * reset whenever it is not playing, and so does this driver. + * + * The speaker monitor next to it (I2C address 0x21) is put in standby + * whenever the amplifier powers up, as iOS does. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define CS35L26_CHIP_ID 0x01 /* 0x35 0xa2 0x6x */ +#define CS35L26_PWR_CTL1 0x06 +#define CS35L26_PWR_CTL2 0x07 +#define CS35L26_PWR_CTL3 0x08 +#define CS35L26_AMP_INP 0x14 +#define CS35L26_BST_VOL 0x1d +#define CS35L26_TDM_RX_SLOT 0x5e +#define CS35L26_STATUS 0x74 /* 0x74..0x77, read to clear */ +#define CS35L26_STATUS2 0x75 +#define CS35L26_PDN_DONE BIT(4) + +/* The top speaker's slot in the loudspeaker frame (slot 1) */ +#define CS35L26_TDM_SLOT_TOP 0x24 + +#define CS35L26_MONITOR_ADDR 0x21 +#define CS35L26_MONITOR_STANDBY 0x02 + +struct cs35l26 { + struct i2c_client *i2c; + struct i2c_client *monitor; + struct gpio_desc *reset; + struct clk *mclk; + struct mutex lock; /* the power state below */ + bool on; + bool tdm; /* on with the TDM input (speaker), else PDM (earpiece) */ +}; + +static int cs35l26_write(struct cs35l26 *amp, u8 reg, u8 val) +{ + int ret = i2c_smbus_write_byte_data(amp->i2c, reg, val); + + if (ret) + dev_err(&->i2c->dev, "write %#04x = %#04x: %d\n", reg, val, ret); + return ret; +} + +static int cs35l26_read(struct cs35l26 *amp, u8 reg) +{ + return i2c_smbus_read_byte_data(amp->i2c, reg); +} + +static int cs35l26_write_seq(struct cs35l26 *amp, const u8 (*seq)[2], unsigned int n) +{ + unsigned int i; + int ret = 0; + + for (i = 0; i < n && !ret; i++) + ret = cs35l26_write(amp, seq[i][0], seq[i][1]); + return ret; +} + +static void cs35l26_clear_status(struct cs35l26 *amp) +{ + int i, j; + + for (i = 0; i < 2; i++) + for (j = 0; j < 4; j++) + cs35l26_read(amp, CS35L26_STATUS + j); +} + +/* Out of reset with MCLK running, the speaker monitor in standby */ +static int cs35l26_wake(struct cs35l26 *amp) +{ + int ret; + + ret = clk_prepare_enable(amp->mclk); + if (ret) + return ret; + gpiod_set_value_cansleep(amp->reset, 0); + usleep_range(1000, 2000); + return 0; +} + +static void cs35l26_sleep(struct cs35l26 *amp) +{ + gpiod_set_value_cansleep(amp->reset, 1); + clk_disable_unprepare(amp->mclk); +} + +static void cs35l26_monitor_standby(struct cs35l26 *amp) +{ + if (amp->monitor) + i2c_smbus_write_byte_data(amp->monitor, 0x00, CS35L26_MONITOR_STANDBY); +} + +/* The PDM input: the codec's receiver output, for the earpiece */ +static int cs35l26_pdm_on(struct cs35l26 *amp) +{ + static const u8 seq[][2] = { + { CS35L26_PWR_CTL3, 0x3e }, { CS35L26_AMP_INP, 0x41 }, + { CS35L26_PWR_CTL2, 0xfb }, { 0x17, 0x0d }, { CS35L26_PWR_CTL1, 0x00 }, + { CS35L26_PWR_CTL2, 0xfa }, + }; + int ret; + + ret = cs35l26_wake(amp); + if (ret) + return ret; + cs35l26_monitor_standby(amp); + ret = cs35l26_write_seq(amp, seq, ARRAY_SIZE(seq)); + if (ret) { + cs35l26_sleep(amp); + return ret; + } + fsleep(8000); + cs35l26_clear_status(amp); + amp->on = true; + amp->tdm = false; + return 0; +} + +/* The TDM input: the top speaker's slot of the loudspeaker frame */ +static int cs35l26_tdm_on(struct cs35l26 *amp) +{ + static const u8 seq1[][2] = { + { 0x1e, 0x0b }, { 0x17, 0x0d }, { 0x15, 0x00 }, + { 0x3a, 0x08 }, { 0x3b, 0xf0 }, { 0x3c, 0xe8 }, { 0x3d, 0x40 }, + { 0x52, 0x80 }, { 0x53, 0x80 }, { 0x54, 0x80 }, { 0x55, 0x80 }, + { 0x56, 0x80 }, { 0x57, 0x80 }, { 0x58, 0x80 }, { 0x59, 0x80 }, + { 0x61, 0x80 }, { 0x5a, 0x00 }, { 0x5b, 0x00 }, { 0x5c, 0x00 }, { 0x5d, 0x00 }, + { CS35L26_TDM_RX_SLOT, CS35L26_TDM_SLOT_TOP }, + }; + static const u8 seq2[][2] = { + { 0x5f, 0x80 }, { 0x60, 0x80 }, + { CS35L26_PWR_CTL3, 0x3c }, { 0x16, 0x00 }, { CS35L26_PWR_CTL1, 0x00 }, + { CS35L26_PWR_CTL2, 0xc8 }, + }; + int ret; + + ret = cs35l26_wake(amp); + if (ret) + return ret; + ret = cs35l26_write_seq(amp, seq1, ARRAY_SIZE(seq1)); + if (!ret) { + cs35l26_monitor_standby(amp); + ret = cs35l26_write_seq(amp, seq2, ARRAY_SIZE(seq2)); + } + if (ret) { + cs35l26_sleep(amp); + return ret; + } + fsleep(8000); + cs35l26_write(amp, CS35L26_BST_VOL, 0x30); + cs35l26_clear_status(amp); + amp->on = true; + amp->tdm = true; + return 0; +} + +static void cs35l26_off(struct cs35l26 *amp) +{ + int i, v; + + if (!amp->on) + return; + v = cs35l26_read(amp, CS35L26_PWR_CTL3); + if (v >= 0) + cs35l26_write(amp, CS35L26_PWR_CTL3, v | 0x04); + cs35l26_write(amp, CS35L26_PWR_CTL2, 0xfb); + cs35l26_write(amp, CS35L26_PWR_CTL1, 0x01); + for (i = 0; i < 10; i++) { + v = cs35l26_read(amp, CS35L26_STATUS2); + if (v < 0 || v & CS35L26_PDN_DONE) + break; + usleep_range(5000, 6000); + } + cs35l26_write(amp, CS35L26_PWR_CTL3, 0xbe); + cs35l26_sleep(amp); + amp->on = false; + amp->tdm = false; +} + +static int cs35l26_pdm_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kc, + int event) +{ + struct cs35l26 *amp = snd_soc_component_get_drvdata(snd_soc_dapm_to_component(w->dapm)); + int ret = 0; + + mutex_lock(&->lock); + if (SND_SOC_DAPM_EVENT_ON(event)) { + /* the loudspeakers keep the amplifier while they play */ + if (!amp->on) + ret = cs35l26_pdm_on(amp); + } else if (amp->on && !amp->tdm) { + cs35l26_off(amp); + } + mutex_unlock(&->lock); + return ret; +} + +static int cs35l26_tdm_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kc, + int event) +{ + struct cs35l26 *amp = snd_soc_component_get_drvdata(snd_soc_dapm_to_component(w->dapm)); + int ret = 0; + + mutex_lock(&->lock); + if (SND_SOC_DAPM_EVENT_ON(event)) { + /* the loudspeakers take the amplifier from the earpiece */ + if (amp->on && !amp->tdm) + cs35l26_off(amp); + ret = cs35l26_tdm_on(amp); + } else if (amp->on && amp->tdm) { + cs35l26_off(amp); + } + mutex_unlock(&->lock); + return ret; +} + +static const struct snd_soc_dapm_widget cs35l26_widgets[] = { + SND_SOC_DAPM_INPUT("PDMIN"), + SND_SOC_DAPM_OUT_DRV_E("PDM Amp", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l26_pdm_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + SND_SOC_DAPM_INPUT("TDMIN"), + SND_SOC_DAPM_OUT_DRV_E("TDM Amp", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l26_tdm_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + SND_SOC_DAPM_OUTPUT("SPK"), +}; + +static const struct snd_soc_dapm_route cs35l26_routes[] = { + { "PDM Amp", NULL, "PDMIN" }, + { "SPK", NULL, "PDM Amp" }, + { "TDM Amp", NULL, "TDMIN" }, + { "SPK", NULL, "TDM Amp" }, +}; + +static const struct snd_soc_component_driver cs35l26_component = { + .name = "cs35l26", + .dapm_widgets = cs35l26_widgets, + .num_dapm_widgets = ARRAY_SIZE(cs35l26_widgets), + .dapm_routes = cs35l26_routes, + .num_dapm_routes = ARRAY_SIZE(cs35l26_routes), + .endianness = 1, +}; + +static int cs35l26_probe(struct i2c_client *i2c) +{ + struct device *dev = &i2c->dev; + struct cs35l26 *amp; + int id[3], i; + + amp = devm_kzalloc(dev, sizeof(*amp), GFP_KERNEL); + if (!amp) + return -ENOMEM; + amp->i2c = i2c; + mutex_init(&->lock); + i2c_set_clientdata(i2c, amp); + + amp->mclk = devm_clk_get(dev, "mclk"); + if (IS_ERR(amp->mclk)) + return dev_err_probe(dev, PTR_ERR(amp->mclk), "MCLK\n"); + amp->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); + if (IS_ERR(amp->reset)) + return dev_err_probe(dev, PTR_ERR(amp->reset), "reset GPIO\n"); + + /* The ID registers answer in reset release without MCLK. */ + usleep_range(2000, 3000); + gpiod_set_value_cansleep(amp->reset, 0); + usleep_range(1000, 2000); + for (i = 0; i < 3; i++) + id[i] = cs35l26_read(amp, CS35L26_CHIP_ID + i); + gpiod_set_value_cansleep(amp->reset, 1); + if (id[0] != 0x35 || id[1] != 0xa2) + return dev_err_probe(dev, -ENODEV, "unexpected ID %02x %02x %02x\n", + id[0], id[1], id[2]); + dev_info(dev, "CS35L26, ID %02x%02x%02x\n", id[0], id[1], id[2]); + + amp->monitor = devm_i2c_new_dummy_device(dev, i2c->adapter, CS35L26_MONITOR_ADDR); + if (IS_ERR(amp->monitor)) + amp->monitor = NULL; + + return devm_snd_soc_register_component(dev, &cs35l26_component, NULL, 0); +} + +static void cs35l26_remove(struct i2c_client *i2c) +{ + struct cs35l26 *amp = i2c_get_clientdata(i2c); + + mutex_lock(&->lock); + cs35l26_off(amp); + mutex_unlock(&->lock); +} + +static const struct of_device_id cs35l26_of_match[] = { + { .compatible = "cirrus,cs35l26" }, + {} +}; +MODULE_DEVICE_TABLE(of, cs35l26_of_match); + +static const struct i2c_device_id cs35l26_id[] = { + { "cs35l26" }, + {} +}; +MODULE_DEVICE_TABLE(i2c, cs35l26_id); + +static struct i2c_driver cs35l26_driver = { + .driver = { + .name = "cs35l26", + .of_match_table = cs35l26_of_match, + }, + .id_table = cs35l26_id, + .probe = cs35l26_probe, + .remove = cs35l26_remove, +}; +module_i2c_driver(cs35l26_driver); + +MODULE_DESCRIPTION("Cirrus Logic CS35L26 speaker amplifier (iPhone 7)"); +MODULE_LICENSE("GPL"); diff --git a/sound/soc/codecs/cs42l71.c b/sound/soc/codecs/cs42l71.c index 0307c993572023..a9c19400c239c4 100644 --- a/sound/soc/codecs/cs42l71.c +++ b/sound/soc/codecs/cs42l71.c @@ -19,8 +19,10 @@ * (XSP) on pad 0 / MCA0, with the codec as the clock provider. A plug * raises the codec interrupt (or is polled without one); the codec then * tells headphones from a headset with a mic, and reports the headset's - * centre button. The receiver, Apple remote volume keys and runtime - * power management are not done yet. + * centre button. On the iPhone 7 the codec has no headphone jack, and its + * receiver output leaves as PDM for the earpiece's amplifier: the "PDMOUT" + * pin, which powers only when the board routes it to that amplifier. Apple + * remote volume keys and runtime power management are not done yet. * * The codec's ASP (pad 2) is shared with the CS35L21 loudspeaker that * already works on Linux (MCA2 as clock provider). Never set 0x02000b @@ -88,6 +90,8 @@ struct cs42l71 { struct mutex lock; /* all SPI access and the state below */ unsigned int hp_vol; /* 0..CS42L71_HP_VOL_MAX, 1 dB steps from -50 dB */ bool hp_on; + unsigned int rcv_vol; /* 0..CS42L71_RCV_VOL_MAX, 1 dB steps from -62 dB */ + bool pdm_on; /* the receiver's PDM output runs */ bool play_on; /* XSP RX (playback) running */ bool cap_on; /* XSP TX (capture) running */ unsigned int mic_vol; /* 0..CS42L71_MIC_VOL_MAX, 0.5 dB steps from -6 dB */ @@ -593,6 +597,83 @@ static void cs42l71_hp_off(struct cs42l71 *cs) cs->hp_on = false; } +/* + * The receiver output of the iPhone 7: the DAC's data leaves as PDM for the + * earpiece's amplifier, with iOS's values. The headphone driver stays down, + * DAC-A sets the level together with the PDM path's gain, and DAC-B stays at + * -62 dB. + */ +#define CS42L71_RCV_VOL_MAX 62 +#define CS42L71_RCV_VOL_DEFAULT 56 /* -6 dB */ + +static int cs42l71_rcv_vol_apply(struct cs42l71 *cs) +{ + unsigned int v = cs->rcv_vol; + int ret; + + ret = cs42l71_update8(cs, 0xa0000c, 0xff, (2 * v - 0x7c) & 0xff); + if (!ret) + ret = cs42l71_update8(cs, 0x300000, 0x3f, CS42L71_RCV_VOL_MAX - v); + return ret; +} + +static int cs42l71_pdm_on(struct cs42l71 *cs) +{ + static const struct cs42l71_upd on[] = { + { 0x200003, 0x7f, 0x40 }, + { 0x200004, 0x7f, 0x40 }, + { 0x010001, 0x02, 0x02 }, /* headphone driver down */ + { 0x010001, 0x20, 0x00 }, /* PDM output up */ + { 0x010007, 0x01, 0x00 }, /* DAC up */ + }; + int ret; + + ret = cs42l71_run_seq(cs, on, ARRAY_SIZE(on)); + if (!ret) + ret = cs42l71_rcv_vol_apply(cs); + if (!ret) + ret = cs42l71_update8(cs, 0x010001, 0x08, 0x00); + if (ret) + return ret; + msleep(33); + ret = cs42l71_update8(cs, 0x300002, 0x3f, 0x3e); + if (!ret) + cs->pdm_on = true; + return ret; +} + +static void cs42l71_pdm_off(struct cs42l71 *cs) +{ + static const struct cs42l71_upd off[] = { + { 0x300000, 0x3f, 0x3f }, + { 0x300002, 0x3f, 0x3f }, + { 0x010007, 0x01, 0x01 }, + { 0x010001, 0x0e, 0x0e }, + { 0x010001, 0x20, 0x20 }, /* PDM output down */ + }; + + cs42l71_run_seq(cs, off, ARRAY_SIZE(off)); + cs->pdm_on = false; +} + +static int cs42l71_pdm_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kc, + int event) +{ + struct cs42l71 *cs = snd_soc_component_get_drvdata(snd_soc_dapm_to_component(w->dapm)); + int ret = 0; + + mutex_lock(&cs->lock); + if (SND_SOC_DAPM_EVENT_ON(event)) { + if (cs->hp_on) + cs42l71_hp_off(cs); + ret = cs42l71_pdm_on(cs); + } else if (cs->pdm_on) { + cs42l71_pdm_off(cs); + } + mutex_unlock(&cs->lock); + return ret; +} + /* XSP clocks for @rate, with RX and TX powered down (bd67d8 order) */ static int cs42l71_xsp_clocks(struct cs42l71 *cs, unsigned int rate) { @@ -885,7 +966,8 @@ static int cs42l71_mute_stream(struct snd_soc_dai *dai, int mute, int stream) int ret = 0; mutex_lock(&cs->lock); - if (!mute && !cs->hp_on) + /* the receiver's PDM output, if it runs, came up before the unmute */ + if (!mute && !cs->hp_on && !cs->pdm_on) ret = cs42l71_hp_on(cs); else if (mute && cs->hp_on) cs42l71_hp_off(cs); @@ -1065,6 +1147,51 @@ static int cs42l71_mic_src_put(struct snd_kcontrol *kcontrol, return changed; } +static const DECLARE_TLV_DB_SCALE(cs42l71_rcv_tlv, -100 * CS42L71_RCV_VOL_MAX, 100, 0); + +static int cs42l71_rcv_vol_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = CS42L71_RCV_VOL_MAX; + return 0; +} + +static int cs42l71_rcv_vol_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *c = snd_kcontrol_chip(kcontrol); + struct cs42l71 *cs = snd_soc_component_get_drvdata(c); + + ucontrol->value.integer.value[0] = cs->rcv_vol; + return 0; +} + +static int cs42l71_rcv_vol_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *c = snd_kcontrol_chip(kcontrol); + struct cs42l71 *cs = snd_soc_component_get_drvdata(c); + long v = ucontrol->value.integer.value[0]; + int ret = 0; + + if (v < 0 || v > CS42L71_RCV_VOL_MAX) + return -EINVAL; + + mutex_lock(&cs->lock); + if (v == cs->rcv_vol) { + mutex_unlock(&cs->lock); + return 0; + } + cs->rcv_vol = v; + if (cs->pdm_on) + ret = cs42l71_rcv_vol_apply(cs); + mutex_unlock(&cs->lock); + return ret ?: 1; +} + static const struct snd_kcontrol_new cs42l71_controls[] = { { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, @@ -1083,6 +1210,16 @@ static const struct snd_kcontrol_new cs42l71_controls[] = { .put = cs42l71_hp_vol_put, .tlv.p = cs42l71_hp_tlv, }, + { + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, + .name = "Receiver Playback Volume", + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | + SNDRV_CTL_ELEM_ACCESS_TLV_READ, + .info = cs42l71_rcv_vol_info, + .get = cs42l71_rcv_vol_get, + .put = cs42l71_rcv_vol_put, + .tlv.p = cs42l71_rcv_tlv, + }, { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Headset Mic Capture Volume", @@ -1099,6 +1236,15 @@ static const struct snd_kcontrol_new cs42l71_controls[] = { static const struct snd_soc_dapm_widget cs42l71_widgets[] = { SND_SOC_DAPM_OUTPUT("Headphone"), SND_SOC_DAPM_INPUT("Headset Mic"), + SND_SOC_DAPM_AIF_IN("XSP RX", "XSP Playback", 0, SND_SOC_NOPM, 0, 0), + SND_SOC_DAPM_OUT_DRV_E("PDM", SND_SOC_NOPM, 0, 0, NULL, 0, cs42l71_pdm_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + SND_SOC_DAPM_OUTPUT("PDMOUT"), +}; + +static const struct snd_soc_dapm_route cs42l71_routes[] = { + { "PDM", NULL, "XSP RX" }, + { "PDMOUT", NULL, "PDM" }, }; static struct snd_soc_jack_pin cs42l71_jack_pins[] = { @@ -1487,6 +1633,13 @@ static int cs42l71_component_probe(struct snd_soc_component *component) return ret; snd_jack_set_key(cs->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); + /* + * Routed to an amplifier's input, PDMOUT is no endpoint of its own and + * this does not matter. Left unrouted, it keeps playback off the PDM + * output. + */ + snd_soc_dapm_disable_pin(snd_soc_component_to_dapm(component), "PDMOUT"); + cs->jack_state = -1; INIT_DELAYED_WORK(&cs->jack_work, cs42l71_jack_work); cs->dbg_redetect = debugfs_create_file("cs42l71-redetect", 0200, NULL, @@ -1552,6 +1705,8 @@ static const struct snd_soc_component_driver cs42l71_component = { .num_controls = ARRAY_SIZE(cs42l71_controls), .dapm_widgets = cs42l71_widgets, .num_dapm_widgets = ARRAY_SIZE(cs42l71_widgets), + .dapm_routes = cs42l71_routes, + .num_dapm_routes = ARRAY_SIZE(cs42l71_routes), .endianness = 1, }; @@ -1649,6 +1804,7 @@ static int cs42l71_probe(struct spi_device *spi) cs42l71_jack_regs_show); cs->hp_vol = CS42L71_HP_VOL_DEFAULT; + cs->rcv_vol = CS42L71_RCV_VOL_DEFAULT; cs->mic_vol = CS42L71_MIC_VOL_DEFAULT; cs->mic_src = CS42L71_SRC_AUTO; cs->cap_mic = -1;