Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp1629039imb; Sun, 3 Mar 2019 00:52:56 -0800 (PST) X-Google-Smtp-Source: APXvYqxnPaDt3G5sHpr3SPKdnlOCk7EdilEmi9VjpVwxlOKsmOHB0NnT8h7VAklGsSEd02tHWgmV X-Received: by 2002:a63:c310:: with SMTP id c16mr12709986pgd.233.1551603176397; Sun, 03 Mar 2019 00:52:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551603176; cv=none; d=google.com; s=arc-20160816; b=P8FnFLBjO1+8gwIYcia/kcLPA+zTDsIOwEmhGKeEpmoFgA2Y2RADDPLmzw3C3s/m9k 8cEgVsa8VpSAcw32pX/nAW/EZKd0paAiSRRlXck0g97+6zbNLg5vvb+s2nXMpmFvDZNT 3s8TmUj7+OQP7Hy3liuJo2JExZtxyePw4DUrFmID8sQZo5TB5BK1mEwHnmJWbIXca0I3 kXtv65Z+TRQcqI21nX4wzdY46i9LVx9eunJ95KoLYVDzcmBZRng2WjtSmrDu0+VAoWQ1 Vjt/TABd2yGszXqP6dWTuiGGAFH6H3eWyFJ/fGNwIfLHu8uArMvWI/1lNw73+Bi0z+WB uUyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=uvgl9YB0gG8S1/7re9g1bPiqrwBNcj6I0Q/FoZ7AF54=; b=jmd/MQ7vlMge+/6JZr01vemZbYTUL2Sw1pZ44ueZ2G1MkE/rZlgmzrN4TRiK8n6qih aOT54NPMHyQpI1x5lvxpzF8anST6wEMSkhbFZQAblXYBHKMAzNMeis4JF0QCcLJmZkex x4LFHSmHBar2titedD6FZWbbATjIiF5QTvlcF8vSi+MSUUIg4B2i+dRGi+IknjXSoKja 36KcycCAh5Stl1Bc0dRfG5BFcndXOMJaXTk1GzXQZA/JngtIw7wkJ3llZjMIgt/q+9y5 d3eYyJLmhokmQrR27PENntVFurVl/P2DFOIIPUudWUD5CIbzjwdoZDoCiOb64v2pyNRr gJYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=FdMOmpOu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10si2375761pgs.572.2019.03.03.00.52.40; Sun, 03 Mar 2019 00:52:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=FdMOmpOu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726127AbfCCIua (ORCPT + 99 others); Sun, 3 Mar 2019 03:50:30 -0500 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:54868 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726098AbfCCIu2 (ORCPT ); Sun, 3 Mar 2019 03:50:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1551603027; x=1583139027; h=from:to:subject:date:message-id:in-reply-to:references: mime-version; bh=uvgl9YB0gG8S1/7re9g1bPiqrwBNcj6I0Q/FoZ7AF54=; b=FdMOmpOu1040FJTKL0UU4HNHhjB5nwqccUeYTV2P9KpiG0Gb5Pe5SVux HWvJL17fVCkOJd4uIWvEBVCALe/+cpJJ9mkf47JrGq/7eIcMR9wg7Zf4m thIxJ5L/hm9qL6uhHk6L7pKz3CX+zPdl6B7XNvIdsV47e5HZNCordtLmf s=; X-IronPort-AV: E=Sophos;i="5.58,435,1544486400"; d="scan'208";a="791048739" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-1e-62350142.us-east-1.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 03 Mar 2019 08:50:26 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1e-62350142.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id x238oLOl051229 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL); Sun, 3 Mar 2019 08:50:23 GMT Received: from EX13D01EUB001.ant.amazon.com (10.43.166.194) by EX13MTAUEA001.ant.amazon.com (10.43.61.82) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Sun, 3 Mar 2019 08:50:22 +0000 Received: from udc4a3e82dbc15a031435.hfa14.amazon.com (10.43.161.217) by EX13D01EUB001.ant.amazon.com (10.43.166.194) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Sun, 3 Mar 2019 08:50:16 +0000 From: Talel Shenhar To: , , , , , , , , , , , Subject: [PATCH 2/2] thermal: Introduce thermal MMIO Date: Sun, 3 Mar 2019 10:49:26 +0200 Message-ID: <1551602966-2334-3-git-send-email-talel@amazon.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1551602966-2334-1-git-send-email-talel@amazon.com> References: <1551602966-2334-1-git-send-email-talel@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.217] X-ClientProxiedBy: EX13D24UWA004.ant.amazon.com (10.43.160.233) To EX13D01EUB001.ant.amazon.com (10.43.166.194) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces a generic thermal driver, which enables easy connectivity between "simple" thermal HW devices and the thermal subsystem. "simple" may be any system that allows temperature reading via a single memory-mapped read, be it register or shared memory, and that doesn't require any HW configuration to be done by the driver. Such "simple" HW is a candidate to work with this driver. Signed-off-by: Talel Shenhar --- drivers/thermal/Kconfig | 11 +++ drivers/thermal/Makefile | 3 + drivers/thermal/thermal_mmio.c | 214 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 drivers/thermal/thermal_mmio.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 1775d44..0033ba2 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -199,6 +199,17 @@ config THERMAL_EMULATION because userland can easily disable the thermal policy by simply flooding this sysfs node with low temperature values. +config THERMAL_MMIO + bool + depends on OF || COMPILE_TEST + prompt "mmio thermal driver" + help + This option enables the generic thermal MMIO driver that will use + memory-mapped reads to get the temperature. Any HW/System that + allows temperature reading by a single memory-mapped reading, be it + register or shared memory, is a potential candidate to work with this + driver. + config HISI_THERMAL tristate "Hisilicon thermal driver" depends on ARCH_HISI || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 82bb50d..beec616 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -27,6 +27,9 @@ thermal_sys-$(CONFIG_CLOCK_THERMAL) += clock_cooling.o # devfreq cooling thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o +# generic memory mapped thermal driver +thermal_sys-$(CONFIG_THERMAL_MMIO) += thermal_mmio.o + # platform thermal drivers obj-y += broadcom/ obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM) += qcom-spmi-temp-alarm.o diff --git a/drivers/thermal/thermal_mmio.c b/drivers/thermal/thermal_mmio.c new file mode 100644 index 0000000..1119e71 --- /dev/null +++ b/drivers/thermal/thermal_mmio.c @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + */ + +#include +#include +#include +#include + +#define THERMAL_MMIO_DEFAULT_SENSOR_WIDTH sizeof(u32) +#define THERMAL_MMIO_DEFAULT_SENSOR_FACTOR 1 +#define THERMAL_MMIO_DEFAULT_SENSOR_BIAS 0 +#define THERMAL_MMIO_DEFAULT_SENSOR_DIVIDER 1 + +struct thermal_mmio { + void __iomem *mmio_base; + u32 (*read_mmio)(void __iomem *mmio_base); + int width; + u32 mask; + int factor; + int bias; + int divider; +}; + +static u32 thermal_mmio_readb(void __iomem *mmio_base) +{ + return readb(mmio_base); +} + +static u32 thermal_mmio_readw(void __iomem *mmio_base) +{ + return readw(mmio_base); +} + +static u32 thermal_mmio_readl(void __iomem *mmio_base) +{ + return readl(mmio_base); +} + +static int thermal_mmio_get_temperature(void *private, int *temp) +{ + int t; + struct thermal_mmio *sensor = + (struct thermal_mmio *)private; + + t = sensor->read_mmio(sensor->mmio_base) & sensor->mask; + t *= sensor->factor; + t += sensor->bias; + t /= sensor->divider; + + *temp = t; + + return 0; +} + +static struct thermal_zone_of_device_ops thermal_mmio_ops = { + .get_temp = thermal_mmio_get_temperature, +}; + +static int thermal_mmio_parse_dt(struct platform_device *pdev, + struct thermal_mmio *sensor) +{ + struct resource *resource; + int ret; + + resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (IS_ERR(resource)) { + dev_err(&pdev->dev, + "fail to get platform memory resource (%ld)\n", + PTR_ERR(resource)); + return PTR_ERR(resource); + } + + sensor->mmio_base = devm_ioremap_resource(&pdev->dev, resource); + if (IS_ERR(sensor->mmio_base)) { + dev_err(&pdev->dev, "failed to ioremap memory (%ld)\n", + PTR_ERR(sensor->mmio_base)); + return PTR_ERR(sensor->mmio_base); + } + + ret = of_property_read_u32(pdev->dev.of_node, "sensor-width", + &sensor->width); + if (ret < 0) { + sensor->width = THERMAL_MMIO_DEFAULT_SENSOR_WIDTH; + dev_dbg(&pdev->dev, + "sensor-width undefined (%d) - using default: %d\n", + ret, sensor->width); + } + + ret = of_property_read_u32(pdev->dev.of_node, "sensor-mask", + &sensor->mask); + if (ret < 0) { + sensor->mask = GENMASK(sizeof(u32) * BITS_PER_BYTE - 1, 0); + dev_dbg(&pdev->dev, + "sensor-mask undefined (%d) - using default: 0x%x\n", + ret, sensor->mask); + } + + ret = of_property_read_s32(pdev->dev.of_node, "sensor-factor", + &sensor->factor); + if (ret < 0) { + sensor->factor = THERMAL_MMIO_DEFAULT_SENSOR_FACTOR; + dev_dbg(&pdev->dev, + "sensor-factor undefined (%d) - using default: %d\n", + ret, sensor->factor); + } + + ret = of_property_read_s32(pdev->dev.of_node, "sensor-bias", + &sensor->bias); + if (ret < 0) { + sensor->bias = THERMAL_MMIO_DEFAULT_SENSOR_BIAS; + dev_dbg(&pdev->dev, + "sensor-bias undefined (%d) - using default: %d\n", + ret, sensor->bias); + } + + ret = of_property_read_u32(pdev->dev.of_node, "sensor-divider", + &sensor->divider); + if (ret < 0) { + sensor->divider = THERMAL_MMIO_DEFAULT_SENSOR_DIVIDER; + dev_dbg(&pdev->dev, + "sensor-divider undefined (%d) - using default: %d\n", + ret, sensor->divider); + } + + /* configuration value sanity */ + if (sensor->width > resource_size(resource)) { + dev_err(&pdev->dev, + "too small MMIO size, expected (%d) < supplied (%d)\n", + sensor->width, (int)resource_size(resource)); + return -EINVAL; + } + + dev_dbg(&pdev->dev, + "width=%d, mask=0x%x factor=%d bias=%d divider=%d\n", + sensor->width, sensor->mask, sensor->factor, sensor->bias, + sensor->divider); + + return 0; +} + +static int thermal_mmio_probe(struct platform_device *pdev) +{ + struct thermal_mmio *sensor; + struct thermal_zone_device *thermal_zone; + int ret; + int temperature; + + sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL); + if (!sensor) + return -ENOMEM; + + ret = thermal_mmio_parse_dt(pdev, sensor); + if (ret) { + dev_err(&pdev->dev, "fail to parse dt\n"); + return ret; + } + + switch (sensor->width) { + case sizeof(u8): + sensor->read_mmio = thermal_mmio_readb; + break; + case sizeof(u16): + sensor->read_mmio = thermal_mmio_readw; + break; + case sizeof(u32): + sensor->read_mmio = thermal_mmio_readl; + break; + default: + dev_err(&pdev->dev, "bad sensor-width property supplied: %d\n", + sensor->width); + return -EINVAL; + } + + thermal_zone = devm_thermal_zone_of_sensor_register(&pdev->dev, + 0, + sensor, + &thermal_mmio_ops); + if (IS_ERR(thermal_zone)) { + dev_err(&pdev->dev, + "failed to register sensor (%ld)\n", + PTR_ERR(thermal_zone)); + return PTR_ERR(thermal_zone); + } + + thermal_mmio_get_temperature(sensor, &temperature); + dev_info(&pdev->dev, + "thermal mmio sensor %s registered, current temperature: %d\n", + pdev->name, temperature); + + return 0; +} + +static const struct of_device_id thermal_mmio_id_table[] = { + { .compatible = "thermal-mmio" }, + {} +}; +MODULE_DEVICE_TABLE(of, thermal_mmio_id_table); + +static struct platform_driver thermal_mmio_driver = { + .probe = thermal_mmio_probe, + .driver = { + .name = "thermal-mmio", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(thermal_mmio_id_table), + }, +}; + +module_platform_driver(thermal_mmio_driver); + +MODULE_AUTHOR("Talel Shenhar "); +MODULE_DESCRIPTION("Thermal MMIO Driver"); +MODULE_LICENSE("GPL v2"); -- 2.7.4