1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Broadcom AVS RO thermal sensor driver
4  *
5  * based on brcmstb_thermal
6  *
7  * Copyright (C) 2020 Stefan Wahren
8  */
9 
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>
22 
23 #include "../thermal_hwmon.h"
24 
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)
28 
29 struct bcm2711_thermal_priv {
30 	struct regmap *regmap;
31 	struct thermal_zone_device *thermal;
32 };
33 
34 static int bcm2711_get_temp(void *data, int *temp)
35 {
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;
42 
43 	ret = regmap_read(priv->regmap, AVS_RO_TEMP_STATUS, &val);
44 	if (ret)
45 		return ret;
46 
47 	if (!(val & AVS_RO_TEMP_STATUS_VALID_MSK))
48 		return -EIO;
49 
50 	val &= AVS_RO_TEMP_STATUS_DATA_MSK;
51 
52 	/* Convert a HW code to a temperature reading (millidegree celsius) */
53 	t = slope * val + offset;
54 
55 	*temp = t < 0 ? 0 : t;
56 
57 	return 0;
58 }
59 
60 static const struct thermal_zone_of_device_ops bcm2711_thermal_of_ops = {
61 	.get_temp	= bcm2711_get_temp,
62 };
63 
64 static const struct of_device_id bcm2711_thermal_id_table[] = {
65 	{ .compatible = "brcm,bcm2711-thermal" },
66 	{},
67 };
68 MODULE_DEVICE_TABLE(of, bcm2711_thermal_id_table);
69 
70 static int bcm2711_thermal_probe(struct platform_device *pdev)
71 {
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;
78 
79 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
80 	if (!priv)
81 		return -ENOMEM;
82 
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;
91 	}
92 	priv->regmap = regmap;
93 
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;
100 	}
101 
102 	priv->thermal = thermal;
103 
104 	thermal->tzp->no_hwmon = false;
105 	ret = thermal_add_hwmon_sysfs(thermal);
106 	if (ret)
107 		return ret;
108 
109 	return 0;
110 }
111 
112 static struct platform_driver bcm2711_thermal_driver = {
113 	.probe = bcm2711_thermal_probe,
114 	.driver = {
115 		.name = "bcm2711_thermal",
116 		.of_match_table = bcm2711_thermal_id_table,
117 	},
118 };
119 module_platform_driver(bcm2711_thermal_driver);
120 
121 MODULE_LICENSE("GPL");
122 MODULE_AUTHOR("Stefan Wahren");
123 MODULE_DESCRIPTION("Broadcom AVS RO thermal sensor driver");
124