From patchwork Fri Oct 18 10:48:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13841589 X-Patchwork-Delegate: daniel.lezcano@linaro.org Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABA472010FC; Fri, 18 Oct 2024 10:49:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248559; cv=none; b=vD4AMQ7xRyGWjtLRFvffAWH1Rqg7nQ5PPDsVDU2ISzfq+v0nzClf0SdnSnkrkl5DrbS0rpc182dEw2PRZZQIAE1FIHXwp4PyBDlVwe0DTyoyQ2TzUac8H5Kg33Isl5mNeoCg3ofvPk9UFv9NkuRI/YimtrnIR5wwSZ6OWkFjjO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248559; c=relaxed/simple; bh=RSdMFumWyD7Mx4sKNxKfjNlsDJaTB6uemxsTmcKyHuo=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=qLRxjkJmxG3b/XPrWKB91wTQf8D0+ky4zZetx12dhWzKsPS/ntPxWvoCnwrNaUw3ns/FT44tyrlx4nYYLvbNaJQjzwMjaxsHn/YRXvH3fBlVp79Oi2pJvvdUKNurAkMwclrIELDvMn9Lg9NFYS8JKApEbnFY0boXw/9wMDqEIIo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kMvYjveM; arc=none smtp.client-ip=209.85.221.42 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.b="kMvYjveM" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-37d4c1b1455so1480416f8f.3; Fri, 18 Oct 2024 03:49:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729248556; x=1729853356; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=NRHQQGm7cGssgdoLEtNCNKA/IiRlPKI/cHGPt5rH+50=; b=kMvYjveMotunGr76a6OsgU1LFpRaH3V8PBaOZb4UXj74nwe6Zk6cR1gTHeeXY/cjeY eoJSmFxrBdr0PO9i2cLdmGyo4wRFQJneAhT/+1/Df/QtsY0JWljeUEWjOSz9GJyalMp1 h5mj3cc+6kjyyfAQ8dUYOrj8UCikoVJdkZbt/iXKkW+KSQ5ILNFhmSA6xqqse7jpWpc1 SLMk7MgW1JFgKjNGgOlm+haL83EZDbsndkhDwvO3Evmaty3m9Mx03inEqBL1oRs21Cnl gXYItJFwP47gZBTaR33fzvvx8aD3kleCl72AN57aYXwrEFmyEreQZ0MUp+t83mF8RSiC 40gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729248556; x=1729853356; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NRHQQGm7cGssgdoLEtNCNKA/IiRlPKI/cHGPt5rH+50=; b=JE3r8ojcwRi4wq9CdNCelwyE2nwyEViJnZwJswmEOE1tjZCTAtayft26ZWMbbzMRli mXGKpi7pbcQsR5Y9QkpxT0shtMz+REvgLEbIcY3v3RT+BUsn+9+F8kgpnU/xJ31X5lYj e8i/ju/m2aQ4yFyZapVZYoGMlQ8JwSFttUrHhpssG8PXU2+cLOSQfUYUnhgYdRIYwKjR cCxFzbJcX8jV3qJEb9Waa+AG3QHShdAnWdWFVU7ZxypHC2vbT9AaM1vQ1oLqdBF7DRyl nJFdMblK3zHzPVI7Y/lpKxlWNXzQXQRSOSMQsV+6vM7rC62xfviLzsUOsZbY7Q4BSJRS bsuA== X-Forwarded-Encrypted: i=1; AJvYcCUNPp1lGsiirXDQkI/LItMijBXBgioMfQs+g0uXEmpV8njf61laT4bcqXHNAudLCepq+Lkm7qr5Rvk5xTfB@vger.kernel.org, AJvYcCUU1qRZRqqYhiNaEHqfAEFjtxNMpQTkopcbxY7IdwgbxJhjCCfKsLbOAGNz2edQJ9pAfbtRFj7au/K0@vger.kernel.org, AJvYcCUc1hjiyEGoPK3k6qEa24qbwcK4TxfKd0JNKh0YINSJdAHr5ufyT8GriyDw7NqViTdJdKAmTmq9ym8=@vger.kernel.org X-Gm-Message-State: AOJu0YykYN7+UIFD/XuNSahhBPs8Wfmq+5VehdY0hY8rM3QE4uHvEPqn tvLZgg/TZOfETYI7b2sat//pUKC5sod+fcD/MrjJwjrbNEBYqicA X-Google-Smtp-Source: AGHT+IH8LYkBdN9+FI+6EBaR0iOr2VBsrCSKHDoBM+xZIc2oRAjfNBoXbr4SfQESdAqEnwFRN/G9IQ== X-Received: by 2002:a5d:4ec9:0:b0:371:8319:4dcc with SMTP id ffacd0b85a97d-37eaa48f8b5mr1570068f8f.2.1729248555773; Fri, 18 Oct 2024 03:49:15 -0700 (PDT) Received: from localhost.localdomain (93-34-90-105.ip49.fastwebnet.it. [93.34.90.105]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37ecf0ed5f4sm1606240f8f.68.2024.10.18.03.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:49:15 -0700 (PDT) From: Christian Marangi To: "Rafael J. Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Christian Marangi , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Lorenzo Bianconi , upstream@airoha.com Subject: [PATCH v3 1/3] dt-bindings: thermal: Add support for Airoha EN7581 thermal sensor Date: Fri, 18 Oct 2024 12:48:04 +0200 Message-ID: <20241018104839.13296-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add support for Airoha EN7581 thermal sensor and monitor. This is a simple sensor for the CPU or SoC Package that provide thermal sensor and trip point for hot low and critical condition to fire interrupt and react on the abnormal state. Signed-off-by: Christian Marangi Reviewed-by: Rob Herring (Arm) --- Changes v2: - Add Reviewed-by tag .../thermal/airoha,en7581-thermal.yaml | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/thermal/airoha,en7581-thermal.yaml diff --git a/Documentation/devicetree/bindings/thermal/airoha,en7581-thermal.yaml b/Documentation/devicetree/bindings/thermal/airoha,en7581-thermal.yaml new file mode 100644 index 000000000000..ca0242ef0378 --- /dev/null +++ b/Documentation/devicetree/bindings/thermal/airoha,en7581-thermal.yaml @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/thermal/airoha,en7581-thermal.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Airoha EN7581 Thermal Sensor and Monitor + +maintainers: + - Christian Marangi + +properties: + compatible: + const: airoha,en7581-thermal + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + airoha,chip-scu: + description: phandle to the chip SCU syscon + $ref: /schemas/types.yaml#/definitions/phandle + + '#thermal-sensor-cells': + const: 0 + +required: + - compatible + - reg + - interrupts + - airoha,chip-scu + +additionalProperties: false + +examples: + - | + #include + + thermal-sensor@1efbd800 { + compatible = "airoha,en7581-thermal"; + reg = <0x1efbd000 0xd5c>; + interrupts = ; + airoha,chip-scu = <&chip_scu>; + + #thermal-sensor-cells = <0>; + }; From patchwork Fri Oct 18 10:48:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13841590 X-Patchwork-Delegate: daniel.lezcano@linaro.org Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1996D200CA4; Fri, 18 Oct 2024 10:49:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248561; cv=none; b=OwrJ+8ozY9l9wg9mD9I/d5G4pLSvGeKqKfTblF+NcWX5m8rZLedkMV7ShX9vFLziGK+MaYSAVav4o05Y72V/hbbd8s6Pae1AY7Mw1ljLvdhzFdExHeZrOemrOh9Zga9k+5P5cFwBDO+6ajMoMG1p/Xg19qJXF61T4ZiC10zHOt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248561; c=relaxed/simple; bh=Xkjy4d53ZyxC4SQCRouYD4HITzPwapOHuevIE3Vm+Oo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G6scX8nIOgVAgp8dQ1mXbZp1R9lEws7XnmxaK212PmTbuc3UMyklnKT398ol3uW1RuUccpYrZIXanLZrMeZ82F2iMwaGhEtkV/4VwOUvYqZhWbgmJ8HQOuufrwfws9KUVDMsGsy25bFrHJlnrNAAQBnwwyalTiqcY90jS6ViP9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PkxPDrGc; arc=none smtp.client-ip=209.85.221.44 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.b="PkxPDrGc" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-37d4821e6b4so1210511f8f.3; Fri, 18 Oct 2024 03:49:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729248557; x=1729853357; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=OZFlOJNXwxAQ2TJgCN69ClEqy96CSklKV7EGj2px/IY=; b=PkxPDrGcuELCTkjJTHCTiZDNKc4Zp6HmGfiqSosRSK0RmMSB/Fxcr9wLJcUmAfkNYn g2qhZy4zr751mKzoTy/O+fDXZCCZkxlMv033JKaZ86Y8RUCu7VyF+2J6CePXmmk7oqDF PydLaW1dryN/8dmN7XTkoEYHdyaJR1pnDbVHut9bleq9dyOSuXhA6ovvC2s0poqx4A0a 9tCBEn9aaupv134hmG6xEr69cGugIJnilsrFjQ/z0jx1YjbP+bXdlZm/16vMDcnyOI/6 +cGuH6WNXJmVj9JQ/NqhMNyjSzFF9fo8FZEOQKtp02RIDYrfvGo5Gv/pT3swIvn9qKjd URYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729248557; x=1729853357; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OZFlOJNXwxAQ2TJgCN69ClEqy96CSklKV7EGj2px/IY=; b=kDPv60UEneHgPlYg8zLR75NskZpS3a3TsxacKyE6eisH+Npr7P/7PxSiDOUJtvYvVY olf2WHTYLwAMr1NP7JKzO8Wp3cevFaY/1wRNhUdzXLt4/Shb7AvXf3V/71EiRTV/duf5 su191UbrGUViq4qTl+jo7sDI4aghZa9/XmsJileQAPp5ZhEfiqkyY6VBy9zLDaG8Pta5 mNpprPqkDkariX4Z6SklpMw1MoE+45om0VkfH/EDy9MjT5AdC5EdvtiyCLWaKYD0kPru 1nOHiD+i4IcxzOqkAtesf93nTizir5dzhT7Yi0fLc9AYqYpILF23nEbsvG2EWY7FCyFr uuuQ== X-Forwarded-Encrypted: i=1; AJvYcCVfHhxpUqvlTNTuQfpUyO4lqoN3PvjyC7T19W2q8AupXFr+SW2CjSJLLM+GPNqy6AzqVTuFFOCLMsc=@vger.kernel.org, AJvYcCW13RFnzgoCSkaf++hpWYRdRPTr3Ywh0jQNueWyBAxAROjbhrxeOWPUb+U89WecHtyS6B+NmpUaiHWsSNKw@vger.kernel.org, AJvYcCWJSnYekFMPDu4OXEvXDtccmg48QlPswdhwKCQnaJOAvNWIskEkzNZ0G8ImeMH4x2T1w07hWNYPaow7@vger.kernel.org X-Gm-Message-State: AOJu0YyaROb/Iq7CuOiCZjkId7nGTtUeGv2l1xGQasXslBMVRklP/9dp nrtWzV+Pt0JnxvVX+ArE+PmgvAh19JJG8eNJ+1R6+SgtoA3OOuo/ X-Google-Smtp-Source: AGHT+IGCOJLPZ3wkYiux9WLmR1J/5aQsuolmbHpT9L+JknJZhTqsPEtlV08H47nEj0r3+24W73wysw== X-Received: by 2002:adf:f80e:0:b0:374:c847:852 with SMTP id ffacd0b85a97d-37eab6e4800mr1313169f8f.29.1729248557027; Fri, 18 Oct 2024 03:49:17 -0700 (PDT) Received: from localhost.localdomain (93-34-90-105.ip49.fastwebnet.it. [93.34.90.105]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37ecf0ed5f4sm1606240f8f.68.2024.10.18.03.49.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:49:16 -0700 (PDT) From: Christian Marangi To: "Rafael J. Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Christian Marangi , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Lorenzo Bianconi , upstream@airoha.com Subject: [PATCH v3 2/3] thermal: of: Add devm_thermal_of_zone_register_with_params() variant Date: Fri, 18 Oct 2024 12:48:05 +0200 Message-ID: <20241018104839.13296-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018104839.13296-1-ansuelsmth@gmail.com> References: <20241018104839.13296-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Commit b1ae92dcfa8e ("thermal: core: Make struct thermal_zone_device definition internal") moved the thermal_zone_device struct from global thermal.h to internal thermal_core.h making the internal variables of the struct not accessible from the user drivers (without inclusing thermal_core.h). One case where the internal variables might be needed is for the thermal_zone_params in the context of OF probe. In such case a thermal driver might have default params that can only be parsed at runtime (example present in EFUSE or derived from other values) and wants to update the values in the thermal_zone_params for the thermal device. (to use the helper like get_slope() or get_offset()) To account for this scenario, introduce a variant of devm_thermal_of_zone_register(), devm_thermal_of_zone_register_with_params(), that takes and additional variable and permits to register the thermal device with default thermal_zone_params. To follow OF implementation, these params are only treated as default params and are ignored if a related one is defined in DT. (example a slope or offset value defined in DT have priority to the default one passed in a thermal_device_params struct) This permits to support both implementation, use the helpers and expose these values in sysfs. Signed-off-by: Christian Marangi --- Changes v3: - Add this patch drivers/thermal/thermal_of.c | 67 +++++++++++++++++++++++++++++------- include/linux/thermal.h | 13 +++++++ 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c index aa34b6e82e26..c9a65eb9ee1e 100644 --- a/drivers/thermal/thermal_of.c +++ b/drivers/thermal/thermal_of.c @@ -250,7 +250,7 @@ static void thermal_of_parameters_init(struct device_node *np, { int coef[2]; int ncoef = ARRAY_SIZE(coef); - int prop, ret; + int prop; tzp->no_hwmon = true; @@ -262,14 +262,11 @@ static void thermal_of_parameters_init(struct device_node *np, * thermal zone. Thus, we are considering only the first two * values as slope and offset. */ - ret = of_property_read_u32_array(np, "coefficients", coef, ncoef); - if (ret) { - coef[0] = 1; - coef[1] = 0; + if (!of_property_read_u32_array(np, "coefficients", coef, ncoef)) { + tzp->slope = coef[0]; + tzp->offset = coef[1]; } - tzp->slope = coef[0]; - tzp->offset = coef[1]; } static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz) @@ -458,10 +455,15 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz) * zone properties and registers new thermal zone with those * properties. * + * The passed thermal zone params are treated as default values and ignored if + * the related property is found in DT. (DT params have priority to + * default values) + * * @sensor: A device node pointer corresponding to the sensor in the device tree * @id: An integer as sensor identifier * @data: A private data to be stored in the thermal zone dedicated private area * @ops: A set of thermal sensor ops + * @tzp: a pointer to the default thermal zone params structure associated with the sensor * * Return: a valid thermal zone structure pointer on success. * - EINVAL: if the device tree thermal description is malformed @@ -469,12 +471,12 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz) * - Other negative errors are returned by the underlying called functions */ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data, - const struct thermal_zone_device_ops *ops) + const struct thermal_zone_device_ops *ops, + struct thermal_zone_params *tzp) { struct thermal_zone_device_ops of_ops = *ops; struct thermal_zone_device *tz; struct thermal_trip *trips; - struct thermal_zone_params tzp = {}; struct device_node *np; const char *action; int delay, pdelay; @@ -500,7 +502,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * goto out_kfree_trips; } - thermal_of_parameters_init(np, &tzp); + thermal_of_parameters_init(np, tzp); of_ops.bind = thermal_of_bind; of_ops.unbind = thermal_of_unbind; @@ -511,7 +513,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * of_ops.critical = thermal_zone_device_critical_reboot; tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips, - data, &of_ops, &tzp, + data, &of_ops, tzp, pdelay, delay); if (IS_ERR(tz)) { ret = PTR_ERR(tz); @@ -566,6 +568,7 @@ static int devm_thermal_of_zone_match(struct device *dev, void *res, struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data, const struct thermal_zone_device_ops *ops) { + struct thermal_zone_params tzp = { .slope = 1 }; struct thermal_zone_device **ptr, *tzd; ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr), @@ -573,7 +576,7 @@ struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, in if (!ptr) return ERR_PTR(-ENOMEM); - tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops); + tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops, &tzp); if (IS_ERR(tzd)) { devres_free(ptr); return tzd; @@ -586,6 +589,46 @@ struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, in } EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register); +/** + * devm_thermal_of_zone_register_with_params - register a thermal tied with the sensor life cycle + * with default params + * + * This function is the device version of the thermal_of_zone_register() function. + * + * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle + * @sensor_id: the sensor identifier + * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field + * @ops: a pointer to the ops structure associated with the sensor + * @tzp: a pointer to the default thermal zone params structure associated with the sensor + * + * The thermal zone params are treated as default values and ignored if the related property is + * found in DT. (DT params have priority to default values) + */ +struct thermal_zone_device *devm_thermal_of_zone_register_with_params(struct device *dev, int sensor_id, + void *data, + const struct thermal_zone_device_ops *ops, + struct thermal_zone_params *tzp) +{ + struct thermal_zone_device **ptr, *tzd; + + ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr), + GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops, tzp); + if (IS_ERR(tzd)) { + devres_free(ptr); + return tzd; + } + + *ptr = tzd; + devres_add(dev, ptr); + + return tzd; +} +EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register_with_params); + /** * devm_thermal_of_zone_unregister - Resource managed version of * thermal_of_zone_unregister(). diff --git a/include/linux/thermal.h b/include/linux/thermal.h index f1155c0439c4..8130e111210d 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -180,6 +180,10 @@ struct thermal_zone_params { #ifdef CONFIG_THERMAL_OF struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, const struct thermal_zone_device_ops *ops); +struct thermal_zone_device *devm_thermal_of_zone_register_with_params(struct device *dev, int id, + void *data, + const struct thermal_zone_device_ops *ops, + struct thermal_zone_params *tzp); void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz); @@ -192,6 +196,15 @@ struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, in return ERR_PTR(-ENOTSUPP); } +static inline +struct thermal_zone_device *devm_thermal_of_zone_register_with_params(struct device *dev, int id, + void *data, + const struct thermal_zone_device_ops *ops, + struct thermal_zone_params *tzp) +{ + return ERR_PTR(-ENOTSUPP); +} + static inline void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz) { From patchwork Fri Oct 18 10:48:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13841591 X-Patchwork-Delegate: daniel.lezcano@linaro.org Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84999202F90; Fri, 18 Oct 2024 10:49:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248562; cv=none; b=oBCzj56Xa95XtW7KPCxrEr3TBkkrEqQji/SWfPxfg8IcVu8peiCS1Yz/DXhh5YHzv1QBbo93yYjyfZXvs6LEKL82CQkeEPSQ8Knf8bHBpPeuMaWGYUNEvWYY0i/bbMKVrz/bnkF7uaeT/XHz+igqOuDjj83ptAT1stIiitEcahA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729248562; c=relaxed/simple; bh=oHKzOgXlOA1zGpEsoQRc0QpJq2O2gDE/SwidiC57k4I=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VPys4IiPgUmp1cu2TBh8owYdZ4gEOQSEBBrKke4KZ9fZQ83pT2a8GOO0WaDjuu5Wx0iLvwnTiwvc9LvW/hZXU3r9123nDeItHB6By9/3y40sfVOO4EiH2m+xDKBECh5zp3zkp9kp4afh/MAhVl9qTJjZJ5N7gnynQLedok8+y9g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KSLDSr3H; arc=none smtp.client-ip=209.85.221.44 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.b="KSLDSr3H" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-37d3e8d923fso1454198f8f.0; Fri, 18 Oct 2024 03:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729248559; x=1729853359; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=631Tk4IHWx1BAcJ9Tme4tCRai0xzigRtmeSzo6CdVhA=; b=KSLDSr3H+U87bkdw3/y14aQoRtnX7yN2Ux89tN6MSI0R9tSk/TERAfzseQGic2yW5C 57wbkct8Kron5XRrXxr7IdeI8DAUXnd3Ig5nR5vuw0Xn3Vm/V4amztE5Y3k47UsOYSTF 1Uo8vUtJqLLNoa8rd2qflgm0Y9KrPt5/95GRdNqhGWZMlIXROK/wtgxy+EQM8rXwXqtH a7FUb13QCAxUO5UfJaOU/tM185HGjBTdmGOWYj+QrFFXHC58DQgcMmH3MJqrrpdhRmxg 0ak2ORtT1jJYuYO2wDbTwWR9FHFU7WiDk0bMG6+QEiBZevLC68d1y3bacA6BWYJDBXsd ppjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729248559; x=1729853359; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=631Tk4IHWx1BAcJ9Tme4tCRai0xzigRtmeSzo6CdVhA=; b=E4crSO9A3KJ8oSMMQ+VLUToKCZW9fayI5+YBXPw9FmSGoECss9GECKM3UJXZuP5LtS NXCXmScCzyyuxe/e9hSN1F7UnxDtHs56m+9L5XNNeP3Xhi4eUlmY4M2M4ddls+ukcdOX r+q+PcLniY7UXbE0XfOi+oV78jFLiDJodeXZJiL7+GjNGqBgGbCk8GCoH96bYQR7Przi DYuUwQ9j786E8t+aSaN66FuqBLl1AdEvUPvtMC9GjLmj8P/qu6pU9mFXCZInNIDa1s8n p6HrTR8fSznnec5LzDXVrgLW+sb9vNs1w8O8PzXqpvxNBA0F8+pkJQDVA1YQxsCzX9p5 vSBw== X-Forwarded-Encrypted: i=1; AJvYcCUBSKBtFn43r4I4/GXGZPAoz5AFcvrvGtJuNqWtfFl9R7Avig8lsGBNR0F8DmyWicGoOTGZdt35TwNOz9fl@vger.kernel.org, AJvYcCUJSye+w4qovDl0biGAubUxAl8czLLnqT/lMtcIEJnLQf78WISNbOGTZRBf+JyGBZTcHdVE1BaROuU=@vger.kernel.org, AJvYcCWB/o9NN1LlgfJfmzqWJcYZNgT5k8bMd9r4GGWeafbjg6YYqM7TxhdmHE6eZ48EMUb2leFCH347LDRp@vger.kernel.org X-Gm-Message-State: AOJu0YzluczBqHh4nt3Ho1vBMzJHIm2ov9lHRfwKCEkQ1SboDNunwsi2 TLc6Nnmj1LPEUxnXsfLl92XZZRbhr0yI7EHUl6Q7eTcGbgx2abNy X-Google-Smtp-Source: AGHT+IH16YhgqJc4TDFTY4WqVFA6OgUeI7TOrM2qYc0gJLtft2hHJcY8r3m6gACAVo421s+GTOYLsg== X-Received: by 2002:adf:fb10:0:b0:37d:47d8:5fff with SMTP id ffacd0b85a97d-37eb47693c5mr1220989f8f.37.1729248558678; Fri, 18 Oct 2024 03:49:18 -0700 (PDT) Received: from localhost.localdomain (93-34-90-105.ip49.fastwebnet.it. [93.34.90.105]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37ecf0ed5f4sm1606240f8f.68.2024.10.18.03.49.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:49:18 -0700 (PDT) From: Christian Marangi To: "Rafael J. Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Christian Marangi , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Lorenzo Bianconi , upstream@airoha.com Subject: [PATCH v3 3/3] thermal: Add support for Airoha EN7581 thermal sensor Date: Fri, 18 Oct 2024 12:48:06 +0200 Message-ID: <20241018104839.13296-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018104839.13296-1-ansuelsmth@gmail.com> References: <20241018104839.13296-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add support for Airoha EN7581 thermal sensor. This provide support for reading the CPU or SoC Package sensor and to setup trip points for hot and critical condition. An interrupt is fired to react on this and doesn't require passive poll to read the temperature. The thermal regs provide a way to read the ADC value from an external register placed in the Chip SCU regs. Monitor will read this value and fire an interrupt if the trip condition configured is reached. Signed-off-by: Christian Marangi --- Changes v3: - Handle thermal_zone_device moved in different header - Enable interrupt after thermal register - Use new way to provide slope and offset Changes v2: - Add missing Makefile and Kconfig entry (somehow not included in v1) - Sort include header - Add missing bitfield.h drivers/thermal/Kconfig | 9 + drivers/thermal/Makefile | 1 + drivers/thermal/airoha_thermal.c | 482 +++++++++++++++++++++++++++++++ 3 files changed, 492 insertions(+) create mode 100644 drivers/thermal/airoha_thermal.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 204ed89a3ec9..766ef6ae9a9e 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -309,6 +309,15 @@ config QORIQ_THERMAL cpufreq is used as the cooling device to throttle CPUs when the passive trip is crossed. +config AIROHA_THERMAL + tristate "Airoha thermal sensor driver" + depends on ARCH_AIROHA || COMPILE_TEST + depends on MFD_SYSCON + depends on OF + help + Enable this to plug the Airoha thermal sensor driver into the Linux + thermal framework. + config SPEAR_THERMAL tristate "SPEAr thermal sensor driver" depends on PLAT_SPEAR || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 5cdf7d68687f..9b7431a371a9 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -35,6 +35,7 @@ obj-$(CONFIG_K3_THERMAL) += k3_bandgap.o k3_j72xx_bandgap.o # platform thermal drivers obj-y += broadcom/ obj-$(CONFIG_THERMAL_MMIO) += thermal_mmio.o +obj-$(CONFIG_AIROHA_THERMAL) += airoha_thermal.o obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o obj-$(CONFIG_SUN8I_THERMAL) += sun8i_thermal.o obj-$(CONFIG_ROCKCHIP_THERMAL) += rockchip_thermal.o diff --git a/drivers/thermal/airoha_thermal.c b/drivers/thermal/airoha_thermal.c new file mode 100644 index 000000000000..b747ff290991 --- /dev/null +++ b/drivers/thermal/airoha_thermal.c @@ -0,0 +1,482 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* SCU regs */ +#define EN7581_PLLRG_PROTECT 0x268 +#define EN7581_PWD_TADC 0x2ec +#define EN7581_MUX_TADC GENMASK(3, 1) +#define EN7581_DOUT_TADC 0x2f8 +#define EN7581_DOUT_TADC_MASK GENMASK(15, 0) + +/* PTP_THERMAL regs */ +#define EN7581_TEMPMONCTL0 0x800 +#define EN7581_SENSE3_EN BIT(3) +#define EN7581_SENSE2_EN BIT(2) +#define EN7581_SENSE1_EN BIT(1) +#define EN7581_SENSE0_EN BIT(0) +#define EN7581_TEMPMONCTL1 0x804 +/* period unit calculated in BUS clock * 256 scaling-up */ +#define EN7581_PERIOD_UNIT GENMASK(9, 0) +#define EN7581_TEMPMONCTL2 0x808 +#define EN7581_FILT_INTERVAL GENMASK(25, 16) +#define EN7581_SEN_INTERVAL GENMASK(9, 0) +#define EN7581_TEMPMONINT 0x80C +#define EN7581_STAGE3_INT_EN BIT(31) +#define EN7581_STAGE2_INT_EN BIT(30) +#define EN7581_STAGE1_INT_EN BIT(29) +#define EN7581_FILTER_INT_EN_3 BIT(28) +#define EN7581_IMMD_INT_EN3 BIT(27) +#define EN7581_NOHOTINTEN3 BIT(26) +#define EN7581_HOFSINTEN3 BIT(25) +#define EN7581_LOFSINTEN3 BIT(24) +#define EN7581_HINTEN3 BIT(23) +#define EN7581_CINTEN3 BIT(22) +#define EN7581_FILTER_INT_EN_2 BIT(21) +#define EN7581_FILTER_INT_EN_1 BIT(20) +#define EN7581_FILTER_INT_EN_0 BIT(19) +#define EN7581_IMMD_INT_EN2 BIT(18) +#define EN7581_IMMD_INT_EN1 BIT(17) +#define EN7581_IMMD_INT_EN0 BIT(16) +#define EN7581_TIME_OUT_INT_EN BIT(15) +#define EN7581_NOHOTINTEN2 BIT(14) +#define EN7581_HOFSINTEN2 BIT(13) +#define EN7581_LOFSINTEN2 BIT(12) +#define EN7581_HINTEN2 BIT(11) +#define EN7581_CINTEN2 BIT(10) +#define EN7581_NOHOTINTEN1 BIT(9) +#define EN7581_HOFSINTEN1 BIT(8) +#define EN7581_LOFSINTEN1 BIT(7) +#define EN7581_HINTEN1 BIT(6) +#define EN7581_CINTEN1 BIT(5) +#define EN7581_NOHOTINTEN0 BIT(4) +/* Similar to COLD and HOT also these seems to be swapped in documentation */ +#define EN7581_LOFSINTEN0 BIT(3) /* In documentation: BIT(2) */ +#define EN7581_HOFSINTEN0 BIT(2) /* In documentation: BIT(3) */ +/* It seems documentation have these swapped as the HW + * - Fire BIT(1) when lower than EN7581_COLD_THRE + * - Fire BIT(0) and BIT(5) when higher than EN7581_HOT2NORMAL_THRE or + * EN7581_HOT_THRE + */ +#define EN7581_CINTEN0 BIT(1) /* In documentation: BIT(0) */ +#define EN7581_HINTEN0 BIT(0) /* In documentation: BIT(1) */ +#define EN7581_TEMPMONINTSTS 0x810 +#define EN7581_STAGE3_INT_STAT BIT(31) +#define EN7581_STAGE2_INT_STAT BIT(30) +#define EN7581_STAGE1_INT_STAT BIT(29) +#define EN7581_FILTER_INT_STAT_3 BIT(28) +#define EN7581_IMMD_INT_STS3 BIT(27) +#define EN7581_NOHOTINTSTS3 BIT(26) +#define EN7581_HOFSINTSTS3 BIT(25) +#define EN7581_LOFSINTSTS3 BIT(24) +#define EN7581_HINTSTS3 BIT(23) +#define EN7581_CINTSTS3 BIT(22) +#define EN7581_FILTER_INT_STAT_2 BIT(21) +#define EN7581_FILTER_INT_STAT_1 BIT(20) +#define EN7581_FILTER_INT_STAT_0 BIT(19) +#define EN7581_IMMD_INT_STS2 BIT(18) +#define EN7581_IMMD_INT_STS1 BIT(17) +#define EN7581_IMMD_INT_STS0 BIT(16) +#define EN7581_TIME_OUT_INT_STAT BIT(15) +#define EN7581_NOHOTINTSTS2 BIT(14) +#define EN7581_HOFSINTSTS2 BIT(13) +#define EN7581_LOFSINTSTS2 BIT(12) +#define EN7581_HINTSTS2 BIT(11) +#define EN7581_CINTSTS2 BIT(10) +#define EN7581_NOHOTINTSTS1 BIT(9) +#define EN7581_HOFSINTSTS1 BIT(8) +#define EN7581_LOFSINTSTS1 BIT(7) +#define EN7581_HINTSTS1 BIT(6) +#define EN7581_CINTSTS1 BIT(5) +#define EN7581_NOHOTINTSTS0 BIT(4) +/* Similar to COLD and HOT also these seems to be swapped in documentation */ +#define EN7581_LOFSINTSTS0 BIT(3) /* In documentation: BIT(2) */ +#define EN7581_HOFSINTSTS0 BIT(2) /* In documentation: BIT(3) */ +/* It seems documentation have these swapped as the HW + * - Fire BIT(1) when lower than EN7581_COLD_THRE + * - Fire BIT(0) and BIT(5) when higher than EN7581_HOT2NORMAL_THRE or + * EN7581_HOT_THRE + * + * To clear things, we swap the define but we keep them documented here. + */ +#define EN7581_CINTSTS0 BIT(1) /* In documentation: BIT(0) */ +#define EN7581_HINTSTS0 BIT(0) /* In documentation: BIT(1)*/ +/* Monitor will take the bigger threshold between HOT2NORMAL and HOT + * and will fire both HOT2NORMAL and HOT interrupt when higher than the 2 + * + * It has also been observed that not setting HOT2NORMAL makes the monitor + * treat COLD threshold as HOT2NORMAL. + */ +#define EN7581_TEMPH2NTHRE 0x824 +/* It seems HOT2NORMAL is actually NORMAL2HOT */ +#define EN7581_HOT2NORMAL_THRE GENMASK(11, 0) +#define EN7581_TEMPHTHRE 0x828 +#define EN7581_HOT_THRE GENMASK(11, 0) +/* Monitor will use this as HOT2NORMAL (fire interrupt when lower than...)*/ +#define EN7581_TEMPCTHRE 0x82c +#define EN7581_COLD_THRE GENMASK(11, 0) +/* Also LOW and HIGH offset register are swapped */ +#define EN7581_TEMPOFFSETL 0x830 /* In documentation: 0x834 */ +#define EN7581_LOW_OFFSET GENMASK(11, 0) +#define EN7581_TEMPOFFSETH 0x834 /* In documentation: 0x830 */ +#define EN7581_HIGH_OFFSET GENMASK(11, 0) +#define EN7581_TEMPMSRCTL0 0x838 +#define EN7581_MSRCTL3 GENMASK(11, 9) +#define EN7581_MSRCTL2 GENMASK(8, 6) +#define EN7581_MSRCTL1 GENMASK(5, 3) +#define EN7581_MSRCTL0 GENMASK(2, 0) +#define EN7581_TEMPADCVALIDADDR 0x878 +#define EN7581_ADC_VALID_ADDR GENMASK(31, 0) +#define EN7581_TEMPADCVOLTADDR 0x87c +#define EN7581_ADC_VOLT_ADDR GENMASK(31, 0) +#define EN7581_TEMPRDCTRL 0x880 +/* + * NOTICE: AHB have this set to 0 by default. Means that + * the same addr is used for ADC volt and valid reading. + * In such case, VALID ADDR is used and volt addr is ignored. + */ +#define EN7581_RD_CTRL_DIFF BIT(0) +#define EN7581_TEMPADCVALIDMASK 0x884 +#define EN7581_ADV_RD_VALID_POLARITY BIT(5) +#define EN7581_ADV_RD_VALID_POS GENMASK(4, 0) +#define EN7581_TEMPADCVOLTAGESHIFT 0x888 +#define EN7581_ADC_VOLTAGE_SHIFT GENMASK(4, 0) +/* + * Same values for each CTL. + * Can operate in: + * - 1 sample + * - 2 sample and make average of them + * - 4,6,10,16 sample, drop max and min and make avgerage of them + */ +#define EN7581_MSRCTL_1SAMPLE 0x0 +#define EN7581_MSRCTL_AVG2SAMPLE 0x1 +#define EN7581_MSRCTL_4SAMPLE_MAX_MIX_AVG2 0x2 +#define EN7581_MSRCTL_6SAMPLE_MAX_MIX_AVG4 0x3 +#define EN7581_MSRCTL_10SAMPLE_MAX_MIX_AVG8 0x4 +#define EN7581_MSRCTL_18SAMPLE_MAX_MIX_AVG16 0x5 +#define EN7581_TEMPAHBPOLL 0x840 +#define EN7581_ADC_POLL_INTVL GENMASK(31, 0) +/* PTPSPARE0,2 reg are used to store efuse info for calibrated temp offset */ +#define EN7581_EFUSE_TEMP_OFFSET_REG 0xf20 /* PTPSPARE0 */ +#define EN7581_EFUSE_TEMP_OFFSET GENMASK(31, 16) +#define EN7581_PTPSPARE1 0xf24 /* PTPSPARE1 */ +#define EN7581_EFUSE_TEMP_CPU_SENSOR_REG 0xf28 /* PTPSPARE2 */ + +#define EN7581_SLOPE_X100_DIO_DEFAULT 5645 +#define EN7581_SLOPE_X100_DIO_AVS 5645 + +#define EN7581_INIT_TEMP_CPK_X10 300 +#define EN7581_INIT_TEMP_FTK_X10 620 +#define EN7581_INIT_TEMP_NONK_X10 550 + +#define EN7581_SCU_THERMAL_PROTECT_KEY 0x12 +#define EN7581_SCU_THERMAL_MUX_DIODE1 0x7 + +/* Convert temp to raw value as read from ADC ((((temp / 100) - init) * slope) / 1000) + offset */ +#define TEMP_TO_RAW(priv, tz, temp) ((((((temp) / 100) - (priv)->init_temp) * \ + thermal_zone_get_slope(tz)) / 1000) + \ + thermal_zone_get_offset(tz)) + +/* Convert raw to temp ((((temp - offset) * 1000) / slope + init) * 100) */ +#define RAW_TO_TEMP(priv, tz, raw) (((((raw) - thermal_zone_get_offset(tz)) * 1000) / \ + thermal_zone_get_slope(tz) + \ + (priv)->init_temp) * 100) + +struct airoha_thermal_priv { + void __iomem *base; + struct regmap *chip_scu; + struct resource scu_adc_res; + + struct thermal_zone_device *tz; + int init_temp; +}; + +static int airoha_get_thermal_ADC(struct airoha_thermal_priv *priv) +{ + u32 val; + + regmap_read(priv->chip_scu, EN7581_DOUT_TADC, &val); + return FIELD_GET(EN7581_DOUT_TADC_MASK, val); +} + +static void airoha_init_thermal_ADC_mode(struct airoha_thermal_priv *priv) +{ + u32 adc_mux, pllrg; + + /* Save PLLRG current value */ + regmap_read(priv->chip_scu, EN7581_PLLRG_PROTECT, &pllrg); + + /* Give access to thermal regs */ + regmap_write(priv->chip_scu, EN7581_PLLRG_PROTECT, EN7581_SCU_THERMAL_PROTECT_KEY); + adc_mux = FIELD_PREP(EN7581_MUX_TADC, EN7581_SCU_THERMAL_MUX_DIODE1); + regmap_write(priv->chip_scu, EN7581_PWD_TADC, adc_mux); + + /* Restore PLLRG value on exit */ + regmap_write(priv->chip_scu, EN7581_PLLRG_PROTECT, pllrg); +} + +static int airoha_thermal_get_temp(struct thermal_zone_device *tz, int *temp) +{ + struct airoha_thermal_priv *priv = thermal_zone_device_priv(tz); + int min, max, avg_temp, temp_adc; + int i; + + /* Get the starting temp */ + temp_adc = airoha_get_thermal_ADC(priv); + min = temp_adc; + max = temp_adc; + avg_temp = temp_adc; + + /* Make 5 more measurement and average the temp ADC difference */ + for (i = 0; i < 5; i++) { + temp_adc = airoha_get_thermal_ADC(priv); + avg_temp += temp_adc; + if (temp_adc > max) + max = temp_adc; + if (temp_adc < min) + min = temp_adc; + } + avg_temp = avg_temp - max - min; + avg_temp /= 4; + + *temp = RAW_TO_TEMP(priv, tz, avg_temp); + return 0; +} + +static int airoha_thermal_set_trips(struct thermal_zone_device *tz, int low, + int high) +{ + struct airoha_thermal_priv *priv = thermal_zone_device_priv(tz); + + if (high != INT_MAX) { + /* Validate high and clamp them a sane value */ + if (high > RAW_TO_TEMP(priv, tz, FIELD_MAX(EN7581_DOUT_TADC_MASK))) + high = 110000; + + /* We offset the high temp of 1°C to trigger correct event */ + writel(TEMP_TO_RAW(priv, tz, high) >> 4, + priv->base + EN7581_TEMPOFFSETH); + } + + if (low != -INT_MAX) { + /* Validate low and clamp them to a sane value */ + if (low < RAW_TO_TEMP(priv, tz, 0)) + low = -33000; + + /* We offset the low temp of 1°C to trigger correct event */ + writel(TEMP_TO_RAW(priv, tz, low) >> 4, + priv->base + EN7581_TEMPOFFSETL); + } + + /* Enable sensor 0 monitor */ + writel(EN7581_SENSE0_EN, priv->base + EN7581_TEMPMONCTL0); + + return 0; +} + +static const struct thermal_zone_device_ops thdev_ops = { + .get_temp = airoha_thermal_get_temp, + .set_trips = airoha_thermal_set_trips, +}; + +static irqreturn_t airoha_thermal_irq(int irq, void *data) +{ + struct airoha_thermal_priv *priv = data; + enum thermal_notify_event event; + u32 status; + + status = readl(priv->base + EN7581_TEMPMONINTSTS); + switch (status & (EN7581_HOFSINTSTS0 | EN7581_LOFSINTSTS0)) { + case EN7581_HOFSINTSTS0: + event = THERMAL_TRIP_VIOLATED; + break; + case EN7581_LOFSINTSTS0: + event = THERMAL_EVENT_UNSPECIFIED; + break; + default: + goto exit; + } + + thermal_zone_device_update(priv->tz, event); + +exit: + /* reset interrupt */ + writel(status, priv->base + EN7581_TEMPMONINTSTS); + + return IRQ_HANDLED; +} + +static void airoha_thermal_setup_adc_val(struct device *dev, + struct airoha_thermal_priv *priv, + struct thermal_zone_params *tzp) +{ + u32 efuse_calib_info, cpu_sensor; + + /* Setup thermal sensor to ADC mode and setup the mux to DIODE1 */ + airoha_init_thermal_ADC_mode(priv); + /* sleep 10 ms for ADC to enable */ + usleep_range(10 * USEC_PER_MSEC, 11 * USEC_PER_MSEC); + + efuse_calib_info = readl(priv->base + EN7581_EFUSE_TEMP_OFFSET_REG); + if (efuse_calib_info) { + tzp->offset = FIELD_GET(EN7581_EFUSE_TEMP_OFFSET, efuse_calib_info); + /* Different slope are applied if the sensor is used for CPU or for package */ + cpu_sensor = readl(priv->base + EN7581_EFUSE_TEMP_CPU_SENSOR_REG); + if (cpu_sensor) { + tzp->slope = EN7581_SLOPE_X100_DIO_DEFAULT; + priv->init_temp = EN7581_INIT_TEMP_FTK_X10; + } else { + tzp->slope = EN7581_SLOPE_X100_DIO_AVS; + priv->init_temp = EN7581_INIT_TEMP_CPK_X10; + } + } else { + tzp->offset = airoha_get_thermal_ADC(priv); + tzp->slope = EN7581_SLOPE_X100_DIO_DEFAULT; + priv->init_temp = EN7581_INIT_TEMP_NONK_X10; + dev_info(dev, "missing thermal calibrarion EFUSE, using non calibrated value\n"); + } +} + +static void airoha_thermal_setup_monitor(struct airoha_thermal_priv *priv) +{ + /* Set measure mode */ + writel(FIELD_PREP(EN7581_MSRCTL0, EN7581_MSRCTL_6SAMPLE_MAX_MIX_AVG4), + priv->base + EN7581_TEMPMSRCTL0); + + /* + * Configure ADC valid reading addr + * The AHB temp monitor system doesn't have direct access to the + * thermal sensor. It does instead work by providing all kind of + * address to configure how to access and setup an ADC for the + * sensor. EN7581 supports only one sensor hence the + * implementation is greatly simplified but the AHB supports + * up to 4 different sensor from the same ADC that can be + * switched by tuning the ADC mux or wiriting address. + * + * We set valid instead of volt as we don't enable valid/volt + * split reading and AHB read valid addr in such case. + */ + writel(priv->scu_adc_res.start + EN7581_DOUT_TADC, + priv->base + EN7581_TEMPADCVALIDADDR); + + /* + * Configure valid bit on a fake value of bit 16. The ADC outputs + * max of 2 bytes for voltage. + */ + writel(FIELD_PREP(EN7581_ADV_RD_VALID_POS, 16), + priv->base + EN7581_TEMPADCVALIDMASK); + + /* + * AHB supports max 12 bytes for ADC voltage. Shift the read + * value 4 bit to the right. Precision lost by this is minimal + * in the order of half a °C and is acceptable in the context + * of triggering interrupt in critical condition. + */ + writel(FIELD_PREP(EN7581_ADC_VOLTAGE_SHIFT, 4), + priv->base + EN7581_TEMPADCVOLTAGESHIFT); + + /* BUS clock is 300MHz counting unit is 3 * 68.64 * 256 = 52.715us */ + writel(FIELD_PREP(EN7581_PERIOD_UNIT, 3), + priv->base + EN7581_TEMPMONCTL1); + + /* + * filt interval is 1 * 52.715us = 52.715us, + * sen interval is 379 * 52.715us = 19.97ms + */ + writel(FIELD_PREP(EN7581_FILT_INTERVAL, 1) | + FIELD_PREP(EN7581_FILT_INTERVAL, 379), + priv->base + EN7581_TEMPMONCTL2); + + /* AHB poll is set to 146 * 68.64 = 10.02us */ + writel(FIELD_PREP(EN7581_ADC_POLL_INTVL, 146), + priv->base + EN7581_TEMPAHBPOLL); +} + +static int airoha_thermal_probe(struct platform_device *pdev) +{ + struct thermal_zone_params tzp = { }; + struct airoha_thermal_priv *priv; + struct device_node *chip_scu_np; + struct device *dev = &pdev->dev; + int irq, ret; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(priv->base)) + return PTR_ERR(priv->base); + + chip_scu_np = of_parse_phandle(dev->of_node, "airoha,chip-scu", 0); + if (!chip_scu_np) + return -EINVAL; + + priv->chip_scu = syscon_node_to_regmap(chip_scu_np); + if (IS_ERR(priv->chip_scu)) + return PTR_ERR(priv->chip_scu); + + of_address_to_resource(chip_scu_np, 0, &priv->scu_adc_res); + of_node_put(chip_scu_np); + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return irq; + + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, + airoha_thermal_irq, IRQF_ONESHOT, + pdev->name, (void *)priv); + if (ret) { + dev_err(dev, "Can't get interrupt working.\n"); + return ret; + } + + airoha_thermal_setup_monitor(priv); + airoha_thermal_setup_adc_val(dev, priv, &tzp); + + /* register of thermal sensor and get info from DT */ + priv->tz = devm_thermal_of_zone_register_with_params(dev, 0, priv, + &thdev_ops, + &tzp); + if (IS_ERR(priv->tz)) { + dev_err(dev, "register thermal zone sensor failed\n"); + return PTR_ERR(priv->tz); + } + + platform_set_drvdata(pdev, priv); + + /* Enable LOW and HIGH interrupt */ + writel(EN7581_HOFSINTEN0 | EN7581_LOFSINTEN0, + priv->base + EN7581_TEMPMONINT); + + return 0; +} + +static const struct of_device_id airoha_thermal_match[] = { + { .compatible = "airoha,en7581-thermal" }, + {}, +}; +MODULE_DEVICE_TABLE(of, airoha_thermal_match); + +static struct platform_driver airoha_thermal_driver = { + .driver = { + .name = "airoha-thermal", + .of_match_table = airoha_thermal_match, + }, + .probe = airoha_thermal_probe, +}; + +module_platform_driver(airoha_thermal_driver); + +MODULE_AUTHOR("Christian Marangi "); +MODULE_DESCRIPTION("Airoha thermal driver"); +MODULE_LICENSE("GPL");