1 From 84477b71c184345517625f5c3eccf26a29eac4df Mon Sep 17 00:00:00 2001
2 From: Stefan Wahren <stefan.wahren@i2se.com>
3 Date: Mon, 13 Jan 2020 19:56:16 +0100
4 Subject: [PATCH] thermal: Add BCM2711 thermal driver
6 Commit 59b781352dc4cb9ae27a8ddae0cda979d29d8af7 upstream.
8 This adds the thermal sensor driver for the Broadcom BCM2711 SoC,
9 which is placed on the Raspberry Pi 4. The driver only provides
10 SoC temperature reading so far.
12 Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
13 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
14 Reviewed-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
15 Tested-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
16 Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
17 Link: https://lore.kernel.org/r/1578941778-23321-3-git-send-email-stefan.wahren@i2se.com
18 Signed-off-by: Chen-Yu Tsai <wens@csie.org>
20 drivers/thermal/broadcom/Kconfig | 7 ++
21 drivers/thermal/broadcom/Makefile | 1 +
22 drivers/thermal/broadcom/bcm2711_thermal.c | 123 +++++++++++++++++++++
23 3 files changed, 131 insertions(+)
24 create mode 100644 drivers/thermal/broadcom/bcm2711_thermal.c
26 --- a/drivers/thermal/broadcom/Kconfig
27 +++ b/drivers/thermal/broadcom/Kconfig
29 # SPDX-License-Identifier: GPL-2.0-only
30 +config BCM2711_THERMAL
31 + tristate "Broadcom AVS RO thermal sensor driver"
32 + depends on ARCH_BCM2835 || COMPILE_TEST
33 + depends on THERMAL_OF && MFD_SYSCON
35 + Support for thermal sensors on Broadcom BCM2711 SoCs.
37 config BCM2835_THERMAL
38 tristate "Thermal sensors on bcm2835 SoC"
39 depends on ARCH_BCM2835 || COMPILE_TEST
40 --- a/drivers/thermal/broadcom/Makefile
41 +++ b/drivers/thermal/broadcom/Makefile
43 # SPDX-License-Identifier: GPL-2.0-only
44 +obj-$(CONFIG_BCM2711_THERMAL) += bcm2711_thermal.o
45 obj-$(CONFIG_BCM2835_THERMAL) += bcm2835_thermal.o
46 obj-$(CONFIG_BRCMSTB_THERMAL) += brcmstb_thermal.o
47 obj-$(CONFIG_BCM_NS_THERMAL) += ns-thermal.o
49 +++ b/drivers/thermal/broadcom/bcm2711_thermal.c
51 +// SPDX-License-Identifier: GPL-2.0+
53 + * Broadcom AVS RO thermal sensor driver
55 + * based on brcmstb_thermal
57 + * Copyright (C) 2020 Stefan Wahren
60 +#include <linux/bitops.h>
61 +#include <linux/clk.h>
62 +#include <linux/device.h>
63 +#include <linux/err.h>
64 +#include <linux/io.h>
65 +#include <linux/kernel.h>
66 +#include <linux/mfd/syscon.h>
67 +#include <linux/module.h>
68 +#include <linux/platform_device.h>
69 +#include <linux/of_device.h>
70 +#include <linux/regmap.h>
71 +#include <linux/thermal.h>
73 +#include "../thermal_hwmon.h"
75 +#define AVS_RO_TEMP_STATUS 0x200
76 +#define AVS_RO_TEMP_STATUS_VALID_MSK (BIT(16) | BIT(10))
77 +#define AVS_RO_TEMP_STATUS_DATA_MSK GENMASK(9, 0)
79 +struct bcm2711_thermal_priv {
80 + struct regmap *regmap;
81 + struct thermal_zone_device *thermal;
84 +static int bcm2711_get_temp(void *data, int *temp)
86 + struct bcm2711_thermal_priv *priv = data;
87 + int slope = thermal_zone_get_slope(priv->thermal);
88 + int offset = thermal_zone_get_offset(priv->thermal);
93 + ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
97 + if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
100 + val &= AVS_RO_TEMP_STATUS_DATA_MSK;
102 + /* Convert a HW code to a temperature reading (millidegree celsius) */
103 + t = slope * val + offset;
105 + *temp = t < 0 ? 0 : t;
110 +static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
111 + .get_temp = bcm2711_get_temp,
114 +static const struct of_device_id bcm2711_thermal_id_table[] = {
115 + { .compatible = "brcm,bcm2711-thermal" },
118 +MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
120 +static int bcm2711_thermal_probe(struct platform_device *pdev)
122 + struct thermal_zone_device *thermal;
123 + struct bcm2711_thermal_priv *priv;
124 + struct device *dev = &pdev->dev;
125 + struct device_node *parent;
126 + struct regmap *regmap;
129 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
133 + /* get regmap from syscon node */
134 + parent = of_get_parent(dev->of_node); /* parent should be syscon node */
135 + regmap = syscon_node_to_regmap(parent);
136 + of_node_put(parent);
137 + if (IS_ERR(regmap)) {
138 + ret = PTR_ERR(regmap);
139 + dev_err(dev, "failed to get regmap: %d\n", ret);
142 + priv->regmap = regmap;
144 + thermal = devm_thermal_zone_of_sensor_register(dev, 0, priv,
145 + &bcm2711_thermal_of_ops);
146 + if (IS_ERR(thermal)) {
147 + ret = PTR_ERR(thermal);
148 + dev_err(dev, "could not register sensor: %d\n", ret);
152 + priv->thermal = thermal;
154 + thermal->tzp->no_hwmon = false;
155 + ret = thermal_add_hwmon_sysfs(thermal);
162 +static struct platform_driver bcm2711_thermal_driver = {
163 + .probe = bcm2711_thermal_probe,
165 + .name = "bcm2711_thermal",
166 + .of_match_table = bcm2711_thermal_id_table,
169 +module_platform_driver(bcm2711_thermal_driver);
171 +MODULE_LICENSE("GPL");
172 +MODULE_AUTHOR("Stefan Wahren");
173 +MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");