xref: /OK3568_Linux_fs/kernel/drivers/thermal/tango_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/io.h>
3*4882a593Smuzhiyun #include <linux/delay.h>
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/thermal.h>
6*4882a593Smuzhiyun #include <linux/platform_device.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * According to a data sheet draft, "this temperature sensor uses a bandgap
10*4882a593Smuzhiyun  * type of circuit to compare a voltage which has a negative temperature
11*4882a593Smuzhiyun  * coefficient with a voltage that is proportional to absolute temperature.
12*4882a593Smuzhiyun  * A resistor bank allows 41 different temperature thresholds to be selected
13*4882a593Smuzhiyun  * and the logic output will then indicate whether the actual die temperature
14*4882a593Smuzhiyun  * lies above or below the selected threshold."
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #define TEMPSI_CMD	0
18*4882a593Smuzhiyun #define TEMPSI_RES	4
19*4882a593Smuzhiyun #define TEMPSI_CFG	8
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define CMD_OFF		0
22*4882a593Smuzhiyun #define CMD_ON		1
23*4882a593Smuzhiyun #define CMD_READ	2
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define IDX_MIN		15
26*4882a593Smuzhiyun #define IDX_MAX		40
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun struct tango_thermal_priv {
29*4882a593Smuzhiyun 	void __iomem *base;
30*4882a593Smuzhiyun 	int thresh_idx;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
temp_above_thresh(void __iomem * base,int thresh_idx)33*4882a593Smuzhiyun static bool temp_above_thresh(void __iomem *base, int thresh_idx)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
36*4882a593Smuzhiyun 	usleep_range(10, 20);
37*4882a593Smuzhiyun 	writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	return readl(base + TEMPSI_RES);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
tango_get_temp(void * arg,int * res)42*4882a593Smuzhiyun static int tango_get_temp(void *arg, int *res)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct tango_thermal_priv *priv = arg;
45*4882a593Smuzhiyun 	int idx = priv->thresh_idx;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (temp_above_thresh(priv->base, idx)) {
48*4882a593Smuzhiyun 		/* Search upward by incrementing thresh_idx */
49*4882a593Smuzhiyun 		while (idx < IDX_MAX && temp_above_thresh(priv->base, ++idx))
50*4882a593Smuzhiyun 			cpu_relax();
51*4882a593Smuzhiyun 		idx = idx - 1; /* always return lower bound */
52*4882a593Smuzhiyun 	} else {
53*4882a593Smuzhiyun 		/* Search downward by decrementing thresh_idx */
54*4882a593Smuzhiyun 		while (idx > IDX_MIN && !temp_above_thresh(priv->base, --idx))
55*4882a593Smuzhiyun 			cpu_relax();
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	*res = (idx * 9 / 2 - 38) * 1000; /* millidegrees Celsius */
59*4882a593Smuzhiyun 	priv->thresh_idx = idx;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static const struct thermal_zone_of_device_ops ops = {
65*4882a593Smuzhiyun 	.get_temp	= tango_get_temp,
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun 
tango_thermal_init(struct tango_thermal_priv * priv)68*4882a593Smuzhiyun static void tango_thermal_init(struct tango_thermal_priv *priv)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	writel(0, priv->base + TEMPSI_CFG);
71*4882a593Smuzhiyun 	writel(CMD_ON, priv->base + TEMPSI_CMD);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
tango_thermal_probe(struct platform_device * pdev)74*4882a593Smuzhiyun static int tango_thermal_probe(struct platform_device *pdev)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	struct resource *res;
77*4882a593Smuzhiyun 	struct tango_thermal_priv *priv;
78*4882a593Smuzhiyun 	struct thermal_zone_device *tzdev;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
81*4882a593Smuzhiyun 	if (!priv)
82*4882a593Smuzhiyun 		return -ENOMEM;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85*4882a593Smuzhiyun 	priv->base = devm_ioremap_resource(&pdev->dev, res);
86*4882a593Smuzhiyun 	if (IS_ERR(priv->base))
87*4882a593Smuzhiyun 		return PTR_ERR(priv->base);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	platform_set_drvdata(pdev, priv);
90*4882a593Smuzhiyun 	priv->thresh_idx = IDX_MIN;
91*4882a593Smuzhiyun 	tango_thermal_init(priv);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &ops);
94*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(tzdev);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
tango_thermal_resume(struct device * dev)97*4882a593Smuzhiyun static int __maybe_unused tango_thermal_resume(struct device *dev)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	tango_thermal_init(dev_get_drvdata(dev));
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(tango_thermal_pm, NULL, tango_thermal_resume);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun static const struct of_device_id tango_sensor_ids[] = {
106*4882a593Smuzhiyun 	{
107*4882a593Smuzhiyun 		.compatible = "sigma,smp8758-thermal",
108*4882a593Smuzhiyun 	},
109*4882a593Smuzhiyun 	{ /* sentinel */ }
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tango_sensor_ids);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static struct platform_driver tango_thermal_driver = {
114*4882a593Smuzhiyun 	.probe	= tango_thermal_probe,
115*4882a593Smuzhiyun 	.driver	= {
116*4882a593Smuzhiyun 		.name		= "tango-thermal",
117*4882a593Smuzhiyun 		.of_match_table	= tango_sensor_ids,
118*4882a593Smuzhiyun 		.pm		= &tango_thermal_pm,
119*4882a593Smuzhiyun 	},
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun module_platform_driver(tango_thermal_driver);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun MODULE_LICENSE("GPL");
125*4882a593Smuzhiyun MODULE_AUTHOR("Sigma Designs");
126*4882a593Smuzhiyun MODULE_DESCRIPTION("Tango temperature sensor");
127