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