From patchwork Thu Aug 10 08:32:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wangtao \(Kevin, Kirin\)" X-Patchwork-Id: 109794 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2033306qge; Thu, 10 Aug 2017 01:40:19 -0700 (PDT) X-Received: by 10.99.107.3 with SMTP id g3mr10663676pgc.172.1502354418909; Thu, 10 Aug 2017 01:40:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502354418; cv=none; d=google.com; s=arc-20160816; b=eJI8fjrddtWZANfc43ZVdL3nv+yhNfRTW5QT8B368v/R9552dngvDRF7LyRjfL019w 9pFyVZGGLVX8BQjQ3xuig+nYTMiVE90hJf358nbtl51fVXMylxlli9BVoNdywnTalS8N +NUbo+yV54vswJxRyi3K+fMh7DCsZNv1WhO+Skie5Kd+KPt0gbdAYmeQCqnqs3avX4dt MOB6a9DK7KiLA/fr6MZJQ+w0dUGXwcC4xt2nPHPop9LrisoIRJD0KM2vabQGYn50RcEN FASbIq6XkiJC2L6iKNA0E+JB/sLHguEdcfirvUx9jM/4GbA8tf/Y1Md6mYxvs6LBimFt vY5w== 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:cc:to:from:arc-authentication-results; bh=ByUDiysw/yr9qOGL9cGBPH+k60IJf7sj7Q/hP3u6ZdM=; b=BYBPPazt+9G7jjSiNgQXp4ochpJ3F1dxjaVVlELgnppJUJnEpZI9Is0ift0pGrUmc9 4XIomZDce/uz9MXOlsHlC7qvCrucfdfoMsWGyUrAWkjftdxKvu/g/4CFni3jbu0+/gpH gPuY9vPDg0q59kSLacpEZqtMr3W7ff7OvuttxgcbNUHGKl9EwgV5D1BIVhfn/On8u57p 9LDFKO0y1BdeqW3MHRAPBYdLHR5sICK22lxyaU2tfO/LMKHU+J/i3ecc4nvgrrAD65UT L6niwn/PhSkOQYINpKsnBaDx/LxysLjRm6jG+k/92RYzFhOgH653u/xcimTCRiHpiNWT MtSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si4111665pli.422.2017.08.10.01.40.18; Thu, 10 Aug 2017 01:40:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751675AbdHJIkR (ORCPT + 6 others); Thu, 10 Aug 2017 04:40:17 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:3042 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751361AbdHJIiM (ORCPT ); Thu, 10 Aug 2017 04:38:12 -0400 Received: from 172.30.72.60 (EHLO DGGEMS409-HUB.china.huawei.com) ([172.30.72.60]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DEX89444; Thu, 10 Aug 2017 16:38:06 +0800 (CST) Received: from HSH1000038028.huawei.com (10.177.161.152) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.301.0; Thu, 10 Aug 2017 16:37:56 +0800 From: Tao Wang To: , , , , , , CC: , , , , , , Subject: [PATCH v3 2/3] thermal: hisilicon: add thermal sensor driver for Hi3660 Date: Thu, 10 Aug 2017 16:32:14 +0800 Message-ID: <1502353935-92924-3-git-send-email-kevin.wangtao@hisilicon.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1502353935-92924-1-git-send-email-kevin.wangtao@hisilicon.com> References: <1498102923-68481-1-git-send-email-kevin.wangtao@hisilicon.com> <1502353935-92924-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.0A020206.598C1B6F.005F, 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: d8cd2a569ea1dc5ce3cd9802ec9aa838 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Tao Wang 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 | 13 +++ drivers/thermal/Makefile | 1 + drivers/thermal/hisi_tsensor.c | 223 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 drivers/thermal/hisi_tsensor.c -- 2.8.1 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index b5b5fac..32f582d 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -203,6 +203,19 @@ config HISI_THERMAL thermal framework. cpufreq is used as the cooling device to throttle CPUs when the passive trip is crossed. +config HISI_TSENSOR + tristate "Hisilicon tsensor driver" + depends on ARCH_HISI || COMPILE_TEST + depends on HAS_IOMEM + depends on OF + default y + help + Enable this to plug Hisilicon's tsensor driver into the Linux thermal + framework. Besides all the hardware sensors, this also support two + virtual sensors, one for maximum of all the hardware sensor, and + one for average of all the hardware sensor. + Compitable with Hi3660 or higher. + 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..8a16bd4 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -56,6 +56,7 @@ obj-$(CONFIG_ST_THERMAL) += st/ obj-$(CONFIG_QCOM_TSENS) += qcom/ obj-$(CONFIG_TEGRA_SOCTHERM) += tegra/ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o +obj-$(CONFIG_HISI_TSENSOR) += hisi_tsensor.o obj-$(CONFIG_MTK_THERMAL) += mtk_thermal.o obj-$(CONFIG_GENERIC_ADC_THERMAL) += thermal-generic-adc.o obj-$(CONFIG_ZX2967_THERMAL) += zx2967_thermal.o diff --git a/drivers/thermal/hisi_tsensor.c b/drivers/thermal/hisi_tsensor.c new file mode 100644 index 0000000..c8eec9d --- /dev/null +++ b/drivers/thermal/hisi_tsensor.c @@ -0,0 +1,223 @@ +/* + * linux/drivers/thermal/hisi_tsensor.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 VIRTUAL_SENSORS 2 + +/* hisi Thermal Sensor Dev Structure */ +struct hisi_thermal_sensor { + struct hisi_thermal_data *thermal; + struct thermal_zone_device *tzd; + unsigned int id; +}; + +struct hisi_thermal_data { + struct platform_device *pdev; + struct hisi_thermal_sensor *sensors; + void __iomem *thermal_base; + unsigned int *reg_offset; + unsigned int range[2]; + unsigned int coef[2]; + unsigned int max_hw_sensor; +}; + +static int hisi_thermal_get_temp(void *_sensor, int *temp) +{ + struct hisi_thermal_sensor *sensor = _sensor; + struct hisi_thermal_data *data = sensor->thermal; + unsigned int idx, adc_min, adc_max, max_sensor; + int val, average = 0, max = 0; + + adc_min = data->range[0]; + adc_max = data->range[1]; + max_sensor = data->max_hw_sensor; + + if (sensor->id < max_sensor) { + val = readl(data->thermal_base + data->reg_offset[sensor->id]); + val = clamp_val(val, adc_min, adc_max); + } else { + for (idx = 0; idx < max_sensor; idx++) { + val = readl(data->thermal_base + + data->reg_offset[idx]); + val = clamp_val(val, adc_min, adc_max); + average += val; + if (val > max) + max = val; + } + + if (sensor->id == max_sensor) + val = max; + else + val = average / max_sensor; + } + + *temp = ((val - adc_min) * data->coef[0]) / (adc_max - adc_min) + + data->coef[1]; + + return 0; +} + +static struct thermal_zone_of_device_ops hisi_of_thermal_ops = { + .get_temp = hisi_thermal_get_temp, +}; + +static void hisi_thermal_toggle_sensor(struct hisi_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 hisi_thermal_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + struct hisi_thermal_data *data; + struct hisi_thermal_sensor *sensor; + struct resource *res; + unsigned int max_sensor; + int ret, 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 to get reg base\n"); + return -ENOMEM; + } + + max_sensor = of_property_count_u32_elems(np, "offset"); + if (max_sensor < 0) { + dev_err(dev, "failed to get max sensor\n"); + return -EINVAL; + } + data->max_hw_sensor = max_sensor; + + data->sensors = devm_kzalloc(dev, + sizeof(*data->sensors) * (max_sensor + VIRTUAL_SENSORS), + GFP_KERNEL); + if (IS_ERR(data->sensors)) { + dev_err(dev, "failed to alloc sensors\n"); + return -ENOMEM; + } + + data->reg_offset = devm_kzalloc(dev, + sizeof(*data->reg_offset) * max_sensor, GFP_KERNEL); + if (IS_ERR(data->reg_offset)) { + dev_err(dev, "failed to alloc sensor offset\n"); + return -ENOMEM; + } + + ret = of_property_read_u32_array(np, + "offset", data->reg_offset, max_sensor); + if (ret < 0) { + dev_err(dev, "failed to get reg offset\n"); + return -EINVAL; + } + + ret = of_property_read_u32_array(np, "coefficients", data->coef, 2); + if (ret < 0) { + dev_err(dev, "failed to get coef\n"); + return -EINVAL; + } + + ret = of_property_read_u32_array(np, "hisi,adc-range", data->range, 2); + if (ret < 0) { + dev_err(dev, "failed to get range\n"); + return -EINVAL; + } + + platform_set_drvdata(pdev, data); + + for (i = 0; i < max_sensor + VIRTUAL_SENSORS; ++i) { + sensor = &data->sensors[i]; + sensor->id = i; + sensor->thermal = data; + sensor->tzd = thermal_zone_of_sensor_register(dev, + i, sensor, &hisi_of_thermal_ops); + if (IS_ERR(sensor->tzd)) { + sensor->tzd = NULL; + } else { + hisi_thermal_toggle_sensor(sensor, true); + dev_info(dev, "thermal sensor%d registered\n", i); + } + } + + return 0; +} + +static int hisi_thermal_exit(struct platform_device *pdev) +{ + struct hisi_thermal_data *data = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < data->max_hw_sensor + VIRTUAL_SENSORS; i++) { + struct hisi_thermal_sensor *sensor = &data->sensors[i]; + + if (!sensor->tzd) + continue; + + hisi_thermal_toggle_sensor(sensor, false); + thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd); + } + + return 0; +} + +static const struct of_device_id hisi_thermal_id_table[] = { + { .compatible = "hisilicon,tsensor" }, + {} +}; +MODULE_DEVICE_TABLE(of, hisi_thermal_id_table); + +static struct platform_driver hisi_thermal_driver = { + .probe = hisi_thermal_probe, + .remove = hisi_thermal_exit, + .driver = { + .name = "hisi_tsensor", + .of_match_table = hisi_thermal_id_table, + }, +}; + +module_platform_driver(hisi_thermal_driver); + +MODULE_AUTHOR("Tao Wang "); +MODULE_AUTHOR("Leo Yan "); +MODULE_DESCRIPTION("hisi tsensor driver"); +MODULE_LICENSE("GPL v2");