kernel: backport Broadcom thermal drivers
[openwrt/openwrt.git] / target / linux / generic / patches-4.9 / 081-0002-thermal-broadcom-add-Northstar-thermal-driver.patch
1 From a94cb7eeecc4104a6874339f90c5d0647359c102 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Mon, 3 Apr 2017 17:48:29 +0200
4 Subject: [PATCH] thermal: broadcom: add Northstar thermal driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Northstar is a SoC family commonly used in home routers. This commit
10 adds a driver for checking CPU temperature. As Northstar Plus seems to
11 also have this IP block this new symbol gets ARCH_BCM_IPROC dependency.
12
13 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
14 Signed-off-by: Jon Mason <jon.mason@broadcom.com>
15 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
16 ---
17 drivers/thermal/Kconfig | 5 ++
18 drivers/thermal/Makefile | 1 +
19 drivers/thermal/broadcom/Kconfig | 8 +++
20 drivers/thermal/broadcom/Makefile | 1 +
21 drivers/thermal/broadcom/ns-thermal.c | 105 ++++++++++++++++++++++++++++++++++
22 5 files changed, 120 insertions(+)
23 create mode 100644 drivers/thermal/broadcom/Kconfig
24 create mode 100644 drivers/thermal/broadcom/Makefile
25 create mode 100644 drivers/thermal/broadcom/ns-thermal.c
26
27 --- a/drivers/thermal/Kconfig
28 +++ b/drivers/thermal/Kconfig
29 @@ -381,6 +381,11 @@ config MTK_THERMAL
30 Enable this option if you want to have support for thermal management
31 controller present in Mediatek SoCs
32
33 +menu "Broadcom thermal drivers"
34 +depends on ARCH_BCM || COMPILE_TEST
35 +source "drivers/thermal/broadcom/Kconfig"
36 +endmenu
37 +
38 menu "Texas Instruments thermal drivers"
39 depends on ARCH_HAS_BANDGAP || COMPILE_TEST
40 depends on HAS_IOMEM
41 --- a/drivers/thermal/Makefile
42 +++ b/drivers/thermal/Makefile
43 @@ -26,6 +26,7 @@ thermal_sys-$(CONFIG_CLOCK_THERMAL) += c
44 thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
45
46 # platform thermal drivers
47 +obj-y += broadcom/
48 obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM) += qcom-spmi-temp-alarm.o
49 obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
50 obj-$(CONFIG_ROCKCHIP_THERMAL) += rockchip_thermal.o
51 --- /dev/null
52 +++ b/drivers/thermal/broadcom/Kconfig
53 @@ -0,0 +1,8 @@
54 +config BCM_NS_THERMAL
55 + tristate "Northstar thermal driver"
56 + depends on ARCH_BCM_IPROC || COMPILE_TEST
57 + help
58 + Northstar is a family of SoCs that includes e.g. BCM4708, BCM47081,
59 + BCM4709 and BCM47094. It contains DMU (Device Management Unit) block
60 + with a thermal sensor that allows checking CPU temperature. This
61 + driver provides support for it.
62 --- /dev/null
63 +++ b/drivers/thermal/broadcom/Makefile
64 @@ -0,0 +1 @@
65 +obj-$(CONFIG_BCM_NS_THERMAL) += ns-thermal.o
66 --- /dev/null
67 +++ b/drivers/thermal/broadcom/ns-thermal.c
68 @@ -0,0 +1,105 @@
69 +/*
70 + * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
71 + *
72 + * This program is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License version 2 as
74 + * published by the Free Software Foundation.
75 + */
76 +
77 +#include <linux/module.h>
78 +#include <linux/of_address.h>
79 +#include <linux/platform_device.h>
80 +#include <linux/thermal.h>
81 +
82 +#define PVTMON_CONTROL0 0x00
83 +#define PVTMON_CONTROL0_SEL_MASK 0x0000000e
84 +#define PVTMON_CONTROL0_SEL_TEMP_MONITOR 0x00000000
85 +#define PVTMON_CONTROL0_SEL_TEST_MODE 0x0000000e
86 +#define PVTMON_STATUS 0x08
87 +
88 +struct ns_thermal {
89 + struct thermal_zone_device *tz;
90 + void __iomem *pvtmon;
91 +};
92 +
93 +static int ns_thermal_get_temp(void *data, int *temp)
94 +{
95 + struct ns_thermal *ns_thermal = data;
96 + int offset = thermal_zone_get_offset(ns_thermal->tz);
97 + int slope = thermal_zone_get_slope(ns_thermal->tz);
98 + u32 val;
99 +
100 + val = readl(ns_thermal->pvtmon + PVTMON_CONTROL0);
101 + if ((val & PVTMON_CONTROL0_SEL_MASK) != PVTMON_CONTROL0_SEL_TEMP_MONITOR) {
102 + /* Clear current mode selection */
103 + val &= ~PVTMON_CONTROL0_SEL_MASK;
104 +
105 + /* Set temp monitor mode (it's the default actually) */
106 + val |= PVTMON_CONTROL0_SEL_TEMP_MONITOR;
107 +
108 + writel(val, ns_thermal->pvtmon + PVTMON_CONTROL0);
109 + }
110 +
111 + val = readl(ns_thermal->pvtmon + PVTMON_STATUS);
112 + *temp = slope * val + offset;
113 +
114 + return 0;
115 +}
116 +
117 +static const struct thermal_zone_of_device_ops ns_thermal_ops = {
118 + .get_temp = ns_thermal_get_temp,
119 +};
120 +
121 +static int ns_thermal_probe(struct platform_device *pdev)
122 +{
123 + struct device *dev = &pdev->dev;
124 + struct ns_thermal *ns_thermal;
125 +
126 + ns_thermal = devm_kzalloc(dev, sizeof(*ns_thermal), GFP_KERNEL);
127 + if (!ns_thermal)
128 + return -ENOMEM;
129 +
130 + ns_thermal->pvtmon = of_iomap(dev_of_node(dev), 0);
131 + if (WARN_ON(!ns_thermal->pvtmon))
132 + return -ENOENT;
133 +
134 + ns_thermal->tz = devm_thermal_zone_of_sensor_register(dev, 0,
135 + ns_thermal,
136 + &ns_thermal_ops);
137 + if (IS_ERR(ns_thermal->tz)) {
138 + iounmap(ns_thermal->pvtmon);
139 + return PTR_ERR(ns_thermal->tz);
140 + }
141 +
142 + platform_set_drvdata(pdev, ns_thermal);
143 +
144 + return 0;
145 +}
146 +
147 +static int ns_thermal_remove(struct platform_device *pdev)
148 +{
149 + struct ns_thermal *ns_thermal = platform_get_drvdata(pdev);
150 +
151 + iounmap(ns_thermal->pvtmon);
152 +
153 + return 0;
154 +}
155 +
156 +static const struct of_device_id ns_thermal_of_match[] = {
157 + { .compatible = "brcm,ns-thermal", },
158 + {},
159 +};
160 +MODULE_DEVICE_TABLE(of, ns_thermal_of_match);
161 +
162 +static struct platform_driver ns_thermal_driver = {
163 + .probe = ns_thermal_probe,
164 + .remove = ns_thermal_remove,
165 + .driver = {
166 + .name = "ns-thermal",
167 + .of_match_table = ns_thermal_of_match,
168 + },
169 +};
170 +module_platform_driver(ns_thermal_driver);
171 +
172 +MODULE_DESCRIPTION("Northstar thermal driver");
173 +MODULE_LICENSE("GPL v2");