From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796313455; b=edZTNqGN1O83dCl/8mwCGuBhUUpz1tMgqxdS9y0if4pgQbyBf6y4tsjRQp5iwRmH0Zhm VGbP4pXzBqBqP9JACmfEA/UGCyefWZ+O4vhegKN4KTe+3jlylhVa/kwBixtf7bZOlL5Vq UGSXgoZavIBrAszICWVrjuPp0Z2aga76PLS4o56Zfoiwtc3VfR3PjlCLVaVZEVN76nA5X puZvnQGkbDXBV9xi0fWn34Z1uoOZCSoY2cvr/znZLrFhEc2FXHDoOMqr7xsou+yNSAVOu kSdkSA9DZ8p4Bty2gEXvten/H5E/HJTGeoVAKv491+byEHz0cm2JGN7lC56u5l3aPtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796313455; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=MGWtvUtrCJ23UTdH+k1/9Ok/f9axAxZ9xj/EuCfggg4=; b=F2DT/cHW2dTmDwBZRdpzxVHSj8uphs1/RMWR2mU1lKt3Jcgru/d3pA2npKTRUX7PCUZJ a5AeMNg7rUSBLCxc1/RtdEckqq4wxC/8wBF3t8lYJ1TUEN9ZPtw6UHj/jLCpPvciq4tm8 +KGWBG9DlrglXwNrVhxLA6D3nzunZ7bSM0qUEnN/r558pOLxggUxpPQwxMSnU9tVAJ2KG wToM3r5/DFbY+xpYHScf5p7Cyt/2k4RhFkuYDVZCJykc7dfidAVt6xoTdzuqCsXOjbX93 9Nx1YIyTISCqsFHjfRagkZIDviM+Osx7hVFRs/w8/xyHCiQ4lo62DnZXtBiXrzfK/6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JRZAcubQ; arc=none smtp.remote-ip=209.85.208.53 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 316AB606A697 for ; Thu, 3 Dec 2026 15:57:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 316AB606A697 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JRZAcubQ Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-6b8a1f59d5eso19641619a12.3 for ; Thu, 03 Dec 2026 07:57:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796313453; x=1796918253; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=MGWtvUtrCJ23UTdH+k1/9Ok/f9axAxZ9xj/EuCfggg4=; b=JRZAcubQjwTSgmxQu5EpDGG6XwVGGeEoG1Qll/THw1Sbnx58OV0VR0hsD8xmatHN0R Lc9oA273F56TfSDQg91suJOSet+kMv6Di1VJKqQdXgsChqF+HE7Poidztdh4aglZjkzP fHzJt+iasD7NSDmdvMqEwfXfamVkvPa+ub1BwQexNLy8S724KlkwPUV4Llxxp6/K/8kD U1+kdTiOmGeGNoioCxdOGDN7DwQJDOqUc/NKAeTf7LfcqJUWyJCioEK0mP4l5VFrvPFe yZEoGFKbou0oAQbYtGHLLY6d+iw7v+ucj/SmD0Aaxq7jhJ3wADUUiUv8OC906D4uQdvV gbfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796313453; x=1796918253; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MGWtvUtrCJ23UTdH+k1/9Ok/f9axAxZ9xj/EuCfggg4=; b=L0muBV40drwu90J/SjDKPh8MRN9dNZjnlYzSxcfaqxu7eqAS7i69sJYm4ZNlBjBEFf huFB5DPazU3BpWvC+spUanvCPmlPvqaTVevA+rde/AmMzr9geBLw0UA9Lu+i89GWd2PU LrNZ5fwjxyPFX+qXlA1viZqAfdoJMfK9KWM/Rvxd9EMMMdCCzna5XhOj31l2S2hLPBI/ 9vhjL9CrS712R1XzsC/eN3DsY7LR3NHS/Iw9ExkRumgicnUijRauWFYSt0HHyr3i6KLS PrznqiLFLVY7JpcuUctpsg/xRV+zWPEa7Jx0/B75XHfWkiuBNqsGQx/MmmULLbzTd4DU 1GmA== X-Forwarded-Encrypted: i=1; AL+hErH2H1ywhjl9JKmLodLPfJUbGDDjtOoOvXkoQ0H2wVahpueQT/Fx9uF7Ab2587XTnxY45ekwrTFIMV0haus=@vger.kernel.org X-Gm-Message-State: APuU4Z8sIqV19a2+lutdV9cXuqoRnvyvbCSZQGqrREg0gFW3N9p+BPg8 dse55/t7jDgWFohKsJuOl4on1GEk6R76qqQkLBzPPuyZi0EIF5ypBZjP X-Gm-Gg: AZFdTZcP0GLMf2yyYZdZRGt9LBbamnDdiHBuq1iMyupQr6yYS+mcpy7qK+sFSbHhco2 8EKKGde31PR3YJ4Vxik+3NVMv/p5pmv2Zy4UJjeEZuzx7pCekq1AGqi/d84514HhB9+n/+lMUbc qdmB7t7u+nSbqVdgmhSCiPQvHrm1jxM0zQS16MSh7HWc1LzwtrGGdCAONy5z+at+N/LVfuDjdMD YG4YOh2O6BLy2vGPAl8JlnST2EaggccChuFHnQeohLcEz0RKwHl6RJLrtjUQWHJaItDDw4ObdtI EzGqXmS0L+j4c6gPcLmNMcnahUT3oaS1Bp53bBGCCiJISHiZl3YXh31v1zG5OV2zXjcfsNbkP/K t5eld/RUlSdosDQ92jnzyZYNdR3s1cI6rAnUIkyNJC307hT4lsUYZE2Ka6jHpPoUcC8DDb08Yfg fFz/K/fTz/ypYg/guyWGa5ngUkdpI4HSP/f1ecLUmfLQwswfET8kxmnTwDZQdKjKxmvQqTHbvqs sudNLhPW5JtOqAC7qLqEULrNO4BK9g+3bmZSm8PpsDu+uXOlyd9uvs2i+asrVma5ys3CDXEK9mN CKr8xBuRNgDDVL46O8CVw7YMCrHndQu04QtgUaBlTovxTsn6AVa7E/DR/u5zCBuOMh8RkCHcbUC DUSfD4uin2xMDPcig4qvm5nCfYomTTiXdaeOA4jKteIsr X-Received: by 2002:a05:6402:5516:b0:6ba:85f6:9b4b with SMTP id 4fb4d7f45d1cf-6ba85f69da8mr27340764a12.41.1796313453270; Thu, 03 Dec 2026 07:57:33 -0800 (PST) Received: from thinkpad16.ts.appmire.be ([94.110.48.221]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-6bc65ddf159sm1896383a12.10.2026.12.03.07.57.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 07:57:32 -0800 (PST) From: Jeroen Trappers To: rafael@kernel.org, daniel.lezcano@kernel.org, sven@kernel.org, j@jannau.net, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org Cc: rui.zhang@intel.com, lukasz.luba@arm.com, neal@gompa.dev, towinchenmi@gmail.com, konradybcio@kernel.org, asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] thermal/drivers/apple: Add T7000 (A8) on-die temperature sensor driver Date: Thu, 3 Dec 2026 16:55:59 +0100 Message-ID: <20261203155600.3911447-3-jeroen.trappers@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203155600.3911447-1-jeroen.trappers@gmail.com> References: <20261203155600.3911447-1-jeroen.trappers@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Apple A8 (T7000) has the temperature sensor block iOS drives with its AppleT700XTempSensor class. The per-sensor register offsets are the sensor-offset-* properties of the tempsensor0..4 nodes in the iPhone 6 device tree. Each sensor accumulates samples between a start and a freeze of CTL0 bit 3; a read freezes it, reads the result word (bit 31 valid, signed Q6 degrees Celsius) and the sample count, and starts the next interval, as iOS's timer callback does. Only 32-bit accesses are made. The driver never writes the trims, CTL1/CTL2, the alarms or the SoCHOT thresholds, never sets a sensor's enable bit (a sensor the boot chain left disabled is skipped), and only starts sensors that a thermal zone uses, stopping them again on unbind. The TCC page is optional. It is a thermal driver rather than hwmon because the zones drive CPU cooling; the readings are also exposed through the thermal hwmon bridge. Adapted from Berkay Kösebay's out-of-tree driver for the A9 (S8003), which uses the same block. The driver was drafted with an LLM coding assistant and reviewed by me. Signed-off-by: Jeroen Trappers --- MAINTAINERS | 1 + drivers/thermal/Kconfig | 13 ++ drivers/thermal/Makefile | 1 + drivers/thermal/apple_t7000_thermal.c | 239 ++++++++++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 drivers/thermal/apple_t7000_thermal.c diff --git a/MAINTAINERS b/MAINTAINERS index 667f9f37b824..049c3b8c8377 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2146,6 +2146,7 @@ L: asahi@lists.linux.dev L: linux-pm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/thermal/apple,t7000-thermal.yaml +F: drivers/thermal/apple_t7000_thermal.c APPLETALK NETWORK LAYER L: netdev@vger.kernel.org diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 810eeccedfba..4a2190b9f4a0 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -432,6 +432,19 @@ depends on ARCH_MEDIATEK || COMPILE_TEST source "drivers/thermal/mediatek/Kconfig" endmenu +config APPLE_T7000_THERMAL + tristate "Apple T7000 (A8) on-die temperature sensors" + depends on ARCH_APPLE || COMPILE_TEST + depends on THERMAL_OF + help + Thermal zone sensors for the die temperature sensors of the + Apple A8 (T7000) SoC, as used in the iPhone 6 family and other + 2014 / 2015 Apple devices. The sensors are read with the + calibration the boot chain programs. + + This driver can also be built as a module. If so, the module will + be called apple_t7000_thermal. + config AMLOGIC_THERMAL tristate "Amlogic Thermal Support" default ARCH_MESON diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 3b249195c088..f2b6cbbe3307 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o obj-y += mediatek/ obj-$(CONFIG_GENERIC_ADC_THERMAL) += thermal-generic-adc.o obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o +obj-$(CONFIG_APPLE_T7000_THERMAL) += apple_t7000_thermal.o obj-$(CONFIG_AMLOGIC_THERMAL) += amlogic_thermal.o obj-y += spacemit/ obj-$(CONFIG_SPRD_THERMAL) += sprd_thermal.o diff --git a/drivers/thermal/apple_t7000_thermal.c b/drivers/thermal/apple_t7000_thermal.c new file mode 100644 index 000000000000..7d80ddf6e2f5 --- /dev/null +++ b/drivers/thermal/apple_t7000_thermal.c @@ -0,0 +1,239 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Apple T7000 (A8) on-die temperature sensors. + * + * Copyright (C) 2026 Jeroen Trappers + * + * Adapted from the out-of-tree Apple S8003 (A9) temperature sensor driver + * by Berkay Kösebay (GPL-2.0), which drives the same block. + * + * The block is the one iOS drives with its AppleT700XTempSensor class; the + * per-sensor register offsets are the "sensor-offset-*" properties of the + * tempsensor0..4 nodes in the iPhone 6 device tree. The hardware converts + * with the calibration the boot chain programs: this driver never writes the + * trims, CTL1/CTL2, the alarm registers or the SoCHOT thresholds, and never + * sets a sensor's enable bit (a sensor the boot chain left disabled is + * skipped). Only 32-bit accesses are made. + * + * Each sensor accumulates samples between a "start" (CTL0 set, bit 3) and a + * "freeze" (CTL0 clear, bit 3). A read freezes the accumulator, reads the + * result word (bit 31 valid, signed Q6 degrees Celsius in the low bits) and + * the sample count, and starts the next interval, as iOS's timer callback + * does. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "thermal_hwmon.h" + +#define T7000_CTL0_ENABLE BIT(0) +#define T7000_CTL0_ACCUMULATE BIT(3) +#define T7000_BK_VALID BIT(31) +/* + * Every read restarts the accumulator, and the thermal core, sysfs and hwmon + * may all read a zone: readers within this interval get the last sample, so + * each one still averages over a useful window (about 400 samples a second). + */ +#define T7000_MIN_INTERVAL_MS 100 + +enum t7000_page { + T7000_PAGE_SOC, /* power manager sensors */ + T7000_PAGE_TCC, /* CPU complex sensors */ + T7000_PAGES, +}; + +/** + * struct t7000_sensor_desc - one sensor channel + * @page: register page + * @ctl_set: CTL0 set register + * @ctl_clear: CTL0 clear register + * @bk: result register + * @count: accumulated sample count + * @sign_bit: sign bit of the Q6 value (14 for sensor-accuracy 2, else 15) + */ +struct t7000_sensor_desc { + enum t7000_page page; + u16 ctl_set; + u16 ctl_clear; + u16 bk; + u16 count; + u8 sign_bit; +}; + +/* Sensor IDs are the device tree's tempsensor indices (see the binding). */ +static const struct t7000_sensor_desc t7000_sensors[] = { + [0] = { .page = T7000_PAGE_SOC, .ctl_set = 0x000, .ctl_clear = 0x004, + .bk = 0x018, .count = 0x024, .sign_bit = 14 }, + [1] = { .page = T7000_PAGE_SOC, .ctl_set = 0x044, .ctl_clear = 0x048, + .bk = 0x05c, .count = 0x068, .sign_bit = 14 }, + [2] = { .page = T7000_PAGE_TCC, .ctl_set = 0x000, .ctl_clear = 0x408, + .bk = 0x020, .count = 0x038, .sign_bit = 15 }, + [3] = { .page = T7000_PAGE_TCC, .ctl_set = 0x040, .ctl_clear = 0x438, + .bk = 0x060, .count = 0x078, .sign_bit = 15 }, + [4] = { .page = T7000_PAGE_TCC, .ctl_set = 0x200, .ctl_clear = 0x468, + .bk = 0x220, .count = 0x238, .sign_bit = 15 }, +}; + +/** + * struct t7000_sensor - one registered sensor + * @base: its register page + * @desc: its registers + * @sampled: jiffies of the last valid sample + * @temp: the last valid sample, millidegrees Celsius + * @valid: whether @temp holds a sample + * + * The thermal core serializes the get_temp() calls of a zone. + */ +struct t7000_sensor { + void __iomem *base; + const struct t7000_sensor_desc *desc; + unsigned long sampled; + int temp; + bool valid; +}; + +static int t7000_sample(struct t7000_sensor *s) +{ + const struct t7000_sensor_desc *d = s->desc; + u32 ctl, bk, count; + int temp; + + writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_clear); + ctl = readl(s->base + d->ctl_set); + bk = readl(s->base + d->bk); + count = readl(s->base + d->count); + writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_set); + + if (!(ctl & T7000_CTL0_ENABLE) || !(bk & T7000_BK_VALID) || !count) + return -EAGAIN; + + temp = DIV_ROUND_CLOSEST(sign_extend32(bk, d->sign_bit) * 1000, 64); + /* An impossible code is a glitch; iOS substitutes 30 C instead. */ + if (temp < -40000 || temp > 150000) + return -EAGAIN; + + s->temp = temp; + s->sampled = jiffies; + s->valid = true; + return 0; +} + +static int t7000_get_temp(struct thermal_zone_device *tz, int *temp) +{ + struct t7000_sensor *s = thermal_zone_device_priv(tz); + int ret = 0; + + if (!s->valid || + time_after_eq(jiffies, s->sampled + msecs_to_jiffies(T7000_MIN_INTERVAL_MS))) + ret = t7000_sample(s); + /* No new sample (an interval without one, or a glitch): keep the last one. */ + if (ret == -EAGAIN && s->valid) + ret = 0; + if (!ret) + *temp = s->temp; + + return ret; +} + +static const struct thermal_zone_device_ops t7000_thermal_ops = { + .get_temp = t7000_get_temp, +}; + +/* On unbind, stop accumulating: the sensor is left as the boot chain left it. */ +static void t7000_sensor_stop(void *data) +{ + struct t7000_sensor *s = data; + + writel(T7000_CTL0_ACCUMULATE, s->base + s->desc->ctl_clear); +} + +static int t7000_thermal_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + void __iomem *page[T7000_PAGES] = { }; + struct resource *res; + unsigned int i; + int ret; + + page[T7000_PAGE_SOC] = devm_platform_ioremap_resource_byname(pdev, "soc"); + if (IS_ERR(page[T7000_PAGE_SOC])) + return PTR_ERR(page[T7000_PAGE_SOC]); + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcc"); + if (res) { + page[T7000_PAGE_TCC] = devm_ioremap_resource(dev, res); + if (IS_ERR(page[T7000_PAGE_TCC])) + return PTR_ERR(page[T7000_PAGE_TCC]); + } + + for (i = 0; i < ARRAY_SIZE(t7000_sensors); i++) { + const struct t7000_sensor_desc *d = &t7000_sensors[i]; + struct thermal_zone_device *tz; + struct t7000_sensor *s; + + if (!page[d->page]) + continue; + if (!(readl(page[d->page] + d->ctl_set) & T7000_CTL0_ENABLE)) { + dev_dbg(dev, "sensor %u not enabled by the boot chain\n", i); + continue; + } + + s = devm_kzalloc(dev, sizeof(*s), GFP_KERNEL); + if (!s) + return -ENOMEM; + s->base = page[d->page]; + s->desc = d; + + /* Start accumulating, as iOS does, before the zone's first read. */ + writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_set); + ret = devm_add_action_or_reset(dev, t7000_sensor_stop, s); + if (ret) + return ret; + + tz = devm_thermal_of_zone_register(dev, i, s, &t7000_thermal_ops); + if (IS_ERR(tz)) { + /* No zone uses this sensor: leave it alone. */ + if (PTR_ERR(tz) == -ENODEV) { + devm_release_action(dev, t7000_sensor_stop, s); + continue; + } + return dev_err_probe(dev, PTR_ERR(tz), + "failed to register sensor %u\n", i); + } + + ret = devm_thermal_add_hwmon_sysfs(dev, tz); + if (ret) + dev_warn(dev, "failed to add hwmon for sensor %u: %d\n", + i, ret); + } + + return 0; +} + +static const struct of_device_id t7000_thermal_of_match[] = { + { .compatible = "apple,t7000-thermal" }, + { } +}; +MODULE_DEVICE_TABLE(of, t7000_thermal_of_match); + +static struct platform_driver t7000_thermal_driver = { + .probe = t7000_thermal_probe, + .driver = { + .name = "apple-t7000-thermal", + .of_match_table = t7000_thermal_of_match, + }, +}; +module_platform_driver(t7000_thermal_driver); + +MODULE_AUTHOR("Jeroen Trappers "); +MODULE_DESCRIPTION("Apple T7000 on-die temperature sensors"); +MODULE_LICENSE("GPL"); -- 2.47.3