Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / thermal / broadcom / bcm2711_thermal.c
blob1ec57d9ecf5395eb557199204d52b4ea23302536
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Broadcom AVS RO thermal sensor driver
5 * based on brcmstb_thermal
7 * Copyright (C) 2020 Stefan Wahren
8 */
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_device.h>
20 #include <linux/regmap.h>
21 #include <linux/thermal.h>
23 #include "../thermal_hwmon.h"
25 #define AVS_RO_TEMP_STATUS 0x200
26 #define AVS_RO_TEMP_STATUS_VALID_MSK (BIT(16) | BIT(10))
27 #define AVS_RO_TEMP_STATUS_DATA_MSK GENMASK(9, 0)
29 struct bcm2711_thermal_priv {
30 struct regmap *regmap;
31 struct thermal_zone_device *thermal;
34 static int bcm2711_get_temp(void *data, int *temp)
36 struct bcm2711_thermal_priv *priv = data;
37 int slope = thermal_zone_get_slope(priv->thermal);
38 int offset = thermal_zone_get_offset(priv->thermal);
39 u32 val;
40 int ret;
41 long t;
43 ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
44 if (ret)
45 return ret;
47 if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
48 return -EIO;
50 val &= AVS_RO_TEMP_STATUS_DATA_MSK;
52 /* Convert a HW code to a temperature reading (millidegree celsius) */
53 t = slope * val + offset;
55 *temp = t < 0 ? 0 : t;
57 return 0;
60 static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
61 .get_temp = bcm2711_get_temp,
64 static const struct of_device_id bcm2711_thermal_id_table[] = {
65 { .compatible = "brcm,bcm2711-thermal" },
66 {},
68 MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
70 static int bcm2711_thermal_probe(struct platform_device *pdev)
72 struct thermal_zone_device *thermal;
73 struct bcm2711_thermal_priv *priv;
74 struct device *dev = &pdev->dev;
75 struct device_node *parent;
76 struct regmap *regmap;
77 int ret;
79 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
80 if (!priv)
81 return -ENOMEM;
83 /* get regmap from syscon node */
84 parent = of_get_parent(dev->of_node); /* parent should be syscon node */
85 regmap = syscon_node_to_regmap(parent);
86 of_node_put(parent);
87 if (IS_ERR(regmap)) {
88 ret = PTR_ERR(regmap);
89 dev_err(dev, "failed to get regmap: %d\n", ret);
90 return ret;
92 priv->regmap = regmap;
94 thermal = devm_thermal_zone_of_sensor_register(dev, 0, priv,
95 &bcm2711_thermal_of_ops);
96 if (IS_ERR(thermal)) {
97 ret = PTR_ERR(thermal);
98 dev_err(dev, "could not register sensor: %d\n", ret);
99 return ret;
102 priv->thermal = thermal;
104 thermal->tzp->no_hwmon = false;
105 return thermal_add_hwmon_sysfs(thermal);
108 static struct platform_driver bcm2711_thermal_driver = {
109 .probe = bcm2711_thermal_probe,
110 .driver = {
111 .name = "bcm2711_thermal",
112 .of_match_table = bcm2711_thermal_id_table,
115 module_platform_driver(bcm2711_thermal_driver);
117 MODULE_LICENSE("GPL");
118 MODULE_AUTHOR("Stefan Wahren");
119 MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");