1 // SPDX-License-Identifier: GPL-2.0+
3 * Broadcom AVS RO thermal sensor driver
5 * based on brcmstb_thermal
7 * Copyright (C) 2020 Stefan Wahren
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/err.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);
42 ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
46 if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
49 val &= AVS_RO_TEMP_STATUS_DATA_MSK;
51 /* Convert a HW code to a temperature reading (millidegree celsius) */
52 *temp = slope * val + offset;
57 static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
58 .get_temp = bcm2711_get_temp,
61 static const struct of_device_id bcm2711_thermal_id_table[] = {
62 { .compatible = "brcm,bcm2711-thermal" },
65 MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
67 static int bcm2711_thermal_probe(struct platform_device *pdev)
69 struct thermal_zone_device *thermal;
70 struct bcm2711_thermal_priv *priv;
71 struct device *dev = &pdev->dev;
72 struct device_node *parent;
73 struct regmap *regmap;
76 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
80 /* get regmap from syscon node */
81 parent = of_get_parent(dev->of_node); /* parent should be syscon node */
82 regmap = syscon_node_to_regmap(parent);
85 ret = PTR_ERR(regmap);
86 dev_err(dev, "failed to get regmap: %d\n", ret);
89 priv->regmap = regmap;
91 thermal = devm_thermal_zone_of_sensor_register(dev, 0, priv,
92 &bcm2711_thermal_of_ops);
93 if (IS_ERR(thermal)) {
94 ret = PTR_ERR(thermal);
95 dev_err_probe(dev, ret, "could not register sensor: %d\n", ret);
99 priv->thermal = thermal;
101 thermal->tzp->no_hwmon = false;
102 return thermal_add_hwmon_sysfs(thermal);
105 static struct platform_driver bcm2711_thermal_driver = {
106 .probe = bcm2711_thermal_probe,
108 .name = "bcm2711_thermal",
109 .of_match_table = bcm2711_thermal_id_table,
112 module_platform_driver(bcm2711_thermal_driver);
114 MODULE_LICENSE("GPL");
115 MODULE_AUTHOR("Stefan Wahren");
116 MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");