kernel: bump 5.4 to 5.4.48
[oweals/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0758-thermal-Add-BCM2711-thermal-driver.patch
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
5
6 Commit 59b781352dc4cb9ae27a8ddae0cda979d29d8af7 upstream.
7
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.
11
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>
19 ---
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
25
26 --- a/drivers/thermal/broadcom/Kconfig
27 +++ b/drivers/thermal/broadcom/Kconfig
28 @@ -1,4 +1,11 @@
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
34 +       help
35 +         Support for thermal sensors on Broadcom BCM2711 SoCs.
36 +
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
42 @@ -1,4 +1,5 @@
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
48 --- /dev/null
49 +++ b/drivers/thermal/broadcom/bcm2711_thermal.c
50 @@ -0,0 +1,123 @@
51 +// SPDX-License-Identifier: GPL-2.0+
52 +/*
53 + * Broadcom AVS RO thermal sensor driver
54 + *
55 + * based on brcmstb_thermal
56 + *
57 + * Copyright (C) 2020 Stefan Wahren
58 + */
59 +
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>
72 +
73 +#include "../thermal_hwmon.h"
74 +
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)
78 +
79 +struct bcm2711_thermal_priv {
80 +       struct regmap *regmap;
81 +       struct thermal_zone_device *thermal;
82 +};
83 +
84 +static int bcm2711_get_temp(void *data, int *temp)
85 +{
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);
89 +       u32 val;
90 +       int ret;
91 +       long t;
92 +
93 +       ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
94 +       if (ret)
95 +               return ret;
96 +
97 +       if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
98 +               return -EIO;
99 +
100 +       val &= AVS_RO_TEMP_STATUS_DATA_MSK;
101 +
102 +       /* Convert a HW code to a temperature reading (millidegree celsius) */
103 +       t = slope * val + offset;
104 +
105 +       *temp = t < 0 ? 0 : t;
106 +
107 +       return 0;
108 +}
109 +
110 +static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
111 +       .get_temp       = bcm2711_get_temp,
112 +};
113 +
114 +static const struct of_device_id bcm2711_thermal_id_table[] = {
115 +       { .compatible = "brcm,bcm2711-thermal" },
116 +       {},
117 +};
118 +MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
119 +
120 +static int bcm2711_thermal_probe(struct platform_device *pdev)
121 +{
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;
127 +       int ret;
128 +
129 +       priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
130 +       if (!priv)
131 +               return -ENOMEM;
132 +
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);
140 +               return ret;
141 +       }
142 +       priv->regmap = regmap;
143 +
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);
149 +               return ret;
150 +       }
151 +
152 +       priv->thermal = thermal;
153 +
154 +       thermal->tzp->no_hwmon = false;
155 +       ret = thermal_add_hwmon_sysfs(thermal);
156 +       if (ret)
157 +               return ret;
158 +
159 +       return 0;
160 +}
161 +
162 +static struct platform_driver bcm2711_thermal_driver = {
163 +       .probe = bcm2711_thermal_probe,
164 +       .driver = {
165 +               .name = "bcm2711_thermal",
166 +               .of_match_table = bcm2711_thermal_id_table,
167 +       },
168 +};
169 +module_platform_driver(bcm2711_thermal_driver);
170 +
171 +MODULE_LICENSE("GPL");
172 +MODULE_AUTHOR("Stefan Wahren");
173 +MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");