From patchwork Tue Jun 20 03:40:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Wang X-Patchwork-Id: 9798455 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 45880600C5 for ; Tue, 20 Jun 2017 03:49:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 40F3D204BE for ; Tue, 20 Jun 2017 03:49:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 35835205A4; Tue, 20 Jun 2017 03:49:34 +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 910AB204BE for ; Tue, 20 Jun 2017 03:49:33 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=dk2BeSLH5undBq4dCKyQUNApGdbsF57d3hLODMix1Iw=; b=iPaj6Z8ii7Mrna ujuyNd2X5hu6dblm9yNgeCZ9zIW/dFTnHA4NipvA1M1/6Hz/S1HVtuWpIzd84f4KbINtyw9ldrd1e 8xUfYTal0s3STapnuBE0gn8mFcin3Kn/qU/9mOxcjqktH61yvSUW4F/zrEqD46vAQ98CfTUOTFnpE jKP2ckk5byZ8JEPKzK3n/UjtEru0VX8NQkNCMbfX4AtW4Dc2JBlpu3eEAgqXYA4+NYbCeMaPwBJhc 4+W/LGtB+t4E5PEIfSfiZz2lnaRYzS8G13BDa3/2Aen+BjWfmFAnTUfYIxq1e68WfK1g+x4KEA9WE 5/Re6rhR36VlZeN9MPzw==; 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 1dNAAW-0000i9-Cn; Tue, 20 Jun 2017 03:49:32 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dNAAI-0007IM-0Y for linux-arm-kernel@lists.infradead.org; Tue, 20 Jun 2017 03:49:20 +0000 Received: from 172.30.72.53 (EHLO DGGEML401-HUB.china.huawei.com) ([172.30.72.53]) by dggrg02-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id APR06433; Tue, 20 Jun 2017 11:44:48 +0800 (CST) Received: from HSH1000038028.huawei.com (10.177.161.152) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server id 14.3.301.0; Tue, 20 Jun 2017 11:44:40 +0800 From: Tao Wang To: , , , , , , Subject: [PATCH 2/3] thermal: hisilicon: add thermal sensor driver for Hi3660 Date: Tue, 20 Jun 2017 11:40:34 +0800 Message-ID: <1497930035-60894-2-git-send-email-kevin.wangtao@hisilicon.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1497930035-60894-1-git-send-email-kevin.wangtao@hisilicon.com> References: <1497930035-60894-1-git-send-email-kevin.wangtao@hisilicon.com> MIME-Version: 1.0 X-Originating-IP: [10.177.161.152] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.59489A30.004E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: c3378aad449ba137e97a407a71b39069 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170619_204918_613071_E95BE7CA X-CRM114-Status: GOOD ( 23.49 ) 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, sunzhaosheng@hisilicon.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, leo.yan@linaro.org, Tao Wang , linux-arm-kernel@lists.infradead.org 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 This patch adds the support for thermal sensor of Hi3660 SoC. this will register sensors for thermal framework and use device tree to bind cooling device. Signed-off-by: Tao Wang Signed-off-by: Leo Yan --- drivers/thermal/Kconfig | 10 ++ drivers/thermal/Makefile | 1 + drivers/thermal/hi3660_thermal.c | 198 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 drivers/thermal/hi3660_thermal.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index b5b5fac..3e7fa95 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -203,6 +203,16 @@ config HISI_THERMAL thermal framework. cpufreq is used as the cooling device to throttle CPUs when the passive trip is crossed. +config HI3660_THERMAL + tristate "Hi3660 thermal driver" + depends on ARCH_HISI || COMPILE_TEST + depends on HAS_IOMEM + depends on OF + default y + help + Enable this to plug Hi3660 thermal driver into the Linux thermal + framework. + config IMX_THERMAL tristate "Temperature sensor driver for Freescale i.MX SoCs" depends on (ARCH_MXC && CPU_THERMAL) || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 094d703..f29d0a5 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -55,6 +55,7 @@ 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-$(CONFIG_HI3660_THERMAL) += hi3660_thermal.o 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/hi3660_thermal.c b/drivers/thermal/hi3660_thermal.c new file mode 100644 index 0000000..a538721 --- /dev/null +++ b/drivers/thermal/hi3660_thermal.c @@ -0,0 +1,198 @@ +/* + * linux/drivers/thermal/hi3660_thermal.c + * + * Copyright (c) 2017 Hisilicon Limited. + * Copyright (c) 2017 Linaro Limited. + * + * Author: Tao Wang + * Author: Leo Yan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "thermal_core.h" + +#define HW_MAX_SENSORS 4 +#define HISI_MAX_SENSORS 6 +#define SENSOR_MAX 4 +#define SENSOR_AVG 5 + +#define ADC_MIN 116 +#define ADC_MAX 922 + +/* hi3660 Thermal Sensor Dev Structure */ +struct hi3660_thermal_sensor { + struct hi3660_thermal_data *thermal; + struct thermal_zone_device *tzd; + + uint32_t id; +}; + +struct hi3660_thermal_data { + struct platform_device *pdev; + struct hi3660_thermal_sensor sensors[HISI_MAX_SENSORS]; + void __iomem *thermal_base; +}; + +unsigned int sensor_reg_offset[HW_MAX_SENSORS] = { 0x1c, 0x5c, 0x9c, 0xdc }; + + +static int hi3660_thermal_get_temp(void *_sensor, int *temp) +{ + struct hi3660_thermal_sensor *sensor = _sensor; + struct hi3660_thermal_data *data = sensor->thermal; + unsigned int idx; + int val, average = 0, max = 0; + + if (sensor->id < HW_MAX_SENSORS) { + val = readl(data->thermal_base + sensor_reg_offset[sensor->id]); + val = clamp_val(val, ADC_MIN, ADC_MAX); + } else { + for (idx = 0; idx < HW_MAX_SENSORS; idx++) { + val = readl(data->thermal_base + + sensor_reg_offset[idx]); + val = clamp_val(val, ADC_MIN, ADC_MAX); + average += val; + if (val > max) + max = val; + } + + if (sensor->id == SENSOR_MAX) + val = max; + else if (sensor->id == SENSOR_AVG) + val = average / HW_MAX_SENSORS; + } + + *temp = ((val - ADC_MIN) * 165000) / (ADC_MAX - ADC_MIN) - 40000; + + return 0; +} + +static struct thermal_zone_of_device_ops hi3660_of_thermal_ops = { + .get_temp = hi3660_thermal_get_temp, +}; + +static int hi3660_thermal_register_sensor(struct platform_device *pdev, + struct hi3660_thermal_data *data, + struct hi3660_thermal_sensor *sensor, + int index) +{ + int ret = 0; + + sensor->id = index; + sensor->thermal = data; + + sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, + sensor->id, sensor, &hi3660_of_thermal_ops); + if (IS_ERR(sensor->tzd)) { + ret = PTR_ERR(sensor->tzd); + sensor->tzd = NULL; + } + + return ret; +} + +static void hi3660_thermal_toggle_sensor(struct hi3660_thermal_sensor *sensor, + bool on) +{ + struct thermal_zone_device *tzd = sensor->tzd; + + tzd->ops->set_mode(tzd, + on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED); +} + +static int hi3660_thermal_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct hi3660_thermal_data *data; + struct resource *res; + int ret = 0; + int i; + + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->pdev = pdev; + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + data->thermal_base = devm_ioremap_resource(dev, res); + if (IS_ERR(data->thermal_base)) { + dev_err(dev, "failed get reg base\n"); + return -ENOMEM; + } + + platform_set_drvdata(pdev, data); + + for (i = 0; i < HISI_MAX_SENSORS; ++i) { + ret = hi3660_thermal_register_sensor(pdev, data, + &data->sensors[i], i); + if (ret) + dev_err(&pdev->dev, + "failed to register thermal sensor%d: %d\n", + i, ret); + else + hi3660_thermal_toggle_sensor(&data->sensors[i], true); + } + + dev_info(&pdev->dev, "Thermal Sensor Loaded\n"); + return 0; +} + +static int hi3660_thermal_exit(struct platform_device *pdev) +{ + struct hi3660_thermal_data *data = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < HISI_MAX_SENSORS; i++) { + struct hi3660_thermal_sensor *sensor = &data->sensors[i]; + + if (!sensor->tzd) + continue; + + hi3660_thermal_toggle_sensor(sensor, false); + } + + return 0; +} + +static const struct of_device_id hi3660_thermal_id_table[] = { + { .compatible = "hisilicon,thermal-hi3660" }, + {} +}; +MODULE_DEVICE_TABLE(of, hi3660_thermal_id_table); + +static struct platform_driver hi3660_thermal_driver = { + .probe = hi3660_thermal_probe, + .remove = hi3660_thermal_exit, + .driver = { + .name = "hi3660_thermal", + .of_match_table = hi3660_thermal_id_table, + }, +}; + +module_platform_driver(hi3660_thermal_driver); + +MODULE_AUTHOR("Tao Wang "); +MODULE_AUTHOR("Leo Yan "); +MODULE_DESCRIPTION("hi3660 thermal driver"); +MODULE_LICENSE("GPL v2");