This includes driver for Northstar and for Raspberry Pi.
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
--- /dev/null
+From e498b4984db82b4ba3ceea7dba813222a31e9c2e Mon Sep 17 00:00:00 2001
+From: Laxman Dewangan <ldewangan@nvidia.com>
+Date: Wed, 9 Mar 2016 18:40:06 +0530
+Subject: [PATCH] thermal: of-thermal: Add devm version of
+ thermal_zone_of_sensor_register
+
+Add resource managed version of thermal_zone_of_sensor_register() and
+thermal_zone_of_sensor_unregister().
+
+This helps in reducing the code size in error path, remove of
+driver remove callbacks and making proper sequence for deallocations.
+
+Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+---
+ drivers/thermal/of-thermal.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
+ include/linux/thermal.h | 18 ++++++++++
+ 2 files changed, 99 insertions(+)
+
+--- a/drivers/thermal/of-thermal.c
++++ b/drivers/thermal/of-thermal.c
+@@ -559,6 +559,87 @@ void thermal_zone_of_sensor_unregister(s
+ }
+ EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
+
++static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
++{
++ thermal_zone_of_sensor_unregister(dev,
++ *(struct thermal_zone_device **)res);
++}
++
++static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
++ void *data)
++{
++ struct thermal_zone_device **r = res;
++
++ if (WARN_ON(!r || !*r))
++ return 0;
++
++ return *r == data;
++}
++
++/**
++ * devm_thermal_zone_of_sensor_register - Resource managed version of
++ * thermal_zone_of_sensor_register()
++ * @dev: a valid struct device pointer of a sensor device. Must contain
++ * a valid .of_node, for the sensor node.
++ * @sensor_id: a sensor identifier, in case the sensor IP has more
++ * than one sensors
++ * @data: a private pointer (owned by the caller) that will be passed
++ * back, when a temperature reading is needed.
++ * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
++ *
++ * Refer thermal_zone_of_sensor_register() for more details.
++ *
++ * Return: On success returns a valid struct thermal_zone_device,
++ * otherwise, it returns a corresponding ERR_PTR(). Caller must
++ * check the return value with help of IS_ERR() helper.
++ * Registered hermal_zone_device device will automatically be
++ * released when device is unbounded.
++ */
++struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
++ struct device *dev, int sensor_id,
++ void *data, const struct thermal_zone_of_device_ops *ops)
++{
++ struct thermal_zone_device **ptr, *tzd;
++
++ ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
++ GFP_KERNEL);
++ if (!ptr)
++ return ERR_PTR(-ENOMEM);
++
++ tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
++ if (IS_ERR(tzd)) {
++ devres_free(ptr);
++ return tzd;
++ }
++
++ *ptr = tzd;
++ devres_add(dev, ptr);
++
++ return tzd;
++}
++EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
++
++/**
++ * devm_thermal_zone_of_sensor_unregister - Resource managed version of
++ * thermal_zone_of_sensor_unregister().
++ * @dev: Device for which which resource was allocated.
++ * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
++ *
++ * This function removes the sensor callbacks and private data from the
++ * thermal zone device registered with devm_thermal_zone_of_sensor_register()
++ * API. It will also silent the zone by remove the .get_temp() and .get_trend()
++ * thermal zone device callbacks.
++ * Normally this function will not need to be called and the resource
++ * management code will ensure that the resource is freed.
++ */
++void devm_thermal_zone_of_sensor_unregister(struct device *dev,
++ struct thermal_zone_device *tzd)
++{
++ WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
++ devm_thermal_zone_of_sensor_match, tzd));
++}
++EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
++
+ /*** functions parsing device tree nodes ***/
+
+ /**
+--- a/include/linux/thermal.h
++++ b/include/linux/thermal.h
+@@ -364,6 +364,11 @@ thermal_zone_of_sensor_register(struct d
+ const struct thermal_zone_of_device_ops *ops);
+ void thermal_zone_of_sensor_unregister(struct device *dev,
+ struct thermal_zone_device *tz);
++struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
++ struct device *dev, int id, void *data,
++ const struct thermal_zone_of_device_ops *ops);
++void devm_thermal_zone_of_sensor_unregister(struct device *dev,
++ struct thermal_zone_device *tz);
+ #else
+ static inline struct thermal_zone_device *
+ thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
+@@ -378,6 +383,19 @@ void thermal_zone_of_sensor_unregister(s
+ {
+ }
+
++static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
++ struct device *dev, int id, void *data,
++ const struct thermal_zone_of_device_ops *ops)
++{
++ return ERR_PTR(-ENODEV);
++}
++
++static inline
++void devm_thermal_zone_of_sensor_unregister(struct device *dev,
++ struct thermal_zone_device *tz)
++{
++}
++
+ #endif
+
+ #if IS_ENABLED(CONFIG_THERMAL)
--- /dev/null
+From 4a7069a32c99a81950de035535b0a064dcceaeba Mon Sep 17 00:00:00 2001
+From: Rajendra Nayak <rnayak@codeaurora.org>
+Date: Thu, 5 May 2016 14:21:42 +0530
+Subject: [PATCH] thermal: core: export apis to get slope and offset
+
+Add apis for platform thermal drivers to query for slope and offset
+attributes, which might be needed for temperature calculations.
+
+Signed-off-by: Rajendra Nayak <rnayak@codeaurora.org>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+Signed-off-by: Zhang Rui <rui.zhang@intel.com>
+---
+ Documentation/thermal/sysfs-api.txt | 12 ++++++++++++
+ drivers/thermal/thermal_core.c | 30 ++++++++++++++++++++++++++++++
+ include/linux/thermal.h | 8 ++++++++
+ 3 files changed, 50 insertions(+)
+
+--- a/Documentation/thermal/sysfs-api.txt
++++ b/Documentation/thermal/sysfs-api.txt
+@@ -72,6 +72,18 @@ temperature) and throttle appropriate de
+ It deletes the corresponding entry form /sys/class/thermal folder and
+ unbind all the thermal cooling devices it uses.
+
++1.1.7 int thermal_zone_get_slope(struct thermal_zone_device *tz)
++
++ This interface is used to read the slope attribute value
++ for the thermal zone device, which might be useful for platform
++ drivers for temperature calculations.
++
++1.1.8 int thermal_zone_get_offset(struct thermal_zone_device *tz)
++
++ This interface is used to read the offset attribute value
++ for the thermal zone device, which might be useful for platform
++ drivers for temperature calculations.
++
+ 1.2 thermal cooling device interface
+ 1.2.1 struct thermal_cooling_device *thermal_cooling_device_register(char *name,
+ void *devdata, struct thermal_cooling_device_ops *)
+--- a/drivers/thermal/thermal_core.c
++++ b/drivers/thermal/thermal_core.c
+@@ -2061,6 +2061,36 @@ exit:
+ }
+ EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
+
++/**
++ * thermal_zone_get_slope - return the slope attribute of the thermal zone
++ * @tz: thermal zone device with the slope attribute
++ *
++ * Return: If the thermal zone device has a slope attribute, return it, else
++ * return 1.
++ */
++int thermal_zone_get_slope(struct thermal_zone_device *tz)
++{
++ if (tz && tz->tzp)
++ return tz->tzp->slope;
++ return 1;
++}
++EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
++
++/**
++ * thermal_zone_get_offset - return the offset attribute of the thermal zone
++ * @tz: thermal zone device with the offset attribute
++ *
++ * Return: If the thermal zone device has a offset attribute, return it, else
++ * return 0.
++ */
++int thermal_zone_get_offset(struct thermal_zone_device *tz)
++{
++ if (tz && tz->tzp)
++ return tz->tzp->offset;
++ return 0;
++}
++EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
++
+ #ifdef CONFIG_NET
+ static const struct genl_multicast_group thermal_event_mcgrps[] = {
+ { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
+--- a/include/linux/thermal.h
++++ b/include/linux/thermal.h
+@@ -432,6 +432,8 @@ thermal_of_cooling_device_register(struc
+ void thermal_cooling_device_unregister(struct thermal_cooling_device *);
+ struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
+ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
++int thermal_zone_get_slope(struct thermal_zone_device *tz);
++int thermal_zone_get_offset(struct thermal_zone_device *tz);
+
+ int get_tz_trend(struct thermal_zone_device *, int);
+ struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
+@@ -489,6 +491,12 @@ static inline struct thermal_zone_device
+ static inline int thermal_zone_get_temp(
+ struct thermal_zone_device *tz, int *temp)
+ { return -ENODEV; }
++static inline int thermal_zone_get_slope(
++ struct thermal_zone_device *tz)
++{ return -ENODEV; }
++static inline int thermal_zone_get_offset(
++ struct thermal_zone_device *tz)
++{ return -ENODEV; }
+ static inline int get_tz_trend(struct thermal_zone_device *tz, int trip)
+ { return -ENODEV; }
+ static inline struct thermal_instance *
--- /dev/null
+From bcb7dd9ef206f7d646ed8dac6fe7772083714253 Mon Sep 17 00:00:00 2001
+From: Stefan Wahren <stefan.wahren@i2se.com>
+Date: Fri, 31 Mar 2017 20:03:06 +0000
+Subject: [PATCH] thermal: bcm2835: add thermal driver for bcm2835 SoC
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Add basic thermal driver for bcm2835 SoC.
+
+This driver currently make sure that tsense HW block is set up
+correctly.
+
+Tested-by: Rafał Miłecki <rafal@milecki.pl>
+Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
+Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
+Acked-by: Eric Anholt <eric@anholt.net>
+Acked-by: Eduardo Valentin <edubezval@gmail.com>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+---
+ drivers/thermal/Kconfig | 8 +
+ drivers/thermal/Makefile | 1 +
+ drivers/thermal/bcm2835_thermal.c | 314 ++++++++++++++++++++++++++++++++++++++
+ 3 files changed, 323 insertions(+)
+ create mode 100644 drivers/thermal/bcm2835_thermal.c
+
+--- a/drivers/thermal/Kconfig
++++ b/drivers/thermal/Kconfig
+@@ -391,4 +391,12 @@ config QCOM_SPMI_TEMP_ALARM
+ real time die temperature if an ADC is present or an estimate of the
+ temperature based upon the over temperature stage value.
+
++config BCM2835_THERMAL
++ tristate "Thermal sensors on bcm2835 SoC"
++ depends on ARCH_BCM2835 || COMPILE_TEST
++ depends on HAS_IOMEM
++ depends on THERMAL_OF
++ help
++ Support for thermal sensors on Broadcom bcm2835 SoCs.
++
+ endif
+--- a/drivers/thermal/Makefile
++++ b/drivers/thermal/Makefile
+@@ -48,3 +48,4 @@ obj-$(CONFIG_INTEL_PCH_THERMAL) += intel
+ obj-$(CONFIG_ST_THERMAL) += st/
+ obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o
+ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
++obj-$(CONFIG_BCM2835_THERMAL) += bcm2835_thermal.o
+--- /dev/null
++++ b/drivers/thermal/bcm2835_thermal.c
+@@ -0,0 +1,314 @@
++/*
++ * Driver for Broadcom BCM2835 SoC temperature sensor
++ *
++ * Copyright (C) 2016 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 <linux/clk.h>
++#include <linux/debugfs.h>
++#include <linux/device.h>
++#include <linux/err.h>
++#include <linux/io.h>
++#include <linux/kernel.h>
++#include <linux/module.h>
++#include <linux/of.h>
++#include <linux/of_address.h>
++#include <linux/of_device.h>
++#include <linux/platform_device.h>
++#include <linux/thermal.h>
++
++#define BCM2835_TS_TSENSCTL 0x00
++#define BCM2835_TS_TSENSSTAT 0x04
++
++#define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
++#define BCM2835_TS_TSENSCTL_RSTB BIT(1)
++
++/*
++ * bandgap reference voltage in 6 mV increments
++ * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
++ */
++#define BCM2835_TS_TSENSCTL_CTRL_BITS 3
++#define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
++#define BCM2835_TS_TSENSCTL_CTRL_MASK \
++ GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT)
++#define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
++#define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
++#define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
++#define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
++#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)
++/*
++ * time how long the block to be asserted in reset
++ * which based on a clock counter (TSENS clock assumed)
++ */
++#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)
++
++struct bcm2835_thermal_data {
++ struct thermal_zone_device *tz;
++ void __iomem *regs;
++ struct clk *clk;
++ struct dentry *debugfsdir;
++};
++
++static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
++{
++ return offset + slope * adc;
++}
++
++static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
++{
++ temp -= offset;
++ temp /= slope;
++
++ if (temp < 0)
++ temp = 0;
++ if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
++ temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
++
++ return temp;
++}
++
++static int bcm2835_thermal_get_temp(void *d, int *temp)
++{
++ struct bcm2835_thermal_data *data = d;
++ u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
++
++ if (!(val & BCM2835_TS_TSENSSTAT_VALID))
++ return -EIO;
++
++ val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
++
++ *temp = bcm2835_thermal_adc2temp(
++ val,
++ thermal_zone_get_offset(data->tz),
++ thermal_zone_get_slope(data->tz));
++
++ 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", 0444, data->debugfsdir, regset);
++}
++
++static struct thermal_zone_of_device_ops bcm2835_thermal_ops = {
++ .get_temp = bcm2835_thermal_get_temp,
++};
++
++/*
++ * Note: as per Raspberry Foundation FAQ
++ * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
++ * the recommended temperature range for the SoC -40C to +85C
++ * so the trip limit is set to 80C.
++ * this applies to all the BCM283X SoC
++ */
++
++static const struct of_device_id bcm2835_thermal_of_match_table[] = {
++ {
++ .compatible = "brcm,bcm2835-thermal",
++ },
++ {
++ .compatible = "brcm,bcm2836-thermal",
++ },
++ {
++ .compatible = "brcm,bcm2837-thermal",
++ },
++ {},
++};
++MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
++
++static int bcm2835_thermal_probe(struct platform_device *pdev)
++{
++ const struct of_device_id *match;
++ struct thermal_zone_device *tz;
++ struct bcm2835_thermal_data *data;
++ struct resource *res;
++ int err = 0;
++ u32 val;
++ unsigned long rate;
++
++ data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
++ if (!data)
++ return -ENOMEM;
++
++ match = of_match_device(bcm2835_thermal_of_match_table,
++ &pdev->dev);
++ if (!match)
++ return -EINVAL;
++
++ 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;
++ }
++
++ 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;
++ }
++
++ err = clk_prepare_enable(data->clk);
++ if (err)
++ return err;
++
++ rate = clk_get_rate(data->clk);
++ if ((rate < 1920000) || (rate > 5000000))
++ dev_warn(&pdev->dev,
++ "Clock %pCn running at %pCr Hz is outside of the recommended range: 1.92 to 5MHz\n",
++ data->clk, data->clk);
++
++ /* register of thermal sensor and get info from DT */
++ tz = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
++ &bcm2835_thermal_ops);
++ if (IS_ERR(tz)) {
++ err = PTR_ERR(tz);
++ dev_err(&pdev->dev,
++ "Failed to register the thermal device: %d\n",
++ err);
++ goto err_clk;
++ }
++
++ /*
++ * right now the FW does set up the HW-block, so we are not
++ * touching the configuration registers.
++ * But if the HW is not enabled, then set it up
++ * using "sane" values used by the firmware right now.
++ */
++ val = readl(data->regs + BCM2835_TS_TSENSCTL);
++ if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
++ int trip_temp, offset, slope;
++
++ slope = thermal_zone_get_slope(tz);
++ offset = thermal_zone_get_offset(tz);
++ /*
++ * For now we deal only with critical, otherwise
++ * would need to iterate
++ */
++ err = tz->ops->get_trip_temp(tz, 0, &trip_temp);
++ if (err < 0) {
++ err = PTR_ERR(tz);
++ dev_err(&pdev->dev,
++ "Not able to read trip_temp: %d\n",
++ err);
++ goto err_tz;
++ }
++
++ /* set bandgap reference voltage and enable voltage regulator */
++ val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
++ BCM2835_TS_TSENSCTL_REGULEN;
++
++ /* use the recommended reset duration */
++ val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
++
++ /* trip_adc value from info */
++ val |= bcm2835_thermal_temp2adc(trip_temp,
++ offset,
++ slope)
++ << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
++
++ /* write the value back to the register as 2 steps */
++ writel(val, data->regs + BCM2835_TS_TSENSCTL);
++ val |= BCM2835_TS_TSENSCTL_RSTB;
++ writel(val, data->regs + BCM2835_TS_TSENSCTL);
++ }
++
++ data->tz = tz;
++
++ platform_set_drvdata(pdev, tz);
++
++ bcm2835_thermal_debugfs(pdev);
++
++ return 0;
++err_tz:
++ thermal_zone_of_sensor_unregister(&pdev->dev, tz);
++err_clk:
++ clk_disable_unprepare(data->clk);
++
++ return err;
++}
++
++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_of_sensor_unregister(&pdev->dev, tz);
++ clk_disable_unprepare(data->clk);
++
++ return 0;
++}
++
++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");
--- /dev/null
+From a94cb7eeecc4104a6874339f90c5d0647359c102 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
+Date: Mon, 3 Apr 2017 17:48:29 +0200
+Subject: [PATCH] thermal: broadcom: add Northstar thermal driver
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Northstar is a SoC family commonly used in home routers. This commit
+adds a driver for checking CPU temperature. As Northstar Plus seems to
+also have this IP block this new symbol gets ARCH_BCM_IPROC dependency.
+
+Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
+Signed-off-by: Jon Mason <jon.mason@broadcom.com>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+---
+ drivers/thermal/Kconfig | 5 ++
+ drivers/thermal/Makefile | 1 +
+ drivers/thermal/broadcom/Kconfig | 8 +++
+ drivers/thermal/broadcom/Makefile | 1 +
+ drivers/thermal/broadcom/ns-thermal.c | 105 ++++++++++++++++++++++++++++++++++
+ 5 files changed, 120 insertions(+)
+ create mode 100644 drivers/thermal/broadcom/Kconfig
+ create mode 100644 drivers/thermal/broadcom/Makefile
+ create mode 100644 drivers/thermal/broadcom/ns-thermal.c
+
+--- a/drivers/thermal/Kconfig
++++ b/drivers/thermal/Kconfig
+@@ -365,6 +365,11 @@ config INTEL_PCH_THERMAL
+ Thermal reporting device will provide temperature reading,
+ programmable trip points and other information.
+
++menu "Broadcom thermal drivers"
++depends on ARCH_BCM || COMPILE_TEST
++source "drivers/thermal/broadcom/Kconfig"
++endmenu
++
+ menu "Texas Instruments thermal drivers"
+ depends on ARCH_HAS_BANDGAP || COMPILE_TEST
+ source "drivers/thermal/ti-soc-thermal/Kconfig"
+--- a/drivers/thermal/Makefile
++++ b/drivers/thermal/Makefile
+@@ -26,6 +26,7 @@ thermal_sys-$(CONFIG_CLOCK_THERMAL) += c
+ thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
+
+ # platform thermal drivers
++obj-y += broadcom/
+ obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM) += qcom-spmi-temp-alarm.o
+ obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
+ obj-$(CONFIG_ROCKCHIP_THERMAL) += rockchip_thermal.o
+--- /dev/null
++++ b/drivers/thermal/broadcom/Kconfig
+@@ -0,0 +1,8 @@
++config BCM_NS_THERMAL
++ tristate "Northstar thermal driver"
++ depends on ARCH_BCM_IPROC || COMPILE_TEST
++ help
++ Northstar is a family of SoCs that includes e.g. BCM4708, BCM47081,
++ BCM4709 and BCM47094. It contains DMU (Device Management Unit) block
++ with a thermal sensor that allows checking CPU temperature. This
++ driver provides support for it.
+--- /dev/null
++++ b/drivers/thermal/broadcom/Makefile
+@@ -0,0 +1 @@
++obj-$(CONFIG_BCM_NS_THERMAL) += ns-thermal.o
+--- /dev/null
++++ b/drivers/thermal/broadcom/ns-thermal.c
+@@ -0,0 +1,105 @@
++/*
++ * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License version 2 as
++ * published by the Free Software Foundation.
++ */
++
++#include <linux/module.h>
++#include <linux/of_address.h>
++#include <linux/platform_device.h>
++#include <linux/thermal.h>
++
++#define PVTMON_CONTROL0 0x00
++#define PVTMON_CONTROL0_SEL_MASK 0x0000000e
++#define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
++#define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
++#define PVTMON_STATUS 0x08
++
++struct ns_thermal {
++ struct thermal_zone_device *tz;
++ void __iomem *pvtmon;
++};
++
++static int ns_thermal_get_temp(void *data, int *temp)
++{
++ struct ns_thermal *ns_thermal = data;
++ int offset = thermal_zone_get_offset(ns_thermal->tz);
++ int slope = thermal_zone_get_slope(ns_thermal->tz);
++ u32 val;
++
++ val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
++ if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
++ /* Clear current mode selection */
++ val &= ~PVTMON_CONTROL0_SEL_MASK;
++
++ /* Set temp monitor mode (it's the default actually) */
++ val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
++
++ writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
++ }
++
++ val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
++ *temp = slope * val + offset;
++
++ return 0;
++}
++
++static const struct thermal_zone_of_device_ops ns_thermal_ops = {
++ .get_temp = ns_thermal_get_temp,
++};
++
++static int ns_thermal_probe(struct platform_device *pdev)
++{
++ struct device *dev = &pdev->dev;
++ struct ns_thermal *ns_thermal;
++
++ ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
++ if (!ns_thermal)
++ return -ENOMEM;
++
++ ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
++ if (WARN_ON(!ns_thermal->pvtmon))
++ return -ENOENT;
++
++ ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
++ ns_thermal,
++ &ns_thermal_ops);
++ if (IS_ERR(ns_thermal->tz)) {
++ iounmap(ns_thermal->pvtmon);
++ return PTR_ERR(ns_thermal->tz);
++ }
++
++ platform_set_drvdata(pdev, ns_thermal);
++
++ return 0;
++}
++
++static int ns_thermal_remove(struct platform_device *pdev)
++{
++ struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
++
++ iounmap(ns_thermal->pvtmon);
++
++ return 0;
++}
++
++static const struct of_device_id ns_thermal_of_match[] = {
++ { .compatible = "brcm,ns-thermal", },
++ {},
++};
++MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
++
++static struct platform_driver ns_thermal_driver = {
++ .probe = ns_thermal_probe,
++ .remove = ns_thermal_remove,
++ .driver = {
++ .name = "ns-thermal",
++ .of_match_table = ns_thermal_of_match,
++ },
++};
++module_platform_driver(ns_thermal_driver);
++
++MODULE_DESCRIPTION("Northstar thermal driver");
++MODULE_LICENSE("GPL v2");
--- /dev/null
+From d44264c8735f79da3253520024841311c555ca31 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
+Date: Fri, 14 Apr 2017 22:25:12 +0200
+Subject: [PATCH] thermal: broadcom: fix compilation of Northstar driver
+
+---
+ drivers/thermal/broadcom/ns-thermal.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+--- a/drivers/thermal/broadcom/ns-thermal.c
++++ b/drivers/thermal/broadcom/ns-thermal.c
+@@ -6,6 +6,7 @@
+ * published by the Free Software Foundation.
+ */
+
++#include <asm/io.h>
+ #include <linux/module.h>
+ #include <linux/of_address.h>
+ #include <linux/platform_device.h>
--- /dev/null
+From bcb7dd9ef206f7d646ed8dac6fe7772083714253 Mon Sep 17 00:00:00 2001
+From: Stefan Wahren <stefan.wahren@i2se.com>
+Date: Fri, 31 Mar 2017 20:03:06 +0000
+Subject: [PATCH] thermal: bcm2835: add thermal driver for bcm2835 SoC
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Add basic thermal driver for bcm2835 SoC.
+
+This driver currently make sure that tsense HW block is set up
+correctly.
+
+Tested-by: Rafał Miłecki <rafal@milecki.pl>
+Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
+Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
+Acked-by: Eric Anholt <eric@anholt.net>
+Acked-by: Eduardo Valentin <edubezval@gmail.com>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+---
+ drivers/thermal/Kconfig | 8 +
+ drivers/thermal/Makefile | 1 +
+ drivers/thermal/bcm2835_thermal.c | 314 ++++++++++++++++++++++++++++++++++++++
+ 3 files changed, 323 insertions(+)
+ create mode 100644 drivers/thermal/bcm2835_thermal.c
+
+--- a/drivers/thermal/Kconfig
++++ b/drivers/thermal/Kconfig
+@@ -434,4 +434,12 @@ depends on (ARCH_QCOM && OF) || COMPILE_
+ source "drivers/thermal/qcom/Kconfig"
+ endmenu
+
++config BCM2835_THERMAL
++ tristate "Thermal sensors on bcm2835 SoC"
++ depends on ARCH_BCM2835 || COMPILE_TEST
++ depends on HAS_IOMEM
++ depends on THERMAL_OF
++ help
++ Support for thermal sensors on Broadcom bcm2835 SoCs.
++
+ endif
+--- a/drivers/thermal/Makefile
++++ b/drivers/thermal/Makefile
+@@ -55,3 +55,4 @@ obj-$(CONFIG_TEGRA_SOCTHERM) += tegra/
+ obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
+ obj-$(CONFIG_MTK_THERMAL) += mtk_thermal.o
+ obj-$(CONFIG_GENERIC_ADC_THERMAL) += thermal-generic-adc.o
++obj-$(CONFIG_BCM2835_THERMAL) += bcm2835_thermal.o
+--- /dev/null
++++ b/drivers/thermal/bcm2835_thermal.c
+@@ -0,0 +1,314 @@
++/*
++ * Driver for Broadcom BCM2835 SoC temperature sensor
++ *
++ * Copyright (C) 2016 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 <linux/clk.h>
++#include <linux/debugfs.h>
++#include <linux/device.h>
++#include <linux/err.h>
++#include <linux/io.h>
++#include <linux/kernel.h>
++#include <linux/module.h>
++#include <linux/of.h>
++#include <linux/of_address.h>
++#include <linux/of_device.h>
++#include <linux/platform_device.h>
++#include <linux/thermal.h>
++
++#define BCM2835_TS_TSENSCTL 0x00
++#define BCM2835_TS_TSENSSTAT 0x04
++
++#define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
++#define BCM2835_TS_TSENSCTL_RSTB BIT(1)
++
++/*
++ * bandgap reference voltage in 6 mV increments
++ * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
++ */
++#define BCM2835_TS_TSENSCTL_CTRL_BITS 3
++#define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
++#define BCM2835_TS_TSENSCTL_CTRL_MASK \
++ GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT)
++#define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
++#define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
++#define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
++#define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
++#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)
++/*
++ * time how long the block to be asserted in reset
++ * which based on a clock counter (TSENS clock assumed)
++ */
++#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)
++
++struct bcm2835_thermal_data {
++ struct thermal_zone_device *tz;
++ void __iomem *regs;
++ struct clk *clk;
++ struct dentry *debugfsdir;
++};
++
++static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
++{
++ return offset + slope * adc;
++}
++
++static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
++{
++ temp -= offset;
++ temp /= slope;
++
++ if (temp < 0)
++ temp = 0;
++ if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
++ temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
++
++ return temp;
++}
++
++static int bcm2835_thermal_get_temp(void *d, int *temp)
++{
++ struct bcm2835_thermal_data *data = d;
++ u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
++
++ if (!(val & BCM2835_TS_TSENSSTAT_VALID))
++ return -EIO;
++
++ val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
++
++ *temp = bcm2835_thermal_adc2temp(
++ val,
++ thermal_zone_get_offset(data->tz),
++ thermal_zone_get_slope(data->tz));
++
++ 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", 0444, data->debugfsdir, regset);
++}
++
++static struct thermal_zone_of_device_ops bcm2835_thermal_ops = {
++ .get_temp = bcm2835_thermal_get_temp,
++};
++
++/*
++ * Note: as per Raspberry Foundation FAQ
++ * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
++ * the recommended temperature range for the SoC -40C to +85C
++ * so the trip limit is set to 80C.
++ * this applies to all the BCM283X SoC
++ */
++
++static const struct of_device_id bcm2835_thermal_of_match_table[] = {
++ {
++ .compatible = "brcm,bcm2835-thermal",
++ },
++ {
++ .compatible = "brcm,bcm2836-thermal",
++ },
++ {
++ .compatible = "brcm,bcm2837-thermal",
++ },
++ {},
++};
++MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
++
++static int bcm2835_thermal_probe(struct platform_device *pdev)
++{
++ const struct of_device_id *match;
++ struct thermal_zone_device *tz;
++ struct bcm2835_thermal_data *data;
++ struct resource *res;
++ int err = 0;
++ u32 val;
++ unsigned long rate;
++
++ data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
++ if (!data)
++ return -ENOMEM;
++
++ match = of_match_device(bcm2835_thermal_of_match_table,
++ &pdev->dev);
++ if (!match)
++ return -EINVAL;
++
++ 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;
++ }
++
++ 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;
++ }
++
++ err = clk_prepare_enable(data->clk);
++ if (err)
++ return err;
++
++ rate = clk_get_rate(data->clk);
++ if ((rate < 1920000) || (rate > 5000000))
++ dev_warn(&pdev->dev,
++ "Clock %pCn running at %pCr Hz is outside of the recommended range: 1.92 to 5MHz\n",
++ data->clk, data->clk);
++
++ /* register of thermal sensor and get info from DT */
++ tz = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
++ &bcm2835_thermal_ops);
++ if (IS_ERR(tz)) {
++ err = PTR_ERR(tz);
++ dev_err(&pdev->dev,
++ "Failed to register the thermal device: %d\n",
++ err);
++ goto err_clk;
++ }
++
++ /*
++ * right now the FW does set up the HW-block, so we are not
++ * touching the configuration registers.
++ * But if the HW is not enabled, then set it up
++ * using "sane" values used by the firmware right now.
++ */
++ val = readl(data->regs + BCM2835_TS_TSENSCTL);
++ if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
++ int trip_temp, offset, slope;
++
++ slope = thermal_zone_get_slope(tz);
++ offset = thermal_zone_get_offset(tz);
++ /*
++ * For now we deal only with critical, otherwise
++ * would need to iterate
++ */
++ err = tz->ops->get_trip_temp(tz, 0, &trip_temp);
++ if (err < 0) {
++ err = PTR_ERR(tz);
++ dev_err(&pdev->dev,
++ "Not able to read trip_temp: %d\n",
++ err);
++ goto err_tz;
++ }
++
++ /* set bandgap reference voltage and enable voltage regulator */
++ val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
++ BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
++ BCM2835_TS_TSENSCTL_REGULEN;
++
++ /* use the recommended reset duration */
++ val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
++
++ /* trip_adc value from info */
++ val |= bcm2835_thermal_temp2adc(trip_temp,
++ offset,
++ slope)
++ << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
++
++ /* write the value back to the register as 2 steps */
++ writel(val, data->regs + BCM2835_TS_TSENSCTL);
++ val |= BCM2835_TS_TSENSCTL_RSTB;
++ writel(val, data->regs + BCM2835_TS_TSENSCTL);
++ }
++
++ data->tz = tz;
++
++ platform_set_drvdata(pdev, tz);
++
++ bcm2835_thermal_debugfs(pdev);
++
++ return 0;
++err_tz:
++ thermal_zone_of_sensor_unregister(&pdev->dev, tz);
++err_clk:
++ clk_disable_unprepare(data->clk);
++
++ return err;
++}
++
++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_of_sensor_unregister(&pdev->dev, tz);
++ clk_disable_unprepare(data->clk);
++
++ return 0;
++}
++
++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");
--- /dev/null
+From a94cb7eeecc4104a6874339f90c5d0647359c102 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
+Date: Mon, 3 Apr 2017 17:48:29 +0200
+Subject: [PATCH] thermal: broadcom: add Northstar thermal driver
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Northstar is a SoC family commonly used in home routers. This commit
+adds a driver for checking CPU temperature. As Northstar Plus seems to
+also have this IP block this new symbol gets ARCH_BCM_IPROC dependency.
+
+Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
+Signed-off-by: Jon Mason <jon.mason@broadcom.com>
+Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
+---
+ drivers/thermal/Kconfig | 5 ++
+ drivers/thermal/Makefile | 1 +
+ drivers/thermal/broadcom/Kconfig | 8 +++
+ drivers/thermal/broadcom/Makefile | 1 +
+ drivers/thermal/broadcom/ns-thermal.c | 105 ++++++++++++++++++++++++++++++++++
+ 5 files changed, 120 insertions(+)
+ create mode 100644 drivers/thermal/broadcom/Kconfig
+ create mode 100644 drivers/thermal/broadcom/Makefile
+ create mode 100644 drivers/thermal/broadcom/ns-thermal.c
+
+--- 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/broadcom/Kconfig"
++endmenu
++
+ menu "Texas Instruments thermal drivers"
+ depends on ARCH_HAS_BANDGAP || COMPILE_TEST
+ depends on HAS_IOMEM
+--- a/drivers/thermal/Makefile
++++ b/drivers/thermal/Makefile
+@@ -26,6 +26,7 @@ thermal_sys-$(CONFIG_CLOCK_THERMAL) += c
+ thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
+
+ # platform thermal drivers
++obj-y += broadcom/
+ obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM) += qcom-spmi-temp-alarm.o
+ obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
+ obj-$(CONFIG_ROCKCHIP_THERMAL) += rockchip_thermal.o
+--- /dev/null
++++ b/drivers/thermal/broadcom/Kconfig
+@@ -0,0 +1,8 @@
++config BCM_NS_THERMAL
++ tristate "Northstar thermal driver"
++ depends on ARCH_BCM_IPROC || COMPILE_TEST
++ help
++ Northstar is a family of SoCs that includes e.g. BCM4708, BCM47081,
++ BCM4709 and BCM47094. It contains DMU (Device Management Unit) block
++ with a thermal sensor that allows checking CPU temperature. This
++ driver provides support for it.
+--- /dev/null
++++ b/drivers/thermal/broadcom/Makefile
+@@ -0,0 +1 @@
++obj-$(CONFIG_BCM_NS_THERMAL) += ns-thermal.o
+--- /dev/null
++++ b/drivers/thermal/broadcom/ns-thermal.c
+@@ -0,0 +1,105 @@
++/*
++ * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License version 2 as
++ * published by the Free Software Foundation.
++ */
++
++#include <linux/module.h>
++#include <linux/of_address.h>
++#include <linux/platform_device.h>
++#include <linux/thermal.h>
++
++#define PVTMON_CONTROL0 0x00
++#define PVTMON_CONTROL0_SEL_MASK 0x0000000e
++#define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
++#define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
++#define PVTMON_STATUS 0x08
++
++struct ns_thermal {
++ struct thermal_zone_device *tz;
++ void __iomem *pvtmon;
++};
++
++static int ns_thermal_get_temp(void *data, int *temp)
++{
++ struct ns_thermal *ns_thermal = data;
++ int offset = thermal_zone_get_offset(ns_thermal->tz);
++ int slope = thermal_zone_get_slope(ns_thermal->tz);
++ u32 val;
++
++ val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
++ if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
++ /* Clear current mode selection */
++ val &= ~PVTMON_CONTROL0_SEL_MASK;
++
++ /* Set temp monitor mode (it's the default actually) */
++ val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
++
++ writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
++ }
++
++ val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
++ *temp = slope * val + offset;
++
++ return 0;
++}
++
++static const struct thermal_zone_of_device_ops ns_thermal_ops = {
++ .get_temp = ns_thermal_get_temp,
++};
++
++static int ns_thermal_probe(struct platform_device *pdev)
++{
++ struct device *dev = &pdev->dev;
++ struct ns_thermal *ns_thermal;
++
++ ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
++ if (!ns_thermal)
++ return -ENOMEM;
++
++ ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
++ if (WARN_ON(!ns_thermal->pvtmon))
++ return -ENOENT;
++
++ ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
++ ns_thermal,
++ &ns_thermal_ops);
++ if (IS_ERR(ns_thermal->tz)) {
++ iounmap(ns_thermal->pvtmon);
++ return PTR_ERR(ns_thermal->tz);
++ }
++
++ platform_set_drvdata(pdev, ns_thermal);
++
++ return 0;
++}
++
++static int ns_thermal_remove(struct platform_device *pdev)
++{
++ struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
++
++ iounmap(ns_thermal->pvtmon);
++
++ return 0;
++}
++
++static const struct of_device_id ns_thermal_of_match[] = {
++ { .compatible = "brcm,ns-thermal", },
++ {},
++};
++MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
++
++static struct platform_driver ns_thermal_driver = {
++ .probe = ns_thermal_probe,
++ .remove = ns_thermal_remove,
++ .driver = {
++ .name = "ns-thermal",
++ .of_match_table = ns_thermal_of_match,
++ },
++};
++module_platform_driver(ns_thermal_driver);
++
++MODULE_DESCRIPTION("Northstar thermal driver");
++MODULE_LICENSE("GPL v2");