From patchwork Fri Jun 16 11:28:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikko Perttunen X-Patchwork-Id: 9791145 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4B68760325 for ; Fri, 16 Jun 2017 11:30:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F0022823D for ; Fri, 16 Jun 2017 11:30:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33BF228456; Fri, 16 Jun 2017 11:30:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7D6A02848F for ; Fri, 16 Jun 2017 11:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4yXn27RspS0SnWQyMnuCuqmRUTLKZUY3WRiAmkwV8pY=; b=KFGXm/nZ7RSWqwSDRvodQbUCrZ Rq7AqyWAyVeSHs05dHuVnjwctmP0yadStK9F1QtThouNMkhJGnK0aYc6BL6ze+u9if9OjkqGWrDC0 /eiBDAOX4uCiuy4+ajSuyeDNfrRZeKekiotz4V1egBabi4InEuK7QOdbi30GX/EeB+gAQ2BEPBKQB dNPV76bemqseACfx4FmBQGaXJlWyeVWnf5YyQgO7aLiWEpO+TTPvJ129mrr4gq9iV4prOYJrZ1cQP 7gmsKcsL8/xPh18R2pQ+JTap/Tg3LlYjCPo62v2n7LqxHDSDdIvLCaVuhMEzQ+ddkWQhQs5AvQxkU NvZU3JqQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dLpSW-0001vR-Gd; Fri, 16 Jun 2017 11:30:36 +0000 Received: from mail.kapsi.fi ([2001:1bc8:1004::1:25]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dLpRz-0008Oe-Np for linux-arm-kernel@lists.infradead.org; Fri, 16 Jun 2017 11:30:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=kapsi.fi; s=20161220; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=huPUxX2BZQCzwSxtDYu8Nw1BlHV8PSLy56gf55p6smw=; b=gBro1qDn3HmuuYBrfmiVk/I67S/in1/tjMuhFwxZt9U084uHhs9OmUXpkkHnSDaBn8l2CYOnEZETw87ovMH/Jz+2MeaPMbW8g9321pQTMIu/pXnckW/ej12/RYcNcCZAHq5nvPTBbnK6ocL8JeIORIOSsUC+WEAfpz5KjJPVuOlLp3zTdkIs9DjRqGun++3A7VWiu9ZX8yMLN/CMNGaERVqd+ZHglg0QRvQnfE5zh6b2ijDX41R2Tmtk05R6KwRMV9yTAGehn60KElxLPG+QC/BmmJ20DCZDk5KhKI2dtN0iXOsaXuSxKd9fTA1MwQMlxduzbHKi46uKFpxDxXCdQQ==; Received: from dsl-hkibng41-56732b-124.dhcp.inet.fi ([86.115.43.124] helo=localhost.localdomain) by mail.kapsi.fi with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1dLpRO-00080N-Ua; Fri, 16 Jun 2017 14:29:26 +0300 From: Mikko Perttunen To: robh+dt@kernel.org, mark.rutland@arm.com, rui.zhang@intel.com, edubezval@gmail.com, thierry.reding@gmail.com, jonathanh@nvidia.com Subject: [PATCH 4/4] thermal: Add Tegra BPMP thermal sensor driver Date: Fri, 16 Jun 2017 14:28:25 +0300 Message-Id: <20170616112825.31025-4-mperttunen@nvidia.com> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20170616112825.31025-1-mperttunen@nvidia.com> References: <20170616112825.31025-1-mperttunen@nvidia.com> X-SA-Exim-Connect-IP: 86.115.43.124 X-SA-Exim-Mail-From: mperttunen@nvidia.COM X-SA-Exim-Scanned: No (on mail.kapsi.fi); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170616_043004_200902_A9705BA4 X-CRM114-Status: GOOD ( 19.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Mikko Perttunen , linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On Tegra186, the BPMP (Boot and Power Management Processor) exposes an interface to thermal sensors on the system-on-chip. This driver implements access to the interface. It supports reading the temperature, setting trip points and receiving notification of a tripped trip point. Signed-off-by: Mikko Perttunen --- drivers/thermal/Makefile | 2 +- drivers/thermal/tegra/Kconfig | 7 + drivers/thermal/tegra/Makefile | 3 +- drivers/thermal/tegra/bpmp-thermal.c | 253 +++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 drivers/thermal/tegra/bpmp-thermal.c diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 094d7039981c..c03dccdba7b8 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -54,7 +54,7 @@ obj-$(CONFIG_INTEL_BXT_PMIC_THERMAL) += intel_bxt_pmic_thermal.o obj-$(CONFIG_INTEL_PCH_THERMAL) += intel_pch_thermal.o obj-$(CONFIG_ST_THERMAL) += st/ obj-$(CONFIG_QCOM_TSENS) += qcom/ -obj-$(CONFIG_TEGRA_SOCTHERM) += tegra/ +obj-y += tegra/ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o obj-$(CONFIG_MTK_THERMAL) += mtk_thermal.o obj-$(CONFIG_GENERIC_ADC_THERMAL) += thermal-generic-adc.o diff --git a/drivers/thermal/tegra/Kconfig b/drivers/thermal/tegra/Kconfig index cec586ec7e4b..36e4b03bb98b 100644 --- a/drivers/thermal/tegra/Kconfig +++ b/drivers/thermal/tegra/Kconfig @@ -10,4 +10,11 @@ config TEGRA_SOCTHERM zones to manage temperatures. This option is also required for the emergency thermal reset (thermtrip) feature to function. +config TEGRA_BPMP_THERMAL + tristate "Tegra BPMP thermal sensing" + depends on TEGRA_BPMP + help + Enable this option for support for sensing system temperature of NVIDIA + Tegra systems-on-chip with the BPMP coprocessor (Tegra186). + endmenu diff --git a/drivers/thermal/tegra/Makefile b/drivers/thermal/tegra/Makefile index 1ce1af2cf0f5..757abcd1feaf 100644 --- a/drivers/thermal/tegra/Makefile +++ b/drivers/thermal/tegra/Makefile @@ -1,4 +1,5 @@ -obj-$(CONFIG_TEGRA_SOCTHERM) += tegra-soctherm.o +obj-$(CONFIG_TEGRA_SOCTHERM) += tegra-soctherm.o +obj-$(CONFIG_TEGRA_BPMP_THERMAL) += bpmp-thermal.o tegra-soctherm-y := soctherm.o soctherm-fuse.o tegra-soctherm-$(CONFIG_ARCH_TEGRA_124_SOC) += tegra124-soctherm.o diff --git a/drivers/thermal/tegra/bpmp-thermal.c b/drivers/thermal/tegra/bpmp-thermal.c new file mode 100644 index 000000000000..3465a201d1ac --- /dev/null +++ b/drivers/thermal/tegra/bpmp-thermal.c @@ -0,0 +1,253 @@ +/* + * Copyright (c) 2015-2017, NVIDIA CORPORATION. All rights reserved. + * + * Author: + * Mikko Perttunen + * Aapo Vienamo + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +struct tegra_bpmp_thermal_zone { + struct tegra_bpmp_thermal *tegra; + struct thermal_zone_device *tzd; + struct work_struct tz_device_update_work; + unsigned int idx; +}; + +struct tegra_bpmp_thermal { + struct device *dev; + struct tegra_bpmp *bpmp; + unsigned int num_zones; + struct tegra_bpmp_thermal_zone *zones; +}; + +static int tegra_bpmp_thermal_get_temp(void *data, int *out_temp) +{ + struct tegra_bpmp_thermal_zone *zone = data; + struct mrq_thermal_host_to_bpmp_request req; + union mrq_thermal_bpmp_to_host_response reply; + struct tegra_bpmp_message msg; + int err; + + memset(&req, 0, sizeof(req)); + req.type = CMD_THERMAL_GET_TEMP; + req.get_temp.zone = zone->idx; + + memset(&msg, 0, sizeof(msg)); + msg.mrq = MRQ_THERMAL; + msg.tx.data = &req; + msg.tx.size = sizeof(req); + msg.rx.data = &reply; + msg.rx.size = sizeof(reply); + + err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg); + if (err) + return err; + + *out_temp = reply.get_temp.temp; + + return 0; +} + +static int tegra_bpmp_thermal_set_trips(void *data, int low, int high) +{ + struct tegra_bpmp_thermal_zone *zone = data; + struct mrq_thermal_host_to_bpmp_request req; + struct tegra_bpmp_message msg; + + memset(&req, 0, sizeof(req)); + req.type = CMD_THERMAL_SET_TRIP; + req.set_trip.zone = zone->idx; + req.set_trip.enabled = true; + req.set_trip.low = low; + req.set_trip.high = high; + + memset(&msg, 0, sizeof(msg)); + msg.mrq = MRQ_THERMAL; + msg.tx.data = &req; + msg.tx.size = sizeof(req); + + return tegra_bpmp_transfer(zone->tegra->bpmp, &msg); +} + +static void tz_device_update_work_fn(struct work_struct *work) +{ + struct tegra_bpmp_thermal_zone *zone; + + zone = container_of(work, struct tegra_bpmp_thermal_zone, + tz_device_update_work); + + thermal_zone_device_update(zone->tzd, THERMAL_TRIP_VIOLATED); +} + +static void bpmp_mrq_thermal(unsigned int mrq, struct tegra_bpmp_channel *ch, + void *data) +{ + struct mrq_thermal_bpmp_to_host_request *req; + struct tegra_bpmp_thermal *tegra = data; + int zone_idx; + + req = (struct mrq_thermal_bpmp_to_host_request *)ch->ib->data; + + if (req->type != CMD_THERMAL_HOST_TRIP_REACHED) { + dev_err(tegra->dev, "%s: invalid request type: %d\n", + __func__, req->type); + tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); + return; + } + + zone_idx = req->host_trip_reached.zone; + if (zone_idx >= tegra->num_zones) { + dev_err(tegra->dev, "%s: invalid thermal zone: %d\n", + __func__, zone_idx); + tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); + return; + } + + tegra_bpmp_mrq_return(ch, 0, NULL, 0); + + schedule_work(&tegra->zones[zone_idx].tz_device_update_work); +} + +static int tegra_bpmp_thermal_get_num_zones(struct tegra_bpmp *bpmp, + int *num_zones) +{ + struct mrq_thermal_host_to_bpmp_request req; + union mrq_thermal_bpmp_to_host_response reply; + struct tegra_bpmp_message msg; + int err; + + memset(&req, 0, sizeof(req)); + req.type = CMD_THERMAL_GET_NUM_ZONES; + + memset(&msg, 0, sizeof(msg)); + msg.mrq = MRQ_THERMAL; + msg.tx.data = &req; + msg.tx.size = sizeof(req); + msg.rx.data = &reply; + msg.rx.size = sizeof(reply); + + err = tegra_bpmp_transfer(bpmp, &msg); + if (err) + return err; + + *num_zones = reply.get_num_zones.num; + + return 0; +} + +static const struct thermal_zone_of_device_ops tegra_bpmp_of_thermal_ops = { + .get_temp = tegra_bpmp_thermal_get_temp, + .set_trips = tegra_bpmp_thermal_set_trips, +}; + +static int tegra_bpmp_thermal_probe(struct platform_device *pdev) +{ + struct tegra_bpmp *bpmp = dev_get_drvdata(pdev->dev.parent); + struct tegra_bpmp_thermal *tegra; + struct thermal_zone_device *tzd; + unsigned int i; + int err; + + tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); + if (!tegra) + return -ENOMEM; + + tegra->dev = &pdev->dev; + tegra->bpmp = bpmp; + + err = tegra_bpmp_thermal_get_num_zones(bpmp, &tegra->num_zones); + if (err) { + dev_err(&pdev->dev, "failed to get the number of zones: %d\n", + err); + return err; + } + + tegra->zones = devm_kcalloc(&pdev->dev, tegra->num_zones, + sizeof(*tegra->zones), GFP_KERNEL); + if (!tegra->zones) + return -ENOMEM; + + for (i = 0; i < tegra->num_zones; ++i) { + int temp; + + tegra->zones[i].idx = i; + tegra->zones[i].tegra = tegra; + + err = tegra_bpmp_thermal_get_temp(&tegra->zones[i], &temp); + if (err < 0) + continue; + + tzd = devm_thermal_zone_of_sensor_register( + &pdev->dev, i, &tegra->zones[i], + &tegra_bpmp_of_thermal_ops); + if (IS_ERR(tzd)) { + if (PTR_ERR(tzd) == -EPROBE_DEFER) + return -EPROBE_DEFER; + continue; + } + + tegra->zones[i].tzd = tzd; + INIT_WORK(&tegra->zones[i].tz_device_update_work, + tz_device_update_work_fn); + } + + err = tegra_bpmp_request_mrq(bpmp, MRQ_THERMAL, bpmp_mrq_thermal, + tegra); + if (err) { + dev_err(&pdev->dev, "failed to register mrq handler: %d\n", + err); + return err; + } + + platform_set_drvdata(pdev, tegra); + + return 0; +} + +static int tegra_bpmp_thermal_remove(struct platform_device *pdev) +{ + struct tegra_bpmp_thermal *tegra = platform_get_drvdata(pdev); + + tegra_bpmp_free_mrq(tegra->bpmp, MRQ_THERMAL, tegra); + + return 0; +} + +static const struct of_device_id tegra_bpmp_thermal_of_match[] = { + { .compatible = "nvidia,tegra186-bpmp-thermal" }, + { }, +}; +MODULE_DEVICE_TABLE(of, tegra_bpmp_thermal_of_match); + +static struct platform_driver tegra_bpmp_thermal_driver = { + .probe = tegra_bpmp_thermal_probe, + .remove = tegra_bpmp_thermal_remove, + .driver = { + .name = "tegra-bpmp-thermal", + .of_match_table = tegra_bpmp_thermal_of_match, + }, +}; +module_platform_driver(tegra_bpmp_thermal_driver); + +MODULE_AUTHOR("Mikko Perttunen "); +MODULE_DESCRIPTION("NVIDIA Tegra BPMP thermal sensor driver"); +MODULE_LICENSE("GPL v2");