From patchwork Thu May 12 14:35:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 9082051 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EB459BF29F for ; Thu, 12 May 2016 14:37:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C41ED2024C for ; Thu, 12 May 2016 14:37:56 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A887A20220 for ; Thu, 12 May 2016 14:37:55 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b0riz-0002oF-Nf; Thu, 12 May 2016 14:36:25 +0000 Received: from 212-186-180-163.dynamic.surfer.at ([212.186.180.163] helo=cgate.sperl.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b0rij-0002bY-I7; Thu, 12 May 2016 14:36:11 +0000 Received: from rasp3a.intern.sperl.org (account martin@sperl.org [10.10.10.43] verified) by sperl.org (CommuniGate Pro SMTP 6.1.2) with ESMTPSA id 6454366; Thu, 12 May 2016 14:35:43 +0000 From: kernel@martin.sperl.org To: Zhang Rui , Eduardo Valentin , Rob Herring , Pawel Moll , Mark Rutland , Stephen Warren , Lee Jones , Eric Anholt , Russell King , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH 2/4] thermal: bcm2835: add thermal driver for bcm2835 soc Date: Thu, 12 May 2016 14:35:36 +0000 Message-Id: <1463063738-11506-3-git-send-email-kernel@martin.sperl.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1463063738-11506-1-git-send-email-kernel@martin.sperl.org> References: <1463063738-11506-1-git-send-email-kernel@martin.sperl.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160512_073610_180654_D8726F38 X-CRM114-Status: GOOD ( 19.40 ) X-Spam-Score: -0.9 (/) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Martin Sperl MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl Add basic thermal driver for bcm2835 SOC. This driver currently relies on the firmware setting up the tsense HW block and does not set it up itself. Signed-off-by: Martin Sperl --- drivers/thermal/Kconfig | 5 + drivers/thermal/Makefile | 1 + drivers/thermal/bcm/Kconfig | 4 + drivers/thermal/bcm/Makefile | 1 + drivers/thermal/bcm/bcm2835_thermal.c | 205 ++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 drivers/thermal/bcm/Kconfig create mode 100644 drivers/thermal/bcm/Makefile create mode 100644 drivers/thermal/bcm/bcm2835_thermal.c diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index c37eedc..10f92c6 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -381,6 +381,11 @@ config MTK_THERMAL Enable this option if you want to have support for thermal management controller present in Mediatek SoCs +menu "Broadcom thermal drivers" +depends on ARCH_BCM || COMPILE_TEST +source "drivers/thermal/bcm/Kconfig" +endmenu + menu "Texas Instruments thermal drivers" depends on ARCH_HAS_BANDGAP || COMPILE_TEST depends on HAS_IOMEM diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 8e9cbc3..6df1aa3 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -49,3 +49,4 @@ obj-$(CONFIG_ST_THERMAL) += st/ obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o obj-$(CONFIG_MTK_THERMAL) += mtk_thermal.o +obj-y += bcm/ diff --git a/drivers/thermal/bcm/Kconfig b/drivers/thermal/bcm/Kconfig new file mode 100644 index 0000000..62245ae --- /dev/null +++ b/drivers/thermal/bcm/Kconfig @@ -0,0 +1,4 @@ +config BCM2835_THERMAL + tristate "Thermal sensors on bcm2835 SoC" + help + Support for thermal sensors on Broadcom bcm2835 SoCs. diff --git a/drivers/thermal/bcm/Makefile b/drivers/thermal/bcm/Makefile new file mode 100644 index 0000000..13456d2 --- /dev/null +++ b/drivers/thermal/bcm/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_BCM2835_THERMAL) := bcm2835_thermal.o diff --git a/drivers/thermal/bcm/bcm2835_thermal.c b/drivers/thermal/bcm/bcm2835_thermal.c new file mode 100644 index 0000000..a1fdc84 --- /dev/null +++ b/drivers/thermal/bcm/bcm2835_thermal.c @@ -0,0 +1,205 @@ +/* + * Driver for Broadcom BCM2835 soc temperature sensor + * + * Copyright (C) 2015 Martin Sperl + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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 +#include +#include +#include +#include + +#define BCM2835_TS_TSENSCTL 0x00 +#define BCM2835_TS_TSENSSTAT 0x04 + +#define BCM2835_TS_TSENSCTL_PRWDW BIT(0) +#define BCM2835_TS_TSENSCTL_RSTB BIT(1) +#define BCM2835_TS_TSENSCTL_CTRL BIT(0) +#define BCM2835_TS_TSENSCTL_EN_INT BIT(0) +#define BCM2835_TS_TSENSCTL_DIRECT BIT(0) +#define BCM2835_TS_TSENSCTL_CLR_INT BIT(0) +#define BCM2835_TS_TSENSCTL_THOLD_SHIFT 8 +#define BCM2835_TS_TSENSCTL_THOLD_BITS 10 +#define BCM2835_TS_TSENSCTL_THOLD_MASK \ + GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS + \ + BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \ + BCM2835_TS_TSENSCTL_THOLD_SHIFT) +#define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT 18 +#define BCM2835_TS_TSENSCTL_RSTDELAY_BITS 8 +#define BCM2835_TS_TSENSCTL_REGULEN BIT(26) + +#define BCM2835_TS_TSENSSTAT_DATA_BITS 10 +#define BCM2835_TS_TSENSSTAT_DATA_SHIFT 0 +#define BCM2835_TS_TSENSSTAT_DATA_MASK \ + GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS + \ + BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \ + BCM2835_TS_TSENSSTAT_DATA_SHIFT) +#define BCM2835_TS_TSENSSTAT_VALID BIT(10) +#define BCM2835_TS_TSENSSTAT_INTERRUPT BIT(11) + +/* empirical linear approximation for conversion to temperature */ +#define BCM2835_TS_VALUE_OFFSET 407000 +#define BCM2835_TS_VALUE_SLOPE -538 + +struct bcm2835_thermal_data { + void __iomem *regs; + struct clk *clk; + struct dentry *debugfsdir; +}; + +static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz, + int *temp) +{ + struct bcm2835_thermal_data *data = tz->devdata; + u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT); + + /* mask the relevant bits */ + val &= BCM2835_TS_TSENSSTAT_DATA_MASK; + + /* linear approximation */ + *temp = BCM2835_TS_VALUE_OFFSET + + (val * BCM2835_TS_VALUE_SLOPE); + + return 0; +} + +static const struct debugfs_reg32 bcm2835_thermal_regs[] = { + { + .name = "ctl", + .offset = 0 + }, + { + .name = "stat", + .offset = 4 + } +}; + +static void bcm2835_thermal_debugfs(struct platform_device *pdev) +{ + struct thermal_zone_device *tz = platform_get_drvdata(pdev); + struct bcm2835_thermal_data *data = tz->devdata; + struct debugfs_regset32 *regset; + + data->debugfsdir = debugfs_create_dir("bcm2835_thermal", NULL); + if (!data->debugfsdir) + return; + + regset = devm_kzalloc(&pdev->dev, sizeof(*regset), GFP_KERNEL); + if (!regset) + return; + + regset->regs = bcm2835_thermal_regs; + regset->nregs = ARRAY_SIZE(bcm2835_thermal_regs); + regset->base = data->regs; + + debugfs_create_regset32("regset", S_IRUGO, + data->debugfsdir, regset); +} + +static struct thermal_zone_device_ops bcm2835_thermal_ops = { + .get_temp = bcm2835_thermal_get_temp, +}; + +static int bcm2835_thermal_probe(struct platform_device *pdev) +{ + struct thermal_zone_device *tz; + struct bcm2835_thermal_data *data; + struct resource *res; + int err; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + /* get registers */ + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + data->regs = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(data->regs)) { + err = PTR_ERR(data->regs); + dev_err(&pdev->dev, "Could not get registers: %d\n", err); + return err; + } + + /* get clock */ + data->clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(data->clk)) { + err = PTR_ERR(data->clk); + if (err != -EPROBE_DEFER) + dev_err(&pdev->dev, "Could not get clk: %d\n", err); + return err; + } + + /* + * for now we assume the firmware sets up the device, + * so we will not write to ctl, we just prepare the clock + */ + clk_prepare_enable(data->clk); + + /* register it */ + tz = thermal_zone_device_register("bcm2835_thermal", + 0, 0, data, + &bcm2835_thermal_ops, + NULL, 0, 0); + if (IS_ERR(tz)) { + clk_disable_unprepare(data->clk); + err = PTR_ERR(tz); + dev_err(&pdev->dev, + "Failed to register the thermal device: %d\n", + err); + return err; + } + + platform_set_drvdata(pdev, tz); + + bcm2835_thermal_debugfs(pdev); + + return 0; +} + +static int bcm2835_thermal_remove(struct platform_device *pdev) +{ + struct thermal_zone_device *tz = platform_get_drvdata(pdev); + struct bcm2835_thermal_data *data = tz->devdata; + + debugfs_remove_recursive(data->debugfsdir); + thermal_zone_device_unregister(tz); + + return 0; +} + +static const struct of_device_id bcm2835_thermal_of_match_table[] = { + { .compatible = "brcm,bcm2835-thermal", }, + {}, +}; +MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table); + +static struct platform_driver bcm2835_thermal_driver = { + .probe = bcm2835_thermal_probe, + .remove = bcm2835_thermal_remove, + .driver = { + .name = "bcm2835_thermal", + .of_match_table = bcm2835_thermal_of_match_table, + }, +}; +module_platform_driver(bcm2835_thermal_driver); + +MODULE_AUTHOR("Martin Sperl"); +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip"); +MODULE_LICENSE("GPL");