0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/bitops.h>
0011 #include <linux/clk.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/io.h>
0015 #include <linux/kernel.h>
0016 #include <linux/mfd/syscon.h>
0017 #include <linux/module.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/of_device.h>
0020 #include <linux/regmap.h>
0021 #include <linux/thermal.h>
0022
0023 #include "../thermal_hwmon.h"
0024
0025 #define AVS_RO_TEMP_STATUS 0x200
0026 #define AVS_RO_TEMP_STATUS_VALID_MSK (BIT(16) | BIT(10))
0027 #define AVS_RO_TEMP_STATUS_DATA_MSK GENMASK(9, 0)
0028
0029 struct bcm2711_thermal_priv {
0030 struct regmap *regmap;
0031 struct thermal_zone_device *thermal;
0032 };
0033
0034 static int bcm2711_get_temp(void *data, int *temp)
0035 {
0036 struct bcm2711_thermal_priv *priv = data;
0037 int slope = thermal_zone_get_slope(priv->thermal);
0038 int offset = thermal_zone_get_offset(priv->thermal);
0039 u32 val;
0040 int ret;
0041
0042 ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
0043 if (ret)
0044 return ret;
0045
0046 if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
0047 return -EIO;
0048
0049 val &= AVS_RO_TEMP_STATUS_DATA_MSK;
0050
0051
0052 *temp = slope * val + offset;
0053
0054 return 0;
0055 }
0056
0057 static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
0058 .get_temp = bcm2711_get_temp,
0059 };
0060
0061 static const struct of_device_id bcm2711_thermal_id_table[] = {
0062 { .compatible = "brcm,bcm2711-thermal" },
0063 {},
0064 };
0065 MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
0066
0067 static int bcm2711_thermal_probe(struct platform_device *pdev)
0068 {
0069 struct thermal_zone_device *thermal;
0070 struct bcm2711_thermal_priv *priv;
0071 struct device *dev = &pdev->dev;
0072 struct device_node *parent;
0073 struct regmap *regmap;
0074 int ret;
0075
0076 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0077 if (!priv)
0078 return -ENOMEM;
0079
0080
0081 parent = of_get_parent(dev->of_node);
0082 regmap = syscon_node_to_regmap(parent);
0083 of_node_put(parent);
0084 if (IS_ERR(regmap)) {
0085 ret = PTR_ERR(regmap);
0086 dev_err(dev, "failed to get regmap: %d\n", ret);
0087 return ret;
0088 }
0089 priv->regmap = regmap;
0090
0091 thermal = devm_thermal_zone_of_sensor_register(dev, 0, priv,
0092 &bcm2711_thermal_of_ops);
0093 if (IS_ERR(thermal)) {
0094 ret = PTR_ERR(thermal);
0095 dev_err(dev, "could not register sensor: %d\n", ret);
0096 return ret;
0097 }
0098
0099 priv->thermal = thermal;
0100
0101 thermal->tzp->no_hwmon = false;
0102 return thermal_add_hwmon_sysfs(thermal);
0103 }
0104
0105 static struct platform_driver bcm2711_thermal_driver = {
0106 .probe = bcm2711_thermal_probe,
0107 .driver = {
0108 .name = "bcm2711_thermal",
0109 .of_match_table = bcm2711_thermal_id_table,
0110 },
0111 };
0112 module_platform_driver(bcm2711_thermal_driver);
0113
0114 MODULE_LICENSE("GPL");
0115 MODULE_AUTHOR("Stefan Wahren");
0116 MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");